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