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>
35 #include "gstavcodecmap.h"
36 #include "gstavutils.h"
37 #include "gstavauddec.h"
39 /* A number of function prototypes are given so we can refer to them later. */
40 static void gst_ffmpegauddec_base_init (GstFFMpegAudDecClass * klass);
41 static void gst_ffmpegauddec_class_init (GstFFMpegAudDecClass * klass);
42 static void gst_ffmpegauddec_init (GstFFMpegAudDec * ffmpegdec);
43 static void gst_ffmpegauddec_finalize (GObject * object);
44 static gboolean gst_ffmpegauddec_propose_allocation (GstAudioDecoder * decoder,
47 static gboolean gst_ffmpegauddec_start (GstAudioDecoder * decoder);
48 static gboolean gst_ffmpegauddec_stop (GstAudioDecoder * decoder);
49 static void gst_ffmpegauddec_flush (GstAudioDecoder * decoder, gboolean hard);
50 static gboolean gst_ffmpegauddec_set_format (GstAudioDecoder * decoder,
52 static GstFlowReturn gst_ffmpegauddec_handle_frame (GstAudioDecoder * decoder,
55 static gboolean gst_ffmpegauddec_negotiate (GstFFMpegAudDec * ffmpegdec,
56 AVCodecContext * context, AVFrame * frame, gboolean force);
58 static void gst_ffmpegauddec_drain (GstFFMpegAudDec * ffmpegdec);
60 #define GST_FFDEC_PARAMS_QDATA g_quark_from_static_string("avdec-params")
62 static GstElementClass *parent_class = NULL;
65 gst_ffmpegauddec_base_init (GstFFMpegAudDecClass * klass)
67 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
68 GstPadTemplate *sinktempl, *srctempl;
69 GstCaps *sinkcaps, *srccaps;
71 gchar *longname, *description;
74 (AVCodec *) g_type_get_qdata (G_OBJECT_CLASS_TYPE (klass),
75 GST_FFDEC_PARAMS_QDATA);
76 g_assert (in_plugin != NULL);
78 /* construct the element details struct */
79 longname = g_strdup_printf ("libav %s decoder", in_plugin->long_name);
80 description = g_strdup_printf ("libav %s decoder", in_plugin->name);
81 gst_element_class_set_metadata (element_class, longname,
82 "Codec/Decoder/Audio", description,
83 "Wim Taymans <wim.taymans@gmail.com>, "
84 "Ronald Bultje <rbultje@ronald.bitfreak.net>, "
85 "Edward Hervey <bilboed@bilboed.com>");
90 sinkcaps = gst_ffmpeg_codecid_to_caps (in_plugin->id, NULL, FALSE);
92 GST_DEBUG ("Couldn't get sink caps for decoder '%s'", in_plugin->name);
93 sinkcaps = gst_caps_from_string ("unknown/unknown");
95 srccaps = gst_ffmpeg_codectype_to_audio_caps (NULL,
96 in_plugin->id, FALSE, in_plugin);
98 GST_DEBUG ("Couldn't get source caps for decoder '%s'", in_plugin->name);
99 srccaps = gst_caps_from_string ("audio/x-raw");
103 sinktempl = gst_pad_template_new ("sink", GST_PAD_SINK,
104 GST_PAD_ALWAYS, sinkcaps);
105 srctempl = gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS, srccaps);
107 gst_element_class_add_pad_template (element_class, srctempl);
108 gst_element_class_add_pad_template (element_class, sinktempl);
110 gst_caps_unref (sinkcaps);
111 gst_caps_unref (srccaps);
113 klass->in_plugin = in_plugin;
114 klass->srctempl = srctempl;
115 klass->sinktempl = sinktempl;
119 gst_ffmpegauddec_class_init (GstFFMpegAudDecClass * klass)
121 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
122 GstAudioDecoderClass *gstaudiodecoder_class = GST_AUDIO_DECODER_CLASS (klass);
124 parent_class = g_type_class_peek_parent (klass);
126 gobject_class->finalize = gst_ffmpegauddec_finalize;
128 gstaudiodecoder_class->start = GST_DEBUG_FUNCPTR (gst_ffmpegauddec_start);
129 gstaudiodecoder_class->stop = GST_DEBUG_FUNCPTR (gst_ffmpegauddec_stop);
130 gstaudiodecoder_class->set_format =
131 GST_DEBUG_FUNCPTR (gst_ffmpegauddec_set_format);
132 gstaudiodecoder_class->handle_frame =
133 GST_DEBUG_FUNCPTR (gst_ffmpegauddec_handle_frame);
134 gstaudiodecoder_class->flush = GST_DEBUG_FUNCPTR (gst_ffmpegauddec_flush);
135 gstaudiodecoder_class->propose_allocation =
136 GST_DEBUG_FUNCPTR (gst_ffmpegauddec_propose_allocation);
140 gst_ffmpegauddec_init (GstFFMpegAudDec * ffmpegdec)
142 GstFFMpegAudDecClass *klass =
143 (GstFFMpegAudDecClass *) G_OBJECT_GET_CLASS (ffmpegdec);
145 /* some ffmpeg data */
146 ffmpegdec->context = avcodec_alloc_context3 (klass->in_plugin);
147 ffmpegdec->context->opaque = ffmpegdec;
148 ffmpegdec->opened = FALSE;
150 ffmpegdec->frame = av_frame_alloc ();
152 GST_PAD_SET_ACCEPT_TEMPLATE (GST_VIDEO_DECODER_SINK_PAD (ffmpegdec));
153 gst_audio_decoder_set_use_default_pad_acceptcaps (GST_AUDIO_DECODER_CAST
156 gst_audio_decoder_set_drainable (GST_AUDIO_DECODER (ffmpegdec), TRUE);
157 gst_audio_decoder_set_needs_format (GST_AUDIO_DECODER (ffmpegdec), TRUE);
161 gst_ffmpegauddec_finalize (GObject * object)
163 GstFFMpegAudDec *ffmpegdec = (GstFFMpegAudDec *) object;
165 av_frame_free (&ffmpegdec->frame);
167 if (ffmpegdec->context != NULL) {
168 gst_ffmpeg_avcodec_close (ffmpegdec->context);
169 av_free (ffmpegdec->context);
170 ffmpegdec->context = NULL;
173 G_OBJECT_CLASS (parent_class)->finalize (object);
178 gst_ffmpegauddec_close (GstFFMpegAudDec * ffmpegdec, gboolean reset)
180 GstFFMpegAudDecClass *oclass;
182 oclass = (GstFFMpegAudDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
184 GST_LOG_OBJECT (ffmpegdec, "closing libav codec");
186 gst_caps_replace (&ffmpegdec->last_caps, NULL);
187 gst_buffer_replace (&ffmpegdec->outbuf, NULL);
189 gst_ffmpeg_avcodec_close (ffmpegdec->context);
190 ffmpegdec->opened = FALSE;
192 if (ffmpegdec->context->extradata) {
193 av_free (ffmpegdec->context->extradata);
194 ffmpegdec->context->extradata = NULL;
198 if (avcodec_get_context_defaults3 (ffmpegdec->context,
199 oclass->in_plugin) < 0) {
200 GST_DEBUG_OBJECT (ffmpegdec, "Failed to set context defaults");
203 ffmpegdec->context->opaque = ffmpegdec;
210 gst_ffmpegauddec_start (GstAudioDecoder * decoder)
212 GstFFMpegAudDec *ffmpegdec = (GstFFMpegAudDec *) decoder;
213 GstFFMpegAudDecClass *oclass;
215 oclass = (GstFFMpegAudDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
217 GST_OBJECT_LOCK (ffmpegdec);
218 gst_ffmpeg_avcodec_close (ffmpegdec->context);
219 if (avcodec_get_context_defaults3 (ffmpegdec->context, oclass->in_plugin) < 0) {
220 GST_DEBUG_OBJECT (ffmpegdec, "Failed to set context defaults");
221 GST_OBJECT_UNLOCK (ffmpegdec);
224 ffmpegdec->context->opaque = ffmpegdec;
225 GST_OBJECT_UNLOCK (ffmpegdec);
231 gst_ffmpegauddec_stop (GstAudioDecoder * decoder)
233 GstFFMpegAudDec *ffmpegdec = (GstFFMpegAudDec *) decoder;
235 GST_OBJECT_LOCK (ffmpegdec);
236 gst_ffmpegauddec_close (ffmpegdec, FALSE);
237 g_free (ffmpegdec->padded);
238 ffmpegdec->padded = NULL;
239 ffmpegdec->padded_size = 0;
240 GST_OBJECT_UNLOCK (ffmpegdec);
241 gst_audio_info_init (&ffmpegdec->info);
242 gst_caps_replace (&ffmpegdec->last_caps, NULL);
249 gst_ffmpegauddec_open (GstFFMpegAudDec * ffmpegdec)
251 GstFFMpegAudDecClass *oclass;
253 oclass = (GstFFMpegAudDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
255 if (gst_ffmpeg_avcodec_open (ffmpegdec->context, oclass->in_plugin) < 0)
258 ffmpegdec->opened = TRUE;
260 GST_LOG_OBJECT (ffmpegdec, "Opened libav codec %s, id %d",
261 oclass->in_plugin->name, oclass->in_plugin->id);
263 gst_audio_info_init (&ffmpegdec->info);
270 gst_ffmpegauddec_close (ffmpegdec, TRUE);
271 GST_DEBUG_OBJECT (ffmpegdec, "avdec_%s: Failed to open libav codec",
272 oclass->in_plugin->name);
278 gst_ffmpegauddec_propose_allocation (GstAudioDecoder * decoder,
281 GstAllocationParams params;
283 gst_allocation_params_init (¶ms);
284 params.flags = GST_MEMORY_FLAG_ZERO_PADDED;
286 params.padding = AV_INPUT_BUFFER_PADDING_SIZE;
287 /* we would like to have some padding so that we don't have to
288 * memcpy. We don't suggest an allocator. */
289 gst_query_add_allocation_param (query, NULL, ¶ms);
291 return GST_AUDIO_DECODER_CLASS (parent_class)->propose_allocation (decoder,
296 gst_ffmpegauddec_set_format (GstAudioDecoder * decoder, GstCaps * caps)
298 GstFFMpegAudDec *ffmpegdec = (GstFFMpegAudDec *) decoder;
299 GstFFMpegAudDecClass *oclass;
302 oclass = (GstFFMpegAudDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
304 GST_DEBUG_OBJECT (ffmpegdec, "setcaps called");
306 GST_OBJECT_LOCK (ffmpegdec);
308 if (ffmpegdec->last_caps && gst_caps_is_equal (ffmpegdec->last_caps, caps)) {
309 GST_DEBUG_OBJECT (ffmpegdec, "same caps");
310 GST_OBJECT_UNLOCK (ffmpegdec);
314 gst_caps_replace (&ffmpegdec->last_caps, caps);
316 /* close old session */
317 if (ffmpegdec->opened) {
318 GST_OBJECT_UNLOCK (ffmpegdec);
319 gst_ffmpegauddec_drain (ffmpegdec);
320 GST_OBJECT_LOCK (ffmpegdec);
321 if (!gst_ffmpegauddec_close (ffmpegdec, TRUE)) {
322 GST_OBJECT_UNLOCK (ffmpegdec);
327 /* get size and so */
328 gst_ffmpeg_caps_with_codecid (oclass->in_plugin->id,
329 oclass->in_plugin->type, caps, ffmpegdec->context);
331 /* workaround encoder bugs */
332 ffmpegdec->context->workaround_bugs |= FF_BUG_AUTODETECT;
333 ffmpegdec->context->err_recognition = 1;
335 /* open codec - we don't select an output pix_fmt yet,
336 * simply because we don't know! We only get it
337 * during playback... */
338 if (!gst_ffmpegauddec_open (ffmpegdec))
342 GST_OBJECT_UNLOCK (ffmpegdec);
349 GST_DEBUG_OBJECT (ffmpegdec, "Failed to open");
356 settings_changed (GstFFMpegAudDec * ffmpegdec, AVFrame * frame)
358 GstAudioFormat format;
360 av_get_channel_layout_nb_channels (av_frame_get_channel_layout (frame));
362 format = gst_ffmpeg_smpfmt_to_audioformat (frame->format);
363 if (format == GST_AUDIO_FORMAT_UNKNOWN)
366 return !(ffmpegdec->info.rate ==
367 av_frame_get_sample_rate (frame) &&
368 ffmpegdec->info.channels == channels &&
369 ffmpegdec->info.finfo->format == format);
373 gst_ffmpegauddec_negotiate (GstFFMpegAudDec * ffmpegdec,
374 AVCodecContext * context, AVFrame * frame, gboolean force)
376 GstFFMpegAudDecClass *oclass;
377 GstAudioFormat format;
379 GstAudioChannelPosition pos[64] = { 0, };
381 oclass = (GstFFMpegAudDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
383 format = gst_ffmpeg_smpfmt_to_audioformat (frame->format);
384 if (format == GST_AUDIO_FORMAT_UNKNOWN)
387 av_get_channel_layout_nb_channels (av_frame_get_channel_layout (frame));
389 channels = av_frame_get_channels (frame);
393 if (!force && !settings_changed (ffmpegdec, frame))
396 GST_DEBUG_OBJECT (ffmpegdec,
397 "Renegotiating audio from %dHz@%dchannels (%d) to %dHz@%dchannels (%d)",
398 ffmpegdec->info.rate, ffmpegdec->info.channels,
399 ffmpegdec->info.finfo->format, av_frame_get_sample_rate (frame), channels,
402 gst_ffmpeg_channel_layout_to_gst (av_frame_get_channel_layout (frame),
404 memcpy (ffmpegdec->ffmpeg_layout, pos,
405 sizeof (GstAudioChannelPosition) * channels);
407 /* Get GStreamer channel layout */
408 gst_audio_channel_positions_to_valid_order (pos, channels);
409 ffmpegdec->needs_reorder =
410 memcmp (pos, ffmpegdec->ffmpeg_layout, sizeof (pos[0]) * channels) != 0;
411 gst_audio_info_set_format (&ffmpegdec->info, format,
412 av_frame_get_sample_rate (frame), channels, pos);
414 if (!gst_audio_decoder_set_output_format (GST_AUDIO_DECODER (ffmpegdec),
423 #ifdef HAVE_LIBAV_UNINSTALLED
424 /* using internal ffmpeg snapshot */
425 GST_ELEMENT_ERROR (ffmpegdec, CORE, NEGOTIATION,
426 ("Could not find GStreamer caps mapping for libav codec '%s'.",
427 oclass->in_plugin->name), (NULL));
429 /* using external ffmpeg */
430 GST_ELEMENT_ERROR (ffmpegdec, CORE, NEGOTIATION,
431 ("Could not find GStreamer caps mapping for libav codec '%s', and "
432 "you are using an external libavcodec. This is most likely due to "
433 "a packaging problem and/or libavcodec having been upgraded to a "
434 "version that is not compatible with this version of "
435 "gstreamer-libav. Make sure your gstreamer-libav and libavcodec "
436 "packages come from the same source/repository.",
437 oclass->in_plugin->name), (NULL));
443 GST_ELEMENT_ERROR (ffmpegdec, CORE, NEGOTIATION, (NULL),
444 ("Could not set caps for libav decoder (%s), not fixed?",
445 oclass->in_plugin->name));
446 memset (&ffmpegdec->info, 0, sizeof (ffmpegdec->info));
453 gst_avpacket_init (AVPacket * packet, guint8 * data, guint size)
455 memset (packet, 0, sizeof (AVPacket));
461 gst_ffmpegauddec_audio_frame (GstFFMpegAudDec * ffmpegdec,
462 AVCodec * in_plugin, guint8 * data, guint size, gint * have_data,
463 GstBuffer ** outbuf, GstFlowReturn * ret)
468 GST_DEBUG_OBJECT (ffmpegdec, "size: %d", size);
470 gst_avpacket_init (&packet, data, size);
472 avcodec_decode_audio4 (ffmpegdec->context, ffmpegdec->frame, have_data,
475 GST_DEBUG_OBJECT (ffmpegdec,
476 "Decode audio: len=%d, have_data=%d", len, *have_data);
478 if (len >= 0 && *have_data) {
479 gint nsamples, channels, byte_per_sample;
482 if (!gst_ffmpegauddec_negotiate (ffmpegdec, ffmpegdec->context,
483 ffmpegdec->frame, FALSE)) {
485 *ret = GST_FLOW_NOT_NEGOTIATED;
490 channels = ffmpegdec->info.channels;
491 nsamples = ffmpegdec->frame->nb_samples;
492 byte_per_sample = ffmpegdec->info.finfo->width / 8;
494 /* ffmpegdec->frame->linesize[0] might contain padding, allocate only what's needed */
495 output_size = nsamples * byte_per_sample * channels;
497 GST_DEBUG_OBJECT (ffmpegdec, "Creating output buffer");
498 if (av_sample_fmt_is_planar (ffmpegdec->context->sample_fmt)
503 /* note: linesize[0] might contain padding, allocate only what's needed */
505 gst_audio_decoder_allocate_output_buffer (GST_AUDIO_DECODER
506 (ffmpegdec), output_size);
508 gst_buffer_map (*outbuf, &minfo, GST_MAP_WRITE);
510 switch (ffmpegdec->info.finfo->width) {
512 guint8 *odata = minfo.data;
514 for (i = 0; i < nsamples; i++) {
515 for (j = 0; j < channels; j++) {
517 ((const guint8 *) ffmpegdec->frame->extended_data[j])[i];
524 guint16 *odata = (guint16 *) minfo.data;
526 for (i = 0; i < nsamples; i++) {
527 for (j = 0; j < channels; j++) {
529 ((const guint16 *) ffmpegdec->frame->extended_data[j])[i];
536 guint32 *odata = (guint32 *) minfo.data;
538 for (i = 0; i < nsamples; i++) {
539 for (j = 0; j < channels; j++) {
541 ((const guint32 *) ffmpegdec->frame->extended_data[j])[i];
548 guint64 *odata = (guint64 *) minfo.data;
550 for (i = 0; i < nsamples; i++) {
551 for (j = 0; j < channels; j++) {
553 ((const guint64 *) ffmpegdec->frame->extended_data[j])[i];
560 g_assert_not_reached ();
563 gst_buffer_unmap (*outbuf, &minfo);
566 gst_audio_decoder_allocate_output_buffer (GST_AUDIO_DECODER
567 (ffmpegdec), output_size);
568 gst_buffer_fill (*outbuf, 0, ffmpegdec->frame->data[0], output_size);
571 GST_DEBUG_OBJECT (ffmpegdec, "Buffer created. Size: %" G_GSIZE_FORMAT,
574 /* Reorder channels to the GStreamer channel order */
575 if (ffmpegdec->needs_reorder) {
576 *outbuf = gst_buffer_make_writable (*outbuf);
577 gst_audio_buffer_reorder_channels (*outbuf, ffmpegdec->info.finfo->format,
578 ffmpegdec->info.channels, ffmpegdec->ffmpeg_layout,
579 ffmpegdec->info.position);
582 /* Mark corrupted frames as corrupted */
583 if (ffmpegdec->frame->flags & AV_FRAME_FLAG_CORRUPT)
584 GST_BUFFER_FLAG_SET (*outbuf, GST_BUFFER_FLAG_CORRUPTED);
590 av_frame_unref (ffmpegdec->frame);
591 GST_DEBUG_OBJECT (ffmpegdec, "return flow %d, out %p, len %d",
596 /* gst_ffmpegauddec_frame:
598 * data: pointer to the data to decode
599 * size: size of data in bytes
600 * got_data: 0 if no data was decoded, != 0 otherwise.
601 * in_time: timestamp of data
602 * in_duration: duration of data
603 * ret: GstFlowReturn to return in the chain function
605 * Decode the given frame and pushes it downstream.
607 * Returns: Number of bytes used in decoding, -1 on error/failure.
611 gst_ffmpegauddec_frame (GstFFMpegAudDec * ffmpegdec,
612 guint8 * data, guint size, gint * have_data, GstFlowReturn * ret)
614 GstFFMpegAudDecClass *oclass;
615 GstBuffer *outbuf = NULL;
618 if (G_UNLIKELY (ffmpegdec->context->codec == NULL))
621 GST_LOG_OBJECT (ffmpegdec, "data:%p, size:%d", data, size);
624 ffmpegdec->context->frame_number++;
626 oclass = (GstFFMpegAudDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
629 gst_ffmpegauddec_audio_frame (ffmpegdec, oclass->in_plugin, data, size,
630 have_data, &outbuf, ret);
633 GST_WARNING_OBJECT (ffmpegdec,
634 "avdec_%s: decoding error (len: %d, have_data: %d)",
635 oclass->in_plugin->name, len, *have_data);
640 GST_LOG_OBJECT (ffmpegdec, "Decoded data, now storing buffer %p", outbuf);
642 if (ffmpegdec->outbuf)
643 ffmpegdec->outbuf = gst_buffer_append (ffmpegdec->outbuf, outbuf);
645 ffmpegdec->outbuf = outbuf;
647 GST_DEBUG_OBJECT (ffmpegdec, "We didn't get a decoded buffer");
656 GST_ERROR_OBJECT (ffmpegdec, "no codec context");
662 gst_ffmpegauddec_drain (GstFFMpegAudDec * ffmpegdec)
664 GstFFMpegAudDecClass *oclass;
666 oclass = (GstFFMpegAudDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
668 if (oclass->in_plugin->capabilities & AV_CODEC_CAP_DELAY) {
671 GST_LOG_OBJECT (ffmpegdec,
672 "codec has delay capabilities, calling until libav has drained everything");
677 len = gst_ffmpegauddec_frame (ffmpegdec, NULL, 0, &have_data, &ret);
679 } while (len >= 0 && have_data == 1);
680 avcodec_flush_buffers (ffmpegdec->context);
683 if (ffmpegdec->outbuf)
684 gst_audio_decoder_finish_frame (GST_AUDIO_DECODER (ffmpegdec),
685 ffmpegdec->outbuf, 1);
686 ffmpegdec->outbuf = NULL;
690 gst_ffmpegauddec_flush (GstAudioDecoder * decoder, gboolean hard)
692 GstFFMpegAudDec *ffmpegdec = (GstFFMpegAudDec *) decoder;
694 if (ffmpegdec->opened) {
695 avcodec_flush_buffers (ffmpegdec->context);
700 gst_ffmpegauddec_handle_frame (GstAudioDecoder * decoder, GstBuffer * inbuf)
702 GstFFMpegAudDec *ffmpegdec;
703 GstFFMpegAudDecClass *oclass;
704 guint8 *data, *bdata;
706 gint size, bsize, len, have_data;
707 GstFlowReturn ret = GST_FLOW_OK;
708 gboolean do_padding, is_header;
710 ffmpegdec = (GstFFMpegAudDec *) decoder;
712 if (G_UNLIKELY (!ffmpegdec->opened))
716 gst_ffmpegauddec_drain (ffmpegdec);
720 inbuf = gst_buffer_ref (inbuf);
721 is_header = GST_BUFFER_FLAG_IS_SET (inbuf, GST_BUFFER_FLAG_HEADER);
723 oclass = (GstFFMpegAudDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
725 GST_LOG_OBJECT (ffmpegdec,
726 "Received new data of size %" G_GSIZE_FORMAT ", offset:%" G_GUINT64_FORMAT
727 ", ts:%" GST_TIME_FORMAT ", dur:%" GST_TIME_FORMAT,
728 gst_buffer_get_size (inbuf), GST_BUFFER_OFFSET (inbuf),
729 GST_TIME_ARGS (GST_BUFFER_PTS (inbuf)),
730 GST_TIME_ARGS (GST_BUFFER_DURATION (inbuf)));
732 /* workarounds, functions write to buffers:
733 * libavcodec/svq1.c:svq1_decode_frame writes to the given buffer.
734 * libavcodec/svq3.c:svq3_decode_slice_header too.
735 * ffmpeg devs know about it and will fix it (they said). */
736 if (oclass->in_plugin->id == AV_CODEC_ID_SVQ1 ||
737 oclass->in_plugin->id == AV_CODEC_ID_SVQ3) {
738 inbuf = gst_buffer_make_writable (inbuf);
741 gst_buffer_map (inbuf, &map, GST_MAP_READ);
746 if (bsize > 0 && (!GST_MEMORY_IS_ZERO_PADDED (map.memory)
747 || (map.maxsize - map.size) < AV_INPUT_BUFFER_PADDING_SIZE)) {
749 if (ffmpegdec->padded_size < bsize + AV_INPUT_BUFFER_PADDING_SIZE) {
750 ffmpegdec->padded_size = bsize + AV_INPUT_BUFFER_PADDING_SIZE;
751 ffmpegdec->padded = g_realloc (ffmpegdec->padded, ffmpegdec->padded_size);
752 GST_LOG_OBJECT (ffmpegdec, "resized padding buffer to %d",
753 ffmpegdec->padded_size);
755 GST_CAT_TRACE_OBJECT (CAT_PERFORMANCE, ffmpegdec,
756 "Copy input to add padding");
757 memcpy (ffmpegdec->padded, bdata, bsize);
758 memset (ffmpegdec->padded + bsize, 0, AV_INPUT_BUFFER_PADDING_SIZE);
760 bdata = ffmpegdec->padded;
767 guint8 tmp_padding[AV_INPUT_BUFFER_PADDING_SIZE];
773 /* add temporary padding */
774 GST_CAT_TRACE_OBJECT (CAT_PERFORMANCE, ffmpegdec,
775 "Add temporary input padding");
776 memcpy (tmp_padding, data + size, AV_INPUT_BUFFER_PADDING_SIZE);
777 memset (data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
780 /* decode a frame of audio now */
781 len = gst_ffmpegauddec_frame (ffmpegdec, data, size, &have_data, &ret);
784 memcpy (data + size, tmp_padding, AV_INPUT_BUFFER_PADDING_SIZE);
787 if (ret != GST_FLOW_OK) {
788 GST_LOG_OBJECT (ffmpegdec, "breaking because of flow ret %s",
789 gst_flow_get_name (ret));
790 /* bad flow return, make sure we discard all data and exit */
795 if (len == 0 && have_data == 0) {
796 /* nothing was decoded, this could be because no data was available or
797 * because we were skipping frames.
798 * If we have no context we must exit and wait for more data, we keep the
800 GST_LOG_OBJECT (ffmpegdec, "Decoding didn't return any data, breaking");
802 } else if (len < 0) {
803 /* a decoding error happened, we must break and try again with next data. */
804 GST_LOG_OBJECT (ffmpegdec, "Decoding error, breaking");
808 /* prepare for the next round, for codecs with a context we did this
809 * already when using the parser. */
815 GST_LOG_OBJECT (ffmpegdec, "Before (while bsize>0). bsize:%d , bdata:%p",
819 gst_buffer_unmap (inbuf, &map);
820 gst_buffer_unref (inbuf);
822 if (ffmpegdec->outbuf)
824 gst_audio_decoder_finish_frame (GST_AUDIO_DECODER (ffmpegdec),
825 ffmpegdec->outbuf, 1);
826 else if (len < 0 || is_header)
828 gst_audio_decoder_finish_frame (GST_AUDIO_DECODER (ffmpegdec), NULL, 1);
829 ffmpegdec->outbuf = NULL;
832 GST_DEBUG_OBJECT (ffmpegdec, "Dropping %d bytes of data", bsize);
840 oclass = (GstFFMpegAudDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
841 GST_ELEMENT_ERROR (ffmpegdec, CORE, NEGOTIATION, (NULL),
842 ("avdec_%s: input format was not set before data start",
843 oclass->in_plugin->name));
844 return GST_FLOW_NOT_NEGOTIATED;
849 gst_ffmpegauddec_register (GstPlugin * plugin)
851 GTypeInfo typeinfo = {
852 sizeof (GstFFMpegAudDecClass),
853 (GBaseInitFunc) gst_ffmpegauddec_base_init,
855 (GClassInitFunc) gst_ffmpegauddec_class_init,
858 sizeof (GstFFMpegAudDec),
860 (GInstanceInitFunc) gst_ffmpegauddec_init,
866 in_plugin = av_codec_next (NULL);
868 GST_LOG ("Registering decoders");
874 if (!av_codec_is_decoder (in_plugin)
875 || in_plugin->type != AVMEDIA_TYPE_AUDIO) {
879 /* no quasi codecs, please */
880 if (in_plugin->id == AV_CODEC_ID_PCM_S16LE_PLANAR ||
881 (in_plugin->id >= AV_CODEC_ID_PCM_S16LE &&
882 in_plugin->id <= AV_CODEC_ID_PCM_BLURAY) ||
883 (in_plugin->id >= AV_CODEC_ID_PCM_S8_PLANAR &&
884 #if AV_VERSION_INT (LIBAVCODEC_VERSION_MAJOR, LIBAVCODEC_VERSION_MINOR, LIBAVCODEC_VERSION_MICRO) >= AV_VERSION_INT (57,69,0)
885 in_plugin->id <= AV_CODEC_ID_PCM_F24LE)) {
886 #elif AV_VERSION_INT (LIBAVCODEC_VERSION_MAJOR, LIBAVCODEC_VERSION_MINOR, LIBAVCODEC_VERSION_MICRO) >= AV_VERSION_INT (57,54,0)
887 in_plugin->id <= AV_CODEC_ID_PCM_S64BE)) {
889 in_plugin->id <= AV_CODEC_ID_PCM_S16BE_PLANAR)) {
894 /* No decoders depending on external libraries (we don't build them, but
895 * people who build against an external ffmpeg might have them.
896 * We have native gstreamer plugins for all of those libraries anyway. */
897 if (!strncmp (in_plugin->name, "lib", 3)) {
899 ("Not using external library decoder %s. Use the gstreamer-native ones instead.",
904 GST_DEBUG ("Trying plugin %s [%s]", in_plugin->name, in_plugin->long_name);
906 /* no codecs for which we're GUARANTEED to have better alternatives */
907 /* MP1 : Use MP3 for decoding */
908 /* MP2 : Use MP3 for decoding */
909 /* Theora: Use libtheora based theoradec */
910 if (!strcmp (in_plugin->name, "vorbis") ||
911 !strcmp (in_plugin->name, "wavpack") ||
912 !strcmp (in_plugin->name, "mp1") ||
913 !strcmp (in_plugin->name, "mp2") ||
914 !strcmp (in_plugin->name, "libfaad") ||
915 !strcmp (in_plugin->name, "mpeg4aac") ||
916 !strcmp (in_plugin->name, "ass") ||
917 !strcmp (in_plugin->name, "srt") ||
918 !strcmp (in_plugin->name, "pgssub") ||
919 !strcmp (in_plugin->name, "dvdsub") ||
920 !strcmp (in_plugin->name, "dvbsub")) {
921 GST_LOG ("Ignoring decoder %s", in_plugin->name);
925 /* construct the type */
926 type_name = g_strdup_printf ("avdec_%s", in_plugin->name);
927 g_strdelimit (type_name, ".,|-<> ", '_');
929 type = g_type_from_name (type_name);
932 /* create the gtype now */
934 g_type_register_static (GST_TYPE_AUDIO_DECODER, type_name, &typeinfo,
936 g_type_set_qdata (type, GST_FFDEC_PARAMS_QDATA, (gpointer) in_plugin);
939 /* (Ronald) MPEG-4 gets a higher priority because it has been well-
940 * tested and by far outperforms divxdec/xviddec - so we prefer it.
941 * msmpeg4v3 same, as it outperforms divxdec for divx3 playback.
942 * VC1/WMV3 are not working and thus unpreferred for now. */
943 switch (in_plugin->id) {
944 case AV_CODEC_ID_RA_144:
945 case AV_CODEC_ID_RA_288:
946 case AV_CODEC_ID_COOK:
947 case AV_CODEC_ID_AAC:
948 rank = GST_RANK_PRIMARY;
950 /* SIPR: decoder should have a higher rank than realaudiodec.
952 case AV_CODEC_ID_SIPR:
953 rank = GST_RANK_SECONDARY;
956 rank = GST_RANK_MARGINAL;
959 if (!gst_element_register (plugin, type_name, rank, type)) {
960 g_warning ("Failed to register %s", type_name);
968 in_plugin = av_codec_next (in_plugin);
971 GST_LOG ("Finished Registering decoders");