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.

1322 lines
58KB

  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. unsigned 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, ptrdiff_t 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 int decode_residual_chroma(AVSContext *h)
  568. {
  569. if (h->cbp & (1 << 4)) {
  570. int ret = decode_residual_block(h, &h->gb, chroma_dec, 0,
  571. ff_cavs_chroma_qp[h->qp], h->cu, h->c_stride);
  572. if (ret < 0)
  573. return ret;
  574. }
  575. if (h->cbp & (1 << 5)) {
  576. int ret = decode_residual_block(h, &h->gb, chroma_dec, 0,
  577. ff_cavs_chroma_qp[h->qp], h->cv, h->c_stride);
  578. if (ret < 0)
  579. return ret;
  580. }
  581. return 0;
  582. }
  583. static inline int decode_residual_inter(AVSContext *h)
  584. {
  585. int block;
  586. /* get coded block pattern */
  587. int cbp = get_ue_golomb(&h->gb);
  588. if (cbp > 63U) {
  589. av_log(h->avctx, AV_LOG_ERROR, "illegal inter cbp %d\n", cbp);
  590. return AVERROR_INVALIDDATA;
  591. }
  592. h->cbp = cbp_tab[cbp][1];
  593. /* get quantizer */
  594. if (h->cbp && !h->qp_fixed)
  595. h->qp = (h->qp + (unsigned)get_se_golomb(&h->gb)) & 63;
  596. for (block = 0; block < 4; block++)
  597. if (h->cbp & (1 << block))
  598. decode_residual_block(h, &h->gb, inter_dec, 0, h->qp,
  599. h->cy + h->luma_scan[block], h->l_stride);
  600. decode_residual_chroma(h);
  601. return 0;
  602. }
  603. /*****************************************************************************
  604. *
  605. * macroblock level
  606. *
  607. ****************************************************************************/
  608. static inline void set_mv_intra(AVSContext *h)
  609. {
  610. h->mv[MV_FWD_X0] = ff_cavs_intra_mv;
  611. set_mvs(&h->mv[MV_FWD_X0], BLK_16X16);
  612. h->mv[MV_BWD_X0] = ff_cavs_intra_mv;
  613. set_mvs(&h->mv[MV_BWD_X0], BLK_16X16);
  614. if (h->cur.f->pict_type != AV_PICTURE_TYPE_B)
  615. h->col_type_base[h->mbidx] = I_8X8;
  616. }
  617. static int decode_mb_i(AVSContext *h, int cbp_code)
  618. {
  619. GetBitContext *gb = &h->gb;
  620. unsigned pred_mode_uv;
  621. int block;
  622. uint8_t top[18];
  623. uint8_t *left = NULL;
  624. uint8_t *d;
  625. int ret;
  626. ff_cavs_init_mb(h);
  627. /* get intra prediction modes from stream */
  628. for (block = 0; block < 4; block++) {
  629. int nA, nB, predpred;
  630. int pos = scan3x3[block];
  631. nA = h->pred_mode_Y[pos - 1];
  632. nB = h->pred_mode_Y[pos - 3];
  633. predpred = FFMIN(nA, nB);
  634. if (predpred == NOT_AVAIL) // if either is not available
  635. predpred = INTRA_L_LP;
  636. if (!get_bits1(gb)) {
  637. int rem_mode = get_bits(gb, 2);
  638. predpred = rem_mode + (rem_mode >= predpred);
  639. }
  640. h->pred_mode_Y[pos] = predpred;
  641. }
  642. pred_mode_uv = get_ue_golomb(gb);
  643. if (pred_mode_uv > 6) {
  644. av_log(h->avctx, AV_LOG_ERROR, "illegal intra chroma pred mode\n");
  645. return AVERROR_INVALIDDATA;
  646. }
  647. ff_cavs_modify_mb_i(h, &pred_mode_uv);
  648. /* get coded block pattern */
  649. if (h->cur.f->pict_type == AV_PICTURE_TYPE_I)
  650. cbp_code = get_ue_golomb(gb);
  651. if (cbp_code > 63U) {
  652. av_log(h->avctx, AV_LOG_ERROR, "illegal intra cbp\n");
  653. return AVERROR_INVALIDDATA;
  654. }
  655. h->cbp = cbp_tab[cbp_code][0];
  656. if (h->cbp && !h->qp_fixed)
  657. h->qp = (h->qp + (unsigned)get_se_golomb(gb)) & 63; //qp_delta
  658. /* luma intra prediction interleaved with residual decode/transform/add */
  659. for (block = 0; block < 4; block++) {
  660. d = h->cy + h->luma_scan[block];
  661. ff_cavs_load_intra_pred_luma(h, top, &left, block);
  662. h->intra_pred_l[h->pred_mode_Y[scan3x3[block]]]
  663. (d, top, left, h->l_stride);
  664. if (h->cbp & (1<<block)) {
  665. ret = decode_residual_block(h, gb, intra_dec, 1, h->qp, d, h->l_stride);
  666. if (ret < 0)
  667. return ret;
  668. }
  669. }
  670. /* chroma intra prediction */
  671. ff_cavs_load_intra_pred_chroma(h);
  672. h->intra_pred_c[pred_mode_uv](h->cu, &h->top_border_u[h->mbx * 10],
  673. h->left_border_u, h->c_stride);
  674. h->intra_pred_c[pred_mode_uv](h->cv, &h->top_border_v[h->mbx * 10],
  675. h->left_border_v, h->c_stride);
  676. ret = decode_residual_chroma(h);
  677. if (ret < 0)
  678. return ret;
  679. ff_cavs_filter(h, I_8X8);
  680. set_mv_intra(h);
  681. return 0;
  682. }
  683. static inline void set_intra_mode_default(AVSContext *h)
  684. {
  685. if (h->stream_revision > 0) {
  686. h->pred_mode_Y[3] = h->pred_mode_Y[6] = NOT_AVAIL;
  687. h->top_pred_Y[h->mbx * 2 + 0] = h->top_pred_Y[h->mbx * 2 + 1] = NOT_AVAIL;
  688. } else {
  689. h->pred_mode_Y[3] = h->pred_mode_Y[6] = INTRA_L_LP;
  690. h->top_pred_Y[h->mbx * 2 + 0] = h->top_pred_Y[h->mbx * 2 + 1] = INTRA_L_LP;
  691. }
  692. }
  693. static void decode_mb_p(AVSContext *h, enum cavs_mb mb_type)
  694. {
  695. GetBitContext *gb = &h->gb;
  696. int ref[4];
  697. ff_cavs_init_mb(h);
  698. switch (mb_type) {
  699. case P_SKIP:
  700. ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_PSKIP, BLK_16X16, 0);
  701. break;
  702. case P_16X16:
  703. ref[0] = h->ref_flag ? 0 : get_bits1(gb);
  704. ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16, ref[0]);
  705. break;
  706. case P_16X8:
  707. ref[0] = h->ref_flag ? 0 : get_bits1(gb);
  708. ref[2] = h->ref_flag ? 0 : get_bits1(gb);
  709. ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_TOP, BLK_16X8, ref[0]);
  710. ff_cavs_mv(h, MV_FWD_X2, MV_FWD_A1, MV_PRED_LEFT, BLK_16X8, ref[2]);
  711. break;
  712. case P_8X16:
  713. ref[0] = h->ref_flag ? 0 : get_bits1(gb);
  714. ref[1] = h->ref_flag ? 0 : get_bits1(gb);
  715. ff_cavs_mv(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_LEFT, BLK_8X16, ref[0]);
  716. ff_cavs_mv(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_TOPRIGHT, BLK_8X16, ref[1]);
  717. break;
  718. case P_8X8:
  719. ref[0] = h->ref_flag ? 0 : get_bits1(gb);
  720. ref[1] = h->ref_flag ? 0 : get_bits1(gb);
  721. ref[2] = h->ref_flag ? 0 : get_bits1(gb);
  722. ref[3] = h->ref_flag ? 0 : get_bits1(gb);
  723. ff_cavs_mv(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_MEDIAN, BLK_8X8, ref[0]);
  724. ff_cavs_mv(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_MEDIAN, BLK_8X8, ref[1]);
  725. ff_cavs_mv(h, MV_FWD_X2, MV_FWD_X1, MV_PRED_MEDIAN, BLK_8X8, ref[2]);
  726. ff_cavs_mv(h, MV_FWD_X3, MV_FWD_X0, MV_PRED_MEDIAN, BLK_8X8, ref[3]);
  727. }
  728. ff_cavs_inter(h, mb_type);
  729. set_intra_mode_default(h);
  730. store_mvs(h);
  731. if (mb_type != P_SKIP)
  732. decode_residual_inter(h);
  733. ff_cavs_filter(h, mb_type);
  734. h->col_type_base[h->mbidx] = mb_type;
  735. }
  736. static int decode_mb_b(AVSContext *h, enum cavs_mb mb_type)
  737. {
  738. int block;
  739. enum cavs_sub_mb sub_type[4];
  740. int flags;
  741. ff_cavs_init_mb(h);
  742. /* reset all MVs */
  743. h->mv[MV_FWD_X0] = ff_cavs_dir_mv;
  744. set_mvs(&h->mv[MV_FWD_X0], BLK_16X16);
  745. h->mv[MV_BWD_X0] = ff_cavs_dir_mv;
  746. set_mvs(&h->mv[MV_BWD_X0], BLK_16X16);
  747. switch (mb_type) {
  748. case B_SKIP:
  749. case B_DIRECT:
  750. if (!h->col_type_base[h->mbidx]) {
  751. /* intra MB at co-location, do in-plane prediction */
  752. ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_BSKIP, BLK_16X16, 1);
  753. ff_cavs_mv(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_BSKIP, BLK_16X16, 0);
  754. } else
  755. /* direct prediction from co-located P MB, block-wise */
  756. for (block = 0; block < 4; block++)
  757. mv_pred_direct(h, &h->mv[mv_scan[block]],
  758. &h->col_mv[h->mbidx * 4 + block]);
  759. break;
  760. case B_FWD_16X16:
  761. ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16, 1);
  762. break;
  763. case B_SYM_16X16:
  764. ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16, 1);
  765. mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_16X16);
  766. break;
  767. case B_BWD_16X16:
  768. ff_cavs_mv(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_MEDIAN, BLK_16X16, 0);
  769. break;
  770. case B_8X8:
  771. #define TMP_UNUSED_INX 7
  772. flags = 0;
  773. for (block = 0; block < 4; block++)
  774. sub_type[block] = get_bits(&h->gb, 2);
  775. for (block = 0; block < 4; block++) {
  776. switch (sub_type[block]) {
  777. case B_SUB_DIRECT:
  778. if (!h->col_type_base[h->mbidx]) {
  779. /* intra MB at co-location, do in-plane prediction */
  780. if(flags==0) {
  781. // if col-MB is a Intra MB, current Block size is 16x16.
  782. // AVS standard section 9.9.1
  783. if(block>0){
  784. h->mv[TMP_UNUSED_INX ] = h->mv[MV_FWD_X0 ];
  785. h->mv[TMP_UNUSED_INX + MV_BWD_OFFS] = h->mv[MV_FWD_X0 + MV_BWD_OFFS];
  786. }
  787. ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2,
  788. MV_PRED_BSKIP, BLK_8X8, 1);
  789. ff_cavs_mv(h, MV_FWD_X0+MV_BWD_OFFS,
  790. MV_FWD_C2+MV_BWD_OFFS,
  791. MV_PRED_BSKIP, BLK_8X8, 0);
  792. if(block>0) {
  793. flags = mv_scan[block];
  794. h->mv[flags ] = h->mv[MV_FWD_X0 ];
  795. h->mv[flags + MV_BWD_OFFS] = h->mv[MV_FWD_X0 + MV_BWD_OFFS];
  796. h->mv[MV_FWD_X0 ] = h->mv[TMP_UNUSED_INX ];
  797. h->mv[MV_FWD_X0 + MV_BWD_OFFS] = h->mv[TMP_UNUSED_INX + MV_BWD_OFFS];
  798. } else
  799. flags = MV_FWD_X0;
  800. } else {
  801. h->mv[mv_scan[block] ] = h->mv[flags ];
  802. h->mv[mv_scan[block] + MV_BWD_OFFS] = h->mv[flags + MV_BWD_OFFS];
  803. }
  804. } else
  805. mv_pred_direct(h, &h->mv[mv_scan[block]],
  806. &h->col_mv[h->mbidx * 4 + block]);
  807. break;
  808. case B_SUB_FWD:
  809. ff_cavs_mv(h, mv_scan[block], mv_scan[block] - 3,
  810. MV_PRED_MEDIAN, BLK_8X8, 1);
  811. break;
  812. case B_SUB_SYM:
  813. ff_cavs_mv(h, mv_scan[block], mv_scan[block] - 3,
  814. MV_PRED_MEDIAN, BLK_8X8, 1);
  815. mv_pred_sym(h, &h->mv[mv_scan[block]], BLK_8X8);
  816. break;
  817. }
  818. }
  819. #undef TMP_UNUSED_INX
  820. for (block = 0; block < 4; block++) {
  821. if (sub_type[block] == B_SUB_BWD)
  822. ff_cavs_mv(h, mv_scan[block] + MV_BWD_OFFS,
  823. mv_scan[block] + MV_BWD_OFFS - 3,
  824. MV_PRED_MEDIAN, BLK_8X8, 0);
  825. }
  826. break;
  827. default:
  828. if (mb_type <= B_SYM_16X16) {
  829. av_log(h->avctx, AV_LOG_ERROR, "Invalid mb_type %d in B frame\n", mb_type);
  830. return AVERROR_INVALIDDATA;
  831. }
  832. av_assert2(mb_type < B_8X8);
  833. flags = ff_cavs_partition_flags[mb_type];
  834. if (mb_type & 1) { /* 16x8 macroblock types */
  835. if (flags & FWD0)
  836. ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_TOP, BLK_16X8, 1);
  837. if (flags & SYM0)
  838. mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_16X8);
  839. if (flags & FWD1)
  840. ff_cavs_mv(h, MV_FWD_X2, MV_FWD_A1, MV_PRED_LEFT, BLK_16X8, 1);
  841. if (flags & SYM1)
  842. mv_pred_sym(h, &h->mv[MV_FWD_X2], BLK_16X8);
  843. if (flags & BWD0)
  844. ff_cavs_mv(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_TOP, BLK_16X8, 0);
  845. if (flags & BWD1)
  846. ff_cavs_mv(h, MV_BWD_X2, MV_BWD_A1, MV_PRED_LEFT, BLK_16X8, 0);
  847. } else { /* 8x16 macroblock types */
  848. if (flags & FWD0)
  849. ff_cavs_mv(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_LEFT, BLK_8X16, 1);
  850. if (flags & SYM0)
  851. mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_8X16);
  852. if (flags & FWD1)
  853. ff_cavs_mv(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_TOPRIGHT, BLK_8X16, 1);
  854. if (flags & SYM1)
  855. mv_pred_sym(h, &h->mv[MV_FWD_X1], BLK_8X16);
  856. if (flags & BWD0)
  857. ff_cavs_mv(h, MV_BWD_X0, MV_BWD_B3, MV_PRED_LEFT, BLK_8X16, 0);
  858. if (flags & BWD1)
  859. ff_cavs_mv(h, MV_BWD_X1, MV_BWD_C2, MV_PRED_TOPRIGHT, BLK_8X16, 0);
  860. }
  861. }
  862. ff_cavs_inter(h, mb_type);
  863. set_intra_mode_default(h);
  864. if (mb_type != B_SKIP)
  865. decode_residual_inter(h);
  866. ff_cavs_filter(h, mb_type);
  867. return 0;
  868. }
  869. /*****************************************************************************
  870. *
  871. * slice level
  872. *
  873. ****************************************************************************/
  874. static inline int decode_slice_header(AVSContext *h, GetBitContext *gb)
  875. {
  876. if (h->stc > 0xAF)
  877. av_log(h->avctx, AV_LOG_ERROR, "unexpected start code 0x%02x\n", h->stc);
  878. if (h->stc >= h->mb_height) {
  879. av_log(h->avctx, AV_LOG_ERROR, "stc 0x%02x is too large\n", h->stc);
  880. return AVERROR_INVALIDDATA;
  881. }
  882. h->mby = h->stc;
  883. h->mbidx = h->mby * h->mb_width;
  884. /* mark top macroblocks as unavailable */
  885. h->flags &= ~(B_AVAIL | C_AVAIL);
  886. if (!h->pic_qp_fixed) {
  887. h->qp_fixed = get_bits1(gb);
  888. h->qp = get_bits(gb, 6);
  889. }
  890. /* inter frame or second slice can have weighting params */
  891. if ((h->cur.f->pict_type != AV_PICTURE_TYPE_I) ||
  892. (!h->pic_structure && h->mby >= h->mb_width / 2))
  893. if (get_bits1(gb)) { //slice_weighting_flag
  894. av_log(h->avctx, AV_LOG_ERROR,
  895. "weighted prediction not yet supported\n");
  896. }
  897. return 0;
  898. }
  899. static inline int check_for_slice(AVSContext *h)
  900. {
  901. GetBitContext *gb = &h->gb;
  902. int align;
  903. if (h->mbx)
  904. return 0;
  905. align = (-get_bits_count(gb)) & 7;
  906. /* check for stuffing byte */
  907. if (!align && (show_bits(gb, 8) == 0x80))
  908. align = 8;
  909. if ((show_bits_long(gb, 24 + align) & 0xFFFFFF) == 0x000001) {
  910. skip_bits_long(gb, 24 + align);
  911. h->stc = get_bits(gb, 8);
  912. if (h->stc >= h->mb_height)
  913. return 0;
  914. decode_slice_header(h, gb);
  915. return 1;
  916. }
  917. return 0;
  918. }
  919. /*****************************************************************************
  920. *
  921. * frame level
  922. *
  923. ****************************************************************************/
  924. static int decode_pic(AVSContext *h)
  925. {
  926. int ret;
  927. int skip_count = -1;
  928. enum cavs_mb mb_type;
  929. if (!h->top_qp) {
  930. av_log(h->avctx, AV_LOG_ERROR, "No sequence header decoded yet\n");
  931. return AVERROR_INVALIDDATA;
  932. }
  933. av_frame_unref(h->cur.f);
  934. skip_bits(&h->gb, 16);//bbv_dwlay
  935. if (h->stc == PIC_PB_START_CODE) {
  936. h->cur.f->pict_type = get_bits(&h->gb, 2) + AV_PICTURE_TYPE_I;
  937. if (h->cur.f->pict_type > AV_PICTURE_TYPE_B) {
  938. av_log(h->avctx, AV_LOG_ERROR, "illegal picture type\n");
  939. return AVERROR_INVALIDDATA;
  940. }
  941. /* make sure we have the reference frames we need */
  942. if (!h->DPB[0].f->data[0] ||
  943. (!h->DPB[1].f->data[0] && h->cur.f->pict_type == AV_PICTURE_TYPE_B))
  944. return AVERROR_INVALIDDATA;
  945. } else {
  946. h->cur.f->pict_type = AV_PICTURE_TYPE_I;
  947. if (get_bits1(&h->gb))
  948. skip_bits(&h->gb, 24);//time_code
  949. /* old sample clips were all progressive and no low_delay,
  950. bump stream revision if detected otherwise */
  951. if (h->low_delay || !(show_bits(&h->gb, 9) & 1))
  952. h->stream_revision = 1;
  953. /* similarly test top_field_first and repeat_first_field */
  954. else if (show_bits(&h->gb, 11) & 3)
  955. h->stream_revision = 1;
  956. if (h->stream_revision > 0)
  957. skip_bits(&h->gb, 1); //marker_bit
  958. }
  959. ret = ff_get_buffer(h->avctx, h->cur.f, h->cur.f->pict_type == AV_PICTURE_TYPE_B ?
  960. 0 : AV_GET_BUFFER_FLAG_REF);
  961. if (ret < 0)
  962. return ret;
  963. if (!h->edge_emu_buffer) {
  964. int alloc_size = FFALIGN(FFABS(h->cur.f->linesize[0]) + 32, 32);
  965. h->edge_emu_buffer = av_mallocz(alloc_size * 2 * 24);
  966. if (!h->edge_emu_buffer)
  967. return AVERROR(ENOMEM);
  968. }
  969. if ((ret = ff_cavs_init_pic(h)) < 0)
  970. return ret;
  971. h->cur.poc = get_bits(&h->gb, 8) * 2;
  972. /* get temporal distances and MV scaling factors */
  973. if (h->cur.f->pict_type != AV_PICTURE_TYPE_B) {
  974. h->dist[0] = (h->cur.poc - h->DPB[0].poc) & 511;
  975. } else {
  976. h->dist[0] = (h->DPB[0].poc - h->cur.poc) & 511;
  977. }
  978. h->dist[1] = (h->cur.poc - h->DPB[1].poc) & 511;
  979. h->scale_den[0] = h->dist[0] ? 512/h->dist[0] : 0;
  980. h->scale_den[1] = h->dist[1] ? 512/h->dist[1] : 0;
  981. if (h->cur.f->pict_type == AV_PICTURE_TYPE_B) {
  982. h->sym_factor = h->dist[0] * h->scale_den[1];
  983. if (FFABS(h->sym_factor) > 32768) {
  984. av_log(h->avctx, AV_LOG_ERROR, "sym_factor %d too large\n", h->sym_factor);
  985. return AVERROR_INVALIDDATA;
  986. }
  987. } else {
  988. h->direct_den[0] = h->dist[0] ? 16384 / h->dist[0] : 0;
  989. h->direct_den[1] = h->dist[1] ? 16384 / h->dist[1] : 0;
  990. }
  991. if (h->low_delay)
  992. get_ue_golomb(&h->gb); //bbv_check_times
  993. h->progressive = get_bits1(&h->gb);
  994. h->pic_structure = 1;
  995. if (!h->progressive)
  996. h->pic_structure = get_bits1(&h->gb);
  997. if (!h->pic_structure && h->stc == PIC_PB_START_CODE)
  998. skip_bits1(&h->gb); //advanced_pred_mode_disable
  999. skip_bits1(&h->gb); //top_field_first
  1000. skip_bits1(&h->gb); //repeat_first_field
  1001. h->pic_qp_fixed =
  1002. h->qp_fixed = get_bits1(&h->gb);
  1003. h->qp = get_bits(&h->gb, 6);
  1004. if (h->cur.f->pict_type == AV_PICTURE_TYPE_I) {
  1005. if (!h->progressive && !h->pic_structure)
  1006. skip_bits1(&h->gb);//what is this?
  1007. skip_bits(&h->gb, 4); //reserved bits
  1008. } else {
  1009. if (!(h->cur.f->pict_type == AV_PICTURE_TYPE_B && h->pic_structure == 1))
  1010. h->ref_flag = get_bits1(&h->gb);
  1011. skip_bits(&h->gb, 4); //reserved bits
  1012. h->skip_mode_flag = get_bits1(&h->gb);
  1013. }
  1014. h->loop_filter_disable = get_bits1(&h->gb);
  1015. if (!h->loop_filter_disable && get_bits1(&h->gb)) {
  1016. h->alpha_offset = get_se_golomb(&h->gb);
  1017. h->beta_offset = get_se_golomb(&h->gb);
  1018. if ( h->alpha_offset < -64 || h->alpha_offset > 64
  1019. || h-> beta_offset < -64 || h-> beta_offset > 64) {
  1020. h->alpha_offset = h->beta_offset = 0;
  1021. return AVERROR_INVALIDDATA;
  1022. }
  1023. } else {
  1024. h->alpha_offset = h->beta_offset = 0;
  1025. }
  1026. ret = 0;
  1027. if (h->cur.f->pict_type == AV_PICTURE_TYPE_I) {
  1028. do {
  1029. check_for_slice(h);
  1030. ret = decode_mb_i(h, 0);
  1031. if (ret < 0)
  1032. break;
  1033. } while (ff_cavs_next_mb(h));
  1034. } else if (h->cur.f->pict_type == AV_PICTURE_TYPE_P) {
  1035. do {
  1036. if (check_for_slice(h))
  1037. skip_count = -1;
  1038. if (h->skip_mode_flag && (skip_count < 0)) {
  1039. if (get_bits_left(&h->gb) < 1) {
  1040. ret = AVERROR_INVALIDDATA;
  1041. break;
  1042. }
  1043. skip_count = get_ue_golomb(&h->gb);
  1044. }
  1045. if (h->skip_mode_flag && skip_count--) {
  1046. decode_mb_p(h, P_SKIP);
  1047. } else {
  1048. if (get_bits_left(&h->gb) < 1) {
  1049. ret = AVERROR_INVALIDDATA;
  1050. break;
  1051. }
  1052. mb_type = get_ue_golomb(&h->gb) + P_SKIP + h->skip_mode_flag;
  1053. if (mb_type > P_8X8)
  1054. ret = decode_mb_i(h, mb_type - P_8X8 - 1);
  1055. else
  1056. decode_mb_p(h, mb_type);
  1057. }
  1058. if (ret < 0)
  1059. break;
  1060. } while (ff_cavs_next_mb(h));
  1061. } else { /* AV_PICTURE_TYPE_B */
  1062. do {
  1063. if (check_for_slice(h))
  1064. skip_count = -1;
  1065. if (h->skip_mode_flag && (skip_count < 0)) {
  1066. if (get_bits_left(&h->gb) < 1) {
  1067. ret = AVERROR_INVALIDDATA;
  1068. break;
  1069. }
  1070. skip_count = get_ue_golomb(&h->gb);
  1071. }
  1072. if (h->skip_mode_flag && skip_count--) {
  1073. ret = decode_mb_b(h, B_SKIP);
  1074. } else {
  1075. if (get_bits_left(&h->gb) < 1) {
  1076. ret = AVERROR_INVALIDDATA;
  1077. break;
  1078. }
  1079. mb_type = get_ue_golomb(&h->gb) + B_SKIP + h->skip_mode_flag;
  1080. if (mb_type > B_8X8)
  1081. ret = decode_mb_i(h, mb_type - B_8X8 - 1);
  1082. else
  1083. ret = decode_mb_b(h, mb_type);
  1084. }
  1085. if (ret < 0)
  1086. break;
  1087. } while (ff_cavs_next_mb(h));
  1088. }
  1089. emms_c();
  1090. if (ret >= 0 && h->cur.f->pict_type != AV_PICTURE_TYPE_B) {
  1091. av_frame_unref(h->DPB[1].f);
  1092. FFSWAP(AVSFrame, h->cur, h->DPB[1]);
  1093. FFSWAP(AVSFrame, h->DPB[0], h->DPB[1]);
  1094. }
  1095. return ret;
  1096. }
  1097. /*****************************************************************************
  1098. *
  1099. * headers and interface
  1100. *
  1101. ****************************************************************************/
  1102. static int decode_seq_header(AVSContext *h)
  1103. {
  1104. int frame_rate_code;
  1105. int width, height;
  1106. int ret;
  1107. h->profile = get_bits(&h->gb, 8);
  1108. h->level = get_bits(&h->gb, 8);
  1109. skip_bits1(&h->gb); //progressive sequence
  1110. width = get_bits(&h->gb, 14);
  1111. height = get_bits(&h->gb, 14);
  1112. if ((h->width || h->height) && (h->width != width || h->height != height)) {
  1113. avpriv_report_missing_feature(h->avctx,
  1114. "Width/height changing in CAVS");
  1115. return AVERROR_PATCHWELCOME;
  1116. }
  1117. if (width <= 0 || height <= 0) {
  1118. av_log(h->avctx, AV_LOG_ERROR, "Dimensions invalid\n");
  1119. return AVERROR_INVALIDDATA;
  1120. }
  1121. skip_bits(&h->gb, 2); //chroma format
  1122. skip_bits(&h->gb, 3); //sample_precision
  1123. h->aspect_ratio = get_bits(&h->gb, 4);
  1124. frame_rate_code = get_bits(&h->gb, 4);
  1125. if (frame_rate_code == 0 || frame_rate_code > 13) {
  1126. av_log(h->avctx, AV_LOG_WARNING,
  1127. "frame_rate_code %d is invalid\n", frame_rate_code);
  1128. frame_rate_code = 1;
  1129. }
  1130. skip_bits(&h->gb, 18); //bit_rate_lower
  1131. skip_bits1(&h->gb); //marker_bit
  1132. skip_bits(&h->gb, 12); //bit_rate_upper
  1133. h->low_delay = get_bits1(&h->gb);
  1134. ret = ff_set_dimensions(h->avctx, width, height);
  1135. if (ret < 0)
  1136. return ret;
  1137. h->width = width;
  1138. h->height = height;
  1139. h->mb_width = (h->width + 15) >> 4;
  1140. h->mb_height = (h->height + 15) >> 4;
  1141. h->avctx->framerate = ff_mpeg12_frame_rate_tab[frame_rate_code];
  1142. if (!h->top_qp)
  1143. return ff_cavs_init_top_lines(h);
  1144. return 0;
  1145. }
  1146. static void cavs_flush(AVCodecContext * avctx)
  1147. {
  1148. AVSContext *h = avctx->priv_data;
  1149. h->got_keyframe = 0;
  1150. }
  1151. static int cavs_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  1152. AVPacket *avpkt)
  1153. {
  1154. AVSContext *h = avctx->priv_data;
  1155. const uint8_t *buf = avpkt->data;
  1156. int buf_size = avpkt->size;
  1157. uint32_t stc = -1;
  1158. int input_size, ret;
  1159. const uint8_t *buf_end;
  1160. const uint8_t *buf_ptr;
  1161. int frame_start = 0;
  1162. if (buf_size == 0) {
  1163. if (!h->low_delay && h->DPB[0].f->data[0]) {
  1164. *got_frame = 1;
  1165. av_frame_move_ref(data, h->DPB[0].f);
  1166. }
  1167. return 0;
  1168. }
  1169. h->stc = 0;
  1170. buf_ptr = buf;
  1171. buf_end = buf + buf_size;
  1172. for(;;) {
  1173. buf_ptr = avpriv_find_start_code(buf_ptr, buf_end, &stc);
  1174. if ((stc & 0xFFFFFE00) || buf_ptr == buf_end) {
  1175. if (!h->stc)
  1176. av_log(h->avctx, AV_LOG_WARNING, "no frame decoded\n");
  1177. return FFMAX(0, buf_ptr - buf);
  1178. }
  1179. input_size = (buf_end - buf_ptr) * 8;
  1180. switch (stc) {
  1181. case CAVS_START_CODE:
  1182. init_get_bits(&h->gb, buf_ptr, input_size);
  1183. decode_seq_header(h);
  1184. break;
  1185. case PIC_I_START_CODE:
  1186. if (!h->got_keyframe) {
  1187. av_frame_unref(h->DPB[0].f);
  1188. av_frame_unref(h->DPB[1].f);
  1189. h->got_keyframe = 1;
  1190. }
  1191. case PIC_PB_START_CODE:
  1192. if (frame_start > 1)
  1193. return AVERROR_INVALIDDATA;
  1194. frame_start ++;
  1195. if (*got_frame)
  1196. av_frame_unref(data);
  1197. *got_frame = 0;
  1198. if (!h->got_keyframe)
  1199. break;
  1200. init_get_bits(&h->gb, buf_ptr, input_size);
  1201. h->stc = stc;
  1202. if (decode_pic(h))
  1203. break;
  1204. *got_frame = 1;
  1205. if (h->cur.f->pict_type != AV_PICTURE_TYPE_B) {
  1206. if (h->DPB[!h->low_delay].f->data[0]) {
  1207. if ((ret = av_frame_ref(data, h->DPB[!h->low_delay].f)) < 0)
  1208. return ret;
  1209. } else {
  1210. *got_frame = 0;
  1211. }
  1212. } else {
  1213. av_frame_move_ref(data, h->cur.f);
  1214. }
  1215. break;
  1216. case EXT_START_CODE:
  1217. //mpeg_decode_extension(avctx, buf_ptr, input_size);
  1218. break;
  1219. case USER_START_CODE:
  1220. //mpeg_decode_user_data(avctx, buf_ptr, input_size);
  1221. break;
  1222. default:
  1223. if (stc <= SLICE_MAX_START_CODE) {
  1224. init_get_bits(&h->gb, buf_ptr, input_size);
  1225. decode_slice_header(h, &h->gb);
  1226. }
  1227. break;
  1228. }
  1229. }
  1230. }
  1231. AVCodec ff_cavs_decoder = {
  1232. .name = "cavs",
  1233. .long_name = NULL_IF_CONFIG_SMALL("Chinese AVS (Audio Video Standard) (AVS1-P2, JiZhun profile)"),
  1234. .type = AVMEDIA_TYPE_VIDEO,
  1235. .id = AV_CODEC_ID_CAVS,
  1236. .priv_data_size = sizeof(AVSContext),
  1237. .init = ff_cavs_init,
  1238. .close = ff_cavs_end,
  1239. .decode = cavs_decode_frame,
  1240. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY,
  1241. .flush = cavs_flush,
  1242. };