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.

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