changeset 25836:bc425ba00960

added code to check and handle the presence of LATM streams in the init() and decode() functions
author nicodvb
date Sat, 26 Jan 2008 18:45:11 +0000
parents 645cbba10a57
children 643064afe2a4
files libfaad2/decoder.c libfaad2/syntax.c
diffstat 2 files changed, 69 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/libfaad2/decoder.c	Sat Jan 26 18:42:17 2008 +0000
+++ b/libfaad2/decoder.c	Sat Jan 26 18:45:11 2008 +0000
@@ -195,6 +195,31 @@
     return 0;
 }
 
+static int latmCheck(latm_header *latm, bitfile *ld)
+{
+    uint32_t good=0, bad=0, bits, m;
+
+    while(!ld->error && !ld->no_more_reading)
+    {
+        bits = faad_latm_frame(latm, ld);
+        if(bits==-1U)
+            bad++;
+        else
+        {
+            good++;
+            while(bits>0)
+            {
+                m = min(bits, 8);
+                faad_getbits(ld, m);
+                bits -= m;
+            }
+        }
+    }
+
+    return (good>0);
+}
+
+
 int32_t NEAACDECAPI NeAACDecInit(NeAACDecHandle hDecoder, uint8_t *buffer,
                                  uint32_t buffer_size,
                                  uint32_t *samplerate, uint8_t *channels)
@@ -214,8 +239,25 @@
 
     if (buffer != NULL)
     {
+        int is_latm;
+        latm_header *l = &hDecoder->latm_config;
         faad_initbits(&ld, buffer, buffer_size);
 
+        memset(l, 0, sizeof(latm_header));
+        is_latm = latmCheck(l, &ld);
+        l->inited = 0;
+        l->frameLength = 0;
+        faad_rewindbits(&ld);
+        if(is_latm && l->ASCbits>0)
+        {
+            int32_t x;
+            hDecoder->latm_header_present = 1;
+            x = NeAACDecInit2(hDecoder, &l->ASC, (l->ASCbits+7)/8, samplerate, channels);
+            if(x!=0)
+                hDecoder->latm_header_present = 0;
+            return x;
+        }
+        else
         /* Check if an ADIF header is present */
         if ((buffer[0] == 'A') && (buffer[1] == 'D') &&
             (buffer[2] == 'I') && (buffer[3] == 'F'))
@@ -733,6 +775,7 @@
     uint32_t bitsconsumed;
     uint16_t frame_len;
     void *sample_buffer;
+    uint32_t startbit=0, endbit=0, payload_bits=0;
 
 #ifdef PROFILE
     int64_t count = faad_get_ts();
@@ -775,6 +818,17 @@
     }
 #endif
 
+    if(hDecoder->latm_header_present)
+    {
+        payload_bits = faad_latm_frame(&hDecoder->latm_config, &ld);
+        startbit = faad_get_processed_bits(&ld);
+        if(payload_bits == -1U)
+        {
+            hInfo->error = 1;
+            goto error;
+        }
+    }
+
 #ifdef DRM
     if (hDecoder->object_type == DRM_ER_LC)
     {
@@ -820,6 +874,17 @@
     }
 #endif
 
+    if(hDecoder->latm_header_present)
+    {
+        endbit = faad_get_processed_bits(&ld);
+        if(endbit-startbit > payload_bits)
+            fprintf(stderr, "\r\nERROR, too many payload bits read: %u > %d. Please. report with a link to a sample\n",
+                endbit-startbit, payload_bits);
+        if(hDecoder->latm_config.otherDataLenBits > 0)
+            faad_getbits(&ld, hDecoder->latm_config.otherDataLenBits);
+        faad_byte_align(&ld);
+    }
+
     channels = hDecoder->fr_channels;
 
     if (hInfo->error > 0)
@@ -844,7 +909,7 @@
     faad_endbits(&ld);
 
 
-    if (!hDecoder->adts_header_present && !hDecoder->adif_header_present)
+    if (!hDecoder->adts_header_present && !hDecoder->adif_header_present && !hDecoder->latm_header_present)
     {
         if (hDecoder->channelConfiguration == 0)
             hDecoder->channelConfiguration = channels;
@@ -893,6 +958,8 @@
         hInfo->header_type = ADIF;
     if (hDecoder->adts_header_present)
         hInfo->header_type = ADTS;
+    if (hDecoder->latm_header_present)
+        hInfo->header_type = LATM;
 #if (defined(PS_DEC) || defined(DRM_PS))
     hInfo->ps = hDecoder->ps_used_global;
 #endif
--- a/libfaad2/syntax.c	Sat Jan 26 18:42:17 2008 +0000
+++ b/libfaad2/syntax.c	Sat Jan 26 18:45:11 2008 +0000
@@ -548,7 +548,7 @@
 
     /* new in corrigendum 14496-3:2002 */
 #ifdef DRM
-    if (hDecoder->object_type != DRM_ER_LC)
+    if (hDecoder->object_type != DRM_ER_LC && !hDecoder->latm_header_present)
 #endif
     {
         faad_byte_align(ld);