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