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.

2793 lines
106KB

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