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.

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