1 /*****************************************************************************
2 * mpeg_audio.c: parse MPEG audio sync info and packetize the stream
3 *****************************************************************************
4 * Copyright (C) 2001-2003 VLC authors and VideoLAN
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Eric Petit <titer@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 it
13 * under the terms of the GNU Lesser General Public License as published by
14 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public License
23 * along with this program; if not, write to the Free Software Foundation,
24 * 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>
38 #include <vlc_modules.h>
41 #include <vlc_block_helper.h>
43 #include "../packetizer/packetizer_helper.h"
45 /*****************************************************************************
46 * decoder_sys_t : decoder descriptor
47 *****************************************************************************/
58 block_bytestream_t bytestream;
64 unsigned int i_current_layer;
68 int i_frame_size, i_free_frame_size;
69 unsigned int i_channels_conf, i_channels;
70 unsigned int i_rate, i_max_frame_size, i_frame_length;
71 unsigned int i_layer, i_bit_rate;
76 /* This isn't the place to put mad-specific stuff. However, it makes the
77 * mad plug-in's life much easier if we put 8 extra bytes at the end of the
78 * buffer, because that way it doesn't have to copy the block_t to a bigger
79 * buffer. This has no implication on other plug-ins, and we only lose 8 bytes
80 * per frame. --Meuuh */
81 #define MAD_BUFFER_GUARD 8
82 #define MPGA_HEADER_SIZE 4
84 /****************************************************************************
86 ****************************************************************************/
87 static int OpenDecoder ( vlc_object_t * );
88 static int OpenPacketizer( vlc_object_t * );
89 static void CloseDecoder ( vlc_object_t * );
90 static block_t *DecodeBlock ( decoder_t *, block_t ** );
92 static uint8_t *GetOutBuffer ( decoder_t *, block_t ** );
93 static block_t *GetAoutBuffer( decoder_t * );
94 static block_t *GetSoutBuffer( decoder_t * );
96 static int SyncInfo( uint32_t i_header, unsigned int * pi_channels,
97 unsigned int * pi_channels_conf,
98 unsigned int * pi_sample_rate, unsigned int * pi_bit_rate,
99 unsigned int * pi_frame_length,
100 unsigned int * pi_max_frame_size,
101 unsigned int * pi_layer );
103 /*****************************************************************************
105 *****************************************************************************/
107 set_description( N_("MPEG audio layer I/II/III decoder") )
108 set_category( CAT_INPUT )
109 set_subcategory( SUBCAT_INPUT_ACODEC )
110 set_capability( "decoder", 100 )
111 set_callbacks( OpenDecoder, CloseDecoder )
114 set_description( N_("MPEG audio layer I/II/III packetizer") )
115 set_capability( "packetizer", 10 )
116 set_callbacks( OpenPacketizer, CloseDecoder )
119 /*****************************************************************************
120 * Open: probe the decoder and return score
121 *****************************************************************************/
122 static int Open( vlc_object_t *p_this )
124 decoder_t *p_dec = (decoder_t*)p_this;
125 decoder_sys_t *p_sys;
127 if( p_dec->fmt_in.i_codec != VLC_CODEC_MPGA )
132 /* Allocate the memory needed to store the decoder's structure */
133 if( ( p_dec->p_sys = p_sys =
134 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
138 p_sys->b_packetizer = false;
139 p_sys->i_state = STATE_NOSYNC;
140 date_Set( &p_sys->end_date, 0 );
141 block_BytestreamInit( &p_sys->bytestream );
142 p_sys->i_pts = VLC_TS_INVALID;
143 p_sys->b_discontinuity = false;
145 /* Set output properties */
146 p_dec->fmt_out.i_cat = AUDIO_ES;
147 p_dec->fmt_out.i_codec = VLC_CODEC_MPGA;
148 p_dec->fmt_out.audio.i_rate = 0; /* So end_date gets initialized */
151 p_dec->pf_decode_audio = DecodeBlock;
152 p_dec->pf_packetize = DecodeBlock;
154 /* Start with the minimum size for a free bitrate frame */
155 p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
160 static int OpenDecoder( vlc_object_t *p_this )
162 /* HACK: Don't use this codec if we don't have an mpga audio filter */
163 if( !module_exists( "mpgatofixed32" ) )
166 return Open( p_this );
169 static int OpenPacketizer( vlc_object_t *p_this )
171 decoder_t *p_dec = (decoder_t*)p_this;
173 int i_ret = Open( p_this );
175 if( i_ret == VLC_SUCCESS ) p_dec->p_sys->b_packetizer = true;
180 /****************************************************************************
181 * DecodeBlock: the whole thing
182 ****************************************************************************
183 * This function is called just after the thread is launched.
184 ****************************************************************************/
185 static block_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
187 decoder_sys_t *p_sys = p_dec->p_sys;
188 uint8_t p_header[MAD_BUFFER_GUARD];
191 block_t *p_out_buffer;
193 if( !pp_block || !*pp_block ) return NULL;
195 if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
197 if( (*pp_block)->i_flags&BLOCK_FLAG_CORRUPTED )
199 p_sys->i_state = STATE_NOSYNC;
200 block_BytestreamEmpty( &p_sys->bytestream );
202 date_Set( &p_sys->end_date, 0 );
203 block_Release( *pp_block );
204 p_sys->b_discontinuity = true;
208 if( !date_Get( &p_sys->end_date ) && (*pp_block)->i_pts <= VLC_TS_INVALID )
210 /* We've just started the stream, wait for the first PTS. */
211 msg_Dbg( p_dec, "waiting for PTS" );
212 block_Release( *pp_block );
216 block_BytestreamPush( &p_sys->bytestream, *pp_block );
220 switch( p_sys->i_state )
224 while( block_PeekBytes( &p_sys->bytestream, p_header, 2 )
227 /* Look for sync word - should be 0xffe */
228 if( p_header[0] == 0xff && (p_header[1] & 0xe0) == 0xe0 )
230 p_sys->i_state = STATE_SYNC;
233 block_SkipByte( &p_sys->bytestream );
235 if( p_sys->i_state != STATE_SYNC )
237 block_BytestreamFlush( &p_sys->bytestream );
244 /* New frame, set the Presentation Time Stamp */
245 p_sys->i_pts = p_sys->bytestream.p_block->i_pts;
246 if( p_sys->i_pts > VLC_TS_INVALID &&
247 p_sys->i_pts != date_Get( &p_sys->end_date ) )
249 date_Set( &p_sys->end_date, p_sys->i_pts );
251 p_sys->i_state = STATE_HEADER;
254 /* Get MPGA frame header (MPGA_HEADER_SIZE bytes) */
255 if( block_PeekBytes( &p_sys->bytestream, p_header,
256 MPGA_HEADER_SIZE ) != VLC_SUCCESS )
262 /* Build frame header */
263 i_header = (p_header[0]<<24)|(p_header[1]<<16)|(p_header[2]<<8)
266 /* Check if frame is valid and get frame info */
267 p_sys->i_frame_size = SyncInfo( i_header,
269 &p_sys->i_channels_conf,
272 &p_sys->i_frame_length,
273 &p_sys->i_max_frame_size,
276 p_dec->fmt_in.i_profile = p_sys->i_layer;
278 if( p_sys->i_frame_size == -1 )
280 msg_Dbg( p_dec, "emulated startcode" );
281 block_SkipByte( &p_sys->bytestream );
282 p_sys->i_state = STATE_NOSYNC;
283 p_sys->b_discontinuity = true;
287 if( p_sys->i_bit_rate == 0 )
289 /* Free bitrate, but 99% emulated startcode :( */
290 if( p_dec->p_sys->i_free_frame_size == MPGA_HEADER_SIZE )
292 msg_Dbg( p_dec, "free bitrate mode");
294 /* The -1 below is to account for the frame padding */
295 p_sys->i_frame_size = p_sys->i_free_frame_size - 1;
298 p_sys->i_state = STATE_NEXT_SYNC;
300 case STATE_NEXT_SYNC:
301 /* TODO: If p_block == NULL, flush the buffer without checking the
304 /* Check if next expected frame contains the sync word */
305 if( block_PeekOffsetBytes( &p_sys->bytestream,
306 p_sys->i_frame_size, p_header,
307 MAD_BUFFER_GUARD ) != VLC_SUCCESS )
313 if( p_header[0] == 0xff && (p_header[1] & 0xe0) == 0xe0 )
315 /* Startcode is fine, let's try the header as an extra check */
316 int i_next_frame_size;
317 unsigned int i_next_channels, i_next_channels_conf;
318 unsigned int i_next_rate, i_next_bit_rate;
319 unsigned int i_next_frame_length, i_next_max_frame_size;
320 unsigned int i_next_layer;
322 /* Build frame header */
323 i_header = (p_header[0]<<24)|(p_header[1]<<16)|(p_header[2]<<8)
326 i_next_frame_size = SyncInfo( i_header,
328 &i_next_channels_conf,
331 &i_next_frame_length,
332 &i_next_max_frame_size,
335 /* Free bitrate only */
336 if( p_sys->i_bit_rate == 0 && i_next_frame_size == -1 )
338 if( (unsigned int)p_sys->i_frame_size >
339 p_sys->i_max_frame_size )
341 msg_Dbg( p_dec, "frame too big %d > %d "
342 "(emulated startcode ?)", p_sys->i_frame_size,
343 p_sys->i_max_frame_size );
344 block_SkipByte( &p_sys->bytestream );
345 p_sys->i_state = STATE_NOSYNC;
346 p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
350 p_sys->i_frame_size++;
354 if( i_next_frame_size == -1 )
356 msg_Dbg( p_dec, "emulated startcode on next frame" );
357 block_SkipByte( &p_sys->bytestream );
358 p_sys->i_state = STATE_NOSYNC;
359 p_sys->b_discontinuity = true;
363 /* Check info is in sync with previous one */
364 if( i_next_channels_conf != p_sys->i_channels_conf ||
365 i_next_rate != p_sys->i_rate ||
366 i_next_layer != p_sys->i_layer ||
367 i_next_frame_length != p_sys->i_frame_length )
369 /* Free bitrate only */
370 if( p_sys->i_bit_rate == 0 )
372 p_sys->i_frame_size++;
376 msg_Dbg( p_dec, "parameters changed unexpectedly "
377 "(emulated startcode ?)" );
378 block_SkipByte( &p_sys->bytestream );
379 p_sys->i_state = STATE_NOSYNC;
383 /* Free bitrate only */
384 if( p_sys->i_bit_rate == 0 )
386 if( i_next_bit_rate != 0 )
388 p_sys->i_frame_size++;
396 /* Free bitrate only */
397 if( p_sys->i_bit_rate == 0 )
399 if( (unsigned int)p_sys->i_frame_size >
400 p_sys->i_max_frame_size )
402 msg_Dbg( p_dec, "frame too big %d > %d "
403 "(emulated startcode ?)", p_sys->i_frame_size,
404 p_sys->i_max_frame_size );
405 block_SkipByte( &p_sys->bytestream );
406 p_sys->i_state = STATE_NOSYNC;
407 p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
411 p_sys->i_frame_size++;
415 msg_Dbg( p_dec, "emulated startcode "
416 "(no startcode on following frame)" );
417 p_sys->i_state = STATE_NOSYNC;
418 block_SkipByte( &p_sys->bytestream );
422 p_sys->i_state = STATE_SEND_DATA;
426 /* Make sure we have enough data.
427 * (Not useful if we went through NEXT_SYNC) */
428 if( block_WaitBytes( &p_sys->bytestream,
429 p_sys->i_frame_size ) != VLC_SUCCESS )
434 p_sys->i_state = STATE_SEND_DATA;
436 case STATE_SEND_DATA:
437 if( !(p_buf = GetOutBuffer( p_dec, &p_out_buffer )) )
439 //p_dec->b_error = true;
443 /* Free bitrate only */
444 if( p_sys->i_bit_rate == 0 )
446 p_sys->i_free_frame_size = p_sys->i_frame_size;
449 /* Copy the whole frame into the buffer. When we reach this point
450 * we already know we have enough data available. */
451 block_GetBytes( &p_sys->bytestream,
452 p_buf, __MIN( (unsigned)p_sys->i_frame_size, p_out_buffer->i_buffer ) );
454 /* Get beginning of next frame for libmad */
455 if( !p_sys->b_packetizer )
457 assert( p_out_buffer->i_buffer >= (unsigned)p_sys->i_frame_size + MAD_BUFFER_GUARD );
458 memcpy( p_buf + p_sys->i_frame_size,
459 p_header, MAD_BUFFER_GUARD );
462 p_sys->i_state = STATE_NOSYNC;
464 /* Make sure we don't reuse the same pts twice */
465 if( p_sys->i_pts == p_sys->bytestream.p_block->i_pts )
466 p_sys->i_pts = p_sys->bytestream.p_block->i_pts = VLC_TS_INVALID;
468 /* So p_block doesn't get re-added several times */
469 *pp_block = block_BytestreamPop( &p_sys->bytestream );
478 /*****************************************************************************
480 *****************************************************************************/
481 static uint8_t *GetOutBuffer( decoder_t *p_dec, block_t **pp_out_buffer )
483 decoder_sys_t *p_sys = p_dec->p_sys;
486 if( p_dec->fmt_out.audio.i_rate != p_sys->i_rate )
488 msg_Dbg( p_dec, "MPGA channels:%d samplerate:%d bitrate:%d",
489 p_sys->i_channels, p_sys->i_rate, p_sys->i_bit_rate );
491 date_Init( &p_sys->end_date, p_sys->i_rate, 1 );
492 date_Set( &p_sys->end_date, p_sys->i_pts );
495 p_dec->fmt_out.audio.i_rate = p_sys->i_rate;
496 p_dec->fmt_out.audio.i_channels = p_sys->i_channels;
497 p_dec->fmt_out.audio.i_frame_length = p_sys->i_frame_length;
498 p_dec->fmt_out.audio.i_bytes_per_frame =
499 p_sys->i_max_frame_size + MAD_BUFFER_GUARD;
501 p_dec->fmt_out.audio.i_original_channels = p_sys->i_channels_conf;
502 p_dec->fmt_out.audio.i_physical_channels =
503 p_sys->i_channels_conf & AOUT_CHAN_PHYSMASK;
505 p_dec->fmt_out.i_bitrate = p_sys->i_bit_rate * 1000;
507 if( p_sys->b_packetizer )
509 block_t *p_sout_buffer = GetSoutBuffer( p_dec );
510 p_buf = p_sout_buffer ? p_sout_buffer->p_buffer : NULL;
511 *pp_out_buffer = p_sout_buffer;
515 block_t *p_aout_buffer = GetAoutBuffer( p_dec );
516 p_buf = p_aout_buffer ? p_aout_buffer->p_buffer : NULL;
517 *pp_out_buffer = p_aout_buffer;
523 /*****************************************************************************
525 *****************************************************************************/
526 static block_t *GetAoutBuffer( decoder_t *p_dec )
528 decoder_sys_t *p_sys = p_dec->p_sys;
531 p_buf = decoder_NewAudioBuffer( p_dec, p_sys->i_frame_length );
532 if( p_buf == NULL ) return NULL;
534 p_buf->i_pts = date_Get( &p_sys->end_date );
535 p_buf->i_length = date_Increment( &p_sys->end_date, p_sys->i_frame_length )
537 if( p_sys->b_discontinuity )
538 p_buf->i_flags |= BLOCK_FLAG_DISCONTINUITY;
539 p_sys->b_discontinuity = false;
541 /* Hack for libmad filter */
542 p_buf = block_Realloc( p_buf, 0, p_sys->i_frame_size + MAD_BUFFER_GUARD );
547 /*****************************************************************************
549 *****************************************************************************/
550 static block_t *GetSoutBuffer( decoder_t *p_dec )
552 decoder_sys_t *p_sys = p_dec->p_sys;
555 p_block = block_Alloc( p_sys->i_frame_size );
556 if( p_block == NULL ) return NULL;
558 p_block->i_pts = p_block->i_dts = date_Get( &p_sys->end_date );
561 date_Increment( &p_sys->end_date, p_sys->i_frame_length ) - p_block->i_pts;
566 /*****************************************************************************
567 * CloseDecoder: clean up the decoder
568 *****************************************************************************/
569 static void CloseDecoder( vlc_object_t *p_this )
571 decoder_t *p_dec = (decoder_t *)p_this;
572 decoder_sys_t *p_sys = p_dec->p_sys;
574 block_BytestreamRelease( &p_sys->bytestream );
579 /*****************************************************************************
580 * SyncInfo: parse MPEG audio sync info
581 *****************************************************************************/
582 static int SyncInfo( uint32_t i_header, unsigned int * pi_channels,
583 unsigned int * pi_channels_conf,
584 unsigned int * pi_sample_rate, unsigned int * pi_bit_rate,
585 unsigned int * pi_frame_length,
586 unsigned int * pi_max_frame_size, unsigned int * pi_layer)
588 static const int ppi_bitrate[2][3][16] =
592 { 0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384,
595 { 0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256,
598 { 0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224,
604 { 0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192,
607 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128,
610 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128,
615 static const int ppi_samplerate[2][4] = /* version 1 then 2 */
617 { 44100, 48000, 32000, 0 },
618 { 22050, 24000, 16000, 0 }
621 int i_version, i_mode, i_emphasis;
622 bool b_padding, b_mpeg_2_5;
623 int i_frame_size = 0;
624 int i_bitrate_index, i_samplerate_index;
627 b_mpeg_2_5 = 1 - ((i_header & 0x100000) >> 20);
628 i_version = 1 - ((i_header & 0x80000) >> 19);
629 *pi_layer = 4 - ((i_header & 0x60000) >> 17);
630 //bool b_crc = !((i_header >> 16) & 0x01);
631 i_bitrate_index = (i_header & 0xf000) >> 12;
632 i_samplerate_index = (i_header & 0xc00) >> 10;
633 b_padding = (i_header & 0x200) >> 9;
635 i_mode = (i_header & 0xc0) >> 6;
636 /* Modeext, copyright & original */
637 i_emphasis = i_header & 0x3;
639 if( *pi_layer != 4 &&
640 i_bitrate_index < 0x0f &&
641 i_samplerate_index != 0x03 &&
647 case 1: /* joint stereo */
649 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
651 case 2: /* dual-mono */
653 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
654 | AOUT_CHAN_DUALMONO;
658 *pi_channels_conf = AOUT_CHAN_CENTER;
661 *pi_bit_rate = ppi_bitrate[i_version][*pi_layer-1][i_bitrate_index];
662 i_max_bit_rate = ppi_bitrate[i_version][*pi_layer-1][14];
663 *pi_sample_rate = ppi_samplerate[i_version][i_samplerate_index];
667 *pi_sample_rate >>= 1;
673 i_frame_size = ( 12000 * *pi_bit_rate / *pi_sample_rate +
675 *pi_max_frame_size = ( 12000 * i_max_bit_rate /
676 *pi_sample_rate + 1 ) * 4;
677 *pi_frame_length = 384;
681 i_frame_size = 144000 * *pi_bit_rate / *pi_sample_rate + b_padding;
682 *pi_max_frame_size = 144000 * i_max_bit_rate / *pi_sample_rate + 1;
683 *pi_frame_length = 1152;
687 i_frame_size = ( i_version ? 72000 : 144000 ) *
688 *pi_bit_rate / *pi_sample_rate + b_padding;
689 *pi_max_frame_size = ( i_version ? 72000 : 144000 ) *
690 i_max_bit_rate / *pi_sample_rate + 1;
691 *pi_frame_length = i_version ? 576 : 1152;
698 /* Free bitrate mode can support higher bitrates */
699 if( !*pi_bit_rate ) *pi_max_frame_size *= 2;