bsd lang segv fix, enable bsd lv2, lv2 gui enable fix, proxy/ffmpeg toggle resize...
[goodguy/history.git] / cinelerra-5.1 / cinelerra / fileexr.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 #ifdef HAVE_OPENEXR
23
24 #include "asset.h"
25 #include "bcsignals.h"
26 #include "clip.h"
27 #include "fileexr.h"
28 #include "filesystem.h"
29 #include "interlacemodes.h"
30
31 #include "mwindow.inc"
32 #include "vframe.h"
33
34 #include "ImfChannelList.h"
35 #include "ImfChromaticities.h"
36 #include "ImfCompression.h"
37 #include "ImfIO.h"
38 #include "ImfInputFile.h"
39 #include "ImfOutputFile.h"
40 #include "ImfPixelType.h"
41 #include "ImfRgbaFile.h"
42 #include "ImfRgbaYca.h"
43 #include "ImfVersion.h"
44
45 class EXRIStream : public Imf::IStream
46 {
47 public:
48         EXRIStream(char *data, int size);
49         ~EXRIStream();
50
51         bool read (char c[], int n);
52         Imf::Int64 tellg ();
53         void seekg (Imf::Int64 pos);
54         void clear ();
55
56 private:
57         char *data;
58         int size;
59         int position;
60 };
61
62 class EXROStream : public Imf::OStream
63 {
64 public:
65         EXROStream(VFrame *data);
66         ~EXROStream();
67
68     virtual void write(const char c[], int n);
69     virtual Imf::Int64 tellp();
70     virtual void seekp(Imf::Int64 pos);
71
72 private:
73         VFrame *data;
74         int position;
75 };
76
77
78
79 EXRIStream::EXRIStream(char *data, int size)
80  : Imf::IStream("mypath")
81 {
82         this->data = data;
83         this->size = size;
84         position = 0;
85 }
86
87 EXRIStream::~EXRIStream()
88 {
89 }
90
91 bool EXRIStream::read(char c[], int n)
92 {
93         int fragment = n;
94         if(position + fragment > size)
95         {
96                 fragment = size - position;
97         }
98         memcpy(c, data + position, fragment);
99         position += fragment;
100
101         if(n != fragment)
102         {
103                 throw Iex::InputExc ("EXRIStream::read: Unexpected end of file.");
104         }
105         return position >= size;
106 }
107
108 Imf::Int64 EXRIStream::tellg ()
109 {
110         return position;
111 }
112
113 void EXRIStream::seekg(Imf::Int64 pos)
114 {
115         position = pos;
116 }
117
118 void EXRIStream::clear()
119 {
120 }
121
122
123
124
125
126
127
128
129
130
131
132 EXROStream::EXROStream(VFrame *data)
133  : Imf::OStream("mypath")
134 {
135         this->data = data;
136         position = 0;
137 }
138 EXROStream::~EXROStream()
139 {
140 }
141
142 void EXROStream::write(const char c[], int n)
143 {
144         if(position + n > data->get_compressed_allocated())
145                 data->allocate_compressed_data(MAX(position + n, data->get_compressed_allocated() * 2));
146
147         memcpy(data->get_data() + position, c, n);
148         position += n;
149         data->set_compressed_size(MAX(position, data->get_compressed_size()));
150 }
151
152 Imf::Int64 EXROStream::tellp()
153 {
154         return position;
155 }
156
157 void EXROStream::seekp(Imf::Int64 pos)
158 {
159         position = pos;
160 }
161
162
163
164
165
166
167
168
169
170
171
172 FileEXR::FileEXR(Asset *asset, File *file)
173  : FileList(asset, file, "EXRLIST", ".exr", FILE_EXR, FILE_EXR_LIST)
174 {
175         native_cmodel = BC_RGB_FLOAT;
176         is_yuv = 0;
177         temp_y = 0;
178         temp_u = 0;
179         temp_v = 0;
180 }
181
182 FileEXR::~FileEXR()
183 {
184         if(temp_y) delete [] temp_y;
185         if(temp_u) delete [] temp_u;
186         if(temp_v) delete [] temp_v;
187 }
188
189 const char* FileEXR::compression_to_str(int compression)
190 {
191         switch(compression)
192         {
193                 case FileEXR::NONE: return "None"; break;
194                 case FileEXR::PIZ: return "PIZ"; break;
195                 case FileEXR::ZIP: return "ZIP"; break;
196                 case FileEXR::ZIPS: return "ZIPS"; break;
197                 case FileEXR::RLE: return "RLE"; break;
198                 case FileEXR::PXR24: return "PXR24"; break;
199         }
200         return _("None");
201 }
202
203 int FileEXR::compression_to_exr(int compression)
204 {
205         switch(compression)
206         {
207                 case FileEXR::NONE: return (int)Imf::NO_COMPRESSION; break;
208                 case FileEXR::PIZ: return (int)Imf::PIZ_COMPRESSION; break;
209                 case FileEXR::ZIP: return (int)Imf::ZIP_COMPRESSION; break;
210                 case FileEXR::ZIPS: return (int)Imf::ZIPS_COMPRESSION; break;
211                 case FileEXR::RLE: return (int)Imf::RLE_COMPRESSION; break;
212                 case FileEXR::PXR24: return (int)Imf::PXR24_COMPRESSION; break;
213         }
214         return Imf::NO_COMPRESSION;
215 }
216
217 int FileEXR::str_to_compression(char *string)
218 {
219         if(!strcmp(compression_to_str(FileEXR::NONE), string))
220                 return FileEXR::NONE;
221         if(!strcmp(compression_to_str(FileEXR::PIZ), string))
222                 return FileEXR::PIZ;
223         if(!strcmp(compression_to_str(FileEXR::ZIP), string))
224                 return FileEXR::ZIP;
225         if(!strcmp(compression_to_str(FileEXR::ZIPS), string))
226                 return FileEXR::ZIPS;
227         if(!strcmp(compression_to_str(FileEXR::RLE), string))
228                 return FileEXR::RLE;
229         if(!strcmp(compression_to_str(FileEXR::PXR24), string))
230                 return PXR24;
231         return FileEXR::NONE;
232 }
233
234 int FileEXR::check_sig(Asset *asset, char *test)
235 {
236         if(Imf::isImfMagic(test)) return 1;
237         if(test[0] == 'E' && test[1] == 'X' && test[2] == 'R' &&
238                 test[3] == 'L' && test[4] == 'I' && test[5] == 'S' && test[6] == 'T')
239         {
240                 return 1;
241         }
242
243         return 0;
244 }
245
246 void FileEXR::get_parameters(BC_WindowBase *parent_window,
247         Asset *asset, BC_WindowBase* &format_window,
248         int audio_options, int video_options, EDL *edl)
249 {
250         if(video_options)
251         {
252                 EXRConfigVideo *window = new EXRConfigVideo(parent_window, asset);
253                 format_window = window;
254                 window->create_objects();
255                 window->run_window();
256                 delete window;
257         }
258 }
259
260 int FileEXR::colormodel_supported(int colormodel)
261 {
262         return native_cmodel;
263 }
264
265 int FileEXR::get_best_colormodel(Asset *asset, int driver)
266 {
267         if(asset->exr_use_alpha)
268                 return BC_RGBA_FLOAT;
269         else
270                 return BC_RGB_FLOAT;
271 }
272
273 int64_t FileEXR::get_memory_usage()
274 {
275         int64_t result = FileList::get_memory_usage();
276         if(temp_y) result += (int64_t)asset->width * asset->height * 3 / 2;
277         return result;
278 }
279
280
281 int FileEXR::read_frame_header(char *path)
282 {
283         int result = 0;
284
285 // This may have been used by VFS
286 //      FILE *stream;
287 //
288 //      if(!(stream = fopen(path, "rb")))
289 //      {
290 //              perror("FileEXR::read_frame_header");
291 //              return 1;
292 //      }
293 //      int size = FileSystem::get_size(path);
294 //      char *buffer = new char[size];
295 //      fread(buffer, size, 1, stream);
296 //      fclose(stream);
297 //
298 //      EXRIStream exr_stream(buffer, size);
299 //      Imf::InputFile file(exr_stream);
300
301
302         Imf::InputFile file(path);
303
304         Imath::Box2i dw = file.header().dataWindow();
305
306         asset->width = dw.max.x - dw.min.x + 1;
307         asset->height = dw.max.y - dw.min.y + 1;
308         asset->interlace_mode = ILACE_MODE_NOTINTERLACED;
309
310         const Imf::ChannelList &channels = file.header().channels();
311
312         if(channels.findChannel("A"))
313                 native_cmodel = BC_RGBA_FLOAT;
314         else
315                 native_cmodel = BC_RGB_FLOAT;
316         asset->exr_use_alpha = BC_CModels::has_alpha(native_cmodel) ? 1 : 0;
317
318         if(channels.findChannel("Y"))
319                 is_yuv = 1;
320 // for (Imf::ChannelList::ConstIterator i = channels.begin(); i != channels.end(); ++i)
321 // {
322 // printf("%s\n", i.name());
323 // }
324
325 //      delete [] buffer;
326         return result;
327 }
328
329 int FileEXR::read_frame(VFrame *frame, VFrame *data)
330 {
331         EXRIStream exr_stream((char*)data->get_data(), data->get_compressed_size());
332         Imf::InputFile file(exr_stream);
333         Imath::Box2i dw = file.header().dataWindow();
334     int dx = dw.min.x;
335     int dy = dw.min.y;
336         Imf::FrameBuffer framebuffer;
337         float **rows = (float**)frame->get_rows();
338         int components = BC_CModels::components(frame->get_color_model());
339
340         if(is_yuv)
341         {
342                 if(!temp_y) temp_y = new float[asset->width * asset->height];
343                 if(!temp_u) temp_u = new float[asset->width * asset->height / 4];
344                 if(!temp_v) temp_v = new float[asset->width * asset->height / 4];
345                 framebuffer.insert("Y", Imf::Slice(Imf::FLOAT,
346                         (char*)(temp_y - dy * asset->width - dx),
347                         sizeof(float),
348                         sizeof(float) * frame->get_w()));
349                 framebuffer.insert("BY", Imf::Slice(Imf::FLOAT,
350                         (char*)(temp_u - dy * asset->width / 4 - dx / 2),
351                         sizeof(float),
352                         sizeof(float) * frame->get_w() / 2,
353                         2,
354                         2));
355                 framebuffer.insert("RY", Imf::Slice(Imf::FLOAT,
356                         (char*)(temp_v - dy * asset->width / 4 - dx / 2),
357                         sizeof(float),
358                         sizeof(float) * frame->get_w() / 2,
359                         2,
360                         2));
361         }
362         else
363         {
364                 framebuffer.insert("R", Imf::Slice(Imf::FLOAT,
365                         (char*)(&rows[-dy][-dx * components]),
366                         sizeof(float) * components,
367                         sizeof(float) * components * frame->get_w()));
368                 framebuffer.insert("G", Imf::Slice(Imf::FLOAT,
369                         (char*)(&rows[-dy][-dx * components + 1]),
370                         sizeof(float) * components,
371                         sizeof(float) * components * frame->get_w()));
372                 framebuffer.insert("B", Imf::Slice(Imf::FLOAT,
373                         (char*)(&rows[-dy][-dx * components + 2]),
374                         sizeof(float) * components,
375                         sizeof(float) * components * frame->get_w()));
376         }
377
378 // Alpha always goes directly to the output frame
379         if(components == 4)
380         {
381                 framebuffer.insert("A", Imf::Slice(Imf::FLOAT,
382                         (char*)(&rows[-dy][-dx * components + 3]),
383                         sizeof(float) * components,
384                         sizeof(float) * components * frame->get_w()));
385         }
386
387         file.setFrameBuffer(framebuffer);
388         file.readPixels (dw.min.y, dw.max.y);
389
390         if(is_yuv)
391         {
392 // Convert to RGB using crazy ILM equations
393                 Imath::V3f yw;
394                 Imf::Chromaticities cr;
395                 yw = Imf::RgbaYca::computeYw(cr);
396
397                 for(int i = 0; i < asset->height - 1; i += 2)
398                 {
399                         float *y_row1 = temp_y + i * asset->width;
400                         float *y_row2 = temp_y + (i + 1) * asset->width;
401                         float *u_row = temp_u + (i * asset->width / 4);
402                         float *v_row = temp_v + (i * asset->width / 4);
403                         float *out_row1 = rows[i];
404                         float *out_row2 = rows[i + 1];
405                         for(int j = 0; j < asset->width - 1; j += 2)
406                         {
407                                 float v = *u_row++;
408                                 float u = *v_row++;
409                                 float y;
410
411                                 float r, g, b;
412                                 y = *y_row1++;
413                                 r = (u + 1) * y;
414                                 b = (v + 1) * y;
415                                 g = (y - r * yw.x - b * yw.z) / yw.y;
416                                 *out_row1++ = r;
417                                 *out_row1++ = g;
418                                 *out_row1++ = b;
419                                 if(components == 4) out_row1++;
420
421                                 y = *y_row1++;
422                                 r = (u + 1) * y;
423                                 b = (v + 1) * y;
424                                 g = (y - r * yw.x - b * yw.z) / yw.y;
425                                 *out_row1++ = r;
426                                 *out_row1++ = g;
427                                 *out_row1++ = b;
428                                 if(components == 4) out_row1++;
429
430                                 y = *y_row2++;
431                                 r = (u + 1) * y;
432                                 b = (v + 1) * y;
433                                 g = (y - r * yw.x - b * yw.z) / yw.y;
434                                 *out_row2++ = r;
435                                 *out_row2++ = g;
436                                 *out_row2++ = b;
437                                 if(components == 4) out_row1++;
438
439                                 y = *y_row2++;
440                                 r = (u + 1) * y;
441                                 b = (v + 1) * y;
442                                 g = (y - r * yw.x - b * yw.z) / yw.y;
443                                 *out_row2++ = r;
444                                 *out_row2++ = g;
445                                 *out_row2++ = b;
446                                 if(components == 4) out_row1++;
447                         }
448                 }
449         }
450         return 0;
451 }
452
453
454
455
456 int FileEXR::write_frame(VFrame *frame, VFrame *data, FrameWriterUnit *unit)
457 {
458         EXRUnit *exr_unit = (EXRUnit*)unit;
459
460         VFrame *output_frame;
461         data->set_compressed_size(0);
462
463
464         int native_cmodel = asset->exr_use_alpha ? BC_RGBA_FLOAT : BC_RGB_FLOAT;
465         int components = BC_CModels::components(native_cmodel);
466
467         if(frame->get_color_model() != native_cmodel)
468         {
469                 if(!exr_unit->temp_frame) exr_unit->temp_frame =
470                         new VFrame(asset->width, asset->height, native_cmodel, 0);
471                 BC_CModels::transfer(exr_unit->temp_frame->get_rows(), /* Leave NULL if non existent */
472                         frame->get_rows(),
473                         exr_unit->temp_frame->get_y(), /* Leave NULL if non existent */
474                         exr_unit->temp_frame->get_u(),
475                         exr_unit->temp_frame->get_v(),
476                         frame->get_y(), /* Leave NULL if non existent */
477                         frame->get_u(),
478                         frame->get_v(),
479                         0,        /* Dimensions to capture from input frame */
480                         0,
481                         asset->width,
482                         asset->height,
483                         0,       /* Dimensions to project on output frame */
484                         0,
485                         asset->width,
486                         asset->height,
487                         frame->get_color_model(),
488                         native_cmodel,
489                         0,         /* When transfering BC_RGBA8888 to non-alpha this is the background color in 0xRRGGBB hex */
490                         asset->width,       /* For planar use the luma rowspan */
491                         asset->height);
492                 output_frame = exr_unit->temp_frame;
493         }
494         else
495                 output_frame = frame;
496
497         Imf::Header header(output_frame->get_w(), output_frame->get_h());
498         header.compression() = (Imf::Compression)compression_to_exr(
499                 asset->exr_compression);
500         header.channels().insert("R", Imf::Channel(Imf::FLOAT));
501         header.channels().insert("G", Imf::Channel(Imf::FLOAT));
502         header.channels().insert("B", Imf::Channel(Imf::FLOAT));
503         if(asset->exr_use_alpha) header.channels().insert("A", Imf::Channel(Imf::FLOAT));
504
505         EXROStream exr_stream(data);
506         Imf::OutputFile file(exr_stream, header);
507         Imf::FrameBuffer framebuffer;
508         float **rows = (float**)output_frame->get_rows();
509         framebuffer.insert("R",
510                 Imf::Slice(Imf::FLOAT,
511                         (char*)(rows[0]),
512                         sizeof(float) * components,
513                         sizeof(float) * components * output_frame->get_w()));
514         framebuffer.insert("G",
515                 Imf::Slice(Imf::FLOAT,
516                         (char*)(rows[0] + 1),
517                         sizeof(float) * components,
518                         sizeof(float) * components * output_frame->get_w()));
519         framebuffer.insert("B",
520                 Imf::Slice(Imf::FLOAT,
521                         (char*)(rows[0] + 2),
522                         sizeof(float) * components,
523                         sizeof(float) * components * output_frame->get_w()));
524         if(asset->exr_use_alpha)
525                 framebuffer.insert("A",
526                         Imf::Slice(Imf::FLOAT,
527                                 (char*)(rows[0] + 3),
528                                 sizeof(float) * components,
529                                 sizeof(float) * components * output_frame->get_w()));
530         file.setFrameBuffer(framebuffer);
531         file.writePixels(asset->height);
532         return 0;
533 }
534
535 FrameWriterUnit* FileEXR::new_writer_unit(FrameWriter *writer)
536 {
537         return new EXRUnit(this, writer);
538 }
539
540
541
542
543
544
545
546
547
548
549
550
551 EXRUnit::EXRUnit(FileEXR *file, FrameWriter *writer)
552  : FrameWriterUnit(writer)
553 {
554         this->file = file;
555         temp_frame = 0;
556 }
557
558 EXRUnit::~EXRUnit()
559 {
560         if(temp_frame) delete temp_frame;
561 }
562
563
564
565
566
567
568
569
570
571
572
573 EXRConfigVideo::EXRConfigVideo(BC_WindowBase *parent_window, Asset *asset)
574  : BC_Window(_(PROGRAM_NAME ": Video Compression"),
575         parent_window->get_abs_cursor_x(1),
576         parent_window->get_abs_cursor_y(1),
577         300,
578         BC_OKButton::calculate_h() + 100)
579 {
580         this->parent_window = parent_window;
581         this->asset = asset;
582 }
583
584 EXRConfigVideo::~EXRConfigVideo()
585 {
586 }
587
588 void EXRConfigVideo::create_objects()
589 {
590         lock_window("EXRConfigVideo::create_objects");
591         int x = 10, y = 10;
592         add_subwindow(new EXRUseAlpha(this, x, y));
593         y += 30;
594         EXRCompression *menu;
595         add_subwindow(new BC_Title(x, y, _("Compression:")));
596         x += 110;
597         add_subwindow(menu = new EXRCompression(this, x, y, 100));
598         menu->create_objects();
599         add_subwindow(new BC_OKButton(this));
600         show_window(1);
601         unlock_window();
602 }
603
604 int EXRConfigVideo::close_event()
605 {
606         set_done(0);
607         return 1;
608 }
609
610
611 EXRUseAlpha::EXRUseAlpha(EXRConfigVideo *gui, int x, int y)
612  : BC_CheckBox(x, y, gui->asset->exr_use_alpha, _("Use alpha"))
613 {
614         this->gui = gui;
615 }
616
617 int EXRUseAlpha::handle_event()
618 {
619         gui->asset->exr_use_alpha = get_value();
620         return 1;
621 }
622
623
624
625 EXRCompression::EXRCompression(EXRConfigVideo *gui, int x, int y, int w)
626  : BC_PopupMenu(x,
627         y,
628         w,
629         FileEXR::compression_to_str(gui->asset->exr_compression))
630 {
631         this->gui = gui;
632 }
633 void EXRCompression::create_objects()
634 {
635         add_item(new EXRCompressionItem(gui, FileEXR::NONE));
636         add_item(new EXRCompressionItem(gui, FileEXR::PIZ));
637         add_item(new EXRCompressionItem(gui, FileEXR::ZIP));
638         add_item(new EXRCompressionItem(gui, FileEXR::ZIPS));
639         add_item(new EXRCompressionItem(gui, FileEXR::RLE));
640         add_item(new EXRCompressionItem(gui, FileEXR::PXR24));
641 }
642
643 int EXRCompression::handle_event()
644 {
645         return 1;
646 }
647
648 EXRCompressionItem::EXRCompressionItem(EXRConfigVideo *gui, int value)
649  : BC_MenuItem(FileEXR::compression_to_str(value))
650 {
651         this->gui = gui;
652         this->value = value;
653 }
654
655 int EXRCompressionItem::handle_event()
656 {
657         gui->asset->exr_compression = value;
658         return 0;
659 }
660
661 #endif