Branch data Line data Source code
1 : : /* GStreamer Jasper based j2k image decoder
2 : : * Copyright (C) 2008 Mark Nauwelaerts <mnauw@users.sf.net>
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., 59 Temple Place - Suite 330,
17 : : * Boston, MA 02111-1307, USA.
18 : : */
19 : :
20 : : /**
21 : : * SECTION:element-jasperdec
22 : : *
23 : : * Decodes jpeg2000 images.
24 : : */
25 : :
26 : : #ifdef HAVE_CONFIG_H
27 : : #include "config.h"
28 : : #endif
29 : : #include <gst/gst.h>
30 : : #include <gst/video/video.h>
31 : : #include <string.h>
32 : :
33 : : #include <jasper/jasper.h>
34 : :
35 : : #include "gstjasperdec.h"
36 : :
37 : : GST_DEBUG_CATEGORY_STATIC (gst_jasper_dec_debug);
38 : : #define GST_CAT_DEFAULT gst_jasper_dec_debug
39 : :
40 : : enum
41 : : {
42 : : ARG_0,
43 : : };
44 : :
45 : : /* FIXME 0.11: Use a single caps name for jpeg2000 codestreams
46 : : * and drop the "boxed" variant
47 : : */
48 : :
49 : : static GstStaticPadTemplate gst_jasper_dec_sink_template =
50 : : GST_STATIC_PAD_TEMPLATE ("sink",
51 : : GST_PAD_SINK,
52 : : GST_PAD_ALWAYS,
53 : : GST_STATIC_CAPS ("image/x-j2c, "
54 : : "framerate = " GST_VIDEO_FPS_RANGE ", "
55 : : "fields = (int) 1; "
56 : : "image/x-jpc, "
57 : : "framerate = " GST_VIDEO_FPS_RANGE ", "
58 : : "fields = (int) 1; " "image/jp2")
59 : : );
60 : :
61 : : static GstStaticPadTemplate gst_jasper_dec_src_template =
62 : : GST_STATIC_PAD_TEMPLATE ("src",
63 : : GST_PAD_SRC,
64 : : GST_PAD_ALWAYS,
65 : : GST_STATIC_CAPS (GST_VIDEO_CAPS_RGB "; " GST_VIDEO_CAPS_BGR "; "
66 : : GST_VIDEO_CAPS_RGBx "; " GST_VIDEO_CAPS_xRGB "; "
67 : : GST_VIDEO_CAPS_BGRx "; " GST_VIDEO_CAPS_xBGR "; "
68 : : GST_VIDEO_CAPS_YUV ("{ I420, YV12, YUY2, UYVY, Y41B, Y42B, v308 }"))
69 : : );
70 : :
71 : : static void gst_jasper_dec_set_property (GObject * object, guint prop_id,
72 : : const GValue * value, GParamSpec * pspec);
73 : : static void gst_jasper_dec_get_property (GObject * object, guint prop_id,
74 : : GValue * value, GParamSpec * pspec);
75 : :
76 : : static void gst_jasper_dec_reset (GstJasperDec * dec);
77 : : static GstStateChangeReturn gst_jasper_dec_change_state (GstElement * element,
78 : : GstStateChange transition);
79 : : static gboolean gst_jasper_dec_sink_setcaps (GstPad * pad, GstCaps * caps);
80 : : static GstFlowReturn gst_jasper_dec_chain (GstPad * pad, GstBuffer * buffer);
81 : : static gboolean gst_jasper_dec_src_event (GstPad * pad, GstEvent * event);
82 : : static gboolean gst_jasper_dec_sink_event (GstPad * pad, GstEvent * event);
83 : : static void gst_jasper_dec_update_qos (GstJasperDec * dec, gdouble proportion,
84 : : GstClockTime time);
85 : : static void gst_jasper_dec_reset_qos (GstJasperDec * dec);
86 : : static void gst_jasper_dec_read_qos (GstJasperDec * dec, gdouble * proportion,
87 : : GstClockTime * time);
88 : :
89 : : /* minor trick:
90 : : * keep original naming but use unique name here for a happy type system
91 : : */
92 : :
93 : : typedef GstJasperDec GstJp2kDec;
94 : : typedef GstJasperDecClass GstJp2kDecClass;
95 : :
96 [ # # ]: 0 : GST_BOILERPLATE (GstJp2kDec, gst_jasper_dec, GstElement, GST_TYPE_ELEMENT);
97 : :
98 : : static void
99 : 0 : gst_jasper_dec_base_init (gpointer g_class)
100 : : {
101 : 0 : GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
102 : :
103 : 0 : gst_element_class_add_pad_template (element_class,
104 : : gst_static_pad_template_get (&gst_jasper_dec_src_template));
105 : 0 : gst_element_class_add_pad_template (element_class,
106 : : gst_static_pad_template_get (&gst_jasper_dec_sink_template));
107 : 0 : gst_element_class_set_details_simple (element_class,
108 : : "Jasper JPEG2000 image decoder", "Codec/Decoder/Image",
109 : : "Decodes JPEG2000 encoded images using jasper",
110 : : "Mark Nauwelaerts <mnauw@users.sf.net>");
111 : 0 : }
112 : :
113 : : /* initialize the plugin's class */
114 : : static void
115 : 0 : gst_jasper_dec_class_init (GstJasperDecClass * klass)
116 : : {
117 : : GObjectClass *gobject_class;
118 : : GstElementClass *gstelement_class;
119 : :
120 : 0 : gobject_class = (GObjectClass *) klass;
121 : 0 : gstelement_class = (GstElementClass *) klass;
122 : :
123 [ # # ]: 0 : GST_DEBUG_CATEGORY_INIT (gst_jasper_dec_debug, "jp2kdec", 0,
124 : : "Jasper JPEG2000 decoder");
125 : :
126 : 0 : gobject_class->set_property = gst_jasper_dec_set_property;
127 : 0 : gobject_class->get_property = gst_jasper_dec_get_property;
128 : :
129 : 0 : gstelement_class->change_state =
130 : 0 : GST_DEBUG_FUNCPTR (gst_jasper_dec_change_state);
131 : 0 : }
132 : :
133 : : static void
134 : 0 : gst_jasper_dec_init (GstJasperDec * dec, GstJasperDecClass * klass)
135 : : {
136 : 0 : dec->sinkpad =
137 : 0 : gst_pad_new_from_static_template (&gst_jasper_dec_sink_template, "sink");
138 : 0 : gst_pad_set_setcaps_function (dec->sinkpad,
139 : 0 : GST_DEBUG_FUNCPTR (gst_jasper_dec_sink_setcaps));
140 : 0 : gst_pad_set_chain_function (dec->sinkpad,
141 : 0 : GST_DEBUG_FUNCPTR (gst_jasper_dec_chain));
142 : 0 : gst_pad_set_event_function (dec->sinkpad,
143 : 0 : GST_DEBUG_FUNCPTR (gst_jasper_dec_sink_event));
144 : 0 : gst_element_add_pad (GST_ELEMENT (dec), dec->sinkpad);
145 : :
146 : 0 : dec->srcpad =
147 : 0 : gst_pad_new_from_static_template (&gst_jasper_dec_src_template, "src");
148 : 0 : gst_pad_use_fixed_caps (dec->srcpad);
149 : 0 : gst_pad_set_event_function (dec->srcpad,
150 : 0 : GST_DEBUG_FUNCPTR (gst_jasper_dec_src_event));
151 : 0 : gst_element_add_pad (GST_ELEMENT (dec), dec->srcpad);
152 : :
153 : 0 : dec->codec_data = NULL;
154 : 0 : dec->buf = NULL;
155 : 0 : gst_jasper_dec_reset (dec);
156 : 0 : }
157 : :
158 : : static void
159 : 0 : gst_jasper_dec_reset (GstJasperDec * dec)
160 : : {
161 [ # # ]: 0 : if (dec->codec_data)
162 : 0 : gst_buffer_unref (dec->codec_data);
163 : 0 : dec->codec_data = NULL;
164 [ # # ]: 0 : if (dec->buf)
165 : 0 : g_free (dec->buf);
166 : 0 : dec->buf = NULL;
167 : 0 : dec->fmt = -1;
168 : 0 : dec->clrspc = JAS_CLRSPC_UNKNOWN;
169 : 0 : dec->format = GST_VIDEO_FORMAT_UNKNOWN;
170 : 0 : gst_jasper_dec_reset_qos (dec);
171 : 0 : gst_segment_init (&dec->segment, GST_FORMAT_TIME);
172 : 0 : dec->discont = TRUE;
173 : 0 : }
174 : :
175 : : static gboolean
176 : 0 : gst_jasper_dec_sink_setcaps (GstPad * pad, GstCaps * caps)
177 : : {
178 : : GstJasperDec *dec;
179 : : const GValue *framerate;
180 : : GstStructure *s;
181 : : const gchar *mimetype;
182 : : guint32 fourcc;
183 : :
184 : 0 : dec = GST_JASPER_DEC (GST_PAD_PARENT (pad));
185 : 0 : s = gst_caps_get_structure (caps, 0);
186 : 0 : mimetype = gst_structure_get_name (s);
187 : :
188 : : /* reset negotiation */
189 : 0 : dec->fmt = -1;
190 : 0 : dec->strip = 0;
191 : 0 : dec->format = GST_VIDEO_FORMAT_UNKNOWN;
192 [ # # ]: 0 : if (dec->codec_data) {
193 : 0 : gst_buffer_unref (dec->codec_data);
194 : 0 : dec->codec_data = NULL;
195 : : }
196 : :
197 [ # # ][ # # ]: 0 : if (!strcmp (mimetype, "image/x-j2c") || !strcmp (mimetype, "image/x-jpc")) {
198 : : const GValue *codec_data;
199 : : gint fields;
200 : :
201 : : /* we only handle single field, packetized input */
202 [ # # ]: 0 : if (gst_structure_get_value (s, "framerate") == NULL)
203 : 0 : goto refuse_caps;
204 [ # # ][ # # ]: 0 : if (gst_structure_get_int (s, "fields", &fields) && fields != 1)
205 : 0 : goto refuse_caps;
206 : :
207 [ # # ]: 0 : if (!gst_structure_get_fourcc (s, "fourcc", &fourcc))
208 : 0 : goto refuse_caps;
209 [ # # # ]: 0 : switch (fourcc) {
210 : : case GST_MAKE_FOURCC ('s', 'R', 'G', 'B'):
211 : 0 : dec->clrspc = JAS_CLRSPC_SRGB;
212 : 0 : break;
213 : : case GST_MAKE_FOURCC ('s', 'Y', 'U', 'V'):
214 : 0 : dec->clrspc = JAS_CLRSPC_SYCBCR;
215 : 0 : break;
216 : : default:
217 : 0 : goto refuse_caps;
218 : : break;
219 : : }
220 : :
221 : 0 : dec->fmt = jas_image_strtofmt ((char *) "jpc");
222 : : /* strip the j2c box stuff it is embedded in */
223 [ # # ]: 0 : if (!strcmp (mimetype, "image/x-jpc"))
224 : 0 : dec->strip = 0;
225 : : else
226 : 0 : dec->strip = 8;
227 : :
228 : 0 : codec_data = gst_structure_get_value (s, "codec_data");
229 [ # # ]: 0 : if (codec_data) {
230 : 0 : dec->codec_data = gst_value_get_buffer (codec_data);
231 : 0 : gst_buffer_ref (dec->codec_data);
232 : : }
233 [ # # ]: 0 : } else if (!strcmp (mimetype, "image/jp2"))
234 : 0 : dec->fmt = jas_image_strtofmt ((char *) "jp2");
235 : :
236 [ # # ]: 0 : if (dec->fmt < 0)
237 : 0 : goto refuse_caps;
238 : :
239 [ # # ]: 0 : if ((framerate = gst_structure_get_value (s, "framerate")) != NULL) {
240 : 0 : dec->framerate_numerator = gst_value_get_fraction_numerator (framerate);
241 : 0 : dec->framerate_denominator = gst_value_get_fraction_denominator (framerate);
242 [ # # ]: 0 : GST_DEBUG_OBJECT (dec, "got framerate of %d/%d fps => packetized mode",
243 : : dec->framerate_numerator, dec->framerate_denominator);
244 : : } else {
245 : 0 : dec->framerate_numerator = 0;
246 : 0 : dec->framerate_denominator = 1;
247 [ # # ]: 0 : GST_DEBUG_OBJECT (dec, "no framerate, assuming single image");
248 : : }
249 : :
250 : 0 : return TRUE;
251 : :
252 : : refuse_caps:
253 : : {
254 [ # # ]: 0 : GST_WARNING_OBJECT (dec, "refused caps %" GST_PTR_FORMAT, caps);
255 : 0 : return FALSE;
256 : : }
257 : : }
258 : :
259 : : static GstFlowReturn
260 : 0 : gst_jasper_dec_negotiate (GstJasperDec * dec, jas_image_t * image)
261 : : {
262 : 0 : GstFlowReturn flow_ret = GST_FLOW_OK;
263 : : gint width, height, channels;
264 : : gint i, j;
265 : 0 : gboolean negotiate = FALSE;
266 : : jas_clrspc_t clrspc;
267 : : GstCaps *allowed_caps, *caps;
268 : :
269 : 0 : width = jas_image_width (image);
270 : 0 : height = jas_image_height (image);
271 : 0 : channels = jas_image_numcmpts (image);
272 : :
273 [ # # ]: 0 : GST_LOG_OBJECT (dec, "%d x %d, %d components", width, height, channels);
274 : :
275 : : /* jp2c bitstream has no real colour space info (kept in container),
276 : : * so decoder may only pretend to know, where it really does not */
277 [ # # ]: 0 : if (!jas_clrspc_isunknown (dec->clrspc)) {
278 : 0 : clrspc = dec->clrspc;
279 [ # # ]: 0 : GST_DEBUG_OBJECT (dec, "forcing container supplied colour space %d",
280 : : clrspc);
281 : 0 : jas_image_setclrspc (image, clrspc);
282 : : } else
283 : 0 : clrspc = jas_image_clrspc (image);
284 : :
285 [ # # ][ # # ]: 0 : if (!width || !height || !channels || jas_clrspc_isunknown (clrspc))
[ # # ][ # # ]
286 : : goto fail_image;
287 : :
288 [ # # ][ # # ]: 0 : if (dec->width != width || dec->height != height ||
[ # # ]
289 [ # # ]: 0 : dec->channels != channels || dec->clrspc != clrspc)
290 : 0 : negotiate = TRUE;
291 : :
292 [ # # ]: 0 : if (channels != 3)
293 : 0 : goto not_supported;
294 : :
295 [ # # ]: 0 : for (i = 0; i < channels; i++) {
296 : : gint cheight, cwidth, depth, sgnd;
297 : :
298 : 0 : cheight = jas_image_cmptheight (image, i);
299 : 0 : cwidth = jas_image_cmptwidth (image, i);
300 : 0 : depth = jas_image_cmptprec (image, i);
301 : 0 : sgnd = jas_image_cmptsgnd (image, i);
302 : :
303 [ # # ]: 0 : GST_LOG_OBJECT (dec, "image component %d, %dx%d, depth %d, sgnd %d", i,
304 : : cwidth, cheight, depth, sgnd);
305 : :
306 [ # # ][ # # ]: 0 : if (depth != 8 || sgnd)
307 : : goto not_supported;
308 : :
309 [ # # ][ # # ]: 0 : if (dec->cheight[i] != cheight || dec->cwidth[i] != cwidth) {
310 : 0 : dec->cheight[i] = cheight;
311 : 0 : dec->cwidth[i] = cwidth;
312 : 0 : negotiate = TRUE;
313 : : }
314 : : }
315 : :
316 [ # # ][ # # ]: 0 : if (!negotiate && dec->format != GST_VIDEO_FORMAT_UNKNOWN)
317 : 0 : goto done;
318 : :
319 : : /* clear and refresh to new state */
320 : 0 : flow_ret = GST_FLOW_NOT_NEGOTIATED;
321 : 0 : dec->format = GST_VIDEO_FORMAT_UNKNOWN;
322 : 0 : dec->width = width;
323 : 0 : dec->height = height;
324 : 0 : dec->channels = channels;
325 : :
326 : : /* retrieve allowed caps, and find the first one that reasonably maps
327 : : * to the parameters of the colourspace */
328 : 0 : caps = gst_pad_get_allowed_caps (dec->srcpad);
329 [ # # ]: 0 : if (!caps) {
330 [ # # ]: 0 : GST_DEBUG_OBJECT (dec, "... but no peer, using template caps");
331 : : /* need to copy because get_allowed_caps returns a ref,
332 : : and get_pad_template_caps doesn't */
333 : 0 : caps = gst_caps_copy (gst_pad_get_pad_template_caps (dec->srcpad));
334 : : }
335 : : /* avoid lists of fourcc, etc */
336 : 0 : allowed_caps = gst_caps_normalize (caps);
337 : 0 : gst_caps_unref (caps);
338 : 0 : caps = NULL;
339 [ # # ]: 0 : GST_LOG_OBJECT (dec, "allowed source caps %" GST_PTR_FORMAT, allowed_caps);
340 : :
341 [ # # ]: 0 : for (i = 0; i < gst_caps_get_size (allowed_caps); i++) {
342 : : GstVideoFormat format;
343 : : gboolean ok;
344 : :
345 [ # # ]: 0 : if (caps)
346 : 0 : gst_caps_unref (caps);
347 : 0 : caps = gst_caps_copy_nth (allowed_caps, i);
348 : : /* sigh, ds and _parse_caps need fixed caps for parsing, fixate */
349 : 0 : gst_pad_fixate_caps (dec->srcpad, caps);
350 [ # # ]: 0 : GST_LOG_OBJECT (dec, "checking caps %" GST_PTR_FORMAT, caps);
351 [ # # ]: 0 : if (!gst_video_format_parse_caps (caps, &format, NULL, NULL))
352 : 0 : continue;
353 [ # # ][ # # ]: 0 : if (gst_video_format_is_rgb (format) &&
354 : 0 : jas_clrspc_fam (clrspc) == JAS_CLRSPC_FAM_RGB) {
355 [ # # ]: 0 : GST_DEBUG_OBJECT (dec, "trying RGB");
356 [ # # ]: 0 : if ((dec->cmpt[0] = jas_image_getcmptbytype (image,
357 [ # # ]: 0 : JAS_IMAGE_CT_COLOR (JAS_CLRSPC_CHANIND_RGB_R))) < 0 ||
358 : 0 : (dec->cmpt[1] = jas_image_getcmptbytype (image,
359 [ # # ]: 0 : JAS_IMAGE_CT_COLOR (JAS_CLRSPC_CHANIND_RGB_G))) < 0 ||
360 : 0 : (dec->cmpt[2] = jas_image_getcmptbytype (image,
361 : : JAS_IMAGE_CT_COLOR (JAS_CLRSPC_CHANIND_RGB_B))) < 0) {
362 [ # # ]: 0 : GST_DEBUG_OBJECT (dec, "missing RGB color component");
363 : 0 : continue;
364 : : }
365 [ # # ][ # # ]: 0 : } else if (gst_video_format_is_yuv (format) &&
366 : 0 : jas_clrspc_fam (clrspc) == JAS_CLRSPC_FAM_YCBCR) {
367 [ # # ]: 0 : GST_DEBUG_OBJECT (dec, "trying YUV");
368 [ # # ]: 0 : if ((dec->cmpt[0] = jas_image_getcmptbytype (image,
369 [ # # ]: 0 : JAS_IMAGE_CT_COLOR (JAS_CLRSPC_CHANIND_YCBCR_Y))) < 0 ||
370 : 0 : (dec->cmpt[1] = jas_image_getcmptbytype (image,
371 [ # # ]: 0 : JAS_IMAGE_CT_COLOR (JAS_CLRSPC_CHANIND_YCBCR_CB))) < 0 ||
372 : 0 : (dec->cmpt[2] = jas_image_getcmptbytype (image,
373 : : JAS_IMAGE_CT_COLOR (JAS_CLRSPC_CHANIND_YCBCR_CR))) < 0) {
374 [ # # ]: 0 : GST_DEBUG_OBJECT (dec, "missing YUV color component");
375 : 0 : continue;
376 : : }
377 : : } else
378 : 0 : continue;
379 : : /* match format with validity checks */
380 : 0 : ok = TRUE;
381 [ # # ]: 0 : for (j = 0; j < channels; j++) {
382 : : gint cmpt;
383 : :
384 : 0 : cmpt = dec->cmpt[j];
385 [ # # ]: 0 : if (dec->cwidth[cmpt] != gst_video_format_get_component_width (format, j,
386 [ # # ]: 0 : width) ||
387 : 0 : dec->cheight[cmpt] != gst_video_format_get_component_height (format,
388 : : j, height))
389 : 0 : ok = FALSE;
390 : : }
391 : : /* commit to this format */
392 [ # # ]: 0 : if (ok) {
393 : 0 : dec->format = format;
394 : 0 : break;
395 : : }
396 : : }
397 : :
398 [ # # ]: 0 : if (caps)
399 : 0 : gst_caps_unref (caps);
400 : 0 : gst_caps_unref (allowed_caps);
401 : :
402 [ # # ]: 0 : if (dec->format != GST_VIDEO_FORMAT_UNKNOWN) {
403 : : /* cache some video format properties */
404 [ # # ]: 0 : for (j = 0; j < channels; ++j) {
405 : 0 : dec->offset[j] = gst_video_format_get_component_offset (dec->format, j,
406 : : dec->width, dec->height);
407 : 0 : dec->inc[j] = gst_video_format_get_pixel_stride (dec->format, j);
408 : 0 : dec->stride[j] = gst_video_format_get_row_stride (dec->format, j,
409 : : dec->width);
410 : : }
411 : 0 : dec->image_size = gst_video_format_get_size (dec->format, width, height);
412 : 0 : dec->alpha = gst_video_format_has_alpha (dec->format);
413 : :
414 [ # # ]: 0 : if (dec->buf)
415 : 0 : g_free (dec->buf);
416 : 0 : dec->buf = g_new0 (glong, dec->width);
417 : :
418 : 0 : caps = gst_video_format_new_caps (dec->format, dec->width, dec->height,
419 : : dec->framerate_numerator, dec->framerate_denominator, 1, 1);
420 : :
421 [ # # ]: 0 : GST_DEBUG_OBJECT (dec, "Set format to %d, size to %dx%d", dec->format,
422 : : dec->width, dec->height);
423 : :
424 [ # # ]: 0 : if (!gst_pad_set_caps (dec->srcpad, caps))
425 : 0 : flow_ret = GST_FLOW_NOT_NEGOTIATED;
426 : : else
427 : 0 : flow_ret = GST_FLOW_OK;
428 : :
429 : 0 : gst_caps_unref (caps);
430 : : }
431 : :
432 : : done:
433 : 0 : return flow_ret;
434 : :
435 : : /* ERRORS */
436 : : fail_image:
437 : : {
438 [ # # ]: 0 : GST_DEBUG_OBJECT (dec, "Failed to process decoded image.");
439 : 0 : flow_ret = GST_FLOW_NOT_NEGOTIATED;
440 : 0 : goto done;
441 : : }
442 : : not_supported:
443 : : {
444 [ # # ]: 0 : GST_DEBUG_OBJECT (dec, "Decoded image has unsupported colour space.");
445 [ # # ][ # # ]: 0 : GST_ELEMENT_ERROR (dec, STREAM, DECODE, (NULL), ("Unsupported colorspace"));
[ # # ][ # # ]
446 : 0 : flow_ret = GST_FLOW_ERROR;
447 : 0 : goto done;
448 : : }
449 : : }
450 : :
451 : : static GstFlowReturn
452 : 0 : gst_jasper_dec_get_picture (GstJasperDec * dec, guint8 * data,
453 : : guint size, GstBuffer ** outbuf)
454 : : {
455 : 0 : GstFlowReturn ret = GST_FLOW_OK;
456 : 0 : jas_stream_t *stream = NULL;
457 : 0 : jas_image_t *image = NULL;
458 : : gint i;
459 : :
460 [ # # ]: 0 : g_return_val_if_fail (outbuf != NULL, GST_FLOW_ERROR);
461 : :
462 : 0 : *outbuf = NULL;
463 : :
464 [ # # ]: 0 : if (!(stream = jas_stream_memopen ((gpointer) data, size)))
465 : 0 : goto fail_stream;
466 : :
467 [ # # ]: 0 : if (!(image = jas_image_decode (stream, dec->fmt, (char *) "")))
468 : 0 : goto fail_decode;
469 : :
470 : 0 : ret = gst_jasper_dec_negotiate (dec, image);
471 [ # # ]: 0 : if (ret != GST_FLOW_OK)
472 : 0 : goto fail_negotiate;
473 : :
474 : 0 : ret = gst_pad_alloc_buffer_and_set_caps (dec->srcpad,
475 : : GST_BUFFER_OFFSET_NONE,
476 : 0 : dec->image_size, GST_PAD_CAPS (dec->srcpad), outbuf);
477 : :
478 [ # # ]: 0 : if (ret != GST_FLOW_OK)
479 : 0 : goto no_buffer;
480 : :
481 [ # # ]: 0 : if (dec->alpha)
482 : 0 : memset (GST_BUFFER_DATA (*outbuf), 0xff, dec->image_size);
483 : :
484 [ # # ]: 0 : for (i = 0; i < dec->channels; ++i) {
485 : : gint x, y, cwidth, cheight, inc, stride, cmpt;
486 : : guint8 *row_pix, *out_pix;
487 : : glong *tb;
488 : :
489 : 0 : inc = dec->inc[i];
490 : 0 : stride = dec->stride[i];
491 : 0 : cmpt = dec->cmpt[i];
492 : 0 : cheight = dec->cheight[cmpt];
493 : 0 : cwidth = dec->cwidth[cmpt];
494 : :
495 [ # # ]: 0 : GST_LOG_OBJECT (dec,
496 : : "retrieve component %d<=%d, size %dx%d, offset %d, inc %d, stride %d",
497 : : i, cmpt, cwidth, cheight, dec->offset[i], inc, stride);
498 : :
499 : 0 : out_pix = GST_BUFFER_DATA (*outbuf) + dec->offset[i];
500 : :
501 [ # # ]: 0 : for (y = 0; y < cheight; y++) {
502 : 0 : row_pix = out_pix;
503 : 0 : tb = dec->buf;
504 [ # # ]: 0 : if (jas_image_readcmpt2 (image, i, 0, y, cwidth, 1, dec->buf))
505 : 0 : goto fail_image;
506 [ # # ]: 0 : for (x = 0; x < cwidth; x++) {
507 : 0 : *out_pix = *tb;
508 : 0 : tb++;
509 : 0 : out_pix += inc;
510 : : }
511 : 0 : out_pix = row_pix + stride;
512 : : }
513 : : }
514 : :
515 [ # # ]: 0 : GST_LOG_OBJECT (dec, "all components retrieved");
516 : :
517 : : done:
518 [ # # ]: 0 : if (image)
519 : 0 : jas_image_destroy (image);
520 [ # # ]: 0 : if (stream)
521 : 0 : jas_stream_close (stream);
522 : :
523 : 0 : return ret;
524 : :
525 : : /* ERRORS */
526 : : fail_stream:
527 : : {
528 [ # # ]: 0 : GST_DEBUG_OBJECT (dec, "Failed to create inputstream.");
529 : 0 : goto fail;
530 : : }
531 : : fail_decode:
532 : : {
533 [ # # ]: 0 : GST_DEBUG_OBJECT (dec, "Failed to decode image.");
534 : 0 : goto fail;
535 : : }
536 : : fail_image:
537 : : {
538 [ # # ]: 0 : GST_DEBUG_OBJECT (dec, "Failed to process decoded image.");
539 : 0 : goto fail;
540 : : }
541 : : fail:
542 : : {
543 [ # # ]: 0 : if (*outbuf)
544 : 0 : gst_buffer_unref (*outbuf);
545 : 0 : *outbuf = NULL;
546 [ # # ][ # # ]: 0 : GST_ELEMENT_WARNING (dec, STREAM, DECODE, (NULL), (NULL));
[ # # ][ # # ]
547 : 0 : ret = GST_FLOW_OK;
548 : 0 : goto done;
549 : : }
550 : : no_buffer:
551 : : {
552 [ # # ]: 0 : GST_DEBUG_OBJECT (dec, "Failed to create outbuffer - %s",
553 : : gst_flow_get_name (ret));
554 : 0 : goto done;
555 : : }
556 : : fail_negotiate:
557 : : {
558 [ # # ]: 0 : GST_DEBUG_OBJECT (dec, "Failed to determine output caps.");
559 : 0 : goto done;
560 : : }
561 : : }
562 : :
563 : : /* Perform qos calculations before decoding the next frame. Returns TRUE if the
564 : : * frame should be decoded, FALSE if the frame can be dropped entirely */
565 : : static gboolean
566 : 0 : gst_jasper_dec_do_qos (GstJasperDec * dec, GstClockTime timestamp)
567 : : {
568 : : GstClockTime qostime, earliest_time;
569 : : gdouble proportion;
570 : :
571 : : /* no timestamp, can't do QoS => decode frame */
572 [ # # ]: 0 : if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (timestamp))) {
573 [ # # ]: 0 : GST_LOG_OBJECT (dec, "invalid timestamp, can't do QoS, decode frame");
574 : 0 : return TRUE;
575 : : }
576 : :
577 : : /* get latest QoS observation values */
578 : 0 : gst_jasper_dec_read_qos (dec, &proportion, &earliest_time);
579 : :
580 : : /* skip qos if we have no observation (yet) => decode frame */
581 [ # # ]: 0 : if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (earliest_time))) {
582 [ # # ]: 0 : GST_LOG_OBJECT (dec, "no observation yet, decode frame");
583 : 0 : return TRUE;
584 : : }
585 : :
586 : : /* qos is done on running time */
587 : 0 : qostime = gst_segment_to_running_time (&dec->segment, GST_FORMAT_TIME,
588 : : timestamp);
589 : :
590 : : /* see how our next timestamp relates to the latest qos timestamp */
591 [ # # ][ # # ]: 0 : GST_LOG_OBJECT (dec, "qostime %" GST_TIME_FORMAT ", earliest %"
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
592 : : GST_TIME_FORMAT, GST_TIME_ARGS (qostime), GST_TIME_ARGS (earliest_time));
593 : :
594 [ # # ][ # # ]: 0 : if (qostime != GST_CLOCK_TIME_NONE && qostime <= earliest_time) {
595 [ # # ]: 0 : GST_DEBUG_OBJECT (dec, "we are late, drop frame");
596 : 0 : return FALSE;
597 : : }
598 : :
599 [ # # ]: 0 : GST_LOG_OBJECT (dec, "decode frame");
600 : 0 : return TRUE;
601 : : }
602 : :
603 : : static GstFlowReturn
604 : 0 : gst_jasper_dec_chain (GstPad * pad, GstBuffer * buf)
605 : : {
606 : : GstJasperDec *dec;
607 : 0 : GstFlowReturn ret = GST_FLOW_OK;
608 : : GstClockTime ts;
609 : 0 : GstBuffer *outbuf = NULL;
610 : : guint8 *data;
611 : : guint size;
612 : : gboolean decode;
613 : :
614 : 0 : dec = GST_JASPER_DEC (GST_PAD_PARENT (pad));
615 : :
616 [ # # ]: 0 : if (dec->fmt < 0)
617 : 0 : goto not_negotiated;
618 : :
619 : 0 : ts = GST_BUFFER_TIMESTAMP (buf);
620 : :
621 [ # # ][ # # ]: 0 : GST_LOG_OBJECT (dec, "buffer with ts: %" GST_TIME_FORMAT, GST_TIME_ARGS (ts));
[ # # ][ # # ]
[ # # ]
622 : :
623 [ # # ]: 0 : if (GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DISCONT))
624 : 0 : dec->discont = TRUE;
625 : :
626 : 0 : decode = gst_jasper_dec_do_qos (dec, ts);
627 : :
628 : : /* FIXME: do clipping */
629 : :
630 [ # # ]: 0 : if (G_UNLIKELY (!decode)) {
631 : 0 : dec->discont = TRUE;
632 : 0 : goto done;
633 : : }
634 : :
635 : : /* strip possible prefix */
636 [ # # ]: 0 : if (dec->strip) {
637 : : GstBuffer *tmp;
638 : :
639 : 0 : tmp = gst_buffer_create_sub (buf, dec->strip,
640 : 0 : GST_BUFFER_SIZE (buf) - dec->strip);
641 : 0 : gst_buffer_copy_metadata (tmp, buf, GST_BUFFER_COPY_TIMESTAMPS);
642 : 0 : gst_buffer_unref (buf);
643 : 0 : buf = tmp;
644 : : }
645 : : /* preprend possible codec_data */
646 [ # # ]: 0 : if (dec->codec_data) {
647 : : GstBuffer *tmp;
648 : :
649 : 0 : tmp = gst_buffer_merge (dec->codec_data, buf);
650 : 0 : gst_buffer_copy_metadata (tmp, buf, GST_BUFFER_COPY_TIMESTAMPS);
651 : 0 : gst_buffer_unref (buf);
652 : 0 : buf = tmp;
653 : : }
654 : :
655 : : /* now really feed the data to decoder */
656 : 0 : data = GST_BUFFER_DATA (buf);
657 : 0 : size = GST_BUFFER_SIZE (buf);
658 : :
659 : 0 : ret = gst_jasper_dec_get_picture (dec, data, size, &outbuf);
660 : :
661 [ # # ]: 0 : if (outbuf) {
662 : 0 : gst_buffer_copy_metadata (outbuf, buf, GST_BUFFER_COPY_TIMESTAMPS);
663 [ # # ]: 0 : if (dec->discont) {
664 : 0 : GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
665 : 0 : dec->discont = FALSE;
666 : : }
667 : :
668 [ # # ]: 0 : if (ret == GST_FLOW_OK)
669 : 0 : ret = gst_pad_push (dec->srcpad, outbuf);
670 : : else
671 : 0 : gst_buffer_unref (outbuf);
672 : : }
673 : :
674 : : done:
675 : 0 : gst_buffer_unref (buf);
676 : :
677 : 0 : return ret;
678 : :
679 : : /* ERRORS */
680 : : not_negotiated:
681 : : {
682 [ # # ][ # # ]: 0 : GST_ELEMENT_ERROR (dec, CORE, NEGOTIATION, (NULL),
[ # # ][ # # ]
683 : : ("format wasn't negotiated before chain function"));
684 : 0 : ret = GST_FLOW_NOT_NEGOTIATED;
685 : 0 : goto done;
686 : : }
687 : : }
688 : :
689 : : static void
690 : 0 : gst_jasper_dec_update_qos (GstJasperDec * dec, gdouble proportion,
691 : : GstClockTime time)
692 : : {
693 : 0 : GST_OBJECT_LOCK (dec);
694 : 0 : dec->proportion = proportion;
695 : 0 : dec->earliest_time = time;
696 : 0 : GST_OBJECT_UNLOCK (dec);
697 : 0 : }
698 : :
699 : : static void
700 : 0 : gst_jasper_dec_reset_qos (GstJasperDec * dec)
701 : : {
702 : 0 : gst_jasper_dec_update_qos (dec, 0.5, GST_CLOCK_TIME_NONE);
703 : 0 : }
704 : :
705 : : static void
706 : 0 : gst_jasper_dec_read_qos (GstJasperDec * dec, gdouble * proportion,
707 : : GstClockTime * time)
708 : : {
709 : 0 : GST_OBJECT_LOCK (dec);
710 : 0 : *proportion = dec->proportion;
711 : 0 : *time = dec->earliest_time;
712 : 0 : GST_OBJECT_UNLOCK (dec);
713 : 0 : }
714 : :
715 : : static gboolean
716 : 0 : gst_jasper_dec_src_event (GstPad * pad, GstEvent * event)
717 : : {
718 : : GstJasperDec *dec;
719 : : gboolean res;
720 : :
721 : 0 : dec = GST_JASPER_DEC (gst_pad_get_parent (pad));
722 : :
723 [ # # ]: 0 : switch (GST_EVENT_TYPE (event)) {
724 : : case GST_EVENT_QOS:{
725 : : GstClockTimeDiff diff;
726 : : GstClockTime timestamp;
727 : : gdouble proportion;
728 : :
729 : 0 : gst_event_parse_qos (event, &proportion, &diff, ×tamp);
730 : :
731 : 0 : gst_jasper_dec_update_qos (dec, proportion, timestamp + diff);
732 : 0 : break;
733 : : }
734 : : default:
735 : 0 : break;
736 : : }
737 : :
738 : 0 : res = gst_pad_push_event (dec->sinkpad, event);
739 : :
740 : 0 : gst_object_unref (dec);
741 : 0 : return res;
742 : : }
743 : :
744 : : static gboolean
745 : 0 : gst_jasper_dec_sink_event (GstPad * pad, GstEvent * event)
746 : : {
747 : : GstJasperDec *dec;
748 : 0 : gboolean res = FALSE;
749 : :
750 : 0 : dec = GST_JASPER_DEC (gst_pad_get_parent (pad));
751 : :
752 [ # # # ]: 0 : switch (GST_EVENT_TYPE (event)) {
753 : : case GST_EVENT_FLUSH_STOP:
754 : 0 : gst_jasper_dec_reset_qos (dec);
755 : 0 : gst_segment_init (&dec->segment, GST_FORMAT_TIME);
756 : 0 : dec->discont = TRUE;
757 : 0 : break;
758 : : case GST_EVENT_NEWSEGMENT:{
759 : : gboolean update;
760 : : GstFormat fmt;
761 : : gint64 start, stop, time;
762 : : gdouble rate, arate;
763 : :
764 : 0 : gst_event_parse_new_segment_full (event, &update, &rate, &arate, &fmt,
765 : : &start, &stop, &time);
766 : :
767 [ # # # ]: 0 : switch (fmt) {
768 : : case GST_FORMAT_TIME:
769 : : /* great, our native segment format */
770 : 0 : break;
771 : : case GST_FORMAT_BYTES:
772 : : /* hmm .. */
773 [ # # ][ # # ]: 0 : if (start != 0 || time != 0)
774 : : goto invalid_bytes_segment;
775 : : /* create bogus segment in TIME format, starting from 0 */
776 : 0 : gst_event_unref (event);
777 : 0 : fmt = GST_FORMAT_TIME;
778 : 0 : start = 0;
779 : 0 : stop = -1;
780 : 0 : time = 0;
781 : 0 : event = gst_event_new_new_segment (update, rate, fmt, start, stop,
782 : : time);
783 : 0 : break;
784 : : default:
785 : : /* invalid format */
786 : 0 : goto invalid_format;
787 : : }
788 : :
789 : 0 : gst_segment_set_newsegment_full (&dec->segment, update, rate, arate,
790 : : fmt, start, stop, time);
791 : :
792 [ # # ]: 0 : GST_DEBUG_OBJECT (dec, "NEWSEGMENT %" GST_SEGMENT_FORMAT, &dec->segment);
793 : 0 : break;
794 : : }
795 : : default:
796 : 0 : break;
797 : : }
798 : :
799 : 0 : res = gst_pad_push_event (dec->srcpad, event);
800 : :
801 : : done:
802 : :
803 : 0 : gst_object_unref (dec);
804 : 0 : return res;
805 : :
806 : : /* ERRORS */
807 : : invalid_format:
808 : : {
809 [ # # ]: 0 : GST_WARNING_OBJECT (dec, "unknown format received in NEWSEGMENT event");
810 : 0 : gst_event_unref (event);
811 : 0 : goto done;
812 : : }
813 : : invalid_bytes_segment:
814 : : {
815 [ # # ]: 0 : GST_WARNING_OBJECT (dec, "can't handle NEWSEGMENT event in BYTES format "
816 : : "with a non-0 start or non-0 time value");
817 : 0 : gst_event_unref (event);
818 : 0 : goto done;
819 : : }
820 : : }
821 : :
822 : : static void
823 : 0 : gst_jasper_dec_set_property (GObject * object, guint prop_id,
824 : : const GValue * value, GParamSpec * pspec)
825 : : {
826 : : GstJasperDec *filter;
827 : :
828 : 0 : filter = GST_JASPER_DEC (object);
829 : :
830 : : switch (prop_id) {
831 : : default:
832 : 0 : G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
833 : 0 : break;
834 : : }
835 : 0 : }
836 : :
837 : : static void
838 : 0 : gst_jasper_dec_get_property (GObject * object, guint prop_id,
839 : : GValue * value, GParamSpec * pspec)
840 : : {
841 : : GstJasperDec *filter;
842 : :
843 : 0 : filter = GST_JASPER_DEC (object);
844 : :
845 : : switch (prop_id) {
846 : : default:
847 : 0 : G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
848 : 0 : break;
849 : : }
850 : 0 : }
851 : :
852 : : static GstStateChangeReturn
853 : 0 : gst_jasper_dec_change_state (GstElement * element, GstStateChange transition)
854 : : {
855 : 0 : GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
856 : 0 : GstJasperDec *dec = GST_JASPER_DEC (element);
857 : :
858 [ # # # ]: 0 : switch (transition) {
859 : : case GST_STATE_CHANGE_NULL_TO_READY:
860 [ # # ]: 0 : if (jas_init ())
861 : 0 : goto fail_init;
862 : 0 : break;
863 : : case GST_STATE_CHANGE_READY_TO_PAUSED:
864 : 0 : break;
865 : : default:
866 : 0 : break;
867 : : }
868 : :
869 : 0 : ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
870 [ # # ]: 0 : if (ret == GST_STATE_CHANGE_FAILURE)
871 : 0 : return ret;
872 : :
873 [ # # # ]: 0 : switch (transition) {
874 : : case GST_STATE_CHANGE_PAUSED_TO_READY:
875 : 0 : gst_jasper_dec_reset (dec);
876 : 0 : break;
877 : : case GST_STATE_CHANGE_READY_TO_NULL:
878 : 0 : jas_cleanup ();
879 : 0 : break;
880 : : default:
881 : 0 : break;
882 : : }
883 : :
884 : 0 : return ret;
885 : :
886 : : /* ERRORS */
887 : : fail_init:
888 : : {
889 [ # # ][ # # ]: 0 : GST_ELEMENT_ERROR (dec, LIBRARY, INIT, (NULL), (NULL));
[ # # ][ # # ]
890 : 0 : return GST_STATE_CHANGE_FAILURE;
891 : : }
892 : : }
|