Merge CV, ver=5.1; ops/methods from HV, and interface from CV where possible
[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         FFPacket();
44         ~FFPacket();
45         void init();
46         operator AVPacket*() { return &pkt; }
47         operator AVPacket&() { return pkt; }
48         AVPacket *operator ->() { return &pkt; }
49 };
50
51 class FFrame : public ListItem<FFrame> {
52         AVFrame *frm;
53         int init;
54 public:
55         int64_t position;
56         FFStream *fst;
57
58         FFrame(FFStream *fst);
59         ~FFrame();
60
61         operator AVFrame*() { return frm; }
62         operator AVFrame&() { return *frm; }
63         AVFrame *operator ->() { return frm; }
64
65         int initted() { return init; }
66         void queue(int64_t pos);
67         void dequeue();
68 };
69
70 class FFStream {
71 public:
72         FFStream(FFMPEG *ffmpeg, AVStream *st, int fidx);
73         ~FFStream();
74         static void ff_lock(const char *cp=0);
75         static void ff_unlock();
76         void queue(FFrame *frm);
77         void dequeue(FFrame *frm);
78
79         virtual int encode_activate();
80         virtual int decode_activate();
81         int read_packet();
82         int seek(int64_t no, double rate);
83         int write_packet(FFPacket &pkt);
84         int flush();
85         int decode(AVFrame *frame);
86         void load_markers(IndexMarks &marks, double rate);
87
88         virtual int is_audio() = 0;
89         virtual int is_video() = 0;
90         virtual int decode_frame(AVPacket *pkt, AVFrame *frame, int &got_frame) = 0;
91         virtual int encode_frame(AVPacket *pkt, AVFrame *frame, int &got_frame) = 0;
92         virtual int init_frame(AVFrame *frame) = 0;
93         virtual int create_filter(const char *filter_spec,
94                 AVCodecContext *src_ctx, AVCodecContext *sink_ctx) = 0;
95         virtual void load_markers() = 0;
96         int create_filter(const char *filter_spec);
97         int load_filter(AVFrame *frame);
98         int read_filter(AVFrame *frame);
99         int read_frame(AVFrame *frame);
100
101         FFMPEG *ffmpeg;
102         AVStream *st;
103         AVFormatContext *fmt_ctx;
104
105         AVFilterContext *buffersink_ctx;
106         AVFilterContext *buffersrc_ctx;
107         AVFilterGraph *filter_graph;
108         AVFrame *frame, *fframe;
109
110         class BSFilter {
111         public:
112                 AVBitStreamFilterContext *bsfc;
113                 const char *args;
114                 BSFilter(const char *bsf, const char *ap) {
115                         bsfc = av_bitstream_filter_init(bsf);
116                         args = ap ? cstrdup(ap) : 0;
117                 }
118                 ~BSFilter() {
119                         av_bitstream_filter_close(bsfc);
120                         delete [] args;
121                 }
122         };
123         void add_bsfilter(const char *bsf, const char *ap);
124         ArrayList<BSFilter *> bsfilter;
125         int bs_filter(AVPacket *pkt);
126
127         FFPacket ipkt;
128         int need_packet, flushed;
129
130         int frm_count;
131         List<FFrame> frms;
132         Mutex *frm_lock;
133         IndexMarks *index_markers;
134
135         int64_t nudge;
136         int64_t seek_pos, curr_pos;
137         int fidx;
138         int reading, writing;
139         int seeked, eof;
140
141         int st_eof() { return eof; }
142         void st_eof(int v) { eof = v; }
143 };
144
145 class FFAudioStream : public FFStream {
146         float *inp, *outp, *bfr, *lmt;
147         int64_t hpos, sz;
148         int nch;
149
150         int read(float *fp, long len);
151         void realloc(long nsz, int nch, long len);
152         void realloc(long nsz, int nch);
153         void reserve(long nsz, int nch);
154         long used();
155         long avail();
156         void iseek(int64_t ofs);
157         float *get_outp(int len);
158         int64_t put_inp(int len);
159         int write(const float *fp, long len);
160         int zero(long len);
161         int write(const double *dp, long len, int ch);
162 public:
163         FFAudioStream(FFMPEG *ffmpeg, AVStream *strm, int idx, int fidx);
164         virtual ~FFAudioStream();
165         int is_audio() { return 1; }
166         int is_video() { return 0; }
167         int get_samples(float *&samples, uint8_t **data, int len);
168         int load_history(uint8_t **data, int len);
169         int decode_frame(AVPacket *pkt, AVFrame *frame, int &got_frame);
170         int encode_frame(AVPacket *pkt, AVFrame *frame, int &got_frame);
171         int create_filter(const char *filter_spec,
172                 AVCodecContext *src_ctx, AVCodecContext *sink_ctx);
173         void load_markers();
174
175         int encode_activate();
176         int nb_samples();
177         int64_t load_buffer(double ** const sp, int len);
178         int in_history(int64_t pos);
179         void reset_history();
180         int read(double *dp, long len, int ch);
181
182         int init_frame(AVFrame *frame);
183         int load(int64_t pos, int len);
184         int audio_seek(int64_t pos);
185         int encode(double **samples, int len);
186
187         int idx;
188         int channel0, channels;
189         int sample_rate;
190         int mbsz, frame_sz;
191         int64_t length;
192
193         SwrContext *resample_context;
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,
210                 AVFrame *ip, AVPixelFormat ifmt, int iw, int ih);
211         int convert_cmodel(VFrame *frame_out,
212                 AVFrame *ip, AVPixelFormat ifmt, int iw, int ih);
213         int transfer_cmodel(VFrame *frame_in,  //defaults->metadata
214                 AVFrame *ifp, AVPixelFormat ifmt, int iw, int ih);
215         int convert_vframe_picture(VFrame *frame,
216                 AVFrame *op, AVPixelFormat ofmt, int ow, int oh);
217         int convert_pixfmt(VFrame *frame_in,
218                  AVFrame *op, AVPixelFormat ofmt, int ow, int oh);
219         int transfer_pixfmt(VFrame *frame_in,  //metadata->defaults
220                  AVFrame *ofp, AVPixelFormat ofmt, int ow, int oh);
221 };
222
223 class FFVideoStream : public FFStream, public FFVideoConvert {
224 public:
225         FFVideoStream(FFMPEG *ffmpeg, AVStream *strm, int idx, int fidx);
226         virtual ~FFVideoStream();
227         int is_audio() { return 0; }
228         int is_video() { return 1; }
229         int decode_frame(AVPacket *pkt, AVFrame *frame, int &got_frame);
230         int encode_frame(AVPacket *pkt, AVFrame *frame, int &got_frame);
231         int create_filter(const char *filter_spec,
232                 AVCodecContext *src_ctx, AVCodecContext *sink_ctx);
233         void load_markers();
234
235         int init_frame(AVFrame *picture);
236         int load(VFrame *vframe, int64_t pos);
237         int video_seek(int64_t pos);
238         int encode(VFrame *vframe);
239
240         int idx;
241         double frame_rate;
242         int width, height;
243         int64_t length;
244         float aspect_ratio;
245
246         uint8_t *pkt_bfr;
247         int pkt_bfr_sz;
248         int64_t start_pts;
249 };
250
251 class FFMPEG : public Thread {
252 public:
253         static Mutex fflock;
254         static void ff_lock(const char *cp=0) { fflock.lock(cp); }
255         static void ff_unlock() { fflock.unlock(); }
256
257         int check_sample_rate(AVCodec *codec, int sample_rate);
258         AVRational check_frame_rate(AVCodec *codec, double frame_rate);
259         AVRational to_sample_aspect_ratio(Asset *asset);
260         AVRational to_time_base(int sample_rate);
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, char *spec);
265         static int scan_option_line(char *cp,char *tag,char *val);
266         int get_file_format();
267         int get_encoder(const char *options,
268                 char *format, char *codec, char *bsfilter, char *bsargs);
269         int get_encoder(FILE *fp,
270                 char *format, char *codec, char *bsfilter, char *bsargs);
271         int read_options(const char *options, AVDictionary *&opts);
272         int scan_options(const char *options, AVDictionary *&opts, AVStream *st);
273         int read_options(FILE *fp, const char *options, AVDictionary *&opts);
274         int load_options(const char *options, AVDictionary *&opts);
275         static int load_options(const char *path, char *bfr, int len);
276         void set_loglevel(const char *ap);
277         static double to_secs(int64_t time, AVRational time_base);
278         int info(char *text, int len);
279
280         int init_decoder(const char *filename);
281         int open_decoder();
282         int init_encoder(const char *filename);
283         int open_encoder(const char *type, const char *spec);
284         int close_encoder();
285
286         int total_audio_channels();
287         int total_video_channels();
288
289         int audio_seek(int ch, int64_t pos);
290         int video_seek(int layer, int64_t pos);
291
292         int decode(int chn, int64_t pos, double *samples, int len);
293         int decode(int layer, int64_t pos, VFrame *frame);
294         int decode_activate();
295         int encode(int stream, double **samples, int len);
296         int encode(int stream, VFrame *frame);
297         int encode_activate();
298
299         FileBase *file_base;
300         AVFormatContext *fmt_ctx;
301         ArrayList<FFAudioStream*> ffaudio;
302         ArrayList<FFVideoStream*> ffvideo;
303         AVDictionary *opts;
304         double opt_duration;
305         char *opt_video_filter;
306         char *opt_audio_filter;
307         char file_format[BCTEXTLEN];
308
309         class ffidx {
310         public:
311                 uint16_t st_idx, st_ch;
312                 ffidx() { st_idx = st_ch = 0; }
313                 ffidx(const ffidx &t) { st_idx = t.st_idx;  st_ch = t.st_ch; }
314                 ffidx(uint16_t fidx, uint16_t ch) { st_idx = fidx; st_ch = ch; }
315         };
316
317         ArrayList<ffidx> astrm_index;
318         ArrayList<ffidx> vstrm_index;
319         int mux_audio(FFrame *frm);
320         int mux_video(FFrame *frm);
321         Condition *mux_lock;
322         Condition *flow_lock;
323         int done, flow;
324         
325         void start_muxer();
326         void stop_muxer();
327         void flow_off();
328         void flow_on();
329         void flow_ctl();
330         void mux();
331         void run();
332
333         int decoding, encoding;
334         int has_audio, has_video;
335
336         FFMPEG(FileBase *file_base=0);
337         ~FFMPEG();
338         int scan(IndexState *index_state, int64_t *scan_position, int *canceled);
339
340         int ff_audio_stream(int channel) { return astrm_index[channel].st_idx; }
341         int ff_video_stream(int layer) { return vstrm_index[layer].st_idx; }
342
343         int ff_total_audio_channels();
344         int ff_total_astreams();
345         int ff_audio_channels(int stream);
346         int ff_sample_rate(int stream);
347         const char *ff_audio_format(int stream);
348         int ff_audio_pid(int stream);
349         int64_t ff_audio_samples(int stream);
350         int ff_audio_for_video(int vstream, int astream, int64_t &channels);
351
352         int ff_total_video_layers();
353         int ff_total_vstreams();
354         int ff_video_width(int stream);
355         int ff_video_height(int stream);
356         int ff_set_video_width(int stream, int width);
357         int ff_set_video_height(int stream, int height);
358         int ff_coded_width(int stream);
359         int ff_coded_height(int stream);
360         float ff_aspect_ratio(int stream);
361         double ff_frame_rate(int stream);
362         const char *ff_video_format(int stream);
363         int64_t ff_video_frames(int stream);
364         int ff_video_pid(int stream);
365
366         int ff_cpus();
367         void dump_context(AVCodecContext *ctx);
368 };
369
370 #endif /* FFMPEG_H */