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