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.

2826 lines
108KB

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