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.

2609 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. //#define DEBUG
  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. #endif
  995. AV_PIX_FMT_YUV420P,
  996. AV_PIX_FMT_NONE
  997. };
  998. static const enum AVPixelFormat mpeg2_hwaccel_pixfmt_list_420[] = {
  999. #if CONFIG_MPEG_XVMC_DECODER
  1000. AV_PIX_FMT_XVMC_MPEG2_IDCT,
  1001. AV_PIX_FMT_XVMC_MPEG2_MC,
  1002. #endif
  1003. #if CONFIG_MPEG2_VDPAU_HWACCEL
  1004. AV_PIX_FMT_VDPAU_MPEG2,
  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 inline int uses_vdpau(AVCodecContext *avctx) {
  1016. return avctx->pix_fmt == AV_PIX_FMT_VDPAU_MPEG1 || avctx->pix_fmt == AV_PIX_FMT_VDPAU_MPEG2;
  1017. }
  1018. static enum AVPixelFormat mpeg_get_pixelformat(AVCodecContext *avctx)
  1019. {
  1020. Mpeg1Context *s1 = avctx->priv_data;
  1021. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1022. if(s->chroma_format < 2) {
  1023. return avctx->get_format(avctx,
  1024. avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO ?
  1025. mpeg1_hwaccel_pixfmt_list_420 :
  1026. mpeg2_hwaccel_pixfmt_list_420);
  1027. } else if(s->chroma_format == 2)
  1028. return AV_PIX_FMT_YUV422P;
  1029. else
  1030. return AV_PIX_FMT_YUV444P;
  1031. }
  1032. static void setup_hwaccel_for_pixfmt(AVCodecContext *avctx)
  1033. {
  1034. if (avctx->pix_fmt != AV_PIX_FMT_XVMC_MPEG2_IDCT && avctx->pix_fmt != AV_PIX_FMT_XVMC_MPEG2_MC) {
  1035. avctx->xvmc_acceleration = 0;
  1036. } else if (!avctx->xvmc_acceleration) {
  1037. avctx->xvmc_acceleration = 2;
  1038. }
  1039. avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
  1040. // until then pix_fmt may be changed right after codec init
  1041. if (avctx->pix_fmt == AV_PIX_FMT_XVMC_MPEG2_IDCT ||
  1042. avctx->hwaccel || uses_vdpau(avctx))
  1043. if (avctx->idct_algo == FF_IDCT_AUTO)
  1044. avctx->idct_algo = FF_IDCT_SIMPLE;
  1045. }
  1046. /* Call this function when we know all parameters.
  1047. * It may be called in different places for MPEG-1 and MPEG-2. */
  1048. static int mpeg_decode_postinit(AVCodecContext *avctx)
  1049. {
  1050. Mpeg1Context *s1 = avctx->priv_data;
  1051. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1052. uint8_t old_permutation[64];
  1053. if ((s1->mpeg_enc_ctx_allocated == 0) ||
  1054. avctx->coded_width != s->width ||
  1055. avctx->coded_height != s->height ||
  1056. s1->save_width != s->width ||
  1057. s1->save_height != s->height ||
  1058. s1->save_aspect_info != s->aspect_ratio_info ||
  1059. s1->save_progressive_seq != s->progressive_sequence ||
  1060. 0)
  1061. {
  1062. if (s1->mpeg_enc_ctx_allocated) {
  1063. ParseContext pc = s->parse_context;
  1064. s->parse_context.buffer = 0;
  1065. ff_MPV_common_end(s);
  1066. s->parse_context = pc;
  1067. }
  1068. if ((s->width == 0) || (s->height == 0))
  1069. return -2;
  1070. avcodec_set_dimensions(avctx, s->width, s->height);
  1071. if (avctx->codec_id == AV_CODEC_ID_MPEG2VIDEO && s->bit_rate) {
  1072. avctx->rc_max_rate = s->bit_rate;
  1073. } else if (avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO && s->bit_rate &&
  1074. (s->bit_rate != 0x3FFFF*400 || s->vbv_delay != 0xFFFF)) {
  1075. avctx->bit_rate = s->bit_rate;
  1076. }
  1077. s1->save_aspect_info = s->aspect_ratio_info;
  1078. s1->save_width = s->width;
  1079. s1->save_height = s->height;
  1080. s1->save_progressive_seq = s->progressive_sequence;
  1081. /* low_delay may be forced, in this case we will have B-frames
  1082. * that behave like P-frames. */
  1083. avctx->has_b_frames = !s->low_delay;
  1084. if (avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
  1085. //MPEG-1 fps
  1086. avctx->time_base.den = ff_mpeg12_frame_rate_tab[s->frame_rate_index].num;
  1087. avctx->time_base.num = ff_mpeg12_frame_rate_tab[s->frame_rate_index].den;
  1088. //MPEG-1 aspect
  1089. avctx->sample_aspect_ratio = av_d2q(1.0/ff_mpeg1_aspect[s->aspect_ratio_info], 255);
  1090. avctx->ticks_per_frame=1;
  1091. } else {//MPEG-2
  1092. //MPEG-2 fps
  1093. av_reduce(&s->avctx->time_base.den,
  1094. &s->avctx->time_base.num,
  1095. ff_mpeg12_frame_rate_tab[s->frame_rate_index].num * s1->frame_rate_ext.num*2,
  1096. ff_mpeg12_frame_rate_tab[s->frame_rate_index].den * s1->frame_rate_ext.den,
  1097. 1 << 30);
  1098. avctx->ticks_per_frame = 2;
  1099. //MPEG-2 aspect
  1100. if (s->aspect_ratio_info > 1) {
  1101. AVRational dar =
  1102. av_mul_q(av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
  1103. (AVRational) {s1->pan_scan.width, s1->pan_scan.height}),
  1104. (AVRational) {s->width, s->height});
  1105. // we ignore the spec here and guess a bit as reality does not match the spec, see for example
  1106. // res_change_ffmpeg_aspect.ts and sequence-display-aspect.mpg
  1107. // issue1613, 621, 562
  1108. if ((s1->pan_scan.width == 0) || (s1->pan_scan.height == 0) ||
  1109. (av_cmp_q(dar, (AVRational) {4, 3}) && av_cmp_q(dar, (AVRational) {16, 9}))) {
  1110. s->avctx->sample_aspect_ratio =
  1111. av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
  1112. (AVRational) {s->width, s->height});
  1113. } else {
  1114. s->avctx->sample_aspect_ratio =
  1115. av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
  1116. (AVRational) {s1->pan_scan.width, s1->pan_scan.height});
  1117. //issue1613 4/3 16/9 -> 16/9
  1118. //res_change_ffmpeg_aspect.ts 4/3 225/44 ->4/3
  1119. //widescreen-issue562.mpg 4/3 16/9 -> 16/9
  1120. // s->avctx->sample_aspect_ratio = av_mul_q(s->avctx->sample_aspect_ratio, (AVRational) {s->width, s->height});
  1121. av_dlog(avctx, "A %d/%d\n",
  1122. ff_mpeg2_aspect[s->aspect_ratio_info].num, ff_mpeg2_aspect[s->aspect_ratio_info].den);
  1123. av_dlog(avctx, "B %d/%d\n", s->avctx->sample_aspect_ratio.num,
  1124. s->avctx->sample_aspect_ratio.den);
  1125. }
  1126. } else {
  1127. s->avctx->sample_aspect_ratio =
  1128. ff_mpeg2_aspect[s->aspect_ratio_info];
  1129. }
  1130. } // MPEG-2
  1131. avctx->pix_fmt = mpeg_get_pixelformat(avctx);
  1132. setup_hwaccel_for_pixfmt(avctx);
  1133. /* Quantization matrices may need reordering
  1134. * if DCT permutation is changed. */
  1135. memcpy(old_permutation, s->dsp.idct_permutation, 64 * sizeof(uint8_t));
  1136. if (ff_MPV_common_init(s) < 0)
  1137. return -2;
  1138. quant_matrix_rebuild(s->intra_matrix, old_permutation, s->dsp.idct_permutation);
  1139. quant_matrix_rebuild(s->inter_matrix, old_permutation, s->dsp.idct_permutation);
  1140. quant_matrix_rebuild(s->chroma_intra_matrix, old_permutation, s->dsp.idct_permutation);
  1141. quant_matrix_rebuild(s->chroma_inter_matrix, old_permutation, s->dsp.idct_permutation);
  1142. s1->mpeg_enc_ctx_allocated = 1;
  1143. }
  1144. return 0;
  1145. }
  1146. static int mpeg1_decode_picture(AVCodecContext *avctx,
  1147. const uint8_t *buf, int buf_size)
  1148. {
  1149. Mpeg1Context *s1 = avctx->priv_data;
  1150. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1151. int ref, f_code, vbv_delay;
  1152. init_get_bits(&s->gb, buf, buf_size*8);
  1153. ref = get_bits(&s->gb, 10); /* temporal ref */
  1154. s->pict_type = get_bits(&s->gb, 3);
  1155. if (s->pict_type == 0 || s->pict_type > 3)
  1156. return -1;
  1157. vbv_delay = get_bits(&s->gb, 16);
  1158. s->vbv_delay = vbv_delay;
  1159. if (s->pict_type == AV_PICTURE_TYPE_P || s->pict_type == AV_PICTURE_TYPE_B) {
  1160. s->full_pel[0] = get_bits1(&s->gb);
  1161. f_code = get_bits(&s->gb, 3);
  1162. if (f_code == 0 && (avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT)))
  1163. return -1;
  1164. f_code += !f_code;
  1165. s->mpeg_f_code[0][0] = f_code;
  1166. s->mpeg_f_code[0][1] = f_code;
  1167. }
  1168. if (s->pict_type == AV_PICTURE_TYPE_B) {
  1169. s->full_pel[1] = get_bits1(&s->gb);
  1170. f_code = get_bits(&s->gb, 3);
  1171. if (f_code == 0 && (avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT)))
  1172. return -1;
  1173. f_code += !f_code;
  1174. s->mpeg_f_code[1][0] = f_code;
  1175. s->mpeg_f_code[1][1] = f_code;
  1176. }
  1177. s->current_picture.f.pict_type = s->pict_type;
  1178. s->current_picture.f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
  1179. if (avctx->debug & FF_DEBUG_PICT_INFO)
  1180. av_log(avctx, AV_LOG_DEBUG, "vbv_delay %d, ref %d type:%d\n", vbv_delay, ref, s->pict_type);
  1181. s->y_dc_scale = 8;
  1182. s->c_dc_scale = 8;
  1183. return 0;
  1184. }
  1185. static void mpeg_decode_sequence_extension(Mpeg1Context *s1)
  1186. {
  1187. MpegEncContext *s= &s1->mpeg_enc_ctx;
  1188. int horiz_size_ext, vert_size_ext;
  1189. int bit_rate_ext;
  1190. skip_bits(&s->gb, 1); /* profile and level esc*/
  1191. s->avctx->profile = get_bits(&s->gb, 3);
  1192. s->avctx->level = get_bits(&s->gb, 4);
  1193. s->progressive_sequence = get_bits1(&s->gb); /* progressive_sequence */
  1194. s->chroma_format = get_bits(&s->gb, 2); /* chroma_format 1=420, 2=422, 3=444 */
  1195. horiz_size_ext = get_bits(&s->gb, 2);
  1196. vert_size_ext = get_bits(&s->gb, 2);
  1197. s->width |= (horiz_size_ext << 12);
  1198. s->height |= (vert_size_ext << 12);
  1199. bit_rate_ext = get_bits(&s->gb, 12); /* XXX: handle it */
  1200. s->bit_rate += (bit_rate_ext << 18) * 400;
  1201. skip_bits1(&s->gb); /* marker */
  1202. s->avctx->rc_buffer_size += get_bits(&s->gb, 8) * 1024 * 16 << 10;
  1203. s->low_delay = get_bits1(&s->gb);
  1204. if (s->flags & CODEC_FLAG_LOW_DELAY)
  1205. s->low_delay = 1;
  1206. s1->frame_rate_ext.num = get_bits(&s->gb, 2) + 1;
  1207. s1->frame_rate_ext.den = get_bits(&s->gb, 5) + 1;
  1208. av_dlog(s->avctx, "sequence extension\n");
  1209. s->codec_id = s->avctx->codec_id = AV_CODEC_ID_MPEG2VIDEO;
  1210. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  1211. av_log(s->avctx, AV_LOG_DEBUG, "profile: %d, level: %d vbv buffer: %d, bitrate:%d\n",
  1212. s->avctx->profile, s->avctx->level, s->avctx->rc_buffer_size, s->bit_rate);
  1213. }
  1214. static void mpeg_decode_sequence_display_extension(Mpeg1Context *s1)
  1215. {
  1216. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1217. int color_description, w, h;
  1218. skip_bits(&s->gb, 3); /* video format */
  1219. color_description = get_bits1(&s->gb);
  1220. if (color_description) {
  1221. s->avctx->color_primaries = get_bits(&s->gb, 8);
  1222. s->avctx->color_trc = get_bits(&s->gb, 8);
  1223. s->avctx->colorspace = get_bits(&s->gb, 8);
  1224. }
  1225. w = get_bits(&s->gb, 14);
  1226. skip_bits(&s->gb, 1); //marker
  1227. h = get_bits(&s->gb, 14);
  1228. // remaining 3 bits are zero padding
  1229. s1->pan_scan.width = 16 * w;
  1230. s1->pan_scan.height = 16 * h;
  1231. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  1232. av_log(s->avctx, AV_LOG_DEBUG, "sde w:%d, h:%d\n", w, h);
  1233. }
  1234. static void mpeg_decode_picture_display_extension(Mpeg1Context *s1)
  1235. {
  1236. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1237. int i, nofco;
  1238. nofco = 1;
  1239. if (s->progressive_sequence) {
  1240. if (s->repeat_first_field) {
  1241. nofco++;
  1242. if (s->top_field_first)
  1243. nofco++;
  1244. }
  1245. } else {
  1246. if (s->picture_structure == PICT_FRAME) {
  1247. nofco++;
  1248. if (s->repeat_first_field)
  1249. nofco++;
  1250. }
  1251. }
  1252. for (i = 0; i < nofco; i++) {
  1253. s1->pan_scan.position[i][0] = get_sbits(&s->gb, 16);
  1254. skip_bits(&s->gb, 1); // marker
  1255. s1->pan_scan.position[i][1] = get_sbits(&s->gb, 16);
  1256. skip_bits(&s->gb, 1); // marker
  1257. }
  1258. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  1259. av_log(s->avctx, AV_LOG_DEBUG, "pde (%d,%d) (%d,%d) (%d,%d)\n",
  1260. s1->pan_scan.position[0][0], s1->pan_scan.position[0][1],
  1261. s1->pan_scan.position[1][0], s1->pan_scan.position[1][1],
  1262. s1->pan_scan.position[2][0], s1->pan_scan.position[2][1]);
  1263. }
  1264. static int load_matrix(MpegEncContext *s, uint16_t matrix0[64], uint16_t matrix1[64], int intra)
  1265. {
  1266. int i;
  1267. for (i = 0; i < 64; i++) {
  1268. int j = s->dsp.idct_permutation[ff_zigzag_direct[i]];
  1269. int v = get_bits(&s->gb, 8);
  1270. if (v == 0) {
  1271. av_log(s->avctx, AV_LOG_ERROR, "matrix damaged\n");
  1272. return -1;
  1273. }
  1274. if (intra && i == 0 && v != 8) {
  1275. av_log(s->avctx, AV_LOG_DEBUG, "intra matrix specifies invalid DC quantizer %d, ignoring\n", v);
  1276. v = 8; // needed by pink.mpg / issue1046
  1277. }
  1278. matrix0[j] = v;
  1279. if (matrix1)
  1280. matrix1[j] = v;
  1281. }
  1282. return 0;
  1283. }
  1284. static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
  1285. {
  1286. av_dlog(s->avctx, "matrix extension\n");
  1287. if (get_bits1(&s->gb)) load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
  1288. if (get_bits1(&s->gb)) load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
  1289. if (get_bits1(&s->gb)) load_matrix(s, s->chroma_intra_matrix, NULL , 1);
  1290. if (get_bits1(&s->gb)) load_matrix(s, s->chroma_inter_matrix, NULL , 0);
  1291. }
  1292. static void mpeg_decode_picture_coding_extension(Mpeg1Context *s1)
  1293. {
  1294. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1295. s->full_pel[0] = s->full_pel[1] = 0;
  1296. s->mpeg_f_code[0][0] = get_bits(&s->gb, 4);
  1297. s->mpeg_f_code[0][1] = get_bits(&s->gb, 4);
  1298. s->mpeg_f_code[1][0] = get_bits(&s->gb, 4);
  1299. s->mpeg_f_code[1][1] = get_bits(&s->gb, 4);
  1300. if (!s->pict_type && s1->mpeg_enc_ctx_allocated) {
  1301. av_log(s->avctx, AV_LOG_ERROR, "Missing picture start code, guessing missing values\n");
  1302. if (s->mpeg_f_code[1][0] == 15 && s->mpeg_f_code[1][1] == 15) {
  1303. if (s->mpeg_f_code[0][0] == 15 && s->mpeg_f_code[0][1] == 15)
  1304. s->pict_type = AV_PICTURE_TYPE_I;
  1305. else
  1306. s->pict_type = AV_PICTURE_TYPE_P;
  1307. } else
  1308. s->pict_type = AV_PICTURE_TYPE_B;
  1309. s->current_picture.f.pict_type = s->pict_type;
  1310. s->current_picture.f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
  1311. }
  1312. s->mpeg_f_code[0][0] += !s->mpeg_f_code[0][0];
  1313. s->mpeg_f_code[0][1] += !s->mpeg_f_code[0][1];
  1314. s->mpeg_f_code[1][0] += !s->mpeg_f_code[1][0];
  1315. s->mpeg_f_code[1][1] += !s->mpeg_f_code[1][1];
  1316. s->intra_dc_precision = get_bits(&s->gb, 2);
  1317. s->picture_structure = get_bits(&s->gb, 2);
  1318. s->top_field_first = get_bits1(&s->gb);
  1319. s->frame_pred_frame_dct = get_bits1(&s->gb);
  1320. s->concealment_motion_vectors = get_bits1(&s->gb);
  1321. s->q_scale_type = get_bits1(&s->gb);
  1322. s->intra_vlc_format = get_bits1(&s->gb);
  1323. s->alternate_scan = get_bits1(&s->gb);
  1324. s->repeat_first_field = get_bits1(&s->gb);
  1325. s->chroma_420_type = get_bits1(&s->gb);
  1326. s->progressive_frame = get_bits1(&s->gb);
  1327. if (s->alternate_scan) {
  1328. ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable, ff_alternate_vertical_scan);
  1329. ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable, ff_alternate_vertical_scan);
  1330. } else {
  1331. ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable, ff_zigzag_direct);
  1332. ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable, ff_zigzag_direct);
  1333. }
  1334. /* composite display not parsed */
  1335. av_dlog(s->avctx, "intra_dc_precision=%d\n", s->intra_dc_precision);
  1336. av_dlog(s->avctx, "picture_structure=%d\n", s->picture_structure);
  1337. av_dlog(s->avctx, "top field first=%d\n", s->top_field_first);
  1338. av_dlog(s->avctx, "repeat first field=%d\n", s->repeat_first_field);
  1339. av_dlog(s->avctx, "conceal=%d\n", s->concealment_motion_vectors);
  1340. av_dlog(s->avctx, "intra_vlc_format=%d\n", s->intra_vlc_format);
  1341. av_dlog(s->avctx, "alternate_scan=%d\n", s->alternate_scan);
  1342. av_dlog(s->avctx, "frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
  1343. av_dlog(s->avctx, "progressive_frame=%d\n", s->progressive_frame);
  1344. }
  1345. static int mpeg_field_start(MpegEncContext *s, const uint8_t *buf, int buf_size)
  1346. {
  1347. AVCodecContext *avctx = s->avctx;
  1348. Mpeg1Context *s1 = (Mpeg1Context*)s;
  1349. /* start frame decoding */
  1350. if (s->first_field || s->picture_structure == PICT_FRAME) {
  1351. AVFrameSideData *pan_scan;
  1352. if (ff_MPV_frame_start(s, avctx) < 0)
  1353. return -1;
  1354. ff_mpeg_er_frame_start(s);
  1355. /* first check if we must repeat the frame */
  1356. s->current_picture_ptr->f.repeat_pict = 0;
  1357. if (s->repeat_first_field) {
  1358. if (s->progressive_sequence) {
  1359. if (s->top_field_first)
  1360. s->current_picture_ptr->f.repeat_pict = 4;
  1361. else
  1362. s->current_picture_ptr->f.repeat_pict = 2;
  1363. } else if (s->progressive_frame) {
  1364. s->current_picture_ptr->f.repeat_pict = 1;
  1365. }
  1366. }
  1367. pan_scan = av_frame_new_side_data(&s->current_picture_ptr->f,
  1368. AV_FRAME_DATA_PANSCAN,
  1369. sizeof(s1->pan_scan));
  1370. if (!pan_scan)
  1371. return AVERROR(ENOMEM);
  1372. memcpy(pan_scan->data, &s1->pan_scan, sizeof(s1->pan_scan));
  1373. if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_FRAME))
  1374. ff_thread_finish_setup(avctx);
  1375. } else { // second field
  1376. int i;
  1377. if (!s->current_picture_ptr) {
  1378. av_log(s->avctx, AV_LOG_ERROR, "first field missing\n");
  1379. return -1;
  1380. }
  1381. if (s->avctx->hwaccel &&
  1382. (s->avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD)) {
  1383. if (s->avctx->hwaccel->end_frame(s->avctx) < 0)
  1384. av_log(avctx, AV_LOG_ERROR, "hardware accelerator failed to decode first field\n");
  1385. }
  1386. for (i = 0; i < 4; i++) {
  1387. s->current_picture.f.data[i] = s->current_picture_ptr->f.data[i];
  1388. if (s->picture_structure == PICT_BOTTOM_FIELD) {
  1389. s->current_picture.f.data[i] += s->current_picture_ptr->f.linesize[i];
  1390. }
  1391. }
  1392. }
  1393. if (avctx->hwaccel) {
  1394. if (avctx->hwaccel->start_frame(avctx, buf, buf_size) < 0)
  1395. return -1;
  1396. }
  1397. // MPV_frame_start will call this function too,
  1398. // but we need to call it on every field
  1399. if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
  1400. if (ff_xvmc_field_start(s, avctx) < 0)
  1401. return -1;
  1402. return 0;
  1403. }
  1404. #define DECODE_SLICE_ERROR -1
  1405. #define DECODE_SLICE_OK 0
  1406. /**
  1407. * Decode a slice.
  1408. * MpegEncContext.mb_y must be set to the MB row from the startcode.
  1409. * @return DECODE_SLICE_ERROR if the slice is damaged,
  1410. * DECODE_SLICE_OK if this slice is OK
  1411. */
  1412. static int mpeg_decode_slice(MpegEncContext *s, int mb_y,
  1413. const uint8_t **buf, int buf_size)
  1414. {
  1415. AVCodecContext *avctx = s->avctx;
  1416. const int lowres = s->avctx->lowres;
  1417. const int field_pic = s->picture_structure != PICT_FRAME;
  1418. s->resync_mb_x =
  1419. s->resync_mb_y = -1;
  1420. av_assert0(mb_y < s->mb_height);
  1421. init_get_bits(&s->gb, *buf, buf_size * 8);
  1422. if(s->codec_id != AV_CODEC_ID_MPEG1VIDEO && s->mb_height > 2800/16)
  1423. skip_bits(&s->gb, 3);
  1424. ff_mpeg1_clean_buffers(s);
  1425. s->interlaced_dct = 0;
  1426. s->qscale = get_qscale(s);
  1427. if (s->qscale == 0) {
  1428. av_log(s->avctx, AV_LOG_ERROR, "qscale == 0\n");
  1429. return -1;
  1430. }
  1431. /* extra slice info */
  1432. while (get_bits1(&s->gb) != 0) {
  1433. skip_bits(&s->gb, 8);
  1434. }
  1435. s->mb_x = 0;
  1436. if (mb_y == 0 && s->codec_tag == AV_RL32("SLIF")) {
  1437. skip_bits1(&s->gb);
  1438. } else {
  1439. while (get_bits_left(&s->gb) > 0) {
  1440. int code = get_vlc2(&s->gb, ff_mbincr_vlc.table,
  1441. MBINCR_VLC_BITS, 2);
  1442. if (code < 0) {
  1443. av_log(s->avctx, AV_LOG_ERROR, "first mb_incr damaged\n");
  1444. return -1;
  1445. }
  1446. if (code >= 33) {
  1447. if (code == 33) {
  1448. s->mb_x += 33;
  1449. }
  1450. /* otherwise, stuffing, nothing to do */
  1451. } else {
  1452. s->mb_x += code;
  1453. break;
  1454. }
  1455. }
  1456. }
  1457. if (s->mb_x >= (unsigned)s->mb_width) {
  1458. av_log(s->avctx, AV_LOG_ERROR, "initial skip overflow\n");
  1459. return -1;
  1460. }
  1461. if (avctx->hwaccel) {
  1462. const uint8_t *buf_end, *buf_start = *buf - 4; /* include start_code */
  1463. int start_code = -1;
  1464. buf_end = avpriv_find_start_code(buf_start + 2, *buf + buf_size, &start_code);
  1465. if (buf_end < *buf + buf_size)
  1466. buf_end -= 4;
  1467. s->mb_y = mb_y;
  1468. if (avctx->hwaccel->decode_slice(avctx, buf_start, buf_end - buf_start) < 0)
  1469. return DECODE_SLICE_ERROR;
  1470. *buf = buf_end;
  1471. return DECODE_SLICE_OK;
  1472. }
  1473. s->resync_mb_x = s->mb_x;
  1474. s->resync_mb_y = s->mb_y = mb_y;
  1475. s->mb_skip_run = 0;
  1476. ff_init_block_index(s);
  1477. if (s->mb_y == 0 && s->mb_x == 0 && (s->first_field || s->picture_structure == PICT_FRAME)) {
  1478. if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
  1479. 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",
  1480. 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],
  1481. 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")),
  1482. s->progressive_sequence ? "ps" :"", s->progressive_frame ? "pf" : "", s->alternate_scan ? "alt" :"", s->top_field_first ? "top" :"",
  1483. s->intra_dc_precision, s->picture_structure, s->frame_pred_frame_dct, s->concealment_motion_vectors,
  1484. s->q_scale_type, s->intra_vlc_format, s->repeat_first_field, s->chroma_420_type ? "420" :"");
  1485. }
  1486. }
  1487. for (;;) {
  1488. // If 1, we memcpy blocks in xvmcvideo.
  1489. if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1)
  1490. ff_xvmc_init_block(s); // set s->block
  1491. if (mpeg_decode_mb(s, s->block) < 0)
  1492. return -1;
  1493. if (s->current_picture.motion_val[0] && !s->encoding) { // note motion_val is normally NULL unless we want to extract the MVs
  1494. const int wrap = s->b8_stride;
  1495. int xy = s->mb_x * 2 + s->mb_y * 2 * wrap;
  1496. int b8_xy = 4 * (s->mb_x + s->mb_y * s->mb_stride);
  1497. int motion_x, motion_y, dir, i;
  1498. for (i = 0; i < 2; i++) {
  1499. for (dir = 0; dir < 2; dir++) {
  1500. if (s->mb_intra || (dir == 1 && s->pict_type != AV_PICTURE_TYPE_B)) {
  1501. motion_x = motion_y = 0;
  1502. } else if (s->mv_type == MV_TYPE_16X16 || (s->mv_type == MV_TYPE_FIELD && field_pic)) {
  1503. motion_x = s->mv[dir][0][0];
  1504. motion_y = s->mv[dir][0][1];
  1505. } else /*if ((s->mv_type == MV_TYPE_FIELD) || (s->mv_type == MV_TYPE_16X8))*/ {
  1506. motion_x = s->mv[dir][i][0];
  1507. motion_y = s->mv[dir][i][1];
  1508. }
  1509. s->current_picture.motion_val[dir][xy ][0] = motion_x;
  1510. s->current_picture.motion_val[dir][xy ][1] = motion_y;
  1511. s->current_picture.motion_val[dir][xy + 1][0] = motion_x;
  1512. s->current_picture.motion_val[dir][xy + 1][1] = motion_y;
  1513. s->current_picture.ref_index [dir][b8_xy ] =
  1514. s->current_picture.ref_index [dir][b8_xy + 1] = s->field_select[dir][i];
  1515. av_assert2(s->field_select[dir][i] == 0 || s->field_select[dir][i] == 1);
  1516. }
  1517. xy += wrap;
  1518. b8_xy +=2;
  1519. }
  1520. }
  1521. s->dest[0] += 16 >> lowres;
  1522. s->dest[1] +=(16 >> lowres) >> s->chroma_x_shift;
  1523. s->dest[2] +=(16 >> lowres) >> s->chroma_x_shift;
  1524. ff_MPV_decode_mb(s, s->block);
  1525. if (++s->mb_x >= s->mb_width) {
  1526. const int mb_size = 16 >> s->avctx->lowres;
  1527. ff_mpeg_draw_horiz_band(s, mb_size*(s->mb_y >> field_pic), mb_size);
  1528. ff_MPV_report_decode_progress(s);
  1529. s->mb_x = 0;
  1530. s->mb_y += 1 << field_pic;
  1531. if (s->mb_y >= s->mb_height) {
  1532. int left = get_bits_left(&s->gb);
  1533. int is_d10 = s->chroma_format == 2 && s->pict_type == AV_PICTURE_TYPE_I && avctx->profile == 0 && avctx->level == 5
  1534. && s->intra_dc_precision == 2 && s->q_scale_type == 1 && s->alternate_scan == 0
  1535. && s->progressive_frame == 0 /* vbv_delay == 0xBBB || 0xE10*/;
  1536. if (left >= 32 && !is_d10) {
  1537. GetBitContext gb = s->gb;
  1538. align_get_bits(&gb);
  1539. if (show_bits(&gb, 24) == 0x060E2B) {
  1540. av_log(avctx, AV_LOG_DEBUG, "Invalid MXF data found in video stream\n");
  1541. is_d10 = 1;
  1542. }
  1543. }
  1544. if (left < 0 || (left && show_bits(&s->gb, FFMIN(left, 23)) && !is_d10)
  1545. || ((avctx->err_recognition & (AV_EF_BITSTREAM | AV_EF_AGGRESSIVE)) && left > 8)) {
  1546. av_log(avctx, AV_LOG_ERROR, "end mismatch left=%d %0X\n", left, show_bits(&s->gb, FFMIN(left, 23)));
  1547. return -1;
  1548. } else
  1549. goto eos;
  1550. }
  1551. ff_init_block_index(s);
  1552. }
  1553. /* skip mb handling */
  1554. if (s->mb_skip_run == -1) {
  1555. /* read increment again */
  1556. s->mb_skip_run = 0;
  1557. for (;;) {
  1558. int code = get_vlc2(&s->gb, ff_mbincr_vlc.table,
  1559. MBINCR_VLC_BITS, 2);
  1560. if (code < 0) {
  1561. av_log(s->avctx, AV_LOG_ERROR, "mb incr damaged\n");
  1562. return -1;
  1563. }
  1564. if (code >= 33) {
  1565. if (code == 33) {
  1566. s->mb_skip_run += 33;
  1567. } else if (code == 35) {
  1568. if (s->mb_skip_run != 0 || show_bits(&s->gb, 15) != 0) {
  1569. av_log(s->avctx, AV_LOG_ERROR, "slice mismatch\n");
  1570. return -1;
  1571. }
  1572. goto eos; /* end of slice */
  1573. }
  1574. /* otherwise, stuffing, nothing to do */
  1575. } else {
  1576. s->mb_skip_run += code;
  1577. break;
  1578. }
  1579. }
  1580. if (s->mb_skip_run) {
  1581. int i;
  1582. if (s->pict_type == AV_PICTURE_TYPE_I) {
  1583. av_log(s->avctx, AV_LOG_ERROR, "skipped MB in I frame at %d %d\n", s->mb_x, s->mb_y);
  1584. return -1;
  1585. }
  1586. /* skip mb */
  1587. s->mb_intra = 0;
  1588. for (i = 0; i < 12; i++)
  1589. s->block_last_index[i] = -1;
  1590. if (s->picture_structure == PICT_FRAME)
  1591. s->mv_type = MV_TYPE_16X16;
  1592. else
  1593. s->mv_type = MV_TYPE_FIELD;
  1594. if (s->pict_type == AV_PICTURE_TYPE_P) {
  1595. /* if P type, zero motion vector is implied */
  1596. s->mv_dir = MV_DIR_FORWARD;
  1597. s->mv[0][0][0] = s->mv[0][0][1] = 0;
  1598. s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
  1599. s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
  1600. s->field_select[0][0] = (s->picture_structure - 1) & 1;
  1601. } else {
  1602. /* if B type, reuse previous vectors and directions */
  1603. s->mv[0][0][0] = s->last_mv[0][0][0];
  1604. s->mv[0][0][1] = s->last_mv[0][0][1];
  1605. s->mv[1][0][0] = s->last_mv[1][0][0];
  1606. s->mv[1][0][1] = s->last_mv[1][0][1];
  1607. }
  1608. }
  1609. }
  1610. }
  1611. eos: // end of slice
  1612. *buf += (get_bits_count(&s->gb)-1)/8;
  1613. av_dlog(s, "y %d %d %d %d\n", s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y);
  1614. return 0;
  1615. }
  1616. static int slice_decode_thread(AVCodecContext *c, void *arg)
  1617. {
  1618. MpegEncContext *s = *(void**)arg;
  1619. const uint8_t *buf = s->gb.buffer;
  1620. int mb_y = s->start_mb_y;
  1621. const int field_pic = s->picture_structure != PICT_FRAME;
  1622. s->er.error_count = (3 * (s->end_mb_y - s->start_mb_y) * s->mb_width) >> field_pic;
  1623. for (;;) {
  1624. uint32_t start_code;
  1625. int ret;
  1626. ret = mpeg_decode_slice(s, mb_y, &buf, s->gb.buffer_end - buf);
  1627. emms_c();
  1628. av_dlog(c, "ret:%d resync:%d/%d mb:%d/%d ts:%d/%d ec:%d\n",
  1629. ret, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y,
  1630. s->start_mb_y, s->end_mb_y, s->er.error_count);
  1631. if (ret < 0) {
  1632. if (c->err_recognition & AV_EF_EXPLODE)
  1633. return ret;
  1634. if (s->resync_mb_x >= 0 && s->resync_mb_y >= 0)
  1635. 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);
  1636. } else {
  1637. 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);
  1638. }
  1639. if (s->mb_y == s->end_mb_y)
  1640. return 0;
  1641. start_code = -1;
  1642. buf = avpriv_find_start_code(buf, s->gb.buffer_end, &start_code);
  1643. mb_y= start_code - SLICE_MIN_START_CODE;
  1644. if(s->codec_id != AV_CODEC_ID_MPEG1VIDEO && s->mb_height > 2800/16)
  1645. mb_y += (*buf&0xE0)<<2;
  1646. mb_y <<= field_pic;
  1647. if (s->picture_structure == PICT_BOTTOM_FIELD)
  1648. mb_y++;
  1649. if (mb_y < 0 || mb_y >= s->end_mb_y)
  1650. return -1;
  1651. }
  1652. }
  1653. /**
  1654. * Handle slice ends.
  1655. * @return 1 if it seems to be the last slice
  1656. */
  1657. static int slice_end(AVCodecContext *avctx, AVFrame *pict)
  1658. {
  1659. Mpeg1Context *s1 = avctx->priv_data;
  1660. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1661. if (!s1->mpeg_enc_ctx_allocated || !s->current_picture_ptr)
  1662. return 0;
  1663. if (s->avctx->hwaccel) {
  1664. if (s->avctx->hwaccel->end_frame(s->avctx) < 0)
  1665. av_log(avctx, AV_LOG_ERROR, "hardware accelerator failed to decode picture\n");
  1666. }
  1667. if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
  1668. ff_xvmc_field_end(s);
  1669. /* end of slice reached */
  1670. if (/*s->mb_y << field_pic == s->mb_height &&*/ !s->first_field && !s->first_slice) {
  1671. /* end of image */
  1672. ff_er_frame_end(&s->er);
  1673. ff_MPV_frame_end(s);
  1674. if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
  1675. int ret = av_frame_ref(pict, &s->current_picture_ptr->f);
  1676. if (ret < 0)
  1677. return ret;
  1678. ff_print_debug_info(s, s->current_picture_ptr, pict);
  1679. ff_mpv_export_qp_table(s, pict, s->current_picture_ptr, FF_QSCALE_TYPE_MPEG2);
  1680. } else {
  1681. if (avctx->active_thread_type & FF_THREAD_FRAME)
  1682. s->picture_number++;
  1683. /* latency of 1 frame for I- and P-frames */
  1684. /* XXX: use another variable than picture_number */
  1685. if (s->last_picture_ptr != NULL) {
  1686. int ret = av_frame_ref(pict, &s->last_picture_ptr->f);
  1687. if (ret < 0)
  1688. return ret;
  1689. ff_print_debug_info(s, s->last_picture_ptr, pict);
  1690. ff_mpv_export_qp_table(s, pict, s->last_picture_ptr, FF_QSCALE_TYPE_MPEG2);
  1691. }
  1692. }
  1693. return 1;
  1694. } else {
  1695. return 0;
  1696. }
  1697. }
  1698. static int mpeg1_decode_sequence(AVCodecContext *avctx,
  1699. const uint8_t *buf, int buf_size)
  1700. {
  1701. Mpeg1Context *s1 = avctx->priv_data;
  1702. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1703. int width, height;
  1704. int i, v, j;
  1705. init_get_bits(&s->gb, buf, buf_size*8);
  1706. width = get_bits(&s->gb, 12);
  1707. height = get_bits(&s->gb, 12);
  1708. if (width == 0 || height == 0) {
  1709. av_log(avctx, AV_LOG_WARNING, "Invalid horizontal or vertical size "
  1710. "value.\n");
  1711. if (avctx->err_recognition & (AV_EF_BITSTREAM | AV_EF_COMPLIANT))
  1712. return AVERROR_INVALIDDATA;
  1713. }
  1714. s->aspect_ratio_info = get_bits(&s->gb, 4);
  1715. if (s->aspect_ratio_info == 0) {
  1716. av_log(avctx, AV_LOG_ERROR, "aspect ratio has forbidden 0 value\n");
  1717. if (avctx->err_recognition & (AV_EF_BITSTREAM | AV_EF_COMPLIANT))
  1718. return -1;
  1719. }
  1720. s->frame_rate_index = get_bits(&s->gb, 4);
  1721. if (s->frame_rate_index == 0 || s->frame_rate_index > 13)
  1722. return -1;
  1723. s->bit_rate = get_bits(&s->gb, 18) * 400;
  1724. if (get_bits1(&s->gb) == 0) /* marker */
  1725. return -1;
  1726. s->width = width;
  1727. s->height = height;
  1728. s->avctx->rc_buffer_size = get_bits(&s->gb, 10) * 1024 * 16;
  1729. skip_bits(&s->gb, 1);
  1730. /* get matrix */
  1731. if (get_bits1(&s->gb)) {
  1732. load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
  1733. } else {
  1734. for (i = 0; i < 64; i++) {
  1735. j = s->dsp.idct_permutation[i];
  1736. v = ff_mpeg1_default_intra_matrix[i];
  1737. s->intra_matrix[j] = v;
  1738. s->chroma_intra_matrix[j] = v;
  1739. }
  1740. }
  1741. if (get_bits1(&s->gb)) {
  1742. load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
  1743. } else {
  1744. for (i = 0; i < 64; i++) {
  1745. int j = s->dsp.idct_permutation[i];
  1746. v = ff_mpeg1_default_non_intra_matrix[i];
  1747. s->inter_matrix[j] = v;
  1748. s->chroma_inter_matrix[j] = v;
  1749. }
  1750. }
  1751. if (show_bits(&s->gb, 23) != 0) {
  1752. av_log(s->avctx, AV_LOG_ERROR, "sequence header damaged\n");
  1753. return -1;
  1754. }
  1755. /* we set MPEG-2 parameters so that it emulates MPEG-1 */
  1756. s->progressive_sequence = 1;
  1757. s->progressive_frame = 1;
  1758. s->picture_structure = PICT_FRAME;
  1759. s->first_field = 0;
  1760. s->frame_pred_frame_dct = 1;
  1761. s->chroma_format = 1;
  1762. s->codec_id = s->avctx->codec_id = AV_CODEC_ID_MPEG1VIDEO;
  1763. s->out_format = FMT_MPEG1;
  1764. s->swap_uv = 0; // AFAIK VCR2 does not have SEQ_HEADER
  1765. if (s->flags & CODEC_FLAG_LOW_DELAY)
  1766. s->low_delay = 1;
  1767. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  1768. av_log(s->avctx, AV_LOG_DEBUG, "vbv buffer: %d, bitrate:%d\n",
  1769. s->avctx->rc_buffer_size, s->bit_rate);
  1770. return 0;
  1771. }
  1772. static int vcr2_init_sequence(AVCodecContext *avctx)
  1773. {
  1774. Mpeg1Context *s1 = avctx->priv_data;
  1775. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1776. int i, v;
  1777. /* start new MPEG-1 context decoding */
  1778. s->out_format = FMT_MPEG1;
  1779. if (s1->mpeg_enc_ctx_allocated) {
  1780. ff_MPV_common_end(s);
  1781. }
  1782. s->width = avctx->coded_width;
  1783. s->height = avctx->coded_height;
  1784. avctx->has_b_frames = 0; // true?
  1785. s->low_delay = 1;
  1786. avctx->pix_fmt = mpeg_get_pixelformat(avctx);
  1787. setup_hwaccel_for_pixfmt(avctx);
  1788. if (ff_MPV_common_init(s) < 0)
  1789. return -1;
  1790. s1->mpeg_enc_ctx_allocated = 1;
  1791. for (i = 0; i < 64; i++) {
  1792. int j = s->dsp.idct_permutation[i];
  1793. v = ff_mpeg1_default_intra_matrix[i];
  1794. s->intra_matrix[j] = v;
  1795. s->chroma_intra_matrix[j] = v;
  1796. v = ff_mpeg1_default_non_intra_matrix[i];
  1797. s->inter_matrix[j] = v;
  1798. s->chroma_inter_matrix[j] = v;
  1799. }
  1800. s->progressive_sequence = 1;
  1801. s->progressive_frame = 1;
  1802. s->picture_structure = PICT_FRAME;
  1803. s->first_field = 0;
  1804. s->frame_pred_frame_dct = 1;
  1805. s->chroma_format = 1;
  1806. if (s->codec_tag == AV_RL32("BW10")) {
  1807. s->codec_id = s->avctx->codec_id = AV_CODEC_ID_MPEG1VIDEO;
  1808. } else {
  1809. exchange_uv(s); // common init reset pblocks, so we swap them here
  1810. s->swap_uv = 1; // in case of xvmc we need to swap uv for each MB
  1811. s->codec_id = s->avctx->codec_id = AV_CODEC_ID_MPEG2VIDEO;
  1812. }
  1813. s1->save_width = s->width;
  1814. s1->save_height = s->height;
  1815. s1->save_progressive_seq = s->progressive_sequence;
  1816. return 0;
  1817. }
  1818. static void mpeg_decode_user_data(AVCodecContext *avctx,
  1819. const uint8_t *p, int buf_size)
  1820. {
  1821. Mpeg1Context *s = avctx->priv_data;
  1822. const uint8_t *buf_end = p + buf_size;
  1823. if(buf_size > 29){
  1824. int i;
  1825. for(i=0; i<20; i++)
  1826. if(!memcmp(p+i, "\0TMPGEXS\0", 9)){
  1827. s->tmpgexs= 1;
  1828. }
  1829. /* for(i=0; !(!p[i-2] && !p[i-1] && p[i]==1) && i<buf_size; i++){
  1830. av_log(avctx, AV_LOG_ERROR, "%c", p[i]);
  1831. }
  1832. av_log(avctx, AV_LOG_ERROR, "\n");*/
  1833. }
  1834. /* we parse the DTG active format information */
  1835. if (buf_end - p >= 5 &&
  1836. p[0] == 'D' && p[1] == 'T' && p[2] == 'G' && p[3] == '1') {
  1837. int flags = p[4];
  1838. p += 5;
  1839. if (flags & 0x80) {
  1840. /* skip event id */
  1841. p += 2;
  1842. }
  1843. if (flags & 0x40) {
  1844. if (buf_end - p < 1)
  1845. return;
  1846. avctx->dtg_active_format = p[0] & 0x0f;
  1847. }
  1848. }
  1849. }
  1850. static void mpeg_decode_gop(AVCodecContext *avctx,
  1851. const uint8_t *buf, int buf_size)
  1852. {
  1853. Mpeg1Context *s1 = avctx->priv_data;
  1854. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1855. int broken_link;
  1856. int64_t tc;
  1857. init_get_bits(&s->gb, buf, buf_size*8);
  1858. tc = avctx->timecode_frame_start = get_bits(&s->gb, 25);
  1859. s->closed_gop = get_bits1(&s->gb);
  1860. /*broken_link indicate that after editing the
  1861. reference frames of the first B-Frames after GOP I-Frame
  1862. are missing (open gop)*/
  1863. broken_link = get_bits1(&s->gb);
  1864. if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
  1865. char tcbuf[AV_TIMECODE_STR_SIZE];
  1866. av_timecode_make_mpeg_tc_string(tcbuf, tc);
  1867. av_log(s->avctx, AV_LOG_DEBUG,
  1868. "GOP (%s) closed_gop=%d broken_link=%d\n",
  1869. tcbuf, s->closed_gop, broken_link);
  1870. }
  1871. }
  1872. static int decode_chunks(AVCodecContext *avctx,
  1873. AVFrame *picture, int *got_output,
  1874. const uint8_t *buf, int buf_size)
  1875. {
  1876. Mpeg1Context *s = avctx->priv_data;
  1877. MpegEncContext *s2 = &s->mpeg_enc_ctx;
  1878. const uint8_t *buf_ptr = buf;
  1879. const uint8_t *buf_end = buf + buf_size;
  1880. int ret, input_size;
  1881. int last_code = 0;
  1882. int picture_start_code_seen = 0;
  1883. for (;;) {
  1884. /* find next start code */
  1885. uint32_t start_code = -1;
  1886. buf_ptr = avpriv_find_start_code(buf_ptr, buf_end, &start_code);
  1887. if (start_code > 0x1ff) {
  1888. if (s2->pict_type != AV_PICTURE_TYPE_B || avctx->skip_frame <= AVDISCARD_DEFAULT) {
  1889. if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE)) {
  1890. int i;
  1891. av_assert0(avctx->thread_count > 1);
  1892. avctx->execute(avctx, slice_decode_thread, &s2->thread_context[0], NULL, s->slice_count, sizeof(void*));
  1893. for (i = 0; i < s->slice_count; i++)
  1894. s2->er.error_count += s2->thread_context[i]->er.error_count;
  1895. }
  1896. if ((CONFIG_MPEG_VDPAU_DECODER || CONFIG_MPEG1_VDPAU_DECODER)
  1897. && uses_vdpau(avctx))
  1898. ff_vdpau_mpeg_picture_complete(s2, buf, buf_size, s->slice_count);
  1899. ret = slice_end(avctx, picture);
  1900. if (ret < 0)
  1901. return ret;
  1902. else if (ret) {
  1903. if (s2->last_picture_ptr || s2->low_delay) //FIXME merge with the stuff in mpeg_decode_slice
  1904. *got_output = 1;
  1905. }
  1906. }
  1907. s2->pict_type = 0;
  1908. return FFMAX(0, buf_ptr - buf - s2->parse_context.last_index);
  1909. }
  1910. input_size = buf_end - buf_ptr;
  1911. if (avctx->debug & FF_DEBUG_STARTCODE) {
  1912. av_log(avctx, AV_LOG_DEBUG, "%3X at %td left %d\n", start_code, buf_ptr-buf, input_size);
  1913. }
  1914. /* prepare data for next start code */
  1915. switch (start_code) {
  1916. case SEQ_START_CODE:
  1917. if (last_code == 0) {
  1918. mpeg1_decode_sequence(avctx, buf_ptr, input_size);
  1919. if(buf != avctx->extradata)
  1920. s->sync=1;
  1921. } else {
  1922. av_log(avctx, AV_LOG_ERROR, "ignoring SEQ_START_CODE after %X\n", last_code);
  1923. if (avctx->err_recognition & AV_EF_EXPLODE)
  1924. return AVERROR_INVALIDDATA;
  1925. }
  1926. break;
  1927. case PICTURE_START_CODE:
  1928. if (picture_start_code_seen && s2->picture_structure == PICT_FRAME) {
  1929. /* If it's a frame picture, there can't be more than one picture header.
  1930. Yet, it does happen and we need to handle it. */
  1931. av_log(avctx, AV_LOG_WARNING, "ignoring extra picture following a frame-picture\n");
  1932. break;
  1933. }
  1934. picture_start_code_seen = 1;
  1935. if (s2->width <= 0 || s2->height <= 0) {
  1936. av_log(avctx, AV_LOG_ERROR, "Invalid frame dimensions %dx%d.\n",
  1937. s2->width, s2->height);
  1938. return AVERROR_INVALIDDATA;
  1939. }
  1940. if(s->tmpgexs){
  1941. s2->intra_dc_precision= 3;
  1942. s2->intra_matrix[0]= 1;
  1943. }
  1944. if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE) && s->slice_count) {
  1945. int i;
  1946. avctx->execute(avctx, slice_decode_thread,
  1947. s2->thread_context, NULL,
  1948. s->slice_count, sizeof(void*));
  1949. for (i = 0; i < s->slice_count; i++)
  1950. s2->er.error_count += s2->thread_context[i]->er.error_count;
  1951. s->slice_count = 0;
  1952. }
  1953. if (last_code == 0 || last_code == SLICE_MIN_START_CODE) {
  1954. ret = mpeg_decode_postinit(avctx);
  1955. if (ret < 0) {
  1956. av_log(avctx, AV_LOG_ERROR, "mpeg_decode_postinit() failure\n");
  1957. return ret;
  1958. }
  1959. /* we have a complete image: we try to decompress it */
  1960. if (mpeg1_decode_picture(avctx, buf_ptr, input_size) < 0)
  1961. s2->pict_type = 0;
  1962. s2->first_slice = 1;
  1963. last_code = PICTURE_START_CODE;
  1964. } else {
  1965. av_log(avctx, AV_LOG_ERROR, "ignoring pic after %X\n", last_code);
  1966. if (avctx->err_recognition & AV_EF_EXPLODE)
  1967. return AVERROR_INVALIDDATA;
  1968. }
  1969. break;
  1970. case EXT_START_CODE:
  1971. init_get_bits(&s2->gb, buf_ptr, input_size*8);
  1972. switch (get_bits(&s2->gb, 4)) {
  1973. case 0x1:
  1974. if (last_code == 0) {
  1975. mpeg_decode_sequence_extension(s);
  1976. } else {
  1977. av_log(avctx, AV_LOG_ERROR, "ignoring seq ext after %X\n", last_code);
  1978. if (avctx->err_recognition & AV_EF_EXPLODE)
  1979. return AVERROR_INVALIDDATA;
  1980. }
  1981. break;
  1982. case 0x2:
  1983. mpeg_decode_sequence_display_extension(s);
  1984. break;
  1985. case 0x3:
  1986. mpeg_decode_quant_matrix_extension(s2);
  1987. break;
  1988. case 0x7:
  1989. mpeg_decode_picture_display_extension(s);
  1990. break;
  1991. case 0x8:
  1992. if (last_code == PICTURE_START_CODE) {
  1993. mpeg_decode_picture_coding_extension(s);
  1994. } else {
  1995. av_log(avctx, AV_LOG_ERROR, "ignoring pic cod ext after %X\n", last_code);
  1996. if (avctx->err_recognition & AV_EF_EXPLODE)
  1997. return AVERROR_INVALIDDATA;
  1998. }
  1999. break;
  2000. }
  2001. break;
  2002. case USER_START_CODE:
  2003. mpeg_decode_user_data(avctx, buf_ptr, input_size);
  2004. break;
  2005. case GOP_START_CODE:
  2006. if (last_code == 0) {
  2007. s2->first_field=0;
  2008. mpeg_decode_gop(avctx, buf_ptr, input_size);
  2009. s->sync=1;
  2010. } else {
  2011. av_log(avctx, AV_LOG_ERROR, "ignoring GOP_START_CODE after %X\n", last_code);
  2012. if (avctx->err_recognition & AV_EF_EXPLODE)
  2013. return AVERROR_INVALIDDATA;
  2014. }
  2015. break;
  2016. default:
  2017. if (start_code >= SLICE_MIN_START_CODE &&
  2018. start_code <= SLICE_MAX_START_CODE && last_code == PICTURE_START_CODE) {
  2019. if (s2->progressive_sequence && !s2->progressive_frame) {
  2020. s2->progressive_frame = 1;
  2021. av_log(s2->avctx, AV_LOG_ERROR, "interlaced frame in progressive sequence, ignoring\n");
  2022. }
  2023. if (s2->picture_structure == 0 || (s2->progressive_frame && s2->picture_structure != PICT_FRAME)) {
  2024. av_log(s2->avctx, AV_LOG_ERROR, "picture_structure %d invalid, ignoring\n", s2->picture_structure);
  2025. s2->picture_structure = PICT_FRAME;
  2026. }
  2027. if (s2->progressive_sequence && !s2->frame_pred_frame_dct) {
  2028. av_log(s2->avctx, AV_LOG_WARNING, "invalid frame_pred_frame_dct\n");
  2029. }
  2030. if (s2->picture_structure == PICT_FRAME) {
  2031. s2->first_field = 0;
  2032. s2->v_edge_pos = 16 * s2->mb_height;
  2033. } else {
  2034. s2->first_field ^= 1;
  2035. s2->v_edge_pos = 8 * s2->mb_height;
  2036. memset(s2->mbskip_table, 0, s2->mb_stride * s2->mb_height);
  2037. }
  2038. }
  2039. if (start_code >= SLICE_MIN_START_CODE &&
  2040. start_code <= SLICE_MAX_START_CODE && last_code != 0) {
  2041. const int field_pic = s2->picture_structure != PICT_FRAME;
  2042. int mb_y = start_code - SLICE_MIN_START_CODE;
  2043. last_code = SLICE_MIN_START_CODE;
  2044. if(s2->codec_id != AV_CODEC_ID_MPEG1VIDEO && s2->mb_height > 2800/16)
  2045. mb_y += (*buf_ptr&0xE0)<<2;
  2046. mb_y <<= field_pic;
  2047. if (s2->picture_structure == PICT_BOTTOM_FIELD)
  2048. mb_y++;
  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. break;
  2058. }
  2059. }
  2060. if (s2->pict_type == AV_PICTURE_TYPE_I || (s2->flags2 & CODEC_FLAG2_SHOW_ALL))
  2061. s->sync=1;
  2062. if (s2->next_picture_ptr == NULL) {
  2063. /* Skip P-frames if we do not have a reference frame or we have an invalid header. */
  2064. if (s2->pict_type == AV_PICTURE_TYPE_P && !s->sync) break;
  2065. }
  2066. if ((avctx->skip_frame >= AVDISCARD_NONREF && s2->pict_type == AV_PICTURE_TYPE_B) ||
  2067. (avctx->skip_frame >= AVDISCARD_NONKEY && s2->pict_type != AV_PICTURE_TYPE_I) ||
  2068. avctx->skip_frame >= AVDISCARD_ALL)
  2069. break;
  2070. if (!s->mpeg_enc_ctx_allocated)
  2071. break;
  2072. if (s2->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
  2073. if (mb_y < avctx->skip_top || mb_y >= s2->mb_height - avctx->skip_bottom)
  2074. break;
  2075. }
  2076. if (!s2->pict_type) {
  2077. av_log(avctx, AV_LOG_ERROR, "Missing picture start code\n");
  2078. if (avctx->err_recognition & AV_EF_EXPLODE)
  2079. return AVERROR_INVALIDDATA;
  2080. break;
  2081. }
  2082. if (s2->first_slice) {
  2083. s2->first_slice = 0;
  2084. if (mpeg_field_start(s2, buf, buf_size) < 0)
  2085. return -1;
  2086. }
  2087. if (!s2->current_picture_ptr) {
  2088. av_log(avctx, AV_LOG_ERROR, "current_picture not initialized\n");
  2089. return AVERROR_INVALIDDATA;
  2090. }
  2091. if (uses_vdpau(avctx)) {
  2092. s->slice_count++;
  2093. break;
  2094. }
  2095. if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE)) {
  2096. int threshold = (s2->mb_height * s->slice_count +
  2097. s2->slice_context_count / 2) /
  2098. s2->slice_context_count;
  2099. av_assert0(avctx->thread_count > 1);
  2100. if (threshold <= mb_y) {
  2101. MpegEncContext *thread_context = s2->thread_context[s->slice_count];
  2102. thread_context->start_mb_y = mb_y;
  2103. thread_context->end_mb_y = s2->mb_height;
  2104. if (s->slice_count) {
  2105. s2->thread_context[s->slice_count-1]->end_mb_y = mb_y;
  2106. ret = ff_update_duplicate_context(thread_context,
  2107. s2);
  2108. if (ret < 0)
  2109. return ret;
  2110. }
  2111. init_get_bits(&thread_context->gb, buf_ptr, input_size*8);
  2112. s->slice_count++;
  2113. }
  2114. buf_ptr += 2; // FIXME add minimum number of bytes per slice
  2115. } else {
  2116. ret = mpeg_decode_slice(s2, mb_y, &buf_ptr, input_size);
  2117. emms_c();
  2118. if (ret < 0) {
  2119. if (avctx->err_recognition & AV_EF_EXPLODE)
  2120. return ret;
  2121. if (s2->resync_mb_x >= 0 && s2->resync_mb_y >= 0)
  2122. 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);
  2123. } else {
  2124. 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);
  2125. }
  2126. }
  2127. }
  2128. break;
  2129. }
  2130. }
  2131. }
  2132. static int mpeg_decode_frame(AVCodecContext *avctx,
  2133. void *data, int *got_output,
  2134. AVPacket *avpkt)
  2135. {
  2136. const uint8_t *buf = avpkt->data;
  2137. int ret;
  2138. int buf_size = avpkt->size;
  2139. Mpeg1Context *s = avctx->priv_data;
  2140. AVFrame *picture = data;
  2141. MpegEncContext *s2 = &s->mpeg_enc_ctx;
  2142. av_dlog(avctx, "fill_buffer\n");
  2143. if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == SEQ_END_CODE)) {
  2144. /* special case for last picture */
  2145. if (s2->low_delay == 0 && s2->next_picture_ptr) {
  2146. int ret = av_frame_ref(picture, &s2->next_picture_ptr->f);
  2147. if (ret < 0)
  2148. return ret;
  2149. s2->next_picture_ptr = NULL;
  2150. *got_output = 1;
  2151. }
  2152. return buf_size;
  2153. }
  2154. if (s2->flags & CODEC_FLAG_TRUNCATED) {
  2155. int next = ff_mpeg1_find_frame_end(&s2->parse_context, buf, buf_size, NULL);
  2156. if (ff_combine_frame(&s2->parse_context, next, (const uint8_t **)&buf, &buf_size) < 0)
  2157. return buf_size;
  2158. }
  2159. s2->codec_tag = avpriv_toupper4(avctx->codec_tag);
  2160. if (s->mpeg_enc_ctx_allocated == 0 && ( s2->codec_tag == AV_RL32("VCR2")
  2161. || s2->codec_tag == AV_RL32("BW10")
  2162. ))
  2163. vcr2_init_sequence(avctx);
  2164. s->slice_count = 0;
  2165. if (avctx->extradata && !s->extradata_decoded) {
  2166. ret = decode_chunks(avctx, picture, got_output, avctx->extradata, avctx->extradata_size);
  2167. if(*got_output) {
  2168. av_log(avctx, AV_LOG_ERROR, "picture in extradata\n");
  2169. *got_output = 0;
  2170. }
  2171. s->extradata_decoded = 1;
  2172. if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE)) {
  2173. s2->current_picture_ptr = NULL;
  2174. return ret;
  2175. }
  2176. }
  2177. ret = decode_chunks(avctx, picture, got_output, buf, buf_size);
  2178. if (ret<0 || *got_output)
  2179. s2->current_picture_ptr = NULL;
  2180. return ret;
  2181. }
  2182. static void flush(AVCodecContext *avctx)
  2183. {
  2184. Mpeg1Context *s = avctx->priv_data;
  2185. s->sync=0;
  2186. ff_mpeg_flush(avctx);
  2187. }
  2188. static int mpeg_decode_end(AVCodecContext *avctx)
  2189. {
  2190. Mpeg1Context *s = avctx->priv_data;
  2191. if (s->mpeg_enc_ctx_allocated)
  2192. ff_MPV_common_end(&s->mpeg_enc_ctx);
  2193. return 0;
  2194. }
  2195. static const AVProfile mpeg2_video_profiles[] = {
  2196. { FF_PROFILE_MPEG2_422, "4:2:2" },
  2197. { FF_PROFILE_MPEG2_HIGH, "High" },
  2198. { FF_PROFILE_MPEG2_SS, "Spatially Scalable" },
  2199. { FF_PROFILE_MPEG2_SNR_SCALABLE, "SNR Scalable" },
  2200. { FF_PROFILE_MPEG2_MAIN, "Main" },
  2201. { FF_PROFILE_MPEG2_SIMPLE, "Simple" },
  2202. { FF_PROFILE_RESERVED, "Reserved" },
  2203. { FF_PROFILE_RESERVED, "Reserved" },
  2204. { FF_PROFILE_UNKNOWN },
  2205. };
  2206. AVCodec ff_mpeg1video_decoder = {
  2207. .name = "mpeg1video",
  2208. .type = AVMEDIA_TYPE_VIDEO,
  2209. .id = AV_CODEC_ID_MPEG1VIDEO,
  2210. .priv_data_size = sizeof(Mpeg1Context),
  2211. .init = mpeg_decode_init,
  2212. .close = mpeg_decode_end,
  2213. .decode = mpeg_decode_frame,
  2214. .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
  2215. CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY |
  2216. CODEC_CAP_SLICE_THREADS,
  2217. .flush = flush,
  2218. .max_lowres = 3,
  2219. .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video"),
  2220. .update_thread_context = ONLY_IF_THREADS_ENABLED(mpeg_decode_update_thread_context)
  2221. };
  2222. AVCodec ff_mpeg2video_decoder = {
  2223. .name = "mpeg2video",
  2224. .type = AVMEDIA_TYPE_VIDEO,
  2225. .id = AV_CODEC_ID_MPEG2VIDEO,
  2226. .priv_data_size = sizeof(Mpeg1Context),
  2227. .init = mpeg_decode_init,
  2228. .close = mpeg_decode_end,
  2229. .decode = mpeg_decode_frame,
  2230. .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
  2231. CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY |
  2232. CODEC_CAP_SLICE_THREADS,
  2233. .flush = flush,
  2234. .max_lowres = 3,
  2235. .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 video"),
  2236. .profiles = NULL_IF_CONFIG_SMALL(mpeg2_video_profiles),
  2237. };
  2238. //legacy decoder
  2239. AVCodec ff_mpegvideo_decoder = {
  2240. .name = "mpegvideo",
  2241. .type = AVMEDIA_TYPE_VIDEO,
  2242. .id = AV_CODEC_ID_MPEG2VIDEO,
  2243. .priv_data_size = sizeof(Mpeg1Context),
  2244. .init = mpeg_decode_init,
  2245. .close = mpeg_decode_end,
  2246. .decode = mpeg_decode_frame,
  2247. .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS,
  2248. .flush = flush,
  2249. .max_lowres = 3,
  2250. .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video"),
  2251. };
  2252. #if CONFIG_MPEG_XVMC_DECODER
  2253. static av_cold int mpeg_mc_decode_init(AVCodecContext *avctx)
  2254. {
  2255. if (avctx->active_thread_type & FF_THREAD_SLICE)
  2256. return -1;
  2257. if (!(avctx->slice_flags & SLICE_FLAG_CODED_ORDER))
  2258. return -1;
  2259. if (!(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD)) {
  2260. av_dlog(avctx, "mpeg12.c: XvMC decoder will work better if SLICE_FLAG_ALLOW_FIELD is set\n");
  2261. }
  2262. mpeg_decode_init(avctx);
  2263. avctx->pix_fmt = AV_PIX_FMT_XVMC_MPEG2_IDCT;
  2264. avctx->xvmc_acceleration = 2; // 2 - the blocks are packed!
  2265. return 0;
  2266. }
  2267. AVCodec ff_mpeg_xvmc_decoder = {
  2268. .name = "mpegvideo_xvmc",
  2269. .type = AVMEDIA_TYPE_VIDEO,
  2270. .id = AV_CODEC_ID_MPEG2VIDEO_XVMC,
  2271. .priv_data_size = sizeof(Mpeg1Context),
  2272. .init = mpeg_mc_decode_init,
  2273. .close = mpeg_decode_end,
  2274. .decode = mpeg_decode_frame,
  2275. .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
  2276. CODEC_CAP_TRUNCATED| CODEC_CAP_HWACCEL | CODEC_CAP_DELAY,
  2277. .flush = flush,
  2278. .long_name = NULL_IF_CONFIG_SMALL("MPEG-1/2 video XvMC (X-Video Motion Compensation)"),
  2279. };
  2280. #endif
  2281. #if CONFIG_MPEG_VDPAU_DECODER
  2282. AVCodec ff_mpeg_vdpau_decoder = {
  2283. .name = "mpegvideo_vdpau",
  2284. .type = AVMEDIA_TYPE_VIDEO,
  2285. .id = AV_CODEC_ID_MPEG2VIDEO,
  2286. .priv_data_size = sizeof(Mpeg1Context),
  2287. .init = mpeg_decode_init,
  2288. .close = mpeg_decode_end,
  2289. .decode = mpeg_decode_frame,
  2290. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED |
  2291. CODEC_CAP_HWACCEL_VDPAU | CODEC_CAP_DELAY,
  2292. .flush = flush,
  2293. .long_name = NULL_IF_CONFIG_SMALL("MPEG-1/2 video (VDPAU acceleration)"),
  2294. };
  2295. #endif
  2296. #if CONFIG_MPEG1_VDPAU_DECODER
  2297. AVCodec ff_mpeg1_vdpau_decoder = {
  2298. .name = "mpeg1video_vdpau",
  2299. .type = AVMEDIA_TYPE_VIDEO,
  2300. .id = AV_CODEC_ID_MPEG1VIDEO,
  2301. .priv_data_size = sizeof(Mpeg1Context),
  2302. .init = mpeg_decode_init,
  2303. .close = mpeg_decode_end,
  2304. .decode = mpeg_decode_frame,
  2305. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED |
  2306. CODEC_CAP_HWACCEL_VDPAU | CODEC_CAP_DELAY,
  2307. .flush = flush,
  2308. .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video (VDPAU acceleration)"),
  2309. };
  2310. #endif