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.

2927 lines
105KB

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