r125: This commit was manufactured by cvs2svn to create tag 'r1_1_7-last'.
[cinelerra_cv/mob.git] / hvirtual / cinelerra / filejpeg.C
blob08450c0dd86939dbe696023339c193496684567e
1 #include "assets.h"
2 #include "edit.h"
3 #include "file.h"
4 #include "filejpeg.h"
5 #include "jpegwrapper.h"
6 #include "libmjpeg.h"
7 #include "mwindow.inc"
8 #include "quicktime.h"
9 #include "vframe.h"
10 #include "videodevice.inc"
12 #include <libintl.h>
13 #define _(String) gettext(String)
14 #define gettext_noop(String) String
15 #define N_(String) gettext_noop (String)
18 FileJPEG::FileJPEG(Asset *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 *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 *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 *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_FIREWIRE:
109                         return BC_YUV420P;
110                         break;
111                 case PLAYBACK_LML:
112                 case PLAYBACK_BUZ:
113                         return BC_YUV422P;
114                         break;
115                 case VIDEO4LINUX:
116                 case VIDEO4LINUX2:
117                         return BC_YUV420P;
118                         break;
119                 case CAPTURE_BUZ:
120                 case CAPTURE_LML:
121                         return BC_YUV422;
122                         break;
123                 case CAPTURE_FIREWIRE:
124                         return BC_YUV420P;
125                         break;
126         }
127         return BC_YUV420P;
131 int FileJPEG::write_frame(VFrame *frame, VFrame *data, FrameWriterUnit *unit)
133         int result = 0;
134         JPEGUnit *jpeg_unit = (JPEGUnit*)unit;
136         if(!jpeg_unit->compressor)
137                 jpeg_unit->compressor = mjpeg_new(asset->width, 
138                         asset->height, 
139                         1);
140         mjpeg_set_quality((mjpeg_t*)jpeg_unit->compressor, asset->jpeg_quality);
141         
142         
143         mjpeg_compress((mjpeg_t*)jpeg_unit->compressor, 
144                 frame->get_rows(), 
145                 frame->get_y(), 
146                 frame->get_u(), 
147                 frame->get_v(),
148                 frame->get_color_model(),
149                 1);
150         
151         data->allocate_compressed_data(mjpeg_output_size((mjpeg_t*)jpeg_unit->compressor));
152         data->set_compressed_size(mjpeg_output_size((mjpeg_t*)jpeg_unit->compressor));
153         memcpy(data->get_data(), 
154                 mjpeg_output_buffer((mjpeg_t*)jpeg_unit->compressor), 
155                 mjpeg_output_size((mjpeg_t*)jpeg_unit->compressor));
156         
157         return result;
169 int FileJPEG::read_frame_header(char *path)
171         int result = 0;
174         FILE *stream;
176         if(!(stream = fopen(path, "rb")))
177         {
178                 perror("FileJPEG::read_frame_header");
179                 return 1;
180         }
181         
182         
184         struct jpeg_decompress_struct jpeg_decompress;
185         struct jpeg_error_mgr jpeg_error;
187         jpeg_decompress.err = jpeg_std_error(&jpeg_error);
188         jpeg_create_decompress(&jpeg_decompress);
190         jpeg_stdio_src(&jpeg_decompress, stream);
191         jpeg_read_header(&jpeg_decompress, TRUE);
193         asset->width = jpeg_decompress.image_width;
194         asset->height = jpeg_decompress.image_height;
196         jpeg_destroy((j_common_ptr)&jpeg_decompress);
197         fclose(stream);
198         
199         
200         
201         return result;
206 int FileJPEG::read_frame(VFrame *output, VFrame *input)
208 //printf("FileJPEG::read_frame 1\n");
209         if(!decompressor) decompressor = mjpeg_new(asset->width, 
210                 asset->height, 
211                 1);
212 //printf("FileJPEG::read_frame 1\n");
213         mjpeg_decompress((mjpeg_t*)decompressor, 
214                 input->get_data(), 
215                 input->get_compressed_size(),
216                 0,  
217                 output->get_rows(), 
218                 output->get_y(), 
219                 output->get_u(), 
220                 output->get_v(),
221                 output->get_color_model(),
222                 1);
223 //printf("FileJPEG::read_frame 1\n");
224 //printf("FileJPEG::read_frame 2\n");
225         
226         
227         return 0;
230 FrameWriterUnit* FileJPEG::new_writer_unit(FrameWriter *writer)
232         return new JPEGUnit(this, writer);
240 JPEGUnit::JPEGUnit(FileJPEG *file, FrameWriter *writer)
241  : FrameWriterUnit(writer)
243         this->file = file;
244         compressor = 0;
246 JPEGUnit::~JPEGUnit()
248         if(compressor) mjpeg_delete((mjpeg_t*)compressor);
257 JPEGConfigVideo::JPEGConfigVideo(BC_WindowBase *parent_window, Asset *asset)
258  : BC_Window(PROGRAM_NAME ": Video Compression",
259         parent_window->get_abs_cursor_x(),
260         parent_window->get_abs_cursor_y(),
261         400,
262         100)
264         this->parent_window = parent_window;
265         this->asset = asset;
268 JPEGConfigVideo::~JPEGConfigVideo()
272 int JPEGConfigVideo::create_objects()
274         int x = 10, y = 10;
276         add_subwindow(new BC_Title(x, y, _("Quality:")));
277         add_subwindow(new BC_ISlider(x + 80, 
278                 y,
279                 0,
280                 200,
281                 200,
282                 0,
283                 100,
284                 asset->jpeg_quality,
285                 0,
286                 0,
287                 &asset->jpeg_quality));
289         add_subwindow(new BC_OKButton(this));
290         return 0;
293 int JPEGConfigVideo::close_event()
295         set_done(0);
296         return 1;