6 #include <libavcodec/avcodec.h>
7 #include <libavformat/avformat.h>
8 #include <libswresample/swresample.h>
9 #include <libswscale/swscale.h>
12 #define FFS_SAMPLEFMT AV_SAMPLE_FMT_S16
13 #define FFS_CHLAYOUT AV_CH_LAYOUT_STEREO
20 int si
; /* stream index */
21 long ts
; /* frame timestamp (ms) */
22 long seq
; /* current position in this stream */
23 long seq_all
; /* seen packets after ffs_seek() */
24 long seq_cur
; /* decoded packet after ffs_seek() */
26 /* decoding video frames */
27 struct SwsContext
*swsc
;
28 struct SwrContext
*swrc
;
33 struct ffs
*ffs_alloc(char *path
, int flags
)
36 int type
= flags
& FFS_VIDEO
? AVMEDIA_TYPE_VIDEO
: AVMEDIA_TYPE_AUDIO
;
37 int idx
= (flags
& FFS_STRIDX
) - 1;
38 AVDictionary
*opt
= NULL
;
39 ffs
= malloc(sizeof(*ffs
));
40 memset(ffs
, 0, sizeof(*ffs
));
42 if (avformat_open_input(&ffs
->fc
, path
, NULL
, NULL
))
44 if (avformat_find_stream_info(ffs
->fc
, NULL
) < 0)
46 ffs
->si
= av_find_best_stream(ffs
->fc
, type
, idx
, -1, NULL
, 0);
49 ffs
->cc
= ffs
->fc
->streams
[ffs
->si
]->codec
;
50 if (avcodec_open2(ffs
->cc
, avcodec_find_decoder(ffs
->cc
->codec_id
), &opt
))
52 ffs
->tmp
= avcodec_alloc_frame();
53 ffs
->dst
= avcodec_alloc_frame();
60 void ffs_free(struct ffs
*ffs
)
65 sws_freeContext(ffs
->swsc
);
71 avcodec_close(ffs
->cc
);
73 avformat_close_input(&ffs
->fc
);
77 static AVPacket
*ffs_pkt(struct ffs
*ffs
)
79 AVPacket
*pkt
= &ffs
->pkt
;
80 while (av_read_frame(ffs
->fc
, pkt
) >= 0) {
82 if (pkt
->stream_index
== ffs
->si
) {
92 static long ts_ms(void)
95 gettimeofday(&tv
, NULL
);
96 return tv
.tv_sec
* 1000 + tv
.tv_usec
/ 1000;
99 static int wait(long ts
, int vdelay
)
102 if (nts
> ts
&& ts
+ vdelay
> nts
) {
103 usleep((ts
+ vdelay
- nts
) * 1000);
109 #define MAX(a, b) ((a) < (b) ? (b) : (a))
111 void ffs_wait(struct ffs
*ffs
)
113 AVRational
*r
= &ffs
->fc
->streams
[ffs
->si
]->r_frame_rate
;
114 int vdelay
= 1000 * r
->den
/ r
->num
;
115 if (!wait(ffs
->ts
, MAX(vdelay
, 20)))
116 ffs
->ts
+= MAX(vdelay
, 20);
118 ffs
->ts
= ts_ms(); /* out of sync */
121 /* audio/video frame offset difference */
122 int ffs_avdiff(struct ffs
*ffs
, struct ffs
*affs
)
124 return affs
->seq_all
- ffs
->seq_all
;
127 long ffs_pos(struct ffs
*ffs
, int diff
)
129 return (ffs
->si
<< 28) | (ffs
->seq
+ diff
);
132 void ffs_seek(struct ffs
*ffs
, long pos
, int perframe
)
134 long idx
= pos
>> 28;
135 long seq
= pos
& 0x00ffffff;
136 av_seek_frame(ffs
->fc
, idx
, seq
* perframe
,
137 perframe
== 1 ? AVSEEK_FLAG_FRAME
: 0);
144 void ffs_vinfo(struct ffs
*ffs
, int *w
, int *h
)
146 *h
= ffs
->cc
->height
;
150 void ffs_ainfo(struct ffs
*ffs
, int *rate
, int *bps
, int *ch
)
152 *rate
= ffs
->cc
->sample_rate
;
153 *ch
= av_get_channel_layout_nb_channels(FFS_CHLAYOUT
);
157 int ffs_vdec(struct ffs
*ffs
, void **buf
)
159 AVCodecContext
*vcc
= ffs
->cc
;
160 AVPacket
*pkt
= ffs_pkt(ffs
);
164 avcodec_decode_video2(vcc
, ffs
->tmp
, &fine
, pkt
);
167 sws_scale(ffs
->swsc
, (void *) ffs
->tmp
->data
, ffs
->tmp
->linesize
,
168 0, vcc
->height
, ffs
->dst
->data
, ffs
->dst
->linesize
);
169 *buf
= (void *) ffs
->dst
->data
[0];
170 return ffs
->dst
->linesize
[0];
175 static int ffs_bytespersample(ffs
)
177 return av_get_bytes_per_sample(FFS_SAMPLEFMT
) *
178 av_get_channel_layout_nb_channels(FFS_CHLAYOUT
);
181 int ffs_adec(struct ffs
*ffs
, void *buf
, int blen
)
184 AVPacket tmppkt
= {0};
185 AVPacket
*pkt
= ffs_pkt(ffs
);
186 uint8_t *out
[] = {NULL
};
189 tmppkt
.size
= pkt
->size
;
190 tmppkt
.data
= pkt
->data
;
191 while (tmppkt
.size
> 0) {
193 len
= avcodec_decode_audio4(ffs
->cc
, ffs
->tmp
, &size
, &tmppkt
);
201 len
= swr_convert(ffs
->swrc
,
202 out
, (blen
- rdec
) / ffs_bytespersample(ffs
),
203 (void *) ffs
->tmp
->extended_data
, ffs
->tmp
->nb_samples
);
205 rdec
+= len
* ffs_bytespersample(ffs
);
211 static int fbm2pixfmt(int fbm
)
213 switch (fbm
& 0x0fff) {
215 return PIX_FMT_RGB32
;
217 return PIX_FMT_RGB565
;
221 fprintf(stderr
, "ffs: unknown fb_mode()\n");
222 return PIX_FMT_RGB32
;
226 void ffs_vconf(struct ffs
*ffs
, float zoom
, int fbm
)
228 int h
= ffs
->cc
->height
;
229 int w
= ffs
->cc
->width
;
230 int fmt
= ffs
->cc
->pix_fmt
;
231 int pixfmt
= fbm2pixfmt(fbm
);
234 ffs
->swsc
= sws_getContext(w
, h
, fmt
, w
* zoom
, h
* zoom
,
235 pixfmt
, SWS_FAST_BILINEAR
| SWS_CPU_CAPS_MMX2
,
237 n
= avpicture_get_size(pixfmt
, w
* zoom
, h
* zoom
);
238 buf
= av_malloc(n
* sizeof(uint8_t));
239 avpicture_fill((AVPicture
*) ffs
->dst
, buf
, pixfmt
, w
* zoom
, h
* zoom
);
242 void ffs_aconf(struct ffs
*ffs
)
245 ffs_ainfo(ffs
, &rate
, &bps
, &ch
);
246 ffs
->swrc
= swr_alloc_set_opts(NULL
,
247 FFS_CHLAYOUT
, FFS_SAMPLEFMT
, rate
,
248 ffs
->cc
->channel_layout
, ffs
->cc
->sample_fmt
, ffs
->cc
->sample_rate
,
253 void ffs_globinit(void)
256 avformat_network_init();