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.

2941 lines
106KB

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