18 new shapewipe transitions from rafa, rework savefile/confirm for nested edl edits
[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 }
41
42 class FFPacket  {
43         AVPacket pkt;
44 public:
45         operator AVPacket*() { return &pkt; }
46         operator AVPacket&() { return pkt; }
47         AVPacket *operator ->() { return &pkt; }
48
49         void init();
50         void finit();
51         FFPacket() { init(); }
52         ~FFPacket() { finit(); }
53 };
54
55 class FFrame : public ListItem<FFrame> {
56         AVFrame *frm;
57         int init;
58 public:
59         int64_t position;
60         FFStream *fst;
61
62         FFrame(FFStream *fst);
63         ~FFrame();
64
65         operator AVFrame*() { return frm; }
66         operator AVFrame&() { return *frm; }
67         AVFrame *operator ->() { return frm; }
68
69         int initted() { return init; }
70         void queue(int64_t pos);
71         void dequeue();
72         void set_hw_frame(AVFrame *frame);
73 };
74
75 class FFStream {
76 public:
77         FFStream(FFMPEG *ffmpeg, AVStream *st, int fidx);
78         ~FFStream();
79         static void ff_lock(const char *cp=0);
80         static void ff_unlock();
81         void queue(FFrame *frm);
82         void dequeue(FFrame *frm);
83
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);
89         int read_packet();
90         int seek(int64_t no, double rate);
91         int flush();
92         int decode(AVFrame *frame);
93         void load_markers(IndexMarks &marks, double rate);
94
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();
112
113         FFMPEG *ffmpeg;
114         AVStream *st;
115         AVFormatContext *fmt_ctx;
116         AVCodecContext *avctx;
117
118         AVFilterContext *buffersink_ctx;
119         AVFilterContext *buffersrc_ctx;
120         AVFilterGraph *filter_graph;
121         AVFrame *frame, *fframe;
122         AVFrame *probe_frame;
123         AVBSFContext *bsfc;
124
125         FFPacket ipkt;
126         int need_packet, flushed;
127
128         int frm_count;
129         List<FFrame> frms;
130         Mutex *frm_lock;
131
132         int64_t nudge;
133         int64_t seek_pos, curr_pos;
134         int fidx;
135         int reading, writing;
136         int seeked, eof;
137
138         int hw_pixfmt;
139         AVBufferRef *hw_device_ctx;
140
141         FILE *stats_fp;
142         char *stats_filename;
143         char *stats_in;
144         int pass;
145
146         int st_eof() { return eof; }
147         void st_eof(int v) { eof = v; }
148 };
149
150 class FFAudioStream : public FFStream {
151         float *inp, *outp, *bfr, *lmt;
152         int64_t hpos, sz;
153         int nch;
154
155         int read(float *fp, long len);
156         void realloc(long nsz, int nch, long len);
157         void realloc(long nsz, int nch);
158         void reserve(long nsz, int nch);
159         long used();
160         long avail();
161         void iseek(int64_t ofs);
162         float *get_outp(int len);
163         int64_t put_inp(int len);
164         int write(const float *fp, long len);
165         int zero(long len);
166         int write(const double *dp, long len, int ch);
167         int write_packet(FFPacket &pkt);
168 public:
169         FFAudioStream(FFMPEG *ffmpeg, AVStream *strm, int idx, int fidx);
170         virtual ~FFAudioStream();
171         int is_audio() { return 1; }
172         int is_video() { return 0; }
173         void init_swr(int ichs, int ifmt, int irate);
174         int get_samples(float *&samples, uint8_t **data, int len);
175         int load_history(uint8_t **data, int len);
176         int decode_frame(AVFrame *frame);
177         int encode_frame(AVFrame *frame);
178         int create_filter(const char *filter_spec, AVCodecParameters *avpar);
179         void load_markers();
180         IndexMarks *get_markers();
181
182         int encode_activate();
183         int64_t load_buffer(double ** const sp, int len);
184         int in_history(int64_t pos);
185         void reset_history();
186         int read(double *dp, long len, int ch);
187
188         int init_frame(AVFrame *frame);
189         int load(int64_t pos, int len);
190         int audio_seek(int64_t pos);
191         int encode(double **samples, int len);
192         int drain();
193
194         int idx;
195         int channel0, channels;
196         int sample_rate;
197         int mbsz, frame_sz;
198         int64_t length;
199
200         SwrContext *resample_context;
201         int swr_ichs, swr_ifmt, swr_irate;
202         int aud_bfr_sz;
203         float *aud_bfr;
204 };
205
206
207 class FFVideoConvert {
208 public:
209         Preferences *preferences;
210         struct SwsContext *convert_ctx;
211         AVFrame *sw_frame;
212
213         FFVideoConvert(Preferences *preferences) {
214                 this->preferences = preferences;
215                 convert_ctx = 0; sw_frame = 0;
216         }
217         ~FFVideoConvert() {
218                 if( convert_ctx ) sws_freeContext(convert_ctx);
219                 if( sw_frame ) av_frame_free(&sw_frame);
220         }
221
222         static AVPixelFormat color_model_to_pix_fmt(int color_model);
223         static int pix_fmt_to_color_model(AVPixelFormat pix_fmt);
224
225         int convert_picture_vframe(VFrame *frame, AVFrame *ip);
226         int convert_picture_vframe(VFrame *frame, AVFrame *ip, AVFrame *ipic);
227         int convert_cmodel(VFrame *frame, AVFrame *ip);
228         int transfer_cmodel(VFrame *frame, AVFrame *ifp);
229         int convert_vframe_picture(VFrame *frame, AVFrame *op);
230         int convert_vframe_picture(VFrame *frame, AVFrame *op, AVFrame *opic);
231         int convert_pixfmt(VFrame *frame, AVFrame *op);
232         int transfer_pixfmt(VFrame *frame, AVFrame *ofp);
233 };
234
235 class FFVideoStream : public FFStream, public FFVideoConvert {
236         int write_packet(FFPacket &pkt);
237 public:
238         FFVideoStream(FFMPEG *ffmpeg, AVStream *strm, int idx, int fidx);
239         virtual ~FFVideoStream();
240         int is_audio() { return 0; }
241         int is_video() { return 1; }
242         int decode_frame(AVFrame *frame);
243         AVHWDeviceType decode_hw_activate();
244         int decode_hw_format(AVCodec *decoder, AVHWDeviceType type);
245         AVHWDeviceType encode_hw_activate(const char *hw_dev);
246         int encode_hw_write(FFrame *picture);
247         int encode_frame(AVFrame *frame);
248         int create_filter(const char *filter_spec, AVCodecParameters *avpar);
249         void load_markers();
250         IndexMarks *get_markers();
251
252         int init_frame(AVFrame *picture);
253         int load(VFrame *vframe, int64_t pos);
254         int video_seek(int64_t pos);
255         int encode(VFrame *vframe);
256         int drain();
257
258         int idx;
259         double frame_rate;
260         int width, height;
261         int64_t length;
262         float aspect_ratio;
263
264         int interlaced;
265         int top_field_first;
266         int color_space, color_range;
267 };
268
269 class FFCodecRemap
270 {
271 public:
272         FFCodecRemap();
273         ~FFCodecRemap();
274         const char *old_codec, *new_codec;
275 };
276
277 class FFCodecRemaps : public ArrayList<FFCodecRemap>
278 {
279 public:
280         FFCodecRemaps() {}
281         int add(const char *val);
282         int update(AVCodecID &codec_id, AVCodec *&decoder);
283 };
284
285 class FFMPEG : public Thread {
286 public:
287         static Mutex fflock;
288         static void ff_lock(const char *cp=0) { fflock.lock(cp); }
289         static void ff_unlock() { fflock.unlock(); }
290
291         int check_sample_rate(AVCodec *codec, int sample_rate);
292         AVRational check_frame_rate(AVCodec *codec, double frame_rate);
293         AVRational to_sample_aspect_ratio(Asset *asset);
294         AVRational to_time_base(int sample_rate);
295         static int get_fmt_score(AVSampleFormat dst_fmt, AVSampleFormat src_fmt);
296         static AVSampleFormat find_best_sample_fmt_of_list(
297                 const AVSampleFormat *sample_fmts, AVSampleFormat src_fmt);
298
299         static void set_option_path(char *path, const char *fmt, ...);
300         static void get_option_path(char *path, const char *type, const char *spec);
301         static int get_format(char *format, const char *path, const char *spec);
302         static int get_codec(char *codec, const char *path, const char *spec);
303         static int scan_option_line(const char *cp,char *tag,char *val);
304         static int load_defaults(const char *path, const char *type,
305                  char *codec, char *codec_options, int len);
306         static int can_render(const char *fformat, const char *type);
307         static int renders_audio(const char *fformat) { return can_render(fformat, "audio"); }
308         static int renders_video(const char *fformat) { return can_render(fformat, "video"); }
309         static int get_ff_option(const char *nm, const char *options, char *value);
310         static void scan_audio_options(Asset *asset, EDL *edl);
311         static void load_audio_options(Asset *asset, EDL *edl);
312         static void scan_video_options(Asset *asset, EDL *edl);
313         static void load_video_options(Asset *asset, EDL *edl);
314         static void scan_format_options(Asset *asset, EDL *edl);
315         static void load_format_options(Asset *asset, EDL *edl);
316         static void set_asset_format(Asset *asset, EDL *edl, const char *text);
317         int get_file_format();
318         static int get_encoder(const char *options, char *format, char *codec, char *bsfilter);
319         static int scan_encoder(const char *line, char *format, char *codec, char *bsfilter);
320         int read_options(const char *options, AVDictionary *&opts, int skip=0);
321         int scan_options(const char *options, AVDictionary *&opts, AVStream *st);
322         int read_options(FILE *fp, const char *options, AVDictionary *&opts);
323         int load_options(const char *options, AVDictionary *&opts);
324         static int load_options(const char *path, char *bfr, int len);
325         void set_loglevel(const char *ap);
326         static double to_secs(int64_t time, AVRational time_base);
327         int info(char *text, int len);
328
329         int init_decoder(const char *filename);
330         int open_decoder();
331         int init_encoder(const char *filename);
332         int open_encoder(const char *type, const char *spec);
333         int close_encoder();
334
335         int total_audio_channels();
336         int total_video_channels();
337
338         int audio_seek(int ch, int64_t pos);
339         int video_seek(int layer, int64_t pos);
340
341         int decode(int chn, int64_t pos, double *samples, int len);
342         int decode(int layer, int64_t pos, VFrame *frame);
343         int decode_activate();
344         int encode(int stream, double **samples, int len);
345         int encode(int stream, VFrame *frame);
346         int encode_activate();
347
348         FileBase *file_base;
349         AVFormatContext *fmt_ctx;
350         ArrayList<FFAudioStream*> ffaudio;
351         ArrayList<FFVideoStream*> ffvideo;
352         AVDictionary *opts;
353         double opt_duration;
354         char *opt_video_filter;
355         char *opt_audio_filter;
356         char *opt_hw_dev;
357         char *opt_video_decoder;
358         char *opt_audio_decoder;
359         FFCodecRemaps video_codec_remaps;
360         FFCodecRemaps audio_codec_remaps;
361         char file_format[BCTEXTLEN];
362         int fflags;
363
364         class ffidx {
365         public:
366                 uint16_t st_idx, st_ch;
367                 ffidx() { st_idx = st_ch = 0; }
368                 ffidx(const ffidx &t) { st_idx = t.st_idx;  st_ch = t.st_ch; }
369                 ffidx(uint16_t fidx, uint16_t ch) { st_idx = fidx; st_ch = ch; }
370         };
371
372         ArrayList<ffidx> astrm_index;
373         ArrayList<ffidx> vstrm_index;
374         int mux_audio(FFrame *frm);
375         int mux_video(FFrame *frm);
376         Condition *mux_lock;
377         Condition *flow_lock;
378         int done, flow;
379
380         void start_muxer();
381         void stop_muxer();
382         void flow_off();
383         void flow_on();
384         void flow_ctl();
385         void mux();
386         void run();
387
388         int decoding, encoding;
389         int has_audio, has_video;
390
391         FFMPEG(FileBase *file_base=0);
392         ~FFMPEG();
393         int scan(IndexState *index_state, int64_t *scan_position, int *canceled);
394
395         int ff_audio_stream(int channel) { return astrm_index[channel].st_idx; }
396         int ff_video_stream(int layer) { return vstrm_index[layer].st_idx; }
397
398         int ff_total_audio_channels();
399         int ff_total_astreams();
400         int ff_audio_channels(int stream);
401         int ff_sample_rate(int stream);
402         const char *ff_audio_format(int stream);
403         int ff_audio_pid(int stream);
404         int64_t ff_audio_samples(int stream);
405         int ff_audio_for_video(int vstream, int astream, int64_t &channels);
406
407         int ff_total_video_layers();
408         int ff_total_vstreams();
409         int ff_video_width(int stream);
410         int ff_video_height(int stream);
411         int ff_set_video_width(int stream, int width);
412         int ff_set_video_height(int stream, int height);
413         int ff_coded_width(int stream);
414         int ff_coded_height(int stream);
415         float ff_aspect_ratio(int stream);
416         int ff_color_range(int stream);
417         int ff_color_space(int stream);
418         double ff_frame_rate(int stream);
419         const char *ff_video_codec(int stream);
420         int64_t ff_video_frames(int stream);
421         int ff_video_pid(int stream);
422         int ff_video_mpeg_color_range(int stream);
423
424         int ff_cpus();
425         const char *ff_hw_dev();
426         Preferences *ff_prefs();
427         void dump_context(AVCodecContext *ctx);
428 };
429
430 #endif /* FFMPEG_H */