cavsdec.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335
  1. /*
  2. * Chinese AVS video (AVS1-P2, JiZhun profile) decoder.
  3. * Copyright (c) 2006 Stefan Gehrer <stefan.gehrer@gmx.de>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * Chinese AVS video (AVS1-P2, JiZhun profile) decoder
  24. * @author Stefan Gehrer <stefan.gehrer@gmx.de>
  25. */
  26. #include "libavutil/avassert.h"
  27. #include "libavutil/emms.h"
  28. #include "libavutil/mem.h"
  29. #include "avcodec.h"
  30. #include "get_bits.h"
  31. #include "golomb.h"
  32. #include "cavs.h"
  33. #include "codec_internal.h"
  34. #include "decode.h"
  35. #include "mathops.h"
  36. #include "mpeg12data.h"
  37. #include "startcode.h"
  38. static const uint8_t mv_scan[4] = {
  39. MV_FWD_X0, MV_FWD_X1,
  40. MV_FWD_X2, MV_FWD_X3
  41. };
  42. static const uint8_t cbp_tab[64][2] = {
  43. { 63, 0 }, { 15, 15 }, { 31, 63 }, { 47, 31 }, { 0, 16 }, { 14, 32 }, { 13, 47 }, { 11, 13 },
  44. { 7, 14 }, { 5, 11 }, { 10, 12 }, { 8, 5 }, { 12, 10 }, { 61, 7 }, { 4, 48 }, { 55, 3 },
  45. { 1, 2 }, { 2, 8 }, { 59, 4 }, { 3, 1 }, { 62, 61 }, { 9, 55 }, { 6, 59 }, { 29, 62 },
  46. { 45, 29 }, { 51, 27 }, { 23, 23 }, { 39, 19 }, { 27, 30 }, { 46, 28 }, { 53, 9 }, { 30, 6 },
  47. { 43, 60 }, { 37, 21 }, { 60, 44 }, { 16, 26 }, { 21, 51 }, { 28, 35 }, { 19, 18 }, { 35, 20 },
  48. { 42, 24 }, { 26, 53 }, { 44, 17 }, { 32, 37 }, { 58, 39 }, { 24, 45 }, { 20, 58 }, { 17, 43 },
  49. { 18, 42 }, { 48, 46 }, { 22, 36 }, { 33, 33 }, { 25, 34 }, { 49, 40 }, { 40, 52 }, { 36, 49 },
  50. { 34, 50 }, { 50, 56 }, { 52, 25 }, { 54, 22 }, { 41, 54 }, { 56, 57 }, { 38, 41 }, { 57, 38 }
  51. };
  52. static const uint8_t scan3x3[4] = { 4, 5, 7, 8 };
  53. static const uint8_t dequant_shift[64] = {
  54. 14, 14, 14, 14, 14, 14, 14, 14,
  55. 13, 13, 13, 13, 13, 13, 13, 13,
  56. 13, 12, 12, 12, 12, 12, 12, 12,
  57. 11, 11, 11, 11, 11, 11, 11, 11,
  58. 11, 10, 10, 10, 10, 10, 10, 10,
  59. 10, 9, 9, 9, 9, 9, 9, 9,
  60. 9, 8, 8, 8, 8, 8, 8, 8,
  61. 7, 7, 7, 7, 7, 7, 7, 7
  62. };
  63. static const uint16_t dequant_mul[64] = {
  64. 32768, 36061, 38968, 42495, 46341, 50535, 55437, 60424,
  65. 32932, 35734, 38968, 42495, 46177, 50535, 55109, 59933,
  66. 65535, 35734, 38968, 42577, 46341, 50617, 55027, 60097,
  67. 32809, 35734, 38968, 42454, 46382, 50576, 55109, 60056,
  68. 65535, 35734, 38968, 42495, 46320, 50515, 55109, 60076,
  69. 65535, 35744, 38968, 42495, 46341, 50535, 55099, 60087,
  70. 65535, 35734, 38973, 42500, 46341, 50535, 55109, 60097,
  71. 32771, 35734, 38965, 42497, 46341, 50535, 55109, 60099
  72. };
  73. #define EOB 0, 0, 0
  74. static const struct dec_2dvlc intra_dec[7] = {
  75. {
  76. { //level / run / table_inc
  77. { 1, 1, 1 }, { -1, 1, 1 }, { 1, 2, 1 }, { -1, 2, 1 }, { 1, 3, 1 }, { -1, 3, 1 },
  78. { 1, 4, 1 }, { -1, 4, 1 }, { 1, 5, 1 }, { -1, 5, 1 }, { 1, 6, 1 }, { -1, 6, 1 },
  79. { 1, 7, 1 }, { -1, 7, 1 }, { 1, 8, 1 }, { -1, 8, 1 }, { 1, 9, 1 }, { -1, 9, 1 },
  80. { 1, 10, 1 }, { -1, 10, 1 }, { 1, 11, 1 }, { -1, 11, 1 }, { 2, 1, 2 }, { -2, 1, 2 },
  81. { 1, 12, 1 }, { -1, 12, 1 }, { 1, 13, 1 }, { -1, 13, 1 }, { 1, 14, 1 }, { -1, 14, 1 },
  82. { 1, 15, 1 }, { -1, 15, 1 }, { 2, 2, 2 }, { -2, 2, 2 }, { 1, 16, 1 }, { -1, 16, 1 },
  83. { 1, 17, 1 }, { -1, 17, 1 }, { 3, 1, 3 }, { -3, 1, 3 }, { 1, 18, 1 }, { -1, 18, 1 },
  84. { 1, 19, 1 }, { -1, 19, 1 }, { 2, 3, 2 }, { -2, 3, 2 }, { 1, 20, 1 }, { -1, 20, 1 },
  85. { 1, 21, 1 }, { -1, 21, 1 }, { 2, 4, 2 }, { -2, 4, 2 }, { 1, 22, 1 }, { -1, 22, 1 },
  86. { 2, 5, 2 }, { -2, 5, 2 }, { 1, 23, 1 }, { -1, 23, 1 }, { EOB }
  87. },
  88. //level_add
  89. { 0, 4, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1, -1, -1 },
  90. 2, //golomb_order
  91. 0, //inc_limit
  92. 23, //max_run
  93. },
  94. {
  95. { //level / run
  96. { 1, 1, 0 }, { -1, 1, 0 }, { 1, 2, 0 }, { -1, 2, 0 }, { 2, 1, 1 }, { -2, 1, 1 },
  97. { 1, 3, 0 }, { -1, 3, 0 }, { EOB }, { 1, 4, 0 }, { -1, 4, 0 }, { 1, 5, 0 },
  98. { -1, 5, 0 }, { 1, 6, 0 }, { -1, 6, 0 }, { 3, 1, 2 }, { -3, 1, 2 }, { 2, 2, 1 },
  99. { -2, 2, 1 }, { 1, 7, 0 }, { -1, 7, 0 }, { 1, 8, 0 }, { -1, 8, 0 }, { 1, 9, 0 },
  100. { -1, 9, 0 }, { 2, 3, 1 }, { -2, 3, 1 }, { 4, 1, 2 }, { -4, 1, 2 }, { 1, 10, 0 },
  101. { -1, 10, 0 }, { 1, 11, 0 }, { -1, 11, 0 }, { 2, 4, 1 }, { -2, 4, 1 }, { 3, 2, 2 },
  102. { -3, 2, 2 }, { 1, 12, 0 }, { -1, 12, 0 }, { 2, 5, 1 }, { -2, 5, 1 }, { 5, 1, 3 },
  103. { -5, 1, 3 }, { 1, 13, 0 }, { -1, 13, 0 }, { 2, 6, 1 }, { -2, 6, 1 }, { 1, 14, 0 },
  104. { -1, 14, 0 }, { 2, 7, 1 }, { -2, 7, 1 }, { 2, 8, 1 }, { -2, 8, 1 }, { 3, 3, 2 },
  105. { -3, 3, 2 }, { 6, 1, 3 }, { -6, 1, 3 }, { 1, 15, 0 }, { -1, 15, 0 }
  106. },
  107. //level_add
  108. { 0, 7, 4, 4, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  109. 2, //golomb_order
  110. 1, //inc_limit
  111. 15, //max_run
  112. },
  113. {
  114. { //level / run
  115. { 1, 1, 0 }, { -1, 1, 0 }, { 2, 1, 0 }, { -2, 1, 0 }, { 1, 2, 0 }, { -1, 2, 0 },
  116. { 3, 1, 1 }, { -3, 1, 1 }, { EOB }, { 1, 3, 0 }, { -1, 3, 0 }, { 2, 2, 0 },
  117. { -2, 2, 0 }, { 4, 1, 1 }, { -4, 1, 1 }, { 1, 4, 0 }, { -1, 4, 0 }, { 5, 1, 2 },
  118. { -5, 1, 2 }, { 1, 5, 0 }, { -1, 5, 0 }, { 3, 2, 1 }, { -3, 2, 1 }, { 2, 3, 0 },
  119. { -2, 3, 0 }, { 1, 6, 0 }, { -1, 6, 0 }, { 6, 1, 2 }, { -6, 1, 2 }, { 2, 4, 0 },
  120. { -2, 4, 0 }, { 1, 7, 0 }, { -1, 7, 0 }, { 4, 2, 1 }, { -4, 2, 1 }, { 7, 1, 2 },
  121. { -7, 1, 2 }, { 3, 3, 1 }, { -3, 3, 1 }, { 2, 5, 0 }, { -2, 5, 0 }, { 1, 8, 0 },
  122. { -1, 8, 0 }, { 2, 6, 0 }, { -2, 6, 0 }, { 8, 1, 3 }, { -8, 1, 3 }, { 1, 9, 0 },
  123. { -1, 9, 0 }, { 5, 2, 2 }, { -5, 2, 2 }, { 3, 4, 1 }, { -3, 4, 1 }, { 2, 7, 0 },
  124. { -2, 7, 0 }, { 9, 1, 3 }, { -9, 1, 3 }, { 1, 10, 0 }, { -1, 10, 0 }
  125. },
  126. //level_add
  127. { 0, 10, 6, 4, 4, 3, 3, 3, 2, 2, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  128. 2, //golomb_order
  129. 2, //inc_limit
  130. 10, //max_run
  131. },
  132. {
  133. { //level / run
  134. { 1, 1, 0 }, { -1, 1, 0 }, { 2, 1, 0 }, { -2, 1, 0 }, { 3, 1, 0 }, { -3, 1, 0 },
  135. { 1, 2, 0 }, { -1, 2, 0 }, { EOB }, { 4, 1, 0 }, { -4, 1, 0 }, { 5, 1, 1 },
  136. { -5, 1, 1 }, { 2, 2, 0 }, { -2, 2, 0 }, { 1, 3, 0 }, { -1, 3, 0 }, { 6, 1, 1 },
  137. { -6, 1, 1 }, { 3, 2, 0 }, { -3, 2, 0 }, { 7, 1, 1 }, { -7, 1, 1 }, { 1, 4, 0 },
  138. { -1, 4, 0 }, { 8, 1, 2 }, { -8, 1, 2 }, { 2, 3, 0 }, { -2, 3, 0 }, { 4, 2, 0 },
  139. { -4, 2, 0 }, { 1, 5, 0 }, { -1, 5, 0 }, { 9, 1, 2 }, { -9, 1, 2 }, { 5, 2, 1 },
  140. { -5, 2, 1 }, { 2, 4, 0 }, { -2, 4, 0 }, { 10, 1, 2 }, {-10, 1, 2 }, { 3, 3, 0 },
  141. { -3, 3, 0 }, { 1, 6, 0 }, { -1, 6, 0 }, { 11, 1, 3 }, {-11, 1, 3 }, { 6, 2, 1 },
  142. { -6, 2, 1 }, { 1, 7, 0 }, { -1, 7, 0 }, { 2, 5, 0 }, { -2, 5, 0 }, { 3, 4, 0 },
  143. { -3, 4, 0 }, { 12, 1, 3 }, {-12, 1, 3 }, { 4, 3, 0 }, { -4, 3, 0 }
  144. },
  145. //level_add
  146. { 0, 13, 7, 5, 4, 3, 2, 2, -1, -1, -1 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  147. 2, //golomb_order
  148. 4, //inc_limit
  149. 7, //max_run
  150. },
  151. {
  152. { //level / run
  153. { 1, 1, 0 }, { -1, 1, 0 }, { 2, 1, 0 }, { -2, 1, 0 }, { 3, 1, 0 }, { -3, 1, 0 },
  154. { EOB }, { 4, 1, 0 }, { -4, 1, 0 }, { 5, 1, 0 }, { -5, 1, 0 }, { 6, 1, 0 },
  155. { -6, 1, 0 }, { 1, 2, 0 }, { -1, 2, 0 }, { 7, 1, 0 }, { -7, 1, 0 }, { 8, 1, 1 },
  156. { -8, 1, 1 }, { 2, 2, 0 }, { -2, 2, 0 }, { 9, 1, 1 }, { -9, 1, 1 }, { 10, 1, 1 },
  157. {-10, 1, 1 }, { 1, 3, 0 }, { -1, 3, 0 }, { 3, 2, 0 }, { -3, 2, 0 }, { 11, 1, 2 },
  158. {-11, 1, 2 }, { 4, 2, 0 }, { -4, 2, 0 }, { 12, 1, 2 }, {-12, 1, 2 }, { 13, 1, 2 },
  159. {-13, 1, 2 }, { 5, 2, 0 }, { -5, 2, 0 }, { 1, 4, 0 }, { -1, 4, 0 }, { 2, 3, 0 },
  160. { -2, 3, 0 }, { 14, 1, 2 }, {-14, 1, 2 }, { 6, 2, 0 }, { -6, 2, 0 }, { 15, 1, 2 },
  161. {-15, 1, 2 }, { 16, 1, 2 }, {-16, 1, 2 }, { 3, 3, 0 }, { -3, 3, 0 }, { 1, 5, 0 },
  162. { -1, 5, 0 }, { 7, 2, 0 }, { -7, 2, 0 }, { 17, 1, 2 }, {-17, 1, 2 }
  163. },
  164. //level_add
  165. { 0,18, 8, 4, 2, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  166. 2, //golomb_order
  167. 7, //inc_limit
  168. 5, //max_run
  169. },
  170. {
  171. { //level / run
  172. { EOB }, { 1, 1, 0 }, { -1, 1, 0 }, { 2, 1, 0 }, { -2, 1, 0 }, { 3, 1, 0 },
  173. { -3, 1, 0 }, { 4, 1, 0 }, { -4, 1, 0 }, { 5, 1, 0 }, { -5, 1, 0 }, { 6, 1, 0 },
  174. { -6, 1, 0 }, { 7, 1, 0 }, { -7, 1, 0 }, { 8, 1, 0 }, { -8, 1, 0 }, { 9, 1, 0 },
  175. { -9, 1, 0 }, { 10, 1, 0 }, {-10, 1, 0 }, { 1, 2, 0 }, { -1, 2, 0 }, { 11, 1, 1 },
  176. {-11, 1, 1 }, { 12, 1, 1 }, {-12, 1, 1 }, { 13, 1, 1 }, {-13, 1, 1 }, { 2, 2, 0 },
  177. { -2, 2, 0 }, { 14, 1, 1 }, {-14, 1, 1 }, { 15, 1, 1 }, {-15, 1, 1 }, { 3, 2, 0 },
  178. { -3, 2, 0 }, { 16, 1, 1 }, {-16, 1, 1 }, { 1, 3, 0 }, { -1, 3, 0 }, { 17, 1, 1 },
  179. {-17, 1, 1 }, { 4, 2, 0 }, { -4, 2, 0 }, { 18, 1, 1 }, {-18, 1, 1 }, { 5, 2, 0 },
  180. { -5, 2, 0 }, { 19, 1, 1 }, {-19, 1, 1 }, { 20, 1, 1 }, {-20, 1, 1 }, { 6, 2, 0 },
  181. { -6, 2, 0 }, { 21, 1, 1 }, {-21, 1, 1 }, { 2, 3, 0 }, { -2, 3, 0 }
  182. },
  183. //level_add
  184. { 0, 22, 7, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  185. 2, //golomb_order
  186. 10, //inc_limit
  187. 3, //max_run
  188. },
  189. {
  190. { //level / run
  191. { EOB }, { 1, 1, 0 }, { -1, 1, 0 }, { 2, 1, 0 }, { -2, 1, 0 }, { 3, 1, 0 },
  192. { -3, 1, 0 }, { 4, 1, 0 }, { -4, 1, 0 }, { 5, 1, 0 }, { -5, 1, 0 }, { 6, 1, 0 },
  193. { -6, 1, 0 }, { 7, 1, 0 }, { -7, 1, 0 }, { 8, 1, 0 }, { -8, 1, 0 }, { 9, 1, 0 },
  194. { -9, 1, 0 }, { 10, 1, 0 }, {-10, 1, 0 }, { 11, 1, 0 }, {-11, 1, 0 }, { 12, 1, 0 },
  195. {-12, 1, 0 }, { 13, 1, 0 }, {-13, 1, 0 }, { 14, 1, 0 }, {-14, 1, 0 }, { 15, 1, 0 },
  196. {-15, 1, 0 }, { 16, 1, 0 }, {-16, 1, 0 }, { 1, 2, 0 }, { -1, 2, 0 }, { 17, 1, 0 },
  197. {-17, 1, 0 }, { 18, 1, 0 }, {-18, 1, 0 }, { 19, 1, 0 }, {-19, 1, 0 }, { 20, 1, 0 },
  198. {-20, 1, 0 }, { 21, 1, 0 }, {-21, 1, 0 }, { 2, 2, 0 }, { -2, 2, 0 }, { 22, 1, 0 },
  199. {-22, 1, 0 }, { 23, 1, 0 }, {-23, 1, 0 }, { 24, 1, 0 }, {-24, 1, 0 }, { 25, 1, 0 },
  200. {-25, 1, 0 }, { 3, 2, 0 }, { -3, 2, 0 }, { 26, 1, 0 }, {-26, 1, 0 }
  201. },
  202. //level_add
  203. { 0, 27, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  204. 2, //golomb_order
  205. INT_MAX, //inc_limit
  206. 2, //max_run
  207. }
  208. };
  209. static const struct dec_2dvlc inter_dec[7] = {
  210. {
  211. { //level / run
  212. { 1, 1, 1 }, { -1, 1, 1 }, { 1, 2, 1 }, { -1, 2, 1 }, { 1, 3, 1 }, { -1, 3, 1 },
  213. { 1, 4, 1 }, { -1, 4, 1 }, { 1, 5, 1 }, { -1, 5, 1 }, { 1, 6, 1 }, { -1, 6, 1 },
  214. { 1, 7, 1 }, { -1, 7, 1 }, { 1, 8, 1 }, { -1, 8, 1 }, { 1, 9, 1 }, { -1, 9, 1 },
  215. { 1, 10, 1 }, { -1, 10, 1 }, { 1, 11, 1 }, { -1, 11, 1 }, { 1, 12, 1 }, { -1, 12, 1 },
  216. { 1, 13, 1 }, { -1, 13, 1 }, { 2, 1, 2 }, { -2, 1, 2 }, { 1, 14, 1 }, { -1, 14, 1 },
  217. { 1, 15, 1 }, { -1, 15, 1 }, { 1, 16, 1 }, { -1, 16, 1 }, { 1, 17, 1 }, { -1, 17, 1 },
  218. { 1, 18, 1 }, { -1, 18, 1 }, { 1, 19, 1 }, { -1, 19, 1 }, { 3, 1, 3 }, { -3, 1, 3 },
  219. { 1, 20, 1 }, { -1, 20, 1 }, { 1, 21, 1 }, { -1, 21, 1 }, { 2, 2, 2 }, { -2, 2, 2 },
  220. { 1, 22, 1 }, { -1, 22, 1 }, { 1, 23, 1 }, { -1, 23, 1 }, { 1, 24, 1 }, { -1, 24, 1 },
  221. { 1, 25, 1 }, { -1, 25, 1 }, { 1, 26, 1 }, { -1, 26, 1 }, { EOB }
  222. },
  223. //level_add
  224. { 0, 4, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 },
  225. 3, //golomb_order
  226. 0, //inc_limit
  227. 26 //max_run
  228. },
  229. {
  230. { //level / run
  231. { 1, 1, 0 }, { -1, 1, 0 }, { EOB }, { 1, 2, 0 }, { -1, 2, 0 }, { 1, 3, 0 },
  232. { -1, 3, 0 }, { 1, 4, 0 }, { -1, 4, 0 }, { 1, 5, 0 }, { -1, 5, 0 }, { 1, 6, 0 },
  233. { -1, 6, 0 }, { 2, 1, 1 }, { -2, 1, 1 }, { 1, 7, 0 }, { -1, 7, 0 }, { 1, 8, 0 },
  234. { -1, 8, 0 }, { 1, 9, 0 }, { -1, 9, 0 }, { 1, 10, 0 }, { -1, 10, 0 }, { 2, 2, 1 },
  235. { -2, 2, 1 }, { 1, 11, 0 }, { -1, 11, 0 }, { 1, 12, 0 }, { -1, 12, 0 }, { 3, 1, 2 },
  236. { -3, 1, 2 }, { 1, 13, 0 }, { -1, 13, 0 }, { 1, 14, 0 }, { -1, 14, 0 }, { 2, 3, 1 },
  237. { -2, 3, 1 }, { 1, 15, 0 }, { -1, 15, 0 }, { 2, 4, 1 }, { -2, 4, 1 }, { 1, 16, 0 },
  238. { -1, 16, 0 }, { 2, 5, 1 }, { -2, 5, 1 }, { 1, 17, 0 }, { -1, 17, 0 }, { 4, 1, 3 },
  239. { -4, 1, 3 }, { 2, 6, 1 }, { -2, 6, 1 }, { 1, 18, 0 }, { -1, 18, 0 }, { 1, 19, 0 },
  240. { -1, 19, 0 }, { 2, 7, 1 }, { -2, 7, 1 }, { 3, 2, 2 }, { -3, 2, 2 }
  241. },
  242. //level_add
  243. { 0, 5, 4, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1, -1, -1, -1, -1, -1, -1 },
  244. 2, //golomb_order
  245. 1, //inc_limit
  246. 19 //max_run
  247. },
  248. {
  249. { //level / run
  250. { 1, 1, 0 }, { -1, 1, 0 }, { EOB }, { 1, 2, 0 }, { -1, 2, 0 }, { 2, 1, 0 },
  251. { -2, 1, 0 }, { 1, 3, 0 }, { -1, 3, 0 }, { 1, 4, 0 }, { -1, 4, 0 }, { 3, 1, 1 },
  252. { -3, 1, 1 }, { 2, 2, 0 }, { -2, 2, 0 }, { 1, 5, 0 }, { -1, 5, 0 }, { 1, 6, 0 },
  253. { -1, 6, 0 }, { 1, 7, 0 }, { -1, 7, 0 }, { 2, 3, 0 }, { -2, 3, 0 }, { 4, 1, 2 },
  254. { -4, 1, 2 }, { 1, 8, 0 }, { -1, 8, 0 }, { 3, 2, 1 }, { -3, 2, 1 }, { 2, 4, 0 },
  255. { -2, 4, 0 }, { 1, 9, 0 }, { -1, 9, 0 }, { 1, 10, 0 }, { -1, 10, 0 }, { 5, 1, 2 },
  256. { -5, 1, 2 }, { 2, 5, 0 }, { -2, 5, 0 }, { 1, 11, 0 }, { -1, 11, 0 }, { 2, 6, 0 },
  257. { -2, 6, 0 }, { 1, 12, 0 }, { -1, 12, 0 }, { 3, 3, 1 }, { -3, 3, 1 }, { 6, 1, 2 },
  258. { -6, 1, 2 }, { 4, 2, 2 }, { -4, 2, 2 }, { 1, 13, 0 }, { -1, 13, 0 }, { 2, 7, 0 },
  259. { -2, 7, 0 }, { 3, 4, 1 }, { -3, 4, 1 }, { 1, 14, 0 }, { -1, 14, 0 }
  260. },
  261. //level_add
  262. { 0, 7, 5, 4, 4, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  263. 2, //golomb_order
  264. 2, //inc_limit
  265. 14 //max_run
  266. },
  267. {
  268. { //level / run
  269. { 1, 1, 0 }, { -1, 1, 0 }, { EOB }, { 2, 1, 0 }, { -2, 1, 0 }, { 1, 2, 0 },
  270. { -1, 2, 0 }, { 3, 1, 0 }, { -3, 1, 0 }, { 1, 3, 0 }, { -1, 3, 0 }, { 2, 2, 0 },
  271. { -2, 2, 0 }, { 4, 1, 1 }, { -4, 1, 1 }, { 1, 4, 0 }, { -1, 4, 0 }, { 5, 1, 1 },
  272. { -5, 1, 1 }, { 1, 5, 0 }, { -1, 5, 0 }, { 3, 2, 0 }, { -3, 2, 0 }, { 2, 3, 0 },
  273. { -2, 3, 0 }, { 1, 6, 0 }, { -1, 6, 0 }, { 6, 1, 1 }, { -6, 1, 1 }, { 2, 4, 0 },
  274. { -2, 4, 0 }, { 1, 7, 0 }, { -1, 7, 0 }, { 4, 2, 1 }, { -4, 2, 1 }, { 7, 1, 2 },
  275. { -7, 1, 2 }, { 3, 3, 0 }, { -3, 3, 0 }, { 1, 8, 0 }, { -1, 8, 0 }, { 2, 5, 0 },
  276. { -2, 5, 0 }, { 8, 1, 2 }, { -8, 1, 2 }, { 1, 9, 0 }, { -1, 9, 0 }, { 3, 4, 0 },
  277. { -3, 4, 0 }, { 2, 6, 0 }, { -2, 6, 0 }, { 5, 2, 1 }, { -5, 2, 1 }, { 1, 10, 0 },
  278. { -1, 10, 0 }, { 9, 1, 2 }, { -9, 1, 2 }, { 4, 3, 1 }, { -4, 3, 1 }
  279. },
  280. //level_add
  281. { 0,10, 6, 5, 4, 3, 3, 2, 2, 2, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  282. 2, //golomb_order
  283. 3, //inc_limit
  284. 10 //max_run
  285. },
  286. {
  287. { //level / run
  288. { 1, 1, 0 }, { -1, 1, 0 }, { EOB }, { 2, 1, 0 }, { -2, 1, 0 }, { 3, 1, 0 },
  289. { -3, 1, 0 }, { 1, 2, 0 }, { -1, 2, 0 }, { 4, 1, 0 }, { -4, 1, 0 }, { 5, 1, 0 },
  290. { -5, 1, 0 }, { 2, 2, 0 }, { -2, 2, 0 }, { 1, 3, 0 }, { -1, 3, 0 }, { 6, 1, 0 },
  291. { -6, 1, 0 }, { 3, 2, 0 }, { -3, 2, 0 }, { 7, 1, 1 }, { -7, 1, 1 }, { 1, 4, 0 },
  292. { -1, 4, 0 }, { 8, 1, 1 }, { -8, 1, 1 }, { 2, 3, 0 }, { -2, 3, 0 }, { 4, 2, 0 },
  293. { -4, 2, 0 }, { 1, 5, 0 }, { -1, 5, 0 }, { 9, 1, 1 }, { -9, 1, 1 }, { 5, 2, 0 },
  294. { -5, 2, 0 }, { 2, 4, 0 }, { -2, 4, 0 }, { 1, 6, 0 }, { -1, 6, 0 }, { 10, 1, 2 },
  295. {-10, 1, 2 }, { 3, 3, 0 }, { -3, 3, 0 }, { 11, 1, 2 }, {-11, 1, 2 }, { 1, 7, 0 },
  296. { -1, 7, 0 }, { 6, 2, 0 }, { -6, 2, 0 }, { 3, 4, 0 }, { -3, 4, 0 }, { 2, 5, 0 },
  297. { -2, 5, 0 }, { 12, 1, 2 }, {-12, 1, 2 }, { 4, 3, 0 }, { -4, 3, 0 }
  298. },
  299. //level_add
  300. { 0, 13, 7, 5, 4, 3, 2, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  301. 2, //golomb_order
  302. 6, //inc_limit
  303. 7 //max_run
  304. },
  305. {
  306. { //level / run
  307. { EOB }, { 1, 1, 0 }, { -1, 1, 0 }, { 2, 1, 0 }, { -2, 1, 0 }, { 3, 1, 0 },
  308. { -3, 1, 0 }, { 4, 1, 0 }, { -4, 1, 0 }, { 5, 1, 0 }, { -5, 1, 0 }, { 1, 2, 0 },
  309. { -1, 2, 0 }, { 6, 1, 0 }, { -6, 1, 0 }, { 7, 1, 0 }, { -7, 1, 0 }, { 8, 1, 0 },
  310. { -8, 1, 0 }, { 2, 2, 0 }, { -2, 2, 0 }, { 9, 1, 0 }, { -9, 1, 0 }, { 1, 3, 0 },
  311. { -1, 3, 0 }, { 10, 1, 1 }, { -10, 1, 1 }, { 3, 2, 0 }, { -3, 2, 0 }, { 11, 1, 1 },
  312. { -11, 1, 1 }, { 4, 2, 0 }, { -4, 2, 0 }, { 12, 1, 1 }, { -12, 1, 1 }, { 1, 4, 0 },
  313. { -1, 4, 0 }, { 2, 3, 0 }, { -2, 3, 0 }, { 13, 1, 1 }, { -13, 1, 1 }, { 5, 2, 0 },
  314. { -5, 2, 0 }, { 14, 1, 1 }, { -14, 1, 1 }, { 6, 2, 0 }, { -6, 2, 0 }, { 1, 5, 0 },
  315. { -1, 5, 0 }, { 15, 1, 1 }, { -15, 1, 1 }, { 3, 3, 0 }, { -3, 3, 0 }, { 16, 1, 1 },
  316. { -16, 1, 1 }, { 2, 4, 0 }, { -2, 4, 0 }, { 7, 2, 0 }, { -7, 2, 0 }
  317. },
  318. //level_add
  319. { 0, 17, 8, 4, 3, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  320. 2, //golomb_order
  321. 9, //inc_limit
  322. 5 //max_run
  323. },
  324. {
  325. { //level / run
  326. { EOB }, { 1, 1, 0 }, { -1, 1, 0 }, { 2, 1, 0 }, { -2, 1, 0 }, { 3, 1, 0 },
  327. { -3, 1, 0 }, { 4, 1, 0 }, { -4, 1, 0 }, { 5, 1, 0 }, { -5, 1, 0 }, { 6, 1, 0 },
  328. { -6, 1, 0 }, { 7, 1, 0 }, { -7, 1, 0 }, { 1, 2, 0 }, { -1, 2, 0 }, { 8, 1, 0 },
  329. { -8, 1, 0 }, { 9, 1, 0 }, { -9, 1, 0 }, { 10, 1, 0 }, { -10, 1, 0 }, { 11, 1, 0 },
  330. { -11, 1, 0 }, { 12, 1, 0 }, { -12, 1, 0 }, { 2, 2, 0 }, { -2, 2, 0 }, { 13, 1, 0 },
  331. { -13, 1, 0 }, { 1, 3, 0 }, { -1, 3, 0 }, { 14, 1, 0 }, { -14, 1, 0 }, { 15, 1, 0 },
  332. { -15, 1, 0 }, { 3, 2, 0 }, { -3, 2, 0 }, { 16, 1, 0 }, { -16, 1, 0 }, { 17, 1, 0 },
  333. { -17, 1, 0 }, { 18, 1, 0 }, { -18, 1, 0 }, { 4, 2, 0 }, { -4, 2, 0 }, { 19, 1, 0 },
  334. { -19, 1, 0 }, { 20, 1, 0 }, { -20, 1, 0 }, { 2, 3, 0 }, { -2, 3, 0 }, { 1, 4, 0 },
  335. { -1, 4, 0 }, { 5, 2, 0 }, { -5, 2, 0 }, { 21, 1, 0 }, { -21, 1, 0 }
  336. },
  337. //level_add
  338. { 0, 22, 6, 3, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  339. 2, //golomb_order
  340. INT_MAX, //inc_limit
  341. 4 //max_run
  342. }
  343. };
  344. static const struct dec_2dvlc chroma_dec[5] = {
  345. {
  346. { //level / run
  347. { 1, 1, 1 }, { -1, 1, 1 }, { 1, 2, 1 }, { -1, 2, 1 }, { 1, 3, 1 }, { -1, 3, 1 },
  348. { 1, 4, 1 }, { -1, 4, 1 }, { 1, 5, 1 }, { -1, 5, 1 }, { 1, 6, 1 }, { -1, 6, 1 },
  349. { 1, 7, 1 }, { -1, 7, 1 }, { 2, 1, 2 }, { -2, 1, 2 }, { 1, 8, 1 }, { -1, 8, 1 },
  350. { 1, 9, 1 }, { -1, 9, 1 }, { 1, 10, 1 }, { -1, 10, 1 }, { 1, 11, 1 }, { -1, 11, 1 },
  351. { 1, 12, 1 }, { -1, 12, 1 }, { 1, 13, 1 }, { -1, 13, 1 }, { 1, 14, 1 }, { -1, 14, 1 },
  352. { 1, 15, 1 }, { -1, 15, 1 }, { 3, 1, 3 }, { -3, 1, 3 }, { 1, 16, 1 }, { -1, 16, 1 },
  353. { 1, 17, 1 }, { -1, 17, 1 }, { 1, 18, 1 }, { -1, 18, 1 }, { 1, 19, 1 }, { -1, 19, 1 },
  354. { 1, 20, 1 }, { -1, 20, 1 }, { 1, 21, 1 }, { -1, 21, 1 }, { 1, 22, 1 }, { -1, 22, 1 },
  355. { 2, 2, 2 }, { -2, 2, 2 }, { 1, 23, 1 }, { -1, 23, 1 }, { 1, 24, 1 }, { -1, 24, 1 },
  356. { 1, 25, 1 }, { -1, 25, 1 }, { 4, 1, 3 }, { -4, 1, 3 }, { EOB }
  357. },
  358. //level_add
  359. { 0, 5, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1 },
  360. 2, //golomb_order
  361. 0, //inc_limit
  362. 25 //max_run
  363. },
  364. {
  365. { //level / run
  366. { EOB }, { 1, 1, 0 }, { -1, 1, 0 }, { 1, 2, 0 }, { -1, 2, 0 }, { 2, 1, 1 },
  367. { -2, 1, 1 }, { 1, 3, 0 }, { -1, 3, 0 }, { 1, 4, 0 }, { -1, 4, 0 }, { 1, 5, 0 },
  368. { -1, 5, 0 }, { 1, 6, 0 }, { -1, 6, 0 }, { 3, 1, 2 }, { -3, 1, 2 }, { 1, 7, 0 },
  369. { -1, 7, 0 }, { 1, 8, 0 }, { -1, 8, 0 }, { 2, 2, 1 }, { -2, 2, 1 }, { 1, 9, 0 },
  370. { -1, 9, 0 }, { 1, 10, 0 }, { -1, 10, 0 }, { 1, 11, 0 }, { -1, 11, 0 }, { 4, 1, 2 },
  371. { -4, 1, 2 }, { 1, 12, 0 }, { -1, 12, 0 }, { 1, 13, 0 }, { -1, 13, 0 }, { 1, 14, 0 },
  372. { -1, 14, 0 }, { 2, 3, 1 }, { -2, 3, 1 }, { 1, 15, 0 }, { -1, 15, 0 }, { 2, 4, 1 },
  373. { -2, 4, 1 }, { 5, 1, 3 }, { -5, 1, 3 }, { 3, 2, 2 }, { -3, 2, 2 }, { 1, 16, 0 },
  374. { -1, 16, 0 }, { 1, 17, 0 }, { -1, 17, 0 }, { 1, 18, 0 }, { -1, 18, 0 }, { 2, 5, 1 },
  375. { -2, 5, 1 }, { 1, 19, 0 }, { -1, 19, 0 }, { 1, 20, 0 }, { -1, 20, 0 }
  376. },
  377. //level_add
  378. { 0, 6, 4, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1, -1, -1, -1, -1, -1 },
  379. 0, //golomb_order
  380. 1, //inc_limit
  381. 20 //max_run
  382. },
  383. {
  384. { //level / run
  385. { 1, 1, 0 }, { -1, 1, 0 }, { EOB }, { 2, 1, 0 }, { -2, 1, 0 }, { 1, 2, 0 },
  386. { -1, 2, 0 }, { 3, 1, 1 }, { -3, 1, 1 }, { 1, 3, 0 }, { -1, 3, 0 }, { 4, 1, 1 },
  387. { -4, 1, 1 }, { 2, 2, 0 }, { -2, 2, 0 }, { 1, 4, 0 }, { -1, 4, 0 }, { 5, 1, 2 },
  388. { -5, 1, 2 }, { 1, 5, 0 }, { -1, 5, 0 }, { 3, 2, 1 }, { -3, 2, 1 }, { 2, 3, 0 },
  389. { -2, 3, 0 }, { 1, 6, 0 }, { -1, 6, 0 }, { 6, 1, 2 }, { -6, 1, 2 }, { 1, 7, 0 },
  390. { -1, 7, 0 }, { 2, 4, 0 }, { -2, 4, 0 }, { 7, 1, 2 }, { -7, 1, 2 }, { 1, 8, 0 },
  391. { -1, 8, 0 }, { 4, 2, 1 }, { -4, 2, 1 }, { 1, 9, 0 }, { -1, 9, 0 }, { 3, 3, 1 },
  392. { -3, 3, 1 }, { 2, 5, 0 }, { -2, 5, 0 }, { 2, 6, 0 }, { -2, 6, 0 }, { 8, 1, 2 },
  393. { -8, 1, 2 }, { 1, 10, 0 }, { -1, 10, 0 }, { 1, 11, 0 }, { -1, 11, 0 }, { 9, 1, 2 },
  394. { -9, 1, 2 }, { 5, 2, 2 }, { -5, 2, 2 }, { 3, 4, 1 }, { -3, 4, 1 },
  395. },
  396. //level_add
  397. { 0,10, 6, 4, 4, 3, 3, 2, 2, 2, 2, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  398. 1, //golomb_order
  399. 2, //inc_limit
  400. 11 //max_run
  401. },
  402. {
  403. { //level / run
  404. { EOB }, { 1, 1, 0 }, { -1, 1, 0 }, { 2, 1, 0 }, { -2, 1, 0 }, { 3, 1, 0 },
  405. { -3, 1, 0 }, { 4, 1, 0 }, { -4, 1, 0 }, { 1, 2, 0 }, { -1, 2, 0 }, { 5, 1, 1 },
  406. { -5, 1, 1 }, { 2, 2, 0 }, { -2, 2, 0 }, { 6, 1, 1 }, { -6, 1, 1 }, { 1, 3, 0 },
  407. { -1, 3, 0 }, { 7, 1, 1 }, { -7, 1, 1 }, { 3, 2, 0 }, { -3, 2, 0 }, { 8, 1, 1 },
  408. { -8, 1, 1 }, { 1, 4, 0 }, { -1, 4, 0 }, { 2, 3, 0 }, { -2, 3, 0 }, { 9, 1, 1 },
  409. { -9, 1, 1 }, { 4, 2, 0 }, { -4, 2, 0 }, { 1, 5, 0 }, { -1, 5, 0 }, { 10, 1, 1 },
  410. {-10, 1, 1 }, { 3, 3, 0 }, { -3, 3, 0 }, { 5, 2, 1 }, { -5, 2, 1 }, { 2, 4, 0 },
  411. { -2, 4, 0 }, { 11, 1, 1 }, {-11, 1, 1 }, { 1, 6, 0 }, { -1, 6, 0 }, { 12, 1, 1 },
  412. {-12, 1, 1 }, { 1, 7, 0 }, { -1, 7, 0 }, { 6, 2, 1 }, { -6, 2, 1 }, { 13, 1, 1 },
  413. {-13, 1, 1 }, { 2, 5, 0 }, { -2, 5, 0 }, { 1, 8, 0 }, { -1, 8, 0 },
  414. },
  415. //level_add
  416. { 0, 14, 7, 4, 3, 3, 2, 2, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  417. 1, //golomb_order
  418. 4, //inc_limit
  419. 8 //max_run
  420. },
  421. {
  422. { //level / run
  423. { EOB }, { 1, 1, 0 }, { -1, 1, 0 }, { 2, 1, 0 }, { -2, 1, 0 }, { 3, 1, 0 },
  424. { -3, 1, 0 }, { 4, 1, 0 }, { -4, 1, 0 }, { 5, 1, 0 }, { -5, 1, 0 }, { 6, 1, 0 },
  425. { -6, 1, 0 }, { 7, 1, 0 }, { -7, 1, 0 }, { 8, 1, 0 }, { -8, 1, 0 }, { 1, 2, 0 },
  426. { -1, 2, 0 }, { 9, 1, 0 }, { -9, 1, 0 }, { 10, 1, 0 }, { -10, 1, 0 }, { 11, 1, 0 },
  427. { -11, 1, 0 }, { 2, 2, 0 }, { -2, 2, 0 }, { 12, 1, 0 }, { -12, 1, 0 }, { 13, 1, 0 },
  428. { -13, 1, 0 }, { 3, 2, 0 }, { -3, 2, 0 }, { 14, 1, 0 }, { -14, 1, 0 }, { 1, 3, 0 },
  429. { -1, 3, 0 }, { 15, 1, 0 }, { -15, 1, 0 }, { 4, 2, 0 }, { -4, 2, 0 }, { 16, 1, 0 },
  430. { -16, 1, 0 }, { 17, 1, 0 }, { -17, 1, 0 }, { 5, 2, 0 }, { -5, 2, 0 }, { 1, 4, 0 },
  431. { -1, 4, 0 }, { 2, 3, 0 }, { -2, 3, 0 }, { 18, 1, 0 }, { -18, 1, 0 }, { 6, 2, 0 },
  432. { -6, 2, 0 }, { 19, 1, 0 }, { -19, 1, 0 }, { 1, 5, 0 }, { -1, 5, 0 },
  433. },
  434. //level_add
  435. { 0, 20, 7, 3, 2, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  436. 0, //golomb_order
  437. INT_MAX, //inc_limit
  438. 5, //max_run
  439. }
  440. };
  441. #undef EOB
  442. /*****************************************************************************
  443. *
  444. * motion vector prediction
  445. *
  446. ****************************************************************************/
  447. static inline void store_mvs(AVSContext *h)
  448. {
  449. h->col_mv[h->mbidx * 4 + 0] = h->mv[MV_FWD_X0];
  450. h->col_mv[h->mbidx * 4 + 1] = h->mv[MV_FWD_X1];
  451. h->col_mv[h->mbidx * 4 + 2] = h->mv[MV_FWD_X2];
  452. h->col_mv[h->mbidx * 4 + 3] = h->mv[MV_FWD_X3];
  453. }
  454. static inline void mv_pred_direct(AVSContext *h, cavs_vector *pmv_fw,
  455. cavs_vector *col_mv)
  456. {
  457. cavs_vector *pmv_bw = pmv_fw + MV_BWD_OFFS;
  458. unsigned den = h->direct_den[col_mv->ref];
  459. int m = FF_SIGNBIT(col_mv->x);
  460. pmv_fw->dist = h->dist[1];
  461. pmv_bw->dist = h->dist[0];
  462. pmv_fw->ref = 1;
  463. pmv_bw->ref = 0;
  464. /* scale the co-located motion vector according to its temporal span */
  465. pmv_fw->x = (((den + (den * col_mv->x * pmv_fw->dist ^ m) - m - 1) >> 14) ^ m) - m;
  466. pmv_bw->x = m - (((den + (den * col_mv->x * pmv_bw->dist ^ m) - m - 1) >> 14) ^ m);
  467. m = FF_SIGNBIT(col_mv->y);
  468. pmv_fw->y = (((den + (den * col_mv->y * pmv_fw->dist ^ m) - m - 1) >> 14) ^ m) - m;
  469. pmv_bw->y = m - (((den + (den * col_mv->y * pmv_bw->dist ^ m) - m - 1) >> 14) ^ m);
  470. }
  471. static inline void mv_pred_sym(AVSContext *h, cavs_vector *src,
  472. enum cavs_block size)
  473. {
  474. cavs_vector *dst = src + MV_BWD_OFFS;
  475. /* backward mv is the scaled and negated forward mv */
  476. dst->x = -((src->x * h->sym_factor + 256) >> 9);
  477. dst->y = -((src->y * h->sym_factor + 256) >> 9);
  478. dst->ref = 0;
  479. dst->dist = h->dist[0];
  480. set_mvs(dst, size);
  481. }
  482. /*****************************************************************************
  483. *
  484. * residual data decoding
  485. *
  486. ****************************************************************************/
  487. /** kth-order exponential golomb code */
  488. static inline int get_ue_code(GetBitContext *gb, int order)
  489. {
  490. unsigned ret = get_ue_golomb(gb);
  491. if (ret >= ((1U<<31)>>order)) {
  492. av_log(NULL, AV_LOG_ERROR, "get_ue_code: value too large\n");
  493. return AVERROR_INVALIDDATA;
  494. }
  495. if (order) {
  496. return (ret<<order) + get_bits(gb, order);
  497. }
  498. return ret;
  499. }
  500. static inline int dequant(AVSContext *h, int16_t *level_buf, uint8_t *run_buf,
  501. int16_t *dst, int mul, int shift, int coeff_num)
  502. {
  503. int round = 1 << (shift - 1);
  504. int pos = -1;
  505. const uint8_t *scantab = h->permutated_scantable;
  506. /* inverse scan and dequantization */
  507. while (--coeff_num >= 0) {
  508. pos += run_buf[coeff_num];
  509. if (pos > 63) {
  510. av_log(h->avctx, AV_LOG_ERROR,
  511. "position out of block bounds at pic %d MB(%d,%d)\n",
  512. h->cur.poc, h->mbx, h->mby);
  513. return AVERROR_INVALIDDATA;
  514. }
  515. dst[scantab[pos]] = (level_buf[coeff_num] * mul + round) >> shift;
  516. }
  517. return 0;
  518. }
  519. /**
  520. * decode coefficients from one 8x8 block, dequantize, inverse transform
  521. * and add them to sample block
  522. * @param r pointer to 2D VLC table
  523. * @param esc_golomb_order escape codes are k-golomb with this order k
  524. * @param qp quantizer
  525. * @param dst location of sample block
  526. * @param stride line stride in frame buffer
  527. */
  528. static int decode_residual_block(AVSContext *h, GetBitContext *gb,
  529. const struct dec_2dvlc *r, int esc_golomb_order,
  530. int qp, uint8_t *dst, ptrdiff_t stride)
  531. {
  532. int i, esc_code, level, mask, ret;
  533. unsigned int level_code, run;
  534. int16_t level_buf[65];
  535. uint8_t run_buf[65];
  536. int16_t *block = h->block;
  537. for (i = 0; i < 65; i++) {
  538. level_code = get_ue_code(gb, r->golomb_order);
  539. if (level_code >= ESCAPE_CODE) {
  540. run = ((level_code - ESCAPE_CODE) >> 1) + 1;
  541. if(run > 64) {
  542. av_log(h->avctx, AV_LOG_ERROR, "run %d is too large\n", run);
  543. return AVERROR_INVALIDDATA;
  544. }
  545. esc_code = get_ue_code(gb, esc_golomb_order);
  546. if (esc_code < 0 || esc_code > 32767) {
  547. av_log(h->avctx, AV_LOG_ERROR, "esc_code invalid\n");
  548. return AVERROR_INVALIDDATA;
  549. }
  550. level = esc_code + (run > r->max_run ? 1 : r->level_add[run]);
  551. while (level > r->inc_limit)
  552. r++;
  553. mask = -(level_code & 1);
  554. level = (level ^ mask) - mask;
  555. } else {
  556. level = r->rltab[level_code][0];
  557. if (!level) //end of block signal
  558. break;
  559. run = r->rltab[level_code][1];
  560. r += r->rltab[level_code][2];
  561. }
  562. level_buf[i] = level;
  563. run_buf[i] = run;
  564. }
  565. if ((ret = dequant(h, level_buf, run_buf, block, dequant_mul[qp],
  566. dequant_shift[qp], i)) < 0)
  567. return ret;
  568. h->cdsp.cavs_idct8_add(dst, block, stride);
  569. h->bdsp.clear_block(block);
  570. return 0;
  571. }
  572. static inline int decode_residual_chroma(AVSContext *h)
  573. {
  574. if (h->cbp & (1 << 4)) {
  575. int ret = decode_residual_block(h, &h->gb, chroma_dec, 0,
  576. ff_cavs_chroma_qp[h->qp], h->cu, h->c_stride);
  577. if (ret < 0)
  578. return ret;
  579. }
  580. if (h->cbp & (1 << 5)) {
  581. int ret = decode_residual_block(h, &h->gb, chroma_dec, 0,
  582. ff_cavs_chroma_qp[h->qp], h->cv, h->c_stride);
  583. if (ret < 0)
  584. return ret;
  585. }
  586. return 0;
  587. }
  588. static inline int decode_residual_inter(AVSContext *h)
  589. {
  590. int block;
  591. /* get coded block pattern */
  592. int cbp = get_ue_golomb(&h->gb);
  593. if (cbp > 63U) {
  594. av_log(h->avctx, AV_LOG_ERROR, "illegal inter cbp %d\n", cbp);
  595. return AVERROR_INVALIDDATA;
  596. }
  597. h->cbp = cbp_tab[cbp][1];
  598. /* get quantizer */
  599. if (h->cbp && !h->qp_fixed)
  600. h->qp = (h->qp + (unsigned)get_se_golomb(&h->gb)) & 63;
  601. for (block = 0; block < 4; block++)
  602. if (h->cbp & (1 << block))
  603. decode_residual_block(h, &h->gb, inter_dec, 0, h->qp,
  604. h->cy + h->luma_scan[block], h->l_stride);
  605. decode_residual_chroma(h);
  606. return 0;
  607. }
  608. /*****************************************************************************
  609. *
  610. * macroblock level
  611. *
  612. ****************************************************************************/
  613. static inline void set_mv_intra(AVSContext *h)
  614. {
  615. h->mv[MV_FWD_X0] = ff_cavs_intra_mv;
  616. set_mvs(&h->mv[MV_FWD_X0], BLK_16X16);
  617. h->mv[MV_BWD_X0] = ff_cavs_intra_mv;
  618. set_mvs(&h->mv[MV_BWD_X0], BLK_16X16);
  619. if (h->cur.f->pict_type != AV_PICTURE_TYPE_B)
  620. h->col_type_base[h->mbidx] = I_8X8;
  621. }
  622. static int decode_mb_i(AVSContext *h, int cbp_code)
  623. {
  624. GetBitContext *gb = &h->gb;
  625. unsigned pred_mode_uv;
  626. int block;
  627. uint8_t top[18];
  628. uint8_t *left = NULL;
  629. uint8_t *d;
  630. int ret;
  631. ff_cavs_init_mb(h);
  632. /* get intra prediction modes from stream */
  633. for (block = 0; block < 4; block++) {
  634. int nA, nB, predpred;
  635. int pos = scan3x3[block];
  636. nA = h->pred_mode_Y[pos - 1];
  637. nB = h->pred_mode_Y[pos - 3];
  638. predpred = FFMIN(nA, nB);
  639. if (predpred == NOT_AVAIL) // if either is not available
  640. predpred = INTRA_L_LP;
  641. if (!get_bits1(gb)) {
  642. int rem_mode = get_bits(gb, 2);
  643. predpred = rem_mode + (rem_mode >= predpred);
  644. }
  645. h->pred_mode_Y[pos] = predpred;
  646. }
  647. pred_mode_uv = get_ue_golomb_31(gb);
  648. if (pred_mode_uv > 6) {
  649. av_log(h->avctx, AV_LOG_ERROR, "illegal intra chroma pred mode\n");
  650. return AVERROR_INVALIDDATA;
  651. }
  652. ff_cavs_modify_mb_i(h, &pred_mode_uv);
  653. /* get coded block pattern */
  654. if (h->cur.f->pict_type == AV_PICTURE_TYPE_I)
  655. cbp_code = get_ue_golomb(gb);
  656. if (cbp_code > 63U) {
  657. av_log(h->avctx, AV_LOG_ERROR, "illegal intra cbp\n");
  658. return AVERROR_INVALIDDATA;
  659. }
  660. h->cbp = cbp_tab[cbp_code][0];
  661. if (h->cbp && !h->qp_fixed)
  662. h->qp = (h->qp + (unsigned)get_se_golomb(gb)) & 63; //qp_delta
  663. /* luma intra prediction interleaved with residual decode/transform/add */
  664. for (block = 0; block < 4; block++) {
  665. d = h->cy + h->luma_scan[block];
  666. ff_cavs_load_intra_pred_luma(h, top, &left, block);
  667. h->intra_pred_l[h->pred_mode_Y[scan3x3[block]]]
  668. (d, top, left, h->l_stride);
  669. if (h->cbp & (1<<block)) {
  670. ret = decode_residual_block(h, gb, intra_dec, 1, h->qp, d, h->l_stride);
  671. if (ret < 0)
  672. return ret;
  673. }
  674. }
  675. /* chroma intra prediction */
  676. ff_cavs_load_intra_pred_chroma(h);
  677. h->intra_pred_c[pred_mode_uv](h->cu, &h->top_border_u[h->mbx * 10],
  678. h->left_border_u, h->c_stride);
  679. h->intra_pred_c[pred_mode_uv](h->cv, &h->top_border_v[h->mbx * 10],
  680. h->left_border_v, h->c_stride);
  681. ret = decode_residual_chroma(h);
  682. if (ret < 0)
  683. return ret;
  684. ff_cavs_filter(h, I_8X8);
  685. set_mv_intra(h);
  686. return 0;
  687. }
  688. static inline void set_intra_mode_default(AVSContext *h)
  689. {
  690. if (h->stream_revision > 0) {
  691. h->pred_mode_Y[3] = h->pred_mode_Y[6] = NOT_AVAIL;
  692. h->top_pred_Y[h->mbx * 2 + 0] = h->top_pred_Y[h->mbx * 2 + 1] = NOT_AVAIL;
  693. } else {
  694. h->pred_mode_Y[3] = h->pred_mode_Y[6] = INTRA_L_LP;
  695. h->top_pred_Y[h->mbx * 2 + 0] = h->top_pred_Y[h->mbx * 2 + 1] = INTRA_L_LP;
  696. }
  697. }
  698. static void decode_mb_p(AVSContext *h, enum cavs_mb mb_type)
  699. {
  700. GetBitContext *gb = &h->gb;
  701. int ref[4];
  702. ff_cavs_init_mb(h);
  703. switch (mb_type) {
  704. case P_SKIP:
  705. ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_PSKIP, BLK_16X16, 0);
  706. break;
  707. case P_16X16:
  708. ref[0] = h->ref_flag ? 0 : get_bits1(gb);
  709. ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16, ref[0]);
  710. break;
  711. case P_16X8:
  712. ref[0] = h->ref_flag ? 0 : get_bits1(gb);
  713. ref[2] = h->ref_flag ? 0 : get_bits1(gb);
  714. ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_TOP, BLK_16X8, ref[0]);
  715. ff_cavs_mv(h, MV_FWD_X2, MV_FWD_A1, MV_PRED_LEFT, BLK_16X8, ref[2]);
  716. break;
  717. case P_8X16:
  718. ref[0] = h->ref_flag ? 0 : get_bits1(gb);
  719. ref[1] = h->ref_flag ? 0 : get_bits1(gb);
  720. ff_cavs_mv(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_LEFT, BLK_8X16, ref[0]);
  721. ff_cavs_mv(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_TOPRIGHT, BLK_8X16, ref[1]);
  722. break;
  723. case P_8X8:
  724. ref[0] = h->ref_flag ? 0 : get_bits1(gb);
  725. ref[1] = h->ref_flag ? 0 : get_bits1(gb);
  726. ref[2] = h->ref_flag ? 0 : get_bits1(gb);
  727. ref[3] = h->ref_flag ? 0 : get_bits1(gb);
  728. ff_cavs_mv(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_MEDIAN, BLK_8X8, ref[0]);
  729. ff_cavs_mv(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_MEDIAN, BLK_8X8, ref[1]);
  730. ff_cavs_mv(h, MV_FWD_X2, MV_FWD_X1, MV_PRED_MEDIAN, BLK_8X8, ref[2]);
  731. ff_cavs_mv(h, MV_FWD_X3, MV_FWD_X0, MV_PRED_MEDIAN, BLK_8X8, ref[3]);
  732. }
  733. ff_cavs_inter(h, mb_type);
  734. set_intra_mode_default(h);
  735. store_mvs(h);
  736. if (mb_type != P_SKIP)
  737. decode_residual_inter(h);
  738. ff_cavs_filter(h, mb_type);
  739. h->col_type_base[h->mbidx] = mb_type;
  740. }
  741. static int decode_mb_b(AVSContext *h, enum cavs_mb mb_type)
  742. {
  743. int block;
  744. enum cavs_sub_mb sub_type[4];
  745. int flags;
  746. ff_cavs_init_mb(h);
  747. /* reset all MVs */
  748. h->mv[MV_FWD_X0] = ff_cavs_dir_mv;
  749. set_mvs(&h->mv[MV_FWD_X0], BLK_16X16);
  750. h->mv[MV_BWD_X0] = ff_cavs_dir_mv;
  751. set_mvs(&h->mv[MV_BWD_X0], BLK_16X16);
  752. switch (mb_type) {
  753. case B_SKIP:
  754. case B_DIRECT:
  755. if (!h->col_type_base[h->mbidx]) {
  756. /* intra MB at co-location, do in-plane prediction */
  757. ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_BSKIP, BLK_16X16, 1);
  758. ff_cavs_mv(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_BSKIP, BLK_16X16, 0);
  759. } else
  760. /* direct prediction from co-located P MB, block-wise */
  761. for (block = 0; block < 4; block++)
  762. mv_pred_direct(h, &h->mv[mv_scan[block]],
  763. &h->col_mv[h->mbidx * 4 + block]);
  764. break;
  765. case B_FWD_16X16:
  766. ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16, 1);
  767. break;
  768. case B_SYM_16X16:
  769. ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16, 1);
  770. mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_16X16);
  771. break;
  772. case B_BWD_16X16:
  773. ff_cavs_mv(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_MEDIAN, BLK_16X16, 0);
  774. break;
  775. case B_8X8:
  776. #define TMP_UNUSED_INX 7
  777. flags = 0;
  778. for (block = 0; block < 4; block++)
  779. sub_type[block] = get_bits(&h->gb, 2);
  780. for (block = 0; block < 4; block++) {
  781. switch (sub_type[block]) {
  782. case B_SUB_DIRECT:
  783. if (!h->col_type_base[h->mbidx]) {
  784. /* intra MB at co-location, do in-plane prediction */
  785. if(flags==0) {
  786. // if col-MB is a Intra MB, current Block size is 16x16.
  787. // AVS standard section 9.9.1
  788. if(block>0){
  789. h->mv[TMP_UNUSED_INX ] = h->mv[MV_FWD_X0 ];
  790. h->mv[TMP_UNUSED_INX + MV_BWD_OFFS] = h->mv[MV_FWD_X0 + MV_BWD_OFFS];
  791. }
  792. ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2,
  793. MV_PRED_BSKIP, BLK_8X8, 1);
  794. ff_cavs_mv(h, MV_FWD_X0+MV_BWD_OFFS,
  795. MV_FWD_C2+MV_BWD_OFFS,
  796. MV_PRED_BSKIP, BLK_8X8, 0);
  797. if(block>0) {
  798. flags = mv_scan[block];
  799. h->mv[flags ] = h->mv[MV_FWD_X0 ];
  800. h->mv[flags + MV_BWD_OFFS] = h->mv[MV_FWD_X0 + MV_BWD_OFFS];
  801. h->mv[MV_FWD_X0 ] = h->mv[TMP_UNUSED_INX ];
  802. h->mv[MV_FWD_X0 + MV_BWD_OFFS] = h->mv[TMP_UNUSED_INX + MV_BWD_OFFS];
  803. } else
  804. flags = MV_FWD_X0;
  805. } else {
  806. h->mv[mv_scan[block] ] = h->mv[flags ];
  807. h->mv[mv_scan[block] + MV_BWD_OFFS] = h->mv[flags + MV_BWD_OFFS];
  808. }
  809. } else
  810. mv_pred_direct(h, &h->mv[mv_scan[block]],
  811. &h->col_mv[h->mbidx * 4 + block]);
  812. break;
  813. case B_SUB_FWD:
  814. ff_cavs_mv(h, mv_scan[block], mv_scan[block] - 3,
  815. MV_PRED_MEDIAN, BLK_8X8, 1);
  816. break;
  817. case B_SUB_SYM:
  818. ff_cavs_mv(h, mv_scan[block], mv_scan[block] - 3,
  819. MV_PRED_MEDIAN, BLK_8X8, 1);
  820. mv_pred_sym(h, &h->mv[mv_scan[block]], BLK_8X8);
  821. break;
  822. }
  823. }
  824. #undef TMP_UNUSED_INX
  825. for (block = 0; block < 4; block++) {
  826. if (sub_type[block] == B_SUB_BWD)
  827. ff_cavs_mv(h, mv_scan[block] + MV_BWD_OFFS,
  828. mv_scan[block] + MV_BWD_OFFS - 3,
  829. MV_PRED_MEDIAN, BLK_8X8, 0);
  830. }
  831. break;
  832. default:
  833. if (mb_type <= B_SYM_16X16) {
  834. av_log(h->avctx, AV_LOG_ERROR, "Invalid mb_type %d in B frame\n", mb_type);
  835. return AVERROR_INVALIDDATA;
  836. }
  837. av_assert2(mb_type < B_8X8);
  838. flags = ff_cavs_partition_flags[mb_type];
  839. if (mb_type & 1) { /* 16x8 macroblock types */
  840. if (flags & FWD0)
  841. ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_TOP, BLK_16X8, 1);
  842. if (flags & SYM0)
  843. mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_16X8);
  844. if (flags & FWD1)
  845. ff_cavs_mv(h, MV_FWD_X2, MV_FWD_A1, MV_PRED_LEFT, BLK_16X8, 1);
  846. if (flags & SYM1)
  847. mv_pred_sym(h, &h->mv[MV_FWD_X2], BLK_16X8);
  848. if (flags & BWD0)
  849. ff_cavs_mv(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_TOP, BLK_16X8, 0);
  850. if (flags & BWD1)
  851. ff_cavs_mv(h, MV_BWD_X2, MV_BWD_A1, MV_PRED_LEFT, BLK_16X8, 0);
  852. } else { /* 8x16 macroblock types */
  853. if (flags & FWD0)
  854. ff_cavs_mv(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_LEFT, BLK_8X16, 1);
  855. if (flags & SYM0)
  856. mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_8X16);
  857. if (flags & FWD1)
  858. ff_cavs_mv(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_TOPRIGHT, BLK_8X16, 1);
  859. if (flags & SYM1)
  860. mv_pred_sym(h, &h->mv[MV_FWD_X1], BLK_8X16);
  861. if (flags & BWD0)
  862. ff_cavs_mv(h, MV_BWD_X0, MV_BWD_B3, MV_PRED_LEFT, BLK_8X16, 0);
  863. if (flags & BWD1)
  864. ff_cavs_mv(h, MV_BWD_X1, MV_BWD_C2, MV_PRED_TOPRIGHT, BLK_8X16, 0);
  865. }
  866. }
  867. ff_cavs_inter(h, mb_type);
  868. set_intra_mode_default(h);
  869. if (mb_type != B_SKIP)
  870. decode_residual_inter(h);
  871. ff_cavs_filter(h, mb_type);
  872. return 0;
  873. }
  874. /*****************************************************************************
  875. *
  876. * slice level
  877. *
  878. ****************************************************************************/
  879. static inline int decode_slice_header(AVSContext *h, GetBitContext *gb)
  880. {
  881. if (h->stc > 0xAF)
  882. av_log(h->avctx, AV_LOG_ERROR, "unexpected start code 0x%02x\n", h->stc);
  883. if (h->stc >= h->mb_height) {
  884. av_log(h->avctx, AV_LOG_ERROR, "stc 0x%02x is too large\n", h->stc);
  885. return AVERROR_INVALIDDATA;
  886. }
  887. h->mby = h->stc;
  888. h->mbidx = h->mby * h->mb_width;
  889. /* mark top macroblocks as unavailable */
  890. h->flags &= ~(B_AVAIL | C_AVAIL);
  891. if (!h->pic_qp_fixed) {
  892. h->qp_fixed = get_bits1(gb);
  893. h->qp = get_bits(gb, 6);
  894. }
  895. /* inter frame or second slice can have weighting params */
  896. if ((h->cur.f->pict_type != AV_PICTURE_TYPE_I) ||
  897. (!h->pic_structure && h->mby >= h->mb_width / 2))
  898. if (get_bits1(gb)) { //slice_weighting_flag
  899. av_log(h->avctx, AV_LOG_ERROR,
  900. "weighted prediction not yet supported\n");
  901. }
  902. return 0;
  903. }
  904. static inline int check_for_slice(AVSContext *h)
  905. {
  906. GetBitContext *gb = &h->gb;
  907. int align;
  908. if (h->mbx)
  909. return 0;
  910. align = (-get_bits_count(gb)) & 7;
  911. /* check for stuffing byte */
  912. if (!align && (show_bits(gb, 8) == 0x80))
  913. align = 8;
  914. if ((show_bits_long(gb, 24 + align) & 0xFFFFFF) == 0x000001) {
  915. skip_bits_long(gb, 24 + align);
  916. h->stc = get_bits(gb, 8);
  917. if (h->stc >= h->mb_height)
  918. return 0;
  919. decode_slice_header(h, gb);
  920. return 1;
  921. }
  922. return 0;
  923. }
  924. /*****************************************************************************
  925. *
  926. * frame level
  927. *
  928. ****************************************************************************/
  929. static int decode_pic(AVSContext *h)
  930. {
  931. int ret;
  932. int skip_count = -1;
  933. enum cavs_mb mb_type;
  934. if (!h->top_qp) {
  935. av_log(h->avctx, AV_LOG_ERROR, "No sequence header decoded yet\n");
  936. return AVERROR_INVALIDDATA;
  937. }
  938. av_frame_unref(h->cur.f);
  939. skip_bits(&h->gb, 16);//bbv_dwlay
  940. if (h->stc == PIC_PB_START_CODE) {
  941. h->cur.f->pict_type = get_bits(&h->gb, 2) + AV_PICTURE_TYPE_I;
  942. if (h->cur.f->pict_type > AV_PICTURE_TYPE_B) {
  943. av_log(h->avctx, AV_LOG_ERROR, "illegal picture type\n");
  944. return AVERROR_INVALIDDATA;
  945. }
  946. /* make sure we have the reference frames we need */
  947. if (!h->DPB[0].f->data[0] ||
  948. (!h->DPB[1].f->data[0] && h->cur.f->pict_type == AV_PICTURE_TYPE_B))
  949. return AVERROR_INVALIDDATA;
  950. } else {
  951. h->cur.f->pict_type = AV_PICTURE_TYPE_I;
  952. if (get_bits1(&h->gb))
  953. skip_bits(&h->gb, 24);//time_code
  954. /* old sample clips were all progressive and no low_delay,
  955. bump stream revision if detected otherwise */
  956. if (h->low_delay || !(show_bits(&h->gb, 9) & 1))
  957. h->stream_revision = 1;
  958. /* similarly test top_field_first and repeat_first_field */
  959. else if (show_bits(&h->gb, 11) & 3)
  960. h->stream_revision = 1;
  961. if (h->stream_revision > 0)
  962. skip_bits(&h->gb, 1); //marker_bit
  963. }
  964. if (get_bits_left(&h->gb) < 23)
  965. return AVERROR_INVALIDDATA;
  966. ret = ff_get_buffer(h->avctx, h->cur.f, h->cur.f->pict_type == AV_PICTURE_TYPE_B ?
  967. 0 : AV_GET_BUFFER_FLAG_REF);
  968. if (ret < 0)
  969. return ret;
  970. if (!h->edge_emu_buffer) {
  971. int alloc_size = FFALIGN(FFABS(h->cur.f->linesize[0]) + 32, 32);
  972. h->edge_emu_buffer = av_mallocz(alloc_size * 2 * 24);
  973. if (!h->edge_emu_buffer)
  974. return AVERROR(ENOMEM);
  975. }
  976. if ((ret = ff_cavs_init_pic(h)) < 0)
  977. return ret;
  978. h->cur.poc = get_bits(&h->gb, 8) * 2;
  979. /* get temporal distances and MV scaling factors */
  980. if (h->cur.f->pict_type != AV_PICTURE_TYPE_B) {
  981. h->dist[0] = (h->cur.poc - h->DPB[0].poc) & 511;
  982. } else {
  983. h->dist[0] = (h->DPB[0].poc - h->cur.poc) & 511;
  984. }
  985. h->dist[1] = (h->cur.poc - h->DPB[1].poc) & 511;
  986. h->scale_den[0] = h->dist[0] ? 512/h->dist[0] : 0;
  987. h->scale_den[1] = h->dist[1] ? 512/h->dist[1] : 0;
  988. if (h->cur.f->pict_type == AV_PICTURE_TYPE_B) {
  989. h->sym_factor = h->dist[0] * h->scale_den[1];
  990. if (FFABS(h->sym_factor) > 32768) {
  991. av_log(h->avctx, AV_LOG_ERROR, "sym_factor %d too large\n", h->sym_factor);
  992. return AVERROR_INVALIDDATA;
  993. }
  994. } else {
  995. h->direct_den[0] = h->dist[0] ? 16384 / h->dist[0] : 0;
  996. h->direct_den[1] = h->dist[1] ? 16384 / h->dist[1] : 0;
  997. }
  998. if (h->low_delay)
  999. get_ue_golomb(&h->gb); //bbv_check_times
  1000. h->progressive = get_bits1(&h->gb);
  1001. h->pic_structure = 1;
  1002. if (!h->progressive)
  1003. h->pic_structure = get_bits1(&h->gb);
  1004. if (!h->pic_structure && h->stc == PIC_PB_START_CODE)
  1005. skip_bits1(&h->gb); //advanced_pred_mode_disable
  1006. skip_bits1(&h->gb); //top_field_first
  1007. skip_bits1(&h->gb); //repeat_first_field
  1008. h->pic_qp_fixed =
  1009. h->qp_fixed = get_bits1(&h->gb);
  1010. h->qp = get_bits(&h->gb, 6);
  1011. if (h->cur.f->pict_type == AV_PICTURE_TYPE_I) {
  1012. if (!h->progressive && !h->pic_structure)
  1013. skip_bits1(&h->gb);//what is this?
  1014. skip_bits(&h->gb, 4); //reserved bits
  1015. } else {
  1016. if (!(h->cur.f->pict_type == AV_PICTURE_TYPE_B && h->pic_structure == 1))
  1017. h->ref_flag = get_bits1(&h->gb);
  1018. skip_bits(&h->gb, 4); //reserved bits
  1019. h->skip_mode_flag = get_bits1(&h->gb);
  1020. }
  1021. h->loop_filter_disable = get_bits1(&h->gb);
  1022. if (!h->loop_filter_disable && get_bits1(&h->gb)) {
  1023. h->alpha_offset = get_se_golomb(&h->gb);
  1024. h->beta_offset = get_se_golomb(&h->gb);
  1025. if ( h->alpha_offset < -64 || h->alpha_offset > 64
  1026. || h-> beta_offset < -64 || h-> beta_offset > 64) {
  1027. h->alpha_offset = h->beta_offset = 0;
  1028. return AVERROR_INVALIDDATA;
  1029. }
  1030. } else {
  1031. h->alpha_offset = h->beta_offset = 0;
  1032. }
  1033. ret = 0;
  1034. if (h->cur.f->pict_type == AV_PICTURE_TYPE_I) {
  1035. do {
  1036. check_for_slice(h);
  1037. ret = decode_mb_i(h, 0);
  1038. if (ret < 0)
  1039. break;
  1040. } while (ff_cavs_next_mb(h));
  1041. } else if (h->cur.f->pict_type == AV_PICTURE_TYPE_P) {
  1042. do {
  1043. if (check_for_slice(h))
  1044. skip_count = -1;
  1045. if (h->skip_mode_flag && (skip_count < 0)) {
  1046. if (get_bits_left(&h->gb) < 1) {
  1047. ret = AVERROR_INVALIDDATA;
  1048. break;
  1049. }
  1050. skip_count = get_ue_golomb(&h->gb);
  1051. }
  1052. if (h->skip_mode_flag && skip_count--) {
  1053. decode_mb_p(h, P_SKIP);
  1054. } else {
  1055. if (get_bits_left(&h->gb) < 1) {
  1056. ret = AVERROR_INVALIDDATA;
  1057. break;
  1058. }
  1059. mb_type = get_ue_golomb(&h->gb) + P_SKIP + h->skip_mode_flag;
  1060. if (mb_type > P_8X8)
  1061. ret = decode_mb_i(h, mb_type - P_8X8 - 1);
  1062. else
  1063. decode_mb_p(h, mb_type);
  1064. }
  1065. if (ret < 0)
  1066. break;
  1067. } while (ff_cavs_next_mb(h));
  1068. } else { /* AV_PICTURE_TYPE_B */
  1069. do {
  1070. if (check_for_slice(h))
  1071. skip_count = -1;
  1072. if (h->skip_mode_flag && (skip_count < 0)) {
  1073. if (get_bits_left(&h->gb) < 1) {
  1074. ret = AVERROR_INVALIDDATA;
  1075. break;
  1076. }
  1077. skip_count = get_ue_golomb(&h->gb);
  1078. }
  1079. if (h->skip_mode_flag && skip_count--) {
  1080. ret = decode_mb_b(h, B_SKIP);
  1081. } else {
  1082. if (get_bits_left(&h->gb) < 1) {
  1083. ret = AVERROR_INVALIDDATA;
  1084. break;
  1085. }
  1086. mb_type = get_ue_golomb(&h->gb) + B_SKIP + h->skip_mode_flag;
  1087. if (mb_type > B_8X8)
  1088. ret = decode_mb_i(h, mb_type - B_8X8 - 1);
  1089. else
  1090. ret = decode_mb_b(h, mb_type);
  1091. }
  1092. if (ret < 0)
  1093. break;
  1094. } while (ff_cavs_next_mb(h));
  1095. }
  1096. emms_c();
  1097. if (ret >= 0 && h->cur.f->pict_type != AV_PICTURE_TYPE_B) {
  1098. av_frame_unref(h->DPB[1].f);
  1099. FFSWAP(AVSFrame, h->cur, h->DPB[1]);
  1100. FFSWAP(AVSFrame, h->DPB[0], h->DPB[1]);
  1101. }
  1102. return ret;
  1103. }
  1104. /*****************************************************************************
  1105. *
  1106. * headers and interface
  1107. *
  1108. ****************************************************************************/
  1109. static int decode_seq_header(AVSContext *h)
  1110. {
  1111. int frame_rate_code;
  1112. int width, height;
  1113. int ret;
  1114. h->profile = get_bits(&h->gb, 8);
  1115. if (h->profile != 0x20) {
  1116. avpriv_report_missing_feature(h->avctx,
  1117. "only support JiZhun profile");
  1118. return AVERROR_PATCHWELCOME;
  1119. }
  1120. h->level = get_bits(&h->gb, 8);
  1121. skip_bits1(&h->gb); //progressive sequence
  1122. width = get_bits(&h->gb, 14);
  1123. height = get_bits(&h->gb, 14);
  1124. if ((h->width || h->height) && (h->width != width || h->height != height)) {
  1125. avpriv_report_missing_feature(h->avctx,
  1126. "Width/height changing in CAVS");
  1127. return AVERROR_PATCHWELCOME;
  1128. }
  1129. if (width <= 0 || height <= 0) {
  1130. av_log(h->avctx, AV_LOG_ERROR, "Dimensions invalid\n");
  1131. return AVERROR_INVALIDDATA;
  1132. }
  1133. skip_bits(&h->gb, 2); //chroma format
  1134. skip_bits(&h->gb, 3); //sample_precision
  1135. h->aspect_ratio = get_bits(&h->gb, 4);
  1136. frame_rate_code = get_bits(&h->gb, 4);
  1137. if (frame_rate_code == 0 || frame_rate_code > 13) {
  1138. av_log(h->avctx, AV_LOG_WARNING,
  1139. "frame_rate_code %d is invalid\n", frame_rate_code);
  1140. frame_rate_code = 1;
  1141. }
  1142. skip_bits(&h->gb, 18); //bit_rate_lower
  1143. skip_bits1(&h->gb); //marker_bit
  1144. skip_bits(&h->gb, 12); //bit_rate_upper
  1145. h->low_delay = get_bits1(&h->gb);
  1146. ret = ff_set_dimensions(h->avctx, width, height);
  1147. if (ret < 0)
  1148. return ret;
  1149. h->width = width;
  1150. h->height = height;
  1151. h->mb_width = (h->width + 15) >> 4;
  1152. h->mb_height = (h->height + 15) >> 4;
  1153. h->avctx->framerate = ff_mpeg12_frame_rate_tab[frame_rate_code];
  1154. if (!h->top_qp)
  1155. return ff_cavs_init_top_lines(h);
  1156. return 0;
  1157. }
  1158. static void cavs_flush(AVCodecContext * avctx)
  1159. {
  1160. AVSContext *h = avctx->priv_data;
  1161. h->got_keyframe = 0;
  1162. }
  1163. static int cavs_decode_frame(AVCodecContext *avctx, AVFrame *rframe,
  1164. int *got_frame, AVPacket *avpkt)
  1165. {
  1166. AVSContext *h = avctx->priv_data;
  1167. const uint8_t *buf = avpkt->data;
  1168. int buf_size = avpkt->size;
  1169. uint32_t stc = -1;
  1170. int input_size, ret;
  1171. const uint8_t *buf_end;
  1172. const uint8_t *buf_ptr;
  1173. int frame_start = 0;
  1174. if (buf_size == 0) {
  1175. if (!h->low_delay && h->DPB[0].f->data[0]) {
  1176. *got_frame = 1;
  1177. av_frame_move_ref(rframe, h->DPB[0].f);
  1178. }
  1179. return 0;
  1180. }
  1181. h->stc = 0;
  1182. buf_ptr = buf;
  1183. buf_end = buf + buf_size;
  1184. for(;;) {
  1185. buf_ptr = avpriv_find_start_code(buf_ptr, buf_end, &stc);
  1186. if ((stc & 0xFFFFFE00) || buf_ptr == buf_end) {
  1187. if (!h->stc)
  1188. av_log(h->avctx, AV_LOG_WARNING, "no frame decoded\n");
  1189. return FFMAX(0, buf_ptr - buf);
  1190. }
  1191. input_size = (buf_end - buf_ptr) * 8;
  1192. switch (stc) {
  1193. case CAVS_START_CODE:
  1194. init_get_bits(&h->gb, buf_ptr, input_size);
  1195. decode_seq_header(h);
  1196. break;
  1197. case PIC_I_START_CODE:
  1198. if (!h->got_keyframe) {
  1199. av_frame_unref(h->DPB[0].f);
  1200. av_frame_unref(h->DPB[1].f);
  1201. h->got_keyframe = 1;
  1202. }
  1203. case PIC_PB_START_CODE:
  1204. if (frame_start > 1)
  1205. return AVERROR_INVALIDDATA;
  1206. frame_start ++;
  1207. if (*got_frame)
  1208. av_frame_unref(rframe);
  1209. *got_frame = 0;
  1210. if (!h->got_keyframe)
  1211. break;
  1212. init_get_bits(&h->gb, buf_ptr, input_size);
  1213. h->stc = stc;
  1214. if (decode_pic(h))
  1215. break;
  1216. *got_frame = 1;
  1217. if (h->cur.f->pict_type != AV_PICTURE_TYPE_B) {
  1218. if (h->DPB[!h->low_delay].f->data[0]) {
  1219. if ((ret = av_frame_ref(rframe, h->DPB[!h->low_delay].f)) < 0)
  1220. return ret;
  1221. } else {
  1222. *got_frame = 0;
  1223. }
  1224. } else {
  1225. av_frame_move_ref(rframe, h->cur.f);
  1226. }
  1227. break;
  1228. case EXT_START_CODE:
  1229. //mpeg_decode_extension(avctx, buf_ptr, input_size);
  1230. break;
  1231. case USER_START_CODE:
  1232. //mpeg_decode_user_data(avctx, buf_ptr, input_size);
  1233. break;
  1234. default:
  1235. if (stc <= SLICE_MAX_START_CODE) {
  1236. init_get_bits(&h->gb, buf_ptr, input_size);
  1237. decode_slice_header(h, &h->gb);
  1238. }
  1239. break;
  1240. }
  1241. }
  1242. }
  1243. const FFCodec ff_cavs_decoder = {
  1244. .p.name = "cavs",
  1245. CODEC_LONG_NAME("Chinese AVS (Audio Video Standard) (AVS1-P2, JiZhun profile)"),
  1246. .p.type = AVMEDIA_TYPE_VIDEO,
  1247. .p.id = AV_CODEC_ID_CAVS,
  1248. .priv_data_size = sizeof(AVSContext),
  1249. .init = ff_cavs_init,
  1250. .close = ff_cavs_end,
  1251. FF_CODEC_DECODE_CB(cavs_decode_frame),
  1252. .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY,
  1253. .flush = cavs_flush,
  1254. .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
  1255. };