Andrew improves asset of Interlace + some aspect ratio fixups
[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 #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"
42 }
43
44 class FFPacket  {
45         AVPacket pkt;
46 public:
47         operator AVPacket*() { return &pkt; }
48         operator AVPacket&() { return pkt; }
49         AVPacket *operator ->() { return &pkt; }
50
51         void init();
52         void finit();
53         FFPacket() { init(); }
54         ~FFPacket() { finit(); }
55 };
56
57 class FFrame : public ListItem<FFrame> {
58         AVFrame *frm;
59         int init;
60 public:
61         int64_t position;
62         FFStream *fst;
63
64         FFrame(FFStream *fst);
65         ~FFrame();
66
67         operator AVFrame*() { return frm; }
68         operator AVFrame&() { return *frm; }
69         AVFrame *operator ->() { return frm; }
70
71         int initted() { return init; }
72         void queue(int64_t pos);
73         void dequeue();
74         void set_hw_frame(AVFrame *frame);
75 };
76
77 class FFStream {
78 public:
79         FFStream(FFMPEG *ffmpeg, AVStream *st, int fidx);
80         ~FFStream();
81         static void ff_lock(const char *cp=0);
82         static void ff_unlock();
83         void queue(FFrame *frm);
84         void dequeue(FFrame *frm);
85
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);
91         int read_packet();
92         int seek(int64_t no, double rate);
93         int flush();
94         int decode(AVFrame *frame);
95         void load_markers(IndexMarks &marks, double rate);
96
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();
115
116         FFMPEG *ffmpeg;
117         AVStream *st;
118         AVFormatContext *fmt_ctx;
119         AVCodecContext *avctx;
120
121         AVFilterContext *filt_ctx;
122         int filt_id;
123         AVFilterContext *buffersrc_ctx;
124         AVFilterContext *buffersink_ctx;
125         AVFilterGraph *filter_graph;
126         AVFrame *frame, *fframe;
127         AVFrame *probe_frame;
128         AVBSFContext *bsfc;
129
130         FFPacket ipkt;
131         int need_packet, flushed;
132
133         int frm_count;
134         List<FFrame> frms;
135         Mutex *frm_lock;
136
137         int64_t nudge;
138         int64_t seek_pos, curr_pos;
139         int fidx;
140         int reading, writing;
141         int seeking, seeked, eof;
142
143         int hw_pixfmt;
144         AVBufferRef *hw_device_ctx;
145
146         FILE *stats_fp;
147         char *stats_filename;
148         char *stats_in;
149         int pass;
150
151         int st_eof() { return eof; }
152         void st_eof(int v) { eof = v; }
153 };
154
155 class FFAudioStream : public FFStream {
156         float *inp, *outp, *bfr, *lmt;
157         int64_t hpos, sz;
158         int nch;
159
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);
164         long used();
165         long avail();
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);
170         int zero(long len);
171         int write(const double *dp, long len, int ch);
172         int write_packet(FFPacket &pkt);
173 public:
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);
184         void load_markers();
185         IndexMarks *get_markers();
186
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);
192
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);
197         int drain();
198
199         int idx;
200         int channel0, channels;
201         int sample_rate;
202         int mbsz, frame_sz;
203         int64_t length;
204
205         SwrContext *resample_context;
206         int swr_ichs, swr_ifmt, swr_irate;
207         int aud_bfr_sz;
208         float *aud_bfr;
209 };
210
211
212 class FFVideoConvert {
213 public:
214         Preferences *preferences;
215         struct SwsContext *convert_ctx;
216         AVFrame *sw_frame;
217
218         FFVideoConvert(Preferences *preferences) {
219                 this->preferences = preferences;
220                 convert_ctx = 0; sw_frame = 0;
221         }
222         ~FFVideoConvert() {
223                 if( convert_ctx ) sws_freeContext(convert_ctx);
224                 if( sw_frame ) av_frame_free(&sw_frame);
225         }
226
227         static AVPixelFormat color_model_to_pix_fmt(int color_model);
228         static int pix_fmt_to_color_model(AVPixelFormat pix_fmt);
229
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);
238 };
239
240 class FFVideoStream : public FFStream, public FFVideoConvert {
241         int write_packet(FFPacket &pkt);
242 public:
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);
254         void load_markers();
255         IndexMarks *get_markers();
256
257         int init_frame(AVFrame *picture);
258         int load(VFrame *vframe, int64_t pos);
259         int probe(int64_t pos);
260         int video_seek(int64_t pos);
261         int encode(VFrame *vframe);
262         int drain();
263         int convert_hw_frame(AVFrame *ifrm, AVFrame *ofrm);
264         int load_filter(AVFrame *frame);
265         double get_rotation_angle();
266         int flip(double theta);
267
268         int idx;
269         double frame_rate;
270         int width, height, transpose;
271         int64_t length;
272         float aspect_ratio;
273
274         int interlaced;
275         int top_field_first;
276         int color_space, color_range;
277         struct SwsContext *fconvert_ctx;
278 };
279
280 class FFCodecRemap
281 {
282 public:
283         FFCodecRemap();
284         ~FFCodecRemap();
285         const char *old_codec, *new_codec;
286 };
287
288 class FFCodecRemaps : public ArrayList<FFCodecRemap>
289 {
290 public:
291         FFCodecRemaps() {}
292         int add(const char *val);
293         int update(AVCodecID &codec_id, AVCodec *&decoder);
294 };
295
296 // for get_initial_timecode auto deletes
297 class avFrame {
298         AVFrame *frm;
299 public:
300         avFrame() { frm = av_frame_alloc(); }
301         ~avFrame() { av_frame_free(&frm); }
302         operator AVFrame *() { return frm; }
303         AVFrame *operator ->() { return frm; }
304 };
305
306 class avPacket {
307         AVPacket pkt;
308 public:
309         avPacket() {
310                 av_init_packet(&pkt);
311                 pkt.data = 0; pkt.size = 0;
312         }
313         ~avPacket() { av_packet_unref(&pkt); }
314         operator AVPacket *() { return &pkt; }
315         AVPacket *operator ->() { return &pkt; }
316 };
317
318 class avCodecContext {
319         AVCodecContext *avctx;
320 public:
321         avCodecContext(AVCodecContext *ctx) { avctx = ctx; }
322         ~avCodecContext() { avcodec_free_context(&avctx); }
323         operator AVCodecContext *() { return avctx; }
324         AVCodecContext *operator ->() { return avctx; }
325 };
326
327
328 class FFMPEG : public Thread {
329 public:
330         static Mutex fflock;
331         static void ff_lock(const char *cp=0) { fflock.lock(cp); }
332         static void ff_unlock() { fflock.unlock(); }
333
334         int check_sample_rate(AVCodec *codec, int sample_rate);
335         AVRational check_frame_rate(const AVRational *p, double frame_rate);
336         AVRational to_sample_aspect_ratio(Asset *asset);
337         AVRational to_time_base(int sample_rate);
338         static int get_fmt_score(AVSampleFormat dst_fmt, AVSampleFormat src_fmt);
339         static AVSampleFormat find_best_sample_fmt_of_list(
340                 const AVSampleFormat *sample_fmts, AVSampleFormat src_fmt);
341
342         static void set_option_path(char *path, const char *fmt, ...);
343         static void get_option_path(char *path, const char *type, const char *spec);
344         static int get_format(char *format, const char *path, const char *spec);
345         static int get_codec(char *codec, const char *path, const char *spec);
346         static int scan_option_line(const char *cp,char *tag,char *val);
347         static int load_defaults(const char *path, const char *type,
348                  char *codec, char *codec_options, int len);
349         static int can_render(const char *fformat, const char *type);
350         static int renders_audio(const char *fformat) { return can_render(fformat, "audio"); }
351         static int renders_video(const char *fformat) { return can_render(fformat, "video"); }
352         static int get_ff_option(const char *nm, const char *options, char *value);
353         static void scan_audio_options(Asset *asset, EDL *edl);
354         static void load_audio_options(Asset *asset, EDL *edl);
355         static void scan_video_options(Asset *asset, EDL *edl);
356         static void load_video_options(Asset *asset, EDL *edl);
357         static void scan_format_options(Asset *asset, EDL *edl);
358         static void load_format_options(Asset *asset, EDL *edl);
359         static void set_asset_format(Asset *asset, EDL *edl, const char *text);
360         int get_file_format();
361         static int get_encoder(const char *options, char *format, char *codec, char *bsfilter);
362         static int scan_encoder(const char *line, char *format, char *codec, char *bsfilter);
363         int read_options(const char *options, AVDictionary *&opts, int skip=0);
364         int scan_options(const char *options, AVDictionary *&opts, AVStream *st);
365         int read_options(FILE *fp, const char *options, AVDictionary *&opts);
366         int load_options(const char *options, AVDictionary *&opts);
367         static int load_options(const char *path, char *bfr, int len);
368         void set_loglevel(const char *ap);
369         static double to_secs(int64_t time, AVRational time_base);
370         int info(char *text, int len);
371
372         void put_cache_frame(VFrame *frame, int64_t position);
373         int get_use_cache();
374         void purge_cache();
375
376         int init_decoder(const char *filename);
377         int open_decoder();
378         int init_encoder(const char *filename);
379         int open_encoder(const char *type, const char *spec);
380         int close_encoder();
381
382         int total_audio_channels();
383         int total_video_channels();
384         double get_initial_timecode(int data_type, int channel, double frame_rate);
385
386         int audio_seek(int ch, int64_t pos);
387         int video_probe(int64_t pos);
388         int video_seek(int layer, int64_t pos);
389
390         int decode(int chn, int64_t pos, double *samples, int len);
391         int decode(int layer, int64_t pos, VFrame *frame);
392         int decode_activate();
393         int encode(int stream, double **samples, int len);
394         int encode(int stream, VFrame *frame);
395         int encode_activate();
396
397         FileBase *file_base;
398         AVFormatContext *fmt_ctx;
399         ArrayList<FFAudioStream*> ffaudio;
400         ArrayList<FFVideoStream*> ffvideo;
401         AVDictionary *opts;
402         double opt_duration;
403         char *opt_video_filter;
404         char *opt_audio_filter;
405         char *opt_hw_dev;
406         char *opt_video_decoder;
407         char *opt_audio_decoder;
408         FFCodecRemaps video_codec_remaps;
409         FFCodecRemaps audio_codec_remaps;
410         char file_format[BCTEXTLEN];
411         int fflags;
412
413         class ffidx {
414         public:
415                 uint16_t st_idx, st_ch;
416                 ffidx() { st_idx = st_ch = 0; }
417                 ffidx(const ffidx &t) { st_idx = t.st_idx;  st_ch = t.st_ch; }
418                 ffidx(uint16_t fidx, uint16_t ch) { st_idx = fidx; st_ch = ch; }
419         };
420
421         ArrayList<ffidx> astrm_index;
422         ArrayList<ffidx> vstrm_index;
423         int mux_audio(FFrame *frm);
424         int mux_video(FFrame *frm);
425         Condition *mux_lock;
426         Condition *flow_lock;
427         int done, flow;
428
429         void start_muxer();
430         void stop_muxer();
431         void flow_off();
432         void flow_on();
433         void flow_ctl();
434         void mux();
435         void run();
436
437         int decoding, encoding;
438         int has_audio, has_video;
439         int interlace_from_codec;
440
441         FFMPEG(FileBase *file_base=0);
442         ~FFMPEG();
443         AVCodecContext *activate_decoder(AVStream *st);
444         int scan(IndexState *index_state, int64_t *scan_position, int *canceled);
445
446         int ff_audio_stream(int channel) { return astrm_index[channel].st_idx; }
447         int ff_video_stream(int layer) { return vstrm_index[layer].st_idx; }
448
449         int ff_total_audio_channels();
450         int ff_total_astreams();
451         int ff_audio_channels(int stream);
452         int ff_sample_rate(int stream);
453         const char *ff_audio_format(int stream);
454         int ff_audio_pid(int stream);
455         int64_t ff_audio_samples(int stream);
456         int ff_audio_for_video(int vstream, int astream, int64_t &channels);
457
458         int ff_total_video_layers();
459         int ff_total_vstreams();
460         int ff_video_width(int stream);
461         int ff_video_height(int stream);
462         int ff_set_video_width(int stream, int width);
463         int ff_set_video_height(int stream, int height);
464         int ff_coded_width(int stream);
465         int ff_coded_height(int stream);
466         float ff_aspect_ratio(int stream);
467         int ff_color_range(int stream);
468         int ff_color_space(int stream);
469         int ff_interlace(int stream);
470         double ff_frame_rate(int stream);
471         const char *ff_video_codec(int stream);
472         int64_t ff_video_frames(int stream);
473         int ff_video_pid(int stream);
474         int ff_video_mpeg_color_range(int stream);
475         double ff_get_timecode(char *str, AVRational rate, double pos);
476         static double get_timecode(const char *path, int data_type, int channel, double rate);
477
478         int ff_cpus();
479         const char *ff_hw_dev();
480         Preferences *ff_prefs();
481         void dump_context(AVCodecContext *ctx);
482 };
483
484 #endif /* FFMPEG_H */