rework rev vid caching, rework vid read_frame, rework framecache, tweak cached transi...
[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 video_seek(int64_t pos);
260         int encode(VFrame *vframe);
261         int drain();
262         int convert_hw_frame(AVFrame *ifrm, AVFrame *ofrm);
263         int load_filter(AVFrame *frame);
264         double get_rotation_angle();
265         int flip(double theta);
266
267         int idx;
268         double frame_rate;
269         int width, height, transpose;
270         int64_t length;
271         float aspect_ratio;
272
273         int interlaced;
274         int top_field_first;
275         int color_space, color_range;
276         struct SwsContext *fconvert_ctx;
277 };
278
279 class FFCodecRemap
280 {
281 public:
282         FFCodecRemap();
283         ~FFCodecRemap();
284         const char *old_codec, *new_codec;
285 };
286
287 class FFCodecRemaps : public ArrayList<FFCodecRemap>
288 {
289 public:
290         FFCodecRemaps() {}
291         int add(const char *val);
292         int update(AVCodecID &codec_id, AVCodec *&decoder);
293 };
294
295 // for get_initial_timecode auto deletes
296 class avFrame {
297         AVFrame *frm;
298 public:
299         avFrame() { frm = av_frame_alloc(); }
300         ~avFrame() { av_frame_free(&frm); }
301         operator AVFrame *() { return frm; }
302         AVFrame *operator ->() { return frm; }
303 };
304
305 class avPacket {
306         AVPacket pkt;
307 public:
308         avPacket() {
309                 av_init_packet(&pkt);
310                 pkt.data = 0; pkt.size = 0;
311         }
312         ~avPacket() { av_packet_unref(&pkt); }
313         operator AVPacket *() { return &pkt; }
314         AVPacket *operator ->() { return &pkt; }
315 };
316
317 class avCodecContext {
318         AVCodecContext *avctx;
319 public:
320         avCodecContext(AVCodecContext *ctx) { avctx = ctx; }
321         ~avCodecContext() { avcodec_free_context(&avctx); }
322         operator AVCodecContext *() { return avctx; }
323         AVCodecContext *operator ->() { return avctx; }
324 };
325
326
327 class FFMPEG : public Thread {
328 public:
329         static Mutex fflock;
330         static void ff_lock(const char *cp=0) { fflock.lock(cp); }
331         static void ff_unlock() { fflock.unlock(); }
332
333         int check_sample_rate(AVCodec *codec, int sample_rate);
334         AVRational check_frame_rate(const AVRational *p, double frame_rate);
335         AVRational to_sample_aspect_ratio(Asset *asset);
336         AVRational to_time_base(int sample_rate);
337         static int get_fmt_score(AVSampleFormat dst_fmt, AVSampleFormat src_fmt);
338         static AVSampleFormat find_best_sample_fmt_of_list(
339                 const AVSampleFormat *sample_fmts, AVSampleFormat src_fmt);
340
341         static void set_option_path(char *path, const char *fmt, ...);
342         static void get_option_path(char *path, const char *type, const char *spec);
343         static int get_format(char *format, const char *path, const char *spec);
344         static int get_codec(char *codec, const char *path, const char *spec);
345         static int scan_option_line(const char *cp,char *tag,char *val);
346         static int load_defaults(const char *path, const char *type,
347                  char *codec, char *codec_options, int len);
348         static int can_render(const char *fformat, const char *type);
349         static int renders_audio(const char *fformat) { return can_render(fformat, "audio"); }
350         static int renders_video(const char *fformat) { return can_render(fformat, "video"); }
351         static int get_ff_option(const char *nm, const char *options, char *value);
352         static void scan_audio_options(Asset *asset, EDL *edl);
353         static void load_audio_options(Asset *asset, EDL *edl);
354         static void scan_video_options(Asset *asset, EDL *edl);
355         static void load_video_options(Asset *asset, EDL *edl);
356         static void scan_format_options(Asset *asset, EDL *edl);
357         static void load_format_options(Asset *asset, EDL *edl);
358         static void set_asset_format(Asset *asset, EDL *edl, const char *text);
359         int get_file_format();
360         static int get_encoder(const char *options, char *format, char *codec, char *bsfilter);
361         static int scan_encoder(const char *line, char *format, char *codec, char *bsfilter);
362         int read_options(const char *options, AVDictionary *&opts, int skip=0);
363         int scan_options(const char *options, AVDictionary *&opts, AVStream *st);
364         int read_options(FILE *fp, const char *options, AVDictionary *&opts);
365         int load_options(const char *options, AVDictionary *&opts);
366         static int load_options(const char *path, char *bfr, int len);
367         void set_loglevel(const char *ap);
368         static double to_secs(int64_t time, AVRational time_base);
369         int info(char *text, int len);
370
371         void put_cache_frame(VFrame *frame, int64_t position);
372         int get_use_cache();
373         void purge_cache();
374
375         int init_decoder(const char *filename);
376         int open_decoder();
377         int init_encoder(const char *filename);
378         int open_encoder(const char *type, const char *spec);
379         int close_encoder();
380
381         int total_audio_channels();
382         int total_video_channels();
383         double get_initial_timecode(int data_type, int channel, double frame_rate);
384
385         int audio_seek(int ch, int64_t pos);
386         int video_seek(int layer, int64_t pos);
387
388         int decode(int chn, int64_t pos, double *samples, int len);
389         int decode(int layer, int64_t pos, VFrame *frame);
390         int decode_activate();
391         int encode(int stream, double **samples, int len);
392         int encode(int stream, VFrame *frame);
393         int encode_activate();
394
395         FileBase *file_base;
396         AVFormatContext *fmt_ctx;
397         ArrayList<FFAudioStream*> ffaudio;
398         ArrayList<FFVideoStream*> ffvideo;
399         AVDictionary *opts;
400         double opt_duration;
401         char *opt_video_filter;
402         char *opt_audio_filter;
403         char *opt_hw_dev;
404         char *opt_video_decoder;
405         char *opt_audio_decoder;
406         FFCodecRemaps video_codec_remaps;
407         FFCodecRemaps audio_codec_remaps;
408         char file_format[BCTEXTLEN];
409         int fflags;
410
411         class ffidx {
412         public:
413                 uint16_t st_idx, st_ch;
414                 ffidx() { st_idx = st_ch = 0; }
415                 ffidx(const ffidx &t) { st_idx = t.st_idx;  st_ch = t.st_ch; }
416                 ffidx(uint16_t fidx, uint16_t ch) { st_idx = fidx; st_ch = ch; }
417         };
418
419         ArrayList<ffidx> astrm_index;
420         ArrayList<ffidx> vstrm_index;
421         int mux_audio(FFrame *frm);
422         int mux_video(FFrame *frm);
423         Condition *mux_lock;
424         Condition *flow_lock;
425         int done, flow;
426
427         void start_muxer();
428         void stop_muxer();
429         void flow_off();
430         void flow_on();
431         void flow_ctl();
432         void mux();
433         void run();
434
435         int decoding, encoding;
436         int has_audio, has_video;
437
438         FFMPEG(FileBase *file_base=0);
439         ~FFMPEG();
440         AVCodecContext *activate_decoder(AVStream *st);
441         int scan(IndexState *index_state, int64_t *scan_position, int *canceled);
442
443         int ff_audio_stream(int channel) { return astrm_index[channel].st_idx; }
444         int ff_video_stream(int layer) { return vstrm_index[layer].st_idx; }
445
446         int ff_total_audio_channels();
447         int ff_total_astreams();
448         int ff_audio_channels(int stream);
449         int ff_sample_rate(int stream);
450         const char *ff_audio_format(int stream);
451         int ff_audio_pid(int stream);
452         int64_t ff_audio_samples(int stream);
453         int ff_audio_for_video(int vstream, int astream, int64_t &channels);
454
455         int ff_total_video_layers();
456         int ff_total_vstreams();
457         int ff_video_width(int stream);
458         int ff_video_height(int stream);
459         int ff_set_video_width(int stream, int width);
460         int ff_set_video_height(int stream, int height);
461         int ff_coded_width(int stream);
462         int ff_coded_height(int stream);
463         float ff_aspect_ratio(int stream);
464         int ff_color_range(int stream);
465         int ff_color_space(int stream);
466         double ff_frame_rate(int stream);
467         const char *ff_video_codec(int stream);
468         int64_t ff_video_frames(int stream);
469         int ff_video_pid(int stream);
470         int ff_video_mpeg_color_range(int stream);
471         double ff_get_timecode(char *str, AVRational rate, double pos);
472         static double get_timecode(const char *path, int data_type, int channel, double rate);
473
474         int ff_cpus();
475         const char *ff_hw_dev();
476         Preferences *ff_prefs();
477         void dump_context(AVCodecContext *ctx);
478 };
479
480 #endif /* FFMPEG_H */