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.

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