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"
45 operator AVPacket*() { return &pkt; }
46 operator AVPacket&() { return pkt; }
47 AVPacket *operator ->() { return &pkt; }
51 FFPacket() { init(); }
52 ~FFPacket() { finit(); }
55 class FFrame : public ListItem<FFrame> {
62 FFrame(FFStream *fst);
65 operator AVFrame*() { return frm; }
66 operator AVFrame&() { return *frm; }
67 AVFrame *operator ->() { return frm; }
69 int initted() { return init; }
70 void queue(int64_t pos);
72 void set_hw_frame(AVFrame *frame);
77 FFStream(FFMPEG *ffmpeg, AVStream *st, int fidx);
79 static void ff_lock(const char *cp=0);
80 static void ff_unlock();
81 void queue(FFrame *frm);
82 void dequeue(FFrame *frm);
84 virtual int encode_activate();
85 virtual int decode_activate();
86 virtual AVHWDeviceType decode_hw_activate();
87 virtual int decode_hw_format(AVCodec *decoder, AVHWDeviceType type);
88 virtual int write_packet(FFPacket &pkt);
90 int seek(int64_t no, double rate);
92 int decode(AVFrame *frame);
93 void load_markers(IndexMarks &marks, double rate);
95 virtual int is_audio() = 0;
96 virtual int is_video() = 0;
97 virtual int decode_frame(AVFrame *frame) = 0;
98 virtual int encode_frame(AVFrame *frame) = 0;
99 virtual int init_frame(AVFrame *frame) = 0;
100 virtual int create_filter(const char *filter_spec, AVCodecParameters *avpar) = 0;
101 virtual void load_markers() = 0;
102 virtual IndexMarks *get_markers() = 0;
103 int create_filter(const char *filter_spec);
104 int load_filter(AVFrame *frame);
105 int read_filter(AVFrame *frame);
106 int read_frame(AVFrame *frame);
107 int open_stats_file();
108 int close_stats_file();
109 int read_stats_file();
110 int write_stats_file();
111 int init_stats_file();
115 AVFormatContext *fmt_ctx;
116 AVCodecContext *avctx;
118 AVFilterContext *buffersink_ctx;
119 AVFilterContext *buffersrc_ctx;
120 AVFilterGraph *filter_graph;
121 AVFrame *frame, *fframe;
125 int need_packet, flushed;
132 int64_t seek_pos, curr_pos;
134 int reading, writing;
138 AVBufferRef *hw_device_ctx;
141 char *stats_filename;
145 int st_eof() { return eof; }
146 void st_eof(int v) { eof = v; }
149 class FFAudioStream : public FFStream {
150 float *inp, *outp, *bfr, *lmt;
154 int read(float *fp, long len);
155 void realloc(long nsz, int nch, long len);
156 void realloc(long nsz, int nch);
157 void reserve(long nsz, int nch);
160 void iseek(int64_t ofs);
161 float *get_outp(int len);
162 int64_t put_inp(int len);
163 int write(const float *fp, long len);
165 int write(const double *dp, long len, int ch);
166 int write_packet(FFPacket &pkt);
168 FFAudioStream(FFMPEG *ffmpeg, AVStream *strm, int idx, int fidx);
169 virtual ~FFAudioStream();
170 int is_audio() { return 1; }
171 int is_video() { return 0; }
172 void init_swr(int ichs, int ifmt, int irate);
173 int get_samples(float *&samples, uint8_t **data, int len);
174 int load_history(uint8_t **data, int len);
175 int decode_frame(AVFrame *frame);
176 int encode_frame(AVFrame *frame);
177 int create_filter(const char *filter_spec, AVCodecParameters *avpar);
179 IndexMarks *get_markers();
181 int encode_activate();
182 int64_t load_buffer(double ** const sp, int len);
183 int in_history(int64_t pos);
184 void reset_history();
185 int read(double *dp, long len, int ch);
187 int init_frame(AVFrame *frame);
188 int load(int64_t pos, int len);
189 int audio_seek(int64_t pos);
190 int encode(double **samples, int len);
194 int channel0, channels;
199 SwrContext *resample_context;
200 int swr_ichs, swr_ifmt, swr_irate;
206 class FFVideoConvert {
208 Preferences *preferences;
209 struct SwsContext *convert_ctx;
212 FFVideoConvert(Preferences *preferences) {
213 this->preferences = preferences;
214 convert_ctx = 0; sw_frame = 0;
217 if( convert_ctx ) sws_freeContext(convert_ctx);
218 if( sw_frame ) av_frame_free(&sw_frame);
221 static AVPixelFormat color_model_to_pix_fmt(int color_model);
222 static int pix_fmt_to_color_model(AVPixelFormat pix_fmt);
224 int convert_picture_vframe(VFrame *frame, AVFrame *ip);
225 int convert_picture_vframe(VFrame *frame, AVFrame *ip, AVFrame *ipic);
226 int convert_cmodel(VFrame *frame, AVFrame *ip);
227 int transfer_cmodel(VFrame *frame, AVFrame *ifp);
228 int convert_vframe_picture(VFrame *frame, AVFrame *op);
229 int convert_vframe_picture(VFrame *frame, AVFrame *op, AVFrame *opic);
230 int convert_pixfmt(VFrame *frame, AVFrame *op);
231 int transfer_pixfmt(VFrame *frame, AVFrame *ofp);
234 class FFVideoStream : public FFStream, public FFVideoConvert {
235 int write_packet(FFPacket &pkt);
237 FFVideoStream(FFMPEG *ffmpeg, AVStream *strm, int idx, int fidx);
238 virtual ~FFVideoStream();
239 int is_audio() { return 0; }
240 int is_video() { return 1; }
241 int decode_frame(AVFrame *frame);
242 AVHWDeviceType decode_hw_activate();
243 int decode_hw_format(AVCodec *decoder, AVHWDeviceType type);
244 AVHWDeviceType encode_hw_activate(const char *hw_dev);
245 int encode_hw_write(FFrame *picture);
246 int encode_frame(AVFrame *frame);
247 int create_filter(const char *filter_spec, AVCodecParameters *avpar);
249 IndexMarks *get_markers();
251 int init_frame(AVFrame *picture);
252 int load(VFrame *vframe, int64_t pos);
253 int video_seek(int64_t pos);
254 int encode(VFrame *vframe);
272 const char *old_codec, *new_codec;
275 class FFCodecRemaps : public ArrayList<FFCodecRemap>
279 int add(const char *val);
280 int update(AVCodecID &codec_id, AVCodec *&decoder);
283 class FFMPEG : public Thread {
286 static void ff_lock(const char *cp=0) { fflock.lock(cp); }
287 static void ff_unlock() { fflock.unlock(); }
289 int check_sample_rate(AVCodec *codec, int sample_rate);
290 AVRational check_frame_rate(AVCodec *codec, double frame_rate);
291 AVRational to_sample_aspect_ratio(Asset *asset);
292 AVRational to_time_base(int sample_rate);
293 static int get_fmt_score(AVSampleFormat dst_fmt, AVSampleFormat src_fmt);
294 static AVSampleFormat find_best_sample_fmt_of_list(
295 const AVSampleFormat *sample_fmts, AVSampleFormat src_fmt);
297 static void set_option_path(char *path, const char *fmt, ...);
298 static void get_option_path(char *path, const char *type, const char *spec);
299 static int get_format(char *format, const char *path, const char *spec);
300 static int get_codec(char *codec, const char *path, const char *spec);
301 static int scan_option_line(const char *cp,char *tag,char *val);
302 static int load_defaults(const char *path, const char *type,
303 char *codec, char *codec_options, int len);
304 static int can_render(const char *fformat, const char *type);
305 static int renders_audio(const char *fformat) { return can_render(fformat, "audio"); }
306 static int renders_video(const char *fformat) { return can_render(fformat, "video"); }
307 static int get_ff_option(const char *nm, const char *options, char *value);
308 static void scan_audio_options(Asset *asset, EDL *edl);
309 static void load_audio_options(Asset *asset, EDL *edl);
310 static void scan_video_options(Asset *asset, EDL *edl);
311 static void load_video_options(Asset *asset, EDL *edl);
312 static void set_asset_format(Asset *asset, EDL *edl, const char *text);
313 int get_file_format();
314 static int get_encoder(const char *options, char *format, char *codec, char *bsfilter);
315 static int scan_encoder(const char *line, char *format, char *codec, char *bsfilter);
316 int read_options(const char *options, AVDictionary *&opts, int skip=0);
317 int scan_options(const char *options, AVDictionary *&opts, AVStream *st);
318 int read_options(FILE *fp, const char *options, AVDictionary *&opts);
319 int load_options(const char *options, AVDictionary *&opts);
320 static int load_options(const char *path, char *bfr, int len);
321 void set_loglevel(const char *ap);
322 static double to_secs(int64_t time, AVRational time_base);
323 int info(char *text, int len);
325 int init_decoder(const char *filename);
327 int init_encoder(const char *filename);
328 int open_encoder(const char *type, const char *spec);
331 int total_audio_channels();
332 int total_video_channels();
334 int audio_seek(int ch, int64_t pos);
335 int video_seek(int layer, int64_t pos);
337 int decode(int chn, int64_t pos, double *samples, int len);
338 int decode(int layer, int64_t pos, VFrame *frame);
339 int decode_activate();
340 int encode(int stream, double **samples, int len);
341 int encode(int stream, VFrame *frame);
342 int encode_activate();
345 AVFormatContext *fmt_ctx;
346 ArrayList<FFAudioStream*> ffaudio;
347 ArrayList<FFVideoStream*> ffvideo;
350 char *opt_video_filter;
351 char *opt_audio_filter;
353 char *opt_video_decoder;
354 char *opt_audio_decoder;
355 FFCodecRemaps video_codec_remaps;
356 FFCodecRemaps audio_codec_remaps;
357 char file_format[BCTEXTLEN];
362 uint16_t st_idx, st_ch;
363 ffidx() { st_idx = st_ch = 0; }
364 ffidx(const ffidx &t) { st_idx = t.st_idx; st_ch = t.st_ch; }
365 ffidx(uint16_t fidx, uint16_t ch) { st_idx = fidx; st_ch = ch; }
368 ArrayList<ffidx> astrm_index;
369 ArrayList<ffidx> vstrm_index;
370 int mux_audio(FFrame *frm);
371 int mux_video(FFrame *frm);
373 Condition *flow_lock;
384 int decoding, encoding;
385 int has_audio, has_video;
387 FFMPEG(FileBase *file_base=0);
389 int scan(IndexState *index_state, int64_t *scan_position, int *canceled);
391 int ff_audio_stream(int channel) { return astrm_index[channel].st_idx; }
392 int ff_video_stream(int layer) { return vstrm_index[layer].st_idx; }
394 int ff_total_audio_channels();
395 int ff_total_astreams();
396 int ff_audio_channels(int stream);
397 int ff_sample_rate(int stream);
398 const char *ff_audio_format(int stream);
399 int ff_audio_pid(int stream);
400 int64_t ff_audio_samples(int stream);
401 int ff_audio_for_video(int vstream, int astream, int64_t &channels);
403 int ff_total_video_layers();
404 int ff_total_vstreams();
405 int ff_video_width(int stream);
406 int ff_video_height(int stream);
407 int ff_set_video_width(int stream, int width);
408 int ff_set_video_height(int stream, int height);
409 int ff_coded_width(int stream);
410 int ff_coded_height(int stream);
411 float ff_aspect_ratio(int stream);
412 double ff_frame_rate(int stream);
413 const char *ff_video_format(int stream);
414 int64_t ff_video_frames(int stream);
415 int ff_video_pid(int stream);
416 int ff_video_mpeg_color_range(int stream);
419 const char *ff_hw_dev();
420 Preferences *ff_prefs();
421 void dump_context(AVCodecContext *ctx);
424 #endif /* FFMPEG_H */