move ffmpeg dependent parts to ffs
[fbff.git] / ffs.c
blobc65260fe80445ce3c1673de331e97e9423101c6d
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; /* frame number among packets of this stream */
19 long allseq; /* frame number among all packets */
21 /* decoding video frames */
22 struct SwsContext *swsc;
23 AVFrame *dst;
24 AVFrame *tmp;
27 struct ffs *ffs_alloc(char *path, int video)
29 struct ffs *ffs;
30 int codec_type = video ? CODEC_TYPE_VIDEO : CODEC_TYPE_AUDIO;
31 int i;
32 ffs = malloc(sizeof(*ffs));
33 memset(ffs, 0, sizeof(*ffs));
34 ffs->si = -1;
35 if (av_open_input_file(&ffs->fc, path, NULL, 0, NULL))
36 goto failed;
37 if (av_find_stream_info(ffs->fc) < 0)
38 goto failed;
39 for (i = 0; i < ffs->fc->nb_streams; i++)
40 if (ffs->fc->streams[i]->codec->codec_type == codec_type)
41 ffs->si = i;
42 if (ffs->si == -1)
43 goto failed;
44 ffs->cc = ffs->fc->streams[ffs->si]->codec;
45 avcodec_open(ffs->cc, avcodec_find_decoder(ffs->cc->codec_id));
46 return ffs;
47 failed:
48 ffs_free(ffs);
49 return NULL;
52 void ffs_free(struct ffs *ffs)
54 if (ffs->swsc)
55 sws_freeContext(ffs->swsc);
56 if (ffs->dst)
57 av_free(ffs->dst);
58 if (ffs->tmp)
59 av_free(ffs->tmp);
60 if (ffs->cc)
61 avcodec_close(ffs->cc);
62 if (ffs->fc)
63 av_close_input_file(ffs->fc);
64 free(ffs);
67 static AVPacket *ffs_pkt(struct ffs *ffs)
69 AVPacket *pkt = &ffs->pkt;
70 while (av_read_frame(ffs->fc, pkt) >= 0) {
71 ffs->allseq++;
72 if (pkt->stream_index == ffs->si) {
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 void wait(long ts, int vdelay)
90 int nts = ts_ms();
91 if (ts + vdelay > nts)
92 usleep((ts + vdelay - nts) * 1000);
95 void ffs_wait(struct ffs *ffs)
97 long ts = ts_ms();
98 if (ts && ffs->ts) {
99 AVRational *r = &ffs->fc->streams[ffs->si]->time_base;
100 int vdelay = 1000 * r->num / r->den;
101 wait(ffs->ts, vdelay);
103 ffs->ts = ts_ms();
106 long ffs_pos(struct ffs *ffs, int diff)
108 return (ffs->si << 28) | (ffs->seq + diff);
111 void ffs_seek(struct ffs *ffs, long pos, int perframe)
113 long idx = pos >> 28;
114 long seq = pos & 0x0fffffff;
115 av_seek_frame(ffs->fc, idx, seq * perframe,
116 perframe == 1 ? AVSEEK_FLAG_FRAME : 0);
117 ffs->seq = seq;
118 ffs->allseq = 0;
119 ffs->ts = 0;
122 long ffs_seq(struct ffs *ffs, int all)
124 return all ? ffs->allseq : ffs->seq;
127 void ffs_vinfo(struct ffs *ffs, int *w, int *h)
129 *h = ffs->cc->height;
130 *w = ffs->cc->width;
133 void ffs_ainfo(struct ffs *ffs, int *rate, int *bps, int *ch)
135 *rate = ffs->cc->sample_rate;
136 *ch = ffs->cc->channels;
137 *bps = 16;
140 int ffs_vdec(struct ffs *ffs, char **buf)
142 AVCodecContext *vcc = ffs->cc;
143 AVPacket *pkt = ffs_pkt(ffs);
144 int fine = 0;
145 if (!pkt)
146 return -1;
147 avcodec_decode_video2(vcc, ffs->tmp, &fine, pkt);
148 av_free_packet(pkt);
149 if (fine && buf) {
150 sws_scale(ffs->swsc, ffs->tmp->data, ffs->tmp->linesize,
151 0, vcc->height, ffs->dst->data, ffs->dst->linesize);
152 *buf = (void *) ffs->dst->data[0];
153 return ffs->dst->linesize[0];
155 return 0;
158 int ffs_adec(struct ffs *ffs, char *buf, int blen)
160 int rdec = 0;
161 AVPacket tmppkt;
162 AVPacket *pkt = ffs_pkt(ffs);
163 if (!pkt)
164 return -1;
165 tmppkt.size = pkt->size;
166 tmppkt.data = pkt->data;
167 while (tmppkt.size > 0) {
168 int size = blen - rdec;
169 int len = avcodec_decode_audio3(ffs->cc, (int16_t *) (buf + rdec),
170 &size, &tmppkt);
171 if (len < 0)
172 break;
173 tmppkt.size -= len;
174 tmppkt.data += len;
175 if (size > 0)
176 rdec += size;
178 av_free_packet(pkt);
179 return rdec;
182 void ffs_vsetup(struct ffs *ffs, float zoom, int pixfmt)
184 int h = ffs->cc->height;
185 int w = ffs->cc->width;
186 int fmt = ffs->cc->pix_fmt;
187 uint8_t *buf = NULL;
188 int n;
189 ffs->swsc = sws_getContext(w, h, fmt, w * zoom, h * zoom,
190 pixfmt, SWS_FAST_BILINEAR | SWS_CPU_CAPS_MMX2,
191 NULL, NULL, NULL);
192 ffs->dst = avcodec_alloc_frame();
193 ffs->tmp = avcodec_alloc_frame();
194 n = avpicture_get_size(pixfmt, w * zoom, h * zoom);
195 buf = av_malloc(n * sizeof(uint8_t));
196 avpicture_fill((AVPicture *) ffs->dst, buf, pixfmt, w * zoom, h * zoom);
199 void ffs_globinit(void)
201 av_register_all();