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
450c1bc742181ded4930842b46e9507372f0b1b963James Dong#include "basetype.h"
460c1bc742181ded4930842b46e9507372f0b1b963James Dong#include "h264bsd_stream.h"
470c1bc742181ded4930842b46e9507372f0b1b963James Dong#include "h264bsd_image.h"
480c1bc742181ded4930842b46e9507372f0b1b963James Dong
490c1bc742181ded4930842b46e9507372f0b1b963James Dong/*------------------------------------------------------------------------------
500c1bc742181ded4930842b46e9507372f0b1b963James Dong    2. Module defines
510c1bc742181ded4930842b46e9507372f0b1b963James Dong------------------------------------------------------------------------------*/
520c1bc742181ded4930842b46e9507372f0b1b963James Dong
530c1bc742181ded4930842b46e9507372f0b1b963James Dong#define HANTRO_OK   0
540c1bc742181ded4930842b46e9507372f0b1b963James Dong#define HANTRO_NOK  1
550c1bc742181ded4930842b46e9507372f0b1b963James Dong
560c1bc742181ded4930842b46e9507372f0b1b963James Dong#define HANTRO_TRUE     (1)
570c1bc742181ded4930842b46e9507372f0b1b963James Dong#define HANTRO_FALSE    (0)
580c1bc742181ded4930842b46e9507372f0b1b963James Dong
590c1bc742181ded4930842b46e9507372f0b1b963James Dong#ifndef NULL
600c1bc742181ded4930842b46e9507372f0b1b963James Dong#define NULL 0
610c1bc742181ded4930842b46e9507372f0b1b963James Dong#endif
620c1bc742181ded4930842b46e9507372f0b1b963James Dong
630c1bc742181ded4930842b46e9507372f0b1b963James Dong#define MEMORY_ALLOCATION_ERROR 0xFFFF
640c1bc742181ded4930842b46e9507372f0b1b963James Dong#define PARAM_SET_ERROR 0xFFF0
650c1bc742181ded4930842b46e9507372f0b1b963James Dong
660c1bc742181ded4930842b46e9507372f0b1b963James Dong/* value to be returned by GetBits if stream buffer is empty */
670c1bc742181ded4930842b46e9507372f0b1b963James Dong#define END_OF_STREAM 0xFFFFFFFFU
680c1bc742181ded4930842b46e9507372f0b1b963James Dong
690c1bc742181ded4930842b46e9507372f0b1b963James Dong#define EMPTY_RESIDUAL_INDICATOR 0xFFFFFF
700c1bc742181ded4930842b46e9507372f0b1b963James Dong
710c1bc742181ded4930842b46e9507372f0b1b963James Dong/* macro to mark a residual block empty, i.e. contain zero coefficients */
720c1bc742181ded4930842b46e9507372f0b1b963James Dong#define MARK_RESIDUAL_EMPTY(residual) ((residual)[0] = EMPTY_RESIDUAL_INDICATOR)
730c1bc742181ded4930842b46e9507372f0b1b963James Dong/* macro to check if residual block is empty */
740c1bc742181ded4930842b46e9507372f0b1b963James Dong#define IS_RESIDUAL_EMPTY(residual) ((residual)[0] == EMPTY_RESIDUAL_INDICATOR)
750c1bc742181ded4930842b46e9507372f0b1b963James Dong
760c1bc742181ded4930842b46e9507372f0b1b963James Dong/* macro for assertion, used only if compiler flag _ASSERT_USED is defined */
770c1bc742181ded4930842b46e9507372f0b1b963James Dong#ifdef _ASSERT_USED
780c1bc742181ded4930842b46e9507372f0b1b963James Dong#define ASSERT(expr) assert(expr)
790c1bc742181ded4930842b46e9507372f0b1b963James Dong#else
800c1bc742181ded4930842b46e9507372f0b1b963James Dong#define ASSERT(expr)
810c1bc742181ded4930842b46e9507372f0b1b963James Dong#endif
820c1bc742181ded4930842b46e9507372f0b1b963James Dong
830c1bc742181ded4930842b46e9507372f0b1b963James Dong/* macro for range checking an value, used only if compiler flag _RANGE_CHECK
840c1bc742181ded4930842b46e9507372f0b1b963James Dong * is defined */
850c1bc742181ded4930842b46e9507372f0b1b963James Dong#ifdef _RANGE_CHECK
860c1bc742181ded4930842b46e9507372f0b1b963James Dong#define RANGE_CHECK(value, minBound, maxBound) \
870c1bc742181ded4930842b46e9507372f0b1b963James Dong{ \
880c1bc742181ded4930842b46e9507372f0b1b963James Dong    if ((value) < (minBound) || (value) > (maxBound)) \
890c1bc742181ded4930842b46e9507372f0b1b963James Dong        fprintf(stderr, "Warning: Value exceeds given limit(s)!\n"); \
900c1bc742181ded4930842b46e9507372f0b1b963James Dong}
910c1bc742181ded4930842b46e9507372f0b1b963James Dong#else
920c1bc742181ded4930842b46e9507372f0b1b963James Dong#define RANGE_CHECK(value, minBound, maxBound)
930c1bc742181ded4930842b46e9507372f0b1b963James Dong#endif
940c1bc742181ded4930842b46e9507372f0b1b963James Dong
950c1bc742181ded4930842b46e9507372f0b1b963James Dong/* macro for range checking an array, used only if compiler flag _RANGE_CHECK
960c1bc742181ded4930842b46e9507372f0b1b963James Dong * is defined */
970c1bc742181ded4930842b46e9507372f0b1b963James Dong#ifdef _RANGE_CHECK
980c1bc742181ded4930842b46e9507372f0b1b963James Dong#define RANGE_CHECK_ARRAY(array, minBound, maxBound, length) \
990c1bc742181ded4930842b46e9507372f0b1b963James Dong{ \
1000c1bc742181ded4930842b46e9507372f0b1b963James Dong    i32 i; \
1010c1bc742181ded4930842b46e9507372f0b1b963James Dong    for (i = 0; i < (length); i++) \
1020c1bc742181ded4930842b46e9507372f0b1b963James Dong        if ((array)[i] < (minBound) || (array)[i] > (maxBound)) \
1030c1bc742181ded4930842b46e9507372f0b1b963James Dong            fprintf(stderr,"Warning: Value [%d] exceeds given limit(s)!\n",i); \
1040c1bc742181ded4930842b46e9507372f0b1b963James Dong}
1050c1bc742181ded4930842b46e9507372f0b1b963James Dong#else
1060c1bc742181ded4930842b46e9507372f0b1b963James Dong#define RANGE_CHECK_ARRAY(array, minBound, maxBound, length)
1070c1bc742181ded4930842b46e9507372f0b1b963James Dong#endif
1080c1bc742181ded4930842b46e9507372f0b1b963James Dong
1090c1bc742181ded4930842b46e9507372f0b1b963James Dong/* macro for debug printing, used only if compiler flag _DEBUG_PRINT is
1100c1bc742181ded4930842b46e9507372f0b1b963James Dong * defined */
1110c1bc742181ded4930842b46e9507372f0b1b963James Dong#ifdef _DEBUG_PRINT
1120c1bc742181ded4930842b46e9507372f0b1b963James Dong#define DEBUG(args) printf args
1130c1bc742181ded4930842b46e9507372f0b1b963James Dong#else
1140c1bc742181ded4930842b46e9507372f0b1b963James Dong#define DEBUG(args)
1150c1bc742181ded4930842b46e9507372f0b1b963James Dong#endif
1160c1bc742181ded4930842b46e9507372f0b1b963James Dong
1170c1bc742181ded4930842b46e9507372f0b1b963James Dong/* macro for error printing, used only if compiler flag _ERROR_PRINT is
1180c1bc742181ded4930842b46e9507372f0b1b963James Dong * defined */
1190c1bc742181ded4930842b46e9507372f0b1b963James Dong#ifdef _ERROR_PRINT
1200c1bc742181ded4930842b46e9507372f0b1b963James Dong#define EPRINT(msg) fprintf(stderr,"ERROR: %s\n",msg)
1210c1bc742181ded4930842b46e9507372f0b1b963James Dong#else
1220c1bc742181ded4930842b46e9507372f0b1b963James Dong#define EPRINT(msg)
1230c1bc742181ded4930842b46e9507372f0b1b963James Dong#endif
1240c1bc742181ded4930842b46e9507372f0b1b963James Dong
1250c1bc742181ded4930842b46e9507372f0b1b963James Dong/* macro to get smaller of two values */
1260c1bc742181ded4930842b46e9507372f0b1b963James Dong#define MIN(a, b) (((a) < (b)) ? (a) : (b))
1270c1bc742181ded4930842b46e9507372f0b1b963James Dong
1280c1bc742181ded4930842b46e9507372f0b1b963James Dong/* macro to get greater of two values */
1290c1bc742181ded4930842b46e9507372f0b1b963James Dong#define MAX(a, b) (((a) > (b)) ? (a) : (b))
1300c1bc742181ded4930842b46e9507372f0b1b963James Dong
1310c1bc742181ded4930842b46e9507372f0b1b963James Dong/* macro to get absolute value */
1320c1bc742181ded4930842b46e9507372f0b1b963James Dong#define ABS(a) (((a) < 0) ? -(a) : (a))
1330c1bc742181ded4930842b46e9507372f0b1b963James Dong
1340c1bc742181ded4930842b46e9507372f0b1b963James Dong/* macro to clip a value z, so that x <= z =< y */
1350c1bc742181ded4930842b46e9507372f0b1b963James Dong#define CLIP3(x,y,z) (((z) < (x)) ? (x) : (((z) > (y)) ? (y) : (z)))
1360c1bc742181ded4930842b46e9507372f0b1b963James Dong
1370c1bc742181ded4930842b46e9507372f0b1b963James Dong/* macro to clip a value z, so that 0 <= z =< 255 */
1380c1bc742181ded4930842b46e9507372f0b1b963James Dong#define CLIP1(z) (((z) < 0) ? 0 : (((z) > 255) ? 255 : (z)))
1390c1bc742181ded4930842b46e9507372f0b1b963James Dong
1400c1bc742181ded4930842b46e9507372f0b1b963James Dong/* macro to allocate memory */
1410c1bc742181ded4930842b46e9507372f0b1b963James Dong#define ALLOCATE(ptr, count, type) \
1420c1bc742181ded4930842b46e9507372f0b1b963James Dong{ \
1430c1bc742181ded4930842b46e9507372f0b1b963James Dong    (ptr) = H264SwDecMalloc((count) * sizeof(type)); \
1440c1bc742181ded4930842b46e9507372f0b1b963James Dong}
1450c1bc742181ded4930842b46e9507372f0b1b963James Dong
1460c1bc742181ded4930842b46e9507372f0b1b963James Dong/* macro to free allocated memory */
1470c1bc742181ded4930842b46e9507372f0b1b963James Dong#define FREE(ptr) \
1480c1bc742181ded4930842b46e9507372f0b1b963James Dong{ \
1490c1bc742181ded4930842b46e9507372f0b1b963James Dong    H264SwDecFree((ptr)); (ptr) = NULL; \
1500c1bc742181ded4930842b46e9507372f0b1b963James Dong}
1510c1bc742181ded4930842b46e9507372f0b1b963James Dong
1520c1bc742181ded4930842b46e9507372f0b1b963James Dong#define ALIGN(ptr, bytePos) \
1530c1bc742181ded4930842b46e9507372f0b1b963James Dong        (ptr + ( ((bytePos - (int)ptr) & (bytePos - 1)) / sizeof(*ptr) ))
1540c1bc742181ded4930842b46e9507372f0b1b963James Dong
1550c1bc742181ded4930842b46e9507372f0b1b963James Dongextern const u32 h264bsdQpC[52];
1560c1bc742181ded4930842b46e9507372f0b1b963James Dong
1570c1bc742181ded4930842b46e9507372f0b1b963James Dong/*------------------------------------------------------------------------------
1580c1bc742181ded4930842b46e9507372f0b1b963James Dong    3. Data types
1590c1bc742181ded4930842b46e9507372f0b1b963James Dong------------------------------------------------------------------------------*/
1600c1bc742181ded4930842b46e9507372f0b1b963James Dong
1610c1bc742181ded4930842b46e9507372f0b1b963James Dong/*------------------------------------------------------------------------------
1620c1bc742181ded4930842b46e9507372f0b1b963James Dong    4. Function prototypes
1630c1bc742181ded4930842b46e9507372f0b1b963James Dong------------------------------------------------------------------------------*/
1640c1bc742181ded4930842b46e9507372f0b1b963James Dong#ifndef H264DEC_NEON
1650c1bc742181ded4930842b46e9507372f0b1b963James Dongu32 h264bsdCountLeadingZeros(u32 value, u32 length);
1660c1bc742181ded4930842b46e9507372f0b1b963James Dong#else
1670c1bc742181ded4930842b46e9507372f0b1b963James Dongu32 h264bsdCountLeadingZeros(u32 value);
1680c1bc742181ded4930842b46e9507372f0b1b963James Dong#endif
1690c1bc742181ded4930842b46e9507372f0b1b963James Dongu32 h264bsdRbspTrailingBits(strmData_t *strmData);
1700c1bc742181ded4930842b46e9507372f0b1b963James Dong
1710c1bc742181ded4930842b46e9507372f0b1b963James Dongu32 h264bsdMoreRbspData(strmData_t *strmData);
1720c1bc742181ded4930842b46e9507372f0b1b963James Dong
1730c1bc742181ded4930842b46e9507372f0b1b963James Dongu32 h264bsdNextMbAddress(u32 *pSliceGroupMap, u32 picSizeInMbs, u32 currMbAddr);
1740c1bc742181ded4930842b46e9507372f0b1b963James Dong
1750c1bc742181ded4930842b46e9507372f0b1b963James Dongvoid h264bsdSetCurrImageMbPointers(image_t *image, u32 mbNum);
1760c1bc742181ded4930842b46e9507372f0b1b963James Dong
1770c1bc742181ded4930842b46e9507372f0b1b963James Dong#endif /* #ifdef H264SWDEC_UTIL_H */
1780c1bc742181ded4930842b46e9507372f0b1b963James Dong
179