]> git.sesse.net Git - vlc/blob - modules/codec/lpcm.c
Remove most stray semi-colons in module descriptions
[vlc] / modules / codec / lpcm.c
1 /*****************************************************************************
2  * lpcm.c: lpcm decoder/packetizer module
3  *****************************************************************************
4  * Copyright (C) 1999-2005 the VideoLAN team
5  * $Id$
6  *
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  *
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.
16  *
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.
21  *
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  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_codec.h>
37 #include <vlc_aout.h>
38
39 /*****************************************************************************
40  * decoder_sys_t : lpcm decoder descriptor
41  *****************************************************************************/
42 struct decoder_sys_t
43 {
44     /* Module mode */
45     bool b_packetizer;
46
47     /*
48      * Output properties
49      */
50     audio_date_t end_date;
51
52 };
53
54 /*
55  * LPCM header :
56  * - PES header
57  * - private stream ID (16 bits) == 0xA0 -> not in the bitstream
58  *
59  * - frame number (8 bits)
60  * - unknown (16 bits) == 0x0003 ?
61  * - unknown (4 bits)
62  * - current frame (4 bits)
63  * - unknown (2 bits)
64  * - frequency (2 bits) 0 == 48 kHz, 1 == 32 kHz, 2 == ?, 3 == ?
65  * - unknown (1 bit)
66  * - number of channels - 1 (3 bits) 1 == 2 channels
67  * - start code (8 bits) == 0x80
68  */
69
70 #define LPCM_HEADER_LEN 6
71
72 /*****************************************************************************
73  * Local prototypes
74  *****************************************************************************/
75 static int  OpenDecoder   ( vlc_object_t * );
76 static int  OpenPacketizer( vlc_object_t * );
77 static void CloseDecoder  ( vlc_object_t * );
78
79 static void *DecodeFrame  ( decoder_t *, block_t ** );
80
81 /*****************************************************************************
82  * Module descriptor
83  *****************************************************************************/
84 vlc_module_begin ()
85
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 )
91
92     add_submodule ()
93     set_description( N_("Linear PCM audio packetizer") )
94     set_capability( "packetizer", 100 )
95     set_callbacks( OpenPacketizer, CloseDecoder )
96
97 vlc_module_end ()
98
99 /*****************************************************************************
100  * OpenDecoder: probe the decoder and return score
101  *****************************************************************************/
102 static int OpenDecoder( vlc_object_t *p_this )
103 {
104     decoder_t *p_dec = (decoder_t*)p_this;
105     decoder_sys_t *p_sys;
106
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') )
109     {
110         return VLC_EGENERIC;
111     }
112
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 )
116         return VLC_ENOMEM;
117
118     /* Misc init */
119     p_sys->b_packetizer = false;
120     aout_DateSet( &p_sys->end_date, 0 );
121
122     /* Set output properties */
123     p_dec->fmt_out.i_cat = AUDIO_ES;
124
125     if( p_dec->fmt_out.audio.i_bitspersample == 24 )
126     {
127         p_dec->fmt_out.i_codec = VLC_FOURCC('s','2','4','b');
128     }
129     else
130     {
131         p_dec->fmt_out.i_codec = VLC_FOURCC('s','1','6','b');
132         p_dec->fmt_out.audio.i_bitspersample = 16;
133     }
134
135     /* Set callback */
136     p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
137         DecodeFrame;
138     p_dec->pf_packetize    = (block_t *(*)(decoder_t *, block_t **))
139         DecodeFrame;
140
141     return VLC_SUCCESS;
142 }
143
144 static int OpenPacketizer( vlc_object_t *p_this )
145 {
146     decoder_t *p_dec = (decoder_t*)p_this;
147
148     int i_ret = OpenDecoder( p_this );
149
150     if( i_ret != VLC_SUCCESS ) return i_ret;
151
152     p_dec->p_sys->b_packetizer = true;
153
154     p_dec->fmt_out.i_codec = VLC_FOURCC('l','p','c','m');
155
156     return i_ret;
157 }
158
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 )
165 {
166     decoder_sys_t *p_sys = p_dec->p_sys;
167     block_t       *p_block;
168     unsigned int  i_rate = 0, i_original_channels = 0, i_channels = 0;
169     int           i_frame_length, i_bitspersample;
170     uint8_t       i_header;
171
172     if( !pp_block || !*pp_block ) return NULL;
173
174     p_block = *pp_block;
175     *pp_block = NULL; /* So the packet doesn't get re-sent */
176
177     /* Date management */
178     if( p_block->i_pts > 0 &&
179         p_block->i_pts != aout_DateGet( &p_sys->end_date ) )
180     {
181         aout_DateSet( &p_sys->end_date, p_block->i_pts );
182     }
183
184     if( !aout_DateGet( &p_sys->end_date ) )
185     {
186         /* We've just started the stream, wait for the first PTS. */
187         block_Release( p_block );
188         return NULL;
189     }
190
191     if( p_block->i_buffer <= LPCM_HEADER_LEN )
192     {
193         msg_Err(p_dec, "frame is too short");
194         block_Release( p_block );
195         return NULL;
196     }
197
198     i_header = p_block->p_buffer[4];
199     switch ( (i_header >> 4) & 0x3 )
200     {
201     case 0:
202         i_rate = 48000;
203         break;
204     case 1:
205         i_rate = 96000;
206         break;
207     case 2:
208         i_rate = 44100;
209         break;
210     case 3:
211         i_rate = 32000;
212         break;
213     }
214
215     i_channels = (i_header & 0x7);
216     switch ( i_channels )
217     {
218     case 0:
219         i_original_channels = AOUT_CHAN_CENTER;
220         break;
221     case 1:
222         i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
223         break;
224     case 2:
225         /* This is unsure. */
226         i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_LFE;
227         break;
228     case 3:
229         i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
230                                | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
231         break;
232     case 4:
233         /* This is unsure. */
234         i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
235                                | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
236                                | AOUT_CHAN_LFE;
237         break;
238     case 5:
239         i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
240                                | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
241                                | AOUT_CHAN_CENTER | AOUT_CHAN_LFE;
242         break;
243     case 6:
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;
248         break;
249     case 7:
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;
254         break;
255     }
256
257     switch ( (i_header >> 6) & 0x3 )
258     {
259     case 2:
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;
263         break;
264     case 1:
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;
268         break;
269     case 0:
270     default:
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;
274         break;
275     }
276
277     /* Check frame sync and drop it. */
278     if( p_block->p_buffer[5] != 0x80 )
279     {
280         msg_Warn( p_dec, "no frame sync" );
281         block_Release( p_block );
282         return NULL;
283     }
284
285     /* Set output properties */
286     if( p_dec->fmt_out.audio.i_rate != i_rate )
287     {
288         aout_DateInit( &p_sys->end_date, i_rate );
289         aout_DateSet( &p_sys->end_date, p_block->i_pts );
290     }
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;
296
297     i_frame_length = (p_block->i_buffer - LPCM_HEADER_LEN) /
298         p_dec->fmt_out.audio.i_channels * 8 / i_bitspersample;
299
300     if( p_sys->b_packetizer )
301     {
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 );
304         p_block->i_length =
305             aout_DateIncrement( &p_sys->end_date, i_frame_length ) -
306             p_block->i_pts;
307
308         /* Just pass on the incoming frame */
309         return p_block;
310     }
311     else
312     {
313         aout_buffer_t *p_aout_buffer;
314         p_aout_buffer = decoder_NewAudioBuffer( p_dec, i_frame_length );
315         if( p_aout_buffer == NULL ) return NULL;
316
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 );
320
321         p_block->p_buffer += LPCM_HEADER_LEN;
322         p_block->i_buffer -= LPCM_HEADER_LEN;
323
324         /* 20/24 bits LPCM use special packing */
325         if( i_bitspersample == 24 )
326         {
327             uint8_t *p_out = p_aout_buffer->p_buffer;
328
329             while( p_block->i_buffer / 12 )
330             {
331                 /* Sample 1 */
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];
335                 /* Sample 2 */
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];
339                 /* Sample 3 */
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];
343                 /* Sample 4 */
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];
347
348                 p_block->i_buffer -= 12;
349                 p_block->p_buffer += 12;
350                 p_out += 12;
351             }
352         }
353         else if( i_bitspersample == 20 )
354         {
355             uint8_t *p_out = p_aout_buffer->p_buffer;
356
357             while( p_block->i_buffer / 10 )
358             {
359                 /* Sample 1 */
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;
363                 /* Sample 2 */
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;
367                 /* Sample 3 */
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;
371                 /* Sample 4 */
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;
375
376                 p_block->i_buffer -= 10;
377                 p_block->p_buffer += 10;
378                 p_out += 12;
379             }
380         }
381         else
382         {
383             memcpy( p_aout_buffer->p_buffer,
384                     p_block->p_buffer, p_block->i_buffer );
385         }
386
387         block_Release( p_block );
388         return p_aout_buffer;
389     }
390 }
391
392 /*****************************************************************************
393  * CloseDecoder : lpcm decoder destruction
394  *****************************************************************************/
395 static void CloseDecoder( vlc_object_t *p_this )
396 {
397     decoder_t *p_dec = (decoder_t*)p_this;
398     free( p_dec->p_sys );
399 }