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