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.

2657 lines
96KB

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