summaryrefslogtreecommitdiffstats
path: root/media/libstagefright/codecs/on2/h264dec/source/H264SwDecApi.c
blob: f820dfd576cfa15ef6dde551433ee24ea2dd0719 (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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
/*
 * Copyright (C) 2009 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.
 */

/*------------------------------------------------------------------------------

    Table of contents

     1. Include headers
     2. External compiler flags
     3. Module defines
     4. Local function prototypes
     5. Functions
          H264SwDecInit
          H264SwDecGetInfo
          H264SwDecRelease
          H264SwDecDecode
          H264SwDecGetAPIVersion
          H264SwDecNextPicture

------------------------------------------------------------------------------*/

/*------------------------------------------------------------------------------
    1. Include headers
------------------------------------------------------------------------------*/
#include <log/log.h>

#include <stdlib.h>
#include <string.h>
#include "basetype.h"
#include "h264bsd_container.h"
#include "H264SwDecApi.h"
#include "h264bsd_decoder.h"
#include "h264bsd_util.h"

#define UNUSED(x) (void)(x)

/*------------------------------------------------------------------------------
       Version Information
------------------------------------------------------------------------------*/

#define H264SWDEC_MAJOR_VERSION 2
#define H264SWDEC_MINOR_VERSION 3

/*------------------------------------------------------------------------------
    2. External compiler flags
--------------------------------------------------------------------------------

H264DEC_TRACE           Trace H264 Decoder API function calls.
H264DEC_EVALUATION      Compile evaluation version, restricts number of frames
                        that can be decoded

--------------------------------------------------------------------------------
    3. Module defines
------------------------------------------------------------------------------*/

#ifdef H264DEC_TRACE
#include <stdio.h>
#define DEC_API_TRC(str)    H264SwDecTrace(str)
#else
#define DEC_API_TRC(str)
#endif

#ifdef H264DEC_EVALUATION
#define H264DEC_EVALUATION_LIMIT   500
#endif

void H264SwDecTrace(char *string) {
    UNUSED(string);
}

void* H264SwDecMalloc(u32 size, u32 num) {
    if (size > UINT32_MAX / num) {
        ALOGE("can't allocate %u * %u bytes", size, num);
        android_errorWriteLog(0x534e4554, "27855419");
        return NULL;
    }
    return malloc(size * num);
}

void H264SwDecFree(void *ptr) {
    free(ptr);
}

void H264SwDecMemcpy(void *dest, void *src, u32 count) {
    memcpy(dest, src, count);
}

void H264SwDecMemset(void *ptr, i32 value, u32 count) {
    memset(ptr, value, count);
}


/*------------------------------------------------------------------------------

    Function: H264SwDecInit()

        Functional description:
            Initialize decoder software. Function reserves memory for the
            decoder instance and calls h264bsdInit to initialize the
            instance data.

        Inputs:
            noOutputReordering  flag to indicate decoder that it doesn't have
                                to try to provide output pictures in display
                                order, saves memory

        Outputs:
            decInst             pointer to initialized instance is stored here

        Returns:
            H264SWDEC_OK        successfully initialized the instance
            H264SWDEC_INITFAIL  initialization failed
            H264SWDEC_PARAM_ERR invalid parameters
            H264SWDEC_MEM_FAIL  memory allocation failed

------------------------------------------------------------------------------*/

H264SwDecRet H264SwDecInit(H264SwDecInst *decInst, u32 noOutputReordering)
{
    u32 rv = 0;

    decContainer_t *pDecCont;

    DEC_API_TRC("H264SwDecInit#");

    /* check that right shift on negative numbers is performed signed */
    /*lint -save -e* following check causes multiple lint messages */
    if ( ((-1)>>1) != (-1) )
    {
        DEC_API_TRC("H264SwDecInit# ERROR: Right shift is not signed");
        return(H264SWDEC_INITFAIL);
    }
    /*lint -restore */

    if (decInst == NULL)
    {
        DEC_API_TRC("H264SwDecInit# ERROR: decInst == NULL");
        return(H264SWDEC_PARAM_ERR);
    }

    pDecCont = (decContainer_t *)H264SwDecMalloc(sizeof(decContainer_t), 1);

    if (pDecCont == NULL)
    {
        DEC_API_TRC("H264SwDecInit# ERROR: Memory allocation failed");
        return(H264SWDEC_MEMFAIL);
    }

#ifdef H264DEC_TRACE
    sprintf(pDecCont->str, "H264SwDecInit# decInst %p noOutputReordering %d",
            (void*)decInst, noOutputReordering);
    DEC_API_TRC(pDecCont->str);
#endif

    rv = h264bsdInit(&pDecCont->storage, noOutputReordering);
    if (rv != HANTRO_OK)
    {
        H264SwDecRelease(pDecCont);
        return(H264SWDEC_MEMFAIL);
    }

    pDecCont->decStat  = INITIALIZED;
    pDecCont->picNumber = 0;

#ifdef H264DEC_TRACE
    sprintf(pDecCont->str, "H264SwDecInit# OK: return %p", (void*)pDecCont);
    DEC_API_TRC(pDecCont->str);
#endif

    *decInst = (decContainer_t *)pDecCont;

    return(H264SWDEC_OK);

}

/*------------------------------------------------------------------------------

    Function: H264SwDecGetInfo()

        Functional description:
            This function provides read access to decoder information. This
            function should not be called before H264SwDecDecode function has
            indicated that headers are ready.

        Inputs:
            decInst     decoder instance

        Outputs:
            pDecInfo    pointer to info struct where data is written

        Returns:
            H264SWDEC_OK            success
            H264SWDEC_PARAM_ERR     invalid parameters
            H264SWDEC_HDRS_NOT_RDY  information not available yet

------------------------------------------------------------------------------*/

H264SwDecRet H264SwDecGetInfo(H264SwDecInst decInst, H264SwDecInfo *pDecInfo)
{

    storage_t *pStorage;

    DEC_API_TRC("H264SwDecGetInfo#");

    if (decInst == NULL || pDecInfo == NULL)
    {
        DEC_API_TRC("H264SwDecGetInfo# ERROR: decInst or pDecInfo is NULL");
        return(H264SWDEC_PARAM_ERR);
    }

    pStorage = &(((decContainer_t *)decInst)->storage);

    if (pStorage->activeSps == NULL || pStorage->activePps == NULL)
    {
        DEC_API_TRC("H264SwDecGetInfo# ERROR: Headers not decoded yet");
        return(H264SWDEC_HDRS_NOT_RDY);
    }

#ifdef H264DEC_TRACE
    sprintf(((decContainer_t*)decInst)->str,
        "H264SwDecGetInfo# decInst %p  pDecInfo %p", decInst, (void*)pDecInfo);
    DEC_API_TRC(((decContainer_t*)decInst)->str);
#endif

    /* h264bsdPicWidth and -Height return dimensions in macroblock units,
     * picWidth and -Height in pixels */
    pDecInfo->picWidth        = h264bsdPicWidth(pStorage) << 4;
    pDecInfo->picHeight       = h264bsdPicHeight(pStorage) << 4;
    pDecInfo->videoRange      = h264bsdVideoRange(pStorage);
    pDecInfo->matrixCoefficients = h264bsdMatrixCoefficients(pStorage);

    h264bsdCroppingParams(pStorage,
        &pDecInfo->croppingFlag,
        &pDecInfo->cropParams.cropLeftOffset,
        &pDecInfo->cropParams.cropOutWidth,
        &pDecInfo->cropParams.cropTopOffset,
        &pDecInfo->cropParams.cropOutHeight);

    /* sample aspect ratio */
    h264bsdSampleAspectRatio(pStorage,
                             &pDecInfo->parWidth,
                             &pDecInfo->parHeight);

    /* profile */
    pDecInfo->profile = h264bsdProfile(pStorage);

    DEC_API_TRC("H264SwDecGetInfo# OK");

    return(H264SWDEC_OK);

}

/*------------------------------------------------------------------------------

    Function: H264SwDecRelease()

        Functional description:
            Release the decoder instance. Function calls h264bsdShutDown to
            release instance data and frees the memory allocated for the
            instance.

        Inputs:
            decInst     Decoder instance

        Outputs:
            none

        Returns:
            none

------------------------------------------------------------------------------*/

void H264SwDecRelease(H264SwDecInst decInst)
{

    decContainer_t *pDecCont;

    DEC_API_TRC("H264SwDecRelease#");

    if (decInst == NULL)
    {
        DEC_API_TRC("H264SwDecRelease# ERROR: decInst == NULL");
        return;
    }

    pDecCont = (decContainer_t*)decInst;

#ifdef H264DEC_TRACE
    sprintf(pDecCont->str, "H264SwDecRelease# decInst %p",decInst);
    DEC_API_TRC(pDecCont->str);
#endif

    h264bsdShutdown(&pDecCont->storage);

    H264SwDecFree(pDecCont);

}

/*------------------------------------------------------------------------------

    Function: H264SwDecDecode

        Functional description:
            Decode stream data. Calls h264bsdDecode to do the actual decoding.

        Input:
            decInst     decoder instance
            pInput      pointer to input struct

        Outputs:
            pOutput     pointer to output struct

        Returns:
            H264SWDEC_NOT_INITIALIZED   decoder instance not initialized yet
            H264SWDEC_PARAM_ERR         invalid parameters

            H264SWDEC_STRM_PROCESSED    stream buffer decoded
            H264SWDEC_HDRS_RDY_BUFF_NOT_EMPTY   headers decoded,
                                                stream buffer not finished
            H264SWDEC_PIC_RDY                   decoding of a picture finished
            H264SWDEC_PIC_RDY_BUFF_NOT_EMPTY    decoding of a picture finished,
                                                stream buffer not finished
            H264SWDEC_STRM_ERR                  serious error in decoding, no
                                                valid parameter sets available
                                                to decode picture data
            H264SWDEC_EVALUATION_LIMIT_EXCEEDED this can only occur when
                                                evaluation version is used,
                                                max number of frames reached

------------------------------------------------------------------------------*/

H264SwDecRet H264SwDecDecode(H264SwDecInst decInst, H264SwDecInput *pInput,
                  H264SwDecOutput *pOutput)
{

    decContainer_t *pDecCont;
    u32 strmLen;
    u32 numReadBytes;
    u8 *tmpStream;
    u32 decResult = 0;
    H264SwDecRet returnValue = H264SWDEC_STRM_PROCESSED;

    DEC_API_TRC("H264SwDecDecode#");

    /* Check that function input parameters are valid */
    if (pInput == NULL || pOutput == NULL)
    {
        DEC_API_TRC("H264SwDecDecode# ERROR: pInput or pOutput is NULL");
        return(H264SWDEC_PARAM_ERR);
    }

    if ((pInput->pStream == NULL) || (pInput->dataLen == 0))
    {
        DEC_API_TRC("H264SwDecDecode# ERROR: Invalid input parameters");
        return(H264SWDEC_PARAM_ERR);
    }

    pDecCont = (decContainer_t *)decInst;

    /* Check if decoder is in an incorrect mode */
    if (decInst == NULL || pDecCont->decStat == UNINITIALIZED)
    {
        DEC_API_TRC("H264SwDecDecode# ERROR: Decoder not initialized");
        return(H264SWDEC_NOT_INITIALIZED);
    }

#ifdef H264DEC_EVALUATION
    if (pDecCont->picNumber >= H264DEC_EVALUATION_LIMIT)
        return(H264SWDEC_EVALUATION_LIMIT_EXCEEDED);
#endif

#ifdef H264DEC_TRACE
    sprintf(pDecCont->str, "H264SwDecDecode# decInst %p  pInput %p  pOutput %p",
            decInst, (void*)pInput, (void*)pOutput);
    DEC_API_TRC(pDecCont->str);
#endif

    pOutput->pStrmCurrPos   = NULL;

    numReadBytes = 0;
    strmLen = pInput->dataLen;
    tmpStream = pInput->pStream;
    pDecCont->storage.intraConcealmentFlag = pInput->intraConcealmentMethod;

    do
    {
        /* Return HDRS_RDY after DPB flush caused by new SPS */
        if (pDecCont->decStat == NEW_HEADERS)
        {
            decResult = H264BSD_HDRS_RDY;
            pDecCont->decStat = INITIALIZED;
        }
        else /* Continue decoding normally */
        {
            decResult = h264bsdDecode(&pDecCont->storage, tmpStream, strmLen,
                pInput->picId, &numReadBytes);
        }
        tmpStream += numReadBytes;
        /* check if too many bytes are read from stream */
        if ( (i32)(strmLen - numReadBytes) >= 0 )
            strmLen -= numReadBytes;
        else
            strmLen = 0;

        pOutput->pStrmCurrPos = tmpStream;

        switch (decResult)
        {
            case H264BSD_HDRS_RDY:

                if(pDecCont->storage.dpb->flushed &&
                   pDecCont->storage.dpb->numOut !=
                   pDecCont->storage.dpb->outIndex)
                {
                    /* output first all DPB stored pictures
                     * DPB flush caused by new SPS */
                    pDecCont->storage.dpb->flushed = 0;
                    pDecCont->decStat = NEW_HEADERS;
                    returnValue = H264SWDEC_PIC_RDY_BUFF_NOT_EMPTY;
                    strmLen = 0;
                }
                else
                {
                    returnValue = H264SWDEC_HDRS_RDY_BUFF_NOT_EMPTY;
                    strmLen = 0;
                }
                break;

            case H264BSD_PIC_RDY:
                pDecCont->picNumber++;

                if (strmLen == 0)
                    returnValue = H264SWDEC_PIC_RDY;
                else
                    returnValue = H264SWDEC_PIC_RDY_BUFF_NOT_EMPTY;

                strmLen = 0;
                break;

            case H264BSD_PARAM_SET_ERROR:
                if ( !h264bsdCheckValidParamSets(&pDecCont->storage) &&
                     strmLen == 0 )
                {
                    returnValue = H264SWDEC_STRM_ERR;
                }
                break;
            case H264BSD_MEMALLOC_ERROR:
                {
                    returnValue = H264SWDEC_MEMFAIL;
                    strmLen = 0;
                }
                break;
            default:
                break;
        }

    } while (strmLen);

#ifdef H264DEC_TRACE
    sprintf(pDecCont->str, "H264SwDecDecode# OK: DecResult %d",
            returnValue);
    DEC_API_TRC(pDecCont->str);
#endif

    return(returnValue);

}

/*------------------------------------------------------------------------------

    Function: H264SwDecGetAPIVersion

        Functional description:
            Return version information of the API

        Inputs:
            none

        Outputs:
            none

        Returns:
            API version

------------------------------------------------------------------------------*/

H264SwDecApiVersion H264SwDecGetAPIVersion()
{
    H264SwDecApiVersion ver;

    ver.major = H264SWDEC_MAJOR_VERSION;
    ver.minor = H264SWDEC_MINOR_VERSION;

    return(ver);
}

/*------------------------------------------------------------------------------

    Function: H264SwDecNextPicture

        Functional description:
            Get next picture in display order if any available.

        Input:
            decInst     decoder instance.
            flushBuffer force output of all buffered pictures

        Output:
            pOutput     pointer to output structure

        Returns:
            H264SWDEC_OK            no pictures available for display
            H264SWDEC_PIC_RDY       picture available for display
            H264SWDEC_PARAM_ERR     invalid parameters

------------------------------------------------------------------------------*/

H264SwDecRet H264SwDecNextPicture(H264SwDecInst decInst,
    H264SwDecPicture *pOutput, u32 flushBuffer)
{

    decContainer_t *pDecCont;
    u32 numErrMbs, isIdrPic, picId;
    u32 *pOutPic;

    DEC_API_TRC("H264SwDecNextPicture#");

    if (decInst == NULL || pOutput == NULL)
    {
        DEC_API_TRC("H264SwDecNextPicture# ERROR: decInst or pOutput is NULL");
        return(H264SWDEC_PARAM_ERR);
    }

    pDecCont = (decContainer_t*)decInst;

#ifdef H264DEC_TRACE
    sprintf(pDecCont->str, "H264SwDecNextPicture# decInst %p pOutput %p %s %d",
            decInst, (void*)pOutput, "flushBuffer", flushBuffer);
    DEC_API_TRC(pDecCont->str);
#endif

    if (flushBuffer)
        h264bsdFlushBuffer(&pDecCont->storage);

    pOutPic = (u32*)h264bsdNextOutputPicture(&pDecCont->storage, &picId,
                                             &isIdrPic, &numErrMbs);

    if (pOutPic == NULL)
    {
        DEC_API_TRC("H264SwDecNextPicture# OK: return H264SWDEC_OK");
        return(H264SWDEC_OK);
    }
    else
    {
        pOutput->pOutputPicture = pOutPic;
        pOutput->picId          = picId;
        pOutput->isIdrPicture   = isIdrPic;
        pOutput->nbrOfErrMBs    = numErrMbs;
        DEC_API_TRC("H264SwDecNextPicture# OK: return H264SWDEC_PIC_RDY");
        return(H264SWDEC_PIC_RDY);
    }

}