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 *stream = fopen(asset->path, "rb");
56 (void)fread(test, 10, 1, stream);
59 if(test[6] == 'J' && test[7] == 'F' && test[8] == 'I' && test[9] == 'F')
64 if(test[0] == 'J' && test[1] == 'P' && test[2] == 'E' && test[3] == 'G' &&
65 test[4] == 'L' && test[5] == 'I' && test[6] == 'S' && test[7] == 'T')
71 if(strlen(asset->path) > 4)
73 int len = strlen(asset->path);
74 if(!strncasecmp(asset->path + len - 4, ".jpg", 4)) return 1;
81 void FileJPEG::get_parameters(BC_WindowBase *parent_window,
83 BC_WindowBase* &format_window,
89 JPEGConfigVideo *window = new JPEGConfigVideo(parent_window, asset);
90 format_window = window;
91 window->create_objects();
98 int FileJPEG::can_copy_from(Asset *asset, int64_t position)
100 //printf("FileJPEG::can_copy_from %d %s\n", asset->format, asset->vcodec);
101 if(asset->format == FILE_JPEG ||
102 asset->format == FILE_JPEG_LIST)
108 int FileJPEG::colormodel_supported(int colormodel)
114 int FileJPEG::get_best_colormodel(Asset *asset, int driver)
121 case PLAYBACK_X11_XV:
122 case PLAYBACK_DV1394:
123 case PLAYBACK_FIREWIRE:
124 case PLAYBACK_ASYNCHRONOUS:
127 case PLAYBACK_X11_GL:
140 case VIDEO4LINUX2JPEG:
143 case CAPTURE_FIREWIRE:
144 case CAPTURE_IEC61883:
148 case VIDEO4LINUX2MPEG:
156 int FileJPEG::write_frame(VFrame *frame, VFrame *data, FrameWriterUnit *unit)
159 JPEGUnit *jpeg_unit = (JPEGUnit*)unit;
161 if(!jpeg_unit->compressor)
162 jpeg_unit->compressor = mjpeg_new(asset->width,
166 mjpeg_set_quality((mjpeg_t*)jpeg_unit->compressor, asset->jpeg_quality);
169 mjpeg_compress((mjpeg_t*)jpeg_unit->compressor,
174 frame->get_color_model(),
177 data->allocate_compressed_data(mjpeg_output_size((mjpeg_t*)jpeg_unit->compressor));
178 data->set_compressed_size(mjpeg_output_size((mjpeg_t*)jpeg_unit->compressor));
179 memcpy(data->get_data(),
180 mjpeg_output_buffer((mjpeg_t*)jpeg_unit->compressor),
181 mjpeg_output_size((mjpeg_t*)jpeg_unit->compressor));
195 int FileJPEG::read_frame_header(char *path)
202 if(!(stream = fopen(path, "rb")))
204 eprintf("FileJPEG::read_frame_header %s: %m\n", path);
209 unsigned char test[2];
210 (void)fread(test, 2, 1, stream);
211 if(test[0] != 0xff || test[1] != 0xd8)
213 eprintf("FileJPEG::read_frame_header %s bad header %02x%02x\n",
214 path, test[0], test[1]);
218 fseek(stream, 0, SEEK_SET);
220 struct jpeg_decompress_struct jpeg_decompress;
221 struct jpeg_error_mgr jpeg_error;
223 jpeg_decompress.err = jpeg_std_error(&jpeg_error);
224 jpeg_create_decompress(&jpeg_decompress);
226 jpeg_stdio_src(&jpeg_decompress, stream);
227 jpeg_read_header(&jpeg_decompress, TRUE);
229 asset->width = jpeg_decompress.image_width;
230 asset->height = jpeg_decompress.image_height;
232 asset->interlace_mode = BC_ILACE_MODE_NOTINTERLACED;
234 jpeg_destroy((j_common_ptr)&jpeg_decompress);
242 int FileJPEG::read_frame(VFrame *output, VFrame *input)
244 if(input->get_compressed_size() < 2 ||
245 input->get_data()[0] != 0xff ||
246 input->get_data()[1] != 0xd8)
249 if(!decompressor) decompressor = mjpeg_new(asset->width,
252 // printf("FileJPEG::read_frame %d %p %d %d %d %p %p %p %p %d\n",
254 // input->get_data(),
255 // input->get_compressed_size(),
258 // output->get_rows(),
262 // output->get_color_model());
263 mjpeg_decompress((mjpeg_t*)decompressor,
265 input->get_compressed_size(),
271 output->get_color_model(),
275 //printf("FileJPEG::read_frame %d\n", __LINE__);
279 FrameWriterUnit* FileJPEG::new_writer_unit(FrameWriter *writer)
281 return new JPEGUnit(this, writer);
289 JPEGUnit::JPEGUnit(FileJPEG *file, FrameWriter *writer)
290 : FrameWriterUnit(writer)
295 JPEGUnit::~JPEGUnit()
297 if(compressor) mjpeg_delete((mjpeg_t*)compressor);
306 JPEGConfigVideo::JPEGConfigVideo(BC_WindowBase *parent_window, Asset *asset)
307 : BC_Window(_(PROGRAM_NAME ": Video Compression"),
308 parent_window->get_abs_cursor_x(1),
309 parent_window->get_abs_cursor_y(1),
313 this->parent_window = parent_window;
317 JPEGConfigVideo::~JPEGConfigVideo()
321 void JPEGConfigVideo::create_objects()
324 lock_window("JPEGConfigVideo::create_objects");
325 add_subwindow(new BC_Title(x, y, _("Quality:")));
326 add_subwindow(new BC_ISlider(x + 80,
336 &asset->jpeg_quality));
338 add_subwindow(new BC_OKButton(this));
343 int JPEGConfigVideo::close_event()