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.

2488 lines
89KB

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