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.

2579 lines
94KB

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