1 /*****************************************************************************
2 * aes3.c: aes3 decoder/packetizer module
3 *****************************************************************************
4 * Copyright (C) 2008 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_codec.h>
37 /*****************************************************************************
39 *****************************************************************************/
40 static int OpenDecoder ( vlc_object_t * );
41 static int OpenPacketizer( vlc_object_t * );
42 static void Close ( vlc_object_t * );
46 set_category( CAT_INPUT )
47 set_subcategory( SUBCAT_INPUT_ACODEC )
48 set_description( _("AES3/SMPTE 302M audio decoder") )
49 set_capability( "decoder", 100 )
50 set_callbacks( OpenDecoder, Close )
53 set_description( N_("AES3/SMPTE 302M audio packetizer") )
54 set_capability( "packetizer", 100 )
55 set_callbacks( OpenPacketizer, Close )
59 /*****************************************************************************
60 * decoder_sys_t : aes3 decoder descriptor
61 *****************************************************************************/
67 audio_date_t end_date;
70 #define AES3_HEADER_LEN 4
72 /*****************************************************************************
74 *****************************************************************************/
75 static int Open( decoder_t *p_dec, bool b_packetizer );
77 static block_t *Parse( decoder_t *p_dec, int *pi_frame_length, int *pi_bits,
78 block_t **pp_block, bool b_packetizer );
80 static inline uint8_t Reverse8( int n )
82 n = ((n >> 1) & 0x55) | ((n << 1) & 0xaa);
83 n = ((n >> 2) & 0x33) | ((n << 2) & 0xcc);
84 n = ((n >> 4) & 0x0f) | ((n << 4) & 0xf0);
88 /*****************************************************************************
90 *****************************************************************************/
91 static int OpenDecoder( vlc_object_t *p_this )
93 decoder_t *p_dec = (decoder_t*)p_this;
95 return Open( p_dec, false );
98 /*****************************************************************************
100 *****************************************************************************/
101 static int OpenPacketizer( vlc_object_t *p_this )
103 decoder_t *p_dec = (decoder_t*)p_this;
105 return Open( p_dec, true );
108 /*****************************************************************************
109 * Close : aes3 decoder destruction
110 *****************************************************************************/
111 static void Close( vlc_object_t *p_this )
113 decoder_t *p_dec = (decoder_t*)p_this;
114 free( p_dec->p_sys );
117 /*****************************************************************************
118 * Decode: decodes an aes3 frame.
119 ****************************************************************************
120 * Beware, this function must be fed with complete frames (PES packet).
121 *****************************************************************************/
122 static aout_buffer_t *Decode( decoder_t *p_dec, block_t **pp_block )
124 decoder_sys_t *p_sys = p_dec->p_sys;
126 aout_buffer_t *p_aout_buffer;
127 int i_frame_length, i_bits;
129 p_block = Parse( p_dec, &i_frame_length, &i_bits, pp_block, false );
133 p_aout_buffer = decoder_NewAudioBuffer( p_dec, i_frame_length );
134 if( p_aout_buffer == NULL )
137 p_aout_buffer->start_date = aout_DateGet( &p_sys->end_date );
138 p_aout_buffer->end_date = aout_DateIncrement( &p_sys->end_date, i_frame_length );
140 p_block->i_buffer -= AES3_HEADER_LEN;
141 p_block->p_buffer += AES3_HEADER_LEN;
145 uint8_t *p_out = p_aout_buffer->p_buffer;
147 while( p_block->i_buffer / 7 )
149 p_out[0] = Reverse8( p_block->p_buffer[0] );
150 p_out[1] = Reverse8( p_block->p_buffer[1] );
151 p_out[2] = Reverse8( p_block->p_buffer[2] );
153 p_out[3] = (Reverse8( p_block->p_buffer[3] ) >> 4) | ( (Reverse8( p_block->p_buffer[4] ) << 4) & 0xf0 );
154 p_out[4] = (Reverse8( p_block->p_buffer[4] ) >> 4) | ( (Reverse8( p_block->p_buffer[5] ) << 4) & 0xf0 );
155 p_out[5] = (Reverse8( p_block->p_buffer[5] ) >> 4) | ( (Reverse8( p_block->p_buffer[6] ) << 4) & 0xf0 );
157 p_block->i_buffer -= 7;
158 p_block->p_buffer += 7;
163 else if( i_bits == 20 )
165 uint8_t *p_out = p_aout_buffer->p_buffer;
167 while( p_block->i_buffer / 6 )
169 p_out[0] = ( (Reverse8( p_block->p_buffer[0] ) << 4) & 0xf0 );
170 p_out[1] = ( Reverse8( p_block->p_buffer[0]) >> 4 ) | ( (Reverse8( p_block->p_buffer[1]) << 4 ) & 0xf0 );
171 p_out[2] = ( Reverse8( p_block->p_buffer[1]) >> 4 ) | ( (Reverse8( p_block->p_buffer[2]) << 4 ) & 0xf0 );
173 p_out[3] = ( (Reverse8( p_block->p_buffer[3] ) << 4) & 0xf0 );
174 p_out[4] = ( Reverse8( p_block->p_buffer[3]) >> 4 ) | ( (Reverse8( p_block->p_buffer[4]) << 4 ) & 0xf0 );
175 p_out[5] = ( Reverse8( p_block->p_buffer[4]) >> 4 ) | ( (Reverse8( p_block->p_buffer[5]) << 4 ) & 0xf0 );
177 p_block->i_buffer -= 6;
178 p_block->p_buffer += 6;
184 uint8_t *p_out = p_aout_buffer->p_buffer;
186 assert( i_bits == 16 );
188 while( p_block->i_buffer / 5 )
190 p_out[0] = Reverse8( p_block->p_buffer[0] );
191 p_out[1] = Reverse8( p_block->p_buffer[1] );
193 p_out[2] = (Reverse8( p_block->p_buffer[2] ) >> 4) | ( (Reverse8( p_block->p_buffer[3] ) << 4) & 0xf0 );
194 p_out[3] = (Reverse8( p_block->p_buffer[3] ) >> 4) | ( (Reverse8( p_block->p_buffer[4] ) << 4) & 0xf0 );
196 p_block->i_buffer -= 5;
197 p_block->p_buffer += 5;
203 block_Release( p_block );
204 return p_aout_buffer;
207 /*****************************************************************************
208 * Packetize: packetizes an aes3 frame.
209 ****************************************************************************
210 * Beware, this function must be fed with complete frames (PES packet).
211 *****************************************************************************/
212 static block_t *Packetize( decoder_t *p_dec, block_t **pp_block )
214 decoder_sys_t *p_sys = p_dec->p_sys;
216 int i_frame_length, i_bits;
218 p_block = Parse( p_dec, &i_frame_length, &i_bits, pp_block, true );
222 p_block->i_pts = p_block->i_dts = aout_DateGet( &p_sys->end_date );
223 p_block->i_length = aout_DateIncrement( &p_sys->end_date, i_frame_length ) - p_block->i_pts;
225 /* Just pass on the incoming frame */
229 /*****************************************************************************
231 ****************************************************************************/
232 static int Open( decoder_t *p_dec, bool b_packetizer )
234 decoder_sys_t *p_sys;
236 if( p_dec->fmt_in.i_codec != VLC_FOURCC('a','e','s','3') )
239 /* Allocate the memory needed to store the decoder's structure */
240 p_dec->p_sys = p_sys = malloc( sizeof(decoder_sys_t) );
246 aout_DateInit( &p_sys->end_date, 48000 );
247 aout_DateSet( &p_sys->end_date, 0 );
249 /* Set output properties */
250 p_dec->fmt_out.i_cat = AUDIO_ES;
251 p_dec->fmt_out.audio.i_rate = 48000;
256 p_dec->fmt_out.i_codec = VLC_FOURCC('a','e','s','3');
258 p_dec->pf_decode_audio = NULL;
259 p_dec->pf_packetize = Packetize;
263 p_dec->pf_decode_audio = Decode;
264 p_dec->pf_packetize = NULL;
269 static const unsigned int pi_original_channels[4] = {
270 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
271 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
272 AOUT_CHAN_CENTER | AOUT_CHAN_LFE,
273 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
274 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT |
275 AOUT_CHAN_CENTER | AOUT_CHAN_LFE,
276 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
277 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT |
278 AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT |
279 AOUT_CHAN_CENTER | AOUT_CHAN_LFE,
282 static block_t *Parse( decoder_t *p_dec, int *pi_frame_length, int *pi_bits,
283 block_t **pp_block, bool b_packetizer )
285 decoder_sys_t *p_sys = p_dec->p_sys;
293 if( !pp_block || !*pp_block ) return NULL;
296 *pp_block = NULL; /* So the packet doesn't get re-sent */
298 /* Date management */
299 if( p_block->i_pts > 0 &&
300 p_block->i_pts != aout_DateGet( &p_sys->end_date ) )
302 aout_DateSet( &p_sys->end_date, p_block->i_pts );
305 if( !aout_DateGet( &p_sys->end_date ) )
307 /* We've just started the stream, wait for the first PTS. */
308 block_Release( p_block );
312 if( p_block->i_buffer <= AES3_HEADER_LEN )
314 msg_Err(p_dec, "frame is too short");
315 block_Release( p_block );
328 h = GetDWBE( p_block->p_buffer );
329 i_size = (h >> 16) & 0xffff;
330 i_channels = 2 + 2*( (h >> 14) & 0x03 );
331 i_id = (h >> 6) & 0xff;
332 i_bits = 16 + 4*( (h >> 4)&0x03 );
334 if( AES3_HEADER_LEN + i_size != p_block->i_buffer || i_bits > 24 )
336 msg_Err(p_dec, "frame has invalid header");
337 block_Release( p_block );
341 /* Set output properties */
344 p_dec->fmt_out.audio.i_bitspersample = i_bits;
348 p_dec->fmt_out.i_codec = i_bits == 16 ? VLC_FOURCC('s','1','6','l') : VLC_FOURCC('s','2','4','l');
349 p_dec->fmt_out.audio.i_bitspersample = i_bits == 16 ? 16 : 24;
352 p_dec->fmt_out.audio.i_channels = i_channels;
353 p_dec->fmt_out.audio.i_original_channels = pi_original_channels[i_channels/2-1];
354 p_dec->fmt_out.audio.i_physical_channels = pi_original_channels[i_channels/2-1] & AOUT_CHAN_PHYSMASK;
356 *pi_frame_length = (p_block->i_buffer - AES3_HEADER_LEN) / ( (4+i_bits) * i_channels / 8 );