1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  *               <2005> Wim Taymans <wim@fluendo.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20 
21 /**
22  * SECTION:element-dvdec
23  *
24  * dvdec decodes DV video into raw video. The element expects a full DV frame
25  * as input, which is 120000 bytes for NTSC and 144000 for PAL video.
26  *
27  * This element can perform simple frame dropping with the #GstDVDec:drop-factor
28  * property. Setting this property to a value N > 1 will only decode every
29  * Nth frame.
30  *
31  * <refsect2>
32  * <title>Example launch line</title>
33  * |[
34  * gst-launch-1.0 filesrc location=test.dv ! dvdemux name=demux ! dvdec ! xvimagesink
35  * ]| This pipeline decodes and renders the raw DV stream to a videosink.
36  * </refsect2>
37  */
38 
39 #ifdef HAVE_CONFIG_H
40 #include "config.h"
41 #endif
42 #include <string.h>
43 #include <math.h>
44 #include <gst/video/video.h>
45 #include <gst/video/gstvideometa.h>
46 #include <gst/video/gstvideopool.h>
47 
48 #include "gstdvdec.h"
49 
50 /* sizes of one input buffer */
51 #define NTSC_HEIGHT 480
52 #define NTSC_BUFFER 120000
53 #define NTSC_FRAMERATE_NUMERATOR 30000
54 #define NTSC_FRAMERATE_DENOMINATOR 1001
55 
56 #define PAL_HEIGHT 576
57 #define PAL_BUFFER 144000
58 #define PAL_FRAMERATE_NUMERATOR 25
59 #define PAL_FRAMERATE_DENOMINATOR 1
60 
61 #define PAL_NORMAL_PAR_X        16
62 #define PAL_NORMAL_PAR_Y        15
63 #define PAL_WIDE_PAR_X          64
64 #define PAL_WIDE_PAR_Y          45
65 
66 #define NTSC_NORMAL_PAR_X       8
67 #define NTSC_NORMAL_PAR_Y       9
68 #define NTSC_WIDE_PAR_X         32
69 #define NTSC_WIDE_PAR_Y         27
70 
71 #define DV_DEFAULT_QUALITY DV_QUALITY_BEST
72 #define DV_DEFAULT_DECODE_NTH 1
73 
74 GST_DEBUG_CATEGORY_STATIC (dvdec_debug);
75 #define GST_CAT_DEFAULT dvdec_debug
76 
77 enum
78 {
79   PROP_0,
80   PROP_CLAMP_LUMA,
81   PROP_CLAMP_CHROMA,
82   PROP_QUALITY,
83   PROP_DECODE_NTH
84 };
85 
86 const gint qualities[] = {
87   DV_QUALITY_DC,
88   DV_QUALITY_AC_1,
89   DV_QUALITY_AC_2,
90   DV_QUALITY_DC | DV_QUALITY_COLOR,
91   DV_QUALITY_AC_1 | DV_QUALITY_COLOR,
92   DV_QUALITY_AC_2 | DV_QUALITY_COLOR
93 };
94 
95 static GstStaticPadTemplate sink_temp = GST_STATIC_PAD_TEMPLATE ("sink",
96     GST_PAD_SINK,
97     GST_PAD_ALWAYS,
98     GST_STATIC_CAPS ("video/x-dv, systemstream = (boolean) false")
99     );
100 
101 static GstStaticPadTemplate src_temp = GST_STATIC_PAD_TEMPLATE ("src",
102     GST_PAD_SRC,
103     GST_PAD_ALWAYS,
104     GST_STATIC_CAPS ("video/x-raw, "
105         "format = (string) { YUY2, BGRx, RGB }, "
106         "framerate = (fraction) [ 1/1, 60/1 ], "
107         "width = (int) 720, " "height = (int) { 576, 480 }")
108     );
109 
110 #define GST_TYPE_DVDEC_QUALITY (gst_dvdec_quality_get_type())
111 static GType
gst_dvdec_quality_get_type(void)112 gst_dvdec_quality_get_type (void)
113 {
114   static GType qtype = 0;
115 
116   if (qtype == 0) {
117     static const GEnumValue values[] = {
118       {0, "Monochrome, DC (Fastest)", "fastest"},
119       {1, "Monochrome, first AC coefficient", "monochrome-ac"},
120       {2, "Monochrome, highest quality", "monochrome-best"},
121       {3, "Colour, DC, fastest", "colour-fastest"},
122       {4, "Colour, using only the first AC coefficient", "colour-ac"},
123       {5, "Highest quality colour decoding", "best"},
124       {0, NULL, NULL},
125     };
126 
127     qtype = g_enum_register_static ("GstDVDecQualityEnum", values);
128   }
129   return qtype;
130 }
131 
132 #define gst_dvdec_parent_class parent_class
133 G_DEFINE_TYPE (GstDVDec, gst_dvdec, GST_TYPE_ELEMENT);
134 
135 static GstFlowReturn gst_dvdec_chain (GstPad * pad, GstObject * parent,
136     GstBuffer * buffer);
137 static gboolean gst_dvdec_sink_event (GstPad * pad, GstObject * parent,
138     GstEvent * event);
139 
140 static GstStateChangeReturn gst_dvdec_change_state (GstElement * element,
141     GstStateChange transition);
142 
143 static void gst_dvdec_set_property (GObject * object, guint prop_id,
144     const GValue * value, GParamSpec * pspec);
145 static void gst_dvdec_get_property (GObject * object, guint prop_id,
146     GValue * value, GParamSpec * pspec);
147 
148 static void
gst_dvdec_class_init(GstDVDecClass * klass)149 gst_dvdec_class_init (GstDVDecClass * klass)
150 {
151   GObjectClass *gobject_class;
152   GstElementClass *gstelement_class;
153 
154   gobject_class = (GObjectClass *) klass;
155   gstelement_class = (GstElementClass *) klass;
156 
157   gobject_class->set_property = gst_dvdec_set_property;
158   gobject_class->get_property = gst_dvdec_get_property;
159 
160   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_CLAMP_LUMA,
161       g_param_spec_boolean ("clamp-luma", "Clamp luma", "Clamp luma",
162           FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
163   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_CLAMP_CHROMA,
164       g_param_spec_boolean ("clamp-chroma", "Clamp chroma", "Clamp chroma",
165           FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
166   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_QUALITY,
167       g_param_spec_enum ("quality", "Quality", "Decoding quality",
168           GST_TYPE_DVDEC_QUALITY, DV_DEFAULT_QUALITY,
169           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
170   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_DECODE_NTH,
171       g_param_spec_int ("drop-factor", "Drop Factor", "Only decode Nth frame",
172           1, G_MAXINT, DV_DEFAULT_DECODE_NTH,
173           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
174 
175   gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_dvdec_change_state);
176 
177   gst_element_class_add_static_pad_template (gstelement_class, &sink_temp);
178   gst_element_class_add_static_pad_template (gstelement_class, &src_temp);
179 
180   gst_element_class_set_static_metadata (gstelement_class, "DV video decoder",
181       "Codec/Decoder/Video",
182       "Uses libdv to decode DV video (smpte314) (libdv.sourceforge.net)",
183       "Erik Walthinsen <omega@cse.ogi.edu>," "Wim Taymans <wim@fluendo.com>");
184 
185   GST_DEBUG_CATEGORY_INIT (dvdec_debug, "dvdec", 0, "DV decoding element");
186 }
187 
188 static void
gst_dvdec_init(GstDVDec * dvdec)189 gst_dvdec_init (GstDVDec * dvdec)
190 {
191   dvdec->sinkpad = gst_pad_new_from_static_template (&sink_temp, "sink");
192   gst_pad_set_chain_function (dvdec->sinkpad, gst_dvdec_chain);
193   gst_pad_set_event_function (dvdec->sinkpad, gst_dvdec_sink_event);
194   gst_element_add_pad (GST_ELEMENT (dvdec), dvdec->sinkpad);
195 
196   dvdec->srcpad = gst_pad_new_from_static_template (&src_temp, "src");
197   gst_pad_use_fixed_caps (dvdec->srcpad);
198   gst_element_add_pad (GST_ELEMENT (dvdec), dvdec->srcpad);
199 
200   dvdec->framerate_numerator = 0;
201   dvdec->framerate_denominator = 0;
202   dvdec->wide = FALSE;
203   dvdec->drop_factor = 1;
204 
205   dvdec->clamp_luma = FALSE;
206   dvdec->clamp_chroma = FALSE;
207   dvdec->quality = DV_DEFAULT_QUALITY;
208 }
209 
210 static gboolean
gst_dvdec_sink_setcaps(GstDVDec * dvdec,GstCaps * caps)211 gst_dvdec_sink_setcaps (GstDVDec * dvdec, GstCaps * caps)
212 {
213   GstStructure *s;
214   const GValue *par = NULL, *rate = NULL;
215 
216   /* first parse the caps */
217   s = gst_caps_get_structure (caps, 0);
218 
219   /* we allow framerate and PAR to be overwritten. framerate is mandatory. */
220   if (!(rate = gst_structure_get_value (s, "framerate")))
221     goto no_framerate;
222   par = gst_structure_get_value (s, "pixel-aspect-ratio");
223 
224   if (par) {
225     dvdec->par_x = gst_value_get_fraction_numerator (par);
226     dvdec->par_y = gst_value_get_fraction_denominator (par);
227     dvdec->need_par = FALSE;
228   } else {
229     dvdec->par_x = 0;
230     dvdec->par_y = 0;
231     dvdec->need_par = TRUE;
232   }
233   dvdec->framerate_numerator = gst_value_get_fraction_numerator (rate);
234   dvdec->framerate_denominator = gst_value_get_fraction_denominator (rate);
235   dvdec->sink_negotiated = TRUE;
236   dvdec->src_negotiated = FALSE;
237 
238   return TRUE;
239 
240   /* ERRORS */
241 no_framerate:
242   {
243     GST_DEBUG_OBJECT (dvdec, "no framerate specified in caps");
244     return FALSE;
245   }
246 }
247 
248 static void
gst_dvdec_negotiate_pool(GstDVDec * dec,GstCaps * caps,GstVideoInfo * info)249 gst_dvdec_negotiate_pool (GstDVDec * dec, GstCaps * caps, GstVideoInfo * info)
250 {
251   GstQuery *query;
252   GstBufferPool *pool;
253   guint size, min, max;
254   GstStructure *config;
255 
256   /* find a pool for the negotiated caps now */
257   query = gst_query_new_allocation (caps, TRUE);
258 
259   if (!gst_pad_peer_query (dec->srcpad, query)) {
260     GST_DEBUG_OBJECT (dec, "didn't get downstream ALLOCATION hints");
261   }
262 
263   if (gst_query_get_n_allocation_pools (query) > 0) {
264     /* we got configuration from our peer, parse them */
265     gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
266     size = MAX (size, info->size);
267   } else {
268     pool = NULL;
269     size = info->size;
270     min = max = 0;
271   }
272 
273   if (pool == NULL) {
274     /* we did not get a pool, make one ourselves then */
275     pool = gst_video_buffer_pool_new ();
276   }
277 
278   if (dec->pool) {
279     gst_buffer_pool_set_active (dec->pool, FALSE);
280     gst_object_unref (dec->pool);
281   }
282   dec->pool = pool;
283 
284   config = gst_buffer_pool_get_config (pool);
285   gst_buffer_pool_config_set_params (config, caps, size, min, max);
286 
287   if (gst_query_find_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL)) {
288     /* just set the option, if the pool can support it we will transparently use
289      * it through the video info API. We could also see if the pool support this
290      * option and only activate it then. */
291     gst_buffer_pool_config_add_option (config,
292         GST_BUFFER_POOL_OPTION_VIDEO_META);
293   }
294   gst_buffer_pool_set_config (pool, config);
295 
296   /* and activate */
297   gst_buffer_pool_set_active (pool, TRUE);
298 
299   gst_query_unref (query);
300 }
301 
302 static gboolean
gst_dvdec_src_negotiate(GstDVDec * dvdec)303 gst_dvdec_src_negotiate (GstDVDec * dvdec)
304 {
305   GstCaps *othercaps;
306   gboolean ret;
307 
308   /* no PAR was specified in input, derive from encoded data */
309   if (dvdec->need_par) {
310     if (dvdec->PAL) {
311       if (dvdec->wide) {
312         dvdec->par_x = PAL_WIDE_PAR_X;
313         dvdec->par_y = PAL_WIDE_PAR_Y;
314       } else {
315         dvdec->par_x = PAL_NORMAL_PAR_X;
316         dvdec->par_y = PAL_NORMAL_PAR_Y;
317       }
318     } else {
319       if (dvdec->wide) {
320         dvdec->par_x = NTSC_WIDE_PAR_X;
321         dvdec->par_y = NTSC_WIDE_PAR_Y;
322       } else {
323         dvdec->par_x = NTSC_NORMAL_PAR_X;
324         dvdec->par_y = NTSC_NORMAL_PAR_Y;
325       }
326     }
327     GST_DEBUG_OBJECT (dvdec, "Inferred PAR %d/%d from video format",
328         dvdec->par_x, dvdec->par_y);
329   }
330 
331   /* ignoring rgb, bgr0 for now */
332   dvdec->bpp = 2;
333 
334   gst_video_info_set_format (&dvdec->vinfo, GST_VIDEO_FORMAT_YUY2,
335       720, dvdec->height);
336   dvdec->vinfo.fps_n = dvdec->framerate_numerator;
337   dvdec->vinfo.fps_d = dvdec->framerate_denominator;
338   dvdec->vinfo.par_n = dvdec->par_x;
339   dvdec->vinfo.par_d = dvdec->par_y;
340   if (dvdec->interlaced) {
341     dvdec->vinfo.interlace_mode = GST_VIDEO_INTERLACE_MODE_INTERLEAVED;
342   } else {
343     dvdec->vinfo.interlace_mode = GST_VIDEO_INTERLACE_MODE_PROGRESSIVE;
344   }
345 
346   othercaps = gst_video_info_to_caps (&dvdec->vinfo);
347   ret = gst_pad_set_caps (dvdec->srcpad, othercaps);
348 
349   gst_dvdec_negotiate_pool (dvdec, othercaps, &dvdec->vinfo);
350   gst_caps_unref (othercaps);
351 
352   dvdec->src_negotiated = TRUE;
353 
354   return ret;
355 }
356 
357 static gboolean
gst_dvdec_sink_event(GstPad * pad,GstObject * parent,GstEvent * event)358 gst_dvdec_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
359 {
360   GstDVDec *dvdec;
361   gboolean res = TRUE;
362 
363   dvdec = GST_DVDEC (parent);
364 
365   switch (GST_EVENT_TYPE (event)) {
366     case GST_EVENT_FLUSH_STOP:
367       gst_segment_init (&dvdec->segment, GST_FORMAT_UNDEFINED);
368       dvdec->need_segment = FALSE;
369       break;
370     case GST_EVENT_SEGMENT:{
371       const GstSegment *segment;
372 
373       gst_event_parse_segment (event, &segment);
374 
375       GST_DEBUG_OBJECT (dvdec, "Got SEGMENT %" GST_SEGMENT_FORMAT, &segment);
376 
377       gst_segment_copy_into (segment, &dvdec->segment);
378       if (!gst_pad_has_current_caps (dvdec->srcpad)) {
379         dvdec->need_segment = TRUE;
380         gst_event_unref (event);
381         event = NULL;
382         res = TRUE;
383       } else {
384         dvdec->need_segment = FALSE;
385       }
386       break;
387     }
388     case GST_EVENT_CAPS:
389     {
390       GstCaps *caps;
391 
392       gst_event_parse_caps (event, &caps);
393       res = gst_dvdec_sink_setcaps (dvdec, caps);
394       gst_event_unref (event);
395       event = NULL;
396       break;
397     }
398 
399     default:
400       break;
401   }
402 
403   if (event)
404     res = gst_pad_push_event (dvdec->srcpad, event);
405 
406   return res;
407 }
408 
409 static GstFlowReturn
gst_dvdec_chain(GstPad * pad,GstObject * parent,GstBuffer * buf)410 gst_dvdec_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
411 {
412   GstDVDec *dvdec;
413   guint8 *inframe;
414   guint8 *outframe_ptrs[3];
415   gint outframe_pitches[3];
416   GstMapInfo map;
417   GstVideoFrame frame;
418   GstBuffer *outbuf;
419   GstFlowReturn ret = GST_FLOW_OK;
420   guint length;
421   guint64 cstart = GST_CLOCK_TIME_NONE, cstop = GST_CLOCK_TIME_NONE;
422   gboolean PAL, wide;
423 
424   dvdec = GST_DVDEC (parent);
425 
426   gst_buffer_map (buf, &map, GST_MAP_READ);
427   inframe = map.data;
428 
429   /* buffer should be at least the size of one NTSC frame, this should
430    * be enough to decode the header. */
431   if (G_UNLIKELY (map.size < NTSC_BUFFER))
432     goto wrong_size;
433 
434   /* preliminary dropping. unref and return if outside of configured segment */
435   if ((dvdec->segment.format == GST_FORMAT_TIME) &&
436       (!(gst_segment_clip (&dvdec->segment, GST_FORMAT_TIME,
437                   GST_BUFFER_TIMESTAMP (buf),
438                   GST_BUFFER_TIMESTAMP (buf) + GST_BUFFER_DURATION (buf),
439                   &cstart, &cstop))))
440     goto dropping;
441 
442   if (G_UNLIKELY (dv_parse_header (dvdec->decoder, inframe) < 0))
443     goto parse_header_error;
444 
445   /* get size */
446   PAL = dv_system_50_fields (dvdec->decoder);
447   wide = dv_format_wide (dvdec->decoder);
448 
449   /* check the buffer is of right size after we know if we are
450    * dealing with PAL or NTSC */
451   length = (PAL ? PAL_BUFFER : NTSC_BUFFER);
452   if (G_UNLIKELY (map.size < length))
453     goto wrong_size;
454 
455   dv_parse_packs (dvdec->decoder, inframe);
456 
457   if (dvdec->video_offset % dvdec->drop_factor != 0)
458     goto skip;
459 
460   /* renegotiate on change */
461   if (PAL != dvdec->PAL || wide != dvdec->wide) {
462     dvdec->src_negotiated = FALSE;
463     dvdec->PAL = PAL;
464     dvdec->wide = wide;
465   }
466 
467   dvdec->height = (dvdec->PAL ? PAL_HEIGHT : NTSC_HEIGHT);
468 
469   dvdec->interlaced = !dv_is_progressive (dvdec->decoder);
470 
471   /* negotiate if not done yet */
472   if (!dvdec->src_negotiated) {
473     if (!gst_dvdec_src_negotiate (dvdec))
474       goto not_negotiated;
475   }
476 
477   if (gst_pad_check_reconfigure (dvdec->srcpad)) {
478     GstCaps *caps;
479 
480     caps = gst_pad_get_current_caps (dvdec->srcpad);
481     if (!caps)
482       goto flushing;
483 
484     gst_dvdec_negotiate_pool (dvdec, caps, &dvdec->vinfo);
485     gst_caps_unref (caps);
486   }
487 
488   if (dvdec->need_segment) {
489     gst_pad_push_event (dvdec->srcpad, gst_event_new_segment (&dvdec->segment));
490     dvdec->need_segment = FALSE;
491   }
492 
493   ret = gst_buffer_pool_acquire_buffer (dvdec->pool, &outbuf, NULL);
494   if (G_UNLIKELY (ret != GST_FLOW_OK))
495     goto no_buffer;
496 
497   gst_video_frame_map (&frame, &dvdec->vinfo, outbuf, GST_MAP_WRITE);
498 
499   outframe_ptrs[0] = GST_VIDEO_FRAME_COMP_DATA (&frame, 0);
500   outframe_pitches[0] = GST_VIDEO_FRAME_COMP_STRIDE (&frame, 0);
501 
502   /* the rest only matters for YUY2 */
503   if (dvdec->bpp < 3) {
504     outframe_ptrs[1] = GST_VIDEO_FRAME_COMP_DATA (&frame, 1);
505     outframe_ptrs[2] = GST_VIDEO_FRAME_COMP_DATA (&frame, 2);
506 
507     outframe_pitches[1] = GST_VIDEO_FRAME_COMP_STRIDE (&frame, 1);
508     outframe_pitches[2] = GST_VIDEO_FRAME_COMP_STRIDE (&frame, 2);
509   }
510 
511   GST_DEBUG_OBJECT (dvdec, "decoding and pushing buffer");
512   dv_decode_full_frame (dvdec->decoder, inframe,
513       e_dv_color_yuv, outframe_ptrs, outframe_pitches);
514 
515   gst_video_frame_unmap (&frame);
516 
517   GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
518 
519   GST_BUFFER_OFFSET (outbuf) = GST_BUFFER_OFFSET (buf);
520   GST_BUFFER_OFFSET_END (outbuf) = GST_BUFFER_OFFSET_END (buf);
521 
522   /* FIXME : Compute values when using non-TIME segments,
523    * but for the moment make sure we at least don't set bogus values
524    */
525   if (GST_CLOCK_TIME_IS_VALID (cstart)) {
526     GST_BUFFER_TIMESTAMP (outbuf) = cstart;
527     if (GST_CLOCK_TIME_IS_VALID (cstop))
528       GST_BUFFER_DURATION (outbuf) = cstop - cstart;
529   }
530 
531   ret = gst_pad_push (dvdec->srcpad, outbuf);
532 
533 skip:
534   dvdec->video_offset++;
535 
536 done:
537   gst_buffer_unmap (buf, &map);
538   gst_buffer_unref (buf);
539 
540   return ret;
541 
542   /* ERRORS */
543 wrong_size:
544   {
545     GST_ELEMENT_ERROR (dvdec, STREAM, DECODE,
546         (NULL), ("Input buffer too small"));
547     ret = GST_FLOW_ERROR;
548     goto done;
549   }
550 parse_header_error:
551   {
552     GST_ELEMENT_ERROR (dvdec, STREAM, DECODE,
553         (NULL), ("Error parsing DV header"));
554     ret = GST_FLOW_ERROR;
555     goto done;
556   }
557 not_negotiated:
558   {
559     GST_DEBUG_OBJECT (dvdec, "could not negotiate output");
560     if (GST_PAD_IS_FLUSHING (dvdec->srcpad))
561       ret = GST_FLOW_FLUSHING;
562     else
563       ret = GST_FLOW_NOT_NEGOTIATED;
564     goto done;
565   }
566 flushing:
567   {
568     GST_DEBUG_OBJECT (dvdec, "have no current caps");
569     ret = GST_FLOW_FLUSHING;
570     goto done;
571   }
572 no_buffer:
573   {
574     GST_DEBUG_OBJECT (dvdec, "could not allocate buffer");
575     goto done;
576   }
577 
578 dropping:
579   {
580     GST_DEBUG_OBJECT (dvdec,
581         "dropping buffer since it's out of the configured segment");
582     goto done;
583   }
584 }
585 
586 static GstStateChangeReturn
gst_dvdec_change_state(GstElement * element,GstStateChange transition)587 gst_dvdec_change_state (GstElement * element, GstStateChange transition)
588 {
589   GstDVDec *dvdec = GST_DVDEC (element);
590   GstStateChangeReturn ret;
591 
592 
593   switch (transition) {
594     case GST_STATE_CHANGE_NULL_TO_READY:
595       break;
596     case GST_STATE_CHANGE_READY_TO_PAUSED:
597       dvdec->decoder =
598           dv_decoder_new (0, dvdec->clamp_luma, dvdec->clamp_chroma);
599       dvdec->decoder->quality = qualities[dvdec->quality];
600       dv_set_error_log (dvdec->decoder, NULL);
601       gst_video_info_init (&dvdec->vinfo);
602       gst_segment_init (&dvdec->segment, GST_FORMAT_UNDEFINED);
603       dvdec->src_negotiated = FALSE;
604       dvdec->sink_negotiated = FALSE;
605       dvdec->need_segment = FALSE;
606       /*
607        * Enable this function call when libdv2 0.100 or higher is more
608        * common
609        */
610       /* dv_set_quality (dvdec->decoder, qualities [dvdec->quality]); */
611       break;
612     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
613       break;
614     default:
615       break;
616   }
617 
618   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
619 
620   switch (transition) {
621     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
622       break;
623     case GST_STATE_CHANGE_PAUSED_TO_READY:
624       dv_decoder_free (dvdec->decoder);
625       dvdec->decoder = NULL;
626       if (dvdec->pool) {
627         gst_buffer_pool_set_active (dvdec->pool, FALSE);
628         gst_object_unref (dvdec->pool);
629         dvdec->pool = NULL;
630       }
631       break;
632     case GST_STATE_CHANGE_READY_TO_NULL:
633       break;
634     default:
635       break;
636   }
637   return ret;
638 }
639 
640 static void
gst_dvdec_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)641 gst_dvdec_set_property (GObject * object, guint prop_id, const GValue * value,
642     GParamSpec * pspec)
643 {
644   GstDVDec *dvdec = GST_DVDEC (object);
645 
646   switch (prop_id) {
647     case PROP_CLAMP_LUMA:
648       dvdec->clamp_luma = g_value_get_boolean (value);
649       break;
650     case PROP_CLAMP_CHROMA:
651       dvdec->clamp_chroma = g_value_get_boolean (value);
652       break;
653     case PROP_QUALITY:
654       dvdec->quality = g_value_get_enum (value);
655       if ((dvdec->quality < 0) || (dvdec->quality > 5))
656         dvdec->quality = 0;
657       break;
658     case PROP_DECODE_NTH:
659       dvdec->drop_factor = g_value_get_int (value);
660       break;
661     default:
662       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
663       break;
664   }
665 }
666 
667 static void
gst_dvdec_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)668 gst_dvdec_get_property (GObject * object, guint prop_id, GValue * value,
669     GParamSpec * pspec)
670 {
671   GstDVDec *dvdec = GST_DVDEC (object);
672 
673   switch (prop_id) {
674     case PROP_CLAMP_LUMA:
675       g_value_set_boolean (value, dvdec->clamp_luma);
676       break;
677     case PROP_CLAMP_CHROMA:
678       g_value_set_boolean (value, dvdec->clamp_chroma);
679       break;
680     case PROP_QUALITY:
681       g_value_set_enum (value, dvdec->quality);
682       break;
683     case PROP_DECODE_NTH:
684       g_value_set_int (value, dvdec->drop_factor);
685       break;
686     default:
687       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
688       break;
689   }
690 }
691