Merge CV, ver=5.1; ops/methods from HV, and interface from CV where possible
[goodguy/history.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
25 #include "../config.h"
26 #include "filebase.h"
27 #include "file.inc"
28 #include "packagingengine.h"
29
30 #include <theora/theora.h>
31 #include <vorbis/codec.h>
32 #include <vorbis/vorbisenc.h>
33
34
35 #include <libdv/dv.h>
36
37 /* This code was aspired by ffmpeg2theora */
38 /* Special thanks for help on this code goes out to j@v2v.cc */
39
40 typedef struct
41 {
42         ogg_sync_state sync;
43         off_t file_bufpos; // position of the start of the buffer inside the file
44         off_t file_pagepos; // position of the page that will be next read
45         off_t file_pagepos_found; // position of last page that was returned (in seeking operations)
46         int wlen;
47 } sync_window_t;
48
49 typedef struct
50 {
51     ogg_page audiopage;
52     ogg_page videopage;
53
54     double audiotime;
55     double videotime;
56     ogg_int64_t audio_bytesout;
57     ogg_int64_t video_bytesout;
58
59     ogg_page og;    /* one Ogg bitstream page.  Vorbis packets are inside */
60     ogg_packet op;  /* one raw packet of data for decode */
61
62     theora_info ti;
63     theora_comment tc;
64     theora_state td;
65
66     vorbis_info vi;       /* struct that stores all the static vorbis bitstream settings */
67     vorbis_comment vc;    /* struct that stores all the user comments */
68     vorbis_dsp_state vd; /* central working state for the packet<->PCM encoder/decoder */
69     vorbis_block vb;     /* local working space for packet<->PCM encode/decode */
70
71     /* used for muxing */
72     ogg_stream_state to;    /* take physical pages, weld into a logical
73                              * stream of packets */
74     ogg_stream_state vo;    /* take physical pages, weld into a logical
75                              * stream of packets */
76
77     int apage_valid;
78     int vpage_valid;
79     unsigned char *apage;
80     unsigned char *vpage;
81     int vpage_len;
82     int apage_len;
83     int vpage_buffer_length;
84     int apage_buffer_length;
85
86
87 // stuff needed for reading only
88         sync_window_t *audiosync;
89         sync_window_t *videosync;
90
91     //to do some manual page flusing
92     int v_pkg;
93     int a_pkg;
94
95 }
96 theoraframes_info_t;
97
98 class FileOGG : public FileBase
99 {
100 friend class PackagingEngineOGG;
101 public:
102         FileOGG(Asset *asset, File *file);
103         ~FileOGG();
104
105         static void get_parameters(BC_WindowBase *parent_window,
106                 Asset *asset,
107                 BC_WindowBase* &format_window,
108                 int audio_options,
109                 int video_options);
110
111         int reset_parameters_derived();
112         int open_file(int rd, int wr);
113         static int check_sig(Asset *asset);
114         int close_file();
115         int close_file_derived();
116         int64_t get_video_position();
117         int64_t get_audio_position();
118         int set_video_position(int64_t x);
119         int set_audio_position(int64_t x);
120         int colormodel_supported(int colormodel);
121         int get_best_colormodel(Asset *asset, int driver);
122         int write_samples(double **buffer, int64_t len);
123         int write_frames(VFrame ***frames, int len);
124         int read_samples(double *buffer, int64_t len);
125         int read_frame(VFrame *frame);
126
127 private:
128         int write_samples_vorbis(double **buffer, int64_t len, int e_o_s);
129         int write_frames_theora(VFrame ***frames, int len, int e_o_s);
130         void flush_ogg(int e_o_s);
131         int write_audio_page();
132         int write_video_page();
133
134         FILE *stream;
135         off_t file_length;
136
137         theoraframes_info_t *tf;
138         VFrame *temp_frame;
139         Mutex *flush_lock;
140
141         off_t filedata_begin;
142
143         int ogg_get_last_page(sync_window_t *sw, long serialno, ogg_page *og);
144         int ogg_get_prev_page(sync_window_t *sw, long serialno, ogg_page *og);
145         int ogg_get_first_page(sync_window_t *sw, long serialno, ogg_page *og);
146         int ogg_get_next_page(sync_window_t *sw, long serialno, ogg_page *og);
147         int ogg_sync_and_get_next_page(sync_window_t *sw, long serialno, ogg_page *og);
148
149         int ogg_get_page_of_sample(sync_window_t *sw, long serialno, ogg_page *og, int64_t sample);
150         int ogg_seek_to_sample(sync_window_t *sw, long serialno, int64_t sample);
151         int ogg_decode_more_samples(sync_window_t *sw, long serialno);
152
153         int ogg_get_page_of_frame(sync_window_t *sw, long serialno, ogg_page *og, int64_t frame);
154         int ogg_seek_to_keyframe(sync_window_t *sw, long serialno, int64_t frame, int64_t *keyframe_number);
155         int ogg_seek_to_databegin(sync_window_t *sw, long serialno);
156
157
158         int64_t start_sample; // first and last sample inside this file
159         int64_t last_sample;
160         int64_t start_frame; // first and last frame inside this file
161         int64_t last_frame;
162
163
164         int64_t ogg_sample_position;  // what will be the next sample taken from vorbis decoder
165         int64_t next_sample_position; // what is the next sample read_samples must deliver
166
167         int move_history(int from, int to, int len);
168
169         float **pcm_history;
170 #ifndef HISTORY_MAX
171 #define HISTORY_MAX 0x100000
172 #endif
173         int64_t history_start;
174         int64_t history_size;
175
176         int theora_cmodel;
177         int64_t ogg_frame_position;    // LAST decoded frame position
178         int64_t next_frame_position;   // what is the next sample read_frames must deliver
179         char theora_keyframe_granule_shift;
180         int final_write;
181 };
182
183 class OGGConfigAudio;
184 class OGGConfigVideo;
185
186 class OGGVorbisFixedBitrate : public BC_Radial
187 {
188 public:
189         OGGVorbisFixedBitrate(int x, int y, OGGConfigAudio *gui);
190         int handle_event();
191         OGGConfigAudio *gui;
192 };
193
194 class OGGVorbisVariableBitrate : public BC_Radial
195 {
196 public:
197         OGGVorbisVariableBitrate(int x, int y, OGGConfigAudio *gui);
198         int handle_event();
199         OGGConfigAudio *gui;
200 };
201
202 class OGGVorbisMinBitrate : public BC_TextBox
203 {
204 public:
205         OGGVorbisMinBitrate(int x,
206                 int y,
207                 OGGConfigAudio *gui,
208                 char *text);
209         int handle_event();
210         OGGConfigAudio *gui;
211 };
212
213 class OGGVorbisMaxBitrate : public BC_TextBox
214 {
215 public:
216         OGGVorbisMaxBitrate(int x,
217                 int y,
218                 OGGConfigAudio *gui,
219                 char *text);
220         int handle_event();
221         OGGConfigAudio *gui;
222 };
223
224 class OGGVorbisAvgBitrate : public BC_TextBox
225 {
226 public:
227         OGGVorbisAvgBitrate(int x,
228                 int y,
229                 OGGConfigAudio *gui,
230                 char *text);
231         int handle_event();
232         OGGConfigAudio *gui;
233 };
234
235
236 class OGGConfigAudio: public BC_Window
237 {
238 public:
239         OGGConfigAudio(BC_WindowBase *parent_window, Asset *asset);
240         ~OGGConfigAudio();
241
242         void create_objects();
243         int close_event();
244
245         Asset *asset;
246         OGGVorbisFixedBitrate *fixed_bitrate;
247         OGGVorbisVariableBitrate *variable_bitrate;
248 private:
249         BC_WindowBase *parent_window;
250         char string[BCTEXTLEN];
251 };
252
253
254 class OGGTheoraBitrate : public BC_TextBox
255 {
256 public:
257         OGGTheoraBitrate(int x, int y, OGGConfigVideo *gui);
258         int handle_event();
259         OGGConfigVideo *gui;
260 };
261
262 class OGGTheoraKeyframeFrequency : public BC_TumbleTextBox
263 {
264 public:
265         OGGTheoraKeyframeFrequency(int x, int y, OGGConfigVideo *gui);
266         int handle_event();
267         OGGConfigVideo *gui;
268 };
269
270 class OGGTheoraKeyframeForceFrequency : public BC_TumbleTextBox
271 {
272 public:
273         OGGTheoraKeyframeForceFrequency(int x, int y, OGGConfigVideo *gui);
274         int handle_event();
275         OGGConfigVideo *gui;
276 };
277
278 class OGGTheoraSharpness : public BC_TumbleTextBox
279 {
280 public:
281         OGGTheoraSharpness(int x, int y, OGGConfigVideo *gui);
282         int handle_event();
283         OGGConfigVideo *gui;
284 };
285
286 class OGGTheoraFixedBitrate : public BC_Radial
287 {
288 public:
289         OGGTheoraFixedBitrate(int x, int y, OGGConfigVideo *gui);
290         int handle_event();
291         OGGConfigVideo *gui;
292 };
293
294 class OGGTheoraFixedQuality : public BC_Radial
295 {
296 public:
297         OGGTheoraFixedQuality(int x, int y, OGGConfigVideo *gui);
298         int handle_event();
299         OGGConfigVideo *gui;
300 };
301
302
303
304 class OGGConfigVideo: public BC_Window
305 {
306 public:
307         OGGConfigVideo(BC_WindowBase *parent_window, Asset *asset);
308         ~OGGConfigVideo();
309
310         void create_objects();
311         int close_event();
312
313         OGGTheoraFixedBitrate *fixed_bitrate;
314         OGGTheoraFixedQuality *fixed_quality;
315         Asset *asset;
316 private:
317         BC_WindowBase *parent_window;
318 };
319
320 class PackagingEngineOGG : public PackagingEngine
321 {
322 public:
323         PackagingEngineOGG();
324         ~PackagingEngineOGG();
325         int create_packages_single_farm(
326                 EDL *edl,
327                 Preferences *preferences,
328                 Asset *default_asset,
329                 double total_start,
330                 double total_end);
331         RenderPackage* get_package_single_farm(double frames_per_second,
332                 int client_number,
333                 int use_local_rate);
334         int64_t get_progress_max();
335         void get_package_paths(ArrayList<char*> *path_list);
336         int packages_are_done();
337
338 private:
339         EDL *edl;
340
341         RenderPackage **packages;
342         int total_packages;
343         double video_package_len;    // Target length of a single package
344
345         Asset *default_asset;
346         Preferences *preferences;
347         int current_package;
348         double total_start;
349         double total_end;
350         int64_t audio_position;
351         int64_t video_position;
352         int64_t audio_start;
353         int64_t video_start;
354         int64_t audio_end;
355         int64_t video_end;
356
357 };
358
359
360 #endif