d9f4086e5745feb1e71f6b359a77a2b52a8be922
[platform/upstream/gstreamer.git] / ext / libav / gstavauddec.c
1 /* GStreamer
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>
5  *
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.
10  *
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.
15  *
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.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include <assert.h>
27 #include <string.h>
28
29 #include <libavcodec/avcodec.h>
30 #include <libavutil/channel_layout.h>
31
32 #include <gst/gst.h>
33
34 #include "gstav.h"
35 #include "gstavcodecmap.h"
36 #include "gstavutils.h"
37 #include "gstavauddec.h"
38
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,
45     GstQuery * query);
46
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,
51     GstCaps * caps);
52 static GstFlowReturn gst_ffmpegauddec_handle_frame (GstAudioDecoder * decoder,
53     GstBuffer * inbuf);
54
55 static gboolean gst_ffmpegauddec_negotiate (GstFFMpegAudDec * ffmpegdec,
56     AVCodecContext * context, AVFrame * frame, gboolean force);
57
58 static void gst_ffmpegauddec_drain (GstFFMpegAudDec * ffmpegdec);
59
60 #define GST_FFDEC_PARAMS_QDATA g_quark_from_static_string("avdec-params")
61
62 static GstElementClass *parent_class = NULL;
63
64 static void
65 gst_ffmpegauddec_base_init (GstFFMpegAudDecClass * klass)
66 {
67   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
68   GstPadTemplate *sinktempl, *srctempl;
69   GstCaps *sinkcaps, *srccaps;
70   AVCodec *in_plugin;
71   gchar *longname, *description;
72
73   in_plugin =
74       (AVCodec *) g_type_get_qdata (G_OBJECT_CLASS_TYPE (klass),
75       GST_FFDEC_PARAMS_QDATA);
76   g_assert (in_plugin != NULL);
77
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>");
86   g_free (longname);
87   g_free (description);
88
89   /* get the caps */
90   sinkcaps = gst_ffmpeg_codecid_to_caps (in_plugin->id, NULL, FALSE);
91   if (!sinkcaps) {
92     GST_DEBUG ("Couldn't get sink caps for decoder '%s'", in_plugin->name);
93     sinkcaps = gst_caps_from_string ("unknown/unknown");
94   }
95   srccaps = gst_ffmpeg_codectype_to_audio_caps (NULL,
96       in_plugin->id, FALSE, in_plugin);
97   if (!srccaps) {
98     GST_DEBUG ("Couldn't get source caps for decoder '%s'", in_plugin->name);
99     srccaps = gst_caps_from_string ("audio/x-raw");
100   }
101
102   /* pad templates */
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);
106
107   gst_element_class_add_pad_template (element_class, srctempl);
108   gst_element_class_add_pad_template (element_class, sinktempl);
109
110   gst_caps_unref (sinkcaps);
111   gst_caps_unref (srccaps);
112
113   klass->in_plugin = in_plugin;
114   klass->srctempl = srctempl;
115   klass->sinktempl = sinktempl;
116 }
117
118 static void
119 gst_ffmpegauddec_class_init (GstFFMpegAudDecClass * klass)
120 {
121   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
122   GstAudioDecoderClass *gstaudiodecoder_class = GST_AUDIO_DECODER_CLASS (klass);
123
124   parent_class = g_type_class_peek_parent (klass);
125
126   gobject_class->finalize = gst_ffmpegauddec_finalize;
127
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);
137 }
138
139 static void
140 gst_ffmpegauddec_init (GstFFMpegAudDec * ffmpegdec)
141 {
142   GstFFMpegAudDecClass *klass =
143       (GstFFMpegAudDecClass *) G_OBJECT_GET_CLASS (ffmpegdec);
144
145   /* some ffmpeg data */
146   ffmpegdec->context = avcodec_alloc_context3 (klass->in_plugin);
147   ffmpegdec->context->opaque = ffmpegdec;
148   ffmpegdec->opened = FALSE;
149
150   ffmpegdec->frame = av_frame_alloc ();
151
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
154       (ffmpegdec), TRUE);
155
156   gst_audio_decoder_set_drainable (GST_AUDIO_DECODER (ffmpegdec), TRUE);
157   gst_audio_decoder_set_needs_format (GST_AUDIO_DECODER (ffmpegdec), TRUE);
158 }
159
160 static void
161 gst_ffmpegauddec_finalize (GObject * object)
162 {
163   GstFFMpegAudDec *ffmpegdec = (GstFFMpegAudDec *) object;
164
165   av_frame_free (&ffmpegdec->frame);
166
167   if (ffmpegdec->context != NULL) {
168     gst_ffmpeg_avcodec_close (ffmpegdec->context);
169     av_free (ffmpegdec->context);
170     ffmpegdec->context = NULL;
171   }
172
173   G_OBJECT_CLASS (parent_class)->finalize (object);
174 }
175
176 /* With LOCK */
177 static gboolean
178 gst_ffmpegauddec_close (GstFFMpegAudDec * ffmpegdec, gboolean reset)
179 {
180   GstFFMpegAudDecClass *oclass;
181
182   oclass = (GstFFMpegAudDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
183
184   GST_LOG_OBJECT (ffmpegdec, "closing libav codec");
185
186   gst_caps_replace (&ffmpegdec->last_caps, NULL);
187   gst_buffer_replace (&ffmpegdec->outbuf, NULL);
188
189   gst_ffmpeg_avcodec_close (ffmpegdec->context);
190   ffmpegdec->opened = FALSE;
191
192   if (ffmpegdec->context->extradata) {
193     av_free (ffmpegdec->context->extradata);
194     ffmpegdec->context->extradata = NULL;
195   }
196
197   if (reset) {
198     if (avcodec_get_context_defaults3 (ffmpegdec->context,
199             oclass->in_plugin) < 0) {
200       GST_DEBUG_OBJECT (ffmpegdec, "Failed to set context defaults");
201       return FALSE;
202     }
203     ffmpegdec->context->opaque = ffmpegdec;
204   }
205
206   return TRUE;
207 }
208
209 static gboolean
210 gst_ffmpegauddec_start (GstAudioDecoder * decoder)
211 {
212   GstFFMpegAudDec *ffmpegdec = (GstFFMpegAudDec *) decoder;
213   GstFFMpegAudDecClass *oclass;
214
215   oclass = (GstFFMpegAudDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
216
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);
222     return FALSE;
223   }
224   ffmpegdec->context->opaque = ffmpegdec;
225   GST_OBJECT_UNLOCK (ffmpegdec);
226
227   return TRUE;
228 }
229
230 static gboolean
231 gst_ffmpegauddec_stop (GstAudioDecoder * decoder)
232 {
233   GstFFMpegAudDec *ffmpegdec = (GstFFMpegAudDec *) decoder;
234
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);
243
244   return TRUE;
245 }
246
247 /* with LOCK */
248 static gboolean
249 gst_ffmpegauddec_open (GstFFMpegAudDec * ffmpegdec)
250 {
251   GstFFMpegAudDecClass *oclass;
252
253   oclass = (GstFFMpegAudDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
254
255   if (gst_ffmpeg_avcodec_open (ffmpegdec->context, oclass->in_plugin) < 0)
256     goto could_not_open;
257
258   ffmpegdec->opened = TRUE;
259
260   GST_LOG_OBJECT (ffmpegdec, "Opened libav codec %s, id %d",
261       oclass->in_plugin->name, oclass->in_plugin->id);
262
263   gst_audio_info_init (&ffmpegdec->info);
264
265   return TRUE;
266
267   /* ERRORS */
268 could_not_open:
269   {
270     gst_ffmpegauddec_close (ffmpegdec, TRUE);
271     GST_DEBUG_OBJECT (ffmpegdec, "avdec_%s: Failed to open libav codec",
272         oclass->in_plugin->name);
273     return FALSE;
274   }
275 }
276
277 static gboolean
278 gst_ffmpegauddec_propose_allocation (GstAudioDecoder * decoder,
279     GstQuery * query)
280 {
281   GstAllocationParams params;
282
283   gst_allocation_params_init (&params);
284   params.flags = GST_MEMORY_FLAG_ZERO_PADDED;
285   params.align = 15;
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, &params);
290
291   return GST_AUDIO_DECODER_CLASS (parent_class)->propose_allocation (decoder,
292       query);
293 }
294
295 static gboolean
296 gst_ffmpegauddec_set_format (GstAudioDecoder * decoder, GstCaps * caps)
297 {
298   GstFFMpegAudDec *ffmpegdec = (GstFFMpegAudDec *) decoder;
299   GstFFMpegAudDecClass *oclass;
300   gboolean ret = TRUE;
301
302   oclass = (GstFFMpegAudDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
303
304   GST_DEBUG_OBJECT (ffmpegdec, "setcaps called");
305
306   GST_OBJECT_LOCK (ffmpegdec);
307
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);
311     return TRUE;
312   }
313
314   gst_caps_replace (&ffmpegdec->last_caps, caps);
315
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);
323       return FALSE;
324     }
325   }
326
327   /* get size and so */
328   gst_ffmpeg_caps_with_codecid (oclass->in_plugin->id,
329       oclass->in_plugin->type, caps, ffmpegdec->context);
330
331   /* workaround encoder bugs */
332   ffmpegdec->context->workaround_bugs |= FF_BUG_AUTODETECT;
333   ffmpegdec->context->err_recognition = 1;
334
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))
339     goto open_failed;
340
341 done:
342   GST_OBJECT_UNLOCK (ffmpegdec);
343
344   return ret;
345
346   /* ERRORS */
347 open_failed:
348   {
349     GST_DEBUG_OBJECT (ffmpegdec, "Failed to open");
350     ret = FALSE;
351     goto done;
352   }
353 }
354
355 static gboolean
356 settings_changed (GstFFMpegAudDec * ffmpegdec, AVFrame * frame)
357 {
358   GstAudioFormat format;
359   gint channels =
360       av_get_channel_layout_nb_channels (av_frame_get_channel_layout (frame));
361
362   format = gst_ffmpeg_smpfmt_to_audioformat (frame->format);
363   if (format == GST_AUDIO_FORMAT_UNKNOWN)
364     return TRUE;
365
366   return !(ffmpegdec->info.rate ==
367       av_frame_get_sample_rate (frame) &&
368       ffmpegdec->info.channels == channels &&
369       ffmpegdec->info.finfo->format == format);
370 }
371
372 static gboolean
373 gst_ffmpegauddec_negotiate (GstFFMpegAudDec * ffmpegdec,
374     AVCodecContext * context, AVFrame * frame, gboolean force)
375 {
376   GstFFMpegAudDecClass *oclass;
377   GstAudioFormat format;
378   gint channels;
379   GstAudioChannelPosition pos[64] = { 0, };
380
381   oclass = (GstFFMpegAudDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
382
383   format = gst_ffmpeg_smpfmt_to_audioformat (frame->format);
384   if (format == GST_AUDIO_FORMAT_UNKNOWN)
385     goto no_caps;
386   channels =
387       av_get_channel_layout_nb_channels (av_frame_get_channel_layout (frame));
388   if (channels == 0)
389     channels = av_frame_get_channels (frame);
390   if (channels == 0)
391     goto no_caps;
392
393   if (!force && !settings_changed (ffmpegdec, frame))
394     return TRUE;
395
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,
400       format);
401
402   gst_ffmpeg_channel_layout_to_gst (av_frame_get_channel_layout (frame),
403       channels, pos);
404   memcpy (ffmpegdec->ffmpeg_layout, pos,
405       sizeof (GstAudioChannelPosition) * channels);
406
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);
413
414   if (!gst_audio_decoder_set_output_format (GST_AUDIO_DECODER (ffmpegdec),
415           &ffmpegdec->info))
416     goto caps_failed;
417
418   return TRUE;
419
420   /* ERRORS */
421 no_caps:
422   {
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));
428 #else
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));
438 #endif
439     return FALSE;
440   }
441 caps_failed:
442   {
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));
447
448     return FALSE;
449   }
450 }
451
452 static void
453 gst_avpacket_init (AVPacket * packet, guint8 * data, guint size)
454 {
455   memset (packet, 0, sizeof (AVPacket));
456   packet->data = data;
457   packet->size = size;
458 }
459
460 static gint
461 gst_ffmpegauddec_audio_frame (GstFFMpegAudDec * ffmpegdec,
462     AVCodec * in_plugin, guint8 * data, guint size, gint * have_data,
463     GstBuffer ** outbuf, GstFlowReturn * ret)
464 {
465   gint len = -1;
466   AVPacket packet;
467
468   GST_DEBUG_OBJECT (ffmpegdec, "size: %d", size);
469
470   gst_avpacket_init (&packet, data, size);
471   len =
472       avcodec_decode_audio4 (ffmpegdec->context, ffmpegdec->frame, have_data,
473       &packet);
474
475   GST_DEBUG_OBJECT (ffmpegdec,
476       "Decode audio: len=%d, have_data=%d", len, *have_data);
477
478   if (len >= 0 && *have_data) {
479     gint nsamples, channels, byte_per_sample;
480     gsize output_size;
481
482     if (!gst_ffmpegauddec_negotiate (ffmpegdec, ffmpegdec->context,
483             ffmpegdec->frame, FALSE)) {
484       *outbuf = NULL;
485       *ret = GST_FLOW_NOT_NEGOTIATED;
486       len = -1;
487       goto beach;
488     }
489
490     channels = ffmpegdec->info.channels;
491     nsamples = ffmpegdec->frame->nb_samples;
492     byte_per_sample = ffmpegdec->info.finfo->width / 8;
493
494     /* ffmpegdec->frame->linesize[0] might contain padding, allocate only what's needed */
495     output_size = nsamples * byte_per_sample * channels;
496
497     GST_DEBUG_OBJECT (ffmpegdec, "Creating output buffer");
498     if (av_sample_fmt_is_planar (ffmpegdec->context->sample_fmt)
499         && channels > 1) {
500       gint i, j;
501       GstMapInfo minfo;
502
503       /* note: linesize[0] might contain padding, allocate only what's needed */
504       *outbuf =
505           gst_audio_decoder_allocate_output_buffer (GST_AUDIO_DECODER
506           (ffmpegdec), output_size);
507
508       gst_buffer_map (*outbuf, &minfo, GST_MAP_WRITE);
509
510       switch (ffmpegdec->info.finfo->width) {
511         case 8:{
512           guint8 *odata = minfo.data;
513
514           for (i = 0; i < nsamples; i++) {
515             for (j = 0; j < channels; j++) {
516               odata[j] =
517                   ((const guint8 *) ffmpegdec->frame->extended_data[j])[i];
518             }
519             odata += channels;
520           }
521           break;
522         }
523         case 16:{
524           guint16 *odata = (guint16 *) minfo.data;
525
526           for (i = 0; i < nsamples; i++) {
527             for (j = 0; j < channels; j++) {
528               odata[j] =
529                   ((const guint16 *) ffmpegdec->frame->extended_data[j])[i];
530             }
531             odata += channels;
532           }
533           break;
534         }
535         case 32:{
536           guint32 *odata = (guint32 *) minfo.data;
537
538           for (i = 0; i < nsamples; i++) {
539             for (j = 0; j < channels; j++) {
540               odata[j] =
541                   ((const guint32 *) ffmpegdec->frame->extended_data[j])[i];
542             }
543             odata += channels;
544           }
545           break;
546         }
547         case 64:{
548           guint64 *odata = (guint64 *) minfo.data;
549
550           for (i = 0; i < nsamples; i++) {
551             for (j = 0; j < channels; j++) {
552               odata[j] =
553                   ((const guint64 *) ffmpegdec->frame->extended_data[j])[i];
554             }
555             odata += channels;
556           }
557           break;
558         }
559         default:
560           g_assert_not_reached ();
561           break;
562       }
563       gst_buffer_unmap (*outbuf, &minfo);
564     } else {
565       *outbuf =
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);
569     }
570
571     GST_DEBUG_OBJECT (ffmpegdec, "Buffer created. Size: %" G_GSIZE_FORMAT,
572         output_size);
573
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);
580     }
581
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);
585   } else {
586     *outbuf = NULL;
587   }
588
589 beach:
590   av_frame_unref (ffmpegdec->frame);
591   GST_DEBUG_OBJECT (ffmpegdec, "return flow %d, out %p, len %d",
592       *ret, *outbuf, len);
593   return len;
594 }
595
596 /* gst_ffmpegauddec_frame:
597  * ffmpegdec:
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
604  *
605  * Decode the given frame and pushes it downstream.
606  *
607  * Returns: Number of bytes used in decoding, -1 on error/failure.
608  */
609
610 static gint
611 gst_ffmpegauddec_frame (GstFFMpegAudDec * ffmpegdec,
612     guint8 * data, guint size, gint * have_data, GstFlowReturn * ret)
613 {
614   GstFFMpegAudDecClass *oclass;
615   GstBuffer *outbuf = NULL;
616   gint len = 0;
617
618   if (G_UNLIKELY (ffmpegdec->context->codec == NULL))
619     goto no_codec;
620
621   GST_LOG_OBJECT (ffmpegdec, "data:%p, size:%d", data, size);
622
623   *ret = GST_FLOW_OK;
624   ffmpegdec->context->frame_number++;
625
626   oclass = (GstFFMpegAudDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
627
628   len =
629       gst_ffmpegauddec_audio_frame (ffmpegdec, oclass->in_plugin, data, size,
630       have_data, &outbuf, ret);
631
632   if (len < 0) {
633     GST_WARNING_OBJECT (ffmpegdec,
634         "avdec_%s: decoding error (len: %d, have_data: %d)",
635         oclass->in_plugin->name, len, *have_data);
636     goto beach;
637   }
638
639   if (outbuf) {
640     GST_LOG_OBJECT (ffmpegdec, "Decoded data, now storing buffer %p", outbuf);
641
642     if (ffmpegdec->outbuf)
643       ffmpegdec->outbuf = gst_buffer_append (ffmpegdec->outbuf, outbuf);
644     else
645       ffmpegdec->outbuf = outbuf;
646   } else {
647     GST_DEBUG_OBJECT (ffmpegdec, "We didn't get a decoded buffer");
648   }
649
650 beach:
651   return len;
652
653   /* ERRORS */
654 no_codec:
655   {
656     GST_ERROR_OBJECT (ffmpegdec, "no codec context");
657     return -1;
658   }
659 }
660
661 static void
662 gst_ffmpegauddec_drain (GstFFMpegAudDec * ffmpegdec)
663 {
664   GstFFMpegAudDecClass *oclass;
665
666   oclass = (GstFFMpegAudDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
667
668   if (oclass->in_plugin->capabilities & CODEC_CAP_DELAY) {
669     gint have_data, len;
670
671     GST_LOG_OBJECT (ffmpegdec,
672         "codec has delay capabilities, calling until libav has drained everything");
673
674     do {
675       GstFlowReturn ret;
676
677       len = gst_ffmpegauddec_frame (ffmpegdec, NULL, 0, &have_data, &ret);
678
679     } while (len >= 0 && have_data == 1);
680     avcodec_flush_buffers (ffmpegdec->context);
681   }
682
683   if (ffmpegdec->outbuf)
684     gst_audio_decoder_finish_frame (GST_AUDIO_DECODER (ffmpegdec),
685         ffmpegdec->outbuf, 1);
686   ffmpegdec->outbuf = NULL;
687 }
688
689 static void
690 gst_ffmpegauddec_flush (GstAudioDecoder * decoder, gboolean hard)
691 {
692   GstFFMpegAudDec *ffmpegdec = (GstFFMpegAudDec *) decoder;
693
694   if (ffmpegdec->opened) {
695     avcodec_flush_buffers (ffmpegdec->context);
696   }
697 }
698
699 static GstFlowReturn
700 gst_ffmpegauddec_handle_frame (GstAudioDecoder * decoder, GstBuffer * inbuf)
701 {
702   GstFFMpegAudDec *ffmpegdec;
703   GstFFMpegAudDecClass *oclass;
704   guint8 *data, *bdata;
705   GstMapInfo map;
706   gint size, bsize, len, have_data;
707   GstFlowReturn ret = GST_FLOW_OK;
708   gboolean do_padding, is_header;
709
710   ffmpegdec = (GstFFMpegAudDec *) decoder;
711
712   if (G_UNLIKELY (!ffmpegdec->opened))
713     goto not_negotiated;
714
715   if (inbuf == NULL) {
716     gst_ffmpegauddec_drain (ffmpegdec);
717     return GST_FLOW_OK;
718   }
719
720   inbuf = gst_buffer_ref (inbuf);
721   is_header = GST_BUFFER_FLAG_IS_SET (inbuf, GST_BUFFER_FLAG_HEADER);
722
723   oclass = (GstFFMpegAudDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
724
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)));
731
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);
739   }
740
741   gst_buffer_map (inbuf, &map, GST_MAP_READ);
742
743   bdata = map.data;
744   bsize = map.size;
745
746   if (bsize > 0 && (!GST_MEMORY_IS_ZERO_PADDED (map.memory)
747           || (map.maxsize - map.size) < AV_INPUT_BUFFER_PADDING_SIZE)) {
748     /* add padding */
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);
754     }
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);
759
760     bdata = ffmpegdec->padded;
761     do_padding = TRUE;
762   } else {
763     do_padding = FALSE;
764   }
765
766   do {
767     guint8 tmp_padding[AV_INPUT_BUFFER_PADDING_SIZE];
768
769     data = bdata;
770     size = bsize;
771
772     if (do_padding) {
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);
778     }
779
780     /* decode a frame of audio now */
781     len = gst_ffmpegauddec_frame (ffmpegdec, data, size, &have_data, &ret);
782
783     if (do_padding) {
784       memcpy (data + size, tmp_padding, AV_INPUT_BUFFER_PADDING_SIZE);
785     }
786
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 */
791       bsize = 0;
792       break;
793     }
794
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
799        * data we tried. */
800       GST_LOG_OBJECT (ffmpegdec, "Decoding didn't return any data, breaking");
801       break;
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");
805       bsize = 0;
806       break;
807     }
808     /* prepare for the next round, for codecs with a context we did this
809      * already when using the parser. */
810     bsize -= len;
811     bdata += len;
812
813     do_padding = TRUE;
814
815     GST_LOG_OBJECT (ffmpegdec, "Before (while bsize>0).  bsize:%d , bdata:%p",
816         bsize, bdata);
817   } while (bsize > 0);
818
819   gst_buffer_unmap (inbuf, &map);
820   gst_buffer_unref (inbuf);
821
822   if (ffmpegdec->outbuf)
823     ret =
824         gst_audio_decoder_finish_frame (GST_AUDIO_DECODER (ffmpegdec),
825         ffmpegdec->outbuf, 1);
826   else if (len < 0 || is_header)
827     ret =
828         gst_audio_decoder_finish_frame (GST_AUDIO_DECODER (ffmpegdec), NULL, 1);
829   ffmpegdec->outbuf = NULL;
830
831   if (bsize > 0) {
832     GST_DEBUG_OBJECT (ffmpegdec, "Dropping %d bytes of data", bsize);
833   }
834
835   return ret;
836
837   /* ERRORS */
838 not_negotiated:
839   {
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;
845   }
846 }
847
848 gboolean
849 gst_ffmpegauddec_register (GstPlugin * plugin)
850 {
851   GTypeInfo typeinfo = {
852     sizeof (GstFFMpegAudDecClass),
853     (GBaseInitFunc) gst_ffmpegauddec_base_init,
854     NULL,
855     (GClassInitFunc) gst_ffmpegauddec_class_init,
856     NULL,
857     NULL,
858     sizeof (GstFFMpegAudDec),
859     0,
860     (GInstanceInitFunc) gst_ffmpegauddec_init,
861   };
862   GType type;
863   AVCodec *in_plugin;
864   gint rank;
865
866   in_plugin = av_codec_next (NULL);
867
868   GST_LOG ("Registering decoders");
869
870   while (in_plugin) {
871     gchar *type_name;
872
873     /* only decoders */
874     if (!av_codec_is_decoder (in_plugin)
875         || in_plugin->type != AVMEDIA_TYPE_AUDIO) {
876       goto next;
877     }
878
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)) {
888 #else
889             in_plugin->id <= AV_CODEC_ID_PCM_S16BE_PLANAR)) {
890 #endif
891       goto next;
892     }
893
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)) {
898       GST_DEBUG
899           ("Not using external library decoder %s. Use the gstreamer-native ones instead.",
900           in_plugin->name);
901       goto next;
902     }
903
904     GST_DEBUG ("Trying plugin %s [%s]", in_plugin->name, in_plugin->long_name);
905
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);
922       goto next;
923     }
924
925     /* construct the type */
926     type_name = g_strdup_printf ("avdec_%s", in_plugin->name);
927     g_strdelimit (type_name, ".,|-<> ", '_');
928
929     type = g_type_from_name (type_name);
930
931     if (!type) {
932       /* create the gtype now */
933       type =
934           g_type_register_static (GST_TYPE_AUDIO_DECODER, type_name, &typeinfo,
935           0);
936       g_type_set_qdata (type, GST_FFDEC_PARAMS_QDATA, (gpointer) in_plugin);
937     }
938
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;
949         break;
950         /* SIPR: decoder should have a higher rank than realaudiodec.
951          */
952       case AV_CODEC_ID_SIPR:
953         rank = GST_RANK_SECONDARY;
954         break;
955       default:
956         rank = GST_RANK_MARGINAL;
957         break;
958     }
959     if (!gst_element_register (plugin, type_name, rank, type)) {
960       g_warning ("Failed to register %s", type_name);
961       g_free (type_name);
962       return FALSE;
963     }
964
965     g_free (type_name);
966
967   next:
968     in_plugin = av_codec_next (in_plugin);
969   }
970
971   GST_LOG ("Finished Registering decoders");
972
973   return TRUE;
974 }