3 * Copyright (C) 2012-2014 Paolo Rampino
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published
7 * by the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public
16 * License along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
30 #include "arraylist.h"
32 #include "bccmodels.h"
33 #include "bcwindowbase.inc"
34 #include "condition.h"
39 #include "filebase.inc"
40 #include "fileffmpeg.inc"
41 #include "indexstate.inc"
43 #include "preferences.inc"
48 #include "libavformat/avformat.h"
49 #include "libavformat/avio.h"
50 #include "libavcodec/avcodec.h"
51 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,18,100)
52 #include "libavcodec/bsf.h"
54 #include "libavfilter/avfilter.h"
55 #include "libavutil/avutil.h"
56 #include "libavfilter/buffersrc.h"
57 #include "libavfilter/buffersink.h"
58 #include "libavutil/imgutils.h"
59 #include "libavutil/opt.h"
60 #include "libavutil/pixdesc.h"
61 #include "libswresample/swresample.h"
62 #include "libswscale/swscale.h"
63 #include "libavutil/parseutils.h"
64 #include "libavutil/timecode.h"
70 operator AVPacket*() { return &pkt; }
71 operator AVPacket&() { return pkt; }
72 AVPacket *operator ->() { return &pkt; }
76 FFPacket() { init(); }
77 ~FFPacket() { finit(); }
80 class FFrame : public ListItem<FFrame> {
87 FFrame(FFStream *fst);
90 operator AVFrame*() { return frm; }
91 operator AVFrame&() { return *frm; }
92 AVFrame *operator ->() { return frm; }
94 int initted() { return init; }
95 void queue(int64_t pos);
97 void set_hw_frame(AVFrame *frame);
102 FFStream(FFMPEG *ffmpeg, AVStream *st, int fidx);
104 static void ff_lock(const char *cp=0);
105 static void ff_unlock();
106 void queue(FFrame *frm);
107 void dequeue(FFrame *frm);
109 virtual int encode_activate();
110 virtual int decode_activate();
111 virtual AVHWDeviceType decode_hw_activate();
112 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,16,100)
113 virtual int decode_hw_format(const AVCodec *decoder, AVHWDeviceType type);
115 virtual int decode_hw_format(AVCodec *decoder, AVHWDeviceType type);
117 virtual int write_packet(FFPacket &pkt);
119 int seek(int64_t no, double rate);
121 int decode(AVFrame *frame);
122 void load_markers(IndexMarks &marks, double rate);
124 virtual int is_audio() = 0;
125 virtual int is_video() = 0;
126 virtual int decode_frame(AVFrame *frame) = 0;
127 virtual int encode_frame(AVFrame *frame) = 0;
128 virtual int init_frame(AVFrame *frame) = 0;
129 virtual int create_filter(const char *filter_spec) = 0;
130 virtual void load_markers() = 0;
131 virtual IndexMarks *get_markers() = 0;
132 int insert_filter(const char *name, const char *arg, const char *inst_name=0);
133 int config_filters(const char *filter_spec, AVFilterContext *fsrc);
134 virtual int load_filter(AVFrame *frame);
135 int read_filter(AVFrame *frame);
136 int read_frame(AVFrame *frame);
137 int open_stats_file();
138 int close_stats_file();
139 int read_stats_file();
140 int write_stats_file();
141 int init_stats_file();
145 AVFormatContext *fmt_ctx;
146 AVCodecContext *avctx;
148 AVFilterContext *filt_ctx;
150 AVFilterContext *buffersrc_ctx;
151 AVFilterContext *buffersink_ctx;
152 AVFilterGraph *filter_graph;
153 AVFrame *frame, *fframe;
154 AVFrame *probe_frame;
158 int need_packet, flushed;
165 int64_t seek_pos, curr_pos;
167 int reading, writing;
168 int seeking, seeked, eof;
171 AVBufferRef *hw_device_ctx;
174 char *stats_filename;
178 int st_eof() { return eof; }
179 void st_eof(int v) { eof = v; }
182 class FFAudioStream : public FFStream {
183 float *inp, *outp, *bfr, *lmt;
187 int read(float *fp, long len);
188 void realloc(long nsz, int nch, long len);
189 void realloc(long nsz, int nch);
190 void reserve(long nsz, int nch);
193 void iseek(int64_t ofs);
194 float *get_outp(int len);
195 int64_t put_inp(int len);
196 int write(const float *fp, long len);
198 int write(const double *dp, long len, int ch);
199 int write_packet(FFPacket &pkt);
201 FFAudioStream(FFMPEG *ffmpeg, AVStream *strm, int idx, int fidx);
202 virtual ~FFAudioStream();
203 int is_audio() { return 1; }
204 int is_video() { return 0; }
205 void init_swr(int ichs, int ifmt, int irate);
206 int get_samples(float *&samples, uint8_t **data, int len);
207 int load_history(uint8_t **data, int len);
208 int decode_frame(AVFrame *frame);
209 int encode_frame(AVFrame *frame);
210 int create_filter(const char *filter_spec);
212 IndexMarks *get_markers();
214 int encode_activate();
215 int64_t load_buffer(double ** const sp, int len);
216 int in_history(int64_t pos);
217 void reset_history();
218 int read(double *dp, long len, int ch);
220 int init_frame(AVFrame *frame);
221 int load(int64_t pos, int len);
222 int audio_seek(int64_t pos);
223 int encode(double **samples, int len);
227 int channel0, channels;
232 SwrContext *resample_context;
233 int swr_ichs, swr_ifmt, swr_irate;
239 class FFVideoConvert {
241 Preferences *preferences;
242 struct SwsContext *convert_ctx;
245 FFVideoConvert(Preferences *preferences) {
246 this->preferences = preferences;
247 convert_ctx = 0; sw_frame = 0;
250 if( convert_ctx ) sws_freeContext(convert_ctx);
251 if( sw_frame ) av_frame_free(&sw_frame);
254 static AVPixelFormat color_model_to_pix_fmt(int color_model);
255 static int pix_fmt_to_color_model(AVPixelFormat pix_fmt);
257 int convert_picture_vframe(VFrame *frame, AVFrame *ip);
258 int convert_picture_vframe(VFrame *frame, AVFrame *ip, AVFrame *ipic);
259 int convert_cmodel(VFrame *frame, AVFrame *ip);
260 int transfer_cmodel(VFrame *frame, AVFrame *ifp);
261 int convert_vframe_picture(VFrame *frame, AVFrame *op);
262 int convert_vframe_picture(VFrame *frame, AVFrame *op, AVFrame *opic);
263 int convert_pixfmt(VFrame *frame, AVFrame *op);
264 int transfer_pixfmt(VFrame *frame, AVFrame *ofp);
267 class FFVideoStream : public FFStream, public FFVideoConvert {
268 int write_packet(FFPacket &pkt);
270 FFVideoStream(FFMPEG *ffmpeg, AVStream *strm, int idx, int fidx);
271 virtual ~FFVideoStream();
272 int is_audio() { return 0; }
273 int is_video() { return 1; }
274 int decode_frame(AVFrame *frame);
275 AVHWDeviceType decode_hw_activate();
276 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,16,100)
277 int decode_hw_format(const AVCodec *decoder, AVHWDeviceType type);
279 int decode_hw_format(AVCodec *decoder, AVHWDeviceType type);
281 AVHWDeviceType encode_hw_activate(const char *hw_dev);
282 int encode_hw_write(FFrame *picture);
283 int encode_frame(AVFrame *frame);
284 int create_filter(const char *filter_spec);
286 IndexMarks *get_markers();
288 int init_frame(AVFrame *picture);
289 int load(VFrame *vframe, int64_t pos);
290 int probe(int64_t pos);
291 int video_seek(int64_t pos);
292 int encode(VFrame *vframe);
294 int convert_hw_frame(AVFrame *ifrm, AVFrame *ofrm);
295 int load_filter(AVFrame *frame);
296 double get_rotation_angle();
297 int flip(double theta);
301 int width, height, transpose;
307 int color_space, color_range;
308 struct SwsContext *fconvert_ctx;
316 const char *old_codec, *new_codec;
319 class FFCodecRemaps : public ArrayList<FFCodecRemap>
323 int add(const char *val);
324 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,16,100)
325 int update(AVCodecID &codec_id, const AVCodec *&decoder);
327 int update(AVCodecID &codec_id, AVCodec *&decoder);
331 // for get_initial_timecode auto deletes
335 avFrame() { frm = av_frame_alloc(); }
336 ~avFrame() { av_frame_free(&frm); }
337 operator AVFrame *() { return frm; }
338 AVFrame *operator ->() { return frm; }
345 av_init_packet(&pkt);
346 pkt.data = 0; pkt.size = 0;
348 ~avPacket() { av_packet_unref(&pkt); }
349 operator AVPacket *() { return &pkt; }
350 AVPacket *operator ->() { return &pkt; }
353 class avCodecContext {
354 AVCodecContext *avctx;
356 avCodecContext(AVCodecContext *ctx) { avctx = ctx; }
357 ~avCodecContext() { avcodec_free_context(&avctx); }
358 operator AVCodecContext *() { return avctx; }
359 AVCodecContext *operator ->() { return avctx; }
363 class FFMPEG : public Thread {
366 static void ff_lock(const char *cp=0) { fflock.lock(cp); }
367 static void ff_unlock() { fflock.unlock(); }
369 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,16,100)
370 int check_sample_rate(const AVCodec *codec, int sample_rate);
372 int check_sample_rate(AVCodec *codec, int sample_rate);
374 AVRational check_frame_rate(const AVRational *p, double frame_rate);
375 AVRational to_sample_aspect_ratio(Asset *asset);
376 AVRational to_time_base(int sample_rate);
377 static int get_fmt_score(AVSampleFormat dst_fmt, AVSampleFormat src_fmt);
378 static AVSampleFormat find_best_sample_fmt_of_list(
379 const AVSampleFormat *sample_fmts, AVSampleFormat src_fmt);
381 static void set_option_path(char *path, const char *fmt, ...);
382 static void get_option_path(char *path, const char *type, const char *spec);
383 static int get_format(char *format, const char *path, const char *spec);
384 static int get_codec(char *codec, const char *path, const char *spec);
385 static int scan_option_line(const char *cp,char *tag,char *val);
386 static int load_defaults(const char *path, const char *type,
387 char *codec, char *codec_options, int len);
388 static int can_render(const char *fformat, const char *type);
389 static int renders_audio(const char *fformat) { return can_render(fformat, "audio"); }
390 static int renders_video(const char *fformat) { return can_render(fformat, "video"); }
391 static int get_ff_option(const char *nm, const char *options, char *value);
392 static void scan_audio_options(Asset *asset, EDL *edl);
393 static void load_audio_options(Asset *asset, EDL *edl);
394 static void scan_video_options(Asset *asset, EDL *edl);
395 static void load_video_options(Asset *asset, EDL *edl);
396 static void scan_format_options(Asset *asset, EDL *edl);
397 static void load_format_options(Asset *asset, EDL *edl);
398 static void set_asset_format(Asset *asset, EDL *edl, const char *text);
399 int get_file_format();
400 static int get_encoder(const char *options, char *format, char *codec, char *bsfilter);
401 static int scan_encoder(const char *line, char *format, char *codec, char *bsfilter);
402 int read_options(const char *options, AVDictionary *&opts, int skip=0);
403 int scan_options(const char *options, AVDictionary *&opts, AVStream *st);
404 int read_options(FILE *fp, const char *options, AVDictionary *&opts);
405 int load_options(const char *options, AVDictionary *&opts);
406 static int load_options(const char *path, char *bfr, int len);
407 void set_loglevel(const char *ap);
408 static double to_secs(int64_t time, AVRational time_base);
409 int info(char *text, int len);
411 void put_cache_frame(VFrame *frame, int64_t position);
415 int init_decoder(const char *filename);
417 int init_encoder(const char *filename);
418 int open_encoder(const char *type, const char *spec);
421 int total_audio_channels();
422 int total_video_channels();
423 double get_initial_timecode(int data_type, int channel, double frame_rate);
425 int audio_seek(int ch, int64_t pos);
426 int video_probe(int64_t pos);
427 int video_seek(int layer, int64_t pos);
429 int decode(int chn, int64_t pos, double *samples, int len);
430 int decode(int layer, int64_t pos, VFrame *frame);
431 int decode_activate();
432 int encode(int stream, double **samples, int len);
433 int encode(int stream, VFrame *frame);
434 int encode_activate();
437 AVFormatContext *fmt_ctx;
438 ArrayList<FFAudioStream*> ffaudio;
439 ArrayList<FFVideoStream*> ffvideo;
442 char *opt_video_filter;
443 char *opt_audio_filter;
445 char *opt_video_decoder;
446 char *opt_audio_decoder;
447 FFCodecRemaps video_codec_remaps;
448 FFCodecRemaps audio_codec_remaps;
449 char file_format[BCTEXTLEN];
454 uint16_t st_idx, st_ch;
455 ffidx() { st_idx = st_ch = 0; }
456 ffidx(const ffidx &t) { st_idx = t.st_idx; st_ch = t.st_ch; }
457 ffidx(uint16_t fidx, uint16_t ch) { st_idx = fidx; st_ch = ch; }
460 ArrayList<ffidx> astrm_index;
461 ArrayList<ffidx> vstrm_index;
462 int mux_audio(FFrame *frm);
463 int mux_video(FFrame *frm);
465 Condition *flow_lock;
476 int decoding, encoding;
477 int has_audio, has_video;
478 int interlace_from_codec;
480 FFMPEG(FileBase *file_base=0);
482 AVCodecContext *activate_decoder(AVStream *st);
483 int scan(IndexState *index_state, int64_t *scan_position, int *canceled);
485 int ff_audio_stream(int channel) { return astrm_index[channel].st_idx; }
486 int ff_video_stream(int layer) { return vstrm_index[layer].st_idx; }
488 int ff_total_audio_channels();
489 int ff_total_astreams();
490 int ff_audio_channels(int stream);
491 int ff_sample_rate(int stream);
492 const char *ff_audio_format(int stream);
493 int ff_audio_pid(int stream);
494 int64_t ff_audio_samples(int stream);
495 int ff_audio_for_video(int vstream, int astream, int64_t &channels);
497 int ff_total_video_layers();
498 int ff_total_vstreams();
499 int ff_video_width(int stream);
500 int ff_video_height(int stream);
501 int ff_set_video_width(int stream, int width);
502 int ff_set_video_height(int stream, int height);
503 int ff_coded_width(int stream);
504 int ff_coded_height(int stream);
505 float ff_aspect_ratio(int stream);
506 int ff_color_range(int stream);
507 int ff_color_space(int stream);
508 int ff_interlace(int stream);
509 double ff_frame_rate(int stream);
510 const char *ff_video_codec(int stream);
511 int64_t ff_video_frames(int stream);
512 int ff_video_pid(int stream);
513 int ff_video_mpeg_color_range(int stream);
514 double ff_get_timecode(char *str, AVRational rate, double pos);
515 static double get_timecode(const char *path, int data_type, int channel, double rate);
518 const char *ff_hw_dev();
519 Preferences *ff_prefs();
520 void dump_context(AVCodecContext *ctx);
523 #endif /* FFMPEG_H */