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., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, 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 * [mjpegtools](http://mjpeg.sourceforge.net/) library.
29 *
30 * Documentation on MPEG encoding in general can be found in the
31 * [MJPEG Howto](https://sourceforge.net/docman/display_doc.php?docid=3456&group_id=5776)
32 * and on the various available parameters in the documentation
33 * of the mpeg2enc tool in particular, which shares options with this element.
34 *
35 * ## Example pipeline
36 *
37 * |[
38 * gst-launch-1.0 videotestsrc num-buffers=1000 ! mpeg2enc ! filesink location=videotestsrc.m1v
39 * ]| This example pipeline will encode a test video source to a an MPEG1
40 * elementary stream (with Generic MPEG1 profile).
41 *
42 * Likely, the #GstMpeg2enc:format property
43 * is most important, as it selects the type of MPEG stream that is produced.
44 * In particular, default property values are dependent on the format,
45 * and can even be forcibly restrained to certain pre-sets (and thereby ignored).
46 * Note that the (S)VCD profiles also restrict the image size, so some scaling
47 * may be needed to accommodate this. The so-called generic profiles (as used
48 * in the example above) allow most parameters to be adjusted.
49 *
50 * |[
51 * gst-launch-1.0 videotestsrc num-buffers=1000 ! videoscale ! mpeg2enc format=1 norm=p ! filesink location=videotestsrc.m1v
52 * ]| This will produce an MPEG1 profile stream according to VCD2.0 specifications
53 * for PAL #GstMpeg2enc:norm (as the image height is dependent on video norm).
54 */
55
56 #ifdef HAVE_CONFIG_H
57 #include "config.h"
58 #endif
59
60 #include <gst/glib-compat-private.h>
61 #include "gstmpeg2enc.hh"
62
63 GST_DEBUG_CATEGORY (mpeg2enc_debug);
64
65 #define COMMON_VIDEO_CAPS \
66 "width = (int) [ 16, 4096 ], " \
67 "height = (int) [ 16, 4096 ], " \
68 "framerate = " \
69 " (fraction) { 24000/1001, 24/1, 25/1, 30000/1001, 30/1, 50/1, 60000/1001 }"
70
71 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
72 GST_PAD_SINK,
73 GST_PAD_ALWAYS,
74 GST_STATIC_CAPS ("video/x-raw, format = (string) 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
89 static gboolean gst_mpeg2enc_start (GstVideoEncoder * video_encoder);
90 static gboolean gst_mpeg2enc_stop (GstVideoEncoder * video_encoder);
91 static gboolean gst_mpeg2enc_set_format (GstVideoEncoder *
92 video_encoder, GstVideoCodecState * state);
93 static GstCaps * gst_mpeg2enc_getcaps (GstVideoEncoder *
94 video_encoder, GstCaps * filter);
95 static GstFlowReturn gst_mpeg2enc_handle_frame (GstVideoEncoder *
96 video_encoder, GstVideoCodecFrame * frame);
97 static gboolean gst_mpeg2enc_sink_event (GstVideoEncoder *
98 video_encoder, GstEvent * event);
99
100 static GstFlowReturn gst_mpeg2enc_finish (GstVideoEncoder * video_encoder);
101 static void gst_mpeg2enc_loop (GstVideoEncoder * video_encoder);
102
103 static void gst_mpeg2enc_get_property (GObject * object,
104 guint prop_id, GValue * value, GParamSpec * pspec);
105 static void gst_mpeg2enc_set_property (GObject * object,
106 guint prop_id, const GValue * value, GParamSpec * pspec);
107 static gboolean gst_mpeg2enc_src_activate_mode (GstPad * pad, GstObject * parent,
108 GstPadMode mode, gboolean active);
109 static gboolean mpeg2enc_element_init (GstPlugin * plugin);
110
111 #define gst_mpeg2enc_parent_class parent_class
112 G_DEFINE_TYPE_WITH_CODE (GstMpeg2enc, gst_mpeg2enc, GST_TYPE_VIDEO_ENCODER,
113 G_IMPLEMENT_INTERFACE (GST_TYPE_PRESET, NULL));
114 GST_ELEMENT_REGISTER_DEFINE_CUSTOM (mpeg2enc, mpeg2enc_element_init);
115
116 static void
gst_mpeg2enc_class_init(GstMpeg2encClass * klass)117 gst_mpeg2enc_class_init (GstMpeg2encClass * klass)
118 {
119 GObjectClass *object_class = G_OBJECT_CLASS (klass);
120 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
121 GstVideoEncoderClass *video_encoder_class = GST_VIDEO_ENCODER_CLASS (klass);
122
123 GST_DEBUG_CATEGORY_INIT (mpeg2enc_debug, "mpeg2enc", 0, "MPEG1/2 encoder");
124
125 object_class->set_property = gst_mpeg2enc_set_property;
126 object_class->get_property = gst_mpeg2enc_get_property;
127
128 /* register properties */
129 GstMpeg2EncOptions::initProperties (object_class);
130
131 object_class->finalize = GST_DEBUG_FUNCPTR (gst_mpeg2enc_finalize);
132
133 #if 0
134 element_class->change_state = GST_DEBUG_FUNCPTR (gst_mpeg2enc_change_state);
135 #endif
136
137 gst_element_class_add_static_pad_template (element_class, &src_template);
138 gst_element_class_add_static_pad_template (element_class, &sink_template);
139
140 gst_element_class_set_static_metadata (element_class,
141 "mpeg2enc video encoder", "Codec/Encoder/Video",
142 "High-quality MPEG-1/2 video encoder",
143 "Andrew Stevens <andrew.stevens@nexgo.de>\n"
144 "Ronald Bultje <rbultje@ronald.bitfreak.net>");
145
146 video_encoder_class->start = GST_DEBUG_FUNCPTR (gst_mpeg2enc_start);
147 video_encoder_class->stop = GST_DEBUG_FUNCPTR (gst_mpeg2enc_stop);
148 video_encoder_class->handle_frame = GST_DEBUG_FUNCPTR (gst_mpeg2enc_handle_frame);
149 video_encoder_class->set_format = GST_DEBUG_FUNCPTR (gst_mpeg2enc_set_format);
150 video_encoder_class->finish = GST_DEBUG_FUNCPTR (gst_mpeg2enc_finish);
151 //video_encoder_class->pre_push = GST_DEBUG_FUNCPTR (gst_mpeg2enc_pre_push);
152 video_encoder_class->sink_event = GST_DEBUG_FUNCPTR (gst_mpeg2enc_sink_event);
153 video_encoder_class->getcaps = GST_DEBUG_FUNCPTR (gst_mpeg2enc_getcaps);
154 }
155
156 static void
gst_mpeg2enc_finalize(GObject * object)157 gst_mpeg2enc_finalize (GObject * object)
158 {
159 GstMpeg2enc *enc = GST_MPEG2ENC (object);
160
161 /* clean up */
162 gst_mpeg2enc_reset (enc);
163
164 delete enc->options;
165
166 g_mutex_clear (&enc->tlock);
167 g_cond_clear (&enc->cond);
168
169 G_OBJECT_CLASS (parent_class)->finalize (object);
170 }
171
172 static void
gst_mpeg2enc_init(GstMpeg2enc * enc)173 gst_mpeg2enc_init (GstMpeg2enc * enc)
174 {
175 enc->options = new GstMpeg2EncOptions ();
176 enc->encoder = NULL;
177
178 g_mutex_init (&enc->tlock);
179 g_cond_init (&enc->cond);
180 enc->started = FALSE;
181
182 gst_pad_set_activatemode_function (GST_VIDEO_ENCODER_SRC_PAD (enc),
183 GST_DEBUG_FUNCPTR (gst_mpeg2enc_src_activate_mode));
184
185 gst_mpeg2enc_reset (enc);
186 }
187
188 static gboolean
gst_mpeg2enc_src_activate_mode(GstPad * pad,GstObject * parent,GstPadMode mode,gboolean active)189 gst_mpeg2enc_src_activate_mode (GstPad * pad, GstObject * parent,
190 GstPadMode mode, gboolean active)
191 {
192 gboolean result = TRUE;
193 GstMpeg2enc *enc;
194
195 enc = GST_MPEG2ENC (parent);
196
197 if (mode != GST_PAD_MODE_PUSH)
198 return FALSE;
199
200 if (active) {
201 /* setcaps will start task once encoder is setup */
202 } else {
203 /* can only end the encoding loop by forcing eos */
204 GST_MPEG2ENC_MUTEX_LOCK (enc);
205 enc->eos = TRUE;
206 enc->srcresult = GST_FLOW_FLUSHING;
207 GST_MPEG2ENC_SIGNAL (enc);
208 GST_MPEG2ENC_MUTEX_UNLOCK (enc);
209 }
210
211 return result;
212 }
213
214 static void
gst_mpeg2enc_reset(GstMpeg2enc * enc)215 gst_mpeg2enc_reset (GstMpeg2enc * enc)
216 {
217 enc->eos = FALSE;
218 enc->srcresult = GST_FLOW_OK;
219
220 /* in case of error'ed ending */
221 if (enc->pending_frame) {
222 gst_video_encoder_finish_frame (GST_VIDEO_ENCODER (enc), enc->pending_frame);
223 enc->pending_frame = NULL;
224 }
225
226 if (enc->encoder) {
227 delete enc->encoder;
228
229 enc->encoder = NULL;
230 }
231 }
232
233 static gboolean
gst_mpeg2enc_start(GstVideoEncoder * video_encoder)234 gst_mpeg2enc_start (GstVideoEncoder * video_encoder)
235 {
236 GstMpeg2enc *enc = GST_MPEG2ENC (video_encoder);
237
238 GST_DEBUG_OBJECT (video_encoder, "start");
239
240 if (!enc->options) {
241 GST_ELEMENT_ERROR (enc, LIBRARY, INIT,
242 ("Failed to get default encoder options"), (NULL));
243 return FALSE;
244 }
245 /* start task to create multiplexor and start muxing */
246 if (G_UNLIKELY (enc->srcresult != GST_FLOW_OK)) {
247 GST_ELEMENT_ERROR (enc, LIBRARY, INIT,
248 ("Invalid encoder state"), (NULL));
249 return FALSE;
250 }
251
252 return TRUE;
253 }
254
255 static gboolean
gst_mpeg2enc_stop(GstVideoEncoder * video_encoder)256 gst_mpeg2enc_stop (GstVideoEncoder * video_encoder)
257 {
258 gboolean result = TRUE;
259 GstMpeg2enc *enc;
260
261 GST_DEBUG_OBJECT (video_encoder, "stop");
262
263 enc = GST_MPEG2ENC (video_encoder);
264
265 /* can only end the encoding loop by forcing eos */
266 GST_MPEG2ENC_MUTEX_LOCK (enc);
267 enc->eos = TRUE;
268 enc->srcresult = GST_FLOW_FLUSHING;
269 GST_MPEG2ENC_SIGNAL (enc);
270 GST_MPEG2ENC_MUTEX_UNLOCK (enc);
271
272 /* encoding loop should have ended now and can be joined */
273 if (enc->started) {
274 result = gst_pad_stop_task (video_encoder->srcpad);
275 enc->started = FALSE;
276 }
277
278
279 GST_MPEG2ENC_MUTEX_LOCK (enc);
280 gst_mpeg2enc_reset (enc);
281 GST_MPEG2ENC_MUTEX_UNLOCK (enc);
282
283 return result;
284 }
285
286
287
288 /* some (!) coding to get caps depending on the video norm and chosen format */
289 static void
gst_mpeg2enc_add_fps(GstStructure * structure,gint fpss[])290 gst_mpeg2enc_add_fps (GstStructure * structure, gint fpss[])
291 {
292 GValue list = { 0, }, fps = {
293 0,};
294 guint n;
295
296 g_value_init (&list, GST_TYPE_LIST);
297 g_value_init (&fps, GST_TYPE_FRACTION);
298 for (n = 0; fpss[n] != 0; n += 2) {
299 gst_value_set_fraction (&fps, fpss[n], fpss[n + 1]);
300 gst_value_list_append_value (&list, &fps);
301 }
302 gst_structure_set_value (structure, "framerate", &list);
303 g_value_unset (&list);
304 g_value_unset (&fps);
305 }
306
307 static inline gint *
gst_mpeg2enc_get_fps(GstMpeg2enc * enc)308 gst_mpeg2enc_get_fps (GstMpeg2enc * enc)
309 {
310 static gint fps_pal[]
311 = { 24, 1, 25, 1, 50, 1, 0 };
312 static gint fps_ntsc[]
313 = { 24000, 1001, 24, 1, 30000, 1001, 30, 1, 60000, 1001, 0 };
314 static gint fps_all[]
315 = { 24000, 1001, 24, 1, 30000, 1001, 30, 1, 60000, 1001, 25, 1, 50, 1, 0 };
316
317 if (enc->options->norm == 'n')
318 return fps_ntsc;
319 else if (enc->options->norm == 0)
320 return fps_all;
321 else
322 return fps_pal;
323 }
324
325 static gboolean
gst_mpeg2enc_set_format(GstVideoEncoder * video_encoder,GstVideoCodecState * state)326 gst_mpeg2enc_set_format (GstVideoEncoder * video_encoder, GstVideoCodecState * state)
327 {
328 GstVideoCodecState *output_state;
329 GstMpeg2enc *enc;
330 GstCaps *caps;
331
332 enc = GST_MPEG2ENC (video_encoder);
333
334 GST_DEBUG_OBJECT (video_encoder, "set_format");
335 /* Store input state */
336 if (enc->input_state)
337 gst_video_codec_state_unref (enc->input_state);
338 enc->input_state = gst_video_codec_state_ref (state);
339
340 /* does not go well to restart stream mid-way */
341 if (enc->encoder != NULL)
342 goto refuse_renegotiation;
343
344
345 /* since mpeg encoder does not really check, let's check caps */
346 if (GST_VIDEO_INFO_FORMAT (&state->info) != GST_VIDEO_FORMAT_I420)
347 goto refuse_caps;
348
349 caps = gst_caps_new_simple ("video/mpeg",
350 "systemstream", G_TYPE_BOOLEAN, FALSE,
351 "mpegversion", G_TYPE_INT, (enc->options->mpeg == 1)?1:2, NULL);
352
353 output_state =
354 gst_video_encoder_set_output_state (video_encoder,
355 caps, state);
356 gst_video_codec_state_unref (output_state);
357
358 gst_video_encoder_negotiate (GST_VIDEO_ENCODER (enc));
359
360 return TRUE;
361
362 refuse_caps:
363 {
364 GST_WARNING_OBJECT (enc, "refused caps %" GST_PTR_FORMAT, state->caps);
365
366 return FALSE;
367 }
368 refuse_renegotiation:
369 {
370 GST_WARNING_OBJECT (enc, "refused renegotiation (to %" GST_PTR_FORMAT ")",
371 state->caps);
372
373 return FALSE;
374 }
375 }
376
377 static GstStructure *
gst_mpeg2enc_structure_from_norm(GstMpeg2enc * enc,gint horiz,gint pal_v,gint ntsc_v)378 gst_mpeg2enc_structure_from_norm (GstMpeg2enc * enc, gint horiz,
379 gint pal_v, gint ntsc_v)
380 {
381 GstStructure *structure;
382
383 structure = gst_structure_new ("video/x-raw",
384 "format", G_TYPE_STRING, "I420", NULL);
385
386 switch (enc->options->norm) {
387 case 0:
388 {
389 GValue list = { 0, }
390 , val = {
391 0,};
392
393 g_value_init (&list, GST_TYPE_LIST);
394 g_value_init (&val, G_TYPE_INT);
395 g_value_set_int (&val, pal_v);
396 gst_value_list_append_value (&list, &val);
397 g_value_set_int (&val, ntsc_v);
398 gst_value_list_append_value (&list, &val);
399 gst_structure_set_value (structure, "height", &list);
400 g_value_unset (&list);
401 g_value_unset (&val);
402 break;
403 }
404 case 'n':
405 gst_structure_set (structure, "height", G_TYPE_INT, ntsc_v,
406 (void *) NULL);
407 break;
408 default:
409 gst_structure_set (structure, "height", G_TYPE_INT, pal_v, (void *) NULL);
410 break;
411 }
412 gst_structure_set (structure, "width", G_TYPE_INT, horiz, (void *) NULL);
413 gst_mpeg2enc_add_fps (structure, gst_mpeg2enc_get_fps (enc));
414
415 return structure;
416 }
417
418 static GstCaps *
gst_mpeg2enc_getcaps(GstVideoEncoder * video_encoder,GstCaps * filter)419 gst_mpeg2enc_getcaps (GstVideoEncoder * video_encoder, GstCaps * filter)
420 {
421 GstCaps *caps;
422 GstMpeg2enc *enc = GST_MPEG2ENC (video_encoder);
423
424 caps = gst_pad_get_current_caps (video_encoder->sinkpad);
425 if (caps)
426 return caps;
427
428 switch (enc->options->format) {
429 case 1: /* vcd */
430 case 2: /* user vcd */
431 caps = gst_caps_new_full (gst_mpeg2enc_structure_from_norm (enc,
432 352, 288, 240), NULL);
433 break;
434 case 4: /* svcd */
435 case 5: /* user svcd */
436 caps = gst_caps_new_full (gst_mpeg2enc_structure_from_norm (enc,
437 480, 576, 480), NULL);
438 break;
439 case 6: /* vcd stills */
440 /* low resolution */
441 caps = gst_caps_new_full (gst_mpeg2enc_structure_from_norm (enc,
442 352, 288, 240), NULL);
443 /* high resolution */
444 gst_caps_append_structure (caps,
445 gst_mpeg2enc_structure_from_norm (enc, 704, 576, 480));
446 break;
447 case 7: /* svcd stills */
448 /* low resolution */
449 caps = gst_caps_new_full (gst_mpeg2enc_structure_from_norm (enc,
450 480, 576, 480), NULL);
451 /* high resolution */
452 gst_caps_append_structure (caps,
453 gst_mpeg2enc_structure_from_norm (enc, 704, 576, 480));
454 break;
455 case 0:
456 case 3:
457 case 8:
458 case 9:
459 default:
460 caps = gst_caps_copy (gst_pad_get_pad_template_caps (video_encoder->sinkpad));
461 gst_mpeg2enc_add_fps (gst_caps_get_structure (caps, 0),
462 gst_mpeg2enc_get_fps (enc));
463 break;
464 }
465
466 return caps;
467 }
468
469 static GstFlowReturn
gst_mpeg2enc_finish(GstVideoEncoder * video_encoder)470 gst_mpeg2enc_finish (GstVideoEncoder * video_encoder)
471 {
472 GstMpeg2enc *enc;
473
474 enc = GST_MPEG2ENC (video_encoder);
475
476 GST_DEBUG_OBJECT (video_encoder, "finish");
477
478 /* inform the encoding task that it can stop now */
479 GST_MPEG2ENC_MUTEX_LOCK (enc);
480 enc->eos = TRUE;
481 GST_MPEG2ENC_SIGNAL (enc);
482 GST_MPEG2ENC_MUTEX_UNLOCK (enc);
483
484 return GST_FLOW_OK;
485 }
486
487 static gboolean
gst_mpeg2enc_sink_event(GstVideoEncoder * video_encoder,GstEvent * event)488 gst_mpeg2enc_sink_event (GstVideoEncoder * video_encoder, GstEvent * event)
489 {
490 GstMpeg2enc *enc;
491 gboolean result = TRUE;
492
493 enc = GST_MPEG2ENC (video_encoder);
494 GST_DEBUG_OBJECT (video_encoder, "sink_event");
495
496 switch (GST_EVENT_TYPE (event)) {
497 case GST_EVENT_FLUSH_START:
498 /* forward event */
499 result = GST_VIDEO_ENCODER_CLASS (parent_class)->sink_event (video_encoder, event);
500
501 /* no special action as there is not much to flush;
502 * neither is it possible to halt the mpeg encoding loop */
503 goto done;
504 case GST_EVENT_FLUSH_STOP:
505 /* forward event */
506 result = GST_VIDEO_ENCODER_CLASS (parent_class)->sink_event (video_encoder, event);
507 if (!result)
508 goto done;
509
510 /* this clears the error state in case of a failure in encoding task;
511 * so handle_frame function can carry on again */
512 GST_MPEG2ENC_MUTEX_LOCK (enc);
513 enc->srcresult = GST_FLOW_OK;
514 GST_MPEG2ENC_MUTEX_UNLOCK (enc);
515 goto done;
516 case GST_EVENT_EOS:
517 /* inform the encoding task that it can stop now */
518 GST_MPEG2ENC_MUTEX_LOCK (enc);
519 enc->eos = TRUE;
520 GST_MPEG2ENC_SIGNAL (enc);
521 GST_MPEG2ENC_MUTEX_UNLOCK (enc);
522
523 /* eat this event for now, task will send eos when finished */
524 gst_event_unref (event);
525 goto done;
526 default:
527 /* for a serialized event, wait until an earlier buffer is gone,
528 * though this is no guarantee as to when the encoder is done with it */
529 if (GST_EVENT_IS_SERIALIZED (event)) {
530 GST_MPEG2ENC_MUTEX_LOCK (enc);
531 while (enc->pending_frame != NULL)
532 GST_MPEG2ENC_WAIT (enc);
533 GST_MPEG2ENC_MUTEX_UNLOCK (enc);
534 }
535 break;
536 }
537
538 result = GST_VIDEO_ENCODER_CLASS (parent_class)->sink_event (video_encoder, event);
539
540 done:
541 return result;
542 }
543
544 static void
gst_mpeg2enc_loop(GstVideoEncoder * video_encoder)545 gst_mpeg2enc_loop (GstVideoEncoder * video_encoder)
546 {
547 GstMpeg2enc *enc = GST_MPEG2ENC (video_encoder);
548
549 GST_DEBUG_OBJECT (enc, "encoding task loop:START");
550 /* do not try to resume or start when output problems;
551 * also ensures a proper (forced) state change */
552 if (enc->srcresult != GST_FLOW_OK) {
553 GST_MPEG2ENC_MUTEX_LOCK (enc);
554 enc->srcresult = GST_FLOW_ERROR;
555 GST_MPEG2ENC_SIGNAL (enc);
556 GST_MPEG2ENC_MUTEX_UNLOCK (enc);
557 goto ignore;
558 }
559 GST_DEBUG_OBJECT (enc, "encoding task loop: flow OK");
560
561 if (!enc->encoder) {
562 gboolean ret;
563 GstClockTime latency;
564 GstVideoInfo *info = &enc->input_state->info;
565
566 /* create new encoder with these settings */
567 enc->encoder = new GstMpeg2Encoder (enc->options, GST_ELEMENT (video_encoder),
568 gst_pad_get_current_caps(video_encoder->sinkpad));
569
570 ret = enc->encoder->setup ();
571
572 /* We must have a fixated max GOP size after setting up */
573 g_assert (enc->options->max_GOP_size != -1);
574
575 /* mjpeg tools outputs encoded data on a GOP basis, our latency is
576 * thus at least max_GOP_size. It also introduces a 5-frame delay on
577 * top of that, this was determined empirically, a surface level inspection
578 * of the code didn't show where and why that delay is introduced exactly,
579 * and this is not specifically documented, but it needs to be taken
580 * into account when calculating the latency
581 */
582 if (GST_VIDEO_INFO_FPS_D (info) == 0 || GST_VIDEO_INFO_FPS_N (info) == 0) {
583 /* Assume 25fps for unknown framerates. Better than reporting
584 * that we introduce no latency while we actually do
585 */
586 latency = gst_util_uint64_scale (enc->options->max_GOP_size + 5,
587 1 * GST_SECOND, 25);
588 } else {
589 latency = gst_util_uint64_scale (enc->options->max_GOP_size + 5,
590 GST_VIDEO_INFO_FPS_D (info) * GST_SECOND, GST_VIDEO_INFO_FPS_N (info));
591 }
592
593 gst_video_encoder_set_latency (video_encoder, latency, latency);
594
595 /* SeqEncoder init requires at least two frames */
596 enc->encoder->init ();
597
598 if (!ret) {
599 GST_MPEG2ENC_MUTEX_LOCK (enc);
600 enc->srcresult = GST_FLOW_NOT_NEGOTIATED;
601 GST_MPEG2ENC_SIGNAL (enc);
602 GST_MPEG2ENC_MUTEX_UNLOCK (enc);
603 goto encoder;
604 }
605 }
606 GST_DEBUG_OBJECT (enc, "encoding task loop: setup and init DONE");
607
608 /* note that init performs a pre-fill and therefore needs buffers */
609 /* task will stay in here during all of the encoding */
610 enc->encoder->encode ();
611 GST_DEBUG_OBJECT (enc, "encoding task loop: encode DONE");
612 /* if not well and truly eos, something strange happened */
613 if (!enc->eos) {
614 GST_ERROR_OBJECT (enc, "encoding task ended without being eos");
615 /* notify the handle_frame function that it's over */
616 GST_MPEG2ENC_MUTEX_LOCK (enc);
617 enc->srcresult = GST_FLOW_ERROR;
618 GST_MPEG2ENC_SIGNAL (enc);
619 GST_MPEG2ENC_MUTEX_UNLOCK (enc);
620 } else {
621 /* send eos if this was not a forced stop or other problem */
622 GST_DEBUG_OBJECT (enc, "encoding task loop: eos REACHED");
623 if (enc->srcresult == GST_FLOW_OK) {
624 gst_pad_push_event (video_encoder->srcpad, gst_event_new_eos ());
625 GST_DEBUG_OBJECT (enc, "encoding task loop: eos SENT");
626 }
627 goto eos;
628 }
629
630 /* fall-through */
631 done:
632 {
633 /* no need to run wildly, stopped elsewhere, e.g. state change */
634 GST_DEBUG_OBJECT (enc, "pausing encoding task");
635 gst_pad_pause_task (video_encoder->srcpad);
636
637 return;
638 }
639 encoder:
640 {
641 GST_ELEMENT_ERROR (enc, CORE, NEGOTIATION, (NULL),
642 ("encoder setup failed"));
643 if (enc->encoder) {
644 delete enc->encoder;
645
646 enc->encoder = NULL;
647 }
648
649 goto done;
650 }
651 eos:
652 {
653 GST_DEBUG_OBJECT (enc, "encoding task reached eos");
654 goto done;
655 }
656 ignore:
657 {
658 GST_DEBUG_OBJECT (enc, "not looping because encoding task encountered %s",
659 gst_flow_get_name (enc->srcresult));
660 goto done;
661 }
662 }
663
664 static GstFlowReturn
gst_mpeg2enc_handle_frame(GstVideoEncoder * video_encoder,GstVideoCodecFrame * frame)665 gst_mpeg2enc_handle_frame (GstVideoEncoder *video_encoder, GstVideoCodecFrame *frame)
666 {
667 GstMpeg2enc *enc = GST_MPEG2ENC (video_encoder);
668
669 GST_DEBUG_OBJECT (video_encoder, "handle_frame");
670 GST_MPEG2ENC_MUTEX_LOCK (enc);
671
672 if (G_UNLIKELY (enc->eos))
673 goto eos;
674 GST_DEBUG_OBJECT (video_encoder, "handle_frame: NOT eos");
675
676 if (G_UNLIKELY (enc->srcresult != GST_FLOW_OK))
677 goto ignore;
678 GST_DEBUG_OBJECT (video_encoder, "handle_frame: flow OK");
679
680 /* If the encoder is busy with a previous frame still, wait
681 * for it to be done */
682 if (enc->pending_frame != NULL) {
683 do {
684 GST_VIDEO_ENCODER_STREAM_UNLOCK (enc);
685 GST_MPEG2ENC_WAIT (enc);
686 GST_VIDEO_ENCODER_STREAM_LOCK (enc);
687
688 /* Re-check the srcresult, since we waited */
689 if (G_UNLIKELY (enc->srcresult != GST_FLOW_OK))
690 goto ignore;
691 } while (enc->pending_frame != NULL);
692 }
693
694 /* frame will be released by task */
695 enc->pending_frame = frame;
696
697 if (!enc->started) {
698 GST_DEBUG_OBJECT (video_encoder, "handle_frame: START task");
699 gst_pad_start_task (video_encoder->srcpad, (GstTaskFunction) gst_mpeg2enc_loop, enc, NULL);
700 enc->started = TRUE;
701 }
702
703 /* things look good, now inform the encoding task that a frame is ready */
704 GST_MPEG2ENC_SIGNAL (enc);
705 GST_MPEG2ENC_MUTEX_UNLOCK (enc);
706
707 return GST_FLOW_OK;
708 eos:
709 {
710 GST_DEBUG_OBJECT (enc, "ignoring frame at end-of-stream");
711 GST_MPEG2ENC_MUTEX_UNLOCK (enc);
712
713 gst_video_encoder_finish_frame (video_encoder, frame);
714
715 return GST_FLOW_EOS;
716 }
717 ignore:
718 {
719 GstFlowReturn ret = enc->srcresult;
720
721 GST_DEBUG_OBJECT (enc,
722 "ignoring frame because encoding task encountered %s",
723 gst_flow_get_name (enc->srcresult));
724
725 enc->eos = TRUE;
726
727 GST_MPEG2ENC_MUTEX_UNLOCK (enc);
728
729 gst_video_encoder_finish_frame (video_encoder, frame);
730
731 return ret;
732 }
733 }
734
735 static void
gst_mpeg2enc_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)736 gst_mpeg2enc_get_property (GObject * object,
737 guint prop_id, GValue * value, GParamSpec * pspec)
738 {
739 GST_MPEG2ENC (object)->options->getProperty (prop_id, value);
740 }
741
742 static void
gst_mpeg2enc_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)743 gst_mpeg2enc_set_property (GObject * object,
744 guint prop_id, const GValue * value, GParamSpec * pspec)
745 {
746 GST_MPEG2ENC (object)->options->setProperty (prop_id, value);
747 }
748
749 #ifndef GST_DISABLE_GST_DEBUG
750
751 static mjpeg_log_handler_t old_handler = NULL;
752
753 /* note that this will affect all mjpegtools elements/threads */
754 static void
gst_mpeg2enc_log_callback(log_level_t level,const char * message)755 gst_mpeg2enc_log_callback (log_level_t level, const char *message)
756 {
757 GstDebugLevel gst_level;
758
759 #if GST_MJPEGTOOLS_API >= 10903
760 static const gint mjpeg_log_error = mjpeg_loglev_t ("error");
761 static const gint mjpeg_log_warn = mjpeg_loglev_t ("warn");
762 static const gint mjpeg_log_info = mjpeg_loglev_t ("info");
763 static const gint mjpeg_log_debug = mjpeg_loglev_t ("debug");
764 #else
765 static const gint mjpeg_log_error = LOG_ERROR;
766 static const gint mjpeg_log_warn = LOG_WARN;
767 static const gint mjpeg_log_info = LOG_INFO;
768 static const gint mjpeg_log_debug = LOG_DEBUG;
769 #endif
770
771 if (level == mjpeg_log_error) {
772 gst_level = GST_LEVEL_ERROR;
773 } else if (level == mjpeg_log_warn) {
774 gst_level = GST_LEVEL_WARNING;
775 } else if (level == mjpeg_log_info) {
776 gst_level = GST_LEVEL_INFO;
777 } else if (level == mjpeg_log_debug) {
778 gst_level = GST_LEVEL_DEBUG;
779 } else {
780 gst_level = GST_LEVEL_INFO;
781 }
782
783 /* message could have a % in it, do not segfault in such case */
784 gst_debug_log (mpeg2enc_debug, gst_level, "", "", 0, NULL, "%s", message);
785
786 /* chain up to the old handler;
787 * this could actually be a handler from another mjpegtools based
788 * plugin; in which case messages can come out double or from
789 * the wrong plugin (element)... */
790 old_handler (level, message);
791 }
792 #endif
793
794 static gboolean
mpeg2enc_element_init(GstPlugin * plugin)795 mpeg2enc_element_init (GstPlugin * plugin)
796 {
797 #ifndef GST_DISABLE_GST_DEBUG
798 old_handler = mjpeg_log_set_handler (gst_mpeg2enc_log_callback);
799 g_assert (old_handler != NULL);
800 #endif
801 /* in any case, we do not want default handler output */
802 mjpeg_default_handler_verbosity (0);
803
804 return gst_element_register (plugin, "mpeg2enc",
805 GST_RANK_MARGINAL, GST_TYPE_MPEG2ENC);
806 }
807
808 static gboolean
plugin_init(GstPlugin * plugin)809 plugin_init (GstPlugin * plugin)
810 {
811 return GST_ELEMENT_REGISTER (mpeg2enc, plugin);
812 }
813
814 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
815 GST_VERSION_MINOR,
816 mpeg2enc,
817 "High-quality MPEG-1/2 video encoder",
818 plugin_init, VERSION, "GPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)
819