draw: variable shifts in fb_val(); update from fbpad
[fbff.git] / ffs.c
blob6152efd369790da63a892310f183d4186c18bfb2
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 video)
30 struct ffs *ffs;
31 int type = video ? AVMEDIA_TYPE_VIDEO : AVMEDIA_TYPE_AUDIO;
32 ffs = malloc(sizeof(*ffs));
33 memset(ffs, 0, sizeof(*ffs));
34 ffs->si = -1;
35 if (avformat_open_input(&ffs->fc, path, NULL, NULL))
36 goto failed;
37 if (av_find_stream_info(ffs->fc) < 0)
38 goto failed;
39 ffs->si = av_find_best_stream(ffs->fc, type, -1, -1, NULL, 0);
40 if (ffs->si < 0)
41 goto failed;
42 ffs->cc = ffs->fc->streams[ffs->si]->codec;
43 avcodec_open(ffs->cc, avcodec_find_decoder(ffs->cc->codec_id));
44 return ffs;
45 failed:
46 ffs_free(ffs);
47 return NULL;
50 void ffs_free(struct ffs *ffs)
52 if (ffs->swsc)
53 sws_freeContext(ffs->swsc);
54 if (ffs->dst)
55 av_free(ffs->dst);
56 if (ffs->tmp)
57 av_free(ffs->tmp);
58 if (ffs->cc)
59 avcodec_close(ffs->cc);
60 if (ffs->fc)
61 av_close_input_file(ffs->fc);
62 free(ffs);
65 static AVPacket *ffs_pkt(struct ffs *ffs)
67 AVPacket *pkt = &ffs->pkt;
68 while (av_read_frame(ffs->fc, pkt) >= 0) {
69 ffs->seq_all++;
70 if (pkt->stream_index == ffs->si) {
71 ffs->seq_cur++;
72 ffs->seq++;
73 return pkt;
75 av_free_packet(pkt);
77 return NULL;
80 static long ts_ms(void)
82 struct timeval tv;
83 gettimeofday(&tv, NULL);
84 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
87 static int wait(long ts, int vdelay)
89 int nts = ts_ms();
90 if (nts > ts && ts + vdelay > nts) {
91 usleep((ts + vdelay - nts) * 1000);
92 return 0;
94 return 1;
97 #define MAX(a, b) ((a) < (b) ? (b) : (a))
99 void ffs_wait(struct ffs *ffs)
101 AVRational *r = &ffs->fc->streams[ffs->si]->r_frame_rate;
102 int vdelay = 1000 * r->den / r->num;
103 if (!wait(ffs->ts, MAX(vdelay, 20)))
104 ffs->ts += MAX(vdelay, 20);
105 else
106 ffs->ts = ts_ms(); /* out of sync */
109 /* audio/video frame offset difference */
110 int ffs_avdiff(struct ffs *ffs, struct ffs *affs)
112 return affs->seq_all - ffs->seq_all;
115 long ffs_pos(struct ffs *ffs, int diff)
117 return (ffs->si << 28) | (ffs->seq + diff);
120 void ffs_seek(struct ffs *ffs, long pos, int perframe)
122 long idx = pos >> 28;
123 long seq = pos & 0x00ffffff;
124 av_seek_frame(ffs->fc, idx, seq * perframe,
125 perframe == 1 ? AVSEEK_FLAG_FRAME : 0);
126 ffs->seq = seq;
127 ffs->seq_all = 0;
128 ffs->seq_cur = 0;
129 ffs->ts = 0;
132 void ffs_vinfo(struct ffs *ffs, int *w, int *h)
134 *h = ffs->cc->height;
135 *w = ffs->cc->width;
138 void ffs_ainfo(struct ffs *ffs, int *rate, int *bps, int *ch)
140 *rate = ffs->cc->sample_rate;
141 *ch = ffs->cc->channels;
142 *bps = 16;
145 int ffs_vdec(struct ffs *ffs, void **buf)
147 AVCodecContext *vcc = ffs->cc;
148 AVPacket *pkt = ffs_pkt(ffs);
149 int fine = 0;
150 if (!pkt)
151 return -1;
152 avcodec_decode_video2(vcc, ffs->tmp, &fine, pkt);
153 av_free_packet(pkt);
154 if (fine && buf) {
155 sws_scale(ffs->swsc, (void *) ffs->tmp->data, ffs->tmp->linesize,
156 0, vcc->height, ffs->dst->data, ffs->dst->linesize);
157 *buf = (void *) ffs->dst->data[0];
158 return ffs->dst->linesize[0];
160 return 0;
163 int ffs_adec(struct ffs *ffs, void *buf, int blen)
165 int rdec = 0;
166 AVPacket tmppkt;
167 AVPacket *pkt = ffs_pkt(ffs);
168 if (!pkt)
169 return -1;
170 tmppkt.size = pkt->size;
171 tmppkt.data = pkt->data;
172 while (tmppkt.size > 0) {
173 int size = blen - rdec;
174 int len = avcodec_decode_audio3(ffs->cc, (int16_t *) (buf + rdec),
175 &size, &tmppkt);
176 if (len < 0)
177 break;
178 tmppkt.size -= len;
179 tmppkt.data += len;
180 if (size > 0)
181 rdec += size;
183 av_free_packet(pkt);
184 return rdec;
187 static int fbm2pixfmt(int fbm)
189 switch (fbm & 0x0fff) {
190 case 0x888:
191 return PIX_FMT_RGB32;
192 case 0x565:
193 return PIX_FMT_RGB565;
194 case 0x233:
195 return PIX_FMT_RGB8;
196 default:
197 fprintf(stderr, "ffs: unknown fb_mode()\n");
198 return PIX_FMT_RGB32;
202 void ffs_vsetup(struct ffs *ffs, float zoom, int fbm)
204 int h = ffs->cc->height;
205 int w = ffs->cc->width;
206 int fmt = ffs->cc->pix_fmt;
207 int pixfmt = fbm2pixfmt(fbm);
208 uint8_t *buf = NULL;
209 int n;
210 ffs->swsc = sws_getContext(w, h, fmt, w * zoom, h * zoom,
211 pixfmt, SWS_FAST_BILINEAR | SWS_CPU_CAPS_MMX2,
212 NULL, NULL, NULL);
213 ffs->dst = avcodec_alloc_frame();
214 ffs->tmp = avcodec_alloc_frame();
215 n = avpicture_get_size(pixfmt, w * zoom, h * zoom);
216 buf = av_malloc(n * sizeof(uint8_t));
217 avpicture_fill((AVPicture *) ffs->dst, buf, pixfmt, w * zoom, h * zoom);
220 void ffs_globinit(void)
222 av_register_all();