Imported Upstream version 0.10.23
[profile/ivi/gst-plugins-bad.git] / ext / mpeg2enc / gstmpeg2enc.cc
1 /* GStreamer mpeg2enc (mjpegtools) wrapper
2  * (c) 2003 Ronald Bultje <rbultje@ronald.bitfreak.net>
3  * (c) 2006 Mark Nauwelaerts <manauw@skynet.be>
4  *
5  * gstmpeg2enc.cc: gstreamer mpeg2enc wrapping
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 /**
24  * SECTION:element-mpeg2enc
25  * @see_also: mpeg2dec
26  *
27  * This element encodes raw video into an MPEG-1/2 elementary stream using the
28  * <ulink url="http://mjpeg.sourceforge.net/">mjpegtools</ulink> library.
29  * Documentation on MPEG encoding in general can be found in the 
30  * <ulink url="https://sourceforge.net/docman/display_doc.php?docid=3456&group_id=5776">MJPEG Howto</ulink>
31  * and on the various available parameters in the documentation
32  * of the mpeg2enc tool in particular, which shares options with this element.
33  *
34  * <refsect2>
35  * <title>Example pipeline</title>
36  * |[
37  * gst-launch-0.10 videotestsrc num-buffers=1000 ! mpeg2enc ! filesink location=videotestsrc.m1v
38  * ]| This example pipeline will encode a test video source to a an MPEG1
39  * elementary stream (with Generic MPEG1 profile).
40  * <para>
41  * Likely, the #GstMpeg2enc:format property
42  * is most important, as it selects the type of MPEG stream that is produced.
43  * In particular, default property values are dependent on the format,
44  * and can even be forcibly restrained to certain pre-sets (and thereby ignored).
45  * Note that the (S)VCD profiles also restrict the image size, so some scaling
46  * may be needed to accomodate this.  The so-called generic profiles (as used
47  * in the example above) allow most parameters to be adjusted.
48  * </para>
49  * |[
50  * gst-launch-0.10 videotestsrc num-buffers=1000 ! videoscale ! mpeg2enc format=1 norm=p ! filesink location=videotestsrc.m1v
51  * ]| This will produce an MPEG1 profile stream according to VCD2.0 specifications
52  * for PAL #GstMpeg2enc:norm (as the image height is dependent on video norm).
53  * </refsect2>
54  */
55
56 #ifdef HAVE_CONFIG_H
57 #include "config.h"
58 #endif
59
60 #include "gstmpeg2enc.hh"
61
62 GST_DEBUG_CATEGORY (mpeg2enc_debug);
63
64 #define COMMON_VIDEO_CAPS \
65   "width = (int) [ 16, 4096 ], " \
66   "height = (int) [ 16, 4096 ], " \
67   "framerate = " \
68   " (fraction) { 24000/1001, 24/1, 25/1, 30000/1001, 30/1, 50/1, 60000/1001 }"
69
70 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
71     GST_PAD_SINK,
72     GST_PAD_ALWAYS,
73     GST_STATIC_CAPS ("video/x-raw-yuv, "
74         "format = (fourcc) { I420 }, " COMMON_VIDEO_CAPS)
75     );
76
77 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
78     GST_PAD_SRC,
79     GST_PAD_ALWAYS,
80     GST_STATIC_CAPS ("video/mpeg, "
81         "systemstream = (boolean) false, "
82         "mpegversion = (int) { 1, 2 }, " COMMON_VIDEO_CAPS)
83     );
84
85
86 static void gst_mpeg2enc_finalize (GObject * object);
87 static void gst_mpeg2enc_reset (GstMpeg2enc * enc);
88 static gboolean gst_mpeg2enc_setcaps (GstPad * pad, GstCaps * caps);
89 static GstCaps *gst_mpeg2enc_getcaps (GstPad * pad);
90 static gboolean gst_mpeg2enc_sink_event (GstPad * pad, GstEvent * event);
91 static void gst_mpeg2enc_loop (GstMpeg2enc * enc);
92 static GstFlowReturn gst_mpeg2enc_chain (GstPad * pad, GstBuffer * buffer);
93 static gboolean gst_mpeg2enc_src_activate_push (GstPad * pad, gboolean active);
94 static GstStateChangeReturn gst_mpeg2enc_change_state (GstElement * element,
95     GstStateChange transition);
96
97 static void gst_mpeg2enc_get_property (GObject * object,
98     guint prop_id, GValue * value, GParamSpec * pspec);
99 static void gst_mpeg2enc_set_property (GObject * object,
100     guint prop_id, const GValue * value, GParamSpec * pspec);
101
102 static void
103 _do_init (GType object_type)
104 {
105   const GInterfaceInfo preset_interface_info = {
106     NULL,                       /* interface_init */
107     NULL,                       /* interface_finalize */
108     NULL                        /* interface_data */
109   };
110
111   g_type_add_interface_static (object_type, GST_TYPE_PRESET,
112       &preset_interface_info);
113 }
114
115 GST_BOILERPLATE_FULL (GstMpeg2enc, gst_mpeg2enc, GstElement, GST_TYPE_ELEMENT,
116     _do_init);
117
118 static void
119 gst_mpeg2enc_base_init (gpointer klass)
120 {
121   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
122
123   gst_element_class_set_details_simple (element_class,
124       "mpeg2enc video encoder", "Codec/Encoder/Video",
125       "High-quality MPEG-1/2 video encoder",
126       "Andrew Stevens <andrew.stevens@nexgo.de>\n"
127       "Ronald Bultje <rbultje@ronald.bitfreak.net>");
128
129   gst_element_class_add_static_pad_template (element_class, &src_template);
130   gst_element_class_add_static_pad_template (element_class,
131       &sink_template);
132 }
133
134 static void
135 gst_mpeg2enc_class_init (GstMpeg2encClass * klass)
136 {
137   GObjectClass *object_class = G_OBJECT_CLASS (klass);
138   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
139
140   GST_DEBUG_CATEGORY_INIT (mpeg2enc_debug, "mpeg2enc", 0, "MPEG1/2 encoder");
141
142   object_class->set_property = gst_mpeg2enc_set_property;
143   object_class->get_property = gst_mpeg2enc_get_property;
144
145   /* register properties */
146   GstMpeg2EncOptions::initProperties (object_class);
147
148   object_class->finalize = GST_DEBUG_FUNCPTR (gst_mpeg2enc_finalize);
149
150   element_class->change_state = GST_DEBUG_FUNCPTR (gst_mpeg2enc_change_state);
151 }
152
153 static void
154 gst_mpeg2enc_finalize (GObject * object)
155 {
156   GstMpeg2enc *enc = GST_MPEG2ENC (object);
157
158   if (enc->encoder) {
159     delete enc->encoder;
160
161     enc->encoder = NULL;
162   }
163   delete enc->options;
164
165   g_mutex_free (enc->tlock);
166   g_cond_free (enc->cond);
167   g_queue_free (enc->time);
168
169   G_OBJECT_CLASS (parent_class)->finalize (object);
170 }
171
172 static void
173 gst_mpeg2enc_init (GstMpeg2enc * enc, GstMpeg2encClass * g_class)
174 {
175   GstElement *element = GST_ELEMENT (enc);
176   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
177
178   enc->sinkpad =
179       gst_pad_new_from_template (gst_element_class_get_pad_template
180       (element_class, "sink"), "sink");
181   gst_pad_set_setcaps_function (enc->sinkpad,
182       GST_DEBUG_FUNCPTR (gst_mpeg2enc_setcaps));
183   gst_pad_set_getcaps_function (enc->sinkpad,
184       GST_DEBUG_FUNCPTR (gst_mpeg2enc_getcaps));
185   gst_pad_set_event_function (enc->sinkpad,
186       GST_DEBUG_FUNCPTR (gst_mpeg2enc_sink_event));
187   gst_pad_set_chain_function (enc->sinkpad,
188       GST_DEBUG_FUNCPTR (gst_mpeg2enc_chain));
189   gst_element_add_pad (element, enc->sinkpad);
190
191   enc->srcpad =
192       gst_pad_new_from_template (gst_element_class_get_pad_template
193       (element_class, "src"), "src");
194   gst_pad_use_fixed_caps (enc->srcpad);
195   gst_pad_set_activatepush_function (enc->srcpad,
196       GST_DEBUG_FUNCPTR (gst_mpeg2enc_src_activate_push));
197   gst_element_add_pad (element, enc->srcpad);
198
199   enc->options = new GstMpeg2EncOptions ();
200   enc->encoder = NULL;
201
202   enc->buffer = NULL;
203   enc->tlock = g_mutex_new ();
204   enc->cond = g_cond_new ();
205   enc->time = g_queue_new ();
206
207   gst_mpeg2enc_reset (enc);
208 }
209
210 static void
211 gst_mpeg2enc_reset (GstMpeg2enc * enc)
212 {
213   GstBuffer *buf;
214
215   enc->eos = FALSE;
216   enc->srcresult = GST_FLOW_OK;
217
218   /* in case of error'ed ending */
219   if (enc->buffer)
220     gst_buffer_unref (enc->buffer);
221   enc->buffer = NULL;
222   while ((buf = (GstBuffer *) g_queue_pop_head (enc->time)))
223     gst_buffer_unref (buf);
224
225   if (enc->encoder) {
226     delete enc->encoder;
227
228     enc->encoder = NULL;
229   }
230 }
231
232 /* some (!) coding to get caps depending on the video norm and chosen format */
233 static void
234 gst_mpeg2enc_add_fps (GstStructure * structure, gint fpss[])
235 {
236   GValue list = { 0, }, fps = {
237   0,};
238   guint n;
239
240   g_value_init (&list, GST_TYPE_LIST);
241   g_value_init (&fps, GST_TYPE_FRACTION);
242   for (n = 0; fpss[n] != 0; n++) {
243     gst_value_set_fraction (&fps, fpss[n], fpss[n + 1]);
244     gst_value_list_append_value (&list, &fps);
245     n++;
246   }
247   gst_structure_set_value (structure, "framerate", &list);
248   g_value_unset (&list);
249   g_value_unset (&fps);
250 }
251
252 static inline gint *
253 gst_mpeg2enc_get_fps (GstMpeg2enc * enc)
254 {
255   static gint fps_pal[]
256   = { 24, 1, 25, 1, 50, 1, 0 };
257   static gint fps_ntsc[]
258   = { 24000, 1001, 24, 1, 30000, 1001, 30, 1, 60000, 1001, 0 };
259   static gint fps_all[]
260   = { 24000, 1001, 24, 1, 30000, 1001, 30, 1, 60000, 1001, 25, 1, 50, 1, 0 };
261
262   if (enc->options->norm == 'n')
263     return fps_ntsc;
264   else if (enc->options->norm == 0)
265     return fps_all;
266   else
267     return fps_pal;
268 }
269
270 static GstStructure *
271 gst_mpeg2enc_structure_from_norm (GstMpeg2enc * enc, gint horiz,
272     gint pal_v, gint ntsc_v)
273 {
274   GstStructure *structure;
275
276   structure = gst_structure_new ("video/x-raw-yuv",
277       "format", GST_TYPE_FOURCC, GST_MAKE_FOURCC ('I', '4', '2', '0'), NULL);
278
279   switch (enc->options->norm) {
280     case 0:
281     {
282       GValue list = { 0, }
283       , val = {
284       0,};
285
286       g_value_init (&list, GST_TYPE_LIST);
287       g_value_init (&val, G_TYPE_INT);
288       g_value_set_int (&val, pal_v);
289       gst_value_list_append_value (&list, &val);
290       g_value_set_int (&val, ntsc_v);
291       gst_value_list_append_value (&list, &val);
292       gst_structure_set_value (structure, "height", &list);
293       g_value_unset (&list);
294       g_value_unset (&val);
295       break;
296     }
297     case 'n':
298       gst_structure_set (structure, "height", G_TYPE_INT, ntsc_v,
299           (void *) NULL);
300       break;
301     default:
302       gst_structure_set (structure, "height", G_TYPE_INT, pal_v, (void *) NULL);
303       break;
304   }
305   gst_structure_set (structure, "width", G_TYPE_INT, horiz, (void *) NULL);
306   gst_mpeg2enc_add_fps (structure, gst_mpeg2enc_get_fps (enc));
307
308   return structure;
309 }
310
311 static GstCaps *
312 gst_mpeg2enc_getcaps (GstPad * pad)
313 {
314   GstMpeg2enc *enc = GST_MPEG2ENC (GST_PAD_PARENT (pad));
315   GstCaps *caps;
316
317   caps = GST_PAD_CAPS (pad);
318   if (caps) {
319     gst_caps_ref (caps);
320     return caps;
321   }
322
323   switch (enc->options->format) {
324     case 1:                    /* vcd */
325     case 2:                    /* user vcd */
326       caps = gst_caps_new_full (gst_mpeg2enc_structure_from_norm (enc,
327               352, 288, 240), NULL);
328       break;
329     case 4:                    /* svcd */
330     case 5:                    /* user svcd */
331       caps = gst_caps_new_full (gst_mpeg2enc_structure_from_norm (enc,
332               480, 576, 480), NULL);
333       break;
334     case 6:                    /* vcd stills */
335       /* low resolution */
336       caps = gst_caps_new_full (gst_mpeg2enc_structure_from_norm (enc,
337               352, 288, 240), NULL);
338       /* high resolution */
339       gst_caps_append_structure (caps,
340           gst_mpeg2enc_structure_from_norm (enc, 704, 576, 480));
341       break;
342     case 7:                    /* svcd stills */
343       /* low resolution */
344       caps = gst_caps_new_full (gst_mpeg2enc_structure_from_norm (enc,
345               480, 576, 480), NULL);
346       /* high resolution */
347       gst_caps_append_structure (caps,
348           gst_mpeg2enc_structure_from_norm (enc, 704, 576, 480));
349       break;
350     case 0:
351     case 3:
352     case 8:
353     case 9:
354     default:
355       caps = gst_caps_copy (gst_pad_get_pad_template_caps (pad));
356       gst_mpeg2enc_add_fps (gst_caps_get_structure (caps, 0),
357           gst_mpeg2enc_get_fps (enc));
358       break;
359   }
360
361   GST_DEBUG_OBJECT (enc, "returned caps %" GST_PTR_FORMAT, caps);
362   return caps;
363 }
364
365 static gboolean
366 gst_mpeg2enc_setcaps (GstPad * pad, GstCaps * caps)
367 {
368   GstMpeg2enc *enc = GST_MPEG2ENC (GST_PAD_PARENT (pad));
369   GstCaps *othercaps = NULL, *mycaps;
370   gboolean ret;
371
372   /* does not go well to restart stream mid-way */
373   if (enc->encoder)
374     goto refuse_renegotiation;
375
376   /* since mpeg encoder does not really check, let's check caps */
377   mycaps = gst_pad_get_caps (pad);
378   othercaps = gst_caps_intersect (caps, mycaps);
379   gst_caps_unref (mycaps);
380   if (!othercaps || gst_caps_is_empty (othercaps))
381     goto refuse_caps;
382   gst_caps_unref (othercaps);
383   othercaps = NULL;
384
385   /* create new encoder with these settings */
386   enc->encoder = new GstMpeg2Encoder (enc->options, GST_ELEMENT (enc), caps);
387
388   if (!enc->encoder->setup ())
389     goto refuse_caps;
390
391   /* and set caps on other side, which should accept anyway */
392   othercaps = enc->encoder->getFormat ();
393   ret = gst_pad_set_caps (enc->srcpad, othercaps);
394   gst_caps_unref (othercaps);
395   othercaps = NULL;
396   if (!ret)
397     goto refuse_caps;
398
399   /* now that we have all the setup and buffers are expected incoming;
400    * task can get going */
401   gst_pad_start_task (enc->srcpad, (GstTaskFunction) gst_mpeg2enc_loop, enc);
402
403   return TRUE;
404
405 refuse_caps:
406   {
407     GST_WARNING_OBJECT (enc, "refused caps %" GST_PTR_FORMAT, caps);
408
409     if (othercaps)
410       gst_caps_unref (othercaps);
411
412     if (enc->encoder) {
413       delete enc->encoder;
414
415       enc->encoder = NULL;
416     }
417
418     return FALSE;
419   }
420 refuse_renegotiation:
421   {
422     GST_WARNING_OBJECT (enc, "refused renegotiation (to %" GST_PTR_FORMAT ")",
423         caps);
424
425     return FALSE;
426   }
427 }
428
429 static gboolean
430 gst_mpeg2enc_sink_event (GstPad * pad, GstEvent * event)
431 {
432   GstMpeg2enc *enc;
433   gboolean result = TRUE;
434
435   enc = GST_MPEG2ENC (GST_PAD_PARENT (pad));
436
437   switch (GST_EVENT_TYPE (event)) {
438     case GST_EVENT_FLUSH_START:
439       /* forward event */
440       result = gst_pad_push_event (enc->srcpad, event);
441
442       /* no special action as there is not much to flush;
443        * neither is it possible to halt the mpeg encoding loop */
444       goto done;
445       break;
446     case GST_EVENT_FLUSH_STOP:
447       /* forward event */
448       result = gst_pad_push_event (enc->srcpad, event);
449       if (!result)
450         goto done;
451
452       /* this clears the error state in case of a failure in encoding task;
453        * so chain function can carry on again */
454       GST_MPEG2ENC_MUTEX_LOCK (enc);
455       enc->srcresult = GST_FLOW_OK;
456       GST_MPEG2ENC_MUTEX_UNLOCK (enc);
457       goto done;
458       break;
459     case GST_EVENT_EOS:
460       /* inform the encoding task that it can stop now */
461       GST_MPEG2ENC_MUTEX_LOCK (enc);
462       enc->eos = TRUE;
463       GST_MPEG2ENC_SIGNAL (enc);
464       GST_MPEG2ENC_MUTEX_UNLOCK (enc);
465
466       /* eat this event for now, task will send eos when finished */
467       gst_event_unref (event);
468       goto done;
469       break;
470     default:
471       /* for a serialized event, wait until an earlier buffer is gone,
472        * though this is no guarantee as to when the encoder is done with it */
473       if (GST_EVENT_IS_SERIALIZED (event)) {
474         GST_MPEG2ENC_MUTEX_LOCK (enc);
475         while (enc->buffer)
476           GST_MPEG2ENC_WAIT (enc);
477         GST_MPEG2ENC_MUTEX_UNLOCK (enc);
478       }
479       break;
480   }
481
482   result = gst_pad_push_event (enc->srcpad, event);
483
484 done:
485   return result;
486 }
487
488 static void
489 gst_mpeg2enc_loop (GstMpeg2enc * enc)
490 {
491   /* do not try to resume or start when output problems;
492    * also ensures a proper (forced) state change */
493   if (enc->srcresult != GST_FLOW_OK)
494     goto ignore;
495
496   if (enc->encoder) {
497     /* note that init performs a pre-fill and therefore needs buffers */
498     enc->encoder->init ();
499     /* task will stay in here during all of the encoding */
500     enc->encoder->encode ();
501
502     /* if not well and truly eos, something strange happened  */
503     if (!enc->eos) {
504       GST_ERROR_OBJECT (enc, "encoding task ended without being eos");
505       /* notify the chain function that it's over */
506       GST_MPEG2ENC_MUTEX_LOCK (enc);
507       enc->srcresult = GST_FLOW_ERROR;
508       GST_MPEG2ENC_SIGNAL (enc);
509       GST_MPEG2ENC_MUTEX_UNLOCK (enc);
510     } else {
511       /* send eos if this was not a forced stop or other problem */
512       if (enc->srcresult == GST_FLOW_OK)
513         gst_pad_push_event (enc->srcpad, gst_event_new_eos ());
514       goto eos;
515     }
516   } else {
517     GST_WARNING_OBJECT (enc, "task started without Mpeg2Encoder");
518   }
519
520   /* fall-through */
521 done:
522   {
523     /* no need to run wildly, stopped elsewhere, e.g. state change */
524     GST_DEBUG_OBJECT (enc, "pausing encoding task");
525     gst_pad_pause_task (enc->srcpad);
526
527     return;
528   }
529 eos:
530   {
531     GST_DEBUG_OBJECT (enc, "encoding task reached eos");
532     goto done;
533   }
534 ignore:
535   {
536     GST_DEBUG_OBJECT (enc, "not looping because encoding task encountered %s",
537         gst_flow_get_name (enc->srcresult));
538     goto done;
539   }
540 }
541
542 static GstFlowReturn
543 gst_mpeg2enc_chain (GstPad * pad, GstBuffer * buffer)
544 {
545   GstMpeg2enc *enc;
546
547   enc = GST_MPEG2ENC (GST_PAD_PARENT (pad));
548
549   if (G_UNLIKELY (!enc->encoder))
550     goto not_negotiated;
551
552   GST_MPEG2ENC_MUTEX_LOCK (enc);
553
554   if (G_UNLIKELY (enc->eos))
555     goto eos;
556
557   if (G_UNLIKELY (enc->srcresult != GST_FLOW_OK))
558     goto ignore;
559
560   /* things look good, now inform the encoding task that a buffer is ready */
561   while (enc->buffer)
562     GST_MPEG2ENC_WAIT (enc);
563   enc->buffer = buffer;
564   g_queue_push_tail (enc->time, gst_buffer_ref (buffer));
565   GST_MPEG2ENC_SIGNAL (enc);
566   GST_MPEG2ENC_MUTEX_UNLOCK (enc);
567
568   /* buffer will be released by task */
569   return GST_FLOW_OK;
570
571   /* special cases */
572 not_negotiated:
573   {
574     GST_ELEMENT_ERROR (enc, CORE, NEGOTIATION, (NULL),
575         ("format wasn't negotiated before chain function"));
576
577     gst_buffer_unref (buffer);
578     return GST_FLOW_NOT_NEGOTIATED;
579   }
580 eos:
581   {
582     GST_DEBUG_OBJECT (enc, "ignoring buffer at end-of-stream");
583     GST_MPEG2ENC_MUTEX_UNLOCK (enc);
584
585     gst_buffer_unref (buffer);
586     return GST_FLOW_UNEXPECTED;
587   }
588 ignore:
589   {
590     GstFlowReturn ret = enc->srcresult;
591
592     GST_DEBUG_OBJECT (enc,
593         "ignoring buffer because encoding task encountered %s",
594         gst_flow_get_name (enc->srcresult));
595     GST_MPEG2ENC_MUTEX_UNLOCK (enc);
596
597     gst_buffer_unref (buffer);
598     return ret;
599   }
600 }
601
602 static void
603 gst_mpeg2enc_get_property (GObject * object,
604     guint prop_id, GValue * value, GParamSpec * pspec)
605 {
606   GST_MPEG2ENC (object)->options->getProperty (prop_id, value);
607 }
608
609 static void
610 gst_mpeg2enc_set_property (GObject * object,
611     guint prop_id, const GValue * value, GParamSpec * pspec)
612 {
613   GST_MPEG2ENC (object)->options->setProperty (prop_id, value);
614 }
615
616 static gboolean
617 gst_mpeg2enc_src_activate_push (GstPad * pad, gboolean active)
618 {
619   gboolean result = TRUE;
620   GstMpeg2enc *enc;
621
622   enc = GST_MPEG2ENC (GST_PAD_PARENT (pad));
623
624   if (active) {
625     /* setcaps will start task once encoder is setup */
626   } else {
627     /* can only end the encoding loop by forcing eos */
628     GST_MPEG2ENC_MUTEX_LOCK (enc);
629     enc->eos = TRUE;
630     enc->srcresult = GST_FLOW_WRONG_STATE;
631     GST_MPEG2ENC_SIGNAL (enc);
632     GST_MPEG2ENC_MUTEX_UNLOCK (enc);
633
634     /* encoding loop should have ended now and can be joined */
635     result = gst_pad_stop_task (pad);
636   }
637
638   return result;
639 }
640
641 static GstStateChangeReturn
642 gst_mpeg2enc_change_state (GstElement * element, GstStateChange transition)
643 {
644   GstMpeg2enc *enc = GST_MPEG2ENC (element);
645   GstStateChangeReturn ret;
646
647   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
648   if (ret == GST_STATE_CHANGE_FAILURE)
649     goto done;
650
651   switch (transition) {
652     case GST_STATE_CHANGE_PAUSED_TO_READY:
653       gst_mpeg2enc_reset (enc);
654       break;
655     default:
656       break;
657   }
658
659 done:
660   return ret;
661 }
662
663 #ifndef GST_DISABLE_GST_DEBUG
664
665 static mjpeg_log_handler_t old_handler = NULL;
666
667 /* note that this will affect all mjpegtools elements/threads */
668 static void
669 gst_mpeg2enc_log_callback (log_level_t level, const char *message)
670 {
671   GstDebugLevel gst_level;
672
673 #if GST_MJPEGTOOLS_API >= 10903
674   static const gint mjpeg_log_error = mjpeg_loglev_t ("error");
675   static const gint mjpeg_log_warn = mjpeg_loglev_t ("warn");
676   static const gint mjpeg_log_info = mjpeg_loglev_t ("info");
677   static const gint mjpeg_log_debug = mjpeg_loglev_t ("debug");
678 #else
679   static const gint mjpeg_log_error = LOG_ERROR;
680   static const gint mjpeg_log_warn = LOG_WARN;
681   static const gint mjpeg_log_info = LOG_INFO;
682   static const gint mjpeg_log_debug = LOG_DEBUG;
683 #endif
684
685   if (level == mjpeg_log_error) {
686     gst_level = GST_LEVEL_ERROR;
687   } else if (level == mjpeg_log_warn) {
688     gst_level = GST_LEVEL_WARNING;
689   } else if (level == mjpeg_log_info) {
690     gst_level = GST_LEVEL_INFO;
691   } else if (level == mjpeg_log_debug) {
692     gst_level = GST_LEVEL_DEBUG;
693   } else {
694     gst_level = GST_LEVEL_INFO;
695   }
696
697   /* message could have a % in it, do not segfault in such case */
698   gst_debug_log (mpeg2enc_debug, gst_level, "", "", 0, NULL, "%s", message);
699
700   /* chain up to the old handler;
701    * this could actually be a handler from another mjpegtools based
702    * plugin; in which case messages can come out double or from
703    * the wrong plugin (element)... */
704   old_handler (level, message);
705 }
706 #endif
707
708 static gboolean
709 plugin_init (GstPlugin * plugin)
710 {
711 #ifndef GST_DISABLE_GST_DEBUG
712   old_handler = mjpeg_log_set_handler (gst_mpeg2enc_log_callback);
713   g_assert (old_handler != NULL);
714 #endif
715   /* in any case, we do not want default handler output */
716   mjpeg_default_handler_verbosity (0);
717
718   return gst_element_register (plugin, "mpeg2enc",
719       GST_RANK_MARGINAL, GST_TYPE_MPEG2ENC);
720 }
721
722 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
723     GST_VERSION_MINOR,
724     "mpeg2enc",
725     "High-quality MPEG-1/2 video encoder",
726     plugin_init, VERSION, "GPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)