ed864b59663dda321523ef54ee68aec587831744
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / ffmpeg.h
1 /*
2  * CINELERRA
3  * Copyright (C) 2012-2014 Paolo Rampino
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published
7  * by the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA
19  */
20
21 #ifndef FFMPEG_H
22 #define FFMPEG_H
23
24 #include <stdio.h>
25 #include <stdint.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <string.h>
29
30 #include "arraylist.h"
31 #include "asset.inc"
32 #include "bccmodels.h"
33 #include "bcwindowbase.inc"
34 #include "condition.h"
35 #include "cstrdup.h"
36 #include "edl.inc"
37 #include "linklist.h"
38 #include "ffmpeg.inc"
39 #include "filebase.inc"
40 #include "fileffmpeg.inc"
41 #include "indexstate.inc"
42 #include "mutex.h"
43 #include "preferences.inc"
44 #include "thread.h"
45 #include "vframe.inc"
46
47 extern "C" {
48 #include "libavformat/avformat.h"
49 #include "libavformat/avio.h"
50 #include "libavcodec/avcodec.h"
51 #if LIBAVCODEC_VERSION_INT  >= AV_VERSION_INT(59,18,100)
52 #include "libavcodec/bsf.h"
53 #endif
54 #include "libavfilter/avfilter.h"
55 #include "libavutil/avutil.h"
56 #include "libavfilter/buffersrc.h"
57 #include "libavfilter/buffersink.h"
58 #include "libavutil/imgutils.h"
59 #include "libavutil/opt.h"
60 #include "libavutil/pixdesc.h"
61 #include "libswresample/swresample.h"
62 #include "libswscale/swscale.h"
63 #include "libavutil/parseutils.h"
64 #include "libavutil/timecode.h"
65 }
66
67 class FFPacket  {
68         AVPacket pkt;
69 public:
70         operator AVPacket*() { return &pkt; }
71         operator AVPacket&() { return pkt; }
72         AVPacket *operator ->() { return &pkt; }
73
74         void init();
75         void finit();
76         FFPacket() { init(); }
77         ~FFPacket() { finit(); }
78 };
79
80 class FFrame : public ListItem<FFrame> {
81         AVFrame *frm;
82         int init;
83 public:
84         int64_t position;
85         FFStream *fst;
86
87         FFrame(FFStream *fst);
88         ~FFrame();
89
90         operator AVFrame*() { return frm; }
91         operator AVFrame&() { return *frm; }
92         AVFrame *operator ->() { return frm; }
93
94         int initted() { return init; }
95         void queue(int64_t pos);
96         void dequeue();
97         void set_hw_frame(AVFrame *frame);
98 };
99
100 class FFStream {
101 public:
102         FFStream(FFMPEG *ffmpeg, AVStream *st, int fidx);
103         ~FFStream();
104         static void ff_lock(const char *cp=0);
105         static void ff_unlock();
106         void queue(FFrame *frm);
107         void dequeue(FFrame *frm);
108
109         virtual int encode_activate();
110         virtual int decode_activate();
111         virtual AVHWDeviceType decode_hw_activate();
112 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,16,100)
113         virtual int decode_hw_format(const AVCodec *decoder, AVHWDeviceType type);
114 #else
115         virtual int decode_hw_format(AVCodec *decoder, AVHWDeviceType type);
116 #endif
117         virtual int write_packet(FFPacket &pkt);
118         int read_packet();
119         int seek(int64_t no, double rate);
120         int flush();
121         int decode(AVFrame *frame);
122         void load_markers(IndexMarks &marks, double rate);
123
124         virtual int is_audio() = 0;
125         virtual int is_video() = 0;
126         virtual int decode_frame(AVFrame *frame) = 0;
127         virtual int encode_frame(AVFrame *frame) = 0;
128         virtual int init_frame(AVFrame *frame) = 0;
129         virtual int create_filter(const char *filter_spec) = 0;
130         virtual void load_markers() = 0;
131         virtual IndexMarks *get_markers() = 0;
132         int insert_filter(const char *name, const char *arg, const char *inst_name=0);
133         int config_filters(const char *filter_spec, AVFilterContext *fsrc);
134         virtual int load_filter(AVFrame *frame);
135         int read_filter(AVFrame *frame);
136         int read_frame(AVFrame *frame);
137         int open_stats_file();
138         int close_stats_file();
139         int read_stats_file();
140         int write_stats_file();
141         int init_stats_file();
142
143         FFMPEG *ffmpeg;
144         AVStream *st;
145         AVFormatContext *fmt_ctx;
146         AVCodecContext *avctx;
147
148         AVFilterContext *filt_ctx;
149         int filt_id;
150         AVFilterContext *buffersrc_ctx;
151         AVFilterContext *buffersink_ctx;
152         AVFilterGraph *filter_graph;
153         AVFrame *frame, *fframe;
154         AVFrame *probe_frame;
155         AVBSFContext *bsfc;
156
157         FFPacket ipkt;
158         int need_packet, flushed;
159
160         int frm_count;
161         List<FFrame> frms;
162         Mutex *frm_lock;
163
164         int64_t nudge;
165         int64_t seek_pos, curr_pos;
166         int fidx;
167         int reading, writing;
168         int seeking, seeked, eof;
169
170         int hw_pixfmt;
171         AVBufferRef *hw_device_ctx;
172
173         FILE *stats_fp;
174         char *stats_filename;
175         char *stats_in;
176         int pass;
177
178         int st_eof() { return eof; }
179         void st_eof(int v) { eof = v; }
180 };
181
182 class FFAudioStream : public FFStream {
183         float *inp, *outp, *bfr, *lmt;
184         int64_t hpos, sz;
185         int nch;
186
187         int read(float *fp, long len);
188         void realloc(long nsz, int nch, long len);
189         void realloc(long nsz, int nch);
190         void reserve(long nsz, int nch);
191         long used();
192         long avail();
193         void iseek(int64_t ofs);
194         float *get_outp(int len);
195         int64_t put_inp(int len);
196         int write(const float *fp, long len);
197         int zero(long len);
198         int write(const double *dp, long len, int ch);
199         int write_packet(FFPacket &pkt);
200 public:
201         FFAudioStream(FFMPEG *ffmpeg, AVStream *strm, int idx, int fidx);
202         virtual ~FFAudioStream();
203         int is_audio() { return 1; }
204         int is_video() { return 0; }
205         void init_swr(int ichs, int ifmt, int irate);
206         int get_samples(float *&samples, uint8_t **data, int len);
207         int load_history(uint8_t **data, int len);
208         int decode_frame(AVFrame *frame);
209         int encode_frame(AVFrame *frame);
210         int create_filter(const char *filter_spec);
211         void load_markers();
212         IndexMarks *get_markers();
213
214         int encode_activate();
215         int64_t load_buffer(double ** const sp, int len);
216         int in_history(int64_t pos);
217         void reset_history();
218         int read(double *dp, long len, int ch);
219
220         int init_frame(AVFrame *frame);
221         int load(int64_t pos, int len);
222         int audio_seek(int64_t pos);
223         int encode(double **samples, int len);
224         int drain();
225
226         int idx;
227         int channel0, channels;
228         int sample_rate;
229         int mbsz, frame_sz;
230         int64_t length;
231
232         SwrContext *resample_context;
233         int swr_ichs, swr_ifmt, swr_irate;
234         int aud_bfr_sz;
235         float *aud_bfr;
236 };
237
238
239 class FFVideoConvert {
240 public:
241         Preferences *preferences;
242         struct SwsContext *convert_ctx;
243         AVFrame *sw_frame;
244
245         FFVideoConvert(Preferences *preferences) {
246                 this->preferences = preferences;
247                 convert_ctx = 0; sw_frame = 0;
248         }
249         ~FFVideoConvert() {
250                 if( convert_ctx ) sws_freeContext(convert_ctx);
251                 if( sw_frame ) av_frame_free(&sw_frame);
252         }
253
254         static AVPixelFormat color_model_to_pix_fmt(int color_model);
255         static int pix_fmt_to_color_model(AVPixelFormat pix_fmt);
256
257         int convert_picture_vframe(VFrame *frame, AVFrame *ip);
258         int convert_picture_vframe(VFrame *frame, AVFrame *ip, AVFrame *ipic);
259         int convert_cmodel(VFrame *frame, AVFrame *ip);
260         int transfer_cmodel(VFrame *frame, AVFrame *ifp);
261         int convert_vframe_picture(VFrame *frame, AVFrame *op);
262         int convert_vframe_picture(VFrame *frame, AVFrame *op, AVFrame *opic);
263         int convert_pixfmt(VFrame *frame, AVFrame *op);
264         int transfer_pixfmt(VFrame *frame, AVFrame *ofp);
265 };
266
267 class FFVideoStream : public FFStream, public FFVideoConvert {
268         int write_packet(FFPacket &pkt);
269 public:
270         FFVideoStream(FFMPEG *ffmpeg, AVStream *strm, int idx, int fidx);
271         virtual ~FFVideoStream();
272         int is_audio() { return 0; }
273         int is_video() { return 1; }
274         int decode_frame(AVFrame *frame);
275         AVHWDeviceType decode_hw_activate();
276 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,16,100)
277         int decode_hw_format(const AVCodec *decoder, AVHWDeviceType type);
278 #else
279         int decode_hw_format(AVCodec *decoder, AVHWDeviceType type);
280 #endif
281         AVHWDeviceType encode_hw_activate(const char *hw_dev);
282         int encode_hw_write(FFrame *picture);
283         int encode_frame(AVFrame *frame);
284         int create_filter(const char *filter_spec);
285         void load_markers();
286         IndexMarks *get_markers();
287
288         int init_frame(AVFrame *picture);
289         int load(VFrame *vframe, int64_t pos);
290         int probe(int64_t pos);
291         int video_seek(int64_t pos);
292         int encode(VFrame *vframe);
293         int drain();
294         int convert_hw_frame(AVFrame *ifrm, AVFrame *ofrm);
295         int load_filter(AVFrame *frame);
296         double get_rotation_angle();
297         int flip(double theta);
298
299         int idx;
300         double frame_rate;
301         int width, height, transpose;
302         int64_t length;
303         float aspect_ratio;
304
305         int interlaced;
306         int top_field_first;
307         int color_space, color_range;
308         struct SwsContext *fconvert_ctx;
309 };
310
311 class FFCodecRemap
312 {
313 public:
314         FFCodecRemap();
315         ~FFCodecRemap();
316         const char *old_codec, *new_codec;
317 };
318
319 class FFCodecRemaps : public ArrayList<FFCodecRemap>
320 {
321 public:
322         FFCodecRemaps() {}
323         int add(const char *val);
324 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,16,100)
325         int update(AVCodecID &codec_id, const AVCodec *&decoder);
326 #else
327         int update(AVCodecID &codec_id, AVCodec *&decoder);
328 #endif
329 };
330
331 // for get_initial_timecode auto deletes
332 class avFrame {
333         AVFrame *frm;
334 public:
335         avFrame() { frm = av_frame_alloc(); }
336         ~avFrame() { av_frame_free(&frm); }
337         operator AVFrame *() { return frm; }
338         AVFrame *operator ->() { return frm; }
339 };
340
341 class avPacket {
342         AVPacket pkt;
343 public:
344         avPacket() {
345                 av_init_packet(&pkt);
346                 pkt.data = 0; pkt.size = 0;
347         }
348         ~avPacket() { av_packet_unref(&pkt); }
349         operator AVPacket *() { return &pkt; }
350         AVPacket *operator ->() { return &pkt; }
351 };
352
353 class avCodecContext {
354         AVCodecContext *avctx;
355 public:
356         avCodecContext(AVCodecContext *ctx) { avctx = ctx; }
357         ~avCodecContext() { avcodec_free_context(&avctx); }
358         operator AVCodecContext *() { return avctx; }
359         AVCodecContext *operator ->() { return avctx; }
360 };
361
362
363 class FFMPEG : public Thread {
364 public:
365         static Mutex fflock;
366         static void ff_lock(const char *cp=0) { fflock.lock(cp); }
367         static void ff_unlock() { fflock.unlock(); }
368
369 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,16,100)
370         int check_sample_rate(const AVCodec *codec, int sample_rate);
371 #else
372         int check_sample_rate(AVCodec *codec, int sample_rate);
373 #endif
374         AVRational check_frame_rate(const AVRational *p, double frame_rate);
375         AVRational to_sample_aspect_ratio(Asset *asset);
376         AVRational to_time_base(int sample_rate);
377         static int get_fmt_score(AVSampleFormat dst_fmt, AVSampleFormat src_fmt);
378         static AVSampleFormat find_best_sample_fmt_of_list(
379                 const AVSampleFormat *sample_fmts, AVSampleFormat src_fmt);
380
381         static void set_option_path(char *path, const char *fmt, ...);
382         static void get_option_path(char *path, const char *type, const char *spec);
383         static int get_format(char *format, const char *path, const char *spec);
384         static int get_codec(char *codec, const char *path, const char *spec);
385         static int scan_option_line(const char *cp,char *tag,char *val);
386         static int load_defaults(const char *path, const char *type,
387                  char *codec, char *codec_options, int len);
388         static int can_render(const char *fformat, const char *type);
389         static int renders_audio(const char *fformat) { return can_render(fformat, "audio"); }
390         static int renders_video(const char *fformat) { return can_render(fformat, "video"); }
391         static int get_ff_option(const char *nm, const char *options, char *value);
392         static void scan_audio_options(Asset *asset, EDL *edl);
393         static void load_audio_options(Asset *asset, EDL *edl);
394         static void scan_video_options(Asset *asset, EDL *edl);
395         static void load_video_options(Asset *asset, EDL *edl);
396         static void scan_format_options(Asset *asset, EDL *edl);
397         static void load_format_options(Asset *asset, EDL *edl);
398         static void set_asset_format(Asset *asset, EDL *edl, const char *text);
399         int get_file_format();
400         static int get_encoder(const char *options, char *format, char *codec, char *bsfilter);
401         static int scan_encoder(const char *line, char *format, char *codec, char *bsfilter);
402         int read_options(const char *options, AVDictionary *&opts, int skip=0);
403         int scan_options(const char *options, AVDictionary *&opts, AVStream *st);
404         int read_options(FILE *fp, const char *options, AVDictionary *&opts);
405         int load_options(const char *options, AVDictionary *&opts);
406         static int load_options(const char *path, char *bfr, int len);
407         void set_loglevel(const char *ap);
408         static double to_secs(int64_t time, AVRational time_base);
409         int info(char *text, int len);
410
411         void put_cache_frame(VFrame *frame, int64_t position);
412         int get_use_cache();
413         void purge_cache();
414
415         int init_decoder(const char *filename);
416         int open_decoder();
417         int init_encoder(const char *filename);
418         int open_encoder(const char *type, const char *spec);
419         int close_encoder();
420
421         int total_audio_channels();
422         int total_video_channels();
423         double get_initial_timecode(int data_type, int channel, double frame_rate);
424
425         int audio_seek(int ch, int64_t pos);
426         int video_probe(int64_t pos);
427         int video_seek(int layer, int64_t pos);
428
429         int decode(int chn, int64_t pos, double *samples, int len);
430         int decode(int layer, int64_t pos, VFrame *frame);
431         int decode_activate();
432         int encode(int stream, double **samples, int len);
433         int encode(int stream, VFrame *frame);
434         int encode_activate();
435
436         FileBase *file_base;
437         AVFormatContext *fmt_ctx;
438         ArrayList<FFAudioStream*> ffaudio;
439         ArrayList<FFVideoStream*> ffvideo;
440         AVDictionary *opts;
441         double opt_duration;
442         char *opt_video_filter;
443         char *opt_audio_filter;
444         char *opt_hw_dev;
445         char *opt_video_decoder;
446         char *opt_audio_decoder;
447         FFCodecRemaps video_codec_remaps;
448         FFCodecRemaps audio_codec_remaps;
449         char file_format[BCTEXTLEN];
450         int fflags;
451
452         class ffidx {
453         public:
454                 uint16_t st_idx, st_ch;
455                 ffidx() { st_idx = st_ch = 0; }
456                 ffidx(const ffidx &t) { st_idx = t.st_idx;  st_ch = t.st_ch; }
457                 ffidx(uint16_t fidx, uint16_t ch) { st_idx = fidx; st_ch = ch; }
458         };
459
460         ArrayList<ffidx> astrm_index;
461         ArrayList<ffidx> vstrm_index;
462         int mux_audio(FFrame *frm);
463         int mux_video(FFrame *frm);
464         Condition *mux_lock;
465         Condition *flow_lock;
466         int done, flow;
467
468         void start_muxer();
469         void stop_muxer();
470         void flow_off();
471         void flow_on();
472         void flow_ctl();
473         void mux();
474         void run();
475
476         int decoding, encoding;
477         int has_audio, has_video;
478         int interlace_from_codec;
479
480         FFMPEG(FileBase *file_base=0);
481         ~FFMPEG();
482         AVCodecContext *activate_decoder(AVStream *st);
483         int scan(IndexState *index_state, int64_t *scan_position, int *canceled);
484
485         int ff_audio_stream(int channel) { return astrm_index[channel].st_idx; }
486         int ff_video_stream(int layer) { return vstrm_index[layer].st_idx; }
487
488         int ff_total_audio_channels();
489         int ff_total_astreams();
490         int ff_audio_channels(int stream);
491         int ff_sample_rate(int stream);
492         const char *ff_audio_format(int stream);
493         int ff_audio_pid(int stream);
494         int64_t ff_audio_samples(int stream);
495         int ff_audio_for_video(int vstream, int astream, int64_t &channels);
496
497         int ff_total_video_layers();
498         int ff_total_vstreams();
499         int ff_video_width(int stream);
500         int ff_video_height(int stream);
501         int ff_set_video_width(int stream, int width);
502         int ff_set_video_height(int stream, int height);
503         int ff_coded_width(int stream);
504         int ff_coded_height(int stream);
505         float ff_aspect_ratio(int stream);
506         int ff_color_range(int stream);
507         int ff_color_space(int stream);
508         int ff_interlace(int stream);
509         double ff_frame_rate(int stream);
510         const char *ff_video_codec(int stream);
511         int64_t ff_video_frames(int stream);
512         int ff_video_pid(int stream);
513         int ff_video_mpeg_color_range(int stream);
514         double ff_get_timecode(char *str, AVRational rate, double pos);
515         static double get_timecode(const char *path, int data_type, int channel, double rate);
516
517         int ff_cpus();
518         const char *ff_hw_dev();
519         Preferences *ff_prefs();
520         void dump_context(AVCodecContext *ctx);
521 };
522
523 #endif /* FFMPEG_H */