6 #include <libavformat/avformat.h>
7 #include <libavcodec/avcodec.h>
8 #include <libswscale/swscale.h>
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
;
28 struct ffs
*ffs_alloc(char *path
, int flags
)
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
));
36 if (avformat_open_input(&ffs
->fc
, path
, NULL
, NULL
))
38 if (av_find_stream_info(ffs
->fc
) < 0)
40 ffs
->si
= av_find_best_stream(ffs
->fc
, type
, idx
, -1, NULL
, 0);
43 ffs
->cc
= ffs
->fc
->streams
[ffs
->si
]->codec
;
44 avcodec_open(ffs
->cc
, avcodec_find_decoder(ffs
->cc
->codec_id
));
51 void ffs_free(struct ffs
*ffs
)
54 sws_freeContext(ffs
->swsc
);
60 avcodec_close(ffs
->cc
);
62 av_close_input_file(ffs
->fc
);
66 static AVPacket
*ffs_pkt(struct ffs
*ffs
)
68 AVPacket
*pkt
= &ffs
->pkt
;
69 while (av_read_frame(ffs
->fc
, pkt
) >= 0) {
71 if (pkt
->stream_index
== ffs
->si
) {
81 static long ts_ms(void)
84 gettimeofday(&tv
, NULL
);
85 return tv
.tv_sec
* 1000 + tv
.tv_usec
/ 1000;
88 static int wait(long ts
, int vdelay
)
91 if (nts
> ts
&& ts
+ vdelay
> nts
) {
92 usleep((ts
+ vdelay
- nts
) * 1000);
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);
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);
133 void ffs_vinfo(struct ffs
*ffs
, int *w
, int *h
)
135 *h
= ffs
->cc
->height
;
139 void ffs_ainfo(struct ffs
*ffs
, int *rate
, int *bps
, int *ch
)
141 *rate
= ffs
->cc
->sample_rate
;
142 *ch
= ffs
->cc
->channels
;
146 int ffs_vdec(struct ffs
*ffs
, void **buf
)
148 AVCodecContext
*vcc
= ffs
->cc
;
149 AVPacket
*pkt
= ffs_pkt(ffs
);
153 avcodec_decode_video2(vcc
, ffs
->tmp
, &fine
, pkt
);
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];
164 int ffs_adec(struct ffs
*ffs
, void *buf
, int blen
)
168 AVPacket
*pkt
= ffs_pkt(ffs
);
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
),
188 static int fbm2pixfmt(int fbm
)
190 switch (fbm
& 0x0fff) {
192 return PIX_FMT_RGB32
;
194 return PIX_FMT_RGB565
;
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
);
211 ffs
->swsc
= sws_getContext(w
, h
, fmt
, w
* zoom
, h
* zoom
,
212 pixfmt
, SWS_FAST_BILINEAR
| SWS_CPU_CAPS_MMX2
,
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)
224 avformat_network_init();