Credit Andrew - improve in-tree documentation
[goodguy/cinelerra.git] / 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 "interlacemodes.h"
28 #include "jpegwrapper.h"
29 #include "language.h"
30 #include "libmjpeg.h"
31 #include "mwindow.inc"
32 #include "vframe.h"
33 #include "videodevice.inc"
34 #include "mainerror.h"
35
36
37 FileJPEG::FileJPEG(Asset *asset, File *file)
38  : FileList(asset, file, "JPEGLIST", ".jpg", FILE_JPEG, FILE_JPEG_LIST)
39 {
40         decompressor = 0;
41 }
42
43 FileJPEG::~FileJPEG()
44 {
45         if(decompressor) mjpeg_delete((mjpeg_t*)decompressor);
46 }
47
48
49 int FileJPEG::check_sig(Asset *asset)
50 {
51         FILE *fp = fopen(asset->path, "r");
52         if( !fp ) return 0;
53         char test[10];
54         int result = -1;
55         if( fread(test, 1, sizeof(test), fp) == sizeof(test) ) {
56                 if( test[6] == 'J' && test[7] == 'F' && test[8] == 'I' && test[9] == 'F' ) {
57                         fseek(fp, 0, SEEK_SET);
58                         int w = 0, h = 0;
59                         result = read_header(fp, w, h);
60                 }
61                 else if(test[0] == 'J' && test[1] == 'P' && test[2] == 'E' && test[3] == 'G' &&
62                         test[4] == 'L' && test[5] == 'I' && test[6] == 'S' && test[7] == 'T') {
63                         result = 0;
64                 }
65         }
66         fclose(fp);
67
68         if( result < 0 ) {
69                 int i = strlen(asset->path) - 4;
70                 if( i >= 0 && !strcasecmp(asset->path+i, ".jpg") )
71                         result = 0;
72         }
73         return !result ? 1 : 0;
74 }
75
76
77 void FileJPEG::get_parameters(BC_WindowBase *parent_window,
78         Asset *asset, BC_WindowBase* &format_window,
79         int audio_options, int video_options, EDL *edl)
80 {
81         if(video_options)
82         {
83                 JPEGConfigVideo *window = new JPEGConfigVideo(parent_window, asset);
84                 format_window = window;
85                 window->create_objects();
86                 window->run_window();
87                 delete window;
88         }
89 }
90
91
92 int FileJPEG::can_copy_from(Asset *asset, int64_t position)
93 {
94 //printf("FileJPEG::can_copy_from %d %s\n", asset->format, asset->vcodec);
95         if(asset->format == FILE_JPEG ||
96                 asset->format == FILE_JPEG_LIST)
97                 return 1;
98
99         return 0;
100 }
101
102 int FileJPEG::colormodel_supported(int colormodel)
103 {
104         return colormodel;
105 }
106
107
108 int FileJPEG::get_best_colormodel(Asset *asset, int driver)
109 {
110         switch(driver)
111         {
112                 case PLAYBACK_X11:
113                         return BC_RGB888;
114                         break;
115                 case PLAYBACK_X11_XV:
116                 case PLAYBACK_DV1394:
117                 case PLAYBACK_FIREWIRE:
118                 case PLAYBACK_ASYNCHRONOUS:
119                         return BC_YUV420P;
120                         break;
121                 case PLAYBACK_X11_GL:
122                         return BC_YUV888;
123                         break;
124                 case VIDEO4LINUX2:
125                         return BC_YUV420P;
126                         break;
127                 case VIDEO4LINUX2JPEG:
128                         return BC_YUV422;
129                         break;
130                 case CAPTURE_FIREWIRE:
131                 case CAPTURE_IEC61883:
132                         return BC_YUV420P;
133                         break;
134                 case CAPTURE_DVB:
135                 case VIDEO4LINUX2MPEG:
136                         return BC_YUV422P;
137                         break;
138         }
139         return BC_YUV420P;
140 }
141
142
143 int FileJPEG::write_frame(VFrame *frame, VFrame *data, FrameWriterUnit *unit)
144 {
145         int result = 0;
146         JPEGUnit *jpeg_unit = (JPEGUnit*)unit;
147
148         if(!jpeg_unit->compressor)
149                 jpeg_unit->compressor = mjpeg_new(asset->width,
150                         asset->height,
151                         1);
152
153         mjpeg_set_quality((mjpeg_t*)jpeg_unit->compressor, asset->jpeg_quality);
154
155
156         mjpeg_compress((mjpeg_t*)jpeg_unit->compressor,
157                 frame->get_rows(),
158                 frame->get_y(),
159                 frame->get_u(),
160                 frame->get_v(),
161                 frame->get_color_model(),
162                 1);
163
164 // insert spherical tag
165         if(asset->jpeg_sphere)
166         {
167                 const char *sphere_tag = 
168                         "http://ns.adobe.com/xap/1.0/\x00<?xpacket begin='\xef\xbb\xbf' id='W5M0MpCehiHzreSzNTczkc9d'?>\n"
169                         "<x:xmpmeta xmlns:x='adobe:ns:meta/' x:xmptk='Image::Cinelerra'>\n"
170                         "<rdf:RDF xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'>\n"
171                         "\n"
172                         " <rdf:Description rdf:about=''\n"
173                         "  xmlns:GPano='http://ns.google.com/photos/1.0/panorama/'>\n"
174                         "  <GPano:ProjectionType>equirectangular</GPano:ProjectionType>\n"
175                         " </rdf:Description>\n"
176                         "</rdf:RDF>\n"
177                         "</x:xmpmeta>\n"
178                         "<?xpacket end='w'?>";
179
180 // calculate length by skipping the \x00 byte
181                 int skip = 32;
182                 int tag_len = strlen(sphere_tag + skip) + skip;
183                 int tag_len2 = tag_len + 2;
184                 int tag_len3 = tag_len + 4;
185                 
186                 data->allocate_compressed_data(
187                         mjpeg_output_size((mjpeg_t*)jpeg_unit->compressor) + tag_len3);
188                 data->set_compressed_size(
189                         mjpeg_output_size((mjpeg_t*)jpeg_unit->compressor) + tag_len3);
190                         
191                 int jfif_size = 0x14;
192                 uint8_t *ptr = data->get_data();
193                 memcpy(ptr, 
194                         mjpeg_output_buffer((mjpeg_t*)jpeg_unit->compressor), 
195                         jfif_size);
196                 ptr += jfif_size;
197                 *ptr++ = 0xff;
198                 *ptr++ = 0xe1;
199                 *ptr++ = (tag_len2 >> 8) & 0xff;
200                 *ptr++ = tag_len2 & 0xff;
201                 memcpy(ptr,
202                         sphere_tag,
203                         tag_len);
204                 ptr += tag_len;
205                 memcpy(ptr,
206                         mjpeg_output_buffer((mjpeg_t*)jpeg_unit->compressor) + jfif_size,
207                         mjpeg_output_size((mjpeg_t*)jpeg_unit->compressor) - jfif_size);
208         }
209         else
210         {
211                 data->allocate_compressed_data(mjpeg_output_size((mjpeg_t*)jpeg_unit->compressor));
212                 data->set_compressed_size(mjpeg_output_size((mjpeg_t*)jpeg_unit->compressor));
213                 memcpy(data->get_data(), 
214                         mjpeg_output_buffer((mjpeg_t*)jpeg_unit->compressor), 
215                         mjpeg_output_size((mjpeg_t*)jpeg_unit->compressor));
216         }
217         data->allocate_compressed_data(mjpeg_output_size((mjpeg_t*)jpeg_unit->compressor));
218         data->set_compressed_size(mjpeg_output_size((mjpeg_t*)jpeg_unit->compressor));
219         memcpy(data->get_data(),
220                 mjpeg_output_buffer((mjpeg_t*)jpeg_unit->compressor),
221                 mjpeg_output_size((mjpeg_t*)jpeg_unit->compressor));
222
223         return result;
224 }
225
226
227
228
229
230
231
232
233
234
235 int FileJPEG::read_frame_header(char *path)
236 {
237         FILE *fp = fopen(path, "rb");
238         if( !fp ) {
239                 eprintf("FileJPEG::read_frame_header %s: %m\n", path);
240                 return 1;
241         }
242         int w = 0, h = 0, result = 1;
243         unsigned char test[2];
244         if( fread(test, 1, sizeof(test), fp) == sizeof(test) &&
245             test[0] == 0xff && test[1] == 0xd8 ) {
246                 fseek(fp, 0, SEEK_SET);
247                 result = read_header(fp, w, h);
248         }
249         fclose(fp);
250         if( !result ) {
251                 asset->width = w;  asset->height = h;
252                 asset->interlace_mode = ILACE_MODE_NOTINTERLACED;
253         }
254         else
255                 eprintf("FileJPEG::read_frame_header %s bad header\n", path);
256         return result;
257 }
258
259 int FileJPEG::read_header(FILE *fp, int &w, int &h)
260 {
261         int result = 0;
262         struct jpeg_error_mgr jpeg_error;
263         struct jpeg_decompress_struct jpeg_decompress;
264         jpeg_decompress.err = jpeg_std_error(&jpeg_error);
265         jpeg_create_decompress(&jpeg_decompress);
266         jpeg_stdio_src(&jpeg_decompress, fp);
267         if( jpeg_read_header(&jpeg_decompress, TRUE) != JPEG_HEADER_OK ) result = 1;
268         if( !result && jpeg_decompress.jpeg_color_space != JCS_YCbCr ) result = 1;
269         if( !result && jpeg_decompress.comp_info[0].h_samp_factor > 2 ) result = 1;
270         if( !result && jpeg_decompress.comp_info[0].v_samp_factor > 2 ) result = 1;
271         if( !result ) {
272                 w = jpeg_decompress.image_width;
273                 h = jpeg_decompress.image_height;
274         }
275         jpeg_destroy((j_common_ptr)&jpeg_decompress);
276         return result;
277 }
278
279
280 int FileJPEG::read_frame(VFrame *output, VFrame *input)
281 {
282         if(input->get_compressed_size() < 2 ||
283                 input->get_data()[0] != 0xff ||
284                 input->get_data()[1] != 0xd8)
285                 return 1;
286
287         if(!decompressor) decompressor = mjpeg_new(asset->width,
288                 asset->height,
289                 1);
290 // printf("FileJPEG::read_frame %d %p %d %d %d %p %p %p %p %d\n",
291 // __LINE__,
292 // input->get_data(),
293 // input->get_compressed_size(),
294 // output->get_w(),
295 // output->get_h(),
296 // output->get_rows(),
297 // output->get_y(),
298 // output->get_u(),
299 // output->get_v(),
300 // output->get_color_model());
301         mjpeg_decompress((mjpeg_t*)decompressor,
302                 input->get_data(),
303                 input->get_compressed_size(),
304                 0,
305                 output->get_rows(),
306                 output->get_y(),
307                 output->get_u(),
308                 output->get_v(),
309                 output->get_color_model(),
310                 1);
311 //      PRINT_TRACE
312
313 //printf("FileJPEG::read_frame %d\n", __LINE__);
314         return 0;
315 }
316
317 FrameWriterUnit* FileJPEG::new_writer_unit(FrameWriter *writer)
318 {
319         return new JPEGUnit(this, writer);
320 }
321
322
323
324
325
326
327 JPEGUnit::JPEGUnit(FileJPEG *file, FrameWriter *writer)
328  : FrameWriterUnit(writer)
329 {
330         this->file = file;
331         compressor = 0;
332 }
333 JPEGUnit::~JPEGUnit()
334 {
335         if(compressor) mjpeg_delete((mjpeg_t*)compressor);
336 }
337
338
339
340
341
342
343
344 JPEGConfigVideo::JPEGConfigVideo(BC_WindowBase *parent_window, Asset *asset)
345  : BC_Window(_(PROGRAM_NAME ": Video Compression"),
346         parent_window->get_abs_cursor_x(1),
347         parent_window->get_abs_cursor_y(1),
348         400, 200)
349 {
350         this->parent_window = parent_window;
351         this->asset = asset;
352 }
353
354 JPEGConfigVideo::~JPEGConfigVideo()
355 {
356 }
357
358 void JPEGConfigVideo::create_objects()
359 {
360         int x = 10, y = 10;
361         lock_window("JPEGConfigVideo::create_objects");
362         add_subwindow(new BC_Title(x, y, _("Quality:")));
363         BC_ISlider *slider;
364         add_subwindow(slider = new BC_ISlider(x + 80, y,
365                 0, 200, 200, 0, 100, asset->jpeg_quality, 0, 0,
366                 &asset->jpeg_quality));
367         y += slider->get_h() + 10;
368         add_subwindow(new BC_CheckBox(x, y, 
369                 &asset->jpeg_sphere, _("Tag for spherical playback")));
370
371         add_subwindow(new BC_OKButton(this));
372         show_window(1);
373         unlock_window();
374 }
375
376 int JPEGConfigVideo::close_event()
377 {
378         set_done(0);
379         return 1;
380 }
381
382
383