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.

2643 lines
99KB

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