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