initial commit
[goodguy/history.git] / cinelerra-5.0 / cinelerra / filemov.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 FILEMOV_H
23 #define FILEMOV_H
24
25 #include "bitspopup.inc"
26 #include "condition.inc"
27 #include "filebase.h"
28 #include "file.inc"
29
30 #ifdef HAVE_STDLIB_H
31 #undef HAVE_STDLIB_H
32 #endif
33
34 #include "libmjpeg.h"
35 #include "mutex.inc"
36 #include "quicktime.h"
37 #include "thread.h"
38
39 class FileMOVThread;
40
41 class ThreadStruct
42 {
43 public:
44         ThreadStruct();
45         ~ThreadStruct();
46
47         void load_output(mjpeg_t *mjpeg);
48
49         VFrame *input;
50         unsigned char *output;  // Output buffer
51         int64_t output_size;        // Size of output buffer
52         int64_t output_allocated;  // Allocation of output buffer
53         Condition *completion_lock;
54 };
55
56 class FileMOV : public FileBase
57 {
58 public:
59         FileMOV(Asset *asset, File *file);
60         ~FileMOV();
61
62         friend class FileMOVThread;
63
64         static void get_parameters(BC_WindowBase *parent_window, 
65                 Asset *asset, 
66                 BC_WindowBase* &format_window,
67                 int audio_options,
68                 int video_options,
69                 const char *locked_compressor);
70         static int check_sig(Asset *asset);
71
72         int open_file(int rd, int wr);
73         int close_file();
74         void asset_to_format();
75         void format_to_asset();
76         int64_t get_video_length();
77         int64_t get_audio_length();
78         int set_video_position(int64_t x);
79         int set_audio_position(int64_t x);
80         int write_samples(double **buffer, 
81                         int64_t len);
82         int write_frames(VFrame ***frames, int len);
83         int64_t compressed_frame_size();
84         int read_compressed_frame(VFrame *buffer);
85         int write_compressed_frame(VFrame *buffer);
86
87         int read_frame(VFrame *frame);
88         int read_samples(double *buffer, int64_t len);
89
90 // Direct copy routines
91         static int get_best_colormodel(Asset *asset, int driver);
92         int64_t get_memory_usage();
93         int colormodel_supported(int colormodel);
94         int can_copy_from(Edit *edit, int64_t position); // This file can copy frames directly from the asset
95         static const char *strtocompression(const char *string);
96         static const char *compressiontostr(const char *string);
97
98 // Fix codec to what AVI or MOV support
99         static void fix_codecs(Asset *asset);
100 // Check if resolutions match the DV codec when used
101         int check_codec_params(Asset *asset);
102
103 // set programme timecode
104         void set_frame_start(int64_t offset);
105
106 private:
107         void new_audio_temp(int64_t len);
108 // read raw audio data
109         int read_raw(char *buffer, int64_t samples, int track);  
110 // overlay raw frame from the current layer and position
111         int read_raw(VFrame *frame, 
112                 float in_x1, float in_y1, float in_x2, float in_y2,
113                 float out_x1, float out_y1, float out_x2, float out_y2, 
114                 int use_float, int interpolate);
115         int reset_parameters_derived();
116         int quicktime_atracks;
117         int quicktime_vtracks;
118 // current positions for when the file descriptor doesn't have the right position
119         quicktime_t *fd;
120         int depth;        // Depth in bits per pixel
121         int64_t frames_correction;  // Correction after 32bit overflow
122         int64_t samples_correction;  // Correction after 32bit overflow
123
124 // An array of frames for threads to look up and compress on their own.
125         ArrayList<ThreadStruct*> threadframes;
126
127         int total_threadframes;     // Number of thread frames in this buffer
128         int current_threadframe;    // Next threadframe to compress
129         Mutex *threadframe_lock;     // Lock threadframe array.
130
131         FileMOVThread **threads;   // One thread for every CPU
132         char prefix_path[1024];    // Prefix for new file when 2G limit is exceeded
133         int suffix_number;         // Number for new file
134
135 // Temp buffers for converting from double to float
136         float **temp_float;
137         int64_t temp_allocated;
138 };
139
140
141 // Encoder thread to parallelize certain compression formats, mainly JPEG.
142 // Only works when no alpha.
143
144 class FileMOVThread : public Thread
145 {
146 public:
147         FileMOVThread(FileMOV *filemov, int fields);
148         ~FileMOVThread();
149
150         int start_encoding();
151         int stop_encoding();
152         int encode_buffer();
153         void run();
154
155         ThreadStruct *threadframe;    // The frame currently being processed.
156         int done;
157         FileMOV *filemov;
158         Condition *input_lock;     // Wait for new array of threads or completion.
159         mjpeg_t *mjpeg;
160         int fields;
161 };
162
163
164 class MOVConfigAudioNum;
165 class MOVConfigAudioPopup;
166 class MOVConfigAudioToggle;
167
168 class MOVConfigAudio : public BC_Window
169 {
170 public:
171         MOVConfigAudio(BC_WindowBase *parent_window, Asset *asset);
172         ~MOVConfigAudio();
173
174         void create_objects();
175         int close_event();
176         void update_parameters();
177         void reset();
178
179         MOVConfigAudioPopup *compression_popup;
180         ArrayList<BC_ListBoxItem*> compression_items;
181         BC_WindowBase *parent_window;
182         BitsPopup *bits_popup;
183         BC_Title *bits_title;
184         BC_CheckBox *dither;
185         Asset *asset;
186         MOVConfigAudioNum *vorbis_min_bitrate;
187         MOVConfigAudioNum *vorbis_bitrate;
188         MOVConfigAudioNum *vorbis_max_bitrate;
189         MOVConfigAudioToggle *vorbis_vbr;
190
191         MOVConfigAudioNum *mp3_bitrate;
192         MOVConfigAudioNum *mp4a_bitrate;
193         MOVConfigAudioNum *mp4a_quantqual;
194 };
195
196
197 class MOVConfigAudioPopup : public BC_PopupTextBox
198 {
199 public:
200         MOVConfigAudioPopup(MOVConfigAudio *popup, int x, int y);
201         int handle_event();
202         MOVConfigAudio *popup;
203 };
204
205
206 class MOVConfigAudioToggle : public BC_CheckBox
207 {
208 public:
209         MOVConfigAudioToggle(MOVConfigAudio *popup,
210                 char *title_text,
211                 int x,
212                 int y,
213                 int *output);
214         int handle_event();
215         int *output;
216         MOVConfigAudio *popup;
217 };
218
219
220 class MOVConfigAudioNum : public BC_TumbleTextBox
221 {
222 public:
223         MOVConfigAudioNum(MOVConfigAudio *popup, 
224                 char *title_text, 
225                 int x, 
226                 int y, 
227                 int *output);
228         ~MOVConfigAudioNum();
229         
230         void create_objects();
231         int handle_event();
232         int *output;
233         MOVConfigAudio *popup;
234         BC_Title *title;
235         char *title_text;
236         int x, y;
237 };
238
239
240 class MOVConfigVideoPopup;
241 class MOVConfigVideoNum;
242 class MOVConfigVideoFix;
243 class MOVConfigVideoFixBitrate;
244 class MOVConfigVideoFixQuant;
245 class MOVConfigVideoCheckBox;
246
247
248 class MOVConfigVideo : public BC_Window
249 {
250 public:
251         MOVConfigVideo(BC_WindowBase *parent_window, 
252                 Asset *asset, 
253                 const char *locked_compressor);
254         ~MOVConfigVideo();
255
256         void create_objects();
257         int close_event();
258         void reset();
259
260         void update_parameters();
261
262         ArrayList<BC_ListBoxItem*> compression_items;
263         MOVConfigVideoPopup *compression_popup;
264         BC_WindowBase *parent_window;
265         Asset *asset;
266         int param_x, param_y;
267         const char *locked_compressor;
268
269         BC_ISlider *jpeg_quality;
270         BC_Title *jpeg_quality_title;
271
272         MOVConfigVideoNum *ms_bitrate;
273         MOVConfigVideoNum *ms_bitrate_tolerance;
274         MOVConfigVideoNum *ms_quantization;
275         MOVConfigVideoNum *ms_gop_size;
276         MOVConfigVideoCheckBox *ms_interlaced;
277         MOVConfigVideoFixBitrate *ms_fix_bitrate;
278         MOVConfigVideoFixQuant *ms_fix_quant;
279
280         MOVConfigVideoNum *divx_bitrate;
281         MOVConfigVideoNum *divx_quantizer;
282         MOVConfigVideoNum *divx_rc_period;
283         MOVConfigVideoNum *divx_rc_reaction_ratio;
284         MOVConfigVideoNum *divx_rc_reaction_period;
285         MOVConfigVideoNum *divx_max_key_interval;
286         MOVConfigVideoNum *divx_max_quantizer;
287         MOVConfigVideoNum *divx_min_quantizer;
288         MOVConfigVideoNum *divx_quality;
289         MOVConfigVideoFixBitrate *divx_fix_bitrate;
290         MOVConfigVideoFixQuant *divx_fix_quant;
291
292         MOVConfigVideoNum *h264_bitrate;
293         MOVConfigVideoNum *h264_quantizer;
294         MOVConfigVideoFixBitrate *h264_fix_bitrate;
295         MOVConfigVideoFixQuant *h264_fix_quant;
296 };
297
298 class MOVConfigVideoPopup : public BC_PopupTextBox
299 {
300 public:
301         MOVConfigVideoPopup(MOVConfigVideo *popup, int x, int y);
302         int handle_event();
303         MOVConfigVideo *popup;
304 };
305
306 class MOVConfigVideoFixBitrate : public BC_Radial
307 {
308 public:
309         MOVConfigVideoFixBitrate(int x,
310                 int y,
311                 int *output,
312                 int value);
313         int handle_event();
314         BC_Radial *opposite;
315         int *output;
316         int value;
317 };
318
319 class MOVConfigVideoFixQuant : public BC_Radial
320 {
321 public:
322         MOVConfigVideoFixQuant(int x,
323                 int y,
324                 int *output,
325                 int value);
326         int handle_event();
327         BC_Radial *opposite;
328         int *output;
329         int value;
330 };
331
332 class MOVConfigVideoCheckBox : public BC_CheckBox
333 {
334 public:
335         MOVConfigVideoCheckBox(char *title_text,
336                 int x,
337                 int y,
338                 int *output);
339         int handle_event();
340         int *output;
341 };
342
343 class MOVConfigVideoNum : public BC_TumbleTextBox
344 {
345 public:
346         MOVConfigVideoNum(MOVConfigVideo *popup,
347                 char *title_text,
348                 int x,
349                 int y,
350                 int *output);
351         MOVConfigVideoNum(MOVConfigVideo *popup,
352                 char *title_text,
353                 int x,
354                 int y,
355                 int min,
356                 int max,
357                 int *output);
358         ~MOVConfigVideoNum();
359
360         void create_objects();
361         int handle_event();
362         int *output;
363         MOVConfigVideo *popup;
364         BC_Title *title;
365         char *title_text;
366         int x, y;
367 };
368
369 #endif