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.

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