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.

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