]> git.sesse.net Git - vlc/blob - modules/packetizer/mpeg4audio.c
Remove stdlib.h
[vlc] / modules / packetizer / mpeg4audio.c
1 /*****************************************************************************
2  * mpeg4audio.c: parse and packetize an MPEG 4 audio stream
3  *****************************************************************************
4  * Copyright (C) 2001, 2002, 2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Gildas Bazin <gbazin@netcourrier.com>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <string.h>                                              /* strdup() */
29
30 #include <vlc/vlc.h>
31 #include <vlc_aout.h>
32 #include <vlc_codec.h>
33 #include <vlc_block.h>
34 #include <vlc_sout.h>
35 #include <vlc_codecs.h>
36 #include <vlc_input.h>
37
38 #include "vlc_block_helper.h"
39
40 /* AAC Config in ES:
41  *
42  * AudioObjectType          5 bits
43  * samplingFrequencyIndex   4 bits
44  * if (samplingFrequencyIndex == 0xF)
45  *  samplingFrequency   24 bits
46  * channelConfiguration     4 bits
47  * GA_SpecificConfig
48  *  FrameLengthFlag         1 bit 1024 or 960
49  *  DependsOnCoreCoder      1 bit (always 0)
50  *  ExtensionFlag           1 bit (always 0)
51  */
52
53 /*****************************************************************************
54  * decoder_sys_t : decoder descriptor
55  *****************************************************************************/
56 struct decoder_sys_t
57 {
58     /*
59      * Input properties
60      */
61     int i_state;
62
63     block_bytestream_t bytestream;
64
65     /*
66      * Common properties
67      */
68     audio_date_t end_date;
69     mtime_t i_pts;
70
71     int i_frame_size;
72     unsigned int i_raw_blocks;
73     unsigned int i_channels;
74     unsigned int i_rate, i_frame_length, i_header_size;
75
76     int i_input_rate;
77 };
78
79 enum {
80
81     STATE_NOSYNC,
82     STATE_SYNC,
83     STATE_HEADER,
84     STATE_NEXT_SYNC,
85     STATE_GET_DATA,
86     STATE_SEND_DATA
87 };
88
89 static int i_sample_rates[] =
90 {
91     96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
92     16000, 12000, 11025, 8000,  7350,  0,     0,     0
93 };
94
95 #define ADTS_HEADER_SIZE 9
96
97 /****************************************************************************
98  * Local prototypes
99  ****************************************************************************/
100 static int  OpenPacketizer( vlc_object_t * );
101 static void ClosePacketizer( vlc_object_t * );
102
103 static block_t *PacketizeBlock    ( decoder_t *, block_t ** );
104 static block_t *ADTSPacketizeBlock( decoder_t *, block_t ** );
105
106 static uint8_t *GetOutBuffer ( decoder_t *, void ** );
107
108 static int ADTSSyncInfo( decoder_t *, const byte_t * p_buf,
109                          unsigned int * pi_channels,
110                          unsigned int * pi_sample_rate,
111                          unsigned int * pi_frame_length,
112                          unsigned int * pi_header_size,
113                          unsigned int * pi_raw_blocks );
114
115 /*****************************************************************************
116  * Module descriptor
117  *****************************************************************************/
118 vlc_module_begin();
119     set_category( CAT_SOUT );
120     set_subcategory( SUBCAT_SOUT_PACKETIZER );
121     set_description( _("MPEG4 audio packetizer") );
122     set_capability( "packetizer", 50 );
123     set_callbacks( OpenPacketizer, ClosePacketizer );
124 vlc_module_end();
125
126 /*****************************************************************************
127  * OpenPacketizer: probe the packetizer and return score
128  *****************************************************************************/
129 static int OpenPacketizer( vlc_object_t *p_this )
130 {
131     decoder_t *p_dec = (decoder_t*)p_this;
132     decoder_sys_t *p_sys;
133
134     if( p_dec->fmt_in.i_codec != VLC_FOURCC( 'm', 'p', '4', 'a' ) )
135     {
136         return VLC_EGENERIC;
137     }
138
139     /* Allocate the memory needed to store the decoder's structure */
140     if( ( p_dec->p_sys = p_sys =
141           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
142     {
143         msg_Err( p_dec, "out of memory" );
144         return VLC_EGENERIC;
145     }
146
147     /* Misc init */
148     p_sys->i_state = STATE_NOSYNC;
149     aout_DateSet( &p_sys->end_date, 0 );
150     p_sys->bytestream = block_BytestreamInit( p_dec );
151     p_sys->i_input_rate = INPUT_RATE_DEFAULT;
152
153     /* Set output properties */
154     p_dec->fmt_out.i_cat = AUDIO_ES;
155     p_dec->fmt_out.i_codec = VLC_FOURCC('m','p','4','a');
156
157     /* Set callback */
158     p_dec->pf_packetize = PacketizeBlock;
159
160     msg_Dbg( p_dec, "running MPEG4 audio packetizer" );
161
162     if( p_dec->fmt_in.i_extra > 0 )
163     {
164         uint8_t *p_config = (uint8_t*)p_dec->fmt_in.p_extra;
165         int     i_index;
166
167         i_index = ( ( p_config[0] << 1 ) | ( p_config[1] >> 7 ) ) & 0x0f;
168         if( i_index != 0x0f )
169         {
170             p_dec->fmt_out.audio.i_rate = i_sample_rates[i_index];
171             p_dec->fmt_out.audio.i_frame_length =
172                 (( p_config[1] >> 2 ) & 0x01) ? 960 : 1024;
173         }
174         else
175         {
176             p_dec->fmt_out.audio.i_rate = ( ( p_config[1] & 0x7f ) << 17 ) |
177                 ( p_config[2] << 9 ) | ( p_config[3] << 1 ) |
178                 ( p_config[4] >> 7 );
179             p_dec->fmt_out.audio.i_frame_length =
180                 (( p_config[4] >> 2 ) & 0x01) ? 960 : 1024;
181         }
182
183         msg_Dbg( p_dec, "AAC %dHz %d samples/frame",
184                  p_dec->fmt_out.audio.i_rate,
185                  p_dec->fmt_out.audio.i_frame_length );
186
187         aout_DateInit( &p_sys->end_date, p_dec->fmt_out.audio.i_rate );
188
189         p_dec->fmt_out.audio.i_channels = p_dec->fmt_in.audio.i_channels;
190         p_dec->fmt_out.i_extra = p_dec->fmt_in.i_extra;
191         p_dec->fmt_out.p_extra = malloc( p_dec->fmt_in.i_extra );
192         memcpy( p_dec->fmt_out.p_extra, p_dec->fmt_in.p_extra,
193                 p_dec->fmt_in.i_extra );
194     }
195     else
196     {
197         msg_Dbg( p_dec, "no decoder specific info, must be an ADTS stream" );
198
199         aout_DateInit( &p_sys->end_date, p_dec->fmt_in.audio.i_rate );
200
201         /* We will try to create a AAC Config from adts */
202         p_dec->fmt_out.i_extra = 0;
203         p_dec->fmt_out.p_extra = NULL;
204         p_dec->pf_packetize = ADTSPacketizeBlock;
205     }
206
207     return VLC_SUCCESS;
208 }
209
210 /****************************************************************************
211  * PacketizeBlock: the whole thing
212  ****************************************************************************
213  * This function must be fed with complete frames.
214  ****************************************************************************/
215 static block_t *PacketizeBlock( decoder_t *p_dec, block_t **pp_block )
216 {
217     decoder_sys_t *p_sys = p_dec->p_sys;
218     block_t *p_block;
219
220     if( !pp_block || !*pp_block ) return NULL;
221
222     if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
223     {
224         //aout_DateSet( &p_sys->end_date, 0 );
225         block_Release( *pp_block );
226         return NULL;
227     }
228
229     p_block = *pp_block;
230     *pp_block = NULL; /* Don't reuse this block */
231
232     if( !aout_DateGet( &p_sys->end_date ) && !p_block->i_pts )
233     {
234         /* We've just started the stream, wait for the first PTS. */
235         block_Release( p_block );
236         return NULL;
237     }
238     else if( p_block->i_pts != 0 &&
239              p_block->i_pts != aout_DateGet( &p_sys->end_date ) )
240     {
241         aout_DateSet( &p_sys->end_date, p_block->i_pts );
242     }
243
244     p_block->i_pts = p_block->i_dts = aout_DateGet( &p_sys->end_date );
245
246     p_block->i_length = aout_DateIncrement( &p_sys->end_date,
247         p_dec->fmt_out.audio.i_frame_length * p_sys->i_input_rate / INPUT_RATE_DEFAULT ) - p_block->i_pts;
248
249     return p_block;
250 }
251
252 /****************************************************************************
253  * DTSPacketizeBlock: the whole thing
254  ****************************************************************************/
255 static block_t *ADTSPacketizeBlock( decoder_t *p_dec, block_t **pp_block )
256 {
257     decoder_sys_t *p_sys = p_dec->p_sys;
258     uint8_t p_header[ADTS_HEADER_SIZE];
259     void *p_out_buffer;
260     uint8_t *p_buf;
261
262     if( !pp_block || !*pp_block ) return NULL;
263
264     if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
265     {
266         if( (*pp_block)->i_flags&BLOCK_FLAG_CORRUPTED )
267         {
268             p_sys->i_state = STATE_NOSYNC;
269             block_BytestreamFlush( &p_sys->bytestream );
270         }
271         //aout_DateSet( &p_sys->end_date, 0 );
272         block_Release( *pp_block );
273         return NULL;
274     }
275
276     if( !aout_DateGet( &p_sys->end_date ) && !(*pp_block)->i_pts )
277     {
278         /* We've just started the stream, wait for the first PTS. */
279         block_Release( *pp_block );
280         return NULL;
281     }
282
283     if( (*pp_block)->i_rate > 0 )
284         p_sys->i_input_rate = (*pp_block)->i_rate;
285
286     block_BytestreamPush( &p_sys->bytestream, *pp_block );
287
288     while( 1 )
289     {
290         switch( p_sys->i_state )
291         {
292
293         case STATE_NOSYNC:
294             while( block_PeekBytes( &p_sys->bytestream, p_header, 2 )
295                    == VLC_SUCCESS )
296             {
297                 /* Look for sync word - should be 0xfff + 2 layer bits */
298                 if( p_header[0] == 0xff && (p_header[1] & 0xf6) == 0xf0 )
299                 {
300                     p_sys->i_state = STATE_SYNC;
301                     break;
302                 }
303                 block_SkipByte( &p_sys->bytestream );
304             }
305             if( p_sys->i_state != STATE_SYNC )
306             {
307                 block_BytestreamFlush( &p_sys->bytestream );
308
309                 /* Need more data */
310                 return NULL;
311             }
312
313         case STATE_SYNC:
314             /* New frame, set the Presentation Time Stamp */
315             p_sys->i_pts = p_sys->bytestream.p_block->i_pts;
316             if( p_sys->i_pts != 0 &&
317                 p_sys->i_pts != aout_DateGet( &p_sys->end_date ) )
318             {
319                 aout_DateSet( &p_sys->end_date, p_sys->i_pts );
320             }
321             p_sys->i_state = STATE_HEADER;
322             break;
323
324         case STATE_HEADER:
325             /* Get ADTS frame header (ADTS_HEADER_SIZE bytes) */
326             if( block_PeekBytes( &p_sys->bytestream, p_header,
327                                  ADTS_HEADER_SIZE ) != VLC_SUCCESS )
328             {
329                 /* Need more data */
330                 return NULL;
331             }
332
333             /* Check if frame is valid and get frame info */
334             p_sys->i_frame_size = ADTSSyncInfo( p_dec, p_header,
335                                                 &p_sys->i_channels,
336                                                 &p_sys->i_rate,
337                                                 &p_sys->i_frame_length,
338                                                 &p_sys->i_header_size,
339                                                 &p_sys->i_raw_blocks );
340             if( p_sys->i_frame_size <= 0 )
341             {
342                 msg_Dbg( p_dec, "emulated sync word" );
343                 block_SkipByte( &p_sys->bytestream );
344                 p_sys->i_state = STATE_NOSYNC;
345                 break;
346             }
347
348             p_sys->i_state = STATE_NEXT_SYNC;
349
350         case STATE_NEXT_SYNC:
351             /* TODO: If p_block == NULL, flush the buffer without checking the
352              * next sync word */
353
354             /* Check if next expected frame contains the sync word */
355             if( block_PeekOffsetBytes( &p_sys->bytestream, p_sys->i_frame_size
356                                        + p_sys->i_header_size, p_header, 2 )
357                 != VLC_SUCCESS )
358             {
359                 /* Need more data */
360                 return NULL;
361             }
362
363             if( p_header[0] != 0xff || (p_header[1] & 0xf6) != 0xf0 )
364             {
365                 msg_Dbg( p_dec, "emulated sync word "
366                          "(no sync on following frame)" );
367                 p_sys->i_state = STATE_NOSYNC;
368                 block_SkipByte( &p_sys->bytestream );
369                 break;
370             }
371
372             p_sys->i_state = STATE_SEND_DATA;
373             break;
374
375         case STATE_GET_DATA:
376             /* Make sure we have enough data.
377              * (Not useful if we went through NEXT_SYNC) */
378             if( block_WaitBytes( &p_sys->bytestream, p_sys->i_frame_size +
379                                  p_sys->i_header_size) != VLC_SUCCESS )
380             {
381                 /* Need more data */
382                 return NULL;
383             }
384             p_sys->i_state = STATE_SEND_DATA;
385
386         case STATE_SEND_DATA:
387             if( !(p_buf = GetOutBuffer( p_dec, &p_out_buffer )) )
388             {
389                 //p_dec->b_error = VLC_TRUE;
390                 return NULL;
391             }
392
393             /* When we reach this point we already know we have enough
394              * data available. */
395
396             /* Skip the ADTS header */
397             block_SkipBytes( &p_sys->bytestream, p_sys->i_header_size );
398
399             /* Copy the whole frame into the buffer */
400             block_GetBytes( &p_sys->bytestream, p_buf, p_sys->i_frame_size );
401
402             /* Make sure we don't reuse the same pts twice */
403             if( p_sys->i_pts == p_sys->bytestream.p_block->i_pts )
404                 p_sys->i_pts = p_sys->bytestream.p_block->i_pts = 0;
405
406             /* So p_block doesn't get re-added several times */
407             *pp_block = block_BytestreamPop( &p_sys->bytestream );
408
409             p_sys->i_state = STATE_NOSYNC;
410
411             return p_out_buffer;
412         }
413     }
414
415     return NULL;
416 }
417
418 /*****************************************************************************
419  * GetOutBuffer:
420  *****************************************************************************/
421 static uint8_t *GetOutBuffer( decoder_t *p_dec, void **pp_out_buffer )
422 {
423     decoder_sys_t *p_sys = p_dec->p_sys;
424     block_t *p_block;
425
426     if( p_dec->fmt_out.audio.i_rate != p_sys->i_rate )
427     {
428         msg_Info( p_dec, "AAC channels: %d samplerate: %d",
429                   p_sys->i_channels, p_sys->i_rate );
430
431         aout_DateInit( &p_sys->end_date, p_sys->i_rate );
432         aout_DateSet( &p_sys->end_date, p_sys->i_pts );
433     }
434
435     p_dec->fmt_out.audio.i_rate     = p_sys->i_rate;
436     p_dec->fmt_out.audio.i_channels = p_sys->i_channels;
437     p_dec->fmt_out.audio.i_bytes_per_frame = p_sys->i_frame_size;
438     p_dec->fmt_out.audio.i_frame_length = p_sys->i_frame_length;
439
440 #if 0
441     p_dec->fmt_out.audio.i_original_channels = p_sys->i_channels_conf;
442     p_dec->fmt_out.audio.i_physical_channels =
443         p_sys->i_channels_conf & AOUT_CHAN_PHYSMASK;
444 #endif
445
446     p_block = block_New( p_dec, p_sys->i_frame_size );
447     if( p_block == NULL ) return NULL;
448
449     p_block->i_pts = p_block->i_dts = aout_DateGet( &p_sys->end_date );
450
451     p_block->i_length = aout_DateIncrement( &p_sys->end_date,
452                             p_sys->i_frame_length * p_sys->i_input_rate / INPUT_RATE_DEFAULT ) -
453                                 p_block->i_pts;
454
455     *pp_out_buffer = p_block;
456     return p_block->p_buffer;
457 }
458
459 /*****************************************************************************
460  * ClosePacketizer: clean up the packetizer
461  *****************************************************************************/
462 static void ClosePacketizer( vlc_object_t *p_this )
463 {
464     decoder_t *p_dec = (decoder_t *)p_this;
465     decoder_sys_t *p_sys = p_dec->p_sys;
466
467     block_BytestreamRelease( &p_sys->bytestream );
468
469     free( p_dec->p_sys );
470 }
471
472 /*****************************************************************************
473  * ADTSSyncInfo: parse MPEG 4 audio ADTS sync info
474  *****************************************************************************/
475 static int ADTSSyncInfo( decoder_t * p_dec, const byte_t * p_buf,
476                          unsigned int * pi_channels,
477                          unsigned int * pi_sample_rate,
478                          unsigned int * pi_frame_length,
479                          unsigned int * pi_header_size,
480                          unsigned int * pi_raw_blocks_in_frame )
481 {
482     int i_id, i_profile, i_sample_rate_idx, i_frame_size;
483     vlc_bool_t b_crc;
484
485     /* Fixed header between frames */
486     i_id = ( (p_buf[1] >> 3) & 0x01 ) ? 2 : 4;
487     b_crc = !(p_buf[1] & 0x01);
488     i_profile = p_buf[2] >> 6;
489     i_sample_rate_idx = (p_buf[2] >> 2) & 0x0f;
490     *pi_sample_rate = i_sample_rates[i_sample_rate_idx];
491     *pi_channels = ((p_buf[2] & 0x01) << 2) | ((p_buf[3] >> 6) & 0x03);
492
493     /* Variable header */
494     i_frame_size = ((p_buf[3] & 0x03) << 11) | (p_buf[4] << 3) |
495                    ((p_buf[5] >> 5) & 0x7);
496     *pi_raw_blocks_in_frame = (p_buf[6] & 0x02) + 1;
497
498     if( !*pi_sample_rate || !*pi_channels || !i_frame_size )
499     {
500         return 0;
501     }
502
503     /* Fixme */
504     *pi_frame_length = 1024;
505
506     /* Build the decoder specific info header */
507     if( !p_dec->fmt_out.i_extra )
508     {
509         p_dec->fmt_out.i_extra = 2;
510         p_dec->fmt_out.p_extra = malloc( 2 );
511         ((uint8_t *)p_dec->fmt_out.p_extra)[0] =
512             (i_profile + 1) << 3 | (i_sample_rate_idx >> 1);
513         ((uint8_t *)p_dec->fmt_out.p_extra)[1] =
514             ((i_sample_rate_idx & 0x01) << 7) | (*pi_channels <<3);
515     }
516
517     /* ADTS header length */
518     *pi_header_size = b_crc ? 9 : 7;
519
520     return i_frame_size - *pi_header_size;
521 }