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