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"
23 #include "preferences.inc"
28 #include "libavformat/avformat.h"
29 #include "libavformat/avio.h"
30 #include "libavcodec/avcodec.h"
31 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,18,100)
32 #include "libavcodec/bsf.h"
34 #include "libavfilter/avfilter.h"
35 #include "libavutil/avutil.h"
36 #include "libavfilter/buffersrc.h"
37 #include "libavfilter/buffersink.h"
38 #include "libavutil/imgutils.h"
39 #include "libavutil/opt.h"
40 #include "libavutil/pixdesc.h"
41 #include "libswresample/swresample.h"
42 #include "libswscale/swscale.h"
43 #include "libavutil/parseutils.h"
44 #include "libavutil/timecode.h"
50 operator AVPacket*() { return &pkt; }
51 operator AVPacket&() { return pkt; }
52 AVPacket *operator ->() { return &pkt; }
56 FFPacket() { init(); }
57 ~FFPacket() { finit(); }
60 class FFrame : public ListItem<FFrame> {
67 FFrame(FFStream *fst);
70 operator AVFrame*() { return frm; }
71 operator AVFrame&() { return *frm; }
72 AVFrame *operator ->() { return frm; }
74 int initted() { return init; }
75 void queue(int64_t pos);
77 void set_hw_frame(AVFrame *frame);
82 FFStream(FFMPEG *ffmpeg, AVStream *st, int fidx);
84 static void ff_lock(const char *cp=0);
85 static void ff_unlock();
86 void queue(FFrame *frm);
87 void dequeue(FFrame *frm);
89 virtual int encode_activate();
90 virtual int decode_activate();
91 virtual AVHWDeviceType decode_hw_activate();
92 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,16,100)
93 virtual int decode_hw_format(const AVCodec *decoder, AVHWDeviceType type);
95 virtual int decode_hw_format(AVCodec *decoder, AVHWDeviceType type);
97 virtual int write_packet(FFPacket &pkt);
99 int seek(int64_t no, double rate);
101 int decode(AVFrame *frame);
102 void load_markers(IndexMarks &marks, double rate);
104 virtual int is_audio() = 0;
105 virtual int is_video() = 0;
106 virtual int decode_frame(AVFrame *frame) = 0;
107 virtual int encode_frame(AVFrame *frame) = 0;
108 virtual int init_frame(AVFrame *frame) = 0;
109 virtual int create_filter(const char *filter_spec) = 0;
110 virtual void load_markers() = 0;
111 virtual IndexMarks *get_markers() = 0;
112 int insert_filter(const char *name, const char *arg, const char *inst_name=0);
113 int config_filters(const char *filter_spec, AVFilterContext *fsrc);
114 virtual int load_filter(AVFrame *frame);
115 int read_filter(AVFrame *frame);
116 int read_frame(AVFrame *frame);
117 int open_stats_file();
118 int close_stats_file();
119 int read_stats_file();
120 int write_stats_file();
121 int init_stats_file();
125 AVFormatContext *fmt_ctx;
126 AVCodecContext *avctx;
128 AVFilterContext *filt_ctx;
130 AVFilterContext *buffersrc_ctx;
131 AVFilterContext *buffersink_ctx;
132 AVFilterGraph *filter_graph;
133 AVFrame *frame, *fframe;
134 AVFrame *probe_frame;
138 int need_packet, flushed;
145 int64_t seek_pos, curr_pos;
147 int reading, writing;
148 int seeking, seeked, eof;
151 AVBufferRef *hw_device_ctx;
154 char *stats_filename;
158 int st_eof() { return eof; }
159 void st_eof(int v) { eof = v; }
162 class FFAudioStream : public FFStream {
163 float *inp, *outp, *bfr, *lmt;
167 int read(float *fp, long len);
168 void realloc(long nsz, int nch, long len);
169 void realloc(long nsz, int nch);
170 void reserve(long nsz, int nch);
173 void iseek(int64_t ofs);
174 float *get_outp(int len);
175 int64_t put_inp(int len);
176 int write(const float *fp, long len);
178 int write(const double *dp, long len, int ch);
179 int write_packet(FFPacket &pkt);
181 FFAudioStream(FFMPEG *ffmpeg, AVStream *strm, int idx, int fidx);
182 virtual ~FFAudioStream();
183 int is_audio() { return 1; }
184 int is_video() { return 0; }
185 void init_swr(int ichs, int ifmt, int irate);
186 int get_samples(float *&samples, uint8_t **data, int len);
187 int load_history(uint8_t **data, int len);
188 int decode_frame(AVFrame *frame);
189 int encode_frame(AVFrame *frame);
190 int create_filter(const char *filter_spec);
192 IndexMarks *get_markers();
194 int encode_activate();
195 int64_t load_buffer(double ** const sp, int len);
196 int in_history(int64_t pos);
197 void reset_history();
198 int read(double *dp, long len, int ch);
200 int init_frame(AVFrame *frame);
201 int load(int64_t pos, int len);
202 int audio_seek(int64_t pos);
203 int encode(double **samples, int len);
207 int channel0, channels;
212 SwrContext *resample_context;
213 int swr_ichs, swr_ifmt, swr_irate;
219 class FFVideoConvert {
221 Preferences *preferences;
222 struct SwsContext *convert_ctx;
225 FFVideoConvert(Preferences *preferences) {
226 this->preferences = preferences;
227 convert_ctx = 0; sw_frame = 0;
230 if( convert_ctx ) sws_freeContext(convert_ctx);
231 if( sw_frame ) av_frame_free(&sw_frame);
234 static AVPixelFormat color_model_to_pix_fmt(int color_model);
235 static int pix_fmt_to_color_model(AVPixelFormat pix_fmt);
237 int convert_picture_vframe(VFrame *frame, AVFrame *ip);
238 int convert_picture_vframe(VFrame *frame, AVFrame *ip, AVFrame *ipic);
239 int convert_cmodel(VFrame *frame, AVFrame *ip);
240 int transfer_cmodel(VFrame *frame, AVFrame *ifp);
241 int convert_vframe_picture(VFrame *frame, AVFrame *op);
242 int convert_vframe_picture(VFrame *frame, AVFrame *op, AVFrame *opic);
243 int convert_pixfmt(VFrame *frame, AVFrame *op);
244 int transfer_pixfmt(VFrame *frame, AVFrame *ofp);
247 class FFVideoStream : public FFStream, public FFVideoConvert {
248 int write_packet(FFPacket &pkt);
250 FFVideoStream(FFMPEG *ffmpeg, AVStream *strm, int idx, int fidx);
251 virtual ~FFVideoStream();
252 int is_audio() { return 0; }
253 int is_video() { return 1; }
254 int decode_frame(AVFrame *frame);
255 AVHWDeviceType decode_hw_activate();
256 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,16,100)
257 int decode_hw_format(const AVCodec *decoder, AVHWDeviceType type);
259 int decode_hw_format(AVCodec *decoder, AVHWDeviceType type);
261 AVHWDeviceType encode_hw_activate(const char *hw_dev);
262 int encode_hw_write(FFrame *picture);
263 int encode_frame(AVFrame *frame);
264 int create_filter(const char *filter_spec);
266 IndexMarks *get_markers();
268 int init_frame(AVFrame *picture);
269 int load(VFrame *vframe, int64_t pos);
270 int probe(int64_t pos);
271 int video_seek(int64_t pos);
272 int encode(VFrame *vframe);
274 int convert_hw_frame(AVFrame *ifrm, AVFrame *ofrm);
275 int load_filter(AVFrame *frame);
276 double get_rotation_angle();
277 int flip(double theta);
281 int width, height, transpose;
287 int color_space, color_range;
288 struct SwsContext *fconvert_ctx;
296 const char *old_codec, *new_codec;
299 class FFCodecRemaps : public ArrayList<FFCodecRemap>
303 int add(const char *val);
304 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,16,100)
305 int update(AVCodecID &codec_id, const AVCodec *&decoder);
307 int update(AVCodecID &codec_id, AVCodec *&decoder);
311 // for get_initial_timecode auto deletes
315 avFrame() { frm = av_frame_alloc(); }
316 ~avFrame() { av_frame_free(&frm); }
317 operator AVFrame *() { return frm; }
318 AVFrame *operator ->() { return frm; }
325 av_init_packet(&pkt);
326 pkt.data = 0; pkt.size = 0;
328 ~avPacket() { av_packet_unref(&pkt); }
329 operator AVPacket *() { return &pkt; }
330 AVPacket *operator ->() { return &pkt; }
333 class avCodecContext {
334 AVCodecContext *avctx;
336 avCodecContext(AVCodecContext *ctx) { avctx = ctx; }
337 ~avCodecContext() { avcodec_free_context(&avctx); }
338 operator AVCodecContext *() { return avctx; }
339 AVCodecContext *operator ->() { return avctx; }
343 class FFMPEG : public Thread {
346 static void ff_lock(const char *cp=0) { fflock.lock(cp); }
347 static void ff_unlock() { fflock.unlock(); }
349 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,16,100)
350 int check_sample_rate(const AVCodec *codec, int sample_rate);
352 int check_sample_rate(AVCodec *codec, int sample_rate);
354 AVRational check_frame_rate(const AVRational *p, double frame_rate);
355 AVRational to_sample_aspect_ratio(Asset *asset);
356 AVRational to_time_base(int sample_rate);
357 static int get_fmt_score(AVSampleFormat dst_fmt, AVSampleFormat src_fmt);
358 static AVSampleFormat find_best_sample_fmt_of_list(
359 const AVSampleFormat *sample_fmts, AVSampleFormat src_fmt);
361 static void set_option_path(char *path, const char *fmt, ...);
362 static void get_option_path(char *path, const char *type, const char *spec);
363 static int get_format(char *format, const char *path, const char *spec);
364 static int get_codec(char *codec, const char *path, const char *spec);
365 static int scan_option_line(const char *cp,char *tag,char *val);
366 static int load_defaults(const char *path, const char *type,
367 char *codec, char *codec_options, int len);
368 static int can_render(const char *fformat, const char *type);
369 static int renders_audio(const char *fformat) { return can_render(fformat, "audio"); }
370 static int renders_video(const char *fformat) { return can_render(fformat, "video"); }
371 static int get_ff_option(const char *nm, const char *options, char *value);
372 static void scan_audio_options(Asset *asset, EDL *edl);
373 static void load_audio_options(Asset *asset, EDL *edl);
374 static void scan_video_options(Asset *asset, EDL *edl);
375 static void load_video_options(Asset *asset, EDL *edl);
376 static void scan_format_options(Asset *asset, EDL *edl);
377 static void load_format_options(Asset *asset, EDL *edl);
378 static void set_asset_format(Asset *asset, EDL *edl, const char *text);
379 int get_file_format();
380 static int get_encoder(const char *options, char *format, char *codec, char *bsfilter);
381 static int scan_encoder(const char *line, char *format, char *codec, char *bsfilter);
382 int read_options(const char *options, AVDictionary *&opts, int skip=0);
383 int scan_options(const char *options, AVDictionary *&opts, AVStream *st);
384 int read_options(FILE *fp, const char *options, AVDictionary *&opts);
385 int load_options(const char *options, AVDictionary *&opts);
386 static int load_options(const char *path, char *bfr, int len);
387 void set_loglevel(const char *ap);
388 static double to_secs(int64_t time, AVRational time_base);
389 int info(char *text, int len);
391 void put_cache_frame(VFrame *frame, int64_t position);
395 int init_decoder(const char *filename);
397 int init_encoder(const char *filename);
398 int open_encoder(const char *type, const char *spec);
401 int total_audio_channels();
402 int total_video_channels();
403 double get_initial_timecode(int data_type, int channel, double frame_rate);
405 int audio_seek(int ch, int64_t pos);
406 int video_probe(int64_t pos);
407 int video_seek(int layer, int64_t pos);
409 int decode(int chn, int64_t pos, double *samples, int len);
410 int decode(int layer, int64_t pos, VFrame *frame);
411 int decode_activate();
412 int encode(int stream, double **samples, int len);
413 int encode(int stream, VFrame *frame);
414 int encode_activate();
417 AVFormatContext *fmt_ctx;
418 ArrayList<FFAudioStream*> ffaudio;
419 ArrayList<FFVideoStream*> ffvideo;
422 char *opt_video_filter;
423 char *opt_audio_filter;
425 char *opt_video_decoder;
426 char *opt_audio_decoder;
427 FFCodecRemaps video_codec_remaps;
428 FFCodecRemaps audio_codec_remaps;
429 char file_format[BCTEXTLEN];
434 uint16_t st_idx, st_ch;
435 ffidx() { st_idx = st_ch = 0; }
436 ffidx(const ffidx &t) { st_idx = t.st_idx; st_ch = t.st_ch; }
437 ffidx(uint16_t fidx, uint16_t ch) { st_idx = fidx; st_ch = ch; }
440 ArrayList<ffidx> astrm_index;
441 ArrayList<ffidx> vstrm_index;
442 int mux_audio(FFrame *frm);
443 int mux_video(FFrame *frm);
445 Condition *flow_lock;
456 int decoding, encoding;
457 int has_audio, has_video;
458 int interlace_from_codec;
460 FFMPEG(FileBase *file_base=0);
462 AVCodecContext *activate_decoder(AVStream *st);
463 int scan(IndexState *index_state, int64_t *scan_position, int *canceled);
465 int ff_audio_stream(int channel) { return astrm_index[channel].st_idx; }
466 int ff_video_stream(int layer) { return vstrm_index[layer].st_idx; }
468 int ff_total_audio_channels();
469 int ff_total_astreams();
470 int ff_audio_channels(int stream);
471 int ff_sample_rate(int stream);
472 const char *ff_audio_format(int stream);
473 int ff_audio_pid(int stream);
474 int64_t ff_audio_samples(int stream);
475 int ff_audio_for_video(int vstream, int astream, int64_t &channels);
477 int ff_total_video_layers();
478 int ff_total_vstreams();
479 int ff_video_width(int stream);
480 int ff_video_height(int stream);
481 int ff_set_video_width(int stream, int width);
482 int ff_set_video_height(int stream, int height);
483 int ff_coded_width(int stream);
484 int ff_coded_height(int stream);
485 float ff_aspect_ratio(int stream);
486 int ff_color_range(int stream);
487 int ff_color_space(int stream);
488 int ff_interlace(int stream);
489 double ff_frame_rate(int stream);
490 const char *ff_video_codec(int stream);
491 int64_t ff_video_frames(int stream);
492 int ff_video_pid(int stream);
493 int ff_video_mpeg_color_range(int stream);
494 double ff_get_timecode(char *str, AVRational rate, double pos);
495 static double get_timecode(const char *path, int data_type, int channel, double rate);
498 const char *ff_hw_dev();
499 Preferences *ff_prefs();
500 void dump_context(AVCodecContext *ctx);
503 #endif /* FFMPEG_H */