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.

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