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.

2817 lines
107KB

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