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.

2507 lines
90KB

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