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.

2930 lines
105KB

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