decoder: remove unused variable
[vlc.git] / modules / packetizer / mpegaudio.c
blob1398da0813201d84694818d27e5016799d3edaec
1 /*****************************************************************************
2 * mpegaudio.c: parse MPEG audio sync info and packetize the stream
3 *****************************************************************************
4 * Copyright (C) 2001-2016 VLC authors and VideoLAN
6 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7 * Eric Petit <titer@videolan.org>
8 * Christophe Massiot <massiot@via.ecp.fr>
9 * Gildas Bazin <gbazin@videolan.org>
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation; either version 2.1 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this program; if not, write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 /*****************************************************************************
27 * Preamble
28 *****************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_codec.h>
36 #include <vlc_aout.h>
37 #include <vlc_modules.h>
38 #include <assert.h>
40 #include <vlc_block_helper.h>
42 #include "packetizer_helper.h"
44 /*****************************************************************************
45 * decoder_sys_t : decoder descriptor
46 *****************************************************************************/
47 typedef struct
50 * Input properties
52 int i_state;
54 block_bytestream_t bytestream;
57 * Common properties
59 date_t end_date;
61 vlc_tick_t i_pts;
63 int i_frame_size, i_free_frame_size;
64 unsigned int i_channels_conf, i_chan_mode, i_channels;
65 unsigned int i_rate, i_max_frame_size, i_frame_length;
66 unsigned int i_layer, i_bit_rate;
68 bool b_discontinuity;
69 } decoder_sys_t;
71 #define MAD_BUFFER_GUARD 8
72 #define MPGA_HEADER_SIZE 4
74 /****************************************************************************
75 * Local prototypes
76 ****************************************************************************/
78 static int Open( vlc_object_t * );
79 static void Close( vlc_object_t * );
81 /*****************************************************************************
82 * Module descriptor
83 *****************************************************************************/
84 vlc_module_begin ()
85 set_category( CAT_INPUT )
86 set_subcategory( SUBCAT_INPUT_ACODEC )
87 set_description( N_("MPEG audio layer I/II/III packetizer") )
88 set_capability( "packetizer", 10 )
89 set_callbacks( Open, Close )
90 vlc_module_end ()
92 /*****************************************************************************
93 * Flush:
94 *****************************************************************************/
95 static void Flush( decoder_t *p_dec )
97 decoder_sys_t *p_sys = p_dec->p_sys;
99 date_Set( &p_sys->end_date, VLC_TICK_INVALID );
100 p_sys->i_state = STATE_NOSYNC;
101 block_BytestreamEmpty( &p_sys->bytestream );
102 p_sys->b_discontinuity = true;
105 /*****************************************************************************
106 * GetOutBuffer:
107 *****************************************************************************/
108 static uint8_t *GetOutBuffer( decoder_t *p_dec, block_t **pp_out_buffer )
110 decoder_sys_t *p_sys = p_dec->p_sys;
112 if( p_dec->fmt_out.audio.i_rate != p_sys->i_rate ||
113 date_Get( &p_sys->end_date ) == VLC_TICK_INVALID )
115 msg_Dbg( p_dec, "MPGA channels:%d samplerate:%d bitrate:%d",
116 p_sys->i_channels, p_sys->i_rate, p_sys->i_bit_rate );
118 if( p_sys->end_date.i_divider_num == 0 )
119 date_Init( &p_sys->end_date, p_sys->i_rate, 1 );
120 else
121 date_Change( &p_sys->end_date, p_sys->i_rate, 1 );
122 date_Set( &p_sys->end_date, p_sys->i_pts );
125 p_dec->fmt_out.i_profile = p_sys->i_layer;
126 p_dec->fmt_out.audio.i_rate = p_sys->i_rate;
127 p_dec->fmt_out.audio.i_channels = p_sys->i_channels;
128 p_dec->fmt_out.audio.i_frame_length = p_sys->i_frame_length;
129 p_dec->fmt_out.audio.i_bytes_per_frame = p_sys->i_max_frame_size;
131 p_dec->fmt_out.audio.i_physical_channels = p_sys->i_channels_conf;
132 p_dec->fmt_out.audio.i_chan_mode = p_sys->i_chan_mode;
134 p_dec->fmt_out.i_bitrate = p_sys->i_bit_rate * 1000;
136 block_t *p_block = block_Alloc( p_sys->i_frame_size );
137 if( p_block == NULL )
138 return NULL;
140 p_block->i_pts = p_block->i_dts = date_Get( &p_sys->end_date );
141 p_block->i_length =
142 date_Increment( &p_sys->end_date, p_sys->i_frame_length ) - p_block->i_pts;
144 *pp_out_buffer = p_block;
145 return p_block->p_buffer;
148 /*****************************************************************************
149 * SyncInfo: parse MPEG audio sync info
150 *****************************************************************************/
151 static int SyncInfo( uint32_t i_header, unsigned int * pi_channels,
152 unsigned int * pi_channels_conf,
153 unsigned int * pi_chan_mode,
154 unsigned int * pi_sample_rate, unsigned int * pi_bit_rate,
155 unsigned int * pi_frame_length,
156 unsigned int * pi_max_frame_size, unsigned int * pi_layer)
158 static const int ppi_bitrate[2][3][16] =
161 /* v1 l1 */
162 { 0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384,
163 416, 448, 0},
164 /* v1 l2 */
165 { 0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256,
166 320, 384, 0},
167 /* v1 l3 */
168 { 0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224,
169 256, 320, 0}
173 /* v2 l1 */
174 { 0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192,
175 224, 256, 0},
176 /* v2 l2 */
177 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128,
178 144, 160, 0},
179 /* v2 l3 */
180 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128,
181 144, 160, 0}
185 static const int ppi_samplerate[2][4] = /* version 1 then 2 */
187 { 44100, 48000, 32000, 0 },
188 { 22050, 24000, 16000, 0 }
191 int i_version, i_mode, i_emphasis;
192 bool b_padding, b_mpeg_2_5;
193 int i_frame_size = 0;
194 int i_bitrate_index, i_samplerate_index;
195 int i_max_bit_rate;
197 b_mpeg_2_5 = 1 - ((i_header & 0x100000) >> 20);
198 i_version = 1 - ((i_header & 0x80000) >> 19);
199 *pi_layer = 4 - ((i_header & 0x60000) >> 17);
200 //bool b_crc = !((i_header >> 16) & 0x01);
201 i_bitrate_index = (i_header & 0xf000) >> 12;
202 i_samplerate_index = (i_header & 0xc00) >> 10;
203 b_padding = (i_header & 0x200) >> 9;
204 /* Extension */
205 i_mode = (i_header & 0xc0) >> 6;
206 /* Modeext, copyright & original */
207 i_emphasis = i_header & 0x3;
208 *pi_chan_mode = 0;
210 if( *pi_layer != 4 &&
211 i_bitrate_index < 0x0f &&
212 i_samplerate_index != 0x03 &&
213 i_emphasis != 0x02 )
215 switch ( i_mode )
217 case 2: /* dual-mono */
218 *pi_chan_mode = AOUT_CHANMODE_DUALMONO;
219 /* fall through */
220 case 0: /* stereo */
221 case 1: /* joint stereo */
222 *pi_channels = 2;
223 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
224 break;
225 case 3: /* mono */
226 *pi_channels = 1;
227 *pi_channels_conf = AOUT_CHAN_CENTER;
228 break;
230 *pi_bit_rate = ppi_bitrate[i_version][*pi_layer-1][i_bitrate_index];
231 i_max_bit_rate = ppi_bitrate[i_version][*pi_layer-1][14];
232 *pi_sample_rate = ppi_samplerate[i_version][i_samplerate_index];
234 if ( b_mpeg_2_5 )
236 *pi_sample_rate >>= 1;
239 switch( *pi_layer )
241 case 1:
242 i_frame_size = ( 12000 * *pi_bit_rate / *pi_sample_rate +
243 b_padding ) * 4;
244 *pi_max_frame_size = ( 12000 * i_max_bit_rate /
245 *pi_sample_rate + 1 ) * 4;
246 *pi_frame_length = 384;
247 break;
249 case 2:
250 i_frame_size = 144000 * *pi_bit_rate / *pi_sample_rate + b_padding;
251 *pi_max_frame_size = 144000 * i_max_bit_rate / *pi_sample_rate + 1;
252 *pi_frame_length = 1152;
253 break;
255 case 3:
256 i_frame_size = ( i_version ? 72000 : 144000 ) *
257 *pi_bit_rate / *pi_sample_rate + b_padding;
258 *pi_max_frame_size = ( i_version ? 72000 : 144000 ) *
259 i_max_bit_rate / *pi_sample_rate + 1;
260 *pi_frame_length = i_version ? 576 : 1152;
261 break;
263 default:
264 break;
267 /* Free bitrate mode can support higher bitrates */
268 if( !*pi_bit_rate ) *pi_max_frame_size *= 2;
270 else
272 return -1;
275 return i_frame_size;
278 /****************************************************************************
279 * DecodeBlock: the whole thing
280 ****************************************************************************
281 * This function is called just after the thread is launched.
282 ****************************************************************************/
283 static block_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
285 decoder_sys_t *p_sys = p_dec->p_sys;
286 uint8_t p_header[MAD_BUFFER_GUARD];
287 uint32_t i_header;
288 uint8_t *p_buf;
289 block_t *p_out_buffer;
291 block_t *p_block = pp_block ? *pp_block : NULL;
293 if (p_block)
295 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
297 /* First always drain complete blocks before discontinuity */
298 block_t *p_drain = DecodeBlock( p_dec, NULL );
299 if( p_drain )
300 return p_drain;
302 Flush( p_dec );
304 if( p_block->i_flags & BLOCK_FLAG_CORRUPTED )
306 block_Release( p_block );
307 return NULL;
311 if( p_block->i_pts == VLC_TICK_INVALID &&
312 date_Get( &p_sys->end_date ) == VLC_TICK_INVALID )
314 /* We've just started the stream, wait for the first PTS. */
315 msg_Dbg( p_dec, "waiting for PTS" );
316 block_Release( p_block );
317 return NULL;
320 block_BytestreamPush( &p_sys->bytestream, p_block );
323 while( 1 )
325 switch( p_sys->i_state )
328 case STATE_NOSYNC:
329 while( block_PeekBytes( &p_sys->bytestream, p_header, 2 )
330 == VLC_SUCCESS )
332 /* Look for sync word - should be 0xffe */
333 if( p_header[0] == 0xff && (p_header[1] & 0xe0) == 0xe0 )
335 p_sys->i_state = STATE_SYNC;
336 break;
338 block_SkipByte( &p_sys->bytestream );
340 if( p_sys->i_state != STATE_SYNC )
342 block_BytestreamFlush( &p_sys->bytestream );
344 /* Need more data */
345 return NULL;
347 /* fallthrough */
349 case STATE_SYNC:
350 /* New frame, set the Presentation Time Stamp */
351 p_sys->i_pts = p_sys->bytestream.p_block->i_pts;
352 if( p_sys->i_pts != VLC_TICK_INVALID &&
353 p_sys->i_pts != date_Get( &p_sys->end_date ) )
355 if( p_dec->fmt_in.i_original_fourcc == VLC_FOURCC( 'D','V','R',' ') )
357 if( date_Get( &p_sys->end_date ) == VLC_TICK_INVALID )
358 date_Set( &p_sys->end_date, p_sys->i_pts );
360 else if ( p_sys->i_pts != date_Get( &p_sys->end_date ) )
362 date_Set( &p_sys->end_date, p_sys->i_pts );
365 p_sys->i_state = STATE_HEADER;
366 /* fallthrough */
368 case STATE_HEADER:
369 /* Get MPGA frame header (MPGA_HEADER_SIZE bytes) */
370 if( block_PeekBytes( &p_sys->bytestream, p_header,
371 MPGA_HEADER_SIZE ) != VLC_SUCCESS )
373 /* Need more data */
374 return NULL;
377 /* Build frame header */
378 i_header = GetDWBE(p_header);
380 /* Check if frame is valid and get frame info */
381 p_sys->i_frame_size = SyncInfo( i_header,
382 &p_sys->i_channels,
383 &p_sys->i_channels_conf,
384 &p_sys->i_chan_mode,
385 &p_sys->i_rate,
386 &p_sys->i_bit_rate,
387 &p_sys->i_frame_length,
388 &p_sys->i_max_frame_size,
389 &p_sys->i_layer );
391 if( p_sys->i_frame_size == -1 )
393 msg_Dbg( p_dec, "emulated startcode" );
394 block_SkipByte( &p_sys->bytestream );
395 p_sys->i_state = STATE_NOSYNC;
396 break;
399 if( p_sys->i_bit_rate == 0 )
401 /* Free bitrate, but 99% emulated startcode :( */
402 if( p_sys->i_free_frame_size == MPGA_HEADER_SIZE )
404 msg_Dbg( p_dec, "free bitrate mode");
406 /* The -1 below is to account for the frame padding */
407 p_sys->i_frame_size = p_sys->i_free_frame_size - 1;
410 p_sys->i_state = STATE_NEXT_SYNC;
411 /* fallthrough */
413 case STATE_NEXT_SYNC:
414 /* Check if next expected frame contains the sync word */
415 if( block_PeekOffsetBytes( &p_sys->bytestream,
416 p_sys->i_frame_size, p_header,
417 MAD_BUFFER_GUARD ) != VLC_SUCCESS )
419 if( p_block == NULL ) /* drain */
421 p_sys->i_state = STATE_SEND_DATA;
422 break;
424 /* Need more data */
425 return NULL;
428 if( p_header[0] == 0xff && (p_header[1] & 0xe0) == 0xe0 )
430 /* Startcode is fine, let's try the header as an extra check */
431 int i_next_frame_size;
432 unsigned int i_next_channels, i_next_stereo_mode, i_next_channels_conf;
433 unsigned int i_next_rate, i_next_bit_rate;
434 unsigned int i_next_frame_length, i_next_max_frame_size;
435 unsigned int i_next_layer;
437 /* Build frame header */
438 i_header = GetDWBE(p_header);
440 i_next_frame_size = SyncInfo( i_header,
441 &i_next_channels,
442 &i_next_channels_conf,
443 &i_next_stereo_mode,
444 &i_next_rate,
445 &i_next_bit_rate,
446 &i_next_frame_length,
447 &i_next_max_frame_size,
448 &i_next_layer );
450 /* Free bitrate only */
451 if( p_sys->i_bit_rate == 0 && i_next_frame_size == -1 )
453 if( (unsigned int)p_sys->i_frame_size >
454 p_sys->i_max_frame_size )
456 msg_Dbg( p_dec, "frame too big %d > %d "
457 "(emulated startcode ?)", p_sys->i_frame_size,
458 p_sys->i_max_frame_size );
459 block_SkipByte( &p_sys->bytestream );
460 p_sys->i_state = STATE_NOSYNC;
461 p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
462 break;
465 p_sys->i_frame_size++;
466 break;
469 if( i_next_frame_size == -1 )
471 msg_Dbg( p_dec, "emulated startcode on next frame" );
472 block_SkipByte( &p_sys->bytestream );
473 p_sys->i_state = STATE_NOSYNC;
474 break;
477 /* Check info is in sync with previous one */
478 if( i_next_channels_conf != p_sys->i_channels_conf ||
479 i_next_stereo_mode != p_sys->i_chan_mode ||
480 i_next_rate != p_sys->i_rate ||
481 i_next_layer != p_sys->i_layer ||
482 i_next_frame_length != p_sys->i_frame_length )
484 /* Free bitrate only */
485 if( p_sys->i_bit_rate == 0 )
487 p_sys->i_frame_size++;
488 break;
491 msg_Dbg( p_dec, "parameters changed unexpectedly "
492 "(emulated startcode ?)" );
493 block_SkipByte( &p_sys->bytestream );
494 p_sys->i_state = STATE_NOSYNC;
495 break;
498 /* Free bitrate only */
499 if( p_sys->i_bit_rate == 0 )
501 if( i_next_bit_rate != 0 )
503 p_sys->i_frame_size++;
504 break;
509 else
511 /* Free bitrate only */
512 if( p_sys->i_bit_rate == 0 )
514 if( (unsigned int)p_sys->i_frame_size >
515 p_sys->i_max_frame_size )
517 msg_Dbg( p_dec, "frame too big %d > %d "
518 "(emulated startcode ?)", p_sys->i_frame_size,
519 p_sys->i_max_frame_size );
520 block_SkipByte( &p_sys->bytestream );
521 p_sys->i_state = STATE_NOSYNC;
522 p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
523 break;
526 p_sys->i_frame_size++;
527 break;
530 msg_Dbg( p_dec, "emulated startcode "
531 "(no startcode on following frame)" );
532 p_sys->i_state = STATE_NOSYNC;
533 block_SkipByte( &p_sys->bytestream );
534 break;
537 p_sys->i_state = STATE_GET_DATA;
538 break;
540 case STATE_GET_DATA:
541 /* Make sure we have enough data.
542 * (Not useful if we went through NEXT_SYNC) */
543 if( block_WaitBytes( &p_sys->bytestream,
544 p_sys->i_frame_size ) != VLC_SUCCESS )
546 /* Need more data */
547 return NULL;
549 p_sys->i_state = STATE_SEND_DATA;
550 /* fallthrough */
552 case STATE_SEND_DATA:
553 if( !(p_buf = GetOutBuffer( p_dec, &p_out_buffer )) )
555 return NULL;
558 /* Free bitrate only */
559 if( p_sys->i_bit_rate == 0 )
561 p_sys->i_free_frame_size = p_sys->i_frame_size;
564 /* Copy the whole frame into the buffer. */
565 if (block_GetBytes( &p_sys->bytestream,
566 p_buf, __MIN( (unsigned)p_sys->i_frame_size, p_out_buffer->i_buffer ) )) {
567 block_Release(p_out_buffer);
568 return NULL;
571 p_sys->i_state = STATE_NOSYNC;
573 /* Make sure we don't reuse the same pts twice */
574 if( p_sys->i_pts == p_sys->bytestream.p_block->i_pts )
575 p_sys->i_pts = p_sys->bytestream.p_block->i_pts = VLC_TICK_INVALID;
577 if( p_sys->b_discontinuity )
579 p_out_buffer->i_flags |= BLOCK_FLAG_DISCONTINUITY;
580 p_sys->b_discontinuity = false;
583 /* So p_block doesn't get re-added several times */
584 p_block = block_BytestreamPop( &p_sys->bytestream );
585 if (pp_block)
586 *pp_block = p_block;
587 else if (p_block)
588 block_Release(p_block);
590 return p_out_buffer;
594 return NULL;
597 /*****************************************************************************
598 * Close: clean up the decoder
599 *****************************************************************************/
600 static void Close( vlc_object_t *p_this )
602 decoder_t *p_dec = (decoder_t *)p_this;
603 decoder_sys_t *p_sys = p_dec->p_sys;
605 block_BytestreamRelease( &p_sys->bytestream );
607 free( p_sys );
610 /*****************************************************************************
611 * Open: probe the decoder and return score
612 *****************************************************************************/
613 static int Open( vlc_object_t *p_this )
615 decoder_t *p_dec = (decoder_t*)p_this;
616 decoder_sys_t *p_sys;
618 if(( p_dec->fmt_in.i_codec != VLC_CODEC_MPGA ) &&
619 ( p_dec->fmt_in.i_codec != VLC_CODEC_MP3 ) )
621 return VLC_EGENERIC;
624 /* Allocate the memory needed to store the decoder's structure */
625 if( ( p_dec->p_sys = p_sys =
626 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
627 return VLC_ENOMEM;
629 /* Misc init */
630 p_sys->i_state = STATE_NOSYNC;
631 date_Init( &p_sys->end_date, 1, 1 );
632 block_BytestreamInit( &p_sys->bytestream );
633 p_sys->i_pts = VLC_TICK_INVALID;
634 p_sys->b_discontinuity = false;
635 p_sys->i_frame_size = 0;
637 p_sys->i_channels_conf = p_sys->i_chan_mode = p_sys->i_channels =
638 p_sys->i_rate = p_sys->i_max_frame_size = p_sys->i_frame_length =
639 p_sys->i_layer = p_sys->i_bit_rate = 0;
641 /* Set output properties */
642 p_dec->fmt_out.i_codec = VLC_CODEC_MPGA;
643 p_dec->fmt_out.audio.i_rate = 0; /* So end_date gets initialized */
645 /* Set callback */
646 p_dec->pf_packetize = DecodeBlock;
647 p_dec->pf_flush = Flush;
648 p_dec->pf_get_cc = NULL;
650 /* Start with the minimum size for a free bitrate frame */
651 p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
653 return VLC_SUCCESS;