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