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.

2883 lines
111KB

  1. /*
  2. * MPEG4 decoder.
  3. * Copyright (c) 2000,2001 Fabrice Bellard
  4. * Copyright (c) 2002-2010 Michael Niedermayer <michaelni@gmx.at>
  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. #define UNCHECKED_BITSTREAM_READER 1
  23. #include "libavutil/internal.h"
  24. #include "libavutil/opt.h"
  25. #include "error_resilience.h"
  26. #include "idctdsp.h"
  27. #include "internal.h"
  28. #include "mpegutils.h"
  29. #include "mpegvideo.h"
  30. #include "mpegvideodata.h"
  31. #include "mpeg4video.h"
  32. #include "h263.h"
  33. #include "thread.h"
  34. #include "xvididct.h"
  35. /* The defines below define the number of bits that are read at once for
  36. * reading vlc values. Changing these may improve speed and data cache needs
  37. * be aware though that decreasing them may need the number of stages that is
  38. * passed to get_vlc* to be increased. */
  39. #define SPRITE_TRAJ_VLC_BITS 6
  40. #define DC_VLC_BITS 9
  41. #define MB_TYPE_B_VLC_BITS 4
  42. static VLC dc_lum, dc_chrom;
  43. static VLC sprite_trajectory;
  44. static VLC mb_type_b_vlc;
  45. static const int mb_type_b_map[4] = {
  46. MB_TYPE_DIRECT2 | MB_TYPE_L0L1,
  47. MB_TYPE_L0L1 | MB_TYPE_16x16,
  48. MB_TYPE_L1 | MB_TYPE_16x16,
  49. MB_TYPE_L0 | MB_TYPE_16x16,
  50. };
  51. /**
  52. * Predict the ac.
  53. * @param n block index (0-3 are luma, 4-5 are chroma)
  54. * @param dir the ac prediction direction
  55. */
  56. void ff_mpeg4_pred_ac(MpegEncContext *s, int16_t *block, int n, int dir)
  57. {
  58. int i;
  59. int16_t *ac_val, *ac_val1;
  60. int8_t *const qscale_table = s->current_picture.qscale_table;
  61. /* find prediction */
  62. ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
  63. ac_val1 = ac_val;
  64. if (s->ac_pred) {
  65. if (dir == 0) {
  66. const int xy = s->mb_x - 1 + s->mb_y * s->mb_stride;
  67. /* left prediction */
  68. ac_val -= 16;
  69. if (s->mb_x == 0 || s->qscale == qscale_table[xy] ||
  70. n == 1 || n == 3) {
  71. /* same qscale */
  72. for (i = 1; i < 8; i++)
  73. block[s->idsp.idct_permutation[i << 3]] += ac_val[i];
  74. } else {
  75. /* different qscale, we must rescale */
  76. for (i = 1; i < 8; i++)
  77. block[s->idsp.idct_permutation[i << 3]] += ROUNDED_DIV(ac_val[i] * qscale_table[xy], s->qscale);
  78. }
  79. } else {
  80. const int xy = s->mb_x + s->mb_y * s->mb_stride - s->mb_stride;
  81. /* top prediction */
  82. ac_val -= 16 * s->block_wrap[n];
  83. if (s->mb_y == 0 || s->qscale == qscale_table[xy] ||
  84. n == 2 || n == 3) {
  85. /* same qscale */
  86. for (i = 1; i < 8; i++)
  87. block[s->idsp.idct_permutation[i]] += ac_val[i + 8];
  88. } else {
  89. /* different qscale, we must rescale */
  90. for (i = 1; i < 8; i++)
  91. block[s->idsp.idct_permutation[i]] += ROUNDED_DIV(ac_val[i + 8] * qscale_table[xy], s->qscale);
  92. }
  93. }
  94. }
  95. /* left copy */
  96. for (i = 1; i < 8; i++)
  97. ac_val1[i] = block[s->idsp.idct_permutation[i << 3]];
  98. /* top copy */
  99. for (i = 1; i < 8; i++)
  100. ac_val1[8 + i] = block[s->idsp.idct_permutation[i]];
  101. }
  102. /**
  103. * check if the next stuff is a resync marker or the end.
  104. * @return 0 if not
  105. */
  106. static inline int mpeg4_is_resync(Mpeg4DecContext *ctx)
  107. {
  108. MpegEncContext *s = &ctx->m;
  109. int bits_count = get_bits_count(&s->gb);
  110. int v = show_bits(&s->gb, 16);
  111. if (s->workaround_bugs & FF_BUG_NO_PADDING && !ctx->resync_marker)
  112. return 0;
  113. while (v <= 0xFF) {
  114. if (s->pict_type == AV_PICTURE_TYPE_B ||
  115. (v >> (8 - s->pict_type) != 1) || s->partitioned_frame)
  116. break;
  117. skip_bits(&s->gb, 8 + s->pict_type);
  118. bits_count += 8 + s->pict_type;
  119. v = show_bits(&s->gb, 16);
  120. }
  121. if (bits_count + 8 >= s->gb.size_in_bits) {
  122. v >>= 8;
  123. v |= 0x7F >> (7 - (bits_count & 7));
  124. if (v == 0x7F)
  125. return s->mb_num;
  126. } else {
  127. if (v == ff_mpeg4_resync_prefix[bits_count & 7]) {
  128. int len, mb_num;
  129. int mb_num_bits = av_log2(s->mb_num - 1) + 1;
  130. GetBitContext gb = s->gb;
  131. skip_bits(&s->gb, 1);
  132. align_get_bits(&s->gb);
  133. for (len = 0; len < 32; len++)
  134. if (get_bits1(&s->gb))
  135. break;
  136. mb_num = get_bits(&s->gb, mb_num_bits);
  137. if (!mb_num || mb_num > s->mb_num || get_bits_count(&s->gb)+6 > s->gb.size_in_bits)
  138. mb_num= -1;
  139. s->gb = gb;
  140. if (len >= ff_mpeg4_get_video_packet_prefix_length(s))
  141. return mb_num;
  142. }
  143. }
  144. return 0;
  145. }
  146. static int mpeg4_decode_sprite_trajectory(Mpeg4DecContext *ctx, GetBitContext *gb)
  147. {
  148. MpegEncContext *s = &ctx->m;
  149. int a = 2 << s->sprite_warping_accuracy;
  150. int rho = 3 - s->sprite_warping_accuracy;
  151. int r = 16 / a;
  152. int alpha = 1;
  153. int beta = 0;
  154. int w = s->width;
  155. int h = s->height;
  156. int min_ab, i, w2, h2, w3, h3;
  157. int sprite_ref[4][2];
  158. int virtual_ref[2][2];
  159. int64_t sprite_offset[2][2];
  160. int64_t sprite_delta[2][2];
  161. // only true for rectangle shapes
  162. const int vop_ref[4][2] = { { 0, 0 }, { s->width, 0 },
  163. { 0, s->height }, { s->width, s->height } };
  164. int d[4][2] = { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 } };
  165. if (w <= 0 || h <= 0)
  166. return AVERROR_INVALIDDATA;
  167. for (i = 0; i < ctx->num_sprite_warping_points; i++) {
  168. int length;
  169. int x = 0, y = 0;
  170. length = get_vlc2(gb, sprite_trajectory.table, SPRITE_TRAJ_VLC_BITS, 3);
  171. if (length > 0)
  172. x = get_xbits(gb, length);
  173. if (!(ctx->divx_version == 500 && ctx->divx_build == 413))
  174. check_marker(gb, "before sprite_trajectory");
  175. length = get_vlc2(gb, sprite_trajectory.table, SPRITE_TRAJ_VLC_BITS, 3);
  176. if (length > 0)
  177. y = get_xbits(gb, length);
  178. check_marker(gb, "after sprite_trajectory");
  179. ctx->sprite_traj[i][0] = d[i][0] = x;
  180. ctx->sprite_traj[i][1] = d[i][1] = y;
  181. }
  182. for (; i < 4; i++)
  183. ctx->sprite_traj[i][0] = ctx->sprite_traj[i][1] = 0;
  184. while ((1 << alpha) < w)
  185. alpha++;
  186. while ((1 << beta) < h)
  187. beta++; /* typo in the mpeg4 std for the definition of w' and h' */
  188. w2 = 1 << alpha;
  189. h2 = 1 << beta;
  190. // Note, the 4th point isn't used for GMC
  191. if (ctx->divx_version == 500 && ctx->divx_build == 413) {
  192. sprite_ref[0][0] = a * vop_ref[0][0] + d[0][0];
  193. sprite_ref[0][1] = a * vop_ref[0][1] + d[0][1];
  194. sprite_ref[1][0] = a * vop_ref[1][0] + d[0][0] + d[1][0];
  195. sprite_ref[1][1] = a * vop_ref[1][1] + d[0][1] + d[1][1];
  196. sprite_ref[2][0] = a * vop_ref[2][0] + d[0][0] + d[2][0];
  197. sprite_ref[2][1] = a * vop_ref[2][1] + d[0][1] + d[2][1];
  198. } else {
  199. sprite_ref[0][0] = (a >> 1) * (2 * vop_ref[0][0] + d[0][0]);
  200. sprite_ref[0][1] = (a >> 1) * (2 * vop_ref[0][1] + d[0][1]);
  201. sprite_ref[1][0] = (a >> 1) * (2 * vop_ref[1][0] + d[0][0] + d[1][0]);
  202. sprite_ref[1][1] = (a >> 1) * (2 * vop_ref[1][1] + d[0][1] + d[1][1]);
  203. sprite_ref[2][0] = (a >> 1) * (2 * vop_ref[2][0] + d[0][0] + d[2][0]);
  204. sprite_ref[2][1] = (a >> 1) * (2 * vop_ref[2][1] + d[0][1] + d[2][1]);
  205. }
  206. /* sprite_ref[3][0] = (a >> 1) * (2 * vop_ref[3][0] + d[0][0] + d[1][0] + d[2][0] + d[3][0]);
  207. * sprite_ref[3][1] = (a >> 1) * (2 * vop_ref[3][1] + d[0][1] + d[1][1] + d[2][1] + d[3][1]); */
  208. /* this is mostly identical to the mpeg4 std (and is totally unreadable
  209. * because of that...). Perhaps it should be reordered to be more readable.
  210. * The idea behind this virtual_ref mess is to be able to use shifts later
  211. * per pixel instead of divides so the distance between points is converted
  212. * from w&h based to w2&h2 based which are of the 2^x form. */
  213. virtual_ref[0][0] = 16 * (vop_ref[0][0] + w2) +
  214. ROUNDED_DIV(((w - w2) *
  215. (r * sprite_ref[0][0] - 16LL * vop_ref[0][0]) +
  216. w2 * (r * sprite_ref[1][0] - 16LL * vop_ref[1][0])), w);
  217. virtual_ref[0][1] = 16 * vop_ref[0][1] +
  218. ROUNDED_DIV(((w - w2) *
  219. (r * sprite_ref[0][1] - 16LL * vop_ref[0][1]) +
  220. w2 * (r * sprite_ref[1][1] - 16LL * vop_ref[1][1])), w);
  221. virtual_ref[1][0] = 16 * vop_ref[0][0] +
  222. ROUNDED_DIV(((h - h2) * (r * sprite_ref[0][0] - 16LL * vop_ref[0][0]) +
  223. h2 * (r * sprite_ref[2][0] - 16LL * vop_ref[2][0])), h);
  224. virtual_ref[1][1] = 16 * (vop_ref[0][1] + h2) +
  225. ROUNDED_DIV(((h - h2) * (r * sprite_ref[0][1] - 16LL * vop_ref[0][1]) +
  226. h2 * (r * sprite_ref[2][1] - 16LL * vop_ref[2][1])), h);
  227. switch (ctx->num_sprite_warping_points) {
  228. case 0:
  229. sprite_offset[0][0] =
  230. sprite_offset[0][1] =
  231. sprite_offset[1][0] =
  232. sprite_offset[1][1] = 0;
  233. sprite_delta[0][0] = a;
  234. sprite_delta[0][1] =
  235. sprite_delta[1][0] = 0;
  236. sprite_delta[1][1] = a;
  237. ctx->sprite_shift[0] =
  238. ctx->sprite_shift[1] = 0;
  239. break;
  240. case 1: // GMC only
  241. sprite_offset[0][0] = sprite_ref[0][0] - a * vop_ref[0][0];
  242. sprite_offset[0][1] = sprite_ref[0][1] - a * vop_ref[0][1];
  243. sprite_offset[1][0] = ((sprite_ref[0][0] >> 1) | (sprite_ref[0][0] & 1)) -
  244. a * (vop_ref[0][0] / 2);
  245. sprite_offset[1][1] = ((sprite_ref[0][1] >> 1) | (sprite_ref[0][1] & 1)) -
  246. a * (vop_ref[0][1] / 2);
  247. sprite_delta[0][0] = a;
  248. sprite_delta[0][1] =
  249. sprite_delta[1][0] = 0;
  250. sprite_delta[1][1] = a;
  251. ctx->sprite_shift[0] =
  252. ctx->sprite_shift[1] = 0;
  253. break;
  254. case 2:
  255. sprite_offset[0][0] = ((int64_t) sprite_ref[0][0] * (1 << alpha + rho)) +
  256. ((int64_t) -r * sprite_ref[0][0] + virtual_ref[0][0]) *
  257. ((int64_t) -vop_ref[0][0]) +
  258. ((int64_t) r * sprite_ref[0][1] - virtual_ref[0][1]) *
  259. ((int64_t) -vop_ref[0][1]) + (1 << (alpha + rho - 1));
  260. sprite_offset[0][1] = ((int64_t) sprite_ref[0][1] * (1 << alpha + rho)) +
  261. ((int64_t) -r * sprite_ref[0][1] + virtual_ref[0][1]) *
  262. ((int64_t) -vop_ref[0][0]) +
  263. ((int64_t) -r * sprite_ref[0][0] + virtual_ref[0][0]) *
  264. ((int64_t) -vop_ref[0][1]) + (1 << (alpha + rho - 1));
  265. sprite_offset[1][0] = (((int64_t)-r * sprite_ref[0][0] + virtual_ref[0][0]) *
  266. ((int64_t)-2 * vop_ref[0][0] + 1) +
  267. ((int64_t) r * sprite_ref[0][1] - virtual_ref[0][1]) *
  268. ((int64_t)-2 * vop_ref[0][1] + 1) + 2 * w2 * r *
  269. (int64_t) sprite_ref[0][0] - 16 * w2 + (1 << (alpha + rho + 1)));
  270. sprite_offset[1][1] = (((int64_t)-r * sprite_ref[0][1] + virtual_ref[0][1]) *
  271. ((int64_t)-2 * vop_ref[0][0] + 1) +
  272. ((int64_t)-r * sprite_ref[0][0] + virtual_ref[0][0]) *
  273. ((int64_t)-2 * vop_ref[0][1] + 1) + 2 * w2 * r *
  274. (int64_t) sprite_ref[0][1] - 16 * w2 + (1 << (alpha + rho + 1)));
  275. sprite_delta[0][0] = (-r * sprite_ref[0][0] + virtual_ref[0][0]);
  276. sprite_delta[0][1] = (+r * sprite_ref[0][1] - virtual_ref[0][1]);
  277. sprite_delta[1][0] = (-r * sprite_ref[0][1] + virtual_ref[0][1]);
  278. sprite_delta[1][1] = (-r * sprite_ref[0][0] + virtual_ref[0][0]);
  279. ctx->sprite_shift[0] = alpha + rho;
  280. ctx->sprite_shift[1] = alpha + rho + 2;
  281. break;
  282. case 3:
  283. min_ab = FFMIN(alpha, beta);
  284. w3 = w2 >> min_ab;
  285. h3 = h2 >> min_ab;
  286. sprite_offset[0][0] = ((int64_t)sprite_ref[0][0] * (1 << (alpha + beta + rho - min_ab))) +
  287. ((int64_t)-r * sprite_ref[0][0] + virtual_ref[0][0]) * h3 * (-vop_ref[0][0]) +
  288. ((int64_t)-r * sprite_ref[0][0] + virtual_ref[1][0]) * w3 * (-vop_ref[0][1]) +
  289. ((int64_t)1 << (alpha + beta + rho - min_ab - 1));
  290. sprite_offset[0][1] = ((int64_t)sprite_ref[0][1] * (1 << (alpha + beta + rho - min_ab))) +
  291. ((int64_t)-r * sprite_ref[0][1] + virtual_ref[0][1]) * h3 * (-vop_ref[0][0]) +
  292. ((int64_t)-r * sprite_ref[0][1] + virtual_ref[1][1]) * w3 * (-vop_ref[0][1]) +
  293. ((int64_t)1 << (alpha + beta + rho - min_ab - 1));
  294. sprite_offset[1][0] = ((int64_t)-r * sprite_ref[0][0] + virtual_ref[0][0]) * h3 * (-2 * vop_ref[0][0] + 1) +
  295. ((int64_t)-r * sprite_ref[0][0] + virtual_ref[1][0]) * w3 * (-2 * vop_ref[0][1] + 1) +
  296. (int64_t)2 * w2 * h3 * r * sprite_ref[0][0] - 16 * w2 * h3 +
  297. ((int64_t)1 << (alpha + beta + rho - min_ab + 1));
  298. sprite_offset[1][1] = ((int64_t)-r * sprite_ref[0][1] + virtual_ref[0][1]) * h3 * (-2 * vop_ref[0][0] + 1) +
  299. ((int64_t)-r * sprite_ref[0][1] + virtual_ref[1][1]) * w3 * (-2 * vop_ref[0][1] + 1) +
  300. (int64_t)2 * w2 * h3 * r * sprite_ref[0][1] - 16 * w2 * h3 +
  301. ((int64_t)1 << (alpha + beta + rho - min_ab + 1));
  302. sprite_delta[0][0] = (-r * (int64_t)sprite_ref[0][0] + virtual_ref[0][0]) * h3;
  303. sprite_delta[0][1] = (-r * (int64_t)sprite_ref[0][0] + virtual_ref[1][0]) * w3;
  304. sprite_delta[1][0] = (-r * (int64_t)sprite_ref[0][1] + virtual_ref[0][1]) * h3;
  305. sprite_delta[1][1] = (-r * (int64_t)sprite_ref[0][1] + virtual_ref[1][1]) * w3;
  306. ctx->sprite_shift[0] = alpha + beta + rho - min_ab;
  307. ctx->sprite_shift[1] = alpha + beta + rho - min_ab + 2;
  308. break;
  309. }
  310. /* try to simplify the situation */
  311. if (sprite_delta[0][0] == a << ctx->sprite_shift[0] &&
  312. sprite_delta[0][1] == 0 &&
  313. sprite_delta[1][0] == 0 &&
  314. sprite_delta[1][1] == a << ctx->sprite_shift[0]) {
  315. sprite_offset[0][0] >>= ctx->sprite_shift[0];
  316. sprite_offset[0][1] >>= ctx->sprite_shift[0];
  317. sprite_offset[1][0] >>= ctx->sprite_shift[1];
  318. sprite_offset[1][1] >>= ctx->sprite_shift[1];
  319. sprite_delta[0][0] = a;
  320. sprite_delta[0][1] = 0;
  321. sprite_delta[1][0] = 0;
  322. sprite_delta[1][1] = a;
  323. ctx->sprite_shift[0] = 0;
  324. ctx->sprite_shift[1] = 0;
  325. s->real_sprite_warping_points = 1;
  326. } else {
  327. int shift_y = 16 - ctx->sprite_shift[0];
  328. int shift_c = 16 - ctx->sprite_shift[1];
  329. for (i = 0; i < 2; i++) {
  330. if (shift_c < 0 || shift_y < 0 ||
  331. FFABS( sprite_offset[0][i]) >= INT_MAX >> shift_y ||
  332. FFABS( sprite_offset[1][i]) >= INT_MAX >> shift_c ||
  333. FFABS( sprite_delta[0][i]) >= INT_MAX >> shift_y ||
  334. FFABS( sprite_delta[1][i]) >= INT_MAX >> shift_y
  335. ) {
  336. avpriv_request_sample(s->avctx, "Too large sprite shift, delta or offset");
  337. goto overflow;
  338. }
  339. }
  340. for (i = 0; i < 2; i++) {
  341. sprite_offset[0][i] *= 1 << shift_y;
  342. sprite_offset[1][i] *= 1 << shift_c;
  343. sprite_delta[0][i] *= 1 << shift_y;
  344. sprite_delta[1][i] *= 1 << shift_y;
  345. ctx->sprite_shift[i] = 16;
  346. }
  347. for (i = 0; i < 2; i++) {
  348. int64_t sd[2] = {
  349. sprite_delta[i][0] - a * (1LL<<16),
  350. sprite_delta[i][1] - a * (1LL<<16)
  351. };
  352. if (llabs(sprite_offset[0][i] + sprite_delta[i][0] * (w+16LL)) >= INT_MAX ||
  353. llabs(sprite_offset[0][i] + sprite_delta[i][1] * (h+16LL)) >= INT_MAX ||
  354. llabs(sprite_offset[0][i] + sprite_delta[i][0] * (w+16LL) + sprite_delta[i][1] * (h+16LL)) >= INT_MAX ||
  355. llabs(sprite_delta[i][0] * (w+16LL)) >= INT_MAX ||
  356. llabs(sprite_delta[i][1] * (h+16LL)) >= INT_MAX ||
  357. llabs(sd[0]) >= INT_MAX ||
  358. llabs(sd[1]) >= INT_MAX ||
  359. llabs(sprite_offset[0][i] + sd[0] * (w+16LL)) >= INT_MAX ||
  360. llabs(sprite_offset[0][i] + sd[1] * (h+16LL)) >= INT_MAX ||
  361. llabs(sprite_offset[0][i] + sd[0] * (w+16LL) + sd[1] * (h+16LL)) >= INT_MAX
  362. ) {
  363. avpriv_request_sample(s->avctx, "Overflow on sprite points");
  364. goto overflow;
  365. }
  366. }
  367. s->real_sprite_warping_points = ctx->num_sprite_warping_points;
  368. }
  369. for (i = 0; i < 4; i++) {
  370. s->sprite_offset[i&1][i>>1] = sprite_offset[i&1][i>>1];
  371. s->sprite_delta [i&1][i>>1] = sprite_delta [i&1][i>>1];
  372. }
  373. return 0;
  374. overflow:
  375. memset(s->sprite_offset, 0, sizeof(s->sprite_offset));
  376. memset(s->sprite_delta, 0, sizeof(s->sprite_delta));
  377. return AVERROR_PATCHWELCOME;
  378. }
  379. static int decode_new_pred(Mpeg4DecContext *ctx, GetBitContext *gb) {
  380. int len = FFMIN(ctx->time_increment_bits + 3, 15);
  381. get_bits(gb, len);
  382. if (get_bits1(gb))
  383. get_bits(gb, len);
  384. check_marker(gb, "after new_pred");
  385. return 0;
  386. }
  387. /**
  388. * Decode the next video packet.
  389. * @return <0 if something went wrong
  390. */
  391. int ff_mpeg4_decode_video_packet_header(Mpeg4DecContext *ctx)
  392. {
  393. MpegEncContext *s = &ctx->m;
  394. int mb_num_bits = av_log2(s->mb_num - 1) + 1;
  395. int header_extension = 0, mb_num, len;
  396. /* is there enough space left for a video packet + header */
  397. if (get_bits_count(&s->gb) > s->gb.size_in_bits - 20)
  398. return -1;
  399. for (len = 0; len < 32; len++)
  400. if (get_bits1(&s->gb))
  401. break;
  402. if (len != ff_mpeg4_get_video_packet_prefix_length(s)) {
  403. av_log(s->avctx, AV_LOG_ERROR, "marker does not match f_code\n");
  404. return -1;
  405. }
  406. if (ctx->shape != RECT_SHAPE) {
  407. header_extension = get_bits1(&s->gb);
  408. // FIXME more stuff here
  409. }
  410. mb_num = get_bits(&s->gb, mb_num_bits);
  411. if (mb_num >= s->mb_num || !mb_num) {
  412. av_log(s->avctx, AV_LOG_ERROR,
  413. "illegal mb_num in video packet (%d %d) \n", mb_num, s->mb_num);
  414. return -1;
  415. }
  416. s->mb_x = mb_num % s->mb_width;
  417. s->mb_y = mb_num / s->mb_width;
  418. if (ctx->shape != BIN_ONLY_SHAPE) {
  419. int qscale = get_bits(&s->gb, s->quant_precision);
  420. if (qscale)
  421. s->chroma_qscale = s->qscale = qscale;
  422. }
  423. if (ctx->shape == RECT_SHAPE)
  424. header_extension = get_bits1(&s->gb);
  425. if (header_extension) {
  426. int time_incr = 0;
  427. while (get_bits1(&s->gb) != 0)
  428. time_incr++;
  429. check_marker(&s->gb, "before time_increment in video packed header");
  430. skip_bits(&s->gb, ctx->time_increment_bits); /* time_increment */
  431. check_marker(&s->gb, "before vop_coding_type in video packed header");
  432. skip_bits(&s->gb, 2); /* vop coding type */
  433. // FIXME not rect stuff here
  434. if (ctx->shape != BIN_ONLY_SHAPE) {
  435. skip_bits(&s->gb, 3); /* intra dc vlc threshold */
  436. // FIXME don't just ignore everything
  437. if (s->pict_type == AV_PICTURE_TYPE_S &&
  438. ctx->vol_sprite_usage == GMC_SPRITE) {
  439. if (mpeg4_decode_sprite_trajectory(ctx, &s->gb) < 0)
  440. return AVERROR_INVALIDDATA;
  441. av_log(s->avctx, AV_LOG_ERROR, "untested\n");
  442. }
  443. // FIXME reduced res stuff here
  444. if (s->pict_type != AV_PICTURE_TYPE_I) {
  445. int f_code = get_bits(&s->gb, 3); /* fcode_for */
  446. if (f_code == 0)
  447. av_log(s->avctx, AV_LOG_ERROR,
  448. "Error, video packet header damaged (f_code=0)\n");
  449. }
  450. if (s->pict_type == AV_PICTURE_TYPE_B) {
  451. int b_code = get_bits(&s->gb, 3);
  452. if (b_code == 0)
  453. av_log(s->avctx, AV_LOG_ERROR,
  454. "Error, video packet header damaged (b_code=0)\n");
  455. }
  456. }
  457. }
  458. if (ctx->new_pred)
  459. decode_new_pred(ctx, &s->gb);
  460. return 0;
  461. }
  462. /**
  463. * Get the average motion vector for a GMC MB.
  464. * @param n either 0 for the x component or 1 for y
  465. * @return the average MV for a GMC MB
  466. */
  467. static inline int get_amv(Mpeg4DecContext *ctx, int n)
  468. {
  469. MpegEncContext *s = &ctx->m;
  470. int x, y, mb_v, sum, dx, dy, shift;
  471. int len = 1 << (s->f_code + 4);
  472. const int a = s->sprite_warping_accuracy;
  473. if (s->workaround_bugs & FF_BUG_AMV)
  474. len >>= s->quarter_sample;
  475. if (s->real_sprite_warping_points == 1) {
  476. if (ctx->divx_version == 500 && ctx->divx_build == 413 && a >= s->quarter_sample)
  477. sum = s->sprite_offset[0][n] / (1 << (a - s->quarter_sample));
  478. else
  479. sum = RSHIFT(s->sprite_offset[0][n] * (1 << s->quarter_sample), a);
  480. } else {
  481. dx = s->sprite_delta[n][0];
  482. dy = s->sprite_delta[n][1];
  483. shift = ctx->sprite_shift[0];
  484. if (n)
  485. dy -= 1 << (shift + a + 1);
  486. else
  487. dx -= 1 << (shift + a + 1);
  488. mb_v = s->sprite_offset[0][n] + dx * s->mb_x * 16 + dy * s->mb_y * 16;
  489. sum = 0;
  490. for (y = 0; y < 16; y++) {
  491. int v;
  492. v = mb_v + dy * y;
  493. // FIXME optimize
  494. for (x = 0; x < 16; x++) {
  495. sum += v >> shift;
  496. v += dx;
  497. }
  498. }
  499. sum = RSHIFT(sum, a + 8 - s->quarter_sample);
  500. }
  501. if (sum < -len)
  502. sum = -len;
  503. else if (sum >= len)
  504. sum = len - 1;
  505. return sum;
  506. }
  507. /**
  508. * Decode the dc value.
  509. * @param n block index (0-3 are luma, 4-5 are chroma)
  510. * @param dir_ptr the prediction direction will be stored here
  511. * @return the quantized dc
  512. */
  513. static inline int mpeg4_decode_dc(MpegEncContext *s, int n, int *dir_ptr)
  514. {
  515. int level, code;
  516. if (n < 4)
  517. code = get_vlc2(&s->gb, dc_lum.table, DC_VLC_BITS, 1);
  518. else
  519. code = get_vlc2(&s->gb, dc_chrom.table, DC_VLC_BITS, 1);
  520. if (code < 0 || code > 9 /* && s->nbit < 9 */) {
  521. av_log(s->avctx, AV_LOG_ERROR, "illegal dc vlc\n");
  522. return -1;
  523. }
  524. if (code == 0) {
  525. level = 0;
  526. } else {
  527. if (IS_3IV1) {
  528. if (code == 1)
  529. level = 2 * get_bits1(&s->gb) - 1;
  530. else {
  531. if (get_bits1(&s->gb))
  532. level = get_bits(&s->gb, code - 1) + (1 << (code - 1));
  533. else
  534. level = -get_bits(&s->gb, code - 1) - (1 << (code - 1));
  535. }
  536. } else {
  537. level = get_xbits(&s->gb, code);
  538. }
  539. if (code > 8) {
  540. if (get_bits1(&s->gb) == 0) { /* marker */
  541. if (s->avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT)) {
  542. av_log(s->avctx, AV_LOG_ERROR, "dc marker bit missing\n");
  543. return -1;
  544. }
  545. }
  546. }
  547. }
  548. return ff_mpeg4_pred_dc(s, n, level, dir_ptr, 0);
  549. }
  550. /**
  551. * Decode first partition.
  552. * @return number of MBs decoded or <0 if an error occurred
  553. */
  554. static int mpeg4_decode_partition_a(Mpeg4DecContext *ctx)
  555. {
  556. MpegEncContext *s = &ctx->m;
  557. int mb_num = 0;
  558. static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
  559. /* decode first partition */
  560. s->first_slice_line = 1;
  561. for (; s->mb_y < s->mb_height; s->mb_y++) {
  562. ff_init_block_index(s);
  563. for (; s->mb_x < s->mb_width; s->mb_x++) {
  564. const int xy = s->mb_x + s->mb_y * s->mb_stride;
  565. int cbpc;
  566. int dir = 0;
  567. mb_num++;
  568. ff_update_block_index(s);
  569. if (s->mb_x == s->resync_mb_x && s->mb_y == s->resync_mb_y + 1)
  570. s->first_slice_line = 0;
  571. if (s->pict_type == AV_PICTURE_TYPE_I) {
  572. int i;
  573. do {
  574. if (show_bits_long(&s->gb, 19) == DC_MARKER)
  575. return mb_num - 1;
  576. cbpc = get_vlc2(&s->gb, ff_h263_intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 2);
  577. if (cbpc < 0) {
  578. av_log(s->avctx, AV_LOG_ERROR,
  579. "mcbpc corrupted at %d %d\n", s->mb_x, s->mb_y);
  580. return -1;
  581. }
  582. } while (cbpc == 8);
  583. s->cbp_table[xy] = cbpc & 3;
  584. s->current_picture.mb_type[xy] = MB_TYPE_INTRA;
  585. s->mb_intra = 1;
  586. if (cbpc & 4)
  587. ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
  588. s->current_picture.qscale_table[xy] = s->qscale;
  589. s->mbintra_table[xy] = 1;
  590. for (i = 0; i < 6; i++) {
  591. int dc_pred_dir;
  592. int dc = mpeg4_decode_dc(s, i, &dc_pred_dir);
  593. if (dc < 0) {
  594. av_log(s->avctx, AV_LOG_ERROR,
  595. "DC corrupted at %d %d\n", s->mb_x, s->mb_y);
  596. return -1;
  597. }
  598. dir <<= 1;
  599. if (dc_pred_dir)
  600. dir |= 1;
  601. }
  602. s->pred_dir_table[xy] = dir;
  603. } else { /* P/S_TYPE */
  604. int mx, my, pred_x, pred_y, bits;
  605. int16_t *const mot_val = s->current_picture.motion_val[0][s->block_index[0]];
  606. const int stride = s->b8_stride * 2;
  607. try_again:
  608. bits = show_bits(&s->gb, 17);
  609. if (bits == MOTION_MARKER)
  610. return mb_num - 1;
  611. skip_bits1(&s->gb);
  612. if (bits & 0x10000) {
  613. /* skip mb */
  614. if (s->pict_type == AV_PICTURE_TYPE_S &&
  615. ctx->vol_sprite_usage == GMC_SPRITE) {
  616. s->current_picture.mb_type[xy] = MB_TYPE_SKIP |
  617. MB_TYPE_16x16 |
  618. MB_TYPE_GMC |
  619. MB_TYPE_L0;
  620. mx = get_amv(ctx, 0);
  621. my = get_amv(ctx, 1);
  622. } else {
  623. s->current_picture.mb_type[xy] = MB_TYPE_SKIP |
  624. MB_TYPE_16x16 |
  625. MB_TYPE_L0;
  626. mx = my = 0;
  627. }
  628. mot_val[0] =
  629. mot_val[2] =
  630. mot_val[0 + stride] =
  631. mot_val[2 + stride] = mx;
  632. mot_val[1] =
  633. mot_val[3] =
  634. mot_val[1 + stride] =
  635. mot_val[3 + stride] = my;
  636. if (s->mbintra_table[xy])
  637. ff_clean_intra_table_entries(s);
  638. continue;
  639. }
  640. cbpc = get_vlc2(&s->gb, ff_h263_inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
  641. if (cbpc < 0) {
  642. av_log(s->avctx, AV_LOG_ERROR,
  643. "mcbpc corrupted at %d %d\n", s->mb_x, s->mb_y);
  644. return -1;
  645. }
  646. if (cbpc == 20)
  647. goto try_again;
  648. s->cbp_table[xy] = cbpc & (8 + 3); // 8 is dquant
  649. s->mb_intra = ((cbpc & 4) != 0);
  650. if (s->mb_intra) {
  651. s->current_picture.mb_type[xy] = MB_TYPE_INTRA;
  652. s->mbintra_table[xy] = 1;
  653. mot_val[0] =
  654. mot_val[2] =
  655. mot_val[0 + stride] =
  656. mot_val[2 + stride] = 0;
  657. mot_val[1] =
  658. mot_val[3] =
  659. mot_val[1 + stride] =
  660. mot_val[3 + stride] = 0;
  661. } else {
  662. if (s->mbintra_table[xy])
  663. ff_clean_intra_table_entries(s);
  664. if (s->pict_type == AV_PICTURE_TYPE_S &&
  665. ctx->vol_sprite_usage == GMC_SPRITE &&
  666. (cbpc & 16) == 0)
  667. s->mcsel = get_bits1(&s->gb);
  668. else
  669. s->mcsel = 0;
  670. if ((cbpc & 16) == 0) {
  671. /* 16x16 motion prediction */
  672. ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
  673. if (!s->mcsel) {
  674. mx = ff_h263_decode_motion(s, pred_x, s->f_code);
  675. if (mx >= 0xffff)
  676. return -1;
  677. my = ff_h263_decode_motion(s, pred_y, s->f_code);
  678. if (my >= 0xffff)
  679. return -1;
  680. s->current_picture.mb_type[xy] = MB_TYPE_16x16 |
  681. MB_TYPE_L0;
  682. } else {
  683. mx = get_amv(ctx, 0);
  684. my = get_amv(ctx, 1);
  685. s->current_picture.mb_type[xy] = MB_TYPE_16x16 |
  686. MB_TYPE_GMC |
  687. MB_TYPE_L0;
  688. }
  689. mot_val[0] =
  690. mot_val[2] =
  691. mot_val[0 + stride] =
  692. mot_val[2 + stride] = mx;
  693. mot_val[1] =
  694. mot_val[3] =
  695. mot_val[1 + stride] =
  696. mot_val[3 + stride] = my;
  697. } else {
  698. int i;
  699. s->current_picture.mb_type[xy] = MB_TYPE_8x8 |
  700. MB_TYPE_L0;
  701. for (i = 0; i < 4; i++) {
  702. int16_t *mot_val = ff_h263_pred_motion(s, i, 0, &pred_x, &pred_y);
  703. mx = ff_h263_decode_motion(s, pred_x, s->f_code);
  704. if (mx >= 0xffff)
  705. return -1;
  706. my = ff_h263_decode_motion(s, pred_y, s->f_code);
  707. if (my >= 0xffff)
  708. return -1;
  709. mot_val[0] = mx;
  710. mot_val[1] = my;
  711. }
  712. }
  713. }
  714. }
  715. }
  716. s->mb_x = 0;
  717. }
  718. return mb_num;
  719. }
  720. /**
  721. * decode second partition.
  722. * @return <0 if an error occurred
  723. */
  724. static int mpeg4_decode_partition_b(MpegEncContext *s, int mb_count)
  725. {
  726. int mb_num = 0;
  727. static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
  728. s->mb_x = s->resync_mb_x;
  729. s->first_slice_line = 1;
  730. for (s->mb_y = s->resync_mb_y; mb_num < mb_count; s->mb_y++) {
  731. ff_init_block_index(s);
  732. for (; mb_num < mb_count && s->mb_x < s->mb_width; s->mb_x++) {
  733. const int xy = s->mb_x + s->mb_y * s->mb_stride;
  734. mb_num++;
  735. ff_update_block_index(s);
  736. if (s->mb_x == s->resync_mb_x && s->mb_y == s->resync_mb_y + 1)
  737. s->first_slice_line = 0;
  738. if (s->pict_type == AV_PICTURE_TYPE_I) {
  739. int ac_pred = get_bits1(&s->gb);
  740. int cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
  741. if (cbpy < 0) {
  742. av_log(s->avctx, AV_LOG_ERROR,
  743. "cbpy corrupted at %d %d\n", s->mb_x, s->mb_y);
  744. return -1;
  745. }
  746. s->cbp_table[xy] |= cbpy << 2;
  747. s->current_picture.mb_type[xy] |= ac_pred * MB_TYPE_ACPRED;
  748. } else { /* P || S_TYPE */
  749. if (IS_INTRA(s->current_picture.mb_type[xy])) {
  750. int i;
  751. int dir = 0;
  752. int ac_pred = get_bits1(&s->gb);
  753. int cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
  754. if (cbpy < 0) {
  755. av_log(s->avctx, AV_LOG_ERROR,
  756. "I cbpy corrupted at %d %d\n", s->mb_x, s->mb_y);
  757. return -1;
  758. }
  759. if (s->cbp_table[xy] & 8)
  760. ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
  761. s->current_picture.qscale_table[xy] = s->qscale;
  762. for (i = 0; i < 6; i++) {
  763. int dc_pred_dir;
  764. int dc = mpeg4_decode_dc(s, i, &dc_pred_dir);
  765. if (dc < 0) {
  766. av_log(s->avctx, AV_LOG_ERROR,
  767. "DC corrupted at %d %d\n", s->mb_x, s->mb_y);
  768. return -1;
  769. }
  770. dir <<= 1;
  771. if (dc_pred_dir)
  772. dir |= 1;
  773. }
  774. s->cbp_table[xy] &= 3; // remove dquant
  775. s->cbp_table[xy] |= cbpy << 2;
  776. s->current_picture.mb_type[xy] |= ac_pred * MB_TYPE_ACPRED;
  777. s->pred_dir_table[xy] = dir;
  778. } else if (IS_SKIP(s->current_picture.mb_type[xy])) {
  779. s->current_picture.qscale_table[xy] = s->qscale;
  780. s->cbp_table[xy] = 0;
  781. } else {
  782. int cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
  783. if (cbpy < 0) {
  784. av_log(s->avctx, AV_LOG_ERROR,
  785. "P cbpy corrupted at %d %d\n", s->mb_x, s->mb_y);
  786. return -1;
  787. }
  788. if (s->cbp_table[xy] & 8)
  789. ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
  790. s->current_picture.qscale_table[xy] = s->qscale;
  791. s->cbp_table[xy] &= 3; // remove dquant
  792. s->cbp_table[xy] |= (cbpy ^ 0xf) << 2;
  793. }
  794. }
  795. }
  796. if (mb_num >= mb_count)
  797. return 0;
  798. s->mb_x = 0;
  799. }
  800. return 0;
  801. }
  802. /**
  803. * Decode the first and second partition.
  804. * @return <0 if error (and sets error type in the error_status_table)
  805. */
  806. int ff_mpeg4_decode_partitions(Mpeg4DecContext *ctx)
  807. {
  808. MpegEncContext *s = &ctx->m;
  809. int mb_num;
  810. const int part_a_error = s->pict_type == AV_PICTURE_TYPE_I ? (ER_DC_ERROR | ER_MV_ERROR) : ER_MV_ERROR;
  811. const int part_a_end = s->pict_type == AV_PICTURE_TYPE_I ? (ER_DC_END | ER_MV_END) : ER_MV_END;
  812. mb_num = mpeg4_decode_partition_a(ctx);
  813. if (mb_num <= 0) {
  814. ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
  815. s->mb_x, s->mb_y, part_a_error);
  816. return -1;
  817. }
  818. if (s->resync_mb_x + s->resync_mb_y * s->mb_width + mb_num > s->mb_num) {
  819. av_log(s->avctx, AV_LOG_ERROR, "slice below monitor ...\n");
  820. ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
  821. s->mb_x, s->mb_y, part_a_error);
  822. return -1;
  823. }
  824. s->mb_num_left = mb_num;
  825. if (s->pict_type == AV_PICTURE_TYPE_I) {
  826. while (show_bits(&s->gb, 9) == 1)
  827. skip_bits(&s->gb, 9);
  828. if (get_bits_long(&s->gb, 19) != DC_MARKER) {
  829. av_log(s->avctx, AV_LOG_ERROR,
  830. "marker missing after first I partition at %d %d\n",
  831. s->mb_x, s->mb_y);
  832. return -1;
  833. }
  834. } else {
  835. while (show_bits(&s->gb, 10) == 1)
  836. skip_bits(&s->gb, 10);
  837. if (get_bits(&s->gb, 17) != MOTION_MARKER) {
  838. av_log(s->avctx, AV_LOG_ERROR,
  839. "marker missing after first P partition at %d %d\n",
  840. s->mb_x, s->mb_y);
  841. return -1;
  842. }
  843. }
  844. ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
  845. s->mb_x - 1, s->mb_y, part_a_end);
  846. if (mpeg4_decode_partition_b(s, mb_num) < 0) {
  847. if (s->pict_type == AV_PICTURE_TYPE_P)
  848. ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
  849. s->mb_x, s->mb_y, ER_DC_ERROR);
  850. return -1;
  851. } else {
  852. if (s->pict_type == AV_PICTURE_TYPE_P)
  853. ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
  854. s->mb_x - 1, s->mb_y, ER_DC_END);
  855. }
  856. return 0;
  857. }
  858. /**
  859. * Decode a block.
  860. * @return <0 if an error occurred
  861. */
  862. static inline int mpeg4_decode_block(Mpeg4DecContext *ctx, int16_t *block,
  863. int n, int coded, int intra, int rvlc)
  864. {
  865. MpegEncContext *s = &ctx->m;
  866. int level, i, last, run, qmul, qadd;
  867. int av_uninit(dc_pred_dir);
  868. RLTable *rl;
  869. RL_VLC_ELEM *rl_vlc;
  870. const uint8_t *scan_table;
  871. // Note intra & rvlc should be optimized away if this is inlined
  872. if (intra) {
  873. if (ctx->use_intra_dc_vlc) {
  874. /* DC coef */
  875. if (s->partitioned_frame) {
  876. level = s->dc_val[0][s->block_index[n]];
  877. if (n < 4)
  878. level = FASTDIV((level + (s->y_dc_scale >> 1)), s->y_dc_scale);
  879. else
  880. level = FASTDIV((level + (s->c_dc_scale >> 1)), s->c_dc_scale);
  881. dc_pred_dir = (s->pred_dir_table[s->mb_x + s->mb_y * s->mb_stride] << n) & 32;
  882. } else {
  883. level = mpeg4_decode_dc(s, n, &dc_pred_dir);
  884. if (level < 0)
  885. return -1;
  886. }
  887. block[0] = level;
  888. i = 0;
  889. } else {
  890. i = -1;
  891. ff_mpeg4_pred_dc(s, n, 0, &dc_pred_dir, 0);
  892. }
  893. if (!coded)
  894. goto not_coded;
  895. if (rvlc) {
  896. rl = &ff_rvlc_rl_intra;
  897. rl_vlc = ff_rvlc_rl_intra.rl_vlc[0];
  898. } else {
  899. rl = &ff_mpeg4_rl_intra;
  900. rl_vlc = ff_mpeg4_rl_intra.rl_vlc[0];
  901. }
  902. if (s->ac_pred) {
  903. if (dc_pred_dir == 0)
  904. scan_table = s->intra_v_scantable.permutated; /* left */
  905. else
  906. scan_table = s->intra_h_scantable.permutated; /* top */
  907. } else {
  908. scan_table = s->intra_scantable.permutated;
  909. }
  910. qmul = 1;
  911. qadd = 0;
  912. } else {
  913. i = -1;
  914. if (!coded) {
  915. s->block_last_index[n] = i;
  916. return 0;
  917. }
  918. if (rvlc)
  919. rl = &ff_rvlc_rl_inter;
  920. else
  921. rl = &ff_h263_rl_inter;
  922. scan_table = s->intra_scantable.permutated;
  923. if (s->mpeg_quant) {
  924. qmul = 1;
  925. qadd = 0;
  926. if (rvlc)
  927. rl_vlc = ff_rvlc_rl_inter.rl_vlc[0];
  928. else
  929. rl_vlc = ff_h263_rl_inter.rl_vlc[0];
  930. } else {
  931. qmul = s->qscale << 1;
  932. qadd = (s->qscale - 1) | 1;
  933. if (rvlc)
  934. rl_vlc = ff_rvlc_rl_inter.rl_vlc[s->qscale];
  935. else
  936. rl_vlc = ff_h263_rl_inter.rl_vlc[s->qscale];
  937. }
  938. }
  939. {
  940. OPEN_READER(re, &s->gb);
  941. for (;;) {
  942. UPDATE_CACHE(re, &s->gb);
  943. GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 0);
  944. if (level == 0) {
  945. /* escape */
  946. if (rvlc) {
  947. if (SHOW_UBITS(re, &s->gb, 1) == 0) {
  948. av_log(s->avctx, AV_LOG_ERROR,
  949. "1. marker bit missing in rvlc esc\n");
  950. return -1;
  951. }
  952. SKIP_CACHE(re, &s->gb, 1);
  953. last = SHOW_UBITS(re, &s->gb, 1);
  954. SKIP_CACHE(re, &s->gb, 1);
  955. run = SHOW_UBITS(re, &s->gb, 6);
  956. SKIP_COUNTER(re, &s->gb, 1 + 1 + 6);
  957. UPDATE_CACHE(re, &s->gb);
  958. if (SHOW_UBITS(re, &s->gb, 1) == 0) {
  959. av_log(s->avctx, AV_LOG_ERROR,
  960. "2. marker bit missing in rvlc esc\n");
  961. return -1;
  962. }
  963. SKIP_CACHE(re, &s->gb, 1);
  964. level = SHOW_UBITS(re, &s->gb, 11);
  965. SKIP_CACHE(re, &s->gb, 11);
  966. if (SHOW_UBITS(re, &s->gb, 5) != 0x10) {
  967. av_log(s->avctx, AV_LOG_ERROR, "reverse esc missing\n");
  968. return -1;
  969. }
  970. SKIP_CACHE(re, &s->gb, 5);
  971. level = level * qmul + qadd;
  972. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  973. SKIP_COUNTER(re, &s->gb, 1 + 11 + 5 + 1);
  974. i += run + 1;
  975. if (last)
  976. i += 192;
  977. } else {
  978. int cache;
  979. cache = GET_CACHE(re, &s->gb);
  980. if (IS_3IV1)
  981. cache ^= 0xC0000000;
  982. if (cache & 0x80000000) {
  983. if (cache & 0x40000000) {
  984. /* third escape */
  985. SKIP_CACHE(re, &s->gb, 2);
  986. last = SHOW_UBITS(re, &s->gb, 1);
  987. SKIP_CACHE(re, &s->gb, 1);
  988. run = SHOW_UBITS(re, &s->gb, 6);
  989. SKIP_COUNTER(re, &s->gb, 2 + 1 + 6);
  990. UPDATE_CACHE(re, &s->gb);
  991. if (IS_3IV1) {
  992. level = SHOW_SBITS(re, &s->gb, 12);
  993. LAST_SKIP_BITS(re, &s->gb, 12);
  994. } else {
  995. if (SHOW_UBITS(re, &s->gb, 1) == 0) {
  996. av_log(s->avctx, AV_LOG_ERROR,
  997. "1. marker bit missing in 3. esc\n");
  998. if (!(s->avctx->err_recognition & AV_EF_IGNORE_ERR))
  999. return -1;
  1000. }
  1001. SKIP_CACHE(re, &s->gb, 1);
  1002. level = SHOW_SBITS(re, &s->gb, 12);
  1003. SKIP_CACHE(re, &s->gb, 12);
  1004. if (SHOW_UBITS(re, &s->gb, 1) == 0) {
  1005. av_log(s->avctx, AV_LOG_ERROR,
  1006. "2. marker bit missing in 3. esc\n");
  1007. if (!(s->avctx->err_recognition & AV_EF_IGNORE_ERR))
  1008. return -1;
  1009. }
  1010. SKIP_COUNTER(re, &s->gb, 1 + 12 + 1);
  1011. }
  1012. #if 0
  1013. if (s->error_recognition >= FF_ER_COMPLIANT) {
  1014. const int abs_level= FFABS(level);
  1015. if (abs_level<=MAX_LEVEL && run<=MAX_RUN) {
  1016. const int run1= run - rl->max_run[last][abs_level] - 1;
  1017. if (abs_level <= rl->max_level[last][run]) {
  1018. av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, vlc encoding possible\n");
  1019. return -1;
  1020. }
  1021. if (s->error_recognition > FF_ER_COMPLIANT) {
  1022. if (abs_level <= rl->max_level[last][run]*2) {
  1023. av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 1 encoding possible\n");
  1024. return -1;
  1025. }
  1026. if (run1 >= 0 && abs_level <= rl->max_level[last][run1]) {
  1027. av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 2 encoding possible\n");
  1028. return -1;
  1029. }
  1030. }
  1031. }
  1032. }
  1033. #endif
  1034. if (level > 0)
  1035. level = level * qmul + qadd;
  1036. else
  1037. level = level * qmul - qadd;
  1038. if ((unsigned)(level + 2048) > 4095) {
  1039. if (s->avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_AGGRESSIVE)) {
  1040. if (level > 2560 || level < -2560) {
  1041. av_log(s->avctx, AV_LOG_ERROR,
  1042. "|level| overflow in 3. esc, qp=%d\n",
  1043. s->qscale);
  1044. return -1;
  1045. }
  1046. }
  1047. level = level < 0 ? -2048 : 2047;
  1048. }
  1049. i += run + 1;
  1050. if (last)
  1051. i += 192;
  1052. } else {
  1053. /* second escape */
  1054. SKIP_BITS(re, &s->gb, 2);
  1055. GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
  1056. i += run + rl->max_run[run >> 7][level / qmul] + 1; // FIXME opt indexing
  1057. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  1058. LAST_SKIP_BITS(re, &s->gb, 1);
  1059. }
  1060. } else {
  1061. /* first escape */
  1062. SKIP_BITS(re, &s->gb, 1);
  1063. GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
  1064. i += run;
  1065. level = level + rl->max_level[run >> 7][(run - 1) & 63] * qmul; // FIXME opt indexing
  1066. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  1067. LAST_SKIP_BITS(re, &s->gb, 1);
  1068. }
  1069. }
  1070. } else {
  1071. i += run;
  1072. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  1073. LAST_SKIP_BITS(re, &s->gb, 1);
  1074. }
  1075. ff_tlog(s->avctx, "dct[%d][%d] = %- 4d end?:%d\n", scan_table[i&63]&7, scan_table[i&63] >> 3, level, i>62);
  1076. if (i > 62) {
  1077. i -= 192;
  1078. if (i & (~63)) {
  1079. av_log(s->avctx, AV_LOG_ERROR,
  1080. "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
  1081. return -1;
  1082. }
  1083. block[scan_table[i]] = level;
  1084. break;
  1085. }
  1086. block[scan_table[i]] = level;
  1087. }
  1088. CLOSE_READER(re, &s->gb);
  1089. }
  1090. not_coded:
  1091. if (intra) {
  1092. if (!ctx->use_intra_dc_vlc) {
  1093. block[0] = ff_mpeg4_pred_dc(s, n, block[0], &dc_pred_dir, 0);
  1094. i -= i >> 31; // if (i == -1) i = 0;
  1095. }
  1096. ff_mpeg4_pred_ac(s, block, n, dc_pred_dir);
  1097. if (s->ac_pred)
  1098. i = 63; // FIXME not optimal
  1099. }
  1100. s->block_last_index[n] = i;
  1101. return 0;
  1102. }
  1103. /**
  1104. * decode partition C of one MB.
  1105. * @return <0 if an error occurred
  1106. */
  1107. static int mpeg4_decode_partitioned_mb(MpegEncContext *s, int16_t block[6][64])
  1108. {
  1109. Mpeg4DecContext *ctx = s->avctx->priv_data;
  1110. int cbp, mb_type;
  1111. const int xy = s->mb_x + s->mb_y * s->mb_stride;
  1112. av_assert2(s == (void*)ctx);
  1113. mb_type = s->current_picture.mb_type[xy];
  1114. cbp = s->cbp_table[xy];
  1115. ctx->use_intra_dc_vlc = s->qscale < ctx->intra_dc_threshold;
  1116. if (s->current_picture.qscale_table[xy] != s->qscale)
  1117. ff_set_qscale(s, s->current_picture.qscale_table[xy]);
  1118. if (s->pict_type == AV_PICTURE_TYPE_P ||
  1119. s->pict_type == AV_PICTURE_TYPE_S) {
  1120. int i;
  1121. for (i = 0; i < 4; i++) {
  1122. s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
  1123. s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
  1124. }
  1125. s->mb_intra = IS_INTRA(mb_type);
  1126. if (IS_SKIP(mb_type)) {
  1127. /* skip mb */
  1128. for (i = 0; i < 6; i++)
  1129. s->block_last_index[i] = -1;
  1130. s->mv_dir = MV_DIR_FORWARD;
  1131. s->mv_type = MV_TYPE_16X16;
  1132. if (s->pict_type == AV_PICTURE_TYPE_S
  1133. && ctx->vol_sprite_usage == GMC_SPRITE) {
  1134. s->mcsel = 1;
  1135. s->mb_skipped = 0;
  1136. } else {
  1137. s->mcsel = 0;
  1138. s->mb_skipped = 1;
  1139. }
  1140. } else if (s->mb_intra) {
  1141. s->ac_pred = IS_ACPRED(s->current_picture.mb_type[xy]);
  1142. } else if (!s->mb_intra) {
  1143. // s->mcsel = 0; // FIXME do we need to init that?
  1144. s->mv_dir = MV_DIR_FORWARD;
  1145. if (IS_8X8(mb_type)) {
  1146. s->mv_type = MV_TYPE_8X8;
  1147. } else {
  1148. s->mv_type = MV_TYPE_16X16;
  1149. }
  1150. }
  1151. } else { /* I-Frame */
  1152. s->mb_intra = 1;
  1153. s->ac_pred = IS_ACPRED(s->current_picture.mb_type[xy]);
  1154. }
  1155. if (!IS_SKIP(mb_type)) {
  1156. int i;
  1157. s->bdsp.clear_blocks(s->block[0]);
  1158. /* decode each block */
  1159. for (i = 0; i < 6; i++) {
  1160. if (mpeg4_decode_block(ctx, block[i], i, cbp & 32, s->mb_intra, ctx->rvlc) < 0) {
  1161. av_log(s->avctx, AV_LOG_ERROR,
  1162. "texture corrupted at %d %d %d\n",
  1163. s->mb_x, s->mb_y, s->mb_intra);
  1164. return -1;
  1165. }
  1166. cbp += cbp;
  1167. }
  1168. }
  1169. /* per-MB end of slice check */
  1170. if (--s->mb_num_left <= 0) {
  1171. if (mpeg4_is_resync(ctx))
  1172. return SLICE_END;
  1173. else
  1174. return SLICE_NOEND;
  1175. } else {
  1176. if (mpeg4_is_resync(ctx)) {
  1177. const int delta = s->mb_x + 1 == s->mb_width ? 2 : 1;
  1178. if (s->cbp_table[xy + delta])
  1179. return SLICE_END;
  1180. }
  1181. return SLICE_OK;
  1182. }
  1183. }
  1184. static int mpeg4_decode_mb(MpegEncContext *s, int16_t block[6][64])
  1185. {
  1186. Mpeg4DecContext *ctx = s->avctx->priv_data;
  1187. int cbpc, cbpy, i, cbp, pred_x, pred_y, mx, my, dquant;
  1188. int16_t *mot_val;
  1189. static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
  1190. const int xy = s->mb_x + s->mb_y * s->mb_stride;
  1191. av_assert2(s == (void*)ctx);
  1192. av_assert2(s->h263_pred);
  1193. if (s->pict_type == AV_PICTURE_TYPE_P ||
  1194. s->pict_type == AV_PICTURE_TYPE_S) {
  1195. do {
  1196. if (get_bits1(&s->gb)) {
  1197. /* skip mb */
  1198. s->mb_intra = 0;
  1199. for (i = 0; i < 6; i++)
  1200. s->block_last_index[i] = -1;
  1201. s->mv_dir = MV_DIR_FORWARD;
  1202. s->mv_type = MV_TYPE_16X16;
  1203. if (s->pict_type == AV_PICTURE_TYPE_S &&
  1204. ctx->vol_sprite_usage == GMC_SPRITE) {
  1205. s->current_picture.mb_type[xy] = MB_TYPE_SKIP |
  1206. MB_TYPE_GMC |
  1207. MB_TYPE_16x16 |
  1208. MB_TYPE_L0;
  1209. s->mcsel = 1;
  1210. s->mv[0][0][0] = get_amv(ctx, 0);
  1211. s->mv[0][0][1] = get_amv(ctx, 1);
  1212. s->mb_skipped = 0;
  1213. } else {
  1214. s->current_picture.mb_type[xy] = MB_TYPE_SKIP |
  1215. MB_TYPE_16x16 |
  1216. MB_TYPE_L0;
  1217. s->mcsel = 0;
  1218. s->mv[0][0][0] = 0;
  1219. s->mv[0][0][1] = 0;
  1220. s->mb_skipped = 1;
  1221. }
  1222. goto end;
  1223. }
  1224. cbpc = get_vlc2(&s->gb, ff_h263_inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
  1225. if (cbpc < 0) {
  1226. av_log(s->avctx, AV_LOG_ERROR,
  1227. "mcbpc damaged at %d %d\n", s->mb_x, s->mb_y);
  1228. return -1;
  1229. }
  1230. } while (cbpc == 20);
  1231. s->bdsp.clear_blocks(s->block[0]);
  1232. dquant = cbpc & 8;
  1233. s->mb_intra = ((cbpc & 4) != 0);
  1234. if (s->mb_intra)
  1235. goto intra;
  1236. if (s->pict_type == AV_PICTURE_TYPE_S &&
  1237. ctx->vol_sprite_usage == GMC_SPRITE && (cbpc & 16) == 0)
  1238. s->mcsel = get_bits1(&s->gb);
  1239. else
  1240. s->mcsel = 0;
  1241. cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1) ^ 0x0F;
  1242. if (cbpy < 0) {
  1243. av_log(s->avctx, AV_LOG_ERROR,
  1244. "P cbpy damaged at %d %d\n", s->mb_x, s->mb_y);
  1245. return AVERROR_INVALIDDATA;
  1246. }
  1247. cbp = (cbpc & 3) | (cbpy << 2);
  1248. if (dquant)
  1249. ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
  1250. if ((!s->progressive_sequence) &&
  1251. (cbp || (s->workaround_bugs & FF_BUG_XVID_ILACE)))
  1252. s->interlaced_dct = get_bits1(&s->gb);
  1253. s->mv_dir = MV_DIR_FORWARD;
  1254. if ((cbpc & 16) == 0) {
  1255. if (s->mcsel) {
  1256. s->current_picture.mb_type[xy] = MB_TYPE_GMC |
  1257. MB_TYPE_16x16 |
  1258. MB_TYPE_L0;
  1259. /* 16x16 global motion prediction */
  1260. s->mv_type = MV_TYPE_16X16;
  1261. mx = get_amv(ctx, 0);
  1262. my = get_amv(ctx, 1);
  1263. s->mv[0][0][0] = mx;
  1264. s->mv[0][0][1] = my;
  1265. } else if ((!s->progressive_sequence) && get_bits1(&s->gb)) {
  1266. s->current_picture.mb_type[xy] = MB_TYPE_16x8 |
  1267. MB_TYPE_L0 |
  1268. MB_TYPE_INTERLACED;
  1269. /* 16x8 field motion prediction */
  1270. s->mv_type = MV_TYPE_FIELD;
  1271. s->field_select[0][0] = get_bits1(&s->gb);
  1272. s->field_select[0][1] = get_bits1(&s->gb);
  1273. ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
  1274. for (i = 0; i < 2; i++) {
  1275. mx = ff_h263_decode_motion(s, pred_x, s->f_code);
  1276. if (mx >= 0xffff)
  1277. return -1;
  1278. my = ff_h263_decode_motion(s, pred_y / 2, s->f_code);
  1279. if (my >= 0xffff)
  1280. return -1;
  1281. s->mv[0][i][0] = mx;
  1282. s->mv[0][i][1] = my;
  1283. }
  1284. } else {
  1285. s->current_picture.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_L0;
  1286. /* 16x16 motion prediction */
  1287. s->mv_type = MV_TYPE_16X16;
  1288. ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
  1289. mx = ff_h263_decode_motion(s, pred_x, s->f_code);
  1290. if (mx >= 0xffff)
  1291. return -1;
  1292. my = ff_h263_decode_motion(s, pred_y, s->f_code);
  1293. if (my >= 0xffff)
  1294. return -1;
  1295. s->mv[0][0][0] = mx;
  1296. s->mv[0][0][1] = my;
  1297. }
  1298. } else {
  1299. s->current_picture.mb_type[xy] = MB_TYPE_8x8 | MB_TYPE_L0;
  1300. s->mv_type = MV_TYPE_8X8;
  1301. for (i = 0; i < 4; i++) {
  1302. mot_val = ff_h263_pred_motion(s, i, 0, &pred_x, &pred_y);
  1303. mx = ff_h263_decode_motion(s, pred_x, s->f_code);
  1304. if (mx >= 0xffff)
  1305. return -1;
  1306. my = ff_h263_decode_motion(s, pred_y, s->f_code);
  1307. if (my >= 0xffff)
  1308. return -1;
  1309. s->mv[0][i][0] = mx;
  1310. s->mv[0][i][1] = my;
  1311. mot_val[0] = mx;
  1312. mot_val[1] = my;
  1313. }
  1314. }
  1315. } else if (s->pict_type == AV_PICTURE_TYPE_B) {
  1316. int modb1; // first bit of modb
  1317. int modb2; // second bit of modb
  1318. int mb_type;
  1319. s->mb_intra = 0; // B-frames never contain intra blocks
  1320. s->mcsel = 0; // ... true gmc blocks
  1321. if (s->mb_x == 0) {
  1322. for (i = 0; i < 2; i++) {
  1323. s->last_mv[i][0][0] =
  1324. s->last_mv[i][0][1] =
  1325. s->last_mv[i][1][0] =
  1326. s->last_mv[i][1][1] = 0;
  1327. }
  1328. ff_thread_await_progress(&s->next_picture_ptr->tf, s->mb_y, 0);
  1329. }
  1330. /* if we skipped it in the future P Frame than skip it now too */
  1331. s->mb_skipped = s->next_picture.mbskip_table[s->mb_y * s->mb_stride + s->mb_x]; // Note, skiptab=0 if last was GMC
  1332. if (s->mb_skipped) {
  1333. /* skip mb */
  1334. for (i = 0; i < 6; i++)
  1335. s->block_last_index[i] = -1;
  1336. s->mv_dir = MV_DIR_FORWARD;
  1337. s->mv_type = MV_TYPE_16X16;
  1338. s->mv[0][0][0] =
  1339. s->mv[0][0][1] =
  1340. s->mv[1][0][0] =
  1341. s->mv[1][0][1] = 0;
  1342. s->current_picture.mb_type[xy] = MB_TYPE_SKIP |
  1343. MB_TYPE_16x16 |
  1344. MB_TYPE_L0;
  1345. goto end;
  1346. }
  1347. modb1 = get_bits1(&s->gb);
  1348. if (modb1) {
  1349. // like MB_TYPE_B_DIRECT but no vectors coded
  1350. mb_type = MB_TYPE_DIRECT2 | MB_TYPE_SKIP | MB_TYPE_L0L1;
  1351. cbp = 0;
  1352. } else {
  1353. modb2 = get_bits1(&s->gb);
  1354. mb_type = get_vlc2(&s->gb, mb_type_b_vlc.table, MB_TYPE_B_VLC_BITS, 1);
  1355. if (mb_type < 0) {
  1356. av_log(s->avctx, AV_LOG_ERROR, "illegal MB_type\n");
  1357. return -1;
  1358. }
  1359. mb_type = mb_type_b_map[mb_type];
  1360. if (modb2) {
  1361. cbp = 0;
  1362. } else {
  1363. s->bdsp.clear_blocks(s->block[0]);
  1364. cbp = get_bits(&s->gb, 6);
  1365. }
  1366. if ((!IS_DIRECT(mb_type)) && cbp) {
  1367. if (get_bits1(&s->gb))
  1368. ff_set_qscale(s, s->qscale + get_bits1(&s->gb) * 4 - 2);
  1369. }
  1370. if (!s->progressive_sequence) {
  1371. if (cbp)
  1372. s->interlaced_dct = get_bits1(&s->gb);
  1373. if (!IS_DIRECT(mb_type) && get_bits1(&s->gb)) {
  1374. mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
  1375. mb_type &= ~MB_TYPE_16x16;
  1376. if (USES_LIST(mb_type, 0)) {
  1377. s->field_select[0][0] = get_bits1(&s->gb);
  1378. s->field_select[0][1] = get_bits1(&s->gb);
  1379. }
  1380. if (USES_LIST(mb_type, 1)) {
  1381. s->field_select[1][0] = get_bits1(&s->gb);
  1382. s->field_select[1][1] = get_bits1(&s->gb);
  1383. }
  1384. }
  1385. }
  1386. s->mv_dir = 0;
  1387. if ((mb_type & (MB_TYPE_DIRECT2 | MB_TYPE_INTERLACED)) == 0) {
  1388. s->mv_type = MV_TYPE_16X16;
  1389. if (USES_LIST(mb_type, 0)) {
  1390. s->mv_dir = MV_DIR_FORWARD;
  1391. mx = ff_h263_decode_motion(s, s->last_mv[0][0][0], s->f_code);
  1392. my = ff_h263_decode_motion(s, s->last_mv[0][0][1], s->f_code);
  1393. s->last_mv[0][1][0] =
  1394. s->last_mv[0][0][0] =
  1395. s->mv[0][0][0] = mx;
  1396. s->last_mv[0][1][1] =
  1397. s->last_mv[0][0][1] =
  1398. s->mv[0][0][1] = my;
  1399. }
  1400. if (USES_LIST(mb_type, 1)) {
  1401. s->mv_dir |= MV_DIR_BACKWARD;
  1402. mx = ff_h263_decode_motion(s, s->last_mv[1][0][0], s->b_code);
  1403. my = ff_h263_decode_motion(s, s->last_mv[1][0][1], s->b_code);
  1404. s->last_mv[1][1][0] =
  1405. s->last_mv[1][0][0] =
  1406. s->mv[1][0][0] = mx;
  1407. s->last_mv[1][1][1] =
  1408. s->last_mv[1][0][1] =
  1409. s->mv[1][0][1] = my;
  1410. }
  1411. } else if (!IS_DIRECT(mb_type)) {
  1412. s->mv_type = MV_TYPE_FIELD;
  1413. if (USES_LIST(mb_type, 0)) {
  1414. s->mv_dir = MV_DIR_FORWARD;
  1415. for (i = 0; i < 2; i++) {
  1416. mx = ff_h263_decode_motion(s, s->last_mv[0][i][0], s->f_code);
  1417. my = ff_h263_decode_motion(s, s->last_mv[0][i][1] / 2, s->f_code);
  1418. s->last_mv[0][i][0] =
  1419. s->mv[0][i][0] = mx;
  1420. s->last_mv[0][i][1] = (s->mv[0][i][1] = my) * 2;
  1421. }
  1422. }
  1423. if (USES_LIST(mb_type, 1)) {
  1424. s->mv_dir |= MV_DIR_BACKWARD;
  1425. for (i = 0; i < 2; i++) {
  1426. mx = ff_h263_decode_motion(s, s->last_mv[1][i][0], s->b_code);
  1427. my = ff_h263_decode_motion(s, s->last_mv[1][i][1] / 2, s->b_code);
  1428. s->last_mv[1][i][0] =
  1429. s->mv[1][i][0] = mx;
  1430. s->last_mv[1][i][1] = (s->mv[1][i][1] = my) * 2;
  1431. }
  1432. }
  1433. }
  1434. }
  1435. if (IS_DIRECT(mb_type)) {
  1436. if (IS_SKIP(mb_type)) {
  1437. mx =
  1438. my = 0;
  1439. } else {
  1440. mx = ff_h263_decode_motion(s, 0, 1);
  1441. my = ff_h263_decode_motion(s, 0, 1);
  1442. }
  1443. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
  1444. mb_type |= ff_mpeg4_set_direct_mv(s, mx, my);
  1445. }
  1446. s->current_picture.mb_type[xy] = mb_type;
  1447. } else { /* I-Frame */
  1448. do {
  1449. cbpc = get_vlc2(&s->gb, ff_h263_intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 2);
  1450. if (cbpc < 0) {
  1451. av_log(s->avctx, AV_LOG_ERROR,
  1452. "I cbpc damaged at %d %d\n", s->mb_x, s->mb_y);
  1453. return -1;
  1454. }
  1455. } while (cbpc == 8);
  1456. dquant = cbpc & 4;
  1457. s->mb_intra = 1;
  1458. intra:
  1459. s->ac_pred = get_bits1(&s->gb);
  1460. if (s->ac_pred)
  1461. s->current_picture.mb_type[xy] = MB_TYPE_INTRA | MB_TYPE_ACPRED;
  1462. else
  1463. s->current_picture.mb_type[xy] = MB_TYPE_INTRA;
  1464. cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
  1465. if (cbpy < 0) {
  1466. av_log(s->avctx, AV_LOG_ERROR,
  1467. "I cbpy damaged at %d %d\n", s->mb_x, s->mb_y);
  1468. return -1;
  1469. }
  1470. cbp = (cbpc & 3) | (cbpy << 2);
  1471. ctx->use_intra_dc_vlc = s->qscale < ctx->intra_dc_threshold;
  1472. if (dquant)
  1473. ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
  1474. if (!s->progressive_sequence)
  1475. s->interlaced_dct = get_bits1(&s->gb);
  1476. s->bdsp.clear_blocks(s->block[0]);
  1477. /* decode each block */
  1478. for (i = 0; i < 6; i++) {
  1479. if (mpeg4_decode_block(ctx, block[i], i, cbp & 32, 1, 0) < 0)
  1480. return -1;
  1481. cbp += cbp;
  1482. }
  1483. goto end;
  1484. }
  1485. /* decode each block */
  1486. for (i = 0; i < 6; i++) {
  1487. if (mpeg4_decode_block(ctx, block[i], i, cbp & 32, 0, 0) < 0)
  1488. return -1;
  1489. cbp += cbp;
  1490. }
  1491. end:
  1492. /* per-MB end of slice check */
  1493. if (s->codec_id == AV_CODEC_ID_MPEG4) {
  1494. int next = mpeg4_is_resync(ctx);
  1495. if (next) {
  1496. if (s->mb_x + s->mb_y*s->mb_width + 1 > next && (s->avctx->err_recognition & AV_EF_AGGRESSIVE)) {
  1497. return -1;
  1498. } else if (s->mb_x + s->mb_y*s->mb_width + 1 >= next)
  1499. return SLICE_END;
  1500. if (s->pict_type == AV_PICTURE_TYPE_B) {
  1501. const int delta= s->mb_x + 1 == s->mb_width ? 2 : 1;
  1502. ff_thread_await_progress(&s->next_picture_ptr->tf,
  1503. (s->mb_x + delta >= s->mb_width)
  1504. ? FFMIN(s->mb_y + 1, s->mb_height - 1)
  1505. : s->mb_y, 0);
  1506. if (s->next_picture.mbskip_table[xy + delta])
  1507. return SLICE_OK;
  1508. }
  1509. return SLICE_END;
  1510. }
  1511. }
  1512. return SLICE_OK;
  1513. }
  1514. static int mpeg4_decode_gop_header(MpegEncContext *s, GetBitContext *gb)
  1515. {
  1516. int hours, minutes, seconds;
  1517. if (!show_bits(gb, 23)) {
  1518. av_log(s->avctx, AV_LOG_WARNING, "GOP header invalid\n");
  1519. return -1;
  1520. }
  1521. hours = get_bits(gb, 5);
  1522. minutes = get_bits(gb, 6);
  1523. check_marker(gb, "in gop_header");
  1524. seconds = get_bits(gb, 6);
  1525. s->time_base = seconds + 60*(minutes + 60*hours);
  1526. skip_bits1(gb);
  1527. skip_bits1(gb);
  1528. return 0;
  1529. }
  1530. static int mpeg4_decode_profile_level(MpegEncContext *s, GetBitContext *gb)
  1531. {
  1532. s->avctx->profile = get_bits(gb, 4);
  1533. s->avctx->level = get_bits(gb, 4);
  1534. // for Simple profile, level 0
  1535. if (s->avctx->profile == 0 && s->avctx->level == 8) {
  1536. s->avctx->level = 0;
  1537. }
  1538. return 0;
  1539. }
  1540. static int decode_vol_header(Mpeg4DecContext *ctx, GetBitContext *gb)
  1541. {
  1542. MpegEncContext *s = &ctx->m;
  1543. int width, height, vo_ver_id;
  1544. /* vol header */
  1545. skip_bits(gb, 1); /* random access */
  1546. s->vo_type = get_bits(gb, 8);
  1547. if (get_bits1(gb) != 0) { /* is_ol_id */
  1548. vo_ver_id = get_bits(gb, 4); /* vo_ver_id */
  1549. skip_bits(gb, 3); /* vo_priority */
  1550. } else {
  1551. vo_ver_id = 1;
  1552. }
  1553. s->aspect_ratio_info = get_bits(gb, 4);
  1554. if (s->aspect_ratio_info == FF_ASPECT_EXTENDED) {
  1555. s->avctx->sample_aspect_ratio.num = get_bits(gb, 8); // par_width
  1556. s->avctx->sample_aspect_ratio.den = get_bits(gb, 8); // par_height
  1557. } else {
  1558. s->avctx->sample_aspect_ratio = ff_h263_pixel_aspect[s->aspect_ratio_info];
  1559. }
  1560. if ((ctx->vol_control_parameters = get_bits1(gb))) { /* vol control parameter */
  1561. int chroma_format = get_bits(gb, 2);
  1562. if (chroma_format != CHROMA_420)
  1563. av_log(s->avctx, AV_LOG_ERROR, "illegal chroma format\n");
  1564. s->low_delay = get_bits1(gb);
  1565. if (get_bits1(gb)) { /* vbv parameters */
  1566. get_bits(gb, 15); /* first_half_bitrate */
  1567. check_marker(gb, "after first_half_bitrate");
  1568. get_bits(gb, 15); /* latter_half_bitrate */
  1569. check_marker(gb, "after latter_half_bitrate");
  1570. get_bits(gb, 15); /* first_half_vbv_buffer_size */
  1571. check_marker(gb, "after first_half_vbv_buffer_size");
  1572. get_bits(gb, 3); /* latter_half_vbv_buffer_size */
  1573. get_bits(gb, 11); /* first_half_vbv_occupancy */
  1574. check_marker(gb, "after first_half_vbv_occupancy");
  1575. get_bits(gb, 15); /* latter_half_vbv_occupancy */
  1576. check_marker(gb, "after latter_half_vbv_occupancy");
  1577. }
  1578. } else {
  1579. /* is setting low delay flag only once the smartest thing to do?
  1580. * low delay detection won't be overridden. */
  1581. if (s->picture_number == 0)
  1582. s->low_delay = 0;
  1583. }
  1584. ctx->shape = get_bits(gb, 2); /* vol shape */
  1585. if (ctx->shape != RECT_SHAPE)
  1586. av_log(s->avctx, AV_LOG_ERROR, "only rectangular vol supported\n");
  1587. if (ctx->shape == GRAY_SHAPE && vo_ver_id != 1) {
  1588. av_log(s->avctx, AV_LOG_ERROR, "Gray shape not supported\n");
  1589. skip_bits(gb, 4); /* video_object_layer_shape_extension */
  1590. }
  1591. check_marker(gb, "before time_increment_resolution");
  1592. s->avctx->framerate.num = get_bits(gb, 16);
  1593. if (!s->avctx->framerate.num) {
  1594. av_log(s->avctx, AV_LOG_ERROR, "framerate==0\n");
  1595. return AVERROR_INVALIDDATA;
  1596. }
  1597. ctx->time_increment_bits = av_log2(s->avctx->framerate.num - 1) + 1;
  1598. if (ctx->time_increment_bits < 1)
  1599. ctx->time_increment_bits = 1;
  1600. check_marker(gb, "before fixed_vop_rate");
  1601. if (get_bits1(gb) != 0) /* fixed_vop_rate */
  1602. s->avctx->framerate.den = get_bits(gb, ctx->time_increment_bits);
  1603. else
  1604. s->avctx->framerate.den = 1;
  1605. s->avctx->time_base = av_inv_q(av_mul_q(s->avctx->framerate, (AVRational){s->avctx->ticks_per_frame, 1}));
  1606. ctx->t_frame = 0;
  1607. if (ctx->shape != BIN_ONLY_SHAPE) {
  1608. if (ctx->shape == RECT_SHAPE) {
  1609. check_marker(gb, "before width");
  1610. width = get_bits(gb, 13);
  1611. check_marker(gb, "before height");
  1612. height = get_bits(gb, 13);
  1613. check_marker(gb, "after height");
  1614. if (width && height && /* they should be non zero but who knows */
  1615. !(s->width && s->codec_tag == AV_RL32("MP4S"))) {
  1616. if (s->width && s->height &&
  1617. (s->width != width || s->height != height))
  1618. s->context_reinit = 1;
  1619. s->width = width;
  1620. s->height = height;
  1621. }
  1622. }
  1623. s->progressive_sequence =
  1624. s->progressive_frame = get_bits1(gb) ^ 1;
  1625. s->interlaced_dct = 0;
  1626. if (!get_bits1(gb) && (s->avctx->debug & FF_DEBUG_PICT_INFO))
  1627. av_log(s->avctx, AV_LOG_INFO, /* OBMC Disable */
  1628. "MPEG4 OBMC not supported (very likely buggy encoder)\n");
  1629. if (vo_ver_id == 1)
  1630. ctx->vol_sprite_usage = get_bits1(gb); /* vol_sprite_usage */
  1631. else
  1632. ctx->vol_sprite_usage = get_bits(gb, 2); /* vol_sprite_usage */
  1633. if (ctx->vol_sprite_usage == STATIC_SPRITE)
  1634. av_log(s->avctx, AV_LOG_ERROR, "Static Sprites not supported\n");
  1635. if (ctx->vol_sprite_usage == STATIC_SPRITE ||
  1636. ctx->vol_sprite_usage == GMC_SPRITE) {
  1637. if (ctx->vol_sprite_usage == STATIC_SPRITE) {
  1638. skip_bits(gb, 13); // sprite_width
  1639. check_marker(gb, "after sprite_width");
  1640. skip_bits(gb, 13); // sprite_height
  1641. check_marker(gb, "after sprite_height");
  1642. skip_bits(gb, 13); // sprite_left
  1643. check_marker(gb, "after sprite_left");
  1644. skip_bits(gb, 13); // sprite_top
  1645. check_marker(gb, "after sprite_top");
  1646. }
  1647. ctx->num_sprite_warping_points = get_bits(gb, 6);
  1648. if (ctx->num_sprite_warping_points > 3) {
  1649. av_log(s->avctx, AV_LOG_ERROR,
  1650. "%d sprite_warping_points\n",
  1651. ctx->num_sprite_warping_points);
  1652. ctx->num_sprite_warping_points = 0;
  1653. return AVERROR_INVALIDDATA;
  1654. }
  1655. s->sprite_warping_accuracy = get_bits(gb, 2);
  1656. ctx->sprite_brightness_change = get_bits1(gb);
  1657. if (ctx->vol_sprite_usage == STATIC_SPRITE)
  1658. skip_bits1(gb); // low_latency_sprite
  1659. }
  1660. // FIXME sadct disable bit if verid!=1 && shape not rect
  1661. if (get_bits1(gb) == 1) { /* not_8_bit */
  1662. s->quant_precision = get_bits(gb, 4); /* quant_precision */
  1663. if (get_bits(gb, 4) != 8) /* bits_per_pixel */
  1664. av_log(s->avctx, AV_LOG_ERROR, "N-bit not supported\n");
  1665. if (s->quant_precision != 5)
  1666. av_log(s->avctx, AV_LOG_ERROR,
  1667. "quant precision %d\n", s->quant_precision);
  1668. if (s->quant_precision<3 || s->quant_precision>9) {
  1669. s->quant_precision = 5;
  1670. }
  1671. } else {
  1672. s->quant_precision = 5;
  1673. }
  1674. // FIXME a bunch of grayscale shape things
  1675. if ((s->mpeg_quant = get_bits1(gb))) { /* vol_quant_type */
  1676. int i, v;
  1677. /* load default matrixes */
  1678. for (i = 0; i < 64; i++) {
  1679. int j = s->idsp.idct_permutation[i];
  1680. v = ff_mpeg4_default_intra_matrix[i];
  1681. s->intra_matrix[j] = v;
  1682. s->chroma_intra_matrix[j] = v;
  1683. v = ff_mpeg4_default_non_intra_matrix[i];
  1684. s->inter_matrix[j] = v;
  1685. s->chroma_inter_matrix[j] = v;
  1686. }
  1687. /* load custom intra matrix */
  1688. if (get_bits1(gb)) {
  1689. int last = 0;
  1690. for (i = 0; i < 64; i++) {
  1691. int j;
  1692. if (get_bits_left(gb) < 8) {
  1693. av_log(s->avctx, AV_LOG_ERROR, "insufficient data for custom matrix\n");
  1694. return AVERROR_INVALIDDATA;
  1695. }
  1696. v = get_bits(gb, 8);
  1697. if (v == 0)
  1698. break;
  1699. last = v;
  1700. j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
  1701. s->intra_matrix[j] = last;
  1702. s->chroma_intra_matrix[j] = last;
  1703. }
  1704. /* replicate last value */
  1705. for (; i < 64; i++) {
  1706. int j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
  1707. s->intra_matrix[j] = last;
  1708. s->chroma_intra_matrix[j] = last;
  1709. }
  1710. }
  1711. /* load custom non intra matrix */
  1712. if (get_bits1(gb)) {
  1713. int last = 0;
  1714. for (i = 0; i < 64; i++) {
  1715. int j;
  1716. if (get_bits_left(gb) < 8) {
  1717. av_log(s->avctx, AV_LOG_ERROR, "insufficient data for custom matrix\n");
  1718. return AVERROR_INVALIDDATA;
  1719. }
  1720. v = get_bits(gb, 8);
  1721. if (v == 0)
  1722. break;
  1723. last = v;
  1724. j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
  1725. s->inter_matrix[j] = v;
  1726. s->chroma_inter_matrix[j] = v;
  1727. }
  1728. /* replicate last value */
  1729. for (; i < 64; i++) {
  1730. int j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
  1731. s->inter_matrix[j] = last;
  1732. s->chroma_inter_matrix[j] = last;
  1733. }
  1734. }
  1735. // FIXME a bunch of grayscale shape things
  1736. }
  1737. if (vo_ver_id != 1)
  1738. s->quarter_sample = get_bits1(gb);
  1739. else
  1740. s->quarter_sample = 0;
  1741. if (get_bits_left(gb) < 4) {
  1742. av_log(s->avctx, AV_LOG_ERROR, "VOL Header truncated\n");
  1743. return AVERROR_INVALIDDATA;
  1744. }
  1745. if (!get_bits1(gb)) {
  1746. int pos = get_bits_count(gb);
  1747. int estimation_method = get_bits(gb, 2);
  1748. if (estimation_method < 2) {
  1749. if (!get_bits1(gb)) {
  1750. ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* opaque */
  1751. ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* transparent */
  1752. ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* intra_cae */
  1753. ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* inter_cae */
  1754. ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* no_update */
  1755. ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* upampling */
  1756. }
  1757. if (!get_bits1(gb)) {
  1758. ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* intra_blocks */
  1759. ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* inter_blocks */
  1760. ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* inter4v_blocks */
  1761. ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* not coded blocks */
  1762. }
  1763. if (!check_marker(gb, "in complexity estimation part 1")) {
  1764. skip_bits_long(gb, pos - get_bits_count(gb));
  1765. goto no_cplx_est;
  1766. }
  1767. if (!get_bits1(gb)) {
  1768. ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* dct_coeffs */
  1769. ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* dct_lines */
  1770. ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* vlc_syms */
  1771. ctx->cplx_estimation_trash_i += 4 * get_bits1(gb); /* vlc_bits */
  1772. }
  1773. if (!get_bits1(gb)) {
  1774. ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* apm */
  1775. ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* npm */
  1776. ctx->cplx_estimation_trash_b += 8 * get_bits1(gb); /* interpolate_mc_q */
  1777. ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* forwback_mc_q */
  1778. ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* halfpel2 */
  1779. ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* halfpel4 */
  1780. }
  1781. if (!check_marker(gb, "in complexity estimation part 2")) {
  1782. skip_bits_long(gb, pos - get_bits_count(gb));
  1783. goto no_cplx_est;
  1784. }
  1785. if (estimation_method == 1) {
  1786. ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* sadct */
  1787. ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* qpel */
  1788. }
  1789. } else
  1790. av_log(s->avctx, AV_LOG_ERROR,
  1791. "Invalid Complexity estimation method %d\n",
  1792. estimation_method);
  1793. } else {
  1794. no_cplx_est:
  1795. ctx->cplx_estimation_trash_i =
  1796. ctx->cplx_estimation_trash_p =
  1797. ctx->cplx_estimation_trash_b = 0;
  1798. }
  1799. ctx->resync_marker = !get_bits1(gb); /* resync_marker_disabled */
  1800. s->data_partitioning = get_bits1(gb);
  1801. if (s->data_partitioning)
  1802. ctx->rvlc = get_bits1(gb);
  1803. if (vo_ver_id != 1) {
  1804. ctx->new_pred = get_bits1(gb);
  1805. if (ctx->new_pred) {
  1806. av_log(s->avctx, AV_LOG_ERROR, "new pred not supported\n");
  1807. skip_bits(gb, 2); /* requested upstream message type */
  1808. skip_bits1(gb); /* newpred segment type */
  1809. }
  1810. if (get_bits1(gb)) // reduced_res_vop
  1811. av_log(s->avctx, AV_LOG_ERROR,
  1812. "reduced resolution VOP not supported\n");
  1813. } else {
  1814. ctx->new_pred = 0;
  1815. }
  1816. ctx->scalability = get_bits1(gb);
  1817. if (ctx->scalability) {
  1818. GetBitContext bak = *gb;
  1819. int h_sampling_factor_n;
  1820. int h_sampling_factor_m;
  1821. int v_sampling_factor_n;
  1822. int v_sampling_factor_m;
  1823. skip_bits1(gb); // hierarchy_type
  1824. skip_bits(gb, 4); /* ref_layer_id */
  1825. skip_bits1(gb); /* ref_layer_sampling_dir */
  1826. h_sampling_factor_n = get_bits(gb, 5);
  1827. h_sampling_factor_m = get_bits(gb, 5);
  1828. v_sampling_factor_n = get_bits(gb, 5);
  1829. v_sampling_factor_m = get_bits(gb, 5);
  1830. ctx->enhancement_type = get_bits1(gb);
  1831. if (h_sampling_factor_n == 0 || h_sampling_factor_m == 0 ||
  1832. v_sampling_factor_n == 0 || v_sampling_factor_m == 0) {
  1833. /* illegal scalability header (VERY broken encoder),
  1834. * trying to workaround */
  1835. ctx->scalability = 0;
  1836. *gb = bak;
  1837. } else
  1838. av_log(s->avctx, AV_LOG_ERROR, "scalability not supported\n");
  1839. // bin shape stuff FIXME
  1840. }
  1841. }
  1842. if (s->avctx->debug&FF_DEBUG_PICT_INFO) {
  1843. av_log(s->avctx, AV_LOG_DEBUG, "tb %d/%d, tincrbits:%d, qp_prec:%d, ps:%d, %s%s%s%s\n",
  1844. s->avctx->framerate.den, s->avctx->framerate.num,
  1845. ctx->time_increment_bits,
  1846. s->quant_precision,
  1847. s->progressive_sequence,
  1848. ctx->scalability ? "scalability " :"" , s->quarter_sample ? "qpel " : "",
  1849. s->data_partitioning ? "partition " : "", ctx->rvlc ? "rvlc " : ""
  1850. );
  1851. }
  1852. return 0;
  1853. }
  1854. /**
  1855. * Decode the user data stuff in the header.
  1856. * Also initializes divx/xvid/lavc_version/build.
  1857. */
  1858. static int decode_user_data(Mpeg4DecContext *ctx, GetBitContext *gb)
  1859. {
  1860. MpegEncContext *s = &ctx->m;
  1861. char buf[256];
  1862. int i;
  1863. int e;
  1864. int ver = 0, build = 0, ver2 = 0, ver3 = 0;
  1865. char last;
  1866. for (i = 0; i < 255 && get_bits_count(gb) < gb->size_in_bits; i++) {
  1867. if (show_bits(gb, 23) == 0)
  1868. break;
  1869. buf[i] = get_bits(gb, 8);
  1870. }
  1871. buf[i] = 0;
  1872. /* divx detection */
  1873. e = sscanf(buf, "DivX%dBuild%d%c", &ver, &build, &last);
  1874. if (e < 2)
  1875. e = sscanf(buf, "DivX%db%d%c", &ver, &build, &last);
  1876. if (e >= 2) {
  1877. ctx->divx_version = ver;
  1878. ctx->divx_build = build;
  1879. s->divx_packed = e == 3 && last == 'p';
  1880. }
  1881. /* libavcodec detection */
  1882. e = sscanf(buf, "FFmpe%*[^b]b%d", &build) + 3;
  1883. if (e != 4)
  1884. e = sscanf(buf, "FFmpeg v%d.%d.%d / libavcodec build: %d", &ver, &ver2, &ver3, &build);
  1885. if (e != 4) {
  1886. e = sscanf(buf, "Lavc%d.%d.%d", &ver, &ver2, &ver3) + 1;
  1887. if (e > 1) {
  1888. if (ver > 0xFFU || ver2 > 0xFFU || ver3 > 0xFFU) {
  1889. av_log(s->avctx, AV_LOG_WARNING,
  1890. "Unknown Lavc version string encountered, %d.%d.%d; "
  1891. "clamping sub-version values to 8-bits.\n",
  1892. ver, ver2, ver3);
  1893. }
  1894. build = ((ver & 0xFF) << 16) + ((ver2 & 0xFF) << 8) + (ver3 & 0xFF);
  1895. }
  1896. }
  1897. if (e != 4) {
  1898. if (strcmp(buf, "ffmpeg") == 0)
  1899. ctx->lavc_build = 4600;
  1900. }
  1901. if (e == 4)
  1902. ctx->lavc_build = build;
  1903. /* Xvid detection */
  1904. e = sscanf(buf, "XviD%d", &build);
  1905. if (e == 1)
  1906. ctx->xvid_build = build;
  1907. return 0;
  1908. }
  1909. int ff_mpeg4_workaround_bugs(AVCodecContext *avctx)
  1910. {
  1911. Mpeg4DecContext *ctx = avctx->priv_data;
  1912. MpegEncContext *s = &ctx->m;
  1913. if (ctx->xvid_build == -1 && ctx->divx_version == -1 && ctx->lavc_build == -1) {
  1914. if (s->codec_tag == AV_RL32("XVID") ||
  1915. s->codec_tag == AV_RL32("XVIX") ||
  1916. s->codec_tag == AV_RL32("RMP4") ||
  1917. s->codec_tag == AV_RL32("ZMP4") ||
  1918. s->codec_tag == AV_RL32("SIPP"))
  1919. ctx->xvid_build = 0;
  1920. }
  1921. if (ctx->xvid_build == -1 && ctx->divx_version == -1 && ctx->lavc_build == -1)
  1922. if (s->codec_tag == AV_RL32("DIVX") && s->vo_type == 0 &&
  1923. ctx->vol_control_parameters == 0)
  1924. ctx->divx_version = 400; // divx 4
  1925. if (ctx->xvid_build >= 0 && ctx->divx_version >= 0) {
  1926. ctx->divx_version =
  1927. ctx->divx_build = -1;
  1928. }
  1929. if (s->workaround_bugs & FF_BUG_AUTODETECT) {
  1930. if (s->codec_tag == AV_RL32("XVIX"))
  1931. s->workaround_bugs |= FF_BUG_XVID_ILACE;
  1932. if (s->codec_tag == AV_RL32("UMP4"))
  1933. s->workaround_bugs |= FF_BUG_UMP4;
  1934. if (ctx->divx_version >= 500 && ctx->divx_build < 1814)
  1935. s->workaround_bugs |= FF_BUG_QPEL_CHROMA;
  1936. if (ctx->divx_version > 502 && ctx->divx_build < 1814)
  1937. s->workaround_bugs |= FF_BUG_QPEL_CHROMA2;
  1938. if (ctx->xvid_build <= 3U)
  1939. s->padding_bug_score = 256 * 256 * 256 * 64;
  1940. if (ctx->xvid_build <= 1U)
  1941. s->workaround_bugs |= FF_BUG_QPEL_CHROMA;
  1942. if (ctx->xvid_build <= 12U)
  1943. s->workaround_bugs |= FF_BUG_EDGE;
  1944. if (ctx->xvid_build <= 32U)
  1945. s->workaround_bugs |= FF_BUG_DC_CLIP;
  1946. #define SET_QPEL_FUNC(postfix1, postfix2) \
  1947. s->qdsp.put_ ## postfix1 = ff_put_ ## postfix2; \
  1948. s->qdsp.put_no_rnd_ ## postfix1 = ff_put_no_rnd_ ## postfix2; \
  1949. s->qdsp.avg_ ## postfix1 = ff_avg_ ## postfix2;
  1950. if (ctx->lavc_build < 4653U)
  1951. s->workaround_bugs |= FF_BUG_STD_QPEL;
  1952. if (ctx->lavc_build < 4655U)
  1953. s->workaround_bugs |= FF_BUG_DIRECT_BLOCKSIZE;
  1954. if (ctx->lavc_build < 4670U)
  1955. s->workaround_bugs |= FF_BUG_EDGE;
  1956. if (ctx->lavc_build <= 4712U)
  1957. s->workaround_bugs |= FF_BUG_DC_CLIP;
  1958. if (ctx->divx_version >= 0)
  1959. s->workaround_bugs |= FF_BUG_DIRECT_BLOCKSIZE;
  1960. if (ctx->divx_version == 501 && ctx->divx_build == 20020416)
  1961. s->padding_bug_score = 256 * 256 * 256 * 64;
  1962. if (ctx->divx_version < 500U)
  1963. s->workaround_bugs |= FF_BUG_EDGE;
  1964. if (ctx->divx_version >= 0)
  1965. s->workaround_bugs |= FF_BUG_HPEL_CHROMA;
  1966. }
  1967. if (s->workaround_bugs & FF_BUG_STD_QPEL) {
  1968. SET_QPEL_FUNC(qpel_pixels_tab[0][5], qpel16_mc11_old_c)
  1969. SET_QPEL_FUNC(qpel_pixels_tab[0][7], qpel16_mc31_old_c)
  1970. SET_QPEL_FUNC(qpel_pixels_tab[0][9], qpel16_mc12_old_c)
  1971. SET_QPEL_FUNC(qpel_pixels_tab[0][11], qpel16_mc32_old_c)
  1972. SET_QPEL_FUNC(qpel_pixels_tab[0][13], qpel16_mc13_old_c)
  1973. SET_QPEL_FUNC(qpel_pixels_tab[0][15], qpel16_mc33_old_c)
  1974. SET_QPEL_FUNC(qpel_pixels_tab[1][5], qpel8_mc11_old_c)
  1975. SET_QPEL_FUNC(qpel_pixels_tab[1][7], qpel8_mc31_old_c)
  1976. SET_QPEL_FUNC(qpel_pixels_tab[1][9], qpel8_mc12_old_c)
  1977. SET_QPEL_FUNC(qpel_pixels_tab[1][11], qpel8_mc32_old_c)
  1978. SET_QPEL_FUNC(qpel_pixels_tab[1][13], qpel8_mc13_old_c)
  1979. SET_QPEL_FUNC(qpel_pixels_tab[1][15], qpel8_mc33_old_c)
  1980. }
  1981. if (avctx->debug & FF_DEBUG_BUGS)
  1982. av_log(s->avctx, AV_LOG_DEBUG,
  1983. "bugs: %X lavc_build:%d xvid_build:%d divx_version:%d divx_build:%d %s\n",
  1984. s->workaround_bugs, ctx->lavc_build, ctx->xvid_build,
  1985. ctx->divx_version, ctx->divx_build, s->divx_packed ? "p" : "");
  1986. if (CONFIG_MPEG4_DECODER && ctx->xvid_build >= 0 &&
  1987. s->codec_id == AV_CODEC_ID_MPEG4 &&
  1988. avctx->idct_algo == FF_IDCT_AUTO) {
  1989. avctx->idct_algo = FF_IDCT_XVID;
  1990. ff_mpv_idct_init(s);
  1991. return 1;
  1992. }
  1993. return 0;
  1994. }
  1995. static int decode_vop_header(Mpeg4DecContext *ctx, GetBitContext *gb)
  1996. {
  1997. MpegEncContext *s = &ctx->m;
  1998. int time_incr, time_increment;
  1999. int64_t pts;
  2000. s->mcsel = 0;
  2001. s->pict_type = get_bits(gb, 2) + AV_PICTURE_TYPE_I; /* pict type: I = 0 , P = 1 */
  2002. if (s->pict_type == AV_PICTURE_TYPE_B && s->low_delay &&
  2003. ctx->vol_control_parameters == 0 && !(s->avctx->flags & AV_CODEC_FLAG_LOW_DELAY)) {
  2004. av_log(s->avctx, AV_LOG_ERROR, "low_delay flag set incorrectly, clearing it\n");
  2005. s->low_delay = 0;
  2006. }
  2007. s->partitioned_frame = s->data_partitioning && s->pict_type != AV_PICTURE_TYPE_B;
  2008. if (s->partitioned_frame)
  2009. s->decode_mb = mpeg4_decode_partitioned_mb;
  2010. else
  2011. s->decode_mb = mpeg4_decode_mb;
  2012. time_incr = 0;
  2013. while (get_bits1(gb) != 0)
  2014. time_incr++;
  2015. check_marker(gb, "before time_increment");
  2016. if (ctx->time_increment_bits == 0 ||
  2017. !(show_bits(gb, ctx->time_increment_bits + 1) & 1)) {
  2018. av_log(s->avctx, AV_LOG_WARNING,
  2019. "time_increment_bits %d is invalid in relation to the current bitstream, this is likely caused by a missing VOL header\n", ctx->time_increment_bits);
  2020. for (ctx->time_increment_bits = 1;
  2021. ctx->time_increment_bits < 16;
  2022. ctx->time_increment_bits++) {
  2023. if (s->pict_type == AV_PICTURE_TYPE_P ||
  2024. (s->pict_type == AV_PICTURE_TYPE_S &&
  2025. ctx->vol_sprite_usage == GMC_SPRITE)) {
  2026. if ((show_bits(gb, ctx->time_increment_bits + 6) & 0x37) == 0x30)
  2027. break;
  2028. } else if ((show_bits(gb, ctx->time_increment_bits + 5) & 0x1F) == 0x18)
  2029. break;
  2030. }
  2031. av_log(s->avctx, AV_LOG_WARNING,
  2032. "time_increment_bits set to %d bits, based on bitstream analysis\n", ctx->time_increment_bits);
  2033. if (s->avctx->framerate.num && 4*s->avctx->framerate.num < 1<<ctx->time_increment_bits) {
  2034. s->avctx->framerate.num = 1<<ctx->time_increment_bits;
  2035. s->avctx->time_base = av_inv_q(av_mul_q(s->avctx->framerate, (AVRational){s->avctx->ticks_per_frame, 1}));
  2036. }
  2037. }
  2038. if (IS_3IV1)
  2039. time_increment = get_bits1(gb); // FIXME investigate further
  2040. else
  2041. time_increment = get_bits(gb, ctx->time_increment_bits);
  2042. if (s->pict_type != AV_PICTURE_TYPE_B) {
  2043. s->last_time_base = s->time_base;
  2044. s->time_base += time_incr;
  2045. s->time = s->time_base * (int64_t)s->avctx->framerate.num + time_increment;
  2046. if (s->workaround_bugs & FF_BUG_UMP4) {
  2047. if (s->time < s->last_non_b_time) {
  2048. /* header is not mpeg-4-compatible, broken encoder,
  2049. * trying to workaround */
  2050. s->time_base++;
  2051. s->time += s->avctx->framerate.num;
  2052. }
  2053. }
  2054. s->pp_time = s->time - s->last_non_b_time;
  2055. s->last_non_b_time = s->time;
  2056. } else {
  2057. s->time = (s->last_time_base + time_incr) * (int64_t)s->avctx->framerate.num + time_increment;
  2058. s->pb_time = s->pp_time - (s->last_non_b_time - s->time);
  2059. if (s->pp_time <= s->pb_time ||
  2060. s->pp_time <= s->pp_time - s->pb_time ||
  2061. s->pp_time <= 0) {
  2062. /* messed up order, maybe after seeking? skipping current b-frame */
  2063. return FRAME_SKIPPED;
  2064. }
  2065. ff_mpeg4_init_direct_mv(s);
  2066. if (ctx->t_frame == 0)
  2067. ctx->t_frame = s->pb_time;
  2068. if (ctx->t_frame == 0)
  2069. ctx->t_frame = 1; // 1/0 protection
  2070. s->pp_field_time = (ROUNDED_DIV(s->last_non_b_time, ctx->t_frame) -
  2071. ROUNDED_DIV(s->last_non_b_time - s->pp_time, ctx->t_frame)) * 2;
  2072. s->pb_field_time = (ROUNDED_DIV(s->time, ctx->t_frame) -
  2073. ROUNDED_DIV(s->last_non_b_time - s->pp_time, ctx->t_frame)) * 2;
  2074. if (s->pp_field_time <= s->pb_field_time || s->pb_field_time <= 1) {
  2075. s->pb_field_time = 2;
  2076. s->pp_field_time = 4;
  2077. if (!s->progressive_sequence)
  2078. return FRAME_SKIPPED;
  2079. }
  2080. }
  2081. if (s->avctx->framerate.den)
  2082. pts = ROUNDED_DIV(s->time, s->avctx->framerate.den);
  2083. else
  2084. pts = AV_NOPTS_VALUE;
  2085. ff_dlog(s->avctx, "MPEG4 PTS: %"PRId64"\n", pts);
  2086. check_marker(gb, "before vop_coded");
  2087. /* vop coded */
  2088. if (get_bits1(gb) != 1) {
  2089. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  2090. av_log(s->avctx, AV_LOG_ERROR, "vop not coded\n");
  2091. return FRAME_SKIPPED;
  2092. }
  2093. if (ctx->new_pred)
  2094. decode_new_pred(ctx, gb);
  2095. if (ctx->shape != BIN_ONLY_SHAPE &&
  2096. (s->pict_type == AV_PICTURE_TYPE_P ||
  2097. (s->pict_type == AV_PICTURE_TYPE_S &&
  2098. ctx->vol_sprite_usage == GMC_SPRITE))) {
  2099. /* rounding type for motion estimation */
  2100. s->no_rounding = get_bits1(gb);
  2101. } else {
  2102. s->no_rounding = 0;
  2103. }
  2104. // FIXME reduced res stuff
  2105. if (ctx->shape != RECT_SHAPE) {
  2106. if (ctx->vol_sprite_usage != 1 || s->pict_type != AV_PICTURE_TYPE_I) {
  2107. skip_bits(gb, 13); /* width */
  2108. check_marker(gb, "after width");
  2109. skip_bits(gb, 13); /* height */
  2110. check_marker(gb, "after height");
  2111. skip_bits(gb, 13); /* hor_spat_ref */
  2112. check_marker(gb, "after hor_spat_ref");
  2113. skip_bits(gb, 13); /* ver_spat_ref */
  2114. }
  2115. skip_bits1(gb); /* change_CR_disable */
  2116. if (get_bits1(gb) != 0)
  2117. skip_bits(gb, 8); /* constant_alpha_value */
  2118. }
  2119. // FIXME complexity estimation stuff
  2120. if (ctx->shape != BIN_ONLY_SHAPE) {
  2121. skip_bits_long(gb, ctx->cplx_estimation_trash_i);
  2122. if (s->pict_type != AV_PICTURE_TYPE_I)
  2123. skip_bits_long(gb, ctx->cplx_estimation_trash_p);
  2124. if (s->pict_type == AV_PICTURE_TYPE_B)
  2125. skip_bits_long(gb, ctx->cplx_estimation_trash_b);
  2126. if (get_bits_left(gb) < 3) {
  2127. av_log(s->avctx, AV_LOG_ERROR, "Header truncated\n");
  2128. return AVERROR_INVALIDDATA;
  2129. }
  2130. ctx->intra_dc_threshold = ff_mpeg4_dc_threshold[get_bits(gb, 3)];
  2131. if (!s->progressive_sequence) {
  2132. s->top_field_first = get_bits1(gb);
  2133. s->alternate_scan = get_bits1(gb);
  2134. } else
  2135. s->alternate_scan = 0;
  2136. }
  2137. if (s->alternate_scan) {
  2138. ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_alternate_vertical_scan);
  2139. ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_alternate_vertical_scan);
  2140. ff_init_scantable(s->idsp.idct_permutation, &s->intra_h_scantable, ff_alternate_vertical_scan);
  2141. ff_init_scantable(s->idsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
  2142. } else {
  2143. ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_zigzag_direct);
  2144. ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_zigzag_direct);
  2145. ff_init_scantable(s->idsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
  2146. ff_init_scantable(s->idsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
  2147. }
  2148. if (s->pict_type == AV_PICTURE_TYPE_S) {
  2149. if((ctx->vol_sprite_usage == STATIC_SPRITE ||
  2150. ctx->vol_sprite_usage == GMC_SPRITE)) {
  2151. if (mpeg4_decode_sprite_trajectory(ctx, gb) < 0)
  2152. return AVERROR_INVALIDDATA;
  2153. if (ctx->sprite_brightness_change)
  2154. av_log(s->avctx, AV_LOG_ERROR,
  2155. "sprite_brightness_change not supported\n");
  2156. if (ctx->vol_sprite_usage == STATIC_SPRITE)
  2157. av_log(s->avctx, AV_LOG_ERROR, "static sprite not supported\n");
  2158. } else {
  2159. memset(s->sprite_offset, 0, sizeof(s->sprite_offset));
  2160. memset(s->sprite_delta, 0, sizeof(s->sprite_delta));
  2161. }
  2162. }
  2163. if (ctx->shape != BIN_ONLY_SHAPE) {
  2164. s->chroma_qscale = s->qscale = get_bits(gb, s->quant_precision);
  2165. if (s->qscale == 0) {
  2166. av_log(s->avctx, AV_LOG_ERROR,
  2167. "Error, header damaged or not MPEG4 header (qscale=0)\n");
  2168. return AVERROR_INVALIDDATA; // makes no sense to continue, as there is nothing left from the image then
  2169. }
  2170. if (s->pict_type != AV_PICTURE_TYPE_I) {
  2171. s->f_code = get_bits(gb, 3); /* fcode_for */
  2172. if (s->f_code == 0) {
  2173. av_log(s->avctx, AV_LOG_ERROR,
  2174. "Error, header damaged or not MPEG4 header (f_code=0)\n");
  2175. s->f_code = 1;
  2176. return AVERROR_INVALIDDATA; // makes no sense to continue, as there is nothing left from the image then
  2177. }
  2178. } else
  2179. s->f_code = 1;
  2180. if (s->pict_type == AV_PICTURE_TYPE_B) {
  2181. s->b_code = get_bits(gb, 3);
  2182. if (s->b_code == 0) {
  2183. av_log(s->avctx, AV_LOG_ERROR,
  2184. "Error, header damaged or not MPEG4 header (b_code=0)\n");
  2185. s->b_code=1;
  2186. return AVERROR_INVALIDDATA; // makes no sense to continue, as the MV decoding will break very quickly
  2187. }
  2188. } else
  2189. s->b_code = 1;
  2190. if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
  2191. av_log(s->avctx, AV_LOG_DEBUG,
  2192. "qp:%d fc:%d,%d %s size:%d pro:%d alt:%d top:%d %spel part:%d resync:%d w:%d a:%d rnd:%d vot:%d%s dc:%d ce:%d/%d/%d time:%"PRId64" tincr:%d\n",
  2193. s->qscale, s->f_code, s->b_code,
  2194. s->pict_type == AV_PICTURE_TYPE_I ? "I" : (s->pict_type == AV_PICTURE_TYPE_P ? "P" : (s->pict_type == AV_PICTURE_TYPE_B ? "B" : "S")),
  2195. gb->size_in_bits,s->progressive_sequence, s->alternate_scan,
  2196. s->top_field_first, s->quarter_sample ? "q" : "h",
  2197. s->data_partitioning, ctx->resync_marker,
  2198. ctx->num_sprite_warping_points, s->sprite_warping_accuracy,
  2199. 1 - s->no_rounding, s->vo_type,
  2200. ctx->vol_control_parameters ? " VOLC" : " ", ctx->intra_dc_threshold,
  2201. ctx->cplx_estimation_trash_i, ctx->cplx_estimation_trash_p,
  2202. ctx->cplx_estimation_trash_b,
  2203. s->time,
  2204. time_increment
  2205. );
  2206. }
  2207. if (!ctx->scalability) {
  2208. if (ctx->shape != RECT_SHAPE && s->pict_type != AV_PICTURE_TYPE_I)
  2209. skip_bits1(gb); // vop shape coding type
  2210. } else {
  2211. if (ctx->enhancement_type) {
  2212. int load_backward_shape = get_bits1(gb);
  2213. if (load_backward_shape)
  2214. av_log(s->avctx, AV_LOG_ERROR,
  2215. "load backward shape isn't supported\n");
  2216. }
  2217. skip_bits(gb, 2); // ref_select_code
  2218. }
  2219. }
  2220. /* detect buggy encoders which don't set the low_delay flag
  2221. * (divx4/xvid/opendivx). Note we cannot detect divx5 without b-frames
  2222. * easily (although it's buggy too) */
  2223. if (s->vo_type == 0 && ctx->vol_control_parameters == 0 &&
  2224. ctx->divx_version == -1 && s->picture_number == 0) {
  2225. av_log(s->avctx, AV_LOG_WARNING,
  2226. "looks like this file was encoded with (divx4/(old)xvid/opendivx) -> forcing low_delay flag\n");
  2227. s->low_delay = 1;
  2228. }
  2229. s->picture_number++; // better than pic number==0 always ;)
  2230. // FIXME add short header support
  2231. s->y_dc_scale_table = ff_mpeg4_y_dc_scale_table;
  2232. s->c_dc_scale_table = ff_mpeg4_c_dc_scale_table;
  2233. if (s->workaround_bugs & FF_BUG_EDGE) {
  2234. s->h_edge_pos = s->width;
  2235. s->v_edge_pos = s->height;
  2236. }
  2237. return 0;
  2238. }
  2239. /**
  2240. * Decode mpeg4 headers.
  2241. * @return <0 if no VOP found (or a damaged one)
  2242. * FRAME_SKIPPED if a not coded VOP is found
  2243. * 0 if a VOP is found
  2244. */
  2245. int ff_mpeg4_decode_picture_header(Mpeg4DecContext *ctx, GetBitContext *gb)
  2246. {
  2247. MpegEncContext *s = &ctx->m;
  2248. unsigned startcode, v;
  2249. int ret;
  2250. int vol = 0;
  2251. /* search next start code */
  2252. align_get_bits(gb);
  2253. if (s->codec_tag == AV_RL32("WV1F") && show_bits(gb, 24) == 0x575630) {
  2254. skip_bits(gb, 24);
  2255. if (get_bits(gb, 8) == 0xF0)
  2256. goto end;
  2257. }
  2258. startcode = 0xff;
  2259. for (;;) {
  2260. if (get_bits_count(gb) >= gb->size_in_bits) {
  2261. if (gb->size_in_bits == 8 &&
  2262. (ctx->divx_version >= 0 || ctx->xvid_build >= 0) || s->codec_tag == AV_RL32("QMP4")) {
  2263. av_log(s->avctx, AV_LOG_VERBOSE, "frame skip %d\n", gb->size_in_bits);
  2264. return FRAME_SKIPPED; // divx bug
  2265. } else
  2266. return -1; // end of stream
  2267. }
  2268. /* use the bits after the test */
  2269. v = get_bits(gb, 8);
  2270. startcode = ((startcode << 8) | v) & 0xffffffff;
  2271. if ((startcode & 0xFFFFFF00) != 0x100)
  2272. continue; // no startcode
  2273. if (s->avctx->debug & FF_DEBUG_STARTCODE) {
  2274. av_log(s->avctx, AV_LOG_DEBUG, "startcode: %3X ", startcode);
  2275. if (startcode <= 0x11F)
  2276. av_log(s->avctx, AV_LOG_DEBUG, "Video Object Start");
  2277. else if (startcode <= 0x12F)
  2278. av_log(s->avctx, AV_LOG_DEBUG, "Video Object Layer Start");
  2279. else if (startcode <= 0x13F)
  2280. av_log(s->avctx, AV_LOG_DEBUG, "Reserved");
  2281. else if (startcode <= 0x15F)
  2282. av_log(s->avctx, AV_LOG_DEBUG, "FGS bp start");
  2283. else if (startcode <= 0x1AF)
  2284. av_log(s->avctx, AV_LOG_DEBUG, "Reserved");
  2285. else if (startcode == 0x1B0)
  2286. av_log(s->avctx, AV_LOG_DEBUG, "Visual Object Seq Start");
  2287. else if (startcode == 0x1B1)
  2288. av_log(s->avctx, AV_LOG_DEBUG, "Visual Object Seq End");
  2289. else if (startcode == 0x1B2)
  2290. av_log(s->avctx, AV_LOG_DEBUG, "User Data");
  2291. else if (startcode == 0x1B3)
  2292. av_log(s->avctx, AV_LOG_DEBUG, "Group of VOP start");
  2293. else if (startcode == 0x1B4)
  2294. av_log(s->avctx, AV_LOG_DEBUG, "Video Session Error");
  2295. else if (startcode == 0x1B5)
  2296. av_log(s->avctx, AV_LOG_DEBUG, "Visual Object Start");
  2297. else if (startcode == 0x1B6)
  2298. av_log(s->avctx, AV_LOG_DEBUG, "Video Object Plane start");
  2299. else if (startcode == 0x1B7)
  2300. av_log(s->avctx, AV_LOG_DEBUG, "slice start");
  2301. else if (startcode == 0x1B8)
  2302. av_log(s->avctx, AV_LOG_DEBUG, "extension start");
  2303. else if (startcode == 0x1B9)
  2304. av_log(s->avctx, AV_LOG_DEBUG, "fgs start");
  2305. else if (startcode == 0x1BA)
  2306. av_log(s->avctx, AV_LOG_DEBUG, "FBA Object start");
  2307. else if (startcode == 0x1BB)
  2308. av_log(s->avctx, AV_LOG_DEBUG, "FBA Object Plane start");
  2309. else if (startcode == 0x1BC)
  2310. av_log(s->avctx, AV_LOG_DEBUG, "Mesh Object start");
  2311. else if (startcode == 0x1BD)
  2312. av_log(s->avctx, AV_LOG_DEBUG, "Mesh Object Plane start");
  2313. else if (startcode == 0x1BE)
  2314. av_log(s->avctx, AV_LOG_DEBUG, "Still Texture Object start");
  2315. else if (startcode == 0x1BF)
  2316. av_log(s->avctx, AV_LOG_DEBUG, "Texture Spatial Layer start");
  2317. else if (startcode == 0x1C0)
  2318. av_log(s->avctx, AV_LOG_DEBUG, "Texture SNR Layer start");
  2319. else if (startcode == 0x1C1)
  2320. av_log(s->avctx, AV_LOG_DEBUG, "Texture Tile start");
  2321. else if (startcode == 0x1C2)
  2322. av_log(s->avctx, AV_LOG_DEBUG, "Texture Shape Layer start");
  2323. else if (startcode == 0x1C3)
  2324. av_log(s->avctx, AV_LOG_DEBUG, "stuffing start");
  2325. else if (startcode <= 0x1C5)
  2326. av_log(s->avctx, AV_LOG_DEBUG, "reserved");
  2327. else if (startcode <= 0x1FF)
  2328. av_log(s->avctx, AV_LOG_DEBUG, "System start");
  2329. av_log(s->avctx, AV_LOG_DEBUG, " at %d\n", get_bits_count(gb));
  2330. }
  2331. if (startcode >= 0x120 && startcode <= 0x12F) {
  2332. if (vol) {
  2333. av_log(s->avctx, AV_LOG_WARNING, "Ignoring multiple VOL headers\n");
  2334. continue;
  2335. }
  2336. vol++;
  2337. if ((ret = decode_vol_header(ctx, gb)) < 0)
  2338. return ret;
  2339. } else if (startcode == USER_DATA_STARTCODE) {
  2340. decode_user_data(ctx, gb);
  2341. } else if (startcode == GOP_STARTCODE) {
  2342. mpeg4_decode_gop_header(s, gb);
  2343. } else if (startcode == VOS_STARTCODE) {
  2344. mpeg4_decode_profile_level(s, gb);
  2345. } else if (startcode == VOP_STARTCODE) {
  2346. break;
  2347. }
  2348. align_get_bits(gb);
  2349. startcode = 0xff;
  2350. }
  2351. end:
  2352. if (s->avctx->flags & AV_CODEC_FLAG_LOW_DELAY)
  2353. s->low_delay = 1;
  2354. s->avctx->has_b_frames = !s->low_delay;
  2355. return decode_vop_header(ctx, gb);
  2356. }
  2357. av_cold void ff_mpeg4videodec_static_init(void) {
  2358. static int done = 0;
  2359. if (!done) {
  2360. ff_rl_init(&ff_mpeg4_rl_intra, ff_mpeg4_static_rl_table_store[0]);
  2361. ff_rl_init(&ff_rvlc_rl_inter, ff_mpeg4_static_rl_table_store[1]);
  2362. ff_rl_init(&ff_rvlc_rl_intra, ff_mpeg4_static_rl_table_store[2]);
  2363. INIT_VLC_RL(ff_mpeg4_rl_intra, 554);
  2364. INIT_VLC_RL(ff_rvlc_rl_inter, 1072);
  2365. INIT_VLC_RL(ff_rvlc_rl_intra, 1072);
  2366. INIT_VLC_STATIC(&dc_lum, DC_VLC_BITS, 10 /* 13 */,
  2367. &ff_mpeg4_DCtab_lum[0][1], 2, 1,
  2368. &ff_mpeg4_DCtab_lum[0][0], 2, 1, 512);
  2369. INIT_VLC_STATIC(&dc_chrom, DC_VLC_BITS, 10 /* 13 */,
  2370. &ff_mpeg4_DCtab_chrom[0][1], 2, 1,
  2371. &ff_mpeg4_DCtab_chrom[0][0], 2, 1, 512);
  2372. INIT_VLC_STATIC(&sprite_trajectory, SPRITE_TRAJ_VLC_BITS, 15,
  2373. &ff_sprite_trajectory_tab[0][1], 4, 2,
  2374. &ff_sprite_trajectory_tab[0][0], 4, 2, 128);
  2375. INIT_VLC_STATIC(&mb_type_b_vlc, MB_TYPE_B_VLC_BITS, 4,
  2376. &ff_mb_type_b_tab[0][1], 2, 1,
  2377. &ff_mb_type_b_tab[0][0], 2, 1, 16);
  2378. done = 1;
  2379. }
  2380. }
  2381. int ff_mpeg4_frame_end(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
  2382. {
  2383. Mpeg4DecContext *ctx = avctx->priv_data;
  2384. MpegEncContext *s = &ctx->m;
  2385. /* divx 5.01+ bitstream reorder stuff */
  2386. /* Since this clobbers the input buffer and hwaccel codecs still need the
  2387. * data during hwaccel->end_frame we should not do this any earlier */
  2388. if (s->divx_packed) {
  2389. int current_pos = s->gb.buffer == s->bitstream_buffer ? 0 : (get_bits_count(&s->gb) >> 3);
  2390. int startcode_found = 0;
  2391. if (buf_size - current_pos > 7) {
  2392. int i;
  2393. for (i = current_pos; i < buf_size - 4; i++)
  2394. if (buf[i] == 0 &&
  2395. buf[i + 1] == 0 &&
  2396. buf[i + 2] == 1 &&
  2397. buf[i + 3] == 0xB6) {
  2398. startcode_found = !(buf[i + 4] & 0x40);
  2399. break;
  2400. }
  2401. }
  2402. if (startcode_found) {
  2403. if (!ctx->showed_packed_warning) {
  2404. av_log(s->avctx, AV_LOG_INFO, "Video uses a non-standard and "
  2405. "wasteful way to store B-frames ('packed B-frames'). "
  2406. "Consider using the mpeg4_unpack_bframes bitstream filter without encoding but stream copy to fix it.\n");
  2407. ctx->showed_packed_warning = 1;
  2408. }
  2409. av_fast_padded_malloc(&s->bitstream_buffer,
  2410. &s->allocated_bitstream_buffer_size,
  2411. buf_size - current_pos);
  2412. if (!s->bitstream_buffer) {
  2413. s->bitstream_buffer_size = 0;
  2414. return AVERROR(ENOMEM);
  2415. }
  2416. memcpy(s->bitstream_buffer, buf + current_pos,
  2417. buf_size - current_pos);
  2418. s->bitstream_buffer_size = buf_size - current_pos;
  2419. }
  2420. }
  2421. return 0;
  2422. }
  2423. static int mpeg4_update_thread_context(AVCodecContext *dst,
  2424. const AVCodecContext *src)
  2425. {
  2426. Mpeg4DecContext *s = dst->priv_data;
  2427. const Mpeg4DecContext *s1 = src->priv_data;
  2428. int init = s->m.context_initialized;
  2429. int ret = ff_mpeg_update_thread_context(dst, src);
  2430. if (ret < 0)
  2431. return ret;
  2432. memcpy(((uint8_t*)s) + sizeof(MpegEncContext), ((uint8_t*)s1) + sizeof(MpegEncContext), sizeof(Mpeg4DecContext) - sizeof(MpegEncContext));
  2433. if (CONFIG_MPEG4_DECODER && !init && s1->xvid_build >= 0)
  2434. ff_xvid_idct_init(&s->m.idsp, dst);
  2435. return 0;
  2436. }
  2437. static av_cold int decode_init(AVCodecContext *avctx)
  2438. {
  2439. Mpeg4DecContext *ctx = avctx->priv_data;
  2440. MpegEncContext *s = &ctx->m;
  2441. int ret;
  2442. ctx->divx_version =
  2443. ctx->divx_build =
  2444. ctx->xvid_build =
  2445. ctx->lavc_build = -1;
  2446. if ((ret = ff_h263_decode_init(avctx)) < 0)
  2447. return ret;
  2448. ff_mpeg4videodec_static_init();
  2449. s->h263_pred = 1;
  2450. s->low_delay = 0; /* default, might be overridden in the vol header during header parsing */
  2451. s->decode_mb = mpeg4_decode_mb;
  2452. ctx->time_increment_bits = 4; /* default value for broken headers */
  2453. avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
  2454. avctx->internal->allocate_progress = 1;
  2455. return 0;
  2456. }
  2457. static const AVProfile mpeg4_video_profiles[] = {
  2458. { FF_PROFILE_MPEG4_SIMPLE, "Simple Profile" },
  2459. { FF_PROFILE_MPEG4_SIMPLE_SCALABLE, "Simple Scalable Profile" },
  2460. { FF_PROFILE_MPEG4_CORE, "Core Profile" },
  2461. { FF_PROFILE_MPEG4_MAIN, "Main Profile" },
  2462. { FF_PROFILE_MPEG4_N_BIT, "N-bit Profile" },
  2463. { FF_PROFILE_MPEG4_SCALABLE_TEXTURE, "Scalable Texture Profile" },
  2464. { FF_PROFILE_MPEG4_SIMPLE_FACE_ANIMATION, "Simple Face Animation Profile" },
  2465. { FF_PROFILE_MPEG4_BASIC_ANIMATED_TEXTURE, "Basic Animated Texture Profile" },
  2466. { FF_PROFILE_MPEG4_HYBRID, "Hybrid Profile" },
  2467. { FF_PROFILE_MPEG4_ADVANCED_REAL_TIME, "Advanced Real Time Simple Profile" },
  2468. { FF_PROFILE_MPEG4_CORE_SCALABLE, "Code Scalable Profile" },
  2469. { FF_PROFILE_MPEG4_ADVANCED_CODING, "Advanced Coding Profile" },
  2470. { FF_PROFILE_MPEG4_ADVANCED_CORE, "Advanced Core Profile" },
  2471. { FF_PROFILE_MPEG4_ADVANCED_SCALABLE_TEXTURE, "Advanced Scalable Texture Profile" },
  2472. { FF_PROFILE_MPEG4_SIMPLE_STUDIO, "Simple Studio Profile" },
  2473. { FF_PROFILE_MPEG4_ADVANCED_SIMPLE, "Advanced Simple Profile" },
  2474. { FF_PROFILE_UNKNOWN },
  2475. };
  2476. static const AVOption mpeg4_options[] = {
  2477. {"quarter_sample", "1/4 subpel MC", offsetof(MpegEncContext, quarter_sample), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, 0},
  2478. {"divx_packed", "divx style packed b frames", offsetof(MpegEncContext, divx_packed), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, 0},
  2479. {NULL}
  2480. };
  2481. static const AVClass mpeg4_class = {
  2482. "MPEG4 Video Decoder",
  2483. av_default_item_name,
  2484. mpeg4_options,
  2485. LIBAVUTIL_VERSION_INT,
  2486. };
  2487. AVCodec ff_mpeg4_decoder = {
  2488. .name = "mpeg4",
  2489. .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2"),
  2490. .type = AVMEDIA_TYPE_VIDEO,
  2491. .id = AV_CODEC_ID_MPEG4,
  2492. .priv_data_size = sizeof(Mpeg4DecContext),
  2493. .init = decode_init,
  2494. .close = ff_h263_decode_end,
  2495. .decode = ff_h263_decode_frame,
  2496. .capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1 |
  2497. AV_CODEC_CAP_TRUNCATED | AV_CODEC_CAP_DELAY |
  2498. AV_CODEC_CAP_FRAME_THREADS,
  2499. .flush = ff_mpeg_flush,
  2500. .max_lowres = 3,
  2501. .pix_fmts = ff_h263_hwaccel_pixfmt_list_420,
  2502. .profiles = NULL_IF_CONFIG_SMALL(mpeg4_video_profiles),
  2503. .update_thread_context = ONLY_IF_THREADS_ENABLED(mpeg4_update_thread_context),
  2504. .priv_class = &mpeg4_class,
  2505. };
  2506. #if CONFIG_MPEG4_VDPAU_DECODER && FF_API_VDPAU
  2507. static const AVClass mpeg4_vdpau_class = {
  2508. "MPEG4 Video VDPAU Decoder",
  2509. av_default_item_name,
  2510. mpeg4_options,
  2511. LIBAVUTIL_VERSION_INT,
  2512. };
  2513. AVCodec ff_mpeg4_vdpau_decoder = {
  2514. .name = "mpeg4_vdpau",
  2515. .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 (VDPAU)"),
  2516. .type = AVMEDIA_TYPE_VIDEO,
  2517. .id = AV_CODEC_ID_MPEG4,
  2518. .priv_data_size = sizeof(Mpeg4DecContext),
  2519. .init = decode_init,
  2520. .close = ff_h263_decode_end,
  2521. .decode = ff_h263_decode_frame,
  2522. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_TRUNCATED | AV_CODEC_CAP_DELAY |
  2523. AV_CODEC_CAP_HWACCEL_VDPAU,
  2524. .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_VDPAU_MPEG4,
  2525. AV_PIX_FMT_NONE },
  2526. .priv_class = &mpeg4_vdpau_class,
  2527. };
  2528. #endif