demux: mkv: set original fourcc for LATM
[vlc.git] / modules / packetizer / mpegaudio.c
blob43043df6af92ccd574b01863b1ca81cb7f82cbfe
1 /*****************************************************************************
2 * mpegaudio.c: parse MPEG audio sync info and packetize the stream
3 *****************************************************************************
4 * Copyright (C) 2001-2016 VLC authors and VideoLAN
5 * $Id$
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 /*****************************************************************************
28 * Preamble
29 *****************************************************************************/
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_codec.h>
37 #include <vlc_aout.h>
38 #include <vlc_modules.h>
39 #include <assert.h>
41 #include <vlc_block_helper.h>
43 #include "packetizer_helper.h"
45 /*****************************************************************************
46 * decoder_sys_t : decoder descriptor
47 *****************************************************************************/
48 struct decoder_sys_t
51 * Input properties
53 int i_state;
55 block_bytestream_t bytestream;
58 * Common properties
60 date_t end_date;
62 mtime_t i_pts;
64 int i_frame_size, i_free_frame_size;
65 unsigned int i_channels_conf, i_channels;
66 unsigned int i_rate, i_max_frame_size, i_frame_length;
67 unsigned int i_layer, i_bit_rate;
69 bool b_discontinuity;
72 #define MAD_BUFFER_GUARD 8
73 #define MPGA_HEADER_SIZE 4
75 /****************************************************************************
76 * Local prototypes
77 ****************************************************************************/
79 static int Open( vlc_object_t * );
80 static void Close( vlc_object_t * );
82 /*****************************************************************************
83 * Module descriptor
84 *****************************************************************************/
85 vlc_module_begin ()
86 set_category( CAT_INPUT )
87 set_subcategory( SUBCAT_INPUT_ACODEC )
88 set_description( N_("MPEG audio layer I/II/III packetizer") )
89 set_capability( "packetizer", 10 )
90 set_callbacks( Open, Close )
91 vlc_module_end ()
93 /*****************************************************************************
94 * Flush:
95 *****************************************************************************/
96 static void Flush( decoder_t *p_dec )
98 decoder_sys_t *p_sys = p_dec->p_sys;
100 date_Set( &p_sys->end_date, VLC_TS_INVALID );
101 p_sys->i_state = STATE_NOSYNC;
102 block_BytestreamEmpty( &p_sys->bytestream );
103 p_sys->b_discontinuity = true;
106 /*****************************************************************************
107 * GetOutBuffer:
108 *****************************************************************************/
109 static uint8_t *GetOutBuffer( decoder_t *p_dec, block_t **pp_out_buffer )
111 decoder_sys_t *p_sys = p_dec->p_sys;
113 if( p_dec->fmt_out.audio.i_rate != p_sys->i_rate ||
114 date_Get( &p_sys->end_date ) == VLC_TS_INVALID )
116 msg_Dbg( p_dec, "MPGA channels:%d samplerate:%d bitrate:%d",
117 p_sys->i_channels, p_sys->i_rate, p_sys->i_bit_rate );
119 if( p_sys->end_date.i_divider_num == 0 )
120 date_Init( &p_sys->end_date, p_sys->i_rate, 1 );
121 else
122 date_Change( &p_sys->end_date, p_sys->i_rate, 1 );
123 date_Set( &p_sys->end_date, p_sys->i_pts );
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_original_channels = p_sys->i_channels_conf;
132 p_dec->fmt_out.audio.i_physical_channels =
133 p_sys->i_channels_conf & AOUT_CHAN_PHYSMASK;
135 p_dec->fmt_out.i_bitrate = p_sys->i_bit_rate * 1000;
137 block_t *p_block = block_Alloc( p_sys->i_frame_size );
138 if( p_block == NULL )
139 return NULL;
141 p_block->i_pts = p_block->i_dts = date_Get( &p_sys->end_date );
142 p_block->i_length =
143 date_Increment( &p_sys->end_date, p_sys->i_frame_length ) - p_block->i_pts;
145 *pp_out_buffer = p_block;
146 return p_block->p_buffer;
149 /*****************************************************************************
150 * SyncInfo: parse MPEG audio sync info
151 *****************************************************************************/
152 static int SyncInfo( uint32_t i_header, unsigned int * pi_channels,
153 unsigned int * pi_channels_conf,
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;
209 if( *pi_layer != 4 &&
210 i_bitrate_index < 0x0f &&
211 i_samplerate_index != 0x03 &&
212 i_emphasis != 0x02 )
214 switch ( i_mode )
216 case 0: /* stereo */
217 case 1: /* joint stereo */
218 *pi_channels = 2;
219 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
220 break;
221 case 2: /* dual-mono */
222 *pi_channels = 2;
223 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
224 | AOUT_CHAN_DUALMONO;
225 break;
226 case 3: /* mono */
227 *pi_channels = 1;
228 *pi_channels_conf = AOUT_CHAN_CENTER;
229 break;
231 *pi_bit_rate = ppi_bitrate[i_version][*pi_layer-1][i_bitrate_index];
232 i_max_bit_rate = ppi_bitrate[i_version][*pi_layer-1][14];
233 *pi_sample_rate = ppi_samplerate[i_version][i_samplerate_index];
235 if ( b_mpeg_2_5 )
237 *pi_sample_rate >>= 1;
240 switch( *pi_layer )
242 case 1:
243 i_frame_size = ( 12000 * *pi_bit_rate / *pi_sample_rate +
244 b_padding ) * 4;
245 *pi_max_frame_size = ( 12000 * i_max_bit_rate /
246 *pi_sample_rate + 1 ) * 4;
247 *pi_frame_length = 384;
248 break;
250 case 2:
251 i_frame_size = 144000 * *pi_bit_rate / *pi_sample_rate + b_padding;
252 *pi_max_frame_size = 144000 * i_max_bit_rate / *pi_sample_rate + 1;
253 *pi_frame_length = 1152;
254 break;
256 case 3:
257 i_frame_size = ( i_version ? 72000 : 144000 ) *
258 *pi_bit_rate / *pi_sample_rate + b_padding;
259 *pi_max_frame_size = ( i_version ? 72000 : 144000 ) *
260 i_max_bit_rate / *pi_sample_rate + 1;
261 *pi_frame_length = i_version ? 576 : 1152;
262 break;
264 default:
265 break;
268 /* Free bitrate mode can support higher bitrates */
269 if( !*pi_bit_rate ) *pi_max_frame_size *= 2;
271 else
273 return -1;
276 return i_frame_size;
279 /****************************************************************************
280 * DecodeBlock: the whole thing
281 ****************************************************************************
282 * This function is called just after the thread is launched.
283 ****************************************************************************/
284 static block_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
286 decoder_sys_t *p_sys = p_dec->p_sys;
287 uint8_t p_header[MAD_BUFFER_GUARD];
288 uint32_t i_header;
289 uint8_t *p_buf;
290 block_t *p_out_buffer;
292 block_t *p_block = pp_block ? *pp_block : NULL;
294 if (p_block)
296 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
298 /* First always drain complete blocks before discontinuity */
299 block_t *p_drain = DecodeBlock( p_dec, NULL );
300 if( p_drain )
301 return p_drain;
303 Flush( p_dec );
305 if( p_block->i_flags & BLOCK_FLAG_CORRUPTED )
307 block_Release( p_block );
308 return NULL;
312 if( !date_Get( &p_sys->end_date ) && p_block->i_pts <= VLC_TS_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;
348 case STATE_SYNC:
349 /* New frame, set the Presentation Time Stamp */
350 p_sys->i_pts = p_sys->bytestream.p_block->i_pts;
351 if( p_sys->i_pts > VLC_TS_INVALID &&
352 p_sys->i_pts != date_Get( &p_sys->end_date ) )
354 if( p_dec->fmt_in.i_original_fourcc == VLC_FOURCC( 'D','V','R',' ') )
356 if( date_Get( &p_sys->end_date ) == VLC_TS_INVALID )
357 date_Set( &p_sys->end_date, p_sys->i_pts );
359 else if ( p_sys->i_pts != date_Get( &p_sys->end_date ) )
361 date_Set( &p_sys->end_date, p_sys->i_pts );
364 p_sys->i_state = STATE_HEADER;
366 case STATE_HEADER:
367 /* Get MPGA frame header (MPGA_HEADER_SIZE bytes) */
368 if( block_PeekBytes( &p_sys->bytestream, p_header,
369 MPGA_HEADER_SIZE ) != VLC_SUCCESS )
371 /* Need more data */
372 return NULL;
375 /* Build frame header */
376 i_header = GetDWBE(p_header);
378 /* Check if frame is valid and get frame info */
379 p_sys->i_frame_size = SyncInfo( i_header,
380 &p_sys->i_channels,
381 &p_sys->i_channels_conf,
382 &p_sys->i_rate,
383 &p_sys->i_bit_rate,
384 &p_sys->i_frame_length,
385 &p_sys->i_max_frame_size,
386 &p_sys->i_layer );
388 p_dec->fmt_in.i_profile = p_sys->i_layer;
390 if( p_sys->i_frame_size == -1 )
392 msg_Dbg( p_dec, "emulated startcode" );
393 block_SkipByte( &p_sys->bytestream );
394 p_sys->i_state = STATE_NOSYNC;
395 break;
398 if( p_sys->i_bit_rate == 0 )
400 /* Free bitrate, but 99% emulated startcode :( */
401 if( p_dec->p_sys->i_free_frame_size == MPGA_HEADER_SIZE )
403 msg_Dbg( p_dec, "free bitrate mode");
405 /* The -1 below is to account for the frame padding */
406 p_sys->i_frame_size = p_sys->i_free_frame_size - 1;
409 p_sys->i_state = STATE_NEXT_SYNC;
411 case STATE_NEXT_SYNC:
412 /* Check if next expected frame contains the sync word */
413 if( block_PeekOffsetBytes( &p_sys->bytestream,
414 p_sys->i_frame_size, p_header,
415 MAD_BUFFER_GUARD ) != VLC_SUCCESS )
417 if( p_block == NULL ) /* drain */
419 p_sys->i_state = STATE_SEND_DATA;
420 break;
422 /* Need more data */
423 return NULL;
426 if( p_header[0] == 0xff && (p_header[1] & 0xe0) == 0xe0 )
428 /* Startcode is fine, let's try the header as an extra check */
429 int i_next_frame_size;
430 unsigned int i_next_channels, i_next_channels_conf;
431 unsigned int i_next_rate, i_next_bit_rate;
432 unsigned int i_next_frame_length, i_next_max_frame_size;
433 unsigned int i_next_layer;
435 /* Build frame header */
436 i_header = GetDWBE(p_header);
438 i_next_frame_size = SyncInfo( i_header,
439 &i_next_channels,
440 &i_next_channels_conf,
441 &i_next_rate,
442 &i_next_bit_rate,
443 &i_next_frame_length,
444 &i_next_max_frame_size,
445 &i_next_layer );
447 /* Free bitrate only */
448 if( p_sys->i_bit_rate == 0 && i_next_frame_size == -1 )
450 if( (unsigned int)p_sys->i_frame_size >
451 p_sys->i_max_frame_size )
453 msg_Dbg( p_dec, "frame too big %d > %d "
454 "(emulated startcode ?)", p_sys->i_frame_size,
455 p_sys->i_max_frame_size );
456 block_SkipByte( &p_sys->bytestream );
457 p_sys->i_state = STATE_NOSYNC;
458 p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
459 break;
462 p_sys->i_frame_size++;
463 break;
466 if( i_next_frame_size == -1 )
468 msg_Dbg( p_dec, "emulated startcode on next frame" );
469 block_SkipByte( &p_sys->bytestream );
470 p_sys->i_state = STATE_NOSYNC;
471 break;
474 /* Check info is in sync with previous one */
475 if( i_next_channels_conf != p_sys->i_channels_conf ||
476 i_next_rate != p_sys->i_rate ||
477 i_next_layer != p_sys->i_layer ||
478 i_next_frame_length != p_sys->i_frame_length )
480 /* Free bitrate only */
481 if( p_sys->i_bit_rate == 0 )
483 p_sys->i_frame_size++;
484 break;
487 msg_Dbg( p_dec, "parameters changed unexpectedly "
488 "(emulated startcode ?)" );
489 block_SkipByte( &p_sys->bytestream );
490 p_sys->i_state = STATE_NOSYNC;
491 break;
494 /* Free bitrate only */
495 if( p_sys->i_bit_rate == 0 )
497 if( i_next_bit_rate != 0 )
499 p_sys->i_frame_size++;
500 break;
505 else
507 /* Free bitrate only */
508 if( p_sys->i_bit_rate == 0 )
510 if( (unsigned int)p_sys->i_frame_size >
511 p_sys->i_max_frame_size )
513 msg_Dbg( p_dec, "frame too big %d > %d "
514 "(emulated startcode ?)", p_sys->i_frame_size,
515 p_sys->i_max_frame_size );
516 block_SkipByte( &p_sys->bytestream );
517 p_sys->i_state = STATE_NOSYNC;
518 p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
519 break;
522 p_sys->i_frame_size++;
523 break;
526 msg_Dbg( p_dec, "emulated startcode "
527 "(no startcode on following frame)" );
528 p_sys->i_state = STATE_NOSYNC;
529 block_SkipByte( &p_sys->bytestream );
530 break;
533 p_sys->i_state = STATE_GET_DATA;
534 break;
536 case STATE_GET_DATA:
537 /* Make sure we have enough data.
538 * (Not useful if we went through NEXT_SYNC) */
539 if( block_WaitBytes( &p_sys->bytestream,
540 p_sys->i_frame_size ) != VLC_SUCCESS )
542 /* Need more data */
543 return NULL;
545 p_sys->i_state = STATE_SEND_DATA;
547 case STATE_SEND_DATA:
548 if( !(p_buf = GetOutBuffer( p_dec, &p_out_buffer )) )
550 return NULL;
553 /* Free bitrate only */
554 if( p_sys->i_bit_rate == 0 )
556 p_sys->i_free_frame_size = p_sys->i_frame_size;
559 /* Copy the whole frame into the buffer. */
560 if (block_GetBytes( &p_sys->bytestream,
561 p_buf, __MIN( (unsigned)p_sys->i_frame_size, p_out_buffer->i_buffer ) )) {
562 block_Release(p_out_buffer);
563 return NULL;
566 p_sys->i_state = STATE_NOSYNC;
568 /* Make sure we don't reuse the same pts twice */
569 if( p_sys->i_pts == p_sys->bytestream.p_block->i_pts )
570 p_sys->i_pts = p_sys->bytestream.p_block->i_pts = VLC_TS_INVALID;
572 if( p_sys->b_discontinuity )
574 p_out_buffer->i_flags |= BLOCK_FLAG_DISCONTINUITY;
575 p_sys->b_discontinuity = false;
578 /* So p_block doesn't get re-added several times */
579 p_block = block_BytestreamPop( &p_sys->bytestream );
580 if (pp_block)
581 *pp_block = p_block;
582 else if (p_block)
583 block_Release(p_block);
585 return p_out_buffer;
589 return NULL;
592 /*****************************************************************************
593 * Close: clean up the decoder
594 *****************************************************************************/
595 static void Close( vlc_object_t *p_this )
597 decoder_t *p_dec = (decoder_t *)p_this;
598 decoder_sys_t *p_sys = p_dec->p_sys;
600 block_BytestreamRelease( &p_sys->bytestream );
602 free( p_sys );
605 /*****************************************************************************
606 * Open: probe the decoder and return score
607 *****************************************************************************/
608 static int Open( vlc_object_t *p_this )
610 decoder_t *p_dec = (decoder_t*)p_this;
611 decoder_sys_t *p_sys;
613 if(( p_dec->fmt_in.i_codec != VLC_CODEC_MPGA ) &&
614 ( p_dec->fmt_in.i_codec != VLC_CODEC_MP3 ) )
616 return VLC_EGENERIC;
619 /* Allocate the memory needed to store the decoder's structure */
620 if( ( p_dec->p_sys = p_sys =
621 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
622 return VLC_ENOMEM;
624 /* Misc init */
625 p_sys->i_state = STATE_NOSYNC;
626 date_Init( &p_sys->end_date, 1, 1 );
627 date_Set( &p_sys->end_date, VLC_TS_INVALID );
628 block_BytestreamInit( &p_sys->bytestream );
629 p_sys->i_pts = VLC_TS_INVALID;
630 p_sys->b_discontinuity = false;
631 p_sys->i_frame_size = 0;
633 p_sys->i_channels_conf = p_sys->i_channels = p_sys->i_rate =
634 p_sys->i_max_frame_size = p_sys->i_frame_length = p_sys->i_layer =
635 p_sys->i_bit_rate = 0;
637 /* Set output properties */
638 p_dec->fmt_out.i_cat = AUDIO_ES;
639 p_dec->fmt_out.i_codec = VLC_CODEC_MPGA;
640 p_dec->fmt_out.audio.i_rate = 0; /* So end_date gets initialized */
642 /* Set callback */
643 p_dec->pf_packetize = DecodeBlock;
644 p_dec->pf_flush = Flush;
646 /* Start with the minimum size for a free bitrate frame */
647 p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
649 return VLC_SUCCESS;