You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1187 lines
47KB

  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 Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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 "avcodec.h"
  27. #include "get_bits.h"
  28. #include "golomb.h"
  29. #include "cavs.h"
  30. static const uint8_t mv_scan[4] = {
  31. MV_FWD_X0,MV_FWD_X1,
  32. MV_FWD_X2,MV_FWD_X3
  33. };
  34. static const uint8_t cbp_tab[64][2] = {
  35. {63, 0},{15,15},{31,63},{47,31},{ 0,16},{14,32},{13,47},{11,13},
  36. { 7,14},{ 5,11},{10,12},{ 8, 5},{12,10},{61, 7},{ 4,48},{55, 3},
  37. { 1, 2},{ 2, 8},{59, 4},{ 3, 1},{62,61},{ 9,55},{ 6,59},{29,62},
  38. {45,29},{51,27},{23,23},{39,19},{27,30},{46,28},{53, 9},{30, 6},
  39. {43,60},{37,21},{60,44},{16,26},{21,51},{28,35},{19,18},{35,20},
  40. {42,24},{26,53},{44,17},{32,37},{58,39},{24,45},{20,58},{17,43},
  41. {18,42},{48,46},{22,36},{33,33},{25,34},{49,40},{40,52},{36,49},
  42. {34,50},{50,56},{52,25},{54,22},{41,54},{56,57},{38,41},{57,38}
  43. };
  44. static const uint8_t scan3x3[4] = { 4, 5, 7, 8 };
  45. static const uint8_t cavs_chroma_qp[64] = {
  46. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,
  47. 16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,
  48. 32,33,34,35,36,37,38,39,40,41,42,42,43,43,44,44,
  49. 45,45,46,46,47,47,48,48,48,49,49,49,50,50,50,51
  50. };
  51. static const uint8_t dequant_shift[64] = {
  52. 14,14,14,14,14,14,14,14,
  53. 13,13,13,13,13,13,13,13,
  54. 13,12,12,12,12,12,12,12,
  55. 11,11,11,11,11,11,11,11,
  56. 11,10,10,10,10,10,10,10,
  57. 10, 9, 9, 9, 9, 9, 9, 9,
  58. 9, 8, 8, 8, 8, 8, 8, 8,
  59. 7, 7, 7, 7, 7, 7, 7, 7
  60. };
  61. static const uint16_t dequant_mul[64] = {
  62. 32768,36061,38968,42495,46341,50535,55437,60424,
  63. 32932,35734,38968,42495,46177,50535,55109,59933,
  64. 65535,35734,38968,42577,46341,50617,55027,60097,
  65. 32809,35734,38968,42454,46382,50576,55109,60056,
  66. 65535,35734,38968,42495,46320,50515,55109,60076,
  67. 65535,35744,38968,42495,46341,50535,55099,60087,
  68. 65535,35734,38973,42500,46341,50535,55109,60097,
  69. 32771,35734,38965,42497,46341,50535,55109,60099
  70. };
  71. #define EOB 0,0,0
  72. static const struct dec_2dvlc intra_dec[7] = {
  73. {
  74. { //level / run / table_inc
  75. { 1, 1, 1},{ -1, 1, 1},{ 1, 2, 1},{ -1, 2, 1},{ 1, 3, 1},{ -1, 3, 1},
  76. { 1, 4, 1},{ -1, 4, 1},{ 1, 5, 1},{ -1, 5, 1},{ 1, 6, 1},{ -1, 6, 1},
  77. { 1, 7, 1},{ -1, 7, 1},{ 1, 8, 1},{ -1, 8, 1},{ 1, 9, 1},{ -1, 9, 1},
  78. { 1,10, 1},{ -1,10, 1},{ 1,11, 1},{ -1,11, 1},{ 2, 1, 2},{ -2, 1, 2},
  79. { 1,12, 1},{ -1,12, 1},{ 1,13, 1},{ -1,13, 1},{ 1,14, 1},{ -1,14, 1},
  80. { 1,15, 1},{ -1,15, 1},{ 2, 2, 2},{ -2, 2, 2},{ 1,16, 1},{ -1,16, 1},
  81. { 1,17, 1},{ -1,17, 1},{ 3, 1, 3},{ -3, 1, 3},{ 1,18, 1},{ -1,18, 1},
  82. { 1,19, 1},{ -1,19, 1},{ 2, 3, 2},{ -2, 3, 2},{ 1,20, 1},{ -1,20, 1},
  83. { 1,21, 1},{ -1,21, 1},{ 2, 4, 2},{ -2, 4, 2},{ 1,22, 1},{ -1,22, 1},
  84. { 2, 5, 2},{ -2, 5, 2},{ 1,23, 1},{ -1,23, 1},{ EOB }
  85. },
  86. //level_add
  87. { 0, 4, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  88. 2, 2, 2, 2, 2, 2, 2,-1,-1,-1},
  89. 2, //golomb_order
  90. 0, //inc_limit
  91. 23, //max_run
  92. },{
  93. { //level / run
  94. { 1, 1, 0},{ -1, 1, 0},{ 1, 2, 0},{ -1, 2, 0},{ 2, 1, 1},{ -2, 1, 1},
  95. { 1, 3, 0},{ -1, 3, 0},{ EOB },{ 1, 4, 0},{ -1, 4, 0},{ 1, 5, 0},
  96. { -1, 5, 0},{ 1, 6, 0},{ -1, 6, 0},{ 3, 1, 2},{ -3, 1, 2},{ 2, 2, 1},
  97. { -2, 2, 1},{ 1, 7, 0},{ -1, 7, 0},{ 1, 8, 0},{ -1, 8, 0},{ 1, 9, 0},
  98. { -1, 9, 0},{ 2, 3, 1},{ -2, 3, 1},{ 4, 1, 2},{ -4, 1, 2},{ 1,10, 0},
  99. { -1,10, 0},{ 1,11, 0},{ -1,11, 0},{ 2, 4, 1},{ -2, 4, 1},{ 3, 2, 2},
  100. { -3, 2, 2},{ 1,12, 0},{ -1,12, 0},{ 2, 5, 1},{ -2, 5, 1},{ 5, 1, 3},
  101. { -5, 1, 3},{ 1,13, 0},{ -1,13, 0},{ 2, 6, 1},{ -2, 6, 1},{ 1,14, 0},
  102. { -1,14, 0},{ 2, 7, 1},{ -2, 7, 1},{ 2, 8, 1},{ -2, 8, 1},{ 3, 3, 2},
  103. { -3, 3, 2},{ 6, 1, 3},{ -6, 1, 3},{ 1,15, 0},{ -1,15, 0}
  104. },
  105. //level_add
  106. { 0, 7, 4, 4, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2,-1,
  107. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
  108. 2, //golomb_order
  109. 1, //inc_limit
  110. 15, //max_run
  111. },{
  112. { //level / run
  113. { 1, 1, 0},{ -1, 1, 0},{ 2, 1, 0},{ -2, 1, 0},{ 1, 2, 0},{ -1, 2, 0},
  114. { 3, 1, 1},{ -3, 1, 1},{ EOB },{ 1, 3, 0},{ -1, 3, 0},{ 2, 2, 0},
  115. { -2, 2, 0},{ 4, 1, 1},{ -4, 1, 1},{ 1, 4, 0},{ -1, 4, 0},{ 5, 1, 2},
  116. { -5, 1, 2},{ 1, 5, 0},{ -1, 5, 0},{ 3, 2, 1},{ -3, 2, 1},{ 2, 3, 0},
  117. { -2, 3, 0},{ 1, 6, 0},{ -1, 6, 0},{ 6, 1, 2},{ -6, 1, 2},{ 2, 4, 0},
  118. { -2, 4, 0},{ 1, 7, 0},{ -1, 7, 0},{ 4, 2, 1},{ -4, 2, 1},{ 7, 1, 2},
  119. { -7, 1, 2},{ 3, 3, 1},{ -3, 3, 1},{ 2, 5, 0},{ -2, 5, 0},{ 1, 8, 0},
  120. { -1, 8, 0},{ 2, 6, 0},{ -2, 6, 0},{ 8, 1, 3},{ -8, 1, 3},{ 1, 9, 0},
  121. { -1, 9, 0},{ 5, 2, 2},{ -5, 2, 2},{ 3, 4, 1},{ -3, 4, 1},{ 2, 7, 0},
  122. { -2, 7, 0},{ 9, 1, 3},{ -9, 1, 3},{ 1,10, 0},{ -1,10, 0}
  123. },
  124. //level_add
  125. { 0,10, 6, 4, 4, 3, 3, 3, 2, 2, 2,-1,-1,-1,-1,-1,-1,
  126. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
  127. 2, //golomb_order
  128. 2, //inc_limit
  129. 10, //max_run
  130. },{
  131. { //level / run
  132. { 1, 1, 0},{ -1, 1, 0},{ 2, 1, 0},{ -2, 1, 0},{ 3, 1, 0},{ -3, 1, 0},
  133. { 1, 2, 0},{ -1, 2, 0},{ EOB },{ 4, 1, 0},{ -4, 1, 0},{ 5, 1, 1},
  134. { -5, 1, 1},{ 2, 2, 0},{ -2, 2, 0},{ 1, 3, 0},{ -1, 3, 0},{ 6, 1, 1},
  135. { -6, 1, 1},{ 3, 2, 0},{ -3, 2, 0},{ 7, 1, 1},{ -7, 1, 1},{ 1, 4, 0},
  136. { -1, 4, 0},{ 8, 1, 2},{ -8, 1, 2},{ 2, 3, 0},{ -2, 3, 0},{ 4, 2, 0},
  137. { -4, 2, 0},{ 1, 5, 0},{ -1, 5, 0},{ 9, 1, 2},{ -9, 1, 2},{ 5, 2, 1},
  138. { -5, 2, 1},{ 2, 4, 0},{ -2, 4, 0},{ 10, 1, 2},{-10, 1, 2},{ 3, 3, 0},
  139. { -3, 3, 0},{ 1, 6, 0},{ -1, 6, 0},{ 11, 1, 3},{-11, 1, 3},{ 6, 2, 1},
  140. { -6, 2, 1},{ 1, 7, 0},{ -1, 7, 0},{ 2, 5, 0},{ -2, 5, 0},{ 3, 4, 0},
  141. { -3, 4, 0},{ 12, 1, 3},{-12, 1, 3},{ 4, 3, 0},{ -4, 3, 0}
  142. },
  143. //level_add
  144. { 0,13, 7, 5, 4, 3, 2, 2,-1,-1,-1 -1,-1,-1,-1,-1,-1,
  145. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
  146. 2, //golomb_order
  147. 4, //inc_limit
  148. 7, //max_run
  149. },{
  150. { //level / run
  151. { 1, 1, 0},{ -1, 1, 0},{ 2, 1, 0},{ -2, 1, 0},{ 3, 1, 0},{ -3, 1, 0},
  152. { EOB },{ 4, 1, 0},{ -4, 1, 0},{ 5, 1, 0},{ -5, 1, 0},{ 6, 1, 0},
  153. { -6, 1, 0},{ 1, 2, 0},{ -1, 2, 0},{ 7, 1, 0},{ -7, 1, 0},{ 8, 1, 1},
  154. { -8, 1, 1},{ 2, 2, 0},{ -2, 2, 0},{ 9, 1, 1},{ -9, 1, 1},{ 10, 1, 1},
  155. {-10, 1, 1},{ 1, 3, 0},{ -1, 3, 0},{ 3, 2, 0},{ -3, 2, 0},{ 11, 1, 2},
  156. {-11, 1, 2},{ 4, 2, 0},{ -4, 2, 0},{ 12, 1, 2},{-12, 1, 2},{ 13, 1, 2},
  157. {-13, 1, 2},{ 5, 2, 0},{ -5, 2, 0},{ 1, 4, 0},{ -1, 4, 0},{ 2, 3, 0},
  158. { -2, 3, 0},{ 14, 1, 2},{-14, 1, 2},{ 6, 2, 0},{ -6, 2, 0},{ 15, 1, 2},
  159. {-15, 1, 2},{ 16, 1, 2},{-16, 1, 2},{ 3, 3, 0},{ -3, 3, 0},{ 1, 5, 0},
  160. { -1, 5, 0},{ 7, 2, 0},{ -7, 2, 0},{ 17, 1, 2},{-17, 1, 2}
  161. },
  162. //level_add
  163. { 0,18, 8, 4, 2, 2,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  164. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
  165. 2, //golomb_order
  166. 7, //inc_limit
  167. 5, //max_run
  168. },{
  169. { //level / run
  170. { EOB },{ 1, 1, 0},{ -1, 1, 0},{ 2, 1, 0},{ -2, 1, 0},{ 3, 1, 0},
  171. { -3, 1, 0},{ 4, 1, 0},{ -4, 1, 0},{ 5, 1, 0},{ -5, 1, 0},{ 6, 1, 0},
  172. { -6, 1, 0},{ 7, 1, 0},{ -7, 1, 0},{ 8, 1, 0},{ -8, 1, 0},{ 9, 1, 0},
  173. { -9, 1, 0},{ 10, 1, 0},{-10, 1, 0},{ 1, 2, 0},{ -1, 2, 0},{ 11, 1, 1},
  174. {-11, 1, 1},{ 12, 1, 1},{-12, 1, 1},{ 13, 1, 1},{-13, 1, 1},{ 2, 2, 0},
  175. { -2, 2, 0},{ 14, 1, 1},{-14, 1, 1},{ 15, 1, 1},{-15, 1, 1},{ 3, 2, 0},
  176. { -3, 2, 0},{ 16, 1, 1},{-16, 1, 1},{ 1, 3, 0},{ -1, 3, 0},{ 17, 1, 1},
  177. {-17, 1, 1},{ 4, 2, 0},{ -4, 2, 0},{ 18, 1, 1},{-18, 1, 1},{ 5, 2, 0},
  178. { -5, 2, 0},{ 19, 1, 1},{-19, 1, 1},{ 20, 1, 1},{-20, 1, 1},{ 6, 2, 0},
  179. { -6, 2, 0},{ 21, 1, 1},{-21, 1, 1},{ 2, 3, 0},{ -2, 3, 0}
  180. },
  181. //level_add
  182. { 0,22, 7, 3,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  183. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
  184. 2, //golomb_order
  185. 10, //inc_limit
  186. 3, //max_run
  187. },{
  188. { //level / run
  189. { EOB },{ 1, 1, 0},{ -1, 1, 0},{ 2, 1, 0},{ -2, 1, 0},{ 3, 1, 0},
  190. { -3, 1, 0},{ 4, 1, 0},{ -4, 1, 0},{ 5, 1, 0},{ -5, 1, 0},{ 6, 1, 0},
  191. { -6, 1, 0},{ 7, 1, 0},{ -7, 1, 0},{ 8, 1, 0},{ -8, 1, 0},{ 9, 1, 0},
  192. { -9, 1, 0},{ 10, 1, 0},{-10, 1, 0},{ 11, 1, 0},{-11, 1, 0},{ 12, 1, 0},
  193. {-12, 1, 0},{ 13, 1, 0},{-13, 1, 0},{ 14, 1, 0},{-14, 1, 0},{ 15, 1, 0},
  194. {-15, 1, 0},{ 16, 1, 0},{-16, 1, 0},{ 1, 2, 0},{ -1, 2, 0},{ 17, 1, 0},
  195. {-17, 1, 0},{ 18, 1, 0},{-18, 1, 0},{ 19, 1, 0},{-19, 1, 0},{ 20, 1, 0},
  196. {-20, 1, 0},{ 21, 1, 0},{-21, 1, 0},{ 2, 2, 0},{ -2, 2, 0},{ 22, 1, 0},
  197. {-22, 1, 0},{ 23, 1, 0},{-23, 1, 0},{ 24, 1, 0},{-24, 1, 0},{ 25, 1, 0},
  198. {-25, 1, 0},{ 3, 2, 0},{ -3, 2, 0},{ 26, 1, 0},{-26, 1, 0}
  199. },
  200. //level_add
  201. { 0,27, 4,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  202. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
  203. 2, //golomb_order
  204. INT_MAX, //inc_limit
  205. 2, //max_run
  206. }
  207. };
  208. static const struct dec_2dvlc inter_dec[7] = {
  209. {
  210. { //level / run
  211. { 1, 1, 1},{ -1, 1, 1},{ 1, 2, 1},{ -1, 2, 1},{ 1, 3, 1},{ -1, 3, 1},
  212. { 1, 4, 1},{ -1, 4, 1},{ 1, 5, 1},{ -1, 5, 1},{ 1, 6, 1},{ -1, 6, 1},
  213. { 1, 7, 1},{ -1, 7, 1},{ 1, 8, 1},{ -1, 8, 1},{ 1, 9, 1},{ -1, 9, 1},
  214. { 1,10, 1},{ -1,10, 1},{ 1,11, 1},{ -1,11, 1},{ 1,12, 1},{ -1,12, 1},
  215. { 1,13, 1},{ -1,13, 1},{ 2, 1, 2},{ -2, 1, 2},{ 1,14, 1},{ -1,14, 1},
  216. { 1,15, 1},{ -1,15, 1},{ 1,16, 1},{ -1,16, 1},{ 1,17, 1},{ -1,17, 1},
  217. { 1,18, 1},{ -1,18, 1},{ 1,19, 1},{ -1,19, 1},{ 3, 1, 3},{ -3, 1, 3},
  218. { 1,20, 1},{ -1,20, 1},{ 1,21, 1},{ -1,21, 1},{ 2, 2, 2},{ -2, 2, 2},
  219. { 1,22, 1},{ -1,22, 1},{ 1,23, 1},{ -1,23, 1},{ 1,24, 1},{ -1,24, 1},
  220. { 1,25, 1},{ -1,25, 1},{ 1,26, 1},{ -1,26, 1},{ EOB }
  221. },
  222. //level_add
  223. { 0, 4, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  224. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2},
  225. 3, //golomb_order
  226. 0, //inc_limit
  227. 26 //max_run
  228. },{
  229. { //level / run
  230. { 1, 1, 0},{ -1, 1, 0},{ EOB },{ 1, 2, 0},{ -1, 2, 0},{ 1, 3, 0},
  231. { -1, 3, 0},{ 1, 4, 0},{ -1, 4, 0},{ 1, 5, 0},{ -1, 5, 0},{ 1, 6, 0},
  232. { -1, 6, 0},{ 2, 1, 1},{ -2, 1, 1},{ 1, 7, 0},{ -1, 7, 0},{ 1, 8, 0},
  233. { -1, 8, 0},{ 1, 9, 0},{ -1, 9, 0},{ 1,10, 0},{ -1,10, 0},{ 2, 2, 1},
  234. { -2, 2, 1},{ 1,11, 0},{ -1,11, 0},{ 1,12, 0},{ -1,12, 0},{ 3, 1, 2},
  235. { -3, 1, 2},{ 1,13, 0},{ -1,13, 0},{ 1,14, 0},{ -1,14, 0},{ 2, 3, 1},
  236. { -2, 3, 1},{ 1,15, 0},{ -1,15, 0},{ 2, 4, 1},{ -2, 4, 1},{ 1,16, 0},
  237. { -1,16, 0},{ 2, 5, 1},{ -2, 5, 1},{ 1,17, 0},{ -1,17, 0},{ 4, 1, 3},
  238. { -4, 1, 3},{ 2, 6, 1},{ -2, 6, 1},{ 1,18, 0},{ -1,18, 0},{ 1,19, 0},
  239. { -1,19, 0},{ 2, 7, 1},{ -2, 7, 1},{ 3, 2, 2},{ -3, 2, 2}
  240. },
  241. //level_add
  242. { 0, 5, 4, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  243. 2, 2, 2,-1,-1,-1,-1,-1,-1,-1},
  244. 2, //golomb_order
  245. 1, //inc_limit
  246. 19 //max_run
  247. },{
  248. { //level / run
  249. { 1, 1, 0},{ -1, 1, 0},{ EOB },{ 1, 2, 0},{ -1, 2, 0},{ 2, 1, 0},
  250. { -2, 1, 0},{ 1, 3, 0},{ -1, 3, 0},{ 1, 4, 0},{ -1, 4, 0},{ 3, 1, 1},
  251. { -3, 1, 1},{ 2, 2, 0},{ -2, 2, 0},{ 1, 5, 0},{ -1, 5, 0},{ 1, 6, 0},
  252. { -1, 6, 0},{ 1, 7, 0},{ -1, 7, 0},{ 2, 3, 0},{ -2, 3, 0},{ 4, 1, 2},
  253. { -4, 1, 2},{ 1, 8, 0},{ -1, 8, 0},{ 3, 2, 1},{ -3, 2, 1},{ 2, 4, 0},
  254. { -2, 4, 0},{ 1, 9, 0},{ -1, 9, 0},{ 1,10, 0},{ -1,10, 0},{ 5, 1, 2},
  255. { -5, 1, 2},{ 2, 5, 0},{ -2, 5, 0},{ 1,11, 0},{ -1,11, 0},{ 2, 6, 0},
  256. { -2, 6, 0},{ 1,12, 0},{ -1,12, 0},{ 3, 3, 1},{ -3, 3, 1},{ 6, 1, 2},
  257. { -6, 1, 2},{ 4, 2, 2},{ -4, 2, 2},{ 1,13, 0},{ -1,13, 0},{ 2, 7, 0},
  258. { -2, 7, 0},{ 3, 4, 1},{ -3, 4, 1},{ 1,14, 0},{ -1,14, 0}
  259. },
  260. //level_add
  261. { 0, 7, 5, 4, 4, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2,-1,-1,
  262. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
  263. 2, //golomb_order
  264. 2, //inc_limit
  265. 14 //max_run
  266. },{
  267. { //level / run
  268. { 1, 1, 0},{ -1, 1, 0},{ EOB },{ 2, 1, 0},{ -2, 1, 0},{ 1, 2, 0},
  269. { -1, 2, 0},{ 3, 1, 0},{ -3, 1, 0},{ 1, 3, 0},{ -1, 3, 0},{ 2, 2, 0},
  270. { -2, 2, 0},{ 4, 1, 1},{ -4, 1, 1},{ 1, 4, 0},{ -1, 4, 0},{ 5, 1, 1},
  271. { -5, 1, 1},{ 1, 5, 0},{ -1, 5, 0},{ 3, 2, 0},{ -3, 2, 0},{ 2, 3, 0},
  272. { -2, 3, 0},{ 1, 6, 0},{ -1, 6, 0},{ 6, 1, 1},{ -6, 1, 1},{ 2, 4, 0},
  273. { -2, 4, 0},{ 1, 7, 0},{ -1, 7, 0},{ 4, 2, 1},{ -4, 2, 1},{ 7, 1, 2},
  274. { -7, 1, 2},{ 3, 3, 0},{ -3, 3, 0},{ 1, 8, 0},{ -1, 8, 0},{ 2, 5, 0},
  275. { -2, 5, 0},{ 8, 1, 2},{ -8, 1, 2},{ 1, 9, 0},{ -1, 9, 0},{ 3, 4, 0},
  276. { -3, 4, 0},{ 2, 6, 0},{ -2, 6, 0},{ 5, 2, 1},{ -5, 2, 1},{ 1,10, 0},
  277. { -1,10, 0},{ 9, 1, 2},{ -9, 1, 2},{ 4, 3, 1},{ -4, 3, 1}
  278. },
  279. //level_add
  280. { 0,10, 6, 5, 4, 3, 3, 2, 2, 2, 2,-1,-1,-1,-1,-1,-1,
  281. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
  282. 2, //golomb_order
  283. 3, //inc_limit
  284. 10 //max_run
  285. },{
  286. { //level / run
  287. { 1, 1, 0},{ -1, 1, 0},{ EOB },{ 2, 1, 0},{ -2, 1, 0},{ 3, 1, 0},
  288. { -3, 1, 0},{ 1, 2, 0},{ -1, 2, 0},{ 4, 1, 0},{ -4, 1, 0},{ 5, 1, 0},
  289. { -5, 1, 0},{ 2, 2, 0},{ -2, 2, 0},{ 1, 3, 0},{ -1, 3, 0},{ 6, 1, 0},
  290. { -6, 1, 0},{ 3, 2, 0},{ -3, 2, 0},{ 7, 1, 1},{ -7, 1, 1},{ 1, 4, 0},
  291. { -1, 4, 0},{ 8, 1, 1},{ -8, 1, 1},{ 2, 3, 0},{ -2, 3, 0},{ 4, 2, 0},
  292. { -4, 2, 0},{ 1, 5, 0},{ -1, 5, 0},{ 9, 1, 1},{ -9, 1, 1},{ 5, 2, 0},
  293. { -5, 2, 0},{ 2, 4, 0},{ -2, 4, 0},{ 1, 6, 0},{ -1, 6, 0},{ 10, 1, 2},
  294. {-10, 1, 2},{ 3, 3, 0},{ -3, 3, 0},{ 11, 1, 2},{-11, 1, 2},{ 1, 7, 0},
  295. { -1, 7, 0},{ 6, 2, 0},{ -6, 2, 0},{ 3, 4, 0},{ -3, 4, 0},{ 2, 5, 0},
  296. { -2, 5, 0},{ 12, 1, 2},{-12, 1, 2},{ 4, 3, 0},{ -4, 3, 0}
  297. },
  298. //level_add
  299. { 0,13, 7, 5, 4, 3, 2, 2,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  300. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
  301. 2, //golomb_order
  302. 6, //inc_limit
  303. 7 //max_run
  304. },{
  305. { //level / run
  306. { EOB },{ 1, 1, 0},{ -1, 1, 0},{ 2, 1, 0},{ -2, 1, 0},{ 3, 1, 0},
  307. { -3, 1, 0},{ 4, 1, 0},{ -4, 1, 0},{ 5, 1, 0},{ -5, 1, 0},{ 1, 2, 0},
  308. { -1, 2, 0},{ 6, 1, 0},{ -6, 1, 0},{ 7, 1, 0},{ -7, 1, 0},{ 8, 1, 0},
  309. { -8, 1, 0},{ 2, 2, 0},{ -2, 2, 0},{ 9, 1, 0},{ -9, 1, 0},{ 1, 3, 0},
  310. { -1, 3, 0},{ 10, 1, 1},{-10, 1, 1},{ 3, 2, 0},{ -3, 2, 0},{ 11, 1, 1},
  311. {-11, 1, 1},{ 4, 2, 0},{ -4, 2, 0},{ 12, 1, 1},{-12, 1, 1},{ 1, 4, 0},
  312. { -1, 4, 0},{ 2, 3, 0},{ -2, 3, 0},{ 13, 1, 1},{-13, 1, 1},{ 5, 2, 0},
  313. { -5, 2, 0},{ 14, 1, 1},{-14, 1, 1},{ 6, 2, 0},{ -6, 2, 0},{ 1, 5, 0},
  314. { -1, 5, 0},{ 15, 1, 1},{-15, 1, 1},{ 3, 3, 0},{ -3, 3, 0},{ 16, 1, 1},
  315. {-16, 1, 1},{ 2, 4, 0},{ -2, 4, 0},{ 7, 2, 0},{ -7, 2, 0}
  316. },
  317. //level_add
  318. { 0,17, 8, 4, 3, 2,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  319. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
  320. 2, //golomb_order
  321. 9, //inc_limit
  322. 5 //max_run
  323. },{
  324. { //level / run
  325. { EOB },{ 1, 1, 0},{ -1, 1, 0},{ 2, 1, 0},{ -2, 1, 0},{ 3, 1, 0},
  326. { -3, 1, 0},{ 4, 1, 0},{ -4, 1, 0},{ 5, 1, 0},{ -5, 1, 0},{ 6, 1, 0},
  327. { -6, 1, 0},{ 7, 1, 0},{ -7, 1, 0},{ 1, 2, 0},{ -1, 2, 0},{ 8, 1, 0},
  328. { -8, 1, 0},{ 9, 1, 0},{ -9, 1, 0},{ 10, 1, 0},{-10, 1, 0},{ 11, 1, 0},
  329. {-11, 1, 0},{ 12, 1, 0},{-12, 1, 0},{ 2, 2, 0},{ -2, 2, 0},{ 13, 1, 0},
  330. {-13, 1, 0},{ 1, 3, 0},{ -1, 3, 0},{ 14, 1, 0},{-14, 1, 0},{ 15, 1, 0},
  331. {-15, 1, 0},{ 3, 2, 0},{ -3, 2, 0},{ 16, 1, 0},{-16, 1, 0},{ 17, 1, 0},
  332. {-17, 1, 0},{ 18, 1, 0},{-18, 1, 0},{ 4, 2, 0},{ -4, 2, 0},{ 19, 1, 0},
  333. {-19, 1, 0},{ 20, 1, 0},{-20, 1, 0},{ 2, 3, 0},{ -2, 3, 0},{ 1, 4, 0},
  334. { -1, 4, 0},{ 5, 2, 0},{ -5, 2, 0},{ 21, 1, 0},{-21, 1, 0}
  335. },
  336. //level_add
  337. { 0,22, 6, 3, 2,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  338. -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,
  360. 2, 2, 2, 2, 2, 2, 2, 2, 2,-1},
  361. 2, //golomb_order
  362. 0, //inc_limit
  363. 25 //max_run
  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,
  379. 2, 2, 2, 2,-1,-1,-1,-1,-1,-1},
  380. 0, //golomb_order
  381. 1, //inc_limit
  382. 20 //max_run
  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,
  398. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
  399. 1, //golomb_order
  400. 2, //inc_limit
  401. 11 //max_run
  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,
  417. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
  418. 1, //golomb_order
  419. 4, //inc_limit
  420. 8 //max_run
  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,
  436. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
  437. 0, //golomb_order
  438. INT_MAX, //inc_limit
  439. 5, //max_run
  440. }
  441. };
  442. #undef EOB
  443. /*****************************************************************************
  444. *
  445. * motion vector prediction
  446. *
  447. ****************************************************************************/
  448. static inline void store_mvs(AVSContext *h) {
  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. cavs_vector *pmv_bw = pmv_fw + MV_BWD_OFFS;
  457. int den = h->direct_den[col_mv->ref];
  458. int m = col_mv->x >> 31;
  459. pmv_fw->dist = h->dist[1];
  460. pmv_bw->dist = h->dist[0];
  461. pmv_fw->ref = 1;
  462. pmv_bw->ref = 0;
  463. /* scale the co-located motion vector according to its temporal span */
  464. pmv_fw->x = (((den+(den*col_mv->x*pmv_fw->dist^m)-m-1)>>14)^m)-m;
  465. pmv_bw->x = m-(((den+(den*col_mv->x*pmv_bw->dist^m)-m-1)>>14)^m);
  466. m = col_mv->y >> 31;
  467. pmv_fw->y = (((den+(den*col_mv->y*pmv_fw->dist^m)-m-1)>>14)^m)-m;
  468. pmv_bw->y = m-(((den+(den*col_mv->y*pmv_bw->dist^m)-m-1)>>14)^m);
  469. }
  470. static inline void mv_pred_sym(AVSContext *h, cavs_vector *src, enum cavs_block size) {
  471. cavs_vector *dst = src + MV_BWD_OFFS;
  472. /* backward mv is the scaled and negated forward mv */
  473. dst->x = -((src->x * h->sym_factor + 256) >> 9);
  474. dst->y = -((src->y * h->sym_factor + 256) >> 9);
  475. dst->ref = 0;
  476. dst->dist = h->dist[0];
  477. set_mvs(dst, size);
  478. }
  479. /*****************************************************************************
  480. *
  481. * residual data decoding
  482. *
  483. ****************************************************************************/
  484. /** kth-order exponential golomb code */
  485. static inline int get_ue_code(GetBitContext *gb, int order) {
  486. if(order) {
  487. int ret = get_ue_golomb(gb) << order;
  488. return ret + get_bits(gb,order);
  489. }
  490. return get_ue_golomb(gb);
  491. }
  492. static inline int dequant(AVSContext *h, DCTELEM *level_buf, uint8_t *run_buf,
  493. DCTELEM *dst, int mul, int shift, int coeff_num) {
  494. int round = 1 << (shift - 1);
  495. int pos = -1;
  496. const uint8_t *scantab = h->scantable.permutated;
  497. /* inverse scan and dequantization */
  498. while(--coeff_num >= 0){
  499. pos += run_buf[coeff_num];
  500. if(pos > 63) {
  501. av_log(h->s.avctx, AV_LOG_ERROR,
  502. "position out of block bounds at pic %d MB(%d,%d)\n",
  503. h->picture.poc, h->mbx, h->mby);
  504. return -1;
  505. }
  506. dst[scantab[pos]] = (level_buf[coeff_num]*mul + round) >> shift;
  507. }
  508. return 0;
  509. }
  510. /**
  511. * decode coefficients from one 8x8 block, dequantize, inverse transform
  512. * and add them to sample block
  513. * @param r pointer to 2D VLC table
  514. * @param esc_golomb_order escape codes are k-golomb with this order k
  515. * @param qp quantizer
  516. * @param dst location of sample block
  517. * @param stride line stride in frame buffer
  518. */
  519. static int decode_residual_block(AVSContext *h, GetBitContext *gb,
  520. const struct dec_2dvlc *r, int esc_golomb_order,
  521. int qp, uint8_t *dst, int stride) {
  522. int i, level_code, esc_code, level, run, mask;
  523. DCTELEM level_buf[65];
  524. uint8_t run_buf[65];
  525. DCTELEM *block = h->block;
  526. for(i=0;i<65;i++) {
  527. level_code = get_ue_code(gb,r->golomb_order);
  528. if(level_code >= ESCAPE_CODE) {
  529. run = ((level_code - ESCAPE_CODE) >> 1) + 1;
  530. esc_code = get_ue_code(gb,esc_golomb_order);
  531. level = esc_code + (run > r->max_run ? 1 : r->level_add[run]);
  532. while(level > r->inc_limit)
  533. r++;
  534. mask = -(level_code & 1);
  535. level = (level^mask) - mask;
  536. } else if (level_code >= 0) {
  537. level = r->rltab[level_code][0];
  538. if(!level) //end of block signal
  539. break;
  540. run = r->rltab[level_code][1];
  541. r += r->rltab[level_code][2];
  542. } else {
  543. break;
  544. }
  545. level_buf[i] = level;
  546. run_buf[i] = run;
  547. }
  548. if (dequant(h, level_buf, run_buf, block, dequant_mul[qp],
  549. dequant_shift[qp], i))
  550. return -1;
  551. h->cdsp.cavs_idct8_add(dst,block,stride);
  552. h->s.dsp.clear_block(block);
  553. return 0;
  554. }
  555. static inline void decode_residual_chroma(AVSContext *h) {
  556. if(h->cbp & (1<<4))
  557. decode_residual_block(h, &h->s.gb, chroma_dec, 0,
  558. cavs_chroma_qp[h->qp], h->cu, h->c_stride);
  559. if(h->cbp & (1<<5))
  560. decode_residual_block(h, &h->s.gb, chroma_dec, 0,
  561. cavs_chroma_qp[h->qp], h->cv, h->c_stride);
  562. }
  563. static inline int decode_residual_inter(AVSContext *h) {
  564. int block;
  565. /* get coded block pattern */
  566. int cbp= get_ue_golomb(&h->s.gb);
  567. if(cbp > 63){
  568. av_log(h->s.avctx, AV_LOG_ERROR, "illegal inter cbp\n");
  569. return -1;
  570. }
  571. h->cbp = cbp_tab[cbp][1];
  572. /* get quantizer */
  573. if(h->cbp && !h->qp_fixed)
  574. h->qp = (h->qp + get_se_golomb(&h->s.gb)) & 63;
  575. for(block=0;block<4;block++)
  576. if(h->cbp & (1<<block))
  577. decode_residual_block(h, &h->s.gb, inter_dec, 0, h->qp,
  578. h->cy + h->luma_scan[block], h->l_stride);
  579. decode_residual_chroma(h);
  580. return 0;
  581. }
  582. /*****************************************************************************
  583. *
  584. * macroblock level
  585. *
  586. ****************************************************************************/
  587. static inline void set_mv_intra(AVSContext *h) {
  588. h->mv[MV_FWD_X0] = ff_cavs_intra_mv;
  589. set_mvs(&h->mv[MV_FWD_X0], BLK_16X16);
  590. h->mv[MV_BWD_X0] = ff_cavs_intra_mv;
  591. set_mvs(&h->mv[MV_BWD_X0], BLK_16X16);
  592. if(h->pic_type != AV_PICTURE_TYPE_B)
  593. h->col_type_base[h->mbidx] = I_8X8;
  594. }
  595. static int decode_mb_i(AVSContext *h, int cbp_code) {
  596. GetBitContext *gb = &h->s.gb;
  597. unsigned pred_mode_uv;
  598. int block;
  599. uint8_t top[18];
  600. uint8_t *left = NULL;
  601. uint8_t *d;
  602. ff_cavs_init_mb(h);
  603. /* get intra prediction modes from stream */
  604. for(block=0;block<4;block++) {
  605. int nA,nB,predpred;
  606. int pos = scan3x3[block];
  607. nA = h->pred_mode_Y[pos-1];
  608. nB = h->pred_mode_Y[pos-3];
  609. predpred = FFMIN(nA,nB);
  610. if(predpred == NOT_AVAIL) // if either is not available
  611. predpred = INTRA_L_LP;
  612. if(!get_bits1(gb)){
  613. int rem_mode= get_bits(gb, 2);
  614. predpred = rem_mode + (rem_mode >= predpred);
  615. }
  616. h->pred_mode_Y[pos] = predpred;
  617. }
  618. pred_mode_uv = get_ue_golomb(gb);
  619. if(pred_mode_uv > 6) {
  620. av_log(h->s.avctx, AV_LOG_ERROR, "illegal intra chroma pred mode\n");
  621. return -1;
  622. }
  623. ff_cavs_modify_mb_i(h, &pred_mode_uv);
  624. /* get coded block pattern */
  625. if(h->pic_type == AV_PICTURE_TYPE_I)
  626. cbp_code = get_ue_golomb(gb);
  627. if(cbp_code > 63){
  628. av_log(h->s.avctx, AV_LOG_ERROR, "illegal intra cbp\n");
  629. return -1;
  630. }
  631. h->cbp = cbp_tab[cbp_code][0];
  632. if(h->cbp && !h->qp_fixed)
  633. h->qp = (h->qp + get_se_golomb(gb)) & 63; //qp_delta
  634. /* luma intra prediction interleaved with residual decode/transform/add */
  635. for(block=0;block<4;block++) {
  636. d = h->cy + h->luma_scan[block];
  637. ff_cavs_load_intra_pred_luma(h, top, &left, block);
  638. h->intra_pred_l[h->pred_mode_Y[scan3x3[block]]]
  639. (d, top, left, h->l_stride);
  640. if(h->cbp & (1<<block))
  641. decode_residual_block(h, gb, intra_dec, 1, h->qp, d, h->l_stride);
  642. }
  643. /* chroma intra prediction */
  644. ff_cavs_load_intra_pred_chroma(h);
  645. h->intra_pred_c[pred_mode_uv](h->cu, &h->top_border_u[h->mbx*10],
  646. h->left_border_u, h->c_stride);
  647. h->intra_pred_c[pred_mode_uv](h->cv, &h->top_border_v[h->mbx*10],
  648. h->left_border_v, h->c_stride);
  649. decode_residual_chroma(h);
  650. ff_cavs_filter(h,I_8X8);
  651. set_mv_intra(h);
  652. return 0;
  653. }
  654. static inline void set_intra_mode_default(AVSContext *h) {
  655. if(h->stream_revision > 0) {
  656. h->pred_mode_Y[3] = h->pred_mode_Y[6] = NOT_AVAIL;
  657. h->top_pred_Y[h->mbx*2+0] = h->top_pred_Y[h->mbx*2+1] = NOT_AVAIL;
  658. } else {
  659. h->pred_mode_Y[3] = h->pred_mode_Y[6] = INTRA_L_LP;
  660. h->top_pred_Y[h->mbx*2+0] = h->top_pred_Y[h->mbx*2+1] = INTRA_L_LP;
  661. }
  662. }
  663. static void decode_mb_p(AVSContext *h, enum cavs_mb mb_type) {
  664. GetBitContext *gb = &h->s.gb;
  665. int ref[4];
  666. ff_cavs_init_mb(h);
  667. switch(mb_type) {
  668. case P_SKIP:
  669. ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_PSKIP, BLK_16X16, 0);
  670. break;
  671. case P_16X16:
  672. ref[0] = h->ref_flag ? 0 : get_bits1(gb);
  673. ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16,ref[0]);
  674. break;
  675. case P_16X8:
  676. ref[0] = h->ref_flag ? 0 : get_bits1(gb);
  677. ref[2] = h->ref_flag ? 0 : get_bits1(gb);
  678. ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_TOP, BLK_16X8, ref[0]);
  679. ff_cavs_mv(h, MV_FWD_X2, MV_FWD_A1, MV_PRED_LEFT, BLK_16X8, ref[2]);
  680. break;
  681. case P_8X16:
  682. ref[0] = h->ref_flag ? 0 : get_bits1(gb);
  683. ref[1] = h->ref_flag ? 0 : get_bits1(gb);
  684. ff_cavs_mv(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_LEFT, BLK_8X16, ref[0]);
  685. ff_cavs_mv(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_TOPRIGHT,BLK_8X16, ref[1]);
  686. break;
  687. case P_8X8:
  688. ref[0] = h->ref_flag ? 0 : get_bits1(gb);
  689. ref[1] = h->ref_flag ? 0 : get_bits1(gb);
  690. ref[2] = h->ref_flag ? 0 : get_bits1(gb);
  691. ref[3] = h->ref_flag ? 0 : get_bits1(gb);
  692. ff_cavs_mv(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_MEDIAN, BLK_8X8, ref[0]);
  693. ff_cavs_mv(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_MEDIAN, BLK_8X8, ref[1]);
  694. ff_cavs_mv(h, MV_FWD_X2, MV_FWD_X1, MV_PRED_MEDIAN, BLK_8X8, ref[2]);
  695. ff_cavs_mv(h, MV_FWD_X3, MV_FWD_X0, MV_PRED_MEDIAN, BLK_8X8, ref[3]);
  696. }
  697. ff_cavs_inter(h, mb_type);
  698. set_intra_mode_default(h);
  699. store_mvs(h);
  700. if(mb_type != P_SKIP)
  701. decode_residual_inter(h);
  702. ff_cavs_filter(h,mb_type);
  703. h->col_type_base[h->mbidx] = mb_type;
  704. }
  705. static void decode_mb_b(AVSContext *h, enum cavs_mb mb_type) {
  706. int block;
  707. enum cavs_sub_mb sub_type[4];
  708. int flags;
  709. ff_cavs_init_mb(h);
  710. /* reset all MVs */
  711. h->mv[MV_FWD_X0] = ff_cavs_dir_mv;
  712. set_mvs(&h->mv[MV_FWD_X0], BLK_16X16);
  713. h->mv[MV_BWD_X0] = ff_cavs_dir_mv;
  714. set_mvs(&h->mv[MV_BWD_X0], BLK_16X16);
  715. switch(mb_type) {
  716. case B_SKIP:
  717. case B_DIRECT:
  718. if(!h->col_type_base[h->mbidx]) {
  719. /* intra MB at co-location, do in-plane prediction */
  720. ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_BSKIP, BLK_16X16, 1);
  721. ff_cavs_mv(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_BSKIP, BLK_16X16, 0);
  722. } else
  723. /* direct prediction from co-located P MB, block-wise */
  724. for(block=0;block<4;block++)
  725. mv_pred_direct(h,&h->mv[mv_scan[block]],
  726. &h->col_mv[h->mbidx*4 + block]);
  727. break;
  728. case B_FWD_16X16:
  729. ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16, 1);
  730. break;
  731. case B_SYM_16X16:
  732. ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16, 1);
  733. mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_16X16);
  734. break;
  735. case B_BWD_16X16:
  736. ff_cavs_mv(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_MEDIAN, BLK_16X16, 0);
  737. break;
  738. case B_8X8:
  739. for(block=0;block<4;block++)
  740. sub_type[block] = get_bits(&h->s.gb,2);
  741. for(block=0;block<4;block++) {
  742. switch(sub_type[block]) {
  743. case B_SUB_DIRECT:
  744. if(!h->col_type_base[h->mbidx]) {
  745. /* intra MB at co-location, do in-plane prediction */
  746. ff_cavs_mv(h, mv_scan[block], mv_scan[block]-3,
  747. MV_PRED_BSKIP, BLK_8X8, 1);
  748. ff_cavs_mv(h, mv_scan[block]+MV_BWD_OFFS,
  749. mv_scan[block]-3+MV_BWD_OFFS,
  750. MV_PRED_BSKIP, BLK_8X8, 0);
  751. } else
  752. mv_pred_direct(h,&h->mv[mv_scan[block]],
  753. &h->col_mv[h->mbidx*4 + block]);
  754. break;
  755. case B_SUB_FWD:
  756. ff_cavs_mv(h, mv_scan[block], mv_scan[block]-3,
  757. MV_PRED_MEDIAN, BLK_8X8, 1);
  758. break;
  759. case B_SUB_SYM:
  760. ff_cavs_mv(h, mv_scan[block], mv_scan[block]-3,
  761. MV_PRED_MEDIAN, BLK_8X8, 1);
  762. mv_pred_sym(h, &h->mv[mv_scan[block]], BLK_8X8);
  763. break;
  764. }
  765. }
  766. for(block=0;block<4;block++) {
  767. if(sub_type[block] == B_SUB_BWD)
  768. ff_cavs_mv(h, mv_scan[block]+MV_BWD_OFFS,
  769. mv_scan[block]+MV_BWD_OFFS-3,
  770. MV_PRED_MEDIAN, BLK_8X8, 0);
  771. }
  772. break;
  773. default:
  774. assert((mb_type > B_SYM_16X16) && (mb_type < B_8X8));
  775. flags = ff_cavs_partition_flags[mb_type];
  776. if(mb_type & 1) { /* 16x8 macroblock types */
  777. if(flags & FWD0)
  778. ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_TOP, BLK_16X8, 1);
  779. if(flags & SYM0)
  780. mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_16X8);
  781. if(flags & FWD1)
  782. ff_cavs_mv(h, MV_FWD_X2, MV_FWD_A1, MV_PRED_LEFT, BLK_16X8, 1);
  783. if(flags & SYM1)
  784. mv_pred_sym(h, &h->mv[MV_FWD_X2], BLK_16X8);
  785. if(flags & BWD0)
  786. ff_cavs_mv(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_TOP, BLK_16X8, 0);
  787. if(flags & BWD1)
  788. ff_cavs_mv(h, MV_BWD_X2, MV_BWD_A1, MV_PRED_LEFT, BLK_16X8, 0);
  789. } else { /* 8x16 macroblock types */
  790. if(flags & FWD0)
  791. ff_cavs_mv(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_LEFT, BLK_8X16, 1);
  792. if(flags & SYM0)
  793. mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_8X16);
  794. if(flags & FWD1)
  795. ff_cavs_mv(h,MV_FWD_X1,MV_FWD_C2,MV_PRED_TOPRIGHT,BLK_8X16,1);
  796. if(flags & SYM1)
  797. mv_pred_sym(h, &h->mv[MV_FWD_X1], BLK_8X16);
  798. if(flags & BWD0)
  799. ff_cavs_mv(h, MV_BWD_X0, MV_BWD_B3, MV_PRED_LEFT, BLK_8X16, 0);
  800. if(flags & BWD1)
  801. ff_cavs_mv(h,MV_BWD_X1,MV_BWD_C2,MV_PRED_TOPRIGHT,BLK_8X16,0);
  802. }
  803. }
  804. ff_cavs_inter(h, mb_type);
  805. set_intra_mode_default(h);
  806. if(mb_type != B_SKIP)
  807. decode_residual_inter(h);
  808. ff_cavs_filter(h,mb_type);
  809. }
  810. /*****************************************************************************
  811. *
  812. * slice level
  813. *
  814. ****************************************************************************/
  815. static inline int decode_slice_header(AVSContext *h, GetBitContext *gb) {
  816. if(h->stc > 0xAF)
  817. av_log(h->s.avctx, AV_LOG_ERROR, "unexpected start code 0x%02x\n", h->stc);
  818. h->mby = h->stc;
  819. h->mbidx = h->mby*h->mb_width;
  820. /* mark top macroblocks as unavailable */
  821. h->flags &= ~(B_AVAIL|C_AVAIL);
  822. if((h->mby == 0) && (!h->qp_fixed)){
  823. h->qp_fixed = get_bits1(gb);
  824. h->qp = get_bits(gb,6);
  825. }
  826. /* inter frame or second slice can have weighting params */
  827. if((h->pic_type != AV_PICTURE_TYPE_I) || (!h->pic_structure && h->mby >= h->mb_width/2))
  828. if(get_bits1(gb)) { //slice_weighting_flag
  829. av_log(h->s.avctx, AV_LOG_ERROR,
  830. "weighted prediction not yet supported\n");
  831. }
  832. return 0;
  833. }
  834. static inline int check_for_slice(AVSContext *h) {
  835. GetBitContext *gb = &h->s.gb;
  836. int align;
  837. if(h->mbx)
  838. return 0;
  839. align = (-get_bits_count(gb)) & 7;
  840. /* check for stuffing byte */
  841. if(!align && (show_bits(gb,8) == 0x80))
  842. align = 8;
  843. if((show_bits_long(gb,24+align) & 0xFFFFFF) == 0x000001) {
  844. skip_bits_long(gb,24+align);
  845. h->stc = get_bits(gb,8);
  846. if (h->stc >= h->mb_height)
  847. return 0;
  848. decode_slice_header(h,gb);
  849. return 1;
  850. }
  851. return 0;
  852. }
  853. /*****************************************************************************
  854. *
  855. * frame level
  856. *
  857. ****************************************************************************/
  858. static int decode_pic(AVSContext *h) {
  859. MpegEncContext *s = &h->s;
  860. int skip_count = -1;
  861. enum cavs_mb mb_type;
  862. if (!s->context_initialized) {
  863. if (ff_MPV_common_init(s) < 0)
  864. return -1;
  865. ff_init_scantable_permutation(s->dsp.idct_permutation,
  866. h->cdsp.idct_perm);
  867. ff_init_scantable(s->dsp.idct_permutation,&h->scantable,ff_zigzag_direct);
  868. }
  869. skip_bits(&s->gb,16);//bbv_dwlay
  870. if(h->stc == PIC_PB_START_CODE) {
  871. h->pic_type = get_bits(&s->gb,2) + AV_PICTURE_TYPE_I;
  872. if(h->pic_type > AV_PICTURE_TYPE_B) {
  873. av_log(s->avctx, AV_LOG_ERROR, "illegal picture type\n");
  874. return -1;
  875. }
  876. /* make sure we have the reference frames we need */
  877. if(!h->DPB[0].f.data[0] ||
  878. (!h->DPB[1].f.data[0] && h->pic_type == AV_PICTURE_TYPE_B))
  879. return -1;
  880. } else {
  881. h->pic_type = AV_PICTURE_TYPE_I;
  882. if(get_bits1(&s->gb))
  883. skip_bits(&s->gb,24);//time_code
  884. /* old sample clips were all progressive and no low_delay,
  885. bump stream revision if detected otherwise */
  886. if (s->low_delay || !(show_bits(&s->gb,9) & 1))
  887. h->stream_revision = 1;
  888. /* similarly test top_field_first and repeat_first_field */
  889. else if(show_bits(&s->gb,11) & 3)
  890. h->stream_revision = 1;
  891. if(h->stream_revision > 0)
  892. skip_bits(&s->gb,1); //marker_bit
  893. }
  894. /* release last B frame */
  895. if(h->picture.f.data[0])
  896. s->avctx->release_buffer(s->avctx, &h->picture.f);
  897. s->avctx->get_buffer(s->avctx, &h->picture.f);
  898. ff_cavs_init_pic(h);
  899. h->picture.poc = get_bits(&s->gb,8)*2;
  900. /* get temporal distances and MV scaling factors */
  901. if(h->pic_type != AV_PICTURE_TYPE_B) {
  902. h->dist[0] = (h->picture.poc - h->DPB[0].poc + 512) % 512;
  903. } else {
  904. h->dist[0] = (h->DPB[0].poc - h->picture.poc + 512) % 512;
  905. }
  906. h->dist[1] = (h->picture.poc - h->DPB[1].poc + 512) % 512;
  907. h->scale_den[0] = h->dist[0] ? 512/h->dist[0] : 0;
  908. h->scale_den[1] = h->dist[1] ? 512/h->dist[1] : 0;
  909. if(h->pic_type == AV_PICTURE_TYPE_B) {
  910. h->sym_factor = h->dist[0]*h->scale_den[1];
  911. } else {
  912. h->direct_den[0] = h->dist[0] ? 16384/h->dist[0] : 0;
  913. h->direct_den[1] = h->dist[1] ? 16384/h->dist[1] : 0;
  914. }
  915. if(s->low_delay)
  916. get_ue_golomb(&s->gb); //bbv_check_times
  917. h->progressive = get_bits1(&s->gb);
  918. h->pic_structure = 1;
  919. if(!h->progressive)
  920. h->pic_structure = get_bits1(&s->gb);
  921. if(!h->pic_structure && h->stc == PIC_PB_START_CODE)
  922. skip_bits1(&s->gb); //advanced_pred_mode_disable
  923. skip_bits1(&s->gb); //top_field_first
  924. skip_bits1(&s->gb); //repeat_first_field
  925. h->qp_fixed = get_bits1(&s->gb);
  926. h->qp = get_bits(&s->gb,6);
  927. if(h->pic_type == AV_PICTURE_TYPE_I) {
  928. if(!h->progressive && !h->pic_structure)
  929. skip_bits1(&s->gb);//what is this?
  930. skip_bits(&s->gb,4); //reserved bits
  931. } else {
  932. if(!(h->pic_type == AV_PICTURE_TYPE_B && h->pic_structure == 1))
  933. h->ref_flag = get_bits1(&s->gb);
  934. skip_bits(&s->gb,4); //reserved bits
  935. h->skip_mode_flag = get_bits1(&s->gb);
  936. }
  937. h->loop_filter_disable = get_bits1(&s->gb);
  938. if(!h->loop_filter_disable && get_bits1(&s->gb)) {
  939. h->alpha_offset = get_se_golomb(&s->gb);
  940. h->beta_offset = get_se_golomb(&s->gb);
  941. } else {
  942. h->alpha_offset = h->beta_offset = 0;
  943. }
  944. if(h->pic_type == AV_PICTURE_TYPE_I) {
  945. do {
  946. check_for_slice(h);
  947. decode_mb_i(h, 0);
  948. } while(ff_cavs_next_mb(h));
  949. } else if(h->pic_type == AV_PICTURE_TYPE_P) {
  950. do {
  951. if(check_for_slice(h))
  952. skip_count = -1;
  953. if(h->skip_mode_flag && (skip_count < 0))
  954. skip_count = get_ue_golomb(&s->gb);
  955. if(h->skip_mode_flag && skip_count--) {
  956. decode_mb_p(h,P_SKIP);
  957. } else {
  958. mb_type = get_ue_golomb(&s->gb) + P_SKIP + h->skip_mode_flag;
  959. if(mb_type > P_8X8)
  960. decode_mb_i(h, mb_type - P_8X8 - 1);
  961. else
  962. decode_mb_p(h,mb_type);
  963. }
  964. } while(ff_cavs_next_mb(h));
  965. } else { /* AV_PICTURE_TYPE_B */
  966. do {
  967. if(check_for_slice(h))
  968. skip_count = -1;
  969. if(h->skip_mode_flag && (skip_count < 0))
  970. skip_count = get_ue_golomb(&s->gb);
  971. if(h->skip_mode_flag && skip_count--) {
  972. decode_mb_b(h,B_SKIP);
  973. } else {
  974. mb_type = get_ue_golomb(&s->gb) + B_SKIP + h->skip_mode_flag;
  975. if(mb_type > B_8X8)
  976. decode_mb_i(h, mb_type - B_8X8 - 1);
  977. else
  978. decode_mb_b(h,mb_type);
  979. }
  980. } while(ff_cavs_next_mb(h));
  981. }
  982. if(h->pic_type != AV_PICTURE_TYPE_B) {
  983. if(h->DPB[1].f.data[0])
  984. s->avctx->release_buffer(s->avctx, &h->DPB[1].f);
  985. h->DPB[1] = h->DPB[0];
  986. h->DPB[0] = h->picture;
  987. memset(&h->picture,0,sizeof(Picture));
  988. }
  989. return 0;
  990. }
  991. /*****************************************************************************
  992. *
  993. * headers and interface
  994. *
  995. ****************************************************************************/
  996. static int decode_seq_header(AVSContext *h) {
  997. MpegEncContext *s = &h->s;
  998. int frame_rate_code;
  999. int width, height;
  1000. h->profile = get_bits(&s->gb,8);
  1001. h->level = get_bits(&s->gb,8);
  1002. skip_bits1(&s->gb); //progressive sequence
  1003. width = get_bits(&s->gb, 14);
  1004. height = get_bits(&s->gb, 14);
  1005. if ((s->width || s->height) && (s->width != width || s->height != height)) {
  1006. av_log_missing_feature(s, "Width/height changing in CAVS is", 0);
  1007. return AVERROR_PATCHWELCOME;
  1008. }
  1009. s->width = width;
  1010. s->height = height;
  1011. skip_bits(&s->gb,2); //chroma format
  1012. skip_bits(&s->gb,3); //sample_precision
  1013. h->aspect_ratio = get_bits(&s->gb,4);
  1014. frame_rate_code = get_bits(&s->gb,4);
  1015. skip_bits(&s->gb,18);//bit_rate_lower
  1016. skip_bits1(&s->gb); //marker_bit
  1017. skip_bits(&s->gb,12);//bit_rate_upper
  1018. s->low_delay = get_bits1(&s->gb);
  1019. h->mb_width = (s->width + 15) >> 4;
  1020. h->mb_height = (s->height + 15) >> 4;
  1021. h->s.avctx->time_base.den = avpriv_frame_rate_tab[frame_rate_code].num;
  1022. h->s.avctx->time_base.num = avpriv_frame_rate_tab[frame_rate_code].den;
  1023. h->s.avctx->width = s->width;
  1024. h->s.avctx->height = s->height;
  1025. if(!h->top_qp)
  1026. ff_cavs_init_top_lines(h);
  1027. return 0;
  1028. }
  1029. static void cavs_flush(AVCodecContext * avctx) {
  1030. AVSContext *h = avctx->priv_data;
  1031. h->got_keyframe = 0;
  1032. }
  1033. static int cavs_decode_frame(AVCodecContext * avctx,void *data, int *data_size,
  1034. AVPacket *avpkt) {
  1035. const uint8_t *buf = avpkt->data;
  1036. int buf_size = avpkt->size;
  1037. AVSContext *h = avctx->priv_data;
  1038. MpegEncContext *s = &h->s;
  1039. int input_size;
  1040. const uint8_t *buf_end;
  1041. const uint8_t *buf_ptr;
  1042. AVFrame *picture = data;
  1043. uint32_t stc = -1;
  1044. s->avctx = avctx;
  1045. if (buf_size == 0) {
  1046. if (!s->low_delay && h->DPB[0].f.data[0]) {
  1047. *data_size = sizeof(AVPicture);
  1048. *picture = h->DPB[0].f;
  1049. memset(&h->DPB[0], 0, sizeof(h->DPB[0]));
  1050. }
  1051. return 0;
  1052. }
  1053. buf_ptr = buf;
  1054. buf_end = buf + buf_size;
  1055. for(;;) {
  1056. buf_ptr = avpriv_mpv_find_start_code(buf_ptr,buf_end, &stc);
  1057. if((stc & 0xFFFFFE00) || buf_ptr == buf_end)
  1058. return FFMAX(0, buf_ptr - buf - s->parse_context.last_index);
  1059. input_size = (buf_end - buf_ptr)*8;
  1060. switch(stc) {
  1061. case CAVS_START_CODE:
  1062. init_get_bits(&s->gb, buf_ptr, input_size);
  1063. decode_seq_header(h);
  1064. break;
  1065. case PIC_I_START_CODE:
  1066. if(!h->got_keyframe) {
  1067. if(h->DPB[0].f.data[0])
  1068. avctx->release_buffer(avctx, &h->DPB[0].f);
  1069. if(h->DPB[1].f.data[0])
  1070. avctx->release_buffer(avctx, &h->DPB[1].f);
  1071. h->got_keyframe = 1;
  1072. }
  1073. case PIC_PB_START_CODE:
  1074. *data_size = 0;
  1075. if(!h->got_keyframe)
  1076. break;
  1077. init_get_bits(&s->gb, buf_ptr, input_size);
  1078. h->stc = stc;
  1079. if(decode_pic(h))
  1080. break;
  1081. *data_size = sizeof(AVPicture);
  1082. if(h->pic_type != AV_PICTURE_TYPE_B) {
  1083. if(h->DPB[1].f.data[0]) {
  1084. *picture = h->DPB[1].f;
  1085. } else {
  1086. *data_size = 0;
  1087. }
  1088. } else
  1089. *picture = h->picture.f;
  1090. break;
  1091. case EXT_START_CODE:
  1092. //mpeg_decode_extension(avctx,buf_ptr, input_size);
  1093. break;
  1094. case USER_START_CODE:
  1095. //mpeg_decode_user_data(avctx,buf_ptr, input_size);
  1096. break;
  1097. default:
  1098. if (stc <= SLICE_MAX_START_CODE) {
  1099. init_get_bits(&s->gb, buf_ptr, input_size);
  1100. decode_slice_header(h, &s->gb);
  1101. }
  1102. break;
  1103. }
  1104. }
  1105. }
  1106. AVCodec ff_cavs_decoder = {
  1107. .name = "cavs",
  1108. .type = AVMEDIA_TYPE_VIDEO,
  1109. .id = AV_CODEC_ID_CAVS,
  1110. .priv_data_size = sizeof(AVSContext),
  1111. .init = ff_cavs_init,
  1112. .close = ff_cavs_end,
  1113. .decode = cavs_decode_frame,
  1114. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY,
  1115. .flush = cavs_flush,
  1116. .long_name = NULL_IF_CONFIG_SMALL("Chinese AVS (Audio Video Standard) (AVS1-P2, JiZhun profile)"),
  1117. };