2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 * Copyright (C) <2012> Collabora Ltd.
4 * Author: Sebastian Dröge <sebastian.droege@collabora.co.uk>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
29 #include <libavcodec/avcodec.h>
30 #include <libavutil/channel_layout.h>
33 #include <gst/base/gstbytewriter.h>
36 #include "gstavcodecmap.h"
37 #include "gstavutils.h"
38 #include "gstavauddec.h"
40 GST_DEBUG_CATEGORY_STATIC (GST_CAT_PERFORMANCE);
42 /* A number of function prototypes are given so we can refer to them later. */
43 static void gst_ffmpegauddec_base_init (GstFFMpegAudDecClass * klass);
44 static void gst_ffmpegauddec_class_init (GstFFMpegAudDecClass * klass);
45 static void gst_ffmpegauddec_init (GstFFMpegAudDec * ffmpegdec);
46 static void gst_ffmpegauddec_finalize (GObject * object);
47 static gboolean gst_ffmpegauddec_propose_allocation (GstAudioDecoder * decoder,
50 static gboolean gst_ffmpegauddec_start (GstAudioDecoder * decoder);
51 static gboolean gst_ffmpegauddec_stop (GstAudioDecoder * decoder);
52 static void gst_ffmpegauddec_flush (GstAudioDecoder * decoder, gboolean hard);
53 static gboolean gst_ffmpegauddec_set_format (GstAudioDecoder * decoder,
55 static GstFlowReturn gst_ffmpegauddec_handle_frame (GstAudioDecoder * decoder,
58 static gboolean gst_ffmpegauddec_negotiate (GstFFMpegAudDec * ffmpegdec,
59 AVCodecContext * context, AVFrame * frame, gboolean force);
61 static GstFlowReturn gst_ffmpegauddec_drain (GstFFMpegAudDec * ffmpegdec,
64 #define GST_FFDEC_PARAMS_QDATA g_quark_from_static_string("avdec-params")
66 static GstElementClass *parent_class = NULL;
69 gst_ffmpegauddec_base_init (GstFFMpegAudDecClass * klass)
71 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
72 GstPadTemplate *sinktempl, *srctempl;
73 GstCaps *sinkcaps, *srccaps;
75 gchar *longname, *description;
78 (AVCodec *) g_type_get_qdata (G_OBJECT_CLASS_TYPE (klass),
79 GST_FFDEC_PARAMS_QDATA);
80 g_assert (in_plugin != NULL);
82 /* construct the element details struct */
83 longname = g_strdup_printf ("libav %s decoder", in_plugin->long_name);
84 description = g_strdup_printf ("libav %s decoder", in_plugin->name);
85 gst_element_class_set_metadata (element_class, longname,
86 "Codec/Decoder/Audio", description,
87 "Wim Taymans <wim.taymans@gmail.com>, "
88 "Ronald Bultje <rbultje@ronald.bitfreak.net>, "
89 "Edward Hervey <bilboed@bilboed.com>");
94 sinkcaps = gst_ffmpeg_codecid_to_caps (in_plugin->id, NULL, FALSE);
96 GST_DEBUG ("Couldn't get sink caps for decoder '%s'", in_plugin->name);
97 sinkcaps = gst_caps_from_string ("unknown/unknown");
99 srccaps = gst_ffmpeg_codectype_to_audio_caps (NULL,
100 in_plugin->id, FALSE, in_plugin);
102 GST_DEBUG ("Couldn't get source caps for decoder '%s'", in_plugin->name);
103 srccaps = gst_caps_from_string ("audio/x-raw");
107 sinktempl = gst_pad_template_new ("sink", GST_PAD_SINK,
108 GST_PAD_ALWAYS, sinkcaps);
109 srctempl = gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS, srccaps);
111 gst_element_class_add_pad_template (element_class, srctempl);
112 gst_element_class_add_pad_template (element_class, sinktempl);
114 gst_caps_unref (sinkcaps);
115 gst_caps_unref (srccaps);
117 klass->in_plugin = in_plugin;
118 klass->srctempl = srctempl;
119 klass->sinktempl = sinktempl;
123 gst_ffmpegauddec_class_init (GstFFMpegAudDecClass * klass)
125 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
126 GstAudioDecoderClass *gstaudiodecoder_class = GST_AUDIO_DECODER_CLASS (klass);
128 parent_class = g_type_class_peek_parent (klass);
130 gobject_class->finalize = gst_ffmpegauddec_finalize;
132 gstaudiodecoder_class->start = GST_DEBUG_FUNCPTR (gst_ffmpegauddec_start);
133 gstaudiodecoder_class->stop = GST_DEBUG_FUNCPTR (gst_ffmpegauddec_stop);
134 gstaudiodecoder_class->set_format =
135 GST_DEBUG_FUNCPTR (gst_ffmpegauddec_set_format);
136 gstaudiodecoder_class->handle_frame =
137 GST_DEBUG_FUNCPTR (gst_ffmpegauddec_handle_frame);
138 gstaudiodecoder_class->flush = GST_DEBUG_FUNCPTR (gst_ffmpegauddec_flush);
139 gstaudiodecoder_class->propose_allocation =
140 GST_DEBUG_FUNCPTR (gst_ffmpegauddec_propose_allocation);
142 GST_DEBUG_CATEGORY_GET (GST_CAT_PERFORMANCE, "GST_PERFORMANCE");
146 gst_ffmpegauddec_init (GstFFMpegAudDec * ffmpegdec)
148 GstFFMpegAudDecClass *klass =
149 (GstFFMpegAudDecClass *) G_OBJECT_GET_CLASS (ffmpegdec);
151 /* some ffmpeg data */
152 ffmpegdec->context = avcodec_alloc_context3 (klass->in_plugin);
153 ffmpegdec->context->opaque = ffmpegdec;
154 ffmpegdec->opened = FALSE;
156 ffmpegdec->frame = av_frame_alloc ();
158 GST_PAD_SET_ACCEPT_TEMPLATE (GST_AUDIO_DECODER_SINK_PAD (ffmpegdec));
159 gst_audio_decoder_set_use_default_pad_acceptcaps (GST_AUDIO_DECODER_CAST
162 gst_audio_decoder_set_drainable (GST_AUDIO_DECODER (ffmpegdec), TRUE);
163 gst_audio_decoder_set_needs_format (GST_AUDIO_DECODER (ffmpegdec), TRUE);
167 gst_ffmpegauddec_finalize (GObject * object)
169 GstFFMpegAudDec *ffmpegdec = (GstFFMpegAudDec *) object;
171 av_frame_free (&ffmpegdec->frame);
172 avcodec_free_context (&ffmpegdec->context);
174 G_OBJECT_CLASS (parent_class)->finalize (object);
179 gst_ffmpegauddec_close (GstFFMpegAudDec * ffmpegdec, gboolean reset)
181 GstFFMpegAudDecClass *oclass;
183 oclass = (GstFFMpegAudDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
185 GST_LOG_OBJECT (ffmpegdec, "closing libav codec");
187 gst_caps_replace (&ffmpegdec->last_caps, NULL);
189 gst_ffmpeg_avcodec_close (ffmpegdec->context);
190 ffmpegdec->opened = FALSE;
192 av_freep (&ffmpegdec->context->extradata);
195 avcodec_free_context (&ffmpegdec->context);
196 ffmpegdec->context = avcodec_alloc_context3 (oclass->in_plugin);
197 if (ffmpegdec->context == NULL) {
198 GST_DEBUG_OBJECT (ffmpegdec, "Failed to set context defaults");
201 ffmpegdec->context->opaque = ffmpegdec;
208 gst_ffmpegauddec_start (GstAudioDecoder * decoder)
210 GstFFMpegAudDec *ffmpegdec = (GstFFMpegAudDec *) decoder;
211 GstFFMpegAudDecClass *oclass;
213 oclass = (GstFFMpegAudDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
215 GST_OBJECT_LOCK (ffmpegdec);
216 avcodec_free_context (&ffmpegdec->context);
217 ffmpegdec->context = avcodec_alloc_context3 (oclass->in_plugin);
218 if (ffmpegdec->context == NULL) {
219 GST_DEBUG_OBJECT (ffmpegdec, "Failed to set context defaults");
220 GST_OBJECT_UNLOCK (ffmpegdec);
223 ffmpegdec->context->opaque = ffmpegdec;
225 /* FIXME: https://gitlab.freedesktop.org/gstreamer/gstreamer/-/issues/1474 */
226 if ((oclass->in_plugin->capabilities & AV_CODEC_CAP_DELAY) != 0
227 && (oclass->in_plugin->id == AV_CODEC_ID_WMAV1
228 || oclass->in_plugin->id == AV_CODEC_ID_WMAV2)) {
229 ffmpegdec->context->flags2 |= AV_CODEC_FLAG2_SKIP_MANUAL;
232 GST_OBJECT_UNLOCK (ffmpegdec);
238 gst_ffmpegauddec_stop (GstAudioDecoder * decoder)
240 GstFFMpegAudDec *ffmpegdec = (GstFFMpegAudDec *) decoder;
242 GST_OBJECT_LOCK (ffmpegdec);
243 gst_ffmpegauddec_close (ffmpegdec, FALSE);
244 g_free (ffmpegdec->padded);
245 ffmpegdec->padded = NULL;
246 ffmpegdec->padded_size = 0;
247 GST_OBJECT_UNLOCK (ffmpegdec);
248 gst_audio_info_init (&ffmpegdec->info);
249 gst_caps_replace (&ffmpegdec->last_caps, NULL);
256 gst_ffmpegauddec_open (GstFFMpegAudDec * ffmpegdec)
258 GstFFMpegAudDecClass *oclass;
260 oclass = (GstFFMpegAudDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
262 if (gst_ffmpeg_avcodec_open (ffmpegdec->context, oclass->in_plugin) < 0)
265 ffmpegdec->opened = TRUE;
267 GST_LOG_OBJECT (ffmpegdec, "Opened libav codec %s, id %d",
268 oclass->in_plugin->name, oclass->in_plugin->id);
270 gst_audio_info_init (&ffmpegdec->info);
277 gst_ffmpegauddec_close (ffmpegdec, TRUE);
278 GST_DEBUG_OBJECT (ffmpegdec, "avdec_%s: Failed to open libav codec",
279 oclass->in_plugin->name);
285 gst_ffmpegauddec_propose_allocation (GstAudioDecoder * decoder,
288 GstAllocationParams params;
290 gst_allocation_params_init (¶ms);
291 params.flags = GST_MEMORY_FLAG_ZERO_PADDED;
293 params.padding = AV_INPUT_BUFFER_PADDING_SIZE;
294 /* we would like to have some padding so that we don't have to
295 * memcpy. We don't suggest an allocator. */
296 gst_query_add_allocation_param (query, NULL, ¶ms);
298 return GST_AUDIO_DECODER_CLASS (parent_class)->propose_allocation (decoder,
303 gst_ffmpegauddec_set_format (GstAudioDecoder * decoder, GstCaps * caps)
305 GstFFMpegAudDec *ffmpegdec = (GstFFMpegAudDec *) decoder;
306 GstFFMpegAudDecClass *oclass;
309 oclass = (GstFFMpegAudDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
311 GST_DEBUG_OBJECT (ffmpegdec, "setcaps called");
313 GST_OBJECT_LOCK (ffmpegdec);
315 if (ffmpegdec->last_caps && gst_caps_is_equal (ffmpegdec->last_caps, caps)) {
316 GST_DEBUG_OBJECT (ffmpegdec, "same caps");
317 GST_OBJECT_UNLOCK (ffmpegdec);
321 gst_caps_replace (&ffmpegdec->last_caps, caps);
323 /* close old session */
324 if (ffmpegdec->opened) {
325 GST_OBJECT_UNLOCK (ffmpegdec);
326 gst_ffmpegauddec_drain (ffmpegdec, FALSE);
327 GST_OBJECT_LOCK (ffmpegdec);
328 if (!gst_ffmpegauddec_close (ffmpegdec, TRUE)) {
329 GST_OBJECT_UNLOCK (ffmpegdec);
334 /* get size and so */
335 gst_ffmpeg_caps_with_codecid (oclass->in_plugin->id,
336 oclass->in_plugin->type, caps, ffmpegdec->context);
338 /* workaround encoder bugs */
339 ffmpegdec->context->workaround_bugs |= FF_BUG_AUTODETECT;
340 ffmpegdec->context->err_recognition = 1;
342 /* open codec - we don't select an output pix_fmt yet,
343 * simply because we don't know! We only get it
344 * during playback... */
345 if (!gst_ffmpegauddec_open (ffmpegdec))
349 GST_OBJECT_UNLOCK (ffmpegdec);
356 GST_DEBUG_OBJECT (ffmpegdec, "Failed to open");
363 settings_changed (GstFFMpegAudDec * ffmpegdec, AVFrame * frame)
365 GstAudioFormat format;
366 GstAudioLayout layout;
367 gint channels = av_get_channel_layout_nb_channels (frame->channel_layout);
370 channels = frame->channels;
372 format = gst_ffmpeg_smpfmt_to_audioformat (frame->format, &layout);
373 if (format == GST_AUDIO_FORMAT_UNKNOWN)
376 return !(ffmpegdec->info.rate == frame->sample_rate &&
377 ffmpegdec->info.channels == channels &&
378 ffmpegdec->info.finfo->format == format &&
379 ffmpegdec->info.layout == layout);
383 gst_ffmpegauddec_negotiate (GstFFMpegAudDec * ffmpegdec,
384 AVCodecContext * context, AVFrame * frame, gboolean force)
386 GstFFMpegAudDecClass *oclass;
387 GstAudioFormat format;
388 GstAudioLayout layout;
390 GstAudioChannelPosition pos[64] = { 0, };
392 oclass = (GstFFMpegAudDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
394 format = gst_ffmpeg_smpfmt_to_audioformat (frame->format, &layout);
395 if (format == GST_AUDIO_FORMAT_UNKNOWN)
397 channels = av_get_channel_layout_nb_channels (frame->channel_layout);
399 channels = frame->channels;
403 if (!force && !settings_changed (ffmpegdec, frame))
406 GST_DEBUG_OBJECT (ffmpegdec,
407 "Renegotiating audio from %dHz@%dchannels (%d, interleaved=%d) "
408 "to %dHz@%dchannels (%d, interleaved=%d)",
409 ffmpegdec->info.rate, ffmpegdec->info.channels,
410 ffmpegdec->info.finfo->format,
411 ffmpegdec->info.layout == GST_AUDIO_LAYOUT_INTERLEAVED,
412 frame->sample_rate, channels, format,
413 layout == GST_AUDIO_LAYOUT_INTERLEAVED);
415 gst_ffmpeg_channel_layout_to_gst (frame->channel_layout, channels, pos);
416 memcpy (ffmpegdec->ffmpeg_layout, pos,
417 sizeof (GstAudioChannelPosition) * channels);
419 /* Get GStreamer channel layout */
420 gst_audio_channel_positions_to_valid_order (pos, channels);
421 ffmpegdec->needs_reorder =
422 memcmp (pos, ffmpegdec->ffmpeg_layout, sizeof (pos[0]) * channels) != 0;
423 gst_audio_info_set_format (&ffmpegdec->info, format,
424 frame->sample_rate, channels, pos);
425 ffmpegdec->info.layout = layout;
427 if (!gst_audio_decoder_set_output_format (GST_AUDIO_DECODER (ffmpegdec),
436 #ifdef HAVE_LIBAV_UNINSTALLED
437 /* using internal ffmpeg snapshot */
438 GST_ELEMENT_ERROR (ffmpegdec, CORE, NEGOTIATION,
439 ("Could not find GStreamer caps mapping for libav codec '%s'.",
440 oclass->in_plugin->name), (NULL));
442 /* using external ffmpeg */
443 GST_ELEMENT_ERROR (ffmpegdec, CORE, NEGOTIATION,
444 ("Could not find GStreamer caps mapping for libav codec '%s', and "
445 "you are using an external libavcodec. This is most likely due to "
446 "a packaging problem and/or libavcodec having been upgraded to a "
447 "version that is not compatible with this version of "
448 "gstreamer-libav. Make sure your gstreamer-libav and libavcodec "
449 "packages come from the same source/repository.",
450 oclass->in_plugin->name), (NULL));
456 GST_ELEMENT_ERROR (ffmpegdec, CORE, NEGOTIATION, (NULL),
457 ("Could not set caps for libav decoder (%s), not fixed?",
458 oclass->in_plugin->name));
459 memset (&ffmpegdec->info, 0, sizeof (ffmpegdec->info));
466 gst_avpacket_init (AVPacket * packet, guint8 * data, guint size)
468 memset (packet, 0, sizeof (AVPacket));
474 * Returns: whether a frame was decoded
477 gst_ffmpegauddec_audio_frame (GstFFMpegAudDec * ffmpegdec,
478 AVCodec * in_plugin, GstBuffer ** outbuf, GstFlowReturn * ret,
479 gboolean * need_more_data)
481 gboolean got_frame = FALSE;
484 res = avcodec_receive_frame (ffmpegdec->context, ffmpegdec->frame);
487 gint nsamples, channels, byte_per_sample;
491 if (!gst_ffmpegauddec_negotiate (ffmpegdec, ffmpegdec->context,
492 ffmpegdec->frame, FALSE)) {
494 *ret = GST_FLOW_NOT_NEGOTIATED;
500 channels = ffmpegdec->info.channels;
501 nsamples = ffmpegdec->frame->nb_samples;
502 byte_per_sample = ffmpegdec->info.finfo->width / 8;
503 planar = av_sample_fmt_is_planar (ffmpegdec->frame->format);
505 g_return_val_if_fail (ffmpegdec->info.layout == (planar ?
506 GST_AUDIO_LAYOUT_NON_INTERLEAVED : GST_AUDIO_LAYOUT_INTERLEAVED),
507 GST_FLOW_NOT_NEGOTIATED);
509 GST_DEBUG_OBJECT (ffmpegdec, "Creating output buffer");
511 /* ffmpegdec->frame->linesize[0] might contain padding, allocate only what's needed */
512 output_size = nsamples * byte_per_sample * channels;
515 gst_audio_decoder_allocate_output_buffer (GST_AUDIO_DECODER
516 (ffmpegdec), output_size);
522 meta = gst_buffer_add_audio_meta (*outbuf, &ffmpegdec->info, nsamples,
525 for (i = 0; i < channels; i++) {
526 gst_buffer_fill (*outbuf, meta->offsets[i],
527 ffmpegdec->frame->extended_data[i], nsamples * byte_per_sample);
530 gst_buffer_fill (*outbuf, 0, ffmpegdec->frame->data[0], output_size);
533 GST_DEBUG_OBJECT (ffmpegdec, "Buffer created. Size: %" G_GSIZE_FORMAT,
536 /* Reorder channels to the GStreamer channel order */
537 if (ffmpegdec->needs_reorder) {
538 *outbuf = gst_buffer_make_writable (*outbuf);
539 gst_audio_buffer_reorder_channels (*outbuf, ffmpegdec->info.finfo->format,
540 ffmpegdec->info.channels, ffmpegdec->ffmpeg_layout,
541 ffmpegdec->info.position);
544 /* Mark corrupted frames as corrupted */
545 if (ffmpegdec->frame->flags & AV_FRAME_FLAG_CORRUPT)
546 GST_BUFFER_FLAG_SET (*outbuf, GST_BUFFER_FLAG_CORRUPTED);
547 } else if (res == AVERROR (EAGAIN)) {
548 GST_DEBUG_OBJECT (ffmpegdec, "Need more data");
550 *need_more_data = TRUE;
551 } else if (res == AVERROR_EOF) {
553 GST_DEBUG_OBJECT (ffmpegdec, "Context was entirely flushed");
554 } else if (res < 0) {
555 GST_AUDIO_DECODER_ERROR (ffmpegdec, 1, STREAM, DECODE, (NULL),
556 ("Audio decoding error"), *ret);
560 av_frame_unref (ffmpegdec->frame);
561 GST_DEBUG_OBJECT (ffmpegdec, "return flow %s, out %p, got_frame %d",
562 gst_flow_get_name (*ret), *outbuf, got_frame);
567 * Returns: whether a frame was decoded
570 gst_ffmpegauddec_frame (GstFFMpegAudDec * ffmpegdec, GstFlowReturn * ret,
571 gboolean * need_more_data)
573 GstFFMpegAudDecClass *oclass;
574 GstBuffer *outbuf = NULL;
575 gboolean got_frame = FALSE;
577 if (G_UNLIKELY (ffmpegdec->context->codec == NULL))
581 ffmpegdec->context->frame_number++;
583 oclass = (GstFFMpegAudDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
586 gst_ffmpegauddec_audio_frame (ffmpegdec, oclass->in_plugin, &outbuf, ret,
590 GST_LOG_OBJECT (ffmpegdec, "Decoded data, buffer %" GST_PTR_FORMAT, outbuf);
592 gst_audio_decoder_finish_subframe (GST_AUDIO_DECODER_CAST (ffmpegdec),
595 GST_DEBUG_OBJECT (ffmpegdec, "We didn't get a decoded buffer");
604 GST_ERROR_OBJECT (ffmpegdec, "no codec context");
610 gst_ffmpegauddec_drain (GstFFMpegAudDec * ffmpegdec, gboolean force)
612 GstFlowReturn ret = GST_FLOW_OK;
613 gboolean got_any_frames = FALSE;
614 gboolean need_more_data = FALSE;
617 if (avcodec_send_packet (ffmpegdec->context, NULL))
618 goto send_packet_failed;
620 /* FIXME: https://gitlab.freedesktop.org/gstreamer/gstreamer/-/issues/1474 */
621 if (!(ffmpegdec->context->flags2 & AV_CODEC_FLAG2_SKIP_MANUAL)) {
623 got_frame = gst_ffmpegauddec_frame (ffmpegdec, &ret, &need_more_data);
625 got_any_frames = TRUE;
626 } while (got_frame && !need_more_data);
628 avcodec_flush_buffers (ffmpegdec->context);
630 /* FFMpeg will return AVERROR_EOF if it's internal was fully drained
631 * then we are translating it to GST_FLOW_EOS. However, because this behavior
632 * is fully internal stuff of this implementation and gstaudiodecoder
633 * baseclass doesn't convert this GST_FLOW_EOS to GST_FLOW_OK,
634 * convert this flow returned here */
635 if (ret == GST_FLOW_EOS)
638 if (got_any_frames || force) {
639 GstFlowReturn new_ret =
640 gst_audio_decoder_finish_frame (GST_AUDIO_DECODER (ffmpegdec), NULL, 1);
642 if (ret == GST_FLOW_OK)
650 GST_WARNING_OBJECT (ffmpegdec, "send packet failed, could not drain decoder");
655 gst_ffmpegauddec_flush (GstAudioDecoder * decoder, gboolean hard)
657 GstFFMpegAudDec *ffmpegdec = (GstFFMpegAudDec *) decoder;
659 if (ffmpegdec->opened) {
660 avcodec_flush_buffers (ffmpegdec->context);
665 gst_ffmpegauddec_handle_frame (GstAudioDecoder * decoder, GstBuffer * inbuf)
667 GstFFMpegAudDec *ffmpegdec;
668 GstFFMpegAudDecClass *oclass;
672 gboolean got_any_frames = FALSE;
674 GstFlowReturn ret = GST_FLOW_OK;
677 GstAudioClippingMeta *clipping_meta = NULL;
678 guint32 num_clipped_samples = 0;
679 gboolean fully_clipped = FALSE;
680 gboolean need_more_data = FALSE;
682 ffmpegdec = (GstFFMpegAudDec *) decoder;
684 if (G_UNLIKELY (!ffmpegdec->opened))
688 return gst_ffmpegauddec_drain (ffmpegdec, FALSE);
691 inbuf = gst_buffer_ref (inbuf);
692 is_header = GST_BUFFER_FLAG_IS_SET (inbuf, GST_BUFFER_FLAG_HEADER);
694 oclass = (GstFFMpegAudDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
696 GST_LOG_OBJECT (ffmpegdec,
697 "Received new data of size %" G_GSIZE_FORMAT ", offset:%" G_GUINT64_FORMAT
698 ", ts:%" GST_TIME_FORMAT ", dur:%" GST_TIME_FORMAT,
699 gst_buffer_get_size (inbuf), GST_BUFFER_OFFSET (inbuf),
700 GST_TIME_ARGS (GST_BUFFER_PTS (inbuf)),
701 GST_TIME_ARGS (GST_BUFFER_DURATION (inbuf)));
703 /* workarounds, functions write to buffers:
704 * libavcodec/svq1.c:svq1_decode_frame writes to the given buffer.
705 * libavcodec/svq3.c:svq3_decode_slice_header too.
706 * ffmpeg devs know about it and will fix it (they said). */
707 if (oclass->in_plugin->id == AV_CODEC_ID_SVQ1 ||
708 oclass->in_plugin->id == AV_CODEC_ID_SVQ3) {
709 inbuf = gst_buffer_make_writable (inbuf);
712 /* mpegaudioparse is setting buffer flags for the Xing/LAME header. This
713 * should not be passed to the decoder as it results in unnecessary silence
714 * samples to be output */
715 if (oclass->in_plugin->id == AV_CODEC_ID_MP3 &&
716 GST_BUFFER_FLAG_IS_SET (inbuf, GST_BUFFER_FLAG_DECODE_ONLY) &&
717 GST_BUFFER_FLAG_IS_SET (inbuf, GST_BUFFER_FLAG_DROPPABLE)) {
718 gst_buffer_unref (inbuf);
719 return gst_audio_decoder_finish_frame (decoder, NULL, 1);
722 clipping_meta = gst_buffer_get_audio_clipping_meta (inbuf);
724 gst_buffer_map (inbuf, &map, GST_MAP_READ);
729 if (size > 0 && (!GST_MEMORY_IS_ZERO_PADDED (map.memory)
730 || (map.maxsize - map.size) < AV_INPUT_BUFFER_PADDING_SIZE)) {
732 if (ffmpegdec->padded_size < size + AV_INPUT_BUFFER_PADDING_SIZE) {
733 ffmpegdec->padded_size = size + AV_INPUT_BUFFER_PADDING_SIZE;
734 ffmpegdec->padded = g_realloc (ffmpegdec->padded, ffmpegdec->padded_size);
735 GST_LOG_OBJECT (ffmpegdec, "resized padding buffer to %d",
736 ffmpegdec->padded_size);
738 GST_CAT_TRACE_OBJECT (GST_CAT_PERFORMANCE, ffmpegdec,
739 "Copy input to add padding");
740 memcpy (ffmpegdec->padded, data, size);
741 memset (ffmpegdec->padded + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
743 data = ffmpegdec->padded;
746 gst_avpacket_init (&packet, data, size);
751 if (clipping_meta != NULL) {
752 if (clipping_meta->format == GST_FORMAT_DEFAULT) {
753 uint8_t *p = av_packet_new_side_data (&packet, AV_PKT_DATA_SKIP_SAMPLES,
756 GstByteWriter writer;
757 guint32 start = clipping_meta->start;
758 guint32 end = clipping_meta->end;
760 num_clipped_samples = start + end;
762 gst_byte_writer_init_with_data (&writer, p, 10, FALSE);
763 gst_byte_writer_put_uint32_le (&writer, start);
764 gst_byte_writer_put_uint32_le (&writer, end);
765 GST_LOG_OBJECT (ffmpegdec, "buffer has clipping metadata; added skip "
766 "side data to avpacket with start %u and end %u", start, end);
769 GST_WARNING_OBJECT (ffmpegdec,
770 "buffer has clipping metadata in unsupported format %s",
771 gst_format_get_name (clipping_meta->format));
775 if (avcodec_send_packet (ffmpegdec->context, &packet) < 0) {
776 av_packet_free_side_data (&packet);
777 goto send_packet_failed;
779 av_packet_free_side_data (&packet);
782 /* decode a frame of audio now */
783 got_frame = gst_ffmpegauddec_frame (ffmpegdec, &ret, &need_more_data);
786 got_any_frames = TRUE;
788 if (ret != GST_FLOW_OK) {
789 GST_LOG_OBJECT (ffmpegdec, "breaking because of flow ret %s",
790 gst_flow_get_name (ret));
791 /* bad flow return, make sure we discard all data and exit */
794 } while (got_frame && !need_more_data);
796 /* The frame was fully clipped if we have samples to be clipped and
797 * it's either more than the known fixed frame size, or the decoder returned
798 * that it needs more data (EAGAIN) and we didn't decode any frames at all.
800 fully_clipped = (clipping_meta != NULL && num_clipped_samples > 0)
801 && ((ffmpegdec->context->frame_size != 0
802 && num_clipped_samples >= ffmpegdec->context->frame_size)
803 || (need_more_data && !got_any_frames));
805 if (is_header || got_any_frames || fully_clipped) {
806 /* Even if previous return wasn't GST_FLOW_OK, we need to call
807 * _finish_frame() since baseclass is expecting that _finish_frame()
808 * is followed by _finish_subframe()
810 GstFlowReturn new_ret =
811 gst_audio_decoder_finish_frame (GST_AUDIO_DECODER (ffmpegdec), NULL, 1);
813 /* Only override the flow return value if previously did have a GST_FLOW_OK.
814 * Failure to do this would result in skipping downstream issues caught in
816 if (ret == GST_FLOW_OK)
821 gst_buffer_unmap (inbuf, &map);
822 gst_buffer_unref (inbuf);
830 oclass = (GstFFMpegAudDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
831 GST_ELEMENT_ERROR (ffmpegdec, CORE, NEGOTIATION, (NULL),
832 ("avdec_%s: input format was not set before data start",
833 oclass->in_plugin->name));
834 ret = GST_FLOW_NOT_NEGOTIATED;
840 GST_AUDIO_DECODER_ERROR (ffmpegdec, 1, STREAM, DECODE, (NULL),
841 ("Audio decoding error"), ret);
843 if (ret == GST_FLOW_OK) {
844 /* Even if ffmpeg was not able to decode current audio frame,
845 * we should call gst_audio_decoder_finish_frame() so that baseclass
846 * can clear its internal status and can respect timestamp of later
847 * incoming buffers */
848 ret = gst_ffmpegauddec_drain (ffmpegdec, TRUE);
855 gst_ffmpegauddec_register (GstPlugin * plugin)
857 GTypeInfo typeinfo = {
858 sizeof (GstFFMpegAudDecClass),
859 (GBaseInitFunc) gst_ffmpegauddec_base_init,
861 (GClassInitFunc) gst_ffmpegauddec_class_init,
864 sizeof (GstFFMpegAudDec),
866 (GInstanceInitFunc) gst_ffmpegauddec_init,
873 GST_LOG ("Registering decoders");
875 while ((in_plugin = (AVCodec *) av_codec_iterate (&i))) {
879 if (!av_codec_is_decoder (in_plugin)
880 || in_plugin->type != AVMEDIA_TYPE_AUDIO) {
884 /* no quasi codecs, please */
885 if (in_plugin->id == AV_CODEC_ID_PCM_S16LE_PLANAR ||
886 (in_plugin->id >= AV_CODEC_ID_PCM_S16LE &&
887 in_plugin->id <= AV_CODEC_ID_PCM_BLURAY) ||
888 (in_plugin->id >= AV_CODEC_ID_PCM_S8_PLANAR &&
889 in_plugin->id <= AV_CODEC_ID_PCM_F24LE))
892 /* No decoders depending on external libraries (we don't build them, but
893 * people who build against an external ffmpeg might have them.
894 * We have native gstreamer plugins for all of those libraries anyway. */
895 if (!strncmp (in_plugin->name, "lib", 3)) {
897 ("Not using external library decoder %s. Use the gstreamer-native ones instead.",
902 GST_DEBUG ("Trying plugin %s [%s]", in_plugin->name, in_plugin->long_name);
904 /* no codecs for which we're GUARANTEED to have better alternatives */
905 /* MP1 : Use MP3 for decoding */
906 /* MP2 : Use MP3 for decoding */
907 /* Theora: Use libtheora based theoradec */
908 if (!strcmp (in_plugin->name, "vorbis") ||
909 !strcmp (in_plugin->name, "wavpack") ||
910 !strcmp (in_plugin->name, "mp1") ||
911 !strcmp (in_plugin->name, "mp2") ||
912 !strcmp (in_plugin->name, "libfaad") ||
913 !strcmp (in_plugin->name, "mpeg4aac") ||
914 !strcmp (in_plugin->name, "ass") ||
915 !strcmp (in_plugin->name, "srt") ||
916 !strcmp (in_plugin->name, "pgssub") ||
917 !strcmp (in_plugin->name, "dvdsub") ||
918 !strcmp (in_plugin->name, "dvbsub")) {
919 GST_LOG ("Ignoring decoder %s", in_plugin->name);
923 /* construct the type */
924 type_name = g_strdup_printf ("avdec_%s", in_plugin->name);
925 g_strdelimit (type_name, ".,|-<> ", '_');
927 type = g_type_from_name (type_name);
930 /* create the gtype now */
932 g_type_register_static (GST_TYPE_AUDIO_DECODER, type_name, &typeinfo,
934 g_type_set_qdata (type, GST_FFDEC_PARAMS_QDATA, (gpointer) in_plugin);
937 /* (Ronald) MPEG-4 gets a higher priority because it has been well-
938 * tested and by far outperforms divxdec/xviddec - so we prefer it.
939 * msmpeg4v3 same, as it outperforms divxdec for divx3 playback.
940 * VC1/WMV3 are not working and thus unpreferred for now. */
941 switch (in_plugin->id) {
942 case AV_CODEC_ID_RA_144:
943 case AV_CODEC_ID_RA_288:
944 case AV_CODEC_ID_COOK:
945 case AV_CODEC_ID_AAC:
946 rank = GST_RANK_PRIMARY;
948 /* SIPR: decoder should have a higher rank than realaudiodec.
950 case AV_CODEC_ID_SIPR:
951 rank = GST_RANK_SECONDARY;
954 rank = GST_RANK_MARGINAL;
957 if (!gst_element_register (plugin, type_name, rank, type)) {
958 g_warning ("Failed to register %s", type_name);
966 GST_LOG ("Finished Registering decoders");