4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
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.
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.
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
23 #include "bcsignals.h"
27 #include "interlacemodes.h"
28 #include "jpegwrapper.h"
31 #include "mwindow.inc"
33 #include "videodevice.inc"
34 #include "mainerror.h"
37 FileJPEG::FileJPEG(Asset *asset, File *file)
38 : FileList(asset, file, "JPEGLIST", ".jpg", FILE_JPEG, FILE_JPEG_LIST)
45 if(decompressor) mjpeg_delete((mjpeg_t*)decompressor);
49 int FileJPEG::check_sig(Asset *asset)
51 FILE *fp = fopen(asset->path, "r");
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);
59 result = read_header(fp, w, h);
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') {
69 int i = strlen(asset->path) - 4;
70 if( i >= 0 && !strcasecmp(asset->path+i, ".jpg") )
73 return !result ? 1 : 0;
77 void FileJPEG::get_parameters(BC_WindowBase *parent_window,
79 BC_WindowBase* &format_window,
85 JPEGConfigVideo *window = new JPEGConfigVideo(parent_window, asset);
86 format_window = window;
87 window->create_objects();
94 int FileJPEG::can_copy_from(Asset *asset, int64_t position)
96 //printf("FileJPEG::can_copy_from %d %s\n", asset->format, asset->vcodec);
97 if(asset->format == FILE_JPEG ||
98 asset->format == FILE_JPEG_LIST)
104 int FileJPEG::colormodel_supported(int colormodel)
110 int FileJPEG::get_best_colormodel(Asset *asset, int driver)
117 case PLAYBACK_X11_XV:
118 case PLAYBACK_DV1394:
119 case PLAYBACK_FIREWIRE:
120 case PLAYBACK_ASYNCHRONOUS:
123 case PLAYBACK_X11_GL:
129 case VIDEO4LINUX2JPEG:
132 case CAPTURE_FIREWIRE:
133 case CAPTURE_IEC61883:
137 case VIDEO4LINUX2MPEG:
145 int FileJPEG::write_frame(VFrame *frame, VFrame *data, FrameWriterUnit *unit)
148 JPEGUnit *jpeg_unit = (JPEGUnit*)unit;
150 if(!jpeg_unit->compressor)
151 jpeg_unit->compressor = mjpeg_new(asset->width,
155 mjpeg_set_quality((mjpeg_t*)jpeg_unit->compressor, asset->jpeg_quality);
158 mjpeg_compress((mjpeg_t*)jpeg_unit->compressor,
163 frame->get_color_model(),
166 // insert spherical tag
167 if(asset->jpeg_sphere)
169 const char *sphere_tag =
170 "http://ns.adobe.com/xap/1.0/\x00<?xpacket begin='\xef\xbb\xbf' id='W5M0MpCehiHzreSzNTczkc9d'?>\n"
171 "<x:xmpmeta xmlns:x='adobe:ns:meta/' x:xmptk='Image::Cinelerra'>\n"
172 "<rdf:RDF xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'>\n"
174 " <rdf:Description rdf:about=''\n"
175 " xmlns:GPano='http://ns.google.com/photos/1.0/panorama/'>\n"
176 " <GPano:ProjectionType>equirectangular</GPano:ProjectionType>\n"
177 " </rdf:Description>\n"
180 "<?xpacket end='w'?>";
182 // calculate length by skipping the \x00 byte
184 int tag_len = strlen(sphere_tag + skip) + skip;
185 int tag_len2 = tag_len + 2;
186 int tag_len3 = tag_len + 4;
188 data->allocate_compressed_data(
189 mjpeg_output_size((mjpeg_t*)jpeg_unit->compressor) + tag_len3);
190 data->set_compressed_size(
191 mjpeg_output_size((mjpeg_t*)jpeg_unit->compressor) + tag_len3);
193 int jfif_size = 0x14;
194 uint8_t *ptr = data->get_data();
196 mjpeg_output_buffer((mjpeg_t*)jpeg_unit->compressor),
201 *ptr++ = (tag_len2 >> 8) & 0xff;
202 *ptr++ = tag_len2 & 0xff;
208 mjpeg_output_buffer((mjpeg_t*)jpeg_unit->compressor) + jfif_size,
209 mjpeg_output_size((mjpeg_t*)jpeg_unit->compressor) - jfif_size);
213 data->allocate_compressed_data(mjpeg_output_size((mjpeg_t*)jpeg_unit->compressor));
214 data->set_compressed_size(mjpeg_output_size((mjpeg_t*)jpeg_unit->compressor));
215 memcpy(data->get_data(),
216 mjpeg_output_buffer((mjpeg_t*)jpeg_unit->compressor),
217 mjpeg_output_size((mjpeg_t*)jpeg_unit->compressor));
219 data->allocate_compressed_data(mjpeg_output_size((mjpeg_t*)jpeg_unit->compressor));
220 data->set_compressed_size(mjpeg_output_size((mjpeg_t*)jpeg_unit->compressor));
221 memcpy(data->get_data(),
222 mjpeg_output_buffer((mjpeg_t*)jpeg_unit->compressor),
223 mjpeg_output_size((mjpeg_t*)jpeg_unit->compressor));
237 int FileJPEG::read_frame_header(char *path)
239 FILE *fp = fopen(path, "rb");
241 eprintf("FileJPEG::read_frame_header %s: %m\n", path);
244 int w = 0, h = 0, result = 1;
245 unsigned char test[2];
246 if( fread(test, 1, sizeof(test), fp) == sizeof(test) &&
247 test[0] == 0xff && test[1] == 0xd8 ) {
248 fseek(fp, 0, SEEK_SET);
249 result = read_header(fp, w, h);
253 asset->width = w; asset->height = h;
254 asset->interlace_mode = ILACE_MODE_NOTINTERLACED;
257 eprintf("FileJPEG::read_frame_header %s bad header\n", path);
261 int FileJPEG::read_header(FILE *fp, int &w, int &h)
264 struct jpeg_error_mgr jpeg_error;
265 struct jpeg_decompress_struct jpeg_decompress;
266 jpeg_decompress.err = jpeg_std_error(&jpeg_error);
267 jpeg_create_decompress(&jpeg_decompress);
268 jpeg_stdio_src(&jpeg_decompress, fp);
269 if( jpeg_read_header(&jpeg_decompress, TRUE) != JPEG_HEADER_OK ) result = 1;
270 if( !result && jpeg_decompress.jpeg_color_space != JCS_YCbCr ) result = 1;
271 if( !result && jpeg_decompress.comp_info[0].h_samp_factor > 2 ) result = 1;
272 if( !result && jpeg_decompress.comp_info[0].v_samp_factor > 2 ) result = 1;
274 w = jpeg_decompress.image_width;
275 h = jpeg_decompress.image_height;
277 jpeg_destroy((j_common_ptr)&jpeg_decompress);
282 int FileJPEG::read_frame(VFrame *output, VFrame *input)
284 if(input->get_compressed_size() < 2 ||
285 input->get_data()[0] != 0xff ||
286 input->get_data()[1] != 0xd8)
289 if(!decompressor) decompressor = mjpeg_new(asset->width,
292 // printf("FileJPEG::read_frame %d %p %d %d %d %p %p %p %p %d\n",
294 // input->get_data(),
295 // input->get_compressed_size(),
298 // output->get_rows(),
302 // output->get_color_model());
303 mjpeg_decompress((mjpeg_t*)decompressor,
305 input->get_compressed_size(),
311 output->get_color_model(),
315 //printf("FileJPEG::read_frame %d\n", __LINE__);
319 FrameWriterUnit* FileJPEG::new_writer_unit(FrameWriter *writer)
321 return new JPEGUnit(this, writer);
329 JPEGUnit::JPEGUnit(FileJPEG *file, FrameWriter *writer)
330 : FrameWriterUnit(writer)
335 JPEGUnit::~JPEGUnit()
337 if(compressor) mjpeg_delete((mjpeg_t*)compressor);
346 JPEGConfigVideo::JPEGConfigVideo(BC_WindowBase *parent_window, Asset *asset)
347 : BC_Window(_(PROGRAM_NAME ": Video Compression"),
348 parent_window->get_abs_cursor_x(1),
349 parent_window->get_abs_cursor_y(1),
353 this->parent_window = parent_window;
357 JPEGConfigVideo::~JPEGConfigVideo()
361 void JPEGConfigVideo::create_objects()
364 lock_window("JPEGConfigVideo::create_objects");
365 add_subwindow(new BC_Title(x, y, _("Quality:")));
367 add_subwindow(slider = new BC_ISlider(x + 80, y,
368 0, 200, 200, 0, 100, asset->jpeg_quality, 0, 0,
369 &asset->jpeg_quality));
370 y += slider->get_h() + 10;
371 add_subwindow(new BC_CheckBox(x, y,
372 &asset->jpeg_sphere, _("Tag for spherical playback")));
374 add_subwindow(new BC_OKButton(this));
379 int JPEGConfigVideo::close_event()