mpeg2hwd_utils.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*------------------------------------------------------------------------------
  2. -- --
  3. -- This software is confidential and proprietary and may be used --
  4. -- only as expressly authorized by a licensing agreement from --
  5. -- --
  6. -- Hantro Products Oy. --
  7. -- --
  8. -- (C) COPYRIGHT 2006 HANTRO PRODUCTS OY --
  9. -- ALL RIGHTS RESERVED --
  10. -- --
  11. -- The entire notice above must be reproduced --
  12. -- on all copies and should not be removed. --
  13. -- --
  14. --------------------------------------------------------------------------------
  15. --
  16. -- Abstract : Header file for stream decoding utilities
  17. --
  18. --------------------------------------------------------------------------------
  19. --
  20. -- Version control information, please leave untouched.
  21. --
  22. -- $RCSfile: mpeg2hwd_utils.h,v $
  23. -- $Date: 2008/02/19 10:51:10 $
  24. -- $Revision: 1.5 $
  25. --
  26. ------------------------------------------------------------------------------*/
  27. #ifndef STRMDEC_UTILS_H_DEFINED
  28. #define STRMDEC_UTILS_H_DEFINED
  29. /*------------------------------------------------------------------------------
  30. 1. Include headers
  31. ------------------------------------------------------------------------------*/
  32. #include "mpeg2hwd_container.h"
  33. #ifdef _ASSERT_USED
  34. #include <assert.h>
  35. #endif
  36. #ifdef _UTEST
  37. #include <stdio.h>
  38. #endif
  39. /*------------------------------------------------------------------------------
  40. 2. Module defines
  41. ------------------------------------------------------------------------------*/
  42. /* constant definitions */
  43. #ifndef OK
  44. #define OK 0
  45. #endif
  46. #ifndef NOK
  47. #define NOK 1
  48. #endif
  49. #ifndef FALSE
  50. #define FALSE 0
  51. #endif
  52. #ifndef TRUE
  53. #define TRUE 1
  54. #endif
  55. #ifndef NULL
  56. #define NULL 0
  57. #endif
  58. /* decoder states */
  59. enum
  60. {
  61. STATE_OK,
  62. STATE_NOT_READY,
  63. STATE_SYNC_LOST
  64. };
  65. #define HANTRO_OK 0
  66. #define HANTRO_NOK 1
  67. #ifndef NULL
  68. #define NULL 0
  69. #endif
  70. /* picture structure */
  71. #define TOPFIELD 1
  72. #define BOTTOMFIELD 2
  73. #define FRAMEPICTURE 3
  74. /* Error concealment */
  75. #define FREEZED_PIC_RDY 1
  76. /* start codes */
  77. enum
  78. {
  79. SC_PICTURE = 0x00,
  80. SC_SLICE = 0x01, /* throug AF */
  81. SC_USER_DATA = 0xB2,
  82. SC_SEQUENCE = 0xB3,
  83. SC_SEQ_ERROR = 0xB4,
  84. SC_EXTENSION = 0xB5,
  85. SC_SEQ_END = 0xB7,
  86. SC_GROUP = 0xB8,
  87. SC_RESYNC = 0x4,
  88. SC_SV_START = 0x20,
  89. SC_SV_END = 0x3F,
  90. SC_NOT_FOUND = 0xFFFE,
  91. SC_ERROR = 0xFFFF
  92. };
  93. /* start codes */
  94. enum
  95. {
  96. SC_SEQ_EXT = 0x01,
  97. SC_SEQ_DISPLAY_EXT = 0x02,
  98. SC_QMATRIX_EXT = 0x03,
  99. SC_COPYRIGHT_EXT = 0x04,
  100. SC_SEQ_SCALABLE_EXT = 0x05,
  101. SC_PIC_DISPLAY_EXT = 0x07,
  102. SC_PIC_CODING_EXT = 0x08,
  103. SC_PIC_SPATIAL_SCALABLE_EXT = 0x09,
  104. SC_PIC_TEMPORAL_SCALABLE_EXT = 0x0A,
  105. SC_CAMERA_PARAM_EXT = 0x0B,
  106. SC_ITU_T_EXT = 0x0C
  107. };
  108. enum
  109. {
  110. IFRAME = 1,
  111. PFRAME = 2,
  112. BFRAME = 3,
  113. DFRAME = 4
  114. };
  115. /* How many control words(32bit) does one mb take */
  116. #define NBR_OF_WORDS_MB 1
  117. /* how many motion vector data words for mb */
  118. #define NBR_MV_WORDS_MB 4
  119. /* how many DC VLC data words for mb */
  120. #define NBR_DC_WORDS_MB 2
  121. enum
  122. {
  123. OUT_OF_BUFFER = 0xFF
  124. };
  125. /* Bit positions in Asic CtrlBits memory ( specified in top level spec ) */
  126. enum
  127. {
  128. ASICPOS_MBTYPE = 31,
  129. ASICPOS_4MV = 30,
  130. ASICPOS_USEINTRADCVLC = 30,
  131. ASICPOS_VPBI = 29,
  132. ASICPOS_ACPREDFLAG = 28,
  133. ASICPOS_QP = 16,
  134. ASICPOS_CONCEAL = 15,
  135. ASICPOS_MBNOTCODED = 14
  136. };
  137. /* Boundary strenght positions in ctrlBits */
  138. enum
  139. {
  140. BS_VER_00 = 13,
  141. BS_HOR_00 = 10,
  142. BS_VER_02 = 7,
  143. BS_HOR_01 = 4,
  144. BS_VER_04 = 1,
  145. BS_HOR_02_MSB = 0,
  146. BS_HOR_02_LSB = 30,
  147. BS_VER_06 = 27,
  148. BS_HOR_03 = 24,
  149. BS_VER_08 = 21,
  150. BS_HOR_08 = 18,
  151. BS_VER_10 = 15,
  152. BS_HOR_09 = 12,
  153. BS_VER_12 = 9,
  154. BS_HOR_10 = 6,
  155. BS_VER_14 = 3,
  156. BS_HOR_11 = 0
  157. };
  158. enum
  159. {
  160. ASIC_ZERO_MOTION_VECTORS_LUM = 0,
  161. ASIC_ZERO_MOTION_VECTORS_CHR = 0
  162. };
  163. enum
  164. {
  165. MB_NOT_CODED = 0x80
  166. };
  167. /* value to be returned by GetBits if stream buffer is empty */
  168. #define END_OF_STREAM 0xFFFFFFFFU
  169. /* macro for debug printing. Note that double parenthesis has to be used, i.e.
  170. * DEBUG(("Debug printing %d\n",%d)) */
  171. #ifdef _UTEST
  172. #define DEBUG(args) printf args
  173. #else
  174. #define DEBUG(args)
  175. #endif
  176. /* macro for assertion, used only if compiler flag _ASSERT_USED is defined */
  177. #ifdef _ASSERT_USED
  178. #define ASSERT(expr) assert(expr)
  179. #else
  180. #define ASSERT(expr)
  181. #endif
  182. /* macro to check if stream ends */
  183. #define IS_END_OF_STREAM(pContainer) \
  184. ( (pContainer)->StrmDesc.strmBuffReadBits == \
  185. (8*(pContainer)->StrmDesc.strmBuffSize) )
  186. /* macro to saturate value to range [min,max]. Note that for unsigned value
  187. * both min and max should be positive, otherwise result will be wrong due to
  188. * arithmetic conversion. If min > max -> value will be equal to min. */
  189. #define SATURATE(min,value,max) \
  190. if ((value) < (min)) (value) = (min); \
  191. else if ((value) > (max)) (value) = (max);
  192. #define ABS(val) (((val) < 0) ? -(val) : (val))
  193. #define DEQUANTIZE(val,QP) \
  194. { /*lint -e514 */ \
  195. if ((val) < 0) \
  196. { \
  197. (val) = (i32)(QP)*(2*(val) - 1) + !((QP)&0x1); \
  198. if ((val) < -2048) (val) = -2048; \
  199. } \
  200. else if ((val) > 0) \
  201. { \
  202. (val) = (i32)(QP)*(2*(val) + 1) - !((QP)&0x1); \
  203. if ((val) > 2047) (val)=2047; \
  204. }/*lint +e514 */ \
  205. }
  206. /* following four macros are used to handle temporary bit buffer of type
  207. * tmpBuffer_t. BUFFER_SHOW 'shows' numBits bits in outVal. Bits are right
  208. * aligned. BUFFER_FLUSH removes numBits from temporal buffer. There is no
  209. * checking whether buffer contains enough bits -> to be used with caution.
  210. * BUFFER_GET shows and removes bits. numBits should not be 0 in any of the
  211. * macros. Note that BUFFER_GET indicates running out of bits kind of late
  212. * compared to operation of corresponding functions. Additionally, BUFFER_FLUSH
  213. * does not indicate that situation at all. */
  214. #define BUFFER_INIT(buffer) \
  215. { \
  216. (buffer).bits = 32; \
  217. (buffer).value = Dec_ShowBits32(pDecContainer); \
  218. }
  219. #define BUFFER_SHOW(buffer, outVal, numBits) \
  220. { \
  221. if ((buffer).bits < (numBits)) \
  222. { \
  223. if(Dec_FlushBits(pDecContainer,32-(buffer).bits) == END_OF_STREAM) \
  224. return(END_OF_STREAM); \
  225. (buffer).value = Dec_ShowBits32(pDecContainer); \
  226. (buffer).bits = 32; \
  227. } \
  228. (outVal) = (buffer).value >> (32 - (numBits)); \
  229. }
  230. #define BUFFER_FLUSH(buffer, numBits) \
  231. { \
  232. (buffer).value <<= (numBits); \
  233. (buffer).bits -= (numBits); \
  234. }
  235. #define BUFFER_GET(buffer, outVal, numBits) \
  236. { \
  237. if ((buffer).bits < (numBits)) \
  238. { \
  239. if(Dec_FlushBits(pDecContainer,32-(buffer).bits) == END_OF_STREAM) \
  240. return(END_OF_STREAM); \
  241. (buffer).value = Dec_ShowBits32(pDecContainer); \
  242. (buffer).bits = 32; \
  243. } \
  244. (outVal) = (buffer).value >> (32 - (numBits)); \
  245. (buffer).value <<= (numBits); \
  246. (buffer).bits -= (numBits); \
  247. }
  248. /*------------------------------------------------------------------------------
  249. 3. Data types
  250. ------------------------------------------------------------------------------*/
  251. typedef struct
  252. {
  253. u32 value;
  254. u32 bits;
  255. } tmpBuffer_t;
  256. typedef struct
  257. {
  258. u8 *pStrmBuffStart; /* pointer to start of stream buffer */
  259. u8 *pStrmCurrPos; /* current read address in stream buffer */
  260. u32 bitPosInWord; /* bit position in stream buffer byte */
  261. u32 strmBuffSize; /* size of stream buffer (bytes) */
  262. u32 strmBuffReadBits; /* number of bits read from stream buffer */
  263. } strmData_t;
  264. /*------------------------------------------------------------------------------
  265. 4. Function prototypes
  266. ------------------------------------------------------------------------------*/
  267. u32 mpeg2StrmDec_GetBits(DecContainer *, u32 numBits);
  268. u32 mpeg2StrmDec_ShowBits(DecContainer *, u32 numBits);
  269. u32 mpeg2StrmDec_ShowBits32(DecContainer *);
  270. u32 mpeg2StrmDec_ShowBitsAligned(DecContainer *, u32 numBits, u32 numBytes);
  271. u32 mpeg2StrmDec_FlushBits(DecContainer *, u32 numBits);
  272. u32 mpeg2StrmDec_UnFlushBits(DecContainer *, u32 numBits);
  273. u32 mpeg2StrmDec_GetStuffing(DecContainer *);
  274. u32 mpeg2StrmDec_CheckStuffing(DecContainer *);
  275. u32 mpeg2StrmDec_FindSync(DecContainer *);
  276. u32 mpeg2StrmDec_NextStartCode(DecContainer *);
  277. u32 mpeg2StrmDec_NumBits(u32 value);
  278. #endif