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.

2699 lines
97KB

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