aboutsummaryrefslogtreecommitdiff
path: root/decoder/ixheaacd_aacpluscheck.c
blob: 8c7266eddfb3202d666fdb4e373efa9d4e09bf12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/******************************************************************************
 *                                                                            *
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *****************************************************************************
 * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
*/
#include <stdio.h>
#include <string.h>
#include "ixheaacd_sbr_common.h"
#include "ixheaacd_type_def.h"

#include "ixheaacd_constants.h"
#include "ixheaacd_basic_ops32.h"
#include "ixheaacd_basic_ops16.h"
#include "ixheaacd_basic_ops40.h"
#include "ixheaacd_basic_ops.h"

#include "ixheaacd_basic_op.h"
#include "ixheaacd_intrinsics.h"
#include "ixheaacd_bitbuffer.h"
#include "ixheaacd_defines.h"

#include "ixheaacd_pns.h"

#include "ixheaacd_aac_rom.h"
#include "ixheaacd_pulsedata.h"
#include "ixheaacd_drc_data_struct.h"

#include "ixheaacd_lt_predict.h"

#include "ixheaacd_channelinfo.h"
#include "ixheaacd_drc_dec.h"

#include "ixheaacd_sbrdecoder.h"
#include "ixheaacd_sbr_payload.h"
#include "ixheaacd_audioobjtypes.h"
#include "ixheaacd_error_codes.h"

#define SBR_EXTENSION_MPEG SBR_EXTENSION

#define SBR_EXTENSION_CRC_MPEG SBR_EXTENSION_CRC

FLAG ixheaacd_check_for_sbr_payload(
    ia_bit_buf_struct *it_bit_buff,
    ia_aac_dec_sbr_bitstream_struct *pstr_stream_sbr, WORD16 prev_element,
    ia_drc_dec_struct *pstr_drc_dec, WORD32 object_type, WORD32 adtsheader,
    WORD32 cnt_bits, WORD32 ld_sbr_crc_flag, ia_drc_dec_struct *drc_dummy) {
  FLAG ret = 0;
  WORD32 count;

  if (object_type == AOT_ER_AAC_ELD) {
    count = it_bit_buff->cnt_bits >> 3;
    if (adtsheader == 1) count = cnt_bits >> 3;
  } else {
    count = ixheaacd_read_bits_buf(it_bit_buff, 4);

    if ((count - 15) == 0) {
      WORD32 esc_count;
      esc_count = ixheaacd_read_bits_buf(it_bit_buff, 8);
      count = (esc_count + 14);
    }
  }

  if (count > 0) {
    WORD32 extension_type;

    if (object_type == AOT_ER_AAC_ELD)
      extension_type = ld_sbr_crc_flag ? SBR_EXTENSION_CRC : SBR_EXTENSION;
    else
      extension_type = ixheaacd_read_bits_buf(it_bit_buff, 4);

    if (((count < MAXSBRBYTES)) && (((extension_type == SBR_EXTENSION)) ||
                                    ((extension_type == SBR_EXTENSION_CRC))) &&
        ((prev_element == SBR_ID_SCE) || (prev_element == SBR_ID_CPE) ||
         sub_d(prev_element, SBR_ID_CCE) == 0)

            ) {
      WORD32 no_elements = pstr_stream_sbr->no_elements;
      WORD32 byte_count;
      ia_sbr_element_stream_struct *ptr_stream_sbr;

      ret = 1;

      ptr_stream_sbr = &pstr_stream_sbr->str_sbr_ele[no_elements];
      ptr_stream_sbr->size_payload = count;
      byte_count = ptr_stream_sbr->size_payload;
      ptr_stream_sbr->extension_type = extension_type;
      ptr_stream_sbr->sbr_ele_id = prev_element;
      pstr_stream_sbr->no_elements = no_elements + 1;

      if (pstr_drc_dec) pstr_drc_dec->sbr_found = 1;

      if (byte_count > 0 && sub_d(byte_count, MAXSBRBYTES) <= 0) {
        WORD32 i;
        WORD8 *ptr_sbr_data;
        if (object_type != AOT_ER_AAC_ELD) {
          ptr_sbr_data = &ptr_stream_sbr->ptr_sbr_data[1];
          ptr_stream_sbr->ptr_sbr_data[0] =
              (WORD8)ixheaacd_read_bits_buf(it_bit_buff, 4);
        } else
          ptr_sbr_data = ptr_stream_sbr->ptr_sbr_data;

        for (i = byte_count - 2; i >= 0; i--) {
          *ptr_sbr_data++ = (WORD8)ixheaacd_read_bits_buf(it_bit_buff, 8);
          if (object_type == AOT_ER_AAC_ELD) {
            if (adtsheader == 1) {
              cnt_bits = cnt_bits - 8;
            }
          }
        }

        if (object_type == AOT_ER_AAC_ELD) {
          *ptr_sbr_data++ = (WORD8)ixheaacd_read_bits_buf(it_bit_buff, 8);
          if (adtsheader == 1) {
            cnt_bits = cnt_bits - 8;
            if (cnt_bits > 0) {
              WORD32 unaligned_bits = (8 - it_bit_buff->cnt_bits);
              *ptr_sbr_data =
                  (WORD8)ixheaacd_read_bits_buf(it_bit_buff, cnt_bits);
              *ptr_sbr_data = *ptr_sbr_data << unaligned_bits;
              ptr_stream_sbr->size_payload++;
            }
          } else {
            if (it_bit_buff->cnt_bits > 0) {
              WORD32 unaligned_bits = (8 - it_bit_buff->cnt_bits);
              *ptr_sbr_data = (WORD8)ixheaacd_read_bits_buf(
                  it_bit_buff, it_bit_buff->cnt_bits);
              *ptr_sbr_data = *ptr_sbr_data << unaligned_bits;
              ptr_stream_sbr->size_payload++;
            }
          }
        }
      }

    } else if (extension_type == EXT_DYNAMIC_RANGE) {
      pstr_drc_dec->drc_element_found = 1;
      count -=
          ixheaacd_dec_drc_read_element(pstr_drc_dec, drc_dummy, it_bit_buff);
    } else {
      ixheaacd_read_bits_buf(it_bit_buff, 4);

      if (it_bit_buff->cnt_bits < ((count - 1) << 3)) {
        longjmp(*(it_bit_buff->xaac_jmp_buf),
                IA_ENHAACPLUS_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
      }
      it_bit_buff->ptr_read_next += count - 1;
      it_bit_buff->cnt_bits -= ((count - 1) << 3);
    }
  }
  return (ret);
}