1 /*
2 * Copyright (C) 2013 Sebastian Dröge <sebastian@centricular.com>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 *
19 */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include "gstopenexrdec.h"
26
27 #include <gst/base/base.h>
28 #include <string.h>
29
30 #include <ImfRgbaFile.h>
31 #include <ImfIO.h>
32 using namespace Imf;
33 using namespace Imath;
34
35 /* Memory stream reader */
36 class MemIStream:public IStream
37 {
38 public:
MemIStream(const char * file_name,const guint8 * data,gsize size)39 MemIStream (const char *file_name, const guint8 * data,
40 gsize size):IStream (file_name), data (data), offset (0), size (size)
41 {
42 }
43
44 virtual bool read (char c[], int n);
45 virtual Int64 tellg ();
46 virtual void seekg (Int64 pos);
47 virtual void clear ();
48
49 private:
50 const guint8 *data;
51 gsize offset, size;
52 };
53
read(char c[],int n)54 bool MemIStream::read (char c[], int n)
55 {
56 if (offset + n > size)
57 throw
58 Iex::InputExc ("Unexpected end of file");
59
60 memcpy (c, data + offset, n);
61 offset += n;
62
63 return (offset == size);
64 }
65
tellg()66 Int64 MemIStream::tellg ()
67 {
68 return offset;
69 }
70
71 void
seekg(Int64 pos)72 MemIStream::seekg (Int64 pos)
73 {
74 offset = pos;
75 if (offset > size)
76 offset = size;
77 }
78
79 void
clear()80 MemIStream::clear ()
81 {
82
83 }
84
85 GST_DEBUG_CATEGORY_STATIC (gst_openexr_dec_debug);
86 #define GST_CAT_DEFAULT gst_openexr_dec_debug
87
88 static gboolean gst_openexr_dec_start (GstVideoDecoder * decoder);
89 static gboolean gst_openexr_dec_stop (GstVideoDecoder * decoder);
90 static GstFlowReturn gst_openexr_dec_parse (GstVideoDecoder * decoder,
91 GstVideoCodecFrame * frame, GstAdapter * adapter, gboolean at_eos);
92 static gboolean gst_openexr_dec_set_format (GstVideoDecoder * decoder,
93 GstVideoCodecState * state);
94 static GstFlowReturn gst_openexr_dec_handle_frame (GstVideoDecoder * decoder,
95 GstVideoCodecFrame * frame);
96 static gboolean gst_openexr_dec_decide_allocation (GstVideoDecoder * decoder,
97 GstQuery * query);
98
99 static GstStaticPadTemplate gst_openexr_dec_sink_template =
100 GST_STATIC_PAD_TEMPLATE ("sink",
101 GST_PAD_SINK,
102 GST_PAD_ALWAYS,
103 GST_STATIC_CAPS ("image/x-exr")
104 );
105
106 static GstStaticPadTemplate gst_openexr_dec_src_template =
107 GST_STATIC_PAD_TEMPLATE ("src",
108 GST_PAD_SRC,
109 GST_PAD_ALWAYS,
110 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("ARGB64"))
111 );
112
113 #define parent_class gst_openexr_dec_parent_class
114 G_DEFINE_TYPE (GstOpenEXRDec, gst_openexr_dec, GST_TYPE_VIDEO_DECODER);
115
116 static void
gst_openexr_dec_class_init(GstOpenEXRDecClass * klass)117 gst_openexr_dec_class_init (GstOpenEXRDecClass * klass)
118 {
119 GstElementClass *element_class;
120 GstVideoDecoderClass *video_decoder_class;
121
122 element_class = (GstElementClass *) klass;
123 video_decoder_class = (GstVideoDecoderClass *) klass;
124
125 gst_element_class_add_static_pad_template (element_class, &gst_openexr_dec_src_template);
126 gst_element_class_add_static_pad_template (element_class, &gst_openexr_dec_sink_template);
127
128 gst_element_class_set_static_metadata (element_class,
129 "OpenEXR decoder",
130 "Codec/Decoder/Video",
131 "Decode EXR streams", "Sebastian Dröge <sebastian@centricular.com>");
132
133 video_decoder_class->start = GST_DEBUG_FUNCPTR (gst_openexr_dec_start);
134 video_decoder_class->stop = GST_DEBUG_FUNCPTR (gst_openexr_dec_stop);
135 video_decoder_class->parse = GST_DEBUG_FUNCPTR (gst_openexr_dec_parse);
136 video_decoder_class->set_format =
137 GST_DEBUG_FUNCPTR (gst_openexr_dec_set_format);
138 video_decoder_class->handle_frame =
139 GST_DEBUG_FUNCPTR (gst_openexr_dec_handle_frame);
140 video_decoder_class->decide_allocation = gst_openexr_dec_decide_allocation;
141
142 GST_DEBUG_CATEGORY_INIT (gst_openexr_dec_debug, "openexrdec", 0,
143 "OpenEXR Decoder");
144 }
145
146 static void
gst_openexr_dec_init(GstOpenEXRDec * self)147 gst_openexr_dec_init (GstOpenEXRDec * self)
148 {
149 GstVideoDecoder *decoder = (GstVideoDecoder *) self;
150
151 gst_video_decoder_set_packetized (decoder, FALSE);
152 gst_video_decoder_set_use_default_pad_acceptcaps (GST_VIDEO_DECODER_CAST
153 (self), TRUE);
154 GST_PAD_SET_ACCEPT_TEMPLATE (GST_VIDEO_DECODER_SINK_PAD (self));
155 }
156
157 static gboolean
gst_openexr_dec_start(GstVideoDecoder * decoder)158 gst_openexr_dec_start (GstVideoDecoder * decoder)
159 {
160 GstOpenEXRDec *self = GST_OPENEXR_DEC (decoder);
161
162 GST_DEBUG_OBJECT (self, "Starting");
163
164 return TRUE;
165 }
166
167 static gboolean
gst_openexr_dec_stop(GstVideoDecoder * video_decoder)168 gst_openexr_dec_stop (GstVideoDecoder * video_decoder)
169 {
170 GstOpenEXRDec *self = GST_OPENEXR_DEC (video_decoder);
171
172 GST_DEBUG_OBJECT (self, "Stopping");
173
174 if (self->output_state) {
175 gst_video_codec_state_unref (self->output_state);
176 self->output_state = NULL;
177 }
178
179 if (self->input_state) {
180 gst_video_codec_state_unref (self->input_state);
181 self->input_state = NULL;
182 }
183
184 GST_DEBUG_OBJECT (self, "Stopped");
185
186 return TRUE;
187 }
188
189 static GstFlowReturn
gst_openexr_dec_parse(GstVideoDecoder * decoder,GstVideoCodecFrame * frame,GstAdapter * adapter,gboolean at_eos)190 gst_openexr_dec_parse (GstVideoDecoder * decoder,
191 GstVideoCodecFrame * frame, GstAdapter * adapter, gboolean at_eos)
192 {
193 guint8 data[8];
194 gsize size, parsed_size;
195 guint32 magic, flags;
196 gssize offset;
197
198 size = gst_adapter_available (adapter);
199
200 parsed_size = gst_video_decoder_get_pending_frame_size (decoder);
201
202 GST_DEBUG_OBJECT (decoder, "Parsing OpenEXR image data %" G_GSIZE_FORMAT,
203 size);
204
205 if (parsed_size == 0 && size < 8)
206 goto need_more_data;
207
208 /* If we did not parse anything yet, check if the frame starts with the header */
209 if (parsed_size == 0) {
210 gst_adapter_copy (adapter, data, 0, 8);
211
212 magic = GST_READ_UINT32_LE (data);
213 flags = GST_READ_UINT32_LE (data + 4);
214 if (magic != 0x01312f76 || ((flags & 0xff) != 1 && (flags & 0xff) != 2) || ((flags & 0x200) && (flags & 0x1800))) {
215 offset = gst_adapter_masked_scan_uint32_peek (adapter, 0xffffffff, 0x762f3101, 0, size, NULL);
216 if (offset == -1) {
217 gst_adapter_flush (adapter, size - 4);
218 goto need_more_data;
219 }
220
221 /* come back into this function after flushing some data */
222 gst_adapter_flush (adapter, offset);
223 goto need_more_data;
224 }
225 } else {
226 }
227
228 /* valid header, now let's try to find the next one unless we're EOS */
229 if (!at_eos) {
230 gboolean found = FALSE;
231
232 while (!found) {
233 offset = gst_adapter_masked_scan_uint32_peek (adapter, 0xffffffff, 0x762f3101, 8, size - 8 - 4, NULL);
234 if (offset == -1) {
235 gst_video_decoder_add_to_frame (decoder, size - 7);
236 goto need_more_data;
237 }
238
239 gst_adapter_copy (adapter, data, offset, 8);
240 magic = GST_READ_UINT32_LE (data);
241 flags = GST_READ_UINT32_LE (data + 4);
242 if (magic == 0x01312f76 && ((flags & 0xff) == 1 || (flags & 0xff) == 2) && (!(flags & 0x200) || !(flags & 0x1800)))
243 found = TRUE;
244 }
245 size = offset;
246 }
247
248 GST_DEBUG_OBJECT (decoder, "Have complete image of size %" G_GSSIZE_FORMAT, size + parsed_size);
249
250 gst_video_decoder_add_to_frame (decoder, size);
251
252 return gst_video_decoder_have_frame (decoder);
253
254 need_more_data:
255 GST_DEBUG_OBJECT (decoder, "Need more data");
256 return GST_VIDEO_DECODER_FLOW_NEED_DATA;
257 }
258
259 static gboolean
gst_openexr_dec_set_format(GstVideoDecoder * decoder,GstVideoCodecState * state)260 gst_openexr_dec_set_format (GstVideoDecoder * decoder,
261 GstVideoCodecState * state)
262 {
263 GstOpenEXRDec *self = GST_OPENEXR_DEC (decoder);
264
265 GST_DEBUG_OBJECT (self, "Setting format: %" GST_PTR_FORMAT, state->caps);
266
267 if (self->input_state)
268 gst_video_codec_state_unref (self->input_state);
269 self->input_state = gst_video_codec_state_ref (state);
270
271 return TRUE;
272 }
273
274 static GstFlowReturn
gst_openexr_dec_negotiate(GstOpenEXRDec * self,RgbaInputFile * file)275 gst_openexr_dec_negotiate (GstOpenEXRDec * self, RgbaInputFile * file)
276 {
277 GstVideoFormat format;
278 gint width, height;
279 gfloat par;
280
281 /* TODO: Use displayWindow here and also support output of ARGB_F16 */
282 format = GST_VIDEO_FORMAT_ARGB64;
283 Box2i dw = file->dataWindow ();
284 width = dw.max.x - dw.min.x + 1;
285 height = dw.max.y - dw.min.y + 1;
286 par = file->pixelAspectRatio ();
287
288 if (!self->output_state ||
289 self->output_state->info.finfo->format != format ||
290 self->output_state->info.width != width ||
291 self->output_state->info.height != height) {
292 if (self->output_state)
293 gst_video_codec_state_unref (self->output_state);
294 self->output_state =
295 gst_video_decoder_set_output_state (GST_VIDEO_DECODER (self), format,
296 width, height, self->input_state);
297
298 GST_DEBUG_OBJECT (self, "Have image of size %dx%d (par %f)", width, height, par);
299 gst_util_double_to_fraction (par, &self->output_state->info.par_n, &self->output_state->info.par_d);
300
301 if (!gst_video_decoder_negotiate (GST_VIDEO_DECODER (self)))
302 return GST_FLOW_NOT_NEGOTIATED;
303 }
304
305 return GST_FLOW_OK;
306 }
307
308 static GstFlowReturn
gst_openexr_dec_handle_frame(GstVideoDecoder * decoder,GstVideoCodecFrame * frame)309 gst_openexr_dec_handle_frame (GstVideoDecoder * decoder,
310 GstVideoCodecFrame * frame)
311 {
312 GstOpenEXRDec *self = GST_OPENEXR_DEC (decoder);
313 GstFlowReturn ret = GST_FLOW_OK;
314 gint64 deadline;
315 GstMapInfo map;
316 GstVideoFrame vframe;
317
318 GST_DEBUG_OBJECT (self, "Handling frame");
319
320 deadline = gst_video_decoder_get_max_decode_time (decoder, frame);
321 if (deadline < 0) {
322 GST_LOG_OBJECT (self, "Dropping too late frame: deadline %" G_GINT64_FORMAT,
323 deadline);
324 ret = gst_video_decoder_drop_frame (decoder, frame);
325 return ret;
326 }
327
328 if (!gst_buffer_map (frame->input_buffer, &map, GST_MAP_READ)) {
329 gst_video_codec_frame_unref (frame);
330
331 GST_ELEMENT_ERROR (self, CORE, FAILED,
332 ("Failed to map input buffer"), (NULL));
333 return GST_FLOW_ERROR;
334 }
335
336 /* Now read the file and catch any exceptions */
337 MemIStream *istr;
338 RgbaInputFile *file;
339 try {
340 istr =
341 new
342 MemIStream (gst_pad_get_stream_id (GST_VIDEO_DECODER_SINK_PAD
343 (decoder)), map.data, map.size);
344 }
345 catch (Iex::BaseExc& e) {
346 gst_video_codec_frame_unref (frame);
347
348 GST_ELEMENT_ERROR (self, CORE, FAILED,
349 ("Failed to create input stream"), (NULL));
350 return GST_FLOW_ERROR;
351 }
352 try {
353 file = new RgbaInputFile (*istr);
354 }
355 catch (Iex::BaseExc& e) {
356 delete istr;
357 gst_video_codec_frame_unref (frame);
358
359 GST_ELEMENT_ERROR (self, CORE, FAILED,
360 ("Failed to read OpenEXR stream"), (NULL));
361 return GST_FLOW_ERROR;
362 }
363
364 ret = gst_openexr_dec_negotiate (self, file);
365 if (ret != GST_FLOW_OK) {
366 delete file;
367 delete istr;
368 gst_buffer_unmap (frame->input_buffer, &map);
369 gst_video_codec_frame_unref (frame);
370
371 GST_ELEMENT_ERROR (self, CORE, NEGOTIATION,
372 ("Failed to negotiate"), (NULL));
373 return ret;
374 }
375
376 ret = gst_video_decoder_allocate_output_frame (decoder, frame);
377 if (ret != GST_FLOW_OK) {
378 delete file;
379 delete istr;
380 gst_buffer_unmap (frame->input_buffer, &map);
381 gst_video_codec_frame_unref (frame);
382
383 GST_ELEMENT_ERROR (self, CORE, FAILED,
384 ("Failed to allocate output buffer"), (NULL));
385 return ret;
386 }
387
388 if (!gst_video_frame_map (&vframe, &self->output_state->info,
389 frame->output_buffer, GST_MAP_WRITE)) {
390 delete file;
391 delete istr;
392 gst_buffer_unmap (frame->input_buffer, &map);
393 gst_video_codec_frame_unref (frame);
394
395 GST_ELEMENT_ERROR (self, CORE, FAILED,
396 ("Failed to map output buffer"), (NULL));
397 return GST_FLOW_ERROR;
398 }
399
400 /* Decode the file */
401 Box2i dw = file->dataWindow ();
402 int width = dw.max.x - dw.min.x + 1;
403 int height = dw.max.y - dw.min.y + 1;
404 Rgba *fb = new Rgba[width * height];
405
406 try {
407 file->setFrameBuffer (fb - dw.min.x - dw.min.y * width, 1, width);
408 file->readPixels (dw.min.y, dw.max.y);
409 } catch (Iex::BaseExc& e) {
410 delete[](fb);
411 delete file;
412 delete istr;
413 gst_buffer_unmap (frame->input_buffer, &map);
414 gst_video_frame_unmap (&vframe);
415
416 GST_ELEMENT_ERROR (self, CORE, FAILED, ("Failed to read pixels"), (NULL));
417 return GST_FLOW_ERROR;
418 }
419
420 /* And convert from ARGB64_F16 to ARGB64 */
421 gint i, j;
422 guint16 *dest = (guint16 *) GST_VIDEO_FRAME_PLANE_DATA (&vframe, 0);
423 guint dstride = GST_VIDEO_FRAME_PLANE_STRIDE (&vframe, 0);
424 Rgba *ptr = fb;
425
426 /* TODO: Use displayWindow here and also support output of ARGB_F16
427 * and add a conversion filter element that can change exposure and
428 * other things */
429 for (i = 0; i < height; i++) {
430 for (j = 0; j < width; j++) {
431 dest[4 * j + 0] = CLAMP (((float) ptr->a) * 65536, 0, 65535);
432 dest[4 * j + 1] = CLAMP (((float) ptr->r) * 65536, 0, 65535);
433 dest[4 * j + 2] = CLAMP (((float) ptr->g) * 65536, 0, 65535);
434 dest[4 * j + 3] = CLAMP (((float) ptr->b) * 65536, 0, 65535);
435 ptr++;
436 }
437 dest += dstride / 2;
438 }
439
440 delete[](fb);
441 delete file;
442 delete istr;
443 gst_buffer_unmap (frame->input_buffer, &map);
444 gst_video_frame_unmap (&vframe);
445
446 ret = gst_video_decoder_finish_frame (decoder, frame);
447
448 return ret;
449 }
450
451 static gboolean
gst_openexr_dec_decide_allocation(GstVideoDecoder * decoder,GstQuery * query)452 gst_openexr_dec_decide_allocation (GstVideoDecoder * decoder, GstQuery * query)
453 {
454 GstBufferPool *pool;
455 GstStructure *config;
456
457 if (!GST_VIDEO_DECODER_CLASS (parent_class)->decide_allocation (decoder,
458 query))
459 return FALSE;
460
461 g_assert (gst_query_get_n_allocation_pools (query) > 0);
462 gst_query_parse_nth_allocation_pool (query, 0, &pool, NULL, NULL, NULL);
463 g_assert (pool != NULL);
464
465 config = gst_buffer_pool_get_config (pool);
466 if (gst_query_find_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL)) {
467 gst_buffer_pool_config_add_option (config,
468 GST_BUFFER_POOL_OPTION_VIDEO_META);
469 }
470 gst_buffer_pool_set_config (pool, config);
471 gst_object_unref (pool);
472
473 return TRUE;
474 }
475