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