1 /*****************************************************************************
2 * mpeg4audio.c: parse and packetize an MPEG 4 audio stream
3 *****************************************************************************
4 * Copyright (C) 2001, 2002, 2006 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Gildas Bazin <gbazin@netcourrier.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
28 #include <stdlib.h> /* malloc(), free() */
29 #include <string.h> /* strdup() */
33 #include <vlc_codec.h>
34 #include <vlc_block.h>
36 #include <vlc_codecs.h>
38 #include "vlc_block_helper.h"
42 * AudioObjectType 5 bits
43 * samplingFrequencyIndex 4 bits
44 * if (samplingFrequencyIndex == 0xF)
45 * samplingFrequency 24 bits
46 * channelConfiguration 4 bits
48 * FrameLengthFlag 1 bit 1024 or 960
49 * DependsOnCoreCoder 1 bit (always 0)
50 * ExtensionFlag 1 bit (always 0)
53 /*****************************************************************************
54 * decoder_sys_t : decoder descriptor
55 *****************************************************************************/
63 block_bytestream_t bytestream;
68 audio_date_t end_date;
72 unsigned int i_raw_blocks;
73 unsigned int i_channels;
74 unsigned int i_rate, i_frame_length, i_header_size;
87 static int i_sample_rates[] =
89 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
90 16000, 12000, 11025, 8000, 7350, 0, 0, 0
93 #define ADTS_HEADER_SIZE 9
95 /****************************************************************************
97 ****************************************************************************/
98 static int OpenPacketizer( vlc_object_t * );
99 static void ClosePacketizer( vlc_object_t * );
101 static block_t *PacketizeBlock ( decoder_t *, block_t ** );
102 static block_t *ADTSPacketizeBlock( decoder_t *, block_t ** );
104 static uint8_t *GetOutBuffer ( decoder_t *, void ** );
106 static int ADTSSyncInfo( decoder_t *, const byte_t * p_buf,
107 unsigned int * pi_channels,
108 unsigned int * pi_sample_rate,
109 unsigned int * pi_frame_length,
110 unsigned int * pi_header_size,
111 unsigned int * pi_raw_blocks );
113 /*****************************************************************************
115 *****************************************************************************/
117 set_category( CAT_SOUT );
118 set_subcategory( SUBCAT_SOUT_PACKETIZER );
119 set_description( _("MPEG4 audio packetizer") );
120 set_capability( "packetizer", 50 );
121 set_callbacks( OpenPacketizer, ClosePacketizer );
124 /*****************************************************************************
125 * OpenPacketizer: probe the packetizer and return score
126 *****************************************************************************/
127 static int OpenPacketizer( vlc_object_t *p_this )
129 decoder_t *p_dec = (decoder_t*)p_this;
130 decoder_sys_t *p_sys;
132 if( p_dec->fmt_in.i_codec != VLC_FOURCC( 'm', 'p', '4', 'a' ) )
137 /* Allocate the memory needed to store the decoder's structure */
138 if( ( p_dec->p_sys = p_sys =
139 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
141 msg_Err( p_dec, "out of memory" );
146 p_sys->i_state = STATE_NOSYNC;
147 aout_DateSet( &p_sys->end_date, 0 );
148 p_sys->bytestream = block_BytestreamInit( p_dec );
150 /* Set output properties */
151 p_dec->fmt_out.i_cat = AUDIO_ES;
152 p_dec->fmt_out.i_codec = VLC_FOURCC('m','p','4','a');
155 p_dec->pf_packetize = PacketizeBlock;
157 msg_Dbg( p_dec, "running MPEG4 audio packetizer" );
159 if( p_dec->fmt_in.i_extra > 0 )
161 uint8_t *p_config = (uint8_t*)p_dec->fmt_in.p_extra;
164 i_index = ( ( p_config[0] << 1 ) | ( p_config[1] >> 7 ) ) & 0x0f;
165 if( i_index != 0x0f )
167 p_dec->fmt_out.audio.i_rate = i_sample_rates[i_index];
168 p_dec->fmt_out.audio.i_frame_length =
169 (( p_config[1] >> 2 ) & 0x01) ? 960 : 1024;
173 p_dec->fmt_out.audio.i_rate = ( ( p_config[1] & 0x7f ) << 17 ) |
174 ( p_config[2] << 9 ) | ( p_config[3] << 1 ) |
175 ( p_config[4] >> 7 );
176 p_dec->fmt_out.audio.i_frame_length =
177 (( p_config[4] >> 2 ) & 0x01) ? 960 : 1024;
180 msg_Dbg( p_dec, "AAC %dHz %d samples/frame",
181 p_dec->fmt_out.audio.i_rate,
182 p_dec->fmt_out.audio.i_frame_length );
184 aout_DateInit( &p_sys->end_date, p_dec->fmt_out.audio.i_rate );
186 p_dec->fmt_out.audio.i_channels = p_dec->fmt_in.audio.i_channels;
187 p_dec->fmt_out.i_extra = p_dec->fmt_in.i_extra;
188 p_dec->fmt_out.p_extra = malloc( p_dec->fmt_in.i_extra );
189 memcpy( p_dec->fmt_out.p_extra, p_dec->fmt_in.p_extra,
190 p_dec->fmt_in.i_extra );
194 msg_Dbg( p_dec, "no decoder specific info, must be an ADTS stream" );
196 aout_DateInit( &p_sys->end_date, p_dec->fmt_in.audio.i_rate );
198 /* We will try to create a AAC Config from adts */
199 p_dec->fmt_out.i_extra = 0;
200 p_dec->fmt_out.p_extra = NULL;
201 p_dec->pf_packetize = ADTSPacketizeBlock;
207 /****************************************************************************
208 * PacketizeBlock: the whole thing
209 ****************************************************************************
210 * This function must be fed with complete frames.
211 ****************************************************************************/
212 static block_t *PacketizeBlock( decoder_t *p_dec, block_t **pp_block )
214 decoder_sys_t *p_sys = p_dec->p_sys;
217 if( !pp_block || !*pp_block ) return NULL;
219 if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
221 //aout_DateSet( &p_sys->end_date, 0 );
222 block_Release( *pp_block );
227 *pp_block = NULL; /* Don't reuse this block */
229 if( !aout_DateGet( &p_sys->end_date ) && !p_block->i_pts )
231 /* We've just started the stream, wait for the first PTS. */
232 block_Release( p_block );
235 else if( p_block->i_pts != 0 &&
236 p_block->i_pts != aout_DateGet( &p_sys->end_date ) )
238 aout_DateSet( &p_sys->end_date, p_block->i_pts );
241 p_block->i_pts = p_block->i_dts = aout_DateGet( &p_sys->end_date );
243 p_block->i_length = aout_DateIncrement( &p_sys->end_date,
244 p_dec->fmt_out.audio.i_frame_length ) - p_block->i_pts;
249 /****************************************************************************
250 * DTSPacketizeBlock: the whole thing
251 ****************************************************************************/
252 static block_t *ADTSPacketizeBlock( decoder_t *p_dec, block_t **pp_block )
254 decoder_sys_t *p_sys = p_dec->p_sys;
255 uint8_t p_header[ADTS_HEADER_SIZE];
259 if( !pp_block || !*pp_block ) return NULL;
261 if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
263 if( (*pp_block)->i_flags&BLOCK_FLAG_CORRUPTED )
265 p_sys->i_state = STATE_NOSYNC;
266 block_BytestreamFlush( &p_sys->bytestream );
268 //aout_DateSet( &p_sys->end_date, 0 );
269 block_Release( *pp_block );
273 if( !aout_DateGet( &p_sys->end_date ) && !(*pp_block)->i_pts )
275 /* We've just started the stream, wait for the first PTS. */
276 block_Release( *pp_block );
280 block_BytestreamPush( &p_sys->bytestream, *pp_block );
284 switch( p_sys->i_state )
288 while( block_PeekBytes( &p_sys->bytestream, p_header, 2 )
291 /* Look for sync word - should be 0xfff + 2 layer bits */
292 if( p_header[0] == 0xff && (p_header[1] & 0xf6) == 0xf0 )
294 p_sys->i_state = STATE_SYNC;
297 block_SkipByte( &p_sys->bytestream );
299 if( p_sys->i_state != STATE_SYNC )
301 block_BytestreamFlush( &p_sys->bytestream );
308 /* New frame, set the Presentation Time Stamp */
309 p_sys->i_pts = p_sys->bytestream.p_block->i_pts;
310 if( p_sys->i_pts != 0 &&
311 p_sys->i_pts != aout_DateGet( &p_sys->end_date ) )
313 aout_DateSet( &p_sys->end_date, p_sys->i_pts );
315 p_sys->i_state = STATE_HEADER;
319 /* Get ADTS frame header (ADTS_HEADER_SIZE bytes) */
320 if( block_PeekBytes( &p_sys->bytestream, p_header,
321 ADTS_HEADER_SIZE ) != VLC_SUCCESS )
327 /* Check if frame is valid and get frame info */
328 p_sys->i_frame_size = ADTSSyncInfo( p_dec, p_header,
331 &p_sys->i_frame_length,
332 &p_sys->i_header_size,
333 &p_sys->i_raw_blocks );
334 if( p_sys->i_frame_size <= 0 )
336 msg_Dbg( p_dec, "emulated sync word" );
337 block_SkipByte( &p_sys->bytestream );
338 p_sys->i_state = STATE_NOSYNC;
342 p_sys->i_state = STATE_NEXT_SYNC;
344 case STATE_NEXT_SYNC:
345 /* TODO: If p_block == NULL, flush the buffer without checking the
348 /* Check if next expected frame contains the sync word */
349 if( block_PeekOffsetBytes( &p_sys->bytestream, p_sys->i_frame_size
350 + p_sys->i_header_size, p_header, 2 )
357 if( p_header[0] != 0xff || (p_header[1] & 0xf6) != 0xf0 )
359 msg_Dbg( p_dec, "emulated sync word "
360 "(no sync on following frame)" );
361 p_sys->i_state = STATE_NOSYNC;
362 block_SkipByte( &p_sys->bytestream );
366 p_sys->i_state = STATE_SEND_DATA;
370 /* Make sure we have enough data.
371 * (Not useful if we went through NEXT_SYNC) */
372 if( block_WaitBytes( &p_sys->bytestream, p_sys->i_frame_size +
373 p_sys->i_header_size) != VLC_SUCCESS )
378 p_sys->i_state = STATE_SEND_DATA;
380 case STATE_SEND_DATA:
381 if( !(p_buf = GetOutBuffer( p_dec, &p_out_buffer )) )
383 //p_dec->b_error = VLC_TRUE;
387 /* When we reach this point we already know we have enough
390 /* Skip the ADTS header */
391 block_SkipBytes( &p_sys->bytestream, p_sys->i_header_size );
393 /* Copy the whole frame into the buffer */
394 block_GetBytes( &p_sys->bytestream, p_buf, p_sys->i_frame_size );
396 /* Make sure we don't reuse the same pts twice */
397 if( p_sys->i_pts == p_sys->bytestream.p_block->i_pts )
398 p_sys->i_pts = p_sys->bytestream.p_block->i_pts = 0;
400 /* So p_block doesn't get re-added several times */
401 *pp_block = block_BytestreamPop( &p_sys->bytestream );
403 p_sys->i_state = STATE_NOSYNC;
412 /*****************************************************************************
414 *****************************************************************************/
415 static uint8_t *GetOutBuffer( decoder_t *p_dec, void **pp_out_buffer )
417 decoder_sys_t *p_sys = p_dec->p_sys;
420 if( p_dec->fmt_out.audio.i_rate != p_sys->i_rate )
422 msg_Info( p_dec, "AAC channels: %d samplerate: %d",
423 p_sys->i_channels, p_sys->i_rate );
425 aout_DateInit( &p_sys->end_date, p_sys->i_rate );
426 aout_DateSet( &p_sys->end_date, p_sys->i_pts );
429 p_dec->fmt_out.audio.i_rate = p_sys->i_rate;
430 p_dec->fmt_out.audio.i_channels = p_sys->i_channels;
431 p_dec->fmt_out.audio.i_bytes_per_frame = p_sys->i_frame_size;
432 p_dec->fmt_out.audio.i_frame_length = p_sys->i_frame_length;
435 p_dec->fmt_out.audio.i_original_channels = p_sys->i_channels_conf;
436 p_dec->fmt_out.audio.i_physical_channels =
437 p_sys->i_channels_conf & AOUT_CHAN_PHYSMASK;
440 p_block = block_New( p_dec, p_sys->i_frame_size );
441 if( p_block == NULL ) return NULL;
443 p_block->i_pts = p_block->i_dts = aout_DateGet( &p_sys->end_date );
445 p_block->i_length = aout_DateIncrement( &p_sys->end_date,
446 p_sys->i_frame_length ) - p_block->i_pts;
448 *pp_out_buffer = p_block;
449 return p_block->p_buffer;
452 /*****************************************************************************
453 * ClosePacketizer: clean up the packetizer
454 *****************************************************************************/
455 static void ClosePacketizer( vlc_object_t *p_this )
457 decoder_t *p_dec = (decoder_t *)p_this;
458 decoder_sys_t *p_sys = p_dec->p_sys;
460 block_BytestreamRelease( &p_sys->bytestream );
462 free( p_dec->p_sys );
465 /*****************************************************************************
466 * ADTSSyncInfo: parse MPEG 4 audio ADTS sync info
467 *****************************************************************************/
468 static int ADTSSyncInfo( decoder_t * p_dec, const byte_t * p_buf,
469 unsigned int * pi_channels,
470 unsigned int * pi_sample_rate,
471 unsigned int * pi_frame_length,
472 unsigned int * pi_header_size,
473 unsigned int * pi_raw_blocks_in_frame )
475 int i_id, i_profile, i_sample_rate_idx, i_frame_size;
478 /* Fixed header between frames */
479 i_id = ( (p_buf[1] >> 3) & 0x01 ) ? 2 : 4;
480 b_crc = !(p_buf[1] & 0x01);
481 i_profile = p_buf[2] >> 6;
482 i_sample_rate_idx = (p_buf[2] >> 2) & 0x0f;
483 *pi_sample_rate = i_sample_rates[i_sample_rate_idx];
484 *pi_channels = ((p_buf[2] & 0x01) << 2) | ((p_buf[3] >> 6) & 0x03);
486 /* Variable header */
487 i_frame_size = ((p_buf[3] & 0x03) << 11) | (p_buf[4] << 3) |
488 ((p_buf[5] >> 5) & 0x7);
489 *pi_raw_blocks_in_frame = (p_buf[6] & 0x02) + 1;
491 if( !*pi_sample_rate || !*pi_channels || !i_frame_size )
497 *pi_frame_length = 1024;
499 /* Build the decoder specific info header */
500 if( !p_dec->fmt_out.i_extra )
502 p_dec->fmt_out.i_extra = 2;
503 p_dec->fmt_out.p_extra = malloc( 2 );
504 ((uint8_t *)p_dec->fmt_out.p_extra)[0] =
505 (i_profile + 1) << 3 | (i_sample_rate_idx >> 1);
506 ((uint8_t *)p_dec->fmt_out.p_extra)[1] =
507 ((i_sample_rate_idx & 0x01) << 7) | (*pi_channels <<3);
510 /* ADTS header length */
511 *pi_header_size = b_crc ? 9 : 7;
513 return i_frame_size - *pi_header_size;