10 #include "arraylist.h"
12 #include "bccmodels.h"
13 #include "bcwindowbase.inc"
14 #include "condition.h"
18 #include "filebase.inc"
19 #include "fileffmpeg.inc"
20 #include "indexstate.inc"
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"
46 operator AVPacket*() { return &pkt; }
47 operator AVPacket&() { return pkt; }
48 AVPacket *operator ->() { return &pkt; }
51 class FFrame : public ListItem<FFrame> {
58 FFrame(FFStream *fst);
61 operator AVFrame*() { return frm; }
62 operator AVFrame&() { return *frm; }
63 AVFrame *operator ->() { return frm; }
65 int initted() { return init; }
66 void queue(int64_t pos);
72 FFStream(FFMPEG *ffmpeg, AVStream *st, int fidx);
74 static void ff_lock(const char *cp=0);
75 static void ff_unlock();
76 void queue(FFrame *frm);
77 void dequeue(FFrame *frm);
79 virtual int encode_activate();
80 virtual int decode_activate();
82 int seek(int64_t no, double rate);
83 int write_packet(FFPacket &pkt);
85 int decode(AVFrame *frame);
86 void load_markers(IndexMarks &marks, double rate);
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);
103 AVFormatContext *fmt_ctx;
105 AVFilterContext *buffersink_ctx;
106 AVFilterContext *buffersrc_ctx;
107 AVFilterGraph *filter_graph;
108 AVFrame *frame, *fframe;
112 AVBitStreamFilterContext *bsfc;
114 BSFilter(const char *bsf, const char *ap) {
115 bsfc = av_bitstream_filter_init(bsf);
116 args = ap ? cstrdup(ap) : 0;
119 av_bitstream_filter_close(bsfc);
123 void add_bsfilter(const char *bsf, const char *ap);
124 ArrayList<BSFilter *> bsfilter;
125 int bs_filter(AVPacket *pkt);
128 int need_packet, flushed;
133 IndexMarks *index_markers;
136 int64_t seek_pos, curr_pos;
138 int reading, writing;
141 int st_eof() { return eof; }
142 void st_eof(int v) { eof = v; }
145 class FFAudioStream : public FFStream {
146 float *inp, *outp, *bfr, *lmt;
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);
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);
161 int write(const double *dp, long len, int ch);
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);
175 int encode_activate();
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);
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);
188 int channel0, channels;
193 SwrContext *resample_context;
199 class FFVideoConvert {
201 struct SwsContext *convert_ctx;
203 FFVideoConvert() { convert_ctx = 0; }
204 ~FFVideoConvert() { if( convert_ctx ) sws_freeContext(convert_ctx); }
206 static AVPixelFormat color_model_to_pix_fmt(int color_model);
207 static int pix_fmt_to_color_model(AVPixelFormat pix_fmt);
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);
223 class FFVideoStream : public FFStream, public FFVideoConvert {
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);
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);
251 class FFMPEG : public Thread {
254 static void ff_lock(const char *cp=0) { fflock.lock(cp); }
255 static void ff_unlock() { fflock.unlock(); }
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);
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 static int load_defaults(const char *path, const char *type,
267 char *codec, char *codec_options, int len);
268 static void set_asset_format(Asset *asset, const char *text);
269 int get_file_format();
270 int get_encoder(const char *options,
271 char *format, char *codec, char *bsfilter, char *bsargs);
272 int get_encoder(FILE *fp,
273 char *format, char *codec, char *bsfilter, char *bsargs);
274 int read_options(const char *options, AVDictionary *&opts);
275 int scan_options(const char *options, AVDictionary *&opts, AVStream *st);
276 int read_options(FILE *fp, const char *options, AVDictionary *&opts);
277 int load_options(const char *options, AVDictionary *&opts);
278 static int load_options(const char *path, char *bfr, int len);
279 void set_loglevel(const char *ap);
280 static double to_secs(int64_t time, AVRational time_base);
281 int info(char *text, int len);
283 int init_decoder(const char *filename);
285 int init_encoder(const char *filename);
286 int open_encoder(const char *type, const char *spec);
289 int total_audio_channels();
290 int total_video_channels();
292 int audio_seek(int ch, int64_t pos);
293 int video_seek(int layer, int64_t pos);
295 int decode(int chn, int64_t pos, double *samples, int len);
296 int decode(int layer, int64_t pos, VFrame *frame);
297 int decode_activate();
298 int encode(int stream, double **samples, int len);
299 int encode(int stream, VFrame *frame);
300 int encode_activate();
303 AVFormatContext *fmt_ctx;
304 ArrayList<FFAudioStream*> ffaudio;
305 ArrayList<FFVideoStream*> ffvideo;
308 char *opt_video_filter;
309 char *opt_audio_filter;
310 char file_format[BCTEXTLEN];
314 uint16_t st_idx, st_ch;
315 ffidx() { st_idx = st_ch = 0; }
316 ffidx(const ffidx &t) { st_idx = t.st_idx; st_ch = t.st_ch; }
317 ffidx(uint16_t fidx, uint16_t ch) { st_idx = fidx; st_ch = ch; }
320 ArrayList<ffidx> astrm_index;
321 ArrayList<ffidx> vstrm_index;
322 int mux_audio(FFrame *frm);
323 int mux_video(FFrame *frm);
325 Condition *flow_lock;
336 int decoding, encoding;
337 int has_audio, has_video;
339 FFMPEG(FileBase *file_base=0);
341 int scan(IndexState *index_state, int64_t *scan_position, int *canceled);
343 int ff_audio_stream(int channel) { return astrm_index[channel].st_idx; }
344 int ff_video_stream(int layer) { return vstrm_index[layer].st_idx; }
346 int ff_total_audio_channels();
347 int ff_total_astreams();
348 int ff_audio_channels(int stream);
349 int ff_sample_rate(int stream);
350 const char *ff_audio_format(int stream);
351 int ff_audio_pid(int stream);
352 int64_t ff_audio_samples(int stream);
353 int ff_audio_for_video(int vstream, int astream, int64_t &channels);
355 int ff_total_video_layers();
356 int ff_total_vstreams();
357 int ff_video_width(int stream);
358 int ff_video_height(int stream);
359 int ff_set_video_width(int stream, int width);
360 int ff_set_video_height(int stream, int height);
361 int ff_coded_width(int stream);
362 int ff_coded_height(int stream);
363 float ff_aspect_ratio(int stream);
364 double ff_frame_rate(int stream);
365 const char *ff_video_format(int stream);
366 int64_t ff_video_frames(int stream);
367 int ff_video_pid(int stream);
370 void dump_context(AVCodecContext *ctx);
373 #endif /* FFMPEG_H */