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();
83 virtual int write_packet(FFPacket &pkt);
85 int seek(int64_t no, double rate);
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(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);
105 AVFormatContext *fmt_ctx;
106 AVCodecContext *avctx;
108 AVFilterContext *buffersink_ctx;
109 AVFilterContext *buffersrc_ctx;
110 AVFilterGraph *filter_graph;
111 AVFrame *frame, *fframe;
115 int need_packet, flushed;
122 int64_t seek_pos, curr_pos;
124 int reading, writing;
127 int st_eof() { return eof; }
128 void st_eof(int v) { eof = v; }
131 class FFAudioStream : public FFStream {
132 float *inp, *outp, *bfr, *lmt;
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);
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);
147 int write(const double *dp, long len, int ch);
148 int write_packet(FFPacket &pkt);
150 FFAudioStream(FFMPEG *ffmpeg, AVStream *strm, int idx, int fidx);
151 virtual ~FFAudioStream();
152 int is_audio() { return 1; }
153 int is_video() { return 0; }
154 void init_swr(int ichs, int ifmt, int irate);
155 int get_samples(float *&samples, uint8_t **data, int len);
156 int load_history(uint8_t **data, int len);
157 int decode_frame(AVFrame *frame);
158 int encode_frame(AVFrame *frame);
159 int create_filter(const char *filter_spec, AVCodecParameters *avpar);
161 IndexMarks *get_markers();
163 int encode_activate();
164 int64_t load_buffer(double ** const sp, int len);
165 int in_history(int64_t pos);
166 void reset_history();
167 int read(double *dp, long len, int ch);
169 int init_frame(AVFrame *frame);
170 int load(int64_t pos, int len);
171 int audio_seek(int64_t pos);
172 int encode(double **samples, int len);
176 int channel0, channels;
181 SwrContext *resample_context;
182 int swr_ichs, swr_ifmt, swr_irate;
188 class FFVideoConvert {
190 struct SwsContext *convert_ctx;
192 FFVideoConvert() { convert_ctx = 0; }
193 ~FFVideoConvert() { if( convert_ctx ) sws_freeContext(convert_ctx); }
195 static AVPixelFormat color_model_to_pix_fmt(int color_model);
196 static int pix_fmt_to_color_model(AVPixelFormat pix_fmt);
198 int convert_picture_vframe(VFrame *frame, AVFrame *ip);
199 int convert_picture_vframe(VFrame *frame, AVFrame *ip, AVFrame *ipic);
200 int convert_cmodel(VFrame *frame, AVFrame *ip);
201 int transfer_cmodel(VFrame *frame, AVFrame *ifp);
202 int convert_vframe_picture(VFrame *frame, AVFrame *op);
203 int convert_vframe_picture(VFrame *frame, AVFrame *op, AVFrame *opic);
204 int convert_pixfmt(VFrame *frame, AVFrame *op);
205 int transfer_pixfmt(VFrame *frame, AVFrame *ofp);
208 class FFVideoStream : public FFStream, public FFVideoConvert {
209 int write_packet(FFPacket &pkt);
211 FFVideoStream(FFMPEG *ffmpeg, AVStream *strm, int idx, int fidx);
212 virtual ~FFVideoStream();
213 int is_audio() { return 0; }
214 int is_video() { return 1; }
215 int decode_frame(AVFrame *frame);
216 int encode_frame(AVFrame *frame);
217 int create_filter(const char *filter_spec, AVCodecParameters *avpar);
219 IndexMarks *get_markers();
221 int init_frame(AVFrame *picture);
222 int load(VFrame *vframe, int64_t pos);
223 int video_seek(int64_t pos);
224 int encode(VFrame *vframe);
237 class FFMPEG : public Thread {
240 static void ff_lock(const char *cp=0) { fflock.lock(cp); }
241 static void ff_unlock() { fflock.unlock(); }
243 int check_sample_rate(AVCodec *codec, int sample_rate);
244 AVRational check_frame_rate(AVCodec *codec, double frame_rate);
245 AVRational to_sample_aspect_ratio(Asset *asset);
246 AVRational to_time_base(int sample_rate);
248 static void set_option_path(char *path, const char *fmt, ...);
249 static void get_option_path(char *path, const char *type, const char *spec);
250 static int get_format(char *format, const char *path, const char *spec);
251 static int get_codec(char *codec, const char *path, const char *spec);
252 static int scan_option_line(char *cp,char *tag,char *val);
253 static int load_defaults(const char *path, const char *type,
254 char *codec, char *codec_options, int len);
255 static void set_asset_format(Asset *asset, const char *text);
256 int get_file_format();
257 int get_encoder(const char *options, char *format, char *codec, char *bsfilter);
258 int get_encoder(FILE *fp, char *format, char *codec, char *bsfilter);
259 int read_options(const char *options, AVDictionary *&opts, int skip=0);
260 int scan_options(const char *options, AVDictionary *&opts, AVStream *st);
261 int read_options(FILE *fp, const char *options, AVDictionary *&opts);
262 int load_options(const char *options, AVDictionary *&opts);
263 static int load_options(const char *path, char *bfr, int len);
264 void set_loglevel(const char *ap);
265 static double to_secs(int64_t time, AVRational time_base);
266 int info(char *text, int len);
268 int init_decoder(const char *filename);
270 int init_encoder(const char *filename);
271 int open_encoder(const char *type, const char *spec);
274 int total_audio_channels();
275 int total_video_channels();
277 int audio_seek(int ch, int64_t pos);
278 int video_seek(int layer, int64_t pos);
280 int decode(int chn, int64_t pos, double *samples, int len);
281 int decode(int layer, int64_t pos, VFrame *frame);
282 int decode_activate();
283 int encode(int stream, double **samples, int len);
284 int encode(int stream, VFrame *frame);
285 int encode_activate();
288 AVFormatContext *fmt_ctx;
289 ArrayList<FFAudioStream*> ffaudio;
290 ArrayList<FFVideoStream*> ffvideo;
293 char *opt_video_filter;
294 char *opt_audio_filter;
295 char file_format[BCTEXTLEN];
299 uint16_t st_idx, st_ch;
300 ffidx() { st_idx = st_ch = 0; }
301 ffidx(const ffidx &t) { st_idx = t.st_idx; st_ch = t.st_ch; }
302 ffidx(uint16_t fidx, uint16_t ch) { st_idx = fidx; st_ch = ch; }
305 ArrayList<ffidx> astrm_index;
306 ArrayList<ffidx> vstrm_index;
307 int mux_audio(FFrame *frm);
308 int mux_video(FFrame *frm);
310 Condition *flow_lock;
321 int decoding, encoding;
322 int has_audio, has_video;
324 FFMPEG(FileBase *file_base=0);
326 int scan(IndexState *index_state, int64_t *scan_position, int *canceled);
328 int ff_audio_stream(int channel) { return astrm_index[channel].st_idx; }
329 int ff_video_stream(int layer) { return vstrm_index[layer].st_idx; }
331 int ff_total_audio_channels();
332 int ff_total_astreams();
333 int ff_audio_channels(int stream);
334 int ff_sample_rate(int stream);
335 const char *ff_audio_format(int stream);
336 int ff_audio_pid(int stream);
337 int64_t ff_audio_samples(int stream);
338 int ff_audio_for_video(int vstream, int astream, int64_t &channels);
340 int ff_total_video_layers();
341 int ff_total_vstreams();
342 int ff_video_width(int stream);
343 int ff_video_height(int stream);
344 int ff_set_video_width(int stream, int width);
345 int ff_set_video_height(int stream, int height);
346 int ff_coded_width(int stream);
347 int ff_coded_height(int stream);
348 float ff_aspect_ratio(int stream);
349 double ff_frame_rate(int stream);
350 const char *ff_video_format(int stream);
351 int64_t ff_video_frames(int stream);
352 int ff_video_pid(int stream);
355 void dump_context(AVCodecContext *ctx);
358 #endif /* FFMPEG_H */