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.

2916 lines
105KB

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