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, AVCodecParameters *avpar) = 0;
103 virtual void load_markers() = 0;
104 virtual IndexMarks *get_markers() = 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);
109 int open_stats_file();
110 int close_stats_file();
111 int read_stats_file();
112 int write_stats_file();
113 int init_stats_file();
117 AVFormatContext *fmt_ctx;
118 AVCodecContext *avctx;
120 AVFilterContext *buffersink_ctx;
121 AVFilterContext *buffersrc_ctx;
122 AVFilterGraph *filter_graph;
123 AVFrame *frame, *fframe;
124 AVFrame *probe_frame;
128 int need_packet, flushed;
135 int64_t seek_pos, curr_pos;
137 int reading, writing;
141 AVBufferRef *hw_device_ctx;
144 char *stats_filename;
148 int st_eof() { return eof; }
149 void st_eof(int v) { eof = v; }
152 class FFAudioStream : public FFStream {
153 float *inp, *outp, *bfr, *lmt;
157 int read(float *fp, long len);
158 void realloc(long nsz, int nch, long len);
159 void realloc(long nsz, int nch);
160 void reserve(long nsz, int nch);
163 void iseek(int64_t ofs);
164 float *get_outp(int len);
165 int64_t put_inp(int len);
166 int write(const float *fp, long len);
168 int write(const double *dp, long len, int ch);
169 int write_packet(FFPacket &pkt);
171 FFAudioStream(FFMPEG *ffmpeg, AVStream *strm, int idx, int fidx);
172 virtual ~FFAudioStream();
173 int is_audio() { return 1; }
174 int is_video() { return 0; }
175 void init_swr(int ichs, int ifmt, int irate);
176 int get_samples(float *&samples, uint8_t **data, int len);
177 int load_history(uint8_t **data, int len);
178 int decode_frame(AVFrame *frame);
179 int encode_frame(AVFrame *frame);
180 int create_filter(const char *filter_spec, AVCodecParameters *avpar);
182 IndexMarks *get_markers();
184 int encode_activate();
185 int64_t load_buffer(double ** const sp, int len);
186 int in_history(int64_t pos);
187 void reset_history();
188 int read(double *dp, long len, int ch);
190 int init_frame(AVFrame *frame);
191 int load(int64_t pos, int len);
192 int audio_seek(int64_t pos);
193 int encode(double **samples, int len);
197 int channel0, channels;
202 SwrContext *resample_context;
203 int swr_ichs, swr_ifmt, swr_irate;
209 class FFVideoConvert {
211 Preferences *preferences;
212 struct SwsContext *convert_ctx;
215 FFVideoConvert(Preferences *preferences) {
216 this->preferences = preferences;
217 convert_ctx = 0; sw_frame = 0;
220 if( convert_ctx ) sws_freeContext(convert_ctx);
221 if( sw_frame ) av_frame_free(&sw_frame);
224 static AVPixelFormat color_model_to_pix_fmt(int color_model);
225 static int pix_fmt_to_color_model(AVPixelFormat pix_fmt);
227 int convert_picture_vframe(VFrame *frame, AVFrame *ip);
228 int convert_picture_vframe(VFrame *frame, AVFrame *ip, AVFrame *ipic);
229 int convert_cmodel(VFrame *frame, AVFrame *ip);
230 int transfer_cmodel(VFrame *frame, AVFrame *ifp);
231 int convert_vframe_picture(VFrame *frame, AVFrame *op);
232 int convert_vframe_picture(VFrame *frame, AVFrame *op, AVFrame *opic);
233 int convert_pixfmt(VFrame *frame, AVFrame *op);
234 int transfer_pixfmt(VFrame *frame, AVFrame *ofp);
237 class FFVideoStream : public FFStream, public FFVideoConvert {
238 int write_packet(FFPacket &pkt);
240 FFVideoStream(FFMPEG *ffmpeg, AVStream *strm, int idx, int fidx);
241 virtual ~FFVideoStream();
242 int is_audio() { return 0; }
243 int is_video() { return 1; }
244 int decode_frame(AVFrame *frame);
245 AVHWDeviceType decode_hw_activate();
246 int decode_hw_format(AVCodec *decoder, AVHWDeviceType type);
247 AVHWDeviceType encode_hw_activate(const char *hw_dev);
248 int encode_hw_write(FFrame *picture);
249 int encode_frame(AVFrame *frame);
250 int create_filter(const char *filter_spec, AVCodecParameters *avpar);
252 IndexMarks *get_markers();
254 int init_frame(AVFrame *picture);
255 int load(VFrame *vframe, int64_t pos);
256 int video_seek(int64_t pos);
257 int encode(VFrame *vframe);
259 double get_rotation_angle();
264 int width, height, transpose;
270 int color_space, color_range;
278 const char *old_codec, *new_codec;
281 class FFCodecRemaps : public ArrayList<FFCodecRemap>
285 int add(const char *val);
286 int update(AVCodecID &codec_id, AVCodec *&decoder);
289 // for get_initial_timecode auto deletes
293 avFrame() { frm = av_frame_alloc(); }
294 ~avFrame() { av_frame_free(&frm); }
295 operator AVFrame *() { return frm; }
296 AVFrame *operator ->() { return frm; }
303 av_init_packet(&pkt);
304 pkt.data = 0; pkt.size = 0;
306 ~avPacket() { av_packet_unref(&pkt); }
307 operator AVPacket *() { return &pkt; }
308 AVPacket *operator ->() { return &pkt; }
311 class avCodecContext {
312 AVCodecContext *avctx;
314 avCodecContext(AVCodecContext *ctx) { avctx = ctx; }
315 ~avCodecContext() { avcodec_free_context(&avctx); }
316 operator AVCodecContext *() { return avctx; }
317 AVCodecContext *operator ->() { return avctx; }
321 class FFMPEG : public Thread {
324 static void ff_lock(const char *cp=0) { fflock.lock(cp); }
325 static void ff_unlock() { fflock.unlock(); }
327 int check_sample_rate(AVCodec *codec, int sample_rate);
328 AVRational check_frame_rate(const AVRational *p, double frame_rate);
329 AVRational to_sample_aspect_ratio(Asset *asset);
330 AVRational to_time_base(int sample_rate);
331 static int get_fmt_score(AVSampleFormat dst_fmt, AVSampleFormat src_fmt);
332 static AVSampleFormat find_best_sample_fmt_of_list(
333 const AVSampleFormat *sample_fmts, AVSampleFormat src_fmt);
335 static void set_option_path(char *path, const char *fmt, ...);
336 static void get_option_path(char *path, const char *type, const char *spec);
337 static int get_format(char *format, const char *path, const char *spec);
338 static int get_codec(char *codec, const char *path, const char *spec);
339 static int scan_option_line(const char *cp,char *tag,char *val);
340 static int load_defaults(const char *path, const char *type,
341 char *codec, char *codec_options, int len);
342 static int can_render(const char *fformat, const char *type);
343 static int renders_audio(const char *fformat) { return can_render(fformat, "audio"); }
344 static int renders_video(const char *fformat) { return can_render(fformat, "video"); }
345 static int get_ff_option(const char *nm, const char *options, char *value);
346 static void scan_audio_options(Asset *asset, EDL *edl);
347 static void load_audio_options(Asset *asset, EDL *edl);
348 static void scan_video_options(Asset *asset, EDL *edl);
349 static void load_video_options(Asset *asset, EDL *edl);
350 static void scan_format_options(Asset *asset, EDL *edl);
351 static void load_format_options(Asset *asset, EDL *edl);
352 static void set_asset_format(Asset *asset, EDL *edl, const char *text);
353 int get_file_format();
354 static int get_encoder(const char *options, char *format, char *codec, char *bsfilter);
355 static int scan_encoder(const char *line, char *format, char *codec, char *bsfilter);
356 int read_options(const char *options, AVDictionary *&opts, int skip=0);
357 int scan_options(const char *options, AVDictionary *&opts, AVStream *st);
358 int read_options(FILE *fp, const char *options, AVDictionary *&opts);
359 int load_options(const char *options, AVDictionary *&opts);
360 static int load_options(const char *path, char *bfr, int len);
361 void set_loglevel(const char *ap);
362 static double to_secs(int64_t time, AVRational time_base);
363 int info(char *text, int len);
365 int init_decoder(const char *filename);
367 int init_encoder(const char *filename);
368 int open_encoder(const char *type, const char *spec);
371 int total_audio_channels();
372 int total_video_channels();
373 double get_initial_timecode(int data_type, int channel, double frame_rate);
375 int audio_seek(int ch, int64_t pos);
376 int video_seek(int layer, int64_t pos);
378 int decode(int chn, int64_t pos, double *samples, int len);
379 int decode(int layer, int64_t pos, VFrame *frame);
380 int decode_activate();
381 int encode(int stream, double **samples, int len);
382 int encode(int stream, VFrame *frame);
383 int encode_activate();
386 AVFormatContext *fmt_ctx;
387 ArrayList<FFAudioStream*> ffaudio;
388 ArrayList<FFVideoStream*> ffvideo;
391 char *opt_video_filter;
392 char *opt_audio_filter;
394 char *opt_video_decoder;
395 char *opt_audio_decoder;
396 FFCodecRemaps video_codec_remaps;
397 FFCodecRemaps audio_codec_remaps;
398 char file_format[BCTEXTLEN];
403 uint16_t st_idx, st_ch;
404 ffidx() { st_idx = st_ch = 0; }
405 ffidx(const ffidx &t) { st_idx = t.st_idx; st_ch = t.st_ch; }
406 ffidx(uint16_t fidx, uint16_t ch) { st_idx = fidx; st_ch = ch; }
409 ArrayList<ffidx> astrm_index;
410 ArrayList<ffidx> vstrm_index;
411 int mux_audio(FFrame *frm);
412 int mux_video(FFrame *frm);
414 Condition *flow_lock;
425 int decoding, encoding;
426 int has_audio, has_video;
428 FFMPEG(FileBase *file_base=0);
430 AVCodecContext *activate_decoder(AVStream *st);
431 int scan(IndexState *index_state, int64_t *scan_position, int *canceled);
433 int ff_audio_stream(int channel) { return astrm_index[channel].st_idx; }
434 int ff_video_stream(int layer) { return vstrm_index[layer].st_idx; }
436 int ff_total_audio_channels();
437 int ff_total_astreams();
438 int ff_audio_channels(int stream);
439 int ff_sample_rate(int stream);
440 const char *ff_audio_format(int stream);
441 int ff_audio_pid(int stream);
442 int64_t ff_audio_samples(int stream);
443 int ff_audio_for_video(int vstream, int astream, int64_t &channels);
445 int ff_total_video_layers();
446 int ff_total_vstreams();
447 int ff_video_width(int stream);
448 int ff_video_height(int stream);
449 int ff_set_video_width(int stream, int width);
450 int ff_set_video_height(int stream, int height);
451 int ff_coded_width(int stream);
452 int ff_coded_height(int stream);
453 float ff_aspect_ratio(int stream);
454 int ff_color_range(int stream);
455 int ff_color_space(int stream);
456 double ff_frame_rate(int stream);
457 const char *ff_video_codec(int stream);
458 int64_t ff_video_frames(int stream);
459 int ff_video_pid(int stream);
460 int ff_video_mpeg_color_range(int stream);
461 double ff_get_timecode(char *str, AVRational rate, double pos);
462 static double get_timecode(const char *path, int data_type, int channel, double rate);
465 const char *ff_hw_dev();
466 Preferences *ff_prefs();
467 void dump_context(AVCodecContext *ctx);
470 #endif /* FFMPEG_H */