aboutsummaryrefslogtreecommitdiffstats
path: root/libMpegTPDec/src/tpdec_latm.cpp
blob: 28daebc697e39fad4a0f67528e4f9cd1a57cfec4 (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*****************************  MPEG-4 AAC Decoder  **************************

                        (C) Copyright Fraunhofer IIS (2004)
                               All Rights Reserved

    Please be advised that this software and/or program delivery is
    Confidential Information of Fraunhofer and subject to and covered by the

    Fraunhofer IIS Software Evaluation Agreement
    between Google Inc. and  Fraunhofer
    effective and in full force since March 1, 2012.

    You may use this software and/or program only under the terms and
    conditions described in the above mentioned Fraunhofer IIS Software
    Evaluation Agreement. Any other and/or further use requires a separate agreement.


   $Id$
   Author(s):   Daniel Homm
   Description:

   This software and/or program is protected by copyright law and international
   treaties. Any reproduction or distribution of this software and/or program,
   or any portion of it, may result in severe civil and criminal penalties, and
   will be prosecuted to the maximum extent possible under law.

******************************************************************************/

#include "tpdec_latm.h"


#include "FDK_bitstream.h"


#define TPDEC_TRACKINDEX(p,l) (2*(p) + (l))

static
UINT CLatmDemux_GetValue(HANDLE_FDK_BITSTREAM bs)
{
  UCHAR bytesForValue = 0, tmp = 0;
  int value = 0;

  bytesForValue = (UCHAR) FDKreadBits(bs,2);

  for (UINT i=0; i<=bytesForValue; i++) {
    value <<= 8;
    tmp = (UCHAR) FDKreadBits(bs,8);
    value += tmp;
  }

  return value;
}


static
TRANSPORTDEC_ERROR CLatmDemux_ReadAudioMuxElement(
        HANDLE_FDK_BITSTREAM bs,
        CLatmDemux *pLatmDemux,
        int m_muxConfigPresent,
        CSTpCallBacks *pTpDecCallbacks,
        CSAudioSpecificConfig *pAsc
        )
{
  TRANSPORTDEC_ERROR ErrorStatus = TRANSPORTDEC_OK;

  if (m_muxConfigPresent) {
    pLatmDemux->m_useSameStreamMux = FDKreadBits(bs,1);

    if (!pLatmDemux->m_useSameStreamMux) {
      if ((ErrorStatus = CLatmDemux_ReadStreamMuxConfig(bs, pLatmDemux, pTpDecCallbacks, pAsc))) {
        return (ErrorStatus);
      }
    }
  }

  if (pLatmDemux->m_AudioMuxVersionA == 0) {
    /* Do only once per call, because parsing and decoding is done in-line. */
    if ((ErrorStatus = CLatmDemux_ReadPayloadLengthInfo(bs,pLatmDemux))) {
      return (ErrorStatus);
    }
  } else {
    /* audioMuxVersionA > 0 is reserved for future extensions */
    ErrorStatus = TRANSPORTDEC_UNSUPPORTED_FORMAT;
  }

  return (ErrorStatus);
}

TRANSPORTDEC_ERROR CLatmDemux_Read(
        HANDLE_FDK_BITSTREAM bs,
        CLatmDemux *pLatmDemux,
        TRANSPORT_TYPE tt,
        CSTpCallBacks *pTpDecCallbacks,
        CSAudioSpecificConfig *pAsc,
        const INT ignoreBufferFullness
        )
{
  UINT cntBits;
  UINT cmpBufferFullness;
  UINT audioMuxLengthBytesLast = 0;
  TRANSPORTDEC_ERROR ErrorStatus = TRANSPORTDEC_OK;

  cntBits = FDKgetValidBits(bs);

  if ((INT)cntBits < MIN_LATM_HEADERLENGTH) {
    return TRANSPORTDEC_NOT_ENOUGH_BITS;
  }

  if ((ErrorStatus = CLatmDemux_ReadAudioMuxElement(bs, pLatmDemux, (tt != TT_MP4_LATM_MCP0), pTpDecCallbacks, pAsc)))
    return (ErrorStatus);

  if (!ignoreBufferFullness)
  {
    cmpBufferFullness =   24+audioMuxLengthBytesLast*8
                        + pLatmDemux->m_linfo[0][0].m_bufferFullness* pAsc[TPDEC_TRACKINDEX(0,0)].m_channelConfiguration*32;

    /* evaluate buffer fullness */

    if (pLatmDemux->m_linfo[0][0].m_bufferFullness != 0xFF)
    {
      if (!pLatmDemux->BufferFullnessAchieved)
      {
        if (cntBits < cmpBufferFullness)
        {
          /* condition for start of decoding is not fulfilled */

          /* the current frame will not be decoded */
          return TRANSPORTDEC_NOT_ENOUGH_BITS;
        }
        else
        {
          pLatmDemux->BufferFullnessAchieved = 1;
        }
      }
    }
  }

  return (ErrorStatus);
}


TRANSPORTDEC_ERROR CLatmDemux_ReadStreamMuxConfig(
        HANDLE_FDK_BITSTREAM bs,
        CLatmDemux *pLatmDemux,
        CSTpCallBacks *pTpDecCallbacks,
        CSAudioSpecificConfig *pAsc
        )
{
  LATM_LAYER_INFO *p_linfo = NULL;
  TRANSPORTDEC_ERROR ErrorStatus = TRANSPORTDEC_OK;

  pLatmDemux->m_AudioMuxVersion = FDKreadBits(bs,1);

  if (pLatmDemux->m_AudioMuxVersion == 0) {
    pLatmDemux->m_AudioMuxVersionA = 0;
  } else {
    pLatmDemux->m_AudioMuxVersionA = FDKreadBits(bs,1);
  }

  if (pLatmDemux->m_AudioMuxVersionA == 0) {
    if (pLatmDemux->m_AudioMuxVersion == 1) {
      pLatmDemux->m_taraBufferFullness = CLatmDemux_GetValue(bs);
    }
    pLatmDemux->m_allStreamsSameTimeFraming = FDKreadBits(bs,1);
    pLatmDemux->m_noSubFrames = FDKreadBits(bs,6) + 1;
    pLatmDemux->m_numProgram  = FDKreadBits(bs,4) + 1;

    if (pLatmDemux->m_numProgram > 1) {
      return TRANSPORTDEC_UNSUPPORTED_FORMAT;
    }

    int idCnt = 0;
    for (UINT prog = 0; prog < pLatmDemux->m_numProgram; prog++) {
      pLatmDemux->m_numLayer = FDKreadBits(bs,3) + 1;
      if (pLatmDemux->m_numLayer > 2) {
        return TRANSPORTDEC_UNSUPPORTED_FORMAT;
      }

      for (UINT lay = 0; lay < pLatmDemux->m_numLayer; lay++) {
        p_linfo = &pLatmDemux->m_linfo[prog][lay];

        p_linfo->m_streamID = idCnt++;
        p_linfo->m_frameLengthInBits = 0;

        if( (prog == 0) && (lay == 0) ) {
          pLatmDemux->m_useSameConfig = 0;
        } else {
          pLatmDemux->m_useSameConfig = FDKreadBits(bs,1);
        }

        if (pLatmDemux->m_useSameConfig) {
          if (lay > 1) {
            FDKmemcpy(&pAsc[TPDEC_TRACKINDEX(prog,lay)], &pAsc[TPDEC_TRACKINDEX(prog,lay-1)], sizeof(CSAudioSpecificConfig));
          } else {
            return TRANSPORTDEC_PARSE_ERROR;
          }
        } else {
          if (pLatmDemux->m_AudioMuxVersion == 1)
          {
            FDK_BITSTREAM tmpBs;
            UINT ascStartPos, ascLen=0;

            ascLen = CLatmDemux_GetValue(bs);
            ascStartPos = FDKgetValidBits(bs);
            tmpBs = *bs;
            FDKsyncCache(&tmpBs);
            tmpBs.hBitBuf.ValidBits = ascLen;
            
            /* Read ASC */
            if ((ErrorStatus = AudioSpecificConfig_Parse(&pAsc[TPDEC_TRACKINDEX(prog,lay)], &tmpBs, 1, pTpDecCallbacks))) {
              return (ErrorStatus);
            }

            /* The field p_linfo->m_ascLen could be wrong, so check if */
            if ( 0 > (INT)FDKgetValidBits(&tmpBs)) {
              return TRANSPORTDEC_PARSE_ERROR;
            }
            FDKpushFor(bs, ascLen); /* position bitstream after ASC */
          }
          else {
            /* Read ASC */
            if ((ErrorStatus = AudioSpecificConfig_Parse(&pAsc[TPDEC_TRACKINDEX(prog,lay)], bs, 0, pTpDecCallbacks))) {
              return (ErrorStatus);
            }
          }
          {
            int cbError;

            cbError = pTpDecCallbacks->cbUpdateConfig(pTpDecCallbacks->cbUpdateConfigData, &pAsc[TPDEC_TRACKINDEX(prog,lay)]);
            if (cbError != 0) {
              return TRANSPORTDEC_UNKOWN_ERROR;
            }
          }
        }

        p_linfo->m_frameLengthType = FDKreadBits(bs,3);
        switch( p_linfo->m_frameLengthType ) {
        case 0:
          p_linfo->m_bufferFullness = FDKreadBits(bs,8);

          if (!pLatmDemux->m_allStreamsSameTimeFraming) {
            if ((lay > 0) && (pAsc[TPDEC_TRACKINDEX(prog,lay)].m_aot == AOT_AAC_SCAL || pAsc[TPDEC_TRACKINDEX(prog,lay)].m_aot == AOT_ER_AAC_SCAL)) {
              return TRANSPORTDEC_UNSUPPORTED_FORMAT;
            }
          }
          break;
        case 1:
          /* frameLength = FDKreadBits(bs,9); */
        case 3:
        case 4:
        case 5:
          /* CELP */
        case 6:
        case 7:
          /* HVXC */
        default:
          return TRANSPORTDEC_PARSE_ERROR; //_LATM_INVALIDFRAMELENGTHTYPE;

        }  /* switch framelengthtype*/

      }  /* layer loop */
    }  /* prog loop */

    pLatmDemux->m_otherDataPresent = FDKreadBits(bs,1);
    pLatmDemux->m_otherDataLength  = 0;

    if (pLatmDemux->m_otherDataPresent) {
      int otherDataLenEsc  = 0;
      do {
        pLatmDemux->m_otherDataLength <<= 8;   // *= 256
        otherDataLenEsc = FDKreadBits(bs,1);
        pLatmDemux->m_otherDataLength += FDKreadBits(bs,8);
      } while (otherDataLenEsc);
    }

    pLatmDemux->m_crcCheckPresent = FDKreadBits(bs,1);
    pLatmDemux->m_crcCheckSum     = 0;

    if (pLatmDemux->m_crcCheckPresent) {
      pLatmDemux->m_crcCheckSum = FDKreadBits(bs,8);
    }

  }
  else {
    /* audioMuxVersionA > 0 is reserved for future extensions */
    ErrorStatus = TRANSPORTDEC_UNSUPPORTED_FORMAT;
  }
  return (ErrorStatus);
}

TRANSPORTDEC_ERROR CLatmDemux_ReadPayloadLengthInfo(HANDLE_FDK_BITSTREAM bs, CLatmDemux *pLatmDemux)
{
  TRANSPORTDEC_ERROR ErrorStatus = TRANSPORTDEC_OK;
  int totalPayloadBits = 0;

  if( pLatmDemux->m_allStreamsSameTimeFraming == 1 ) {
    for (UINT prog=0; prog<pLatmDemux->m_numProgram; prog++ ) {
      for (UINT lay=0; lay<pLatmDemux->m_numLayer; lay++ ) {
        LATM_LAYER_INFO *p_linfo = &pLatmDemux->m_linfo[prog][lay];

        switch (p_linfo->m_frameLengthType ) {
        case 0:
          p_linfo->m_frameLengthInBits = CLatmDemux_ReadAuChunkLengthInfo(bs);
          totalPayloadBits += p_linfo->m_frameLengthInBits;
          break;
        case 3:
        case 5:
        case 7:
        default:
          return TRANSPORTDEC_PARSE_ERROR; //AAC_DEC_LATM_INVALIDFRAMELENGTHTYPE;
        }
      }
    }
  }
  else {
    ErrorStatus = TRANSPORTDEC_PARSE_ERROR; //AAC_DEC_LATM_TIMEFRAMING;
  }
  if ((INT)FDKgetValidBits(bs) < totalPayloadBits) {
    return TRANSPORTDEC_NOT_ENOUGH_BITS;
  }
  if (pLatmDemux->m_audioMuxLengthBytes > 0 && totalPayloadBits > pLatmDemux->m_audioMuxLengthBytes*8) {
    return TRANSPORTDEC_PARSE_ERROR;
  }
  return (ErrorStatus);
}

int CLatmDemux_ReadAuChunkLengthInfo(HANDLE_FDK_BITSTREAM bs)
{
  UCHAR endFlag;
  int len = 0;

  do {
    UCHAR tmp = (UCHAR) FDKreadBits(bs,8);
    endFlag = (tmp < 255);

    len += tmp;

  } while( endFlag == 0 );

  len <<= 3;  /* convert from bytes to bits */

  return len;
}

int CLatmDemux_GetFrameLengthInBits(CLatmDemux *pLatmDemux)
{
  return pLatmDemux->m_linfo[0][0].m_frameLengthInBits;
}

int CLatmDemux_GetOtherDataPresentFlag(CLatmDemux *pLatmDemux)
{
  return pLatmDemux->m_otherDataPresent ? 1 : 0;
}

int CLatmDemux_GetOtherDataLength(CLatmDemux *pLatmDemux)
{
  return pLatmDemux->m_otherDataLength;
}

UINT CLatmDemux_GetNrOfSubFrames(CLatmDemux *pLatmDemux)
{
  return pLatmDemux->m_noSubFrames;
}