Home | History | Annotate | Download | only in decoder
      1 /******************************************************************************
      2  *                                                                            *
      3  * Copyright (C) 2018 The Android Open Source Project
      4  *
      5  * Licensed under the Apache License, Version 2.0 (the "License");
      6  * you may not use this file except in compliance with the License.
      7  * You may obtain a copy of the License at:
      8  *
      9  * http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  * Unless required by applicable law or agreed to in writing, software
     12  * distributed under the License is distributed on an "AS IS" BASIS,
     13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  * See the License for the specific language governing permissions and
     15  * limitations under the License.
     16  *
     17  *****************************************************************************
     18  * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
     19 */
     20 #include <stdio.h>
     21 #include <string.h>
     22 #include "ixheaacd_sbr_common.h"
     23 #include <ixheaacd_type_def.h>
     24 
     25 #include "ixheaacd_constants.h"
     26 #include <ixheaacd_basic_ops32.h>
     27 #include <ixheaacd_basic_ops16.h>
     28 #include <ixheaacd_basic_ops40.h>
     29 #include "ixheaacd_basic_ops.h"
     30 
     31 #include <ixheaacd_basic_op.h>
     32 #include "ixheaacd_intrinsics.h"
     33 #include "ixheaacd_bitbuffer.h"
     34 #include "ixheaacd_defines.h"
     35 
     36 #include "ixheaacd_pns.h"
     37 
     38 #include <ixheaacd_aac_rom.h>
     39 #include "ixheaacd_pulsedata.h"
     40 #include "ixheaacd_drc_data_struct.h"
     41 
     42 #include "ixheaacd_lt_predict.h"
     43 
     44 #include "ixheaacd_channelinfo.h"
     45 #include "ixheaacd_drc_dec.h"
     46 
     47 #include "ixheaacd_sbrdecoder.h"
     48 #include "ixheaacd_sbr_payload.h"
     49 #include "ixheaacd_audioobjtypes.h"
     50 
     51 #define SBR_EXTENSION_MPEG SBR_EXTENSION
     52 
     53 #define SBR_EXTENSION_CRC_MPEG SBR_EXTENSION_CRC
     54 
     55 FLAG ixheaacd_check_for_sbr_payload(
     56     ia_bit_buf_struct *it_bit_buff,
     57     ia_aac_dec_sbr_bitstream_struct *pstr_stream_sbr, WORD16 prev_element,
     58     ia_drc_dec_struct *pstr_drc_dec, WORD32 object_type, WORD32 adtsheader,
     59     WORD32 cnt_bits, WORD32 ld_sbr_crc_flag, ia_drc_dec_struct *drc_dummy) {
     60   FLAG ret = 0;
     61   WORD32 count;
     62 
     63   if (object_type == AOT_ER_AAC_ELD) {
     64     count = it_bit_buff->cnt_bits >> 3;
     65     if (adtsheader == 1) count = cnt_bits >> 3;
     66   } else {
     67     count = ixheaacd_read_bits_buf(it_bit_buff, 4);
     68 
     69     if ((count - 15) == 0) {
     70       WORD32 esc_count;
     71       esc_count = ixheaacd_read_bits_buf(it_bit_buff, 8);
     72       count = (esc_count + 14);
     73     }
     74   }
     75 
     76   if (count > 0) {
     77     WORD32 extension_type;
     78 
     79     if (object_type == AOT_ER_AAC_ELD)
     80       extension_type = ld_sbr_crc_flag ? SBR_EXTENSION_CRC : SBR_EXTENSION;
     81     else
     82       extension_type = ixheaacd_read_bits_buf(it_bit_buff, 4);
     83 
     84     if (((count < MAXSBRBYTES)) && (((extension_type == SBR_EXTENSION)) ||
     85                                     ((extension_type == SBR_EXTENSION_CRC))) &&
     86         ((prev_element == SBR_ID_SCE) || (prev_element == SBR_ID_CPE) ||
     87          sub_d(prev_element, SBR_ID_CCE) == 0)
     88 
     89             ) {
     90       WORD32 no_elements = pstr_stream_sbr->no_elements;
     91       WORD32 byte_count;
     92       ia_sbr_element_stream_struct *ptr_stream_sbr;
     93 
     94       ret = 1;
     95 
     96       ptr_stream_sbr = &pstr_stream_sbr->str_sbr_ele[no_elements];
     97       ptr_stream_sbr->size_payload = count;
     98       byte_count = ptr_stream_sbr->size_payload;
     99       ptr_stream_sbr->extension_type = extension_type;
    100       ptr_stream_sbr->sbr_ele_id = prev_element;
    101       pstr_stream_sbr->no_elements = no_elements + 1;
    102 
    103       if (pstr_drc_dec) pstr_drc_dec->sbr_found = 1;
    104 
    105       if (byte_count > 0 && sub_d(byte_count, MAXSBRBYTES) <= 0) {
    106         WORD32 i;
    107         WORD8 *ptr_sbr_data;
    108         if (object_type != AOT_ER_AAC_ELD) {
    109           ptr_sbr_data = &ptr_stream_sbr->ptr_sbr_data[1];
    110           ptr_stream_sbr->ptr_sbr_data[0] =
    111               (WORD8)ixheaacd_read_bits_buf(it_bit_buff, 4);
    112         } else
    113           ptr_sbr_data = ptr_stream_sbr->ptr_sbr_data;
    114 
    115         for (i = byte_count - 2; i >= 0; i--) {
    116           *ptr_sbr_data++ = (WORD8)ixheaacd_read_bits_buf(it_bit_buff, 8);
    117           if (object_type == AOT_ER_AAC_ELD) {
    118             if (adtsheader == 1) {
    119               cnt_bits = cnt_bits - 8;
    120             }
    121           }
    122         }
    123 
    124         if (object_type == AOT_ER_AAC_ELD) {
    125           *ptr_sbr_data++ = (WORD8)ixheaacd_read_bits_buf(it_bit_buff, 8);
    126           if (adtsheader == 1) {
    127             cnt_bits = cnt_bits - 8;
    128             if (cnt_bits > 0) {
    129               WORD32 unaligned_bits = (8 - it_bit_buff->cnt_bits);
    130               *ptr_sbr_data =
    131                   (WORD8)ixheaacd_read_bits_buf(it_bit_buff, cnt_bits);
    132               *ptr_sbr_data = *ptr_sbr_data << unaligned_bits;
    133               ptr_stream_sbr->size_payload++;
    134             }
    135           } else {
    136             if (it_bit_buff->cnt_bits > 0) {
    137               WORD32 unaligned_bits = (8 - it_bit_buff->cnt_bits);
    138               *ptr_sbr_data = (WORD8)ixheaacd_read_bits_buf(
    139                   it_bit_buff, it_bit_buff->cnt_bits);
    140               *ptr_sbr_data = *ptr_sbr_data << unaligned_bits;
    141               ptr_stream_sbr->size_payload++;
    142             }
    143           }
    144         }
    145       }
    146 
    147     } else if (extension_type == EXT_DYNAMIC_RANGE) {
    148       pstr_drc_dec->drc_element_found = 1;
    149       count -=
    150           ixheaacd_dec_drc_read_element(pstr_drc_dec, drc_dummy, it_bit_buff);
    151     } else {
    152       ixheaacd_read_bits_buf(it_bit_buff, 4);
    153 
    154       it_bit_buff->ptr_read_next += count - 1;
    155       it_bit_buff->cnt_bits -= ((count - 1) << 3);
    156 
    157       if (it_bit_buff->ptr_read_next > it_bit_buff->ptr_bit_buf_end) {
    158         it_bit_buff->ptr_read_next = it_bit_buff->ptr_bit_buf_base;
    159       }
    160     }
    161   }
    162   if (it_bit_buff->cnt_bits < 0) ret = -1;
    163   return (ret);
    164 }
    165