10c1bc742181ded4930842b46e9507372f0b1b963James Dong/*
20c1bc742181ded4930842b46e9507372f0b1b963James Dong * Copyright (C) 2009 The Android Open Source Project
30c1bc742181ded4930842b46e9507372f0b1b963James Dong *
40c1bc742181ded4930842b46e9507372f0b1b963James Dong * Licensed under the Apache License, Version 2.0 (the "License");
50c1bc742181ded4930842b46e9507372f0b1b963James Dong * you may not use this file except in compliance with the License.
60c1bc742181ded4930842b46e9507372f0b1b963James Dong * You may obtain a copy of the License at
70c1bc742181ded4930842b46e9507372f0b1b963James Dong *
80c1bc742181ded4930842b46e9507372f0b1b963James Dong *      http://www.apache.org/licenses/LICENSE-2.0
90c1bc742181ded4930842b46e9507372f0b1b963James Dong *
100c1bc742181ded4930842b46e9507372f0b1b963James Dong * Unless required by applicable law or agreed to in writing, software
110c1bc742181ded4930842b46e9507372f0b1b963James Dong * distributed under the License is distributed on an "AS IS" BASIS,
120c1bc742181ded4930842b46e9507372f0b1b963James Dong * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
130c1bc742181ded4930842b46e9507372f0b1b963James Dong * See the License for the specific language governing permissions and
140c1bc742181ded4930842b46e9507372f0b1b963James Dong * limitations under the License.
150c1bc742181ded4930842b46e9507372f0b1b963James Dong */
160c1bc742181ded4930842b46e9507372f0b1b963James Dong
170c1bc742181ded4930842b46e9507372f0b1b963James Dong/*------------------------------------------------------------------------------
180c1bc742181ded4930842b46e9507372f0b1b963James Dong
190c1bc742181ded4930842b46e9507372f0b1b963James Dong    Table of contents
200c1bc742181ded4930842b46e9507372f0b1b963James Dong
210c1bc742181ded4930842b46e9507372f0b1b963James Dong    1. Include headers
220c1bc742181ded4930842b46e9507372f0b1b963James Dong    2. Module defines
230c1bc742181ded4930842b46e9507372f0b1b963James Dong    3. Data types
240c1bc742181ded4930842b46e9507372f0b1b963James Dong    4. Function prototypes
250c1bc742181ded4930842b46e9507372f0b1b963James Dong
260c1bc742181ded4930842b46e9507372f0b1b963James Dong------------------------------------------------------------------------------*/
270c1bc742181ded4930842b46e9507372f0b1b963James Dong
280c1bc742181ded4930842b46e9507372f0b1b963James Dong#ifndef H264SWDEC_UTIL_H
290c1bc742181ded4930842b46e9507372f0b1b963James Dong#define H264SWDEC_UTIL_H
300c1bc742181ded4930842b46e9507372f0b1b963James Dong
310c1bc742181ded4930842b46e9507372f0b1b963James Dong/*------------------------------------------------------------------------------
320c1bc742181ded4930842b46e9507372f0b1b963James Dong    1. Include headers
330c1bc742181ded4930842b46e9507372f0b1b963James Dong------------------------------------------------------------------------------*/
340c1bc742181ded4930842b46e9507372f0b1b963James Dong
350c1bc742181ded4930842b46e9507372f0b1b963James Dong#ifdef _ASSERT_USED
360c1bc742181ded4930842b46e9507372f0b1b963James Dong#include <assert.h>
370c1bc742181ded4930842b46e9507372f0b1b963James Dong#endif
380c1bc742181ded4930842b46e9507372f0b1b963James Dong
390c1bc742181ded4930842b46e9507372f0b1b963James Dong#include "H264SwDecApi.h"
400c1bc742181ded4930842b46e9507372f0b1b963James Dong
410c1bc742181ded4930842b46e9507372f0b1b963James Dong#if defined(_RANGE_CHECK) || defined(_DEBUG_PRINT) || defined(_ERROR_PRINT)
420c1bc742181ded4930842b46e9507372f0b1b963James Dong#include <stdio.h>
430c1bc742181ded4930842b46e9507372f0b1b963James Dong#endif
440c1bc742181ded4930842b46e9507372f0b1b963James Dong
45377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETIT#include <stdint.h>
460c1bc742181ded4930842b46e9507372f0b1b963James Dong#include "basetype.h"
470c1bc742181ded4930842b46e9507372f0b1b963James Dong#include "h264bsd_stream.h"
480c1bc742181ded4930842b46e9507372f0b1b963James Dong#include "h264bsd_image.h"
490c1bc742181ded4930842b46e9507372f0b1b963James Dong
500c1bc742181ded4930842b46e9507372f0b1b963James Dong/*------------------------------------------------------------------------------
510c1bc742181ded4930842b46e9507372f0b1b963James Dong    2. Module defines
520c1bc742181ded4930842b46e9507372f0b1b963James Dong------------------------------------------------------------------------------*/
530c1bc742181ded4930842b46e9507372f0b1b963James Dong
540c1bc742181ded4930842b46e9507372f0b1b963James Dong#define HANTRO_OK   0
550c1bc742181ded4930842b46e9507372f0b1b963James Dong#define HANTRO_NOK  1
560c1bc742181ded4930842b46e9507372f0b1b963James Dong
570c1bc742181ded4930842b46e9507372f0b1b963James Dong#define HANTRO_TRUE     (1)
580c1bc742181ded4930842b46e9507372f0b1b963James Dong#define HANTRO_FALSE    (0)
590c1bc742181ded4930842b46e9507372f0b1b963James Dong
600c1bc742181ded4930842b46e9507372f0b1b963James Dong#ifndef NULL
610c1bc742181ded4930842b46e9507372f0b1b963James Dong#define NULL 0
620c1bc742181ded4930842b46e9507372f0b1b963James Dong#endif
630c1bc742181ded4930842b46e9507372f0b1b963James Dong
640c1bc742181ded4930842b46e9507372f0b1b963James Dong#define MEMORY_ALLOCATION_ERROR 0xFFFF
650c1bc742181ded4930842b46e9507372f0b1b963James Dong#define PARAM_SET_ERROR 0xFFF0
660c1bc742181ded4930842b46e9507372f0b1b963James Dong
670c1bc742181ded4930842b46e9507372f0b1b963James Dong/* value to be returned by GetBits if stream buffer is empty */
680c1bc742181ded4930842b46e9507372f0b1b963James Dong#define END_OF_STREAM 0xFFFFFFFFU
690c1bc742181ded4930842b46e9507372f0b1b963James Dong
700c1bc742181ded4930842b46e9507372f0b1b963James Dong#define EMPTY_RESIDUAL_INDICATOR 0xFFFFFF
710c1bc742181ded4930842b46e9507372f0b1b963James Dong
720c1bc742181ded4930842b46e9507372f0b1b963James Dong/* macro to mark a residual block empty, i.e. contain zero coefficients */
730c1bc742181ded4930842b46e9507372f0b1b963James Dong#define MARK_RESIDUAL_EMPTY(residual) ((residual)[0] = EMPTY_RESIDUAL_INDICATOR)
740c1bc742181ded4930842b46e9507372f0b1b963James Dong/* macro to check if residual block is empty */
750c1bc742181ded4930842b46e9507372f0b1b963James Dong#define IS_RESIDUAL_EMPTY(residual) ((residual)[0] == EMPTY_RESIDUAL_INDICATOR)
760c1bc742181ded4930842b46e9507372f0b1b963James Dong
770c1bc742181ded4930842b46e9507372f0b1b963James Dong/* macro for assertion, used only if compiler flag _ASSERT_USED is defined */
780c1bc742181ded4930842b46e9507372f0b1b963James Dong#ifdef _ASSERT_USED
790c1bc742181ded4930842b46e9507372f0b1b963James Dong#define ASSERT(expr) assert(expr)
800c1bc742181ded4930842b46e9507372f0b1b963James Dong#else
810c1bc742181ded4930842b46e9507372f0b1b963James Dong#define ASSERT(expr)
820c1bc742181ded4930842b46e9507372f0b1b963James Dong#endif
830c1bc742181ded4930842b46e9507372f0b1b963James Dong
840c1bc742181ded4930842b46e9507372f0b1b963James Dong/* macro for range checking an value, used only if compiler flag _RANGE_CHECK
850c1bc742181ded4930842b46e9507372f0b1b963James Dong * is defined */
860c1bc742181ded4930842b46e9507372f0b1b963James Dong#ifdef _RANGE_CHECK
870c1bc742181ded4930842b46e9507372f0b1b963James Dong#define RANGE_CHECK(value, minBound, maxBound) \
880c1bc742181ded4930842b46e9507372f0b1b963James Dong{ \
890c1bc742181ded4930842b46e9507372f0b1b963James Dong    if ((value) < (minBound) || (value) > (maxBound)) \
900c1bc742181ded4930842b46e9507372f0b1b963James Dong        fprintf(stderr, "Warning: Value exceeds given limit(s)!\n"); \
910c1bc742181ded4930842b46e9507372f0b1b963James Dong}
920c1bc742181ded4930842b46e9507372f0b1b963James Dong#else
930c1bc742181ded4930842b46e9507372f0b1b963James Dong#define RANGE_CHECK(value, minBound, maxBound)
940c1bc742181ded4930842b46e9507372f0b1b963James Dong#endif
950c1bc742181ded4930842b46e9507372f0b1b963James Dong
960c1bc742181ded4930842b46e9507372f0b1b963James Dong/* macro for range checking an array, used only if compiler flag _RANGE_CHECK
970c1bc742181ded4930842b46e9507372f0b1b963James Dong * is defined */
980c1bc742181ded4930842b46e9507372f0b1b963James Dong#ifdef _RANGE_CHECK
990c1bc742181ded4930842b46e9507372f0b1b963James Dong#define RANGE_CHECK_ARRAY(array, minBound, maxBound, length) \
1000c1bc742181ded4930842b46e9507372f0b1b963James Dong{ \
1010c1bc742181ded4930842b46e9507372f0b1b963James Dong    i32 i; \
1020c1bc742181ded4930842b46e9507372f0b1b963James Dong    for (i = 0; i < (length); i++) \
1030c1bc742181ded4930842b46e9507372f0b1b963James Dong        if ((array)[i] < (minBound) || (array)[i] > (maxBound)) \
1040c1bc742181ded4930842b46e9507372f0b1b963James Dong            fprintf(stderr,"Warning: Value [%d] exceeds given limit(s)!\n",i); \
1050c1bc742181ded4930842b46e9507372f0b1b963James Dong}
1060c1bc742181ded4930842b46e9507372f0b1b963James Dong#else
1070c1bc742181ded4930842b46e9507372f0b1b963James Dong#define RANGE_CHECK_ARRAY(array, minBound, maxBound, length)
1080c1bc742181ded4930842b46e9507372f0b1b963James Dong#endif
1090c1bc742181ded4930842b46e9507372f0b1b963James Dong
1100c1bc742181ded4930842b46e9507372f0b1b963James Dong/* macro for debug printing, used only if compiler flag _DEBUG_PRINT is
1110c1bc742181ded4930842b46e9507372f0b1b963James Dong * defined */
1120c1bc742181ded4930842b46e9507372f0b1b963James Dong#ifdef _DEBUG_PRINT
1130c1bc742181ded4930842b46e9507372f0b1b963James Dong#define DEBUG(args) printf args
1140c1bc742181ded4930842b46e9507372f0b1b963James Dong#else
1150c1bc742181ded4930842b46e9507372f0b1b963James Dong#define DEBUG(args)
1160c1bc742181ded4930842b46e9507372f0b1b963James Dong#endif
1170c1bc742181ded4930842b46e9507372f0b1b963James Dong
1180c1bc742181ded4930842b46e9507372f0b1b963James Dong/* macro for error printing, used only if compiler flag _ERROR_PRINT is
1190c1bc742181ded4930842b46e9507372f0b1b963James Dong * defined */
1200c1bc742181ded4930842b46e9507372f0b1b963James Dong#ifdef _ERROR_PRINT
1210c1bc742181ded4930842b46e9507372f0b1b963James Dong#define EPRINT(msg) fprintf(stderr,"ERROR: %s\n",msg)
1220c1bc742181ded4930842b46e9507372f0b1b963James Dong#else
1230c1bc742181ded4930842b46e9507372f0b1b963James Dong#define EPRINT(msg)
1240c1bc742181ded4930842b46e9507372f0b1b963James Dong#endif
1250c1bc742181ded4930842b46e9507372f0b1b963James Dong
1260c1bc742181ded4930842b46e9507372f0b1b963James Dong/* macro to get smaller of two values */
1270c1bc742181ded4930842b46e9507372f0b1b963James Dong#define MIN(a, b) (((a) < (b)) ? (a) : (b))
1280c1bc742181ded4930842b46e9507372f0b1b963James Dong
1290c1bc742181ded4930842b46e9507372f0b1b963James Dong/* macro to get greater of two values */
1300c1bc742181ded4930842b46e9507372f0b1b963James Dong#define MAX(a, b) (((a) > (b)) ? (a) : (b))
1310c1bc742181ded4930842b46e9507372f0b1b963James Dong
1320c1bc742181ded4930842b46e9507372f0b1b963James Dong/* macro to get absolute value */
1330c1bc742181ded4930842b46e9507372f0b1b963James Dong#define ABS(a) (((a) < 0) ? -(a) : (a))
1340c1bc742181ded4930842b46e9507372f0b1b963James Dong
1350c1bc742181ded4930842b46e9507372f0b1b963James Dong/* macro to clip a value z, so that x <= z =< y */
1360c1bc742181ded4930842b46e9507372f0b1b963James Dong#define CLIP3(x,y,z) (((z) < (x)) ? (x) : (((z) > (y)) ? (y) : (z)))
1370c1bc742181ded4930842b46e9507372f0b1b963James Dong
1380c1bc742181ded4930842b46e9507372f0b1b963James Dong/* macro to clip a value z, so that 0 <= z =< 255 */
1390c1bc742181ded4930842b46e9507372f0b1b963James Dong#define CLIP1(z) (((z) < 0) ? 0 : (((z) > 255) ? 255 : (z)))
1400c1bc742181ded4930842b46e9507372f0b1b963James Dong
1410c1bc742181ded4930842b46e9507372f0b1b963James Dong/* macro to allocate memory */
1420c1bc742181ded4930842b46e9507372f0b1b963James Dong#define ALLOCATE(ptr, count, type) \
1430c1bc742181ded4930842b46e9507372f0b1b963James Dong{ \
144a3dd713893658baf50df88b261aba83bb0c40687Marco Nelissen    (ptr) = H264SwDecMalloc(sizeof(type), (count)); \
1450c1bc742181ded4930842b46e9507372f0b1b963James Dong}
1460c1bc742181ded4930842b46e9507372f0b1b963James Dong
1470c1bc742181ded4930842b46e9507372f0b1b963James Dong/* macro to free allocated memory */
1480c1bc742181ded4930842b46e9507372f0b1b963James Dong#define FREE(ptr) \
1490c1bc742181ded4930842b46e9507372f0b1b963James Dong{ \
1500c1bc742181ded4930842b46e9507372f0b1b963James Dong    H264SwDecFree((ptr)); (ptr) = NULL; \
1510c1bc742181ded4930842b46e9507372f0b1b963James Dong}
1520c1bc742181ded4930842b46e9507372f0b1b963James Dong
1530c1bc742181ded4930842b46e9507372f0b1b963James Dong#define ALIGN(ptr, bytePos) \
154377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETIT        (ptr + ( ((bytePos - (uintptr_t)ptr) & (bytePos - 1)) / sizeof(*ptr) ))
1550c1bc742181ded4930842b46e9507372f0b1b963James Dong
1560c1bc742181ded4930842b46e9507372f0b1b963James Dongextern const u32 h264bsdQpC[52];
1570c1bc742181ded4930842b46e9507372f0b1b963James Dong
1580c1bc742181ded4930842b46e9507372f0b1b963James Dong/*------------------------------------------------------------------------------
1590c1bc742181ded4930842b46e9507372f0b1b963James Dong    3. Data types
1600c1bc742181ded4930842b46e9507372f0b1b963James Dong------------------------------------------------------------------------------*/
1610c1bc742181ded4930842b46e9507372f0b1b963James Dong
1620c1bc742181ded4930842b46e9507372f0b1b963James Dong/*------------------------------------------------------------------------------
1630c1bc742181ded4930842b46e9507372f0b1b963James Dong    4. Function prototypes
1640c1bc742181ded4930842b46e9507372f0b1b963James Dong------------------------------------------------------------------------------*/
1650c1bc742181ded4930842b46e9507372f0b1b963James Dong#ifndef H264DEC_NEON
1660c1bc742181ded4930842b46e9507372f0b1b963James Dongu32 h264bsdCountLeadingZeros(u32 value, u32 length);
1670c1bc742181ded4930842b46e9507372f0b1b963James Dong#else
1680c1bc742181ded4930842b46e9507372f0b1b963James Dongu32 h264bsdCountLeadingZeros(u32 value);
1690c1bc742181ded4930842b46e9507372f0b1b963James Dong#endif
1700c1bc742181ded4930842b46e9507372f0b1b963James Dongu32 h264bsdRbspTrailingBits(strmData_t *strmData);
1710c1bc742181ded4930842b46e9507372f0b1b963James Dong
1720c1bc742181ded4930842b46e9507372f0b1b963James Dongu32 h264bsdMoreRbspData(strmData_t *strmData);
1730c1bc742181ded4930842b46e9507372f0b1b963James Dong
1740c1bc742181ded4930842b46e9507372f0b1b963James Dongu32 h264bsdNextMbAddress(u32 *pSliceGroupMap, u32 picSizeInMbs, u32 currMbAddr);
1750c1bc742181ded4930842b46e9507372f0b1b963James Dong
1760c1bc742181ded4930842b46e9507372f0b1b963James Dongvoid h264bsdSetCurrImageMbPointers(image_t *image, u32 mbNum);
1770c1bc742181ded4930842b46e9507372f0b1b963James Dong
1780c1bc742181ded4930842b46e9507372f0b1b963James Dong#endif /* #ifdef H264SWDEC_UTIL_H */
1790c1bc742181ded4930842b46e9507372f0b1b963James Dong
180