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.

2988 lines
108KB

  1. /*
  2. * MPEG-1/2 decoder
  3. * Copyright (c) 2000, 2001 Fabrice Bellard
  4. * Copyright (c) 2002-2013 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. /**
  23. * @file
  24. * MPEG-1/2 decoder
  25. */
  26. #define UNCHECKED_BITSTREAM_READER 1
  27. #include <inttypes.h>
  28. #include "libavutil/attributes.h"
  29. #include "libavutil/imgutils.h"
  30. #include "libavutil/internal.h"
  31. #include "libavutil/stereo3d.h"
  32. #include "avcodec.h"
  33. #include "bytestream.h"
  34. #include "error_resilience.h"
  35. #include "idctdsp.h"
  36. #include "internal.h"
  37. #include "mpeg_er.h"
  38. #include "mpeg12.h"
  39. #include "mpeg12data.h"
  40. #include "mpegutils.h"
  41. #include "mpegvideo.h"
  42. #include "mpegvideodata.h"
  43. #include "thread.h"
  44. #include "version.h"
  45. #include "vdpau_compat.h"
  46. #include "xvmc_internal.h"
  47. typedef struct Mpeg1Context {
  48. MpegEncContext mpeg_enc_ctx;
  49. int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
  50. int repeat_field; /* true if we must repeat the field */
  51. AVPanScan pan_scan; /* some temporary storage for the panscan */
  52. AVStereo3D stereo3d;
  53. int has_stereo3d;
  54. uint8_t *a53_caption;
  55. int a53_caption_size;
  56. uint8_t afd;
  57. int has_afd;
  58. int slice_count;
  59. AVRational save_aspect;
  60. int save_width, save_height, save_progressive_seq;
  61. AVRational frame_rate_ext; /* MPEG-2 specific framerate modificator */
  62. int sync; /* Did we reach a sync point like a GOP/SEQ/KEYFrame? */
  63. int tmpgexs;
  64. int first_slice;
  65. int extradata_decoded;
  66. } Mpeg1Context;
  67. #define MB_TYPE_ZERO_MV 0x20000000
  68. static const uint32_t ptype2mb_type[7] = {
  69. MB_TYPE_INTRA,
  70. MB_TYPE_L0 | MB_TYPE_CBP | MB_TYPE_ZERO_MV | MB_TYPE_16x16,
  71. MB_TYPE_L0,
  72. MB_TYPE_L0 | MB_TYPE_CBP,
  73. MB_TYPE_QUANT | MB_TYPE_INTRA,
  74. MB_TYPE_QUANT | MB_TYPE_L0 | MB_TYPE_CBP | MB_TYPE_ZERO_MV | MB_TYPE_16x16,
  75. MB_TYPE_QUANT | MB_TYPE_L0 | MB_TYPE_CBP,
  76. };
  77. static const uint32_t btype2mb_type[11] = {
  78. MB_TYPE_INTRA,
  79. MB_TYPE_L1,
  80. MB_TYPE_L1 | MB_TYPE_CBP,
  81. MB_TYPE_L0,
  82. MB_TYPE_L0 | MB_TYPE_CBP,
  83. MB_TYPE_L0L1,
  84. MB_TYPE_L0L1 | MB_TYPE_CBP,
  85. MB_TYPE_QUANT | MB_TYPE_INTRA,
  86. MB_TYPE_QUANT | MB_TYPE_L1 | MB_TYPE_CBP,
  87. MB_TYPE_QUANT | MB_TYPE_L0 | MB_TYPE_CBP,
  88. MB_TYPE_QUANT | MB_TYPE_L0L1 | MB_TYPE_CBP,
  89. };
  90. /* as H.263, but only 17 codes */
  91. static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred)
  92. {
  93. int code, sign, val, shift;
  94. code = get_vlc2(&s->gb, ff_mv_vlc.table, MV_VLC_BITS, 2);
  95. if (code == 0)
  96. return pred;
  97. if (code < 0)
  98. return 0xffff;
  99. sign = get_bits1(&s->gb);
  100. shift = fcode - 1;
  101. val = code;
  102. if (shift) {
  103. val = (val - 1) << shift;
  104. val |= get_bits(&s->gb, shift);
  105. val++;
  106. }
  107. if (sign)
  108. val = -val;
  109. val += pred;
  110. /* modulo decoding */
  111. return sign_extend(val, 5 + shift);
  112. }
  113. #define check_scantable_index(ctx, x) \
  114. do { \
  115. if ((x) > 63) { \
  116. av_log(ctx->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", \
  117. ctx->mb_x, ctx->mb_y); \
  118. return AVERROR_INVALIDDATA; \
  119. } \
  120. } while (0)
  121. static inline int mpeg1_decode_block_intra(MpegEncContext *s,
  122. int16_t *block, int n)
  123. {
  124. int level, dc, diff, i, j, run;
  125. int component;
  126. RLTable *rl = &ff_rl_mpeg1;
  127. uint8_t *const scantable = s->intra_scantable.permutated;
  128. const uint16_t *quant_matrix = s->intra_matrix;
  129. const int qscale = s->qscale;
  130. /* DC coefficient */
  131. component = (n <= 3 ? 0 : n - 4 + 1);
  132. diff = decode_dc(&s->gb, component);
  133. if (diff >= 0xffff)
  134. return AVERROR_INVALIDDATA;
  135. dc = s->last_dc[component];
  136. dc += diff;
  137. s->last_dc[component] = dc;
  138. block[0] = dc * quant_matrix[0];
  139. ff_tlog(s->avctx, "dc=%d diff=%d\n", dc, diff);
  140. i = 0;
  141. {
  142. OPEN_READER(re, &s->gb);
  143. UPDATE_CACHE(re, &s->gb);
  144. if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
  145. goto end;
  146. /* now quantify & encode AC coefficients */
  147. for (;;) {
  148. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
  149. TEX_VLC_BITS, 2, 0);
  150. if (level != 0) {
  151. i += run;
  152. check_scantable_index(s, i);
  153. j = scantable[i];
  154. level = (level * qscale * quant_matrix[j]) >> 4;
  155. level = (level - 1) | 1;
  156. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
  157. SHOW_SBITS(re, &s->gb, 1);
  158. SKIP_BITS(re, &s->gb, 1);
  159. } else {
  160. /* escape */
  161. run = SHOW_UBITS(re, &s->gb, 6) + 1;
  162. LAST_SKIP_BITS(re, &s->gb, 6);
  163. UPDATE_CACHE(re, &s->gb);
  164. level = SHOW_SBITS(re, &s->gb, 8);
  165. SKIP_BITS(re, &s->gb, 8);
  166. if (level == -128) {
  167. level = SHOW_UBITS(re, &s->gb, 8) - 256;
  168. SKIP_BITS(re, &s->gb, 8);
  169. } else if (level == 0) {
  170. level = SHOW_UBITS(re, &s->gb, 8);
  171. SKIP_BITS(re, &s->gb, 8);
  172. }
  173. i += run;
  174. check_scantable_index(s, i);
  175. j = scantable[i];
  176. if (level < 0) {
  177. level = -level;
  178. level = (level * qscale * quant_matrix[j]) >> 4;
  179. level = (level - 1) | 1;
  180. level = -level;
  181. } else {
  182. level = (level * qscale * quant_matrix[j]) >> 4;
  183. level = (level - 1) | 1;
  184. }
  185. }
  186. block[j] = level;
  187. if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
  188. break;
  189. UPDATE_CACHE(re, &s->gb);
  190. }
  191. end:
  192. LAST_SKIP_BITS(re, &s->gb, 2);
  193. CLOSE_READER(re, &s->gb);
  194. }
  195. s->block_last_index[n] = i;
  196. return 0;
  197. }
  198. int ff_mpeg1_decode_block_intra(MpegEncContext *s, int16_t *block, int n)
  199. {
  200. return mpeg1_decode_block_intra(s, block, n);
  201. }
  202. static inline int mpeg1_decode_block_inter(MpegEncContext *s,
  203. int16_t *block, int n)
  204. {
  205. int level, i, j, run;
  206. RLTable *rl = &ff_rl_mpeg1;
  207. uint8_t *const scantable = s->intra_scantable.permutated;
  208. const uint16_t *quant_matrix = s->inter_matrix;
  209. const int qscale = s->qscale;
  210. {
  211. OPEN_READER(re, &s->gb);
  212. i = -1;
  213. // special case for first coefficient, no need to add second VLC table
  214. UPDATE_CACHE(re, &s->gb);
  215. if (((int32_t) GET_CACHE(re, &s->gb)) < 0) {
  216. level = (3 * qscale * quant_matrix[0]) >> 5;
  217. level = (level - 1) | 1;
  218. if (GET_CACHE(re, &s->gb) & 0x40000000)
  219. level = -level;
  220. block[0] = level;
  221. i++;
  222. SKIP_BITS(re, &s->gb, 2);
  223. if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
  224. goto end;
  225. }
  226. /* now quantify & encode AC coefficients */
  227. for (;;) {
  228. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
  229. TEX_VLC_BITS, 2, 0);
  230. if (level != 0) {
  231. i += run;
  232. check_scantable_index(s, i);
  233. j = scantable[i];
  234. level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
  235. level = (level - 1) | 1;
  236. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
  237. SHOW_SBITS(re, &s->gb, 1);
  238. SKIP_BITS(re, &s->gb, 1);
  239. } else {
  240. /* escape */
  241. run = SHOW_UBITS(re, &s->gb, 6) + 1;
  242. LAST_SKIP_BITS(re, &s->gb, 6);
  243. UPDATE_CACHE(re, &s->gb);
  244. level = SHOW_SBITS(re, &s->gb, 8);
  245. SKIP_BITS(re, &s->gb, 8);
  246. if (level == -128) {
  247. level = SHOW_UBITS(re, &s->gb, 8) - 256;
  248. SKIP_BITS(re, &s->gb, 8);
  249. } else if (level == 0) {
  250. level = SHOW_UBITS(re, &s->gb, 8);
  251. SKIP_BITS(re, &s->gb, 8);
  252. }
  253. i += run;
  254. check_scantable_index(s, i);
  255. j = scantable[i];
  256. if (level < 0) {
  257. level = -level;
  258. level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
  259. level = (level - 1) | 1;
  260. level = -level;
  261. } else {
  262. level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
  263. level = (level - 1) | 1;
  264. }
  265. }
  266. block[j] = level;
  267. if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
  268. break;
  269. UPDATE_CACHE(re, &s->gb);
  270. }
  271. end:
  272. LAST_SKIP_BITS(re, &s->gb, 2);
  273. CLOSE_READER(re, &s->gb);
  274. }
  275. s->block_last_index[n] = i;
  276. return 0;
  277. }
  278. /**
  279. * Note: this function can read out of range and crash for corrupt streams.
  280. * Changing this would eat up any speed benefits it has.
  281. * Do not use "fast" flag if you need the code to be robust.
  282. */
  283. static inline int mpeg1_fast_decode_block_inter(MpegEncContext *s,
  284. int16_t *block, int n)
  285. {
  286. int level, i, j, run;
  287. RLTable *rl = &ff_rl_mpeg1;
  288. uint8_t *const scantable = s->intra_scantable.permutated;
  289. const int qscale = s->qscale;
  290. {
  291. OPEN_READER(re, &s->gb);
  292. i = -1;
  293. // Special case for first coefficient, no need to add second VLC table.
  294. UPDATE_CACHE(re, &s->gb);
  295. if (((int32_t) GET_CACHE(re, &s->gb)) < 0) {
  296. level = (3 * qscale) >> 1;
  297. level = (level - 1) | 1;
  298. if (GET_CACHE(re, &s->gb) & 0x40000000)
  299. level = -level;
  300. block[0] = level;
  301. i++;
  302. SKIP_BITS(re, &s->gb, 2);
  303. if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
  304. goto end;
  305. }
  306. /* now quantify & encode AC coefficients */
  307. for (;;) {
  308. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
  309. TEX_VLC_BITS, 2, 0);
  310. if (level != 0) {
  311. i += run;
  312. check_scantable_index(s, i);
  313. j = scantable[i];
  314. level = ((level * 2 + 1) * qscale) >> 1;
  315. level = (level - 1) | 1;
  316. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
  317. SHOW_SBITS(re, &s->gb, 1);
  318. SKIP_BITS(re, &s->gb, 1);
  319. } else {
  320. /* escape */
  321. run = SHOW_UBITS(re, &s->gb, 6) + 1;
  322. LAST_SKIP_BITS(re, &s->gb, 6);
  323. UPDATE_CACHE(re, &s->gb);
  324. level = SHOW_SBITS(re, &s->gb, 8);
  325. SKIP_BITS(re, &s->gb, 8);
  326. if (level == -128) {
  327. level = SHOW_UBITS(re, &s->gb, 8) - 256;
  328. SKIP_BITS(re, &s->gb, 8);
  329. } else if (level == 0) {
  330. level = SHOW_UBITS(re, &s->gb, 8);
  331. SKIP_BITS(re, &s->gb, 8);
  332. }
  333. i += run;
  334. check_scantable_index(s, i);
  335. j = scantable[i];
  336. if (level < 0) {
  337. level = -level;
  338. level = ((level * 2 + 1) * qscale) >> 1;
  339. level = (level - 1) | 1;
  340. level = -level;
  341. } else {
  342. level = ((level * 2 + 1) * qscale) >> 1;
  343. level = (level - 1) | 1;
  344. }
  345. }
  346. block[j] = level;
  347. if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
  348. break;
  349. UPDATE_CACHE(re, &s->gb);
  350. }
  351. end:
  352. LAST_SKIP_BITS(re, &s->gb, 2);
  353. CLOSE_READER(re, &s->gb);
  354. }
  355. s->block_last_index[n] = i;
  356. return 0;
  357. }
  358. static inline int mpeg2_decode_block_non_intra(MpegEncContext *s,
  359. int16_t *block, int n)
  360. {
  361. int level, i, j, run;
  362. RLTable *rl = &ff_rl_mpeg1;
  363. uint8_t *const scantable = s->intra_scantable.permutated;
  364. const uint16_t *quant_matrix;
  365. const int qscale = s->qscale;
  366. int mismatch;
  367. mismatch = 1;
  368. {
  369. OPEN_READER(re, &s->gb);
  370. i = -1;
  371. if (n < 4)
  372. quant_matrix = s->inter_matrix;
  373. else
  374. quant_matrix = s->chroma_inter_matrix;
  375. // Special case for first coefficient, no need to add second VLC table.
  376. UPDATE_CACHE(re, &s->gb);
  377. if (((int32_t) GET_CACHE(re, &s->gb)) < 0) {
  378. level = (3 * qscale * quant_matrix[0]) >> 5;
  379. if (GET_CACHE(re, &s->gb) & 0x40000000)
  380. level = -level;
  381. block[0] = level;
  382. mismatch ^= level;
  383. i++;
  384. SKIP_BITS(re, &s->gb, 2);
  385. if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
  386. goto end;
  387. }
  388. /* now quantify & encode AC coefficients */
  389. for (;;) {
  390. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
  391. TEX_VLC_BITS, 2, 0);
  392. if (level != 0) {
  393. i += run;
  394. check_scantable_index(s, i);
  395. j = scantable[i];
  396. level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
  397. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
  398. SHOW_SBITS(re, &s->gb, 1);
  399. SKIP_BITS(re, &s->gb, 1);
  400. } else {
  401. /* escape */
  402. run = SHOW_UBITS(re, &s->gb, 6) + 1;
  403. LAST_SKIP_BITS(re, &s->gb, 6);
  404. UPDATE_CACHE(re, &s->gb);
  405. level = SHOW_SBITS(re, &s->gb, 12);
  406. SKIP_BITS(re, &s->gb, 12);
  407. i += run;
  408. check_scantable_index(s, i);
  409. j = scantable[i];
  410. if (level < 0) {
  411. level = ((-level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
  412. level = -level;
  413. } else {
  414. level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
  415. }
  416. }
  417. mismatch ^= level;
  418. block[j] = level;
  419. if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
  420. break;
  421. UPDATE_CACHE(re, &s->gb);
  422. }
  423. end:
  424. LAST_SKIP_BITS(re, &s->gb, 2);
  425. CLOSE_READER(re, &s->gb);
  426. }
  427. block[63] ^= (mismatch & 1);
  428. s->block_last_index[n] = i;
  429. return 0;
  430. }
  431. /**
  432. * Note: this function can read out of range and crash for corrupt streams.
  433. * Changing this would eat up any speed benefits it has.
  434. * Do not use "fast" flag if you need the code to be robust.
  435. */
  436. static inline int mpeg2_fast_decode_block_non_intra(MpegEncContext *s,
  437. int16_t *block, int n)
  438. {
  439. int level, i, j, run;
  440. RLTable *rl = &ff_rl_mpeg1;
  441. uint8_t *const scantable = s->intra_scantable.permutated;
  442. const int qscale = s->qscale;
  443. OPEN_READER(re, &s->gb);
  444. i = -1;
  445. // special case for first coefficient, no need to add second VLC table
  446. UPDATE_CACHE(re, &s->gb);
  447. if (((int32_t) GET_CACHE(re, &s->gb)) < 0) {
  448. level = (3 * qscale) >> 1;
  449. if (GET_CACHE(re, &s->gb) & 0x40000000)
  450. level = -level;
  451. block[0] = level;
  452. i++;
  453. SKIP_BITS(re, &s->gb, 2);
  454. if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
  455. goto end;
  456. }
  457. /* now quantify & encode AC coefficients */
  458. for (;;) {
  459. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
  460. if (level != 0) {
  461. i += run;
  462. j = scantable[i];
  463. level = ((level * 2 + 1) * qscale) >> 1;
  464. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
  465. SHOW_SBITS(re, &s->gb, 1);
  466. SKIP_BITS(re, &s->gb, 1);
  467. } else {
  468. /* escape */
  469. run = SHOW_UBITS(re, &s->gb, 6) + 1;
  470. LAST_SKIP_BITS(re, &s->gb, 6);
  471. UPDATE_CACHE(re, &s->gb);
  472. level = SHOW_SBITS(re, &s->gb, 12);
  473. SKIP_BITS(re, &s->gb, 12);
  474. i += run;
  475. j = scantable[i];
  476. if (level < 0) {
  477. level = ((-level * 2 + 1) * qscale) >> 1;
  478. level = -level;
  479. } else {
  480. level = ((level * 2 + 1) * qscale) >> 1;
  481. }
  482. }
  483. block[j] = level;
  484. if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF || i > 63)
  485. break;
  486. UPDATE_CACHE(re, &s->gb);
  487. }
  488. end:
  489. LAST_SKIP_BITS(re, &s->gb, 2);
  490. CLOSE_READER(re, &s->gb);
  491. s->block_last_index[n] = i;
  492. return 0;
  493. }
  494. static inline int mpeg2_decode_block_intra(MpegEncContext *s,
  495. int16_t *block, int n)
  496. {
  497. int level, dc, diff, i, j, run;
  498. int component;
  499. RLTable *rl;
  500. uint8_t *const scantable = s->intra_scantable.permutated;
  501. const uint16_t *quant_matrix;
  502. const int qscale = s->qscale;
  503. int mismatch;
  504. /* DC coefficient */
  505. if (n < 4) {
  506. quant_matrix = s->intra_matrix;
  507. component = 0;
  508. } else {
  509. quant_matrix = s->chroma_intra_matrix;
  510. component = (n & 1) + 1;
  511. }
  512. diff = decode_dc(&s->gb, component);
  513. if (diff >= 0xffff)
  514. return AVERROR_INVALIDDATA;
  515. dc = s->last_dc[component];
  516. dc += diff;
  517. s->last_dc[component] = dc;
  518. block[0] = dc << (3 - s->intra_dc_precision);
  519. ff_tlog(s->avctx, "dc=%d\n", block[0]);
  520. mismatch = block[0] ^ 1;
  521. i = 0;
  522. if (s->intra_vlc_format)
  523. rl = &ff_rl_mpeg2;
  524. else
  525. rl = &ff_rl_mpeg1;
  526. {
  527. OPEN_READER(re, &s->gb);
  528. /* now quantify & encode AC coefficients */
  529. for (;;) {
  530. UPDATE_CACHE(re, &s->gb);
  531. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
  532. TEX_VLC_BITS, 2, 0);
  533. if (level == 127) {
  534. break;
  535. } else if (level != 0) {
  536. i += run;
  537. check_scantable_index(s, i);
  538. j = scantable[i];
  539. level = (level * qscale * quant_matrix[j]) >> 4;
  540. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
  541. SHOW_SBITS(re, &s->gb, 1);
  542. LAST_SKIP_BITS(re, &s->gb, 1);
  543. } else {
  544. /* escape */
  545. run = SHOW_UBITS(re, &s->gb, 6) + 1;
  546. LAST_SKIP_BITS(re, &s->gb, 6);
  547. UPDATE_CACHE(re, &s->gb);
  548. level = SHOW_SBITS(re, &s->gb, 12);
  549. SKIP_BITS(re, &s->gb, 12);
  550. i += run;
  551. check_scantable_index(s, i);
  552. j = scantable[i];
  553. if (level < 0) {
  554. level = (-level * qscale * quant_matrix[j]) >> 4;
  555. level = -level;
  556. } else {
  557. level = (level * qscale * quant_matrix[j]) >> 4;
  558. }
  559. }
  560. mismatch ^= level;
  561. block[j] = level;
  562. }
  563. CLOSE_READER(re, &s->gb);
  564. }
  565. block[63] ^= mismatch & 1;
  566. s->block_last_index[n] = i;
  567. return 0;
  568. }
  569. /**
  570. * Note: this function can read out of range and crash for corrupt streams.
  571. * Changing this would eat up any speed benefits it has.
  572. * Do not use "fast" flag if you need the code to be robust.
  573. */
  574. static inline int mpeg2_fast_decode_block_intra(MpegEncContext *s,
  575. int16_t *block, int n)
  576. {
  577. int level, dc, diff, i, j, run;
  578. int component;
  579. RLTable *rl;
  580. uint8_t *const scantable = s->intra_scantable.permutated;
  581. const uint16_t *quant_matrix;
  582. const int qscale = s->qscale;
  583. /* DC coefficient */
  584. if (n < 4) {
  585. quant_matrix = s->intra_matrix;
  586. component = 0;
  587. } else {
  588. quant_matrix = s->chroma_intra_matrix;
  589. component = (n & 1) + 1;
  590. }
  591. diff = decode_dc(&s->gb, component);
  592. if (diff >= 0xffff)
  593. return AVERROR_INVALIDDATA;
  594. dc = s->last_dc[component];
  595. dc += diff;
  596. s->last_dc[component] = dc;
  597. block[0] = dc << (3 - s->intra_dc_precision);
  598. i = 0;
  599. if (s->intra_vlc_format)
  600. rl = &ff_rl_mpeg2;
  601. else
  602. rl = &ff_rl_mpeg1;
  603. {
  604. OPEN_READER(re, &s->gb);
  605. /* now quantify & encode AC coefficients */
  606. for (;;) {
  607. UPDATE_CACHE(re, &s->gb);
  608. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
  609. TEX_VLC_BITS, 2, 0);
  610. if (level >= 64 || i > 63) {
  611. break;
  612. } else if (level != 0) {
  613. i += run;
  614. j = scantable[i];
  615. level = (level * qscale * quant_matrix[j]) >> 4;
  616. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
  617. SHOW_SBITS(re, &s->gb, 1);
  618. LAST_SKIP_BITS(re, &s->gb, 1);
  619. } else {
  620. /* escape */
  621. run = SHOW_UBITS(re, &s->gb, 6) + 1;
  622. LAST_SKIP_BITS(re, &s->gb, 6);
  623. UPDATE_CACHE(re, &s->gb);
  624. level = SHOW_SBITS(re, &s->gb, 12);
  625. SKIP_BITS(re, &s->gb, 12);
  626. i += run;
  627. j = scantable[i];
  628. if (level < 0) {
  629. level = (-level * qscale * quant_matrix[j]) >> 4;
  630. level = -level;
  631. } else {
  632. level = (level * qscale * quant_matrix[j]) >> 4;
  633. }
  634. }
  635. block[j] = level;
  636. }
  637. CLOSE_READER(re, &s->gb);
  638. }
  639. s->block_last_index[n] = i;
  640. return 0;
  641. }
  642. /******************************************/
  643. /* decoding */
  644. static inline int get_dmv(MpegEncContext *s)
  645. {
  646. if (get_bits1(&s->gb))
  647. return 1 - (get_bits1(&s->gb) << 1);
  648. else
  649. return 0;
  650. }
  651. static inline int get_qscale(MpegEncContext *s)
  652. {
  653. int qscale = get_bits(&s->gb, 5);
  654. if (s->q_scale_type)
  655. return ff_mpeg2_non_linear_qscale[qscale];
  656. else
  657. return qscale << 1;
  658. }
  659. /* motion type (for MPEG-2) */
  660. #define MT_FIELD 1
  661. #define MT_FRAME 2
  662. #define MT_16X8 2
  663. #define MT_DMV 3
  664. static int mpeg_decode_mb(MpegEncContext *s, int16_t block[12][64])
  665. {
  666. int i, j, k, cbp, val, mb_type, motion_type;
  667. const int mb_block_count = 4 + (1 << s->chroma_format);
  668. int ret;
  669. ff_tlog(s->avctx, "decode_mb: x=%d y=%d\n", s->mb_x, s->mb_y);
  670. av_assert2(s->mb_skipped == 0);
  671. if (s->mb_skip_run-- != 0) {
  672. if (s->pict_type == AV_PICTURE_TYPE_P) {
  673. s->mb_skipped = 1;
  674. s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride] =
  675. MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;
  676. } else {
  677. int mb_type;
  678. if (s->mb_x)
  679. mb_type = s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride - 1];
  680. else
  681. // FIXME not sure if this is allowed in MPEG at all
  682. mb_type = s->current_picture.mb_type[s->mb_width + (s->mb_y - 1) * s->mb_stride - 1];
  683. if (IS_INTRA(mb_type)) {
  684. av_log(s->avctx, AV_LOG_ERROR, "skip with previntra\n");
  685. return AVERROR_INVALIDDATA;
  686. }
  687. s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride] =
  688. mb_type | MB_TYPE_SKIP;
  689. if ((s->mv[0][0][0] | s->mv[0][0][1] | s->mv[1][0][0] | s->mv[1][0][1]) == 0)
  690. s->mb_skipped = 1;
  691. }
  692. return 0;
  693. }
  694. switch (s->pict_type) {
  695. default:
  696. case AV_PICTURE_TYPE_I:
  697. if (get_bits1(&s->gb) == 0) {
  698. if (get_bits1(&s->gb) == 0) {
  699. av_log(s->avctx, AV_LOG_ERROR,
  700. "invalid mb type in I Frame at %d %d\n",
  701. s->mb_x, s->mb_y);
  702. return AVERROR_INVALIDDATA;
  703. }
  704. mb_type = MB_TYPE_QUANT | MB_TYPE_INTRA;
  705. } else {
  706. mb_type = MB_TYPE_INTRA;
  707. }
  708. break;
  709. case AV_PICTURE_TYPE_P:
  710. mb_type = get_vlc2(&s->gb, ff_mb_ptype_vlc.table, MB_PTYPE_VLC_BITS, 1);
  711. if (mb_type < 0) {
  712. av_log(s->avctx, AV_LOG_ERROR,
  713. "invalid mb type in P Frame at %d %d\n", s->mb_x, s->mb_y);
  714. return AVERROR_INVALIDDATA;
  715. }
  716. mb_type = ptype2mb_type[mb_type];
  717. break;
  718. case AV_PICTURE_TYPE_B:
  719. mb_type = get_vlc2(&s->gb, ff_mb_btype_vlc.table, MB_BTYPE_VLC_BITS, 1);
  720. if (mb_type < 0) {
  721. av_log(s->avctx, AV_LOG_ERROR,
  722. "invalid mb type in B Frame at %d %d\n", s->mb_x, s->mb_y);
  723. return AVERROR_INVALIDDATA;
  724. }
  725. mb_type = btype2mb_type[mb_type];
  726. break;
  727. }
  728. ff_tlog(s->avctx, "mb_type=%x\n", mb_type);
  729. // motion_type = 0; /* avoid warning */
  730. if (IS_INTRA(mb_type)) {
  731. s->bdsp.clear_blocks(s->block[0]);
  732. if (!s->chroma_y_shift)
  733. s->bdsp.clear_blocks(s->block[6]);
  734. /* compute DCT type */
  735. // FIXME: add an interlaced_dct coded var?
  736. if (s->picture_structure == PICT_FRAME &&
  737. !s->frame_pred_frame_dct)
  738. s->interlaced_dct = get_bits1(&s->gb);
  739. if (IS_QUANT(mb_type))
  740. s->qscale = get_qscale(s);
  741. if (s->concealment_motion_vectors) {
  742. /* just parse them */
  743. if (s->picture_structure != PICT_FRAME)
  744. skip_bits1(&s->gb); /* field select */
  745. s->mv[0][0][0] =
  746. s->last_mv[0][0][0] =
  747. s->last_mv[0][1][0] = mpeg_decode_motion(s, s->mpeg_f_code[0][0],
  748. s->last_mv[0][0][0]);
  749. s->mv[0][0][1] =
  750. s->last_mv[0][0][1] =
  751. s->last_mv[0][1][1] = mpeg_decode_motion(s, s->mpeg_f_code[0][1],
  752. s->last_mv[0][0][1]);
  753. check_marker(&s->gb, "after concealment_motion_vectors");
  754. } else {
  755. /* reset mv prediction */
  756. memset(s->last_mv, 0, sizeof(s->last_mv));
  757. }
  758. s->mb_intra = 1;
  759. // if 1, we memcpy blocks in xvmcvideo
  760. if ((CONFIG_MPEG1_XVMC_HWACCEL || CONFIG_MPEG2_XVMC_HWACCEL) && s->pack_pblocks)
  761. ff_xvmc_pack_pblocks(s, -1); // inter are always full blocks
  762. if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
  763. if (s->avctx->flags2 & AV_CODEC_FLAG2_FAST) {
  764. for (i = 0; i < 6; i++)
  765. mpeg2_fast_decode_block_intra(s, *s->pblocks[i], i);
  766. } else {
  767. for (i = 0; i < mb_block_count; i++)
  768. if ((ret = mpeg2_decode_block_intra(s, *s->pblocks[i], i)) < 0)
  769. return ret;
  770. }
  771. } else {
  772. for (i = 0; i < 6; i++)
  773. if ((ret = mpeg1_decode_block_intra(s, *s->pblocks[i], i)) < 0)
  774. return ret;
  775. }
  776. } else {
  777. if (mb_type & MB_TYPE_ZERO_MV) {
  778. av_assert2(mb_type & MB_TYPE_CBP);
  779. s->mv_dir = MV_DIR_FORWARD;
  780. if (s->picture_structure == PICT_FRAME) {
  781. if (s->picture_structure == PICT_FRAME
  782. && !s->frame_pred_frame_dct)
  783. s->interlaced_dct = get_bits1(&s->gb);
  784. s->mv_type = MV_TYPE_16X16;
  785. } else {
  786. s->mv_type = MV_TYPE_FIELD;
  787. mb_type |= MB_TYPE_INTERLACED;
  788. s->field_select[0][0] = s->picture_structure - 1;
  789. }
  790. if (IS_QUANT(mb_type))
  791. s->qscale = get_qscale(s);
  792. s->last_mv[0][0][0] = 0;
  793. s->last_mv[0][0][1] = 0;
  794. s->last_mv[0][1][0] = 0;
  795. s->last_mv[0][1][1] = 0;
  796. s->mv[0][0][0] = 0;
  797. s->mv[0][0][1] = 0;
  798. } else {
  799. av_assert2(mb_type & MB_TYPE_L0L1);
  800. // FIXME decide if MBs in field pictures are MB_TYPE_INTERLACED
  801. /* get additional motion vector type */
  802. if (s->picture_structure == PICT_FRAME && s->frame_pred_frame_dct) {
  803. motion_type = MT_FRAME;
  804. } else {
  805. motion_type = get_bits(&s->gb, 2);
  806. if (s->picture_structure == PICT_FRAME && HAS_CBP(mb_type))
  807. s->interlaced_dct = get_bits1(&s->gb);
  808. }
  809. if (IS_QUANT(mb_type))
  810. s->qscale = get_qscale(s);
  811. /* motion vectors */
  812. s->mv_dir = (mb_type >> 13) & 3;
  813. ff_tlog(s->avctx, "motion_type=%d\n", motion_type);
  814. switch (motion_type) {
  815. case MT_FRAME: /* or MT_16X8 */
  816. if (s->picture_structure == PICT_FRAME) {
  817. mb_type |= MB_TYPE_16x16;
  818. s->mv_type = MV_TYPE_16X16;
  819. for (i = 0; i < 2; i++) {
  820. if (USES_LIST(mb_type, i)) {
  821. /* MT_FRAME */
  822. s->mv[i][0][0] =
  823. s->last_mv[i][0][0] =
  824. s->last_mv[i][1][0] =
  825. mpeg_decode_motion(s, s->mpeg_f_code[i][0],
  826. s->last_mv[i][0][0]);
  827. s->mv[i][0][1] =
  828. s->last_mv[i][0][1] =
  829. s->last_mv[i][1][1] =
  830. mpeg_decode_motion(s, s->mpeg_f_code[i][1],
  831. s->last_mv[i][0][1]);
  832. /* full_pel: only for MPEG-1 */
  833. if (s->full_pel[i]) {
  834. s->mv[i][0][0] <<= 1;
  835. s->mv[i][0][1] <<= 1;
  836. }
  837. }
  838. }
  839. } else {
  840. mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
  841. s->mv_type = MV_TYPE_16X8;
  842. for (i = 0; i < 2; i++) {
  843. if (USES_LIST(mb_type, i)) {
  844. /* MT_16X8 */
  845. for (j = 0; j < 2; j++) {
  846. s->field_select[i][j] = get_bits1(&s->gb);
  847. for (k = 0; k < 2; k++) {
  848. val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
  849. s->last_mv[i][j][k]);
  850. s->last_mv[i][j][k] = val;
  851. s->mv[i][j][k] = val;
  852. }
  853. }
  854. }
  855. }
  856. }
  857. break;
  858. case MT_FIELD:
  859. s->mv_type = MV_TYPE_FIELD;
  860. if (s->picture_structure == PICT_FRAME) {
  861. mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
  862. for (i = 0; i < 2; i++) {
  863. if (USES_LIST(mb_type, i)) {
  864. for (j = 0; j < 2; j++) {
  865. s->field_select[i][j] = get_bits1(&s->gb);
  866. val = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
  867. s->last_mv[i][j][0]);
  868. s->last_mv[i][j][0] = val;
  869. s->mv[i][j][0] = val;
  870. ff_tlog(s->avctx, "fmx=%d\n", val);
  871. val = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
  872. s->last_mv[i][j][1] >> 1);
  873. s->last_mv[i][j][1] = 2 * val;
  874. s->mv[i][j][1] = val;
  875. ff_tlog(s->avctx, "fmy=%d\n", val);
  876. }
  877. }
  878. }
  879. } else {
  880. av_assert0(!s->progressive_sequence);
  881. mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
  882. for (i = 0; i < 2; i++) {
  883. if (USES_LIST(mb_type, i)) {
  884. s->field_select[i][0] = get_bits1(&s->gb);
  885. for (k = 0; k < 2; k++) {
  886. val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
  887. s->last_mv[i][0][k]);
  888. s->last_mv[i][0][k] = val;
  889. s->last_mv[i][1][k] = val;
  890. s->mv[i][0][k] = val;
  891. }
  892. }
  893. }
  894. }
  895. break;
  896. case MT_DMV:
  897. if (s->progressive_sequence){
  898. av_log(s->avctx, AV_LOG_ERROR, "MT_DMV in progressive_sequence\n");
  899. return AVERROR_INVALIDDATA;
  900. }
  901. s->mv_type = MV_TYPE_DMV;
  902. for (i = 0; i < 2; i++) {
  903. if (USES_LIST(mb_type, i)) {
  904. int dmx, dmy, mx, my, m;
  905. const int my_shift = s->picture_structure == PICT_FRAME;
  906. mx = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
  907. s->last_mv[i][0][0]);
  908. s->last_mv[i][0][0] = mx;
  909. s->last_mv[i][1][0] = mx;
  910. dmx = get_dmv(s);
  911. my = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
  912. s->last_mv[i][0][1] >> my_shift);
  913. dmy = get_dmv(s);
  914. s->last_mv[i][0][1] = my << my_shift;
  915. s->last_mv[i][1][1] = my << my_shift;
  916. s->mv[i][0][0] = mx;
  917. s->mv[i][0][1] = my;
  918. s->mv[i][1][0] = mx; // not used
  919. s->mv[i][1][1] = my; // not used
  920. if (s->picture_structure == PICT_FRAME) {
  921. mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
  922. // m = 1 + 2 * s->top_field_first;
  923. m = s->top_field_first ? 1 : 3;
  924. /* top -> top pred */
  925. s->mv[i][2][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
  926. s->mv[i][2][1] = ((my * m + (my > 0)) >> 1) + dmy - 1;
  927. m = 4 - m;
  928. s->mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
  929. s->mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1;
  930. } else {
  931. mb_type |= MB_TYPE_16x16;
  932. s->mv[i][2][0] = ((mx + (mx > 0)) >> 1) + dmx;
  933. s->mv[i][2][1] = ((my + (my > 0)) >> 1) + dmy;
  934. if (s->picture_structure == PICT_TOP_FIELD)
  935. s->mv[i][2][1]--;
  936. else
  937. s->mv[i][2][1]++;
  938. }
  939. }
  940. }
  941. break;
  942. default:
  943. av_log(s->avctx, AV_LOG_ERROR,
  944. "00 motion_type at %d %d\n", s->mb_x, s->mb_y);
  945. return AVERROR_INVALIDDATA;
  946. }
  947. }
  948. s->mb_intra = 0;
  949. if (HAS_CBP(mb_type)) {
  950. s->bdsp.clear_blocks(s->block[0]);
  951. cbp = get_vlc2(&s->gb, ff_mb_pat_vlc.table, MB_PAT_VLC_BITS, 1);
  952. if (mb_block_count > 6) {
  953. cbp <<= mb_block_count - 6;
  954. cbp |= get_bits(&s->gb, mb_block_count - 6);
  955. s->bdsp.clear_blocks(s->block[6]);
  956. }
  957. if (cbp <= 0) {
  958. av_log(s->avctx, AV_LOG_ERROR,
  959. "invalid cbp %d at %d %d\n", cbp, s->mb_x, s->mb_y);
  960. return AVERROR_INVALIDDATA;
  961. }
  962. // if 1, we memcpy blocks in xvmcvideo
  963. if ((CONFIG_MPEG1_XVMC_HWACCEL || CONFIG_MPEG2_XVMC_HWACCEL) && s->pack_pblocks)
  964. ff_xvmc_pack_pblocks(s, cbp);
  965. if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
  966. if (s->avctx->flags2 & AV_CODEC_FLAG2_FAST) {
  967. for (i = 0; i < 6; i++) {
  968. if (cbp & 32)
  969. mpeg2_fast_decode_block_non_intra(s, *s->pblocks[i], i);
  970. else
  971. s->block_last_index[i] = -1;
  972. cbp += cbp;
  973. }
  974. } else {
  975. cbp <<= 12 - mb_block_count;
  976. for (i = 0; i < mb_block_count; i++) {
  977. if (cbp & (1 << 11)) {
  978. if ((ret = mpeg2_decode_block_non_intra(s, *s->pblocks[i], i)) < 0)
  979. return ret;
  980. } else {
  981. s->block_last_index[i] = -1;
  982. }
  983. cbp += cbp;
  984. }
  985. }
  986. } else {
  987. if (s->avctx->flags2 & AV_CODEC_FLAG2_FAST) {
  988. for (i = 0; i < 6; i++) {
  989. if (cbp & 32)
  990. mpeg1_fast_decode_block_inter(s, *s->pblocks[i], i);
  991. else
  992. s->block_last_index[i] = -1;
  993. cbp += cbp;
  994. }
  995. } else {
  996. for (i = 0; i < 6; i++) {
  997. if (cbp & 32) {
  998. if ((ret = mpeg1_decode_block_inter(s, *s->pblocks[i], i)) < 0)
  999. return ret;
  1000. } else {
  1001. s->block_last_index[i] = -1;
  1002. }
  1003. cbp += cbp;
  1004. }
  1005. }
  1006. }
  1007. } else {
  1008. for (i = 0; i < 12; i++)
  1009. s->block_last_index[i] = -1;
  1010. }
  1011. }
  1012. s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride] = mb_type;
  1013. return 0;
  1014. }
  1015. static av_cold int mpeg_decode_init(AVCodecContext *avctx)
  1016. {
  1017. Mpeg1Context *s = avctx->priv_data;
  1018. MpegEncContext *s2 = &s->mpeg_enc_ctx;
  1019. ff_mpv_decode_defaults(s2);
  1020. if ( avctx->codec_tag != AV_RL32("VCR2")
  1021. && avctx->codec_tag != AV_RL32("BW10"))
  1022. avctx->coded_width = avctx->coded_height = 0; // do not trust dimensions from input
  1023. ff_mpv_decode_init(s2, avctx);
  1024. s->mpeg_enc_ctx.avctx = avctx;
  1025. /* we need some permutation to store matrices,
  1026. * until the decoder sets the real permutation. */
  1027. ff_mpv_idct_init(s2);
  1028. ff_mpeg12_common_init(&s->mpeg_enc_ctx);
  1029. ff_mpeg12_init_vlcs();
  1030. s2->chroma_format = 1;
  1031. s->mpeg_enc_ctx_allocated = 0;
  1032. s->mpeg_enc_ctx.picture_number = 0;
  1033. s->repeat_field = 0;
  1034. s->mpeg_enc_ctx.codec_id = avctx->codec->id;
  1035. avctx->color_range = AVCOL_RANGE_MPEG;
  1036. return 0;
  1037. }
  1038. #if HAVE_THREADS
  1039. static int mpeg_decode_update_thread_context(AVCodecContext *avctx,
  1040. const AVCodecContext *avctx_from)
  1041. {
  1042. Mpeg1Context *ctx = avctx->priv_data, *ctx_from = avctx_from->priv_data;
  1043. MpegEncContext *s = &ctx->mpeg_enc_ctx, *s1 = &ctx_from->mpeg_enc_ctx;
  1044. int err;
  1045. if (avctx == avctx_from ||
  1046. !ctx_from->mpeg_enc_ctx_allocated ||
  1047. !s1->context_initialized)
  1048. return 0;
  1049. err = ff_mpeg_update_thread_context(avctx, avctx_from);
  1050. if (err)
  1051. return err;
  1052. if (!ctx->mpeg_enc_ctx_allocated)
  1053. memcpy(s + 1, s1 + 1, sizeof(Mpeg1Context) - sizeof(MpegEncContext));
  1054. if (!(s->pict_type == AV_PICTURE_TYPE_B || s->low_delay))
  1055. s->picture_number++;
  1056. return 0;
  1057. }
  1058. #endif
  1059. static void quant_matrix_rebuild(uint16_t *matrix, const uint8_t *old_perm,
  1060. const uint8_t *new_perm)
  1061. {
  1062. uint16_t temp_matrix[64];
  1063. int i;
  1064. memcpy(temp_matrix, matrix, 64 * sizeof(uint16_t));
  1065. for (i = 0; i < 64; i++)
  1066. matrix[new_perm[i]] = temp_matrix[old_perm[i]];
  1067. }
  1068. static const enum AVPixelFormat mpeg1_hwaccel_pixfmt_list_420[] = {
  1069. #if CONFIG_MPEG1_XVMC_HWACCEL
  1070. AV_PIX_FMT_XVMC,
  1071. #endif
  1072. #if CONFIG_MPEG1_VDPAU_DECODER && FF_API_VDPAU
  1073. AV_PIX_FMT_VDPAU_MPEG1,
  1074. #endif
  1075. #if CONFIG_MPEG1_VDPAU_HWACCEL
  1076. AV_PIX_FMT_VDPAU,
  1077. #endif
  1078. AV_PIX_FMT_YUV420P,
  1079. AV_PIX_FMT_NONE
  1080. };
  1081. static const enum AVPixelFormat mpeg2_hwaccel_pixfmt_list_420[] = {
  1082. #if CONFIG_MPEG2_XVMC_HWACCEL
  1083. AV_PIX_FMT_XVMC,
  1084. #endif
  1085. #if CONFIG_MPEG_VDPAU_DECODER && FF_API_VDPAU
  1086. AV_PIX_FMT_VDPAU_MPEG2,
  1087. #endif
  1088. #if CONFIG_MPEG2_VDPAU_HWACCEL
  1089. AV_PIX_FMT_VDPAU,
  1090. #endif
  1091. #if CONFIG_MPEG2_DXVA2_HWACCEL
  1092. AV_PIX_FMT_DXVA2_VLD,
  1093. #endif
  1094. #if CONFIG_MPEG2_D3D11VA_HWACCEL
  1095. AV_PIX_FMT_D3D11VA_VLD,
  1096. #endif
  1097. #if CONFIG_MPEG2_VAAPI_HWACCEL
  1098. AV_PIX_FMT_VAAPI,
  1099. #endif
  1100. #if CONFIG_MPEG2_VIDEOTOOLBOX_HWACCEL
  1101. AV_PIX_FMT_VIDEOTOOLBOX,
  1102. #endif
  1103. AV_PIX_FMT_YUV420P,
  1104. AV_PIX_FMT_NONE
  1105. };
  1106. static const enum AVPixelFormat mpeg12_pixfmt_list_422[] = {
  1107. AV_PIX_FMT_YUV422P,
  1108. AV_PIX_FMT_NONE
  1109. };
  1110. static const enum AVPixelFormat mpeg12_pixfmt_list_444[] = {
  1111. AV_PIX_FMT_YUV444P,
  1112. AV_PIX_FMT_NONE
  1113. };
  1114. #if FF_API_VDPAU
  1115. static inline int uses_vdpau(AVCodecContext *avctx) {
  1116. return avctx->pix_fmt == AV_PIX_FMT_VDPAU_MPEG1 || avctx->pix_fmt == AV_PIX_FMT_VDPAU_MPEG2;
  1117. }
  1118. #endif
  1119. static enum AVPixelFormat mpeg_get_pixelformat(AVCodecContext *avctx)
  1120. {
  1121. Mpeg1Context *s1 = avctx->priv_data;
  1122. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1123. const enum AVPixelFormat *pix_fmts;
  1124. if (CONFIG_GRAY && (avctx->flags & AV_CODEC_FLAG_GRAY))
  1125. return AV_PIX_FMT_GRAY8;
  1126. if (s->chroma_format < 2)
  1127. pix_fmts = avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO ?
  1128. mpeg1_hwaccel_pixfmt_list_420 :
  1129. mpeg2_hwaccel_pixfmt_list_420;
  1130. else if (s->chroma_format == 2)
  1131. pix_fmts = mpeg12_pixfmt_list_422;
  1132. else
  1133. pix_fmts = mpeg12_pixfmt_list_444;
  1134. return ff_thread_get_format(avctx, pix_fmts);
  1135. }
  1136. static void setup_hwaccel_for_pixfmt(AVCodecContext *avctx)
  1137. {
  1138. // until then pix_fmt may be changed right after codec init
  1139. if (avctx->hwaccel
  1140. #if FF_API_VDPAU
  1141. || uses_vdpau(avctx)
  1142. #endif
  1143. )
  1144. if (avctx->idct_algo == FF_IDCT_AUTO)
  1145. avctx->idct_algo = FF_IDCT_SIMPLE;
  1146. if (avctx->hwaccel && avctx->pix_fmt == AV_PIX_FMT_XVMC) {
  1147. Mpeg1Context *s1 = avctx->priv_data;
  1148. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1149. s->pack_pblocks = 1;
  1150. #if FF_API_XVMC
  1151. FF_DISABLE_DEPRECATION_WARNINGS
  1152. avctx->xvmc_acceleration = 2;
  1153. FF_ENABLE_DEPRECATION_WARNINGS
  1154. #endif /* FF_API_XVMC */
  1155. }
  1156. }
  1157. /* Call this function when we know all parameters.
  1158. * It may be called in different places for MPEG-1 and MPEG-2. */
  1159. static int mpeg_decode_postinit(AVCodecContext *avctx)
  1160. {
  1161. Mpeg1Context *s1 = avctx->priv_data;
  1162. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1163. uint8_t old_permutation[64];
  1164. int ret;
  1165. if (avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
  1166. // MPEG-1 aspect
  1167. avctx->sample_aspect_ratio = av_d2q(1.0 / ff_mpeg1_aspect[s->aspect_ratio_info], 255);
  1168. } else { // MPEG-2
  1169. // MPEG-2 aspect
  1170. if (s->aspect_ratio_info > 1) {
  1171. AVRational dar =
  1172. av_mul_q(av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
  1173. (AVRational) { s1->pan_scan.width,
  1174. s1->pan_scan.height }),
  1175. (AVRational) { s->width, s->height });
  1176. /* We ignore the spec here and guess a bit as reality does not
  1177. * match the spec, see for example res_change_ffmpeg_aspect.ts
  1178. * and sequence-display-aspect.mpg.
  1179. * issue1613, 621, 562 */
  1180. if ((s1->pan_scan.width == 0) || (s1->pan_scan.height == 0) ||
  1181. (av_cmp_q(dar, (AVRational) { 4, 3 }) &&
  1182. av_cmp_q(dar, (AVRational) { 16, 9 }))) {
  1183. s->avctx->sample_aspect_ratio =
  1184. av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
  1185. (AVRational) { s->width, s->height });
  1186. } else {
  1187. s->avctx->sample_aspect_ratio =
  1188. av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
  1189. (AVRational) { s1->pan_scan.width, s1->pan_scan.height });
  1190. // issue1613 4/3 16/9 -> 16/9
  1191. // res_change_ffmpeg_aspect.ts 4/3 225/44 ->4/3
  1192. // widescreen-issue562.mpg 4/3 16/9 -> 16/9
  1193. // s->avctx->sample_aspect_ratio = av_mul_q(s->avctx->sample_aspect_ratio, (AVRational) {s->width, s->height});
  1194. ff_dlog(avctx, "aspect A %d/%d\n",
  1195. ff_mpeg2_aspect[s->aspect_ratio_info].num,
  1196. ff_mpeg2_aspect[s->aspect_ratio_info].den);
  1197. ff_dlog(avctx, "aspect B %d/%d\n", s->avctx->sample_aspect_ratio.num,
  1198. s->avctx->sample_aspect_ratio.den);
  1199. }
  1200. } else {
  1201. s->avctx->sample_aspect_ratio =
  1202. ff_mpeg2_aspect[s->aspect_ratio_info];
  1203. }
  1204. } // MPEG-2
  1205. if (av_image_check_sar(s->width, s->height,
  1206. avctx->sample_aspect_ratio) < 0) {
  1207. av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
  1208. avctx->sample_aspect_ratio.num,
  1209. avctx->sample_aspect_ratio.den);
  1210. avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
  1211. }
  1212. if ((s1->mpeg_enc_ctx_allocated == 0) ||
  1213. avctx->coded_width != s->width ||
  1214. avctx->coded_height != s->height ||
  1215. s1->save_width != s->width ||
  1216. s1->save_height != s->height ||
  1217. av_cmp_q(s1->save_aspect, s->avctx->sample_aspect_ratio) ||
  1218. (s1->save_progressive_seq != s->progressive_sequence && FFALIGN(s->height, 16) != FFALIGN(s->height, 32)) ||
  1219. 0) {
  1220. if (s1->mpeg_enc_ctx_allocated) {
  1221. ParseContext pc = s->parse_context;
  1222. s->parse_context.buffer = 0;
  1223. ff_mpv_common_end(s);
  1224. s->parse_context = pc;
  1225. s1->mpeg_enc_ctx_allocated = 0;
  1226. }
  1227. ret = ff_set_dimensions(avctx, s->width, s->height);
  1228. if (ret < 0)
  1229. return ret;
  1230. if (avctx->codec_id == AV_CODEC_ID_MPEG2VIDEO && s->bit_rate) {
  1231. avctx->rc_max_rate = s->bit_rate;
  1232. } else if (avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO && s->bit_rate &&
  1233. (s->bit_rate != 0x3FFFF*400 || s->vbv_delay != 0xFFFF)) {
  1234. avctx->bit_rate = s->bit_rate;
  1235. }
  1236. s1->save_aspect = s->avctx->sample_aspect_ratio;
  1237. s1->save_width = s->width;
  1238. s1->save_height = s->height;
  1239. s1->save_progressive_seq = s->progressive_sequence;
  1240. /* low_delay may be forced, in this case we will have B-frames
  1241. * that behave like P-frames. */
  1242. avctx->has_b_frames = !s->low_delay;
  1243. if (avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
  1244. // MPEG-1 fps
  1245. avctx->framerate = ff_mpeg12_frame_rate_tab[s->frame_rate_index];
  1246. avctx->ticks_per_frame = 1;
  1247. avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
  1248. } else { // MPEG-2
  1249. // MPEG-2 fps
  1250. av_reduce(&s->avctx->framerate.num,
  1251. &s->avctx->framerate.den,
  1252. ff_mpeg12_frame_rate_tab[s->frame_rate_index].num * s1->frame_rate_ext.num,
  1253. ff_mpeg12_frame_rate_tab[s->frame_rate_index].den * s1->frame_rate_ext.den,
  1254. 1 << 30);
  1255. avctx->ticks_per_frame = 2;
  1256. switch (s->chroma_format) {
  1257. case 1: avctx->chroma_sample_location = AVCHROMA_LOC_LEFT; break;
  1258. case 2:
  1259. case 3: avctx->chroma_sample_location = AVCHROMA_LOC_TOPLEFT; break;
  1260. default: av_assert0(0);
  1261. }
  1262. } // MPEG-2
  1263. avctx->pix_fmt = mpeg_get_pixelformat(avctx);
  1264. setup_hwaccel_for_pixfmt(avctx);
  1265. /* Quantization matrices may need reordering
  1266. * if DCT permutation is changed. */
  1267. memcpy(old_permutation, s->idsp.idct_permutation, 64 * sizeof(uint8_t));
  1268. ff_mpv_idct_init(s);
  1269. if ((ret = ff_mpv_common_init(s)) < 0)
  1270. return ret;
  1271. quant_matrix_rebuild(s->intra_matrix, old_permutation, s->idsp.idct_permutation);
  1272. quant_matrix_rebuild(s->inter_matrix, old_permutation, s->idsp.idct_permutation);
  1273. quant_matrix_rebuild(s->chroma_intra_matrix, old_permutation, s->idsp.idct_permutation);
  1274. quant_matrix_rebuild(s->chroma_inter_matrix, old_permutation, s->idsp.idct_permutation);
  1275. s1->mpeg_enc_ctx_allocated = 1;
  1276. }
  1277. return 0;
  1278. }
  1279. static int mpeg1_decode_picture(AVCodecContext *avctx, const uint8_t *buf,
  1280. int buf_size)
  1281. {
  1282. Mpeg1Context *s1 = avctx->priv_data;
  1283. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1284. int ref, f_code, vbv_delay;
  1285. init_get_bits(&s->gb, buf, buf_size * 8);
  1286. ref = get_bits(&s->gb, 10); /* temporal ref */
  1287. s->pict_type = get_bits(&s->gb, 3);
  1288. if (s->pict_type == 0 || s->pict_type > 3)
  1289. return AVERROR_INVALIDDATA;
  1290. vbv_delay = get_bits(&s->gb, 16);
  1291. s->vbv_delay = vbv_delay;
  1292. if (s->pict_type == AV_PICTURE_TYPE_P ||
  1293. s->pict_type == AV_PICTURE_TYPE_B) {
  1294. s->full_pel[0] = get_bits1(&s->gb);
  1295. f_code = get_bits(&s->gb, 3);
  1296. if (f_code == 0 && (avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT)))
  1297. return AVERROR_INVALIDDATA;
  1298. f_code += !f_code;
  1299. s->mpeg_f_code[0][0] = f_code;
  1300. s->mpeg_f_code[0][1] = f_code;
  1301. }
  1302. if (s->pict_type == AV_PICTURE_TYPE_B) {
  1303. s->full_pel[1] = get_bits1(&s->gb);
  1304. f_code = get_bits(&s->gb, 3);
  1305. if (f_code == 0 && (avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT)))
  1306. return AVERROR_INVALIDDATA;
  1307. f_code += !f_code;
  1308. s->mpeg_f_code[1][0] = f_code;
  1309. s->mpeg_f_code[1][1] = f_code;
  1310. }
  1311. s->current_picture.f->pict_type = s->pict_type;
  1312. s->current_picture.f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
  1313. if (avctx->debug & FF_DEBUG_PICT_INFO)
  1314. av_log(avctx, AV_LOG_DEBUG,
  1315. "vbv_delay %d, ref %d type:%d\n", vbv_delay, ref, s->pict_type);
  1316. s->y_dc_scale = 8;
  1317. s->c_dc_scale = 8;
  1318. return 0;
  1319. }
  1320. static void mpeg_decode_sequence_extension(Mpeg1Context *s1)
  1321. {
  1322. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1323. int horiz_size_ext, vert_size_ext;
  1324. int bit_rate_ext;
  1325. skip_bits(&s->gb, 1); /* profile and level esc*/
  1326. s->avctx->profile = get_bits(&s->gb, 3);
  1327. s->avctx->level = get_bits(&s->gb, 4);
  1328. s->progressive_sequence = get_bits1(&s->gb); /* progressive_sequence */
  1329. s->chroma_format = get_bits(&s->gb, 2); /* chroma_format 1=420, 2=422, 3=444 */
  1330. if (!s->chroma_format) {
  1331. s->chroma_format = 1;
  1332. av_log(s->avctx, AV_LOG_WARNING, "Chroma format invalid\n");
  1333. }
  1334. horiz_size_ext = get_bits(&s->gb, 2);
  1335. vert_size_ext = get_bits(&s->gb, 2);
  1336. s->width |= (horiz_size_ext << 12);
  1337. s->height |= (vert_size_ext << 12);
  1338. bit_rate_ext = get_bits(&s->gb, 12); /* XXX: handle it */
  1339. s->bit_rate += (bit_rate_ext << 18) * 400LL;
  1340. check_marker(&s->gb, "after bit rate extension");
  1341. s->avctx->rc_buffer_size += get_bits(&s->gb, 8) * 1024 * 16 << 10;
  1342. s->low_delay = get_bits1(&s->gb);
  1343. if (s->avctx->flags & AV_CODEC_FLAG_LOW_DELAY)
  1344. s->low_delay = 1;
  1345. s1->frame_rate_ext.num = get_bits(&s->gb, 2) + 1;
  1346. s1->frame_rate_ext.den = get_bits(&s->gb, 5) + 1;
  1347. ff_dlog(s->avctx, "sequence extension\n");
  1348. s->codec_id = s->avctx->codec_id = AV_CODEC_ID_MPEG2VIDEO;
  1349. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  1350. av_log(s->avctx, AV_LOG_DEBUG,
  1351. "profile: %d, level: %d ps: %d cf:%d vbv buffer: %d, bitrate:%"PRId64"\n",
  1352. s->avctx->profile, s->avctx->level, s->progressive_sequence, s->chroma_format,
  1353. s->avctx->rc_buffer_size, s->bit_rate);
  1354. }
  1355. static void mpeg_decode_sequence_display_extension(Mpeg1Context *s1)
  1356. {
  1357. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1358. int color_description, w, h;
  1359. skip_bits(&s->gb, 3); /* video format */
  1360. color_description = get_bits1(&s->gb);
  1361. if (color_description) {
  1362. s->avctx->color_primaries = get_bits(&s->gb, 8);
  1363. s->avctx->color_trc = get_bits(&s->gb, 8);
  1364. s->avctx->colorspace = get_bits(&s->gb, 8);
  1365. }
  1366. w = get_bits(&s->gb, 14);
  1367. skip_bits(&s->gb, 1); // marker
  1368. h = get_bits(&s->gb, 14);
  1369. // remaining 3 bits are zero padding
  1370. s1->pan_scan.width = 16 * w;
  1371. s1->pan_scan.height = 16 * h;
  1372. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  1373. av_log(s->avctx, AV_LOG_DEBUG, "sde w:%d, h:%d\n", w, h);
  1374. }
  1375. static void mpeg_decode_picture_display_extension(Mpeg1Context *s1)
  1376. {
  1377. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1378. int i, nofco;
  1379. nofco = 1;
  1380. if (s->progressive_sequence) {
  1381. if (s->repeat_first_field) {
  1382. nofco++;
  1383. if (s->top_field_first)
  1384. nofco++;
  1385. }
  1386. } else {
  1387. if (s->picture_structure == PICT_FRAME) {
  1388. nofco++;
  1389. if (s->repeat_first_field)
  1390. nofco++;
  1391. }
  1392. }
  1393. for (i = 0; i < nofco; i++) {
  1394. s1->pan_scan.position[i][0] = get_sbits(&s->gb, 16);
  1395. skip_bits(&s->gb, 1); // marker
  1396. s1->pan_scan.position[i][1] = get_sbits(&s->gb, 16);
  1397. skip_bits(&s->gb, 1); // marker
  1398. }
  1399. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  1400. av_log(s->avctx, AV_LOG_DEBUG,
  1401. "pde (%"PRId16",%"PRId16") (%"PRId16",%"PRId16") (%"PRId16",%"PRId16")\n",
  1402. s1->pan_scan.position[0][0], s1->pan_scan.position[0][1],
  1403. s1->pan_scan.position[1][0], s1->pan_scan.position[1][1],
  1404. s1->pan_scan.position[2][0], s1->pan_scan.position[2][1]);
  1405. }
  1406. static int load_matrix(MpegEncContext *s, uint16_t matrix0[64],
  1407. uint16_t matrix1[64], int intra)
  1408. {
  1409. int i;
  1410. for (i = 0; i < 64; i++) {
  1411. int j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
  1412. int v = get_bits(&s->gb, 8);
  1413. if (v == 0) {
  1414. av_log(s->avctx, AV_LOG_ERROR, "matrix damaged\n");
  1415. return AVERROR_INVALIDDATA;
  1416. }
  1417. if (intra && i == 0 && v != 8) {
  1418. av_log(s->avctx, AV_LOG_DEBUG, "intra matrix specifies invalid DC quantizer %d, ignoring\n", v);
  1419. v = 8; // needed by pink.mpg / issue1046
  1420. }
  1421. matrix0[j] = v;
  1422. if (matrix1)
  1423. matrix1[j] = v;
  1424. }
  1425. return 0;
  1426. }
  1427. static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
  1428. {
  1429. ff_dlog(s->avctx, "matrix extension\n");
  1430. if (get_bits1(&s->gb))
  1431. load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
  1432. if (get_bits1(&s->gb))
  1433. load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
  1434. if (get_bits1(&s->gb))
  1435. load_matrix(s, s->chroma_intra_matrix, NULL, 1);
  1436. if (get_bits1(&s->gb))
  1437. load_matrix(s, s->chroma_inter_matrix, NULL, 0);
  1438. }
  1439. static void mpeg_decode_picture_coding_extension(Mpeg1Context *s1)
  1440. {
  1441. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1442. s->full_pel[0] = s->full_pel[1] = 0;
  1443. s->mpeg_f_code[0][0] = get_bits(&s->gb, 4);
  1444. s->mpeg_f_code[0][1] = get_bits(&s->gb, 4);
  1445. s->mpeg_f_code[1][0] = get_bits(&s->gb, 4);
  1446. s->mpeg_f_code[1][1] = get_bits(&s->gb, 4);
  1447. if (!s->pict_type && s1->mpeg_enc_ctx_allocated) {
  1448. av_log(s->avctx, AV_LOG_ERROR,
  1449. "Missing picture start code, guessing missing values\n");
  1450. if (s->mpeg_f_code[1][0] == 15 && s->mpeg_f_code[1][1] == 15) {
  1451. if (s->mpeg_f_code[0][0] == 15 && s->mpeg_f_code[0][1] == 15)
  1452. s->pict_type = AV_PICTURE_TYPE_I;
  1453. else
  1454. s->pict_type = AV_PICTURE_TYPE_P;
  1455. } else
  1456. s->pict_type = AV_PICTURE_TYPE_B;
  1457. s->current_picture.f->pict_type = s->pict_type;
  1458. s->current_picture.f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
  1459. }
  1460. s->mpeg_f_code[0][0] += !s->mpeg_f_code[0][0];
  1461. s->mpeg_f_code[0][1] += !s->mpeg_f_code[0][1];
  1462. s->mpeg_f_code[1][0] += !s->mpeg_f_code[1][0];
  1463. s->mpeg_f_code[1][1] += !s->mpeg_f_code[1][1];
  1464. s->intra_dc_precision = get_bits(&s->gb, 2);
  1465. s->picture_structure = get_bits(&s->gb, 2);
  1466. s->top_field_first = get_bits1(&s->gb);
  1467. s->frame_pred_frame_dct = get_bits1(&s->gb);
  1468. s->concealment_motion_vectors = get_bits1(&s->gb);
  1469. s->q_scale_type = get_bits1(&s->gb);
  1470. s->intra_vlc_format = get_bits1(&s->gb);
  1471. s->alternate_scan = get_bits1(&s->gb);
  1472. s->repeat_first_field = get_bits1(&s->gb);
  1473. s->chroma_420_type = get_bits1(&s->gb);
  1474. s->progressive_frame = get_bits1(&s->gb);
  1475. if (s->alternate_scan) {
  1476. ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_alternate_vertical_scan);
  1477. ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_alternate_vertical_scan);
  1478. } else {
  1479. ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_zigzag_direct);
  1480. ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_zigzag_direct);
  1481. }
  1482. /* composite display not parsed */
  1483. ff_dlog(s->avctx, "intra_dc_precision=%d\n", s->intra_dc_precision);
  1484. ff_dlog(s->avctx, "picture_structure=%d\n", s->picture_structure);
  1485. ff_dlog(s->avctx, "top field first=%d\n", s->top_field_first);
  1486. ff_dlog(s->avctx, "repeat first field=%d\n", s->repeat_first_field);
  1487. ff_dlog(s->avctx, "conceal=%d\n", s->concealment_motion_vectors);
  1488. ff_dlog(s->avctx, "intra_vlc_format=%d\n", s->intra_vlc_format);
  1489. ff_dlog(s->avctx, "alternate_scan=%d\n", s->alternate_scan);
  1490. ff_dlog(s->avctx, "frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
  1491. ff_dlog(s->avctx, "progressive_frame=%d\n", s->progressive_frame);
  1492. }
  1493. static int mpeg_field_start(MpegEncContext *s, const uint8_t *buf, int buf_size)
  1494. {
  1495. AVCodecContext *avctx = s->avctx;
  1496. Mpeg1Context *s1 = (Mpeg1Context *) s;
  1497. int ret;
  1498. /* start frame decoding */
  1499. if (s->first_field || s->picture_structure == PICT_FRAME) {
  1500. AVFrameSideData *pan_scan;
  1501. if ((ret = ff_mpv_frame_start(s, avctx)) < 0)
  1502. return ret;
  1503. ff_mpeg_er_frame_start(s);
  1504. /* first check if we must repeat the frame */
  1505. s->current_picture_ptr->f->repeat_pict = 0;
  1506. if (s->repeat_first_field) {
  1507. if (s->progressive_sequence) {
  1508. if (s->top_field_first)
  1509. s->current_picture_ptr->f->repeat_pict = 4;
  1510. else
  1511. s->current_picture_ptr->f->repeat_pict = 2;
  1512. } else if (s->progressive_frame) {
  1513. s->current_picture_ptr->f->repeat_pict = 1;
  1514. }
  1515. }
  1516. pan_scan = av_frame_new_side_data(s->current_picture_ptr->f,
  1517. AV_FRAME_DATA_PANSCAN,
  1518. sizeof(s1->pan_scan));
  1519. if (!pan_scan)
  1520. return AVERROR(ENOMEM);
  1521. memcpy(pan_scan->data, &s1->pan_scan, sizeof(s1->pan_scan));
  1522. if (s1->a53_caption) {
  1523. AVFrameSideData *sd = av_frame_new_side_data(
  1524. s->current_picture_ptr->f, AV_FRAME_DATA_A53_CC,
  1525. s1->a53_caption_size);
  1526. if (sd)
  1527. memcpy(sd->data, s1->a53_caption, s1->a53_caption_size);
  1528. av_freep(&s1->a53_caption);
  1529. avctx->properties |= FF_CODEC_PROPERTY_CLOSED_CAPTIONS;
  1530. }
  1531. if (s1->has_stereo3d) {
  1532. AVStereo3D *stereo = av_stereo3d_create_side_data(s->current_picture_ptr->f);
  1533. if (!stereo)
  1534. return AVERROR(ENOMEM);
  1535. *stereo = s1->stereo3d;
  1536. s1->has_stereo3d = 0;
  1537. }
  1538. if (s1->has_afd) {
  1539. AVFrameSideData *sd =
  1540. av_frame_new_side_data(s->current_picture_ptr->f,
  1541. AV_FRAME_DATA_AFD, 1);
  1542. if (!sd)
  1543. return AVERROR(ENOMEM);
  1544. *sd->data = s1->afd;
  1545. s1->has_afd = 0;
  1546. }
  1547. if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_FRAME))
  1548. ff_thread_finish_setup(avctx);
  1549. } else { // second field
  1550. int i;
  1551. if (!s->current_picture_ptr) {
  1552. av_log(s->avctx, AV_LOG_ERROR, "first field missing\n");
  1553. return AVERROR_INVALIDDATA;
  1554. }
  1555. if (s->avctx->hwaccel &&
  1556. (s->avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD)) {
  1557. if ((ret = s->avctx->hwaccel->end_frame(s->avctx)) < 0) {
  1558. av_log(avctx, AV_LOG_ERROR,
  1559. "hardware accelerator failed to decode first field\n");
  1560. return ret;
  1561. }
  1562. }
  1563. for (i = 0; i < 4; i++) {
  1564. s->current_picture.f->data[i] = s->current_picture_ptr->f->data[i];
  1565. if (s->picture_structure == PICT_BOTTOM_FIELD)
  1566. s->current_picture.f->data[i] +=
  1567. s->current_picture_ptr->f->linesize[i];
  1568. }
  1569. }
  1570. if (avctx->hwaccel) {
  1571. if ((ret = avctx->hwaccel->start_frame(avctx, buf, buf_size)) < 0)
  1572. return ret;
  1573. }
  1574. return 0;
  1575. }
  1576. #define DECODE_SLICE_ERROR -1
  1577. #define DECODE_SLICE_OK 0
  1578. /**
  1579. * Decode a slice.
  1580. * MpegEncContext.mb_y must be set to the MB row from the startcode.
  1581. * @return DECODE_SLICE_ERROR if the slice is damaged,
  1582. * DECODE_SLICE_OK if this slice is OK
  1583. */
  1584. static int mpeg_decode_slice(MpegEncContext *s, int mb_y,
  1585. const uint8_t **buf, int buf_size)
  1586. {
  1587. AVCodecContext *avctx = s->avctx;
  1588. const int lowres = s->avctx->lowres;
  1589. const int field_pic = s->picture_structure != PICT_FRAME;
  1590. int ret;
  1591. s->resync_mb_x =
  1592. s->resync_mb_y = -1;
  1593. av_assert0(mb_y < s->mb_height);
  1594. init_get_bits(&s->gb, *buf, buf_size * 8);
  1595. if (s->codec_id != AV_CODEC_ID_MPEG1VIDEO && s->mb_height > 2800/16)
  1596. skip_bits(&s->gb, 3);
  1597. ff_mpeg1_clean_buffers(s);
  1598. s->interlaced_dct = 0;
  1599. s->qscale = get_qscale(s);
  1600. if (s->qscale == 0) {
  1601. av_log(s->avctx, AV_LOG_ERROR, "qscale == 0\n");
  1602. return AVERROR_INVALIDDATA;
  1603. }
  1604. /* extra slice info */
  1605. if (skip_1stop_8data_bits(&s->gb) < 0)
  1606. return AVERROR_INVALIDDATA;
  1607. s->mb_x = 0;
  1608. if (mb_y == 0 && s->codec_tag == AV_RL32("SLIF")) {
  1609. skip_bits1(&s->gb);
  1610. } else {
  1611. while (get_bits_left(&s->gb) > 0) {
  1612. int code = get_vlc2(&s->gb, ff_mbincr_vlc.table,
  1613. MBINCR_VLC_BITS, 2);
  1614. if (code < 0) {
  1615. av_log(s->avctx, AV_LOG_ERROR, "first mb_incr damaged\n");
  1616. return AVERROR_INVALIDDATA;
  1617. }
  1618. if (code >= 33) {
  1619. if (code == 33)
  1620. s->mb_x += 33;
  1621. /* otherwise, stuffing, nothing to do */
  1622. } else {
  1623. s->mb_x += code;
  1624. break;
  1625. }
  1626. }
  1627. }
  1628. if (s->mb_x >= (unsigned) s->mb_width) {
  1629. av_log(s->avctx, AV_LOG_ERROR, "initial skip overflow\n");
  1630. return AVERROR_INVALIDDATA;
  1631. }
  1632. if (avctx->hwaccel && avctx->hwaccel->decode_slice) {
  1633. const uint8_t *buf_end, *buf_start = *buf - 4; /* include start_code */
  1634. int start_code = -1;
  1635. buf_end = avpriv_find_start_code(buf_start + 2, *buf + buf_size, &start_code);
  1636. if (buf_end < *buf + buf_size)
  1637. buf_end -= 4;
  1638. s->mb_y = mb_y;
  1639. if (avctx->hwaccel->decode_slice(avctx, buf_start, buf_end - buf_start) < 0)
  1640. return DECODE_SLICE_ERROR;
  1641. *buf = buf_end;
  1642. return DECODE_SLICE_OK;
  1643. }
  1644. s->resync_mb_x = s->mb_x;
  1645. s->resync_mb_y = s->mb_y = mb_y;
  1646. s->mb_skip_run = 0;
  1647. ff_init_block_index(s);
  1648. if (s->mb_y == 0 && s->mb_x == 0 && (s->first_field || s->picture_structure == PICT_FRAME)) {
  1649. if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
  1650. av_log(s->avctx, AV_LOG_DEBUG,
  1651. "qp:%d fc:%2d%2d%2d%2d %s %s %s %s %s dc:%d pstruct:%d fdct:%d cmv:%d qtype:%d ivlc:%d rff:%d %s\n",
  1652. s->qscale,
  1653. s->mpeg_f_code[0][0], s->mpeg_f_code[0][1],
  1654. s->mpeg_f_code[1][0], s->mpeg_f_code[1][1],
  1655. s->pict_type == AV_PICTURE_TYPE_I ? "I" :
  1656. (s->pict_type == AV_PICTURE_TYPE_P ? "P" :
  1657. (s->pict_type == AV_PICTURE_TYPE_B ? "B" : "S")),
  1658. s->progressive_sequence ? "ps" : "",
  1659. s->progressive_frame ? "pf" : "",
  1660. s->alternate_scan ? "alt" : "",
  1661. s->top_field_first ? "top" : "",
  1662. s->intra_dc_precision, s->picture_structure,
  1663. s->frame_pred_frame_dct, s->concealment_motion_vectors,
  1664. s->q_scale_type, s->intra_vlc_format,
  1665. s->repeat_first_field, s->chroma_420_type ? "420" : "");
  1666. }
  1667. }
  1668. for (;;) {
  1669. // If 1, we memcpy blocks in xvmcvideo.
  1670. if ((CONFIG_MPEG1_XVMC_HWACCEL || CONFIG_MPEG2_XVMC_HWACCEL) && s->pack_pblocks)
  1671. ff_xvmc_init_block(s); // set s->block
  1672. if ((ret = mpeg_decode_mb(s, s->block)) < 0)
  1673. return ret;
  1674. // Note motion_val is normally NULL unless we want to extract the MVs.
  1675. if (s->current_picture.motion_val[0] && !s->encoding) {
  1676. const int wrap = s->b8_stride;
  1677. int xy = s->mb_x * 2 + s->mb_y * 2 * wrap;
  1678. int b8_xy = 4 * (s->mb_x + s->mb_y * s->mb_stride);
  1679. int motion_x, motion_y, dir, i;
  1680. for (i = 0; i < 2; i++) {
  1681. for (dir = 0; dir < 2; dir++) {
  1682. if (s->mb_intra ||
  1683. (dir == 1 && s->pict_type != AV_PICTURE_TYPE_B)) {
  1684. motion_x = motion_y = 0;
  1685. } else if (s->mv_type == MV_TYPE_16X16 ||
  1686. (s->mv_type == MV_TYPE_FIELD && field_pic)) {
  1687. motion_x = s->mv[dir][0][0];
  1688. motion_y = s->mv[dir][0][1];
  1689. } else { /* if ((s->mv_type == MV_TYPE_FIELD) || (s->mv_type == MV_TYPE_16X8)) */
  1690. motion_x = s->mv[dir][i][0];
  1691. motion_y = s->mv[dir][i][1];
  1692. }
  1693. s->current_picture.motion_val[dir][xy][0] = motion_x;
  1694. s->current_picture.motion_val[dir][xy][1] = motion_y;
  1695. s->current_picture.motion_val[dir][xy + 1][0] = motion_x;
  1696. s->current_picture.motion_val[dir][xy + 1][1] = motion_y;
  1697. s->current_picture.ref_index [dir][b8_xy] =
  1698. s->current_picture.ref_index [dir][b8_xy + 1] = s->field_select[dir][i];
  1699. av_assert2(s->field_select[dir][i] == 0 ||
  1700. s->field_select[dir][i] == 1);
  1701. }
  1702. xy += wrap;
  1703. b8_xy += 2;
  1704. }
  1705. }
  1706. s->dest[0] += 16 >> lowres;
  1707. s->dest[1] +=(16 >> lowres) >> s->chroma_x_shift;
  1708. s->dest[2] +=(16 >> lowres) >> s->chroma_x_shift;
  1709. ff_mpv_decode_mb(s, s->block);
  1710. if (++s->mb_x >= s->mb_width) {
  1711. const int mb_size = 16 >> s->avctx->lowres;
  1712. ff_mpeg_draw_horiz_band(s, mb_size * (s->mb_y >> field_pic), mb_size);
  1713. ff_mpv_report_decode_progress(s);
  1714. s->mb_x = 0;
  1715. s->mb_y += 1 << field_pic;
  1716. if (s->mb_y >= s->mb_height) {
  1717. int left = get_bits_left(&s->gb);
  1718. int is_d10 = s->chroma_format == 2 &&
  1719. s->pict_type == AV_PICTURE_TYPE_I &&
  1720. avctx->profile == 0 && avctx->level == 5 &&
  1721. s->intra_dc_precision == 2 &&
  1722. s->q_scale_type == 1 && s->alternate_scan == 0 &&
  1723. s->progressive_frame == 0
  1724. /* vbv_delay == 0xBBB || 0xE10 */;
  1725. if (left >= 32 && !is_d10) {
  1726. GetBitContext gb = s->gb;
  1727. align_get_bits(&gb);
  1728. if (show_bits(&gb, 24) == 0x060E2B) {
  1729. av_log(avctx, AV_LOG_DEBUG, "Invalid MXF data found in video stream\n");
  1730. is_d10 = 1;
  1731. }
  1732. }
  1733. if (left < 0 ||
  1734. (left && show_bits(&s->gb, FFMIN(left, 23)) && !is_d10) ||
  1735. ((avctx->err_recognition & (AV_EF_BITSTREAM | AV_EF_AGGRESSIVE)) && left > 8)) {
  1736. av_log(avctx, AV_LOG_ERROR, "end mismatch left=%d %0X\n",
  1737. left, left>0 ? show_bits(&s->gb, FFMIN(left, 23)) : 0);
  1738. return AVERROR_INVALIDDATA;
  1739. } else
  1740. goto eos;
  1741. }
  1742. // There are some files out there which are missing the last slice
  1743. // in cases where the slice is completely outside the visible
  1744. // area, we detect this here instead of running into the end expecting
  1745. // more data
  1746. if (s->mb_y >= ((s->height + 15) >> 4) &&
  1747. !s->progressive_sequence &&
  1748. get_bits_left(&s->gb) <= 8 &&
  1749. get_bits_left(&s->gb) >= 0 &&
  1750. s->mb_skip_run == -1 &&
  1751. show_bits(&s->gb, 8) == 0)
  1752. goto eos;
  1753. ff_init_block_index(s);
  1754. }
  1755. /* skip mb handling */
  1756. if (s->mb_skip_run == -1) {
  1757. /* read increment again */
  1758. s->mb_skip_run = 0;
  1759. for (;;) {
  1760. int code = get_vlc2(&s->gb, ff_mbincr_vlc.table,
  1761. MBINCR_VLC_BITS, 2);
  1762. if (code < 0) {
  1763. av_log(s->avctx, AV_LOG_ERROR, "mb incr damaged\n");
  1764. return AVERROR_INVALIDDATA;
  1765. }
  1766. if (code >= 33) {
  1767. if (code == 33) {
  1768. s->mb_skip_run += 33;
  1769. } else if (code == 35) {
  1770. if (s->mb_skip_run != 0 || show_bits(&s->gb, 15) != 0) {
  1771. av_log(s->avctx, AV_LOG_ERROR, "slice mismatch\n");
  1772. return AVERROR_INVALIDDATA;
  1773. }
  1774. goto eos; /* end of slice */
  1775. }
  1776. /* otherwise, stuffing, nothing to do */
  1777. } else {
  1778. s->mb_skip_run += code;
  1779. break;
  1780. }
  1781. }
  1782. if (s->mb_skip_run) {
  1783. int i;
  1784. if (s->pict_type == AV_PICTURE_TYPE_I) {
  1785. av_log(s->avctx, AV_LOG_ERROR,
  1786. "skipped MB in I frame at %d %d\n", s->mb_x, s->mb_y);
  1787. return AVERROR_INVALIDDATA;
  1788. }
  1789. /* skip mb */
  1790. s->mb_intra = 0;
  1791. for (i = 0; i < 12; i++)
  1792. s->block_last_index[i] = -1;
  1793. if (s->picture_structure == PICT_FRAME)
  1794. s->mv_type = MV_TYPE_16X16;
  1795. else
  1796. s->mv_type = MV_TYPE_FIELD;
  1797. if (s->pict_type == AV_PICTURE_TYPE_P) {
  1798. /* if P type, zero motion vector is implied */
  1799. s->mv_dir = MV_DIR_FORWARD;
  1800. s->mv[0][0][0] = s->mv[0][0][1] = 0;
  1801. s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
  1802. s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
  1803. s->field_select[0][0] = (s->picture_structure - 1) & 1;
  1804. } else {
  1805. /* if B type, reuse previous vectors and directions */
  1806. s->mv[0][0][0] = s->last_mv[0][0][0];
  1807. s->mv[0][0][1] = s->last_mv[0][0][1];
  1808. s->mv[1][0][0] = s->last_mv[1][0][0];
  1809. s->mv[1][0][1] = s->last_mv[1][0][1];
  1810. }
  1811. }
  1812. }
  1813. }
  1814. eos: // end of slice
  1815. if (get_bits_left(&s->gb) < 0) {
  1816. av_log(s, AV_LOG_ERROR, "overread %d\n", -get_bits_left(&s->gb));
  1817. return AVERROR_INVALIDDATA;
  1818. }
  1819. *buf += (get_bits_count(&s->gb) - 1) / 8;
  1820. ff_dlog(s, "Slice start:%d %d end:%d %d\n", s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y);
  1821. return 0;
  1822. }
  1823. static int slice_decode_thread(AVCodecContext *c, void *arg)
  1824. {
  1825. MpegEncContext *s = *(void **) arg;
  1826. const uint8_t *buf = s->gb.buffer;
  1827. int mb_y = s->start_mb_y;
  1828. const int field_pic = s->picture_structure != PICT_FRAME;
  1829. s->er.error_count = (3 * (s->end_mb_y - s->start_mb_y) * s->mb_width) >> field_pic;
  1830. for (;;) {
  1831. uint32_t start_code;
  1832. int ret;
  1833. ret = mpeg_decode_slice(s, mb_y, &buf, s->gb.buffer_end - buf);
  1834. emms_c();
  1835. ff_dlog(c, "ret:%d resync:%d/%d mb:%d/%d ts:%d/%d ec:%d\n",
  1836. ret, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y,
  1837. s->start_mb_y, s->end_mb_y, s->er.error_count);
  1838. if (ret < 0) {
  1839. if (c->err_recognition & AV_EF_EXPLODE)
  1840. return ret;
  1841. if (s->resync_mb_x >= 0 && s->resync_mb_y >= 0)
  1842. ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
  1843. s->mb_x, s->mb_y,
  1844. ER_AC_ERROR | ER_DC_ERROR | ER_MV_ERROR);
  1845. } else {
  1846. ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
  1847. s->mb_x - 1, s->mb_y,
  1848. ER_AC_END | ER_DC_END | ER_MV_END);
  1849. }
  1850. if (s->mb_y == s->end_mb_y)
  1851. return 0;
  1852. start_code = -1;
  1853. buf = avpriv_find_start_code(buf, s->gb.buffer_end, &start_code);
  1854. mb_y = start_code - SLICE_MIN_START_CODE;
  1855. if (s->codec_id != AV_CODEC_ID_MPEG1VIDEO && s->mb_height > 2800/16)
  1856. mb_y += (*buf&0xE0)<<2;
  1857. mb_y <<= field_pic;
  1858. if (s->picture_structure == PICT_BOTTOM_FIELD)
  1859. mb_y++;
  1860. if (mb_y < 0 || mb_y >= s->end_mb_y)
  1861. return AVERROR_INVALIDDATA;
  1862. }
  1863. }
  1864. /**
  1865. * Handle slice ends.
  1866. * @return 1 if it seems to be the last slice
  1867. */
  1868. static int slice_end(AVCodecContext *avctx, AVFrame *pict)
  1869. {
  1870. Mpeg1Context *s1 = avctx->priv_data;
  1871. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1872. if (!s1->mpeg_enc_ctx_allocated || !s->current_picture_ptr)
  1873. return 0;
  1874. if (s->avctx->hwaccel) {
  1875. int ret = s->avctx->hwaccel->end_frame(s->avctx);
  1876. if (ret < 0) {
  1877. av_log(avctx, AV_LOG_ERROR,
  1878. "hardware accelerator failed to decode picture\n");
  1879. return ret;
  1880. }
  1881. }
  1882. /* end of slice reached */
  1883. if (/* s->mb_y << field_pic == s->mb_height && */ !s->first_field && !s1->first_slice) {
  1884. /* end of image */
  1885. ff_er_frame_end(&s->er);
  1886. ff_mpv_frame_end(s);
  1887. if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
  1888. int ret = av_frame_ref(pict, s->current_picture_ptr->f);
  1889. if (ret < 0)
  1890. return ret;
  1891. ff_print_debug_info(s, s->current_picture_ptr, pict);
  1892. ff_mpv_export_qp_table(s, pict, s->current_picture_ptr, FF_QSCALE_TYPE_MPEG2);
  1893. } else {
  1894. if (avctx->active_thread_type & FF_THREAD_FRAME)
  1895. s->picture_number++;
  1896. /* latency of 1 frame for I- and P-frames */
  1897. /* XXX: use another variable than picture_number */
  1898. if (s->last_picture_ptr) {
  1899. int ret = av_frame_ref(pict, s->last_picture_ptr->f);
  1900. if (ret < 0)
  1901. return ret;
  1902. ff_print_debug_info(s, s->last_picture_ptr, pict);
  1903. ff_mpv_export_qp_table(s, pict, s->last_picture_ptr, FF_QSCALE_TYPE_MPEG2);
  1904. }
  1905. }
  1906. return 1;
  1907. } else {
  1908. return 0;
  1909. }
  1910. }
  1911. static int mpeg1_decode_sequence(AVCodecContext *avctx,
  1912. const uint8_t *buf, int buf_size)
  1913. {
  1914. Mpeg1Context *s1 = avctx->priv_data;
  1915. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1916. int width, height;
  1917. int i, v, j;
  1918. init_get_bits(&s->gb, buf, buf_size * 8);
  1919. width = get_bits(&s->gb, 12);
  1920. height = get_bits(&s->gb, 12);
  1921. if (width == 0 || height == 0) {
  1922. av_log(avctx, AV_LOG_WARNING,
  1923. "Invalid horizontal or vertical size value.\n");
  1924. if (avctx->err_recognition & (AV_EF_BITSTREAM | AV_EF_COMPLIANT))
  1925. return AVERROR_INVALIDDATA;
  1926. }
  1927. s->aspect_ratio_info = get_bits(&s->gb, 4);
  1928. if (s->aspect_ratio_info == 0) {
  1929. av_log(avctx, AV_LOG_ERROR, "aspect ratio has forbidden 0 value\n");
  1930. if (avctx->err_recognition & (AV_EF_BITSTREAM | AV_EF_COMPLIANT))
  1931. return AVERROR_INVALIDDATA;
  1932. }
  1933. s->frame_rate_index = get_bits(&s->gb, 4);
  1934. if (s->frame_rate_index == 0 || s->frame_rate_index > 13) {
  1935. av_log(avctx, AV_LOG_WARNING,
  1936. "frame_rate_index %d is invalid\n", s->frame_rate_index);
  1937. s->frame_rate_index = 1;
  1938. }
  1939. s->bit_rate = get_bits(&s->gb, 18) * 400LL;
  1940. if (check_marker(&s->gb, "in sequence header") == 0) {
  1941. return AVERROR_INVALIDDATA;
  1942. }
  1943. s->avctx->rc_buffer_size = get_bits(&s->gb, 10) * 1024 * 16;
  1944. skip_bits(&s->gb, 1);
  1945. /* get matrix */
  1946. if (get_bits1(&s->gb)) {
  1947. load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
  1948. } else {
  1949. for (i = 0; i < 64; i++) {
  1950. j = s->idsp.idct_permutation[i];
  1951. v = ff_mpeg1_default_intra_matrix[i];
  1952. s->intra_matrix[j] = v;
  1953. s->chroma_intra_matrix[j] = v;
  1954. }
  1955. }
  1956. if (get_bits1(&s->gb)) {
  1957. load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
  1958. } else {
  1959. for (i = 0; i < 64; i++) {
  1960. int j = s->idsp.idct_permutation[i];
  1961. v = ff_mpeg1_default_non_intra_matrix[i];
  1962. s->inter_matrix[j] = v;
  1963. s->chroma_inter_matrix[j] = v;
  1964. }
  1965. }
  1966. if (show_bits(&s->gb, 23) != 0) {
  1967. av_log(s->avctx, AV_LOG_ERROR, "sequence header damaged\n");
  1968. return AVERROR_INVALIDDATA;
  1969. }
  1970. s->width = width;
  1971. s->height = height;
  1972. /* We set MPEG-2 parameters so that it emulates MPEG-1. */
  1973. s->progressive_sequence = 1;
  1974. s->progressive_frame = 1;
  1975. s->picture_structure = PICT_FRAME;
  1976. s->first_field = 0;
  1977. s->frame_pred_frame_dct = 1;
  1978. s->chroma_format = 1;
  1979. s->codec_id =
  1980. s->avctx->codec_id = AV_CODEC_ID_MPEG1VIDEO;
  1981. s->out_format = FMT_MPEG1;
  1982. s->swap_uv = 0; // AFAIK VCR2 does not have SEQ_HEADER
  1983. if (s->avctx->flags & AV_CODEC_FLAG_LOW_DELAY)
  1984. s->low_delay = 1;
  1985. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  1986. av_log(s->avctx, AV_LOG_DEBUG, "vbv buffer: %d, bitrate:%"PRId64", aspect_ratio_info: %d \n",
  1987. s->avctx->rc_buffer_size, s->bit_rate, s->aspect_ratio_info);
  1988. return 0;
  1989. }
  1990. static int vcr2_init_sequence(AVCodecContext *avctx)
  1991. {
  1992. Mpeg1Context *s1 = avctx->priv_data;
  1993. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1994. int i, v, ret;
  1995. /* start new MPEG-1 context decoding */
  1996. s->out_format = FMT_MPEG1;
  1997. if (s1->mpeg_enc_ctx_allocated) {
  1998. ff_mpv_common_end(s);
  1999. s1->mpeg_enc_ctx_allocated = 0;
  2000. }
  2001. s->width = avctx->coded_width;
  2002. s->height = avctx->coded_height;
  2003. avctx->has_b_frames = 0; // true?
  2004. s->low_delay = 1;
  2005. avctx->pix_fmt = mpeg_get_pixelformat(avctx);
  2006. setup_hwaccel_for_pixfmt(avctx);
  2007. ff_mpv_idct_init(s);
  2008. if ((ret = ff_mpv_common_init(s)) < 0)
  2009. return ret;
  2010. s1->mpeg_enc_ctx_allocated = 1;
  2011. for (i = 0; i < 64; i++) {
  2012. int j = s->idsp.idct_permutation[i];
  2013. v = ff_mpeg1_default_intra_matrix[i];
  2014. s->intra_matrix[j] = v;
  2015. s->chroma_intra_matrix[j] = v;
  2016. v = ff_mpeg1_default_non_intra_matrix[i];
  2017. s->inter_matrix[j] = v;
  2018. s->chroma_inter_matrix[j] = v;
  2019. }
  2020. s->progressive_sequence = 1;
  2021. s->progressive_frame = 1;
  2022. s->picture_structure = PICT_FRAME;
  2023. s->first_field = 0;
  2024. s->frame_pred_frame_dct = 1;
  2025. s->chroma_format = 1;
  2026. if (s->codec_tag == AV_RL32("BW10")) {
  2027. s->codec_id = s->avctx->codec_id = AV_CODEC_ID_MPEG1VIDEO;
  2028. } else {
  2029. s->swap_uv = 1; // in case of xvmc we need to swap uv for each MB
  2030. s->codec_id = s->avctx->codec_id = AV_CODEC_ID_MPEG2VIDEO;
  2031. }
  2032. s1->save_width = s->width;
  2033. s1->save_height = s->height;
  2034. s1->save_progressive_seq = s->progressive_sequence;
  2035. return 0;
  2036. }
  2037. static int mpeg_decode_a53_cc(AVCodecContext *avctx,
  2038. const uint8_t *p, int buf_size)
  2039. {
  2040. Mpeg1Context *s1 = avctx->priv_data;
  2041. if (buf_size >= 6 &&
  2042. p[0] == 'G' && p[1] == 'A' && p[2] == '9' && p[3] == '4' &&
  2043. p[4] == 3 && (p[5] & 0x40)) {
  2044. /* extract A53 Part 4 CC data */
  2045. int cc_count = p[5] & 0x1f;
  2046. if (cc_count > 0 && buf_size >= 7 + cc_count * 3) {
  2047. av_freep(&s1->a53_caption);
  2048. s1->a53_caption_size = cc_count * 3;
  2049. s1->a53_caption = av_malloc(s1->a53_caption_size);
  2050. if (s1->a53_caption)
  2051. memcpy(s1->a53_caption, p + 7, s1->a53_caption_size);
  2052. }
  2053. return 1;
  2054. } else if (buf_size >= 11 &&
  2055. p[0] == 'C' && p[1] == 'C' && p[2] == 0x01 && p[3] == 0xf8) {
  2056. /* extract DVD CC data */
  2057. int cc_count = 0;
  2058. int i;
  2059. // There is a caption count field in the data, but it is often
  2060. // incorect. So count the number of captions present.
  2061. for (i = 5; i + 6 <= buf_size && ((p[i] & 0xfe) == 0xfe); i += 6)
  2062. cc_count++;
  2063. // Transform the DVD format into A53 Part 4 format
  2064. if (cc_count > 0) {
  2065. av_freep(&s1->a53_caption);
  2066. s1->a53_caption_size = cc_count * 6;
  2067. s1->a53_caption = av_malloc(s1->a53_caption_size);
  2068. if (s1->a53_caption) {
  2069. uint8_t field1 = !!(p[4] & 0x80);
  2070. uint8_t *cap = s1->a53_caption;
  2071. p += 5;
  2072. for (i = 0; i < cc_count; i++) {
  2073. cap[0] = (p[0] == 0xff && field1) ? 0xfc : 0xfd;
  2074. cap[1] = p[1];
  2075. cap[2] = p[2];
  2076. cap[3] = (p[3] == 0xff && !field1) ? 0xfc : 0xfd;
  2077. cap[4] = p[4];
  2078. cap[5] = p[5];
  2079. cap += 6;
  2080. p += 6;
  2081. }
  2082. }
  2083. }
  2084. return 1;
  2085. }
  2086. return 0;
  2087. }
  2088. static void mpeg_decode_user_data(AVCodecContext *avctx,
  2089. const uint8_t *p, int buf_size)
  2090. {
  2091. Mpeg1Context *s = avctx->priv_data;
  2092. const uint8_t *buf_end = p + buf_size;
  2093. Mpeg1Context *s1 = avctx->priv_data;
  2094. #if 0
  2095. int i;
  2096. for(i=0; !(!p[i-2] && !p[i-1] && p[i]==1) && i<buf_size; i++){
  2097. av_log(avctx, AV_LOG_ERROR, "%c", p[i]);
  2098. }
  2099. av_log(avctx, AV_LOG_ERROR, "\n");
  2100. #endif
  2101. if (buf_size > 29){
  2102. int i;
  2103. for(i=0; i<20; i++)
  2104. if (!memcmp(p+i, "\0TMPGEXS\0", 9)){
  2105. s->tmpgexs= 1;
  2106. }
  2107. }
  2108. /* we parse the DTG active format information */
  2109. if (buf_end - p >= 5 &&
  2110. p[0] == 'D' && p[1] == 'T' && p[2] == 'G' && p[3] == '1') {
  2111. int flags = p[4];
  2112. p += 5;
  2113. if (flags & 0x80) {
  2114. /* skip event id */
  2115. p += 2;
  2116. }
  2117. if (flags & 0x40) {
  2118. if (buf_end - p < 1)
  2119. return;
  2120. #if FF_API_AFD
  2121. FF_DISABLE_DEPRECATION_WARNINGS
  2122. avctx->dtg_active_format = p[0] & 0x0f;
  2123. FF_ENABLE_DEPRECATION_WARNINGS
  2124. #endif /* FF_API_AFD */
  2125. s1->has_afd = 1;
  2126. s1->afd = p[0] & 0x0f;
  2127. }
  2128. } else if (buf_end - p >= 6 &&
  2129. p[0] == 'J' && p[1] == 'P' && p[2] == '3' && p[3] == 'D' &&
  2130. p[4] == 0x03) { // S3D_video_format_length
  2131. // the 0x7F mask ignores the reserved_bit value
  2132. const uint8_t S3D_video_format_type = p[5] & 0x7F;
  2133. if (S3D_video_format_type == 0x03 ||
  2134. S3D_video_format_type == 0x04 ||
  2135. S3D_video_format_type == 0x08 ||
  2136. S3D_video_format_type == 0x23) {
  2137. s1->has_stereo3d = 1;
  2138. switch (S3D_video_format_type) {
  2139. case 0x03:
  2140. s1->stereo3d.type = AV_STEREO3D_SIDEBYSIDE;
  2141. break;
  2142. case 0x04:
  2143. s1->stereo3d.type = AV_STEREO3D_TOPBOTTOM;
  2144. break;
  2145. case 0x08:
  2146. s1->stereo3d.type = AV_STEREO3D_2D;
  2147. break;
  2148. case 0x23:
  2149. s1->stereo3d.type = AV_STEREO3D_SIDEBYSIDE_QUINCUNX;
  2150. break;
  2151. }
  2152. }
  2153. } else if (mpeg_decode_a53_cc(avctx, p, buf_size)) {
  2154. return;
  2155. }
  2156. }
  2157. static void mpeg_decode_gop(AVCodecContext *avctx,
  2158. const uint8_t *buf, int buf_size)
  2159. {
  2160. Mpeg1Context *s1 = avctx->priv_data;
  2161. MpegEncContext *s = &s1->mpeg_enc_ctx;
  2162. int broken_link;
  2163. int64_t tc;
  2164. init_get_bits(&s->gb, buf, buf_size * 8);
  2165. tc = avctx->timecode_frame_start = get_bits(&s->gb, 25);
  2166. s->closed_gop = get_bits1(&s->gb);
  2167. /* broken_link indicate that after editing the
  2168. * reference frames of the first B-Frames after GOP I-Frame
  2169. * are missing (open gop) */
  2170. broken_link = get_bits1(&s->gb);
  2171. if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
  2172. char tcbuf[AV_TIMECODE_STR_SIZE];
  2173. av_timecode_make_mpeg_tc_string(tcbuf, tc);
  2174. av_log(s->avctx, AV_LOG_DEBUG,
  2175. "GOP (%s) closed_gop=%d broken_link=%d\n",
  2176. tcbuf, s->closed_gop, broken_link);
  2177. }
  2178. }
  2179. static int decode_chunks(AVCodecContext *avctx, AVFrame *picture,
  2180. int *got_output, const uint8_t *buf, int buf_size)
  2181. {
  2182. Mpeg1Context *s = avctx->priv_data;
  2183. MpegEncContext *s2 = &s->mpeg_enc_ctx;
  2184. const uint8_t *buf_ptr = buf;
  2185. const uint8_t *buf_end = buf + buf_size;
  2186. int ret, input_size;
  2187. int last_code = 0, skip_frame = 0;
  2188. int picture_start_code_seen = 0;
  2189. for (;;) {
  2190. /* find next start code */
  2191. uint32_t start_code = -1;
  2192. buf_ptr = avpriv_find_start_code(buf_ptr, buf_end, &start_code);
  2193. if (start_code > 0x1ff) {
  2194. if (!skip_frame) {
  2195. if (HAVE_THREADS &&
  2196. (avctx->active_thread_type & FF_THREAD_SLICE) &&
  2197. !avctx->hwaccel) {
  2198. int i;
  2199. av_assert0(avctx->thread_count > 1);
  2200. avctx->execute(avctx, slice_decode_thread,
  2201. &s2->thread_context[0], NULL,
  2202. s->slice_count, sizeof(void *));
  2203. for (i = 0; i < s->slice_count; i++)
  2204. s2->er.error_count += s2->thread_context[i]->er.error_count;
  2205. }
  2206. #if FF_API_VDPAU
  2207. if ((CONFIG_MPEG_VDPAU_DECODER || CONFIG_MPEG1_VDPAU_DECODER)
  2208. && uses_vdpau(avctx))
  2209. ff_vdpau_mpeg_picture_complete(s2, buf, buf_size, s->slice_count);
  2210. #endif
  2211. ret = slice_end(avctx, picture);
  2212. if (ret < 0)
  2213. return ret;
  2214. else if (ret) {
  2215. // FIXME: merge with the stuff in mpeg_decode_slice
  2216. if (s2->last_picture_ptr || s2->low_delay)
  2217. *got_output = 1;
  2218. }
  2219. }
  2220. s2->pict_type = 0;
  2221. if (avctx->err_recognition & AV_EF_EXPLODE && s2->er.error_count)
  2222. return AVERROR_INVALIDDATA;
  2223. return FFMAX(0, buf_ptr - buf - s2->parse_context.last_index);
  2224. }
  2225. input_size = buf_end - buf_ptr;
  2226. if (avctx->debug & FF_DEBUG_STARTCODE)
  2227. av_log(avctx, AV_LOG_DEBUG, "%3"PRIX32" at %"PTRDIFF_SPECIFIER" left %d\n",
  2228. start_code, buf_ptr - buf, input_size);
  2229. /* prepare data for next start code */
  2230. switch (start_code) {
  2231. case SEQ_START_CODE:
  2232. if (last_code == 0) {
  2233. mpeg1_decode_sequence(avctx, buf_ptr, input_size);
  2234. if (buf != avctx->extradata)
  2235. s->sync = 1;
  2236. } else {
  2237. av_log(avctx, AV_LOG_ERROR,
  2238. "ignoring SEQ_START_CODE after %X\n", last_code);
  2239. if (avctx->err_recognition & AV_EF_EXPLODE)
  2240. return AVERROR_INVALIDDATA;
  2241. }
  2242. break;
  2243. case PICTURE_START_CODE:
  2244. if (picture_start_code_seen && s2->picture_structure == PICT_FRAME) {
  2245. /* If it's a frame picture, there can't be more than one picture header.
  2246. Yet, it does happen and we need to handle it. */
  2247. av_log(avctx, AV_LOG_WARNING, "ignoring extra picture following a frame-picture\n");
  2248. break;
  2249. }
  2250. picture_start_code_seen = 1;
  2251. if (s2->width <= 0 || s2->height <= 0) {
  2252. av_log(avctx, AV_LOG_ERROR, "Invalid frame dimensions %dx%d.\n",
  2253. s2->width, s2->height);
  2254. return AVERROR_INVALIDDATA;
  2255. }
  2256. if (s->tmpgexs){
  2257. s2->intra_dc_precision= 3;
  2258. s2->intra_matrix[0]= 1;
  2259. }
  2260. if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE) &&
  2261. !avctx->hwaccel && s->slice_count) {
  2262. int i;
  2263. avctx->execute(avctx, slice_decode_thread,
  2264. s2->thread_context, NULL,
  2265. s->slice_count, sizeof(void *));
  2266. for (i = 0; i < s->slice_count; i++)
  2267. s2->er.error_count += s2->thread_context[i]->er.error_count;
  2268. s->slice_count = 0;
  2269. }
  2270. if (last_code == 0 || last_code == SLICE_MIN_START_CODE) {
  2271. ret = mpeg_decode_postinit(avctx);
  2272. if (ret < 0) {
  2273. av_log(avctx, AV_LOG_ERROR,
  2274. "mpeg_decode_postinit() failure\n");
  2275. return ret;
  2276. }
  2277. /* We have a complete image: we try to decompress it. */
  2278. if (mpeg1_decode_picture(avctx, buf_ptr, input_size) < 0)
  2279. s2->pict_type = 0;
  2280. s->first_slice = 1;
  2281. last_code = PICTURE_START_CODE;
  2282. } else {
  2283. av_log(avctx, AV_LOG_ERROR,
  2284. "ignoring pic after %X\n", last_code);
  2285. if (avctx->err_recognition & AV_EF_EXPLODE)
  2286. return AVERROR_INVALIDDATA;
  2287. }
  2288. break;
  2289. case EXT_START_CODE:
  2290. init_get_bits(&s2->gb, buf_ptr, input_size * 8);
  2291. switch (get_bits(&s2->gb, 4)) {
  2292. case 0x1:
  2293. if (last_code == 0) {
  2294. mpeg_decode_sequence_extension(s);
  2295. } else {
  2296. av_log(avctx, AV_LOG_ERROR,
  2297. "ignoring seq ext after %X\n", last_code);
  2298. if (avctx->err_recognition & AV_EF_EXPLODE)
  2299. return AVERROR_INVALIDDATA;
  2300. }
  2301. break;
  2302. case 0x2:
  2303. mpeg_decode_sequence_display_extension(s);
  2304. break;
  2305. case 0x3:
  2306. mpeg_decode_quant_matrix_extension(s2);
  2307. break;
  2308. case 0x7:
  2309. mpeg_decode_picture_display_extension(s);
  2310. break;
  2311. case 0x8:
  2312. if (last_code == PICTURE_START_CODE) {
  2313. mpeg_decode_picture_coding_extension(s);
  2314. } else {
  2315. av_log(avctx, AV_LOG_ERROR,
  2316. "ignoring pic cod ext after %X\n", last_code);
  2317. if (avctx->err_recognition & AV_EF_EXPLODE)
  2318. return AVERROR_INVALIDDATA;
  2319. }
  2320. break;
  2321. }
  2322. break;
  2323. case USER_START_CODE:
  2324. mpeg_decode_user_data(avctx, buf_ptr, input_size);
  2325. break;
  2326. case GOP_START_CODE:
  2327. if (last_code == 0) {
  2328. s2->first_field = 0;
  2329. mpeg_decode_gop(avctx, buf_ptr, input_size);
  2330. s->sync = 1;
  2331. } else {
  2332. av_log(avctx, AV_LOG_ERROR,
  2333. "ignoring GOP_START_CODE after %X\n", last_code);
  2334. if (avctx->err_recognition & AV_EF_EXPLODE)
  2335. return AVERROR_INVALIDDATA;
  2336. }
  2337. break;
  2338. default:
  2339. if (start_code >= SLICE_MIN_START_CODE &&
  2340. start_code <= SLICE_MAX_START_CODE && last_code == PICTURE_START_CODE) {
  2341. if (s2->progressive_sequence && !s2->progressive_frame) {
  2342. s2->progressive_frame = 1;
  2343. av_log(s2->avctx, AV_LOG_ERROR,
  2344. "interlaced frame in progressive sequence, ignoring\n");
  2345. }
  2346. if (s2->picture_structure == 0 ||
  2347. (s2->progressive_frame && s2->picture_structure != PICT_FRAME)) {
  2348. av_log(s2->avctx, AV_LOG_ERROR,
  2349. "picture_structure %d invalid, ignoring\n",
  2350. s2->picture_structure);
  2351. s2->picture_structure = PICT_FRAME;
  2352. }
  2353. if (s2->progressive_sequence && !s2->frame_pred_frame_dct)
  2354. av_log(s2->avctx, AV_LOG_WARNING, "invalid frame_pred_frame_dct\n");
  2355. if (s2->picture_structure == PICT_FRAME) {
  2356. s2->first_field = 0;
  2357. s2->v_edge_pos = 16 * s2->mb_height;
  2358. } else {
  2359. s2->first_field ^= 1;
  2360. s2->v_edge_pos = 8 * s2->mb_height;
  2361. memset(s2->mbskip_table, 0, s2->mb_stride * s2->mb_height);
  2362. }
  2363. }
  2364. if (start_code >= SLICE_MIN_START_CODE &&
  2365. start_code <= SLICE_MAX_START_CODE && last_code != 0) {
  2366. const int field_pic = s2->picture_structure != PICT_FRAME;
  2367. int mb_y = start_code - SLICE_MIN_START_CODE;
  2368. last_code = SLICE_MIN_START_CODE;
  2369. if (s2->codec_id != AV_CODEC_ID_MPEG1VIDEO && s2->mb_height > 2800/16)
  2370. mb_y += (*buf_ptr&0xE0)<<2;
  2371. mb_y <<= field_pic;
  2372. if (s2->picture_structure == PICT_BOTTOM_FIELD)
  2373. mb_y++;
  2374. if (buf_end - buf_ptr < 2) {
  2375. av_log(s2->avctx, AV_LOG_ERROR, "slice too small\n");
  2376. return AVERROR_INVALIDDATA;
  2377. }
  2378. if (mb_y >= s2->mb_height) {
  2379. av_log(s2->avctx, AV_LOG_ERROR,
  2380. "slice below image (%d >= %d)\n", mb_y, s2->mb_height);
  2381. return AVERROR_INVALIDDATA;
  2382. }
  2383. if (!s2->last_picture_ptr) {
  2384. /* Skip B-frames if we do not have reference frames and
  2385. * GOP is not closed. */
  2386. if (s2->pict_type == AV_PICTURE_TYPE_B) {
  2387. if (!s2->closed_gop) {
  2388. skip_frame = 1;
  2389. break;
  2390. }
  2391. }
  2392. }
  2393. if (s2->pict_type == AV_PICTURE_TYPE_I || (s2->avctx->flags2 & AV_CODEC_FLAG2_SHOW_ALL))
  2394. s->sync = 1;
  2395. if (!s2->next_picture_ptr) {
  2396. /* Skip P-frames if we do not have a reference frame or
  2397. * we have an invalid header. */
  2398. if (s2->pict_type == AV_PICTURE_TYPE_P && !s->sync) {
  2399. skip_frame = 1;
  2400. break;
  2401. }
  2402. }
  2403. if ((avctx->skip_frame >= AVDISCARD_NONREF &&
  2404. s2->pict_type == AV_PICTURE_TYPE_B) ||
  2405. (avctx->skip_frame >= AVDISCARD_NONKEY &&
  2406. s2->pict_type != AV_PICTURE_TYPE_I) ||
  2407. avctx->skip_frame >= AVDISCARD_ALL) {
  2408. skip_frame = 1;
  2409. break;
  2410. }
  2411. if (!s->mpeg_enc_ctx_allocated)
  2412. break;
  2413. if (s2->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
  2414. if (mb_y < avctx->skip_top ||
  2415. mb_y >= s2->mb_height - avctx->skip_bottom)
  2416. break;
  2417. }
  2418. if (!s2->pict_type) {
  2419. av_log(avctx, AV_LOG_ERROR, "Missing picture start code\n");
  2420. if (avctx->err_recognition & AV_EF_EXPLODE)
  2421. return AVERROR_INVALIDDATA;
  2422. break;
  2423. }
  2424. if (s->first_slice) {
  2425. skip_frame = 0;
  2426. s->first_slice = 0;
  2427. if ((ret = mpeg_field_start(s2, buf, buf_size)) < 0)
  2428. return ret;
  2429. }
  2430. if (!s2->current_picture_ptr) {
  2431. av_log(avctx, AV_LOG_ERROR,
  2432. "current_picture not initialized\n");
  2433. return AVERROR_INVALIDDATA;
  2434. }
  2435. #if FF_API_VDPAU
  2436. if (uses_vdpau(avctx)) {
  2437. s->slice_count++;
  2438. break;
  2439. }
  2440. #endif
  2441. if (HAVE_THREADS &&
  2442. (avctx->active_thread_type & FF_THREAD_SLICE) &&
  2443. !avctx->hwaccel) {
  2444. int threshold = (s2->mb_height * s->slice_count +
  2445. s2->slice_context_count / 2) /
  2446. s2->slice_context_count;
  2447. av_assert0(avctx->thread_count > 1);
  2448. if (threshold <= mb_y) {
  2449. MpegEncContext *thread_context = s2->thread_context[s->slice_count];
  2450. thread_context->start_mb_y = mb_y;
  2451. thread_context->end_mb_y = s2->mb_height;
  2452. if (s->slice_count) {
  2453. s2->thread_context[s->slice_count - 1]->end_mb_y = mb_y;
  2454. ret = ff_update_duplicate_context(thread_context, s2);
  2455. if (ret < 0)
  2456. return ret;
  2457. }
  2458. init_get_bits(&thread_context->gb, buf_ptr, input_size * 8);
  2459. s->slice_count++;
  2460. }
  2461. buf_ptr += 2; // FIXME add minimum number of bytes per slice
  2462. } else {
  2463. ret = mpeg_decode_slice(s2, mb_y, &buf_ptr, input_size);
  2464. emms_c();
  2465. if (ret < 0) {
  2466. if (avctx->err_recognition & AV_EF_EXPLODE)
  2467. return ret;
  2468. if (s2->resync_mb_x >= 0 && s2->resync_mb_y >= 0)
  2469. ff_er_add_slice(&s2->er, s2->resync_mb_x,
  2470. s2->resync_mb_y, s2->mb_x, s2->mb_y,
  2471. ER_AC_ERROR | ER_DC_ERROR | ER_MV_ERROR);
  2472. } else {
  2473. ff_er_add_slice(&s2->er, s2->resync_mb_x,
  2474. s2->resync_mb_y, s2->mb_x - 1, s2->mb_y,
  2475. ER_AC_END | ER_DC_END | ER_MV_END);
  2476. }
  2477. }
  2478. }
  2479. break;
  2480. }
  2481. }
  2482. }
  2483. static int mpeg_decode_frame(AVCodecContext *avctx, void *data,
  2484. int *got_output, AVPacket *avpkt)
  2485. {
  2486. const uint8_t *buf = avpkt->data;
  2487. int ret;
  2488. int buf_size = avpkt->size;
  2489. Mpeg1Context *s = avctx->priv_data;
  2490. AVFrame *picture = data;
  2491. MpegEncContext *s2 = &s->mpeg_enc_ctx;
  2492. if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == SEQ_END_CODE)) {
  2493. /* special case for last picture */
  2494. if (s2->low_delay == 0 && s2->next_picture_ptr) {
  2495. int ret = av_frame_ref(picture, s2->next_picture_ptr->f);
  2496. if (ret < 0)
  2497. return ret;
  2498. s2->next_picture_ptr = NULL;
  2499. *got_output = 1;
  2500. }
  2501. return buf_size;
  2502. }
  2503. if (s2->avctx->flags & AV_CODEC_FLAG_TRUNCATED) {
  2504. int next = ff_mpeg1_find_frame_end(&s2->parse_context, buf,
  2505. buf_size, NULL);
  2506. if (ff_combine_frame(&s2->parse_context, next,
  2507. (const uint8_t **) &buf, &buf_size) < 0)
  2508. return buf_size;
  2509. }
  2510. s2->codec_tag = avpriv_toupper4(avctx->codec_tag);
  2511. if (s->mpeg_enc_ctx_allocated == 0 && ( s2->codec_tag == AV_RL32("VCR2")
  2512. || s2->codec_tag == AV_RL32("BW10")
  2513. ))
  2514. vcr2_init_sequence(avctx);
  2515. s->slice_count = 0;
  2516. if (avctx->extradata && !s->extradata_decoded) {
  2517. ret = decode_chunks(avctx, picture, got_output,
  2518. avctx->extradata, avctx->extradata_size);
  2519. if (*got_output) {
  2520. av_log(avctx, AV_LOG_ERROR, "picture in extradata\n");
  2521. *got_output = 0;
  2522. }
  2523. s->extradata_decoded = 1;
  2524. if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE)) {
  2525. s2->current_picture_ptr = NULL;
  2526. return ret;
  2527. }
  2528. }
  2529. ret = decode_chunks(avctx, picture, got_output, buf, buf_size);
  2530. if (ret<0 || *got_output)
  2531. s2->current_picture_ptr = NULL;
  2532. return ret;
  2533. }
  2534. static void flush(AVCodecContext *avctx)
  2535. {
  2536. Mpeg1Context *s = avctx->priv_data;
  2537. s->sync = 0;
  2538. ff_mpeg_flush(avctx);
  2539. }
  2540. static av_cold int mpeg_decode_end(AVCodecContext *avctx)
  2541. {
  2542. Mpeg1Context *s = avctx->priv_data;
  2543. if (s->mpeg_enc_ctx_allocated)
  2544. ff_mpv_common_end(&s->mpeg_enc_ctx);
  2545. av_freep(&s->a53_caption);
  2546. return 0;
  2547. }
  2548. static const AVProfile mpeg2_video_profiles[] = {
  2549. { FF_PROFILE_MPEG2_422, "4:2:2" },
  2550. { FF_PROFILE_MPEG2_HIGH, "High" },
  2551. { FF_PROFILE_MPEG2_SS, "Spatially Scalable" },
  2552. { FF_PROFILE_MPEG2_SNR_SCALABLE, "SNR Scalable" },
  2553. { FF_PROFILE_MPEG2_MAIN, "Main" },
  2554. { FF_PROFILE_MPEG2_SIMPLE, "Simple" },
  2555. { FF_PROFILE_RESERVED, "Reserved" },
  2556. { FF_PROFILE_RESERVED, "Reserved" },
  2557. { FF_PROFILE_UNKNOWN },
  2558. };
  2559. AVCodec ff_mpeg1video_decoder = {
  2560. .name = "mpeg1video",
  2561. .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video"),
  2562. .type = AVMEDIA_TYPE_VIDEO,
  2563. .id = AV_CODEC_ID_MPEG1VIDEO,
  2564. .priv_data_size = sizeof(Mpeg1Context),
  2565. .init = mpeg_decode_init,
  2566. .close = mpeg_decode_end,
  2567. .decode = mpeg_decode_frame,
  2568. .capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1 |
  2569. AV_CODEC_CAP_TRUNCATED | AV_CODEC_CAP_DELAY |
  2570. AV_CODEC_CAP_SLICE_THREADS,
  2571. .flush = flush,
  2572. .max_lowres = 3,
  2573. .update_thread_context = ONLY_IF_THREADS_ENABLED(mpeg_decode_update_thread_context)
  2574. };
  2575. AVCodec ff_mpeg2video_decoder = {
  2576. .name = "mpeg2video",
  2577. .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 video"),
  2578. .type = AVMEDIA_TYPE_VIDEO,
  2579. .id = AV_CODEC_ID_MPEG2VIDEO,
  2580. .priv_data_size = sizeof(Mpeg1Context),
  2581. .init = mpeg_decode_init,
  2582. .close = mpeg_decode_end,
  2583. .decode = mpeg_decode_frame,
  2584. .capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1 |
  2585. AV_CODEC_CAP_TRUNCATED | AV_CODEC_CAP_DELAY |
  2586. AV_CODEC_CAP_SLICE_THREADS,
  2587. .flush = flush,
  2588. .max_lowres = 3,
  2589. .profiles = NULL_IF_CONFIG_SMALL(mpeg2_video_profiles),
  2590. };
  2591. //legacy decoder
  2592. AVCodec ff_mpegvideo_decoder = {
  2593. .name = "mpegvideo",
  2594. .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video"),
  2595. .type = AVMEDIA_TYPE_VIDEO,
  2596. .id = AV_CODEC_ID_MPEG2VIDEO,
  2597. .priv_data_size = sizeof(Mpeg1Context),
  2598. .init = mpeg_decode_init,
  2599. .close = mpeg_decode_end,
  2600. .decode = mpeg_decode_frame,
  2601. .capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1 | AV_CODEC_CAP_TRUNCATED | AV_CODEC_CAP_DELAY | AV_CODEC_CAP_SLICE_THREADS,
  2602. .flush = flush,
  2603. .max_lowres = 3,
  2604. };
  2605. #if FF_API_XVMC
  2606. #if CONFIG_MPEG_XVMC_DECODER
  2607. FF_DISABLE_DEPRECATION_WARNINGS
  2608. static av_cold int mpeg_mc_decode_init(AVCodecContext *avctx)
  2609. {
  2610. if (avctx->active_thread_type & FF_THREAD_SLICE)
  2611. return -1;
  2612. if (!(avctx->slice_flags & SLICE_FLAG_CODED_ORDER))
  2613. return -1;
  2614. if (!(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD)) {
  2615. ff_dlog(avctx, "mpeg12.c: XvMC decoder will work better if SLICE_FLAG_ALLOW_FIELD is set\n");
  2616. }
  2617. mpeg_decode_init(avctx);
  2618. avctx->pix_fmt = AV_PIX_FMT_XVMC_MPEG2_IDCT;
  2619. avctx->xvmc_acceleration = 2; // 2 - the blocks are packed!
  2620. return 0;
  2621. }
  2622. AVCodec ff_mpeg_xvmc_decoder = {
  2623. .name = "mpegvideo_xvmc",
  2624. .long_name = NULL_IF_CONFIG_SMALL("MPEG-1/2 video XvMC (X-Video Motion Compensation)"),
  2625. .type = AVMEDIA_TYPE_VIDEO,
  2626. .id = AV_CODEC_ID_MPEG2VIDEO_XVMC,
  2627. .priv_data_size = sizeof(Mpeg1Context),
  2628. .init = mpeg_mc_decode_init,
  2629. .close = mpeg_decode_end,
  2630. .decode = mpeg_decode_frame,
  2631. .capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1 |
  2632. AV_CODEC_CAP_TRUNCATED | CODEC_CAP_HWACCEL |
  2633. AV_CODEC_CAP_DELAY,
  2634. .flush = flush,
  2635. };
  2636. FF_ENABLE_DEPRECATION_WARNINGS
  2637. #endif
  2638. #endif /* FF_API_XVMC */
  2639. #if CONFIG_MPEG_VDPAU_DECODER && FF_API_VDPAU
  2640. AVCodec ff_mpeg_vdpau_decoder = {
  2641. .name = "mpegvideo_vdpau",
  2642. .long_name = NULL_IF_CONFIG_SMALL("MPEG-1/2 video (VDPAU acceleration)"),
  2643. .type = AVMEDIA_TYPE_VIDEO,
  2644. .id = AV_CODEC_ID_MPEG2VIDEO,
  2645. .priv_data_size = sizeof(Mpeg1Context),
  2646. .init = mpeg_decode_init,
  2647. .close = mpeg_decode_end,
  2648. .decode = mpeg_decode_frame,
  2649. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_TRUNCATED |
  2650. AV_CODEC_CAP_HWACCEL_VDPAU | AV_CODEC_CAP_DELAY,
  2651. .flush = flush,
  2652. };
  2653. #endif
  2654. #if CONFIG_MPEG1_VDPAU_DECODER && FF_API_VDPAU
  2655. AVCodec ff_mpeg1_vdpau_decoder = {
  2656. .name = "mpeg1video_vdpau",
  2657. .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video (VDPAU acceleration)"),
  2658. .type = AVMEDIA_TYPE_VIDEO,
  2659. .id = AV_CODEC_ID_MPEG1VIDEO,
  2660. .priv_data_size = sizeof(Mpeg1Context),
  2661. .init = mpeg_decode_init,
  2662. .close = mpeg_decode_end,
  2663. .decode = mpeg_decode_frame,
  2664. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_TRUNCATED |
  2665. AV_CODEC_CAP_HWACCEL_VDPAU | AV_CODEC_CAP_DELAY,
  2666. .flush = flush,
  2667. };
  2668. #endif