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