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 #include "libavfilter/avfilter.h"
32 #include "libavutil/avutil.h"
33 #include "libavfilter/buffersrc.h"
34 #include "libavfilter/buffersink.h"
35 #include "libavutil/imgutils.h"
36 #include "libavutil/opt.h"
37 #include "libavutil/pixdesc.h"
38 #include "libswresample/swresample.h"
39 #include "libswscale/swscale.h"
40 #include "libavutil/parseutils.h"
41 #include "libavutil/timecode.h"
47 operator AVPacket*() { return &pkt; }
48 operator AVPacket&() { return pkt; }
49 AVPacket *operator ->() { return &pkt; }
53 FFPacket() { init(); }
54 ~FFPacket() { finit(); }
57 class FFrame : public ListItem<FFrame> {
64 FFrame(FFStream *fst);
67 operator AVFrame*() { return frm; }
68 operator AVFrame&() { return *frm; }
69 AVFrame *operator ->() { return frm; }
71 int initted() { return init; }
72 void queue(int64_t pos);
74 void set_hw_frame(AVFrame *frame);
79 FFStream(FFMPEG *ffmpeg, AVStream *st, int fidx);
81 static void ff_lock(const char *cp=0);
82 static void ff_unlock();
83 void queue(FFrame *frm);
84 void dequeue(FFrame *frm);
86 virtual int encode_activate();
87 virtual int decode_activate();
88 virtual AVHWDeviceType decode_hw_activate();
89 virtual int decode_hw_format(AVCodec *decoder, AVHWDeviceType type);
90 virtual int write_packet(FFPacket &pkt);
92 int seek(int64_t no, double rate);
94 int decode(AVFrame *frame);
95 void load_markers(IndexMarks &marks, double rate);
97 virtual int is_audio() = 0;
98 virtual int is_video() = 0;
99 virtual int decode_frame(AVFrame *frame) = 0;
100 virtual int encode_frame(AVFrame *frame) = 0;
101 virtual int init_frame(AVFrame *frame) = 0;
102 virtual int create_filter(const char *filter_spec) = 0;
103 virtual void load_markers() = 0;
104 virtual IndexMarks *get_markers() = 0;
105 int insert_filter(const char *name, const char *arg, const char *inst_name=0);
106 int config_filters(const char *filter_spec, AVFilterContext *fsrc);
107 virtual int load_filter(AVFrame *frame);
108 int read_filter(AVFrame *frame);
109 int read_frame(AVFrame *frame);
110 int open_stats_file();
111 int close_stats_file();
112 int read_stats_file();
113 int write_stats_file();
114 int init_stats_file();
118 AVFormatContext *fmt_ctx;
119 AVCodecContext *avctx;
121 AVFilterContext *filt_ctx;
123 AVFilterContext *buffersrc_ctx;
124 AVFilterContext *buffersink_ctx;
125 AVFilterGraph *filter_graph;
126 AVFrame *frame, *fframe;
127 AVFrame *probe_frame;
131 int need_packet, flushed;
138 int64_t seek_pos, curr_pos;
140 int reading, writing;
141 int seeking, seeked, eof;
144 AVBufferRef *hw_device_ctx;
147 char *stats_filename;
151 int st_eof() { return eof; }
152 void st_eof(int v) { eof = v; }
155 class FFAudioStream : public FFStream {
156 float *inp, *outp, *bfr, *lmt;
160 int read(float *fp, long len);
161 void realloc(long nsz, int nch, long len);
162 void realloc(long nsz, int nch);
163 void reserve(long nsz, int nch);
166 void iseek(int64_t ofs);
167 float *get_outp(int len);
168 int64_t put_inp(int len);
169 int write(const float *fp, long len);
171 int write(const double *dp, long len, int ch);
172 int write_packet(FFPacket &pkt);
174 FFAudioStream(FFMPEG *ffmpeg, AVStream *strm, int idx, int fidx);
175 virtual ~FFAudioStream();
176 int is_audio() { return 1; }
177 int is_video() { return 0; }
178 void init_swr(int ichs, int ifmt, int irate);
179 int get_samples(float *&samples, uint8_t **data, int len);
180 int load_history(uint8_t **data, int len);
181 int decode_frame(AVFrame *frame);
182 int encode_frame(AVFrame *frame);
183 int create_filter(const char *filter_spec);
185 IndexMarks *get_markers();
187 int encode_activate();
188 int64_t load_buffer(double ** const sp, int len);
189 int in_history(int64_t pos);
190 void reset_history();
191 int read(double *dp, long len, int ch);
193 int init_frame(AVFrame *frame);
194 int load(int64_t pos, int len);
195 int audio_seek(int64_t pos);
196 int encode(double **samples, int len);
200 int channel0, channels;
205 SwrContext *resample_context;
206 int swr_ichs, swr_ifmt, swr_irate;
212 class FFVideoConvert {
214 Preferences *preferences;
215 struct SwsContext *convert_ctx;
218 FFVideoConvert(Preferences *preferences) {
219 this->preferences = preferences;
220 convert_ctx = 0; sw_frame = 0;
223 if( convert_ctx ) sws_freeContext(convert_ctx);
224 if( sw_frame ) av_frame_free(&sw_frame);
227 static AVPixelFormat color_model_to_pix_fmt(int color_model);
228 static int pix_fmt_to_color_model(AVPixelFormat pix_fmt);
230 int convert_picture_vframe(VFrame *frame, AVFrame *ip);
231 int convert_picture_vframe(VFrame *frame, AVFrame *ip, AVFrame *ipic);
232 int convert_cmodel(VFrame *frame, AVFrame *ip);
233 int transfer_cmodel(VFrame *frame, AVFrame *ifp);
234 int convert_vframe_picture(VFrame *frame, AVFrame *op);
235 int convert_vframe_picture(VFrame *frame, AVFrame *op, AVFrame *opic);
236 int convert_pixfmt(VFrame *frame, AVFrame *op);
237 int transfer_pixfmt(VFrame *frame, AVFrame *ofp);
240 class FFVideoStream : public FFStream, public FFVideoConvert {
241 int write_packet(FFPacket &pkt);
243 FFVideoStream(FFMPEG *ffmpeg, AVStream *strm, int idx, int fidx);
244 virtual ~FFVideoStream();
245 int is_audio() { return 0; }
246 int is_video() { return 1; }
247 int decode_frame(AVFrame *frame);
248 AVHWDeviceType decode_hw_activate();
249 int decode_hw_format(AVCodec *decoder, AVHWDeviceType type);
250 AVHWDeviceType encode_hw_activate(const char *hw_dev);
251 int encode_hw_write(FFrame *picture);
252 int encode_frame(AVFrame *frame);
253 int create_filter(const char *filter_spec);
255 IndexMarks *get_markers();
257 int init_frame(AVFrame *picture);
258 int load(VFrame *vframe, int64_t pos);
259 int video_seek(int64_t pos);
260 int encode(VFrame *vframe);
262 int convert_hw_frame(AVFrame *ifrm, AVFrame *ofrm);
263 int load_filter(AVFrame *frame);
264 double get_rotation_angle();
265 int flip(double theta);
269 int width, height, transpose;
275 int color_space, color_range;
276 struct SwsContext *fconvert_ctx;
284 const char *old_codec, *new_codec;
287 class FFCodecRemaps : public ArrayList<FFCodecRemap>
291 int add(const char *val);
292 int update(AVCodecID &codec_id, AVCodec *&decoder);
295 // for get_initial_timecode auto deletes
299 avFrame() { frm = av_frame_alloc(); }
300 ~avFrame() { av_frame_free(&frm); }
301 operator AVFrame *() { return frm; }
302 AVFrame *operator ->() { return frm; }
309 av_init_packet(&pkt);
310 pkt.data = 0; pkt.size = 0;
312 ~avPacket() { av_packet_unref(&pkt); }
313 operator AVPacket *() { return &pkt; }
314 AVPacket *operator ->() { return &pkt; }
317 class avCodecContext {
318 AVCodecContext *avctx;
320 avCodecContext(AVCodecContext *ctx) { avctx = ctx; }
321 ~avCodecContext() { avcodec_free_context(&avctx); }
322 operator AVCodecContext *() { return avctx; }
323 AVCodecContext *operator ->() { return avctx; }
327 class FFMPEG : public Thread {
330 static void ff_lock(const char *cp=0) { fflock.lock(cp); }
331 static void ff_unlock() { fflock.unlock(); }
333 int check_sample_rate(AVCodec *codec, int sample_rate);
334 AVRational check_frame_rate(const AVRational *p, double frame_rate);
335 AVRational to_sample_aspect_ratio(Asset *asset);
336 AVRational to_time_base(int sample_rate);
337 static int get_fmt_score(AVSampleFormat dst_fmt, AVSampleFormat src_fmt);
338 static AVSampleFormat find_best_sample_fmt_of_list(
339 const AVSampleFormat *sample_fmts, AVSampleFormat src_fmt);
341 static void set_option_path(char *path, const char *fmt, ...);
342 static void get_option_path(char *path, const char *type, const char *spec);
343 static int get_format(char *format, const char *path, const char *spec);
344 static int get_codec(char *codec, const char *path, const char *spec);
345 static int scan_option_line(const char *cp,char *tag,char *val);
346 static int load_defaults(const char *path, const char *type,
347 char *codec, char *codec_options, int len);
348 static int can_render(const char *fformat, const char *type);
349 static int renders_audio(const char *fformat) { return can_render(fformat, "audio"); }
350 static int renders_video(const char *fformat) { return can_render(fformat, "video"); }
351 static int get_ff_option(const char *nm, const char *options, char *value);
352 static void scan_audio_options(Asset *asset, EDL *edl);
353 static void load_audio_options(Asset *asset, EDL *edl);
354 static void scan_video_options(Asset *asset, EDL *edl);
355 static void load_video_options(Asset *asset, EDL *edl);
356 static void scan_format_options(Asset *asset, EDL *edl);
357 static void load_format_options(Asset *asset, EDL *edl);
358 static void set_asset_format(Asset *asset, EDL *edl, const char *text);
359 int get_file_format();
360 static int get_encoder(const char *options, char *format, char *codec, char *bsfilter);
361 static int scan_encoder(const char *line, char *format, char *codec, char *bsfilter);
362 int read_options(const char *options, AVDictionary *&opts, int skip=0);
363 int scan_options(const char *options, AVDictionary *&opts, AVStream *st);
364 int read_options(FILE *fp, const char *options, AVDictionary *&opts);
365 int load_options(const char *options, AVDictionary *&opts);
366 static int load_options(const char *path, char *bfr, int len);
367 void set_loglevel(const char *ap);
368 static double to_secs(int64_t time, AVRational time_base);
369 int info(char *text, int len);
371 void put_cache_frame(VFrame *frame, int64_t position);
375 int init_decoder(const char *filename);
377 int init_encoder(const char *filename);
378 int open_encoder(const char *type, const char *spec);
381 int total_audio_channels();
382 int total_video_channels();
383 double get_initial_timecode(int data_type, int channel, double frame_rate);
385 int audio_seek(int ch, int64_t pos);
386 int video_seek(int layer, int64_t pos);
388 int decode(int chn, int64_t pos, double *samples, int len);
389 int decode(int layer, int64_t pos, VFrame *frame);
390 int decode_activate();
391 int encode(int stream, double **samples, int len);
392 int encode(int stream, VFrame *frame);
393 int encode_activate();
396 AVFormatContext *fmt_ctx;
397 ArrayList<FFAudioStream*> ffaudio;
398 ArrayList<FFVideoStream*> ffvideo;
401 char *opt_video_filter;
402 char *opt_audio_filter;
404 char *opt_video_decoder;
405 char *opt_audio_decoder;
406 FFCodecRemaps video_codec_remaps;
407 FFCodecRemaps audio_codec_remaps;
408 char file_format[BCTEXTLEN];
413 uint16_t st_idx, st_ch;
414 ffidx() { st_idx = st_ch = 0; }
415 ffidx(const ffidx &t) { st_idx = t.st_idx; st_ch = t.st_ch; }
416 ffidx(uint16_t fidx, uint16_t ch) { st_idx = fidx; st_ch = ch; }
419 ArrayList<ffidx> astrm_index;
420 ArrayList<ffidx> vstrm_index;
421 int mux_audio(FFrame *frm);
422 int mux_video(FFrame *frm);
424 Condition *flow_lock;
435 int decoding, encoding;
436 int has_audio, has_video;
438 FFMPEG(FileBase *file_base=0);
440 AVCodecContext *activate_decoder(AVStream *st);
441 int scan(IndexState *index_state, int64_t *scan_position, int *canceled);
443 int ff_audio_stream(int channel) { return astrm_index[channel].st_idx; }
444 int ff_video_stream(int layer) { return vstrm_index[layer].st_idx; }
446 int ff_total_audio_channels();
447 int ff_total_astreams();
448 int ff_audio_channels(int stream);
449 int ff_sample_rate(int stream);
450 const char *ff_audio_format(int stream);
451 int ff_audio_pid(int stream);
452 int64_t ff_audio_samples(int stream);
453 int ff_audio_for_video(int vstream, int astream, int64_t &channels);
455 int ff_total_video_layers();
456 int ff_total_vstreams();
457 int ff_video_width(int stream);
458 int ff_video_height(int stream);
459 int ff_set_video_width(int stream, int width);
460 int ff_set_video_height(int stream, int height);
461 int ff_coded_width(int stream);
462 int ff_coded_height(int stream);
463 float ff_aspect_ratio(int stream);
464 int ff_color_range(int stream);
465 int ff_color_space(int stream);
466 double ff_frame_rate(int stream);
467 const char *ff_video_codec(int stream);
468 int64_t ff_video_frames(int stream);
469 int ff_video_pid(int stream);
470 int ff_video_mpeg_color_range(int stream);
471 double ff_get_timecode(char *str, AVRational rate, double pos);
472 static double get_timecode(const char *path, int data_type, int channel, double rate);
475 const char *ff_hw_dev();
476 Preferences *ff_prefs();
477 void dump_context(AVCodecContext *ctx);
480 #endif /* FFMPEG_H */