vlc.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769
  1. /*!
  2. ************************************************************************
  3. * \file vlc.c
  4. *
  5. * \brief
  6. * VLC support functions
  7. *
  8. * \author
  9. * Main contributors (see contributors.h for copyright, address and affiliation details)
  10. * - Inge Lille-Langøy <[email protected]>
  11. * - Detlev Marpe <[email protected]>
  12. * - Gabi Blaettermann
  13. ************************************************************************
  14. */
  15. #include "contributors.h"
  16. #include "global.h"
  17. #include "vlc.h"
  18. #include "elements.h"
  19. #include "optim.h"
  20. #include <emmintrin.h>
  21. // A little trick to avoid those horrible #if TRACE all over the source code
  22. #if TRACE
  23. #define SYMTRACESTRING(s) strncpy(symbol.tracestring,s,TRACESTRING_SIZE)
  24. #else
  25. #define SYMTRACESTRING(s) // do nothing
  26. #endif
  27. static int ShowBits (const uint8_t buffer[],int totbitoffset,int bitcount, int numbits);
  28. // Note that all NA values are filled with 0
  29. /*!
  30. *************************************************************************************
  31. * \brief
  32. * ue_v, reads an ue(v) syntax element, the length in bits is stored in
  33. * the global p_Dec->UsedBits variable
  34. *
  35. * \param tracestring
  36. * the string for the trace file
  37. *
  38. * \param bitstream
  39. * the stream to be read from
  40. *
  41. * \return
  42. * the value of the coded syntax element
  43. *
  44. *************************************************************************************
  45. */
  46. int ue_v (const char *tracestring, Bitstream *bitstream)
  47. {
  48. SyntaxElement symbol;
  49. //assert (bitstream->streamBuffer != NULL);
  50. symbol.mapping = linfo_ue; // Mapping rule
  51. SYMTRACESTRING(tracestring);
  52. readSyntaxElement_VLC (&symbol, bitstream);
  53. return symbol.value1;
  54. }
  55. /*!
  56. *************************************************************************************
  57. * \brief
  58. * ue_v, reads an se(v) syntax element, the length in bits is stored in
  59. * the global p_Dec->UsedBits variable
  60. *
  61. * \param tracestring
  62. * the string for the trace file
  63. *
  64. * \param bitstream
  65. * the stream to be read from
  66. *
  67. * \return
  68. * the value of the coded syntax element
  69. *
  70. *************************************************************************************
  71. */
  72. int se_v (const char *tracestring, Bitstream *bitstream)
  73. {
  74. SyntaxElement symbol;
  75. //assert (bitstream->streamBuffer != NULL);
  76. symbol.mapping = linfo_se; // Mapping rule: signed integer
  77. SYMTRACESTRING(tracestring);
  78. readSyntaxElement_VLC (&symbol, bitstream);
  79. return symbol.value1;
  80. }
  81. /*!
  82. *************************************************************************************
  83. * \brief
  84. * ue_v, reads an u(v) syntax element, the length in bits is stored in
  85. * the global p_Dec->UsedBits variable
  86. *
  87. * \param LenInBits
  88. * length of the syntax element
  89. *
  90. * \param tracestring
  91. * the string for the trace file
  92. *
  93. * \param bitstream
  94. * the stream to be read from
  95. *
  96. * \return
  97. * the value of the coded syntax element
  98. *
  99. *************************************************************************************
  100. */
  101. int u_v (int LenInBits, const char*tracestring, Bitstream *bitstream)
  102. {
  103. return readSyntaxElement_FLC(bitstream, LenInBits);
  104. }
  105. /*!
  106. *************************************************************************************
  107. * \brief
  108. * i_v, reads an i(v) syntax element, the length in bits is stored in
  109. * the global p_Dec->UsedBits variable
  110. *
  111. * \param LenInBits
  112. * length of the syntax element
  113. *
  114. * \param tracestring
  115. * the string for the trace file
  116. *
  117. * \param bitstream
  118. * the stream to be read from
  119. *
  120. * \return
  121. * the value of the coded syntax element
  122. *
  123. *************************************************************************************
  124. */
  125. int i_v (int LenInBits, const char*tracestring, Bitstream *bitstream)
  126. {
  127. int val;
  128. val = readSyntaxElement_FLC (bitstream, LenInBits);
  129. // can be negative
  130. val = -( val & (1 << (LenInBits - 1)) ) | val;
  131. return val;
  132. }
  133. /*!
  134. *************************************************************************************
  135. * \brief
  136. * ue_v, reads an u(1) syntax element, the length in bits is stored in
  137. * the global p_Dec->UsedBits variable
  138. *
  139. * \param tracestring
  140. * the string for the trace file
  141. *
  142. * \param bitstream
  143. * the stream to be read from
  144. *
  145. * \return
  146. * the value of the coded syntax element
  147. *
  148. *************************************************************************************
  149. */
  150. Boolean u_1 (const char *tracestring, Bitstream *bitstream)
  151. {
  152. return (Boolean) u_v (1, tracestring, bitstream);
  153. }
  154. /*!
  155. ************************************************************************
  156. * \brief
  157. * mapping rule for ue(v) syntax elements
  158. * \par Input:
  159. * lenght and info
  160. * \par Output:
  161. * number in the code table
  162. ************************************************************************
  163. */
  164. void linfo_ue(int len, int info, int *value1, int *dummy)
  165. {
  166. //assert ((len >> 1) < 32);
  167. *value1 = (int) (((unsigned int) 1 << (len >> 1)) + (unsigned int) (info) - 1);
  168. }
  169. /*!
  170. ************************************************************************
  171. * \brief
  172. * mapping rule for se(v) syntax elements
  173. * \par Input:
  174. * lenght and info
  175. * \par Output:
  176. * signed mvd
  177. ************************************************************************
  178. */
  179. void linfo_se(int len, int info, int *value1, int *dummy)
  180. {
  181. //assert ((len >> 1) < 32);
  182. unsigned int n = ((unsigned int) 1 << (len >> 1)) + (unsigned int) info - 1;
  183. *value1 = (n + 1) >> 1;
  184. if((n & 0x01) == 0) // lsb is signed bit
  185. *value1 = -*value1;
  186. }
  187. /*!
  188. ************************************************************************
  189. * \par Input:
  190. * length and info
  191. * \par Output:
  192. * cbp (intra)
  193. ************************************************************************
  194. */
  195. void linfo_cbp_intra_normal(int len,int info,int *cbp, int *dummy)
  196. {
  197. int cbp_idx;
  198. linfo_ue(len, info, &cbp_idx, dummy);
  199. *cbp=NCBP[1][cbp_idx][0];
  200. }
  201. /*!
  202. ************************************************************************
  203. * \par Input:
  204. * length and info
  205. * \par Output:
  206. * cbp (intra)
  207. ************************************************************************
  208. */
  209. void linfo_cbp_intra_other(int len,int info,int *cbp, int *dummy)
  210. {
  211. int cbp_idx;
  212. linfo_ue(len, info, &cbp_idx, dummy);
  213. *cbp=NCBP[0][cbp_idx][0];
  214. }
  215. /*!
  216. ************************************************************************
  217. * \par Input:
  218. * length and info
  219. * \par Output:
  220. * cbp (inter)
  221. ************************************************************************
  222. */
  223. void linfo_cbp_inter_normal(int len,int info,int *cbp, int *dummy)
  224. {
  225. int cbp_idx;
  226. linfo_ue(len, info, &cbp_idx, dummy);
  227. *cbp=NCBP[1][cbp_idx][1];
  228. }
  229. /*!
  230. ************************************************************************
  231. * \par Input:
  232. * length and info
  233. * \par Output:
  234. * cbp (inter)
  235. ************************************************************************
  236. */
  237. void linfo_cbp_inter_other(int len,int info,int *cbp, int *dummy)
  238. {
  239. int cbp_idx;
  240. linfo_ue(len, info, &cbp_idx, dummy);
  241. *cbp=NCBP[0][cbp_idx][1];
  242. }
  243. /*!
  244. ************************************************************************
  245. * \par Input:
  246. * length and info
  247. * \par Output:
  248. * level, run
  249. ************************************************************************
  250. */
  251. void linfo_levrun_inter(int len, int info, int *level, int *irun)
  252. {
  253. //assert (((len >> 1) - 5) < 32);
  254. if (len <= 9)
  255. {
  256. int l2 = imax(0,(len >> 1)-1);
  257. int inf = info >> 1;
  258. *level = NTAB1[l2][inf][0];
  259. *irun = NTAB1[l2][inf][1];
  260. if ((info & 0x01) == 1)
  261. *level = -*level; // make sign
  262. }
  263. else // if len > 9, skip using the array
  264. {
  265. *irun = (info & 0x1e) >> 1;
  266. *level = LEVRUN1[*irun] + (info >> 5) + ( 1 << ((len >> 1) - 5));
  267. if ((info & 0x01) == 1)
  268. *level = -*level;
  269. }
  270. if (len == 1) // EOB
  271. *level = 0;
  272. }
  273. /*!
  274. ************************************************************************
  275. * \par Input:
  276. * length and info
  277. * \par Output:
  278. * level, run
  279. ************************************************************************
  280. */
  281. void linfo_levrun_c2x2(int len, int info, int *level, int *irun)
  282. {
  283. if (len<=5)
  284. {
  285. int l2 = imax(0, (len >> 1) - 1);
  286. int inf = info >> 1;
  287. *level = NTAB3[l2][inf][0];
  288. *irun = NTAB3[l2][inf][1];
  289. if ((info & 0x01) == 1)
  290. *level = -*level; // make sign
  291. }
  292. else // if len > 5, skip using the array
  293. {
  294. *irun = (info & 0x06) >> 1;
  295. *level = LEVRUN3[*irun] + (info >> 3) + (1 << ((len >> 1) - 3));
  296. if ((info & 0x01) == 1)
  297. *level = -*level;
  298. }
  299. if (len == 1) // EOB
  300. *level = 0;
  301. }
  302. /*!
  303. ************************************************************************
  304. * \brief
  305. * read next UVLC codeword from UVLC-partition and
  306. * map it to the corresponding syntax element
  307. ************************************************************************
  308. */
  309. int readSyntaxElement_VLC(SyntaxElement *sym, Bitstream *currStream)
  310. {
  311. int info;
  312. sym->len = GetVLCSymbol (currStream->streamBuffer, currStream->frame_bitoffset, &info, currStream->bitstream_length);
  313. if (sym->len == -1)
  314. return -1;
  315. currStream->frame_bitoffset += sym->len;
  316. sym->mapping(sym->len, info, &(sym->value1), &(sym->value2));
  317. return 1;
  318. }
  319. /*!
  320. ************************************************************************
  321. * \brief
  322. * read next UVLC codeword from UVLC-partition and
  323. * map it to the corresponding syntax element
  324. ************************************************************************
  325. */
  326. int readSyntaxElement_UVLC(SyntaxElement *sym, struct datapartition *dp)
  327. {
  328. return (readSyntaxElement_VLC(sym, dp->bitstream));
  329. }
  330. /*!
  331. ************************************************************************
  332. * \brief
  333. * read next VLC codeword for 4x4 Intra Prediction Mode and
  334. * map it to the corresponding Intra Prediction Direction
  335. ************************************************************************
  336. */
  337. int readSyntaxElement_Intra4x4PredictionMode(SyntaxElement *sym, Bitstream *currStream)
  338. {
  339. int info;
  340. sym->len = GetVLCSymbol_IntraMode (currStream->streamBuffer, currStream->frame_bitoffset, &info, currStream->bitstream_length);
  341. if (sym->len == -1)
  342. return -1;
  343. currStream->frame_bitoffset += sym->len;
  344. sym->value1 = (sym->len == 1) ? -1 : info;
  345. #if TRACE
  346. tracebits2(sym->tracestring, sym->len, sym->value1);
  347. #endif
  348. return 1;
  349. }
  350. int GetVLCSymbol_IntraMode (const uint8_t buffer[],int totbitoffset,int *info, int bytecount)
  351. {
  352. int byteoffset = (totbitoffset >> 3); // byte from start of buffer
  353. int bitoffset = (7 - (totbitoffset & 0x07)); // bit from start of byte
  354. const uint8_t *cur_byte = &(buffer[byteoffset]);
  355. int ctr_bit = (*cur_byte & (0x01 << bitoffset)); // control bit for current bit posision
  356. //First bit
  357. if (ctr_bit)
  358. {
  359. *info = 0;
  360. return 1;
  361. }
  362. if (byteoffset >= bytecount)
  363. {
  364. return -1;
  365. }
  366. else
  367. {
  368. int inf = (*(cur_byte) << 8) + *(cur_byte + 1);
  369. inf <<= (sizeof(uint8_t) * 8) - bitoffset;
  370. inf = inf & 0xFFFF;
  371. inf >>= (sizeof(uint8_t) * 8) * 2 - 3;
  372. *info = inf;
  373. return 4; // return absolute offset in bit from start of frame
  374. }
  375. }
  376. /*!
  377. ************************************************************************
  378. * \brief
  379. * test if bit buffer contains only stop bit
  380. *
  381. * \param buffer
  382. * buffer containing VLC-coded data bits
  383. * \param totbitoffset
  384. * bit offset from start of partition
  385. * \param bytecount
  386. * buffer length
  387. * \return
  388. * true if more bits available
  389. ************************************************************************
  390. */
  391. int more_rbsp_data (const uint8_t buffer[],int totbitoffset,int bytecount)
  392. {
  393. long byteoffset = (totbitoffset >> 3); // byte from start of buffer
  394. // there is more until we're in the last byte
  395. if (byteoffset < (bytecount - 1))
  396. return TRUE;
  397. else
  398. {
  399. int bitoffset = (7 - (totbitoffset & 0x07)); // bit from start of byte
  400. const uint8_t *cur_byte = &(buffer[byteoffset]);
  401. // read one bit
  402. int ctr_bit = ctr_bit = ((*cur_byte)>> (bitoffset--)) & 0x01; // control bit for current bit posision
  403. //assert (byteoffset<bytecount);
  404. // a stop bit has to be one
  405. if (ctr_bit==0)
  406. return TRUE;
  407. else
  408. {
  409. int cnt = 0;
  410. while (bitoffset>=0 && !cnt)
  411. {
  412. cnt |= ((*cur_byte)>> (bitoffset--)) & 0x01; // set up control bit
  413. }
  414. return (cnt);
  415. }
  416. }
  417. }
  418. /*!
  419. ************************************************************************
  420. * \brief
  421. * Check if there are symbols for the next MB
  422. ************************************************************************
  423. */
  424. int uvlc_startcode_follows(Slice *currSlice, int dummy)
  425. {
  426. byte dp_Nr = assignSE2partition[currSlice->dp_mode][SE_MBTYPE];
  427. DataPartition *dP = &(currSlice->partArr[dp_Nr]);
  428. Bitstream *currStream = dP->bitstream;
  429. const uint8_t *buf = currStream->streamBuffer;
  430. return (!(more_rbsp_data(buf, currStream->frame_bitoffset,currStream->bitstream_length)));
  431. }
  432. /*!
  433. ************************************************************************
  434. * \brief
  435. * read one exp-golomb VLC symbol
  436. *
  437. * \param buffer
  438. * containing VLC-coded data bits
  439. * \param totbitoffset
  440. * bit offset from start of partition
  441. * \param info
  442. * returns the value of the symbol
  443. * \param bytecount
  444. * buffer length
  445. * \return
  446. * bits read
  447. ************************************************************************
  448. */
  449. int GetVLCSymbol (const uint8_t buffer[],int totbitoffset,int *info, int bytecount)
  450. {
  451. long byteoffset = (totbitoffset >> 3); // byte from start of buffer
  452. int bitoffset = (7 - (totbitoffset & 0x07)); // bit from start of byte
  453. int bitcounter = 1;
  454. int len = 0;
  455. const uint8_t *cur_byte = &(buffer[byteoffset]);
  456. int ctr_bit = ((*cur_byte) >> (bitoffset)) & 0x01; // control bit for current bit posision
  457. while (ctr_bit == 0)
  458. { // find leading 1 bit
  459. len++;
  460. bitcounter++;
  461. bitoffset--;
  462. bitoffset &= 0x07;
  463. cur_byte += (bitoffset == 7);
  464. byteoffset+= (bitoffset == 7);
  465. ctr_bit = ((*cur_byte) >> (bitoffset)) & 0x01;
  466. }
  467. if (byteoffset + ((len + 7) >> 3) > bytecount)
  468. return -1;
  469. else
  470. {
  471. // make infoword
  472. int inf = 0; // shortest possible code is 1, then info is always 0
  473. while (len--)
  474. {
  475. bitoffset --;
  476. bitoffset &= 0x07;
  477. cur_byte += (bitoffset == 7);
  478. bitcounter++;
  479. inf <<= 1;
  480. inf |= ((*cur_byte) >> (bitoffset)) & 0x01;
  481. }
  482. *info = inf;
  483. return bitcounter; // return absolute offset in bit from start of frame
  484. }
  485. }
  486. /*!
  487. ************************************************************************
  488. * \brief
  489. * Reads bits from the bitstream buffer (Threshold based)
  490. *
  491. * \param inf
  492. * bytes to extract numbits from with bitoffset already applied
  493. * \param numbits
  494. * number of bits to read
  495. *
  496. ************************************************************************
  497. */
  498. static inline int ShowBitsThres16(int inf, int numbits)
  499. {
  500. return ((inf) >> ((sizeof(uint8_t) * 16) - (numbits)));
  501. }
  502. //static inline int ShowBitsThres (int inf, int bitcount, int numbits)
  503. static inline int ShowBitsThres(int inf, int numbits)
  504. {
  505. return ((inf) >> ((sizeof(uint8_t) * 24) - (numbits)));
  506. /*
  507. if ((numbits + 7) > bitcount)
  508. {
  509. return -1;
  510. }
  511. else
  512. {
  513. //Worst case scenario is that we will need to traverse 3 bytes
  514. inf >>= (sizeof(byte)*8)*3 - numbits;
  515. }
  516. return inf; //Will be a small unsigned integer so will not need any conversion when returning as int
  517. */
  518. }
  519. /*!
  520. ************************************************************************
  521. * \brief
  522. * code from bitstream (2d tables)
  523. ************************************************************************
  524. */
  525. static int code_from_bitstream_2d(SyntaxElement *sym,
  526. Bitstream *currStream,
  527. const uint8_t *lentab,
  528. const uint8_t *codtab,
  529. int tabwidth,
  530. int tabheight,
  531. int *code)
  532. {
  533. int i, j;
  534. const uint8_t *len = &lentab[0], *cod = &codtab[0];
  535. int *frame_bitoffset = &currStream->frame_bitoffset;
  536. const uint8_t *buf = &currStream->streamBuffer[*frame_bitoffset >> 3];
  537. //Apply bitoffset to three bytes (maximum that may be traversed by ShowBitsThres)
  538. unsigned int inf = ((*buf) << 16) + (*(buf + 1) << 8) + *(buf + 2); //Even at the end of a stream we will still be pulling out of allocated memory as alloc is done by MAX_CODED_FRAME_SIZE
  539. inf <<= (*frame_bitoffset & 0x07); //Offset is constant so apply before extracting different numbers of bits
  540. inf &= 0xFFFFFF; //Arithmetic shift so wipe any sign which may be extended inside ShowBitsThres
  541. // this VLC decoding method is not optimized for speed
  542. for (j = 0; j < tabheight; j++)
  543. {
  544. for (i = 0; i < tabwidth; i++)
  545. {
  546. if ((*len == 0) || (ShowBitsThres(inf, *len) != *cod))
  547. {
  548. len++;
  549. cod++;
  550. }
  551. else
  552. {
  553. sym->len = *len;
  554. *frame_bitoffset += *len; // move bitstream pointer
  555. *code = *cod;
  556. sym->value1 = i;
  557. sym->value2 = j;
  558. return 0; // found code and return
  559. }
  560. }
  561. }
  562. return -1; // failed to find code
  563. }
  564. static int code_from_bitstream_2d_16_1(Bitstream *currStream,
  565. const uint8_t *lentab,
  566. const uint8_t *codtab)
  567. {
  568. int i;
  569. const uint8_t *len = &lentab[0], *cod = &codtab[0];
  570. int *frame_bitoffset = &currStream->frame_bitoffset;
  571. const uint8_t *buf = &currStream->streamBuffer[*frame_bitoffset >> 3];
  572. //Apply bitoffset to three bytes (maximum that may be traversed by ShowBitsThres)
  573. unsigned int inf = ((*buf) << 16) + (*(buf + 1) << 8) + *(buf + 2); //Even at the end of a stream we will still be pulling out of allocated memory as alloc is done by MAX_CODED_FRAME_SIZE
  574. inf <<= (*frame_bitoffset & 0x07); //Offset is constant so apply before extracting different numbers of bits
  575. inf &= 0xFFFFFF; //Arithmetic shift so wipe any sign which may be extended inside ShowBitsThres
  576. // this VLC decoding method is not optimized for speed
  577. for (i = 0; i < 16 && len[i]; i++)
  578. {
  579. if (ShowBitsThres(inf, len[i]) == cod[i])
  580. {
  581. *frame_bitoffset += len[i]; // move bitstream pointer
  582. return i; // found code and return
  583. }
  584. }
  585. return -1; // failed to find code
  586. }
  587. int code_from_bitstream_2d_16_1_sse2(Bitstream *currStream, const uint16_t *lentab, const uint16_t *codtab, const uint16_t *masktab)
  588. {
  589. unsigned long result;
  590. int frame_bitoffset = currStream->frame_bitoffset;
  591. const uint8_t *buf = &currStream->streamBuffer[frame_bitoffset >> 3];
  592. uint16_t inf;
  593. __m128i xmm_inf, xmm_mask, xmm_cod;
  594. int match;
  595. unsigned int _inf = _byteswap_ulong(*(unsigned long *)buf);
  596. _inf >>= 16-(frame_bitoffset & 0x07);
  597. _inf &= 0xFFFF;
  598. inf = (uint16_t)_inf;
  599. xmm_inf = _mm_set1_epi16(inf);
  600. xmm_cod = _mm_load_si128((__m128i *)codtab);
  601. xmm_mask = _mm_load_si128((__m128i *)masktab);
  602. xmm_mask = _mm_and_si128(xmm_mask, xmm_inf); // mask = mask & inf
  603. xmm_mask = _mm_cmpeq_epi16(xmm_mask, xmm_cod); // mask == cod
  604. match = _mm_movemask_epi8(xmm_mask);
  605. if (match)
  606. {
  607. _BitScanForward(&result, match);
  608. result >>= 1;
  609. currStream->frame_bitoffset += lentab[result]; // move bitstream pointer
  610. return result; // found code and return
  611. }
  612. xmm_cod = _mm_load_si128((__m128i *)(codtab+8));
  613. xmm_mask = _mm_load_si128((__m128i *)(masktab+8));
  614. xmm_mask = _mm_and_si128(xmm_mask, xmm_inf); // mask = mask & inf
  615. xmm_mask = _mm_cmpeq_epi16(xmm_mask, xmm_cod); // mask == cod
  616. match = _mm_movemask_epi8(xmm_mask);
  617. if (match)
  618. {
  619. _BitScanForward(&result, match);
  620. result >>= 1;
  621. currStream->frame_bitoffset += lentab[result+8]; // move bitstream pointer
  622. return result+8;
  623. }
  624. return -1;
  625. }
  626. int code_from_bitstream_2d_16_1_c(Bitstream *currStream, const uint16_t *lentab, const uint16_t *codtab, const uint16_t *masktab)
  627. {
  628. int i;
  629. int frame_bitoffset = currStream->frame_bitoffset;
  630. const uint8_t *buf = &currStream->streamBuffer[frame_bitoffset >> 3];
  631. uint16_t inf;
  632. unsigned int _inf = _byteswap_ulong(*(unsigned long *)buf);
  633. _inf >>= 16-(frame_bitoffset & 0x07);
  634. _inf &= 0xFFFF;
  635. inf = (uint16_t)_inf;
  636. // this VLC decoding method is not optimized for speed
  637. for (i=0; i < 16; i++)
  638. {
  639. if ((inf & masktab[i]) == codtab[i])//ShowBitsThres(inf, len[i]) == cod[i])
  640. {
  641. currStream->frame_bitoffset += lentab[i]; // move bitstream pointer
  642. return i; // found code and return
  643. }
  644. }
  645. return -1; // failed to find code
  646. }
  647. int code_from_bitstream_2d_17_4_sse2(SyntaxElement *sym, Bitstream *currStream, const uint16_t *lentab, const uint16_t *codtab, const uint16_t *masktab)
  648. {
  649. unsigned long result;
  650. const uint16_t *len = lentab, *cod = codtab, *mask = masktab;
  651. int frame_bitoffset = currStream->frame_bitoffset;
  652. const uint8_t *buf = &currStream->streamBuffer[frame_bitoffset >> 3];
  653. uint16_t inf;
  654. __m128i xmm_inf, xmm_mask, xmm_cod;
  655. int match;
  656. unsigned int _inf = _byteswap_ulong(*(unsigned long *)buf);
  657. _inf >>= 16-(frame_bitoffset & 0x07);
  658. _inf &= 0xFFFF;
  659. inf = (uint16_t)_inf;
  660. xmm_inf = _mm_set1_epi16(inf);
  661. xmm_cod = _mm_loadu_si128((__m128i *)cod);
  662. xmm_mask = _mm_loadu_si128((__m128i *)mask);
  663. xmm_mask = _mm_and_si128(xmm_mask, xmm_inf); // mask = mask & inf
  664. xmm_mask = _mm_cmpeq_epi16(xmm_mask, xmm_cod); // mask == cod
  665. match = _mm_movemask_epi8(xmm_mask);
  666. if (match)
  667. {
  668. _BitScanForward(&result, match);
  669. result >>= 1;
  670. currStream->frame_bitoffset += len[result]; // move bitstream pointer
  671. sym->value1 = result;
  672. sym->value2 = 0;
  673. return 0; // found code and return
  674. }
  675. /* second table - rows 1-8 */
  676. xmm_cod = _mm_loadu_si128((__m128i *)(cod+17));
  677. xmm_mask = _mm_loadu_si128((__m128i *)(mask+17));
  678. xmm_mask = _mm_and_si128(xmm_mask, xmm_inf); // mask = mask & inf
  679. xmm_mask = _mm_cmpeq_epi16(xmm_mask, xmm_cod); // mask == cod
  680. match = _mm_movemask_epi8(xmm_mask);
  681. if (match)
  682. {
  683. _BitScanForward(&result, match);
  684. result >>= 1;
  685. currStream->frame_bitoffset += len[result+17]; // move bitstream pointer
  686. sym->value1 = 1+result;
  687. sym->value2 = 1;
  688. return 0; // found code and return
  689. }
  690. /* first table, rows 9-16 */
  691. xmm_cod = _mm_loadu_si128((__m128i *)(cod+8));
  692. xmm_mask = _mm_loadu_si128((__m128i *)(mask+8));
  693. xmm_mask = _mm_and_si128(xmm_mask, xmm_inf); // mask = mask & inf
  694. xmm_mask = _mm_cmpeq_epi16(xmm_mask, xmm_cod); // mask == cod
  695. match = _mm_movemask_epi8(xmm_mask);
  696. if (match)
  697. {
  698. _BitScanForward(&result, match);
  699. result >>= 1;
  700. currStream->frame_bitoffset += len[result+8]; // move bitstream pointer
  701. sym->value1 = 8+result;
  702. sym->value2 = 0;
  703. return 0; // found code and return
  704. }
  705. /* extra one just for first table */
  706. if ((inf & mask[16]) == cod[16])//ShowBitsThres(inf, len[i]) == cod[i])
  707. {
  708. currStream->frame_bitoffset += len[16]; // move bitstream pointer
  709. sym->value1 = 16;
  710. sym->value2 = 0;
  711. return 0; // found code and return
  712. }
  713. /* second table - rows 9-16 */
  714. xmm_cod = _mm_loadu_si128((__m128i *)(cod+25));
  715. xmm_mask = _mm_loadu_si128((__m128i *)(mask+25));
  716. xmm_mask = _mm_and_si128(xmm_mask, xmm_inf); // mask = mask & inf
  717. xmm_mask = _mm_cmpeq_epi16(xmm_mask, xmm_cod); // mask == cod
  718. match = _mm_movemask_epi8(xmm_mask);
  719. if (match)
  720. {
  721. _BitScanForward(&result, match);
  722. result >>= 1;
  723. currStream->frame_bitoffset += len[result+25]; // move bitstream pointer
  724. sym->value1 = 9+result;
  725. sym->value2 = 1;
  726. return 0; // found code and return
  727. }
  728. /* third table - rows 1-8 */
  729. xmm_cod = _mm_loadu_si128((__m128i *)(cod+34));
  730. xmm_mask = _mm_loadu_si128((__m128i *)(mask+34));
  731. xmm_mask = _mm_and_si128(xmm_mask, xmm_inf); // mask = mask & inf
  732. xmm_mask = _mm_cmpeq_epi16(xmm_mask, xmm_cod); // mask == cod
  733. match = _mm_movemask_epi8(xmm_mask);
  734. if (match)
  735. {
  736. _BitScanForward(&result, match);
  737. result >>= 1;
  738. currStream->frame_bitoffset += len[result+34]; // move bitstream pointer
  739. sym->value1 = 2+result;
  740. sym->value2 = 2;
  741. return 0; // found code and return
  742. }
  743. /* third table - rows 9-16 */
  744. xmm_cod = _mm_loadu_si128((__m128i *)(cod+42));
  745. xmm_mask = _mm_loadu_si128((__m128i *)(mask+42));
  746. xmm_mask = _mm_and_si128(xmm_mask, xmm_inf); // mask = mask & inf
  747. xmm_mask = _mm_cmpeq_epi16(xmm_mask, xmm_cod); // mask == cod
  748. match = _mm_movemask_epi8(xmm_mask);
  749. if (match)
  750. {
  751. _BitScanForward(&result, match);
  752. result >>= 1;
  753. currStream->frame_bitoffset += len[result+42]; // move bitstream pointer
  754. sym->value1 = 10+result;
  755. sym->value2 = 2;
  756. return 0; // found code and return
  757. }
  758. /* fourth table - rows 1-8 */
  759. xmm_cod = _mm_loadu_si128((__m128i *)(cod+51));
  760. xmm_mask = _mm_loadu_si128((__m128i *)(mask+51));
  761. xmm_mask = _mm_and_si128(xmm_mask, xmm_inf); // mask = mask & inf
  762. xmm_mask = _mm_cmpeq_epi16(xmm_mask, xmm_cod); // mask == cod
  763. match = _mm_movemask_epi8(xmm_mask);
  764. if (match)
  765. {
  766. _BitScanForward(&result, match);
  767. result >>= 1;
  768. currStream->frame_bitoffset += len[result+51]; // move bitstream pointer
  769. sym->value1 = 3+result;
  770. sym->value2 = 3;
  771. return 0; // found code and return
  772. }
  773. /* fourth table - rows 9-16 */
  774. xmm_cod = _mm_loadu_si128((__m128i *)(cod+59));
  775. xmm_mask = _mm_loadu_si128((__m128i *)(mask+59));
  776. xmm_mask = _mm_and_si128(xmm_mask, xmm_inf); // mask = mask & inf
  777. xmm_mask = _mm_cmpeq_epi16(xmm_mask, xmm_cod); // mask == cod
  778. match = _mm_movemask_epi8(xmm_mask);
  779. if (match)
  780. {
  781. _BitScanForward(&result, match);
  782. result >>= 1;
  783. currStream->frame_bitoffset += len[result+59]; // move bitstream pointer
  784. sym->value1 = 11+result;
  785. sym->value2 = 3;
  786. return 0; // found code and return
  787. }
  788. return -1; // failed to find code
  789. }
  790. int code_from_bitstream_2d_17_4_c(SyntaxElement *sym, Bitstream *currStream, const uint16_t *lentab, const uint16_t *codtab, const uint16_t *masktab)
  791. {
  792. int i, j;
  793. const uint16_t *len, *cod, *mask;
  794. int frame_bitoffset = currStream->frame_bitoffset;
  795. const uint8_t *buf = &currStream->streamBuffer[frame_bitoffset >> 3];
  796. uint16_t inf;
  797. unsigned int _inf = _byteswap_ulong(*(unsigned long *)buf);
  798. _inf >>= 16-(frame_bitoffset & 0x07);
  799. _inf &= 0xFFFF;
  800. inf = (uint16_t)_inf;
  801. for (j=0;j<4;j++)
  802. {
  803. len = &lentab[j*17];
  804. cod = &codtab[j*17];
  805. mask = &masktab[j*17];
  806. // this VLC decoding method is not optimized for speed
  807. for (i=0; i < 17; i++)
  808. {
  809. if ((inf & mask[i]) == cod[i])//ShowBitsThres(inf, len[i]) == cod[i])
  810. {
  811. currStream->frame_bitoffset += len[i]; // move bitstream pointer
  812. sym->value1 = j+i;
  813. sym->value2 = j;
  814. return 0; // found code and return
  815. }
  816. }
  817. }
  818. return -1; // failed to find code
  819. }
  820. static int code_from_bitstream_2d_9_4(SyntaxElement *sym,
  821. Bitstream *currStream,
  822. const uint16_t *lentab,
  823. const uint16_t *codtab,
  824. const uint16_t *masktab)
  825. {
  826. int i, j;
  827. const uint16_t *len, *cod, *mask;
  828. int frame_bitoffset = currStream->frame_bitoffset;
  829. const uint8_t *buf = &currStream->streamBuffer[frame_bitoffset >> 3];
  830. uint16_t inf;
  831. unsigned int _inf = _byteswap_ulong(*(unsigned long *)buf);
  832. _inf >>= 16-(frame_bitoffset & 0x07);
  833. _inf &= 0xFFFF;
  834. inf = (uint16_t)_inf;
  835. // this VLC decoding method is not optimized for speed
  836. for (j = 0; j < 4; j++)
  837. {
  838. len = &lentab[j*9];
  839. cod = &codtab[j*9];
  840. mask = &masktab[j*9];
  841. for (i=0; i < 9; i++)
  842. {
  843. if ((inf & mask[i]) == cod[i])
  844. {
  845. sym->len = len[i];
  846. currStream->frame_bitoffset += len[i]; // move bitstream pointer
  847. sym->value1 = j+i;
  848. sym->value2 = j;
  849. return 0; // found code and return
  850. }
  851. }
  852. }
  853. return -1; // failed to find code
  854. }
  855. int code_from_bitstream_2d_5_4_c(SyntaxElement *sym, Bitstream *currStream, const uint8_t *lentab, const uint8_t *codtab, const uint8_t *masktab)
  856. {
  857. int i;
  858. int frame_bitoffset = currStream->frame_bitoffset;
  859. const uint8_t *buf = &currStream->streamBuffer[frame_bitoffset >> 3];
  860. uint8_t inf;
  861. unsigned int _inf = _byteswap_ushort(*(unsigned short *)buf);
  862. _inf >>= 8-(frame_bitoffset & 0x07);
  863. _inf &= 0xFF;
  864. inf = (uint8_t)_inf;
  865. for (i = 0; i<16;i++)
  866. {
  867. if ((inf & masktab[i]) == codtab[i])
  868. {
  869. currStream->frame_bitoffset += lentab[i]; // move bitstream pointer
  870. sym->value2 = (i<<1)/9;
  871. sym->value1 = sym->value2 + (((i<<1)%9)>>1);
  872. return 0; // found code and return
  873. }
  874. }
  875. return -1; // failed to find code
  876. }
  877. int code_from_bitstream_2d_5_4_sse2(SyntaxElement *sym, Bitstream *currStream, const uint8_t *lentab, const uint8_t *codtab, const uint8_t *masktab)
  878. {
  879. int frame_bitoffset = currStream->frame_bitoffset;
  880. const uint8_t *buf = &currStream->streamBuffer[frame_bitoffset >> 3];
  881. uint8_t inf;
  882. __m128i xmm_inf, xmm_mask, xmm_cod;
  883. int match;
  884. unsigned int _inf = _byteswap_ushort(*(unsigned short *)buf);
  885. _inf >>= 8-(frame_bitoffset & 0x07);
  886. _inf &= 0xFF;
  887. inf = (uint8_t)_inf;
  888. xmm_inf = _mm_set1_epi8(_inf);
  889. xmm_cod = _mm_load_si128((__m128i *)codtab);
  890. xmm_mask = _mm_load_si128((__m128i *)masktab);
  891. xmm_mask = _mm_and_si128(xmm_mask, xmm_inf); // mask = mask & inf
  892. xmm_mask = _mm_cmpeq_epi8(xmm_mask, xmm_cod); // mask == cod
  893. match = _mm_movemask_epi8(xmm_mask);
  894. if (match)
  895. {
  896. unsigned long result;
  897. _BitScanForward(&result, match);
  898. currStream->frame_bitoffset += lentab[result]; // move bitstream pointer
  899. sym->value2 = (result<<1)/9;
  900. sym->value1 = sym->value2 + (((result<<1)%9)>>1);
  901. return 0; // found code and return
  902. }
  903. return -1;
  904. }
  905. /*!
  906. ************************************************************************
  907. * \brief
  908. * read FLC codeword from UVLC-partition
  909. ************************************************************************
  910. */
  911. int readSyntaxElement_FLC(Bitstream *currStream, int numbits)
  912. {
  913. int totbitoffset = currStream->frame_bitoffset;
  914. int bitoffset = /*7 - */(totbitoffset & 0x07); // bit from start of byte
  915. int byteoffset = (totbitoffset >> 3); // byte from start of buffer
  916. const uint8_t *ptr = &(currStream->streamBuffer[byteoffset]);
  917. uint32_t tmp = (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8);// | (ptr[3]);
  918. tmp <<= bitoffset;
  919. tmp >>= 32 - numbits;
  920. currStream->frame_bitoffset += numbits;
  921. return tmp;
  922. }
  923. /*!
  924. ************************************************************************
  925. * \brief
  926. * read NumCoeff/TrailingOnes codeword from UVLC-partition
  927. ************************************************************************
  928. */
  929. int readSyntaxElement_NumCoeffTrailingOnes(SyntaxElement *sym,
  930. Bitstream *currStream,
  931. int vlcnum)
  932. {
  933. int frame_bitoffset = currStream->frame_bitoffset;
  934. int BitstreamLengthInBytes = currStream->bitstream_length;
  935. int BitstreamLengthInBits = (BitstreamLengthInBytes << 3) + 7;
  936. const uint8_t *buf = currStream->streamBuffer;
  937. static const uint16_t lentab[3][4][17] =
  938. {
  939. { // 0702
  940. { 1, 6, 8, 9,10,11,13,13,13,14,14,15,15,16,16,16,16},
  941. { 2, 6, 8, 9,10,11,13,13,14,14,15,15,15,16,16,16, 0},
  942. { 3, 7, 8, 9,10,11,13,13,14,14,15,15,16,16,16, 0, 0},
  943. { 5, 6, 7, 8, 9,10,11,13,14,14,15,15,16,16, 0, 0, 0},
  944. },
  945. {
  946. { 2, 6, 6, 7, 8, 8, 9,11,11,12,12,12,13,13,13,14,14},
  947. { 2, 5, 6, 6, 7, 8, 9,11,11,12,12,13,13,14,14,14, 0},
  948. { 3, 6, 6, 7, 8, 9,11,11,12,12,13,13,13,14,14, 0, 0},
  949. { 4, 4, 5, 6, 6, 7, 9,11,11,12,13,13,13,14, 0, 0, 0},
  950. },
  951. {
  952. { 4, 6, 6, 6, 7, 7, 7, 7, 8, 8, 9, 9, 9,10,10,10,10},
  953. { 4, 5, 5, 5, 5, 6, 6, 7, 8, 8, 9, 9, 9,10,10,10, 0},
  954. { 4, 5, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9,10,10,10, 0, 0},
  955. { 4, 4, 4, 4, 4, 5, 6, 7, 8, 8, 9,10,10,10, 0, 0, 0},
  956. },
  957. };
  958. #if 0 // save for reference
  959. static const uint32_t codtab[3][4][17] =
  960. {
  961. {
  962. { 1, 5, 7, 7, 7, 7,15,11, 8,15,11,15,11,15,11, 7,4},
  963. { 0, 1, 4, 6, 6, 6, 6,14,10,14,10,14,10, 1,14,10,6},
  964. { 0, 0, 1, 5, 5, 5, 5, 5,13, 9,13, 9,13, 9,13, 9,5},
  965. { 0, 0, 0, 3, 3, 4, 4, 4, 4, 4,12,12, 8,12, 8,12,8},
  966. },
  967. {
  968. { 3,11, 7, 7, 7, 4, 7,15,11,15,11, 8,15,11, 7, 9,7},
  969. { 0, 2, 7,10, 6, 6, 6, 6,14,10,14,10,14,10,11, 8,6},
  970. { 0, 0, 3, 9, 5, 5, 5, 5,13, 9,13, 9,13, 9, 6,10,5},
  971. { 0, 0, 0, 5, 4, 6, 8, 4, 4, 4,12, 8,12,12, 8, 1,4},
  972. },
  973. {
  974. {15,15,11, 8,15,11, 9, 8,15,11,15,11, 8,13, 9, 5,1},
  975. { 0,14,15,12,10, 8,14,10,14,14,10,14,10, 7,12, 8,4},
  976. { 0, 0,13,14,11, 9,13, 9,13,10,13, 9,13, 9,11, 7,3},
  977. { 0, 0, 0,12,11,10, 9, 8,13,12,12,12, 8,12,10, 6,2},
  978. },
  979. };
  980. #endif
  981. static const uint16_t codtab[3][4][17] =
  982. {
  983. {
  984. { 0x8000, 0x1400, 0x0700, 0x0380, 0x01C0, 0x00E0, 0x0078, 0x0058, 0x0040, 0x003C, 0x002C, 0x001E, 0x0016, 0x000F, 0x000B, 0x0007, 0x0004 },
  985. { 0x4000, 0x1000, 0x0600, 0x0300, 0x0180, 0x00C0, 0x0070, 0x0050, 0x0038, 0x0028, 0x001C, 0x0014, 0x0002, 0x000E, 0x000A, 0x0006, 0xFFFF },
  986. { 0x2000, 0x0A00, 0x0500, 0x0280, 0x0140, 0x00A0, 0x0068, 0x0048, 0x0034, 0x0024, 0x001A, 0x0012, 0x000D, 0x0009, 0x0005, 0xFFFF, 0xFFFF },
  987. { 0x1800, 0x0C00, 0x0800, 0x0400, 0x0200, 0x0100, 0x0080, 0x0060, 0x0030, 0x0020, 0x0018, 0x0010, 0x000C, 0x0008, 0xFFFF, 0xFFFF, 0xFFFF }
  988. },
  989. {
  990. { 0xC000, 0x2C00, 0x1C00, 0x0E00, 0x0700, 0x0400, 0x0380, 0x01E0, 0x0160, 0x00F0, 0x00B0, 0x0080, 0x0078, 0x0058, 0x0038, 0x0024, 0x001C },
  991. { 0x8000, 0x3800, 0x2800, 0x1800, 0x0C00, 0x0600, 0x0300, 0x01C0, 0x0140, 0x00E0, 0x00A0, 0x0070, 0x0050, 0x002C, 0x0020, 0x0018, 0xFFFF },
  992. { 0x6000, 0x2400, 0x1400, 0x0A00, 0x0500, 0x0280, 0x01A0, 0x0120, 0x00D0, 0x0090, 0x0068, 0x0048, 0x0030, 0x0028, 0x0014, 0xFFFF, 0xFFFF },
  993. { 0x5000, 0x4000, 0x3000, 0x2000, 0x1000, 0x0800, 0x0200, 0x0180, 0x0100, 0x00C0, 0x0060, 0x0040, 0x0008, 0x0010, 0xFFFF, 0xFFFF, 0xFFFF }
  994. },
  995. {
  996. { 0xF000, 0x3C00, 0x2C00, 0x2000, 0x1E00, 0x1600, 0x1200, 0x1000, 0x0F00, 0x0B00, 0x0780, 0x0580, 0x0400, 0x0340, 0x0240, 0x0140, 0x0040 },
  997. { 0xE000, 0x7800, 0x6000, 0x5000, 0x4000, 0x3800, 0x2800, 0x1C00, 0x0E00, 0x0A00, 0x0700, 0x0500, 0x0380, 0x0300, 0x0200, 0x0100, 0xFFFF },
  998. { 0xD000, 0x7000, 0x5800, 0x4800, 0x3400, 0x2400, 0x1A00, 0x1400, 0x0D00, 0x0900, 0x0680, 0x0480, 0x02C0, 0x01C0, 0x00C0, 0xFFFF, 0xFFFF },
  999. { 0xC000, 0xB000, 0xA000, 0x9000, 0x8000, 0x6800, 0x3000, 0x1800, 0x0C00, 0x0800, 0x0600, 0x0280, 0x0180, 0x0080, 0xFFFF, 0xFFFF, 0xFFFF }
  1000. }
  1001. };
  1002. static const uint16_t masktab[3][4][17] =
  1003. {
  1004. {
  1005. { 0x8000, 0xFC00, 0xFF00, 0xFF80, 0xFFC0, 0xFFE0, 0xFFF8, 0xFFF8, 0xFFF8, 0xFFFC, 0xFFFC, 0xFFFE, 0xFFFE, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF },
  1006. { 0xC000, 0xFC00, 0xFF00, 0xFF80, 0xFFC0, 0xFFE0, 0xFFF8, 0xFFF8, 0xFFFC, 0xFFFC, 0xFFFE, 0xFFFE, 0xFFFE, 0xFFFF, 0xFFFF, 0xFFFF, 0x0000 },
  1007. { 0xE000, 0xFE00, 0xFF00, 0xFF80, 0xFFC0, 0xFFE0, 0xFFF8, 0xFFF8, 0xFFFC, 0xFFFC, 0xFFFE, 0xFFFE, 0xFFFF, 0xFFFF, 0xFFFF, 0x0000, 0x0000 },
  1008. { 0xF800, 0xFC00, 0xFE00, 0xFF00, 0xFF80, 0xFFC0, 0xFFE0, 0xFFF8, 0xFFFC, 0xFFFC, 0xFFFE, 0xFFFE, 0xFFFF, 0xFFFF, 0x0000, 0x0000, 0x0000 }
  1009. },
  1010. {
  1011. { 0xC000, 0xFC00, 0xFC00, 0xFE00, 0xFF00, 0xFF00, 0xFF80, 0xFFE0, 0xFFE0, 0xFFF0, 0xFFF0, 0xFFF0, 0xFFF8, 0xFFF8, 0xFFF8, 0xFFFC, 0xFFFC },
  1012. { 0xC000, 0xF800, 0xFC00, 0xFC00, 0xFE00, 0xFF00, 0xFF80, 0xFFE0, 0xFFE0, 0xFFF0, 0xFFF0, 0xFFF8, 0xFFF8, 0xFFFC, 0xFFFC, 0xFFFC, 0x0000 },
  1013. { 0xE000, 0xFC00, 0xFC00, 0xFE00, 0xFF00, 0xFF80, 0xFFE0, 0xFFE0, 0xFFF0, 0xFFF0, 0xFFF8, 0xFFF8, 0xFFF8, 0xFFFC, 0xFFFC, 0x0000, 0x0000 },
  1014. { 0xF000, 0xF000, 0xF800, 0xFC00, 0xFC00, 0xFE00, 0xFF80, 0xFFE0, 0xFFE0, 0xFFF0, 0xFFF8, 0xFFF8, 0xFFF8, 0xFFFC, 0x0000, 0x0000, 0x0000 }
  1015. },
  1016. {
  1017. { 0xF000, 0xFC00, 0xFC00, 0xFC00, 0xFE00, 0xFE00, 0xFE00, 0xFE00, 0xFF00, 0xFF00, 0xFF80, 0xFF80, 0xFF80, 0xFFC0, 0xFFC0, 0xFFC0, 0xFFC0 },
  1018. { 0xF000, 0xF800, 0xF800, 0xF800, 0xF800, 0xFC00, 0xFC00, 0xFE00, 0xFF00, 0xFF00, 0xFF80, 0xFF80, 0xFF80, 0xFFC0, 0xFFC0, 0xFFC0, 0x0000 },
  1019. { 0xF000, 0xF800, 0xF800, 0xF800, 0xFC00, 0xFC00, 0xFE00, 0xFE00, 0xFF00, 0xFF00, 0xFF80, 0xFF80, 0xFFC0, 0xFFC0, 0xFFC0, 0x0000, 0x0000 },
  1020. { 0xF000, 0xF000, 0xF000, 0xF000, 0xF000, 0xF800, 0xFC00, 0xFE00, 0xFF00, 0xFF00, 0xFF80, 0xFFC0, 0xFFC0, 0xFFC0, 0x0000, 0x0000, 0x0000 }
  1021. }
  1022. };
  1023. int code;
  1024. // vlcnum is the index of Table used to code coeff_token
  1025. // vlcnum==3 means (8<=nC) which uses 6bit FLC
  1026. if (vlcnum == 3)
  1027. {
  1028. // read 6 bit FLC
  1029. //code = ShowBits(buf, frame_bitoffset, BitstreamLengthInBytes, 6);
  1030. code = ShowBits(buf, frame_bitoffset, BitstreamLengthInBits, 6);
  1031. currStream->frame_bitoffset += 6;
  1032. sym->value2 = (code & 3);
  1033. sym->value1 = (code >> 2);
  1034. if (!sym->value1 && sym->value2 == 3)
  1035. {
  1036. // #c = 0, #t1 = 3 => #c = 0
  1037. sym->value2 = 0;
  1038. }
  1039. else
  1040. sym->value1++;
  1041. }
  1042. else
  1043. {
  1044. //retval = code_from_bitstream_2d(sym, currStream, &lentab[vlcnum][0][0], &codtab[vlcnum][0][0], 17, 4, &code);
  1045. code = opt_code_from_bitstream_2d_17_4(sym, currStream, lentab[vlcnum][0], codtab[vlcnum][0], masktab[vlcnum][0]);
  1046. }
  1047. return 0;
  1048. }
  1049. /*!
  1050. ************************************************************************
  1051. * \brief
  1052. * read NumCoeff/TrailingOnes codeword from UVLC-partition ChromaDC
  1053. ************************************************************************
  1054. */
  1055. int readSyntaxElement_NumCoeffTrailingOnesChromaDC(VideoParameters *p_Vid, SyntaxElement *sym, Bitstream *currStream)
  1056. {
  1057. #if 0
  1058. static const uint8_t lentab[3][4][17] =
  1059. {
  1060. //YUV420
  1061. {{ 2, 6, 6, 6, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  1062. { 0, 1, 6, 7, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  1063. { 0, 0, 3, 7, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  1064. { 0, 0, 0, 6, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
  1065. //YUV422
  1066. {{ 1, 7, 7, 9, 9,10,11,12,13, 0, 0, 0, 0, 0, 0, 0, 0},
  1067. { 0, 2, 7, 7, 9,10,11,12,12, 0, 0, 0, 0, 0, 0, 0, 0},
  1068. { 0, 0, 3, 7, 7, 9,10,11,12, 0, 0, 0, 0, 0, 0, 0, 0},
  1069. { 0, 0, 0, 5, 6, 7, 7,10,11, 0, 0, 0, 0, 0, 0, 0, 0}},
  1070. //YUV444
  1071. {{ 1, 6, 8, 9,10,11,13,13,13,14,14,15,15,16,16,16,16},
  1072. { 0, 2, 6, 8, 9,10,11,13,13,14,14,15,15,15,16,16,16},
  1073. { 0, 0, 3, 7, 8, 9,10,11,13,13,14,14,15,15,16,16,16},
  1074. { 0, 0, 0, 5, 6, 7, 8, 9,10,11,13,14,14,15,15,16,16}}
  1075. };
  1076. #endif
  1077. //YUV420
  1078. __declspec(align(32)) static const uint8_t lentab420[16] =
  1079. { 2, 6, 6, 6, 6, 1, 6, 7, 8, 3, 7, 8, 0, 0, 6, 7 };
  1080. __declspec(align(32)) static const uint8_t codtab420[16] =
  1081. { 0x40, 0x1C, 0x10, 0x0C, 0x08, 0x80, 0x18, 0x06, 0x03, 0x20, 0x04, 0x02, 0xFF, 0xFF, 0x14, 0x00 };
  1082. __declspec(align(32)) static const uint8_t masktab420[16] =
  1083. { 0xC0, 0xFC, 0xFC, 0xFC, 0xFC, 0x80, 0xFC, 0xFE, 0xFF, 0xE0, 0xFE, 0xFF, 0x00, 0x00, 0xFC, 0xFE };
  1084. // YUV422
  1085. __declspec(align(32)) static const uint16_t lentab422[4][9] =
  1086. {
  1087. { 1, 7, 7, 9, 9,10,11,12,13 },
  1088. { 2, 7, 7, 9,10,11,12,12, 0 },
  1089. { 3, 7, 7, 9,10,11,12, 0, 0 },
  1090. { 5, 6, 7, 7,10,11, 0, 0, 0 }
  1091. };
  1092. __declspec(align(32)) static const uint16_t codtab422[4][9] =
  1093. {
  1094. { 0x8000, 0x1E00, 0x1C00, 0x0380, 0x0300, 0x01C0, 0x00E0, 0x0070, 0x0038 },
  1095. { 0x4000, 0x1A00, 0x1800, 0x0280, 0x0180, 0x00C0, 0x0060, 0x0050, 0xFFFF },
  1096. { 0x2000, 0x1600, 0x1400, 0x0200, 0x0140, 0x00A0, 0x0040, 0xFFFF, 0xFFFF },
  1097. { 0x0800, 0x0400, 0x1200, 0x1000, 0x0100, 0x0080, 0xFFFF, 0xFFFF, 0xFFFF }
  1098. };
  1099. __declspec(align(32)) static const uint16_t masktab422[4][9] =
  1100. {
  1101. { 0x8000, 0xFE00, 0xFE00, 0xFF80, 0xFF80, 0xFFC0, 0xFFE0, 0xFFF0, 0xFFF8 },
  1102. { 0xC000, 0xFE00, 0xFE00, 0xFF80, 0xFFC0, 0xFFE0, 0xFFF0, 0xFFF0, 0x0000 },
  1103. { 0xE000, 0xFE00, 0xFE00, 0xFF80, 0xFFC0, 0xFFE0, 0xFFF0, 0x0000, 0x0000 },
  1104. { 0xF800, 0xFC00, 0xFE00, 0xFE00, 0xFFC0, 0xFFE0, 0x0000, 0x0000, 0x0000 }
  1105. };
  1106. // YUV444
  1107. __declspec(align(32)) static const uint16_t lentab444[4][17] =
  1108. {
  1109. { 1, 6, 8, 9,10,11,13,13,13,14,14,15,15,16,16,16,16},
  1110. { 2, 6, 8, 9,10,11,13,13,14,14,15,15,15,16,16,16, 0},
  1111. { 3, 7, 8, 9,10,11,13,13,14,14,15,15,16,16,16, 0, 0},
  1112. { 5, 6, 7, 8, 9,10,11,13,14,14,15,15,16,16, 0, 0, 0}
  1113. };
  1114. __declspec(align(32)) static const uint16_t codtab444[4][17] =
  1115. {
  1116. { 0x8000, 0x1400, 0x0700, 0x0380, 0x01C0, 0x00E0, 0x0078, 0x0058, 0x0040, 0x003C, 0x002C, 0x001E, 0x0016, 0x000F, 0x000B, 0x0007, 0x0004 },
  1117. { 0x4000, 0x1000, 0x0600, 0x0300, 0x0180, 0x00C0, 0x0070, 0x0050, 0x0038, 0x0028, 0x001C, 0x0014, 0x0002, 0x000E, 0x000A, 0x0006, 0xFFFF },
  1118. { 0x2000, 0x0A00, 0x0500, 0x0280, 0x0140, 0x00A0, 0x0068, 0x0048, 0x0034, 0x0024, 0x001A, 0x0012, 0x000D, 0x0009, 0x0005, 0xFFFF, 0xFFFF },
  1119. { 0x1800, 0x0C00, 0x0800, 0x0400, 0x0200, 0x0100, 0x0080, 0x0060, 0x0030, 0x0020, 0x0018, 0x0010, 0x000C, 0x0008, 0xFFFF, 0xFFFF, 0xFFFF }
  1120. };
  1121. __declspec(align(32)) static const uint16_t masktab444[4][17] =
  1122. {
  1123. { 0x8000, 0xFC00, 0xFF00, 0xFF80, 0xFFC0, 0xFFE0, 0xFFF8, 0xFFF8, 0xFFF8, 0xFFFC, 0xFFFC, 0xFFFE, 0xFFFE, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF },
  1124. { 0xC000, 0xFC00, 0xFF00, 0xFF80, 0xFFC0, 0xFFE0, 0xFFF8, 0xFFF8, 0xFFFC, 0xFFFC, 0xFFFE, 0xFFFE, 0xFFFE, 0xFFFF, 0xFFFF, 0xFFFF, 0x0000 },
  1125. { 0xE000, 0xFE00, 0xFF00, 0xFF80, 0xFFC0, 0xFFE0, 0xFFF8, 0xFFF8, 0xFFFC, 0xFFFC, 0xFFFE, 0xFFFE, 0xFFFF, 0xFFFF, 0xFFFF, 0x0000, 0x0000 },
  1126. { 0xF800, 0xFC00, 0xFE00, 0xFF00, 0xFF80, 0xFFC0, 0xFFE0, 0xFFF8, 0xFFFC, 0xFFFC, 0xFFFE, 0xFFFE, 0xFFFF, 0xFFFF, 0x0000, 0x0000, 0x0000 }
  1127. };
  1128. #if 0
  1129. static const uint8_t codtab[3][4][17] =
  1130. {
  1131. //YUV420
  1132. {{ 1, 7, 4, 3, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  1133. { 0, 1, 6, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  1134. { 0, 0, 1, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  1135. { 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
  1136. //YUV422
  1137. {{ 1,15,14, 7, 6, 7, 7, 7, 7, 0, 0, 0, 0, 0, 0, 0, 0},
  1138. { 0, 1,13,12, 5, 6, 6, 6, 5, 0, 0, 0, 0, 0, 0, 0, 0},
  1139. { 0, 0, 1,11,10, 4, 5, 5, 4, 0, 0, 0, 0, 0, 0, 0, 0},
  1140. { 0, 0, 0, 1, 1, 9, 8, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0}},
  1141. //YUV444
  1142. {{ 1, 5, 7, 7, 7, 7,15,11, 8,15,11,15,11,15,11, 7, 4},
  1143. { 0, 1, 4, 6, 6, 6, 6,14,10,14,10,14,10, 1,14,10, 6},
  1144. { 0, 0, 1, 5, 5, 5, 5, 5,13, 9,13, 9,13, 9,13, 9, 5},
  1145. { 0, 0, 0, 3, 3, 4, 4, 4, 4, 4,12,12, 8,12, 8,12, 8}}
  1146. };
  1147. #endif
  1148. int code;
  1149. int yuv = p_Vid->active_sps->chroma_format_idc - 1;
  1150. switch(yuv)
  1151. {
  1152. case 0:
  1153. code = opt_code_from_bitstream_2d_5_4(sym, currStream, lentab420, codtab420, masktab420);
  1154. break;
  1155. case 1:
  1156. code = code_from_bitstream_2d_9_4(sym, currStream, lentab422[0], codtab422[0], masktab422[0]);
  1157. break;
  1158. case 2:
  1159. code = opt_code_from_bitstream_2d_17_4(sym, currStream, lentab444[0], codtab444[0], masktab444[0]);
  1160. break;
  1161. default:
  1162. __assume(0);
  1163. return -1;
  1164. }
  1165. return 0;
  1166. }
  1167. /*!
  1168. ************************************************************************
  1169. * \brief
  1170. * read Level VLC0 codeword from UVLC-partition
  1171. ************************************************************************
  1172. */
  1173. int readSyntaxElement_Level_VLC0(Bitstream *currStream)
  1174. {
  1175. int frame_bitoffset = currStream->frame_bitoffset;
  1176. int BitstreamLengthInBytes = currStream->bitstream_length;
  1177. int BitstreamLengthInBits = (BitstreamLengthInBytes << 3) + 7;
  1178. byte *buf = currStream->streamBuffer;
  1179. int len = 1, sign = 0, level = 0, code = 1;
  1180. while (!ShowBits(buf, frame_bitoffset++, BitstreamLengthInBits, 1))
  1181. len++;
  1182. if (len < 15)
  1183. {
  1184. sign = (len - 1) & 1;
  1185. level = ((len - 1) >> 1) + 1;
  1186. }
  1187. else if (len == 15)
  1188. {
  1189. // escape code
  1190. code <<= 4;
  1191. code |= ShowBits(buf, frame_bitoffset, BitstreamLengthInBits, 4);
  1192. len += 4;
  1193. frame_bitoffset += 4;
  1194. sign = (code & 0x01);
  1195. level = ((code >> 1) & 0x07) + 8;
  1196. }
  1197. else if (len >= 16)
  1198. {
  1199. // escape code
  1200. int addbit = (len - 16);
  1201. int offset = (2048 << addbit) - 2032;
  1202. len -= 4;
  1203. code = ShowBits(buf, frame_bitoffset, BitstreamLengthInBits, len);
  1204. sign = (code & 0x01);
  1205. frame_bitoffset += len;
  1206. level = (code >> 1) + offset;
  1207. code |= (1 << (len)); // for display purpose only
  1208. len += addbit + 16;
  1209. }
  1210. currStream->frame_bitoffset = frame_bitoffset;
  1211. return (sign) ? -level : level ;
  1212. //sym->len = len;
  1213. #if TRACE
  1214. tracebits2(sym->tracestring, sym->len, code);
  1215. #endif
  1216. return 0;
  1217. }
  1218. /*!
  1219. ************************************************************************
  1220. * \brief
  1221. * read Level VLC codeword from UVLC-partition
  1222. ************************************************************************
  1223. */
  1224. int readSyntaxElement_Level_VLCN(int vlc, Bitstream *currStream)
  1225. {
  1226. int frame_bitoffset = currStream->frame_bitoffset;
  1227. int BitstreamLengthInBytes = currStream->bitstream_length;
  1228. int BitstreamLengthInBits = (BitstreamLengthInBytes << 3) + 7;
  1229. byte *buf = currStream->streamBuffer;
  1230. int levabs, sign;
  1231. int len = 1;
  1232. int code = 1, sb;
  1233. int shift = vlc - 1;
  1234. // read pre zeros
  1235. while (!ShowBits(buf, frame_bitoffset ++, BitstreamLengthInBits, 1))
  1236. len++;
  1237. frame_bitoffset -= len;
  1238. if (len < 16)
  1239. {
  1240. levabs = ((len - 1) << shift) + 1;
  1241. // read (vlc-1) bits -> suffix
  1242. if (shift)
  1243. {
  1244. sb = ShowBits(buf, frame_bitoffset + len, BitstreamLengthInBits, shift);
  1245. code = (code << (shift) )| sb;
  1246. levabs += sb;
  1247. len += (shift);
  1248. }
  1249. // read 1 bit -> sign
  1250. sign = ShowBits(buf, frame_bitoffset + len, BitstreamLengthInBits, 1);
  1251. code = (code << 1)| sign;
  1252. len ++;
  1253. }
  1254. else // escape
  1255. {
  1256. int addbit = len - 5;
  1257. int offset = (1 << addbit) + (15 << shift) - 2047;
  1258. sb = ShowBits(buf, frame_bitoffset + len, BitstreamLengthInBits, addbit);
  1259. code = (code << addbit ) | sb;
  1260. len += addbit;
  1261. levabs = sb + offset;
  1262. // read 1 bit -> sign
  1263. sign = ShowBits(buf, frame_bitoffset + len, BitstreamLengthInBits, 1);
  1264. code = (code << 1)| sign;
  1265. len++;
  1266. }
  1267. currStream->frame_bitoffset = frame_bitoffset + len;
  1268. return (sign)? -levabs : levabs;
  1269. }
  1270. /*!
  1271. ************************************************************************
  1272. * \brief
  1273. * read Total Zeros codeword from UVLC-partition
  1274. ************************************************************************
  1275. */
  1276. int readSyntaxElement_TotalZeros(Bitstream *currStream, int vlcnum)
  1277. {
  1278. __declspec(align(32)) static const uint16_t lentab[TOTRUN_NUM][16] =
  1279. {
  1280. { 1,3,3,4,4,5,5,6,6,7,7,8,8,9,9,9},
  1281. { 3,3,3,3,3,4,4,4,4,5,5,6,6,6,6},
  1282. { 4,3,3,3,4,4,3,3,4,5,5,6,5,6},
  1283. { 5,3,4,4,3,3,3,4,3,4,5,5,5},
  1284. { 4,4,4,3,3,3,3,3,4,5,4,5},
  1285. { 6,5,3,3,3,3,3,3,4,3,6},
  1286. { 6,5,3,3,3,2,3,4,3,6},
  1287. { 6,4,5,3,2,2,3,3,6},
  1288. { 6,6,4,2,2,3,2,5},
  1289. { 5,5,3,2,2,2,4},
  1290. { 4,4,3,3,1,3},
  1291. { 4,4,2,1,3},
  1292. { 3,3,1,2},
  1293. { 2,2,1},
  1294. { 1,1},
  1295. };
  1296. /*
  1297. static const byte codtab[TOTRUN_NUM][16] =
  1298. {
  1299. {1,3,2,3,2,3,2,3,2,3,2,3,2,3,2,1},
  1300. {7,6,5,4,3,5,4,3,2,3,2,3,2,1,0},
  1301. {5,7,6,5,4,3,4,3,2,3,2,1,1,0},
  1302. {3,7,5,4,6,5,4,3,3,2,2,1,0},
  1303. {5,4,3,7,6,5,4,3,2,1,1,0},
  1304. {1,1,7,6,5,4,3,2,1,1,0},
  1305. {1,1,5,4,3,3,2,1,1,0},
  1306. {1,1,1,3,3,2,2,1,0},
  1307. {1,0,1,3,2,1,1,1,},
  1308. {1,0,1,3,2,1,1,},
  1309. {0,1,1,2,1,3},
  1310. {0,1,1,1,1},
  1311. {0,1,1,1},
  1312. {0,1,1},
  1313. {0,1},
  1314. };*/
  1315. __declspec(align(32)) static const uint16_t codtab[TOTRUN_NUM][16] =
  1316. {
  1317. { 0x8000, 0x6000, 0x4000, 0x3000, 0x2000, 0x1800, 0x1000, 0x0C00, 0x0800, 0x0600, 0x0400, 0x0300, 0x0200, 0x0180, 0x0100, 0x0080, },
  1318. { 0xE000, 0xC000, 0xA000, 0x8000, 0x6000, 0x5000, 0x4000, 0x3000, 0x2000, 0x1800, 0x1000, 0x0C00, 0x0800, 0x0400, 0x0000, 0xFFFF, },
  1319. { 0x5000, 0xE000, 0xC000, 0xA000, 0x4000, 0x3000, 0x8000, 0x6000, 0x2000, 0x1800, 0x1000, 0x0400, 0x0800, 0x0000, 0xFFFF, 0xFFFF, },
  1320. { 0x1800, 0xE000, 0x5000, 0x4000, 0xC000, 0xA000, 0x8000, 0x3000, 0x6000, 0x2000, 0x1000, 0x0800, 0x0000, 0xFFFF, 0xFFFF, 0xFFFF, },
  1321. { 0x5000, 0x4000, 0x3000, 0xE000, 0xC000, 0xA000, 0x8000, 0x6000, 0x2000, 0x0800, 0x1000, 0x0000, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, },
  1322. { 0x0400, 0x0800, 0xE000, 0xC000, 0xA000, 0x8000, 0x6000, 0x4000, 0x1000, 0x2000, 0x0000, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, },
  1323. { 0x0400, 0x0800, 0xA000, 0x8000, 0x6000, 0xC000, 0x4000, 0x1000, 0x2000, 0x0000, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, },
  1324. { 0x0400, 0x1000, 0x0800, 0x6000, 0xC000, 0x8000, 0x4000, 0x2000, 0x0000, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, },
  1325. { 0x0400, 0x0000, 0x1000, 0xC000, 0x8000, 0x2000, 0x4000, 0x0800, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, },
  1326. { 0x0800, 0x0000, 0x2000, 0xC000, 0x8000, 0x4000, 0x1000, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, },
  1327. { 0x0000, 0x1000, 0x2000, 0x4000, 0x8000, 0x6000, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, },
  1328. { 0x0000, 0x1000, 0x4000, 0x8000, 0x2000, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, },
  1329. { 0x0000, 0x2000, 0x8000, 0x4000, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, },
  1330. { 0x0000, 0x4000, 0x8000, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, },
  1331. { 0x0000, 0x8000, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, }
  1332. };
  1333. __declspec(align(32)) static const uint16_t masktab[TOTRUN_NUM][16] =
  1334. {
  1335. { 0x8000, 0xE000, 0xE000, 0xF000, 0xF000, 0xF800, 0xF800, 0xFC00, 0xFC00, 0xFE00, 0xFE00, 0xFF00, 0xFF00, 0xFF80, 0xFF80, 0xFF80, },
  1336. { 0xE000, 0xE000, 0xE000, 0xE000, 0xE000, 0xF000, 0xF000, 0xF000, 0xF000, 0xF800, 0xF800, 0xFC00, 0xFC00, 0xFC00, 0xFC00, 0x0000, },
  1337. { 0xF000, 0xE000, 0xE000, 0xE000, 0xF000, 0xF000, 0xE000, 0xE000, 0xF000, 0xF800, 0xF800, 0xFC00, 0xF800, 0xFC00, 0x0000, 0x0000, },
  1338. { 0xF800, 0xE000, 0xF000, 0xF000, 0xE000, 0xE000, 0xE000, 0xF000, 0xE000, 0xF000, 0xF800, 0xF800, 0xF800, 0x0000, 0x0000, 0x0000, },
  1339. { 0xF000, 0xF000, 0xF000, 0xE000, 0xE000, 0xE000, 0xE000, 0xE000, 0xF000, 0xF800, 0xF000, 0xF800, 0x0000, 0x0000, 0x0000, 0x0000, },
  1340. { 0xFC00, 0xF800, 0xE000, 0xE000, 0xE000, 0xE000, 0xE000, 0xE000, 0xF000, 0xE000, 0xFC00, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, },
  1341. { 0xFC00, 0xF800, 0xE000, 0xE000, 0xE000, 0xC000, 0xE000, 0xF000, 0xE000, 0xFC00, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, },
  1342. { 0xFC00, 0xF000, 0xF800, 0xE000, 0xC000, 0xC000, 0xE000, 0xE000, 0xFC00, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, },
  1343. { 0xFC00, 0xFC00, 0xF000, 0xC000, 0xC000, 0xE000, 0xC000, 0xF800, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, },
  1344. { 0xF800, 0xF800, 0xE000, 0xC000, 0xC000, 0xC000, 0xF000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, },
  1345. { 0xF000, 0xF000, 0xE000, 0xE000, 0x8000, 0xE000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, },
  1346. { 0xF000, 0xF000, 0xC000, 0x8000, 0xE000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, },
  1347. { 0xE000, 0xE000, 0x8000, 0xC000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, },
  1348. { 0xC000, 0xC000, 0x8000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, },
  1349. { 0x8000, 0x8000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, },
  1350. };
  1351. return opt_code_from_bitstream_2d_16_1(currStream, lentab[vlcnum], codtab[vlcnum], masktab[vlcnum]);
  1352. }
  1353. /*!
  1354. ************************************************************************
  1355. * \brief
  1356. * read Total Zeros Chroma DC codeword from UVLC-partition
  1357. ************************************************************************
  1358. */
  1359. int readSyntaxElement_TotalZerosChromaDC(VideoParameters *p_Vid, Bitstream *currStream, int vlcnum)
  1360. {
  1361. static const byte lentab[3][TOTRUN_NUM][16] =
  1362. {
  1363. //YUV420
  1364. {{ 1,2,3,3},
  1365. { 1,2,2},
  1366. { 1,1}},
  1367. //YUV422
  1368. {{ 1,3,3,4,4,4,5,5},
  1369. { 3,2,3,3,3,3,3},
  1370. { 3,3,2,2,3,3},
  1371. { 3,2,2,2,3},
  1372. { 2,2,2,2},
  1373. { 2,2,1},
  1374. { 1,1}},
  1375. //YUV444
  1376. {{ 1,3,3,4,4,5,5,6,6,7,7,8,8,9,9,9},
  1377. { 3,3,3,3,3,4,4,4,4,5,5,6,6,6,6},
  1378. { 4,3,3,3,4,4,3,3,4,5,5,6,5,6},
  1379. { 5,3,4,4,3,3,3,4,3,4,5,5,5},
  1380. { 4,4,4,3,3,3,3,3,4,5,4,5},
  1381. { 6,5,3,3,3,3,3,3,4,3,6},
  1382. { 6,5,3,3,3,2,3,4,3,6},
  1383. { 6,4,5,3,2,2,3,3,6},
  1384. { 6,6,4,2,2,3,2,5},
  1385. { 5,5,3,2,2,2,4},
  1386. { 4,4,3,3,1,3},
  1387. { 4,4,2,1,3},
  1388. { 3,3,1,2},
  1389. { 2,2,1},
  1390. { 1,1}}
  1391. };
  1392. static const byte codtab[3][TOTRUN_NUM][16] =
  1393. {
  1394. //YUV420
  1395. {{ 1,1,1,0},
  1396. { 1,1,0},
  1397. { 1,0}},
  1398. //YUV422
  1399. {{ 1,2,3,2,3,1,1,0},
  1400. { 0,1,1,4,5,6,7},
  1401. { 0,1,1,2,6,7},
  1402. { 6,0,1,2,7},
  1403. { 0,1,2,3},
  1404. { 0,1,1},
  1405. { 0,1}},
  1406. //YUV444
  1407. {{1,3,2,3,2,3,2,3,2,3,2,3,2,3,2,1},
  1408. {7,6,5,4,3,5,4,3,2,3,2,3,2,1,0},
  1409. {5,7,6,5,4,3,4,3,2,3,2,1,1,0},
  1410. {3,7,5,4,6,5,4,3,3,2,2,1,0},
  1411. {5,4,3,7,6,5,4,3,2,1,1,0},
  1412. {1,1,7,6,5,4,3,2,1,1,0},
  1413. {1,1,5,4,3,3,2,1,1,0},
  1414. {1,1,1,3,3,2,2,1,0},
  1415. {1,0,1,3,2,1,1,1,},
  1416. {1,0,1,3,2,1,1,},
  1417. {0,1,1,2,1,3},
  1418. {0,1,1,1,1},
  1419. {0,1,1,1},
  1420. {0,1,1},
  1421. {0,1}}
  1422. };
  1423. int yuv = p_Vid->active_sps->chroma_format_idc - 1;
  1424. return code_from_bitstream_2d_16_1(currStream, &lentab[yuv][vlcnum][0], &codtab[yuv][vlcnum][0]);
  1425. }
  1426. /*!
  1427. ************************************************************************
  1428. * \brief
  1429. * read Run codeword from UVLC-partition
  1430. ************************************************************************
  1431. */
  1432. int readSyntaxElement_Run(Bitstream *currStream, int vlcnum)
  1433. {
  1434. __declspec(align(32)) static const uint16_t lentab[TOTRUN_NUM][16] =
  1435. {
  1436. {1,1},
  1437. {1,2,2},
  1438. {2,2,2,2},
  1439. {2,2,2,3,3},
  1440. {2,2,3,3,3,3},
  1441. {2,3,3,3,3,3,3},
  1442. {3,3,3,3,3,3,3,4,5,6,7,8,9,10,11},
  1443. };
  1444. /*
  1445. static const byte codtab[TOTRUN_NUM][16] =
  1446. {
  1447. {1,0},
  1448. {1,1,0},
  1449. {3,2,1,0},
  1450. {3,2,1,1,0},
  1451. {3,2,3,2,1,0},
  1452. {3,0,1,3,2,5,4},
  1453. {7,6,5,4,3,2,1,1,1,1,1,1,1,1,1},
  1454. };*/
  1455. __declspec(align(32)) static const uint16_t codtab[TOTRUN_NUM][16] =
  1456. {
  1457. { 0x8000, 0x0000, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, },
  1458. { 0x8000, 0x4000, 0x0000, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, },
  1459. { 0xC000, 0x8000, 0x4000, 0x0000, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, },
  1460. { 0xC000, 0x8000, 0x4000, 0x2000, 0x0000, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, },
  1461. { 0xC000, 0x8000, 0x6000, 0x4000, 0x2000, 0x0000, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, },
  1462. { 0xC000, 0x0000, 0x2000, 0x6000, 0x4000, 0xA000, 0x8000, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, },
  1463. { 0xE000, 0xC000, 0xA000, 0x8000, 0x6000, 0x4000, 0x2000, 0x1000, 0x0800, 0x0400, 0x0200, 0x0100, 0x0080, 0x0040, 0x0020, 0xFFFF, }
  1464. };
  1465. __declspec(align(32)) static const uint16_t masktab[TOTRUN_NUM][16] =
  1466. {
  1467. { 0x8000, 0x8000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, },
  1468. { 0x8000, 0xC000, 0xC000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, },
  1469. { 0xC000, 0xC000, 0xC000, 0xC000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, },
  1470. { 0xC000, 0xC000, 0xC000, 0xE000, 0xE000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, },
  1471. { 0xC000, 0xC000, 0xE000, 0xE000, 0xE000, 0xE000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, },
  1472. { 0xC000, 0xE000, 0xE000, 0xE000, 0xE000, 0xE000, 0xE000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, },
  1473. { 0xE000, 0xE000, 0xE000, 0xE000, 0xE000, 0xE000, 0xE000, 0xF000, 0xF800, 0xFC00, 0xFE00, 0xFF00, 0xFF80, 0xFFC0, 0xFFE0, 0x0000, }
  1474. };
  1475. return opt_code_from_bitstream_2d_16_1(currStream, lentab[vlcnum], codtab[vlcnum], masktab[vlcnum]);
  1476. }
  1477. /*!
  1478. ************************************************************************
  1479. * \brief
  1480. * Reads bits from the bitstream buffer
  1481. *
  1482. * \param buffer
  1483. * containing VLC-coded data bits
  1484. * \param totbitoffset
  1485. * bit offset from start of partition
  1486. * \param info
  1487. * returns value of the read bits
  1488. * \param bitcount
  1489. * total bytes in bitstream
  1490. * \param numbits
  1491. * number of bits to read
  1492. *
  1493. ************************************************************************
  1494. */
  1495. int GetBits (const uint8_t buffer[],int totbitoffset,int *info, int bitcount,
  1496. int numbits)
  1497. {
  1498. int bitoffset = /*7 - */(totbitoffset & 0x07); // bit from start of byte
  1499. int byteoffset = (totbitoffset >> 3); // byte from start of buffer
  1500. const uint8_t *ptr = &(buffer[byteoffset]);
  1501. uint32_t tmp = (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | (ptr[3]);
  1502. tmp <<= bitoffset;
  1503. tmp >>= 32 - numbits;
  1504. *info = tmp;
  1505. return numbits;
  1506. }
  1507. /*!
  1508. ************************************************************************
  1509. * \brief
  1510. * Reads bits from the bitstream buffer
  1511. *
  1512. * \param buffer
  1513. * buffer containing VLC-coded data bits
  1514. * \param totbitoffset
  1515. * bit offset from start of partition
  1516. * \param bitcount
  1517. * total bytes in bitstream
  1518. * \param numbits
  1519. * number of bits to read
  1520. *
  1521. ************************************************************************
  1522. */
  1523. static int ShowBits (const uint8_t buffer[],int totbitoffset,int bitcount, int numbits)
  1524. {
  1525. int bitoffset = /*7 - */(totbitoffset & 0x07); // bit from start of byte
  1526. int byteoffset = (totbitoffset >> 3); // byte from start of buffer
  1527. const uint8_t *ptr = &(buffer[byteoffset]);
  1528. uint32_t tmp = (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | (ptr[3]);
  1529. tmp <<= bitoffset;
  1530. tmp >>= 32 - numbits;
  1531. return tmp;
  1532. #if 0
  1533. if ((totbitoffset + numbits ) > bitcount)
  1534. {
  1535. return -1;
  1536. }
  1537. else
  1538. {
  1539. int bitoffset = 7 - (totbitoffset & 0x07); // bit from start of byte
  1540. int byteoffset = (totbitoffset >> 3); // byte from start of buffer
  1541. const uint8_t *curbyte = &(buffer[byteoffset]);
  1542. int inf = 0;
  1543. while (numbits--)
  1544. {
  1545. inf <<=1;
  1546. inf |= ((*curbyte)>> (bitoffset--)) & 0x01;
  1547. if (bitoffset == -1 )
  1548. { //Move onto next byte to get all of numbits
  1549. curbyte++;
  1550. bitoffset = 7;
  1551. }
  1552. }
  1553. return inf; // return absolute offset in bit from start of frame
  1554. }
  1555. #endif
  1556. }