10 #include "arraylist.h"
12 #include "bccmodels.h"
13 #include "bcwindowbase.inc"
14 #include "condition.h"
19 #include "filebase.inc"
20 #include "fileffmpeg.inc"
21 #include "indexstate.inc"
27 #include "libavformat/avformat.h"
28 #include "libavformat/avio.h"
29 #include "libavcodec/avcodec.h"
30 #include "libavfilter/avfilter.h"
31 #include "libavutil/avutil.h"
32 #include "libavfilter/buffersrc.h"
33 #include "libavfilter/buffersink.h"
34 #include "libavutil/imgutils.h"
35 #include "libavutil/opt.h"
36 #include "libavutil/pixdesc.h"
37 #include "libswresample/swresample.h"
38 #include "libswscale/swscale.h"
44 operator AVPacket*() { return &pkt; }
45 operator AVPacket&() { return pkt; }
46 AVPacket *operator ->() { return &pkt; }
50 FFPacket() { init(); }
51 ~FFPacket() { finit(); }
54 class FFrame : public ListItem<FFrame> {
61 FFrame(FFStream *fst);
64 operator AVFrame*() { return frm; }
65 operator AVFrame&() { return *frm; }
66 AVFrame *operator ->() { return frm; }
68 int initted() { return init; }
69 void queue(int64_t pos);
75 FFStream(FFMPEG *ffmpeg, AVStream *st, int fidx);
77 static void ff_lock(const char *cp=0);
78 static void ff_unlock();
79 void queue(FFrame *frm);
80 void dequeue(FFrame *frm);
82 virtual int encode_activate();
83 virtual int decode_activate();
84 virtual int write_packet(FFPacket &pkt);
86 int seek(int64_t no, double rate);
88 int decode(AVFrame *frame);
89 void load_markers(IndexMarks &marks, double rate);
91 virtual int is_audio() = 0;
92 virtual int is_video() = 0;
93 virtual int decode_frame(AVFrame *frame) = 0;
94 virtual int encode_frame(AVFrame *frame) = 0;
95 virtual int init_frame(AVFrame *frame) = 0;
96 virtual int create_filter(const char *filter_spec, AVCodecParameters *avpar) = 0;
97 virtual void load_markers() = 0;
98 virtual IndexMarks *get_markers() = 0;
99 int create_filter(const char *filter_spec);
100 int load_filter(AVFrame *frame);
101 int read_filter(AVFrame *frame);
102 int read_frame(AVFrame *frame);
103 int open_stats_file();
104 int close_stats_file();
105 int read_stats_file();
106 int write_stats_file();
107 int init_stats_file();
111 AVFormatContext *fmt_ctx;
112 AVCodecContext *avctx;
114 AVFilterContext *buffersink_ctx;
115 AVFilterContext *buffersrc_ctx;
116 AVFilterGraph *filter_graph;
117 AVFrame *frame, *fframe;
121 int need_packet, flushed;
128 int64_t seek_pos, curr_pos;
130 int reading, writing;
134 char *stats_filename;
138 int st_eof() { return eof; }
139 void st_eof(int v) { eof = v; }
142 class FFAudioStream : public FFStream {
143 float *inp, *outp, *bfr, *lmt;
147 int read(float *fp, long len);
148 void realloc(long nsz, int nch, long len);
149 void realloc(long nsz, int nch);
150 void reserve(long nsz, int nch);
153 void iseek(int64_t ofs);
154 float *get_outp(int len);
155 int64_t put_inp(int len);
156 int write(const float *fp, long len);
158 int write(const double *dp, long len, int ch);
159 int write_packet(FFPacket &pkt);
161 FFAudioStream(FFMPEG *ffmpeg, AVStream *strm, int idx, int fidx);
162 virtual ~FFAudioStream();
163 int is_audio() { return 1; }
164 int is_video() { return 0; }
165 void init_swr(int ichs, int ifmt, int irate);
166 int get_samples(float *&samples, uint8_t **data, int len);
167 int load_history(uint8_t **data, int len);
168 int decode_frame(AVFrame *frame);
169 int encode_frame(AVFrame *frame);
170 int create_filter(const char *filter_spec, AVCodecParameters *avpar);
172 IndexMarks *get_markers();
174 int encode_activate();
175 int64_t load_buffer(double ** const sp, int len);
176 int in_history(int64_t pos);
177 void reset_history();
178 int read(double *dp, long len, int ch);
180 int init_frame(AVFrame *frame);
181 int load(int64_t pos, int len);
182 int audio_seek(int64_t pos);
183 int encode(double **samples, int len);
187 int channel0, channels;
192 SwrContext *resample_context;
193 int swr_ichs, swr_ifmt, swr_irate;
199 class FFVideoConvert {
201 struct SwsContext *convert_ctx;
203 FFVideoConvert() { convert_ctx = 0; }
204 ~FFVideoConvert() { if( convert_ctx ) sws_freeContext(convert_ctx); }
206 static AVPixelFormat color_model_to_pix_fmt(int color_model);
207 static int pix_fmt_to_color_model(AVPixelFormat pix_fmt);
209 int convert_picture_vframe(VFrame *frame, AVFrame *ip);
210 int convert_picture_vframe(VFrame *frame, AVFrame *ip, AVFrame *ipic);
211 int convert_cmodel(VFrame *frame, AVFrame *ip);
212 int transfer_cmodel(VFrame *frame, AVFrame *ifp);
213 int convert_vframe_picture(VFrame *frame, AVFrame *op);
214 int convert_vframe_picture(VFrame *frame, AVFrame *op, AVFrame *opic);
215 int convert_pixfmt(VFrame *frame, AVFrame *op);
216 int transfer_pixfmt(VFrame *frame, AVFrame *ofp);
219 class FFVideoStream : public FFStream, public FFVideoConvert {
220 int write_packet(FFPacket &pkt);
222 FFVideoStream(FFMPEG *ffmpeg, AVStream *strm, int idx, int fidx);
223 virtual ~FFVideoStream();
224 int is_audio() { return 0; }
225 int is_video() { return 1; }
226 int decode_frame(AVFrame *frame);
227 int encode_frame(AVFrame *frame);
228 int create_filter(const char *filter_spec, AVCodecParameters *avpar);
230 IndexMarks *get_markers();
232 int init_frame(AVFrame *picture);
233 int load(VFrame *vframe, int64_t pos);
234 int video_seek(int64_t pos);
235 int encode(VFrame *vframe);
248 class FFMPEG : public Thread {
251 static void ff_lock(const char *cp=0) { fflock.lock(cp); }
252 static void ff_unlock() { fflock.unlock(); }
254 int check_sample_rate(AVCodec *codec, int sample_rate);
255 AVRational check_frame_rate(AVCodec *codec, double frame_rate);
256 AVRational to_sample_aspect_ratio(Asset *asset);
257 AVRational to_time_base(int sample_rate);
258 static int get_fmt_score(AVSampleFormat dst_fmt, AVSampleFormat src_fmt);
259 static AVSampleFormat find_best_sample_fmt_of_list(
260 const AVSampleFormat *sample_fmts, AVSampleFormat src_fmt);
262 static void set_option_path(char *path, const char *fmt, ...);
263 static void get_option_path(char *path, const char *type, const char *spec);
264 static int get_format(char *format, const char *path, const char *spec);
265 static int get_codec(char *codec, const char *path, const char *spec);
266 static int scan_option_line(const char *cp,char *tag,char *val);
267 static int load_defaults(const char *path, const char *type,
268 char *codec, char *codec_options, int len);
269 static int can_render(const char *fformat, const char *type);
270 static int renders_audio(const char *fformat) { return can_render(fformat, "audio"); }
271 static int renders_video(const char *fformat) { return can_render(fformat, "video"); }
272 static int get_ff_option(const char *nm, const char *options, char *value);
273 static void scan_audio_options(Asset *asset, EDL *edl);
274 static void load_audio_options(Asset *asset, EDL *edl);
275 static void scan_video_options(Asset *asset, EDL *edl);
276 static void load_video_options(Asset *asset, EDL *edl);
277 static void set_asset_format(Asset *asset, EDL *edl, const char *text);
278 int get_file_format();
279 static int get_encoder(const char *options, char *format, char *codec, char *bsfilter);
280 static int scan_encoder(const char *line, char *format, char *codec, char *bsfilter);
281 int read_options(const char *options, AVDictionary *&opts, int skip=0);
282 int scan_options(const char *options, AVDictionary *&opts, AVStream *st);
283 int read_options(FILE *fp, const char *options, AVDictionary *&opts);
284 int load_options(const char *options, AVDictionary *&opts);
285 static int load_options(const char *path, char *bfr, int len);
286 void set_loglevel(const char *ap);
287 static double to_secs(int64_t time, AVRational time_base);
288 int info(char *text, int len);
290 int init_decoder(const char *filename);
292 int init_encoder(const char *filename);
293 int open_encoder(const char *type, const char *spec);
296 int total_audio_channels();
297 int total_video_channels();
299 int audio_seek(int ch, int64_t pos);
300 int video_seek(int layer, int64_t pos);
302 int decode(int chn, int64_t pos, double *samples, int len);
303 int decode(int layer, int64_t pos, VFrame *frame);
304 int decode_activate();
305 int encode(int stream, double **samples, int len);
306 int encode(int stream, VFrame *frame);
307 int encode_activate();
310 AVFormatContext *fmt_ctx;
311 ArrayList<FFAudioStream*> ffaudio;
312 ArrayList<FFVideoStream*> ffvideo;
315 char *opt_video_filter;
316 char *opt_audio_filter;
317 char file_format[BCTEXTLEN];
322 uint16_t st_idx, st_ch;
323 ffidx() { st_idx = st_ch = 0; }
324 ffidx(const ffidx &t) { st_idx = t.st_idx; st_ch = t.st_ch; }
325 ffidx(uint16_t fidx, uint16_t ch) { st_idx = fidx; st_ch = ch; }
328 ArrayList<ffidx> astrm_index;
329 ArrayList<ffidx> vstrm_index;
330 int mux_audio(FFrame *frm);
331 int mux_video(FFrame *frm);
333 Condition *flow_lock;
344 int decoding, encoding;
345 int has_audio, has_video;
347 FFMPEG(FileBase *file_base=0);
349 int scan(IndexState *index_state, int64_t *scan_position, int *canceled);
351 int ff_audio_stream(int channel) { return astrm_index[channel].st_idx; }
352 int ff_video_stream(int layer) { return vstrm_index[layer].st_idx; }
354 int ff_total_audio_channels();
355 int ff_total_astreams();
356 int ff_audio_channels(int stream);
357 int ff_sample_rate(int stream);
358 const char *ff_audio_format(int stream);
359 int ff_audio_pid(int stream);
360 int64_t ff_audio_samples(int stream);
361 int ff_audio_for_video(int vstream, int astream, int64_t &channels);
363 int ff_total_video_layers();
364 int ff_total_vstreams();
365 int ff_video_width(int stream);
366 int ff_video_height(int stream);
367 int ff_set_video_width(int stream, int width);
368 int ff_set_video_height(int stream, int height);
369 int ff_coded_width(int stream);
370 int ff_coded_height(int stream);
371 float ff_aspect_ratio(int stream);
372 double ff_frame_rate(int stream);
373 const char *ff_video_format(int stream);
374 int64_t ff_video_frames(int stream);
375 int ff_video_pid(int stream);
376 int ff_video_mpeg_color_range(int stream);
379 void dump_context(AVCodecContext *ctx);
382 #endif /* FFMPEG_H */