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"
43 operator AVPacket*() { return &pkt; }
44 operator AVPacket&() { return pkt; }
45 AVPacket *operator ->() { return &pkt; }
49 FFPacket() { init(); }
50 ~FFPacket() { finit(); }
53 class FFrame : public ListItem<FFrame> {
60 FFrame(FFStream *fst);
63 operator AVFrame*() { return frm; }
64 operator AVFrame&() { return *frm; }
65 AVFrame *operator ->() { return frm; }
67 int initted() { return init; }
68 void queue(int64_t pos);
74 FFStream(FFMPEG *ffmpeg, AVStream *st, int fidx);
76 static void ff_lock(const char *cp=0);
77 static void ff_unlock();
78 void queue(FFrame *frm);
79 void dequeue(FFrame *frm);
81 virtual int encode_activate();
82 virtual int decode_activate();
84 int seek(int64_t no, double rate);
85 int write_packet(FFPacket &pkt);
87 int decode(AVFrame *frame);
88 void load_markers(IndexMarks &marks, double rate);
90 virtual int is_audio() = 0;
91 virtual int is_video() = 0;
92 virtual int decode_frame(AVPacket *pkt, AVFrame *frame, int &got_frame) = 0;
93 virtual int encode_frame(AVPacket *pkt, AVFrame *frame, int &got_frame) = 0;
94 virtual int init_frame(AVFrame *frame) = 0;
95 virtual int create_filter(const char *filter_spec,
96 AVCodecContext *src_ctx, AVCodecContext *sink_ctx) = 0;
97 virtual void load_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);
105 AVFormatContext *fmt_ctx;
107 AVFilterContext *buffersink_ctx;
108 AVFilterContext *buffersrc_ctx;
109 AVFilterGraph *filter_graph;
110 AVFrame *frame, *fframe;
114 AVBitStreamFilterContext *bsfc;
116 BSFilter(const char *bsf, const char *ap) {
117 bsfc = av_bitstream_filter_init(bsf);
118 args = ap ? cstrdup(ap) : 0;
121 av_bitstream_filter_close(bsfc);
125 void add_bsfilter(const char *bsf, const char *ap);
126 ArrayList<BSFilter *> bsfilter;
127 int bs_filter(AVPacket *pkt);
130 int need_packet, flushed;
135 IndexMarks *index_markers;
138 int64_t seek_pos, curr_pos;
140 int reading, writing;
143 int st_eof() { return eof; }
144 void st_eof(int v) { eof = v; }
147 class FFAudioStream : public FFStream {
148 float *inp, *outp, *bfr, *lmt;
152 int read(float *fp, long len);
153 void realloc(long nsz, int nch, long len);
154 void realloc(long nsz, int nch);
155 void reserve(long nsz, int nch);
158 void iseek(int64_t ofs);
159 float *get_outp(int len);
160 int64_t put_inp(int len);
161 int write(const float *fp, long len);
163 int write(const double *dp, long len, int ch);
165 FFAudioStream(FFMPEG *ffmpeg, AVStream *strm, int idx, int fidx);
166 virtual ~FFAudioStream();
167 int is_audio() { return 1; }
168 int is_video() { return 0; }
169 int get_samples(float *&samples, uint8_t **data, int len);
170 int load_history(uint8_t **data, int len);
171 int decode_frame(AVPacket *pkt, AVFrame *frame, int &got_frame);
172 int encode_frame(AVPacket *pkt, AVFrame *frame, int &got_frame);
173 int create_filter(const char *filter_spec,
174 AVCodecContext *src_ctx, AVCodecContext *sink_ctx);
177 int encode_activate();
179 int64_t load_buffer(double ** const sp, int len);
180 int in_history(int64_t pos);
181 void reset_history();
182 int read(double *dp, long len, int ch);
184 int init_frame(AVFrame *frame);
185 int load(int64_t pos, int len);
186 int audio_seek(int64_t pos);
187 int encode(double **samples, int len);
190 int channel0, channels;
195 SwrContext *resample_context;
201 class FFVideoConvert {
203 struct SwsContext *convert_ctx;
205 FFVideoConvert() { convert_ctx = 0; }
206 ~FFVideoConvert() { if( convert_ctx ) sws_freeContext(convert_ctx); }
208 static AVPixelFormat color_model_to_pix_fmt(int color_model);
209 static int pix_fmt_to_color_model(AVPixelFormat pix_fmt);
211 int convert_picture_vframe(VFrame *frame,
212 AVFrame *ip, AVPixelFormat ifmt, int iw, int ih);
213 int convert_cmodel(VFrame *frame_out,
214 AVFrame *ip, AVPixelFormat ifmt, int iw, int ih);
215 int transfer_cmodel(VFrame *frame_in, //defaults->metadata
216 AVFrame *ifp, AVPixelFormat ifmt, int iw, int ih);
217 int convert_vframe_picture(VFrame *frame,
218 AVFrame *op, AVPixelFormat ofmt, int ow, int oh);
219 int convert_pixfmt(VFrame *frame_in,
220 AVFrame *op, AVPixelFormat ofmt, int ow, int oh);
221 int transfer_pixfmt(VFrame *frame_in, //metadata->defaults
222 AVFrame *ofp, AVPixelFormat ofmt, int ow, int oh);
225 class FFVideoStream : public FFStream, public FFVideoConvert {
227 FFVideoStream(FFMPEG *ffmpeg, AVStream *strm, int idx, int fidx);
228 virtual ~FFVideoStream();
229 int is_audio() { return 0; }
230 int is_video() { return 1; }
231 int decode_frame(AVPacket *pkt, AVFrame *frame, int &got_frame);
232 int encode_frame(AVPacket *pkt, AVFrame *frame, int &got_frame);
233 int create_filter(const char *filter_spec,
234 AVCodecContext *src_ctx, AVCodecContext *sink_ctx);
237 int init_frame(AVFrame *picture);
238 int load(VFrame *vframe, int64_t pos);
239 int video_seek(int64_t pos);
240 int encode(VFrame *vframe);
253 class FFMPEG : public Thread {
256 static void ff_lock(const char *cp=0) { fflock.lock(cp); }
257 static void ff_unlock() { fflock.unlock(); }
259 int check_sample_rate(AVCodec *codec, int sample_rate);
260 AVRational check_frame_rate(AVCodec *codec, double frame_rate);
261 AVRational to_sample_aspect_ratio(Asset *asset);
262 AVRational to_time_base(int sample_rate);
264 static void set_option_path(char *path, const char *fmt, ...);
265 static void get_option_path(char *path, const char *type, const char *spec);
266 static int get_format(char *format, const char *path, const char *spec);
267 static int get_codec(char *codec, const char *path, const char *spec);
268 static int scan_option_line(char *cp,char *tag,char *val);
269 static int load_defaults(const char *path, const char *type,
270 char *codec, char *codec_options, int len);
271 static void set_asset_format(Asset *asset, const char *text);
272 int get_file_format();
273 int get_encoder(const char *options,
274 char *format, char *codec, char *bsfilter, char *bsargs);
275 int get_encoder(FILE *fp,
276 char *format, char *codec, char *bsfilter, char *bsargs);
277 int read_options(const char *options, AVDictionary *&opts);
278 int scan_options(const char *options, AVDictionary *&opts, AVStream *st);
279 int read_options(FILE *fp, const char *options, AVDictionary *&opts);
280 int load_options(const char *options, AVDictionary *&opts);
281 static int load_options(const char *path, char *bfr, int len);
282 void set_loglevel(const char *ap);
283 static double to_secs(int64_t time, AVRational time_base);
284 int info(char *text, int len);
286 int init_decoder(const char *filename);
288 int init_encoder(const char *filename);
289 int open_encoder(const char *type, const char *spec);
292 int total_audio_channels();
293 int total_video_channels();
295 int audio_seek(int ch, int64_t pos);
296 int video_seek(int layer, int64_t pos);
298 int decode(int chn, int64_t pos, double *samples, int len);
299 int decode(int layer, int64_t pos, VFrame *frame);
300 int decode_activate();
301 int encode(int stream, double **samples, int len);
302 int encode(int stream, VFrame *frame);
303 int encode_activate();
306 AVFormatContext *fmt_ctx;
307 ArrayList<FFAudioStream*> ffaudio;
308 ArrayList<FFVideoStream*> ffvideo;
311 char *opt_video_filter;
312 char *opt_audio_filter;
313 char file_format[BCTEXTLEN];
317 uint16_t st_idx, st_ch;
318 ffidx() { st_idx = st_ch = 0; }
319 ffidx(const ffidx &t) { st_idx = t.st_idx; st_ch = t.st_ch; }
320 ffidx(uint16_t fidx, uint16_t ch) { st_idx = fidx; st_ch = ch; }
323 ArrayList<ffidx> astrm_index;
324 ArrayList<ffidx> vstrm_index;
325 int mux_audio(FFrame *frm);
326 int mux_video(FFrame *frm);
328 Condition *flow_lock;
339 int decoding, encoding;
340 int has_audio, has_video;
342 FFMPEG(FileBase *file_base=0);
344 int scan(IndexState *index_state, int64_t *scan_position, int *canceled);
346 int ff_audio_stream(int channel) { return astrm_index[channel].st_idx; }
347 int ff_video_stream(int layer) { return vstrm_index[layer].st_idx; }
349 int ff_total_audio_channels();
350 int ff_total_astreams();
351 int ff_audio_channels(int stream);
352 int ff_sample_rate(int stream);
353 const char *ff_audio_format(int stream);
354 int ff_audio_pid(int stream);
355 int64_t ff_audio_samples(int stream);
356 int ff_audio_for_video(int vstream, int astream, int64_t &channels);
358 int ff_total_video_layers();
359 int ff_total_vstreams();
360 int ff_video_width(int stream);
361 int ff_video_height(int stream);
362 int ff_set_video_width(int stream, int width);
363 int ff_set_video_height(int stream, int height);
364 int ff_coded_width(int stream);
365 int ff_coded_height(int stream);
366 float ff_aspect_ratio(int stream);
367 double ff_frame_rate(int stream);
368 const char *ff_video_format(int stream);
369 int64_t ff_video_frames(int stream);
370 int ff_video_pid(int stream);
373 void dump_context(AVCodecContext *ctx);
376 #endif /* FFMPEG_H */