2 * Copyright (c) 2008 Luca Abeni
4 * This is free software; see GPL.txt
7 #include <libavformat/avformat.h>
10 static uint64_t time_convert(uint64_t time, AVStream *st)
12 if (st->start_time != AV_NOPTS_VALUE) {
14 time -= st->start_time;
16 fprintf(stderr, "%Ld\t%Ld\n", st->start_time, av_rescale_q(st->start_time,
17 st->time_base, AV_TIME_BASE_Q));
20 time = av_rescale_q(time, st->time_base, AV_TIME_BASE_Q);
25 static void time_base_convert(AVPacket *pkt, AVStream *st)
27 if (pkt->pts != AV_NOPTS_VALUE) {
28 pkt->pts = time_convert(pkt->pts, st);
30 if (pkt->dts != AV_NOPTS_VALUE) {
31 pkt->dts = time_convert(pkt->dts, st);
33 if (pkt->duration != 0) {
34 pkt->duration = av_rescale_q(pkt->duration, st->time_base, AV_TIME_BASE_Q);
38 AVFormatContext *open_input_stream(const char *fname, int w, int h, int fps, const char *impform)
42 AVFormatParameters param;
45 memset(¶m, 0, sizeof(AVFormatParameters));
46 /* FIXME: Set these!!! */
49 param.pix_fmt = PIX_FMT_YUV420P;
50 param.time_base.den = fps;
51 param.time_base.num = 1;
53 impform = "video4linux2";
54 fmt = av_find_input_format(impform);
55 res = av_open_input_file(&s, fname, fmt, 0, ¶m);
57 fprintf(stderr, "Error opening %s: %d\n", fname, res);
62 res = av_find_stream_info(s);
64 fprintf(stderr, "Cannot find codec parameters for %s\n", fname);
69 dump_format(s, 0, fname, 0);
74 void close_input_stream(AVFormatContext *s)
76 av_close_input_file(s);
79 AVPacket *read_input_packet(AVFormatContext *s)
85 res = av_read_frame(s, &pkt);
90 st = s->streams[pkt.stream_index];
91 if (pkt.dts == AV_NOPTS_VALUE) {
92 fprintf(stderr, "WTF??? Unknown DTS???\n");
97 time_base_convert(&pkt, st);