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