cabac.c 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123
  1. /*!
  2. *************************************************************************************
  3. * \file cabac.c
  4. *
  5. * \brief
  6. * CABAC entropy coding routines
  7. *
  8. * \author
  9. * Main contributors (see contributors.h for copyright, address and affiliation details)
  10. * - Detlev Marpe <[email protected]>
  11. **************************************************************************************
  12. */
  13. #include "global.h"
  14. #include "cabac.h"
  15. #include "memalloc.h"
  16. #include "elements.h"
  17. #include "image.h"
  18. #include "biaridecod.h"
  19. #include "mb_access.h"
  20. #include "vlc.h"
  21. #include <mmintrin.h>
  22. #define get_bit(x, n) (_mm_cvtsi64_si32(_mm_srli_si64(*(__m64 *)&(x), n)) & 1)
  23. /*static inline int get_bit(int64 x,int n)
  24. {
  25. return (int)(((x >> n) & 1));
  26. }*/
  27. static __forceinline void or_bits_low(int64 *x, int mask, int position)
  28. {
  29. *(int32_t *)x |= (mask << position);
  30. }
  31. static inline void or_bits(int64 *x, int mask, int position)
  32. {
  33. #ifdef _M_IX86
  34. __m64 mmx_x = *(__m64 *)x;
  35. __m64 mmx_mask = _mm_cvtsi32_si64(mask);
  36. mmx_mask=_mm_slli_si64(mmx_mask, position);
  37. mmx_x = _mm_or_si64(mmx_x, mmx_mask);
  38. *(__m64 *)x = mmx_x;
  39. #else
  40. *x |= ((int64) mask << position);
  41. #endif
  42. }
  43. #if TRACE
  44. int symbolCount = 0;
  45. #endif
  46. /***********************************************************************
  47. * L O C A L L Y D E F I N E D F U N C T I O N P R O T O T Y P E S
  48. ***********************************************************************
  49. */
  50. static unsigned int unary_bin_decode(DecodingEnvironmentPtr dep_dp,
  51. BiContextTypePtr ctx,
  52. int ctx_offset);
  53. static unsigned int unary_bin_max_decode(DecodingEnvironmentPtr dep_dp,
  54. BiContextTypePtr ctx,
  55. int ctx_offset,
  56. unsigned int max_symbol);
  57. unsigned int unary_exp_golomb_mv_decode(DecodingEnvironmentPtr dep_dp, BiContextTypePtr ctx, unsigned int max_bin);
  58. unsigned int unary_exp_golomb_mv_decode3(DecodingEnvironmentPtr dep_dp, BiContextTypePtr ctx);
  59. void CheckAvailabilityOfNeighborsCABAC(Macroblock *currMB)
  60. {
  61. VideoParameters *p_Vid = currMB->p_Vid;
  62. PixelPos up, left;
  63. p_Vid->getNeighbourLeftLuma(currMB, &left);
  64. p_Vid->getNeighbourUpLuma(currMB, &up);
  65. if (up.available)
  66. currMB->mb_up = &p_Vid->mb_data[up.mb_addr];
  67. else
  68. currMB->mb_up = NULL;
  69. if (left.available)
  70. currMB->mb_left = &p_Vid->mb_data[left.mb_addr];
  71. else
  72. currMB->mb_left = NULL;
  73. }
  74. void cabac_new_slice(Slice *currSlice)
  75. {
  76. currSlice->last_dquant=0;
  77. }
  78. /*!
  79. ************************************************************************
  80. * \brief
  81. * Allocation of contexts models for the motion info
  82. * used for arithmetic decoding
  83. *
  84. ************************************************************************
  85. */
  86. MotionInfoContexts* create_contexts_MotionInfo(void)
  87. {
  88. MotionInfoContexts *deco_ctx;
  89. deco_ctx = (MotionInfoContexts*) calloc(1, sizeof(MotionInfoContexts) );
  90. if( deco_ctx == NULL )
  91. no_mem_exit("create_contexts_MotionInfo: deco_ctx");
  92. return deco_ctx;
  93. }
  94. /*!
  95. ************************************************************************
  96. * \brief
  97. * Allocates of contexts models for the texture info
  98. * used for arithmetic decoding
  99. ************************************************************************
  100. */
  101. TextureInfoContexts* create_contexts_TextureInfo(void)
  102. {
  103. TextureInfoContexts *deco_ctx;
  104. deco_ctx = (TextureInfoContexts*) calloc(1, sizeof(TextureInfoContexts) );
  105. if( deco_ctx == NULL )
  106. no_mem_exit("create_contexts_TextureInfo: deco_ctx");
  107. return deco_ctx;
  108. }
  109. /*!
  110. ************************************************************************
  111. * \brief
  112. * Frees the memory of the contexts models
  113. * used for arithmetic decoding of the motion info.
  114. ************************************************************************
  115. */
  116. void delete_contexts_MotionInfo(MotionInfoContexts *deco_ctx)
  117. {
  118. if( deco_ctx == NULL )
  119. return;
  120. free( deco_ctx );
  121. }
  122. /*!
  123. ************************************************************************
  124. * \brief
  125. * Frees the memory of the contexts models
  126. * used for arithmetic decoding of the texture info.
  127. ************************************************************************
  128. */
  129. void delete_contexts_TextureInfo(TextureInfoContexts *deco_ctx)
  130. {
  131. if( deco_ctx == NULL )
  132. return;
  133. free( deco_ctx );
  134. }
  135. Boolean readFieldModeInfo_CABAC(Macroblock *currMB, DecodingEnvironmentPtr dep_dp)
  136. {
  137. Slice *currSlice = currMB->p_Slice;
  138. VideoParameters *p_Vid = currMB->p_Vid;
  139. MotionInfoContexts *ctx = currSlice->mot_ctx;
  140. int a = currMB->mb_avail_left ? p_Vid->mb_data[currMB->mb_addr_left].mb_field : 0;
  141. int b = currMB->mb_avail_up ? p_Vid->mb_data[currMB->mb_addr_up].mb_field : 0;
  142. int act_ctx = a + b;
  143. return biari_decode_symbol (dep_dp, &ctx->mb_aff_contexts[act_ctx]);
  144. }
  145. int check_next_mb_and_get_field_mode_CABAC(Slice *currSlice, DataPartition *act_dp)
  146. {
  147. VideoParameters *p_Vid = currSlice->p_Vid;
  148. BiContextTypePtr mb_type_ctx_copy[3];
  149. BiContextTypePtr mb_aff_ctx_copy;
  150. DecodingEnvironmentPtr dep_dp_copy;
  151. int length;
  152. DecodingEnvironmentPtr dep_dp = &(act_dp->de_cabac);
  153. int bframe = (currSlice->slice_type == B_SLICE);
  154. int skip = 0;
  155. int field = 0;
  156. int i;
  157. Macroblock *currMB;
  158. //get next MB
  159. ++p_Vid->current_mb_nr;
  160. currMB = &p_Vid->mb_data[p_Vid->current_mb_nr];
  161. currMB->p_Vid = p_Vid;
  162. currMB->p_Slice = currSlice;
  163. currMB->slice_nr = p_Vid->current_slice_nr;
  164. currMB->mb_field = p_Vid->mb_data[p_Vid->current_mb_nr-1].mb_field;
  165. currMB->mbAddrX = p_Vid->current_mb_nr;
  166. CheckAvailabilityOfNeighbors(currMB);
  167. CheckAvailabilityOfNeighborsCABAC(currMB);
  168. //create
  169. dep_dp_copy = (DecodingEnvironmentPtr) calloc(1, sizeof(DecodingEnvironment) );
  170. for (i=0;i<3;++i)
  171. mb_type_ctx_copy[i] = (BiContextTypePtr) calloc(NUM_MB_TYPE_CTX, sizeof(BiContextType) );
  172. mb_aff_ctx_copy = (BiContextTypePtr) calloc(NUM_MB_AFF_CTX, sizeof(BiContextType) );
  173. //copy
  174. memcpy(dep_dp_copy,dep_dp,sizeof(DecodingEnvironment));
  175. length = *(dep_dp_copy->Dcodestrm_len) = *(dep_dp->Dcodestrm_len);
  176. for (i=0;i<3;++i)
  177. memcpy(mb_type_ctx_copy[i], currSlice->mot_ctx->mb_type_contexts[i],NUM_MB_TYPE_CTX*sizeof(BiContextType) );
  178. memcpy(mb_aff_ctx_copy, currSlice->mot_ctx->mb_aff_contexts,NUM_MB_AFF_CTX*sizeof(BiContextType) );
  179. //check_next_mb
  180. currSlice->last_dquant = 0;
  181. skip = readMB_skip_flagInfo_CABAC(currMB, dep_dp);
  182. if (!skip)
  183. {
  184. field = readFieldModeInfo_CABAC(currMB, dep_dp);
  185. p_Vid->mb_data[p_Vid->current_mb_nr-1].mb_field = field;
  186. }
  187. //reset
  188. p_Vid->current_mb_nr--;
  189. memcpy(dep_dp,dep_dp_copy,sizeof(DecodingEnvironment));
  190. *(dep_dp->Dcodestrm_len) = length;
  191. for (i=0;i<3;++i)
  192. memcpy(currSlice->mot_ctx->mb_type_contexts[i],mb_type_ctx_copy[i], NUM_MB_TYPE_CTX*sizeof(BiContextType) );
  193. memcpy( currSlice->mot_ctx->mb_aff_contexts,mb_aff_ctx_copy,NUM_MB_AFF_CTX*sizeof(BiContextType) );
  194. CheckAvailabilityOfNeighborsCABAC(currMB);
  195. //delete
  196. free(dep_dp_copy);
  197. for (i=0;i<3;++i)
  198. free(mb_type_ctx_copy[i]);
  199. free(mb_aff_ctx_copy);
  200. return skip;
  201. }
  202. /*!
  203. ************************************************************************
  204. * \brief
  205. * This function is used to arithmetically decode the motion
  206. * vector data of a B-frame MB.
  207. ************************************************************************
  208. */
  209. #if defined(_DEBUG) || !defined(_M_IX86)
  210. int decodeMVD_CABAC(DecodingEnvironmentPtr dep_dp, BiContextType mv_ctx[2][NUM_MV_RES_CTX], int act_ctx, int err)
  211. {
  212. int act_sym = biari_decode_symbol(dep_dp,&mv_ctx[0][act_ctx+err] );
  213. if (act_sym != 0)
  214. {
  215. int mv_sign;
  216. act_sym = unary_exp_golomb_mv_decode3(dep_dp,mv_ctx[1]+act_ctx);
  217. ++act_sym;
  218. mv_sign = biari_decode_symbol_eq_prob(dep_dp);
  219. if(mv_sign)
  220. act_sym = -act_sym;
  221. }
  222. return act_sym;
  223. }
  224. #else
  225. int decodeMVD_CABAC(DecodingEnvironmentPtr dep_dp, BiContextType mv_ctx[2][NUM_MV_RES_CTX], int act_ctx, int err);
  226. #endif
  227. int readMVD_CABAC(Macroblock *currMB, DecodingEnvironmentPtr dep_dp, int k, int list_idx, int x, int y)
  228. {
  229. VideoParameters *p_Vid = currMB->p_Vid;
  230. Slice *currSlice = currMB->p_Slice;
  231. MotionInfoContexts *ctx = currSlice->mot_ctx;
  232. int a = 0, b = 0;
  233. // int act_ctx;
  234. // int act_sym;
  235. int mv_local_err;
  236. int err;
  237. PixelPos block_a, block_b;
  238. p_Vid->getNeighbourPXLumaNB_NoPos(currMB, y - 1, &block_b);
  239. if (block_b.available)
  240. {
  241. b = abs(p_Vid->mb_data[block_b.mb_addr].mvd[list_idx][block_b.y>>2][x>>2][k]);
  242. if (currSlice->mb_aff_frame_flag && (k==1))
  243. {
  244. if ((currMB->mb_field==0) && (p_Vid->mb_data[block_b.mb_addr].mb_field==1))
  245. b *= 2;
  246. else if ((currMB->mb_field==1) && (p_Vid->mb_data[block_b.mb_addr].mb_field==0))
  247. b /= 2;
  248. }
  249. }
  250. p_Vid->getNeighbourXPLumaNB_NoPos(currMB, x - 1, y , &block_a);
  251. if (block_a.available)
  252. {
  253. a = abs(p_Vid->mb_data[block_a.mb_addr].mvd[list_idx][block_a.y>>2][block_a.x>>2][k]);
  254. if (currSlice->mb_aff_frame_flag && (k==1))
  255. {
  256. if ((currMB->mb_field==0) && (p_Vid->mb_data[block_a.mb_addr].mb_field==1))
  257. a *= 2;
  258. else if ((currMB->mb_field==1) && (p_Vid->mb_data[block_a.mb_addr].mb_field==0))
  259. a /= 2;
  260. }
  261. }
  262. if ((mv_local_err = a + b)<3)
  263. err = 0;
  264. else
  265. {
  266. if (mv_local_err > 32)
  267. err = 3;
  268. else
  269. err = 2;
  270. }
  271. return decodeMVD_CABAC(dep_dp, ctx->mv_res_contexts, 5*k, err);
  272. /*
  273. act_sym = biari_decode_symbol(dep_dp,&ctx->mv_res_contexts[0][act_ctx] );
  274. if (act_sym != 0)
  275. {
  276. int mv_sign;
  277. act_ctx = 5 * k;
  278. act_sym = unary_exp_golomb_mv_decode3(dep_dp,ctx->mv_res_contexts[1]+act_ctx);
  279. ++act_sym;
  280. mv_sign = biari_decode_symbol_eq_prob(dep_dp);
  281. if(mv_sign)
  282. act_sym = -act_sym;
  283. }
  284. return act_sym;
  285. */
  286. }
  287. /*!
  288. ************************************************************************
  289. * \brief
  290. * This function is used to arithmetically decode the 8x8 block type.
  291. ************************************************************************
  292. */
  293. int readB8_typeInfo_CABAC(Slice *currSlice, DecodingEnvironmentPtr dep_dp)
  294. {
  295. int act_sym = 0;
  296. int bframe = (currSlice->slice_type == B_SLICE);
  297. MotionInfoContexts *ctx = currSlice->mot_ctx;
  298. if (!bframe)
  299. {
  300. if (biari_decode_symbol (dep_dp, &ctx->b8_type_contexts[0][1]))
  301. {
  302. act_sym = 0;
  303. }
  304. else
  305. {
  306. if (biari_decode_symbol (dep_dp, &ctx->b8_type_contexts[0][3]))
  307. {
  308. if (biari_decode_symbol (dep_dp, &ctx->b8_type_contexts[0][4])) act_sym = 2;
  309. else act_sym = 3;
  310. }
  311. else
  312. {
  313. act_sym = 1;
  314. }
  315. }
  316. }
  317. else
  318. {
  319. if (biari_decode_symbol (dep_dp, &ctx->b8_type_contexts[1][0]))
  320. {
  321. if (biari_decode_symbol (dep_dp, &ctx->b8_type_contexts[1][1]))
  322. {
  323. if (biari_decode_symbol (dep_dp, &ctx->b8_type_contexts[1][2]))
  324. {
  325. if (biari_decode_symbol (dep_dp, &ctx->b8_type_contexts[1][3]))
  326. {
  327. act_sym = 10;
  328. if (biari_decode_symbol (dep_dp, &ctx->b8_type_contexts[1][3])) act_sym++;
  329. }
  330. else
  331. {
  332. act_sym = 6;
  333. if (biari_decode_symbol (dep_dp, &ctx->b8_type_contexts[1][3])) act_sym+=2;
  334. if (biari_decode_symbol (dep_dp, &ctx->b8_type_contexts[1][3])) act_sym++;
  335. }
  336. }
  337. else
  338. {
  339. act_sym=2;
  340. if (biari_decode_symbol (dep_dp, &ctx->b8_type_contexts[1][3])) act_sym+=2;
  341. if (biari_decode_symbol (dep_dp, &ctx->b8_type_contexts[1][3])) act_sym+=1;
  342. }
  343. }
  344. else
  345. {
  346. if (biari_decode_symbol (dep_dp, &ctx->b8_type_contexts[1][3])) act_sym = 1;
  347. else act_sym = 0;
  348. }
  349. ++act_sym;
  350. }
  351. else
  352. {
  353. act_sym= 0;
  354. }
  355. }
  356. return act_sym;
  357. }
  358. /*!
  359. ************************************************************************
  360. * \brief
  361. * This function is used to arithmetically decode the macroblock
  362. * type info of a given MB.
  363. ************************************************************************
  364. */
  365. #if defined(_DEBUG) || !defined(_M_IX86)
  366. int readMB_skip_flagInfo_CABAC(Macroblock *currMB, DecodingEnvironmentPtr dep_dp)
  367. {
  368. Slice *currSlice = currMB->p_Slice;
  369. int bframe=(currSlice->slice_type == B_SLICE);
  370. MotionInfoContexts *ctx = currSlice->mot_ctx;
  371. int a = (currMB->mb_left != NULL) ? (currMB->mb_left->skip_flag == 0) : 0;
  372. int b = (currMB->mb_up != NULL) ? (currMB->mb_up ->skip_flag == 0) : 0;
  373. int act_ctx;
  374. int skip;
  375. if (bframe)
  376. {
  377. act_ctx = 7 + a + b;
  378. skip = biari_decode_symbol (dep_dp, &ctx->mb_type_contexts[2][act_ctx]);
  379. }
  380. else
  381. {
  382. act_ctx = a + b;
  383. skip = biari_decode_symbol(dep_dp, &ctx->mb_type_contexts[1][act_ctx]);
  384. }
  385. if (skip)
  386. {
  387. currSlice->last_dquant = 0;
  388. }
  389. return skip;
  390. }
  391. #endif
  392. /*!
  393. ***************************************************************************
  394. * \brief
  395. * This function is used to arithmetically decode the macroblock
  396. * intra_pred_size flag info of a given MB.
  397. ***************************************************************************
  398. */
  399. Boolean readMB_transform_size_flag_CABAC(Macroblock *currMB, DecodingEnvironmentPtr dep_dp)
  400. {
  401. Slice *currSlice = currMB->p_Slice;
  402. TextureInfoContexts*ctx = currSlice->tex_ctx;
  403. int b = (currMB->mb_up == NULL) ? 0 : currMB->mb_up->luma_transform_size_8x8_flag;
  404. int a = (currMB->mb_left == NULL) ? 0 : currMB->mb_left->luma_transform_size_8x8_flag;
  405. int act_ctx = a + b;
  406. int act_sym = biari_decode_symbol(dep_dp, ctx->transform_size_contexts + act_ctx);
  407. return act_sym;
  408. }
  409. /*!
  410. ************************************************************************
  411. * \brief
  412. * This function is used to arithmetically decode the macroblock
  413. * type info of a given MB.
  414. ************************************************************************
  415. */
  416. int readMB_typeInfo_CABAC(Macroblock *currMB, DecodingEnvironmentPtr dep_dp)
  417. {
  418. Slice *currSlice = currMB->p_Slice;
  419. MotionInfoContexts *ctx = currSlice->mot_ctx;
  420. int a = 0, b = 0;
  421. int act_ctx;
  422. int act_sym;
  423. int bframe=(currSlice->slice_type == B_SLICE);
  424. int mode_sym;
  425. int curr_mb_type;
  426. if(currSlice->slice_type == I_SLICE) // INTRA-frame
  427. {
  428. if (currMB->mb_up != NULL)
  429. b = (((currMB->mb_up)->mb_type != I4MB && currMB->mb_up->mb_type != I8MB) ? 1 : 0 );
  430. if (currMB->mb_left != NULL)
  431. a = (((currMB->mb_left)->mb_type != I4MB && currMB->mb_left->mb_type != I8MB) ? 1 : 0 );
  432. act_ctx = a + b;
  433. act_sym = biari_decode_symbol(dep_dp, ctx->mb_type_contexts[0] + act_ctx);
  434. if (act_sym==0) // 4x4 Intra
  435. {
  436. curr_mb_type = act_sym;
  437. }
  438. else // 16x16 Intra
  439. {
  440. mode_sym = biari_decode_final(dep_dp);
  441. if(mode_sym == 1)
  442. {
  443. curr_mb_type = 25;
  444. }
  445. else
  446. {
  447. act_sym = 1;
  448. act_ctx = 4;
  449. mode_sym = biari_decode_symbol(dep_dp, ctx->mb_type_contexts[0] + act_ctx ); // decoding of AC/no AC
  450. act_sym += mode_sym*12;
  451. act_ctx = 5;
  452. // decoding of cbp: 0,1,2
  453. mode_sym = biari_decode_symbol(dep_dp, ctx->mb_type_contexts[0] + act_ctx );
  454. if (mode_sym!=0)
  455. {
  456. act_ctx=6;
  457. mode_sym = biari_decode_symbol(dep_dp, ctx->mb_type_contexts[0] + act_ctx );
  458. act_sym+=4;
  459. if (mode_sym!=0)
  460. act_sym+=4;
  461. }
  462. // decoding of I pred-mode: 0,1,2,3
  463. act_ctx = 7;
  464. mode_sym = biari_decode_symbol(dep_dp, ctx->mb_type_contexts[0] + act_ctx );
  465. act_sym += mode_sym*2;
  466. act_ctx = 8;
  467. mode_sym = biari_decode_symbol(dep_dp, ctx->mb_type_contexts[0] + act_ctx );
  468. act_sym += mode_sym;
  469. curr_mb_type = act_sym;
  470. }
  471. }
  472. }
  473. else if(currSlice->slice_type == SI_SLICE) // SI-frame
  474. {
  475. // special ctx's for SI4MB
  476. if (currMB->mb_up != NULL)
  477. b = (( (currMB->mb_up)->mb_type != SI4MB) ? 1 : 0 );
  478. if (currMB->mb_left != NULL)
  479. a = (( (currMB->mb_left)->mb_type != SI4MB) ? 1 : 0 );
  480. act_ctx = a + b;
  481. act_sym = biari_decode_symbol(dep_dp, ctx->mb_type_contexts[1] + act_ctx);
  482. if (act_sym==0) // SI 4x4 Intra
  483. {
  484. curr_mb_type = 0;
  485. }
  486. else // analog INTRA_IMG
  487. {
  488. if (currMB->mb_up != NULL)
  489. b = (( (currMB->mb_up)->mb_type != I4MB) ? 1 : 0 );
  490. if (currMB->mb_left != NULL)
  491. a = (( (currMB->mb_left)->mb_type != I4MB) ? 1 : 0 );
  492. act_ctx = a + b;
  493. act_sym = biari_decode_symbol(dep_dp, ctx->mb_type_contexts[0] + act_ctx);
  494. if (act_sym==0) // 4x4 Intra
  495. {
  496. curr_mb_type = 1;
  497. }
  498. else // 16x16 Intra
  499. {
  500. mode_sym = biari_decode_final(dep_dp);
  501. if( mode_sym==1 )
  502. {
  503. curr_mb_type = 26;
  504. }
  505. else
  506. {
  507. act_sym = 2;
  508. act_ctx = 4;
  509. mode_sym = biari_decode_symbol(dep_dp, ctx->mb_type_contexts[0] + act_ctx ); // decoding of AC/no AC
  510. act_sym += mode_sym*12;
  511. act_ctx = 5;
  512. // decoding of cbp: 0,1,2
  513. mode_sym = biari_decode_symbol(dep_dp, ctx->mb_type_contexts[0] + act_ctx );
  514. if (mode_sym!=0)
  515. {
  516. act_ctx=6;
  517. mode_sym = biari_decode_symbol(dep_dp, ctx->mb_type_contexts[0] + act_ctx );
  518. act_sym+=4;
  519. if (mode_sym!=0)
  520. act_sym+=4;
  521. }
  522. // decoding of I pred-mode: 0,1,2,3
  523. act_ctx = 7;
  524. mode_sym = biari_decode_symbol(dep_dp, ctx->mb_type_contexts[0] + act_ctx );
  525. act_sym += mode_sym*2;
  526. act_ctx = 8;
  527. mode_sym = biari_decode_symbol(dep_dp, ctx->mb_type_contexts[0] + act_ctx );
  528. act_sym += mode_sym;
  529. curr_mb_type = act_sym;
  530. }
  531. }
  532. }
  533. }
  534. else
  535. {
  536. if (bframe)
  537. {
  538. if (currMB->mb_up != NULL)
  539. b = (( (currMB->mb_up)->mb_type != 0) ? 1 : 0 );
  540. if (currMB->mb_left != NULL)
  541. a = (( (currMB->mb_left)->mb_type != 0) ? 1 : 0 );
  542. act_ctx = a + b;
  543. if (biari_decode_symbol (dep_dp, &ctx->mb_type_contexts[2][act_ctx]))
  544. {
  545. if (biari_decode_symbol (dep_dp, &ctx->mb_type_contexts[2][4]))
  546. {
  547. if (biari_decode_symbol (dep_dp, &ctx->mb_type_contexts[2][5]))
  548. {
  549. act_sym=12;
  550. if (biari_decode_symbol (dep_dp, &ctx->mb_type_contexts[2][6])) act_sym+=8;
  551. if (biari_decode_symbol (dep_dp, &ctx->mb_type_contexts[2][6])) act_sym+=4;
  552. if (biari_decode_symbol (dep_dp, &ctx->mb_type_contexts[2][6])) act_sym+=2;
  553. if (act_sym==24) act_sym=11;
  554. else if (act_sym==26) act_sym=22;
  555. else
  556. {
  557. if (act_sym==22) act_sym=23;
  558. if (biari_decode_symbol (dep_dp, &ctx->mb_type_contexts[2][6])) act_sym+=1;
  559. }
  560. }
  561. else
  562. {
  563. act_sym=3;
  564. if (biari_decode_symbol (dep_dp, &ctx->mb_type_contexts[2][6])) act_sym+=4;
  565. if (biari_decode_symbol (dep_dp, &ctx->mb_type_contexts[2][6])) act_sym+=2;
  566. if (biari_decode_symbol (dep_dp, &ctx->mb_type_contexts[2][6])) act_sym+=1;
  567. }
  568. }
  569. else
  570. {
  571. if (biari_decode_symbol (dep_dp, &ctx->mb_type_contexts[2][6])) act_sym=2;
  572. else act_sym=1;
  573. }
  574. }
  575. else
  576. {
  577. act_sym = 0;
  578. }
  579. }
  580. else // P-frame
  581. {
  582. {
  583. if (biari_decode_symbol(dep_dp, &ctx->mb_type_contexts[1][4] ))
  584. {
  585. if (biari_decode_symbol(dep_dp, &ctx->mb_type_contexts[1][7] )) act_sym = 7;
  586. else act_sym = 6;
  587. }
  588. else
  589. {
  590. if (biari_decode_symbol(dep_dp, &ctx->mb_type_contexts[1][5] ))
  591. {
  592. if (biari_decode_symbol(dep_dp, &ctx->mb_type_contexts[1][7] )) act_sym = 2;
  593. else act_sym = 3;
  594. }
  595. else
  596. {
  597. if (biari_decode_symbol(dep_dp, &ctx->mb_type_contexts[1][6] )) act_sym = 4;
  598. else act_sym = 1;
  599. }
  600. }
  601. }
  602. }
  603. if (act_sym<=6 || (((currSlice->slice_type == B_SLICE) ? 1 : 0) && act_sym<=23))
  604. {
  605. curr_mb_type = act_sym;
  606. }
  607. else // additional info for 16x16 Intra-mode
  608. {
  609. mode_sym = biari_decode_final(dep_dp);
  610. if( mode_sym==1 )
  611. {
  612. if(bframe) // B frame
  613. curr_mb_type = 48;
  614. else // P frame
  615. curr_mb_type = 31;
  616. }
  617. else
  618. {
  619. act_ctx = 8;
  620. mode_sym = biari_decode_symbol(dep_dp, ctx->mb_type_contexts[1] + act_ctx ); // decoding of AC/no AC
  621. act_sym += mode_sym*12;
  622. // decoding of cbp: 0,1,2
  623. act_ctx = 9;
  624. mode_sym = biari_decode_symbol(dep_dp, ctx->mb_type_contexts[1] + act_ctx );
  625. if (mode_sym != 0)
  626. {
  627. act_sym+=4;
  628. mode_sym = biari_decode_symbol(dep_dp, ctx->mb_type_contexts[1] + act_ctx );
  629. if (mode_sym != 0)
  630. act_sym+=4;
  631. }
  632. // decoding of I pred-mode: 0,1,2,3
  633. act_ctx = 10;
  634. mode_sym = biari_decode_symbol(dep_dp, ctx->mb_type_contexts[1] + act_ctx );
  635. act_sym += mode_sym*2;
  636. mode_sym = biari_decode_symbol(dep_dp, ctx->mb_type_contexts[1] + act_ctx );
  637. act_sym += mode_sym;
  638. curr_mb_type = act_sym;
  639. }
  640. }
  641. }
  642. return curr_mb_type;
  643. }
  644. /*!
  645. ************************************************************************
  646. * \brief
  647. * This function is used to arithmetically decode a pair of
  648. * intra prediction modes of a given MB.
  649. ************************************************************************
  650. */
  651. #if defined(_DEBUG) || !defined(_M_IX86)
  652. int readIntraPredMode_CABAC(Slice *currSlice, DecodingEnvironmentPtr dep_dp)
  653. {
  654. TextureInfoContexts *ctx = currSlice->tex_ctx;
  655. int act_sym;
  656. // use_most_probable_mode
  657. act_sym = biari_decode_symbol(dep_dp, ctx->ipr_contexts);
  658. // remaining_mode_selector
  659. if (act_sym == 1)
  660. {
  661. return -1;
  662. }
  663. else
  664. {
  665. int pred_mode=0;
  666. pred_mode |= (biari_decode_symbol(dep_dp, ctx->ipr_contexts+1) );
  667. pred_mode |= (biari_decode_symbol(dep_dp, ctx->ipr_contexts+1) << 1);
  668. pred_mode |= (biari_decode_symbol(dep_dp, ctx->ipr_contexts+1) << 2);
  669. return pred_mode;
  670. }
  671. }
  672. #endif
  673. /*!
  674. ************************************************************************
  675. * \brief
  676. * This function is used to arithmetically decode the reference
  677. * parameter of a given MB.
  678. ************************************************************************
  679. */
  680. char readRefFrame_CABAC(Macroblock *currMB, DecodingEnvironmentPtr dep_dp, int list, int x, int y)
  681. {
  682. Slice *currSlice = currMB->p_Slice;
  683. VideoParameters *p_Vid = currMB->p_Vid;
  684. StorablePicture *dec_picture = p_Vid->dec_picture;
  685. MotionInfoContexts *ctx = currSlice->mot_ctx;
  686. Macroblock *neighborMB = NULL;
  687. int addctx = 0;
  688. int a = 0, b = 0;
  689. int act_ctx;
  690. int act_sym;
  691. PicMotion **refframe_array = dec_picture->motion.motion[list];
  692. PixelPos block_a, block_b;
  693. p_Vid->getNeighbourPXLuma(currMB, x, y - 1, &block_b);
  694. // TODO: this gets called with x << 2 and y << 2, so we can undo the internal >> 2 easily by just passing x and y
  695. if (block_b.available)
  696. {
  697. int b8b=((block_b.x >> 3) & 0x01)+((block_b.y>>2) & 0x02);
  698. neighborMB = &p_Vid->mb_data[block_b.mb_addr];
  699. if (!( (neighborMB->mb_type==IPCM) || IS_DIRECT(neighborMB) || (neighborMB->b8mode[b8b]==0 && neighborMB->b8pdir[b8b]==2)))
  700. {
  701. if (currSlice->mb_aff_frame_flag && (currMB->mb_field == FALSE) && (neighborMB->mb_field == TRUE))
  702. b = (refframe_array[block_b.pos_y>>2][block_b.pos_x>>2].ref_idx > 1 ? 2 : 0);
  703. else
  704. b = (refframe_array[block_b.pos_y>>2][block_b.pos_x>>2].ref_idx > 0 ? 2 : 0);
  705. }
  706. }
  707. p_Vid->getNeighbourXPLuma(currMB, x - 1, y , &block_a);
  708. if (block_a.available)
  709. {
  710. int b8a=((block_a.x >> 3) & 0x01)+((block_a.y>>2) & 0x02);
  711. neighborMB = &p_Vid->mb_data[block_a.mb_addr];
  712. if (!((neighborMB->mb_type==IPCM) || IS_DIRECT(neighborMB) || (neighborMB->b8mode[b8a]==0 && neighborMB->b8pdir[b8a]==2)))
  713. {
  714. if (currSlice->mb_aff_frame_flag && (currMB->mb_field == FALSE) && (neighborMB->mb_field == 1))
  715. a = (refframe_array[block_a.pos_y>>2][block_a.pos_x>>2].ref_idx > 1 ? 1 : 0);
  716. else
  717. a = (refframe_array[block_a.pos_y>>2][block_a.pos_x>>2].ref_idx > 0 ? 1 : 0);
  718. }
  719. }
  720. act_ctx = a + b;
  721. act_sym = biari_decode_symbol(dep_dp,ctx->ref_no_contexts[addctx] + act_ctx );
  722. if (act_sym != 0)
  723. {
  724. act_ctx = 4;
  725. act_sym = unary_bin_decode(dep_dp,ctx->ref_no_contexts[addctx] + act_ctx,1);
  726. ++act_sym;
  727. }
  728. return act_sym;
  729. }
  730. // x == 0
  731. char readRefFrame_CABAC0(Macroblock *currMB, DecodingEnvironmentPtr dep_dp, int list, int y)
  732. {
  733. Slice *currSlice = currMB->p_Slice;
  734. VideoParameters *p_Vid = currMB->p_Vid;
  735. StorablePicture *dec_picture = p_Vid->dec_picture;
  736. MotionInfoContexts *ctx = currSlice->mot_ctx;
  737. Macroblock *neighborMB = NULL;
  738. int addctx = 0;
  739. int a = 0, b = 0;
  740. int act_ctx;
  741. int act_sym;
  742. PicMotion **refframe_array = dec_picture->motion.motion[list];
  743. PixelPos block_a, block_b;
  744. p_Vid->getNeighbour0XLuma(currMB, y - 1, &block_b);
  745. // TODO: this gets called with x << 2 and y << 2, so we can undo the internal >> 2 easily by just passing x and y
  746. if (block_b.available)
  747. {
  748. int b8b=0+((block_b.y>>2) & 0x02);
  749. neighborMB = &p_Vid->mb_data[block_b.mb_addr];
  750. if (!( (neighborMB->mb_type==IPCM) || IS_DIRECT(neighborMB) || (neighborMB->b8mode[b8b]==0 && neighborMB->b8pdir[b8b]==2)))
  751. {
  752. if (currSlice->mb_aff_frame_flag && (currMB->mb_field == FALSE) && (neighborMB->mb_field == TRUE))
  753. b = (refframe_array[block_b.pos_y>>2][block_b.pos_x>>2].ref_idx > 1 ? 2 : 0);
  754. else
  755. b = (refframe_array[block_b.pos_y>>2][block_b.pos_x>>2].ref_idx > 0 ? 2 : 0);
  756. }
  757. }
  758. p_Vid->getNeighbourNXLuma(currMB, y , &block_a);
  759. if (block_a.available)
  760. {
  761. int b8a=((15 >> 3) & 0x01)+((block_a.y>>2) & 0x02);
  762. neighborMB = &p_Vid->mb_data[block_a.mb_addr];
  763. if (!((neighborMB->mb_type==IPCM) || IS_DIRECT(neighborMB) || (neighborMB->b8mode[b8a]==0 && neighborMB->b8pdir[b8a]==2)))
  764. {
  765. if (currSlice->mb_aff_frame_flag && (currMB->mb_field == FALSE) && (neighborMB->mb_field == 1))
  766. a = (refframe_array[block_a.pos_y>>2][block_a.pos_x>>2].ref_idx > 1 ? 1 : 0);
  767. else
  768. a = (refframe_array[block_a.pos_y>>2][block_a.pos_x>>2].ref_idx > 0 ? 1 : 0);
  769. }
  770. }
  771. act_ctx = a + b;
  772. act_sym = biari_decode_symbol(dep_dp,ctx->ref_no_contexts[addctx] + act_ctx );
  773. if (act_sym != 0)
  774. {
  775. act_ctx = 4;
  776. act_sym = unary_bin_decode(dep_dp,ctx->ref_no_contexts[addctx] + act_ctx,1);
  777. ++act_sym;
  778. }
  779. return act_sym;
  780. }
  781. /*!
  782. ************************************************************************
  783. * \brief
  784. * This function is used to arithmetically decode the delta qp
  785. * of a given MB.
  786. ************************************************************************
  787. */
  788. #if defined(_DEBUG) || !defined(_M_IX86)
  789. short readDquant_CABAC(Slice *currSlice, DecodingEnvironmentPtr dep_dp)
  790. {
  791. MotionInfoContexts *ctx = currSlice->mot_ctx;
  792. short dquant;
  793. int act_ctx = ((currSlice->last_dquant != 0) ? 1 : 0);
  794. int act_sym = biari_decode_symbol(dep_dp,ctx->delta_qp_contexts + act_ctx );
  795. if (act_sym != 0)
  796. {
  797. act_ctx = 2;
  798. act_sym = unary_bin_decode(dep_dp,ctx->delta_qp_contexts + act_ctx,1);
  799. ++act_sym;
  800. }
  801. dquant = (act_sym + 1) >> 1;
  802. if((act_sym & 0x01)==0) // lsb is signed bit
  803. dquant = -dquant;
  804. currSlice->last_dquant = dquant;
  805. return dquant;
  806. }
  807. #endif
  808. /*!
  809. ************************************************************************
  810. * \brief
  811. * This function is used to arithmetically decode the coded
  812. * block pattern of a given MB.
  813. ************************************************************************
  814. */
  815. int readCBP_CABAC(Macroblock *currMB, DecodingEnvironmentPtr dep_dp)
  816. {
  817. VideoParameters *p_Vid = currMB->p_Vid;
  818. StorablePicture *dec_picture = p_Vid->dec_picture;
  819. Slice *currSlice = currMB->p_Slice;
  820. TextureInfoContexts *ctx = currSlice->tex_ctx;
  821. Macroblock *neighborMB = NULL;
  822. int a, b;
  823. int curr_cbp_ctx;
  824. int cbp = 0;
  825. int cbp_bit;
  826. PixelPos block_a;
  827. // coding of luma part (bit by bit)
  828. neighborMB = currMB->mb_up;
  829. b = 0;
  830. if (neighborMB != NULL)
  831. {
  832. if(neighborMB->mb_type!=IPCM)
  833. b = (( (neighborMB->cbp & 4) == 0) ? 2 : 0);
  834. }
  835. p_Vid->getNeighbourLeftLuma(currMB, &block_a);
  836. if (block_a.available)
  837. {
  838. if(p_Vid->mb_data[block_a.mb_addr].mb_type==IPCM)
  839. a = 0;
  840. else
  841. a = (( (p_Vid->mb_data[block_a.mb_addr].cbp & (1<<(2*(block_a.y>>3)+1))) == 0) ? 1 : 0);
  842. }
  843. else
  844. a=0;
  845. curr_cbp_ctx = a + b;
  846. cbp_bit = biari_decode_symbol(dep_dp, ctx->cbp_contexts[0] + curr_cbp_ctx );
  847. //if (cbp_bit)
  848. cbp += cbp_bit;//1;
  849. if (neighborMB != NULL)
  850. {
  851. if(neighborMB->mb_type!=IPCM)
  852. b = (( (neighborMB->cbp & 8) == 0) ? 2 : 0);
  853. }
  854. a = ( ((cbp & 1) == 0) ? 1: 0);
  855. curr_cbp_ctx = a + b;
  856. cbp_bit = biari_decode_symbol(dep_dp, ctx->cbp_contexts[0] + curr_cbp_ctx );
  857. //if (cbp_bit)
  858. cbp += (cbp_bit << 1); //2;
  859. b = ( ((cbp & 1) == 0) ? 2: 0);
  860. p_Vid->getNeighbourNPLumaNB(currMB, 8, &block_a);
  861. if (block_a.available)
  862. {
  863. if(p_Vid->mb_data[block_a.mb_addr].mb_type==IPCM)
  864. a = 0;
  865. else
  866. a = (( (p_Vid->mb_data[block_a.mb_addr].cbp & (1<<(2*(block_a.y>>3)+1))) == 0) ? 1 : 0);
  867. }
  868. else
  869. a=0;
  870. curr_cbp_ctx = a + b;
  871. cbp_bit = biari_decode_symbol(dep_dp, ctx->cbp_contexts[0] + curr_cbp_ctx );
  872. //if (cbp_bit)
  873. cbp += (cbp_bit << 2); //4;
  874. b = ( ((cbp & 2) == 0) ? 2: 0);
  875. a = ( ((cbp & 4) == 0) ? 1: 0);
  876. curr_cbp_ctx = a + b;
  877. cbp_bit = biari_decode_symbol(dep_dp, ctx->cbp_contexts[0] + curr_cbp_ctx );
  878. //if (cbp_bit)
  879. cbp += (cbp_bit << 3); //8;
  880. if ((dec_picture->chroma_format_idc != YUV400) && (dec_picture->chroma_format_idc != YUV444))
  881. {
  882. // coding of chroma part
  883. // CABAC decoding for BinIdx 0
  884. b = 0;
  885. neighborMB = currMB->mb_up;
  886. if (neighborMB != NULL)
  887. {
  888. if (neighborMB->mb_type==IPCM || (neighborMB->cbp > 15))
  889. b = 2;
  890. }
  891. a = 0;
  892. neighborMB = currMB->mb_left;
  893. if (neighborMB != NULL)
  894. {
  895. if (neighborMB->mb_type==IPCM || (neighborMB->cbp > 15))
  896. a = 1;
  897. }
  898. curr_cbp_ctx = a + b;
  899. cbp_bit = biari_decode_symbol(dep_dp, ctx->cbp_contexts[1] + curr_cbp_ctx );
  900. // CABAC decoding for BinIdx 1
  901. if (cbp_bit) // set the chroma bits
  902. {
  903. b = 0;
  904. neighborMB = currMB->mb_up;
  905. if (neighborMB != NULL)
  906. {
  907. //if ((neighborMB->mb_type == IPCM) || ((neighborMB->cbp > 15) && ((neighborMB->cbp >> 4) == 2)))
  908. if ((neighborMB->mb_type == IPCM) || ((neighborMB->cbp >> 4) == 2))
  909. b = 2;
  910. }
  911. a = 0;
  912. neighborMB = currMB->mb_left;
  913. if (neighborMB != NULL)
  914. {
  915. if ((neighborMB->mb_type == IPCM) || ((neighborMB->cbp >> 4) == 2))
  916. a = 1;
  917. }
  918. curr_cbp_ctx = a + b;
  919. cbp_bit = biari_decode_symbol(dep_dp, ctx->cbp_contexts[2] + curr_cbp_ctx );
  920. cbp += (16 << cbp_bit); // ? 32 : 16;
  921. }
  922. }
  923. if (!cbp)
  924. {
  925. currSlice->last_dquant = 0;
  926. }
  927. return cbp;
  928. }
  929. /*!
  930. ************************************************************************
  931. * \brief
  932. * This function is used to arithmetically decode the chroma
  933. * intra prediction mode of a given MB.
  934. ************************************************************************
  935. */
  936. char readCIPredMode_CABAC(Macroblock *currMB,
  937. DecodingEnvironmentPtr dep_dp)
  938. {
  939. Slice *currSlice = currMB->p_Slice;
  940. TextureInfoContexts *ctx = currSlice->tex_ctx;
  941. int act_sym;
  942. Macroblock *MbUp = currMB->mb_up;
  943. Macroblock *MbLeft = currMB->mb_left;
  944. int b = (MbUp != NULL) ? (((MbUp->c_ipred_mode != 0) && (MbUp->mb_type != IPCM)) ? 1 : 0) : 0;
  945. int a = (MbLeft != NULL) ? (((MbLeft->c_ipred_mode != 0) && (MbLeft->mb_type != IPCM)) ? 1 : 0) : 0;
  946. int act_ctx = a + b;
  947. act_sym = biari_decode_symbol(dep_dp, ctx->cipr_contexts + act_ctx );
  948. if (act_sym != 0)
  949. act_sym = unary_bin_max_decode(dep_dp, ctx->cipr_contexts + 3, 0, 1) + 1;
  950. return act_sym;
  951. }
  952. static const byte maxpos [] = {15, 14, 63, 31, 31, 15, 3, 14, 7, 15, 15, 14, 63, 31, 31, 15, 15, 14, 63, 31, 31, 15};
  953. static const byte c1isdc [] = { 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1};
  954. static const byte type2ctx_bcbp[] = { 0, 1, 2, 3, 3, 4, 5, 6, 5, 5, 10, 11, 12, 13, 13, 14, 16, 17, 18, 19, 19, 20};
  955. static const byte type2ctx_map [] = { 0, 1, 2, 3, 4, 5, 6, 7, 6, 6, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21}; // 8
  956. static const byte type2ctx_last[] = { 0, 1, 2, 3, 4, 5, 6, 7, 6, 6, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21}; // 8
  957. static const byte type2ctx_one [] = { 0, 1, 2, 3, 3, 4, 5, 6, 5, 5, 10, 11, 12, 13, 13, 14, 16, 17, 18, 19, 19, 20}; // 7
  958. static const byte type2ctx_abs [] = { 0, 1, 2, 3, 3, 4, 5, 6, 5, 5, 10, 11, 12, 13, 13, 14, 16, 17, 18, 19, 19, 20}; // 7
  959. static const byte max_c2 [] = { 4, 4, 4, 4, 4, 4, 3, 4, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4}; // 9
  960. /*!
  961. ************************************************************************
  962. * \brief
  963. * Read CBP4-BIT
  964. ************************************************************************
  965. */
  966. static int read_and_store_CBP_block_bit_444(Macroblock *currMB, DecodingEnvironmentPtr dep_dp, int type)
  967. {
  968. Slice *currSlice = currMB->p_Slice;
  969. VideoParameters *p_Vid = currMB->p_Vid;
  970. StorablePicture *dec_picture = p_Vid->dec_picture;
  971. TextureInfoContexts *tex_ctx = currSlice->tex_ctx;
  972. int y_ac = (type==LUMA_16AC || type==LUMA_8x8 || type==LUMA_8x4 || type==LUMA_4x8 || type==LUMA_4x4
  973. || type==CB_16AC || type==CB_8x8 || type==CB_8x4 || type==CB_4x8 || type==CB_4x4
  974. || type==CR_16AC || type==CR_8x8 || type==CR_8x4 || type==CR_4x8 || type==CR_4x4);
  975. int y_dc = (type==LUMA_16DC || type==CB_16DC || type==CR_16DC);
  976. int u_ac = (type==CHROMA_AC && !currMB->is_v_block);
  977. int v_ac = (type==CHROMA_AC && currMB->is_v_block);
  978. int chroma_dc = (type==CHROMA_DC || type==CHROMA_DC_2x4 || type==CHROMA_DC_4x4);
  979. int u_dc = (chroma_dc && !currMB->is_v_block);
  980. int v_dc = (chroma_dc && currMB->is_v_block);
  981. int j = (y_ac || u_ac || v_ac ? currMB->subblock_y : 0);
  982. int i = (y_ac || u_ac || v_ac ? currMB->subblock_x : 0);
  983. int bit = (y_dc ? 0 : y_ac ? 1 : u_dc ? 17 : v_dc ? 18 : u_ac ? 19 : 35);
  984. int default_bit = (currMB->is_intra_block ? 1 : 0);
  985. int upper_bit = default_bit;
  986. int left_bit = default_bit;
  987. int cbp_bit = 1; // always one for 8x8 mode
  988. int ctx;
  989. int bit_pos_a = 0;
  990. int bit_pos_b = 0;
  991. PixelPos block_a, block_b;
  992. if (y_ac)
  993. {
  994. get4x4NeighbourLuma(currMB, i - 1, j , &block_a);
  995. get4x4NeighbourLuma(currMB, i , j - 1, &block_b);
  996. if (block_a.available)
  997. bit_pos_a = 4*block_a.y + block_a.x;
  998. if (block_b.available)
  999. bit_pos_b = 4*block_b.y + block_b.x;
  1000. }
  1001. else if (y_dc)
  1002. {
  1003. get4x4NeighbourLuma(currMB, i - 1, j , &block_a);
  1004. get4x4NeighbourLuma(currMB, i , j - 1, &block_b);
  1005. }
  1006. else if (u_ac||v_ac)
  1007. {
  1008. get4x4Neighbour(currMB, i - 1, j , p_Vid->mb_size[IS_CHROMA], &block_a);
  1009. get4x4Neighbour(currMB, i , j - 1, p_Vid->mb_size[IS_CHROMA], &block_b);
  1010. if (block_a.available)
  1011. bit_pos_a = 4*block_a.y + block_a.x;
  1012. if (block_b.available)
  1013. bit_pos_b = 4*block_b.y + block_b.x;
  1014. }
  1015. else
  1016. {
  1017. get4x4Neighbour(currMB, i - 1, j , p_Vid->mb_size[IS_CHROMA], &block_a);
  1018. get4x4Neighbour(currMB, i , j - 1, p_Vid->mb_size[IS_CHROMA], &block_b);
  1019. }
  1020. if (dec_picture->chroma_format_idc!=YUV444)
  1021. {
  1022. if (type!=LUMA_8x8)
  1023. {
  1024. //--- get bits from neighboring blocks ---
  1025. if (block_b.available)
  1026. {
  1027. if(p_Vid->mb_data[block_b.mb_addr].mb_type==IPCM)
  1028. upper_bit=1;
  1029. else
  1030. upper_bit = get_bit(p_Vid->mb_data[block_b.mb_addr].cbp_bits[0], bit + bit_pos_b);
  1031. }
  1032. if (block_a.available)
  1033. {
  1034. if(p_Vid->mb_data[block_a.mb_addr].mb_type==IPCM)
  1035. left_bit=1;
  1036. else
  1037. left_bit = get_bit(p_Vid->mb_data[block_a.mb_addr].cbp_bits[0],bit + bit_pos_a);
  1038. }
  1039. ctx = 2 * upper_bit + left_bit;
  1040. //===== encode symbol =====
  1041. cbp_bit = biari_decode_symbol (dep_dp, tex_ctx->bcbp_contexts[type2ctx_bcbp[type]] + ctx);
  1042. }
  1043. }
  1044. else if( IS_INDEPENDENT(p_Vid) )
  1045. {
  1046. if (type!=LUMA_8x8)
  1047. {
  1048. //--- get bits from neighbouring blocks ---
  1049. if (block_b.available)
  1050. {
  1051. if(p_Vid->mb_data[block_b.mb_addr].mb_type==IPCM)
  1052. upper_bit = 1;
  1053. else
  1054. upper_bit = get_bit(p_Vid->mb_data[block_b.mb_addr].cbp_bits[0],bit+bit_pos_b);
  1055. }
  1056. if (block_a.available)
  1057. {
  1058. if(p_Vid->mb_data[block_a.mb_addr].mb_type==IPCM)
  1059. left_bit = 1;
  1060. else
  1061. left_bit = get_bit(p_Vid->mb_data[block_a.mb_addr].cbp_bits[0],bit+bit_pos_a);
  1062. }
  1063. ctx = 2 * upper_bit + left_bit;
  1064. //===== encode symbol =====
  1065. cbp_bit = biari_decode_symbol (dep_dp, tex_ctx->bcbp_contexts[type2ctx_bcbp[type]] + ctx);
  1066. }
  1067. }
  1068. else {
  1069. if (block_b.available)
  1070. {
  1071. if(p_Vid->mb_data[block_b.mb_addr].mb_type==IPCM)
  1072. upper_bit=1;
  1073. else
  1074. {
  1075. if(type==LUMA_8x8)
  1076. upper_bit = get_bit(p_Vid->mb_data[block_b.mb_addr].cbp_bits_8x8[0], bit + bit_pos_b);
  1077. else if (type==CB_8x8)
  1078. upper_bit = get_bit(p_Vid->mb_data[block_b.mb_addr].cbp_bits_8x8[1], bit + bit_pos_b);
  1079. else if (type==CR_8x8)
  1080. upper_bit = get_bit(p_Vid->mb_data[block_b.mb_addr].cbp_bits_8x8[2], bit + bit_pos_b);
  1081. else if ((type==CB_4x4)||(type==CB_4x8)||(type==CB_8x4)||(type==CB_16AC)||(type==CB_16DC))
  1082. upper_bit = get_bit(p_Vid->mb_data[block_b.mb_addr].cbp_bits[1],bit+bit_pos_b);
  1083. else if ((type==CR_4x4)||(type==CR_4x8)||(type==CR_8x4)||(type==CR_16AC)||(type==CR_16DC))
  1084. upper_bit = get_bit(p_Vid->mb_data[block_b.mb_addr].cbp_bits[2],bit+bit_pos_b);
  1085. else
  1086. upper_bit = get_bit(p_Vid->mb_data[block_b.mb_addr].cbp_bits[0],bit+bit_pos_b);
  1087. }
  1088. }
  1089. if (block_a.available)
  1090. {
  1091. if(p_Vid->mb_data[block_a.mb_addr].mb_type==IPCM)
  1092. left_bit=1;
  1093. else
  1094. {
  1095. if(type==LUMA_8x8)
  1096. left_bit = get_bit(p_Vid->mb_data[block_a.mb_addr].cbp_bits_8x8[0],bit+bit_pos_a);
  1097. else if (type==CB_8x8)
  1098. left_bit = get_bit(p_Vid->mb_data[block_a.mb_addr].cbp_bits_8x8[1],bit+bit_pos_a);
  1099. else if (type==CR_8x8)
  1100. left_bit = get_bit(p_Vid->mb_data[block_a.mb_addr].cbp_bits_8x8[2],bit+bit_pos_a);
  1101. else if ((type==CB_4x4)||(type==CB_4x8)||(type==CB_8x4)||(type==CB_16AC)||(type==CB_16DC))
  1102. left_bit = get_bit(p_Vid->mb_data[block_a.mb_addr].cbp_bits[1],bit+bit_pos_a);
  1103. else if ((type==CR_4x4)||(type==CR_4x8)||(type==CR_8x4)||(type==CR_16AC)||(type==CR_16DC))
  1104. left_bit = get_bit(p_Vid->mb_data[block_a.mb_addr].cbp_bits[2],bit+bit_pos_a);
  1105. else
  1106. left_bit = get_bit(p_Vid->mb_data[block_a.mb_addr].cbp_bits[0],bit+bit_pos_a);
  1107. }
  1108. }
  1109. ctx = 2 * upper_bit + left_bit;
  1110. //===== encode symbol =====
  1111. cbp_bit = biari_decode_symbol (dep_dp, tex_ctx->bcbp_contexts[type2ctx_bcbp[type]] + ctx);
  1112. }
  1113. //--- set bits for current block ---
  1114. bit = (y_dc ? 0 : y_ac ? 1 + j + (i >> 2) : u_dc ? 17 : v_dc ? 18 : u_ac ? 19 + j + (i >> 2) : 35 + j + (i >> 2));
  1115. if (cbp_bit)
  1116. {
  1117. if (type==LUMA_8x8)
  1118. {
  1119. currMB->cbp_bits[0] |= ((int64) 0x33 << bit );
  1120. if (dec_picture->chroma_format_idc==YUV444)
  1121. {
  1122. currMB->cbp_bits_8x8[0] |= ((int64) 0x33 << bit );
  1123. }
  1124. }
  1125. else if (type==CB_8x8)
  1126. {
  1127. currMB->cbp_bits_8x8[1] |= ((int64) 0x33 << bit );
  1128. currMB->cbp_bits[1] |= ((int64) 0x33 << bit );
  1129. }
  1130. else if (type==CR_8x8)
  1131. {
  1132. currMB->cbp_bits_8x8[2] |= ((int64) 0x33 << bit );
  1133. currMB->cbp_bits[2] |= ((int64) 0x33 << bit );
  1134. }
  1135. else if (type==LUMA_8x4)
  1136. {
  1137. currMB->cbp_bits[0] |= ((int64) 0x03 << bit );
  1138. }
  1139. else if (type==CB_8x4)
  1140. {
  1141. currMB->cbp_bits[1] |= ((int64) 0x03 << bit );
  1142. }
  1143. else if (type==CR_8x4)
  1144. {
  1145. currMB->cbp_bits[2] |= ((int64) 0x03 << bit );
  1146. }
  1147. else if (type==LUMA_4x8)
  1148. {
  1149. currMB->cbp_bits[0] |= ((int64) 0x11<< bit );
  1150. }
  1151. else if (type==CB_4x8)
  1152. {
  1153. currMB->cbp_bits[1] |= ((int64)0x11<< bit );
  1154. }
  1155. else if (type==CR_4x8)
  1156. {
  1157. currMB->cbp_bits[2] |= ((int64)0x11<< bit );
  1158. }
  1159. else if ((type==CB_4x4)||(type==CB_16AC)||(type==CB_16DC))
  1160. {
  1161. currMB->cbp_bits[1] |= ((int64)0x01<<bit);
  1162. }
  1163. else if ((type==CR_4x4)||(type==CR_16AC)||(type==CR_16DC))
  1164. {
  1165. currMB->cbp_bits[2] |= ((int64)0x01<<bit);
  1166. }
  1167. else
  1168. {
  1169. currMB->cbp_bits[0] |= ((int64)0x01<<bit);
  1170. }
  1171. }
  1172. return cbp_bit;
  1173. }
  1174. /*!
  1175. ************************************************************************
  1176. * \brief
  1177. * Read CBP4-BIT
  1178. ************************************************************************
  1179. */
  1180. static int read_and_store_CBP_block_bit_normal(Macroblock *currMB, DecodingEnvironmentPtr dep_dp, int type)
  1181. {
  1182. Slice *currSlice = currMB->p_Slice;
  1183. VideoParameters *p_Vid = currMB->p_Vid;
  1184. TextureInfoContexts *tex_ctx = currSlice->tex_ctx;
  1185. int cbp_bit = 1; // always one for 8x8 mode
  1186. if (type==LUMA_16DC)
  1187. {
  1188. int upper_bit = 1;
  1189. int left_bit = 1;
  1190. int ctx;
  1191. PixelPos block_a, block_b;
  1192. //--- get bits from neighboring blocks ---
  1193. p_Vid->getNeighbour0X(currMB, -1, p_Vid->mb_size[IS_LUMA], &block_b);
  1194. if (block_b.available)
  1195. {
  1196. if(p_Vid->mb_data[block_b.mb_addr].mb_type==IPCM)
  1197. upper_bit=1;
  1198. else
  1199. upper_bit = (int)p_Vid->mb_data[block_b.mb_addr].cbp_bits[0]&1;
  1200. }
  1201. p_Vid->getNeighbourX0(currMB, -1, p_Vid->mb_size[IS_LUMA], &block_a);
  1202. if (block_a.available)
  1203. {
  1204. if(p_Vid->mb_data[block_a.mb_addr].mb_type==IPCM)
  1205. left_bit=1;
  1206. else
  1207. left_bit = (int)p_Vid->mb_data[block_a.mb_addr].cbp_bits[0]&1;
  1208. }
  1209. ctx = 2 * upper_bit + left_bit;
  1210. //===== encode symbol =====
  1211. cbp_bit = biari_decode_symbol (dep_dp, tex_ctx->bcbp_contexts[type2ctx_bcbp[LUMA_16DC]] + ctx);
  1212. //--- set bits for current block ---
  1213. if (cbp_bit)
  1214. {
  1215. currMB->cbp_bits[0] |= 0x01;
  1216. }
  1217. }
  1218. else if (type == LUMA_8x8)
  1219. {
  1220. int j = currMB->subblock_y;
  1221. int i = currMB->subblock_x;
  1222. //--- set bits for current block ---
  1223. int bit = 1 + j + (i >> 2);
  1224. or_bits(&currMB->cbp_bits[0], 0x33, bit);
  1225. }
  1226. else if (type <= LUMA_4x4) // type==LUMA_16AC || type==LUMA_8x4 || type==LUMA_4x8 || type==LUMA_4x4)
  1227. {
  1228. int j = currMB->subblock_y;
  1229. int i = currMB->subblock_x;
  1230. int bit;
  1231. int default_bit = (currMB->is_intra_block ? 1 : 0);
  1232. int upper_bit = default_bit;
  1233. int left_bit = default_bit;
  1234. int ctx;
  1235. //--- get bits from neighboring blocks ---
  1236. PixelPos block_a, block_b;
  1237. p_Vid->getNeighbourPXLumaNB_NoPos(currMB, j-1, &block_b);
  1238. if (block_b.available)
  1239. {
  1240. int bit_pos_b = (block_b.y&((short)~3)) + (i>>2);
  1241. if(p_Vid->mb_data[block_b.mb_addr].mb_type==IPCM)
  1242. upper_bit=1;
  1243. else
  1244. upper_bit = get_bit(p_Vid->mb_data[block_b.mb_addr].cbp_bits[0], 1 + bit_pos_b);
  1245. }
  1246. p_Vid->getNeighbourXPLumaNB_NoPos(currMB, i-1, j, &block_a);
  1247. if (block_a.available)
  1248. {
  1249. int bit_pos_a = (block_a.y&((short)~3)) + (block_a.x>>2);
  1250. if(p_Vid->mb_data[block_a.mb_addr].mb_type==IPCM)
  1251. left_bit=1;
  1252. else
  1253. left_bit = get_bit(p_Vid->mb_data[block_a.mb_addr].cbp_bits[0],1 + bit_pos_a);
  1254. }
  1255. ctx = 2 * upper_bit + left_bit;
  1256. //===== encode symbol =====
  1257. cbp_bit = biari_decode_symbol (dep_dp, tex_ctx->bcbp_contexts[type2ctx_bcbp[type]] + ctx);
  1258. //--- set bits for current block ---
  1259. bit = 1 + j + (i >> 2);
  1260. if (cbp_bit)
  1261. {
  1262. if (type==LUMA_8x4)
  1263. {
  1264. or_bits_low(&currMB->cbp_bits[0], 0x03, bit);
  1265. }
  1266. else if (type==LUMA_4x8)
  1267. {
  1268. or_bits_low(&currMB->cbp_bits[0], 0x011, bit);
  1269. }
  1270. else
  1271. {
  1272. or_bits_low(&currMB->cbp_bits[0], 0x01, bit);
  1273. }
  1274. }
  1275. }
  1276. else if (type == CHROMA_AC)
  1277. {
  1278. int u_ac = !currMB->is_v_block;
  1279. int default_bit = (currMB->is_intra_block ? 1 : 0);
  1280. int upper_bit = default_bit;
  1281. int left_bit = default_bit;
  1282. int ctx;
  1283. PixelPos block_a, block_b;
  1284. int j = currMB->subblock_y;
  1285. int i = currMB->subblock_x;
  1286. int bit = (u_ac ? 19 : 35);
  1287. p_Vid->getNeighbourXP_NoPos(currMB, i - 1, j , p_Vid->mb_size[IS_CHROMA], &block_a);
  1288. p_Vid->getNeighbourPX_NoPos(currMB, i , j - 1, p_Vid->mb_size[IS_CHROMA], &block_b);
  1289. //--- get bits from neighboring blocks ---
  1290. if (block_b.available)
  1291. {
  1292. if(p_Vid->mb_data[block_b.mb_addr].mb_type==IPCM)
  1293. upper_bit=1;
  1294. else
  1295. upper_bit = get_bit(p_Vid->mb_data[block_b.mb_addr].cbp_bits[0], bit + (block_b.y&((short)~3)) + (block_b.x>>2));
  1296. }
  1297. if (block_a.available)
  1298. {
  1299. if(p_Vid->mb_data[block_a.mb_addr].mb_type==IPCM)
  1300. left_bit=1;
  1301. else
  1302. left_bit = get_bit(p_Vid->mb_data[block_a.mb_addr].cbp_bits[0],bit + (block_a.y&((short)~3)) + (block_a.x>>2));
  1303. }
  1304. ctx = 2 * upper_bit + left_bit;
  1305. //===== encode symbol =====
  1306. cbp_bit = biari_decode_symbol (dep_dp, tex_ctx->bcbp_contexts[type2ctx_bcbp[CHROMA_AC]] + ctx);
  1307. //--- set bits for current block ---
  1308. if (cbp_bit)
  1309. {
  1310. or_bits(&currMB->cbp_bits[0], 0x01, bit + j + (i >> 2));
  1311. }
  1312. }
  1313. else if (type <= CHROMA_DC_4x4)
  1314. {
  1315. int v_dc = currMB->is_v_block;
  1316. int default_bit = (currMB->is_intra_block ? 1 : 0);
  1317. int upper_bit = default_bit;
  1318. int left_bit = default_bit;
  1319. int ctx;
  1320. PixelPos block_a, block_b;
  1321. int bit = (v_dc ? 18 : 17);
  1322. p_Vid->getNeighbourLeft(currMB, p_Vid->mb_size[IS_CHROMA], &block_a);
  1323. p_Vid->getNeighbourUp(currMB, p_Vid->mb_size[IS_CHROMA], &block_b);
  1324. //--- get bits from neighboring blocks ---
  1325. if (block_b.available)
  1326. {
  1327. if(p_Vid->mb_data[block_b.mb_addr].mb_type==IPCM)
  1328. upper_bit=1;
  1329. else
  1330. upper_bit = get_bit(p_Vid->mb_data[block_b.mb_addr].cbp_bits[0], bit);
  1331. }
  1332. if (block_a.available)
  1333. {
  1334. if(p_Vid->mb_data[block_a.mb_addr].mb_type==IPCM)
  1335. left_bit=1;
  1336. else
  1337. left_bit = get_bit(p_Vid->mb_data[block_a.mb_addr].cbp_bits[0],bit);
  1338. }
  1339. ctx = 2 * upper_bit + left_bit;
  1340. //===== encode symbol =====
  1341. cbp_bit = biari_decode_symbol (dep_dp, tex_ctx->bcbp_contexts[type2ctx_bcbp[type]] + ctx);
  1342. //--- set bits for current block ---
  1343. if (cbp_bit)
  1344. {
  1345. or_bits(&currMB->cbp_bits[0], 0x01, bit);
  1346. }
  1347. }
  1348. else
  1349. {
  1350. int default_bit = (currMB->is_intra_block ? 1 : 0);
  1351. int upper_bit = default_bit;
  1352. int left_bit = default_bit;
  1353. int ctx;
  1354. PixelPos block_a, block_b;
  1355. p_Vid->getNeighbourLeft(currMB, p_Vid->mb_size[IS_CHROMA], &block_a);
  1356. p_Vid->getNeighbourUp(currMB, p_Vid->mb_size[IS_CHROMA], &block_b);
  1357. //--- get bits from neighboring blocks ---
  1358. if (block_b.available)
  1359. {
  1360. if(p_Vid->mb_data[block_b.mb_addr].mb_type==IPCM)
  1361. upper_bit=1;
  1362. else
  1363. upper_bit = get_bit(p_Vid->mb_data[block_b.mb_addr].cbp_bits[0], 35);
  1364. }
  1365. if (block_a.available)
  1366. {
  1367. if(p_Vid->mb_data[block_a.mb_addr].mb_type==IPCM)
  1368. left_bit=1;
  1369. else
  1370. left_bit = get_bit(p_Vid->mb_data[block_a.mb_addr].cbp_bits[0],35);
  1371. }
  1372. ctx = 2 * upper_bit + left_bit;
  1373. //===== encode symbol =====
  1374. cbp_bit = biari_decode_symbol (dep_dp, tex_ctx->bcbp_contexts[type2ctx_bcbp[type]] + ctx);
  1375. //--- set bits for current block ---
  1376. if (cbp_bit)
  1377. {
  1378. or_bits(&currMB->cbp_bits[0], 0x01, 35);
  1379. }
  1380. }
  1381. return cbp_bit;
  1382. }
  1383. void set_read_and_store_CBP(Macroblock **currMB, int chroma_format_idc)
  1384. {
  1385. if (chroma_format_idc == YUV444)
  1386. (*currMB)->read_and_store_CBP_block_bit = read_and_store_CBP_block_bit_444;
  1387. else
  1388. (*currMB)->read_and_store_CBP_block_bit = read_and_store_CBP_block_bit_normal;
  1389. }
  1390. //===== position -> ctx for MAP =====
  1391. //--- zig-zag scan ----
  1392. static const byte pos2ctx_map8x8 [] = { 0, 1, 2, 3, 4, 5, 5, 4, 4, 3, 3, 4, 4, 4, 5, 5,
  1393. 4, 4, 4, 4, 3, 3, 6, 7, 7, 7, 8, 9, 10, 9, 8, 7,
  1394. 7, 6, 11, 12, 13, 11, 6, 7, 8, 9, 14, 10, 9, 8, 6, 11,
  1395. 12, 13, 11, 6, 9, 14, 10, 9, 11, 12, 13, 11 ,14, 10, 12, 14}; // 15 CTX
  1396. static const byte pos2ctx_map8x4 [] = { 0, 1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 9, 8, 6, 7, 8,
  1397. 9, 10, 11, 9, 8, 6, 12, 8, 9, 10, 11, 9, 13, 13, 14, 14}; // 15 CTX
  1398. static const byte pos2ctx_map4x4 [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 14}; // 15 CTX
  1399. static const byte pos2ctx_map2x4c[] = { 0, 0, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2}; // 15 CTX
  1400. static const byte pos2ctx_map4x4c[] = { 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2}; // 15 CTX
  1401. static const byte* pos2ctx_map [] = {pos2ctx_map4x4, pos2ctx_map4x4, pos2ctx_map8x8, pos2ctx_map8x4,
  1402. pos2ctx_map8x4, pos2ctx_map4x4, pos2ctx_map4x4, pos2ctx_map4x4,
  1403. pos2ctx_map2x4c, pos2ctx_map4x4c,
  1404. pos2ctx_map4x4, pos2ctx_map4x4, pos2ctx_map8x8,pos2ctx_map8x4,
  1405. pos2ctx_map8x4, pos2ctx_map4x4,
  1406. pos2ctx_map4x4, pos2ctx_map4x4, pos2ctx_map8x8,pos2ctx_map8x4,
  1407. pos2ctx_map8x4,pos2ctx_map4x4};
  1408. //--- interlace scan ----
  1409. //taken from ABT
  1410. static const byte pos2ctx_map8x8i[] = { 0, 1, 1, 2, 2, 3, 3, 4, 5, 6, 7, 7, 7, 8, 4, 5,
  1411. 6, 9, 10, 10, 8, 11, 12, 11, 9, 9, 10, 10, 8, 11, 12, 11,
  1412. 9, 9, 10, 10, 8, 11, 12, 11, 9, 9, 10, 10, 8, 13, 13, 9,
  1413. 9, 10, 10, 8, 13, 13, 9, 9, 10, 10, 14, 14, 14, 14, 14, 14}; // 15 CTX
  1414. static const byte pos2ctx_map8x4i[] = { 0, 1, 2, 3, 4, 5, 6, 3, 4, 5, 6, 3, 4, 7, 6, 8,
  1415. 9, 7, 6, 8, 9, 10, 11, 12, 12, 10, 11, 13, 13, 14, 14, 14}; // 15 CTX
  1416. static const byte pos2ctx_map4x8i[] = { 0, 1, 1, 1, 2, 3, 3, 4, 4, 4, 5, 6, 2, 7, 7, 8,
  1417. 8, 8, 5, 6, 9, 10, 10, 11, 11, 11, 12, 13, 13, 14, 14, 14}; // 15 CTX
  1418. static const byte* pos2ctx_map_int[] = {pos2ctx_map4x4, pos2ctx_map4x4, pos2ctx_map8x8i,pos2ctx_map8x4i,
  1419. pos2ctx_map4x8i,pos2ctx_map4x4, pos2ctx_map4x4, pos2ctx_map4x4,
  1420. pos2ctx_map2x4c, pos2ctx_map4x4c,
  1421. pos2ctx_map4x4, pos2ctx_map4x4, pos2ctx_map8x8i,pos2ctx_map8x4i,
  1422. pos2ctx_map8x4i,pos2ctx_map4x4,
  1423. pos2ctx_map4x4, pos2ctx_map4x4, pos2ctx_map8x8i,pos2ctx_map8x4i,
  1424. pos2ctx_map8x4i,pos2ctx_map4x4};
  1425. //===== position -> ctx for LAST =====
  1426. static const byte pos2ctx_last8x8 [] = { 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1427. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  1428. 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
  1429. 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8}; // 9 CTX
  1430. static const byte pos2ctx_last8x4 [] = { 0, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2,
  1431. 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8}; // 9 CTX
  1432. static const byte pos2ctx_last4x4 [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}; // 15 CTX
  1433. static const byte pos2ctx_last2x4c[] = { 0, 0, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2}; // 15 CTX
  1434. static const byte pos2ctx_last4x4c[] = { 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2}; // 15 CTX
  1435. static const byte* pos2ctx_last [] = {pos2ctx_last4x4, pos2ctx_last4x4, pos2ctx_last8x8, pos2ctx_last8x4,
  1436. pos2ctx_last8x4, pos2ctx_last4x4, pos2ctx_last4x4, pos2ctx_last4x4,
  1437. pos2ctx_last2x4c, pos2ctx_last4x4c,
  1438. pos2ctx_last4x4, pos2ctx_last4x4, pos2ctx_last8x8,pos2ctx_last8x4,
  1439. pos2ctx_last8x4, pos2ctx_last4x4,
  1440. pos2ctx_last4x4, pos2ctx_last4x4, pos2ctx_last8x8,pos2ctx_last8x4,
  1441. pos2ctx_last8x4, pos2ctx_last4x4};
  1442. /*!
  1443. ************************************************************************
  1444. * \brief
  1445. * Read Significance MAP
  1446. ************************************************************************
  1447. */
  1448. #if defined(_DEBUG) || defined(_M_X64)
  1449. static int read_significance_map(TextureInfoContexts *tex_ctx, const Macroblock *currMB, DecodingEnvironmentPtr dep_dp, int type, int16_t coeff[])
  1450. {
  1451. int i;
  1452. int coeff_ctr = 0;
  1453. int i0 = 0;
  1454. int i1 = maxpos[type];
  1455. const VideoParameters *p_Vid = currMB->p_Vid;
  1456. int fld = ( p_Vid->structure!=FRAME || currMB->mb_field );
  1457. const byte *pos2ctx_Map = (fld) ? pos2ctx_map_int[type] : pos2ctx_map[type];
  1458. const byte *last = pos2ctx_last[type];
  1459. BiContextTypePtr map_ctx = tex_ctx->map_contexts[fld][type2ctx_map [type]];
  1460. BiContextTypePtr last_ctx = tex_ctx->last_contexts[fld][type2ctx_last[type]];
  1461. if (!c1isdc[type])
  1462. {
  1463. pos2ctx_Map++;
  1464. last++;
  1465. }
  1466. for (i=0; i < i1; ++i) // if last coeff is reached, it has to be significant
  1467. {
  1468. //--- read significance symbol ---
  1469. if (biari_decode_symbol (dep_dp, map_ctx + pos2ctx_Map[i]))
  1470. {
  1471. coeff[i] = 1;
  1472. ++coeff_ctr;
  1473. //--- read last coefficient symbol ---
  1474. if (biari_decode_symbol (dep_dp, last_ctx + last[i]))
  1475. {
  1476. while (i++ < i1)
  1477. {
  1478. coeff[i] = 0;
  1479. }
  1480. return coeff_ctr;
  1481. //memset(&coeff[i + 1], 0, (i1 - i) * sizeof(int));
  1482. //i = i1;
  1483. }
  1484. }
  1485. else
  1486. {
  1487. coeff[i] = 0;
  1488. }
  1489. }
  1490. //--- last coefficient must be significant if no last symbol was received ---
  1491. coeff[i] = 1;
  1492. return coeff_ctr+1;
  1493. }
  1494. #endif
  1495. /*!
  1496. ************************************************************************
  1497. * \brief
  1498. * Read Levels
  1499. ************************************************************************
  1500. */
  1501. #if defined(_DEBUG) || defined(_M_X64)
  1502. /*!
  1503. ************************************************************************
  1504. * \brief
  1505. * Exp-Golomb decoding for LEVELS
  1506. ***********************************************************************
  1507. */
  1508. unsigned int exp_golomb_decode_eq_prob( DecodingEnvironmentPtr dep_dp, int k);
  1509. static unsigned int unary_exp_golomb_level_decode( DecodingEnvironmentPtr dep_dp,
  1510. BiContextTypePtr ctx)
  1511. {
  1512. unsigned int symbol = biari_decode_symbol(dep_dp, ctx );
  1513. if (symbol==0)
  1514. return 0;
  1515. else
  1516. {
  1517. const unsigned int exp_start = 13;
  1518. for (symbol=0;symbol<(exp_start-1);symbol++)
  1519. {
  1520. if (!biari_decode_symbol(dep_dp, ctx))
  1521. return symbol;
  1522. }
  1523. return exp_golomb_decode_eq_prob(dep_dp,0)+13;
  1524. }
  1525. }
  1526. static void read_significant_coefficients (TextureInfoContexts *tex_ctx,
  1527. DecodingEnvironmentPtr dep_dp,
  1528. int type,
  1529. int16_t coeff[])
  1530. {
  1531. static const int plus_one_clip4[5] = { 1, 2, 3, 4, 4 };
  1532. static const int plus_one_clip3[4] = { 1, 2, 3, 3 };
  1533. const int *c2_clip = (max_c2[type]==4)?plus_one_clip4:plus_one_clip3;
  1534. int i;
  1535. int c1 = 1;
  1536. int c2 = 0;
  1537. BiContextType *one_contexts = tex_ctx->one_contexts[type2ctx_one[type]];
  1538. BiContextType *abs_contexts = tex_ctx->abs_contexts[type2ctx_abs[type]];
  1539. for (i=maxpos[type]; i>=0; i--)
  1540. {
  1541. if (coeff[i]!=0)
  1542. {
  1543. coeff[i] += biari_decode_symbol (dep_dp, one_contexts + c1);
  1544. if (coeff[i]==2)
  1545. {
  1546. coeff[i] += unary_exp_golomb_level_decode (dep_dp, abs_contexts + c2);
  1547. c2 = c2_clip[c2];
  1548. c1=0;
  1549. }
  1550. else if (c1)
  1551. {
  1552. c1 = plus_one_clip4[c1];
  1553. }
  1554. if (biari_decode_symbol_eq_prob(dep_dp))
  1555. {
  1556. coeff[i] *= -1;
  1557. }
  1558. }
  1559. }
  1560. }
  1561. #else
  1562. void read_significant_coefficients (TextureInfoContexts *tex_ctx,
  1563. DecodingEnvironmentPtr dep_dp,
  1564. int type,
  1565. int coeff[]);
  1566. #endif
  1567. /*!
  1568. ************************************************************************
  1569. * \brief
  1570. * Read Block-Transform Coefficients
  1571. ************************************************************************
  1572. */
  1573. #if defined(_DEBUG) || defined(_M_X64)
  1574. RunLevel readRunLevel_CABAC(Macroblock *currMB, DecodingEnvironmentPtr dep_dp, int context)
  1575. {
  1576. RunLevel rl;
  1577. Slice *currSlice = currMB->p_Slice;
  1578. //--- read coefficients for whole block ---
  1579. if (currSlice->coeff_ctr < 0)
  1580. {
  1581. //===== decode CBP-BIT =====
  1582. if ((currSlice->coeff_ctr = currMB->read_and_store_CBP_block_bit (currMB, dep_dp, context) )!=0)
  1583. {
  1584. //===== decode significance map =====
  1585. currSlice->coeff_ctr = read_significance_map (currSlice->tex_ctx, currMB, dep_dp, context, currSlice->coeff);
  1586. //===== decode significant coefficients =====
  1587. read_significant_coefficients (currSlice->tex_ctx, dep_dp, context, currSlice->coeff);
  1588. }
  1589. }
  1590. //--- set run and level ---
  1591. rl.run=0;
  1592. if (currSlice->coeff_ctr--)
  1593. {
  1594. //--- set run and level (coefficient) ---
  1595. for (; currSlice->coeff[currSlice->pos] == 0; ++currSlice->pos, ++rl.run);
  1596. rl.level = currSlice->coeff[currSlice->pos++];
  1597. //--- decrement coefficient counter and re-set position ---
  1598. if (currSlice->coeff_ctr == 0)
  1599. currSlice->pos = 0;
  1600. return rl;
  1601. }
  1602. else
  1603. {
  1604. //--- set run and level (EOB) ---
  1605. currSlice->pos = 0;
  1606. rl.level = 0;
  1607. return rl;
  1608. }
  1609. }
  1610. #endif
  1611. /*!
  1612. ************************************************************************
  1613. * \brief
  1614. * arideco_bits_read
  1615. ************************************************************************
  1616. */
  1617. static int arideco_bits_read(const DecodingEnvironmentPtr dep)
  1618. {
  1619. int tmp = ((*dep->Dcodestrm_len) << 3) - dep->DbitsLeft;
  1620. #if (2==TRACE)
  1621. fprintf(p_trace, "tmp: %d\n", tmp);
  1622. #endif
  1623. return tmp;
  1624. }
  1625. /*!
  1626. ************************************************************************
  1627. * \brief
  1628. * decoding of unary binarization using one or 2 distinct
  1629. * models for the first and all remaining bins; no terminating
  1630. * "0" for max_symbol
  1631. ***********************************************************************
  1632. */
  1633. static unsigned int unary_bin_max_decode(DecodingEnvironmentPtr dep_dp,
  1634. BiContextTypePtr ctx,
  1635. int ctx_offset,
  1636. unsigned int max_symbol)
  1637. {
  1638. unsigned int symbol = biari_decode_symbol(dep_dp, ctx );
  1639. if (symbol==0 || (max_symbol == 0))
  1640. return symbol;
  1641. else
  1642. {
  1643. unsigned int l;
  1644. ctx += ctx_offset;
  1645. symbol = 0;
  1646. do
  1647. {
  1648. l = biari_decode_symbol(dep_dp, ctx);
  1649. ++symbol;
  1650. }
  1651. while( (l != 0) && (symbol < max_symbol) );
  1652. if ((l != 0) && (symbol == max_symbol))
  1653. ++symbol;
  1654. return symbol;
  1655. }
  1656. }
  1657. /*!
  1658. ************************************************************************
  1659. * \brief
  1660. * decoding of unary binarization using one or 2 distinct
  1661. * models for the first and all remaining bins
  1662. ***********************************************************************
  1663. */
  1664. static unsigned int unary_bin_decode(DecodingEnvironmentPtr dep_dp,
  1665. BiContextTypePtr ctx,
  1666. int ctx_offset)
  1667. {
  1668. unsigned int symbol = biari_decode_symbol(dep_dp, ctx );
  1669. if (symbol == 0)
  1670. return 0;
  1671. else
  1672. {
  1673. unsigned int l;
  1674. ctx += ctx_offset;;
  1675. symbol = 0;
  1676. do
  1677. {
  1678. l=biari_decode_symbol(dep_dp, ctx);
  1679. ++symbol;
  1680. }
  1681. while( l != 0 );
  1682. return symbol;
  1683. }
  1684. }
  1685. /*!
  1686. ************************************************************************
  1687. * \brief
  1688. * finding end of a slice in case this is not the end of a frame
  1689. *
  1690. * Unsure whether the "correction" below actually solves an off-by-one
  1691. * problem or whether it introduces one in some cases :-( Anyway,
  1692. * with this change the bit stream format works with CABAC again.
  1693. * StW, 8.7.02
  1694. ************************************************************************
  1695. */
  1696. int cabac_startcode_follows(Slice *currSlice, int eos_bit)
  1697. {
  1698. unsigned int bit;
  1699. if( eos_bit )
  1700. {
  1701. const byte *partMap = assignSE2partition[currSlice->dp_mode];
  1702. DataPartition *dP = &(currSlice->partArr[partMap[SE_MBTYPE]]);
  1703. DecodingEnvironmentPtr dep_dp = &(dP->de_cabac);
  1704. bit = biari_decode_final (dep_dp); //GB
  1705. #if TRACE
  1706. fprintf(p_trace, "@%-6d %-63s (%3d)\n",symbolCount++, "end_of_slice_flag", bit);
  1707. fflush(p_trace);
  1708. #endif
  1709. }
  1710. else
  1711. {
  1712. bit = 0;
  1713. }
  1714. return bit;
  1715. }
  1716. /*!
  1717. ************************************************************************
  1718. * \brief
  1719. * Exp Golomb binarization and decoding of a symbol
  1720. * with prob. of 0.5r
  1721. ************************************************************************
  1722. */
  1723. unsigned int exp_golomb_decode_eq_prob( DecodingEnvironmentPtr dep_dp, int k)
  1724. {
  1725. unsigned int l;
  1726. int symbol = 0;
  1727. int binary_symbol = 0;
  1728. do
  1729. {
  1730. l = biari_decode_symbol_eq_prob(dep_dp);
  1731. if (l) // always returns 1 or zero
  1732. {
  1733. symbol += (l<<k); // l is guaranteed to be one
  1734. ++k;
  1735. }
  1736. }
  1737. while (l!=0);
  1738. while (k--) //next binary part
  1739. if (biari_decode_symbol_eq_prob(dep_dp)==1)
  1740. binary_symbol |= (1<<k);
  1741. return (unsigned int) (symbol + binary_symbol);
  1742. }
  1743. /*!
  1744. ************************************************************************
  1745. * \brief
  1746. * Exp-Golomb decoding for Motion Vectors
  1747. ***********************************************************************
  1748. */
  1749. #if defined(_DEBUG) || defined(_M_X64)
  1750. unsigned int unary_exp_golomb_mv_decode(DecodingEnvironmentPtr dep_dp,
  1751. BiContextTypePtr ctx,
  1752. unsigned int max_bin)
  1753. {
  1754. unsigned int symbol = biari_decode_symbol(dep_dp, ctx );
  1755. if (symbol == 0)
  1756. return 0;
  1757. else
  1758. {
  1759. const unsigned int exp_start = 8;
  1760. ++ctx;
  1761. for (symbol=1;symbol<exp_start;)
  1762. {
  1763. if (!biari_decode_symbol(dep_dp, ctx))
  1764. return symbol;
  1765. if ((++symbol)==2) ctx++;
  1766. if (symbol==max_bin)
  1767. ++ctx;
  1768. }
  1769. return exp_start + exp_golomb_decode_eq_prob(dep_dp,3);
  1770. }
  1771. }
  1772. unsigned int unary_exp_golomb_mv_decode3(DecodingEnvironmentPtr dep_dp,
  1773. BiContextTypePtr ctx)
  1774. {
  1775. unsigned int max_bin = 3;
  1776. unsigned int symbol = biari_decode_symbol(dep_dp, ctx );
  1777. if (symbol == 0)
  1778. return 0;
  1779. else
  1780. {
  1781. const unsigned int exp_start = 8;
  1782. ++ctx;
  1783. for (symbol=1;symbol<exp_start;)
  1784. {
  1785. if (!biari_decode_symbol(dep_dp, ctx))
  1786. return symbol;
  1787. if ((++symbol)==2) ctx++;
  1788. if (symbol==max_bin)
  1789. ++ctx;
  1790. }
  1791. return exp_start + exp_golomb_decode_eq_prob(dep_dp,3);
  1792. }
  1793. }
  1794. #endif
  1795. /*!
  1796. ************************************************************************
  1797. * \brief
  1798. * Read I_PCM macroblock
  1799. ************************************************************************
  1800. */
  1801. void readIPCM_CABAC(Slice *currSlice, struct datapartition *dP)
  1802. {
  1803. VideoParameters *p_Vid = currSlice->p_Vid;
  1804. StorablePicture *dec_picture = p_Vid->dec_picture;
  1805. Bitstream* currStream = dP->bitstream;
  1806. DecodingEnvironmentPtr dep = &(dP->de_cabac);
  1807. byte *buf = currStream->streamBuffer;
  1808. int BitstreamLengthInBits = (dP->bitstream->bitstream_length << 3) + 7;
  1809. int val = 0;
  1810. int bits_read = 0;
  1811. int bitoffset, bitdepth;
  1812. int uv, i, j;
  1813. while (dep->DbitsLeft >= 8)
  1814. {
  1815. dep->Dvalue >>= 8;
  1816. dep->DbitsLeft -= 8;
  1817. (*dep->Dcodestrm_len)--;
  1818. }
  1819. bitoffset = (*dep->Dcodestrm_len) << 3;
  1820. // read luma values
  1821. bitdepth = p_Vid->bitdepth_luma;
  1822. for(i=0;i<MB_BLOCK_SIZE;++i)
  1823. {
  1824. for(j=0;j<MB_BLOCK_SIZE;++j)
  1825. {
  1826. bits_read += GetBits(buf, bitoffset, &val, BitstreamLengthInBits, bitdepth);
  1827. currSlice->ipcm[0][i][j] = val;
  1828. bitoffset += bitdepth;
  1829. }
  1830. }
  1831. // read chroma values
  1832. bitdepth = p_Vid->bitdepth_chroma;
  1833. if ((dec_picture->chroma_format_idc != YUV400) && !IS_INDEPENDENT(p_Vid))
  1834. {
  1835. for (uv=1; uv<3; ++uv)
  1836. {
  1837. for(i=0;i<p_Vid->mb_cr_size_y;++i)
  1838. {
  1839. for(j=0;j<p_Vid->mb_cr_size_x;++j)
  1840. {
  1841. bits_read += GetBits(buf, bitoffset, &val, BitstreamLengthInBits, bitdepth);
  1842. currSlice->ipcm[uv][i][j] = val;
  1843. bitoffset += bitdepth;
  1844. }
  1845. }
  1846. }
  1847. }
  1848. (*dep->Dcodestrm_len) += ( bits_read >> 3);
  1849. if (bits_read & 7)
  1850. {
  1851. ++(*dep->Dcodestrm_len);
  1852. }
  1853. }