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