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.

2974 lines
107KB

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