2 * Initially based on gst-plugins-bad/sys/androidmedia/gstamcvideodec.c
4 * Copyright (C) 2011, Hewlett-Packard Development Company, L.P.
5 * Author: Sebastian Dröge <sebastian.droege@collabora.co.uk>, Collabora Ltd.
7 * Copyright (C) 2012, Collabora Ltd.
8 * Author: Sebastian Dröge <sebastian.droege@collabora.co.uk>
10 * Copyright (C) 2013, Lemote Ltd.
11 * Author: Chen Jie <chenj@lemote.com>
13 * Copyright (C) 2015, Sebastian Dröge <sebastian@centricular.com>
15 * This library is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU Lesser General Public
17 * License as published by the Free Software Foundation
18 * version 2.1 of the License.
20 * This library is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * Lesser General Public License for more details.
25 * You should have received a copy of the GNU Lesser General Public
26 * License along with this library; if not, write to the Free Software
27 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
41 #define orc_memcpy memcpy
45 #include "gstjniutils.h"
48 #include "gstamcvideoenc.h"
49 #include "gstamc-constants.h"
51 GST_DEBUG_CATEGORY_STATIC (gst_amc_video_enc_debug_category);
52 #define GST_CAT_DEFAULT gst_amc_video_enc_debug_category
54 typedef struct _BufferIdentification BufferIdentification;
55 struct _BufferIdentification
60 static BufferIdentification *
61 buffer_identification_new (GstClockTime timestamp)
63 BufferIdentification *id = g_slice_new (BufferIdentification);
65 id->timestamp = timestamp;
71 buffer_identification_free (BufferIdentification * id)
73 g_slice_free (BufferIdentification, id);
77 static void gst_amc_video_enc_finalize (GObject * object);
79 static GstStateChangeReturn
80 gst_amc_video_enc_change_state (GstElement * element,
81 GstStateChange transition);
83 static gboolean gst_amc_video_enc_open (GstVideoEncoder * encoder);
84 static gboolean gst_amc_video_enc_close (GstVideoEncoder * encoder);
85 static gboolean gst_amc_video_enc_start (GstVideoEncoder * encoder);
86 static gboolean gst_amc_video_enc_stop (GstVideoEncoder * encoder);
87 static gboolean gst_amc_video_enc_set_format (GstVideoEncoder * encoder,
88 GstVideoCodecState * state);
89 static gboolean gst_amc_video_enc_flush (GstVideoEncoder * encoder);
90 static GstFlowReturn gst_amc_video_enc_handle_frame (GstVideoEncoder * encoder,
91 GstVideoCodecFrame * frame);
92 static GstFlowReturn gst_amc_video_enc_finish (GstVideoEncoder * encoder);
94 static GstFlowReturn gst_amc_video_enc_drain (GstAmcVideoEnc * self);
96 #define BIT_RATE_DEFAULT (2 * 1024 * 1024)
97 #define I_FRAME_INTERVAL_DEFAULT 0
102 PROP_I_FRAME_INTERVAL,
103 PROP_I_FRAME_INTERVAL_FLOAT
106 /* class initialization */
108 static void gst_amc_video_enc_class_init (GstAmcVideoEncClass * klass);
109 static void gst_amc_video_enc_init (GstAmcVideoEnc * self);
110 static void gst_amc_video_enc_base_init (gpointer g_class);
112 static GstVideoEncoderClass *parent_class = NULL;
115 gst_amc_video_enc_get_type (void)
117 static gsize type = 0;
119 if (g_once_init_enter (&type)) {
121 static const GTypeInfo info = {
122 sizeof (GstAmcVideoEncClass),
123 gst_amc_video_enc_base_init,
125 (GClassInitFunc) gst_amc_video_enc_class_init,
128 sizeof (GstAmcVideoEnc),
130 (GInstanceInitFunc) gst_amc_video_enc_init,
134 _type = g_type_register_static (GST_TYPE_VIDEO_ENCODER, "GstAmcVideoEnc",
137 GST_DEBUG_CATEGORY_INIT (gst_amc_video_enc_debug_category, "amcvideoenc", 0,
138 "Android MediaCodec video encoder");
140 g_once_init_leave (&type, _type);
145 static GstAmcFormat *
146 create_amc_format (GstAmcVideoEnc * encoder, GstVideoCodecState * input_state,
149 GstAmcVideoEncClass *klass;
152 const gchar *mime = NULL;
153 const gchar *profile_string = NULL;
154 const gchar *level_string = NULL;
168 gint stride, slice_height;
169 GstAmcFormat *format = NULL;
170 GstVideoInfo *info = &input_state->info;
173 klass = GST_AMC_VIDEO_ENC_GET_CLASS (encoder);
174 s = gst_caps_get_structure (src_caps, 0);
178 name = gst_structure_get_name (s);
179 profile_string = gst_structure_get_string (s, "profile");
180 level_string = gst_structure_get_string (s, "level");
182 if (strcmp (name, "video/mpeg") == 0) {
185 if (!gst_structure_get_int (s, "mpegversion", &mpegversion))
188 if (mpegversion == 4) {
189 mime = "video/mp4v-es";
191 if (profile_string) {
192 amc_profile.key = "profile"; /* named profile ? */
193 amc_profile.id = gst_amc_mpeg4_profile_from_string (profile_string);
197 amc_level.key = "level"; /* named level ? */
198 amc_level.id = gst_amc_mpeg4_level_from_string (level_string);
200 } else if ( /* mpegversion == 1 || */ mpegversion == 2)
201 mime = "video/mpeg2";
202 } else if (strcmp (name, "video/x-h263") == 0) {
204 } else if (strcmp (name, "video/x-h264") == 0) {
207 if (profile_string) {
208 amc_profile.key = "profile"; /* named profile ? */
209 amc_profile.id = gst_amc_avc_profile_from_string (profile_string);
213 amc_level.key = "level"; /* named level ? */
214 amc_level.id = gst_amc_avc_level_from_string (level_string);
216 } else if (strcmp (name, "video/x-h265") == 0) {
217 const gchar *tier_string = gst_structure_get_string (s, "tier");
221 if (profile_string) {
222 amc_profile.key = "profile"; /* named profile ? */
223 amc_profile.id = gst_amc_hevc_profile_from_string (profile_string);
226 if (level_string && tier_string) {
227 amc_level.key = "level"; /* named level ? */
229 gst_amc_hevc_tier_level_from_string (tier_string, level_string);
231 } else if (strcmp (name, "video/x-vp8") == 0) {
232 mime = "video/x-vnd.on2.vp8";
233 } else if (strcmp (name, "video/x-vp9") == 0) {
234 mime = "video/x-vnd.on2.vp9";
236 GST_ERROR_OBJECT (encoder, "Failed to convert caps(%s/...) to any mime",
241 format = gst_amc_format_new_video (mime, info->width, info->height, &err);
243 GST_ERROR_OBJECT (encoder, "Failed to create a \"%s,%dx%d\" MediaFormat",
244 mime, info->width, info->height);
245 GST_ELEMENT_ERROR_FROM_ERROR (encoder, err);
250 gst_amc_video_format_to_color_format (klass->codec_info,
251 mime, info->finfo->format);
252 if (color_format == -1)
253 goto video_format_failed_to_convert;
255 gst_amc_format_set_int (format, "bitrate", encoder->bitrate, &err);
257 GST_ELEMENT_WARNING_FROM_ERROR (encoder, err);
258 gst_amc_format_set_int (format, "color-format", color_format, &err);
260 GST_ELEMENT_WARNING_FROM_ERROR (encoder, err);
261 stride = GST_ROUND_UP_4 (info->width); /* safe (?) */
262 gst_amc_format_set_int (format, "stride", stride, &err);
264 GST_ELEMENT_WARNING_FROM_ERROR (encoder, err);
265 slice_height = info->height;
266 gst_amc_format_set_int (format, "slice-height", slice_height, &err);
268 GST_ELEMENT_WARNING_FROM_ERROR (encoder, err);
270 if (profile_string) {
271 if (amc_profile.id == -1)
272 goto unsupported_profile;
274 /* FIXME: Set to any value in AVCProfile* leads to
275 * codec configuration fail */
276 /* gst_amc_format_set_int (format, amc_profile.key, 0x40); */
280 if (amc_level.id == -1)
281 goto unsupported_level;
283 /* gst_amc_format_set_int (format, amc_level.key, amc_level.id); */
286 /* On Android N_MR1 and higher, i-frame-interval can be a float value */
288 if (gst_amc_jni_get_android_level () >= 25) {
289 GST_LOG_OBJECT (encoder, "Setting i-frame-interval to %f",
290 encoder->i_frame_int);
291 gst_amc_format_set_float (format, "i-frame-interval", encoder->i_frame_int,
296 int i_frame_int = encoder->i_frame_int;
297 /* Round a fractional interval to 1 per sec on older Android */
298 if (encoder->i_frame_int > 0 && encoder->i_frame_int < 1.0)
300 gst_amc_format_set_int (format, "i-frame-interval", i_frame_int, &err);
303 GST_ELEMENT_WARNING_FROM_ERROR (encoder, err);
306 gst_amc_format_set_float (format, "frame-rate",
307 ((gfloat) info->fps_n) / info->fps_d, &err);
309 GST_ELEMENT_WARNING_FROM_ERROR (encoder, err);
311 encoder->format = info->finfo->format;
312 if (!gst_amc_color_format_info_set (&encoder->color_format_info,
313 klass->codec_info, mime, color_format, info->width, info->height,
314 stride, slice_height, 0, 0, 0, 0))
315 goto color_format_info_failed_to_set;
317 GST_DEBUG_OBJECT (encoder,
318 "Color format info: {color_format=%d, width=%d, height=%d, "
319 "stride=%d, slice-height=%d, crop-left=%d, crop-top=%d, "
320 "crop-right=%d, crop-bottom=%d, frame-size=%d}",
321 encoder->color_format_info.color_format, encoder->color_format_info.width,
322 encoder->color_format_info.height, encoder->color_format_info.stride,
323 encoder->color_format_info.slice_height,
324 encoder->color_format_info.crop_left, encoder->color_format_info.crop_top,
325 encoder->color_format_info.crop_right,
326 encoder->color_format_info.crop_bottom,
327 encoder->color_format_info.frame_size);
331 video_format_failed_to_convert:
332 GST_ERROR_OBJECT (encoder, "Failed to convert video format");
333 gst_amc_format_free (format);
336 color_format_info_failed_to_set:
337 GST_ERROR_OBJECT (encoder, "Failed to set up GstAmcColorFormatInfo");
338 gst_amc_format_free (format);
342 GST_ERROR_OBJECT (encoder, "Unsupported profile '%s'", profile_string);
343 gst_amc_format_free (format);
347 GST_ERROR_OBJECT (encoder, "Unsupported level '%s'", level_string);
348 gst_amc_format_free (format);
353 caps_from_amc_format (GstAmcFormat * amc_format)
355 GstCaps *caps = NULL;
358 gint amc_profile, amc_level;
359 gfloat frame_rate = 0.0;
360 gint fraction_n, fraction_d;
363 if (!gst_amc_format_get_string (amc_format, "mime", &mime, &err)) {
364 GST_ERROR ("Failed to get 'mime': %s", err->message);
365 g_clear_error (&err);
369 if (!gst_amc_format_get_int (amc_format, "width", &width, &err) ||
370 !gst_amc_format_get_int (amc_format, "height", &height, &err)) {
371 GST_ERROR ("Failed to get size: %s", err->message);
372 g_clear_error (&err);
378 gst_amc_format_get_float (amc_format, "frame-rate", &frame_rate, NULL);
379 gst_util_double_to_fraction (frame_rate, &fraction_n, &fraction_d);
381 if (strcmp (mime, "video/mp4v-es") == 0) {
382 const gchar *profile_string, *level_string;
385 gst_caps_new_simple ("video/mpeg", "mpegversion", G_TYPE_INT, 4,
386 "systemstream", G_TYPE_BOOLEAN, FALSE, NULL);
388 if (gst_amc_format_get_int (amc_format, "profile", &amc_profile, NULL)) {
389 profile_string = gst_amc_mpeg4_profile_to_string (amc_profile);
391 goto unsupported_profile;
393 gst_caps_set_simple (caps, "profile", G_TYPE_STRING, profile_string,
397 if (gst_amc_format_get_int (amc_format, "level", &amc_level, NULL)) {
398 level_string = gst_amc_mpeg4_level_to_string (amc_profile);
400 goto unsupported_level;
402 gst_caps_set_simple (caps, "level", G_TYPE_STRING, level_string, NULL);
405 } else if (strcmp (mime, "video/mpeg2") == 0) {
406 caps = gst_caps_new_simple ("video/mpeg", "mpegversion", 2, NULL);
407 } else if (strcmp (mime, "video/3gpp") == 0) {
408 caps = gst_caps_new_empty_simple ("video/x-h263");
409 } else if (strcmp (mime, "video/avc") == 0) {
410 const gchar *profile_string, *level_string;
413 gst_caps_new_simple ("video/x-h264",
414 "stream-format", G_TYPE_STRING, "byte-stream", NULL);
416 if (gst_amc_format_get_int (amc_format, "profile", &amc_profile, NULL)) {
417 profile_string = gst_amc_avc_profile_to_string (amc_profile, NULL);
419 goto unsupported_profile;
421 gst_caps_set_simple (caps, "profile", G_TYPE_STRING, profile_string,
425 if (gst_amc_format_get_int (amc_format, "level", &amc_level, NULL)) {
426 level_string = gst_amc_avc_level_to_string (amc_profile);
428 goto unsupported_level;
430 gst_caps_set_simple (caps, "level", G_TYPE_STRING, level_string, NULL);
432 } else if (strcmp (mime, "video/hevc") == 0) {
433 const gchar *profile_string, *level_string, *tier_string;
436 gst_caps_new_simple ("video/x-h265",
437 "stream-format", G_TYPE_STRING, "byte-stream", NULL);
439 if (gst_amc_format_get_int (amc_format, "profile", &amc_profile, NULL)) {
440 profile_string = gst_amc_avc_profile_to_string (amc_profile, NULL);
442 goto unsupported_profile;
444 gst_caps_set_simple (caps, "profile", G_TYPE_STRING, profile_string,
448 if (gst_amc_format_get_int (amc_format, "level", &amc_level, NULL)) {
450 gst_amc_hevc_tier_level_to_string (amc_profile, &tier_string);
451 if (!level_string || !tier_string)
452 goto unsupported_level;
454 gst_caps_set_simple (caps,
455 "level", G_TYPE_STRING, level_string,
456 "tier", G_TYPE_STRING, tier_string, NULL);
458 } else if (strcmp (mime, "video/x-vnd.on2.vp8") == 0) {
459 caps = gst_caps_new_empty_simple ("video/x-vp8");
460 } else if (strcmp (mime, "video/x-vnd.on2.vp9") == 0) {
461 caps = gst_caps_new_empty_simple ("video/x-vp9");
464 gst_caps_set_simple (caps, "width", G_TYPE_INT, width,
465 "height", G_TYPE_INT, height,
466 "framerate", GST_TYPE_FRACTION, fraction_n, fraction_d, NULL);
472 GST_ERROR ("Unsupported amc profile id %d", amc_profile);
474 gst_caps_unref (caps);
479 GST_ERROR ("Unsupported amc level id %d", amc_level);
481 gst_caps_unref (caps);
487 gst_amc_video_enc_base_init (gpointer g_class)
489 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
490 GstAmcVideoEncClass *videoenc_class = GST_AMC_VIDEO_ENC_CLASS (g_class);
491 const GstAmcCodecInfo *codec_info;
492 GstPadTemplate *templ;
493 GstCaps *sink_caps, *src_caps;
497 g_type_get_qdata (G_TYPE_FROM_CLASS (g_class), gst_amc_codec_info_quark);
498 /* This happens for the base class and abstract subclasses */
502 videoenc_class->codec_info = codec_info;
504 gst_amc_codec_info_to_caps (codec_info, &sink_caps, &src_caps);
505 /* Add pad templates */
507 gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS, sink_caps);
508 gst_element_class_add_pad_template (element_class, templ);
509 gst_caps_unref (sink_caps);
511 templ = gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS, src_caps);
512 gst_element_class_add_pad_template (element_class, templ);
513 gst_caps_unref (src_caps);
515 longname = g_strdup_printf ("Android MediaCodec %s", codec_info->name);
516 gst_element_class_set_metadata (element_class,
518 "Codec/Encoder/Video/Hardware",
519 longname, "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
524 gst_amc_video_enc_set_property (GObject * object, guint prop_id,
525 const GValue * value, GParamSpec * pspec)
527 GstAmcVideoEnc *encoder;
529 gboolean codec_active;
532 encoder = GST_AMC_VIDEO_ENC (object);
534 GST_OBJECT_LOCK (encoder);
536 state = GST_STATE (encoder);
537 codec_active = (encoder->codec && state != GST_STATE_READY
538 && state != GST_STATE_NULL);
542 encoder->bitrate = g_value_get_uint (value);
544 g_mutex_lock (&encoder->codec_lock);
545 if (encoder->codec) {
546 if (!gst_amc_codec_set_dynamic_bitrate (encoder->codec, &err,
548 g_mutex_unlock (&encoder->codec_lock);
552 g_mutex_unlock (&encoder->codec_lock);
554 GST_ELEMENT_WARNING_FROM_ERROR (encoder, err);
555 g_clear_error (&err);
559 case PROP_I_FRAME_INTERVAL:
560 encoder->i_frame_int = g_value_get_uint (value);
564 case PROP_I_FRAME_INTERVAL_FLOAT:
565 encoder->i_frame_int = g_value_get_float (value);
570 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
573 GST_OBJECT_UNLOCK (encoder);
579 GST_WARNING_OBJECT (encoder, "setting property in wrong state");
580 GST_OBJECT_UNLOCK (encoder);
585 gst_amc_video_enc_get_property (GObject * object, guint prop_id,
586 GValue * value, GParamSpec * pspec)
588 GstAmcVideoEnc *encoder;
590 encoder = GST_AMC_VIDEO_ENC (object);
592 GST_OBJECT_LOCK (encoder);
595 g_value_set_uint (value, encoder->bitrate);
597 case PROP_I_FRAME_INTERVAL:
598 g_value_set_uint (value, encoder->i_frame_int);
600 case PROP_I_FRAME_INTERVAL_FLOAT:
601 g_value_set_float (value, encoder->i_frame_int);
604 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
607 GST_OBJECT_UNLOCK (encoder);
612 gst_amc_video_enc_class_init (GstAmcVideoEncClass * klass)
614 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
615 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
616 GstVideoEncoderClass *videoenc_class = GST_VIDEO_ENCODER_CLASS (klass);
617 GParamFlags dynamic_flag = 0;
619 parent_class = g_type_class_peek_parent (klass);
621 gobject_class->set_property = gst_amc_video_enc_set_property;
622 gobject_class->get_property = gst_amc_video_enc_get_property;
623 gobject_class->finalize = gst_amc_video_enc_finalize;
625 element_class->change_state =
626 GST_DEBUG_FUNCPTR (gst_amc_video_enc_change_state);
628 videoenc_class->start = GST_DEBUG_FUNCPTR (gst_amc_video_enc_start);
629 videoenc_class->stop = GST_DEBUG_FUNCPTR (gst_amc_video_enc_stop);
630 videoenc_class->open = GST_DEBUG_FUNCPTR (gst_amc_video_enc_open);
631 videoenc_class->close = GST_DEBUG_FUNCPTR (gst_amc_video_enc_close);
632 videoenc_class->flush = GST_DEBUG_FUNCPTR (gst_amc_video_enc_flush);
633 videoenc_class->set_format = GST_DEBUG_FUNCPTR (gst_amc_video_enc_set_format);
634 videoenc_class->handle_frame =
635 GST_DEBUG_FUNCPTR (gst_amc_video_enc_handle_frame);
636 videoenc_class->finish = GST_DEBUG_FUNCPTR (gst_amc_video_enc_finish);
638 // On Android >= 19, we can set bitrate dynamically
639 // so add the flag so apps can detect it.
640 if (gst_amc_codec_have_dynamic_bitrate ())
641 dynamic_flag = GST_PARAM_MUTABLE_PLAYING;
643 g_object_class_install_property (gobject_class, PROP_BIT_RATE,
644 g_param_spec_uint ("bitrate", "Bitrate", "Bitrate in bit/sec", 1,
645 G_MAXINT, BIT_RATE_DEFAULT,
646 dynamic_flag | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
648 g_object_class_install_property (gobject_class, PROP_I_FRAME_INTERVAL,
649 g_param_spec_uint ("i-frame-interval", "I-frame interval",
650 "The frequency of I frames expressed in seconds between I frames (0 for automatic)",
651 0, G_MAXINT, I_FRAME_INTERVAL_DEFAULT,
652 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
654 g_object_class_install_property (gobject_class, PROP_I_FRAME_INTERVAL_FLOAT,
655 g_param_spec_float ("i-frame-interval-float", "I-frame interval",
656 "The frequency of I frames expressed in seconds between I frames (0 for automatic). "
657 "Fractional intervals work on Android >= 25",
658 0, G_MAXFLOAT, I_FRAME_INTERVAL_DEFAULT,
659 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
663 gst_amc_video_enc_init (GstAmcVideoEnc * self)
665 g_mutex_init (&self->codec_lock);
666 g_mutex_init (&self->drain_lock);
667 g_cond_init (&self->drain_cond);
669 self->bitrate = BIT_RATE_DEFAULT;
670 self->i_frame_int = I_FRAME_INTERVAL_DEFAULT;
674 gst_amc_video_enc_open (GstVideoEncoder * encoder)
676 GstAmcVideoEnc *self = GST_AMC_VIDEO_ENC (encoder);
677 GstAmcVideoEncClass *klass = GST_AMC_VIDEO_ENC_GET_CLASS (self);
680 GST_DEBUG_OBJECT (self, "Opening encoder");
682 g_mutex_lock (&self->codec_lock);
683 self->codec = gst_amc_codec_new (klass->codec_info->name, TRUE, &err);
685 g_mutex_unlock (&self->codec_lock);
686 GST_ELEMENT_ERROR_FROM_ERROR (self, err);
689 g_mutex_unlock (&self->codec_lock);
690 self->started = FALSE;
691 self->flushing = TRUE;
693 GST_DEBUG_OBJECT (self, "Opened encoder");
699 gst_amc_video_enc_close (GstVideoEncoder * encoder)
701 GstAmcVideoEnc *self = GST_AMC_VIDEO_ENC (encoder);
703 GST_DEBUG_OBJECT (self, "Closing encoder");
705 g_mutex_lock (&self->codec_lock);
709 gst_amc_codec_release (self->codec, &err);
711 GST_ELEMENT_WARNING_FROM_ERROR (self, err);
713 gst_amc_codec_free (self->codec);
716 g_mutex_unlock (&self->codec_lock);
718 self->started = FALSE;
719 self->flushing = TRUE;
721 GST_DEBUG_OBJECT (self, "Closed encoder");
727 gst_amc_video_enc_finalize (GObject * object)
729 GstAmcVideoEnc *self = GST_AMC_VIDEO_ENC (object);
731 g_mutex_clear (&self->codec_lock);
732 g_mutex_clear (&self->drain_lock);
733 g_cond_clear (&self->drain_cond);
735 G_OBJECT_CLASS (parent_class)->finalize (object);
738 static GstStateChangeReturn
739 gst_amc_video_enc_change_state (GstElement * element, GstStateChange transition)
741 GstAmcVideoEnc *self;
742 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
745 g_return_val_if_fail (GST_IS_AMC_VIDEO_ENC (element),
746 GST_STATE_CHANGE_FAILURE);
747 self = GST_AMC_VIDEO_ENC (element);
749 switch (transition) {
750 case GST_STATE_CHANGE_NULL_TO_READY:
752 case GST_STATE_CHANGE_READY_TO_PAUSED:
753 self->downstream_flow_ret = GST_FLOW_OK;
754 self->draining = FALSE;
755 self->started = FALSE;
757 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
759 case GST_STATE_CHANGE_PAUSED_TO_READY:
760 self->flushing = TRUE;
761 gst_amc_codec_flush (self->codec, &err);
763 GST_ELEMENT_WARNING_FROM_ERROR (self, err);
764 g_mutex_lock (&self->drain_lock);
765 self->draining = FALSE;
766 g_cond_broadcast (&self->drain_cond);
767 g_mutex_unlock (&self->drain_lock);
773 if (ret == GST_STATE_CHANGE_FAILURE)
776 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
778 if (ret == GST_STATE_CHANGE_FAILURE)
781 switch (transition) {
782 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
784 case GST_STATE_CHANGE_PAUSED_TO_READY:
785 self->downstream_flow_ret = GST_FLOW_FLUSHING;
786 self->started = FALSE;
788 case GST_STATE_CHANGE_READY_TO_NULL:
797 #define MAX_FRAME_DIST_TIME (5 * GST_SECOND)
798 #define MAX_FRAME_DIST_FRAMES (100)
800 static GstVideoCodecFrame *
801 _find_nearest_frame (GstAmcVideoEnc * self, GstClockTime reference_timestamp)
803 GList *l, *best_l = NULL;
804 GList *finish_frames = NULL;
805 GstVideoCodecFrame *best = NULL;
806 guint64 best_timestamp = 0;
807 guint64 best_diff = G_MAXUINT64;
808 BufferIdentification *best_id = NULL;
811 frames = gst_video_encoder_get_frames (GST_VIDEO_ENCODER (self));
813 for (l = frames; l; l = l->next) {
814 GstVideoCodecFrame *tmp = l->data;
815 BufferIdentification *id = gst_video_codec_frame_get_user_data (tmp);
816 guint64 timestamp, diff;
818 /* This happens for frames that were just added but
819 * which were not passed to the component yet. Ignore
825 timestamp = id->timestamp;
827 if (timestamp > reference_timestamp)
828 diff = timestamp - reference_timestamp;
830 diff = reference_timestamp - timestamp;
832 if (best == NULL || diff < best_diff) {
834 best_timestamp = timestamp;
839 /* For frames without timestamp we simply take the first frame */
840 if ((reference_timestamp == 0 && !GST_CLOCK_TIME_IS_VALID (timestamp))
847 for (l = frames; l && l != best_l; l = l->next) {
848 GstVideoCodecFrame *tmp = l->data;
849 BufferIdentification *id = gst_video_codec_frame_get_user_data (tmp);
850 guint64 diff_time, diff_frames;
852 if (id->timestamp > best_timestamp)
855 if (id->timestamp == 0 || best_timestamp == 0)
858 diff_time = best_timestamp - id->timestamp;
859 diff_frames = best->system_frame_number - tmp->system_frame_number;
861 if (diff_time > MAX_FRAME_DIST_TIME
862 || diff_frames > MAX_FRAME_DIST_FRAMES) {
864 g_list_prepend (finish_frames, gst_video_codec_frame_ref (tmp));
870 g_warning ("%s: Too old frames, bug in encoder -- please file a bug",
871 GST_ELEMENT_NAME (self));
872 for (l = finish_frames; l; l = l->next) {
873 gst_video_encoder_finish_frame (GST_VIDEO_ENCODER (self), l->data);
878 gst_video_codec_frame_ref (best);
880 g_list_foreach (frames, (GFunc) gst_video_codec_frame_unref, NULL);
881 g_list_free (frames);
887 gst_amc_video_enc_set_src_caps (GstAmcVideoEnc * self, GstAmcFormat * format)
890 GstVideoCodecState *output_state;
893 caps = caps_from_amc_format (format);
895 GST_ERROR_OBJECT (self, "Failed to create output caps");
899 /* It may not be proper to reference self->input_state here,
900 * because MediaCodec is an async model -- input_state may change multiple times,
901 * the passed-in MediaFormat may not be the one matched to the current input_state.
903 * Though, currently, the final src caps only calculate
904 * width/height/pixel-aspect-ratio/framerate/codec_data from self->input_state.
906 * If input width/height/codec_data change(is_format_change), it will restart
907 * MediaCodec, which means in these cases, self->input_state is matched.
909 output_state = gst_video_encoder_set_output_state (GST_VIDEO_ENCODER (self),
910 caps, self->input_state);
911 gst_video_codec_state_unref (output_state);
913 if (!gst_video_encoder_negotiate (GST_VIDEO_ENCODER (self)))
916 output_state = gst_video_encoder_get_output_state (GST_VIDEO_ENCODER (self));
917 s = gst_caps_get_structure (output_state->caps, 0);
919 if (!strcmp (gst_structure_get_name (s), "video/x-h264") ||
920 !strcmp (gst_structure_get_name (s), "video/x-h265")) {
921 self->codec_data_in_bytestream = TRUE;
923 self->codec_data_in_bytestream = FALSE;
925 gst_video_codec_state_unref (output_state);
930 /* The weird handling of cropping, alignment and everything is taken from
931 * platform/frameworks/media/libstagefright/colorconversion/ColorConversion.cpp
934 gst_amc_video_enc_fill_buffer (GstAmcVideoEnc * self, GstBuffer * inbuf,
935 GstAmcBuffer * outbuf, const GstAmcBufferInfo * buffer_info)
937 GstVideoCodecState *input_state = self->input_state;
938 /* The fill_buffer runs in the same thread as set_format?
939 * then we can use state->info safely */
940 GstVideoInfo *info = &input_state->info;
942 if (buffer_info->size < self->color_format_info.frame_size)
945 return gst_amc_color_format_copy (&self->color_format_info, outbuf,
946 buffer_info, info, inbuf, COLOR_FORMAT_COPY_IN);
950 gst_amc_video_enc_handle_output_frame (GstAmcVideoEnc * self,
951 GstAmcBuffer * buf, const GstAmcBufferInfo * buffer_info,
952 GstVideoCodecFrame * frame)
954 GstFlowReturn flow_ret = GST_FLOW_OK;
955 GstVideoEncoder *encoder = GST_VIDEO_ENCODER_CAST (self);
957 /* The BUFFER_FLAG_CODEC_CONFIG logic is borrowed from
958 * gst-omx. see *_handle_output_frame in
959 * gstomxvideoenc.c and gstomxh264enc.c */
960 if ((buffer_info->flags & BUFFER_FLAG_CODEC_CONFIG)
961 && buffer_info->size > 0) {
963 if (self->codec_data_in_bytestream) {
964 if (buffer_info->size > 4 &&
965 GST_READ_UINT32_BE (buf->data + buffer_info->offset) == 0x00000001) {
969 GST_DEBUG_OBJECT (self, "got codecconfig in byte-stream format");
971 hdrs = gst_buffer_new_and_alloc (buffer_info->size);
972 gst_buffer_fill (hdrs, 0, buf->data + buffer_info->offset,
974 GST_BUFFER_PTS (hdrs) =
975 gst_util_uint64_scale (buffer_info->presentation_time_us,
978 l = g_list_append (l, hdrs);
979 gst_video_encoder_set_headers (encoder, l);
982 GstBuffer *codec_data;
983 GstVideoCodecState *output_state =
984 gst_video_encoder_get_output_state (GST_VIDEO_ENCODER (self));
986 GST_DEBUG_OBJECT (self, "Handling codec data");
988 codec_data = gst_buffer_new_and_alloc (buffer_info->size);
989 gst_buffer_fill (codec_data, 0, buf->data + buffer_info->offset,
991 output_state->codec_data = codec_data;
992 gst_video_codec_state_unref (output_state);
994 if (!gst_video_encoder_negotiate (encoder)) {
995 gst_video_codec_frame_unref (frame);
996 return GST_FLOW_NOT_NEGOTIATED;
1003 if (buffer_info->size > 0) {
1007 if (buffer_info->flags & BUFFER_FLAG_PARTIAL_FRAME) {
1008 GST_FIXME_OBJECT (self, "partial frames are currently not handled");
1011 srcpad = GST_VIDEO_ENCODER_SRC_PAD (encoder);
1013 gst_video_encoder_allocate_output_buffer (encoder, buffer_info->size);
1014 gst_buffer_fill (out_buf, 0, buf->data + buffer_info->offset,
1017 GST_BUFFER_PTS (out_buf) =
1018 gst_util_uint64_scale (buffer_info->presentation_time_us, GST_USECOND,
1022 frame->output_buffer = out_buf;
1023 flow_ret = gst_video_encoder_finish_frame (encoder, frame);
1025 /* This sometimes happens at EOS or if the input is not properly framed,
1026 * let's handle it gracefully by allocating a new buffer for the current
1027 * caps and filling it
1030 GST_ERROR_OBJECT (self, "No corresponding frame found");
1031 flow_ret = gst_pad_push (srcpad, out_buf);
1034 flow_ret = gst_video_encoder_finish_frame (encoder, frame);
1041 gst_amc_video_enc_loop (GstAmcVideoEnc * self)
1043 GstVideoCodecFrame *frame;
1044 GstFlowReturn flow_ret = GST_FLOW_OK;
1046 GstAmcBufferInfo buffer_info;
1051 GST_VIDEO_ENCODER_STREAM_LOCK (self);
1054 GST_DEBUG_OBJECT (self, "Waiting for available output buffer");
1055 GST_VIDEO_ENCODER_STREAM_UNLOCK (self);
1056 /* Wait at most 100ms here, some codecs don't fail dequeueing if
1057 * the codec is flushing, causing deadlocks during shutdown */
1059 gst_amc_codec_dequeue_output_buffer (self->codec, &buffer_info, 100000,
1061 GST_VIDEO_ENCODER_STREAM_LOCK (self);
1064 if (idx < 0 || self->amc_format) {
1065 if (self->flushing) {
1066 g_clear_error (&err);
1070 /* The comments from https://android.googlesource.com/platform/cts/+/android-4.3_r3.1/tests/tests/media/src/android/media/cts/EncodeDecodeTest.java
1071 * line 539 says INFO_OUTPUT_FORMAT_CHANGED is not expected for an encoder
1073 if (self->amc_format || idx == INFO_OUTPUT_FORMAT_CHANGED) {
1074 GstAmcFormat *format;
1075 gchar *format_string;
1077 GST_DEBUG_OBJECT (self, "Output format has changed");
1079 format = (idx == INFO_OUTPUT_FORMAT_CHANGED) ?
1080 gst_amc_codec_get_output_format (self->codec,
1081 &err) : self->amc_format;
1083 format = self->amc_format;
1084 GST_ELEMENT_WARNING_FROM_ERROR (self, err);
1087 if (self->amc_format) {
1088 if (format != self->amc_format)
1089 gst_amc_format_free (self->amc_format);
1090 self->amc_format = NULL;
1096 format_string = gst_amc_format_to_string (format, &err);
1098 gst_amc_format_free (format);
1101 GST_DEBUG_OBJECT (self, "Got new output format: %s", format_string);
1102 g_free (format_string);
1104 if (!gst_amc_video_enc_set_src_caps (self, format)) {
1105 gst_amc_format_free (format);
1109 gst_amc_format_free (format);
1112 goto process_buffer;
1118 case INFO_OUTPUT_BUFFERS_CHANGED:
1119 /* Handled internally */
1120 g_assert_not_reached ();
1122 case INFO_TRY_AGAIN_LATER:
1123 GST_DEBUG_OBJECT (self, "Dequeueing output buffer timed out");
1127 GST_ERROR_OBJECT (self, "Failure dequeueing input buffer");
1131 g_assert_not_reached ();
1139 GST_DEBUG_OBJECT (self,
1140 "Got output buffer at index %d: size %d time %" G_GINT64_FORMAT
1141 " flags 0x%08x", idx, buffer_info.size, buffer_info.presentation_time_us,
1144 buf = gst_amc_codec_get_output_buffer (self->codec, idx, &err);
1146 if (self->flushing) {
1147 g_clear_error (&err);
1150 goto failed_to_get_output_buffer;
1152 goto got_null_output_buffer;
1156 _find_nearest_frame (self,
1157 gst_util_uint64_scale (buffer_info.presentation_time_us, GST_USECOND, 1));
1159 is_eos = ! !(buffer_info.flags & BUFFER_FLAG_END_OF_STREAM);
1162 gst_amc_video_enc_handle_output_frame (self, buf, &buffer_info, frame);
1164 gst_amc_buffer_free (buf);
1167 if (!gst_amc_codec_release_output_buffer (self->codec, idx, FALSE, &err)) {
1168 if (self->flushing) {
1169 g_clear_error (&err);
1172 goto failed_release;
1175 if (is_eos || flow_ret == GST_FLOW_EOS) {
1176 GST_VIDEO_ENCODER_STREAM_UNLOCK (self);
1177 g_mutex_lock (&self->drain_lock);
1178 if (self->draining) {
1179 GST_DEBUG_OBJECT (self, "Drained");
1180 self->draining = FALSE;
1181 g_cond_broadcast (&self->drain_cond);
1182 } else if (flow_ret == GST_FLOW_OK) {
1183 GST_DEBUG_OBJECT (self, "Component signalled EOS");
1184 flow_ret = GST_FLOW_EOS;
1186 g_mutex_unlock (&self->drain_lock);
1187 GST_VIDEO_ENCODER_STREAM_LOCK (self);
1189 GST_DEBUG_OBJECT (self, "Finished frame: %s", gst_flow_get_name (flow_ret));
1192 self->downstream_flow_ret = flow_ret;
1194 if (flow_ret != GST_FLOW_OK)
1197 GST_VIDEO_ENCODER_STREAM_UNLOCK (self);
1203 GST_ELEMENT_ERROR_FROM_ERROR (self, err);
1204 gst_pad_push_event (GST_VIDEO_ENCODER_SRC_PAD (self), gst_event_new_eos ());
1205 gst_pad_pause_task (GST_VIDEO_ENCODER_SRC_PAD (self));
1206 self->downstream_flow_ret = GST_FLOW_ERROR;
1207 GST_VIDEO_ENCODER_STREAM_UNLOCK (self);
1208 g_mutex_lock (&self->drain_lock);
1209 self->draining = FALSE;
1210 g_cond_broadcast (&self->drain_cond);
1211 g_mutex_unlock (&self->drain_lock);
1218 GST_ELEMENT_ERROR_FROM_ERROR (self, err);
1220 GST_ELEMENT_ERROR (self, LIBRARY, FAILED, (NULL),
1221 ("Failed to handle format"));
1222 gst_pad_push_event (GST_VIDEO_ENCODER_SRC_PAD (self), gst_event_new_eos ());
1223 gst_pad_pause_task (GST_VIDEO_ENCODER_SRC_PAD (self));
1224 self->downstream_flow_ret = GST_FLOW_ERROR;
1225 GST_VIDEO_ENCODER_STREAM_UNLOCK (self);
1226 g_mutex_lock (&self->drain_lock);
1227 self->draining = FALSE;
1228 g_cond_broadcast (&self->drain_cond);
1229 g_mutex_unlock (&self->drain_lock);
1234 GST_ELEMENT_ERROR_FROM_ERROR (self, err);
1235 gst_pad_push_event (GST_VIDEO_ENCODER_SRC_PAD (self), gst_event_new_eos ());
1236 gst_pad_pause_task (GST_VIDEO_ENCODER_SRC_PAD (self));
1237 self->downstream_flow_ret = GST_FLOW_ERROR;
1238 GST_VIDEO_ENCODER_STREAM_UNLOCK (self);
1239 g_mutex_lock (&self->drain_lock);
1240 self->draining = FALSE;
1241 g_cond_broadcast (&self->drain_cond);
1242 g_mutex_unlock (&self->drain_lock);
1247 GST_DEBUG_OBJECT (self, "Flushing -- stopping task");
1248 gst_pad_pause_task (GST_VIDEO_ENCODER_SRC_PAD (self));
1249 self->downstream_flow_ret = GST_FLOW_FLUSHING;
1250 GST_VIDEO_ENCODER_STREAM_UNLOCK (self);
1256 if (flow_ret == GST_FLOW_EOS) {
1257 GST_DEBUG_OBJECT (self, "EOS");
1258 gst_pad_push_event (GST_VIDEO_ENCODER_SRC_PAD (self),
1259 gst_event_new_eos ());
1260 gst_pad_pause_task (GST_VIDEO_ENCODER_SRC_PAD (self));
1261 } else if (flow_ret == GST_FLOW_NOT_LINKED || flow_ret < GST_FLOW_EOS) {
1262 GST_ELEMENT_FLOW_ERROR (self, flow_ret);
1263 gst_pad_push_event (GST_VIDEO_ENCODER_SRC_PAD (self),
1264 gst_event_new_eos ());
1265 gst_pad_pause_task (GST_VIDEO_ENCODER_SRC_PAD (self));
1267 GST_VIDEO_ENCODER_STREAM_UNLOCK (self);
1268 g_mutex_lock (&self->drain_lock);
1269 self->draining = FALSE;
1270 g_cond_broadcast (&self->drain_cond);
1271 g_mutex_unlock (&self->drain_lock);
1275 failed_to_get_output_buffer:
1277 GST_ELEMENT_ERROR_FROM_ERROR (self, err);
1278 gst_pad_push_event (GST_VIDEO_ENCODER_SRC_PAD (self), gst_event_new_eos ());
1279 gst_pad_pause_task (GST_VIDEO_ENCODER_SRC_PAD (self));
1280 self->downstream_flow_ret = GST_FLOW_ERROR;
1281 GST_VIDEO_ENCODER_STREAM_UNLOCK (self);
1282 g_mutex_lock (&self->drain_lock);
1283 self->draining = FALSE;
1284 g_cond_broadcast (&self->drain_cond);
1285 g_mutex_unlock (&self->drain_lock);
1289 got_null_output_buffer:
1291 GST_ELEMENT_ERROR (self, LIBRARY, SETTINGS, (NULL),
1292 ("Got no output buffer"));
1293 gst_pad_push_event (GST_VIDEO_ENCODER_SRC_PAD (self), gst_event_new_eos ());
1294 gst_pad_pause_task (GST_VIDEO_ENCODER_SRC_PAD (self));
1295 self->downstream_flow_ret = GST_FLOW_ERROR;
1296 GST_VIDEO_ENCODER_STREAM_UNLOCK (self);
1297 g_mutex_lock (&self->drain_lock);
1298 self->draining = FALSE;
1299 g_cond_broadcast (&self->drain_cond);
1300 g_mutex_unlock (&self->drain_lock);
1306 gst_amc_video_enc_start (GstVideoEncoder * encoder)
1308 GstAmcVideoEnc *self;
1310 self = GST_AMC_VIDEO_ENC (encoder);
1311 self->last_upstream_ts = 0;
1312 self->drained = TRUE;
1313 self->downstream_flow_ret = GST_FLOW_OK;
1314 self->started = FALSE;
1315 self->flushing = TRUE;
1321 gst_amc_video_enc_stop (GstVideoEncoder * encoder)
1323 GstAmcVideoEnc *self;
1326 self = GST_AMC_VIDEO_ENC (encoder);
1327 GST_DEBUG_OBJECT (self, "Stopping encoder");
1328 self->flushing = TRUE;
1329 if (self->started) {
1330 gst_amc_codec_flush (self->codec, &err);
1332 GST_ELEMENT_WARNING_FROM_ERROR (self, err);
1333 gst_amc_codec_stop (self->codec, &err);
1335 GST_ELEMENT_WARNING_FROM_ERROR (self, err);
1336 self->started = FALSE;
1338 gst_pad_stop_task (GST_VIDEO_ENCODER_SRC_PAD (encoder));
1340 self->downstream_flow_ret = GST_FLOW_FLUSHING;
1341 self->drained = TRUE;
1342 g_mutex_lock (&self->drain_lock);
1343 self->draining = FALSE;
1344 g_cond_broadcast (&self->drain_cond);
1345 g_mutex_unlock (&self->drain_lock);
1346 if (self->input_state)
1347 gst_video_codec_state_unref (self->input_state);
1348 self->input_state = NULL;
1350 if (self->amc_format) {
1351 gst_amc_format_free (self->amc_format);
1352 self->amc_format = NULL;
1355 GST_DEBUG_OBJECT (self, "Stopped encoder");
1360 gst_amc_video_enc_set_format (GstVideoEncoder * encoder,
1361 GstVideoCodecState * state)
1363 GstAmcVideoEnc *self;
1364 GstAmcFormat *format = NULL;
1365 GstCaps *allowed_caps = NULL;
1366 gboolean is_format_change = FALSE;
1367 gboolean needs_disable = FALSE;
1368 gchar *format_string;
1372 self = GST_AMC_VIDEO_ENC (encoder);
1374 GST_DEBUG_OBJECT (self, "Setting new caps %" GST_PTR_FORMAT, state->caps);
1376 /* Check if the caps change is a real format change or if only irrelevant
1377 * parts of the caps have changed or nothing at all.
1379 is_format_change |= self->color_format_info.width != state->info.width;
1380 is_format_change |= self->color_format_info.height != state->info.height;
1381 needs_disable = self->started;
1383 /* If the component is not started and a real format change happens
1384 * we have to restart the component. If no real format change
1385 * happened we can just exit here.
1387 if (needs_disable && !is_format_change) {
1389 /* Framerate or something minor changed */
1390 if (self->input_state)
1391 gst_video_codec_state_unref (self->input_state);
1392 self->input_state = gst_video_codec_state_ref (state);
1393 GST_DEBUG_OBJECT (self,
1394 "Already running and caps did not change the format");
1398 if (needs_disable && is_format_change) {
1399 gst_amc_video_enc_drain (self);
1400 GST_VIDEO_ENCODER_STREAM_UNLOCK (self);
1401 gst_amc_video_enc_stop (GST_VIDEO_ENCODER (self));
1402 GST_VIDEO_ENCODER_STREAM_LOCK (self);
1403 gst_amc_video_enc_close (GST_VIDEO_ENCODER (self));
1404 if (!gst_amc_video_enc_open (GST_VIDEO_ENCODER (self))) {
1405 GST_ERROR_OBJECT (self, "Failed to open codec again");
1409 if (!gst_amc_video_enc_start (GST_VIDEO_ENCODER (self))) {
1410 GST_ERROR_OBJECT (self, "Failed to start codec again");
1413 /* srcpad task is not running at this point */
1414 if (self->input_state)
1415 gst_video_codec_state_unref (self->input_state);
1416 self->input_state = NULL;
1418 GST_DEBUG_OBJECT (self, "picking an output format ...");
1419 allowed_caps = gst_pad_get_allowed_caps (GST_VIDEO_ENCODER_SRC_PAD (encoder));
1420 if (!allowed_caps) {
1421 GST_DEBUG_OBJECT (self, "... but no peer, using template caps");
1423 gst_pad_get_pad_template_caps (GST_VIDEO_ENCODER_SRC_PAD (encoder));
1425 GST_DEBUG_OBJECT (self, "chose caps %" GST_PTR_FORMAT, allowed_caps);
1426 allowed_caps = gst_caps_truncate (allowed_caps);
1428 format = create_amc_format (self, state, allowed_caps);
1432 format_string = gst_amc_format_to_string (format, &err);
1434 GST_ELEMENT_WARNING_FROM_ERROR (self, err);
1435 GST_DEBUG_OBJECT (self, "Configuring codec with format: %s",
1436 GST_STR_NULL (format_string));
1437 g_free (format_string);
1439 if (!gst_amc_codec_configure (self->codec, format, NULL, &err)) {
1440 GST_ERROR_OBJECT (self, "Failed to configure codec");
1441 GST_ELEMENT_ERROR_FROM_ERROR (self, err);
1445 if (!gst_amc_codec_start (self->codec, &err)) {
1446 GST_ERROR_OBJECT (self, "Failed to start codec");
1447 GST_ELEMENT_ERROR_FROM_ERROR (self, err);
1451 self->amc_format = format;
1454 self->input_state = gst_video_codec_state_ref (state);
1456 self->started = TRUE;
1458 /* Start the srcpad loop again */
1459 self->flushing = FALSE;
1460 self->downstream_flow_ret = GST_FLOW_OK;
1461 gst_pad_start_task (GST_VIDEO_ENCODER_SRC_PAD (self),
1462 (GstTaskFunction) gst_amc_video_enc_loop, encoder, NULL);
1468 gst_caps_unref (allowed_caps);
1471 gst_amc_format_free (format);
1477 gst_amc_video_enc_flush (GstVideoEncoder * encoder)
1479 GstAmcVideoEnc *self;
1482 self = GST_AMC_VIDEO_ENC (encoder);
1484 GST_DEBUG_OBJECT (self, "Flushing encoder");
1486 if (!self->started) {
1487 GST_DEBUG_OBJECT (self, "Codec not started yet");
1491 self->flushing = TRUE;
1492 gst_amc_codec_flush (self->codec, &err);
1494 GST_ELEMENT_WARNING_FROM_ERROR (self, err);
1496 /* Wait until the srcpad loop is finished,
1497 * unlock GST_VIDEO_ENCODER_STREAM_LOCK to prevent deadlocks
1498 * caused by using this lock from inside the loop function */
1499 GST_VIDEO_ENCODER_STREAM_UNLOCK (self);
1500 GST_PAD_STREAM_LOCK (GST_VIDEO_ENCODER_SRC_PAD (self));
1501 GST_PAD_STREAM_UNLOCK (GST_VIDEO_ENCODER_SRC_PAD (self));
1502 GST_VIDEO_ENCODER_STREAM_LOCK (self);
1503 self->flushing = FALSE;
1505 /* Start the srcpad loop again */
1506 self->last_upstream_ts = 0;
1507 self->drained = TRUE;
1508 self->downstream_flow_ret = GST_FLOW_OK;
1509 gst_pad_start_task (GST_VIDEO_ENCODER_SRC_PAD (self),
1510 (GstTaskFunction) gst_amc_video_enc_loop, encoder, NULL);
1512 GST_DEBUG_OBJECT (self, "Flush encoder");
1517 static GstFlowReturn
1518 gst_amc_video_enc_handle_frame (GstVideoEncoder * encoder,
1519 GstVideoCodecFrame * frame)
1521 GstAmcVideoEnc *self;
1524 GstAmcBufferInfo buffer_info;
1525 GstClockTime timestamp, duration, timestamp_offset = 0;
1526 BufferIdentification *id;
1529 self = GST_AMC_VIDEO_ENC (encoder);
1531 GST_DEBUG_OBJECT (self, "Handling frame");
1533 if (!self->started) {
1534 GST_ERROR_OBJECT (self, "Codec not started yet");
1535 gst_video_codec_frame_unref (frame);
1536 return GST_FLOW_NOT_NEGOTIATED;
1542 if (self->downstream_flow_ret != GST_FLOW_OK)
1543 goto downstream_error;
1545 timestamp = frame->pts;
1546 duration = frame->duration;
1548 if (GST_VIDEO_CODEC_FRAME_IS_FORCE_KEYFRAME (frame)) {
1549 if (gst_amc_codec_request_key_frame (self->codec, &err)) {
1550 GST_DEBUG_OBJECT (self, "Passed keyframe request to MediaCodec");
1553 GST_ELEMENT_WARNING_FROM_ERROR (self, err);
1554 g_clear_error (&err);
1559 /* Make sure to release the base class stream lock, otherwise
1560 * _loop() can't call _finish_frame() and we might block forever
1561 * because no input buffers are released */
1562 GST_VIDEO_ENCODER_STREAM_UNLOCK (self);
1563 /* Wait at most 100ms here, some codecs don't fail dequeueing if
1564 * the codec is flushing, causing deadlocks during shutdown */
1565 idx = gst_amc_codec_dequeue_input_buffer (self->codec, 100000, &err);
1566 GST_VIDEO_ENCODER_STREAM_LOCK (self);
1569 if (self->flushing || self->downstream_flow_ret == GST_FLOW_FLUSHING) {
1570 g_clear_error (&err);
1575 case INFO_TRY_AGAIN_LATER:
1576 GST_DEBUG_OBJECT (self, "Dequeueing input buffer timed out");
1577 goto again; /* next try */
1580 GST_ERROR_OBJECT (self, "Failed to dequeue input buffer");
1583 g_assert_not_reached ();
1590 if (self->flushing) {
1591 memset (&buffer_info, 0, sizeof (buffer_info));
1592 gst_amc_codec_queue_input_buffer (self->codec, idx, &buffer_info, NULL);
1596 if (self->downstream_flow_ret != GST_FLOW_OK) {
1597 memset (&buffer_info, 0, sizeof (buffer_info));
1598 gst_amc_codec_queue_input_buffer (self->codec, idx, &buffer_info, &err);
1599 if (err && !self->flushing)
1600 GST_ELEMENT_WARNING_FROM_ERROR (self, err);
1601 g_clear_error (&err);
1602 goto downstream_error;
1605 /* Now handle the frame */
1607 /* Copy the buffer content in chunks of size as requested
1609 buf = gst_amc_codec_get_input_buffer (self->codec, idx, &err);
1611 goto failed_to_get_input_buffer;
1613 goto got_null_input_buffer;
1615 memset (&buffer_info, 0, sizeof (buffer_info));
1616 buffer_info.offset = 0;
1617 buffer_info.size = MIN (self->color_format_info.frame_size, buf->size);
1618 gst_amc_buffer_set_position_and_limit (buf, NULL, buffer_info.offset,
1621 if (!gst_amc_video_enc_fill_buffer (self, frame->input_buffer, buf,
1623 memset (&buffer_info, 0, sizeof (buffer_info));
1624 gst_amc_codec_queue_input_buffer (self->codec, idx, &buffer_info, &err);
1625 if (err && !self->flushing)
1626 GST_ELEMENT_WARNING_FROM_ERROR (self, err);
1627 g_clear_error (&err);
1628 gst_amc_buffer_free (buf);
1630 goto buffer_fill_error;
1633 gst_amc_buffer_free (buf);
1636 if (timestamp != GST_CLOCK_TIME_NONE) {
1637 buffer_info.presentation_time_us =
1638 gst_util_uint64_scale (timestamp + timestamp_offset, 1, GST_USECOND);
1639 self->last_upstream_ts = timestamp + timestamp_offset;
1641 if (duration != GST_CLOCK_TIME_NONE)
1642 self->last_upstream_ts += duration;
1644 id = buffer_identification_new (timestamp + timestamp_offset);
1645 if (GST_VIDEO_CODEC_FRAME_IS_SYNC_POINT (frame))
1646 buffer_info.flags |= BUFFER_FLAG_SYNC_FRAME;
1647 gst_video_codec_frame_set_user_data (frame, id,
1648 (GDestroyNotify) buffer_identification_free);
1650 GST_DEBUG_OBJECT (self,
1651 "Queueing buffer %d: size %d time %" G_GINT64_FORMAT " flags 0x%08x",
1652 idx, buffer_info.size, buffer_info.presentation_time_us,
1654 if (!gst_amc_codec_queue_input_buffer (self->codec, idx, &buffer_info, &err)) {
1655 if (self->flushing) {
1656 g_clear_error (&err);
1662 self->drained = FALSE;
1664 gst_video_codec_frame_unref (frame);
1666 return self->downstream_flow_ret;
1670 GST_ERROR_OBJECT (self, "Downstream returned %s",
1671 gst_flow_get_name (self->downstream_flow_ret));
1673 gst_video_codec_frame_unref (frame);
1674 return self->downstream_flow_ret;
1676 failed_to_get_input_buffer:
1678 GST_ELEMENT_ERROR_FROM_ERROR (self, err);
1679 gst_video_codec_frame_unref (frame);
1680 return GST_FLOW_ERROR;
1682 got_null_input_buffer:
1684 GST_ELEMENT_ERROR (self, LIBRARY, SETTINGS, (NULL),
1685 ("Got no input buffer"));
1686 gst_video_codec_frame_unref (frame);
1687 return GST_FLOW_ERROR;
1691 GST_ELEMENT_ERROR (self, RESOURCE, WRITE, (NULL),
1692 ("Failed to write input into the amc buffer(write %dB to a %"
1693 G_GSIZE_FORMAT "B buffer)", self->color_format_info.frame_size,
1695 gst_video_codec_frame_unref (frame);
1696 return GST_FLOW_ERROR;
1700 GST_ELEMENT_ERROR_FROM_ERROR (self, err);
1701 gst_video_codec_frame_unref (frame);
1702 return GST_FLOW_ERROR;
1706 GST_ELEMENT_ERROR_FROM_ERROR (self, err);
1707 gst_video_codec_frame_unref (frame);
1708 return GST_FLOW_ERROR;
1712 GST_DEBUG_OBJECT (self, "Flushing -- returning FLUSHING");
1713 gst_video_codec_frame_unref (frame);
1714 return GST_FLOW_FLUSHING;
1718 static GstFlowReturn
1719 gst_amc_video_enc_finish (GstVideoEncoder * encoder)
1721 GstAmcVideoEnc *self;
1723 self = GST_AMC_VIDEO_ENC (encoder);
1725 return gst_amc_video_enc_drain (self);
1728 static GstFlowReturn
1729 gst_amc_video_enc_drain (GstAmcVideoEnc * self)
1735 GST_DEBUG_OBJECT (self, "Draining codec");
1736 if (!self->started) {
1737 GST_DEBUG_OBJECT (self, "Codec not started yet");
1741 /* Don't send drain buffer twice, this doesn't work */
1742 if (self->drained) {
1743 GST_DEBUG_OBJECT (self, "Codec is drained already");
1747 /* Make sure to release the base class stream lock, otherwise
1748 * _loop() can't call _finish_frame() and we might block forever
1749 * because no input buffers are released */
1750 GST_VIDEO_ENCODER_STREAM_UNLOCK (self);
1751 /* Send an EOS buffer to the component and let the base
1752 * class drop the EOS event. We will send it later when
1753 * the EOS buffer arrives on the output port.
1754 * Wait at most 0.5s here. */
1755 idx = gst_amc_codec_dequeue_input_buffer (self->codec, 500000, &err);
1756 GST_VIDEO_ENCODER_STREAM_LOCK (self);
1760 GstAmcBufferInfo buffer_info;
1762 buf = gst_amc_codec_get_input_buffer (self->codec, idx, &err);
1764 GST_VIDEO_ENCODER_STREAM_UNLOCK (self);
1765 g_mutex_lock (&self->drain_lock);
1766 self->draining = TRUE;
1768 memset (&buffer_info, 0, sizeof (buffer_info));
1769 buffer_info.size = 0;
1770 buffer_info.presentation_time_us =
1771 gst_util_uint64_scale (self->last_upstream_ts, 1, GST_USECOND);
1772 buffer_info.flags |= BUFFER_FLAG_END_OF_STREAM;
1774 gst_amc_buffer_set_position_and_limit (buf, NULL, 0, 0);
1775 gst_amc_buffer_free (buf);
1778 if (gst_amc_codec_queue_input_buffer (self->codec, idx, &buffer_info,
1780 GST_DEBUG_OBJECT (self, "Waiting until codec is drained");
1781 g_cond_wait (&self->drain_cond, &self->drain_lock);
1782 GST_DEBUG_OBJECT (self, "Drained codec");
1785 GST_ERROR_OBJECT (self, "Failed to queue input buffer");
1786 if (self->flushing) {
1787 g_clear_error (&err);
1788 ret = GST_FLOW_FLUSHING;
1790 GST_ELEMENT_WARNING_FROM_ERROR (self, err);
1791 ret = GST_FLOW_ERROR;
1795 self->drained = TRUE;
1796 self->draining = FALSE;
1797 g_mutex_unlock (&self->drain_lock);
1798 GST_VIDEO_ENCODER_STREAM_LOCK (self);
1800 GST_ERROR_OBJECT (self, "Failed to get buffer for EOS: %d", idx);
1802 GST_ELEMENT_WARNING_FROM_ERROR (self, err);
1803 ret = GST_FLOW_ERROR;
1806 GST_ERROR_OBJECT (self, "Failed to acquire buffer for EOS: %d", idx);
1808 GST_ELEMENT_WARNING_FROM_ERROR (self, err);
1809 ret = GST_FLOW_ERROR;