1 /*****************************************************************************
2 * lpcm.c: lpcm decoder/packetizer module
3 *****************************************************************************
4 * Copyright (C) 1999-2003 VideoLAN
7 * Authors: Samuel Hocevar <sam@zoy.org>
8 * Henri Fallon <henri@videolan.org>
9 * Christophe Massiot <massiot@via.ecp.fr>
10 * Gildas Bazin <gbazin@netcourrier.com>
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
25 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
31 #include <vlc/decoder.h>
33 /*****************************************************************************
34 * decoder_sys_t : lpcm decoder descriptor
35 *****************************************************************************/
39 vlc_bool_t b_packetizer;
44 audio_date_t end_date;
51 * - private stream ID (16 bits) == 0xA0 -> not in the bitstream
53 * - frame number (8 bits)
54 * - unknown (16 bits) == 0x0003 ?
56 * - current frame (4 bits)
58 * - frequency (2 bits) 0 == 48 kHz, 1 == 32 kHz, 2 == ?, 3 == ?
60 * - number of channels - 1 (3 bits) 1 == 2 channels
61 * - start code (8 bits) == 0x80
64 #define LPCM_HEADER_LEN 6
66 /*****************************************************************************
68 *****************************************************************************/
69 static int OpenDecoder ( vlc_object_t * );
70 static int OpenPacketizer( vlc_object_t * );
71 static void CloseDecoder ( vlc_object_t * );
73 static void *DecodeFrame ( decoder_t *, block_t ** );
75 /*****************************************************************************
77 *****************************************************************************/
80 set_category( CAT_INPUT );
81 set_subcategory( SUBCAT_INPUT_ACODEC );
82 set_description( _("Linear PCM audio decoder") );
83 set_capability( "decoder", 100 );
84 set_callbacks( OpenDecoder, CloseDecoder );
87 set_description( _("Linear PCM audio packetizer") );
88 set_capability( "packetizer", 100 );
89 set_callbacks( OpenPacketizer, CloseDecoder );
93 /*****************************************************************************
94 * OpenDecoder: probe the decoder and return score
95 *****************************************************************************/
96 static int OpenDecoder( vlc_object_t *p_this )
98 decoder_t *p_dec = (decoder_t*)p_this;
101 if( p_dec->fmt_in.i_codec != VLC_FOURCC('l','p','c','m')
102 && p_dec->fmt_in.i_codec != VLC_FOURCC('l','p','c','b') )
107 /* Allocate the memory needed to store the decoder's structure */
108 if( ( p_dec->p_sys = p_sys =
109 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
111 msg_Err( p_dec, "out of memory" );
116 p_sys->b_packetizer = VLC_FALSE;
117 aout_DateSet( &p_sys->end_date, 0 );
119 /* Set output properties */
120 p_dec->fmt_out.i_cat = AUDIO_ES;
121 p_dec->fmt_out.i_codec = VLC_FOURCC('s','1','6','b');
124 p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
126 p_dec->pf_packetize = (block_t *(*)(decoder_t *, block_t **))
132 static int OpenPacketizer( vlc_object_t *p_this )
134 decoder_t *p_dec = (decoder_t*)p_this;
136 int i_ret = OpenDecoder( p_this );
138 if( i_ret != VLC_SUCCESS ) return i_ret;
140 p_dec->p_sys->b_packetizer = VLC_TRUE;
142 p_dec->fmt_out.i_codec = VLC_FOURCC('l','p','c','m');
147 /*****************************************************************************
148 * DecodeFrame: decodes an lpcm frame.
149 ****************************************************************************
150 * Beware, this function must be fed with complete frames (PES packet).
151 *****************************************************************************/
152 static void *DecodeFrame( decoder_t *p_dec, block_t **pp_block )
154 decoder_sys_t *p_sys = p_dec->p_sys;
156 unsigned int i_rate = 0, i_original_channels = 0, i_channels = 0;
160 if( !pp_block || !*pp_block ) return NULL;
163 *pp_block = NULL; /* So the packet doesn't get re-sent */
165 /* Date management */
166 if( p_block->i_pts > 0 &&
167 p_block->i_pts != aout_DateGet( &p_sys->end_date ) )
169 aout_DateSet( &p_sys->end_date, p_block->i_pts );
172 if( !aout_DateGet( &p_sys->end_date ) )
174 /* We've just started the stream, wait for the first PTS. */
175 block_Release( p_block );
179 if( p_block->i_buffer <= LPCM_HEADER_LEN )
181 msg_Err(p_dec, "frame is too short");
182 block_Release( p_block );
186 i_header = p_block->p_buffer[4];
187 switch ( (i_header >> 4) & 0x3 )
203 i_channels = (i_header & 0x7);
204 switch ( i_channels )
207 i_original_channels = AOUT_CHAN_CENTER;
210 i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
213 /* This is unsure. */
214 i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_LFE;
217 i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
218 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
221 /* This is unsure. */
222 i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
223 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
227 i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
228 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
229 | AOUT_CHAN_CENTER | AOUT_CHAN_LFE;
232 i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
233 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
234 | AOUT_CHAN_CENTER | AOUT_CHAN_MIDDLELEFT
235 | AOUT_CHAN_MIDDLERIGHT;
238 i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
239 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
240 | AOUT_CHAN_CENTER | AOUT_CHAN_MIDDLELEFT
241 | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE;
245 /* Check frame sync and drop it. */
246 if( p_block->p_buffer[5] != 0x80 )
248 msg_Warn( p_dec, "no frame sync" );
249 block_Release( p_block );
253 /* Set output properties */
254 if( p_dec->fmt_out.audio.i_rate != i_rate )
256 aout_DateInit( &p_sys->end_date, i_rate );
257 aout_DateSet( &p_sys->end_date, p_block->i_pts );
259 p_dec->fmt_out.audio.i_rate = i_rate;
260 p_dec->fmt_out.audio.i_channels = i_channels + 1;
261 p_dec->fmt_out.audio.i_original_channels = i_original_channels;
262 p_dec->fmt_out.audio.i_physical_channels
263 = i_original_channels & AOUT_CHAN_PHYSMASK;
265 i_frame_length = (p_block->i_buffer - LPCM_HEADER_LEN) /
266 ( p_dec->fmt_out.audio.i_channels * 2 );
268 if( p_sys->b_packetizer )
270 p_block->i_pts = p_block->i_dts = aout_DateGet( &p_sys->end_date );
272 aout_DateIncrement( &p_sys->end_date, i_frame_length ) -
275 /* Just pass on the incoming frame */
280 aout_buffer_t *p_aout_buffer;
281 p_aout_buffer = p_dec->pf_aout_buffer_new( p_dec, i_frame_length );
282 if( p_aout_buffer == NULL ) return NULL;
284 p_aout_buffer->start_date = aout_DateGet( &p_sys->end_date );
285 p_aout_buffer->end_date =
286 aout_DateIncrement( &p_sys->end_date, i_frame_length );
288 memcpy( p_aout_buffer->p_buffer,
289 p_block->p_buffer + LPCM_HEADER_LEN,
290 p_block->i_buffer - LPCM_HEADER_LEN );
292 block_Release( p_block );
293 return p_aout_buffer;
297 /*****************************************************************************
298 * CloseDecoder : lpcm decoder destruction
299 *****************************************************************************/
300 static void CloseDecoder( vlc_object_t *p_this )
302 decoder_t *p_dec = (decoder_t*)p_this;
303 free( p_dec->p_sys );