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.

1226 lines
54KB

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