1 /* GStreamer
2  * Copyright (C) <2001> David I. Lehn <dlehn@users.sourceforge.net>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 
20 /**
21  * SECTION:element-a52dec
22  *
23  * Dolby Digital (AC-3) audio decoder.
24  *
25  * <refsect2>
26  * <title>Example launch line</title>
27  * |[
28  * gst-launch-1.0 dvdreadsrc title=1 ! mpegpsdemux ! a52dec ! audioconvert ! audioresample ! autoaudiosink
29  * ]| Play audio part of a dvd title.
30  * |[
31  * gst-launch-1.0 filesrc location=abc.ac3 ! ac3parse ! a52dec ! audioconvert ! audioresample ! autoaudiosink
32  * ]| Decode and play a stand alone AC-3 file.
33  * </refsect2>
34  */
35 
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
39 
40 #include <string.h>
41 #include <stdlib.h>
42 #ifdef HAVE_STDINT_H
43 #include <stdint.h>
44 #endif
45 
46 #include <gst/gst.h>
47 
48 #include <a52dec/a52.h>
49 #if !defined(A52_ACCEL_DETECT)
50 #  include <a52dec/mm_accel.h>
51 #endif
52 #include "gsta52dec.h"
53 
54 #if HAVE_ORC
55 #include <orc/orc.h>
56 #endif
57 
58 #ifdef LIBA52_DOUBLE
59 #define SAMPLE_WIDTH 64
60 #define SAMPLE_FORMAT GST_AUDIO_NE(F64)
61 #define SAMPLE_TYPE GST_AUDIO_FORMAT_F64
62 #else
63 #define SAMPLE_WIDTH 32
64 #define SAMPLE_FORMAT GST_AUDIO_NE(F32)
65 #define SAMPLE_TYPE GST_AUDIO_FORMAT_F32
66 #endif
67 
68 GST_DEBUG_CATEGORY_STATIC (a52dec_debug);
69 #define GST_CAT_DEFAULT (a52dec_debug)
70 
71 /* A52Dec args */
72 enum
73 {
74   ARG_0,
75   ARG_DRC,
76   ARG_MODE,
77   ARG_LFE,
78 };
79 
80 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
81     GST_PAD_SINK,
82     GST_PAD_ALWAYS,
83     GST_STATIC_CAPS ("audio/x-ac3; audio/ac3; audio/x-private1-ac3")
84     );
85 
86 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
87     GST_PAD_SRC,
88     GST_PAD_ALWAYS,
89     GST_STATIC_CAPS ("audio/x-raw, "
90         "format = (string) " SAMPLE_FORMAT ", "
91         "layout = (string) interleaved, "
92         "rate = (int) [ 4000, 96000 ], " "channels = (int) [ 1, 6 ]")
93     );
94 
95 #define gst_a52dec_parent_class parent_class
96 G_DEFINE_TYPE (GstA52Dec, gst_a52dec, GST_TYPE_AUDIO_DECODER);
97 
98 static gboolean gst_a52dec_start (GstAudioDecoder * dec);
99 static gboolean gst_a52dec_stop (GstAudioDecoder * dec);
100 static gboolean gst_a52dec_set_format (GstAudioDecoder * bdec, GstCaps * caps);
101 static GstFlowReturn gst_a52dec_parse (GstAudioDecoder * dec,
102     GstAdapter * adapter, gint * offset, gint * length);
103 static GstFlowReturn gst_a52dec_handle_frame (GstAudioDecoder * dec,
104     GstBuffer * buffer);
105 
106 static GstFlowReturn gst_a52dec_chain (GstPad * pad, GstObject * parent,
107     GstBuffer * buffer);
108 static void gst_a52dec_set_property (GObject * object, guint prop_id,
109     const GValue * value, GParamSpec * pspec);
110 static void gst_a52dec_get_property (GObject * object, guint prop_id,
111     GValue * value, GParamSpec * pspec);
112 
113 #define GST_TYPE_A52DEC_MODE (gst_a52dec_mode_get_type())
114 static GType
gst_a52dec_mode_get_type(void)115 gst_a52dec_mode_get_type (void)
116 {
117   static GType a52dec_mode_type = 0;
118   static const GEnumValue a52dec_modes[] = {
119     {A52_MONO, "Mono", "mono"},
120     {A52_STEREO, "Stereo", "stereo"},
121     {A52_3F, "3 Front", "3f"},
122     {A52_2F1R, "2 Front, 1 Rear", "2f1r"},
123     {A52_3F1R, "3 Front, 1 Rear", "3f1r"},
124     {A52_2F2R, "2 Front, 2 Rear", "2f2r"},
125     {A52_3F2R, "3 Front, 2 Rear", "3f2r"},
126     {A52_DOLBY, "Dolby", "dolby"},
127     {0, NULL, NULL},
128   };
129 
130   if (!a52dec_mode_type) {
131     a52dec_mode_type = g_enum_register_static ("GstA52DecMode", a52dec_modes);
132   }
133   return a52dec_mode_type;
134 }
135 
136 static void
gst_a52dec_class_init(GstA52DecClass * klass)137 gst_a52dec_class_init (GstA52DecClass * klass)
138 {
139   GObjectClass *gobject_class;
140   GstElementClass *gstelement_class;
141   GstAudioDecoderClass *gstbase_class;
142   guint cpuflags = 0;
143 
144   gobject_class = (GObjectClass *) klass;
145   gstelement_class = (GstElementClass *) klass;
146   gstbase_class = (GstAudioDecoderClass *) klass;
147 
148   gobject_class->set_property = gst_a52dec_set_property;
149   gobject_class->get_property = gst_a52dec_get_property;
150 
151   gstbase_class->start = GST_DEBUG_FUNCPTR (gst_a52dec_start);
152   gstbase_class->stop = GST_DEBUG_FUNCPTR (gst_a52dec_stop);
153   gstbase_class->set_format = GST_DEBUG_FUNCPTR (gst_a52dec_set_format);
154   gstbase_class->parse = GST_DEBUG_FUNCPTR (gst_a52dec_parse);
155   gstbase_class->handle_frame = GST_DEBUG_FUNCPTR (gst_a52dec_handle_frame);
156 
157   /**
158    * GstA52Dec::drc
159    *
160    * Set to true to apply the recommended Dolby Digital dynamic range compression
161    * to the audio stream. Dynamic range compression makes loud sounds
162    * softer and soft sounds louder, so you can more easily listen
163    * to the stream without disturbing other people.
164    */
165   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_DRC,
166       g_param_spec_boolean ("drc", "Dynamic Range Compression",
167           "Use Dynamic Range Compression", FALSE,
168           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
169   /**
170    * GstA52Dec::mode
171    *
172    * Force a particular output channel configuration from the decoder. By default,
173    * the channel downmix (if any) is chosen automatically based on the downstream
174    * capabilities of the pipeline.
175    */
176   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_MODE,
177       g_param_spec_enum ("mode", "Decoder Mode", "Decoding Mode (default 3f2r)",
178           GST_TYPE_A52DEC_MODE, A52_3F2R,
179           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
180   /**
181    * GstA52Dec::lfe
182    *
183    * Whether to output the LFE (Low Frequency Emitter) channel of the audio stream.
184    */
185   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_LFE,
186       g_param_spec_boolean ("lfe", "LFE", "LFE", TRUE,
187           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
188 
189   gst_element_class_add_static_pad_template (gstelement_class, &sink_factory);
190   gst_element_class_add_static_pad_template (gstelement_class, &src_factory);
191   gst_element_class_set_static_metadata (gstelement_class,
192       "ATSC A/52 audio decoder", "Codec/Decoder/Audio",
193       "Decodes ATSC A/52 encoded audio streams",
194       "David I. Lehn <dlehn@users.sourceforge.net>");
195 
196   GST_DEBUG_CATEGORY_INIT (a52dec_debug, "a52dec", 0,
197       "AC3/A52 software decoder");
198 
199   /* If no CPU instruction based acceleration is available, end up using the
200    * generic software djbfft based one when available in the used liba52 */
201 #ifdef MM_ACCEL_DJBFFT
202   klass->a52_cpuflags = MM_ACCEL_DJBFFT;
203 #elif defined(A52_ACCEL_DETECT)
204   klass->a52_cpuflags = A52_ACCEL_DETECT;
205 #else
206   klass->a52_cpuflags = 0;
207 #endif
208 
209 #if HAVE_ORC && !defined(A52_ACCEL_DETECT)
210   cpuflags = orc_target_get_default_flags (orc_target_get_by_name ("mmx"));
211   if (cpuflags & ORC_TARGET_MMX_MMX)
212     klass->a52_cpuflags |= MM_ACCEL_X86_MMX;
213   if (cpuflags & ORC_TARGET_MMX_3DNOW)
214     klass->a52_cpuflags |= MM_ACCEL_X86_3DNOW;
215   if (cpuflags & ORC_TARGET_MMX_MMXEXT)
216     klass->a52_cpuflags |= MM_ACCEL_X86_MMXEXT;
217 #endif
218 
219   GST_LOG ("CPU flags: a52=%08x, orc=%08x", klass->a52_cpuflags, cpuflags);
220 }
221 
222 static void
gst_a52dec_init(GstA52Dec * a52dec)223 gst_a52dec_init (GstA52Dec * a52dec)
224 {
225   a52dec->request_channels = A52_CHANNEL;
226   a52dec->dynamic_range_compression = FALSE;
227 
228   a52dec->state = NULL;
229   a52dec->samples = NULL;
230 
231   gst_audio_decoder_set_use_default_pad_acceptcaps (GST_AUDIO_DECODER_CAST
232       (a52dec), TRUE);
233   GST_PAD_SET_ACCEPT_TEMPLATE (GST_AUDIO_DECODER_SINK_PAD (a52dec));
234 
235   /* retrieve and intercept base class chain.
236    * Quite HACKish, but that's dvd specs/caps for you,
237    * since one buffer needs to be split into 2 frames */
238   a52dec->base_chain = GST_PAD_CHAINFUNC (GST_AUDIO_DECODER_SINK_PAD (a52dec));
239   gst_pad_set_chain_function (GST_AUDIO_DECODER_SINK_PAD (a52dec),
240       GST_DEBUG_FUNCPTR (gst_a52dec_chain));
241 }
242 
243 static gboolean
gst_a52dec_start(GstAudioDecoder * dec)244 gst_a52dec_start (GstAudioDecoder * dec)
245 {
246   GstA52Dec *a52dec = GST_A52DEC (dec);
247   GstA52DecClass *klass;
248   static GMutex init_mutex;
249 
250   GST_DEBUG_OBJECT (dec, "start");
251 
252   klass = GST_A52DEC_CLASS (G_OBJECT_GET_CLASS (a52dec));
253   g_mutex_lock (&init_mutex);
254 #if defined(A52_ACCEL_DETECT)
255   a52dec->state = a52_init ();
256   /* This line is just to avoid being accused of not using klass */
257   a52_accel (klass->a52_cpuflags & A52_ACCEL_DETECT);
258 #else
259   a52dec->state = a52_init (klass->a52_cpuflags);
260 #endif
261   g_mutex_unlock (&init_mutex);
262 
263   if (!a52dec->state) {
264     GST_ELEMENT_ERROR (GST_ELEMENT (a52dec), LIBRARY, INIT, (NULL),
265         ("failed to initialize a52 state"));
266     return FALSE;
267   }
268 
269   a52dec->samples = a52_samples (a52dec->state);
270   a52dec->bit_rate = -1;
271   a52dec->sample_rate = -1;
272   a52dec->stream_channels = A52_CHANNEL;
273   a52dec->using_channels = A52_CHANNEL;
274   a52dec->level = 1;
275   a52dec->bias = 0;
276   a52dec->flag_update = TRUE;
277 
278   /* call upon legacy upstream byte support (e.g. seeking) */
279   gst_audio_decoder_set_estimate_rate (dec, TRUE);
280 
281   return TRUE;
282 }
283 
284 static gboolean
gst_a52dec_stop(GstAudioDecoder * dec)285 gst_a52dec_stop (GstAudioDecoder * dec)
286 {
287   GstA52Dec *a52dec = GST_A52DEC (dec);
288 
289   GST_DEBUG_OBJECT (dec, "stop");
290 
291   a52dec->samples = NULL;
292   if (a52dec->state) {
293     a52_free (a52dec->state);
294     a52dec->state = NULL;
295   }
296 
297   return TRUE;
298 }
299 
300 static GstFlowReturn
gst_a52dec_parse(GstAudioDecoder * bdec,GstAdapter * adapter,gint * _offset,gint * len)301 gst_a52dec_parse (GstAudioDecoder * bdec, GstAdapter * adapter,
302     gint * _offset, gint * len)
303 {
304   GstA52Dec *a52dec;
305   const guint8 *data;
306   gint av, size;
307   gint length = 0, flags, sample_rate, bit_rate;
308   GstFlowReturn result = GST_FLOW_EOS;
309 
310   a52dec = GST_A52DEC (bdec);
311 
312   size = av = gst_adapter_available (adapter);
313   data = (const guint8 *) gst_adapter_map (adapter, av);
314 
315   /* find and read header */
316   bit_rate = a52dec->bit_rate;
317   sample_rate = a52dec->sample_rate;
318   flags = 0;
319   while (size >= 7) {
320     length = a52_syncinfo ((guint8 *) data, &flags, &sample_rate, &bit_rate);
321 
322     if (length == 0) {
323       /* shift window to re-find sync */
324       data++;
325       size--;
326     } else if (length <= size) {
327       GST_LOG_OBJECT (a52dec, "Sync: frame size %d", length);
328       result = GST_FLOW_OK;
329       break;
330     } else {
331       GST_LOG_OBJECT (a52dec, "Not enough data available (needed %d had %d)",
332           length, size);
333       break;
334     }
335   }
336   gst_adapter_unmap (adapter);
337 
338   *_offset = av - size;
339   *len = length;
340 
341   return result;
342 }
343 
344 static gint
gst_a52dec_channels(int flags,GstAudioChannelPosition * pos)345 gst_a52dec_channels (int flags, GstAudioChannelPosition * pos)
346 {
347   gint chans = 0;
348 
349   if (flags & A52_LFE) {
350     chans += 1;
351     if (pos) {
352       pos[0] = GST_AUDIO_CHANNEL_POSITION_LFE1;
353     }
354   }
355   flags &= A52_CHANNEL_MASK;
356   switch (flags) {
357     case A52_3F2R:
358       if (pos) {
359         pos[0 + chans] = GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT;
360         pos[1 + chans] = GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER;
361         pos[2 + chans] = GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT;
362         pos[3 + chans] = GST_AUDIO_CHANNEL_POSITION_REAR_LEFT;
363         pos[4 + chans] = GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT;
364       }
365       chans += 5;
366       break;
367     case A52_2F2R:
368       if (pos) {
369         pos[0 + chans] = GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT;
370         pos[1 + chans] = GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT;
371         pos[2 + chans] = GST_AUDIO_CHANNEL_POSITION_REAR_LEFT;
372         pos[3 + chans] = GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT;
373       }
374       chans += 4;
375       break;
376     case A52_3F1R:
377       if (pos) {
378         pos[0 + chans] = GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT;
379         pos[1 + chans] = GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER;
380         pos[2 + chans] = GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT;
381         pos[3 + chans] = GST_AUDIO_CHANNEL_POSITION_REAR_CENTER;
382       }
383       chans += 4;
384       break;
385     case A52_2F1R:
386       if (pos) {
387         pos[0 + chans] = GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT;
388         pos[1 + chans] = GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT;
389         pos[2 + chans] = GST_AUDIO_CHANNEL_POSITION_REAR_CENTER;
390       }
391       chans += 3;
392       break;
393     case A52_3F:
394       if (pos) {
395         pos[0 + chans] = GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT;
396         pos[1 + chans] = GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER;
397         pos[2 + chans] = GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT;
398       }
399       chans += 3;
400       break;
401     case A52_CHANNEL:          /* Dual mono. Should really be handled as 2 src pads */
402     case A52_STEREO:
403     case A52_DOLBY:
404       if (pos) {
405         pos[0 + chans] = GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT;
406         pos[1 + chans] = GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT;
407       }
408       chans += 2;
409       break;
410     case A52_MONO:
411       if (pos) {
412         pos[0 + chans] = GST_AUDIO_CHANNEL_POSITION_MONO;
413       }
414       chans += 1;
415       break;
416     default:
417       /* error, caller should post error message */
418       return 0;
419   }
420 
421   return chans;
422 }
423 
424 static gboolean
gst_a52dec_reneg(GstA52Dec * a52dec)425 gst_a52dec_reneg (GstA52Dec * a52dec)
426 {
427   gint channels;
428   gboolean result = FALSE;
429   GstAudioChannelPosition from[6], to[6];
430   GstAudioInfo info;
431 
432   channels = gst_a52dec_channels (a52dec->using_channels, from);
433 
434   if (!channels)
435     goto done;
436 
437   GST_INFO_OBJECT (a52dec, "reneg channels:%d rate:%d",
438       channels, a52dec->sample_rate);
439 
440   memcpy (to, from, sizeof (GstAudioChannelPosition) * channels);
441   gst_audio_channel_positions_to_valid_order (to, channels);
442   gst_audio_get_channel_reorder_map (channels, from, to,
443       a52dec->channel_reorder_map);
444 
445   gst_audio_info_init (&info);
446   gst_audio_info_set_format (&info,
447       SAMPLE_TYPE, a52dec->sample_rate, channels, (channels > 1 ? to : NULL));
448 
449   if (!gst_audio_decoder_set_output_format (GST_AUDIO_DECODER (a52dec), &info))
450     goto done;
451 
452   result = TRUE;
453 
454 done:
455   return result;
456 }
457 
458 static void
gst_a52dec_update_streaminfo(GstA52Dec * a52dec)459 gst_a52dec_update_streaminfo (GstA52Dec * a52dec)
460 {
461   GstTagList *taglist;
462 
463   taglist = gst_tag_list_new_empty ();
464   gst_tag_list_add (taglist, GST_TAG_MERGE_APPEND, GST_TAG_BITRATE,
465       (guint) a52dec->bit_rate, NULL);
466 
467   gst_audio_decoder_merge_tags (GST_AUDIO_DECODER (a52dec), taglist,
468       GST_TAG_MERGE_REPLACE);
469   gst_tag_list_unref (taglist);
470 }
471 
472 static GstFlowReturn
gst_a52dec_handle_frame(GstAudioDecoder * bdec,GstBuffer * buffer)473 gst_a52dec_handle_frame (GstAudioDecoder * bdec, GstBuffer * buffer)
474 {
475   GstA52Dec *a52dec;
476   gint channels, i;
477   gboolean need_reneg = FALSE;
478   gint chans;
479   gint length = 0, flags, sample_rate, bit_rate;
480   GstMapInfo map;
481   GstFlowReturn result = GST_FLOW_OK;
482   GstBuffer *outbuf;
483   const gint num_blocks = 6;
484 
485   a52dec = GST_A52DEC (bdec);
486 
487   /* no fancy draining */
488   if (G_UNLIKELY (!buffer))
489     return GST_FLOW_OK;
490 
491   /* parsed stuff already, so this should work out fine */
492   gst_buffer_map (buffer, &map, GST_MAP_READ);
493   g_assert (map.size >= 7);
494 
495   /* re-obtain some sync header info,
496    * should be same as during _parse and could also be cached there,
497    * but anyway ... */
498   bit_rate = a52dec->bit_rate;
499   sample_rate = a52dec->sample_rate;
500   flags = 0;
501   length = a52_syncinfo (map.data, &flags, &sample_rate, &bit_rate);
502   g_assert (length == map.size);
503 
504   /* update stream information, renegotiate or re-streaminfo if needed */
505   need_reneg = FALSE;
506   if (a52dec->sample_rate != sample_rate) {
507     GST_DEBUG_OBJECT (a52dec, "sample rate changed");
508     need_reneg = TRUE;
509     a52dec->sample_rate = sample_rate;
510   }
511 
512   if (flags) {
513     if (a52dec->stream_channels != (flags & (A52_CHANNEL_MASK | A52_LFE))) {
514       GST_DEBUG_OBJECT (a52dec, "stream channel flags changed, marking update");
515       a52dec->flag_update = TRUE;
516     }
517     a52dec->stream_channels = flags & (A52_CHANNEL_MASK | A52_LFE);
518   }
519 
520   if (bit_rate != a52dec->bit_rate) {
521     a52dec->bit_rate = bit_rate;
522     gst_a52dec_update_streaminfo (a52dec);
523   }
524 
525   /* If we haven't had an explicit number of channels chosen through properties
526    * at this point, choose what to downmix to now, based on what the peer will
527    * accept - this allows a52dec to do downmixing in preference to a
528    * downstream element such as audioconvert.
529    */
530   if (a52dec->request_channels != A52_CHANNEL) {
531     flags = a52dec->request_channels;
532   } else if (a52dec->flag_update) {
533     GstCaps *caps;
534 
535     a52dec->flag_update = FALSE;
536 
537     caps = gst_pad_get_allowed_caps (GST_AUDIO_DECODER_SRC_PAD (a52dec));
538     if (caps && gst_caps_get_size (caps) > 0) {
539       GstCaps *copy = gst_caps_copy_nth (caps, 0);
540       GstStructure *structure = gst_caps_get_structure (copy, 0);
541       gint orig_channels = flags ? gst_a52dec_channels (flags, NULL) : 6;
542       gint fixed_channels = 0;
543       const int a52_channels[6] = {
544         A52_MONO,
545         A52_STEREO,
546         A52_STEREO | A52_LFE,
547         A52_2F2R,
548         A52_2F2R | A52_LFE,
549         A52_3F2R | A52_LFE,
550       };
551 
552       /* Prefer the original number of channels, but fixate to something
553        * preferred (first in the caps) downstream if possible.
554        */
555       gst_structure_fixate_field_nearest_int (structure, "channels",
556           orig_channels);
557 
558       if (gst_structure_get_int (structure, "channels", &fixed_channels)
559           && fixed_channels <= 6) {
560         if (fixed_channels < orig_channels)
561           flags = a52_channels[fixed_channels - 1];
562       } else {
563         flags = a52_channels[5];
564       }
565 
566       gst_caps_unref (copy);
567     } else if (flags)
568       flags = a52dec->stream_channels;
569     else
570       flags = A52_3F2R | A52_LFE;
571 
572     if (caps)
573       gst_caps_unref (caps);
574   } else {
575     flags = a52dec->using_channels;
576   }
577 
578   /* process */
579   flags |= A52_ADJUST_LEVEL;
580   a52dec->level = 1;
581   if (a52_frame (a52dec->state, map.data, &flags, &a52dec->level, a52dec->bias)) {
582     gst_buffer_unmap (buffer, &map);
583     GST_AUDIO_DECODER_ERROR (a52dec, 1, STREAM, DECODE, (NULL),
584         ("a52_frame error"), result);
585     goto exit;
586   }
587   gst_buffer_unmap (buffer, &map);
588 
589   channels = flags & (A52_CHANNEL_MASK | A52_LFE);
590   if (a52dec->using_channels != channels) {
591     need_reneg = TRUE;
592     a52dec->using_channels = channels;
593   }
594 
595   /* negotiate if required */
596   if (need_reneg) {
597     GST_DEBUG_OBJECT (a52dec,
598         "a52dec reneg: sample_rate:%d stream_chans:%d using_chans:%d",
599         a52dec->sample_rate, a52dec->stream_channels, a52dec->using_channels);
600     if (!gst_a52dec_reneg (a52dec))
601       goto failed_negotiation;
602   }
603 
604   if (a52dec->dynamic_range_compression == FALSE) {
605     a52_dynrng (a52dec->state, NULL, NULL);
606   }
607 
608   flags &= (A52_CHANNEL_MASK | A52_LFE);
609   chans = gst_a52dec_channels (flags, NULL);
610   if (!chans)
611     goto invalid_flags;
612 
613   /* handle decoded data;
614    * each frame has 6 blocks, one block is 256 samples, ea */
615   outbuf =
616       gst_buffer_new_and_alloc (256 * chans * (SAMPLE_WIDTH / 8) * num_blocks);
617 
618   gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
619   {
620     guint8 *ptr = map.data;
621     for (i = 0; i < num_blocks; i++) {
622       if (a52_block (a52dec->state)) {
623         /* also marks discont */
624         GST_AUDIO_DECODER_ERROR (a52dec, 1, STREAM, DECODE, (NULL),
625             ("error decoding block %d", i), result);
626         if (result != GST_FLOW_OK) {
627           gst_buffer_unmap (outbuf, &map);
628           gst_buffer_unref (outbuf);
629           goto exit;
630         }
631       } else {
632         gint n, c;
633         gint *reorder_map = a52dec->channel_reorder_map;
634 
635         for (n = 0; n < 256; n++) {
636           for (c = 0; c < chans; c++) {
637             ((sample_t *) ptr)[n * chans + reorder_map[c]] =
638                 a52dec->samples[c * 256 + n];
639           }
640         }
641       }
642       ptr += 256 * chans * (SAMPLE_WIDTH / 8);
643     }
644   }
645   gst_buffer_unmap (outbuf, &map);
646 
647   result = gst_audio_decoder_finish_frame (bdec, outbuf, 1);
648 
649 exit:
650   return result;
651 
652   /* ERRORS */
653 failed_negotiation:
654   {
655     GST_ELEMENT_ERROR (a52dec, CORE, NEGOTIATION, (NULL), (NULL));
656     return GST_FLOW_ERROR;
657   }
658 invalid_flags:
659   {
660     GST_ELEMENT_ERROR (GST_ELEMENT (a52dec), STREAM, DECODE, (NULL),
661         ("Invalid channel flags: %d", flags));
662     return GST_FLOW_ERROR;
663   }
664 }
665 
666 static gboolean
gst_a52dec_set_format(GstAudioDecoder * bdec,GstCaps * caps)667 gst_a52dec_set_format (GstAudioDecoder * bdec, GstCaps * caps)
668 {
669   GstA52Dec *a52dec = GST_A52DEC (bdec);
670   GstStructure *structure;
671 
672   structure = gst_caps_get_structure (caps, 0);
673 
674   if (structure && gst_structure_has_name (structure, "audio/x-private1-ac3"))
675     a52dec->dvdmode = TRUE;
676   else
677     a52dec->dvdmode = FALSE;
678 
679   return TRUE;
680 }
681 
682 static GstFlowReturn
gst_a52dec_chain(GstPad * pad,GstObject * parent,GstBuffer * buf)683 gst_a52dec_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
684 {
685   GstA52Dec *a52dec = GST_A52DEC (parent);
686   GstFlowReturn ret = GST_FLOW_OK;
687   gint first_access;
688 
689   if (a52dec->dvdmode) {
690     gsize size;
691     guint8 data[2];
692     gint offset;
693     gint len;
694     GstBuffer *subbuf;
695 
696     size = gst_buffer_get_size (buf);
697     if (size < 2)
698       goto not_enough_data;
699 
700     gst_buffer_extract (buf, 0, data, 2);
701     first_access = (data[0] << 8) | data[1];
702 
703     /* Skip the first_access header */
704     offset = 2;
705 
706     if (first_access > 1) {
707       /* Length of data before first_access */
708       len = first_access - 1;
709 
710       if (len <= 0 || offset + len > size)
711         goto bad_first_access_parameter;
712 
713       subbuf = gst_buffer_copy_region (buf, GST_BUFFER_COPY_ALL, offset, len);
714       GST_BUFFER_TIMESTAMP (subbuf) = GST_CLOCK_TIME_NONE;
715       ret = a52dec->base_chain (pad, parent, subbuf);
716       if (ret != GST_FLOW_OK) {
717         gst_buffer_unref (buf);
718         goto done;
719       }
720 
721       offset += len;
722       len = size - offset;
723 
724       if (len > 0) {
725         subbuf = gst_buffer_copy_region (buf, GST_BUFFER_COPY_ALL, offset, len);
726         GST_BUFFER_TIMESTAMP (subbuf) = GST_BUFFER_TIMESTAMP (buf);
727 
728         ret = a52dec->base_chain (pad, parent, subbuf);
729       }
730       gst_buffer_unref (buf);
731     } else {
732       /* first_access = 0 or 1, so if there's a timestamp it applies to the first byte */
733       subbuf =
734           gst_buffer_copy_region (buf, GST_BUFFER_COPY_ALL, offset,
735           size - offset);
736       GST_BUFFER_TIMESTAMP (subbuf) = GST_BUFFER_TIMESTAMP (buf);
737       gst_buffer_unref (buf);
738       ret = a52dec->base_chain (pad, parent, subbuf);
739     }
740   } else {
741     ret = a52dec->base_chain (pad, parent, buf);
742   }
743 
744 done:
745   return ret;
746 
747 /* ERRORS */
748 not_enough_data:
749   {
750     GST_ELEMENT_ERROR (GST_ELEMENT (a52dec), STREAM, DECODE, (NULL),
751         ("Insufficient data in buffer. Can't determine first_acess"));
752     gst_buffer_unref (buf);
753     return GST_FLOW_ERROR;
754   }
755 bad_first_access_parameter:
756   {
757     GST_ELEMENT_ERROR (GST_ELEMENT (a52dec), STREAM, DECODE, (NULL),
758         ("Bad first_access parameter (%d) in buffer", first_access));
759     gst_buffer_unref (buf);
760     return GST_FLOW_ERROR;
761   }
762 }
763 
764 static void
gst_a52dec_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)765 gst_a52dec_set_property (GObject * object, guint prop_id, const GValue * value,
766     GParamSpec * pspec)
767 {
768   GstA52Dec *src = GST_A52DEC (object);
769 
770   switch (prop_id) {
771     case ARG_DRC:
772       GST_OBJECT_LOCK (src);
773       src->dynamic_range_compression = g_value_get_boolean (value);
774       GST_OBJECT_UNLOCK (src);
775       break;
776     case ARG_MODE:
777       GST_OBJECT_LOCK (src);
778       src->request_channels &= ~A52_CHANNEL_MASK;
779       src->request_channels |= g_value_get_enum (value);
780       GST_OBJECT_UNLOCK (src);
781       break;
782     case ARG_LFE:
783       GST_OBJECT_LOCK (src);
784       src->request_channels &= ~A52_LFE;
785       src->request_channels |= g_value_get_boolean (value) ? A52_LFE : 0;
786       GST_OBJECT_UNLOCK (src);
787       break;
788     default:
789       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
790       break;
791   }
792 }
793 
794 static void
gst_a52dec_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)795 gst_a52dec_get_property (GObject * object, guint prop_id, GValue * value,
796     GParamSpec * pspec)
797 {
798   GstA52Dec *src = GST_A52DEC (object);
799 
800   switch (prop_id) {
801     case ARG_DRC:
802       GST_OBJECT_LOCK (src);
803       g_value_set_boolean (value, src->dynamic_range_compression);
804       GST_OBJECT_UNLOCK (src);
805       break;
806     case ARG_MODE:
807       GST_OBJECT_LOCK (src);
808       g_value_set_enum (value, src->request_channels & A52_CHANNEL_MASK);
809       GST_OBJECT_UNLOCK (src);
810       break;
811     case ARG_LFE:
812       GST_OBJECT_LOCK (src);
813       g_value_set_boolean (value, src->request_channels & A52_LFE);
814       GST_OBJECT_UNLOCK (src);
815       break;
816     default:
817       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
818       break;
819   }
820 }
821 
822 static gboolean
plugin_init(GstPlugin * plugin)823 plugin_init (GstPlugin * plugin)
824 {
825 #if HAVE_ORC
826   orc_init ();
827 #endif
828 
829   if (!gst_element_register (plugin, "a52dec", GST_RANK_SECONDARY,
830           GST_TYPE_A52DEC))
831     return FALSE;
832 
833   return TRUE;
834 }
835 
836 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
837     GST_VERSION_MINOR,
838     a52dec,
839     "Decodes ATSC A/52 encoded audio streams",
840     plugin_init, VERSION, "GPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);
841