1 /*****************************************************************************
2 * lpcm.c: lpcm decoder/packetizer module
3 *****************************************************************************
4 * Copyright (C) 1999-2005 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 /*****************************************************************************
40 * decoder_sys_t : lpcm decoder descriptor
41 *****************************************************************************/
50 audio_date_t end_date;
57 * - private stream ID (16 bits) == 0xA0 -> not in the bitstream
59 * - frame number (8 bits)
60 * - unknown (16 bits) == 0x0003 ?
62 * - current frame (4 bits)
64 * - frequency (2 bits) 0 == 48 kHz, 1 == 32 kHz, 2 == ?, 3 == ?
66 * - number of channels - 1 (3 bits) 1 == 2 channels
67 * - start code (8 bits) == 0x80
70 #define LPCM_HEADER_LEN 6
72 /*****************************************************************************
74 *****************************************************************************/
75 static int OpenDecoder ( vlc_object_t * );
76 static int OpenPacketizer( vlc_object_t * );
77 static void CloseDecoder ( vlc_object_t * );
79 static void *DecodeFrame ( decoder_t *, block_t ** );
81 /*****************************************************************************
83 *****************************************************************************/
86 set_category( CAT_INPUT );
87 set_subcategory( SUBCAT_INPUT_ACODEC );
88 set_description( N_("Linear PCM audio decoder") );
89 set_capability( "decoder", 100 );
90 set_callbacks( OpenDecoder, CloseDecoder );
93 set_description( N_("Linear PCM audio packetizer") );
94 set_capability( "packetizer", 100 );
95 set_callbacks( OpenPacketizer, CloseDecoder );
99 /*****************************************************************************
100 * OpenDecoder: probe the decoder and return score
101 *****************************************************************************/
102 static int OpenDecoder( vlc_object_t *p_this )
104 decoder_t *p_dec = (decoder_t*)p_this;
105 decoder_sys_t *p_sys;
107 if( p_dec->fmt_in.i_codec != VLC_FOURCC('l','p','c','m')
108 && p_dec->fmt_in.i_codec != VLC_FOURCC('l','p','c','b') )
113 /* Allocate the memory needed to store the decoder's structure */
114 if( ( p_dec->p_sys = p_sys =
115 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
119 p_sys->b_packetizer = false;
120 aout_DateSet( &p_sys->end_date, 0 );
122 /* Set output properties */
123 p_dec->fmt_out.i_cat = AUDIO_ES;
125 if( p_dec->fmt_out.audio.i_bitspersample == 24 )
127 p_dec->fmt_out.i_codec = VLC_FOURCC('s','2','4','b');
131 p_dec->fmt_out.i_codec = VLC_FOURCC('s','1','6','b');
132 p_dec->fmt_out.audio.i_bitspersample = 16;
136 p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
138 p_dec->pf_packetize = (block_t *(*)(decoder_t *, block_t **))
144 static int OpenPacketizer( vlc_object_t *p_this )
146 decoder_t *p_dec = (decoder_t*)p_this;
148 int i_ret = OpenDecoder( p_this );
150 if( i_ret != VLC_SUCCESS ) return i_ret;
152 p_dec->p_sys->b_packetizer = true;
154 p_dec->fmt_out.i_codec = VLC_FOURCC('l','p','c','m');
159 /*****************************************************************************
160 * DecodeFrame: decodes an lpcm frame.
161 ****************************************************************************
162 * Beware, this function must be fed with complete frames (PES packet).
163 *****************************************************************************/
164 static void *DecodeFrame( decoder_t *p_dec, block_t **pp_block )
166 decoder_sys_t *p_sys = p_dec->p_sys;
168 unsigned int i_rate = 0, i_original_channels = 0, i_channels = 0;
169 int i_frame_length, i_bitspersample;
172 if( !pp_block || !*pp_block ) return NULL;
175 *pp_block = NULL; /* So the packet doesn't get re-sent */
177 /* Date management */
178 if( p_block->i_pts > 0 &&
179 p_block->i_pts != aout_DateGet( &p_sys->end_date ) )
181 aout_DateSet( &p_sys->end_date, p_block->i_pts );
184 if( !aout_DateGet( &p_sys->end_date ) )
186 /* We've just started the stream, wait for the first PTS. */
187 block_Release( p_block );
191 if( p_block->i_buffer <= LPCM_HEADER_LEN )
193 msg_Err(p_dec, "frame is too short");
194 block_Release( p_block );
198 i_header = p_block->p_buffer[4];
199 switch ( (i_header >> 4) & 0x3 )
215 i_channels = (i_header & 0x7);
216 switch ( i_channels )
219 i_original_channels = AOUT_CHAN_CENTER;
222 i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
225 /* This is unsure. */
226 i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_LFE;
229 i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
230 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
233 /* This is unsure. */
234 i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
235 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
239 i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
240 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
241 | AOUT_CHAN_CENTER | AOUT_CHAN_LFE;
244 i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
245 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
246 | AOUT_CHAN_CENTER | AOUT_CHAN_MIDDLELEFT
247 | AOUT_CHAN_MIDDLERIGHT;
250 i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
251 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
252 | AOUT_CHAN_CENTER | AOUT_CHAN_MIDDLELEFT
253 | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE;
257 switch ( (i_header >> 6) & 0x3 )
260 p_dec->fmt_out.i_codec = VLC_FOURCC('s','2','4','b');
261 p_dec->fmt_out.audio.i_bitspersample = 24;
262 i_bitspersample = 24;
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 = 20;
271 p_dec->fmt_out.i_codec = VLC_FOURCC('s','1','6','b');
272 p_dec->fmt_out.audio.i_bitspersample = 16;
273 i_bitspersample = 16;
277 /* Check frame sync and drop it. */
278 if( p_block->p_buffer[5] != 0x80 )
280 msg_Warn( p_dec, "no frame sync" );
281 block_Release( p_block );
285 /* Set output properties */
286 if( p_dec->fmt_out.audio.i_rate != i_rate )
288 aout_DateInit( &p_sys->end_date, i_rate );
289 aout_DateSet( &p_sys->end_date, p_block->i_pts );
291 p_dec->fmt_out.audio.i_rate = i_rate;
292 p_dec->fmt_out.audio.i_channels = i_channels + 1;
293 p_dec->fmt_out.audio.i_original_channels = i_original_channels;
294 p_dec->fmt_out.audio.i_physical_channels
295 = i_original_channels & AOUT_CHAN_PHYSMASK;
297 i_frame_length = (p_block->i_buffer - LPCM_HEADER_LEN) /
298 p_dec->fmt_out.audio.i_channels * 8 / i_bitspersample;
300 if( p_sys->b_packetizer )
302 p_dec->fmt_out.i_codec = VLC_FOURCC('l','p','c','m');
303 p_block->i_pts = p_block->i_dts = aout_DateGet( &p_sys->end_date );
305 aout_DateIncrement( &p_sys->end_date, i_frame_length ) -
308 /* Just pass on the incoming frame */
313 aout_buffer_t *p_aout_buffer;
314 p_aout_buffer = p_dec->pf_aout_buffer_new( p_dec, i_frame_length );
315 if( p_aout_buffer == NULL ) return NULL;
317 p_aout_buffer->start_date = aout_DateGet( &p_sys->end_date );
318 p_aout_buffer->end_date =
319 aout_DateIncrement( &p_sys->end_date, i_frame_length );
321 p_block->p_buffer += LPCM_HEADER_LEN;
322 p_block->i_buffer -= LPCM_HEADER_LEN;
324 /* 20/24 bits LPCM use special packing */
325 if( i_bitspersample == 24 )
327 uint8_t *p_out = p_aout_buffer->p_buffer;
329 while( p_block->i_buffer / 12 )
332 p_out[0] = p_block->p_buffer[0];
333 p_out[1] = p_block->p_buffer[1];
334 p_out[2] = p_block->p_buffer[8];
336 p_out[3] = p_block->p_buffer[2];
337 p_out[4] = p_block->p_buffer[3];
338 p_out[5] = p_block->p_buffer[9];
340 p_out[6] = p_block->p_buffer[4];
341 p_out[7] = p_block->p_buffer[5];
342 p_out[8] = p_block->p_buffer[10];
344 p_out[9] = p_block->p_buffer[6];
345 p_out[10] = p_block->p_buffer[7];
346 p_out[11] = p_block->p_buffer[11];
348 p_block->i_buffer -= 12;
349 p_block->p_buffer += 12;
353 else if( i_bitspersample == 20 )
355 uint8_t *p_out = p_aout_buffer->p_buffer;
357 while( p_block->i_buffer / 10 )
360 p_out[0] = p_block->p_buffer[0];
361 p_out[1] = p_block->p_buffer[1];
362 p_out[2] = p_block->p_buffer[8] & 0xF0;
364 p_out[3] = p_block->p_buffer[2];
365 p_out[4] = p_block->p_buffer[3];
366 p_out[5] = p_block->p_buffer[8] << 4;
368 p_out[6] = p_block->p_buffer[4];
369 p_out[7] = p_block->p_buffer[5];
370 p_out[8] = p_block->p_buffer[9] & 0xF0;
372 p_out[9] = p_block->p_buffer[6];
373 p_out[10] = p_block->p_buffer[7];
374 p_out[11] = p_block->p_buffer[9] << 4;
376 p_block->i_buffer -= 10;
377 p_block->p_buffer += 10;
383 memcpy( p_aout_buffer->p_buffer,
384 p_block->p_buffer, p_block->i_buffer );
387 block_Release( p_block );
388 return p_aout_buffer;
392 /*****************************************************************************
393 * CloseDecoder : lpcm decoder destruction
394 *****************************************************************************/
395 static void CloseDecoder( vlc_object_t *p_this )
397 decoder_t *p_dec = (decoder_t*)p_this;
398 free( p_dec->p_sys );