]> git.sesse.net Git - vlc/blob - modules/codec/lpcm.c
Missing <assert.h>
[vlc] / modules / codec / lpcm.c
1 /*****************************************************************************
2  * lpcm.c: lpcm decoder/packetizer module
3  *****************************************************************************
4  * Copyright (C) 1999-2008 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  *          Lauren Aimar <fenrir _AT_ videolan _DOT_ org >
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
26  *****************************************************************************/
27
28 /*****************************************************************************
29  * Preamble
30  *****************************************************************************/
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
34
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
37 #include <vlc_codec.h>
38 #include <vlc_aout.h>
39 #include <assert.h>
40
41 /*****************************************************************************
42  * Module descriptor
43  *****************************************************************************/
44 static int  OpenDecoder   ( vlc_object_t * );
45 static int  OpenPacketizer( vlc_object_t * );
46 static void CloseCommon   ( vlc_object_t * );
47
48 vlc_module_begin ()
49
50     set_category( CAT_INPUT )
51     set_subcategory( SUBCAT_INPUT_ACODEC )
52     set_description( N_("Linear PCM audio decoder") )
53     set_capability( "decoder", 100 )
54     set_callbacks( OpenDecoder, CloseCommon )
55
56     add_submodule ()
57     set_description( N_("Linear PCM audio packetizer") )
58     set_capability( "packetizer", 100 )
59     set_callbacks( OpenPacketizer, CloseCommon )
60
61 vlc_module_end ()
62
63
64 /*****************************************************************************
65  * decoder_sys_t : lpcm decoder descriptor
66  *****************************************************************************/
67 struct decoder_sys_t
68 {
69     /* Module mode */
70     bool b_packetizer;
71
72     /*
73      * Output properties
74      */
75     audio_date_t end_date;
76
77     /* */
78     unsigned i_header_size;
79     bool b_dvd;
80 };
81
82 /*
83  * LPCM DVD header :
84  * - frame number (8 bits)
85  * - unknown (16 bits) == 0x0003 ?
86  * - unknown (4 bits)
87  * - current frame (4 bits)
88  * - unknown (2 bits)
89  * - frequency (2 bits) 0 == 48 kHz, 1 == 32 kHz, 2 == ?, 3 == ?
90  * - unknown (1 bit)
91  * - number of channels - 1 (3 bits) 1 == 2 channels
92  * - start code (8 bits) == 0x80
93  *
94  * LPCM BD header :
95  * - unkown (16 bits)
96  * - number of channels (4 bits)
97  * - frequency (4 bits)
98  * - bits per sample (2 bits)
99  * - unknown (6 bits)
100  */
101
102 #define LPCM_DVD_HEADER_LEN (6)
103 #define LPCM_BD_HEADER_LEN (4)
104
105 /*****************************************************************************
106  * Local prototypes
107  *****************************************************************************/
108 static void *DecodeFrame  ( decoder_t *, block_t ** );
109 static int DvdHeader( unsigned *pi_rate,
110                       unsigned *pi_channels, unsigned *pi_original_channels,
111                       unsigned *pi_bits,
112                       const uint8_t *p_header );
113 static void DvdExtract( aout_buffer_t *, block_t *, unsigned i_bits );
114
115 static int BdHeader( unsigned *pi_rate,
116                      unsigned *pi_channels, unsigned *pi_original_channels,
117                      unsigned *pi_bits,
118                      const uint8_t *p_header );
119 static void BdExtract( aout_buffer_t *, block_t * );
120
121
122 /*****************************************************************************
123  * OpenCommon:
124  *****************************************************************************/
125 static int OpenCommon( vlc_object_t *p_this, bool b_packetizer )
126 {
127     decoder_t *p_dec = (decoder_t*)p_this;
128     decoder_sys_t *p_sys;
129     bool b_dvd;
130
131     switch( p_dec->fmt_in.i_codec )
132     {
133     /* DVD LPCM */
134     case VLC_FOURCC('l','p','c','m'):
135     case VLC_FOURCC('l','p','c','b'):
136         b_dvd = true;
137         break;
138     /* BD LPCM */
139     case VLC_FOURCC('b','p','c','m'):
140         b_dvd = false;
141         break;
142     default:
143         return VLC_EGENERIC;
144     }
145
146     /* Allocate the memory needed to store the decoder's structure */
147     if( ( p_dec->p_sys = p_sys = malloc(sizeof(decoder_sys_t)) ) == NULL )
148         return VLC_ENOMEM;
149
150     /* Misc init */
151     p_sys->b_packetizer = b_packetizer;
152     aout_DateSet( &p_sys->end_date, 0 );
153     p_sys->i_header_size = b_dvd ? LPCM_DVD_HEADER_LEN : LPCM_BD_HEADER_LEN;
154     p_sys->b_dvd = b_dvd;
155
156     /* Set output properties */
157     p_dec->fmt_out.i_cat = AUDIO_ES;
158
159     if( b_packetizer )
160     {
161         p_dec->fmt_out.i_codec = b_dvd ? VLC_FOURCC('l','p','c','m') : VLC_FOURCC('b','p','c','m');
162     }
163     else
164     {
165         switch( p_dec->fmt_out.audio.i_bitspersample )
166         {
167         case 24:
168         case 20:
169             p_dec->fmt_out.i_codec = VLC_FOURCC('s','2','4','b');
170             p_dec->fmt_out.audio.i_bitspersample = 24;
171             break;
172         default:
173             p_dec->fmt_out.i_codec = VLC_FOURCC('s','1','6','b');
174             p_dec->fmt_out.audio.i_bitspersample = 16;
175             break;
176         }
177     }
178
179     /* Set callback */
180     p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
181         DecodeFrame;
182     p_dec->pf_packetize    = (block_t *(*)(decoder_t *, block_t **))
183         DecodeFrame;
184
185     return VLC_SUCCESS;
186 }
187 static int OpenDecoder( vlc_object_t *p_this )
188 {
189     return OpenCommon( p_this, false );
190 }
191 static int OpenPacketizer( vlc_object_t *p_this )
192 {
193     return OpenCommon( p_this, true );
194 }
195
196 /*****************************************************************************
197  * DecodeFrame: decodes an lpcm frame.
198  ****************************************************************************
199  * Beware, this function must be fed with complete frames (PES packet).
200  *****************************************************************************/
201 static void *DecodeFrame( decoder_t *p_dec, block_t **pp_block )
202 {
203     decoder_sys_t *p_sys = p_dec->p_sys;
204     block_t       *p_block;
205     unsigned int  i_rate = 0, i_original_channels = 0, i_channels = 0, i_bits = 0;
206     int           i_frame_length;
207
208     if( !pp_block || !*pp_block ) return NULL;
209
210     p_block = *pp_block;
211     *pp_block = NULL; /* So the packet doesn't get re-sent */
212
213     /* Date management */
214     if( p_block->i_pts > 0 &&
215         p_block->i_pts != aout_DateGet( &p_sys->end_date ) )
216     {
217         aout_DateSet( &p_sys->end_date, p_block->i_pts );
218     }
219
220     if( !aout_DateGet( &p_sys->end_date ) )
221     {
222         /* We've just started the stream, wait for the first PTS. */
223         block_Release( p_block );
224         return NULL;
225     }
226
227     if( p_block->i_buffer <= p_sys->i_header_size )
228     {
229         msg_Err(p_dec, "frame is too short");
230         block_Release( p_block );
231         return NULL;
232     }
233
234     int i_ret;
235     if( p_sys->b_dvd )
236         i_ret = DvdHeader( &i_rate, &i_channels, &i_original_channels, &i_bits,
237                            p_block->p_buffer );
238     else
239         i_ret = BdHeader( &i_rate, &i_channels, &i_original_channels, &i_bits,
240                           p_block->p_buffer );
241
242     if( i_ret )
243     {
244         msg_Warn( p_dec, "no frame sync" );
245         block_Release( p_block );
246         return NULL;
247     }
248
249     /* Set output properties */
250     if( p_dec->fmt_out.audio.i_rate != i_rate )
251     {
252         aout_DateInit( &p_sys->end_date, i_rate );
253         aout_DateSet( &p_sys->end_date, p_block->i_pts );
254     }
255     p_dec->fmt_out.audio.i_rate = i_rate;
256     p_dec->fmt_out.audio.i_channels = i_channels;
257     p_dec->fmt_out.audio.i_original_channels = i_original_channels;
258     p_dec->fmt_out.audio.i_physical_channels = i_original_channels & AOUT_CHAN_PHYSMASK;
259
260     i_frame_length = (p_block->i_buffer - p_sys->i_header_size) / i_channels * 8 / i_bits;
261
262     if( p_sys->b_packetizer )
263     {
264         p_block->i_pts = p_block->i_dts = aout_DateGet( &p_sys->end_date );
265         p_block->i_length =
266             aout_DateIncrement( &p_sys->end_date, i_frame_length ) -
267             p_block->i_pts;
268
269         /* Just pass on the incoming frame */
270         return p_block;
271     }
272     else
273     {
274         /* */
275         if( i_bits == 16 )
276         {
277             p_dec->fmt_out.i_codec = VLC_FOURCC('s','1','6','b');
278             p_dec->fmt_out.audio.i_bitspersample = 16;
279         }
280         else
281         {
282             p_dec->fmt_out.i_codec = VLC_FOURCC('s','2','4','b');
283             p_dec->fmt_out.audio.i_bitspersample = 24;
284         }
285
286         /* */
287         aout_buffer_t *p_aout_buffer;
288         p_aout_buffer = decoder_NewAudioBuffer( p_dec, i_frame_length );
289         if( !p_aout_buffer )
290             return NULL;
291
292         p_aout_buffer->start_date = aout_DateGet( &p_sys->end_date );
293         p_aout_buffer->end_date =
294             aout_DateIncrement( &p_sys->end_date, i_frame_length );
295
296         p_block->p_buffer += p_sys->i_header_size;
297         p_block->i_buffer -= p_sys->i_header_size;
298
299         if( p_sys->b_dvd )
300             DvdExtract( p_aout_buffer, p_block, i_bits );
301         else
302             BdExtract( p_aout_buffer, p_block );
303
304         block_Release( p_block );
305         return p_aout_buffer;
306     }
307 }
308
309 /*****************************************************************************
310  * CloseCommon : lpcm decoder destruction
311  *****************************************************************************/
312 static void CloseCommon( vlc_object_t *p_this )
313 {
314     decoder_t *p_dec = (decoder_t*)p_this;
315     free( p_dec->p_sys );
316 }
317
318 /*****************************************************************************
319  *
320  *****************************************************************************/
321 static int DvdHeader( unsigned *pi_rate,
322                       unsigned *pi_channels, unsigned *pi_original_channels,
323                       unsigned *pi_bits,
324                       const uint8_t *p_header )
325 {
326     const uint8_t i_header = p_header[4];
327
328     switch( (i_header >> 4) & 0x3 )
329     {
330     case 0:
331         *pi_rate = 48000;
332         break;
333     case 1:
334         *pi_rate = 96000;
335         break;
336     case 2:
337         *pi_rate = 44100;
338         break;
339     case 3:
340         *pi_rate = 32000;
341         break;
342     }
343
344     *pi_channels = (i_header & 0x7) + 1;
345     switch( *pi_channels - 1 )
346     {
347     case 0:
348         *pi_original_channels = AOUT_CHAN_CENTER;
349         break;
350     case 1:
351         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
352         break;
353     case 2:
354         /* This is unsure. */
355         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_LFE;
356         break;
357     case 3:
358         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
359                                | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
360         break;
361     case 4:
362         /* This is unsure. */
363         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
364                                | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
365                                | AOUT_CHAN_LFE;
366         break;
367     case 5:
368         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
369                                | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
370                                | AOUT_CHAN_CENTER | AOUT_CHAN_LFE;
371         break;
372     case 6:
373         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
374                                | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
375                                | AOUT_CHAN_CENTER | AOUT_CHAN_MIDDLELEFT
376                                | AOUT_CHAN_MIDDLERIGHT;
377         break;
378     case 7:
379         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
380                                | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
381                                | AOUT_CHAN_CENTER | AOUT_CHAN_MIDDLELEFT
382                                | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE;
383         break;
384     }
385
386     switch( (i_header >> 6) & 0x3 )
387     {
388     case 2:
389         *pi_bits = 24;
390         break;
391     case 1:
392         *pi_bits = 20;
393         break;
394     case 0:
395     default:
396         *pi_bits = 16;
397         break;
398     }
399
400     /* Check frame sync and drop it. */
401     if( p_header[5] != 0x80 )
402         return -1;
403     return 0;
404 }
405 static int BdHeader( unsigned *pi_rate,
406                      unsigned *pi_channels, unsigned *pi_original_channels,
407                      unsigned *pi_bits,
408                      const uint8_t *p_header )
409 {
410     const uint32_t h = GetDWBE( p_header );
411     switch( ( h & 0xf000) >> 12 )
412     {
413     case 1:
414         *pi_channels = 1;
415         *pi_original_channels = AOUT_CHAN_CENTER;
416         break;
417     case 3:
418         *pi_channels = 2;
419         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
420         break;
421     case 4:
422         *pi_channels = 3;
423         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER;
424         break;
425     case 5:
426         *pi_channels = 3;
427         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER;
428         break;
429     case 6:
430         *pi_channels = 4;
431         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
432                                 AOUT_CHAN_REARCENTER;
433         break;
434     case 7:
435         *pi_channels = 4;
436         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
437                                 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
438         break;
439     case 8:
440         *pi_channels = 5;
441         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
442                                 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
443         break;
444     case 9:
445         *pi_channels = 6;
446         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
447                                 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT |
448                                 AOUT_CHAN_LFE;
449         break;
450     case 10:
451         *pi_channels = 7;
452         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
453                                 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT |
454                                 AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT;
455         break;
456     case 11:
457         *pi_channels = 8;
458         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
459                                 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT |
460                                 AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT |
461                                 AOUT_CHAN_LFE;
462         break;
463
464     default:
465         return -1;
466     }
467     switch( (h >> 6) & 0x03 )
468     {
469     case 1:
470         *pi_bits = 16;
471         break;
472     case 2: /* 20 bits but samples are stored on 24 bits */
473     case 3: /* 24 bits */
474         *pi_bits = 24;
475         break;
476     default:
477         return -1;
478     }
479     switch( (h >> 8) & 0x0f ) 
480     {
481     case 1:
482         *pi_rate = 48000;
483         break;
484     case 4:
485         *pi_rate = 96000;
486         break;
487     case 5:
488         *pi_rate = 192000;
489         break;
490     default:
491         return -1;
492     }
493     return 0;
494 }
495
496 static void DvdExtract( aout_buffer_t *p_aout_buffer, block_t *p_block,
497                         unsigned i_bits )
498 {
499     uint8_t *p_out = p_aout_buffer->p_buffer;
500
501     /* 20/24 bits LPCM use special packing */
502     if( i_bits == 24 )
503     {
504         while( p_block->i_buffer / 12 )
505         {
506             /* Sample 1 */
507             p_out[0] = p_block->p_buffer[0];
508             p_out[1] = p_block->p_buffer[1];
509             p_out[2] = p_block->p_buffer[8];
510             /* Sample 2 */
511             p_out[3] = p_block->p_buffer[2];
512             p_out[4] = p_block->p_buffer[3];
513             p_out[5] = p_block->p_buffer[9];
514             /* Sample 3 */
515             p_out[6] = p_block->p_buffer[4];
516             p_out[7] = p_block->p_buffer[5];
517             p_out[8] = p_block->p_buffer[10];
518             /* Sample 4 */
519             p_out[9] = p_block->p_buffer[6];
520             p_out[10] = p_block->p_buffer[7];
521             p_out[11] = p_block->p_buffer[11];
522
523             p_block->i_buffer -= 12;
524             p_block->p_buffer += 12;
525             p_out += 12;
526         }
527     }
528     else if( i_bits == 20 )
529     {
530         while( p_block->i_buffer / 10 )
531         {
532             /* Sample 1 */
533             p_out[0] = p_block->p_buffer[0];
534             p_out[1] = p_block->p_buffer[1];
535             p_out[2] = p_block->p_buffer[8] & 0xF0;
536             /* Sample 2 */
537             p_out[3] = p_block->p_buffer[2];
538             p_out[4] = p_block->p_buffer[3];
539             p_out[5] = p_block->p_buffer[8] << 4;
540             /* Sample 3 */
541             p_out[6] = p_block->p_buffer[4];
542             p_out[7] = p_block->p_buffer[5];
543             p_out[8] = p_block->p_buffer[9] & 0xF0;
544             /* Sample 4 */
545             p_out[9] = p_block->p_buffer[6];
546             p_out[10] = p_block->p_buffer[7];
547             p_out[11] = p_block->p_buffer[9] << 4;
548
549             p_block->i_buffer -= 10;
550             p_block->p_buffer += 10;
551             p_out += 12;
552         }
553     }
554     else
555     {
556         assert( i_bits == 16 );
557         memcpy( p_out, p_block->p_buffer, p_block->i_buffer );
558     }
559 }
560 static void BdExtract( aout_buffer_t *p_aout_buffer, block_t *p_block )
561 {
562     memcpy( p_aout_buffer->p_buffer, p_block->p_buffer, p_block->i_buffer );
563 }
564
565