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 "libavfilter/buffersrc.h"
27 #include "libavfilter/buffersink.h"
28 #include "libavformat/avformat.h"
29 #include "libavformat/avio.h"
30 #include "libavcodec/avcodec.h"
31 #include "libavfilter/avfilter.h"
32 #include "libavutil/avutil.h"
33 #include "libavutil/opt.h"
34 #include "libavutil/pixdesc.h"
35 #include "libswresample/swresample.h"
36 #include "libswscale/swscale.h"
45 operator AVPacket*() { return &pkt; }
46 operator AVPacket&() { return pkt; }
47 AVPacket *operator ->() { return &pkt; }
50 class FFrame : public ListItem<FFrame> {
57 FFrame(FFStream *fst);
60 operator AVFrame*() { return frm; }
61 operator AVFrame&() { return *frm; }
62 AVFrame *operator ->() { return frm; }
64 int initted() { return init; }
65 void queue(int64_t pos);
71 FFStream(FFMPEG *ffmpeg, AVStream *st, int fidx);
73 static void ff_lock(const char *cp=0);
74 static void ff_unlock();
75 void queue(FFrame *frm);
76 void dequeue(FFrame *frm);
78 virtual int encode_activate();
79 virtual int decode_activate();
81 int seek(int64_t no, double rate);
82 int write_packet(FFPacket &pkt);
84 int decode(AVFrame *frame);
85 void load_markers(IndexMarks &marks, double rate);
87 virtual int is_audio() = 0;
88 virtual int is_video() = 0;
89 virtual int decode_frame(AVPacket *pkt, AVFrame *frame, int &got_frame) = 0;
90 virtual int encode_frame(AVPacket *pkt, AVFrame *frame, int &got_frame) = 0;
91 virtual int init_frame(AVFrame *frame) = 0;
92 virtual int create_filter(const char *filter_spec,
93 AVCodecContext *src_ctx, AVCodecContext *sink_ctx) = 0;
94 virtual void load_markers() = 0;
95 int create_filter(const char *filter_spec);
96 int load_filter(AVFrame *frame);
97 int read_filter(AVFrame *frame);
98 int read_frame(AVFrame *frame);
102 AVFormatContext *fmt_ctx;
104 AVFilterContext *buffersink_ctx;
105 AVFilterContext *buffersrc_ctx;
106 AVFilterGraph *filter_graph;
107 AVFrame *frame, *fframe;
111 AVBitStreamFilterContext *bsfc;
113 BSFilter(const char *bsf, const char *ap) {
114 bsfc = av_bitstream_filter_init(bsf);
115 args = ap ? cstrdup(ap) : 0;
118 av_bitstream_filter_close(bsfc);
122 void add_bsfilter(const char *bsf, const char *ap);
123 ArrayList<BSFilter *> bsfilter;
124 int bs_filter(AVPacket *pkt);
127 int need_packet, flushed;
132 IndexMarks *index_markers;
135 int64_t seek_pos, curr_pos;
137 int reading, writing;
140 int st_eof() { return eof; }
141 void st_eof(int v) { eof = v; }
144 class FFAudioStream : public FFStream {
145 float *inp, *outp, *bfr, *lmt;
149 int read(float *fp, long len);
150 void realloc(long nsz, int nch, long len);
151 void realloc(long nsz, int nch);
152 void reserve(long nsz, int nch);
155 void iseek(int64_t ofs);
156 float *get_outp(int len);
157 int64_t put_inp(int len);
158 int write(const float *fp, long len);
160 int write(const double *dp, long len, int ch);
162 FFAudioStream(FFMPEG *ffmpeg, AVStream *strm, int idx, int fidx);
163 virtual ~FFAudioStream();
164 int is_audio() { return 1; }
165 int is_video() { return 0; }
166 int get_samples(float *&samples, uint8_t **data, int len);
167 int load_history(uint8_t **data, int len);
168 int decode_frame(AVPacket *pkt, AVFrame *frame, int &got_frame);
169 int encode_frame(AVPacket *pkt, AVFrame *frame, int &got_frame);
170 int create_filter(const char *filter_spec,
171 AVCodecContext *src_ctx, AVCodecContext *sink_ctx);
174 int encode_activate();
176 int64_t load_buffer(double ** const sp, int len);
177 int in_history(int64_t pos);
178 void reset_history();
179 int read(double *dp, long len, int ch);
181 int init_frame(AVFrame *frame);
182 int load(int64_t pos, int len);
183 int audio_seek(int64_t pos);
184 int encode(double **samples, int len);
187 int channel0, channels;
192 SwrContext *resample_context;
197 class FFVideoStream : public FFStream {
199 FFVideoStream(FFMPEG *ffmpeg, AVStream *strm, int idx, int fidx);
200 virtual ~FFVideoStream();
201 int is_audio() { return 0; }
202 int is_video() { return 1; }
203 int decode_frame(AVPacket *pkt, AVFrame *frame, int &got_frame);
204 int encode_frame(AVPacket *pkt, AVFrame *frame, int &got_frame);
205 int create_filter(const char *filter_spec,
206 AVCodecContext *src_ctx, AVCodecContext *sink_ctx);
209 int init_frame(AVFrame *picture);
210 int load(VFrame *vframe, int64_t pos);
211 int video_seek(int64_t pos);
212 int encode(VFrame *vframe);
220 struct SwsContext *convert_ctx;
225 static PixelFormat color_model_to_pix_fmt(int color_model);
226 static int pix_fmt_to_color_model(PixelFormat pix_fmt);
228 int convert_picture_vframe(VFrame *frame,
229 AVPicture *ip, PixelFormat ifmt, int iw, int ih);
230 int convert_cmodel(VFrame *frame_out,
231 AVPicture *ip, PixelFormat ifmt, int iw, int ih);
232 int convert_vframe_picture(VFrame *frame,
233 AVPicture *op, PixelFormat ofmt, int ow, int oh);
234 int convert_pixfmt(VFrame *frame_in,
235 AVPicture *op, PixelFormat ofmt, int ow, int oh);
238 class FFMPEG : public Thread {
241 static void ff_lock(const char *cp=0) { fflock.lock(cp); }
242 static void ff_unlock() { fflock.unlock(); }
244 int check_sample_rate(AVCodec *codec, int sample_rate);
245 AVRational check_frame_rate(AVCodec *codec, double frame_rate);
246 AVRational to_sample_aspect_ratio(Asset *asset);
247 AVRational to_time_base(int sample_rate);
249 static void set_option_path(char *path, const char *fmt, ...);
250 static void get_option_path(char *path, const char *type, const char *spec);
251 static int get_format(char *format, const char *path, char *spec);
252 static int scan_option_line(char *cp,char *tag,char *val);
253 int get_file_format();
254 int get_encoder(const char *options,
255 char *format, char *codec, char *bsfilter, char *bsargs);
256 int get_encoder(FILE *fp,
257 char *format, char *codec, char *bsfilter, char *bsargs);
258 int read_options(const char *options, AVDictionary *&opts);
259 int scan_options(const char *options, AVDictionary *&opts, AVStream *st);
260 int read_options(FILE *fp, const char *options, AVDictionary *&opts);
261 int load_options(const char *options, AVDictionary *&opts);
262 static int load_options(const char *path, char *bfr, int len);
263 void set_loglevel(const char *ap);
264 static double to_secs(int64_t time, AVRational time_base);
265 int info(char *text, int len);
267 int init_decoder(const char *filename);
269 int init_encoder(const char *filename);
270 int open_encoder(const char *type, const char *spec);
273 int total_audio_channels();
274 int total_video_channels();
276 int audio_seek(int ch, int64_t pos);
277 int video_seek(int layer, int64_t pos);
279 int decode(int chn, int64_t pos, double *samples, int len);
280 int decode(int layer, int64_t pos, VFrame *frame);
281 int decode_activate();
282 int encode(int stream, double **samples, int len);
283 int encode(int stream, VFrame *frame);
284 int encode_activate();
287 AVFormatContext *fmt_ctx;
288 ArrayList<FFAudioStream*> ffaudio;
289 ArrayList<FFVideoStream*> ffvideo;
292 char *opt_video_filter;
293 char *opt_audio_filter;
294 char file_format[BCTEXTLEN];
298 uint16_t st_idx, st_ch;
299 ffidx() { st_idx = st_ch = 0; }
300 ffidx(const ffidx &t) { st_idx = t.st_idx; st_ch = t.st_ch; }
301 ffidx(uint16_t fidx, uint16_t ch) { st_idx = fidx; st_ch = ch; }
304 ArrayList<ffidx> astrm_index;
305 ArrayList<ffidx> vstrm_index;
306 int mux_audio(FFrame *frm);
307 int mux_video(FFrame *frm);
309 Condition *flow_lock;
320 int decoding, encoding;
321 int has_audio, has_video;
323 FFMPEG(FileBase *file_base=0);
325 int scan(IndexState *index_state, int64_t *scan_position, int *canceled);
327 int ff_audio_stream(int channel) { return astrm_index[channel].st_idx; }
328 int ff_video_stream(int layer) { return vstrm_index[layer].st_idx; }
330 int ff_total_audio_channels();
331 int ff_total_astreams();
332 int ff_audio_channels(int stream);
333 int ff_sample_rate(int stream);
334 const char *ff_audio_format(int stream);
335 int ff_audio_pid(int stream);
336 int64_t ff_audio_samples(int stream);
337 int ff_audio_for_video(int vstream, int astream, int64_t &channels);
339 int ff_total_video_layers();
340 int ff_total_vstreams();
341 int ff_video_width(int stream);
342 int ff_video_height(int stream);
343 int ff_set_video_width(int stream, int width);
344 int ff_set_video_height(int stream, int height);
345 int ff_coded_width(int stream);
346 int ff_coded_height(int stream);
347 float ff_aspect_ratio(int stream);
348 double ff_frame_rate(int stream);
349 const char *ff_video_format(int stream);
350 int64_t ff_video_frames(int stream);
351 int ff_video_pid(int stream);
354 void dump_context(AVCodecContext *ctx);
357 #endif /* FFMPEG_H */