av sync after seek commands
[fbff.git] / ffs.c
blob0033a0646111060a59e839310701048c2f6fe21c
1 #include <fcntl.h>
2 #include <stdlib.h>
3 #include <stdint.h>
4 #include <unistd.h>
5 #include <sys/time.h>
6 #include <libavformat/avformat.h>
7 #include <libavcodec/avcodec.h>
8 #include <libswscale/swscale.h>
9 #include "ffs.h"
11 /* ffmpeg stream */
12 struct ffs {
13 AVCodecContext *cc;
14 AVFormatContext *fc;
15 AVPacket pkt;
16 int si; /* stream index */
17 long ts; /* frame timestamp (ms) */
18 long seq; /* current position in this stream */
19 long seq_all; /* seen packets after ffs_seek() */
20 long seq_cur; /* decoded packet after ffs_seek() */
22 /* decoding video frames */
23 struct SwsContext *swsc;
24 AVFrame *dst;
25 AVFrame *tmp;
28 struct ffs *ffs_alloc(char *path, int flags)
30 struct ffs *ffs;
31 int type = flags & FFS_VIDEO ? AVMEDIA_TYPE_VIDEO : AVMEDIA_TYPE_AUDIO;
32 int idx = (flags & FFS_STRIDX) - 1;
33 ffs = malloc(sizeof(*ffs));
34 memset(ffs, 0, sizeof(*ffs));
35 ffs->si = -1;
36 if (avformat_open_input(&ffs->fc, path, NULL, NULL))
37 goto failed;
38 if (av_find_stream_info(ffs->fc) < 0)
39 goto failed;
40 ffs->si = av_find_best_stream(ffs->fc, type, idx, -1, NULL, 0);
41 if (ffs->si < 0)
42 goto failed;
43 ffs->cc = ffs->fc->streams[ffs->si]->codec;
44 avcodec_open(ffs->cc, avcodec_find_decoder(ffs->cc->codec_id));
45 return ffs;
46 failed:
47 ffs_free(ffs);
48 return NULL;
51 void ffs_free(struct ffs *ffs)
53 if (ffs->swsc)
54 sws_freeContext(ffs->swsc);
55 if (ffs->dst)
56 av_free(ffs->dst);
57 if (ffs->tmp)
58 av_free(ffs->tmp);
59 if (ffs->cc)
60 avcodec_close(ffs->cc);
61 if (ffs->fc)
62 av_close_input_file(ffs->fc);
63 free(ffs);
66 static AVPacket *ffs_pkt(struct ffs *ffs)
68 AVPacket *pkt = &ffs->pkt;
69 while (av_read_frame(ffs->fc, pkt) >= 0) {
70 ffs->seq_all++;
71 if (pkt->stream_index == ffs->si) {
72 ffs->seq_cur++;
73 ffs->seq++;
74 return pkt;
76 av_free_packet(pkt);
78 return NULL;
81 static long ts_ms(void)
83 struct timeval tv;
84 gettimeofday(&tv, NULL);
85 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
88 static int wait(long ts, int vdelay)
90 int nts = ts_ms();
91 if (nts > ts && ts + vdelay > nts) {
92 usleep((ts + vdelay - nts) * 1000);
93 return 0;
95 return 1;
98 #define MAX(a, b) ((a) < (b) ? (b) : (a))
100 void ffs_wait(struct ffs *ffs)
102 AVRational *r = &ffs->fc->streams[ffs->si]->r_frame_rate;
103 int vdelay = 1000 * r->den / r->num;
104 if (!wait(ffs->ts, MAX(vdelay, 20)))
105 ffs->ts += MAX(vdelay, 20);
106 else
107 ffs->ts = ts_ms(); /* out of sync */
110 /* audio/video frame offset difference */
111 int ffs_avdiff(struct ffs *ffs, struct ffs *affs)
113 return affs->seq_all - ffs->seq_all;
116 long ffs_pos(struct ffs *ffs, int diff)
118 return (ffs->si << 28) | (ffs->seq + diff);
121 void ffs_seek(struct ffs *ffs, long pos, int perframe)
123 long idx = pos >> 28;
124 long seq = pos & 0x00ffffff;
125 av_seek_frame(ffs->fc, idx, seq * perframe,
126 perframe == 1 ? AVSEEK_FLAG_FRAME : 0);
127 ffs->seq = seq;
128 ffs->seq_all = 0;
129 ffs->seq_cur = 0;
130 ffs->ts = 0;
133 void ffs_vinfo(struct ffs *ffs, int *w, int *h)
135 *h = ffs->cc->height;
136 *w = ffs->cc->width;
139 void ffs_ainfo(struct ffs *ffs, int *rate, int *bps, int *ch)
141 *rate = ffs->cc->sample_rate;
142 *ch = ffs->cc->channels;
143 *bps = 16;
146 int ffs_vdec(struct ffs *ffs, void **buf)
148 AVCodecContext *vcc = ffs->cc;
149 AVPacket *pkt = ffs_pkt(ffs);
150 int fine = 0;
151 if (!pkt)
152 return -1;
153 avcodec_decode_video2(vcc, ffs->tmp, &fine, pkt);
154 av_free_packet(pkt);
155 if (fine && buf) {
156 sws_scale(ffs->swsc, (void *) ffs->tmp->data, ffs->tmp->linesize,
157 0, vcc->height, ffs->dst->data, ffs->dst->linesize);
158 *buf = (void *) ffs->dst->data[0];
159 return ffs->dst->linesize[0];
161 return 0;
164 int ffs_adec(struct ffs *ffs, void *buf, int blen)
166 int rdec = 0;
167 AVPacket tmppkt;
168 AVPacket *pkt = ffs_pkt(ffs);
169 if (!pkt)
170 return -1;
171 tmppkt.size = pkt->size;
172 tmppkt.data = pkt->data;
173 while (tmppkt.size > 0) {
174 int size = blen - rdec;
175 int len = avcodec_decode_audio3(ffs->cc, (int16_t *) (buf + rdec),
176 &size, &tmppkt);
177 if (len < 0)
178 break;
179 tmppkt.size -= len;
180 tmppkt.data += len;
181 if (size > 0)
182 rdec += size;
184 av_free_packet(pkt);
185 return rdec;
188 static int fbm2pixfmt(int fbm)
190 switch (fbm & 0x0fff) {
191 case 0x888:
192 return PIX_FMT_RGB32;
193 case 0x565:
194 return PIX_FMT_RGB565;
195 case 0x233:
196 return PIX_FMT_RGB8;
197 default:
198 fprintf(stderr, "ffs: unknown fb_mode()\n");
199 return PIX_FMT_RGB32;
203 void ffs_vsetup(struct ffs *ffs, float zoom, int fbm)
205 int h = ffs->cc->height;
206 int w = ffs->cc->width;
207 int fmt = ffs->cc->pix_fmt;
208 int pixfmt = fbm2pixfmt(fbm);
209 uint8_t *buf = NULL;
210 int n;
211 ffs->swsc = sws_getContext(w, h, fmt, w * zoom, h * zoom,
212 pixfmt, SWS_FAST_BILINEAR | SWS_CPU_CAPS_MMX2,
213 NULL, NULL, NULL);
214 ffs->dst = avcodec_alloc_frame();
215 ffs->tmp = avcodec_alloc_frame();
216 n = avpicture_get_size(pixfmt, w * zoom, h * zoom);
217 buf = av_malloc(n * sizeof(uint8_t));
218 avpicture_fill((AVPicture *) ffs->dst, buf, pixfmt, w * zoom, h * zoom);
221 void ffs_globinit(void)
223 av_register_all();