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.

1440 lines
44KB

  1. /*
  2. * MobiClip Video decoder
  3. * Copyright (c) 2017 Adib Surani
  4. * Copyright (c) 2020 Paul B Mahol
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include <inttypes.h>
  23. #include "libavutil/avassert.h"
  24. #include "avcodec.h"
  25. #include "bytestream.h"
  26. #include "bswapdsp.h"
  27. #include "get_bits.h"
  28. #include "golomb.h"
  29. #include "internal.h"
  30. static const uint8_t zigzag4x4_tab[] =
  31. {
  32. 0x00, 0x04, 0x01, 0x02, 0x05, 0x08, 0x0C, 0x09, 0x06, 0x03, 0x07, 0x0A,
  33. 0x0D, 0x0E, 0x0B, 0x0F
  34. };
  35. static const uint8_t quant4x4_tab[][16] =
  36. {
  37. { 10, 13, 13, 10, 16, 10, 13, 13, 13, 13, 16, 10, 16, 13, 13, 16 },
  38. { 11, 14, 14, 11, 18, 11, 14, 14, 14, 14, 18, 11, 18, 14, 14, 18 },
  39. { 13, 16, 16, 13, 20, 13, 16, 16, 16, 16, 20, 13, 20, 16, 16, 20 },
  40. { 14, 18, 18, 14, 23, 14, 18, 18, 18, 18, 23, 14, 23, 18, 18, 23 },
  41. { 16, 20, 20, 16, 25, 16, 20, 20, 20, 20, 25, 16, 25, 20, 20, 25 },
  42. { 18, 23, 23, 18, 29, 18, 23, 23, 23, 23, 29, 18, 29, 23, 23, 29 },
  43. };
  44. static const uint8_t quant8x8_tab[][64] =
  45. {
  46. { 20, 19, 19, 25, 18, 25, 19, 24, 24, 19, 20, 18, 32, 18, 20, 19, 19, 24, 24, 19, 19, 25, 18, 25, 18, 25, 18, 25, 19, 24, 24, 19,
  47. 19, 24, 24, 19, 18, 32, 18, 20, 18, 32, 18, 24, 24, 19, 19, 24, 24, 18, 25, 18, 25, 18, 19, 24, 24, 19, 18, 32, 18, 24, 24, 18,},
  48. { 22, 21, 21, 28, 19, 28, 21, 26, 26, 21, 22, 19, 35, 19, 22, 21, 21, 26, 26, 21, 21, 28, 19, 28, 19, 28, 19, 28, 21, 26, 26, 21,
  49. 21, 26, 26, 21, 19, 35, 19, 22, 19, 35, 19, 26, 26, 21, 21, 26, 26, 19, 28, 19, 28, 19, 21, 26, 26, 21, 19, 35, 19, 26, 26, 19,},
  50. { 26, 24, 24, 33, 23, 33, 24, 31, 31, 24, 26, 23, 42, 23, 26, 24, 24, 31, 31, 24, 24, 33, 23, 33, 23, 33, 23, 33, 24, 31, 31, 24,
  51. 24, 31, 31, 24, 23, 42, 23, 26, 23, 42, 23, 31, 31, 24, 24, 31, 31, 23, 33, 23, 33, 23, 24, 31, 31, 24, 23, 42, 23, 31, 31, 23,},
  52. { 28, 26, 26, 35, 25, 35, 26, 33, 33, 26, 28, 25, 45, 25, 28, 26, 26, 33, 33, 26, 26, 35, 25, 35, 25, 35, 25, 35, 26, 33, 33, 26,
  53. 26, 33, 33, 26, 25, 45, 25, 28, 25, 45, 25, 33, 33, 26, 26, 33, 33, 25, 35, 25, 35, 25, 26, 33, 33, 26, 25, 45, 25, 33, 33, 25,},
  54. { 32, 30, 30, 40, 28, 40, 30, 38, 38, 30, 32, 28, 51, 28, 32, 30, 30, 38, 38, 30, 30, 40, 28, 40, 28, 40, 28, 40, 30, 38, 38, 30,
  55. 30, 38, 38, 30, 28, 51, 28, 32, 28, 51, 28, 38, 38, 30, 30, 38, 38, 28, 40, 28, 40, 28, 30, 38, 38, 30, 28, 51, 28, 38, 38, 28,},
  56. { 36, 34, 34, 46, 32, 46, 34, 43, 43, 34, 36, 32, 58, 32, 36, 34, 34, 43, 43, 34, 34, 46, 32, 46, 32, 46, 32, 46, 34, 43, 43, 34,
  57. 34, 43, 43, 34, 32, 58, 32, 36, 32, 58, 32, 43, 43, 34, 34, 43, 43, 32, 46, 32, 46, 32, 34, 43, 43, 34, 32, 58, 32, 43, 43, 32,},
  58. };
  59. static const uint8_t block4x4_coefficients_tab[] =
  60. {
  61. 15, 0, 2, 1, 4, 8, 12, 3, 11, 13, 14, 7, 10, 5, 9, 6,
  62. };
  63. static const uint8_t pframe_block4x4_coefficients_tab[] =
  64. {
  65. 0, 4, 1, 8, 2, 12, 3, 5, 10, 15, 7, 13, 14, 11, 9, 6,
  66. };
  67. static const uint8_t block8x8_coefficients_tab[] =
  68. {
  69. 0x00, 0x1F, 0x3F, 0x0F, 0x08, 0x04, 0x02, 0x01, 0x0B, 0x0E, 0x1B, 0x0D,
  70. 0x03, 0x07, 0x0C, 0x17, 0x1D, 0x0A, 0x1E, 0x05, 0x10, 0x2F, 0x37, 0x3B,
  71. 0x13, 0x3D, 0x3E, 0x09, 0x1C, 0x06, 0x15, 0x1A, 0x33, 0x11, 0x12, 0x14,
  72. 0x18, 0x20, 0x3C, 0x35, 0x19, 0x16, 0x3A, 0x30, 0x31, 0x32, 0x27, 0x34,
  73. 0x2B, 0x2D, 0x39, 0x38, 0x23, 0x36, 0x2E, 0x21, 0x25, 0x22, 0x24, 0x2C,
  74. 0x2A, 0x28, 0x29, 0x26,
  75. };
  76. static const uint8_t pframe_block8x8_coefficients_tab[] =
  77. {
  78. 0x00, 0x0F, 0x04, 0x01, 0x08, 0x02, 0x0C, 0x03, 0x05, 0x0A, 0x0D, 0x07, 0x0E, 0x0B, 0x1F, 0x09,
  79. 0x06, 0x10, 0x3F, 0x1E, 0x17, 0x1D, 0x1B, 0x1C, 0x13, 0x18, 0x1A, 0x12, 0x11, 0x14, 0x15, 0x20,
  80. 0x2F, 0x16, 0x19, 0x37, 0x3D, 0x3E, 0x3B, 0x3C, 0x33, 0x35, 0x21, 0x24, 0x22, 0x28, 0x23, 0x2C,
  81. 0x30, 0x27, 0x2D, 0x25, 0x3A, 0x2B, 0x2E, 0x2A, 0x31, 0x34, 0x38, 0x32, 0x29, 0x26, 0x39, 0x36
  82. };
  83. static const uint8_t run_residue[2][256] =
  84. {
  85. {
  86. 12, 6, 4, 3, 3, 3, 3, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
  87. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  88. 3, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  89. 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  90. 1, 27, 11, 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, 1, 1, 1, 1,
  91. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  92. 1, 41, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  93. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  94. },
  95. {
  96. 27, 10, 5, 4, 3, 3, 3, 3, 2, 2, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  97. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  98. 8, 3, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  99. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  100. 1, 15, 10, 8, 4, 3, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  101. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  102. 1, 21, 7, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  103. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  104. },
  105. };
  106. static const uint8_t bits0[] = {
  107. 9, 11, 11, 11, 11, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
  108. 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12,
  109. 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 7, 10, 10, 9,
  110. 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
  111. 9, 9, 9, 9, 9, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  112. 8, 8, 8, 7, 7, 7, 7, 7, 7, 7, 7, 6, 6, 6, 6,
  113. 6, 6, 6, 6, 6, 6, 5, 5, 5, 4, 2, 3, 4, 4,
  114. };
  115. static const uint16_t codes0[] = {
  116. 0x0, 0x4, 0x5, 0x6, 0x7, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xA,
  117. 0xB, 0xC, 0xD, 0xE, 0xF, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25,
  118. 0x26, 0x27, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  119. 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, 0x3, 0x20,
  120. 0x21, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19,
  121. 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23,
  122. 0x24, 0x25, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A,
  123. 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x10, 0x11, 0x12, 0x13, 0x14,
  124. 0x15, 0x16, 0x17, 0xC, 0xD, 0xE, 0xF, 0x10, 0x11, 0x12,
  125. 0x13, 0x14, 0x15, 0xB, 0xC, 0xD, 0x7, 0x2, 0x6, 0xE, 0xF,
  126. };
  127. static const uint16_t syms0[] = {
  128. 0x0, 0x822, 0x803, 0xB, 0xA, 0xB81, 0xB61, 0xB41, 0xB21, 0x122,
  129. 0x102, 0xE2, 0xC2, 0xA2, 0x63, 0x43, 0x24, 0xC, 0x25, 0x2E1, 0x301,
  130. 0xBA1, 0xBC1, 0xBE1, 0xC01, 0x26, 0x44, 0x83, 0xA3, 0xC3, 0x142,
  131. 0x321, 0x341, 0xC21, 0xC41, 0xC61, 0xC81, 0xCA1, 0xCC1, 0xCE1, 0xD01,
  132. 0x0, 0x9, 0x8, 0xB01, 0xAE1, 0xAC1, 0xAA1, 0xA81, 0xA61, 0xA41, 0xA21,
  133. 0x802, 0x2C1, 0x2A1, 0x281, 0x261, 0x241, 0x221, 0x201, 0x1E1, 0x82,
  134. 0x62, 0x7, 0x6, 0xA01, 0x9E1, 0x9C1, 0x9A1, 0x981, 0x961, 0x941, 0x921,
  135. 0x1C1, 0x1A1, 0x42, 0x23, 0x5, 0x901, 0x8E1, 0x8C1, 0x8A1, 0x181, 0x161,
  136. 0x141, 0x4, 0x881, 0x861, 0x841, 0x821, 0x121, 0x101, 0xE1, 0xC1, 0x22,
  137. 0x3, 0xA1, 0x81, 0x61, 0x801, 0x1, 0x21, 0x41, 0x2,
  138. };
  139. static const uint16_t syms1[] = {
  140. 0x0, 0x807, 0x806, 0x16, 0x15, 0x842, 0x823, 0x805, 0x1A1, 0xA3, 0x102, 0x83,
  141. 0x64, 0x44, 0x27, 0x14, 0x13, 0x17, 0x18, 0x28, 0x122, 0x862, 0x882, 0x9E1, 0xA01,
  142. 0x19, 0x1A, 0x1B, 0x29, 0xC3, 0x2A, 0x45, 0xE3, 0x1C1, 0x808, 0x8A2, 0x8C2, 0xA21,
  143. 0xA41, 0xA61, 0xA81, 0x0, 0x12, 0x11, 0x9C1, 0x9A1, 0x981, 0x961, 0x941, 0x822, 0x804,
  144. 0x181, 0x161, 0xE2, 0xC2, 0xA2, 0x63, 0x43, 0x26, 0x25, 0x10, 0x82, 0xF, 0xE, 0xD, 0x901,
  145. 0x8E1, 0x8C1, 0x803, 0x141, 0x121, 0x101, 0x921, 0x62, 0x24, 0xC, 0xB, 0xA, 0x881, 0x861,
  146. 0xC1, 0x8A1, 0xE1, 0x42, 0x23, 0x9, 0x802, 0xA1, 0x841, 0x821, 0x81, 0x61, 0x8, 0x7, 0x22,
  147. 0x6, 0x41, 0x5, 0x4, 0x801, 0x1, 0x2, 0x21, 0x3,
  148. };
  149. static const uint8_t mv_len[16] =
  150. {
  151. 10, 8, 8, 7, 8, 8, 8, 7, 8, 8, 8, 7, 7, 7, 7, 6,
  152. };
  153. static const uint8_t mv_bits[16][10] =
  154. {
  155. { 1, 3, 3, 4, 4, 5, 5, 5, 6, 6 },
  156. { 2, 2, 3, 3, 3, 4, 5, 5 },
  157. { 2, 2, 3, 3, 4, 4, 4, 4 },
  158. { 1, 3, 3, 4, 4, 4, 4 },
  159. { 2, 2, 3, 3, 3, 4, 5, 5 },
  160. { 2, 3, 3, 3, 3, 3, 4, 4 },
  161. { 1, 3, 3, 4, 4, 4, 5, 5 },
  162. { 1, 3, 3, 4, 4, 4, 4 },
  163. { 2, 2, 3, 3, 4, 4, 4, 4 },
  164. { 1, 3, 3, 4, 4, 4, 5, 5 },
  165. { 2, 2, 3, 3, 4, 4, 4, 4 },
  166. { 2, 2, 3, 3, 3, 4, 4 },
  167. { 1, 3, 3, 4, 4, 4, 4 },
  168. { 1, 3, 3, 4, 4, 4, 4 },
  169. { 2, 2, 3, 3, 3, 4, 4 },
  170. { 2, 2, 3, 3, 3, 3 },
  171. };
  172. static const uint8_t mv_codes[16][10] =
  173. {
  174. { 1, 0, 2, 2, 7, 6, 7, 12, 26, 27 },
  175. { 0, 2, 2, 6, 7, 6, 14, 15 },
  176. { 0, 3, 3, 4, 4, 5, 10, 11 },
  177. { 0, 5, 7, 8, 9, 12, 13 },
  178. { 1, 3, 0, 1, 5, 8, 18, 19 },
  179. { 3, 0, 2, 3, 4, 5, 2, 3 },
  180. { 0, 4, 5, 12, 13, 14, 30, 31 },
  181. { 0, 5, 6, 8, 9, 14, 15 },
  182. { 0, 3, 3, 4, 4, 5, 10, 11 },
  183. { 0, 4, 5, 12, 13, 14, 30, 31 },
  184. { 0, 3, 2, 5, 6, 7, 8, 9 },
  185. { 0, 3, 2, 3, 5, 8, 9 },
  186. { 0, 5, 6, 8, 9, 14, 15 },
  187. { 0, 5, 6, 8, 9, 14, 15 },
  188. { 0, 3, 2, 3, 5, 8, 9 },
  189. { 0, 3, 2, 3, 4, 5 },
  190. };
  191. static const uint8_t mv_syms[16][10] =
  192. {
  193. { 0, 8, 1, 2, 9, 3, 6, 7, 5, 4 },
  194. { 9, 1, 2, 8, 0, 3, 5, 4 },
  195. { 0, 1, 2, 9, 5, 4, 3, 8 },
  196. { 1, 2, 0, 5, 4, 8, 3 },
  197. { 8, 1, 2, 9, 0, 3, 5, 4 },
  198. { 1, 3, 2, 9, 8, 0, 5, 4 },
  199. { 1, 2, 0, 9, 8, 3, 5, 4 },
  200. { 1, 2, 0, 8, 5, 4, 3 },
  201. { 0, 1, 2, 8, 5, 4, 3, 9 },
  202. { 1, 2, 0, 9, 8, 3, 5, 4 },
  203. { 0, 1, 3, 2, 9, 8, 5, 4 },
  204. { 0, 1, 4, 3, 2, 8, 5 },
  205. { 1, 2, 0, 5, 4, 9, 3 },
  206. { 1, 2, 0, 9, 5, 4, 3 },
  207. { 0, 1, 5, 3, 2, 9, 4 },
  208. { 0, 1, 4, 5, 3, 2 },
  209. };
  210. static const uint8_t mv_bits_mods[16][10] =
  211. {
  212. { 2, 2, 3, 3, 4, 4, 5, 5, 5, 5 },
  213. { 2, 2, 3, 3, 4, 4, 4, 4 },
  214. { 2, 2, 3, 3, 4, 4, 4, 4 },
  215. { 1, 3, 3, 3, 4, 5, 5 },
  216. { 2, 2, 3, 3, 4, 4, 4, 4 },
  217. { 2, 2, 3, 3, 4, 4, 4, 4 },
  218. { 2, 2, 3, 3, 4, 4, 4, 4 },
  219. { 2, 2, 2, 3, 4, 5, 5 },
  220. { 2, 2, 3, 3, 4, 4, 4, 4 },
  221. { 2, 2, 3, 3, 4, 4, 4, 4 },
  222. { 2, 2, 3, 3, 3, 4, 5, 5 },
  223. { 2, 2, 3, 3, 3, 4, 4 },
  224. { 1, 3, 3, 4, 4, 4, 4 },
  225. { 2, 2, 3, 3, 3, 4, 4 },
  226. { 2, 2, 3, 3, 3, 4, 4 },
  227. { 2, 2, 3, 3, 3, 3 },
  228. };
  229. static const uint8_t mv_codes_mods[16][10] =
  230. {
  231. { 0, 3, 2, 3, 9, 10, 16, 17, 22, 23 },
  232. { 0, 3, 2, 4, 6, 7, 10, 11 },
  233. { 1, 3, 0, 5, 2, 3, 8, 9 },
  234. { 0, 4, 6, 7, 10, 22, 23 },
  235. { 0, 3, 3, 4, 4, 5, 10, 11 },
  236. { 0, 3, 2, 5, 6, 7, 8, 9 },
  237. { 0, 3, 2, 5, 6, 7, 8, 9 },
  238. { 0, 1, 3, 4, 10, 22, 23 },
  239. { 0, 3, 2, 4, 6, 7, 10, 11 },
  240. { 0, 3, 3, 5, 4, 5, 8, 9 },
  241. { 0, 3, 2, 3, 5, 9, 16, 17 },
  242. { 0, 3, 2, 4, 5, 6, 7 },
  243. { 0, 5, 6, 8, 9, 14, 15 },
  244. { 0, 3, 2, 4, 5, 6, 7 },
  245. { 0, 3, 2, 4, 5, 6, 7 },
  246. { 1, 2, 0, 1, 6, 7 },
  247. };
  248. static const uint8_t mv_syms_mods[16][10] =
  249. {
  250. { 1, 0, 8, 9, 2, 7, 4, 3, 5, 6 },
  251. { 0, 1, 9, 2, 5, 4, 3, 8 },
  252. { 0, 1, 3, 2, 9, 5, 4, 8 },
  253. { 1, 3, 2, 0, 4, 8, 5 },
  254. { 0, 1, 8, 2, 5, 4, 3, 9 },
  255. { 0, 1, 3, 2, 5, 9, 4, 8 },
  256. { 0, 1, 3, 2, 9, 5, 8, 4 },
  257. { 0, 2, 1, 3, 4, 8, 5 },
  258. { 0, 1, 3, 2, 8, 4, 5, 9 },
  259. { 2, 1, 3, 0, 8, 9, 5, 4 },
  260. { 0, 1, 4, 3, 2, 5, 8, 9 },
  261. { 0, 1, 4, 3, 2, 8, 5 },
  262. { 1, 2, 0, 9, 4, 5, 3 },
  263. { 2, 1, 4, 3, 0, 9, 5 },
  264. { 0, 1, 4, 3, 2, 9, 5 },
  265. { 1, 0, 5, 4, 3, 2 },
  266. };
  267. typedef struct BlockXY {
  268. int w, h;
  269. int ax, ay;
  270. int x, y;
  271. int size;
  272. uint8_t *block;
  273. int linesize;
  274. } BlockXY;
  275. typedef struct MotionXY {
  276. int x, y;
  277. } MotionXY;
  278. typedef struct MobiClipContext {
  279. AVFrame *pic[6];
  280. int current_pic;
  281. int moflex;
  282. int dct_tab_idx;
  283. int quantizer;
  284. GetBitContext gb;
  285. uint8_t *bitstream;
  286. int bitstream_size;
  287. VLC vlc[2];
  288. VLC mv_vlc[2][16];
  289. int qtab[2][64];
  290. uint8_t pre[32];
  291. MotionXY *motion;
  292. int motion_size;
  293. BswapDSPContext bdsp;
  294. } MobiClipContext;
  295. static av_cold int mobiclip_init(AVCodecContext *avctx)
  296. {
  297. MobiClipContext *s = avctx->priv_data;
  298. int ret;
  299. if (avctx->width & 15 || avctx->height & 15) {
  300. av_log(avctx, AV_LOG_ERROR, "width/height not multiple of 16\n");
  301. return AVERROR_INVALIDDATA;
  302. }
  303. ff_bswapdsp_init(&s->bdsp);
  304. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  305. ret = ff_init_vlc_sparse(&s->vlc[0], 12, 104,
  306. bits0, sizeof(*bits0), sizeof(*bits0),
  307. codes0, sizeof(*codes0), sizeof(*codes0),
  308. syms0, sizeof(*syms0), sizeof(*syms0), 0);
  309. if (ret < 0)
  310. return ret;
  311. ret = ff_init_vlc_sparse(&s->vlc[1], 12, 104,
  312. bits0, sizeof(*bits0), sizeof(*bits0),
  313. codes0, sizeof(*codes0), sizeof(*codes0),
  314. syms1, sizeof(*syms1), sizeof(*syms1), 0);
  315. if (ret < 0)
  316. return ret;
  317. s->motion = av_calloc(avctx->width / 16 + 3, sizeof(MotionXY));
  318. if (!s->motion)
  319. return AVERROR(ENOMEM);
  320. s->motion_size = (avctx->width / 16 + 3) * sizeof(MotionXY);
  321. for (int i = 0; i < 6; i++) {
  322. s->pic[i] = av_frame_alloc();
  323. if (!s->pic[i])
  324. return AVERROR(ENOMEM);
  325. }
  326. for (int j = 0; j < 16; j++) {
  327. ret = ff_init_vlc_sparse(&s->mv_vlc[0][j], 8, mv_len[j],
  328. mv_bits_mods[j], sizeof(*mv_bits_mods[j]), sizeof(*mv_bits_mods[j]),
  329. mv_codes_mods[j], sizeof(*mv_codes_mods[j]), sizeof(*mv_codes_mods[j]),
  330. mv_syms_mods[j], sizeof(*mv_syms_mods[j]), sizeof(*mv_syms_mods[j]), 0);
  331. if (ret < 0)
  332. return ret;
  333. ret = ff_init_vlc_sparse(&s->mv_vlc[1][j], 8, mv_len[j],
  334. mv_bits[j], sizeof(*mv_bits[j]), sizeof(*mv_bits[j]),
  335. mv_codes[j], sizeof(*mv_codes[j]), sizeof(*mv_codes[j]),
  336. mv_syms[j], sizeof(*mv_syms[j]), sizeof(*mv_syms[j]), 0);
  337. if (ret < 0)
  338. return ret;
  339. }
  340. return 0;
  341. }
  342. static void setup_qtables(AVCodecContext *avctx, int quantizer)
  343. {
  344. MobiClipContext *s = avctx->priv_data;
  345. int qx, qy;
  346. s->quantizer = quantizer;
  347. qx = quantizer % 6;
  348. qy = quantizer / 6;
  349. for (int i = 0; i < 16; i++)
  350. s->qtab[0][i] = quant4x4_tab[qx][i] << qy;
  351. for (int i = 0; i < 64; i++)
  352. s->qtab[1][i] = quant8x8_tab[qx][i] << (qy - 2);
  353. for (int i = 0; i < 20; i++)
  354. s->pre[i] = 9;
  355. }
  356. static void inverse4(int *rs)
  357. {
  358. int a = rs[0] + rs[2];
  359. int b = rs[0] - rs[2];
  360. int c = rs[1] + (rs[3] >> 1);
  361. int d = (rs[1] >> 1) - rs[3];
  362. rs[0] = a + c;
  363. rs[1] = b + d;
  364. rs[2] = b - d;
  365. rs[3] = a - c;
  366. }
  367. static void idct(int *arr, int size)
  368. {
  369. int e, f, g, h, x3, x2, x1, x0;
  370. int tmp[4];
  371. if (size == 4) {
  372. inverse4(arr);
  373. return;
  374. }
  375. tmp[0] = arr[0];
  376. tmp[1] = arr[2];
  377. tmp[2] = arr[4];
  378. tmp[3] = arr[6];
  379. inverse4(tmp);
  380. e = arr[7] + arr[1] - arr[3] - (arr[3] >> 1);
  381. f = arr[7] - arr[1] + arr[5] + (arr[5] >> 1);
  382. g = arr[5] - arr[3] - arr[7] - (arr[7] >> 1);
  383. h = arr[5] + arr[3] + arr[1] + (arr[1] >> 1);
  384. x3 = g + (h >> 2);
  385. x2 = e + (f >> 2);
  386. x1 = (e >> 2) - f;
  387. x0 = h - (g >> 2);
  388. arr[0] = tmp[0] + x0;
  389. arr[1] = tmp[1] + x1;
  390. arr[2] = tmp[2] + x2;
  391. arr[3] = tmp[3] + x3;
  392. arr[4] = tmp[3] - x3;
  393. arr[5] = tmp[2] - x2;
  394. arr[6] = tmp[1] - x1;
  395. arr[7] = tmp[0] - x0;
  396. }
  397. static int read_run_encoding(AVCodecContext *avctx,
  398. int *last, int *run, int *level)
  399. {
  400. MobiClipContext *s = avctx->priv_data;
  401. GetBitContext *gb = &s->gb;
  402. int n = get_vlc2(gb, s->vlc[s->dct_tab_idx].table,
  403. s->vlc[s->dct_tab_idx].bits, 2);
  404. if (n < 0)
  405. return AVERROR_INVALIDDATA;
  406. *last = (n >> 11) == 1;
  407. *run = (n >> 5) & 0x3F;
  408. *level = n & 0x1F;
  409. return 0;
  410. }
  411. static int add_coefficients(AVCodecContext *avctx, AVFrame *frame,
  412. int bx, int by, int size, int plane)
  413. {
  414. MobiClipContext *s = avctx->priv_data;
  415. GetBitContext *gb = &s->gb;
  416. int mat[64] = { 0 };
  417. const uint8_t *ztab = size == 8 ? ff_zigzag_direct : zigzag4x4_tab;
  418. const int *qtab = s->qtab[size == 8];
  419. uint8_t *dst = frame->data[plane] + by * frame->linesize[plane] + bx;
  420. int ret = 0;
  421. for (int pos = 0; get_bits_left(gb) > 0; pos++) {
  422. int qval, last, run, level;
  423. ret = read_run_encoding(avctx, &last, &run, &level);
  424. if (ret < 0)
  425. return ret;
  426. if (level) {
  427. if (get_bits1(gb))
  428. level = -level;
  429. } else if (!get_bits1(gb)) {
  430. ret = read_run_encoding(avctx, &last, &run, &level);
  431. if (ret < 0)
  432. return ret;
  433. level += run_residue[s->dct_tab_idx][(last ? 64 : 0) + run];
  434. if (get_bits1(gb))
  435. level = -level;
  436. } else if (!get_bits1(gb)) {
  437. ret = read_run_encoding(avctx, &last, &run, &level);
  438. if (ret < 0)
  439. return ret;
  440. run += run_residue[s->dct_tab_idx][128 + (last ? 64 : 0) + level];
  441. if (get_bits1(gb))
  442. level = -level;
  443. } else {
  444. last = get_bits1(gb);
  445. run = get_bits(gb, 6);
  446. level = get_sbits(gb, 12);
  447. }
  448. pos += run;
  449. if (pos >= size * size)
  450. return AVERROR_INVALIDDATA;
  451. qval = qtab[pos];
  452. mat[ztab[pos]] = qval * level;
  453. if (last)
  454. break;
  455. }
  456. mat[0] += 32;
  457. for (int y = 0; y < size; y++)
  458. idct(&mat[y * size], size);
  459. for (int y = 0; y < size; y++) {
  460. for (int x = y + 1; x < size; x++) {
  461. int a = mat[x * size + y];
  462. int b = mat[y * size + x];
  463. mat[y * size + x] = a;
  464. mat[x * size + y] = b;
  465. }
  466. idct(&mat[y * size], size);
  467. for (int x = 0; x < size; x++)
  468. dst[x] = av_clip_uint8(dst[x] + (mat[y * size + x] >> 6));
  469. dst += frame->linesize[plane];
  470. }
  471. return ret;
  472. }
  473. static int add_pframe_coefficients(AVCodecContext *avctx, AVFrame *frame,
  474. int bx, int by, int size, int plane)
  475. {
  476. MobiClipContext *s = avctx->priv_data;
  477. GetBitContext *gb = &s->gb;
  478. int ret, idx = get_ue_golomb(gb);
  479. if (idx == 0) {
  480. ret = add_coefficients(avctx, frame, bx, by, size, plane);
  481. } else if (idx < FF_ARRAY_ELEMS(pframe_block4x4_coefficients_tab)) {
  482. int flags = pframe_block4x4_coefficients_tab[idx];
  483. for (int y = by; y < by + 8; y += 4) {
  484. for (int x = bx; x < bx + 8; x += 4) {
  485. if (flags & 1) {
  486. ret = add_coefficients(avctx, frame, x, y, 4, plane);
  487. if (ret < 0)
  488. return ret;
  489. }
  490. flags >>= 1;
  491. }
  492. }
  493. } else {
  494. ret = AVERROR_INVALIDDATA;
  495. }
  496. return ret;
  497. }
  498. static int adjust(int x, int size)
  499. {
  500. return size == 16 ? (x + 1) >> 1 : x;
  501. }
  502. static uint8_t pget(BlockXY b)
  503. {
  504. BlockXY ret = b;
  505. int x, y;
  506. if (b.x == -1 && b.y >= b.size) {
  507. ret.x = -1, ret.y = b.size - 1;
  508. } else if (b.x >= -1 && b.y >= -1) {
  509. ret.x = b.x, ret.y = b.y;
  510. } else if (b.x == -1 && b.y == -2) {
  511. ret.x = 0, ret.y = -1;
  512. } else if (b.x == -2 && b.y == -1) {
  513. ret.x = -1, ret.y = 0;
  514. }
  515. y = av_clip(ret.ay + ret.y, 0, ret.h - 1);
  516. x = av_clip(ret.ax + ret.x, 0, ret.w - 1);
  517. return ret.block[y * ret.linesize + x];
  518. }
  519. static uint8_t half(int a, int b)
  520. {
  521. return ((a + b) + 1) / 2;
  522. }
  523. static uint8_t half3(int a, int b, int c)
  524. {
  525. return ((a + b + b + c) * 2 / 4 + 1) / 2;;
  526. }
  527. static uint8_t pick_above(BlockXY bxy)
  528. {
  529. bxy.y = bxy.y - 1;
  530. return pget(bxy);
  531. }
  532. static uint8_t pick_left(BlockXY bxy)
  533. {
  534. bxy.x = bxy.x - 1;
  535. return pget(bxy);
  536. }
  537. static uint8_t half_horz(BlockXY bxy)
  538. {
  539. BlockXY a = bxy, b = bxy, c = bxy;
  540. a.x -= 1;
  541. c.x += 1;
  542. return half3(pget(a), pget(b), pget(c));
  543. }
  544. static uint8_t half_vert(BlockXY bxy)
  545. {
  546. BlockXY a = bxy, b = bxy, c = bxy;
  547. a.y -= 1;
  548. c.y += 1;
  549. return half3(pget(a), pget(b), pget(c));
  550. }
  551. static uint8_t pick_4(BlockXY bxy)
  552. {
  553. int val;
  554. if ((bxy.x % 2) == 0) {
  555. BlockXY ba, bb;
  556. int a, b;
  557. ba = bxy;
  558. ba.x = -1;
  559. ba.y = bxy.y + bxy.x / 2;
  560. a = pget(ba);
  561. bb = bxy;
  562. bb.x = -1;
  563. bb.y = bxy.y + bxy.x / 2 + 1;
  564. b = pget(bb);
  565. val = half(a, b);
  566. } else {
  567. BlockXY ba;
  568. ba = bxy;
  569. ba.x = -1;
  570. ba.y = bxy.y + bxy.x / 2 + 1;
  571. val = half_vert(ba);
  572. }
  573. return val;
  574. }
  575. static uint8_t pick_5(BlockXY bxy)
  576. {
  577. int val;
  578. if (bxy.x == 0) {
  579. BlockXY a = bxy;
  580. BlockXY b = bxy;
  581. a.x = -1;
  582. a.y -= 1;
  583. b.x = -1;
  584. val = half(pget(a), pget(b));
  585. } else if (bxy.y == 0) {
  586. BlockXY a = bxy;
  587. a.x -= 2;
  588. a.y -= 1;
  589. val = half_horz(a);
  590. } else if (bxy.x == 1) {
  591. BlockXY a = bxy;
  592. a.x -= 2;
  593. a.y -= 1;
  594. val = half_vert(a);
  595. } else {
  596. BlockXY a = bxy;
  597. a.x -= 2;
  598. a.y -= 1;
  599. val = pget(a);
  600. }
  601. return val;
  602. }
  603. static uint8_t pick_6(BlockXY bxy)
  604. {
  605. int val;
  606. if (bxy.y == 0) {
  607. BlockXY a = bxy;
  608. BlockXY b = bxy;
  609. a.x -= 1;
  610. a.y = -1;
  611. b.y = -1;
  612. val = half(pget(a), pget(b));
  613. } else if (bxy.x == 0) {
  614. BlockXY a = bxy;
  615. a.x -= 1;
  616. a.y -= 2;
  617. val = half_vert(a);
  618. } else if (bxy.y == 1) {
  619. BlockXY a = bxy;
  620. a.x -= 1;
  621. a.y -= 2;
  622. val = half_horz(a);
  623. } else {
  624. BlockXY a = bxy;
  625. a.x -= 1;
  626. a.y -= 2;
  627. val = pget(a);
  628. }
  629. return val;
  630. }
  631. static uint8_t pick_7(BlockXY bxy)
  632. {
  633. int clr, acc1, acc2;
  634. BlockXY a = bxy;
  635. a.x -= 1;
  636. a.y -= 1;
  637. clr = pget(a);
  638. if (bxy.x && bxy.y)
  639. return clr;
  640. if (bxy.x == 0) {
  641. a.x = -1;
  642. a.y = bxy.y;
  643. } else {
  644. a.x = bxy.x - 2;
  645. a.y = -1;
  646. }
  647. acc1 = pget(a);
  648. if (bxy.y == 0) {
  649. a.x = bxy.x;
  650. a.y = -1;
  651. } else {
  652. a.x = -1;
  653. a.y = bxy.y - 2;
  654. }
  655. acc2 = pget(a);
  656. return half3(acc1, clr, acc2);
  657. }
  658. static uint8_t pick_8(BlockXY bxy)
  659. {
  660. BlockXY ba = bxy;
  661. BlockXY bb = bxy;
  662. int val;
  663. if (bxy.y == 0) {
  664. int a, b;
  665. ba.y = -1;
  666. a = pget(ba);
  667. bb.x += 1;
  668. bb.y = -1;
  669. b = pget(bb);
  670. val = half(a, b);
  671. } else if (bxy.y == 1) {
  672. ba.x += 1;
  673. ba.y -= 2;
  674. val = half_horz(ba);
  675. } else if (bxy.x < bxy.size - 1) {
  676. ba.x += 1;
  677. ba.y -= 2;
  678. val = pget(ba);
  679. } else if (bxy.y % 2 == 0) {
  680. int a, b;
  681. ba.x = bxy.y / 2 + bxy.size - 1;
  682. ba.y = -1;
  683. a = pget(ba);
  684. bb.x = bxy.y / 2 + bxy.size;
  685. bb.y = -1;
  686. b = pget(bb);
  687. val = half(a, b);
  688. } else {
  689. ba.x = bxy.y / 2 + bxy.size;
  690. ba.y = -1;
  691. val = half_horz(ba);
  692. }
  693. return val;
  694. }
  695. static void block_fill_simple(uint8_t *block, int size, int linesize, int fill)
  696. {
  697. for (int y = 0; y < size; y++) {
  698. memset(block, fill, size);
  699. block += linesize;
  700. }
  701. }
  702. static void block_fill(uint8_t *block, int size, int linesize,
  703. int w, int h, int ax, int ay,
  704. uint8_t (*pick)(BlockXY bxy))
  705. {
  706. BlockXY bxy;
  707. bxy.size = size;
  708. bxy.block = block;
  709. bxy.linesize = linesize;
  710. bxy.w = w;
  711. bxy.h = h;
  712. bxy.ay = ay;
  713. bxy.ax = ax;
  714. for (int y = 0; y < size; y++) {
  715. bxy.y = y;
  716. for (int x = 0; x < size; x++) {
  717. uint8_t val;
  718. bxy.x = x;
  719. val = pick(bxy);
  720. block[ax + x + (ay + y) * linesize] = val;
  721. }
  722. }
  723. }
  724. static int block_sum(const uint8_t *block, int w, int h, int linesize)
  725. {
  726. int sum = 0;
  727. for (int y = 0; y < h; y++) {
  728. for (int x = 0; x < w; x++) {
  729. sum += block[x];
  730. }
  731. block += linesize;
  732. }
  733. return sum;
  734. }
  735. static int predict_intra(AVCodecContext *avctx, AVFrame *frame, int ax, int ay,
  736. int pmode, int add_coeffs, int size, int plane)
  737. {
  738. MobiClipContext *s = avctx->priv_data;
  739. GetBitContext *gb = &s->gb;
  740. int w = avctx->width >> !!plane, h = avctx->height >> !!plane;
  741. int ret = 0;
  742. switch (pmode) {
  743. case 0:
  744. block_fill(frame->data[plane], size, frame->linesize[plane], w, h, ax, ay, pick_above);
  745. break;
  746. case 1:
  747. block_fill(frame->data[plane], size, frame->linesize[plane], w, h, ax, ay, pick_left);
  748. break;
  749. case 2:
  750. {
  751. int arr1[16];
  752. int arr2[16];
  753. uint8_t *top = frame->data[plane] + FFMAX(ay - 1, 0) * frame->linesize[plane] + ax;
  754. uint8_t *left = frame->data[plane] + ay * frame->linesize[plane] + ax - 1;
  755. int bottommost = frame->data[plane][(ay + size - 1) * frame->linesize[plane] + FFMAX(ax - 1, 0)];
  756. int rightmost = frame->data[plane][FFMAX(ay - 1, 0) * frame->linesize[plane] + ax + size - 1];
  757. int avg = (bottommost + rightmost + 1) / 2 + 2 * get_se_golomb(gb);
  758. int r6 = adjust(avg - bottommost, size);
  759. int r9 = adjust(avg - rightmost, size);
  760. int shift = adjust(size, size) == 8 ? 3 : 2;
  761. uint8_t *block;
  762. for (int x = 0; x < size; x++) {
  763. int val = top[x];
  764. arr1[x] = adjust(((bottommost - val) * (1 << shift)) + r6 * (x + 1), size);
  765. }
  766. for (int y = 0; y < size; y++) {
  767. int val = left[y * frame->linesize[plane]];
  768. arr2[y] = adjust(((rightmost - val) * (1 << shift)) + r9 * (y + 1), size);
  769. }
  770. block = frame->data[plane] + ay * frame->linesize[plane] + ax;
  771. for (int y = 0; y < size; y++) {
  772. for (int x = 0; x < size; x++) {
  773. block[x] = (((top[x] + left[0] + ((arr1[x] * (y + 1) +
  774. arr2[y] * (x + 1)) >> 2 * shift)) + 1) / 2) & 0xFF;
  775. }
  776. block += frame->linesize[plane];
  777. left += frame->linesize[plane];
  778. }
  779. }
  780. break;
  781. case 3:
  782. {
  783. uint8_t fill;
  784. if (ax == 0 && ay == 0) {
  785. fill = 0x80;
  786. } else if (ax >= 1 && ay >= 1) {
  787. int left = block_sum(frame->data[plane] + ay * frame->linesize[plane] + ax - 1,
  788. 1, size, frame->linesize[plane]);
  789. int top = block_sum(frame->data[plane] + (ay - 1) * frame->linesize[plane] + ax,
  790. size, 1, frame->linesize[plane]);
  791. fill = ((left + top) * 2 / (2 * size) + 1) / 2;
  792. } else if (ax >= 1) {
  793. fill = (block_sum(frame->data[plane] + ay * frame->linesize[plane] + ax - 1,
  794. 1, size, frame->linesize[plane]) * 2 / size + 1) / 2;
  795. } else if (ay >= 1) {
  796. fill = (block_sum(frame->data[plane] + (ay - 1) * frame->linesize[plane] + ax,
  797. size, 1, frame->linesize[plane]) * 2 / size + 1) / 2;
  798. } else {
  799. return -1;
  800. }
  801. block_fill_simple(frame->data[plane] + ay * frame->linesize[plane] + ax,
  802. size, frame->linesize[plane], fill);
  803. }
  804. break;
  805. case 4:
  806. block_fill(frame->data[plane], size, frame->linesize[plane], w, h, ax, ay, pick_4);
  807. break;
  808. case 5:
  809. block_fill(frame->data[plane], size, frame->linesize[plane], w, h, ax, ay, pick_5);
  810. break;
  811. case 6:
  812. block_fill(frame->data[plane], size, frame->linesize[plane], w, h, ax, ay, pick_6);
  813. break;
  814. case 7:
  815. block_fill(frame->data[plane], size, frame->linesize[plane], w, h, ax, ay, pick_7);
  816. break;
  817. case 8:
  818. block_fill(frame->data[plane], size, frame->linesize[plane], w, h, ax, ay, pick_8);
  819. break;
  820. }
  821. if (add_coeffs)
  822. ret = add_coefficients(avctx, frame, ax, ay, size, plane);
  823. return ret;
  824. }
  825. static int get_prediction(AVCodecContext *avctx, int x, int y, int size)
  826. {
  827. MobiClipContext *s = avctx->priv_data;
  828. GetBitContext *gb = &s->gb;
  829. int index = (y & 0xC) | (x / 4 % 4);
  830. uint8_t val = FFMIN(s->pre[index], index % 4 == 0 ? 9 : s->pre[index + 3]);
  831. if (val == 9)
  832. val = 3;
  833. if (!get_bits1(gb)) {
  834. int x = get_bits(gb, 3);
  835. val = x + (x >= val ? 1 : 0);
  836. }
  837. s->pre[index + 4] = val;
  838. if (size == 8)
  839. s->pre[index + 5] = s->pre[index + 8] = s->pre[index + 9] = val;
  840. return val;
  841. }
  842. static int process_block(AVCodecContext *avctx, AVFrame *frame,
  843. int x, int y, int pmode, int has_coeffs, int plane)
  844. {
  845. MobiClipContext *s = avctx->priv_data;
  846. GetBitContext *gb = &s->gb;
  847. int tmp, ret;
  848. if (!has_coeffs) {
  849. if (pmode < 0)
  850. pmode = get_prediction(avctx, x, y, 8);
  851. return predict_intra(avctx, frame, x, y, pmode, 0, 8, plane);
  852. }
  853. tmp = get_ue_golomb(gb);
  854. if (tmp < 0 || tmp > FF_ARRAY_ELEMS(block4x4_coefficients_tab))
  855. return AVERROR_INVALIDDATA;
  856. if (tmp == 0) {
  857. if (pmode < 0)
  858. pmode = get_prediction(avctx, x, y, 8);
  859. ret = predict_intra(avctx, frame, x, y, pmode, 1, 8, plane);
  860. } else {
  861. int flags = block4x4_coefficients_tab[tmp - 1];
  862. for (int by = y; by < y + 8; by += 4) {
  863. for (int bx = x; bx < x + 8; bx += 4) {
  864. int new_pmode = pmode;
  865. if (new_pmode < 0)
  866. new_pmode = get_prediction(avctx, bx, by, 4);
  867. ret = predict_intra(avctx, frame, bx, by, new_pmode, flags & 1, 4, plane);
  868. if (ret < 0)
  869. return ret;
  870. flags >>= 1;
  871. }
  872. }
  873. }
  874. return ret;
  875. }
  876. static int decode_macroblock(AVCodecContext *avctx, AVFrame *frame,
  877. int x, int y, int predict)
  878. {
  879. MobiClipContext *s = avctx->priv_data;
  880. GetBitContext *gb = &s->gb;
  881. int flags, pmode_uv, idx = get_ue_golomb(gb);
  882. int ret = 0;
  883. if (idx < 0 || idx >= FF_ARRAY_ELEMS(block8x8_coefficients_tab))
  884. return AVERROR_INVALIDDATA;
  885. flags = block8x8_coefficients_tab[idx];
  886. if (predict) {
  887. ret = process_block(avctx, frame, x, y, -1, flags & 1, 0);
  888. if (ret < 0)
  889. return ret;
  890. flags >>= 1;
  891. ret = process_block(avctx, frame, x + 8, y, -1, flags & 1, 0);
  892. if (ret < 0)
  893. return ret;
  894. flags >>= 1;
  895. ret = process_block(avctx, frame, x, y + 8, -1, flags & 1, 0);
  896. if (ret < 0)
  897. return ret;
  898. flags >>= 1;
  899. ret = process_block(avctx, frame, x + 8, y + 8, -1, flags & 1, 0);
  900. if (ret < 0)
  901. return ret;
  902. flags >>= 1;
  903. } else {
  904. int pmode = get_bits(gb, 3);
  905. if (pmode == 2) {
  906. ret = predict_intra(avctx, frame, x, y, pmode, 0, 16, 0);
  907. if (ret < 0)
  908. return ret;
  909. pmode = 9;
  910. }
  911. ret = process_block(avctx, frame, x, y, pmode, flags & 1, 0);
  912. if (ret < 0)
  913. return ret;
  914. flags >>= 1;
  915. ret = process_block(avctx, frame, x + 8, y, pmode, flags & 1, 0);
  916. if (ret < 0)
  917. return ret;
  918. flags >>= 1;
  919. ret = process_block(avctx, frame, x, y + 8, pmode, flags & 1, 0);
  920. if (ret < 0)
  921. return ret;
  922. flags >>= 1;
  923. ret = process_block(avctx, frame, x + 8, y + 8, pmode, flags & 1, 0);
  924. if (ret < 0)
  925. return ret;
  926. flags >>= 1;
  927. }
  928. pmode_uv = get_bits(gb, 3);
  929. if (pmode_uv == 2) {
  930. ret = predict_intra(avctx, frame, x >> 1, y >> 1, pmode_uv, 0, 8, 1 + !s->moflex);
  931. if (ret < 0)
  932. return ret;
  933. ret = predict_intra(avctx, frame, x >> 1, y >> 1, pmode_uv, 0, 8, 2 - !s->moflex);
  934. if (ret < 0)
  935. return ret;
  936. pmode_uv = 9;
  937. }
  938. ret = process_block(avctx, frame, x >> 1, y >> 1, pmode_uv, flags & 1, 1 + !s->moflex);
  939. if (ret < 0)
  940. return ret;
  941. flags >>= 1;
  942. ret = process_block(avctx, frame, x >> 1, y >> 1, pmode_uv, flags & 1, 2 - !s->moflex);
  943. if (ret < 0)
  944. return ret;
  945. return 0;
  946. }
  947. static int get_index(int x)
  948. {
  949. return x == 16 ? 0 : x == 8 ? 1 : x == 4 ? 2 : x == 2 ? 3 : 0;
  950. }
  951. static int predict_motion(AVCodecContext *avctx,
  952. int width, int height, int index,
  953. int offsetm, int offsetx, int offsety)
  954. {
  955. MobiClipContext *s = avctx->priv_data;
  956. MotionXY *motion = s->motion;
  957. GetBitContext *gb = &s->gb;
  958. int fheight = avctx->height;
  959. int fwidth = avctx->width;
  960. if (index <= 5) {
  961. int sidx = -FFMAX(1, index) + s->current_pic;
  962. MotionXY mv = s->motion[0];
  963. if (sidx < 0)
  964. sidx += 6;
  965. if (index > 0) {
  966. mv.x = mv.x + get_se_golomb(gb);
  967. mv.y = mv.y + get_se_golomb(gb);
  968. }
  969. motion[offsetm].x = mv.x;
  970. motion[offsetm].y = mv.y;
  971. for (int i = 0; i < 3; i++) {
  972. int method, src_linesize, dst_linesize;
  973. uint8_t *src, *dst;
  974. if (i == 1) {
  975. offsetx = offsetx >> 1;
  976. offsety = offsety >> 1;
  977. mv.x = mv.x >> 1;
  978. mv.y = mv.y >> 1;
  979. width = width >> 1;
  980. height = height >> 1;
  981. fwidth = fwidth >> 1;
  982. fheight = fheight >> 1;
  983. }
  984. av_assert0(s->pic[sidx]);
  985. av_assert0(s->pic[s->current_pic]);
  986. av_assert0(s->pic[s->current_pic]->data[i]);
  987. if (!s->pic[sidx]->data[i])
  988. return AVERROR_INVALIDDATA;
  989. method = (mv.x & 1) | ((mv.y & 1) << 1);
  990. src_linesize = s->pic[sidx]->linesize[i];
  991. dst_linesize = s->pic[s->current_pic]->linesize[i];
  992. dst = s->pic[s->current_pic]->data[i] + offsetx + offsety * dst_linesize;
  993. switch (method) {
  994. case 0:
  995. if (offsety + (mv.y >> 1) < 0 ||
  996. offsety + (mv.y >> 1) >= fheight ||
  997. offsetx + (mv.x >> 1) < 0 ||
  998. offsetx + (mv.x >> 1) >= fwidth)
  999. return AVERROR_INVALIDDATA;
  1000. src = s->pic[sidx]->data[i] + offsetx + (mv.x >> 1) +
  1001. (offsety + (mv.y >> 1)) * src_linesize;
  1002. for (int y = 0; y < height; y++) {
  1003. for (int x = 0; x < width; x++)
  1004. dst[x] = src[x];
  1005. dst += dst_linesize;
  1006. src += src_linesize;
  1007. }
  1008. break;
  1009. case 1:
  1010. if (offsety + (mv.y >> 1) < 0 ||
  1011. offsety + (mv.y >> 1) >= fheight ||
  1012. offsetx + (mv.x >> 1) < 0 ||
  1013. offsetx + (mv.x >> 1) >= fwidth)
  1014. return AVERROR_INVALIDDATA;
  1015. src = s->pic[sidx]->data[i] + offsetx + (mv.x >> 1) +
  1016. (offsety + (mv.y >> 1)) * src_linesize;
  1017. for (int y = 0; y < height; y++) {
  1018. for (int x = 0; x < width; x++) {
  1019. dst[x] = (uint8_t)((src[x] >> 1) + (src[x + 1] >> 1));
  1020. }
  1021. dst += dst_linesize;
  1022. src += src_linesize;
  1023. }
  1024. break;
  1025. case 2:
  1026. if (offsety + (mv.y >> 1) < 0 ||
  1027. offsety + (mv.y >> 1) >= fheight - 1 ||
  1028. offsetx + (mv.x >> 1) < 0 ||
  1029. offsetx + (mv.x >> 1) >= fwidth)
  1030. return AVERROR_INVALIDDATA;
  1031. src = s->pic[sidx]->data[i] + offsetx + (mv.x >> 1) +
  1032. (offsety + (mv.y >> 1)) * src_linesize;
  1033. for (int y = 0; y < height; y++) {
  1034. for (int x = 0; x < width; x++) {
  1035. dst[x] = (uint8_t)((src[x] >> 1) + (src[x + src_linesize] >> 1));
  1036. }
  1037. dst += dst_linesize;
  1038. src += src_linesize;
  1039. }
  1040. break;
  1041. case 3:
  1042. if (offsety + (mv.y >> 1) < 0 ||
  1043. offsety + (mv.y >> 1) >= fheight - 1 ||
  1044. offsetx + (mv.x >> 1) < 0 ||
  1045. offsetx + (mv.x >> 1) >= fwidth)
  1046. return AVERROR_INVALIDDATA;
  1047. src = s->pic[sidx]->data[i] + offsetx + (mv.x >> 1) +
  1048. (offsety + (mv.y >> 1)) * src_linesize;
  1049. for (int y = 0; y < height; y++) {
  1050. for (int x = 0; x < width; x++) {
  1051. dst[x] = (uint8_t)((((src[x] >> 1) + (src[x + 1] >> 1)) >> 1) +
  1052. (((src[x + src_linesize] >> 1) + (src[x + 1 + src_linesize] >> 1)) >> 1));
  1053. }
  1054. dst += dst_linesize;
  1055. src += src_linesize;
  1056. }
  1057. break;
  1058. }
  1059. }
  1060. } else {
  1061. int tidx;
  1062. int adjx = index == 8 ? 0 : width / 2;
  1063. int adjy = index == 8 ? height / 2 : 0;
  1064. width = width - adjx;
  1065. height = height - adjy;
  1066. tidx = get_index(height) * 4 + get_index(width);
  1067. for (int i = 0; i < 2; i++) {
  1068. int ret, idx2;
  1069. idx2 = get_vlc2(gb, s->mv_vlc[s->moflex][tidx].table,
  1070. s->mv_vlc[s->moflex][tidx].bits, 1);
  1071. if (idx2 < 0)
  1072. return AVERROR_INVALIDDATA;
  1073. ret = predict_motion(avctx, width, height, idx2,
  1074. offsetm, offsetx + i * adjx, offsety + i * adjy);
  1075. if (ret < 0)
  1076. return ret;
  1077. }
  1078. }
  1079. return 0;
  1080. }
  1081. static int mobiclip_decode(AVCodecContext *avctx, void *data,
  1082. int *got_frame, AVPacket *pkt)
  1083. {
  1084. MobiClipContext *s = avctx->priv_data;
  1085. GetBitContext *gb = &s->gb;
  1086. AVFrame *frame = s->pic[s->current_pic];
  1087. int ret;
  1088. av_fast_padded_malloc(&s->bitstream, &s->bitstream_size,
  1089. pkt->size);
  1090. if ((ret = ff_reget_buffer(avctx, frame, 0)) < 0)
  1091. return ret;
  1092. s->bdsp.bswap16_buf((uint16_t *)s->bitstream,
  1093. (uint16_t *)pkt->data,
  1094. (pkt->size + 1) >> 1);
  1095. ret = init_get_bits8(gb, s->bitstream, s->bitstream_size);
  1096. if (ret < 0)
  1097. return ret;
  1098. if (get_bits1(gb)) {
  1099. frame->pict_type = AV_PICTURE_TYPE_I;
  1100. frame->key_frame = 1;
  1101. s->moflex = get_bits1(gb);
  1102. s->dct_tab_idx = get_bits1(gb);
  1103. setup_qtables(avctx, get_bits(gb, 6));
  1104. for (int y = 0; y < avctx->height; y += 16) {
  1105. for (int x = 0; x < avctx->width; x += 16) {
  1106. ret = decode_macroblock(avctx, frame, x, y, get_bits1(gb));
  1107. if (ret < 0)
  1108. return ret;
  1109. }
  1110. }
  1111. } else {
  1112. MotionXY *motion = s->motion;
  1113. int quantizer = s->quantizer + get_se_golomb(gb);
  1114. if (quantizer < 12 || quantizer > 161)
  1115. return AVERROR_INVALIDDATA;
  1116. memset(motion, 0, s->motion_size);
  1117. frame->pict_type = AV_PICTURE_TYPE_P;
  1118. frame->key_frame = 0;
  1119. s->dct_tab_idx = 0;
  1120. setup_qtables(avctx, quantizer);
  1121. for (int y = 0; y < avctx->height; y += 16) {
  1122. for (int x = 0; x < avctx->width; x += 16) {
  1123. int idx;
  1124. motion[0].x = mid_pred(motion[x / 16 + 1].x, motion[x / 16 + 2].x, motion[x / 16 + 3].x);
  1125. motion[0].y = mid_pred(motion[x / 16 + 1].y, motion[x / 16 + 2].y, motion[x / 16 + 3].y);
  1126. motion[x / 16 + 2].x = 0;
  1127. motion[x / 16 + 2].y = 0;
  1128. idx = get_vlc2(gb, s->mv_vlc[s->moflex][0].table,
  1129. s->mv_vlc[s->moflex][0].bits, 1);
  1130. if (idx < 0)
  1131. return AVERROR_INVALIDDATA;
  1132. if (idx == 6 || idx == 7) {
  1133. ret = decode_macroblock(avctx, frame, x, y, idx == 7);
  1134. if (ret < 0)
  1135. return ret;
  1136. } else {
  1137. int flags, idx2;
  1138. ret = predict_motion(avctx, 16, 16, idx, x / 16 + 2, x, y);
  1139. if (ret < 0)
  1140. return ret;
  1141. idx2 = get_ue_golomb(gb);
  1142. if (idx2 >= FF_ARRAY_ELEMS(pframe_block8x8_coefficients_tab))
  1143. return AVERROR_INVALIDDATA;
  1144. flags = pframe_block8x8_coefficients_tab[idx2];
  1145. for (int sy = y; sy < y + 16; sy += 8) {
  1146. for (int sx = x; sx < x + 16; sx += 8) {
  1147. if (flags & 1)
  1148. add_pframe_coefficients(avctx, frame, sx, sy, 8, 0);
  1149. flags >>= 1;
  1150. }
  1151. }
  1152. if (flags & 1)
  1153. add_pframe_coefficients(avctx, frame, x >> 1, y >> 1, 8, 1 + !s->moflex);
  1154. flags >>= 1;
  1155. if (flags & 1)
  1156. add_pframe_coefficients(avctx, frame, x >> 1, y >> 1, 8, 2 - !s->moflex);
  1157. }
  1158. }
  1159. }
  1160. }
  1161. if (!s->moflex)
  1162. avctx->colorspace = AVCOL_SPC_YCGCO;
  1163. s->current_pic = (s->current_pic + 1) % 6;
  1164. ret = av_frame_ref(data, frame);
  1165. if (ret < 0)
  1166. return ret;
  1167. *got_frame = 1;
  1168. return 0;
  1169. }
  1170. static void mobiclip_flush(AVCodecContext *avctx)
  1171. {
  1172. MobiClipContext *s = avctx->priv_data;
  1173. for (int i = 0; i < 6; i++)
  1174. av_frame_unref(s->pic[i]);
  1175. }
  1176. static av_cold int mobiclip_close(AVCodecContext *avctx)
  1177. {
  1178. MobiClipContext *s = avctx->priv_data;
  1179. ff_free_vlc(&s->vlc[0]);
  1180. ff_free_vlc(&s->vlc[1]);
  1181. for (int i = 0; i < 16; i++) {
  1182. ff_free_vlc(&s->mv_vlc[0][i]);
  1183. ff_free_vlc(&s->mv_vlc[1][i]);
  1184. }
  1185. av_freep(&s->bitstream);
  1186. s->bitstream_size = 0;
  1187. av_freep(&s->motion);
  1188. s->motion_size = 0;
  1189. for (int i = 0; i < 6; i++) {
  1190. av_frame_free(&s->pic[i]);
  1191. }
  1192. return 0;
  1193. }
  1194. AVCodec ff_mobiclip_decoder = {
  1195. .name = "mobiclip",
  1196. .long_name = NULL_IF_CONFIG_SMALL("MobiClip Video"),
  1197. .type = AVMEDIA_TYPE_VIDEO,
  1198. .id = AV_CODEC_ID_MOBICLIP,
  1199. .priv_data_size = sizeof(MobiClipContext),
  1200. .init = mobiclip_init,
  1201. .decode = mobiclip_decode,
  1202. .flush = mobiclip_flush,
  1203. .close = mobiclip_close,
  1204. .capabilities = AV_CODEC_CAP_DR1,
  1205. };