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.

1269 lines
56KB

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