ffmpeg versioning mods from Andrew
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / ffmpeg.h
1 #ifndef FFMPEG_H
2 #define FFMPEG_H
3
4 #include <stdio.h>
5 #include <stdint.h>
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <string.h>
9
10 #include "arraylist.h"
11 #include "asset.inc"
12 #include "bccmodels.h"
13 #include "bcwindowbase.inc"
14 #include "condition.h"
15 #include "cstrdup.h"
16 #include "edl.inc"
17 #include "linklist.h"
18 #include "ffmpeg.inc"
19 #include "filebase.inc"
20 #include "fileffmpeg.inc"
21 #include "indexstate.inc"
22 #include "mutex.h"
23 #include "preferences.inc"
24 #include "thread.h"
25 #include "vframe.inc"
26
27 extern "C" {
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"
33 #endif
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"
45 }
46
47 class FFPacket  {
48         AVPacket pkt;
49 public:
50         operator AVPacket*() { return &pkt; }
51         operator AVPacket&() { return pkt; }
52         AVPacket *operator ->() { return &pkt; }
53
54         void init();
55         void finit();
56         FFPacket() { init(); }
57         ~FFPacket() { finit(); }
58 };
59
60 class FFrame : public ListItem<FFrame> {
61         AVFrame *frm;
62         int init;
63 public:
64         int64_t position;
65         FFStream *fst;
66
67         FFrame(FFStream *fst);
68         ~FFrame();
69
70         operator AVFrame*() { return frm; }
71         operator AVFrame&() { return *frm; }
72         AVFrame *operator ->() { return frm; }
73
74         int initted() { return init; }
75         void queue(int64_t pos);
76         void dequeue();
77         void set_hw_frame(AVFrame *frame);
78 };
79
80 class FFStream {
81 public:
82         FFStream(FFMPEG *ffmpeg, AVStream *st, int fidx);
83         ~FFStream();
84         static void ff_lock(const char *cp=0);
85         static void ff_unlock();
86         void queue(FFrame *frm);
87         void dequeue(FFrame *frm);
88
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);
94 #else
95         virtual int decode_hw_format(AVCodec *decoder, AVHWDeviceType type);
96 #endif
97         virtual int write_packet(FFPacket &pkt);
98         int read_packet();
99         int seek(int64_t no, double rate);
100         int flush();
101         int decode(AVFrame *frame);
102         void load_markers(IndexMarks &marks, double rate);
103
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();
122
123         FFMPEG *ffmpeg;
124         AVStream *st;
125         AVFormatContext *fmt_ctx;
126         AVCodecContext *avctx;
127
128         AVFilterContext *filt_ctx;
129         int filt_id;
130         AVFilterContext *buffersrc_ctx;
131         AVFilterContext *buffersink_ctx;
132         AVFilterGraph *filter_graph;
133         AVFrame *frame, *fframe;
134         AVFrame *probe_frame;
135         AVBSFContext *bsfc;
136
137         FFPacket ipkt;
138         int need_packet, flushed;
139
140         int frm_count;
141         List<FFrame> frms;
142         Mutex *frm_lock;
143
144         int64_t nudge;
145         int64_t seek_pos, curr_pos;
146         int fidx;
147         int reading, writing;
148         int seeking, seeked, eof;
149
150         int hw_pixfmt;
151         AVBufferRef *hw_device_ctx;
152
153         FILE *stats_fp;
154         char *stats_filename;
155         char *stats_in;
156         int pass;
157
158         int st_eof() { return eof; }
159         void st_eof(int v) { eof = v; }
160 };
161
162 class FFAudioStream : public FFStream {
163         float *inp, *outp, *bfr, *lmt;
164         int64_t hpos, sz;
165         int nch;
166
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);
171         long used();
172         long avail();
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);
177         int zero(long len);
178         int write(const double *dp, long len, int ch);
179         int write_packet(FFPacket &pkt);
180 public:
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);
191         void load_markers();
192         IndexMarks *get_markers();
193
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);
199
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);
204         int drain();
205
206         int idx;
207         int channel0, channels;
208         int sample_rate;
209         int mbsz, frame_sz;
210         int64_t length;
211
212         SwrContext *resample_context;
213         int swr_ichs, swr_ifmt, swr_irate;
214         int aud_bfr_sz;
215         float *aud_bfr;
216 };
217
218
219 class FFVideoConvert {
220 public:
221         Preferences *preferences;
222         struct SwsContext *convert_ctx;
223         AVFrame *sw_frame;
224
225         FFVideoConvert(Preferences *preferences) {
226                 this->preferences = preferences;
227                 convert_ctx = 0; sw_frame = 0;
228         }
229         ~FFVideoConvert() {
230                 if( convert_ctx ) sws_freeContext(convert_ctx);
231                 if( sw_frame ) av_frame_free(&sw_frame);
232         }
233
234         static AVPixelFormat color_model_to_pix_fmt(int color_model);
235         static int pix_fmt_to_color_model(AVPixelFormat pix_fmt);
236
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);
245 };
246
247 class FFVideoStream : public FFStream, public FFVideoConvert {
248         int write_packet(FFPacket &pkt);
249 public:
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);
258 #else
259         int decode_hw_format(AVCodec *decoder, AVHWDeviceType type);
260 #endif
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);
265         void load_markers();
266         IndexMarks *get_markers();
267
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);
273         int drain();
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);
278
279         int idx;
280         double frame_rate;
281         int width, height, transpose;
282         int64_t length;
283         float aspect_ratio;
284
285         int interlaced;
286         int top_field_first;
287         int color_space, color_range;
288         struct SwsContext *fconvert_ctx;
289 };
290
291 class FFCodecRemap
292 {
293 public:
294         FFCodecRemap();
295         ~FFCodecRemap();
296         const char *old_codec, *new_codec;
297 };
298
299 class FFCodecRemaps : public ArrayList<FFCodecRemap>
300 {
301 public:
302         FFCodecRemaps() {}
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);
306 #else
307         int update(AVCodecID &codec_id, AVCodec *&decoder);
308 #endif
309 };
310
311 // for get_initial_timecode auto deletes
312 class avFrame {
313         AVFrame *frm;
314 public:
315         avFrame() { frm = av_frame_alloc(); }
316         ~avFrame() { av_frame_free(&frm); }
317         operator AVFrame *() { return frm; }
318         AVFrame *operator ->() { return frm; }
319 };
320
321 class avPacket {
322         AVPacket pkt;
323 public:
324         avPacket() {
325                 av_init_packet(&pkt);
326                 pkt.data = 0; pkt.size = 0;
327         }
328         ~avPacket() { av_packet_unref(&pkt); }
329         operator AVPacket *() { return &pkt; }
330         AVPacket *operator ->() { return &pkt; }
331 };
332
333 class avCodecContext {
334         AVCodecContext *avctx;
335 public:
336         avCodecContext(AVCodecContext *ctx) { avctx = ctx; }
337         ~avCodecContext() { avcodec_free_context(&avctx); }
338         operator AVCodecContext *() { return avctx; }
339         AVCodecContext *operator ->() { return avctx; }
340 };
341
342
343 class FFMPEG : public Thread {
344 public:
345         static Mutex fflock;
346         static void ff_lock(const char *cp=0) { fflock.lock(cp); }
347         static void ff_unlock() { fflock.unlock(); }
348
349 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,16,100)
350         int check_sample_rate(const AVCodec *codec, int sample_rate);
351 #else
352         int check_sample_rate(AVCodec *codec, int sample_rate);
353 #endif
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);
360
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);
390
391         void put_cache_frame(VFrame *frame, int64_t position);
392         int get_use_cache();
393         void purge_cache();
394
395         int init_decoder(const char *filename);
396         int open_decoder();
397         int init_encoder(const char *filename);
398         int open_encoder(const char *type, const char *spec);
399         int close_encoder();
400
401         int total_audio_channels();
402         int total_video_channels();
403         double get_initial_timecode(int data_type, int channel, double frame_rate);
404
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);
408
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();
415
416         FileBase *file_base;
417         AVFormatContext *fmt_ctx;
418         ArrayList<FFAudioStream*> ffaudio;
419         ArrayList<FFVideoStream*> ffvideo;
420         AVDictionary *opts;
421         double opt_duration;
422         char *opt_video_filter;
423         char *opt_audio_filter;
424         char *opt_hw_dev;
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];
430         int fflags;
431
432         class ffidx {
433         public:
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; }
438         };
439
440         ArrayList<ffidx> astrm_index;
441         ArrayList<ffidx> vstrm_index;
442         int mux_audio(FFrame *frm);
443         int mux_video(FFrame *frm);
444         Condition *mux_lock;
445         Condition *flow_lock;
446         int done, flow;
447
448         void start_muxer();
449         void stop_muxer();
450         void flow_off();
451         void flow_on();
452         void flow_ctl();
453         void mux();
454         void run();
455
456         int decoding, encoding;
457         int has_audio, has_video;
458         int interlace_from_codec;
459
460         FFMPEG(FileBase *file_base=0);
461         ~FFMPEG();
462         AVCodecContext *activate_decoder(AVStream *st);
463         int scan(IndexState *index_state, int64_t *scan_position, int *canceled);
464
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; }
467
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);
476
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);
496
497         int ff_cpus();
498         const char *ff_hw_dev();
499         Preferences *ff_prefs();
500         void dump_context(AVCodecContext *ctx);
501 };
502
503 #endif /* FFMPEG_H */