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 codec_type
= video
? CODEC_TYPE_VIDEO
: CODEC_TYPE_AUDIO
;
33 ffs
= malloc(sizeof(*ffs
));
34 memset(ffs
, 0, sizeof(*ffs
));
36 if (av_open_input_file(&ffs
->fc
, path
, NULL
, 0, NULL
))
38 if (av_find_stream_info(ffs
->fc
) < 0)
40 for (i
= 0; i
< ffs
->fc
->nb_streams
; i
++)
41 if (ffs
->fc
->streams
[i
]->codec
->codec_type
== codec_type
)
45 ffs
->cc
= ffs
->fc
->streams
[ffs
->si
]->codec
;
46 avcodec_open(ffs
->cc
, avcodec_find_decoder(ffs
->cc
->codec_id
));
53 void ffs_free(struct ffs
*ffs
)
56 sws_freeContext(ffs
->swsc
);
62 avcodec_close(ffs
->cc
);
64 av_close_input_file(ffs
->fc
);
68 static AVPacket
*ffs_pkt(struct ffs
*ffs
)
70 AVPacket
*pkt
= &ffs
->pkt
;
71 while (av_read_frame(ffs
->fc
, pkt
) >= 0) {
73 if (pkt
->stream_index
== ffs
->si
) {
83 static long ts_ms(void)
86 gettimeofday(&tv
, NULL
);
87 return tv
.tv_sec
* 1000 + tv
.tv_usec
/ 1000;
90 static void wait(long ts
, int vdelay
)
93 if (ts
+ vdelay
> nts
)
94 usleep((ts
+ vdelay
- nts
) * 1000);
97 void ffs_wait(struct ffs
*ffs
)
101 AVRational
*r
= &ffs
->fc
->streams
[ffs
->si
]->time_base
;
102 int vdelay
= 1000 * r
->num
/ r
->den
;
103 wait(ffs
->ts
, vdelay
< 20 ? 20 : vdelay
);
108 long ffs_pos(struct ffs
*ffs
, int diff
)
110 return (ffs
->si
<< 28) | (ffs
->seq
+ diff
);
113 void ffs_seek(struct ffs
*ffs
, long pos
, int perframe
)
115 long idx
= pos
>> 28;
116 long seq
= pos
& 0x0fffffff;
117 av_seek_frame(ffs
->fc
, idx
, seq
* perframe
,
118 perframe
== 1 ? AVSEEK_FLAG_FRAME
: 0);
125 long ffs_seq(struct ffs
*ffs
, int all
)
127 return all
? ffs
->seq_all
: ffs
->seq_cur
;
130 void ffs_vinfo(struct ffs
*ffs
, int *w
, int *h
)
132 *h
= ffs
->cc
->height
;
136 void ffs_ainfo(struct ffs
*ffs
, int *rate
, int *bps
, int *ch
)
138 *rate
= ffs
->cc
->sample_rate
;
139 *ch
= ffs
->cc
->channels
;
143 int ffs_vdec(struct ffs
*ffs
, char **buf
)
145 AVCodecContext
*vcc
= ffs
->cc
;
146 AVPacket
*pkt
= ffs_pkt(ffs
);
150 avcodec_decode_video2(vcc
, ffs
->tmp
, &fine
, pkt
);
153 sws_scale(ffs
->swsc
, ffs
->tmp
->data
, ffs
->tmp
->linesize
,
154 0, vcc
->height
, ffs
->dst
->data
, ffs
->dst
->linesize
);
155 *buf
= (void *) ffs
->dst
->data
[0];
156 return ffs
->dst
->linesize
[0];
161 int ffs_adec(struct ffs
*ffs
, char *buf
, int blen
)
165 AVPacket
*pkt
= ffs_pkt(ffs
);
168 tmppkt
.size
= pkt
->size
;
169 tmppkt
.data
= pkt
->data
;
170 while (tmppkt
.size
> 0) {
171 int size
= blen
- rdec
;
172 int len
= avcodec_decode_audio3(ffs
->cc
, (int16_t *) (buf
+ rdec
),
185 static int fbm2pixfmt(int fbm
)
187 switch (fbm
& 0x0fff) {
189 return PIX_FMT_RGB32
;
191 return PIX_FMT_RGB565
;
195 fprintf(stderr
, "ffs: unknown fb_mode()\n");
196 return PIX_FMT_RGB32
;
200 void ffs_vsetup(struct ffs
*ffs
, float zoom
, int fbm
)
202 int h
= ffs
->cc
->height
;
203 int w
= ffs
->cc
->width
;
204 int fmt
= ffs
->cc
->pix_fmt
;
205 int pixfmt
= fbm2pixfmt(fbm
);
208 ffs
->swsc
= sws_getContext(w
, h
, fmt
, w
* zoom
, h
* zoom
,
209 pixfmt
, SWS_FAST_BILINEAR
| SWS_CPU_CAPS_MMX2
,
211 ffs
->dst
= avcodec_alloc_frame();
212 ffs
->tmp
= avcodec_alloc_frame();
213 n
= avpicture_get_size(pixfmt
, w
* zoom
, h
* zoom
);
214 buf
= av_malloc(n
* sizeof(uint8_t));
215 avpicture_fill((AVPicture
*) ffs
->dst
, buf
, pixfmt
, w
* zoom
, h
* zoom
);
218 void ffs_globinit(void)