4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
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.
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.
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
25 #include "bcsignals.h"
29 #include "filesystem.h"
30 #include "interlacemodes.h"
32 #include "mwindow.inc"
35 #include "ImfChannelList.h"
36 #include "ImfChromaticities.h"
37 #include "ImfCompression.h"
39 #include "ImfInputFile.h"
40 #include "ImfOutputFile.h"
41 #include "ImfPixelType.h"
42 #include "ImfRgbaFile.h"
43 #include "ImfRgbaYca.h"
44 #include "ImfVersion.h"
46 class EXRIStream : public Imf::IStream
49 EXRIStream(char *data, int size);
52 bool read (char c[], int n);
54 void seekg (uint64_t pos);
63 class EXROStream : public Imf::OStream
66 EXROStream(VFrame *data);
69 virtual void write(const char c[], int n);
70 virtual uint64_t tellp();
71 virtual void seekp(uint64_t pos);
80 EXRIStream::EXRIStream(char *data, int size)
81 : Imf::IStream("mypath")
88 EXRIStream::~EXRIStream()
92 bool EXRIStream::read(char c[], int n)
95 if(position + fragment > size)
97 fragment = size - position;
99 memcpy(c, data + position, fragment);
100 position += fragment;
104 throw Iex::InputExc ("EXRIStream::read: Unexpected end of file.");
106 return position >= size;
109 uint64_t EXRIStream::tellg ()
114 void EXRIStream::seekg(uint64_t pos)
119 void EXRIStream::clear()
124 EXROStream::EXROStream(VFrame *data)
125 : Imf::OStream("mypath")
130 EXROStream::~EXROStream()
134 void EXROStream::write(const char c[], int n)
136 if(position + n > data->get_compressed_allocated())
137 data->allocate_compressed_data(MAX(position + n, data->get_compressed_allocated() * 2));
139 memcpy(data->get_data() + position, c, n);
141 data->set_compressed_size(MAX(position, data->get_compressed_size()));
144 uint64_t EXROStream::tellp()
149 void EXROStream::seekp(uint64_t pos)
155 FileEXR::FileEXR(Asset *asset, File *file)
156 : FileList(asset, file, "EXRLIST", ".exr", FILE_EXR, FILE_EXR_LIST)
158 native_cmodel = BC_RGB_FLOAT;
172 const char* FileEXR::compression_to_str(int compression)
174 switch( compression ) {
176 case PIZ: return "PIZ";
177 case ZIP: return "ZIP";
178 case ZIPS: return "ZIPS";
179 case RLE: return "RLE";
180 case PXR24: return "PXR24";
181 case B44: return "B44";
182 case B44A: return "B44A";
183 case DWAB: return "DWAB";
184 case DWAA: return "DWAA";
189 int FileEXR::compression_to_exr(int compression)
191 switch( compression ) {
192 case NONE: return Imf::NO_COMPRESSION;
193 case PIZ: return Imf::PIZ_COMPRESSION;
194 case ZIP: return Imf::ZIP_COMPRESSION;
195 case ZIPS: return Imf::ZIPS_COMPRESSION;
196 case RLE: return Imf::RLE_COMPRESSION;
197 case PXR24: return Imf::PXR24_COMPRESSION;
198 case B44: return Imf::B44_COMPRESSION;
199 case B44A: return Imf::B44A_COMPRESSION;
200 case DWAA: return Imf::DWAA_COMPRESSION;
201 case DWAB: return Imf::DWAB_COMPRESSION;
203 return Imf::NO_COMPRESSION;
206 int FileEXR::str_to_compression(char *string)
208 if( !strcmp(compression_to_str(NONE), string) ) return NONE;
209 if( !strcmp(compression_to_str(PIZ), string) ) return PIZ;
210 if( !strcmp(compression_to_str(ZIP), string) ) return ZIP;
211 if( !strcmp(compression_to_str(ZIPS), string) ) return ZIPS;
212 if( !strcmp(compression_to_str(RLE), string) ) return RLE;
213 if( !strcmp(compression_to_str(RLE), string) ) return B44;
214 if( !strcmp(compression_to_str(RLE), string) ) return B44A;
215 if( !strcmp(compression_to_str(RLE), string) ) return DWAA;
216 if( !strcmp(compression_to_str(RLE), string) ) return DWAB;
217 if( !strcmp(compression_to_str(PXR24),string) ) return PXR24;
221 int FileEXR::check_sig(Asset *asset, char *test)
223 if(Imf::isImfMagic(test)) return 1;
224 if(test[0] == 'E' && test[1] == 'X' && test[2] == 'R' &&
225 test[3] == 'L' && test[4] == 'I' && test[5] == 'S' && test[6] == 'T')
233 void FileEXR::get_parameters(BC_WindowBase *parent_window,
234 Asset *asset, BC_WindowBase* &format_window,
235 int audio_options, int video_options, EDL *edl)
237 if( video_options ) {
238 EXRConfigVideo *window = new EXRConfigVideo(parent_window, asset);
239 format_window = window;
240 window->create_objects();
241 window->run_window();
246 int FileEXR::colormodel_supported(int colormodel)
248 return native_cmodel;
251 int FileEXR::get_best_colormodel(Asset *asset, int driver)
253 return asset->exr_use_alpha ? BC_RGBA_FLOAT : BC_RGB_FLOAT;
256 int64_t FileEXR::get_memory_usage()
258 int64_t result = FileList::get_memory_usage();
259 if(temp_y) result += (int64_t)asset->width * asset->height * 3 / 2;
264 int FileEXR::read_frame_header(char *path)
268 // This may have been used by VFS
271 // if(!(stream = fopen(path, "rb")))
273 // perror("FileEXR::read_frame_header");
276 // int size = FileSystem::get_size(path);
277 // char *buffer = new char[size];
278 // fread(buffer, size, 1, stream);
281 // EXRIStream exr_stream(buffer, size);
282 // Imf::InputFile file(exr_stream);
284 if( access(path, R_OK) ) return 1;
285 Imf::InputFile file(path);
287 Imath::Box2i dw = file.header().dataWindow();
289 asset->width = dw.max.x - dw.min.x + 1;
290 asset->height = dw.max.y - dw.min.y + 1;
291 asset->interlace_mode = ILACE_MODE_NOTINTERLACED;
293 const Imf::ChannelList &channels = file.header().channels();
295 if(channels.findChannel("A"))
296 native_cmodel = BC_RGBA_FLOAT;
298 native_cmodel = BC_RGB_FLOAT;
299 asset->exr_use_alpha = BC_CModels::has_alpha(native_cmodel) ? 1 : 0;
301 if(channels.findChannel("Y"))
303 // for (Imf::ChannelList::ConstIterator i = channels.begin(); i != channels.end(); ++i)
305 // printf("%s\n", i.name());
312 int FileEXR::read_frame(VFrame *frame, VFrame *data)
314 Imf::setGlobalThreadCount(file->cpus);
315 EXRIStream exr_stream((char*)data->get_data(), data->get_compressed_size());
316 Imf::InputFile file(exr_stream);
317 Imath::Box2i dw = file.header().dataWindow();
318 int dx = dw.min.x, dy = dw.min.y;
319 Imf::FrameBuffer framebuffer;
320 float **rows = (float**)frame->get_rows();
321 int components = BC_CModels::components(frame->get_color_model());
324 if( !temp_y ) temp_y = new float[asset->width * asset->height];
325 if( !temp_u ) temp_u = new float[asset->width * asset->height / 4];
326 if( !temp_v ) temp_v = new float[asset->width * asset->height / 4];
327 framebuffer.insert("Y", Imf::Slice(Imf::FLOAT,
328 (char*)(temp_y - dy * asset->width - dx),
330 sizeof(float) * frame->get_w()));
331 framebuffer.insert("BY", Imf::Slice(Imf::FLOAT,
332 (char*)(temp_u - dy * asset->width / 4 - dx / 2),
334 sizeof(float) * frame->get_w() / 2,
337 framebuffer.insert("RY", Imf::Slice(Imf::FLOAT,
338 (char*)(temp_v - dy * asset->width / 4 - dx / 2),
340 sizeof(float) * frame->get_w() / 2,
345 framebuffer.insert("R", Imf::Slice(Imf::FLOAT,
346 (char*)(&rows[-dy][-dx * components]),
347 sizeof(float) * components,
348 sizeof(float) * components * frame->get_w()));
349 framebuffer.insert("G", Imf::Slice(Imf::FLOAT,
350 (char*)(&rows[-dy][-dx * components + 1]),
351 sizeof(float) * components,
352 sizeof(float) * components * frame->get_w()));
353 framebuffer.insert("B", Imf::Slice(Imf::FLOAT,
354 (char*)(&rows[-dy][-dx * components + 2]),
355 sizeof(float) * components,
356 sizeof(float) * components * frame->get_w()));
359 // Alpha always goes directly to the output frame
360 if( components == 4 ) {
361 framebuffer.insert("A", Imf::Slice(Imf::FLOAT,
362 (char*)(&rows[-dy][-dx * components + 3]),
363 sizeof(float) * components,
364 sizeof(float) * components * frame->get_w()));
367 file.setFrameBuffer(framebuffer);
368 file.readPixels (dw.min.y, dw.max.y);
371 // Convert to RGB using crazy ILM equations
373 Imf::Chromaticities cr;
374 yw = Imf::RgbaYca::computeYw(cr);
376 for( int i=0; i<asset->height-1; i+=2 ) {
377 float *y_row1 = temp_y + i * asset->width;
378 float *y_row2 = temp_y + (i + 1) * asset->width;
379 float *u_row = temp_u + (i * asset->width / 4);
380 float *v_row = temp_v + (i * asset->width / 4);
381 float *out_row1 = rows[i];
382 float *out_row2 = rows[i + 1];
383 for( int j=0; j<asset->width-1; j+=2 ) {
392 g = (y - r * yw.x - b * yw.z) / yw.y;
396 if(components == 4) out_row1++;
401 g = (y - r * yw.x - b * yw.z) / yw.y;
405 if(components == 4) out_row1++;
410 g = (y - r * yw.x - b * yw.z) / yw.y;
414 if(components == 4) out_row1++;
419 g = (y - r * yw.x - b * yw.z) / yw.y;
423 if(components == 4) out_row1++;
431 int FileEXR::write_frame(VFrame *frame, VFrame *data, FrameWriterUnit *unit)
433 EXRUnit *exr_unit = (EXRUnit*)unit;
435 VFrame *output_frame;
436 data->set_compressed_size(0);
437 Imf::setGlobalThreadCount(file->cpus);
439 int native_cmodel = asset->exr_use_alpha ? BC_RGBA_FLOAT : BC_RGB_FLOAT;
440 int components = BC_CModels::components(native_cmodel);
442 if( frame->get_color_model() != native_cmodel ) {
443 if( !exr_unit->temp_frame ) exr_unit->temp_frame =
444 new VFrame(asset->width, asset->height, native_cmodel, 0);
445 BC_CModels::transfer(exr_unit->temp_frame->get_rows(), /* Leave NULL if non existent */
447 exr_unit->temp_frame->get_y(), /* Leave NULL if non existent */
448 exr_unit->temp_frame->get_u(),
449 exr_unit->temp_frame->get_v(),
450 frame->get_y(), /* Leave NULL if non existent */
453 0, /* Dimensions to capture from input frame */
457 0, /* Dimensions to project on output frame */
461 frame->get_color_model(),
463 0, /* When transfering BC_RGBA8888 to non-alpha this is the background color in 0xRRGGBB hex */
464 asset->width, /* For planar use the luma rowspan */
466 output_frame = exr_unit->temp_frame;
469 output_frame = frame;
471 Imf::Header header(output_frame->get_w(), output_frame->get_h());
472 header.compression() = (Imf::Compression)compression_to_exr(
473 asset->exr_compression);
474 header.channels().insert("R", Imf::Channel(Imf::FLOAT));
475 header.channels().insert("G", Imf::Channel(Imf::FLOAT));
476 header.channels().insert("B", Imf::Channel(Imf::FLOAT));
477 if(asset->exr_use_alpha) header.channels().insert("A", Imf::Channel(Imf::FLOAT));
479 EXROStream exr_stream(data);
480 Imf::OutputFile file(exr_stream, header);
481 Imf::FrameBuffer framebuffer;
482 float **rows = (float**)output_frame->get_rows();
483 framebuffer.insert("R",
484 Imf::Slice(Imf::FLOAT,
486 sizeof(float) * components,
487 sizeof(float) * components * output_frame->get_w()));
488 framebuffer.insert("G",
489 Imf::Slice(Imf::FLOAT,
490 (char*)(rows[0] + 1),
491 sizeof(float) * components,
492 sizeof(float) * components * output_frame->get_w()));
493 framebuffer.insert("B",
494 Imf::Slice(Imf::FLOAT,
495 (char*)(rows[0] + 2),
496 sizeof(float) * components,
497 sizeof(float) * components * output_frame->get_w()));
498 if(asset->exr_use_alpha)
499 framebuffer.insert("A",
500 Imf::Slice(Imf::FLOAT,
501 (char*)(rows[0] + 3),
502 sizeof(float) * components,
503 sizeof(float) * components * output_frame->get_w()));
504 file.setFrameBuffer(framebuffer);
505 file.writePixels(asset->height);
509 int FileEXR::can_copy_from(Asset *asset, int64_t position)
511 if(asset->format == FILE_EXR ||
512 asset->format == FILE_EXR_LIST)
518 FrameWriterUnit* FileEXR::new_writer_unit(FrameWriter *writer)
520 return new EXRUnit(this, writer);
524 EXRUnit::EXRUnit(FileEXR *file, FrameWriter *writer)
525 : FrameWriterUnit(writer)
533 if(temp_frame) delete temp_frame;
537 EXRConfigVideo::EXRConfigVideo(BC_WindowBase *parent_window, Asset *asset)
538 : BC_Window(_(PROGRAM_NAME ": Video Compression"),
539 parent_window->get_abs_cursor_x(1), parent_window->get_abs_cursor_y(1),
540 xS(300), BC_OKButton::calculate_h() + yS(100))
542 this->parent_window = parent_window;
544 // *** CONTEXT_HELP ***
545 context_help_set_keyword("Single File Rendering");
548 EXRConfigVideo::~EXRConfigVideo()
552 void EXRConfigVideo::create_objects()
554 lock_window("EXRConfigVideo::create_objects");
555 int x = xS(10), y = yS(10);
556 add_subwindow(new EXRUseAlpha(this, x, y));
558 EXRCompression *menu;
559 add_subwindow(new BC_Title(x, y, _("Compression:")));
561 add_subwindow(menu = new EXRCompression(this, x, y, xS(100)));
562 menu->create_objects();
563 add_subwindow(new BC_OKButton(this));
568 int EXRConfigVideo::close_event()
575 EXRUseAlpha::EXRUseAlpha(EXRConfigVideo *gui, int x, int y)
576 : BC_CheckBox(x, y, gui->asset->exr_use_alpha, _("Use alpha"))
581 int EXRUseAlpha::handle_event()
583 gui->asset->exr_use_alpha = get_value();
589 EXRCompression::EXRCompression(EXRConfigVideo *gui, int x, int y, int w)
590 : BC_PopupMenu(x, y, w,
591 FileEXR::compression_to_str(gui->asset->exr_compression))
595 void EXRCompression::create_objects()
597 add_item(new EXRCompressionItem(gui, FileEXR::NONE));
598 add_item(new EXRCompressionItem(gui, FileEXR::PIZ));
599 add_item(new EXRCompressionItem(gui, FileEXR::ZIP));
600 add_item(new EXRCompressionItem(gui, FileEXR::ZIPS));
601 add_item(new EXRCompressionItem(gui, FileEXR::RLE));
602 add_item(new EXRCompressionItem(gui, FileEXR::PXR24));
603 add_item(new EXRCompressionItem(gui, FileEXR::B44));
604 add_item(new EXRCompressionItem(gui, FileEXR::B44A));
605 add_item(new EXRCompressionItem(gui, FileEXR::DWAA));
606 add_item(new EXRCompressionItem(gui, FileEXR::DWAB));
609 int EXRCompression::handle_event()
614 EXRCompressionItem::EXRCompressionItem(EXRConfigVideo *gui, int value)
615 : BC_MenuItem(FileEXR::compression_to_str(value))
621 int EXRCompressionItem::handle_event()
623 gui->asset->exr_compression = value;