Merge branch 'ct' of git.pipapo.org:cinelerra-ct into ct
[cinelerra_cv/ct.git] / cinelerra / filejpeg.C
blobd543eefff991d8a3a04100115e31424977d22de1
1 #include "asset.h"
2 #include "bcsignals.h"
3 #include "bcwidgetgrid.h"
4 #include "edit.h"
5 #include "file.h"
6 #include "filejpeg.h"
7 #include "interlacemodes.h"
8 #include "jpegwrapper.h"
9 #include "language.h"
10 #include "libmjpeg.h"
11 #include "mwindow.inc"
12 #include "quicktime.h"
13 #include "vframe.h"
14 #include "videodevice.inc"
15 #include "mainerror.h"
18 FileJPEG::FileJPEG(Asset_GC asset, File *file)
19  : FileList(asset, file, "JPEGLIST", ".jpg", FILE_JPEG, FILE_JPEG_LIST)
21         decompressor = 0;
24 FileJPEG::~FileJPEG()
26         if(decompressor) mjpeg_delete((mjpeg_t*)decompressor);
30 int FileJPEG::check_sig(Asset_GC asset)
32         FILE *stream = fopen(asset->path, "rb");
34         if(stream)
35         {
36                 char test[10];
37                 fread(test, 10, 1, stream);
38                 fclose(stream);
40                 if(test[6] == 'J' && test[7] == 'F' && test[8] == 'I' && test[9] == 'F')
41                 {
42                         return 1;
43                 }
44                 else
45                 if(test[0] == 'J' && test[1] == 'P' && test[2] == 'E' && test[3] == 'G' && 
46                         test[4] == 'L' && test[5] == 'I' && test[6] == 'S' && test[7] == 'T')
47                 {
48                         return 1;
49                 }
50         }
52         if(strlen(asset->path) > 4)
53         {
54                 int len = strlen(asset->path);
55                 if(!strncasecmp(asset->path + len - 4, ".jpg", 4)) return 1;
56         }
57         return 0;
62 void FileJPEG::get_parameters(BC_WindowBase *parent_window, 
63         Asset_GC asset, 
64         BC_WindowBase* &format_window,
65         int audio_options,
66         int video_options)
68         if(video_options)
69         {
70                 JPEGConfigVideo *window = new JPEGConfigVideo(parent_window, asset);
71                 format_window = window;
72                 window->create_objects();
73                 window->run_window();
74                 delete window;
75         }
79 int FileJPEG::can_copy_from(Edit *edit, int64_t position)
81 //printf("FileJPEG::can_copy_from %d %s\n", asset->format, asset->vcodec);
82         if(edit->asset->format == FILE_MOV)
83         {
84                 if(match4(edit->asset->vcodec, QUICKTIME_JPEG)) return 1;
85         }
86         else
87         if(edit->asset->format == FILE_JPEG || 
88                 edit->asset->format == FILE_JPEG_LIST)
89                 return 1;
91         return 0;
94 int FileJPEG::colormodel_supported(int colormodel)
96         return colormodel;
100 int FileJPEG::get_best_colormodel(Asset_GC asset, int driver)
102         switch(driver)
103         {
104                 case PLAYBACK_X11:
105                         return BC_RGB888;
106                         break;
107                 case PLAYBACK_X11_XV:
108                 case PLAYBACK_DV1394:
109                 case PLAYBACK_FIREWIRE:
110                 case PLAYBACK_ASYNCHRONOUS:
111                         return BC_YUV420P;
112                         break;
113                 case PLAYBACK_X11_GL:
114                         return BC_YUV888;
115                         break;
116                 case PLAYBACK_LML:
117                 case PLAYBACK_BUZ:
118                         return BC_YUV422P;
119                         break;
120                 case VIDEO4LINUX:
121                 case VIDEO4LINUX2:
122                         return BC_YUV420P;
123                         break;
124                 case CAPTURE_BUZ:
125                 case CAPTURE_LML:
126                 case VIDEO4LINUX2JPEG:
127                         return BC_YUV422;
128                         break;
129                 case CAPTURE_FIREWIRE:
130                 case CAPTURE_IEC61883:
131                         return BC_YUV420P;
132                         break;
133         }
134         return BC_YUV420P;
138 int FileJPEG::write_frame(VFrame *frame, VFrame *data, FrameWriterUnit *unit)
140         int result = 0;
141         JPEGUnit *jpeg_unit = (JPEGUnit*)unit;
143         if(!jpeg_unit->compressor)
144                 jpeg_unit->compressor = mjpeg_new(asset->width, 
145                         asset->height, 
146                         1);
147         mjpeg_set_quality((mjpeg_t*)jpeg_unit->compressor, asset->jpeg_quality);
148         
149         
150         mjpeg_compress((mjpeg_t*)jpeg_unit->compressor, 
151                 frame->get_rows(), 
152                 frame->get_y(), 
153                 frame->get_u(), 
154                 frame->get_v(),
155                 frame->get_color_model(),
156                 1);
157         
158         data->allocate_compressed_data(mjpeg_output_size((mjpeg_t*)jpeg_unit->compressor));
159         data->set_compressed_size(mjpeg_output_size((mjpeg_t*)jpeg_unit->compressor));
160         memcpy(data->get_data(), 
161                 mjpeg_output_buffer((mjpeg_t*)jpeg_unit->compressor), 
162                 mjpeg_output_size((mjpeg_t*)jpeg_unit->compressor));
163         
164         return result;
176 int FileJPEG::read_frame_header(char *path)
178         int result = 0;
181         FILE *stream;
183         if(!(stream = fopen(path, "rb")))
184         {
185                 eprintf("Error while opening \"%s\" for reading. \n%m\n", asset->path);
186                 return 1;
187         }
188         
189         
191         struct jpeg_decompress_struct jpeg_decompress;
192         struct jpeg_error_mgr jpeg_error;
194         jpeg_decompress.err = jpeg_std_error(&jpeg_error);
195         jpeg_create_decompress(&jpeg_decompress);
197         jpeg_stdio_src(&jpeg_decompress, stream);
198         jpeg_read_header(&jpeg_decompress, TRUE);
200         asset->width = jpeg_decompress.image_width;
201         asset->height = jpeg_decompress.image_height;
203         asset->interlace_mode = BC_ILACE_MODE_NOTINTERLACED;
205         jpeg_destroy((j_common_ptr)&jpeg_decompress);
206         fclose(stream);
207         
208         
209         
210         return result;
215 int FileJPEG::read_frame(VFrame *output, VFrame *input)
217         if(!decompressor) decompressor = mjpeg_new(asset->width, 
218                 asset->height, 
219                 1);
220         mjpeg_decompress((mjpeg_t*)decompressor, 
221                 input->get_data(), 
222                 input->get_compressed_size(),
223                 0,  
224                 output->get_rows(), 
225                 output->get_y(), 
226                 output->get_u(), 
227                 output->get_v(),
228                 output->get_color_model(),
229                 1);
232         return 0;
235 FrameWriterUnit* FileJPEG::new_writer_unit(FrameWriter *writer)
237         return new JPEGUnit(this, writer);
245 JPEGUnit::JPEGUnit(FileJPEG *file, FrameWriter *writer)
246  : FrameWriterUnit(writer)
248         this->file = file;
249         compressor = 0;
251 JPEGUnit::~JPEGUnit()
253         if(compressor) mjpeg_delete((mjpeg_t*)compressor);
262 JPEGConfigVideo::JPEGConfigVideo(BC_WindowBase *parent_window, Asset_GC asset)
263  : BC_Window(PROGRAM_NAME ": Video Compression",
264         parent_window->get_abs_cursor_x(1),
265         parent_window->get_abs_cursor_y(1),
266         400,
267         100,
268         400,
269         100,
270         0,
271         1)
273         this->parent_window = parent_window;
274         this->asset = asset;
277 JPEGConfigVideo::~JPEGConfigVideo()
281 int JPEGConfigVideo::create_objects()
283         int x = 10, y = 10;
285         BC_WidgetGrid *wg;
286         BC_RelocatableWidget *rw;
288         wg = add_widgetgrid(new BC_WidgetGrid(10, 10, 10, 10, 3, 3));
290         rw =
291         add_subwindow(new BC_Title(x, y, _("Quality:")));
292         wg->add(rw, 0, 0);
293         rw =
294         add_subwindow(new BC_ISlider(x + 80, 
295                 y,
296                 0,
297                 200,
298                 200,
299                 0,
300                 100,
301                 asset->jpeg_quality,
302                 0,
303                 0,
304                 &asset->jpeg_quality));
306         wg->add(rw, 0, 1);
307         rw =
308         add_subwindow(new BC_OKButton(this));
310         wg->move_widgets();
311         resize_window(wg->get_w_wm(), wg->get_h_wm() + rw->get_h());
312         return 0;
315 int JPEGConfigVideo::close_event()
317         set_done(0);
318         return 1;
324 //      Local Variables:
325 //      mode: C++
326 //      c-file-style: "linux"
327 //      End: