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.

1206 lines
54KB

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