d682abb77d54cd4ace140ae609fc28f105e5aca8
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 video
)
31 int type
= video
? AVMEDIA_TYPE_VIDEO
: AVMEDIA_TYPE_AUDIO
;
32 ffs
= malloc(sizeof(*ffs
));
33 memset(ffs
, 0, sizeof(*ffs
));
35 if (avformat_open_input(&ffs
->fc
, path
, NULL
, NULL
))
37 if (av_find_stream_info(ffs
->fc
) < 0)
39 ffs
->si
= av_find_best_stream(ffs
->fc
, type
, -1, -1, NULL
, 0);
42 ffs
->cc
= ffs
->fc
->streams
[ffs
->si
]->codec
;
43 avcodec_open(ffs
->cc
, avcodec_find_decoder(ffs
->cc
->codec_id
));
50 void ffs_free(struct ffs
*ffs
)
53 sws_freeContext(ffs
->swsc
);
59 avcodec_close(ffs
->cc
);
61 av_close_input_file(ffs
->fc
);
65 static AVPacket
*ffs_pkt(struct ffs
*ffs
)
67 AVPacket
*pkt
= &ffs
->pkt
;
68 while (av_read_frame(ffs
->fc
, pkt
) >= 0) {
70 if (pkt
->stream_index
== ffs
->si
) {
80 static long ts_ms(void)
83 gettimeofday(&tv
, NULL
);
84 return tv
.tv_sec
* 1000 + tv
.tv_usec
/ 1000;
87 static int wait(long ts
, int vdelay
)
90 if (nts
> ts
&& ts
+ vdelay
> nts
) {
91 usleep((ts
+ vdelay
- nts
) * 1000);
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);
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);
132 void ffs_vinfo(struct ffs
*ffs
, int *w
, int *h
)
134 *h
= ffs
->cc
->height
;
138 void ffs_ainfo(struct ffs
*ffs
, int *rate
, int *bps
, int *ch
)
140 *rate
= ffs
->cc
->sample_rate
;
141 *ch
= ffs
->cc
->channels
;
145 int ffs_vdec(struct ffs
*ffs
, void **buf
)
147 AVCodecContext
*vcc
= ffs
->cc
;
148 AVPacket
*pkt
= ffs_pkt(ffs
);
152 avcodec_decode_video2(vcc
, ffs
->tmp
, &fine
, pkt
);
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];
163 int ffs_adec(struct ffs
*ffs
, void *buf
, int blen
)
167 AVPacket
*pkt
= ffs_pkt(ffs
);
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
),
187 static int fbm2pixfmt(int fbm
)
189 switch (fbm
& 0x0fff) {
191 return PIX_FMT_RGB32
;
193 return PIX_FMT_RGB565
;
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
);
210 ffs
->swsc
= sws_getContext(w
, h
, fmt
, w
* zoom
, h
* zoom
,
211 pixfmt
, SWS_FAST_BILINEAR
| SWS_CPU_CAPS_MMX2
,
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)