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