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.

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