initial commit
[goodguy/history.git] / cinelerra-5.0 / cinelerra / filejpeg.C
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 #include "asset.h"
23 #include "bcsignals.h"
24 #include "edit.h"
25 #include "file.h"
26 #include "filejpeg.h"
27 #include "jpegwrapper.h"
28 #include "language.h"
29 #include "libmjpeg.h"
30 #include "mwindow.inc"
31 #include "quicktime.h"
32 #include "vframe.h"
33 #include "videodevice.inc"
34 #include "mainerror.h"
35
36 #include <errno.h>
37 #include <string.h>
38
39
40 FileJPEG::FileJPEG(Asset *asset, File *file)
41  : FileList(asset, file, "JPEGLIST", ".jpg", FILE_JPEG, FILE_JPEG_LIST)
42 {
43         decompressor = 0;
44 }
45
46 FileJPEG::~FileJPEG()
47 {
48         if(decompressor) mjpeg_delete((mjpeg_t*)decompressor);
49 }
50
51
52 int FileJPEG::check_sig(Asset *asset)
53 {
54         FILE *stream = fopen(asset->path, "rb");
55
56         if(stream)
57         {
58                 char test[10];
59                 (void)fread(test, 10, 1, stream);
60                 fclose(stream);
61
62                 if(test[6] == 'J' && test[7] == 'F' && test[8] == 'I' && test[9] == 'F')
63                 {
64                         return 1;
65                 }
66                 else
67                 if(test[0] == 'J' && test[1] == 'P' && test[2] == 'E' && test[3] == 'G' && 
68                         test[4] == 'L' && test[5] == 'I' && test[6] == 'S' && test[7] == 'T')
69                 {
70                         return 1;
71                 }
72         }
73
74         if(strlen(asset->path) > 4)
75         {
76                 int len = strlen(asset->path);
77                 if(!strncasecmp(asset->path + len - 4, ".jpg", 4)) return 1;
78         }
79         return 0;
80 }
81
82
83
84 void FileJPEG::get_parameters(BC_WindowBase *parent_window, 
85         Asset *asset, 
86         BC_WindowBase* &format_window,
87         int audio_options,
88         int video_options)
89 {
90         if(video_options)
91         {
92                 JPEGConfigVideo *window = new JPEGConfigVideo(parent_window, asset);
93                 format_window = window;
94                 window->create_objects();
95                 window->run_window();
96                 delete window;
97         }
98 }
99
100
101 int FileJPEG::can_copy_from(Asset *asset, int64_t position)
102 {
103 //printf("FileJPEG::can_copy_from %d %s\n", asset->format, asset->vcodec);
104         if(asset->format == FILE_MOV)
105         {
106                 if(match4(asset->vcodec, QUICKTIME_JPEG)) return 1;
107         }
108         else
109         if(asset->format == FILE_JPEG || 
110                 asset->format == FILE_JPEG_LIST)
111                 return 1;
112
113         return 0;
114 }
115
116 int FileJPEG::colormodel_supported(int colormodel)
117 {
118         return colormodel;
119 }
120
121
122 int FileJPEG::get_best_colormodel(Asset *asset, int driver)
123 {
124         switch(driver)
125         {
126                 case PLAYBACK_X11:
127                         return BC_RGB888;
128                         break;
129                 case PLAYBACK_X11_XV:
130                 case PLAYBACK_DV1394:
131                 case PLAYBACK_FIREWIRE:
132                 case PLAYBACK_ASYNCHRONOUS:
133                         return BC_YUV420P;
134                         break;
135                 case PLAYBACK_X11_GL:
136                         return BC_YUV888;
137                         break;
138                 case PLAYBACK_LML:
139                 case PLAYBACK_BUZ:
140                         return BC_YUV422P;
141                         break;
142                 case VIDEO4LINUX:
143                 case VIDEO4LINUX2:
144                         return BC_YUV420P;
145                         break;
146                 case CAPTURE_BUZ:
147                 case CAPTURE_LML:
148                 case VIDEO4LINUX2JPEG:
149                         return BC_YUV422;
150                         break;
151                 case CAPTURE_FIREWIRE:
152                 case CAPTURE_IEC61883:
153                         return BC_YUV420P;
154                         break;
155                 case CAPTURE_DVB:
156                 case VIDEO4LINUX2MPEG:
157                         return BC_YUV422P;
158                         break;
159         }
160         return BC_YUV420P;
161 }
162
163
164 int FileJPEG::write_frame(VFrame *frame, VFrame *data, FrameWriterUnit *unit)
165 {
166         int result = 0;
167         JPEGUnit *jpeg_unit = (JPEGUnit*)unit;
168
169         if(!jpeg_unit->compressor)
170                 jpeg_unit->compressor = mjpeg_new(asset->width, 
171                         asset->height, 
172                         1);
173
174         mjpeg_set_quality((mjpeg_t*)jpeg_unit->compressor, asset->jpeg_quality);
175
176
177         mjpeg_compress((mjpeg_t*)jpeg_unit->compressor, 
178                 frame->get_rows(), 
179                 frame->get_y(), 
180                 frame->get_u(), 
181                 frame->get_v(),
182                 frame->get_color_model(),
183                 1);
184
185         data->allocate_compressed_data(mjpeg_output_size((mjpeg_t*)jpeg_unit->compressor));
186         data->set_compressed_size(mjpeg_output_size((mjpeg_t*)jpeg_unit->compressor));
187         memcpy(data->get_data(), 
188                 mjpeg_output_buffer((mjpeg_t*)jpeg_unit->compressor), 
189                 mjpeg_output_size((mjpeg_t*)jpeg_unit->compressor));
190
191         return result;
192 }
193
194
195
196
197
198
199
200
201
202
203 int FileJPEG::read_frame_header(char *path)
204 {
205         int result = 0;
206
207
208         FILE *stream;
209
210         if(!(stream = fopen(path, "rb")))
211         {
212                 eprintf("FileJPEG::read_frame_header %s: %m\n", path);
213                 return 1;
214         }
215         
216
217         unsigned char test[2];
218         (void)fread(test, 2, 1, stream);
219         if(test[0] != 0xff || test[1] != 0xd8)
220         {
221                 eprintf("FileJPEG::read_frame_header %s bad header %02x%02x\n",
222                         path, test[0], test[1]);
223                 fclose(stream);
224                 return 1;
225         }
226         fseek(stream, 0, SEEK_SET);
227
228         struct jpeg_decompress_struct jpeg_decompress;
229         struct jpeg_error_mgr jpeg_error;
230
231         jpeg_decompress.err = jpeg_std_error(&jpeg_error);
232         jpeg_create_decompress(&jpeg_decompress);
233
234         jpeg_stdio_src(&jpeg_decompress, stream);
235         jpeg_read_header(&jpeg_decompress, TRUE);
236
237         asset->width = jpeg_decompress.image_width;
238         asset->height = jpeg_decompress.image_height;
239
240         jpeg_destroy((j_common_ptr)&jpeg_decompress);
241         fclose(stream);
242
243         return result;
244 }
245
246
247
248 int FileJPEG::read_frame(VFrame *output, VFrame *input)
249 {
250         if(input->get_compressed_size() < 2 ||
251                 input->get_data()[0] != 0xff ||
252                 input->get_data()[1] != 0xd8)
253                 return 1;
254
255         if(!decompressor) decompressor = mjpeg_new(asset->width, 
256                 asset->height, 
257                 1);
258 // printf("FileJPEG::read_frame %d %p %d %d %d %p %p %p %p %d\n", 
259 // __LINE__,
260 // input->get_data(), 
261 // input->get_compressed_size(),
262 // output->get_w(),
263 // output->get_h(),
264 // output->get_rows(), 
265 // output->get_y(), 
266 // output->get_u(), 
267 // output->get_v(),
268 // output->get_color_model());
269         mjpeg_decompress((mjpeg_t*)decompressor, 
270                 input->get_data(), 
271                 input->get_compressed_size(),
272                 0,  
273                 output->get_rows(), 
274                 output->get_y(), 
275                 output->get_u(), 
276                 output->get_v(),
277                 output->get_color_model(),
278                 1);
279 //      PRINT_TRACE
280
281 //printf("FileJPEG::read_frame %d\n", __LINE__);
282         return 0;
283 }
284
285 FrameWriterUnit* FileJPEG::new_writer_unit(FrameWriter *writer)
286 {
287         return new JPEGUnit(this, writer);
288 }
289
290
291
292
293
294
295 JPEGUnit::JPEGUnit(FileJPEG *file, FrameWriter *writer)
296  : FrameWriterUnit(writer)
297 {
298         this->file = file;
299         compressor = 0;
300 }
301 JPEGUnit::~JPEGUnit()
302 {
303         if(compressor) mjpeg_delete((mjpeg_t*)compressor);
304 }
305
306
307
308
309
310
311
312 JPEGConfigVideo::JPEGConfigVideo(BC_WindowBase *parent_window, Asset *asset)
313  : BC_Window(PROGRAM_NAME ": Video Compression",
314         parent_window->get_abs_cursor_x(1),
315         parent_window->get_abs_cursor_y(1),
316         400,
317         100)
318 {
319         this->parent_window = parent_window;
320         this->asset = asset;
321 }
322
323 JPEGConfigVideo::~JPEGConfigVideo()
324 {
325 }
326
327 void JPEGConfigVideo::create_objects()
328 {
329         int x = 10, y = 10;
330         lock_window("JPEGConfigVideo::create_objects");
331         add_subwindow(new BC_Title(x, y, _("Quality:")));
332         add_subwindow(new BC_ISlider(x + 80,
333                 y,
334                 0,
335                 200,
336                 200,
337                 0,
338                 100,
339                 asset->jpeg_quality,
340                 0,
341                 0,
342                 &asset->jpeg_quality));
343
344         add_subwindow(new BC_OKButton(this));
345         show_window(1);
346         unlock_window();
347 }
348
349 int JPEGConfigVideo::close_event()
350 {
351         set_done(0);
352         return 1;
353 }
354
355
356