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.

2803 lines
101KB

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