1 /*****************************************************************************
2 * lpcm.c: lpcm decoder/packetizer module
3 *****************************************************************************
4 * Copyright (C) 1999-2008 the VideoLAN team
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>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_codec.h>
39 /*****************************************************************************
41 *****************************************************************************/
42 static int OpenDecoder ( vlc_object_t * );
43 static int OpenPacketizer( vlc_object_t * );
44 static void CloseCommon ( vlc_object_t * );
48 set_category( CAT_INPUT )
49 set_subcategory( SUBCAT_INPUT_ACODEC )
50 set_description( N_("Linear PCM audio decoder") )
51 set_capability( "decoder", 100 )
52 set_callbacks( OpenDecoder, CloseCommon )
55 set_description( N_("Linear PCM audio packetizer") )
56 set_capability( "packetizer", 100 )
57 set_callbacks( OpenPacketizer, CloseCommon )
62 /*****************************************************************************
63 * decoder_sys_t : lpcm decoder descriptor
64 *****************************************************************************/
73 audio_date_t end_date;
76 unsigned int i_header_size;
81 * - frame number (8 bits)
82 * - unknown (16 bits) == 0x0003 ?
84 * - current frame (4 bits)
86 * - frequency (2 bits) 0 == 48 kHz, 1 == 32 kHz, 2 == ?, 3 == ?
88 * - number of channels - 1 (3 bits) 1 == 2 channels
89 * - start code (8 bits) == 0x80
92 #define LPCM_DVD_HEADER_LEN (6)
94 /*****************************************************************************
96 *****************************************************************************/
97 static void *DecodeFrame ( decoder_t *, block_t ** );
99 /*****************************************************************************
101 *****************************************************************************/
102 static int OpenCommon( vlc_object_t *p_this, bool b_packetizer )
104 decoder_t *p_dec = (decoder_t*)p_this;
105 decoder_sys_t *p_sys;
107 switch( p_dec->fmt_in.i_codec )
109 case VLC_FOURCC('l','p','c','m'):
110 case VLC_FOURCC('l','p','c','b'):
116 /* Allocate the memory needed to store the decoder's structure */
117 if( ( p_dec->p_sys = p_sys = malloc(sizeof(decoder_sys_t)) ) == NULL )
121 p_sys->b_packetizer = b_packetizer;
122 p_sys->i_header_size = LPCM_DVD_HEADER_LEN;
123 aout_DateSet( &p_sys->end_date, 0 );
125 /* Set output properties */
126 p_dec->fmt_out.i_cat = AUDIO_ES;
130 p_dec->fmt_out.i_codec = VLC_FOURCC('l','p','c','m');
134 switch( p_dec->fmt_out.audio.i_bitspersample )
138 p_dec->fmt_out.i_codec = VLC_FOURCC('s','2','4','b');
141 p_dec->fmt_out.i_codec = VLC_FOURCC('s','1','6','b');
142 p_dec->fmt_out.audio.i_bitspersample = 16;
148 p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
150 p_dec->pf_packetize = (block_t *(*)(decoder_t *, block_t **))
155 static int OpenDecoder( vlc_object_t *p_this )
157 return OpenCommon( p_this, false );
159 static int OpenPacketizer( vlc_object_t *p_this )
161 return OpenCommon( p_this, true );
164 /*****************************************************************************
165 * DecodeFrame: decodes an lpcm frame.
166 ****************************************************************************
167 * Beware, this function must be fed with complete frames (PES packet).
168 *****************************************************************************/
169 static void *DecodeFrame( decoder_t *p_dec, block_t **pp_block )
171 decoder_sys_t *p_sys = p_dec->p_sys;
173 unsigned int i_rate = 0, i_original_channels = 0, i_channels = 0;
174 int i_frame_length, i_bitspersample;
177 if( !pp_block || !*pp_block ) return NULL;
180 *pp_block = NULL; /* So the packet doesn't get re-sent */
182 /* Date management */
183 if( p_block->i_pts > 0 &&
184 p_block->i_pts != aout_DateGet( &p_sys->end_date ) )
186 aout_DateSet( &p_sys->end_date, p_block->i_pts );
189 if( !aout_DateGet( &p_sys->end_date ) )
191 /* We've just started the stream, wait for the first PTS. */
192 block_Release( p_block );
196 if( p_block->i_buffer <= p_sys->i_header_size )
198 msg_Err(p_dec, "frame is too short");
199 block_Release( p_block );
203 i_header = p_block->p_buffer[4];
204 switch ( (i_header >> 4) & 0x3 )
220 i_channels = (i_header & 0x7);
221 switch ( i_channels )
224 i_original_channels = AOUT_CHAN_CENTER;
227 i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
230 /* This is unsure. */
231 i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_LFE;
234 i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
235 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
238 /* This is unsure. */
239 i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
240 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
244 i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
245 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
246 | AOUT_CHAN_CENTER | AOUT_CHAN_LFE;
249 i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
250 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
251 | AOUT_CHAN_CENTER | AOUT_CHAN_MIDDLELEFT
252 | AOUT_CHAN_MIDDLERIGHT;
255 i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
256 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
257 | AOUT_CHAN_CENTER | AOUT_CHAN_MIDDLELEFT
258 | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE;
262 switch ( (i_header >> 6) & 0x3 )
265 p_dec->fmt_out.i_codec = VLC_FOURCC('s','2','4','b');
266 p_dec->fmt_out.audio.i_bitspersample = 24;
267 i_bitspersample = 24;
270 p_dec->fmt_out.i_codec = VLC_FOURCC('s','2','4','b');
271 p_dec->fmt_out.audio.i_bitspersample = 24;
272 i_bitspersample = 20;
276 p_dec->fmt_out.i_codec = VLC_FOURCC('s','1','6','b');
277 p_dec->fmt_out.audio.i_bitspersample = 16;
278 i_bitspersample = 16;
282 /* Check frame sync and drop it. */
283 if( p_block->p_buffer[5] != 0x80 )
285 msg_Warn( p_dec, "no frame sync" );
286 block_Release( p_block );
290 /* Set output properties */
291 if( p_dec->fmt_out.audio.i_rate != i_rate )
293 aout_DateInit( &p_sys->end_date, i_rate );
294 aout_DateSet( &p_sys->end_date, p_block->i_pts );
296 p_dec->fmt_out.audio.i_rate = i_rate;
297 p_dec->fmt_out.audio.i_channels = i_channels + 1;
298 p_dec->fmt_out.audio.i_original_channels = i_original_channels;
299 p_dec->fmt_out.audio.i_physical_channels
300 = i_original_channels & AOUT_CHAN_PHYSMASK;
302 i_frame_length = (p_block->i_buffer - p_sys->i_header_size) /
303 p_dec->fmt_out.audio.i_channels * 8 / i_bitspersample;
305 if( p_sys->b_packetizer )
307 p_dec->fmt_out.i_codec = VLC_FOURCC('l','p','c','m');
308 p_block->i_pts = p_block->i_dts = aout_DateGet( &p_sys->end_date );
310 aout_DateIncrement( &p_sys->end_date, i_frame_length ) -
313 /* Just pass on the incoming frame */
318 aout_buffer_t *p_aout_buffer;
319 p_aout_buffer = decoder_NewAudioBuffer( p_dec, i_frame_length );
320 if( p_aout_buffer == NULL ) return NULL;
322 p_aout_buffer->start_date = aout_DateGet( &p_sys->end_date );
323 p_aout_buffer->end_date =
324 aout_DateIncrement( &p_sys->end_date, i_frame_length );
326 p_block->p_buffer += p_sys->i_header_size;
327 p_block->i_buffer -= p_sys->i_header_size;
329 /* 20/24 bits LPCM use special packing */
330 if( i_bitspersample == 24 )
332 uint8_t *p_out = p_aout_buffer->p_buffer;
334 while( p_block->i_buffer / 12 )
337 p_out[0] = p_block->p_buffer[0];
338 p_out[1] = p_block->p_buffer[1];
339 p_out[2] = p_block->p_buffer[8];
341 p_out[3] = p_block->p_buffer[2];
342 p_out[4] = p_block->p_buffer[3];
343 p_out[5] = p_block->p_buffer[9];
345 p_out[6] = p_block->p_buffer[4];
346 p_out[7] = p_block->p_buffer[5];
347 p_out[8] = p_block->p_buffer[10];
349 p_out[9] = p_block->p_buffer[6];
350 p_out[10] = p_block->p_buffer[7];
351 p_out[11] = p_block->p_buffer[11];
353 p_block->i_buffer -= 12;
354 p_block->p_buffer += 12;
358 else if( i_bitspersample == 20 )
360 uint8_t *p_out = p_aout_buffer->p_buffer;
362 while( p_block->i_buffer / 10 )
365 p_out[0] = p_block->p_buffer[0];
366 p_out[1] = p_block->p_buffer[1];
367 p_out[2] = p_block->p_buffer[8] & 0xF0;
369 p_out[3] = p_block->p_buffer[2];
370 p_out[4] = p_block->p_buffer[3];
371 p_out[5] = p_block->p_buffer[8] << 4;
373 p_out[6] = p_block->p_buffer[4];
374 p_out[7] = p_block->p_buffer[5];
375 p_out[8] = p_block->p_buffer[9] & 0xF0;
377 p_out[9] = p_block->p_buffer[6];
378 p_out[10] = p_block->p_buffer[7];
379 p_out[11] = p_block->p_buffer[9] << 4;
381 p_block->i_buffer -= 10;
382 p_block->p_buffer += 10;
388 memcpy( p_aout_buffer->p_buffer,
389 p_block->p_buffer, p_block->i_buffer );
392 block_Release( p_block );
393 return p_aout_buffer;
397 /*****************************************************************************
398 * CloseCommon : lpcm decoder destruction
399 *****************************************************************************/
400 static void CloseCommon( vlc_object_t *p_this )
402 decoder_t *p_dec = (decoder_t*)p_this;
403 free( p_dec->p_sys );