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.

2926 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. ff_mpv_decode_init(s2, avctx);
  1024. s->mpeg_enc_ctx.avctx = avctx;
  1025. /* we need some permutation to store matrices,
  1026. * until the decoder sets the real permutation. */
  1027. ff_mpv_idct_init(s2);
  1028. ff_mpeg12_common_init(&s->mpeg_enc_ctx);
  1029. ff_mpeg12_init_vlcs();
  1030. s->mpeg_enc_ctx_allocated = 0;
  1031. s->mpeg_enc_ctx.picture_number = 0;
  1032. s->repeat_field = 0;
  1033. s->mpeg_enc_ctx.codec_id = avctx->codec->id;
  1034. avctx->color_range = AVCOL_RANGE_MPEG;
  1035. if (avctx->codec->id == AV_CODEC_ID_MPEG1VIDEO)
  1036. avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
  1037. else
  1038. avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
  1039. return 0;
  1040. }
  1041. static int mpeg_decode_update_thread_context(AVCodecContext *avctx,
  1042. const AVCodecContext *avctx_from)
  1043. {
  1044. Mpeg1Context *ctx = avctx->priv_data, *ctx_from = avctx_from->priv_data;
  1045. MpegEncContext *s = &ctx->mpeg_enc_ctx, *s1 = &ctx_from->mpeg_enc_ctx;
  1046. int err;
  1047. if (avctx == avctx_from ||
  1048. !ctx_from->mpeg_enc_ctx_allocated ||
  1049. !s1->context_initialized)
  1050. return 0;
  1051. err = ff_mpeg_update_thread_context(avctx, avctx_from);
  1052. if (err)
  1053. return err;
  1054. if (!ctx->mpeg_enc_ctx_allocated)
  1055. memcpy(s + 1, s1 + 1, sizeof(Mpeg1Context) - sizeof(MpegEncContext));
  1056. if (!(s->pict_type == AV_PICTURE_TYPE_B || s->low_delay))
  1057. s->picture_number++;
  1058. return 0;
  1059. }
  1060. static void quant_matrix_rebuild(uint16_t *matrix, const uint8_t *old_perm,
  1061. const uint8_t *new_perm)
  1062. {
  1063. uint16_t temp_matrix[64];
  1064. int i;
  1065. memcpy(temp_matrix, matrix, 64 * sizeof(uint16_t));
  1066. for (i = 0; i < 64; i++)
  1067. matrix[new_perm[i]] = temp_matrix[old_perm[i]];
  1068. }
  1069. static const enum AVPixelFormat mpeg1_hwaccel_pixfmt_list_420[] = {
  1070. #if CONFIG_MPEG1_XVMC_HWACCEL
  1071. AV_PIX_FMT_XVMC,
  1072. #endif
  1073. #if CONFIG_MPEG1_VDPAU_HWACCEL
  1074. AV_PIX_FMT_VDPAU_MPEG1,
  1075. AV_PIX_FMT_VDPAU,
  1076. #endif
  1077. AV_PIX_FMT_YUV420P,
  1078. AV_PIX_FMT_NONE
  1079. };
  1080. static const enum AVPixelFormat mpeg2_hwaccel_pixfmt_list_420[] = {
  1081. #if CONFIG_MPEG2_XVMC_HWACCEL
  1082. AV_PIX_FMT_XVMC,
  1083. #endif
  1084. #if CONFIG_MPEG2_VDPAU_HWACCEL
  1085. AV_PIX_FMT_VDPAU_MPEG2,
  1086. AV_PIX_FMT_VDPAU,
  1087. #endif
  1088. #if CONFIG_MPEG2_DXVA2_HWACCEL
  1089. AV_PIX_FMT_DXVA2_VLD,
  1090. #endif
  1091. #if CONFIG_MPEG2_VAAPI_HWACCEL
  1092. AV_PIX_FMT_VAAPI_VLD,
  1093. #endif
  1094. AV_PIX_FMT_YUV420P,
  1095. AV_PIX_FMT_NONE
  1096. };
  1097. static const enum AVPixelFormat mpeg12_pixfmt_list_422[] = {
  1098. AV_PIX_FMT_YUV422P,
  1099. AV_PIX_FMT_NONE
  1100. };
  1101. static const enum AVPixelFormat mpeg12_pixfmt_list_444[] = {
  1102. AV_PIX_FMT_YUV444P,
  1103. AV_PIX_FMT_NONE
  1104. };
  1105. static inline int uses_vdpau(AVCodecContext *avctx) {
  1106. return avctx->pix_fmt == AV_PIX_FMT_VDPAU_MPEG1 || avctx->pix_fmt == AV_PIX_FMT_VDPAU_MPEG2;
  1107. }
  1108. static enum AVPixelFormat mpeg_get_pixelformat(AVCodecContext *avctx)
  1109. {
  1110. Mpeg1Context *s1 = avctx->priv_data;
  1111. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1112. const enum AVPixelFormat *pix_fmts;
  1113. if (s->chroma_format < 2)
  1114. pix_fmts = avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO ?
  1115. mpeg1_hwaccel_pixfmt_list_420 :
  1116. mpeg2_hwaccel_pixfmt_list_420;
  1117. else if (s->chroma_format == 2)
  1118. pix_fmts = mpeg12_pixfmt_list_422;
  1119. else
  1120. pix_fmts = mpeg12_pixfmt_list_444;
  1121. return ff_thread_get_format(avctx, pix_fmts);
  1122. }
  1123. static void setup_hwaccel_for_pixfmt(AVCodecContext *avctx)
  1124. {
  1125. // until then pix_fmt may be changed right after codec init
  1126. if (avctx->hwaccel || uses_vdpau(avctx))
  1127. if (avctx->idct_algo == FF_IDCT_AUTO)
  1128. avctx->idct_algo = FF_IDCT_SIMPLE;
  1129. if (avctx->hwaccel && avctx->pix_fmt == AV_PIX_FMT_XVMC) {
  1130. Mpeg1Context *s1 = avctx->priv_data;
  1131. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1132. s->pack_pblocks = 1;
  1133. #if FF_API_XVMC
  1134. avctx->xvmc_acceleration = 2;
  1135. #endif /* FF_API_XVMC */
  1136. }
  1137. }
  1138. /* Call this function when we know all parameters.
  1139. * It may be called in different places for MPEG-1 and MPEG-2. */
  1140. static int mpeg_decode_postinit(AVCodecContext *avctx)
  1141. {
  1142. Mpeg1Context *s1 = avctx->priv_data;
  1143. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1144. uint8_t old_permutation[64];
  1145. int ret;
  1146. if ((s1->mpeg_enc_ctx_allocated == 0) ||
  1147. avctx->coded_width != s->width ||
  1148. avctx->coded_height != s->height ||
  1149. s1->save_width != s->width ||
  1150. s1->save_height != s->height ||
  1151. s1->save_aspect_info != s->aspect_ratio_info ||
  1152. (s1->save_progressive_seq != s->progressive_sequence && FFALIGN(s->height, 16) != FFALIGN(s->height, 32)) ||
  1153. 0) {
  1154. if (s1->mpeg_enc_ctx_allocated) {
  1155. ParseContext pc = s->parse_context;
  1156. s->parse_context.buffer = 0;
  1157. ff_mpv_common_end(s);
  1158. s->parse_context = pc;
  1159. s1->mpeg_enc_ctx_allocated = 0;
  1160. }
  1161. if ((s->width == 0) || (s->height == 0))
  1162. return -2;
  1163. ret = ff_set_dimensions(avctx, s->width, s->height);
  1164. if (ret < 0)
  1165. return ret;
  1166. if (avctx->codec_id == AV_CODEC_ID_MPEG2VIDEO && s->bit_rate) {
  1167. avctx->rc_max_rate = s->bit_rate;
  1168. } else if (avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO && s->bit_rate &&
  1169. (s->bit_rate != 0x3FFFF*400 || s->vbv_delay != 0xFFFF)) {
  1170. avctx->bit_rate = s->bit_rate;
  1171. }
  1172. s1->save_aspect_info = s->aspect_ratio_info;
  1173. s1->save_width = s->width;
  1174. s1->save_height = s->height;
  1175. s1->save_progressive_seq = s->progressive_sequence;
  1176. /* low_delay may be forced, in this case we will have B-frames
  1177. * that behave like P-frames. */
  1178. avctx->has_b_frames = !s->low_delay;
  1179. if (avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
  1180. // MPEG-1 fps
  1181. avctx->framerate = ff_mpeg12_frame_rate_tab[s->frame_rate_index];
  1182. // MPEG-1 aspect
  1183. avctx->sample_aspect_ratio = av_d2q(1.0 / ff_mpeg1_aspect[s->aspect_ratio_info], 255);
  1184. avctx->ticks_per_frame = 1;
  1185. } else { // MPEG-2
  1186. // MPEG-2 fps
  1187. av_reduce(&s->avctx->framerate.num,
  1188. &s->avctx->framerate.den,
  1189. ff_mpeg12_frame_rate_tab[s->frame_rate_index].num * s1->frame_rate_ext.num,
  1190. ff_mpeg12_frame_rate_tab[s->frame_rate_index].den * s1->frame_rate_ext.den,
  1191. 1 << 30);
  1192. avctx->ticks_per_frame = 2;
  1193. // MPEG-2 aspect
  1194. if (s->aspect_ratio_info > 1) {
  1195. AVRational dar =
  1196. av_mul_q(av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
  1197. (AVRational) { s1->pan_scan.width,
  1198. s1->pan_scan.height }),
  1199. (AVRational) { s->width, s->height });
  1200. /* We ignore the spec here and guess a bit as reality does not
  1201. * match the spec, see for example res_change_ffmpeg_aspect.ts
  1202. * and sequence-display-aspect.mpg.
  1203. * issue1613, 621, 562 */
  1204. if ((s1->pan_scan.width == 0) || (s1->pan_scan.height == 0) ||
  1205. (av_cmp_q(dar, (AVRational) { 4, 3 }) &&
  1206. av_cmp_q(dar, (AVRational) { 16, 9 }))) {
  1207. s->avctx->sample_aspect_ratio =
  1208. av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
  1209. (AVRational) { s->width, s->height });
  1210. } else {
  1211. s->avctx->sample_aspect_ratio =
  1212. av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
  1213. (AVRational) { s1->pan_scan.width, s1->pan_scan.height });
  1214. // issue1613 4/3 16/9 -> 16/9
  1215. // res_change_ffmpeg_aspect.ts 4/3 225/44 ->4/3
  1216. // widescreen-issue562.mpg 4/3 16/9 -> 16/9
  1217. // s->avctx->sample_aspect_ratio = av_mul_q(s->avctx->sample_aspect_ratio, (AVRational) {s->width, s->height});
  1218. av_dlog(avctx, "A %d/%d\n",
  1219. ff_mpeg2_aspect[s->aspect_ratio_info].num,
  1220. ff_mpeg2_aspect[s->aspect_ratio_info].den);
  1221. av_dlog(avctx, "B %d/%d\n", s->avctx->sample_aspect_ratio.num,
  1222. s->avctx->sample_aspect_ratio.den);
  1223. }
  1224. } else {
  1225. s->avctx->sample_aspect_ratio =
  1226. ff_mpeg2_aspect[s->aspect_ratio_info];
  1227. }
  1228. } // MPEG-2
  1229. ff_set_sar(s->avctx, s->avctx->sample_aspect_ratio);
  1230. avctx->pix_fmt = mpeg_get_pixelformat(avctx);
  1231. setup_hwaccel_for_pixfmt(avctx);
  1232. /* Quantization matrices may need reordering
  1233. * if DCT permutation is changed. */
  1234. memcpy(old_permutation, s->idsp.idct_permutation, 64 * sizeof(uint8_t));
  1235. ff_mpv_idct_init(s);
  1236. if (ff_mpv_common_init(s) < 0)
  1237. return -2;
  1238. quant_matrix_rebuild(s->intra_matrix, old_permutation, s->idsp.idct_permutation);
  1239. quant_matrix_rebuild(s->inter_matrix, old_permutation, s->idsp.idct_permutation);
  1240. quant_matrix_rebuild(s->chroma_intra_matrix, old_permutation, s->idsp.idct_permutation);
  1241. quant_matrix_rebuild(s->chroma_inter_matrix, old_permutation, s->idsp.idct_permutation);
  1242. s1->mpeg_enc_ctx_allocated = 1;
  1243. }
  1244. return 0;
  1245. }
  1246. static int mpeg1_decode_picture(AVCodecContext *avctx, const uint8_t *buf,
  1247. int buf_size)
  1248. {
  1249. Mpeg1Context *s1 = avctx->priv_data;
  1250. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1251. int ref, f_code, vbv_delay;
  1252. init_get_bits(&s->gb, buf, buf_size * 8);
  1253. ref = get_bits(&s->gb, 10); /* temporal ref */
  1254. s->pict_type = get_bits(&s->gb, 3);
  1255. if (s->pict_type == 0 || s->pict_type > 3)
  1256. return -1;
  1257. vbv_delay = get_bits(&s->gb, 16);
  1258. s->vbv_delay = vbv_delay;
  1259. if (s->pict_type == AV_PICTURE_TYPE_P ||
  1260. s->pict_type == AV_PICTURE_TYPE_B) {
  1261. s->full_pel[0] = get_bits1(&s->gb);
  1262. f_code = get_bits(&s->gb, 3);
  1263. if (f_code == 0 && (avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT)))
  1264. return -1;
  1265. f_code += !f_code;
  1266. s->mpeg_f_code[0][0] = f_code;
  1267. s->mpeg_f_code[0][1] = f_code;
  1268. }
  1269. if (s->pict_type == AV_PICTURE_TYPE_B) {
  1270. s->full_pel[1] = get_bits1(&s->gb);
  1271. f_code = get_bits(&s->gb, 3);
  1272. if (f_code == 0 && (avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT)))
  1273. return -1;
  1274. f_code += !f_code;
  1275. s->mpeg_f_code[1][0] = f_code;
  1276. s->mpeg_f_code[1][1] = f_code;
  1277. }
  1278. s->current_picture.f->pict_type = s->pict_type;
  1279. s->current_picture.f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
  1280. if (avctx->debug & FF_DEBUG_PICT_INFO)
  1281. av_log(avctx, AV_LOG_DEBUG,
  1282. "vbv_delay %d, ref %d type:%d\n", vbv_delay, ref, s->pict_type);
  1283. s->y_dc_scale = 8;
  1284. s->c_dc_scale = 8;
  1285. return 0;
  1286. }
  1287. static void mpeg_decode_sequence_extension(Mpeg1Context *s1)
  1288. {
  1289. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1290. int horiz_size_ext, vert_size_ext;
  1291. int bit_rate_ext;
  1292. skip_bits(&s->gb, 1); /* profile and level esc*/
  1293. s->avctx->profile = get_bits(&s->gb, 3);
  1294. s->avctx->level = get_bits(&s->gb, 4);
  1295. s->progressive_sequence = get_bits1(&s->gb); /* progressive_sequence */
  1296. s->chroma_format = get_bits(&s->gb, 2); /* chroma_format 1=420, 2=422, 3=444 */
  1297. horiz_size_ext = get_bits(&s->gb, 2);
  1298. vert_size_ext = get_bits(&s->gb, 2);
  1299. s->width |= (horiz_size_ext << 12);
  1300. s->height |= (vert_size_ext << 12);
  1301. bit_rate_ext = get_bits(&s->gb, 12); /* XXX: handle it */
  1302. s->bit_rate += (bit_rate_ext << 18) * 400;
  1303. skip_bits1(&s->gb); /* marker */
  1304. s->avctx->rc_buffer_size += get_bits(&s->gb, 8) * 1024 * 16 << 10;
  1305. s->low_delay = get_bits1(&s->gb);
  1306. if (s->flags & CODEC_FLAG_LOW_DELAY)
  1307. s->low_delay = 1;
  1308. s1->frame_rate_ext.num = get_bits(&s->gb, 2) + 1;
  1309. s1->frame_rate_ext.den = get_bits(&s->gb, 5) + 1;
  1310. av_dlog(s->avctx, "sequence extension\n");
  1311. s->codec_id = s->avctx->codec_id = AV_CODEC_ID_MPEG2VIDEO;
  1312. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  1313. av_log(s->avctx, AV_LOG_DEBUG,
  1314. "profile: %d, level: %d ps: %d cf:%d vbv buffer: %d, bitrate:%d\n",
  1315. s->avctx->profile, s->avctx->level, s->progressive_sequence, s->chroma_format,
  1316. s->avctx->rc_buffer_size, s->bit_rate);
  1317. }
  1318. static void mpeg_decode_sequence_display_extension(Mpeg1Context *s1)
  1319. {
  1320. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1321. int color_description, w, h;
  1322. skip_bits(&s->gb, 3); /* video format */
  1323. color_description = get_bits1(&s->gb);
  1324. if (color_description) {
  1325. s->avctx->color_primaries = get_bits(&s->gb, 8);
  1326. s->avctx->color_trc = get_bits(&s->gb, 8);
  1327. s->avctx->colorspace = get_bits(&s->gb, 8);
  1328. }
  1329. w = get_bits(&s->gb, 14);
  1330. skip_bits(&s->gb, 1); // marker
  1331. h = get_bits(&s->gb, 14);
  1332. // remaining 3 bits are zero padding
  1333. s1->pan_scan.width = 16 * w;
  1334. s1->pan_scan.height = 16 * h;
  1335. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  1336. av_log(s->avctx, AV_LOG_DEBUG, "sde w:%d, h:%d\n", w, h);
  1337. }
  1338. static void mpeg_decode_picture_display_extension(Mpeg1Context *s1)
  1339. {
  1340. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1341. int i, nofco;
  1342. nofco = 1;
  1343. if (s->progressive_sequence) {
  1344. if (s->repeat_first_field) {
  1345. nofco++;
  1346. if (s->top_field_first)
  1347. nofco++;
  1348. }
  1349. } else {
  1350. if (s->picture_structure == PICT_FRAME) {
  1351. nofco++;
  1352. if (s->repeat_first_field)
  1353. nofco++;
  1354. }
  1355. }
  1356. for (i = 0; i < nofco; i++) {
  1357. s1->pan_scan.position[i][0] = get_sbits(&s->gb, 16);
  1358. skip_bits(&s->gb, 1); // marker
  1359. s1->pan_scan.position[i][1] = get_sbits(&s->gb, 16);
  1360. skip_bits(&s->gb, 1); // marker
  1361. }
  1362. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  1363. av_log(s->avctx, AV_LOG_DEBUG,
  1364. "pde (%"PRId16",%"PRId16") (%"PRId16",%"PRId16") (%"PRId16",%"PRId16")\n",
  1365. s1->pan_scan.position[0][0], s1->pan_scan.position[0][1],
  1366. s1->pan_scan.position[1][0], s1->pan_scan.position[1][1],
  1367. s1->pan_scan.position[2][0], s1->pan_scan.position[2][1]);
  1368. }
  1369. static int load_matrix(MpegEncContext *s, uint16_t matrix0[64],
  1370. uint16_t matrix1[64], int intra)
  1371. {
  1372. int i;
  1373. for (i = 0; i < 64; i++) {
  1374. int j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
  1375. int v = get_bits(&s->gb, 8);
  1376. if (v == 0) {
  1377. av_log(s->avctx, AV_LOG_ERROR, "matrix damaged\n");
  1378. return -1;
  1379. }
  1380. if (intra && i == 0 && v != 8) {
  1381. av_log(s->avctx, AV_LOG_DEBUG, "intra matrix specifies invalid DC quantizer %d, ignoring\n", v);
  1382. v = 8; // needed by pink.mpg / issue1046
  1383. }
  1384. matrix0[j] = v;
  1385. if (matrix1)
  1386. matrix1[j] = v;
  1387. }
  1388. return 0;
  1389. }
  1390. static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
  1391. {
  1392. av_dlog(s->avctx, "matrix extension\n");
  1393. if (get_bits1(&s->gb))
  1394. load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
  1395. if (get_bits1(&s->gb))
  1396. load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
  1397. if (get_bits1(&s->gb))
  1398. load_matrix(s, s->chroma_intra_matrix, NULL, 1);
  1399. if (get_bits1(&s->gb))
  1400. load_matrix(s, s->chroma_inter_matrix, NULL, 0);
  1401. }
  1402. static void mpeg_decode_picture_coding_extension(Mpeg1Context *s1)
  1403. {
  1404. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1405. s->full_pel[0] = s->full_pel[1] = 0;
  1406. s->mpeg_f_code[0][0] = get_bits(&s->gb, 4);
  1407. s->mpeg_f_code[0][1] = get_bits(&s->gb, 4);
  1408. s->mpeg_f_code[1][0] = get_bits(&s->gb, 4);
  1409. s->mpeg_f_code[1][1] = get_bits(&s->gb, 4);
  1410. if (!s->pict_type && s1->mpeg_enc_ctx_allocated) {
  1411. av_log(s->avctx, AV_LOG_ERROR,
  1412. "Missing picture start code, guessing missing values\n");
  1413. if (s->mpeg_f_code[1][0] == 15 && s->mpeg_f_code[1][1] == 15) {
  1414. if (s->mpeg_f_code[0][0] == 15 && s->mpeg_f_code[0][1] == 15)
  1415. s->pict_type = AV_PICTURE_TYPE_I;
  1416. else
  1417. s->pict_type = AV_PICTURE_TYPE_P;
  1418. } else
  1419. s->pict_type = AV_PICTURE_TYPE_B;
  1420. s->current_picture.f->pict_type = s->pict_type;
  1421. s->current_picture.f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
  1422. }
  1423. s->mpeg_f_code[0][0] += !s->mpeg_f_code[0][0];
  1424. s->mpeg_f_code[0][1] += !s->mpeg_f_code[0][1];
  1425. s->mpeg_f_code[1][0] += !s->mpeg_f_code[1][0];
  1426. s->mpeg_f_code[1][1] += !s->mpeg_f_code[1][1];
  1427. s->intra_dc_precision = get_bits(&s->gb, 2);
  1428. s->picture_structure = get_bits(&s->gb, 2);
  1429. s->top_field_first = get_bits1(&s->gb);
  1430. s->frame_pred_frame_dct = get_bits1(&s->gb);
  1431. s->concealment_motion_vectors = get_bits1(&s->gb);
  1432. s->q_scale_type = get_bits1(&s->gb);
  1433. s->intra_vlc_format = get_bits1(&s->gb);
  1434. s->alternate_scan = get_bits1(&s->gb);
  1435. s->repeat_first_field = get_bits1(&s->gb);
  1436. s->chroma_420_type = get_bits1(&s->gb);
  1437. s->progressive_frame = get_bits1(&s->gb);
  1438. if (s->alternate_scan) {
  1439. ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_alternate_vertical_scan);
  1440. ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_alternate_vertical_scan);
  1441. } else {
  1442. ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_zigzag_direct);
  1443. ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_zigzag_direct);
  1444. }
  1445. /* composite display not parsed */
  1446. av_dlog(s->avctx, "intra_dc_precision=%d\n", s->intra_dc_precision);
  1447. av_dlog(s->avctx, "picture_structure=%d\n", s->picture_structure);
  1448. av_dlog(s->avctx, "top field first=%d\n", s->top_field_first);
  1449. av_dlog(s->avctx, "repeat first field=%d\n", s->repeat_first_field);
  1450. av_dlog(s->avctx, "conceal=%d\n", s->concealment_motion_vectors);
  1451. av_dlog(s->avctx, "intra_vlc_format=%d\n", s->intra_vlc_format);
  1452. av_dlog(s->avctx, "alternate_scan=%d\n", s->alternate_scan);
  1453. av_dlog(s->avctx, "frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
  1454. av_dlog(s->avctx, "progressive_frame=%d\n", s->progressive_frame);
  1455. }
  1456. static int mpeg_field_start(MpegEncContext *s, const uint8_t *buf, int buf_size)
  1457. {
  1458. AVCodecContext *avctx = s->avctx;
  1459. Mpeg1Context *s1 = (Mpeg1Context *) s;
  1460. /* start frame decoding */
  1461. if (s->first_field || s->picture_structure == PICT_FRAME) {
  1462. AVFrameSideData *pan_scan;
  1463. if (ff_mpv_frame_start(s, avctx) < 0)
  1464. return -1;
  1465. ff_mpeg_er_frame_start(s);
  1466. /* first check if we must repeat the frame */
  1467. s->current_picture_ptr->f->repeat_pict = 0;
  1468. if (s->repeat_first_field) {
  1469. if (s->progressive_sequence) {
  1470. if (s->top_field_first)
  1471. s->current_picture_ptr->f->repeat_pict = 4;
  1472. else
  1473. s->current_picture_ptr->f->repeat_pict = 2;
  1474. } else if (s->progressive_frame) {
  1475. s->current_picture_ptr->f->repeat_pict = 1;
  1476. }
  1477. }
  1478. pan_scan = av_frame_new_side_data(s->current_picture_ptr->f,
  1479. AV_FRAME_DATA_PANSCAN,
  1480. sizeof(s1->pan_scan));
  1481. if (!pan_scan)
  1482. return AVERROR(ENOMEM);
  1483. memcpy(pan_scan->data, &s1->pan_scan, sizeof(s1->pan_scan));
  1484. if (s1->a53_caption) {
  1485. AVFrameSideData *sd = av_frame_new_side_data(
  1486. s->current_picture_ptr->f, AV_FRAME_DATA_A53_CC,
  1487. s1->a53_caption_size);
  1488. if (sd)
  1489. memcpy(sd->data, s1->a53_caption, s1->a53_caption_size);
  1490. av_freep(&s1->a53_caption);
  1491. }
  1492. if (s1->has_stereo3d) {
  1493. AVStereo3D *stereo = av_stereo3d_create_side_data(s->current_picture_ptr->f);
  1494. if (!stereo)
  1495. return AVERROR(ENOMEM);
  1496. *stereo = s1->stereo3d;
  1497. s1->has_stereo3d = 0;
  1498. }
  1499. if (s1->has_afd) {
  1500. AVFrameSideData *sd =
  1501. av_frame_new_side_data(s->current_picture_ptr->f,
  1502. AV_FRAME_DATA_AFD, 1);
  1503. if (!sd)
  1504. return AVERROR(ENOMEM);
  1505. *sd->data = s1->afd;
  1506. s1->has_afd = 0;
  1507. }
  1508. if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_FRAME))
  1509. ff_thread_finish_setup(avctx);
  1510. } else { // second field
  1511. int i;
  1512. if (!s->current_picture_ptr) {
  1513. av_log(s->avctx, AV_LOG_ERROR, "first field missing\n");
  1514. return -1;
  1515. }
  1516. if (s->avctx->hwaccel &&
  1517. (s->avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD)) {
  1518. if (s->avctx->hwaccel->end_frame(s->avctx) < 0)
  1519. av_log(avctx, AV_LOG_ERROR,
  1520. "hardware accelerator failed to decode first field\n");
  1521. }
  1522. for (i = 0; i < 4; i++) {
  1523. s->current_picture.f->data[i] = s->current_picture_ptr->f->data[i];
  1524. if (s->picture_structure == PICT_BOTTOM_FIELD)
  1525. s->current_picture.f->data[i] +=
  1526. s->current_picture_ptr->f->linesize[i];
  1527. }
  1528. }
  1529. if (avctx->hwaccel) {
  1530. if (avctx->hwaccel->start_frame(avctx, buf, buf_size) < 0)
  1531. return -1;
  1532. }
  1533. return 0;
  1534. }
  1535. #define DECODE_SLICE_ERROR -1
  1536. #define DECODE_SLICE_OK 0
  1537. /**
  1538. * Decode a slice.
  1539. * MpegEncContext.mb_y must be set to the MB row from the startcode.
  1540. * @return DECODE_SLICE_ERROR if the slice is damaged,
  1541. * DECODE_SLICE_OK if this slice is OK
  1542. */
  1543. static int mpeg_decode_slice(MpegEncContext *s, int mb_y,
  1544. const uint8_t **buf, int buf_size)
  1545. {
  1546. AVCodecContext *avctx = s->avctx;
  1547. const int lowres = s->avctx->lowres;
  1548. const int field_pic = s->picture_structure != PICT_FRAME;
  1549. s->resync_mb_x =
  1550. s->resync_mb_y = -1;
  1551. av_assert0(mb_y < s->mb_height);
  1552. init_get_bits(&s->gb, *buf, buf_size * 8);
  1553. if (s->codec_id != AV_CODEC_ID_MPEG1VIDEO && s->mb_height > 2800/16)
  1554. skip_bits(&s->gb, 3);
  1555. ff_mpeg1_clean_buffers(s);
  1556. s->interlaced_dct = 0;
  1557. s->qscale = get_qscale(s);
  1558. if (s->qscale == 0) {
  1559. av_log(s->avctx, AV_LOG_ERROR, "qscale == 0\n");
  1560. return -1;
  1561. }
  1562. /* extra slice info */
  1563. if (skip_1stop_8data_bits(&s->gb) < 0)
  1564. return AVERROR_INVALIDDATA;
  1565. s->mb_x = 0;
  1566. if (mb_y == 0 && s->codec_tag == AV_RL32("SLIF")) {
  1567. skip_bits1(&s->gb);
  1568. } else {
  1569. while (get_bits_left(&s->gb) > 0) {
  1570. int code = get_vlc2(&s->gb, ff_mbincr_vlc.table,
  1571. MBINCR_VLC_BITS, 2);
  1572. if (code < 0) {
  1573. av_log(s->avctx, AV_LOG_ERROR, "first mb_incr damaged\n");
  1574. return -1;
  1575. }
  1576. if (code >= 33) {
  1577. if (code == 33)
  1578. s->mb_x += 33;
  1579. /* otherwise, stuffing, nothing to do */
  1580. } else {
  1581. s->mb_x += code;
  1582. break;
  1583. }
  1584. }
  1585. }
  1586. if (s->mb_x >= (unsigned) s->mb_width) {
  1587. av_log(s->avctx, AV_LOG_ERROR, "initial skip overflow\n");
  1588. return -1;
  1589. }
  1590. if (avctx->hwaccel && avctx->hwaccel->decode_slice) {
  1591. const uint8_t *buf_end, *buf_start = *buf - 4; /* include start_code */
  1592. int start_code = -1;
  1593. buf_end = avpriv_find_start_code(buf_start + 2, *buf + buf_size, &start_code);
  1594. if (buf_end < *buf + buf_size)
  1595. buf_end -= 4;
  1596. s->mb_y = mb_y;
  1597. if (avctx->hwaccel->decode_slice(avctx, buf_start, buf_end - buf_start) < 0)
  1598. return DECODE_SLICE_ERROR;
  1599. *buf = buf_end;
  1600. return DECODE_SLICE_OK;
  1601. }
  1602. s->resync_mb_x = s->mb_x;
  1603. s->resync_mb_y = s->mb_y = mb_y;
  1604. s->mb_skip_run = 0;
  1605. ff_init_block_index(s);
  1606. if (s->mb_y == 0 && s->mb_x == 0 && (s->first_field || s->picture_structure == PICT_FRAME)) {
  1607. if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
  1608. av_log(s->avctx, AV_LOG_DEBUG,
  1609. "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",
  1610. s->qscale,
  1611. s->mpeg_f_code[0][0], s->mpeg_f_code[0][1],
  1612. s->mpeg_f_code[1][0], s->mpeg_f_code[1][1],
  1613. s->pict_type == AV_PICTURE_TYPE_I ? "I" :
  1614. (s->pict_type == AV_PICTURE_TYPE_P ? "P" :
  1615. (s->pict_type == AV_PICTURE_TYPE_B ? "B" : "S")),
  1616. s->progressive_sequence ? "ps" : "",
  1617. s->progressive_frame ? "pf" : "",
  1618. s->alternate_scan ? "alt" : "",
  1619. s->top_field_first ? "top" : "",
  1620. s->intra_dc_precision, s->picture_structure,
  1621. s->frame_pred_frame_dct, s->concealment_motion_vectors,
  1622. s->q_scale_type, s->intra_vlc_format,
  1623. s->repeat_first_field, s->chroma_420_type ? "420" : "");
  1624. }
  1625. }
  1626. for (;;) {
  1627. // If 1, we memcpy blocks in xvmcvideo.
  1628. if ((CONFIG_MPEG1_XVMC_HWACCEL || CONFIG_MPEG2_XVMC_HWACCEL) && s->pack_pblocks)
  1629. ff_xvmc_init_block(s); // set s->block
  1630. if (mpeg_decode_mb(s, s->block) < 0)
  1631. return -1;
  1632. // Note motion_val is normally NULL unless we want to extract the MVs.
  1633. if (s->current_picture.motion_val[0] && !s->encoding) {
  1634. const int wrap = s->b8_stride;
  1635. int xy = s->mb_x * 2 + s->mb_y * 2 * wrap;
  1636. int b8_xy = 4 * (s->mb_x + s->mb_y * s->mb_stride);
  1637. int motion_x, motion_y, dir, i;
  1638. for (i = 0; i < 2; i++) {
  1639. for (dir = 0; dir < 2; dir++) {
  1640. if (s->mb_intra ||
  1641. (dir == 1 && s->pict_type != AV_PICTURE_TYPE_B)) {
  1642. motion_x = motion_y = 0;
  1643. } else if (s->mv_type == MV_TYPE_16X16 ||
  1644. (s->mv_type == MV_TYPE_FIELD && field_pic)) {
  1645. motion_x = s->mv[dir][0][0];
  1646. motion_y = s->mv[dir][0][1];
  1647. } else { /* if ((s->mv_type == MV_TYPE_FIELD) || (s->mv_type == MV_TYPE_16X8)) */
  1648. motion_x = s->mv[dir][i][0];
  1649. motion_y = s->mv[dir][i][1];
  1650. }
  1651. s->current_picture.motion_val[dir][xy][0] = motion_x;
  1652. s->current_picture.motion_val[dir][xy][1] = motion_y;
  1653. s->current_picture.motion_val[dir][xy + 1][0] = motion_x;
  1654. s->current_picture.motion_val[dir][xy + 1][1] = motion_y;
  1655. s->current_picture.ref_index [dir][b8_xy] =
  1656. s->current_picture.ref_index [dir][b8_xy + 1] = s->field_select[dir][i];
  1657. av_assert2(s->field_select[dir][i] == 0 ||
  1658. s->field_select[dir][i] == 1);
  1659. }
  1660. xy += wrap;
  1661. b8_xy += 2;
  1662. }
  1663. }
  1664. s->dest[0] += 16 >> lowres;
  1665. s->dest[1] +=(16 >> lowres) >> s->chroma_x_shift;
  1666. s->dest[2] +=(16 >> lowres) >> s->chroma_x_shift;
  1667. ff_mpv_decode_mb(s, s->block);
  1668. if (++s->mb_x >= s->mb_width) {
  1669. const int mb_size = 16 >> s->avctx->lowres;
  1670. ff_mpeg_draw_horiz_band(s, mb_size * (s->mb_y >> field_pic), mb_size);
  1671. ff_mpv_report_decode_progress(s);
  1672. s->mb_x = 0;
  1673. s->mb_y += 1 << field_pic;
  1674. if (s->mb_y >= s->mb_height) {
  1675. int left = get_bits_left(&s->gb);
  1676. int is_d10 = s->chroma_format == 2 &&
  1677. s->pict_type == AV_PICTURE_TYPE_I &&
  1678. avctx->profile == 0 && avctx->level == 5 &&
  1679. s->intra_dc_precision == 2 &&
  1680. s->q_scale_type == 1 && s->alternate_scan == 0 &&
  1681. s->progressive_frame == 0
  1682. /* vbv_delay == 0xBBB || 0xE10 */;
  1683. if (left >= 32 && !is_d10) {
  1684. GetBitContext gb = s->gb;
  1685. align_get_bits(&gb);
  1686. if (show_bits(&gb, 24) == 0x060E2B) {
  1687. av_log(avctx, AV_LOG_DEBUG, "Invalid MXF data found in video stream\n");
  1688. is_d10 = 1;
  1689. }
  1690. }
  1691. if (left < 0 ||
  1692. (left && show_bits(&s->gb, FFMIN(left, 23)) && !is_d10) ||
  1693. ((avctx->err_recognition & (AV_EF_BITSTREAM | AV_EF_AGGRESSIVE)) && left > 8)) {
  1694. av_log(avctx, AV_LOG_ERROR, "end mismatch left=%d %0X\n",
  1695. left, show_bits(&s->gb, FFMIN(left, 23)));
  1696. return -1;
  1697. } else
  1698. goto eos;
  1699. }
  1700. // There are some files out there which are missing the last slice
  1701. // in cases where the slice is completely outside the visible
  1702. // area, we detect this here instead of running into the end expecting
  1703. // more data
  1704. if (s->mb_y >= ((s->height + 15) >> 4) &&
  1705. s->progressive_frame &&
  1706. !s->progressive_sequence &&
  1707. get_bits_left(&s->gb) <= 8 &&
  1708. get_bits_left(&s->gb) >= 0 &&
  1709. s->mb_skip_run == -1 &&
  1710. show_bits(&s->gb, 8) == 0)
  1711. goto eos;
  1712. ff_init_block_index(s);
  1713. }
  1714. /* skip mb handling */
  1715. if (s->mb_skip_run == -1) {
  1716. /* read increment again */
  1717. s->mb_skip_run = 0;
  1718. for (;;) {
  1719. int code = get_vlc2(&s->gb, ff_mbincr_vlc.table,
  1720. MBINCR_VLC_BITS, 2);
  1721. if (code < 0) {
  1722. av_log(s->avctx, AV_LOG_ERROR, "mb incr damaged\n");
  1723. return -1;
  1724. }
  1725. if (code >= 33) {
  1726. if (code == 33) {
  1727. s->mb_skip_run += 33;
  1728. } else if (code == 35) {
  1729. if (s->mb_skip_run != 0 || show_bits(&s->gb, 15) != 0) {
  1730. av_log(s->avctx, AV_LOG_ERROR, "slice mismatch\n");
  1731. return -1;
  1732. }
  1733. goto eos; /* end of slice */
  1734. }
  1735. /* otherwise, stuffing, nothing to do */
  1736. } else {
  1737. s->mb_skip_run += code;
  1738. break;
  1739. }
  1740. }
  1741. if (s->mb_skip_run) {
  1742. int i;
  1743. if (s->pict_type == AV_PICTURE_TYPE_I) {
  1744. av_log(s->avctx, AV_LOG_ERROR,
  1745. "skipped MB in I frame at %d %d\n", s->mb_x, s->mb_y);
  1746. return -1;
  1747. }
  1748. /* skip mb */
  1749. s->mb_intra = 0;
  1750. for (i = 0; i < 12; i++)
  1751. s->block_last_index[i] = -1;
  1752. if (s->picture_structure == PICT_FRAME)
  1753. s->mv_type = MV_TYPE_16X16;
  1754. else
  1755. s->mv_type = MV_TYPE_FIELD;
  1756. if (s->pict_type == AV_PICTURE_TYPE_P) {
  1757. /* if P type, zero motion vector is implied */
  1758. s->mv_dir = MV_DIR_FORWARD;
  1759. s->mv[0][0][0] = s->mv[0][0][1] = 0;
  1760. s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
  1761. s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
  1762. s->field_select[0][0] = (s->picture_structure - 1) & 1;
  1763. } else {
  1764. /* if B type, reuse previous vectors and directions */
  1765. s->mv[0][0][0] = s->last_mv[0][0][0];
  1766. s->mv[0][0][1] = s->last_mv[0][0][1];
  1767. s->mv[1][0][0] = s->last_mv[1][0][0];
  1768. s->mv[1][0][1] = s->last_mv[1][0][1];
  1769. }
  1770. }
  1771. }
  1772. }
  1773. eos: // end of slice
  1774. if (get_bits_left(&s->gb) < 0) {
  1775. av_log(s, AV_LOG_ERROR, "overread %d\n", -get_bits_left(&s->gb));
  1776. return AVERROR_INVALIDDATA;
  1777. }
  1778. *buf += (get_bits_count(&s->gb) - 1) / 8;
  1779. av_dlog(s, "y %d %d %d %d\n", s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y);
  1780. return 0;
  1781. }
  1782. static int slice_decode_thread(AVCodecContext *c, void *arg)
  1783. {
  1784. MpegEncContext *s = *(void **) arg;
  1785. const uint8_t *buf = s->gb.buffer;
  1786. int mb_y = s->start_mb_y;
  1787. const int field_pic = s->picture_structure != PICT_FRAME;
  1788. s->er.error_count = (3 * (s->end_mb_y - s->start_mb_y) * s->mb_width) >> field_pic;
  1789. for (;;) {
  1790. uint32_t start_code;
  1791. int ret;
  1792. ret = mpeg_decode_slice(s, mb_y, &buf, s->gb.buffer_end - buf);
  1793. emms_c();
  1794. av_dlog(c, "ret:%d resync:%d/%d mb:%d/%d ts:%d/%d ec:%d\n",
  1795. ret, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y,
  1796. s->start_mb_y, s->end_mb_y, s->er.error_count);
  1797. if (ret < 0) {
  1798. if (c->err_recognition & AV_EF_EXPLODE)
  1799. return ret;
  1800. if (s->resync_mb_x >= 0 && s->resync_mb_y >= 0)
  1801. ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
  1802. s->mb_x, s->mb_y,
  1803. ER_AC_ERROR | ER_DC_ERROR | ER_MV_ERROR);
  1804. } else {
  1805. ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
  1806. s->mb_x - 1, s->mb_y,
  1807. ER_AC_END | ER_DC_END | ER_MV_END);
  1808. }
  1809. if (s->mb_y == s->end_mb_y)
  1810. return 0;
  1811. start_code = -1;
  1812. buf = avpriv_find_start_code(buf, s->gb.buffer_end, &start_code);
  1813. mb_y = start_code - SLICE_MIN_START_CODE;
  1814. if (s->codec_id != AV_CODEC_ID_MPEG1VIDEO && s->mb_height > 2800/16)
  1815. mb_y += (*buf&0xE0)<<2;
  1816. mb_y <<= field_pic;
  1817. if (s->picture_structure == PICT_BOTTOM_FIELD)
  1818. mb_y++;
  1819. if (mb_y < 0 || mb_y >= s->end_mb_y)
  1820. return -1;
  1821. }
  1822. }
  1823. /**
  1824. * Handle slice ends.
  1825. * @return 1 if it seems to be the last slice
  1826. */
  1827. static int slice_end(AVCodecContext *avctx, AVFrame *pict)
  1828. {
  1829. Mpeg1Context *s1 = avctx->priv_data;
  1830. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1831. if (!s1->mpeg_enc_ctx_allocated || !s->current_picture_ptr)
  1832. return 0;
  1833. if (s->avctx->hwaccel) {
  1834. if (s->avctx->hwaccel->end_frame(s->avctx) < 0)
  1835. av_log(avctx, AV_LOG_ERROR,
  1836. "hardware accelerator failed to decode picture\n");
  1837. }
  1838. /* end of slice reached */
  1839. if (/* s->mb_y << field_pic == s->mb_height && */ !s->first_field && !s1->first_slice) {
  1840. /* end of image */
  1841. ff_er_frame_end(&s->er);
  1842. ff_mpv_frame_end(s);
  1843. if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
  1844. int ret = av_frame_ref(pict, s->current_picture_ptr->f);
  1845. if (ret < 0)
  1846. return ret;
  1847. ff_print_debug_info(s, s->current_picture_ptr, pict);
  1848. ff_mpv_export_qp_table(s, pict, s->current_picture_ptr, FF_QSCALE_TYPE_MPEG2);
  1849. } else {
  1850. if (avctx->active_thread_type & FF_THREAD_FRAME)
  1851. s->picture_number++;
  1852. /* latency of 1 frame for I- and P-frames */
  1853. /* XXX: use another variable than picture_number */
  1854. if (s->last_picture_ptr) {
  1855. int ret = av_frame_ref(pict, s->last_picture_ptr->f);
  1856. if (ret < 0)
  1857. return ret;
  1858. ff_print_debug_info(s, s->last_picture_ptr, pict);
  1859. ff_mpv_export_qp_table(s, pict, s->last_picture_ptr, FF_QSCALE_TYPE_MPEG2);
  1860. }
  1861. }
  1862. return 1;
  1863. } else {
  1864. return 0;
  1865. }
  1866. }
  1867. static int mpeg1_decode_sequence(AVCodecContext *avctx,
  1868. const uint8_t *buf, int buf_size)
  1869. {
  1870. Mpeg1Context *s1 = avctx->priv_data;
  1871. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1872. int width, height;
  1873. int i, v, j;
  1874. init_get_bits(&s->gb, buf, buf_size * 8);
  1875. width = get_bits(&s->gb, 12);
  1876. height = get_bits(&s->gb, 12);
  1877. if (width == 0 || height == 0) {
  1878. av_log(avctx, AV_LOG_WARNING,
  1879. "Invalid horizontal or vertical size value.\n");
  1880. if (avctx->err_recognition & (AV_EF_BITSTREAM | AV_EF_COMPLIANT))
  1881. return AVERROR_INVALIDDATA;
  1882. }
  1883. s->aspect_ratio_info = get_bits(&s->gb, 4);
  1884. if (s->aspect_ratio_info == 0) {
  1885. av_log(avctx, AV_LOG_ERROR, "aspect ratio has forbidden 0 value\n");
  1886. if (avctx->err_recognition & (AV_EF_BITSTREAM | AV_EF_COMPLIANT))
  1887. return -1;
  1888. }
  1889. s->frame_rate_index = get_bits(&s->gb, 4);
  1890. if (s->frame_rate_index == 0 || s->frame_rate_index > 13)
  1891. return -1;
  1892. s->bit_rate = get_bits(&s->gb, 18) * 400;
  1893. if (get_bits1(&s->gb) == 0) /* marker */
  1894. return -1;
  1895. s->width = width;
  1896. s->height = height;
  1897. s->avctx->rc_buffer_size = get_bits(&s->gb, 10) * 1024 * 16;
  1898. skip_bits(&s->gb, 1);
  1899. /* get matrix */
  1900. if (get_bits1(&s->gb)) {
  1901. load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
  1902. } else {
  1903. for (i = 0; i < 64; i++) {
  1904. j = s->idsp.idct_permutation[i];
  1905. v = ff_mpeg1_default_intra_matrix[i];
  1906. s->intra_matrix[j] = v;
  1907. s->chroma_intra_matrix[j] = v;
  1908. }
  1909. }
  1910. if (get_bits1(&s->gb)) {
  1911. load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
  1912. } else {
  1913. for (i = 0; i < 64; i++) {
  1914. int j = s->idsp.idct_permutation[i];
  1915. v = ff_mpeg1_default_non_intra_matrix[i];
  1916. s->inter_matrix[j] = v;
  1917. s->chroma_inter_matrix[j] = v;
  1918. }
  1919. }
  1920. if (show_bits(&s->gb, 23) != 0) {
  1921. av_log(s->avctx, AV_LOG_ERROR, "sequence header damaged\n");
  1922. return -1;
  1923. }
  1924. /* We set MPEG-2 parameters so that it emulates MPEG-1. */
  1925. s->progressive_sequence = 1;
  1926. s->progressive_frame = 1;
  1927. s->picture_structure = PICT_FRAME;
  1928. s->first_field = 0;
  1929. s->frame_pred_frame_dct = 1;
  1930. s->chroma_format = 1;
  1931. s->codec_id =
  1932. s->avctx->codec_id = AV_CODEC_ID_MPEG1VIDEO;
  1933. s->out_format = FMT_MPEG1;
  1934. s->swap_uv = 0; // AFAIK VCR2 does not have SEQ_HEADER
  1935. if (s->flags & CODEC_FLAG_LOW_DELAY)
  1936. s->low_delay = 1;
  1937. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  1938. av_log(s->avctx, AV_LOG_DEBUG, "vbv buffer: %d, bitrate:%d, aspect_ratio_info: %d \n",
  1939. s->avctx->rc_buffer_size, s->bit_rate, s->aspect_ratio_info);
  1940. return 0;
  1941. }
  1942. static int vcr2_init_sequence(AVCodecContext *avctx)
  1943. {
  1944. Mpeg1Context *s1 = avctx->priv_data;
  1945. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1946. int i, v;
  1947. /* start new MPEG-1 context decoding */
  1948. s->out_format = FMT_MPEG1;
  1949. if (s1->mpeg_enc_ctx_allocated) {
  1950. ff_mpv_common_end(s);
  1951. s1->mpeg_enc_ctx_allocated = 0;
  1952. }
  1953. s->width = avctx->coded_width;
  1954. s->height = avctx->coded_height;
  1955. avctx->has_b_frames = 0; // true?
  1956. s->low_delay = 1;
  1957. avctx->pix_fmt = mpeg_get_pixelformat(avctx);
  1958. setup_hwaccel_for_pixfmt(avctx);
  1959. ff_mpv_idct_init(s);
  1960. if (ff_mpv_common_init(s) < 0)
  1961. return -1;
  1962. s1->mpeg_enc_ctx_allocated = 1;
  1963. for (i = 0; i < 64; i++) {
  1964. int j = s->idsp.idct_permutation[i];
  1965. v = ff_mpeg1_default_intra_matrix[i];
  1966. s->intra_matrix[j] = v;
  1967. s->chroma_intra_matrix[j] = v;
  1968. v = ff_mpeg1_default_non_intra_matrix[i];
  1969. s->inter_matrix[j] = v;
  1970. s->chroma_inter_matrix[j] = v;
  1971. }
  1972. s->progressive_sequence = 1;
  1973. s->progressive_frame = 1;
  1974. s->picture_structure = PICT_FRAME;
  1975. s->first_field = 0;
  1976. s->frame_pred_frame_dct = 1;
  1977. s->chroma_format = 1;
  1978. if (s->codec_tag == AV_RL32("BW10")) {
  1979. s->codec_id = s->avctx->codec_id = AV_CODEC_ID_MPEG1VIDEO;
  1980. } else {
  1981. s->swap_uv = 1; // in case of xvmc we need to swap uv for each MB
  1982. s->codec_id = s->avctx->codec_id = AV_CODEC_ID_MPEG2VIDEO;
  1983. }
  1984. s1->save_width = s->width;
  1985. s1->save_height = s->height;
  1986. s1->save_progressive_seq = s->progressive_sequence;
  1987. return 0;
  1988. }
  1989. static int mpeg_decode_a53_cc(AVCodecContext *avctx,
  1990. const uint8_t *p, int buf_size)
  1991. {
  1992. Mpeg1Context *s1 = avctx->priv_data;
  1993. if (buf_size >= 6 &&
  1994. p[0] == 'G' && p[1] == 'A' && p[2] == '9' && p[3] == '4' &&
  1995. p[4] == 3 && (p[5] & 0x40)) {
  1996. /* extract A53 Part 4 CC data */
  1997. int cc_count = p[5] & 0x1f;
  1998. if (cc_count > 0 && buf_size >= 7 + cc_count * 3) {
  1999. av_freep(&s1->a53_caption);
  2000. s1->a53_caption_size = cc_count * 3;
  2001. s1->a53_caption = av_malloc(s1->a53_caption_size);
  2002. if (s1->a53_caption)
  2003. memcpy(s1->a53_caption, p + 7, s1->a53_caption_size);
  2004. }
  2005. return 1;
  2006. } else if (buf_size >= 11 &&
  2007. p[0] == 'C' && p[1] == 'C' && p[2] == 0x01 && p[3] == 0xf8) {
  2008. /* extract DVD CC data */
  2009. int cc_count = 0;
  2010. int i;
  2011. // There is a caption count field in the data, but it is often
  2012. // incorect. So count the number of captions present.
  2013. for (i = 5; i + 6 <= buf_size && ((p[i] & 0xfe) == 0xfe); i += 6)
  2014. cc_count++;
  2015. // Transform the DVD format into A53 Part 4 format
  2016. if (cc_count > 0) {
  2017. av_freep(&s1->a53_caption);
  2018. s1->a53_caption_size = cc_count * 6;
  2019. s1->a53_caption = av_malloc(s1->a53_caption_size);
  2020. if (s1->a53_caption) {
  2021. uint8_t field1 = !!(p[4] & 0x80);
  2022. uint8_t *cap = s1->a53_caption;
  2023. p += 5;
  2024. for (i = 0; i < cc_count; i++) {
  2025. cap[0] = (p[0] == 0xff && field1) ? 0xfc : 0xfd;
  2026. cap[1] = p[1];
  2027. cap[2] = p[2];
  2028. cap[3] = (p[3] == 0xff && !field1) ? 0xfc : 0xfd;
  2029. cap[4] = p[4];
  2030. cap[5] = p[5];
  2031. cap += 6;
  2032. p += 6;
  2033. }
  2034. }
  2035. }
  2036. return 1;
  2037. }
  2038. return 0;
  2039. }
  2040. static void mpeg_decode_user_data(AVCodecContext *avctx,
  2041. const uint8_t *p, int buf_size)
  2042. {
  2043. Mpeg1Context *s = avctx->priv_data;
  2044. const uint8_t *buf_end = p + buf_size;
  2045. Mpeg1Context *s1 = avctx->priv_data;
  2046. if (buf_size > 29){
  2047. int i;
  2048. for(i=0; i<20; i++)
  2049. if (!memcmp(p+i, "\0TMPGEXS\0", 9)){
  2050. s->tmpgexs= 1;
  2051. }
  2052. /* for(i=0; !(!p[i-2] && !p[i-1] && p[i]==1) && i<buf_size; i++){
  2053. av_log(avctx, AV_LOG_ERROR, "%c", p[i]);
  2054. }
  2055. av_log(avctx, AV_LOG_ERROR, "\n");*/
  2056. }
  2057. /* we parse the DTG active format information */
  2058. if (buf_end - p >= 5 &&
  2059. p[0] == 'D' && p[1] == 'T' && p[2] == 'G' && p[3] == '1') {
  2060. int flags = p[4];
  2061. p += 5;
  2062. if (flags & 0x80) {
  2063. /* skip event id */
  2064. p += 2;
  2065. }
  2066. if (flags & 0x40) {
  2067. if (buf_end - p < 1)
  2068. return;
  2069. #if FF_API_AFD
  2070. FF_DISABLE_DEPRECATION_WARNINGS
  2071. avctx->dtg_active_format = p[0] & 0x0f;
  2072. FF_ENABLE_DEPRECATION_WARNINGS
  2073. #endif /* FF_API_AFD */
  2074. s1->has_afd = 1;
  2075. s1->afd = p[0] & 0x0f;
  2076. }
  2077. } else if (buf_end - p >= 6 &&
  2078. p[0] == 'J' && p[1] == 'P' && p[2] == '3' && p[3] == 'D' &&
  2079. p[4] == 0x03) { // S3D_video_format_length
  2080. // the 0x7F mask ignores the reserved_bit value
  2081. const uint8_t S3D_video_format_type = p[5] & 0x7F;
  2082. if (S3D_video_format_type == 0x03 ||
  2083. S3D_video_format_type == 0x04 ||
  2084. S3D_video_format_type == 0x08 ||
  2085. S3D_video_format_type == 0x23) {
  2086. s1->has_stereo3d = 1;
  2087. switch (S3D_video_format_type) {
  2088. case 0x03:
  2089. s1->stereo3d.type = AV_STEREO3D_SIDEBYSIDE;
  2090. break;
  2091. case 0x04:
  2092. s1->stereo3d.type = AV_STEREO3D_TOPBOTTOM;
  2093. break;
  2094. case 0x08:
  2095. s1->stereo3d.type = AV_STEREO3D_2D;
  2096. break;
  2097. case 0x23:
  2098. s1->stereo3d.type = AV_STEREO3D_SIDEBYSIDE_QUINCUNX;
  2099. break;
  2100. }
  2101. }
  2102. } else if (mpeg_decode_a53_cc(avctx, p, buf_size)) {
  2103. return;
  2104. }
  2105. }
  2106. static void mpeg_decode_gop(AVCodecContext *avctx,
  2107. const uint8_t *buf, int buf_size)
  2108. {
  2109. Mpeg1Context *s1 = avctx->priv_data;
  2110. MpegEncContext *s = &s1->mpeg_enc_ctx;
  2111. int broken_link;
  2112. int64_t tc;
  2113. init_get_bits(&s->gb, buf, buf_size * 8);
  2114. tc = avctx->timecode_frame_start = get_bits(&s->gb, 25);
  2115. s->closed_gop = get_bits1(&s->gb);
  2116. /* broken_link indicate that after editing the
  2117. * reference frames of the first B-Frames after GOP I-Frame
  2118. * are missing (open gop) */
  2119. broken_link = get_bits1(&s->gb);
  2120. if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
  2121. char tcbuf[AV_TIMECODE_STR_SIZE];
  2122. av_timecode_make_mpeg_tc_string(tcbuf, tc);
  2123. av_log(s->avctx, AV_LOG_DEBUG,
  2124. "GOP (%s) closed_gop=%d broken_link=%d\n",
  2125. tcbuf, s->closed_gop, broken_link);
  2126. }
  2127. }
  2128. static int decode_chunks(AVCodecContext *avctx, AVFrame *picture,
  2129. int *got_output, const uint8_t *buf, int buf_size)
  2130. {
  2131. Mpeg1Context *s = avctx->priv_data;
  2132. MpegEncContext *s2 = &s->mpeg_enc_ctx;
  2133. const uint8_t *buf_ptr = buf;
  2134. const uint8_t *buf_end = buf + buf_size;
  2135. int ret, input_size;
  2136. int last_code = 0, skip_frame = 0;
  2137. int picture_start_code_seen = 0;
  2138. for (;;) {
  2139. /* find next start code */
  2140. uint32_t start_code = -1;
  2141. buf_ptr = avpriv_find_start_code(buf_ptr, buf_end, &start_code);
  2142. if (start_code > 0x1ff) {
  2143. if (!skip_frame) {
  2144. if (HAVE_THREADS &&
  2145. (avctx->active_thread_type & FF_THREAD_SLICE) &&
  2146. !avctx->hwaccel) {
  2147. int i;
  2148. av_assert0(avctx->thread_count > 1);
  2149. avctx->execute(avctx, slice_decode_thread,
  2150. &s2->thread_context[0], NULL,
  2151. s->slice_count, sizeof(void *));
  2152. for (i = 0; i < s->slice_count; i++)
  2153. s2->er.error_count += s2->thread_context[i]->er.error_count;
  2154. }
  2155. if ((CONFIG_MPEG_VDPAU_DECODER || CONFIG_MPEG1_VDPAU_DECODER)
  2156. && uses_vdpau(avctx))
  2157. ff_vdpau_mpeg_picture_complete(s2, buf, buf_size, s->slice_count);
  2158. ret = slice_end(avctx, picture);
  2159. if (ret < 0)
  2160. return ret;
  2161. else if (ret) {
  2162. // FIXME: merge with the stuff in mpeg_decode_slice
  2163. if (s2->last_picture_ptr || s2->low_delay)
  2164. *got_output = 1;
  2165. }
  2166. }
  2167. s2->pict_type = 0;
  2168. if (avctx->err_recognition & AV_EF_EXPLODE && s2->er.error_count)
  2169. return AVERROR_INVALIDDATA;
  2170. return FFMAX(0, buf_ptr - buf - s2->parse_context.last_index);
  2171. }
  2172. input_size = buf_end - buf_ptr;
  2173. if (avctx->debug & FF_DEBUG_STARTCODE)
  2174. av_log(avctx, AV_LOG_DEBUG, "%3"PRIX32" at %"PTRDIFF_SPECIFIER" left %d\n",
  2175. start_code, buf_ptr - buf, input_size);
  2176. /* prepare data for next start code */
  2177. switch (start_code) {
  2178. case SEQ_START_CODE:
  2179. if (last_code == 0) {
  2180. mpeg1_decode_sequence(avctx, buf_ptr, input_size);
  2181. if (buf != avctx->extradata)
  2182. s->sync = 1;
  2183. } else {
  2184. av_log(avctx, AV_LOG_ERROR,
  2185. "ignoring SEQ_START_CODE after %X\n", last_code);
  2186. if (avctx->err_recognition & AV_EF_EXPLODE)
  2187. return AVERROR_INVALIDDATA;
  2188. }
  2189. break;
  2190. case PICTURE_START_CODE:
  2191. if (picture_start_code_seen && s2->picture_structure == PICT_FRAME) {
  2192. /* If it's a frame picture, there can't be more than one picture header.
  2193. Yet, it does happen and we need to handle it. */
  2194. av_log(avctx, AV_LOG_WARNING, "ignoring extra picture following a frame-picture\n");
  2195. break;
  2196. }
  2197. picture_start_code_seen = 1;
  2198. if (s2->width <= 0 || s2->height <= 0) {
  2199. av_log(avctx, AV_LOG_ERROR, "Invalid frame dimensions %dx%d.\n",
  2200. s2->width, s2->height);
  2201. return AVERROR_INVALIDDATA;
  2202. }
  2203. if (s->tmpgexs){
  2204. s2->intra_dc_precision= 3;
  2205. s2->intra_matrix[0]= 1;
  2206. }
  2207. if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE) &&
  2208. !avctx->hwaccel && s->slice_count) {
  2209. int i;
  2210. avctx->execute(avctx, slice_decode_thread,
  2211. s2->thread_context, NULL,
  2212. s->slice_count, sizeof(void *));
  2213. for (i = 0; i < s->slice_count; i++)
  2214. s2->er.error_count += s2->thread_context[i]->er.error_count;
  2215. s->slice_count = 0;
  2216. }
  2217. if (last_code == 0 || last_code == SLICE_MIN_START_CODE) {
  2218. ret = mpeg_decode_postinit(avctx);
  2219. if (ret < 0) {
  2220. av_log(avctx, AV_LOG_ERROR,
  2221. "mpeg_decode_postinit() failure\n");
  2222. return ret;
  2223. }
  2224. /* We have a complete image: we try to decompress it. */
  2225. if (mpeg1_decode_picture(avctx, buf_ptr, input_size) < 0)
  2226. s2->pict_type = 0;
  2227. s->first_slice = 1;
  2228. last_code = PICTURE_START_CODE;
  2229. } else {
  2230. av_log(avctx, AV_LOG_ERROR,
  2231. "ignoring pic after %X\n", last_code);
  2232. if (avctx->err_recognition & AV_EF_EXPLODE)
  2233. return AVERROR_INVALIDDATA;
  2234. }
  2235. break;
  2236. case EXT_START_CODE:
  2237. init_get_bits(&s2->gb, buf_ptr, input_size * 8);
  2238. switch (get_bits(&s2->gb, 4)) {
  2239. case 0x1:
  2240. if (last_code == 0) {
  2241. mpeg_decode_sequence_extension(s);
  2242. } else {
  2243. av_log(avctx, AV_LOG_ERROR,
  2244. "ignoring seq ext after %X\n", last_code);
  2245. if (avctx->err_recognition & AV_EF_EXPLODE)
  2246. return AVERROR_INVALIDDATA;
  2247. }
  2248. break;
  2249. case 0x2:
  2250. mpeg_decode_sequence_display_extension(s);
  2251. break;
  2252. case 0x3:
  2253. mpeg_decode_quant_matrix_extension(s2);
  2254. break;
  2255. case 0x7:
  2256. mpeg_decode_picture_display_extension(s);
  2257. break;
  2258. case 0x8:
  2259. if (last_code == PICTURE_START_CODE) {
  2260. mpeg_decode_picture_coding_extension(s);
  2261. } else {
  2262. av_log(avctx, AV_LOG_ERROR,
  2263. "ignoring pic cod ext after %X\n", last_code);
  2264. if (avctx->err_recognition & AV_EF_EXPLODE)
  2265. return AVERROR_INVALIDDATA;
  2266. }
  2267. break;
  2268. }
  2269. break;
  2270. case USER_START_CODE:
  2271. mpeg_decode_user_data(avctx, buf_ptr, input_size);
  2272. break;
  2273. case GOP_START_CODE:
  2274. if (last_code == 0) {
  2275. s2->first_field = 0;
  2276. mpeg_decode_gop(avctx, buf_ptr, input_size);
  2277. s->sync = 1;
  2278. } else {
  2279. av_log(avctx, AV_LOG_ERROR,
  2280. "ignoring GOP_START_CODE after %X\n", last_code);
  2281. if (avctx->err_recognition & AV_EF_EXPLODE)
  2282. return AVERROR_INVALIDDATA;
  2283. }
  2284. break;
  2285. default:
  2286. if (start_code >= SLICE_MIN_START_CODE &&
  2287. start_code <= SLICE_MAX_START_CODE && last_code == PICTURE_START_CODE) {
  2288. if (s2->progressive_sequence && !s2->progressive_frame) {
  2289. s2->progressive_frame = 1;
  2290. av_log(s2->avctx, AV_LOG_ERROR,
  2291. "interlaced frame in progressive sequence, ignoring\n");
  2292. }
  2293. if (s2->picture_structure == 0 ||
  2294. (s2->progressive_frame && s2->picture_structure != PICT_FRAME)) {
  2295. av_log(s2->avctx, AV_LOG_ERROR,
  2296. "picture_structure %d invalid, ignoring\n",
  2297. s2->picture_structure);
  2298. s2->picture_structure = PICT_FRAME;
  2299. }
  2300. if (s2->progressive_sequence && !s2->frame_pred_frame_dct)
  2301. av_log(s2->avctx, AV_LOG_WARNING, "invalid frame_pred_frame_dct\n");
  2302. if (s2->picture_structure == PICT_FRAME) {
  2303. s2->first_field = 0;
  2304. s2->v_edge_pos = 16 * s2->mb_height;
  2305. } else {
  2306. s2->first_field ^= 1;
  2307. s2->v_edge_pos = 8 * s2->mb_height;
  2308. memset(s2->mbskip_table, 0, s2->mb_stride * s2->mb_height);
  2309. }
  2310. }
  2311. if (start_code >= SLICE_MIN_START_CODE &&
  2312. start_code <= SLICE_MAX_START_CODE && last_code != 0) {
  2313. const int field_pic = s2->picture_structure != PICT_FRAME;
  2314. int mb_y = start_code - SLICE_MIN_START_CODE;
  2315. last_code = SLICE_MIN_START_CODE;
  2316. if (s2->codec_id != AV_CODEC_ID_MPEG1VIDEO && s2->mb_height > 2800/16)
  2317. mb_y += (*buf_ptr&0xE0)<<2;
  2318. mb_y <<= field_pic;
  2319. if (s2->picture_structure == PICT_BOTTOM_FIELD)
  2320. mb_y++;
  2321. if (buf_end - buf_ptr < 2) {
  2322. av_log(s2->avctx, AV_LOG_ERROR, "slice too small\n");
  2323. return AVERROR_INVALIDDATA;
  2324. }
  2325. if (mb_y >= s2->mb_height) {
  2326. av_log(s2->avctx, AV_LOG_ERROR,
  2327. "slice below image (%d >= %d)\n", mb_y, s2->mb_height);
  2328. return -1;
  2329. }
  2330. if (!s2->last_picture_ptr) {
  2331. /* Skip B-frames if we do not have reference frames and
  2332. * GOP is not closed. */
  2333. if (s2->pict_type == AV_PICTURE_TYPE_B) {
  2334. if (!s2->closed_gop) {
  2335. skip_frame = 1;
  2336. break;
  2337. }
  2338. }
  2339. }
  2340. if (s2->pict_type == AV_PICTURE_TYPE_I || (s2->flags2 & CODEC_FLAG2_SHOW_ALL))
  2341. s->sync = 1;
  2342. if (!s2->next_picture_ptr) {
  2343. /* Skip P-frames if we do not have a reference frame or
  2344. * we have an invalid header. */
  2345. if (s2->pict_type == AV_PICTURE_TYPE_P && !s->sync) {
  2346. skip_frame = 1;
  2347. break;
  2348. }
  2349. }
  2350. if ((avctx->skip_frame >= AVDISCARD_NONREF &&
  2351. s2->pict_type == AV_PICTURE_TYPE_B) ||
  2352. (avctx->skip_frame >= AVDISCARD_NONKEY &&
  2353. s2->pict_type != AV_PICTURE_TYPE_I) ||
  2354. avctx->skip_frame >= AVDISCARD_ALL) {
  2355. skip_frame = 1;
  2356. break;
  2357. }
  2358. if (!s->mpeg_enc_ctx_allocated)
  2359. break;
  2360. if (s2->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
  2361. if (mb_y < avctx->skip_top ||
  2362. mb_y >= s2->mb_height - avctx->skip_bottom)
  2363. break;
  2364. }
  2365. if (!s2->pict_type) {
  2366. av_log(avctx, AV_LOG_ERROR, "Missing picture start code\n");
  2367. if (avctx->err_recognition & AV_EF_EXPLODE)
  2368. return AVERROR_INVALIDDATA;
  2369. break;
  2370. }
  2371. if (s->first_slice) {
  2372. skip_frame = 0;
  2373. s->first_slice = 0;
  2374. if (mpeg_field_start(s2, buf, buf_size) < 0)
  2375. return -1;
  2376. }
  2377. if (!s2->current_picture_ptr) {
  2378. av_log(avctx, AV_LOG_ERROR,
  2379. "current_picture not initialized\n");
  2380. return AVERROR_INVALIDDATA;
  2381. }
  2382. if (uses_vdpau(avctx)) {
  2383. s->slice_count++;
  2384. break;
  2385. }
  2386. if (HAVE_THREADS &&
  2387. (avctx->active_thread_type & FF_THREAD_SLICE) &&
  2388. !avctx->hwaccel) {
  2389. int threshold = (s2->mb_height * s->slice_count +
  2390. s2->slice_context_count / 2) /
  2391. s2->slice_context_count;
  2392. av_assert0(avctx->thread_count > 1);
  2393. if (threshold <= mb_y) {
  2394. MpegEncContext *thread_context = s2->thread_context[s->slice_count];
  2395. thread_context->start_mb_y = mb_y;
  2396. thread_context->end_mb_y = s2->mb_height;
  2397. if (s->slice_count) {
  2398. s2->thread_context[s->slice_count - 1]->end_mb_y = mb_y;
  2399. ret = ff_update_duplicate_context(thread_context, s2);
  2400. if (ret < 0)
  2401. return ret;
  2402. }
  2403. init_get_bits(&thread_context->gb, buf_ptr, input_size * 8);
  2404. s->slice_count++;
  2405. }
  2406. buf_ptr += 2; // FIXME add minimum number of bytes per slice
  2407. } else {
  2408. ret = mpeg_decode_slice(s2, mb_y, &buf_ptr, input_size);
  2409. emms_c();
  2410. if (ret < 0) {
  2411. if (avctx->err_recognition & AV_EF_EXPLODE)
  2412. return ret;
  2413. if (s2->resync_mb_x >= 0 && s2->resync_mb_y >= 0)
  2414. ff_er_add_slice(&s2->er, s2->resync_mb_x,
  2415. s2->resync_mb_y, s2->mb_x, s2->mb_y,
  2416. ER_AC_ERROR | ER_DC_ERROR | ER_MV_ERROR);
  2417. } else {
  2418. ff_er_add_slice(&s2->er, s2->resync_mb_x,
  2419. s2->resync_mb_y, s2->mb_x - 1, s2->mb_y,
  2420. ER_AC_END | ER_DC_END | ER_MV_END);
  2421. }
  2422. }
  2423. }
  2424. break;
  2425. }
  2426. }
  2427. }
  2428. static int mpeg_decode_frame(AVCodecContext *avctx, void *data,
  2429. int *got_output, AVPacket *avpkt)
  2430. {
  2431. const uint8_t *buf = avpkt->data;
  2432. int ret;
  2433. int buf_size = avpkt->size;
  2434. Mpeg1Context *s = avctx->priv_data;
  2435. AVFrame *picture = data;
  2436. MpegEncContext *s2 = &s->mpeg_enc_ctx;
  2437. if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == SEQ_END_CODE)) {
  2438. /* special case for last picture */
  2439. if (s2->low_delay == 0 && s2->next_picture_ptr) {
  2440. int ret = av_frame_ref(picture, s2->next_picture_ptr->f);
  2441. if (ret < 0)
  2442. return ret;
  2443. s2->next_picture_ptr = NULL;
  2444. *got_output = 1;
  2445. }
  2446. return buf_size;
  2447. }
  2448. if (s2->flags & CODEC_FLAG_TRUNCATED) {
  2449. int next = ff_mpeg1_find_frame_end(&s2->parse_context, buf,
  2450. buf_size, NULL);
  2451. if (ff_combine_frame(&s2->parse_context, next,
  2452. (const uint8_t **) &buf, &buf_size) < 0)
  2453. return buf_size;
  2454. }
  2455. s2->codec_tag = avpriv_toupper4(avctx->codec_tag);
  2456. if (s->mpeg_enc_ctx_allocated == 0 && ( s2->codec_tag == AV_RL32("VCR2")
  2457. || s2->codec_tag == AV_RL32("BW10")
  2458. ))
  2459. vcr2_init_sequence(avctx);
  2460. s->slice_count = 0;
  2461. if (avctx->extradata && !s->extradata_decoded) {
  2462. ret = decode_chunks(avctx, picture, got_output,
  2463. avctx->extradata, avctx->extradata_size);
  2464. if (*got_output) {
  2465. av_log(avctx, AV_LOG_ERROR, "picture in extradata\n");
  2466. *got_output = 0;
  2467. }
  2468. s->extradata_decoded = 1;
  2469. if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE)) {
  2470. s2->current_picture_ptr = NULL;
  2471. return ret;
  2472. }
  2473. }
  2474. ret = decode_chunks(avctx, picture, got_output, buf, buf_size);
  2475. if (ret<0 || *got_output)
  2476. s2->current_picture_ptr = NULL;
  2477. return ret;
  2478. }
  2479. static void flush(AVCodecContext *avctx)
  2480. {
  2481. Mpeg1Context *s = avctx->priv_data;
  2482. s->sync = 0;
  2483. ff_mpeg_flush(avctx);
  2484. }
  2485. static av_cold int mpeg_decode_end(AVCodecContext *avctx)
  2486. {
  2487. Mpeg1Context *s = avctx->priv_data;
  2488. if (s->mpeg_enc_ctx_allocated)
  2489. ff_mpv_common_end(&s->mpeg_enc_ctx);
  2490. av_freep(&s->a53_caption);
  2491. return 0;
  2492. }
  2493. static const AVProfile mpeg2_video_profiles[] = {
  2494. { FF_PROFILE_MPEG2_422, "4:2:2" },
  2495. { FF_PROFILE_MPEG2_HIGH, "High" },
  2496. { FF_PROFILE_MPEG2_SS, "Spatially Scalable" },
  2497. { FF_PROFILE_MPEG2_SNR_SCALABLE, "SNR Scalable" },
  2498. { FF_PROFILE_MPEG2_MAIN, "Main" },
  2499. { FF_PROFILE_MPEG2_SIMPLE, "Simple" },
  2500. { FF_PROFILE_RESERVED, "Reserved" },
  2501. { FF_PROFILE_RESERVED, "Reserved" },
  2502. { FF_PROFILE_UNKNOWN },
  2503. };
  2504. AVCodec ff_mpeg1video_decoder = {
  2505. .name = "mpeg1video",
  2506. .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video"),
  2507. .type = AVMEDIA_TYPE_VIDEO,
  2508. .id = AV_CODEC_ID_MPEG1VIDEO,
  2509. .priv_data_size = sizeof(Mpeg1Context),
  2510. .init = mpeg_decode_init,
  2511. .close = mpeg_decode_end,
  2512. .decode = mpeg_decode_frame,
  2513. .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
  2514. CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY |
  2515. CODEC_CAP_SLICE_THREADS,
  2516. .flush = flush,
  2517. .max_lowres = 3,
  2518. .update_thread_context = ONLY_IF_THREADS_ENABLED(mpeg_decode_update_thread_context)
  2519. };
  2520. AVCodec ff_mpeg2video_decoder = {
  2521. .name = "mpeg2video",
  2522. .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 video"),
  2523. .type = AVMEDIA_TYPE_VIDEO,
  2524. .id = AV_CODEC_ID_MPEG2VIDEO,
  2525. .priv_data_size = sizeof(Mpeg1Context),
  2526. .init = mpeg_decode_init,
  2527. .close = mpeg_decode_end,
  2528. .decode = mpeg_decode_frame,
  2529. .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
  2530. CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY |
  2531. CODEC_CAP_SLICE_THREADS,
  2532. .flush = flush,
  2533. .max_lowres = 3,
  2534. .profiles = NULL_IF_CONFIG_SMALL(mpeg2_video_profiles),
  2535. };
  2536. //legacy decoder
  2537. AVCodec ff_mpegvideo_decoder = {
  2538. .name = "mpegvideo",
  2539. .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video"),
  2540. .type = AVMEDIA_TYPE_VIDEO,
  2541. .id = AV_CODEC_ID_MPEG2VIDEO,
  2542. .priv_data_size = sizeof(Mpeg1Context),
  2543. .init = mpeg_decode_init,
  2544. .close = mpeg_decode_end,
  2545. .decode = mpeg_decode_frame,
  2546. .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS,
  2547. .flush = flush,
  2548. .max_lowres = 3,
  2549. };
  2550. #if FF_API_XVMC
  2551. #if CONFIG_MPEG_XVMC_DECODER
  2552. static av_cold int mpeg_mc_decode_init(AVCodecContext *avctx)
  2553. {
  2554. if (avctx->active_thread_type & FF_THREAD_SLICE)
  2555. return -1;
  2556. if (!(avctx->slice_flags & SLICE_FLAG_CODED_ORDER))
  2557. return -1;
  2558. if (!(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD)) {
  2559. av_dlog(avctx, "mpeg12.c: XvMC decoder will work better if SLICE_FLAG_ALLOW_FIELD is set\n");
  2560. }
  2561. mpeg_decode_init(avctx);
  2562. avctx->pix_fmt = AV_PIX_FMT_XVMC_MPEG2_IDCT;
  2563. avctx->xvmc_acceleration = 2; // 2 - the blocks are packed!
  2564. return 0;
  2565. }
  2566. AVCodec ff_mpeg_xvmc_decoder = {
  2567. .name = "mpegvideo_xvmc",
  2568. .long_name = NULL_IF_CONFIG_SMALL("MPEG-1/2 video XvMC (X-Video Motion Compensation)"),
  2569. .type = AVMEDIA_TYPE_VIDEO,
  2570. .id = AV_CODEC_ID_MPEG2VIDEO_XVMC,
  2571. .priv_data_size = sizeof(Mpeg1Context),
  2572. .init = mpeg_mc_decode_init,
  2573. .close = mpeg_decode_end,
  2574. .decode = mpeg_decode_frame,
  2575. .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
  2576. CODEC_CAP_TRUNCATED | CODEC_CAP_HWACCEL | CODEC_CAP_DELAY,
  2577. .flush = flush,
  2578. };
  2579. #endif
  2580. #endif /* FF_API_XVMC */
  2581. #if CONFIG_MPEG_VDPAU_DECODER
  2582. AVCodec ff_mpeg_vdpau_decoder = {
  2583. .name = "mpegvideo_vdpau",
  2584. .long_name = NULL_IF_CONFIG_SMALL("MPEG-1/2 video (VDPAU acceleration)"),
  2585. .type = AVMEDIA_TYPE_VIDEO,
  2586. .id = AV_CODEC_ID_MPEG2VIDEO,
  2587. .priv_data_size = sizeof(Mpeg1Context),
  2588. .init = mpeg_decode_init,
  2589. .close = mpeg_decode_end,
  2590. .decode = mpeg_decode_frame,
  2591. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED |
  2592. CODEC_CAP_HWACCEL_VDPAU | CODEC_CAP_DELAY,
  2593. .flush = flush,
  2594. };
  2595. #endif
  2596. #if CONFIG_MPEG1_VDPAU_DECODER
  2597. AVCodec ff_mpeg1_vdpau_decoder = {
  2598. .name = "mpeg1video_vdpau",
  2599. .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video (VDPAU acceleration)"),
  2600. .type = AVMEDIA_TYPE_VIDEO,
  2601. .id = AV_CODEC_ID_MPEG1VIDEO,
  2602. .priv_data_size = sizeof(Mpeg1Context),
  2603. .init = mpeg_decode_init,
  2604. .close = mpeg_decode_end,
  2605. .decode = mpeg_decode_frame,
  2606. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED |
  2607. CODEC_CAP_HWACCEL_VDPAU | CODEC_CAP_DELAY,
  2608. .flush = flush,
  2609. };
  2610. #endif