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.

2758 lines
100KB

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