]> rtime.felk.cvut.cz Git - frescor/streamer.git/blob - input.c
Proper CPU contracts in streamer
[frescor/streamer.git] / input.c
1 /*
2  *  Copyright (c) 2008 Luca Abeni
3  *
4  *  This is free software; see GPL.txt
5  */
6 #include <string.h>
7 #include <libavformat/avformat.h>
8 #include "input.h"
9
10 static uint64_t time_convert(uint64_t time, AVStream *st)
11 {
12     if (st->start_time != AV_NOPTS_VALUE) {
13 #if 1
14         time -= st->start_time;
15 #else
16 fprintf(stderr, "%Ld\t%Ld\n", st->start_time, av_rescale_q(st->start_time,
17                                   st->time_base, AV_TIME_BASE_Q));
18 #endif
19     }
20     time = av_rescale_q(time, st->time_base, AV_TIME_BASE_Q);
21
22     return time;
23 }
24
25 static void time_base_convert(AVPacket *pkt, AVStream *st)
26 {
27   if (pkt->pts != AV_NOPTS_VALUE) {
28     pkt->pts = time_convert(pkt->pts, st);
29   }
30   if (pkt->dts != AV_NOPTS_VALUE) {
31     pkt->dts = time_convert(pkt->dts, st);
32   }
33   if (pkt->duration != 0) {
34     pkt->duration = av_rescale_q(pkt->duration, st->time_base, AV_TIME_BASE_Q);
35   }
36 }
37
38 AVFormatContext *open_input_stream(const char *fname, int w, int h, int fps, const char *impform)
39 {
40     AVFormatContext *s;
41     AVInputFormat *fmt;
42     AVFormatParameters param;
43     int res;
44
45     memset(&param, 0, sizeof(AVFormatParameters));
46     /* FIXME: Set these!!! */
47     param.width  = w;
48     param.height = h;
49     param.pix_fmt = PIX_FMT_YUV420P;
50     param.time_base.den = fps;
51     param.time_base.num = 1;
52     if(impform == NULL)
53       impform = "video4linux2";
54     fmt = av_find_input_format(impform);
55     res = av_open_input_file(&s, fname, fmt, 0, &param);
56     if (res < 0) {
57         fprintf(stderr, "Error opening %s: %d\n", fname, res);
58
59         return NULL;
60     }
61
62     res = av_find_stream_info(s);
63     if (res < 0) {
64         fprintf(stderr, "Cannot find codec parameters for %s\n", fname);
65
66         return NULL;
67     }
68
69     dump_format(s, 0, fname, 0);
70
71     return s;
72 }
73
74 void close_input_stream(AVFormatContext *s)
75 {
76     av_close_input_file(s);
77 }
78
79 AVPacket *read_input_packet(AVFormatContext *s)
80 {
81     static AVPacket pkt;
82     AVStream *st;
83     int res;
84
85     res = av_read_frame(s, &pkt);
86     if (res < 0) {
87         return NULL;
88     }
89
90     st = s->streams[pkt.stream_index];
91     if (pkt.dts == AV_NOPTS_VALUE) {
92         fprintf(stderr, "WTF??? Unknown DTS???\n");
93
94         return NULL;
95     }
96
97     time_base_convert(&pkt, st);
98
99     return &pkt;
100 }