remove whitespace at eol
[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         int read_packet();
84         int seek(int64_t no, double rate);
85         int write_packet(FFPacket &pkt);
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(AVPacket *pkt, AVFrame *frame, int &got_frame) = 0;
93         virtual int encode_frame(AVPacket *pkt, AVFrame *frame, int &got_frame) = 0;
94         virtual int init_frame(AVFrame *frame) = 0;
95         virtual int create_filter(const char *filter_spec,
96                 AVCodecContext *src_ctx, AVCodecContext *sink_ctx) = 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
104         FFMPEG *ffmpeg;
105         AVStream *st;
106         AVFormatContext *fmt_ctx;
107
108         AVFilterContext *buffersink_ctx;
109         AVFilterContext *buffersrc_ctx;
110         AVFilterGraph *filter_graph;
111         AVFrame *frame, *fframe;
112
113         class BSFilter {
114         public:
115                 AVBitStreamFilterContext *bsfc;
116                 const char *args;
117                 BSFilter(const char *bsf, const char *ap) {
118                         bsfc = av_bitstream_filter_init(bsf);
119                         args = ap ? cstrdup(ap) : 0;
120                 }
121                 ~BSFilter() {
122                         av_bitstream_filter_close(bsfc);
123                         delete [] args;
124                 }
125         };
126         void add_bsfilter(const char *bsf, const char *ap);
127         ArrayList<BSFilter *> bsfilter;
128         int bs_filter(AVPacket *pkt);
129
130         FFPacket ipkt;
131         int need_packet, flushed;
132
133         int frm_count;
134         List<FFrame> frms;
135         Mutex *frm_lock;
136
137         int64_t nudge;
138         int64_t seek_pos, curr_pos;
139         int fidx;
140         int reading, writing;
141         int seeked, eof;
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 public:
165         FFAudioStream(FFMPEG *ffmpeg, AVStream *strm, int idx, int fidx);
166         virtual ~FFAudioStream();
167         int is_audio() { return 1; }
168         int is_video() { return 0; }
169         int get_samples(float *&samples, uint8_t **data, int len);
170         int load_history(uint8_t **data, int len);
171         int decode_frame(AVPacket *pkt, AVFrame *frame, int &got_frame);
172         int encode_frame(AVPacket *pkt, AVFrame *frame, int &got_frame);
173         int create_filter(const char *filter_spec,
174                 AVCodecContext *src_ctx, AVCodecContext *sink_ctx);
175         void load_markers();
176         IndexMarks *get_markers();
177
178         int encode_activate();
179         int nb_samples();
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
190         int idx;
191         int channel0, channels;
192         int sample_rate;
193         int mbsz, frame_sz;
194         int64_t length;
195
196         SwrContext *resample_context;
197         int aud_bfr_sz;
198         float *aud_bfr;
199 };
200
201
202 class FFVideoConvert {
203 public:
204         struct SwsContext *convert_ctx;
205
206         FFVideoConvert() { convert_ctx = 0; }
207         ~FFVideoConvert() { if( convert_ctx ) sws_freeContext(convert_ctx); }
208
209         static AVPixelFormat color_model_to_pix_fmt(int color_model);
210         static int pix_fmt_to_color_model(AVPixelFormat pix_fmt);
211
212         int convert_picture_vframe(VFrame *frame,
213                 AVFrame *ip, AVPixelFormat ifmt, int iw, int ih);
214         int convert_cmodel(VFrame *frame_out,
215                 AVFrame *ip, AVPixelFormat ifmt, int iw, int ih);
216         int transfer_cmodel(VFrame *frame_in,  //defaults->metadata
217                 AVFrame *ifp, AVPixelFormat ifmt, int iw, int ih);
218         int convert_vframe_picture(VFrame *frame,
219                 AVFrame *op, AVPixelFormat ofmt, int ow, int oh);
220         int convert_pixfmt(VFrame *frame_in,
221                  AVFrame *op, AVPixelFormat ofmt, int ow, int oh);
222         int transfer_pixfmt(VFrame *frame_in,  //metadata->defaults
223                  AVFrame *ofp, AVPixelFormat ofmt, int ow, int oh);
224 };
225
226 class FFVideoStream : public FFStream, public FFVideoConvert {
227 public:
228         FFVideoStream(FFMPEG *ffmpeg, AVStream *strm, int idx, int fidx);
229         virtual ~FFVideoStream();
230         int is_audio() { return 0; }
231         int is_video() { return 1; }
232         int decode_frame(AVPacket *pkt, AVFrame *frame, int &got_frame);
233         int encode_frame(AVPacket *pkt, AVFrame *frame, int &got_frame);
234         int create_filter(const char *filter_spec,
235                 AVCodecContext *src_ctx, AVCodecContext *sink_ctx);
236         void load_markers();
237         IndexMarks *get_markers();
238
239         int init_frame(AVFrame *picture);
240         int load(VFrame *vframe, int64_t pos);
241         int video_seek(int64_t pos);
242         int encode(VFrame *vframe);
243
244         int idx;
245         double frame_rate;
246         int width, height;
247         int64_t length;
248         float aspect_ratio;
249
250         uint8_t *pkt_bfr;
251         int pkt_bfr_sz;
252         int64_t start_pts;
253 };
254
255 class FFMPEG : public Thread {
256 public:
257         static Mutex fflock;
258         static void ff_lock(const char *cp=0) { fflock.lock(cp); }
259         static void ff_unlock() { fflock.unlock(); }
260
261         int check_sample_rate(AVCodec *codec, int sample_rate);
262         AVRational check_frame_rate(AVCodec *codec, double frame_rate);
263         AVRational to_sample_aspect_ratio(Asset *asset);
264         AVRational to_time_base(int sample_rate);
265
266         static void set_option_path(char *path, const char *fmt, ...);
267         static void get_option_path(char *path, const char *type, const char *spec);
268         static int get_format(char *format, const char *path, const char *spec);
269         static int get_codec(char *codec, const char *path, const char *spec);
270         static int scan_option_line(char *cp,char *tag,char *val);
271         static int load_defaults(const char *path, const char *type,
272                  char *codec, char *codec_options, int len);
273         static void set_asset_format(Asset *asset, const char *text);
274         int get_file_format();
275         int get_encoder(const char *options,
276                 char *format, char *codec, char *bsfilter, char *bsargs);
277         int get_encoder(FILE *fp,
278                 char *format, char *codec, char *bsfilter, char *bsargs);
279         int read_options(const char *options, AVDictionary *&opts);
280         int scan_options(const char *options, AVDictionary *&opts, AVStream *st);
281         int read_options(FILE *fp, const char *options, AVDictionary *&opts);
282         int load_options(const char *options, AVDictionary *&opts);
283         static int load_options(const char *path, char *bfr, int len);
284         void set_loglevel(const char *ap);
285         static double to_secs(int64_t time, AVRational time_base);
286         int info(char *text, int len);
287
288         int init_decoder(const char *filename);
289         int open_decoder();
290         int init_encoder(const char *filename);
291         int open_encoder(const char *type, const char *spec);
292         int close_encoder();
293
294         int total_audio_channels();
295         int total_video_channels();
296
297         int audio_seek(int ch, int64_t pos);
298         int video_seek(int layer, int64_t pos);
299
300         int decode(int chn, int64_t pos, double *samples, int len);
301         int decode(int layer, int64_t pos, VFrame *frame);
302         int decode_activate();
303         int encode(int stream, double **samples, int len);
304         int encode(int stream, VFrame *frame);
305         int encode_activate();
306
307         FileBase *file_base;
308         AVFormatContext *fmt_ctx;
309         ArrayList<FFAudioStream*> ffaudio;
310         ArrayList<FFVideoStream*> ffvideo;
311         AVDictionary *opts;
312         double opt_duration;
313         char *opt_video_filter;
314         char *opt_audio_filter;
315         char file_format[BCTEXTLEN];
316
317         class ffidx {
318         public:
319                 uint16_t st_idx, st_ch;
320                 ffidx() { st_idx = st_ch = 0; }
321                 ffidx(const ffidx &t) { st_idx = t.st_idx;  st_ch = t.st_ch; }
322                 ffidx(uint16_t fidx, uint16_t ch) { st_idx = fidx; st_ch = ch; }
323         };
324
325         ArrayList<ffidx> astrm_index;
326         ArrayList<ffidx> vstrm_index;
327         int mux_audio(FFrame *frm);
328         int mux_video(FFrame *frm);
329         Condition *mux_lock;
330         Condition *flow_lock;
331         int done, flow;
332
333         void start_muxer();
334         void stop_muxer();
335         void flow_off();
336         void flow_on();
337         void flow_ctl();
338         void mux();
339         void run();
340
341         int decoding, encoding;
342         int has_audio, has_video;
343
344         FFMPEG(FileBase *file_base=0);
345         ~FFMPEG();
346         int scan(IndexState *index_state, int64_t *scan_position, int *canceled);
347
348         int ff_audio_stream(int channel) { return astrm_index[channel].st_idx; }
349         int ff_video_stream(int layer) { return vstrm_index[layer].st_idx; }
350
351         int ff_total_audio_channels();
352         int ff_total_astreams();
353         int ff_audio_channels(int stream);
354         int ff_sample_rate(int stream);
355         const char *ff_audio_format(int stream);
356         int ff_audio_pid(int stream);
357         int64_t ff_audio_samples(int stream);
358         int ff_audio_for_video(int vstream, int astream, int64_t &channels);
359
360         int ff_total_video_layers();
361         int ff_total_vstreams();
362         int ff_video_width(int stream);
363         int ff_video_height(int stream);
364         int ff_set_video_width(int stream, int width);
365         int ff_set_video_height(int stream, int height);
366         int ff_coded_width(int stream);
367         int ff_coded_height(int stream);
368         float ff_aspect_ratio(int stream);
369         double ff_frame_rate(int stream);
370         const char *ff_video_format(int stream);
371         int64_t ff_video_frames(int stream);
372         int ff_video_pid(int stream);
373
374         int ff_cpus();
375         void dump_context(AVCodecContext *ctx);
376 };
377
378 #endif /* FFMPEG_H */