centos build workarounds
[goodguy/history.git] / cinelerra-5.0 / 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 "mutex.h"
21 #include "thread.h"
22 #include "vframe.inc"
23
24 extern "C" {
25 #include "libavfilter/buffersrc.h"
26 #include "libavfilter/buffersink.h"
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 "libavutil/opt.h"
33 #include "libavutil/pixdesc.h"
34 #include "libswresample/swresample.h"
35 #include "libswscale/swscale.h"
36 }
37
38 class FFPacket  {
39         AVPacket pkt;
40 public:
41         FFPacket();
42         ~FFPacket();
43         void init();
44         operator AVPacket*() { return &pkt; }
45         operator AVPacket&() { return pkt; }
46         AVPacket *operator ->() { return &pkt; }
47 };
48
49 class FFrame : public ListItem<FFrame> {
50         AVFrame *frm;
51         int init;
52 public:
53         int64_t position;
54         FFStream *fst;
55
56         FFrame(FFStream *fst);
57         ~FFrame();
58
59         operator AVFrame*() { return frm; }
60         operator AVFrame&() { return *frm; }
61         AVFrame *operator ->() { return frm; }
62
63         int initted() { return init; }
64         void queue(int64_t pos);
65         void dequeue();
66 };
67
68 class FFStream {
69 public:
70         FFStream(FFMPEG *ffmpeg, AVStream *st, int idx);
71         ~FFStream();
72         static void ff_lock(const char *cp=0);
73         static void ff_unlock();
74         void queue(FFrame *frm);
75         void dequeue(FFrame *frm);
76
77         virtual int encode_activate();
78         virtual int decode_activate();
79         int read_packet();
80         int write_packet(FFPacket &pkt);
81         int flush();
82         int decode(AVFrame *frame);
83
84         virtual int decode_frame(AVFrame *frame, int &got_frame) = 0;
85         virtual int encode_frame(FFPacket &pkt, AVFrame *frame, int &got_frame) = 0;
86         virtual int init_frame(AVFrame *frame) = 0;
87         virtual int create_filter(const char *filter_spec,
88                 AVCodecContext *src_ctx, AVCodecContext *sink_ctx) = 0;
89         int create_filter(const char *filter_spec);
90         int load_filter(AVFrame *frame);
91         int read_filter(AVFrame *frame);
92         int read_frame(AVFrame *frame);
93
94         FFMPEG *ffmpeg;
95         AVStream *st;
96         AVFormatContext *fmt_ctx;
97
98         AVFilterContext *buffersink_ctx;
99         AVFilterContext *buffersrc_ctx;
100         AVFilterGraph *filter_graph;
101         AVFrame *frame, *fframe;
102
103         class BSFilter {
104         public:
105                 AVBitStreamFilterContext *bsfc;
106                 const char *args;
107                 BSFilter(const char *bsf, const char *ap) {
108                         bsfc = av_bitstream_filter_init(bsf);
109                         args = ap ? cstrdup(ap) : 0;
110                 }
111                 ~BSFilter() {
112                         av_bitstream_filter_close(bsfc);
113                         delete [] args;
114                 }
115         };
116         void add_bsfilter(const char *bsf, const char *ap);
117         ArrayList<BSFilter *> bsfilter;
118         int bs_filter(AVPacket *pkt);
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         int idx;
129         int reading, writing;
130         int eof;
131
132         int st_eof() { return eof; }
133         void st_eof(int v) { eof = v; }
134 };
135
136 class FFAudioStream : public FFStream {
137         float *inp, *outp, *bfr, *lmt;
138         long sz;
139         int nch;
140
141         int read(float *fp, long len);
142         void realloc(long sz, int nch, long len);
143         void realloc(long sz, int nch);
144         void reserve(long sz, int nch);
145         long used();
146         long avail();
147         void reset();
148         void iseek(int64_t ofs);
149         float *get_outp(int len);
150         int64_t put_inp(int len);
151         int write(const float *fp, long len);
152         int zero(long len);
153         int write(const double *dp, long len, int ch);
154 public:
155         FFAudioStream(FFMPEG *ffmpeg, AVStream *strm, int idx);
156         virtual ~FFAudioStream();
157         int load_history(uint8_t **data, int len);
158         int decode_frame(AVFrame *frame, int &got_frame);
159         int encode_frame(FFPacket &pkt, AVFrame *frame, int &got_frame);
160         int create_filter(const char *filter_spec,
161                 AVCodecContext *src_ctx, AVCodecContext *sink_ctx);
162
163         int encode_activate();
164         int nb_samples();
165         int64_t load_buffer(double ** const sp, int len);
166         int in_history(int64_t pos);
167         int read(double *dp, long len, int ch);
168
169         int init_frame(AVFrame *frame);
170         int load(int64_t pos, int len);
171         int audio_seek(int64_t pos);
172         int encode(double **samples, int len);
173
174         int channel0, channels;
175         int sample_rate;
176         int mbsz, frame_sz;
177         int64_t seek_pos, curr_pos;
178         int64_t length;
179
180         SwrContext *resample_context;
181         int aud_bfr_sz;
182         float *aud_bfr;
183 };
184
185 class FFVideoStream : public FFStream {
186 public:
187         FFVideoStream(FFMPEG *ffmpeg, AVStream *strm, int idx);
188         virtual ~FFVideoStream();
189         int decode_frame(AVFrame *frame, int &got_frame);
190         int encode_frame(FFPacket &pkt, AVFrame *frame, int &got_frame);
191         int create_filter(const char *filter_spec,
192                 AVCodecContext *src_ctx, AVCodecContext *sink_ctx);
193
194         int init_frame(AVFrame *picture);
195         int load(VFrame *vframe, int64_t pos);
196         int video_seek(int64_t pos);
197         int encode(VFrame *vframe);
198
199         double frame_rate;
200         int width, height;
201         int64_t seek_pos, curr_pos;
202         int64_t length;
203         float aspect_ratio;
204
205         struct SwsContext *convert_ctx;
206         uint8_t *pkt_bfr;
207         int pkt_bfr_sz;
208
209         static PixelFormat color_model_to_pix_fmt(int color_model);
210         static int pix_fmt_to_color_model(PixelFormat pix_fmt);
211
212         int convert_picture_vframe(VFrame *frame,
213                 AVPicture *ip, PixelFormat ifmt, int iw, int ih);
214         int convert_cmodel(VFrame *frame_out,
215                 AVPicture *ip, PixelFormat ifmt, int iw, int ih);
216         int convert_vframe_picture(VFrame *frame,
217                 AVPicture *op, PixelFormat ofmt, int ow, int oh);
218         int convert_pixfmt(VFrame *frame_in,
219                  AVPicture *op, PixelFormat ofmt, int ow, int oh);
220 };
221
222 class FFMPEG : public Thread {
223 public:
224         static Mutex fflock;
225         static void ff_lock(const char *cp=0) { fflock.lock(cp); }
226         static void ff_unlock() { fflock.unlock(); }
227
228         int check_sample_rate(AVCodec *codec, int sample_rate);
229         AVRational check_frame_rate(AVCodec *codec, double frame_rate);
230         AVRational to_sample_aspect_ratio(double aspect_ratio);
231         AVRational to_time_base(int sample_rate);
232
233         static void set_option_path(char *path, const char *fmt, ...);
234         static void get_option_path(char *path, const char *type, const char *spec);
235         static int get_format(char *format, const char *path, char *spec);
236         static int scan_option_line(char *cp,char *tag,char *val);
237         int get_file_format();
238         int get_encoder(const char *options,
239                 char *format, char *codec, char *bsfilter, char *bsargs);
240         int get_encoder(FILE *fp,
241                 char *format, char *codec, char *bsfilter, char *bsargs);
242         int read_options(const char *options, AVDictionary *&opts);
243         int scan_options(const char *options, AVDictionary *&opts, AVStream *st);
244         int read_options(FILE *fp, const char *options, AVDictionary *&opts);
245         int load_options(const char *options, AVDictionary *&opts);
246         static int load_options(const char *path, char *bfr, int len);
247         void set_loglevel(const char *ap);
248         static double to_secs(int64_t time, AVRational time_base);
249         int info(char *text, int len);
250
251         int init_decoder(const char *filename);
252         int open_decoder();
253         int init_encoder(const char *filename);
254         int open_encoder(const char *type, const char *spec);
255         int close_encoder();
256
257         int total_audio_channels();
258         int total_video_channels();
259
260         int audio_seek(int ch, int64_t pos);
261         int video_seek(int layer, int64_t pos);
262
263         int decode(int chn, int64_t pos, double *samples, int len);
264         int decode(int layer, int64_t pos, VFrame *frame);
265         int decode_activate();
266         int encode(int stream, double **samples, int len);
267         int encode(int stream, VFrame *frame);
268         int encode_activate();
269
270         FileBase *file_base;
271         AVFormatContext *fmt_ctx;
272         ArrayList<FFAudioStream*> ffaudio;
273         ArrayList<FFVideoStream*> ffvideo;
274         AVDictionary *opts;
275         double opt_duration;
276         char *opt_video_filter;
277         char *opt_audio_filter;
278         char file_format[BCTEXTLEN];
279
280         class ffidx {
281         public:
282                 uint16_t st_idx, st_ch;
283                 ffidx() { st_idx = st_ch = 0; }
284                 ffidx(const ffidx &t) { st_idx = t.st_idx;  st_ch = t.st_ch; }
285                 ffidx(uint16_t idx, uint16_t ch) { st_idx = idx; st_ch = ch; }
286         };
287
288         ArrayList<ffidx> astrm_index;
289         ArrayList<ffidx> vstrm_index;
290         int mux_audio(FFrame *frm);
291         int mux_video(FFrame *frm);
292         Condition *mux_lock;
293         Condition *flow_lock;
294         int done, flow;
295         
296         void start_muxer();
297         void stop_muxer();
298         void flow_off();
299         void flow_on();
300         void flow_ctl();
301         void mux();
302         void run();
303
304         int decoding, encoding;
305         int has_audio, has_video;
306
307         FFMPEG(FileBase *file_base=0);
308         ~FFMPEG();
309
310         int ff_audio_stream(int channel) { return astrm_index[channel].st_idx; }
311         int ff_video_stream(int layer) { return vstrm_index[layer].st_idx; }
312
313         int ff_total_audio_channels();
314         int ff_total_astreams();
315         int ff_audio_channels(int stream);
316         int ff_sample_rate(int stream);
317         const char *ff_audio_format(int stream);
318         int ff_audio_pid(int stream);
319         int64_t ff_audio_samples(int stream);
320         int ff_audio_for_video(int vstream, int astream, int64_t &channels);
321
322         int ff_total_video_layers();
323         int ff_total_vstreams();
324         int ff_video_width(int stream);
325         int ff_video_height(int stream);
326         int ff_set_video_width(int stream, int width);
327         int ff_set_video_height(int stream, int height);
328         int ff_coded_width(int stream);
329         int ff_coded_height(int stream);
330         float ff_aspect_ratio(int stream);
331         double ff_frame_rate(int stream);
332         const char *ff_video_format(int stream);
333         int64_t ff_video_frames(int stream);
334         int ff_video_pid(int stream);
335
336         int ff_cpus();
337         void dump_context(AVCodecContext *ctx);
338 };
339
340 #endif /* FFMPEG_H */