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.

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