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.

2611 lines
95KB

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