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.

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