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.

1205 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. 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, esc_code, level, mask;
  524. unsigned int level_code, run;
  525. DCTELEM level_buf[65];
  526. uint8_t run_buf[65];
  527. DCTELEM *block = h->block;
  528. for(i=0;i<65;i++) {
  529. level_code = get_ue_code(gb,r->golomb_order);
  530. if(level_code >= ESCAPE_CODE) {
  531. run = ((level_code - ESCAPE_CODE) >> 1) + 1;
  532. if(run > 64)
  533. return -1;
  534. esc_code = get_ue_code(gb,esc_golomb_order);
  535. level = esc_code + (run > r->max_run ? 1 : r->level_add[run]);
  536. while(level > r->inc_limit)
  537. r++;
  538. mask = -(level_code & 1);
  539. level = (level^mask) - mask;
  540. } else {
  541. level = r->rltab[level_code][0];
  542. if(!level) //end of block signal
  543. break;
  544. run = r->rltab[level_code][1];
  545. r += r->rltab[level_code][2];
  546. }
  547. level_buf[i] = level;
  548. run_buf[i] = run;
  549. }
  550. if (dequant(h, level_buf, run_buf, block, dequant_mul[qp],
  551. dequant_shift[qp], i))
  552. return -1;
  553. h->cdsp.cavs_idct8_add(dst,block,stride);
  554. h->s.dsp.clear_block(block);
  555. return 0;
  556. }
  557. static inline void decode_residual_chroma(AVSContext *h) {
  558. if(h->cbp & (1<<4))
  559. decode_residual_block(h, &h->s.gb, chroma_dec, 0,
  560. cavs_chroma_qp[h->qp], h->cu, h->c_stride);
  561. if(h->cbp & (1<<5))
  562. decode_residual_block(h, &h->s.gb, chroma_dec, 0,
  563. cavs_chroma_qp[h->qp], h->cv, h->c_stride);
  564. }
  565. static inline int decode_residual_inter(AVSContext *h) {
  566. int block;
  567. /* get coded block pattern */
  568. int cbp= get_ue_golomb(&h->s.gb);
  569. if(cbp > 63U){
  570. av_log(h->s.avctx, AV_LOG_ERROR, "illegal inter cbp\n");
  571. return -1;
  572. }
  573. h->cbp = cbp_tab[cbp][1];
  574. /* get quantizer */
  575. if(h->cbp && !h->qp_fixed)
  576. h->qp = (h->qp + get_se_golomb(&h->s.gb)) & 63;
  577. for(block=0;block<4;block++)
  578. if(h->cbp & (1<<block))
  579. decode_residual_block(h, &h->s.gb, inter_dec, 0, h->qp,
  580. h->cy + h->luma_scan[block], h->l_stride);
  581. decode_residual_chroma(h);
  582. return 0;
  583. }
  584. /*****************************************************************************
  585. *
  586. * macroblock level
  587. *
  588. ****************************************************************************/
  589. static inline void set_mv_intra(AVSContext *h) {
  590. h->mv[MV_FWD_X0] = ff_cavs_intra_mv;
  591. set_mvs(&h->mv[MV_FWD_X0], BLK_16X16);
  592. h->mv[MV_BWD_X0] = ff_cavs_intra_mv;
  593. set_mvs(&h->mv[MV_BWD_X0], BLK_16X16);
  594. if(h->pic_type != AV_PICTURE_TYPE_B)
  595. h->col_type_base[h->mbidx] = I_8X8;
  596. }
  597. static int decode_mb_i(AVSContext *h, int cbp_code) {
  598. GetBitContext *gb = &h->s.gb;
  599. unsigned pred_mode_uv;
  600. int block;
  601. uint8_t top[18];
  602. uint8_t *left = NULL;
  603. uint8_t *d;
  604. ff_cavs_init_mb(h);
  605. /* get intra prediction modes from stream */
  606. for(block=0;block<4;block++) {
  607. int nA,nB,predpred;
  608. int pos = scan3x3[block];
  609. nA = h->pred_mode_Y[pos-1];
  610. nB = h->pred_mode_Y[pos-3];
  611. predpred = FFMIN(nA,nB);
  612. if(predpred == NOT_AVAIL) // if either is not available
  613. predpred = INTRA_L_LP;
  614. if(!get_bits1(gb)){
  615. int rem_mode= get_bits(gb, 2);
  616. predpred = rem_mode + (rem_mode >= predpred);
  617. }
  618. h->pred_mode_Y[pos] = predpred;
  619. }
  620. pred_mode_uv = get_ue_golomb(gb);
  621. if(pred_mode_uv > 6) {
  622. av_log(h->s.avctx, AV_LOG_ERROR, "illegal intra chroma pred mode\n");
  623. return -1;
  624. }
  625. ff_cavs_modify_mb_i(h, &pred_mode_uv);
  626. /* get coded block pattern */
  627. if(h->pic_type == AV_PICTURE_TYPE_I)
  628. cbp_code = get_ue_golomb(gb);
  629. if(cbp_code > 63U){
  630. av_log(h->s.avctx, AV_LOG_ERROR, "illegal intra cbp\n");
  631. return -1;
  632. }
  633. h->cbp = cbp_tab[cbp_code][0];
  634. if(h->cbp && !h->qp_fixed)
  635. h->qp = (h->qp + get_se_golomb(gb)) & 63; //qp_delta
  636. /* luma intra prediction interleaved with residual decode/transform/add */
  637. for(block=0;block<4;block++) {
  638. d = h->cy + h->luma_scan[block];
  639. ff_cavs_load_intra_pred_luma(h, top, &left, block);
  640. h->intra_pred_l[h->pred_mode_Y[scan3x3[block]]]
  641. (d, top, left, h->l_stride);
  642. if(h->cbp & (1<<block))
  643. decode_residual_block(h, gb, intra_dec, 1, h->qp, d, h->l_stride);
  644. }
  645. /* chroma intra prediction */
  646. ff_cavs_load_intra_pred_chroma(h);
  647. h->intra_pred_c[pred_mode_uv](h->cu, &h->top_border_u[h->mbx*10],
  648. h->left_border_u, h->c_stride);
  649. h->intra_pred_c[pred_mode_uv](h->cv, &h->top_border_v[h->mbx*10],
  650. h->left_border_v, h->c_stride);
  651. decode_residual_chroma(h);
  652. ff_cavs_filter(h,I_8X8);
  653. set_mv_intra(h);
  654. return 0;
  655. }
  656. static inline void set_intra_mode_default(AVSContext *h) {
  657. if(h->stream_revision > 0) {
  658. h->pred_mode_Y[3] = h->pred_mode_Y[6] = NOT_AVAIL;
  659. h->top_pred_Y[h->mbx*2+0] = h->top_pred_Y[h->mbx*2+1] = NOT_AVAIL;
  660. } else {
  661. h->pred_mode_Y[3] = h->pred_mode_Y[6] = INTRA_L_LP;
  662. h->top_pred_Y[h->mbx*2+0] = h->top_pred_Y[h->mbx*2+1] = INTRA_L_LP;
  663. }
  664. }
  665. static void decode_mb_p(AVSContext *h, enum cavs_mb mb_type) {
  666. GetBitContext *gb = &h->s.gb;
  667. int ref[4];
  668. ff_cavs_init_mb(h);
  669. switch(mb_type) {
  670. case P_SKIP:
  671. ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_PSKIP, BLK_16X16, 0);
  672. break;
  673. case P_16X16:
  674. ref[0] = h->ref_flag ? 0 : get_bits1(gb);
  675. ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16,ref[0]);
  676. break;
  677. case P_16X8:
  678. ref[0] = h->ref_flag ? 0 : get_bits1(gb);
  679. ref[2] = h->ref_flag ? 0 : get_bits1(gb);
  680. ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_TOP, BLK_16X8, ref[0]);
  681. ff_cavs_mv(h, MV_FWD_X2, MV_FWD_A1, MV_PRED_LEFT, BLK_16X8, ref[2]);
  682. break;
  683. case P_8X16:
  684. ref[0] = h->ref_flag ? 0 : get_bits1(gb);
  685. ref[1] = h->ref_flag ? 0 : get_bits1(gb);
  686. ff_cavs_mv(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_LEFT, BLK_8X16, ref[0]);
  687. ff_cavs_mv(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_TOPRIGHT,BLK_8X16, ref[1]);
  688. break;
  689. case P_8X8:
  690. ref[0] = h->ref_flag ? 0 : get_bits1(gb);
  691. ref[1] = h->ref_flag ? 0 : get_bits1(gb);
  692. ref[2] = h->ref_flag ? 0 : get_bits1(gb);
  693. ref[3] = h->ref_flag ? 0 : get_bits1(gb);
  694. ff_cavs_mv(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_MEDIAN, BLK_8X8, ref[0]);
  695. ff_cavs_mv(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_MEDIAN, BLK_8X8, ref[1]);
  696. ff_cavs_mv(h, MV_FWD_X2, MV_FWD_X1, MV_PRED_MEDIAN, BLK_8X8, ref[2]);
  697. ff_cavs_mv(h, MV_FWD_X3, MV_FWD_X0, MV_PRED_MEDIAN, BLK_8X8, ref[3]);
  698. }
  699. ff_cavs_inter(h, mb_type);
  700. set_intra_mode_default(h);
  701. store_mvs(h);
  702. if(mb_type != P_SKIP)
  703. decode_residual_inter(h);
  704. ff_cavs_filter(h,mb_type);
  705. h->col_type_base[h->mbidx] = mb_type;
  706. }
  707. static int decode_mb_b(AVSContext *h, enum cavs_mb mb_type) {
  708. int block;
  709. enum cavs_sub_mb sub_type[4];
  710. int flags;
  711. ff_cavs_init_mb(h);
  712. /* reset all MVs */
  713. h->mv[MV_FWD_X0] = ff_cavs_dir_mv;
  714. set_mvs(&h->mv[MV_FWD_X0], BLK_16X16);
  715. h->mv[MV_BWD_X0] = ff_cavs_dir_mv;
  716. set_mvs(&h->mv[MV_BWD_X0], BLK_16X16);
  717. switch(mb_type) {
  718. case B_SKIP:
  719. case B_DIRECT:
  720. if(!h->col_type_base[h->mbidx]) {
  721. /* intra MB at co-location, do in-plane prediction */
  722. ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_BSKIP, BLK_16X16, 1);
  723. ff_cavs_mv(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_BSKIP, BLK_16X16, 0);
  724. } else
  725. /* direct prediction from co-located P MB, block-wise */
  726. for(block=0;block<4;block++)
  727. mv_pred_direct(h,&h->mv[mv_scan[block]],
  728. &h->col_mv[h->mbidx*4 + block]);
  729. break;
  730. case B_FWD_16X16:
  731. ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16, 1);
  732. break;
  733. case B_SYM_16X16:
  734. ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16, 1);
  735. mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_16X16);
  736. break;
  737. case B_BWD_16X16:
  738. ff_cavs_mv(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_MEDIAN, BLK_16X16, 0);
  739. break;
  740. case B_8X8:
  741. for(block=0;block<4;block++)
  742. sub_type[block] = get_bits(&h->s.gb,2);
  743. for(block=0;block<4;block++) {
  744. switch(sub_type[block]) {
  745. case B_SUB_DIRECT:
  746. if(!h->col_type_base[h->mbidx]) {
  747. /* intra MB at co-location, do in-plane prediction */
  748. ff_cavs_mv(h, mv_scan[block], mv_scan[block]-3,
  749. MV_PRED_BSKIP, BLK_8X8, 1);
  750. ff_cavs_mv(h, mv_scan[block]+MV_BWD_OFFS,
  751. mv_scan[block]-3+MV_BWD_OFFS,
  752. MV_PRED_BSKIP, BLK_8X8, 0);
  753. } else
  754. mv_pred_direct(h,&h->mv[mv_scan[block]],
  755. &h->col_mv[h->mbidx*4 + block]);
  756. break;
  757. case B_SUB_FWD:
  758. ff_cavs_mv(h, mv_scan[block], mv_scan[block]-3,
  759. MV_PRED_MEDIAN, BLK_8X8, 1);
  760. break;
  761. case B_SUB_SYM:
  762. ff_cavs_mv(h, mv_scan[block], mv_scan[block]-3,
  763. MV_PRED_MEDIAN, BLK_8X8, 1);
  764. mv_pred_sym(h, &h->mv[mv_scan[block]], BLK_8X8);
  765. break;
  766. }
  767. }
  768. for(block=0;block<4;block++) {
  769. if(sub_type[block] == B_SUB_BWD)
  770. ff_cavs_mv(h, mv_scan[block]+MV_BWD_OFFS,
  771. mv_scan[block]+MV_BWD_OFFS-3,
  772. MV_PRED_MEDIAN, BLK_8X8, 0);
  773. }
  774. break;
  775. default:
  776. if (mb_type <= B_SYM_16X16) {
  777. av_log(h->s.avctx, AV_LOG_ERROR, "Invalid mb_type %d in B frame\n", mb_type);
  778. return AVERROR_INVALIDDATA;
  779. }
  780. av_assert2(mb_type < B_8X8);
  781. flags = ff_cavs_partition_flags[mb_type];
  782. if(mb_type & 1) { /* 16x8 macroblock types */
  783. if(flags & FWD0)
  784. ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_TOP, BLK_16X8, 1);
  785. if(flags & SYM0)
  786. mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_16X8);
  787. if(flags & FWD1)
  788. ff_cavs_mv(h, MV_FWD_X2, MV_FWD_A1, MV_PRED_LEFT, BLK_16X8, 1);
  789. if(flags & SYM1)
  790. mv_pred_sym(h, &h->mv[MV_FWD_X2], BLK_16X8);
  791. if(flags & BWD0)
  792. ff_cavs_mv(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_TOP, BLK_16X8, 0);
  793. if(flags & BWD1)
  794. ff_cavs_mv(h, MV_BWD_X2, MV_BWD_A1, MV_PRED_LEFT, BLK_16X8, 0);
  795. } else { /* 8x16 macroblock types */
  796. if(flags & FWD0)
  797. ff_cavs_mv(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_LEFT, BLK_8X16, 1);
  798. if(flags & SYM0)
  799. mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_8X16);
  800. if(flags & FWD1)
  801. ff_cavs_mv(h,MV_FWD_X1,MV_FWD_C2,MV_PRED_TOPRIGHT,BLK_8X16,1);
  802. if(flags & SYM1)
  803. mv_pred_sym(h, &h->mv[MV_FWD_X1], BLK_8X16);
  804. if(flags & BWD0)
  805. ff_cavs_mv(h, MV_BWD_X0, MV_BWD_B3, MV_PRED_LEFT, BLK_8X16, 0);
  806. if(flags & BWD1)
  807. ff_cavs_mv(h,MV_BWD_X1,MV_BWD_C2,MV_PRED_TOPRIGHT,BLK_8X16,0);
  808. }
  809. }
  810. ff_cavs_inter(h, mb_type);
  811. set_intra_mode_default(h);
  812. if(mb_type != B_SKIP)
  813. decode_residual_inter(h);
  814. ff_cavs_filter(h,mb_type);
  815. return 0;
  816. }
  817. /*****************************************************************************
  818. *
  819. * slice level
  820. *
  821. ****************************************************************************/
  822. static inline int decode_slice_header(AVSContext *h, GetBitContext *gb) {
  823. if(h->stc > 0xAF)
  824. av_log(h->s.avctx, AV_LOG_ERROR, "unexpected start code 0x%02x\n", h->stc);
  825. if (h->stc >= h->mb_height)
  826. return -1;
  827. h->mby = h->stc;
  828. h->mbidx = h->mby*h->mb_width;
  829. /* mark top macroblocks as unavailable */
  830. h->flags &= ~(B_AVAIL|C_AVAIL);
  831. if((h->mby == 0) && (!h->qp_fixed)){
  832. h->qp_fixed = get_bits1(gb);
  833. h->qp = get_bits(gb,6);
  834. }
  835. /* inter frame or second slice can have weighting params */
  836. if((h->pic_type != AV_PICTURE_TYPE_I) || (!h->pic_structure && h->mby >= h->mb_width/2))
  837. if(get_bits1(gb)) { //slice_weighting_flag
  838. av_log(h->s.avctx, AV_LOG_ERROR,
  839. "weighted prediction not yet supported\n");
  840. }
  841. return 0;
  842. }
  843. static inline int check_for_slice(AVSContext *h) {
  844. GetBitContext *gb = &h->s.gb;
  845. int align;
  846. if(h->mbx)
  847. return 0;
  848. align = (-get_bits_count(gb)) & 7;
  849. /* check for stuffing byte */
  850. if(!align && (show_bits(gb,8) == 0x80))
  851. align = 8;
  852. if((show_bits_long(gb,24+align) & 0xFFFFFF) == 0x000001) {
  853. skip_bits_long(gb,24+align);
  854. h->stc = get_bits(gb,8);
  855. if (h->stc >= h->mb_height)
  856. return 0;
  857. decode_slice_header(h,gb);
  858. return 1;
  859. }
  860. return 0;
  861. }
  862. /*****************************************************************************
  863. *
  864. * frame level
  865. *
  866. ****************************************************************************/
  867. static int decode_pic(AVSContext *h) {
  868. MpegEncContext *s = &h->s;
  869. int skip_count = -1;
  870. enum cavs_mb mb_type;
  871. if (!s->context_initialized) {
  872. if (ff_MPV_common_init(s) < 0)
  873. return -1;
  874. ff_init_scantable_permutation(s->dsp.idct_permutation,
  875. h->cdsp.idct_perm);
  876. ff_init_scantable(s->dsp.idct_permutation,&h->scantable,ff_zigzag_direct);
  877. }
  878. skip_bits(&s->gb,16);//bbv_dwlay
  879. if(h->stc == PIC_PB_START_CODE) {
  880. h->pic_type = get_bits(&s->gb,2) + AV_PICTURE_TYPE_I;
  881. if(h->pic_type > AV_PICTURE_TYPE_B) {
  882. av_log(s->avctx, AV_LOG_ERROR, "illegal picture type\n");
  883. return -1;
  884. }
  885. /* make sure we have the reference frames we need */
  886. if(!h->DPB[0].f.data[0] ||
  887. (!h->DPB[1].f.data[0] && h->pic_type == AV_PICTURE_TYPE_B))
  888. return -1;
  889. } else {
  890. h->pic_type = AV_PICTURE_TYPE_I;
  891. if(get_bits1(&s->gb))
  892. skip_bits(&s->gb,24);//time_code
  893. /* old sample clips were all progressive and no low_delay,
  894. bump stream revision if detected otherwise */
  895. if (s->low_delay || !(show_bits(&s->gb,9) & 1))
  896. h->stream_revision = 1;
  897. /* similarly test top_field_first and repeat_first_field */
  898. else if(show_bits(&s->gb,11) & 3)
  899. h->stream_revision = 1;
  900. if(h->stream_revision > 0)
  901. skip_bits(&s->gb,1); //marker_bit
  902. }
  903. /* release last B frame */
  904. if(h->picture.f.data[0])
  905. s->avctx->release_buffer(s->avctx, &h->picture.f);
  906. s->avctx->get_buffer(s->avctx, &h->picture.f);
  907. ff_cavs_init_pic(h);
  908. h->picture.poc = get_bits(&s->gb,8)*2;
  909. /* get temporal distances and MV scaling factors */
  910. if(h->pic_type != AV_PICTURE_TYPE_B) {
  911. h->dist[0] = (h->picture.poc - h->DPB[0].poc + 512) % 512;
  912. } else {
  913. h->dist[0] = (h->DPB[0].poc - h->picture.poc + 512) % 512;
  914. }
  915. h->dist[1] = (h->picture.poc - h->DPB[1].poc + 512) % 512;
  916. h->scale_den[0] = h->dist[0] ? 512/h->dist[0] : 0;
  917. h->scale_den[1] = h->dist[1] ? 512/h->dist[1] : 0;
  918. if(h->pic_type == AV_PICTURE_TYPE_B) {
  919. h->sym_factor = h->dist[0]*h->scale_den[1];
  920. } else {
  921. h->direct_den[0] = h->dist[0] ? 16384/h->dist[0] : 0;
  922. h->direct_den[1] = h->dist[1] ? 16384/h->dist[1] : 0;
  923. }
  924. if(s->low_delay)
  925. get_ue_golomb(&s->gb); //bbv_check_times
  926. h->progressive = get_bits1(&s->gb);
  927. h->pic_structure = 1;
  928. if(!h->progressive)
  929. h->pic_structure = get_bits1(&s->gb);
  930. if(!h->pic_structure && h->stc == PIC_PB_START_CODE)
  931. skip_bits1(&s->gb); //advanced_pred_mode_disable
  932. skip_bits1(&s->gb); //top_field_first
  933. skip_bits1(&s->gb); //repeat_first_field
  934. h->qp_fixed = get_bits1(&s->gb);
  935. h->qp = get_bits(&s->gb,6);
  936. if(h->pic_type == AV_PICTURE_TYPE_I) {
  937. if(!h->progressive && !h->pic_structure)
  938. skip_bits1(&s->gb);//what is this?
  939. skip_bits(&s->gb,4); //reserved bits
  940. } else {
  941. if(!(h->pic_type == AV_PICTURE_TYPE_B && h->pic_structure == 1))
  942. h->ref_flag = get_bits1(&s->gb);
  943. skip_bits(&s->gb,4); //reserved bits
  944. h->skip_mode_flag = get_bits1(&s->gb);
  945. }
  946. h->loop_filter_disable = get_bits1(&s->gb);
  947. if(!h->loop_filter_disable && get_bits1(&s->gb)) {
  948. h->alpha_offset = get_se_golomb(&s->gb);
  949. h->beta_offset = get_se_golomb(&s->gb);
  950. } else {
  951. h->alpha_offset = h->beta_offset = 0;
  952. }
  953. if(h->pic_type == AV_PICTURE_TYPE_I) {
  954. do {
  955. check_for_slice(h);
  956. decode_mb_i(h, 0);
  957. } while(ff_cavs_next_mb(h));
  958. } else if(h->pic_type == AV_PICTURE_TYPE_P) {
  959. do {
  960. if(check_for_slice(h))
  961. skip_count = -1;
  962. if(h->skip_mode_flag && (skip_count < 0))
  963. skip_count = get_ue_golomb(&s->gb);
  964. if(h->skip_mode_flag && skip_count--) {
  965. decode_mb_p(h,P_SKIP);
  966. } else {
  967. mb_type = get_ue_golomb(&s->gb) + P_SKIP + h->skip_mode_flag;
  968. if(mb_type > P_8X8)
  969. decode_mb_i(h, mb_type - P_8X8 - 1);
  970. else
  971. decode_mb_p(h,mb_type);
  972. }
  973. } while(ff_cavs_next_mb(h));
  974. } else { /* AV_PICTURE_TYPE_B */
  975. do {
  976. if(check_for_slice(h))
  977. skip_count = -1;
  978. if(h->skip_mode_flag && (skip_count < 0))
  979. skip_count = get_ue_golomb(&s->gb);
  980. if(h->skip_mode_flag && skip_count--) {
  981. decode_mb_b(h,B_SKIP);
  982. } else {
  983. mb_type = get_ue_golomb(&s->gb) + B_SKIP + h->skip_mode_flag;
  984. if(mb_type > B_8X8)
  985. decode_mb_i(h, mb_type - B_8X8 - 1);
  986. else
  987. decode_mb_b(h,mb_type);
  988. }
  989. } while(ff_cavs_next_mb(h));
  990. }
  991. if(h->pic_type != AV_PICTURE_TYPE_B) {
  992. if(h->DPB[1].f.data[0])
  993. s->avctx->release_buffer(s->avctx, &h->DPB[1].f);
  994. h->DPB[1] = h->DPB[0];
  995. h->DPB[0] = h->picture;
  996. memset(&h->picture,0,sizeof(Picture));
  997. }
  998. return 0;
  999. }
  1000. /*****************************************************************************
  1001. *
  1002. * headers and interface
  1003. *
  1004. ****************************************************************************/
  1005. static int decode_seq_header(AVSContext *h) {
  1006. MpegEncContext *s = &h->s;
  1007. int frame_rate_code;
  1008. int width, height;
  1009. h->profile = get_bits(&s->gb,8);
  1010. h->level = get_bits(&s->gb,8);
  1011. skip_bits1(&s->gb); //progressive sequence
  1012. width = get_bits(&s->gb, 14);
  1013. height = get_bits(&s->gb, 14);
  1014. if ((s->width || s->height) && (s->width != width || s->height != height)) {
  1015. av_log_missing_feature(s, "Width/height changing in CAVS", 0);
  1016. return AVERROR_PATCHWELCOME;
  1017. }
  1018. if (width <= 0 || height <= 0) {
  1019. av_log(s, AV_LOG_ERROR, "Dimensions invalid\n");
  1020. return AVERROR_INVALIDDATA;
  1021. }
  1022. s->width = width;
  1023. s->height = height;
  1024. skip_bits(&s->gb,2); //chroma format
  1025. skip_bits(&s->gb,3); //sample_precision
  1026. h->aspect_ratio = get_bits(&s->gb,4);
  1027. frame_rate_code = get_bits(&s->gb,4);
  1028. skip_bits(&s->gb,18);//bit_rate_lower
  1029. skip_bits1(&s->gb); //marker_bit
  1030. skip_bits(&s->gb,12);//bit_rate_upper
  1031. s->low_delay = get_bits1(&s->gb);
  1032. h->mb_width = (s->width + 15) >> 4;
  1033. h->mb_height = (s->height + 15) >> 4;
  1034. h->s.avctx->time_base.den = avpriv_frame_rate_tab[frame_rate_code].num;
  1035. h->s.avctx->time_base.num = avpriv_frame_rate_tab[frame_rate_code].den;
  1036. h->s.avctx->width = s->width;
  1037. h->s.avctx->height = s->height;
  1038. if(!h->top_qp)
  1039. ff_cavs_init_top_lines(h);
  1040. return 0;
  1041. }
  1042. static void cavs_flush(AVCodecContext * avctx) {
  1043. AVSContext *h = avctx->priv_data;
  1044. h->got_keyframe = 0;
  1045. }
  1046. static int cavs_decode_frame(AVCodecContext * avctx,void *data, int *data_size,
  1047. AVPacket *avpkt) {
  1048. const uint8_t *buf = avpkt->data;
  1049. int buf_size = avpkt->size;
  1050. AVSContext *h = avctx->priv_data;
  1051. MpegEncContext *s = &h->s;
  1052. int input_size;
  1053. const uint8_t *buf_end;
  1054. const uint8_t *buf_ptr;
  1055. AVFrame *picture = data;
  1056. uint32_t stc = -1;
  1057. s->avctx = avctx;
  1058. if (buf_size == 0) {
  1059. if (!s->low_delay && h->DPB[0].f.data[0]) {
  1060. *data_size = sizeof(AVPicture);
  1061. *picture = h->DPB[0].f;
  1062. memset(&h->DPB[0], 0, sizeof(h->DPB[0]));
  1063. }
  1064. return 0;
  1065. }
  1066. buf_ptr = buf;
  1067. buf_end = buf + buf_size;
  1068. for(;;) {
  1069. buf_ptr = avpriv_mpv_find_start_code(buf_ptr,buf_end, &stc);
  1070. if((stc & 0xFFFFFE00) || buf_ptr == buf_end)
  1071. return FFMAX(0, buf_ptr - buf - s->parse_context.last_index);
  1072. input_size = (buf_end - buf_ptr)*8;
  1073. switch(stc) {
  1074. case CAVS_START_CODE:
  1075. init_get_bits(&s->gb, buf_ptr, input_size);
  1076. decode_seq_header(h);
  1077. break;
  1078. case PIC_I_START_CODE:
  1079. if(!h->got_keyframe) {
  1080. if(h->DPB[0].f.data[0])
  1081. avctx->release_buffer(avctx, &h->DPB[0].f);
  1082. if(h->DPB[1].f.data[0])
  1083. avctx->release_buffer(avctx, &h->DPB[1].f);
  1084. h->got_keyframe = 1;
  1085. }
  1086. case PIC_PB_START_CODE:
  1087. *data_size = 0;
  1088. if(!h->got_keyframe)
  1089. break;
  1090. if(!h->top_qp)
  1091. break;
  1092. init_get_bits(&s->gb, buf_ptr, input_size);
  1093. h->stc = stc;
  1094. if(decode_pic(h))
  1095. break;
  1096. *data_size = sizeof(AVPicture);
  1097. if(h->pic_type != AV_PICTURE_TYPE_B) {
  1098. if(h->DPB[1].f.data[0]) {
  1099. *picture = h->DPB[1].f;
  1100. } else {
  1101. *data_size = 0;
  1102. }
  1103. } else
  1104. *picture = h->picture.f;
  1105. break;
  1106. case EXT_START_CODE:
  1107. //mpeg_decode_extension(avctx,buf_ptr, input_size);
  1108. break;
  1109. case USER_START_CODE:
  1110. //mpeg_decode_user_data(avctx,buf_ptr, input_size);
  1111. break;
  1112. default:
  1113. if (stc <= SLICE_MAX_START_CODE) {
  1114. init_get_bits(&s->gb, buf_ptr, input_size);
  1115. decode_slice_header(h, &s->gb);
  1116. }
  1117. break;
  1118. }
  1119. }
  1120. }
  1121. AVCodec ff_cavs_decoder = {
  1122. .name = "cavs",
  1123. .type = AVMEDIA_TYPE_VIDEO,
  1124. .id = AV_CODEC_ID_CAVS,
  1125. .priv_data_size = sizeof(AVSContext),
  1126. .init = ff_cavs_init,
  1127. .close = ff_cavs_end,
  1128. .decode = cavs_decode_frame,
  1129. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY,
  1130. .flush = cavs_flush,
  1131. .long_name = NULL_IF_CONFIG_SMALL("Chinese AVS (Audio Video Standard) (AVS1-P2, JiZhun profile)"),
  1132. };