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