switch move/swap tracks, add mv trk shortcut, update msg
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / fileogg.h
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #ifndef FILEOGG_H
23 #define FILEOGG_H
24 #ifdef HAVE_OGG
25
26 #include "edl.inc"
27 #include "filebase.h"
28 #include "file.inc"
29 #include "mutex.inc"
30
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>
36
37
38 /* This code was aspired by ffmpeg2theora */
39 /* Special thanks for help on this code goes out to j@v2v.cc */
40
41 #define mn_pagesz 32
42
43 class sync_window_t : public ogg_sync_state
44 {
45 public:
46         sync_window_t(FILE *fp, Mutex *sync_lock, int64_t begin=0, int64_t end=0);
47         ~sync_window_t();
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);
60
61         int64_t filepos; // current file position
62         int64_t bufpos;  // position at start of read
63         int64_t pagpos;  // last search target position
64         FILE *fp;
65         Mutex *sync_lock;
66         int64_t file_begin, file_end;
67 };
68
69 class OGG_PageBfr
70 {
71 public:
72         int allocated, len;
73         int valid, packets;
74         int64_t position;
75         uint8_t *page;
76
77         OGG_PageBfr();
78         ~OGG_PageBfr();
79         void demand(int sz);
80         int write_page(FILE *fp);
81         int64_t load(ogg_page *og);
82 };
83
84 class FileOGG : public FileBase
85 {
86 public:
87         FileOGG(Asset *asset, File *file);
88         ~FileOGG();
89
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);
95         int close_file();
96
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);
107
108 private:
109         void init();
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();
118
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();
124
125         FILE *inp, *out;
126         off_t file_length;
127         int audio, video;
128         int64_t frame_position;
129         int64_t sample_position;
130
131         VFrame *temp_frame;
132         Mutex *file_lock;
133         float **pcm_history;
134 #ifndef HISTORY_MAX
135 #define HISTORY_MAX 0x100000
136 #endif
137         int64_t history_start;
138         int64_t history_size;
139         int pcm_channels;
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
144
145         off_t file_begin, file_end;
146         sync_window_t *audiosync;
147         sync_window_t *videosync;
148
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);
156
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();
160
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);
164
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
173
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
183         int force_keyframes;
184         int vp3_compatible;
185         int soft_target;
186
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;
194
195         OGG_PageBfr apage, vpage;
196         double audiotime, videotime;
197         int keyframe_granule_shift;
198         int iframe_granule_offset;
199         int theora_cmodel;
200 };
201
202 class OGGConfigAudio;
203 class OGGConfigVideo;
204
205 class OGGVorbisFixedBitrate : public BC_Radial
206 {
207 public:
208         OGGVorbisFixedBitrate(int x, int y, OGGConfigAudio *gui);
209         int handle_event();
210         OGGConfigAudio *gui;
211 };
212
213 class OGGVorbisVariableBitrate : public BC_Radial
214 {
215 public:
216         OGGVorbisVariableBitrate(int x, int y, OGGConfigAudio *gui);
217         int handle_event();
218         OGGConfigAudio *gui;
219 };
220
221 class OGGVorbisMinBitrate : public BC_TextBox
222 {
223 public:
224         OGGVorbisMinBitrate(int x,
225                 int y,
226                 OGGConfigAudio *gui,
227                 char *text);
228         int handle_event();
229         OGGConfigAudio *gui;
230 };
231
232 class OGGVorbisMaxBitrate : public BC_TextBox
233 {
234 public:
235         OGGVorbisMaxBitrate(int x,
236                 int y,
237                 OGGConfigAudio *gui,
238                 char *text);
239         int handle_event();
240         OGGConfigAudio *gui;
241 };
242
243 class OGGVorbisAvgBitrate : public BC_TextBox
244 {
245 public:
246         OGGVorbisAvgBitrate(int x,
247                 int y,
248                 OGGConfigAudio *gui,
249                 char *text);
250         int handle_event();
251         OGGConfigAudio *gui;
252 };
253
254
255 class OGGConfigAudio: public BC_Window
256 {
257 public:
258         OGGConfigAudio(BC_WindowBase *parent_window, Asset *asset);
259         ~OGGConfigAudio();
260
261         void create_objects();
262         int close_event();
263
264         Asset *asset;
265         OGGVorbisFixedBitrate *fixed_bitrate;
266         OGGVorbisVariableBitrate *variable_bitrate;
267 private:
268         BC_WindowBase *parent_window;
269         char string[BCTEXTLEN];
270 };
271
272
273 class OGGTheoraBitrate : public BC_TextBox
274 {
275 public:
276         OGGTheoraBitrate(int x, int y, OGGConfigVideo *gui);
277         int handle_event();
278         OGGConfigVideo *gui;
279 };
280
281 class OGGTheoraKeyframeFrequency : public BC_TumbleTextBox
282 {
283 public:
284         OGGTheoraKeyframeFrequency(int x, int y, OGGConfigVideo *gui);
285         int handle_event();
286         OGGConfigVideo *gui;
287 };
288
289 class OGGTheoraKeyframeForceFrequency : public BC_TumbleTextBox
290 {
291 public:
292         OGGTheoraKeyframeForceFrequency(int x, int y, OGGConfigVideo *gui);
293         int handle_event();
294         OGGConfigVideo *gui;
295 };
296
297 class OGGTheoraSharpness : public BC_TumbleTextBox
298 {
299 public:
300         OGGTheoraSharpness(int x, int y, OGGConfigVideo *gui);
301         int handle_event();
302         OGGConfigVideo *gui;
303 };
304
305 class OGGTheoraFixedBitrate : public BC_Radial
306 {
307 public:
308         OGGTheoraFixedBitrate(int x, int y, OGGConfigVideo *gui);
309         int handle_event();
310         OGGConfigVideo *gui;
311 };
312
313 class OGGTheoraFixedQuality : public BC_Radial
314 {
315 public:
316         OGGTheoraFixedQuality(int x, int y, OGGConfigVideo *gui);
317         int handle_event();
318         OGGConfigVideo *gui;
319 };
320
321
322
323 class OGGConfigVideo: public BC_Window
324 {
325 public:
326         OGGConfigVideo(BC_WindowBase *parent_window, Asset *asset);
327         ~OGGConfigVideo();
328
329         void create_objects();
330         int close_event();
331
332         OGGTheoraFixedBitrate *fixed_bitrate;
333         OGGTheoraFixedQuality *fixed_quality;
334         Asset *asset;
335 private:
336         BC_WindowBase *parent_window;
337 };
338
339 #endif
340 #endif