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.

2977 lines
107KB

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