r954: Improve icons of the liveaudio and livevideo effects.
[cinelerra_cv.git] / cinelerra / filejpeg.C
blob8b919a7a8fd84d2861a67c6c8173d6e8e7dcc71a
1 #include "asset.h"
2 #include "bcsignals.h"
3 #include "edit.h"
4 #include "file.h"
5 #include "filejpeg.h"
6 #include "interlacemodes.h"
7 #include "jpegwrapper.h"
8 #include "language.h"
9 #include "libmjpeg.h"
10 #include "mwindow.inc"
11 #include "quicktime.h"
12 #include "vframe.h"
13 #include "videodevice.inc"
14 #include "mainerror.h"
17 FileJPEG::FileJPEG(Asset *asset, File *file)
18  : FileList(asset, file, "JPEGLIST", ".jpg", FILE_JPEG, FILE_JPEG_LIST)
20         decompressor = 0;
23 FileJPEG::~FileJPEG()
25         if(decompressor) mjpeg_delete((mjpeg_t*)decompressor);
29 int FileJPEG::check_sig(Asset *asset)
31         FILE *stream = fopen(asset->path, "rb");
33         if(stream)
34         {
35                 char test[10];
36                 fread(test, 10, 1, stream);
37                 fclose(stream);
39                 if(test[6] == 'J' && test[7] == 'F' && test[8] == 'I' && test[9] == 'F')
40                 {
41                         return 1;
42                 }
43                 else
44                 if(test[0] == 'J' && test[1] == 'P' && test[2] == 'E' && test[3] == 'G' && 
45                         test[4] == 'L' && test[5] == 'I' && test[6] == 'S' && test[7] == 'T')
46                 {
47                         return 1;
48                 }
49         }
51         if(strlen(asset->path) > 4)
52         {
53                 int len = strlen(asset->path);
54                 if(!strncasecmp(asset->path + len - 4, ".jpg", 4)) return 1;
55         }
56         return 0;
61 void FileJPEG::get_parameters(BC_WindowBase *parent_window, 
62         Asset *asset, 
63         BC_WindowBase* &format_window,
64         int audio_options,
65         int video_options)
67         if(video_options)
68         {
69                 JPEGConfigVideo *window = new JPEGConfigVideo(parent_window, asset);
70                 format_window = window;
71                 window->create_objects();
72                 window->run_window();
73                 delete window;
74         }
78 int FileJPEG::can_copy_from(Edit *edit, int64_t position)
80 //printf("FileJPEG::can_copy_from %d %s\n", asset->format, asset->vcodec);
81         if(edit->asset->format == FILE_MOV)
82         {
83                 if(match4(edit->asset->vcodec, QUICKTIME_JPEG)) return 1;
84         }
85         else
86         if(edit->asset->format == FILE_JPEG || 
87                 edit->asset->format == FILE_JPEG_LIST)
88                 return 1;
90         return 0;
93 int FileJPEG::colormodel_supported(int colormodel)
95         return colormodel;
99 int FileJPEG::get_best_colormodel(Asset *asset, int driver)
101         switch(driver)
102         {
103                 case PLAYBACK_X11:
104                         return BC_RGB888;
105                         break;
106                 case PLAYBACK_X11_XV:
107                 case PLAYBACK_DV1394:
108                 case PLAYBACK_FIREWIRE:
109                 case PLAYBACK_ASYNCHRONOUS:
110                         return BC_YUV420P;
111                         break;
112                 case PLAYBACK_X11_GL:
113                         return BC_YUV888;
114                         break;
115                 case PLAYBACK_LML:
116                 case PLAYBACK_BUZ:
117                         return BC_YUV422P;
118                         break;
119                 case VIDEO4LINUX:
120                 case VIDEO4LINUX2:
121                         return BC_YUV420P;
122                         break;
123                 case CAPTURE_BUZ:
124                 case CAPTURE_LML:
125                 case VIDEO4LINUX2JPEG:
126                         return BC_YUV422;
127                         break;
128                 case CAPTURE_FIREWIRE:
129                 case CAPTURE_IEC61883:
130                         return BC_YUV420P;
131                         break;
132         }
133         return BC_YUV420P;
137 int FileJPEG::write_frame(VFrame *frame, VFrame *data, FrameWriterUnit *unit)
139         int result = 0;
140         JPEGUnit *jpeg_unit = (JPEGUnit*)unit;
142         if(!jpeg_unit->compressor)
143                 jpeg_unit->compressor = mjpeg_new(asset->width, 
144                         asset->height, 
145                         1);
146         mjpeg_set_quality((mjpeg_t*)jpeg_unit->compressor, asset->jpeg_quality);
147         
148         
149         mjpeg_compress((mjpeg_t*)jpeg_unit->compressor, 
150                 frame->get_rows(), 
151                 frame->get_y(), 
152                 frame->get_u(), 
153                 frame->get_v(),
154                 frame->get_color_model(),
155                 1);
156         
157         data->allocate_compressed_data(mjpeg_output_size((mjpeg_t*)jpeg_unit->compressor));
158         data->set_compressed_size(mjpeg_output_size((mjpeg_t*)jpeg_unit->compressor));
159         memcpy(data->get_data(), 
160                 mjpeg_output_buffer((mjpeg_t*)jpeg_unit->compressor), 
161                 mjpeg_output_size((mjpeg_t*)jpeg_unit->compressor));
162         
163         return result;
175 int FileJPEG::read_frame_header(char *path)
177         int result = 0;
180         FILE *stream;
182         if(!(stream = fopen(path, "rb")))
183         {
184                 eprintf("Error while opening \"%s\" for reading. \n%m\n", asset->path);
185                 return 1;
186         }
187         
188         
190         struct jpeg_decompress_struct jpeg_decompress;
191         struct jpeg_error_mgr jpeg_error;
193         jpeg_decompress.err = jpeg_std_error(&jpeg_error);
194         jpeg_create_decompress(&jpeg_decompress);
196         jpeg_stdio_src(&jpeg_decompress, stream);
197         jpeg_read_header(&jpeg_decompress, TRUE);
199         asset->width = jpeg_decompress.image_width;
200         asset->height = jpeg_decompress.image_height;
202         asset->interlace_mode = BC_ILACE_MODE_NOTINTERLACED;
204         jpeg_destroy((j_common_ptr)&jpeg_decompress);
205         fclose(stream);
206         
207         
208         
209         return result;
214 int FileJPEG::read_frame(VFrame *output, VFrame *input)
216         if(!decompressor) decompressor = mjpeg_new(asset->width, 
217                 asset->height, 
218                 1);
219         mjpeg_decompress((mjpeg_t*)decompressor, 
220                 input->get_data(), 
221                 input->get_compressed_size(),
222                 0,  
223                 output->get_rows(), 
224                 output->get_y(), 
225                 output->get_u(), 
226                 output->get_v(),
227                 output->get_color_model(),
228                 1);
231         return 0;
234 FrameWriterUnit* FileJPEG::new_writer_unit(FrameWriter *writer)
236         return new JPEGUnit(this, writer);
244 JPEGUnit::JPEGUnit(FileJPEG *file, FrameWriter *writer)
245  : FrameWriterUnit(writer)
247         this->file = file;
248         compressor = 0;
250 JPEGUnit::~JPEGUnit()
252         if(compressor) mjpeg_delete((mjpeg_t*)compressor);
261 JPEGConfigVideo::JPEGConfigVideo(BC_WindowBase *parent_window, Asset *asset)
262  : BC_Window(PROGRAM_NAME ": Video Compression",
263         parent_window->get_abs_cursor_x(1),
264         parent_window->get_abs_cursor_y(1),
265         400,
266         100)
268         this->parent_window = parent_window;
269         this->asset = asset;
272 JPEGConfigVideo::~JPEGConfigVideo()
276 int JPEGConfigVideo::create_objects()
278         int x = 10, y = 10;
280         add_subwindow(new BC_Title(x, y, _("Quality:")));
281         add_subwindow(new BC_ISlider(x + 80, 
282                 y,
283                 0,
284                 200,
285                 200,
286                 0,
287                 100,
288                 asset->jpeg_quality,
289                 0,
290                 0,
291                 &asset->jpeg_quality));
293         add_subwindow(new BC_OKButton(this));
294         return 0;
297 int JPEGConfigVideo::close_event()
299         set_done(0);
300         return 1;