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