10 #include "arraylist.h"
13 #include "bccmodels.h"
16 #include "filebase.inc"
17 #include "fileffmpeg.inc"
21 #include "libavfilter/buffersrc.h"
22 #include "libavfilter/buffersink.h"
23 #include "libavformat/avformat.h"
24 #include "libavformat/avio.h"
25 #include "libavcodec/avcodec.h"
26 #include "libavfilter/avfilter.h"
27 #include "libavutil/avutil.h"
28 #include "libavutil/opt.h"
29 #include "libavutil/pixdesc.h"
30 #include "libswresample/swresample.h"
31 #include "libswscale/swscale.h"
40 operator AVPacket*() { return &pkt; }
41 operator AVPacket&() { return pkt; }
42 AVPacket *operator ->() { return &pkt; }
45 class FFrame : public ListItem<FFrame> {
52 FFrame(FFStream *fst);
55 operator AVFrame*() { return frm; }
56 operator AVFrame&() { return *frm; }
57 AVFrame *operator ->() { return frm; }
59 int initted() { return init; }
60 void queue(int64_t pos);
64 class FFAudioHistory {
66 float *inp, *outp, *bfr, *lmt;
72 void reserve(long sz, int nch);
73 void realloc(long sz, int nch);
77 void iseek(int64_t ofs);
78 float *get_outp(int len);
79 int64_t get_inp(int len);
80 int write(const float *fp, long len);
81 int copy(float *fp, long len);
83 int read(double *dp, long len, int ch);
84 int write(const double *dp, long len, int ch);
89 FFStream(FFMPEG *ffmpeg, AVStream *st, int idx);
91 static void ff_lock(const char *cp=0);
92 static void ff_unlock();
93 void queue(FFrame *frm);
94 void dequeue(FFrame *frm);
96 virtual int encode_activate();
97 virtual int decode_activate();
99 int decode(AVFrame *frame);
101 virtual int decode_frame(AVFrame *frame, int &got_frame) = 0;
102 virtual int init_frame(AVFrame *frame) = 0;
103 virtual int create_filter(const char *filter_spec,
104 AVCodecContext *src_ctx, AVCodecContext *sink_ctx) = 0;
105 int create_filter(const char *filter_spec);
106 int load_filter(AVFrame *frame);
107 int read_filter(AVFrame *frame);
108 int read_frame(AVFrame *frame);
112 AVFormatContext *fmt_ctx;
114 AVFilterContext *buffersink_ctx;
115 AVFilterContext *buffersrc_ctx;
116 AVFilterGraph *filter_graph;
117 AVFrame *frame, *fframe;
121 AVBitStreamFilterContext *bsfc;
123 BSFilter(const char *bsf, const char *ap) {
124 bsfc = av_bitstream_filter_init(bsf);
125 args = ap ? cstrdup(ap) : 0;
128 av_bitstream_filter_close(bsfc);
132 void add_bsfilter(const char *bsf, const char *ap);
133 ArrayList<BSFilter *> bsfilter;
134 int bs_filter(AVPacket *pkt);
137 int need_packet, flushed;
145 int reading, writing;
152 if( !v ) { flushed = 0; need_packet = 1; }
157 class FFAudioStream : public FFStream {
159 FFAudioStream(FFMPEG *ffmpeg, AVStream *strm, int idx);
160 virtual ~FFAudioStream();
161 int load_history(float *&bfr, int len);
162 int decode_frame(AVFrame *frame, int &got_frame);
163 int create_filter(const char *filter_spec,
164 AVCodecContext *src_ctx, AVCodecContext *sink_ctx);
166 int encode_activate();
168 void alloc_history(int len);
169 void reserve_history(int len);
170 void append_history(const float *fp, int len);
171 void zero_history(int len);
172 int64_t load_buffer(double ** const sp, int len);
173 float *get_history(int len);
174 int in_history(int64_t pos);
176 int init_frame(AVFrame *frame);
177 int load(int64_t pos, int len);
178 int audio_seek(int64_t pos);
179 int encode(double **samples, int len);
181 int channel0, channels;
184 int64_t seek_pos, curr_pos;
187 FFAudioHistory history;
188 SwrContext *resample_context;
193 class FFVideoStream : public FFStream {
195 FFVideoStream(FFMPEG *ffmpeg, AVStream *strm, int idx);
196 virtual ~FFVideoStream();
197 int decode_frame(AVFrame *frame, int &got_frame);
198 int create_filter(const char *filter_spec,
199 AVCodecContext *src_ctx, AVCodecContext *sink_ctx);
201 int init_frame(AVFrame *picture);
202 int load(VFrame *vframe, int64_t pos);
203 int video_seek(int64_t pos);
204 int encode(VFrame *vframe);
208 int64_t seek_pos, curr_pos;
212 struct SwsContext *convert_ctx;
216 static PixelFormat color_model_to_pix_fmt(int color_model);
217 static int pix_fmt_to_color_model(PixelFormat pix_fmt);
219 int convert_picture_vframe(VFrame *frame,
220 AVPicture *ip, PixelFormat ifmt, int iw, int ih);
221 int convert_cmodel(VFrame *frame_out,
222 AVPicture *ip, PixelFormat ifmt, int iw, int ih);
223 int convert_vframe_picture(VFrame *frame,
224 AVPicture *op, PixelFormat ofmt, int ow, int oh);
225 int convert_pixfmt(VFrame *frame_in,
226 AVPicture *op, PixelFormat ofmt, int ow, int oh);
229 class FFMPEG : public Thread {
232 static void ff_lock(const char *cp=0) { fflock.lock(cp); }
233 static void ff_unlock() { fflock.unlock(); }
235 int check_sample_rate(AVCodec *codec, int sample_rate);
236 AVRational check_frame_rate(AVCodec *codec, double frame_rate);
237 AVRational to_sample_aspect_ratio(double aspect_ratio);
238 AVRational to_time_base(int sample_rate);
240 static void set_option_path(char *path, const char *fmt, ...);
241 static void get_option_path(char *path, const char *type, const char *spec);
242 int check_option(const char *path, char *spec);
243 const char *get_file_format();
244 int scan_option_line(char *cp,char *tag,char *val);
245 int read_options(const char *options, char *format, char *codec,
246 char *bsfilter, char *bsargs, AVDictionary *&opts);
247 int read_options(FILE *fp, const char *options,
248 char *format, char *codec, AVDictionary *&opts);
249 int read_options(const char *options, AVDictionary *&opts);
250 int read_options(FILE *fp, const char *options, AVDictionary *&opts, int no=0);
251 int load_options(const char *options, AVDictionary *&opts);
252 void set_loglevel(const char *ap);
253 static double to_secs(int64_t time, AVRational time_base);
254 int info(char *text, int len);
256 int init_decoder(const char *filename);
258 int init_encoder(const char *filename);
259 int open_encoder(const char *path, const char *spec);
262 int total_audio_channels();
263 int total_video_channels();
265 int audio_seek(int ch, int64_t pos);
266 int video_seek(int layer, int64_t pos);
268 int decode(int chn, int64_t pos, double *samples, int len);
269 int decode(int layer, int64_t pos, VFrame *frame);
270 int decode_activate();
271 int encode(int stream, double **samples, int len);
272 int encode(int stream, VFrame *frame);
273 int encode_activate();
276 AVFormatContext *fmt_ctx;
277 ArrayList<FFAudioStream*> ffaudio;
278 ArrayList<FFVideoStream*> ffvideo;
281 char *opt_video_filter;
282 char *opt_audio_filter;
283 char file_format[BCTEXTLEN];
287 uint16_t st_idx, st_ch;
288 ffidx() { st_idx = st_ch = 0; }
289 ffidx(const ffidx &t) { st_idx = t.st_idx; st_ch = t.st_ch; }
290 ffidx(uint16_t idx, uint16_t ch) { st_idx = idx; st_ch = ch; }
293 ArrayList<ffidx> astrm_index;
294 ArrayList<ffidx> vstrm_index;
295 int mux_audio(FFrame *frm);
296 int mux_video(FFrame *frm);
298 Condition *flow_lock;
309 int decoding, encoding;
310 int has_audio, has_video;
312 FFMPEG(FileBase *file_base=0);
315 int ff_audio_stream(int channel) { return astrm_index[channel].st_idx; }
316 int ff_video_stream(int layer) { return vstrm_index[layer].st_idx; }
318 int ff_total_audio_channels();
319 int ff_total_astreams();
320 int ff_audio_channels(int stream);
321 int ff_sample_rate(int stream);
322 const char *ff_audio_format(int stream);
323 int ff_audio_pid(int stream);
324 int64_t ff_audio_samples(int stream);
325 int ff_audio_for_video(int vstream, int astream, int64_t &channels);
327 int ff_total_video_layers();
328 int ff_total_vstreams();
329 int ff_video_width(int stream);
330 int ff_video_height(int stream);
331 int ff_set_video_width(int stream, int width);
332 int ff_set_video_height(int stream, int height);
333 int ff_coded_width(int stream);
334 int ff_coded_height(int stream);
335 float ff_aspect_ratio(int stream);
336 double ff_frame_rate(int stream);
337 const char *ff_video_format(int stream);
338 int64_t ff_video_frames(int stream);
339 int ff_video_pid(int stream);
342 void dump_context(AVCodecContext *ctx);
345 #endif /* FFMPEG_H */