4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #include <theora/theora.h>
32 #include <theora/theoraenc.h>
33 #include <theora/theoradec.h>
34 #include <vorbis/codec.h>
35 #include <vorbis/vorbisenc.h>
38 /* This code was aspired by ffmpeg2theora */
39 /* Special thanks for help on this code goes out to j@v2v.cc */
43 class sync_window_t : public ogg_sync_state
46 sync_window_t(FILE *fp, Mutex *sync_lock, int64_t begin=0, int64_t end=0);
48 int ogg_read_locked(int buflen);
49 int ogg_read_buffer(int buflen);
50 int ogg_read_buffer_at(off_t filepos, int buflen);
51 int ogg_sync_and_take_page_out(ogg_page *og);
52 int ogg_take_page_out_autoadvance(ogg_page *og);
53 int ogg_sync_and_get_next_page(long serialno, ogg_page *og);
54 int ogg_prev_page_search(long serialno,ogg_page *og,
55 off_t begin, off_t end);
56 int ogg_get_prev_page(long serialno, ogg_page *og);
57 int ogg_get_next_page(long serialno, ogg_page *og);
58 int ogg_get_first_page(long serialno, ogg_page *og);
59 int ogg_get_last_page(long serialno, ogg_page *og);
61 int64_t filepos; // current file position
62 int64_t bufpos; // position at start of read
63 int64_t pagpos; // last search target position
66 int64_t file_begin, file_end;
80 int write_page(FILE *fp);
81 int64_t load(ogg_page *og);
84 class FileOGG : public FileBase
87 FileOGG(Asset *asset, File *file);
90 static void get_parameters(BC_WindowBase *parent_window,
91 Asset *asset, BC_WindowBase *&format_window,
92 int audio_options,int video_options,EDL *edl);
93 static int check_sig(Asset *asset);
94 int open_file(int rd,int wr);
97 int64_t get_video_position();
98 int64_t get_audio_position();
99 int set_video_position(int64_t pos);
100 int colormodel_supported(int colormodel);
101 int get_best_colormodel(Asset *asset,int driver);
102 int read_frame(VFrame *frame);
103 int set_audio_position(int64_t pos);
104 int read_samples(double *buffer,int64_t len);
105 int write_samples(double **buffer,int64_t len);
106 int write_frames(VFrame ***frames,int len);
110 int encode_theora_init();
111 int encode_vorbis_init();
112 int ogg_init_encode(FILE *out);
113 void close_encoder();
114 int decode_theora_init();
115 int decode_vorbis_init();
116 int ogg_init_decode(FILE *inp);
117 void close_decoder();
119 int write_samples_vorbis(double **buffer, int64_t len, int e_o_s);
120 int write_frames_theora(VFrame ***frames, int len, int e_o_s);
121 void flush_ogg(int e_o_s);
122 int write_audio_page();
123 int write_video_page();
128 int64_t frame_position;
129 int64_t sample_position;
135 #define HISTORY_MAX 0x100000
137 int64_t history_start;
138 int64_t history_size;
140 int ach, ahz; // audio channels, sample rate
141 int amn, amx; // audio min/max bitrate
142 int abr, avbr, aqu; // audio bitrate, variable bitrate, quality
143 int asz, afrmsz; // audio sample size, frame size
145 off_t file_begin, file_end;
146 sync_window_t *audiosync;
147 sync_window_t *videosync;
149 int64_t ogg_sample_pos(ogg_page *og);
150 int64_t ogg_next_sample_pos(ogg_page *og);
151 int64_t ogg_frame_pos(ogg_page *og);
152 int64_t ogg_next_frame_pos(ogg_page *og);
153 int ogg_read_buffer(FILE *in, sync_window_t *sy, int buflen);
154 int ogg_get_video_packet(ogg_packet *op);
155 int ogg_get_audio_packet(ogg_packet *op);
157 int ogg_get_page_of_sample(ogg_page *og, int64_t sample);
158 int ogg_seek_to_sample(int64_t sample);
159 int ogg_decode_more_samples();
161 int ogg_get_page_of_frame(ogg_page *og, int64_t frame);
162 int ogg_seek_to_keyframe(int64_t frame, int64_t *keyframe_number);
163 int move_history(int from, int to, int len);
165 ogg_stream_state to; // theora to ogg out
166 ogg_stream_state vo; // vorbis to ogg out
167 int64_t ogg_sample_position, ogg_frame_position;
168 int64_t next_sample_position, next_frame_position;
169 int64_t start_sample, last_sample; // first and last sample inside this file
170 int64_t start_frame, last_frame; // first and last frame inside this file
171 int64_t audio_pos, video_pos; // decoder last sample/frame in
172 int audio_eos, video_eos; // decoder sample/frame end of file
174 th_enc_ctx *enc; // theora video encode context
175 th_dec_ctx *dec; // theora video decode context
176 th_info ti; // theora video encoder init parameters
177 th_setup_info *ts; // theora video setup huff/quant codes
178 th_comment tc; // header init parameters
179 vorbis_info vi; // vorbis audio encoder init parameters
180 vorbis_comment vc; // header init parameters
181 vorbis_dsp_state vd; // vorbis decode audio context
182 vorbis_block vb; // vorbis decode buffering
187 int pic_x, pic_y, pic_w, pic_h;
188 int frame_w, frame_h;
189 int colorspace, pixfmt;
190 int bitrate, quality;
191 int keyframe_period, keyframe_force;
192 int fps_num, fps_den;
193 int aratio_num, aratio_den;
195 OGG_PageBfr apage, vpage;
196 double audiotime, videotime;
197 int keyframe_granule_shift;
198 int iframe_granule_offset;
202 class OGGConfigAudio;
203 class OGGConfigVideo;
205 class OGGVorbisFixedBitrate : public BC_Radial
208 OGGVorbisFixedBitrate(int x, int y, OGGConfigAudio *gui);
213 class OGGVorbisVariableBitrate : public BC_Radial
216 OGGVorbisVariableBitrate(int x, int y, OGGConfigAudio *gui);
221 class OGGVorbisMinBitrate : public BC_TextBox
224 OGGVorbisMinBitrate(int x,
232 class OGGVorbisMaxBitrate : public BC_TextBox
235 OGGVorbisMaxBitrate(int x,
243 class OGGVorbisAvgBitrate : public BC_TextBox
246 OGGVorbisAvgBitrate(int x,
255 class OGGConfigAudio: public BC_Window
258 OGGConfigAudio(BC_WindowBase *parent_window, Asset *asset);
261 void create_objects();
265 OGGVorbisFixedBitrate *fixed_bitrate;
266 OGGVorbisVariableBitrate *variable_bitrate;
268 BC_WindowBase *parent_window;
269 char string[BCTEXTLEN];
273 class OGGTheoraBitrate : public BC_TextBox
276 OGGTheoraBitrate(int x, int y, OGGConfigVideo *gui);
281 class OGGTheoraKeyframeFrequency : public BC_TumbleTextBox
284 OGGTheoraKeyframeFrequency(int x, int y, OGGConfigVideo *gui);
289 class OGGTheoraKeyframeForceFrequency : public BC_TumbleTextBox
292 OGGTheoraKeyframeForceFrequency(int x, int y, OGGConfigVideo *gui);
297 class OGGTheoraSharpness : public BC_TumbleTextBox
300 OGGTheoraSharpness(int x, int y, OGGConfigVideo *gui);
305 class OGGTheoraFixedBitrate : public BC_Radial
308 OGGTheoraFixedBitrate(int x, int y, OGGConfigVideo *gui);
313 class OGGTheoraFixedQuality : public BC_Radial
316 OGGTheoraFixedQuality(int x, int y, OGGConfigVideo *gui);
323 class OGGConfigVideo: public BC_Window
326 OGGConfigVideo(BC_WindowBase *parent_window, Asset *asset);
329 void create_objects();
332 OGGTheoraFixedBitrate *fixed_bitrate;
333 OGGTheoraFixedQuality *fixed_quality;
336 BC_WindowBase *parent_window;