demux: mp4: avoid audio cuts on seek
[vlc.git] / modules / codec / lpcm.c
blobb5ed1903642521cb5ab394025fc7a2424e2640ad
1 /*****************************************************************************
2 * lpcm.c: lpcm decoder/packetizer module
3 *****************************************************************************
4 * Copyright (C) 1999-2008 VLC authors and VideoLAN
5 * $Id$
7 * Authors: Samuel Hocevar <sam@zoy.org>
8 * Henri Fallon <henri@videolan.org>
9 * Christophe Massiot <massiot@via.ecp.fr>
10 * Gildas Bazin <gbazin@videolan.org>
11 * Lauren Aimar <fenrir _AT_ videolan _DOT_ org >
12 * Steinar H. Gunderson <steinar+vlc@gunderson.no>
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU Lesser General Public License as published by
16 * the Free Software Foundation; either version 2.1 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Lesser General Public License for more details.
24 * You should have received a copy of the GNU Lesser General Public License
25 * along with this program; if not, write to the Free Software Foundation,
26 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
27 *****************************************************************************/
29 /*****************************************************************************
30 * Preamble
31 *****************************************************************************/
32 #ifdef HAVE_CONFIG_H
33 # include "config.h"
34 #endif
36 #include <vlc_common.h>
37 #include <vlc_plugin.h>
38 #include <vlc_codec.h>
39 #include <vlc_aout.h>
40 #include <unistd.h>
41 #include <assert.h>
43 /*****************************************************************************
44 * Module descriptor
45 *****************************************************************************/
46 static int OpenDecoder ( vlc_object_t * );
47 static int OpenPacketizer( vlc_object_t * );
48 static void CloseCommon ( vlc_object_t * );
50 #ifdef ENABLE_SOUT
51 static int OpenEncoder ( vlc_object_t * );
52 static void CloseEncoder ( vlc_object_t * );
53 static block_t *EncodeFrames( encoder_t *, block_t * );
54 #endif
56 vlc_module_begin ()
58 set_category( CAT_INPUT )
59 set_subcategory( SUBCAT_INPUT_ACODEC )
60 set_description( N_("Linear PCM audio decoder") )
61 set_capability( "audio decoder", 100 )
62 set_callbacks( OpenDecoder, CloseCommon )
64 add_submodule ()
65 set_description( N_("Linear PCM audio packetizer") )
66 set_capability( "packetizer", 100 )
67 set_callbacks( OpenPacketizer, CloseCommon )
69 #ifdef ENABLE_SOUT
70 add_submodule ()
71 set_description( N_("Linear PCM audio encoder") )
72 set_capability( "encoder", 100 )
73 set_callbacks( OpenEncoder, CloseEncoder )
74 add_shortcut( "lpcm" )
75 #endif
77 vlc_module_end ()
80 /*****************************************************************************
81 * decoder_sys_t : lpcm decoder descriptor
82 *****************************************************************************/
83 struct decoder_sys_t
85 /* Module mode */
86 bool b_packetizer;
89 * Output properties
91 date_t end_date;
93 /* */
94 unsigned i_header_size;
95 int i_type;
96 uint8_t i_chans_to_reorder;
97 uint8_t pi_chan_table[AOUT_CHAN_MAX];
100 #ifdef ENABLE_SOUT
101 struct encoder_sys_t
103 int i_channels;
104 int i_rate;
106 int i_frame_samples;
107 uint8_t *p_buffer;
108 int i_buffer_used;
109 int i_frame_num;
111 #endif
114 * LPCM DVD header :
115 * - number of frames in this packet (8 bits)
116 * - first access unit (16 bits) == 0x0003 ?
117 * - emphasis (1 bit)
118 * - mute (1 bit)
119 * - reserved (1 bit)
120 * - current frame (5 bits)
121 * - quantisation (2 bits) 0 == 16bps, 1 == 20bps, 2 == 24bps, 3 == illegal
122 * - frequency (2 bits) 0 == 48 kHz, 1 == 96 kHz, 2 == 44.1 kHz, 3 == 32 kHz
123 * - reserved (1 bit)
124 * - number of channels - 1 (3 bits) 1 == 2 channels
125 * - dynamic range (8 bits) 0x80 == neutral
127 * LPCM DVD-A header (http://dvd-audio.sourceforge.net/spec/aob.shtml)
128 * - continuity counter (8 bits, clipped to 0x00-0x1f)
129 * - header size (16 bits)
130 * - byte pointer to start of first audio frame.
131 * - unknown (8bits, 0x10 for stereo, 0x00 for surround)
132 * - sample size (4+4 bits)
133 * - samplerate (4+4 bits)
134 * - unknown (8 bits)
135 * - group assignment (8 bits)
136 * - unknown (8 bits)
137 * - padding(variable)
139 * LPCM BD header :
140 * - unknown (16 bits)
141 * - number of channels (4 bits)
142 * - frequency (4 bits)
143 * - bits per sample (2 bits)
144 * - unknown (6 bits)
146 * LPCM WIDI header
147 * refers http://www.dvdforum.org/images/Guideline1394V10R0_20020911.pdf
148 * - sub stream id (8 bits) = 0xa0
149 * - frame header count (8 bits) = 0x06
150 * [ 0b0000000 (7 bits)
151 * - audio emphasis (1 bit) ] (8 bits)
152 * [ qz word length (2 bits) 0x00 == 16bits
153 * - sampling freq (3 bits) 0b001 == 44.1K, 0b010 == 48K Hz
154 * - channels count(3 bits) ] (8 bits) 0b000 == dual mono, 0b001 == stereo
155 * follows: LPCM data (15360 bits/1920 bytes)
158 #define LPCM_VOB_HEADER_LEN (6)
159 #define LPCM_AOB_HEADER_LEN (11)
160 #define LPCM_BD_HEADER_LEN (4)
161 #define LPCM_WIDI_HEADER_LEN (4)
163 enum
165 LPCM_VOB,
166 LPCM_AOB,
167 LPCM_BD,
168 LPCM_WIDI,
171 typedef struct
173 unsigned i_channels;
174 unsigned i_bits;
175 unsigned pi_position[6];
176 } aob_group_t;
178 /*****************************************************************************
179 * Local prototypes
180 *****************************************************************************/
181 static int DecodeFrame ( decoder_t *, block_t * );
182 static block_t *Packetize ( decoder_t *, block_t ** );
183 static void Flush( decoder_t * );
185 /* */
186 static int VobHeader( unsigned *pi_rate,
187 unsigned *pi_channels, unsigned *pi_original_channels,
188 unsigned *pi_bits,
189 const uint8_t *p_header );
190 static void VobExtract( block_t *, block_t *, unsigned i_bits );
191 /* */
192 static int AobHeader( unsigned *pi_rate,
193 unsigned *pi_channels, unsigned *pi_layout,
194 unsigned *pi_bits,
195 unsigned *pi_padding,
196 aob_group_t g[2],
197 const uint8_t *p_header );
198 static void AobExtract( block_t *, block_t *, unsigned i_bits, aob_group_t p_group[2] );
199 /* */
200 static int BdHeader( decoder_sys_t *p_sys,
201 unsigned *pi_rate,
202 unsigned *pi_channels,
203 unsigned *pi_channels_padding,
204 unsigned *pi_original_channels,
205 unsigned *pi_bits,
206 const uint8_t *p_header );
207 static void BdExtract( block_t *, block_t *, unsigned, unsigned, unsigned, unsigned );
208 /* */
209 static int WidiHeader( unsigned *pi_rate,
210 unsigned *pi_channels, unsigned *pi_original_channels,
211 unsigned *pi_bits,
212 const uint8_t *p_header );
214 /*****************************************************************************
215 * OpenCommon:
216 *****************************************************************************/
217 static int OpenCommon( decoder_t *p_dec, bool b_packetizer )
219 decoder_sys_t *p_sys;
220 int i_type;
221 int i_header_size;
223 switch( p_dec->fmt_in.i_codec )
225 /* DVD LPCM */
226 case VLC_CODEC_DVD_LPCM:
227 i_type = LPCM_VOB;
228 i_header_size = LPCM_VOB_HEADER_LEN;
229 break;
230 /* DVD-Audio LPCM */
231 case VLC_CODEC_DVDA_LPCM:
232 i_type = LPCM_AOB;
233 i_header_size = LPCM_AOB_HEADER_LEN;
234 break;
235 /* BD LPCM */
236 case VLC_CODEC_BD_LPCM:
237 i_type = LPCM_BD;
238 i_header_size = LPCM_BD_HEADER_LEN;
239 break;
240 /* WIDI LPCM */
241 case VLC_CODEC_WIDI_LPCM:
242 i_type = LPCM_WIDI;
243 i_header_size = LPCM_WIDI_HEADER_LEN;
244 break;
245 default:
246 return VLC_EGENERIC;
249 /* Allocate the memory needed to store the decoder's structure */
250 if( ( p_dec->p_sys = p_sys = malloc(sizeof(decoder_sys_t)) ) == NULL )
251 return VLC_ENOMEM;
253 /* Misc init */
254 p_sys->b_packetizer = b_packetizer;
255 date_Set( &p_sys->end_date, 0 );
256 p_sys->i_type = i_type;
257 p_sys->i_header_size = i_header_size;
258 p_sys->i_chans_to_reorder = 0;
260 /* Set output properties */
261 if( b_packetizer )
263 switch( i_type )
265 case LPCM_VOB:
266 p_dec->fmt_out.i_codec = VLC_CODEC_DVD_LPCM;
267 break;
268 case LPCM_AOB:
269 p_dec->fmt_out.i_codec = VLC_CODEC_DVDA_LPCM;
270 break;
271 case LPCM_WIDI:
272 p_dec->fmt_out.i_codec = VLC_CODEC_WIDI_LPCM;
273 break;
274 default:
275 vlc_assert_unreachable();
276 case LPCM_BD:
277 p_dec->fmt_out.i_codec = VLC_CODEC_BD_LPCM;
278 break;
281 else
283 switch( p_dec->fmt_out.audio.i_bitspersample )
285 case 24:
286 case 20:
287 p_dec->fmt_out.i_codec = VLC_CODEC_S32N;
288 p_dec->fmt_out.audio.i_bitspersample = 32;
289 break;
290 default:
291 p_dec->fmt_out.i_codec = VLC_CODEC_S16N;
292 p_dec->fmt_out.audio.i_bitspersample = 16;
293 break;
297 /* Set callback */
298 p_dec->pf_decode = DecodeFrame;
299 p_dec->pf_packetize = Packetize;
300 p_dec->pf_flush = Flush;
302 return VLC_SUCCESS;
304 static int OpenDecoder( vlc_object_t *p_this )
306 return OpenCommon( (decoder_t*) p_this, false );
308 static int OpenPacketizer( vlc_object_t *p_this )
310 return OpenCommon( (decoder_t*) p_this, true );
313 /*****************************************************************************
314 * Flush:
315 *****************************************************************************/
316 static void Flush( decoder_t *p_dec )
318 decoder_sys_t *p_sys = p_dec->p_sys;
320 date_Set( &p_sys->end_date, 0 );
323 /*****************************************************************************
324 * DecodeFrame: decodes an lpcm frame.
325 ****************************************************************************
326 * Beware, this function must be fed with complete frames (PES packet).
327 *****************************************************************************/
328 static block_t *Packetize( decoder_t *p_dec, block_t **pp_block )
330 decoder_sys_t *p_sys = p_dec->p_sys;
331 block_t *p_block;
332 unsigned int i_rate = 0, i_original_channels = 0, i_channels = 0, i_bits = 0;
333 int i_frame_length;
335 if( !pp_block || !*pp_block ) return NULL;
337 p_block = *pp_block;
338 *pp_block = NULL; /* So the packet doesn't get re-sent */
340 if( p_block->i_flags & (BLOCK_FLAG_CORRUPTED|BLOCK_FLAG_DISCONTINUITY) )
342 Flush( p_dec );
343 if( p_block->i_flags & BLOCK_FLAG_CORRUPTED )
345 block_Release( p_block );
346 *pp_block = NULL;
347 return NULL;
351 /* Date management */
352 if( p_block->i_pts > VLC_TS_INVALID &&
353 p_block->i_pts != date_Get( &p_sys->end_date ) )
355 date_Set( &p_sys->end_date, p_block->i_pts );
358 if( !date_Get( &p_sys->end_date ) )
360 /* We've just started the stream, wait for the first PTS. */
361 block_Release( p_block );
362 return NULL;
365 if( p_block->i_buffer <= p_sys->i_header_size )
367 msg_Err(p_dec, "frame is too short");
368 block_Release( p_block );
369 return NULL;
372 int i_ret;
373 unsigned i_channels_padding = 0;
374 unsigned i_padding = 0; /* only for AOB */
375 aob_group_t p_aob_group[2];
377 switch( p_sys->i_type )
379 case LPCM_VOB:
380 i_ret = VobHeader( &i_rate, &i_channels, &i_original_channels, &i_bits,
381 p_block->p_buffer );
382 break;
383 case LPCM_AOB:
384 i_ret = AobHeader( &i_rate, &i_channels, &i_original_channels, &i_bits, &i_padding,
385 p_aob_group,
386 p_block->p_buffer );
387 break;
388 case LPCM_BD:
389 i_ret = BdHeader( p_sys, &i_rate, &i_channels, &i_channels_padding, &i_original_channels, &i_bits,
390 p_block->p_buffer );
391 break;
392 case LPCM_WIDI:
393 i_ret = WidiHeader( &i_rate, &i_channels, &i_original_channels, &i_bits,
394 p_block->p_buffer );
395 break;
396 default:
397 abort();
400 if( i_ret || p_block->i_buffer <= p_sys->i_header_size + i_padding )
402 msg_Warn( p_dec, "no frame sync or too small frame" );
403 block_Release( p_block );
404 return NULL;
407 /* Set output properties */
408 if( p_dec->fmt_out.audio.i_rate != i_rate )
410 date_Init( &p_sys->end_date, i_rate, 1 );
411 date_Set( &p_sys->end_date, p_block->i_pts );
413 p_dec->fmt_out.audio.i_rate = i_rate;
414 p_dec->fmt_out.audio.i_channels = i_channels;
415 p_dec->fmt_out.audio.i_physical_channels = i_original_channels;
417 if ( p_sys->i_type == LPCM_AOB )
419 i_frame_length = (p_block->i_buffer - p_sys->i_header_size - i_padding) /
421 ( (p_aob_group[0].i_bits / 8) * p_aob_group[0].i_channels ) +
422 ( (p_aob_group[1].i_bits / 8) * p_aob_group[1].i_channels )
425 else
427 i_frame_length = (p_block->i_buffer - p_sys->i_header_size - i_padding) /
428 (i_channels + i_channels_padding) * 8 / i_bits;
431 if( p_sys->b_packetizer )
433 p_block->i_pts = p_block->i_dts = date_Get( &p_sys->end_date );
434 p_block->i_length =
435 date_Increment( &p_sys->end_date, i_frame_length ) -
436 p_block->i_pts;
438 /* Just pass on the incoming frame */
439 return p_block;
441 else
443 /* */
444 if( i_bits == 16 )
446 p_dec->fmt_out.audio.i_format =
447 p_dec->fmt_out.i_codec = VLC_CODEC_S16N;
448 p_dec->fmt_out.audio.i_bitspersample = 16;
450 else
452 p_dec->fmt_out.audio.i_format =
453 p_dec->fmt_out.i_codec = VLC_CODEC_S32N;
454 p_dec->fmt_out.audio.i_bitspersample = 32;
457 /* */
458 block_t *p_aout_buffer;
459 if( decoder_UpdateAudioFormat( p_dec ) != VLC_SUCCESS ||
460 !(p_aout_buffer = decoder_NewAudioBuffer( p_dec, i_frame_length )) )
462 block_Release( p_block );
463 return NULL;
466 p_aout_buffer->i_pts = date_Get( &p_sys->end_date );
467 p_aout_buffer->i_length =
468 date_Increment( &p_sys->end_date, i_frame_length )
469 - p_aout_buffer->i_pts;
471 p_block->p_buffer += p_sys->i_header_size + i_padding;
472 p_block->i_buffer -= p_sys->i_header_size + i_padding;
474 switch( p_sys->i_type )
476 case LPCM_WIDI:
477 case LPCM_VOB:
478 VobExtract( p_aout_buffer, p_block, i_bits );
479 break;
480 case LPCM_AOB:
481 AobExtract( p_aout_buffer, p_block, i_bits, p_aob_group );
482 break;
483 default:
484 vlc_assert_unreachable();
485 case LPCM_BD:
486 BdExtract( p_aout_buffer, p_block, i_frame_length, i_channels, i_channels_padding, i_bits );
487 break;
490 if( p_sys->i_chans_to_reorder )
492 aout_ChannelReorder( p_aout_buffer->p_buffer, p_aout_buffer->i_buffer,
493 p_sys->i_chans_to_reorder, p_sys->pi_chan_table,
494 p_dec->fmt_out.i_codec );
497 block_Release( p_block );
498 return p_aout_buffer;
502 static int DecodeFrame( decoder_t *p_dec, block_t *p_block )
504 block_t *p_out = Packetize( p_dec, &p_block );
505 if( p_out != NULL )
506 decoder_QueueAudio( p_dec, p_out );
507 return VLCDEC_SUCCESS;
510 /*****************************************************************************
511 * CloseCommon : lpcm decoder destruction
512 *****************************************************************************/
513 static void CloseCommon( vlc_object_t *p_this )
515 decoder_t *p_dec = (decoder_t*)p_this;
516 free( p_dec->p_sys );
519 #ifdef ENABLE_SOUT
520 /*****************************************************************************
521 * OpenEncoder: lpcm encoder construction
522 *****************************************************************************/
523 static int OpenEncoder( vlc_object_t *p_this )
525 encoder_t *p_enc = (encoder_t *)p_this;
526 encoder_sys_t *p_sys;
528 /* We only support DVD LPCM yet. */
529 if( p_enc->fmt_out.i_codec != VLC_CODEC_DVD_LPCM )
530 return VLC_EGENERIC;
532 if( p_enc->fmt_in.audio.i_rate != 48000 &&
533 p_enc->fmt_in.audio.i_rate != 96000 &&
534 p_enc->fmt_in.audio.i_rate != 44100 &&
535 p_enc->fmt_in.audio.i_rate != 32000 )
537 msg_Err( p_enc, "DVD LPCM supports only sample rates of 48, 96, 44.1 or 32 kHz" );
538 return VLC_EGENERIC;
541 if( p_enc->fmt_in.audio.i_channels > 8 )
543 msg_Err( p_enc, "DVD LPCM supports a maximum of eight channels" );
544 return VLC_EGENERIC;
547 /* Allocate the memory needed to store the encoder's structure */
548 if( ( p_enc->p_sys = p_sys =
549 (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
550 return VLC_ENOMEM;
552 /* In DVD LCPM, a frame is always 150 PTS ticks. */
553 p_sys->i_frame_samples = p_enc->fmt_in.audio.i_rate * 150 / 90000;
554 p_sys->p_buffer = xmalloc(p_sys->i_frame_samples
555 * p_enc->fmt_in.audio.i_channels * 16);
556 p_sys->i_buffer_used = 0;
557 p_sys->i_frame_num = 0;
559 p_sys->i_channels = p_enc->fmt_in.audio.i_channels;
560 p_sys->i_rate = p_enc->fmt_in.audio.i_rate;
562 p_enc->pf_encode_audio = EncodeFrames;
563 p_enc->fmt_in.i_codec = p_enc->fmt_out.i_codec;
565 p_enc->fmt_in.audio.i_bitspersample = 16;
566 p_enc->fmt_in.i_codec = VLC_CODEC_S16N;
568 p_enc->fmt_out.i_bitrate =
569 p_enc->fmt_in.audio.i_channels *
570 p_enc->fmt_in.audio.i_rate *
571 p_enc->fmt_in.audio.i_bitspersample *
572 (p_sys->i_frame_samples + LPCM_VOB_HEADER_LEN) /
573 p_sys->i_frame_samples;
575 return VLC_SUCCESS;
578 /*****************************************************************************
579 * CloseEncoder: lpcm encoder destruction
580 *****************************************************************************/
581 static void CloseEncoder ( vlc_object_t *p_this )
583 encoder_t *p_enc = (encoder_t *)p_this;
584 encoder_sys_t *p_sys = p_enc->p_sys;
586 free( p_sys->p_buffer );
587 free( p_sys );
590 /*****************************************************************************
591 * EncodeFrames: encode zero or more LCPM audio packets
592 *****************************************************************************/
593 static block_t *EncodeFrames( encoder_t *p_enc, block_t *p_aout_buf )
595 encoder_sys_t *p_sys = p_enc->p_sys;
596 block_t *p_first_block = NULL, *p_last_block = NULL;
598 if( !p_aout_buf || !p_aout_buf->i_buffer ) return NULL;
600 const int i_num_frames = ( p_sys->i_buffer_used + p_aout_buf->i_nb_samples ) /
601 p_sys->i_frame_samples;
602 const int i_leftover_samples = ( p_sys->i_buffer_used + p_aout_buf->i_nb_samples ) %
603 p_sys->i_frame_samples;
604 const int i_frame_size = p_sys->i_frame_samples * p_sys->i_channels * 2 + LPCM_VOB_HEADER_LEN;
605 const int i_start_offset = -p_sys->i_buffer_used;
607 uint8_t i_freq_code = 0;
609 switch( p_sys->i_rate ) {
610 case 48000:
611 i_freq_code = 0;
612 break;
613 case 96000:
614 i_freq_code = 1;
615 break;
616 case 44100:
617 i_freq_code = 2;
618 break;
619 case 32000:
620 i_freq_code = 3;
621 break;
622 default:
623 vlc_assert_unreachable();
626 int i_bytes_consumed = 0;
628 for ( int i = 0; i < i_num_frames; ++i )
630 block_t *p_block = block_Alloc( i_frame_size );
631 if( !p_block )
632 return NULL;
634 uint8_t *frame = (uint8_t *)p_block->p_buffer;
635 frame[0] = 1; /* one frame in packet */
636 frame[1] = 0;
637 frame[2] = 0; /* no first access unit */
638 frame[3] = (p_sys->i_frame_num + i) & 0x1f; /* no emphasis, no mute */
639 frame[4] = (i_freq_code << 4) | (p_sys->i_channels - 1);
640 frame[5] = 0x80; /* neutral dynamic range */
642 const int i_consume_samples = p_sys->i_frame_samples - p_sys->i_buffer_used;
643 const int i_kept_bytes = p_sys->i_buffer_used * p_sys->i_channels * 2;
644 const int i_consume_bytes = i_consume_samples * p_sys->i_channels * 2;
646 #ifdef WORDS_BIGENDIAN
647 memcpy( frame + 6, p_sys->p_buffer, i_kept_bytes );
648 memcpy( frame + 6 + i_kept_bytes, p_aout_buf->p_buffer + i_bytes_consumed,
649 i_consume_bytes );
650 #else
651 swab( p_sys->p_buffer, frame + 6, i_kept_bytes );
652 swab( p_aout_buf->p_buffer + i_bytes_consumed, frame + 6 + i_kept_bytes,
653 i_consume_bytes );
654 #endif
656 p_sys->i_frame_num++;
657 p_sys->i_buffer_used = 0;
658 i_bytes_consumed += i_consume_bytes;
660 /* We need to find i_length by means of next_pts due to possible roundoff errors. */
661 mtime_t this_pts = p_aout_buf->i_pts +
662 (i * p_sys->i_frame_samples + i_start_offset) * CLOCK_FREQ / p_sys->i_rate;
663 mtime_t next_pts = p_aout_buf->i_pts +
664 ((i + 1) * p_sys->i_frame_samples + i_start_offset) * CLOCK_FREQ / p_sys->i_rate;
666 p_block->i_pts = p_block->i_dts = this_pts;
667 p_block->i_length = next_pts - this_pts;
669 if( !p_first_block )
670 p_first_block = p_last_block = p_block;
671 else
672 p_last_block = p_last_block->p_next = p_block;
675 memcpy( p_sys->p_buffer,
676 p_aout_buf->p_buffer + i_bytes_consumed,
677 i_leftover_samples * p_sys->i_channels * 2 );
678 p_sys->i_buffer_used = i_leftover_samples;
680 return p_first_block;
682 #endif
684 /*****************************************************************************
686 *****************************************************************************/
687 static int VobHeader( unsigned *pi_rate,
688 unsigned *pi_channels, unsigned *pi_original_channels,
689 unsigned *pi_bits,
690 const uint8_t *p_header )
692 const uint8_t i_header = p_header[4];
694 switch( (i_header >> 4) & 0x3 )
696 case 0:
697 *pi_rate = 48000;
698 break;
699 case 1:
700 *pi_rate = 96000;
701 break;
702 case 2:
703 *pi_rate = 44100;
704 break;
705 case 3:
706 *pi_rate = 32000;
707 break;
710 *pi_channels = (i_header & 0x7) + 1;
711 switch( *pi_channels - 1 )
713 case 0:
714 *pi_original_channels = AOUT_CHAN_CENTER;
715 break;
716 case 1:
717 *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
718 break;
719 case 2:
720 /* This is unsure. */
721 *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_LFE;
722 break;
723 case 3:
724 *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
725 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
726 break;
727 case 4:
728 /* This is unsure. */
729 *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
730 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
731 | AOUT_CHAN_LFE;
732 break;
733 case 5:
734 *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
735 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
736 | AOUT_CHAN_CENTER | AOUT_CHAN_LFE;
737 break;
738 case 6:
739 *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
740 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
741 | AOUT_CHAN_CENTER | AOUT_CHAN_MIDDLELEFT
742 | AOUT_CHAN_MIDDLERIGHT;
743 break;
744 case 7:
745 *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
746 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
747 | AOUT_CHAN_CENTER | AOUT_CHAN_MIDDLELEFT
748 | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE;
749 break;
752 switch( (i_header >> 6) & 0x3 )
754 case 2:
755 *pi_bits = 24;
756 break;
757 case 1:
758 *pi_bits = 20;
759 break;
760 case 0:
761 default:
762 *pi_bits = 16;
763 break;
766 /* Check frame sync and drop it. */
767 if( p_header[5] != 0x80 )
768 return -1;
769 return 0;
772 static const unsigned p_aob_group1[21][6] = {
773 { AOUT_CHAN_CENTER, 0 },
774 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, 0 },
775 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, 0 },
776 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, 0 },
777 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, 0 },
778 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, 0 },
779 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, 0 },
780 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, 0 },
781 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, 0 },
782 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, 0 },
783 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, 0 },
784 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, 0 },
785 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, 0 },
786 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_CENTER, 0 },
787 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_CENTER, 0 },
788 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_CENTER, 0 },
789 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_CENTER, 0 },
790 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_CENTER, 0 },
791 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, 0 },
792 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, 0 },
793 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, 0 },
795 static const unsigned p_aob_group2[21][6] = {
796 { 0 },
797 { 0 },
798 { AOUT_CHAN_REARCENTER, 0 },
799 { AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, 0 },
800 { AOUT_CHAN_LFE, 0 },
801 { AOUT_CHAN_LFE, AOUT_CHAN_REARCENTER, 0 },
802 { AOUT_CHAN_LFE, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, 0 },
803 { AOUT_CHAN_CENTER, 0 },
804 { AOUT_CHAN_CENTER, AOUT_CHAN_REARCENTER, 0 },
805 { AOUT_CHAN_CENTER, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, 0 },
806 { AOUT_CHAN_CENTER, AOUT_CHAN_LFE, 0 },
807 { AOUT_CHAN_CENTER, AOUT_CHAN_LFE, AOUT_CHAN_REARCENTER, 0 },
808 { AOUT_CHAN_CENTER, AOUT_CHAN_LFE, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, 0 },
809 { AOUT_CHAN_REARCENTER, 0 },
810 { AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, 0 },
811 { AOUT_CHAN_LFE, 0 },
812 { AOUT_CHAN_LFE, AOUT_CHAN_REARCENTER, 0 },
813 { AOUT_CHAN_LFE, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, 0 },
814 { AOUT_CHAN_LFE, 0 },
815 { AOUT_CHAN_CENTER, 0 },
816 { AOUT_CHAN_CENTER, AOUT_CHAN_LFE, 0 },
819 static int AobHeader( unsigned *pi_rate,
820 unsigned *pi_channels, unsigned *pi_layout,
821 unsigned *pi_bits,
822 unsigned *pi_padding,
823 aob_group_t g[2],
824 const uint8_t *p_header )
826 const unsigned i_header_size = GetWBE( &p_header[1] );
827 if( i_header_size + 3 < LPCM_AOB_HEADER_LEN )
828 return VLC_EGENERIC;
830 /* Padding = Total header size - Normal AOB header
831 * + 3 bytes (1 for continuity counter + 2 for header_size ) */
832 *pi_padding = 3 + i_header_size - LPCM_AOB_HEADER_LEN;
834 const int i_index_size_g1 = (p_header[6] >> 4);
835 const int i_index_size_g2 = (p_header[6] ) & 0x0f;
836 const int i_index_rate_g1 = (p_header[7] >> 4);
837 const int i_index_rate_g2 = (p_header[7] ) & 0x0f;
838 const int i_assignment = p_header[9];
840 /* Validate */
841 if( i_index_size_g1 > 0x02 ||
842 ( i_index_size_g2 != 0x0f && i_index_size_g2 > 0x02 ) )
843 return VLC_EGENERIC;
844 if( (i_index_rate_g1 & 0x07) > 0x02 ||
845 ( i_index_rate_g2 != 0x0f && (i_index_rate_g1 & 0x07) > 0x02 ) )
846 return VLC_EGENERIC;
847 if( i_assignment > 20 )
848 return VLC_EGENERIC;
850 /* */
851 /* max is 0x2, 0xf == unused */
852 g[0].i_bits = 16 + 4 * i_index_size_g1;
853 g[1].i_bits = ( i_index_size_g2 != 0x0f ) ? 16 + 4 * i_index_size_g2 : 0;
855 /* No info about interlacing of different sampling rate */
856 if ( g[1].i_bits && ( i_index_rate_g1 != i_index_rate_g2 ) )
857 return VLC_EGENERIC;
859 /* only set 16bits if both are <= */
860 if( g[0].i_bits )
862 if( g[0].i_bits > 16 || g[1].i_bits > 16 )
863 *pi_bits = 32;
864 else
865 *pi_bits = 16;
867 else
868 return VLC_EGENERIC;
870 if( i_index_rate_g1 & 0x08 )
871 *pi_rate = 44100 << (i_index_rate_g1 & 0x07);
872 else
873 *pi_rate = 48000 << (i_index_rate_g1 & 0x07);
876 /* Group1 */
877 unsigned i_channels1 = 0;
878 unsigned i_layout1 = 0;
879 for( int i = 0; p_aob_group1[i_assignment][i] != 0; i++ )
881 i_channels1++;
882 i_layout1 |= p_aob_group1[i_assignment][i];
884 /* Group2 */
885 unsigned i_channels2 = 0;
886 unsigned i_layout2 = 0;
887 if( i_index_size_g2 != 0x0f && i_index_rate_g2 != 0x0f )
889 for( int i = 0; p_aob_group2[i_assignment][i] != 0; i++ )
891 i_channels2++;
892 i_layout2 |= p_aob_group2[i_assignment][i];
894 assert( (i_layout1 & i_layout2) == 0 );
897 /* */
898 *pi_channels = i_channels1 + ( g[1].i_bits ? i_channels2 : 0 );
899 *pi_layout = i_layout1 | ( g[1].i_bits ? i_layout2 : 0 );
901 /* */
902 for( unsigned i = 0; i < 2; i++ )
904 const unsigned *p_aob = i == 0 ? p_aob_group1[i_assignment] :
905 p_aob_group2[i_assignment];
906 g[i].i_channels = i == 0 ? i_channels1 :
907 i_channels2;
909 if( !g[i].i_bits )
910 continue;
911 for( unsigned j = 0; j < g[i].i_channels; j++ )
913 g[i].pi_position[j] = 0;
914 for( int k = 0; pi_vlc_chan_order_wg4[k] != 0; k++ )
916 const unsigned i_channel = pi_vlc_chan_order_wg4[k];
917 if( i_channel == p_aob[j] )
918 break;
919 if( (*pi_layout) & i_channel )
920 g[i].pi_position[j]++;
924 return VLC_SUCCESS;
927 static const uint32_t pi_8channels_in[] =
928 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_CENTER,
929 AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,
930 AOUT_CHAN_MIDDLERIGHT, AOUT_CHAN_LFE, 0 };
932 static const uint32_t pi_7channels_in[] =
933 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_CENTER,
934 AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,
935 AOUT_CHAN_MIDDLERIGHT, 0 };
937 static const uint32_t pi_6channels_in[] =
938 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_CENTER,
939 AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, AOUT_CHAN_LFE, 0 };
941 static const uint32_t pi_5channels_in[] =
942 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_CENTER,
943 AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT, 0 };
945 static const uint32_t pi_4channels_in[] =
946 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT,
947 AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, 0 };
949 static const uint32_t pi_3channels_in[] =
950 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT,
951 AOUT_CHAN_CENTER, 0 };
954 static int BdHeader( decoder_sys_t *p_sys,
955 unsigned *pi_rate,
956 unsigned *pi_channels,
957 unsigned *pi_channels_padding,
958 unsigned *pi_original_channels,
959 unsigned *pi_bits,
960 const uint8_t *p_header )
962 const uint32_t h = GetDWBE( p_header );
963 const uint32_t *pi_channels_in = NULL;
964 switch( ( h & 0xf000) >> 12 )
966 case 1:
967 *pi_channels = 1;
968 *pi_original_channels = AOUT_CHAN_CENTER;
969 break;
970 case 3:
971 *pi_channels = 2;
972 *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
973 break;
974 case 4:
975 *pi_channels = 3;
976 *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER;
977 pi_channels_in = pi_3channels_in;
978 break;
979 case 5:
980 *pi_channels = 3;
981 *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER;
982 break;
983 case 6:
984 *pi_channels = 4;
985 *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
986 AOUT_CHAN_REARCENTER;
987 break;
988 case 7:
989 *pi_channels = 4;
990 *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
991 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
992 pi_channels_in = pi_4channels_in;
993 break;
994 case 8:
995 *pi_channels = 5;
996 *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
997 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
998 pi_channels_in = pi_5channels_in;
999 break;
1000 case 9:
1001 *pi_channels = 6;
1002 *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
1003 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT |
1004 AOUT_CHAN_LFE;
1005 pi_channels_in = pi_6channels_in;
1006 break;
1007 case 10:
1008 *pi_channels = 7;
1009 *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
1010 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT |
1011 AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT;
1012 pi_channels_in = pi_7channels_in;
1013 break;
1014 case 11:
1015 *pi_channels = 8;
1016 *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
1017 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT |
1018 AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT |
1019 AOUT_CHAN_LFE;
1020 pi_channels_in = pi_8channels_in;
1021 break;
1023 default:
1024 return -1;
1026 *pi_channels_padding = *pi_channels & 1;
1028 switch( (h >> 6) & 0x03 )
1030 case 1:
1031 *pi_bits = 16;
1032 break;
1033 case 2: /* 20 bits but samples are stored on 24 bits */
1034 case 3: /* 24 bits */
1035 *pi_bits = 24;
1036 break;
1037 default:
1038 return -1;
1040 switch( (h >> 8) & 0x0f )
1042 case 1:
1043 *pi_rate = 48000;
1044 break;
1045 case 4:
1046 *pi_rate = 96000;
1047 break;
1048 case 5:
1049 *pi_rate = 192000;
1050 break;
1051 default:
1052 return -1;
1055 if( pi_channels_in )
1057 p_sys->i_chans_to_reorder =
1058 aout_CheckChannelReorder( pi_channels_in, NULL,
1059 *pi_original_channels,
1060 p_sys->pi_chan_table );
1063 return 0;
1066 static int WidiHeader( unsigned *pi_rate,
1067 unsigned *pi_channels, unsigned *pi_original_channels,
1068 unsigned *pi_bits,
1069 const uint8_t *p_header )
1071 if ( p_header[0] != 0xa0 || p_header[1] != 0x06 )
1072 return -1;
1074 switch( ( p_header[3] & 0x38 ) >> 3 )
1076 case 0x01: //0b001
1077 *pi_rate = 44100;
1078 break;
1079 case 0x02: //0b010
1080 *pi_rate = 48000;
1081 break;
1082 default:
1083 return -1;
1086 if( p_header[3] >> 6 != 0 )
1087 return -1;
1088 else
1089 *pi_bits = 16;
1091 *pi_channels = (p_header[3] & 0x7) + 1;
1093 *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
1095 return 0;
1098 static void VobExtract( block_t *p_aout_buffer, block_t *p_block,
1099 unsigned i_bits )
1101 /* 20/24 bits LPCM use special packing */
1102 if( i_bits == 24 )
1104 uint32_t *p_out = (uint32_t *)p_aout_buffer->p_buffer;
1106 while( p_block->i_buffer / 12 )
1108 /* Sample 1 */
1109 *(p_out++) = (p_block->p_buffer[ 0] << 24)
1110 | (p_block->p_buffer[ 1] << 16)
1111 | (p_block->p_buffer[ 8] << 8);
1112 /* Sample 2 */
1113 *(p_out++) = (p_block->p_buffer[ 2] << 24)
1114 | (p_block->p_buffer[ 3] << 16)
1115 | (p_block->p_buffer[ 9] << 8);
1116 /* Sample 3 */
1117 *(p_out++) = (p_block->p_buffer[ 4] << 24)
1118 | (p_block->p_buffer[ 5] << 16)
1119 | (p_block->p_buffer[10] << 8);
1120 /* Sample 4 */
1121 *(p_out++) = (p_block->p_buffer[ 6] << 24)
1122 | (p_block->p_buffer[ 7] << 16)
1123 | (p_block->p_buffer[11] << 8);
1125 p_block->i_buffer -= 12;
1126 p_block->p_buffer += 12;
1129 else if( i_bits == 20 )
1131 uint32_t *p_out = (uint32_t *)p_aout_buffer->p_buffer;
1133 while( p_block->i_buffer / 10 )
1135 /* Sample 1 */
1136 *(p_out++) = ( p_block->p_buffer[0] << 24)
1137 | ( p_block->p_buffer[1] << 16)
1138 | ((p_block->p_buffer[8] & 0xF0) << 8);
1139 /* Sample 2 */
1140 *(p_out++) = ( p_block->p_buffer[2] << 24)
1141 | ( p_block->p_buffer[3] << 16)
1142 | ((p_block->p_buffer[8] & 0x0F) << 12);
1143 /* Sample 3 */
1144 *(p_out++) = ( p_block->p_buffer[4] << 24)
1145 | ( p_block->p_buffer[5] << 16)
1146 | ((p_block->p_buffer[9] & 0xF0) << 8);
1147 /* Sample 4 */
1148 *(p_out++) = ( p_block->p_buffer[6] << 24)
1149 | ( p_block->p_buffer[7] << 16)
1150 | ((p_block->p_buffer[9] & 0x0F) << 12);
1152 p_block->i_buffer -= 10;
1153 p_block->p_buffer += 10;
1156 else
1158 assert( i_bits == 16 );
1159 #ifdef WORDS_BIGENDIAN
1160 memcpy( p_aout_buffer->p_buffer, p_block->p_buffer, p_block->i_buffer );
1161 #else
1162 swab( p_block->p_buffer, p_aout_buffer->p_buffer, p_block->i_buffer );
1163 #endif
1167 static void AobExtract( block_t *p_aout_buffer,
1168 block_t *p_block, unsigned i_aoutbits, aob_group_t p_group[2] )
1170 uint8_t *p_out = p_aout_buffer->p_buffer;
1171 const unsigned i_total_channels = p_group[0].i_channels +
1172 ( p_group[1].i_bits ? p_group[1].i_channels : 0 );
1174 while( p_block->i_buffer > 0 )
1176 unsigned int i_aout_written = 0;
1178 for( int i = 0; i < 2; i++ )
1180 const aob_group_t *g = &p_group[1-i];
1181 const unsigned int i_group_size = 2 * g->i_channels * g->i_bits / 8;
1183 if( p_block->i_buffer < i_group_size )
1185 p_block->i_buffer = 0;
1186 break;
1189 if( !g->i_bits )
1190 continue;
1192 for( unsigned n = 0; n < 2; n++ )
1194 for( unsigned j = 0; j < g->i_channels; j++ )
1196 const int i_src = n * g->i_channels + j;
1197 const int i_dst = n * i_total_channels + g->pi_position[j];
1198 uint32_t *p_out32 = (uint32_t *) &p_out[4*i_dst];
1200 if( g->i_bits == 24 )
1202 assert( i_aoutbits == 32 );
1203 *p_out32 = (p_block->p_buffer[2*i_src+0] << 24)
1204 | (p_block->p_buffer[2*i_src+1] << 16)
1205 | (p_block->p_buffer[4*g->i_channels+i_src] << 8);
1206 #ifdef WORDS_BIGENDIAN
1207 *p_out32 = bswap32(*p_out32);
1208 #endif
1209 i_aout_written += 4;
1211 else if( g->i_bits == 20 )
1213 assert( i_aoutbits == 32 );
1214 *p_out32 = (p_block->p_buffer[2*i_src+0] << 24)
1215 | (p_block->p_buffer[2*i_src+1] << 16)
1216 | (((p_block->p_buffer[4*g->i_channels+i_src] << ((!n)?0:4) ) & 0xf0) << 8);
1217 #ifdef WORDS_BIGENDIAN
1218 *p_out32 = bswap32(*p_out32);
1219 #endif
1220 i_aout_written += 4;
1222 else
1224 assert( g->i_bits == 16 );
1225 assert( i_aoutbits == 16 || i_aoutbits == 32 );
1226 if( i_aoutbits == 16 )
1228 #ifdef WORDS_BIGENDIAN
1229 memcpy( &p_out[2*i_dst], &p_block->p_buffer[2*i_src], 2 );
1230 #else
1231 p_out[2*i_dst+1] = p_block->p_buffer[2*i_src+0];
1232 p_out[2*i_dst+0] = p_block->p_buffer[2*i_src+1];
1233 #endif
1234 i_aout_written += 2;
1236 else
1238 *p_out32 = (p_block->p_buffer[2*i_src+0] << 24)
1239 | (p_block->p_buffer[2*i_src+1] << 16);
1240 #ifdef WORDS_BIGENDIAN
1241 *p_out32 = bswap32(*p_out32);
1242 #endif
1243 i_aout_written += 4;
1249 /* */
1250 p_block->i_buffer -= i_group_size;
1251 p_block->p_buffer += i_group_size;
1253 p_out += i_aout_written;
1256 static void BdExtract( block_t *p_aout_buffer, block_t *p_block,
1257 unsigned i_frame_length,
1258 unsigned i_channels, unsigned i_channels_padding,
1259 unsigned i_bits )
1261 if( i_bits != 16 || i_channels_padding > 0 )
1263 uint8_t *p_src = p_block->p_buffer;
1264 uint8_t *p_dst = p_aout_buffer->p_buffer;
1265 int dst_inc = ((i_bits == 16) ? 2 : 4) * i_channels;
1267 while( i_frame_length > 0 )
1269 #ifdef WORDS_BIGENDIAN
1270 memcpy( p_dst, p_src, i_channels * i_bits / 8 );
1271 #else
1272 if (i_bits == 16) {
1273 swab( p_src, p_dst, (i_channels + i_channels_padding) * i_bits / 8 );
1274 } else {
1275 for (unsigned i = 0; i < i_channels; ++i) {
1276 p_dst[i * 4] = 0;
1277 p_dst[1 + (i * 4)] = p_src[2 + (i * 3)];
1278 p_dst[2 + (i * 4)] = p_src[1 + (i * 3)];
1279 p_dst[3 + (i * 4)] = p_src[i * 3];
1282 #endif
1283 p_src += (i_channels + i_channels_padding) * i_bits / 8;
1284 p_dst += dst_inc;
1285 i_frame_length--;
1288 else
1290 #ifdef WORDS_BIGENDIAN
1291 memcpy( p_aout_buffer->p_buffer, p_block->p_buffer, p_block->i_buffer );
1292 #else
1293 swab( p_block->p_buffer, p_aout_buffer->p_buffer, p_block->i_buffer );
1294 #endif