Debian Codegraph @

ext/a52dec/gsta52dec.c

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