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