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.

2880 lines
104KB

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