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.

2468 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. typedef struct Mpeg1Context {
  39. MpegEncContext mpeg_enc_ctx;
  40. int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
  41. int repeat_field; /* true if we must repeat the field */
  42. AVPanScan pan_scan; /**< some temporary storage for the panscan */
  43. int slice_count;
  44. int swap_uv;//indicate VCR2
  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. static void exchange_uv(MpegEncContext *s)
  592. {
  593. int16_t (*tmp)[64];
  594. tmp = s->pblocks[4];
  595. s->pblocks[4] = s->pblocks[5];
  596. s->pblocks[5] = tmp;
  597. }
  598. /* motion type (for MPEG-2) */
  599. #define MT_FIELD 1
  600. #define MT_FRAME 2
  601. #define MT_16X8 2
  602. #define MT_DMV 3
  603. static int mpeg_decode_mb(MpegEncContext *s, int16_t block[12][64])
  604. {
  605. int i, j, k, cbp, val, mb_type, motion_type;
  606. const int mb_block_count = 4 + (1 << s->chroma_format);
  607. av_dlog(s->avctx, "decode_mb: x=%d y=%d\n", s->mb_x, s->mb_y);
  608. assert(s->mb_skipped == 0);
  609. if (s->mb_skip_run-- != 0) {
  610. if (s->pict_type == AV_PICTURE_TYPE_P) {
  611. s->mb_skipped = 1;
  612. s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride] = MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;
  613. } else {
  614. int mb_type;
  615. if (s->mb_x)
  616. mb_type = s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride - 1];
  617. else
  618. 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
  619. if (IS_INTRA(mb_type))
  620. return -1;
  621. s->current_picture.mb_type[s->mb_x + s->mb_y*s->mb_stride] =
  622. mb_type | MB_TYPE_SKIP;
  623. // assert(s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride - 1] & (MB_TYPE_16x16 | MB_TYPE_16x8));
  624. if ((s->mv[0][0][0] | s->mv[0][0][1] | s->mv[1][0][0] | s->mv[1][0][1]) == 0)
  625. s->mb_skipped = 1;
  626. }
  627. return 0;
  628. }
  629. switch (s->pict_type) {
  630. default:
  631. case AV_PICTURE_TYPE_I:
  632. if (get_bits1(&s->gb) == 0) {
  633. if (get_bits1(&s->gb) == 0) {
  634. av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in I Frame at %d %d\n", s->mb_x, s->mb_y);
  635. return -1;
  636. }
  637. mb_type = MB_TYPE_QUANT | MB_TYPE_INTRA;
  638. } else {
  639. mb_type = MB_TYPE_INTRA;
  640. }
  641. break;
  642. case AV_PICTURE_TYPE_P:
  643. mb_type = get_vlc2(&s->gb, ff_mb_ptype_vlc.table, MB_PTYPE_VLC_BITS, 1);
  644. if (mb_type < 0) {
  645. av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in P Frame at %d %d\n", s->mb_x, s->mb_y);
  646. return -1;
  647. }
  648. mb_type = ptype2mb_type[mb_type];
  649. break;
  650. case AV_PICTURE_TYPE_B:
  651. mb_type = get_vlc2(&s->gb, ff_mb_btype_vlc.table, MB_BTYPE_VLC_BITS, 1);
  652. if (mb_type < 0) {
  653. av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in B Frame at %d %d\n", s->mb_x, s->mb_y);
  654. return -1;
  655. }
  656. mb_type = btype2mb_type[mb_type];
  657. break;
  658. }
  659. av_dlog(s->avctx, "mb_type=%x\n", mb_type);
  660. // motion_type = 0; /* avoid warning */
  661. if (IS_INTRA(mb_type)) {
  662. s->dsp.clear_blocks(s->block[0]);
  663. if (!s->chroma_y_shift) {
  664. s->dsp.clear_blocks(s->block[6]);
  665. }
  666. /* compute DCT type */
  667. if (s->picture_structure == PICT_FRAME && // FIXME add an interlaced_dct coded var?
  668. !s->frame_pred_frame_dct) {
  669. s->interlaced_dct = get_bits1(&s->gb);
  670. }
  671. if (IS_QUANT(mb_type))
  672. s->qscale = get_qscale(s);
  673. if (s->concealment_motion_vectors) {
  674. /* just parse them */
  675. if (s->picture_structure != PICT_FRAME)
  676. skip_bits1(&s->gb); /* field select */
  677. s->mv[0][0][0]= s->last_mv[0][0][0]= s->last_mv[0][1][0] =
  678. mpeg_decode_motion(s, s->mpeg_f_code[0][0], s->last_mv[0][0][0]);
  679. s->mv[0][0][1]= s->last_mv[0][0][1]= s->last_mv[0][1][1] =
  680. mpeg_decode_motion(s, s->mpeg_f_code[0][1], s->last_mv[0][0][1]);
  681. skip_bits1(&s->gb); /* marker */
  682. } else
  683. memset(s->last_mv, 0, sizeof(s->last_mv)); /* reset mv prediction */
  684. s->mb_intra = 1;
  685. // if 1, we memcpy blocks in xvmcvideo
  686. if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1) {
  687. ff_xvmc_pack_pblocks(s, -1); // inter are always full blocks
  688. if (s->swap_uv) {
  689. exchange_uv(s);
  690. }
  691. }
  692. if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
  693. if (s->flags2 & CODEC_FLAG2_FAST) {
  694. for (i = 0; i < 6; i++) {
  695. mpeg2_fast_decode_block_intra(s, *s->pblocks[i], i);
  696. }
  697. } else {
  698. for (i = 0; i < mb_block_count; i++) {
  699. if (mpeg2_decode_block_intra(s, *s->pblocks[i], i) < 0)
  700. return -1;
  701. }
  702. }
  703. } else {
  704. for (i = 0; i < 6; i++) {
  705. if (mpeg1_decode_block_intra(s, *s->pblocks[i], i) < 0)
  706. return -1;
  707. }
  708. }
  709. } else {
  710. if (mb_type & MB_TYPE_ZERO_MV) {
  711. assert(mb_type & MB_TYPE_CBP);
  712. s->mv_dir = MV_DIR_FORWARD;
  713. if (s->picture_structure == PICT_FRAME) {
  714. if (!s->frame_pred_frame_dct)
  715. s->interlaced_dct = get_bits1(&s->gb);
  716. s->mv_type = MV_TYPE_16X16;
  717. } else {
  718. s->mv_type = MV_TYPE_FIELD;
  719. mb_type |= MB_TYPE_INTERLACED;
  720. s->field_select[0][0] = s->picture_structure - 1;
  721. }
  722. if (IS_QUANT(mb_type))
  723. s->qscale = get_qscale(s);
  724. s->last_mv[0][0][0] = 0;
  725. s->last_mv[0][0][1] = 0;
  726. s->last_mv[0][1][0] = 0;
  727. s->last_mv[0][1][1] = 0;
  728. s->mv[0][0][0] = 0;
  729. s->mv[0][0][1] = 0;
  730. } else {
  731. assert(mb_type & MB_TYPE_L0L1);
  732. // FIXME decide if MBs in field pictures are MB_TYPE_INTERLACED
  733. /* get additional motion vector type */
  734. if (s->frame_pred_frame_dct)
  735. motion_type = MT_FRAME;
  736. else {
  737. motion_type = get_bits(&s->gb, 2);
  738. if (s->picture_structure == PICT_FRAME && HAS_CBP(mb_type))
  739. s->interlaced_dct = get_bits1(&s->gb);
  740. }
  741. if (IS_QUANT(mb_type))
  742. s->qscale = get_qscale(s);
  743. /* motion vectors */
  744. s->mv_dir = (mb_type >> 13) & 3;
  745. av_dlog(s->avctx, "motion_type=%d\n", motion_type);
  746. switch (motion_type) {
  747. case MT_FRAME: /* or MT_16X8 */
  748. if (s->picture_structure == PICT_FRAME) {
  749. mb_type |= MB_TYPE_16x16;
  750. s->mv_type = MV_TYPE_16X16;
  751. for (i = 0; i < 2; i++) {
  752. if (USES_LIST(mb_type, i)) {
  753. /* MT_FRAME */
  754. s->mv[i][0][0]= s->last_mv[i][0][0]= s->last_mv[i][1][0] =
  755. mpeg_decode_motion(s, s->mpeg_f_code[i][0], s->last_mv[i][0][0]);
  756. s->mv[i][0][1]= s->last_mv[i][0][1]= s->last_mv[i][1][1] =
  757. mpeg_decode_motion(s, s->mpeg_f_code[i][1], s->last_mv[i][0][1]);
  758. /* full_pel: only for MPEG-1 */
  759. if (s->full_pel[i]) {
  760. s->mv[i][0][0] <<= 1;
  761. s->mv[i][0][1] <<= 1;
  762. }
  763. }
  764. }
  765. } else {
  766. mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
  767. s->mv_type = MV_TYPE_16X8;
  768. for (i = 0; i < 2; i++) {
  769. if (USES_LIST(mb_type, i)) {
  770. /* MT_16X8 */
  771. for (j = 0; j < 2; j++) {
  772. s->field_select[i][j] = get_bits1(&s->gb);
  773. for (k = 0; k < 2; k++) {
  774. val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
  775. s->last_mv[i][j][k]);
  776. s->last_mv[i][j][k] = val;
  777. s->mv[i][j][k] = val;
  778. }
  779. }
  780. }
  781. }
  782. }
  783. break;
  784. case MT_FIELD:
  785. s->mv_type = MV_TYPE_FIELD;
  786. if (s->picture_structure == PICT_FRAME) {
  787. mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
  788. for (i = 0; i < 2; i++) {
  789. if (USES_LIST(mb_type, i)) {
  790. for (j = 0; j < 2; j++) {
  791. s->field_select[i][j] = get_bits1(&s->gb);
  792. val = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
  793. s->last_mv[i][j][0]);
  794. s->last_mv[i][j][0] = val;
  795. s->mv[i][j][0] = val;
  796. av_dlog(s->avctx, "fmx=%d\n", val);
  797. val = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
  798. s->last_mv[i][j][1] >> 1);
  799. s->last_mv[i][j][1] = val << 1;
  800. s->mv[i][j][1] = val;
  801. av_dlog(s->avctx, "fmy=%d\n", val);
  802. }
  803. }
  804. }
  805. } else {
  806. mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
  807. for (i = 0; i < 2; i++) {
  808. if (USES_LIST(mb_type, i)) {
  809. s->field_select[i][0] = get_bits1(&s->gb);
  810. for (k = 0; k < 2; k++) {
  811. val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
  812. s->last_mv[i][0][k]);
  813. s->last_mv[i][0][k] = val;
  814. s->last_mv[i][1][k] = val;
  815. s->mv[i][0][k] = val;
  816. }
  817. }
  818. }
  819. }
  820. break;
  821. case MT_DMV:
  822. s->mv_type = MV_TYPE_DMV;
  823. for (i = 0; i < 2; i++) {
  824. if (USES_LIST(mb_type, i)) {
  825. int dmx, dmy, mx, my, m;
  826. const int my_shift = s->picture_structure == PICT_FRAME;
  827. mx = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
  828. s->last_mv[i][0][0]);
  829. s->last_mv[i][0][0] = mx;
  830. s->last_mv[i][1][0] = mx;
  831. dmx = get_dmv(s);
  832. my = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
  833. s->last_mv[i][0][1] >> my_shift);
  834. dmy = get_dmv(s);
  835. s->last_mv[i][0][1] = my << my_shift;
  836. s->last_mv[i][1][1] = my << my_shift;
  837. s->mv[i][0][0] = mx;
  838. s->mv[i][0][1] = my;
  839. s->mv[i][1][0] = mx; // not used
  840. s->mv[i][1][1] = my; // not used
  841. if (s->picture_structure == PICT_FRAME) {
  842. mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
  843. // m = 1 + 2 * s->top_field_first;
  844. m = s->top_field_first ? 1 : 3;
  845. /* top -> top pred */
  846. s->mv[i][2][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
  847. s->mv[i][2][1] = ((my * m + (my > 0)) >> 1) + dmy - 1;
  848. m = 4 - m;
  849. s->mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
  850. s->mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1;
  851. } else {
  852. mb_type |= MB_TYPE_16x16;
  853. s->mv[i][2][0] = ((mx + (mx > 0)) >> 1) + dmx;
  854. s->mv[i][2][1] = ((my + (my > 0)) >> 1) + dmy;
  855. if (s->picture_structure == PICT_TOP_FIELD)
  856. s->mv[i][2][1]--;
  857. else
  858. s->mv[i][2][1]++;
  859. }
  860. }
  861. }
  862. break;
  863. default:
  864. av_log(s->avctx, AV_LOG_ERROR, "00 motion_type at %d %d\n", s->mb_x, s->mb_y);
  865. return -1;
  866. }
  867. }
  868. s->mb_intra = 0;
  869. if (HAS_CBP(mb_type)) {
  870. s->dsp.clear_blocks(s->block[0]);
  871. cbp = get_vlc2(&s->gb, ff_mb_pat_vlc.table, MB_PAT_VLC_BITS, 1);
  872. if (mb_block_count > 6) {
  873. cbp <<= mb_block_count - 6;
  874. cbp |= get_bits(&s->gb, mb_block_count - 6);
  875. s->dsp.clear_blocks(s->block[6]);
  876. }
  877. if (cbp <= 0) {
  878. av_log(s->avctx, AV_LOG_ERROR, "invalid cbp at %d %d\n", s->mb_x, s->mb_y);
  879. return -1;
  880. }
  881. //if 1, we memcpy blocks in xvmcvideo
  882. if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1) {
  883. ff_xvmc_pack_pblocks(s, cbp);
  884. if (s->swap_uv) {
  885. exchange_uv(s);
  886. }
  887. }
  888. if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
  889. if (s->flags2 & CODEC_FLAG2_FAST) {
  890. for (i = 0; i < 6; i++) {
  891. if (cbp & 32) {
  892. mpeg2_fast_decode_block_non_intra(s, *s->pblocks[i], i);
  893. } else {
  894. s->block_last_index[i] = -1;
  895. }
  896. cbp += cbp;
  897. }
  898. } else {
  899. cbp <<= 12-mb_block_count;
  900. for (i = 0; i < mb_block_count; i++) {
  901. if (cbp & (1 << 11)) {
  902. if (mpeg2_decode_block_non_intra(s, *s->pblocks[i], i) < 0)
  903. return -1;
  904. } else {
  905. s->block_last_index[i] = -1;
  906. }
  907. cbp += cbp;
  908. }
  909. }
  910. } else {
  911. if (s->flags2 & CODEC_FLAG2_FAST) {
  912. for (i = 0; i < 6; i++) {
  913. if (cbp & 32) {
  914. mpeg1_fast_decode_block_inter(s, *s->pblocks[i], i);
  915. } else {
  916. s->block_last_index[i] = -1;
  917. }
  918. cbp += cbp;
  919. }
  920. } else {
  921. for (i = 0; i < 6; i++) {
  922. if (cbp & 32) {
  923. if (mpeg1_decode_block_inter(s, *s->pblocks[i], i) < 0)
  924. return -1;
  925. } else {
  926. s->block_last_index[i] = -1;
  927. }
  928. cbp += cbp;
  929. }
  930. }
  931. }
  932. } else {
  933. for (i = 0; i < 12; i++)
  934. s->block_last_index[i] = -1;
  935. }
  936. }
  937. s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride] = mb_type;
  938. return 0;
  939. }
  940. static av_cold int mpeg_decode_init(AVCodecContext *avctx)
  941. {
  942. Mpeg1Context *s = avctx->priv_data;
  943. MpegEncContext *s2 = &s->mpeg_enc_ctx;
  944. int i;
  945. /* we need some permutation to store matrices,
  946. * until MPV_common_init() sets the real permutation. */
  947. for (i = 0; i < 64; i++)
  948. s2->dsp.idct_permutation[i]=i;
  949. ff_MPV_decode_defaults(s2);
  950. s->mpeg_enc_ctx.avctx = avctx;
  951. s->mpeg_enc_ctx.flags = avctx->flags;
  952. s->mpeg_enc_ctx.flags2 = avctx->flags2;
  953. ff_mpeg12_common_init(&s->mpeg_enc_ctx);
  954. ff_mpeg12_init_vlcs();
  955. s->mpeg_enc_ctx_allocated = 0;
  956. s->mpeg_enc_ctx.picture_number = 0;
  957. s->repeat_field = 0;
  958. s->mpeg_enc_ctx.codec_id = avctx->codec->id;
  959. avctx->color_range = AVCOL_RANGE_MPEG;
  960. if (avctx->codec->id == AV_CODEC_ID_MPEG1VIDEO)
  961. avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
  962. else
  963. avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
  964. return 0;
  965. }
  966. static int mpeg_decode_update_thread_context(AVCodecContext *avctx, const AVCodecContext *avctx_from)
  967. {
  968. Mpeg1Context *ctx = avctx->priv_data, *ctx_from = avctx_from->priv_data;
  969. MpegEncContext *s = &ctx->mpeg_enc_ctx, *s1 = &ctx_from->mpeg_enc_ctx;
  970. int err;
  971. if (avctx == avctx_from || !ctx_from->mpeg_enc_ctx_allocated || !s1->context_initialized)
  972. return 0;
  973. err = ff_mpeg_update_thread_context(avctx, avctx_from);
  974. if (err) return err;
  975. if (!ctx->mpeg_enc_ctx_allocated)
  976. memcpy(s + 1, s1 + 1, sizeof(Mpeg1Context) - sizeof(MpegEncContext));
  977. if (!(s->pict_type == AV_PICTURE_TYPE_B || s->low_delay))
  978. s->picture_number++;
  979. return 0;
  980. }
  981. static void quant_matrix_rebuild(uint16_t *matrix, const uint8_t *old_perm,
  982. const uint8_t *new_perm)
  983. {
  984. uint16_t temp_matrix[64];
  985. int i;
  986. memcpy(temp_matrix, matrix, 64 * sizeof(uint16_t));
  987. for (i = 0; i < 64; i++) {
  988. matrix[new_perm[i]] = temp_matrix[old_perm[i]];
  989. }
  990. }
  991. static const enum AVPixelFormat pixfmt_xvmc_mpg2_420[] = {
  992. AV_PIX_FMT_XVMC_MPEG2_IDCT,
  993. AV_PIX_FMT_XVMC_MPEG2_MC,
  994. AV_PIX_FMT_NONE };
  995. static const enum AVPixelFormat mpeg12_hwaccel_pixfmt_list_420[] = {
  996. #if CONFIG_MPEG2_DXVA2_HWACCEL
  997. AV_PIX_FMT_DXVA2_VLD,
  998. #endif
  999. #if CONFIG_MPEG2_VAAPI_HWACCEL
  1000. AV_PIX_FMT_VAAPI_VLD,
  1001. #endif
  1002. #if CONFIG_MPEG1_VDPAU_HWACCEL | CONFIG_MPEG2_VDPAU_HWACCEL
  1003. AV_PIX_FMT_VDPAU,
  1004. #endif
  1005. AV_PIX_FMT_YUV420P,
  1006. AV_PIX_FMT_NONE
  1007. };
  1008. static enum AVPixelFormat mpeg_get_pixelformat(AVCodecContext *avctx)
  1009. {
  1010. Mpeg1Context *s1 = avctx->priv_data;
  1011. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1012. if (avctx->xvmc_acceleration)
  1013. return avctx->get_format(avctx, pixfmt_xvmc_mpg2_420);
  1014. else {
  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. }
  1023. /* Call this function when we know all parameters.
  1024. * It may be called in different places for MPEG-1 and MPEG-2. */
  1025. static int mpeg_decode_postinit(AVCodecContext *avctx)
  1026. {
  1027. Mpeg1Context *s1 = avctx->priv_data;
  1028. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1029. uint8_t old_permutation[64];
  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. avcodec_set_dimensions(avctx, s->width, s->height);
  1048. avctx->bit_rate = s->bit_rate;
  1049. s1->save_aspect_info = s->aspect_ratio_info;
  1050. s1->save_width = s->width;
  1051. s1->save_height = s->height;
  1052. s1->save_progressive_seq = s->progressive_sequence;
  1053. /* low_delay may be forced, in this case we will have B-frames
  1054. * that behave like P-frames. */
  1055. avctx->has_b_frames = !s->low_delay;
  1056. if (avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
  1057. //MPEG-1 fps
  1058. avctx->time_base.den = ff_mpeg12_frame_rate_tab[s->frame_rate_index].num;
  1059. avctx->time_base.num = ff_mpeg12_frame_rate_tab[s->frame_rate_index].den;
  1060. //MPEG-1 aspect
  1061. avctx->sample_aspect_ratio = av_d2q(1.0/ff_mpeg1_aspect[s->aspect_ratio_info], 255);
  1062. avctx->ticks_per_frame=1;
  1063. } else {//MPEG-2
  1064. //MPEG-2 fps
  1065. av_reduce(&s->avctx->time_base.den,
  1066. &s->avctx->time_base.num,
  1067. ff_mpeg12_frame_rate_tab[s->frame_rate_index].num * s1->frame_rate_ext.num*2,
  1068. ff_mpeg12_frame_rate_tab[s->frame_rate_index].den * s1->frame_rate_ext.den,
  1069. 1 << 30);
  1070. avctx->ticks_per_frame = 2;
  1071. //MPEG-2 aspect
  1072. if (s->aspect_ratio_info > 1) {
  1073. AVRational dar =
  1074. av_mul_q(av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
  1075. (AVRational) {s1->pan_scan.width, s1->pan_scan.height}),
  1076. (AVRational) {s->width, s->height});
  1077. // we ignore the spec here and guess a bit as reality does not match the spec, see for example
  1078. // res_change_ffmpeg_aspect.ts and sequence-display-aspect.mpg
  1079. // issue1613, 621, 562
  1080. if ((s1->pan_scan.width == 0) || (s1->pan_scan.height == 0) ||
  1081. (av_cmp_q(dar, (AVRational) {4, 3}) && av_cmp_q(dar, (AVRational) {16, 9}))) {
  1082. s->avctx->sample_aspect_ratio =
  1083. av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
  1084. (AVRational) {s->width, s->height});
  1085. } else {
  1086. s->avctx->sample_aspect_ratio =
  1087. av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
  1088. (AVRational) {s1->pan_scan.width, s1->pan_scan.height});
  1089. //issue1613 4/3 16/9 -> 16/9
  1090. //res_change_ffmpeg_aspect.ts 4/3 225/44 ->4/3
  1091. //widescreen-issue562.mpg 4/3 16/9 -> 16/9
  1092. // s->avctx->sample_aspect_ratio = av_mul_q(s->avctx->sample_aspect_ratio, (AVRational) {s->width, s->height});
  1093. av_dlog(avctx, "A %d/%d\n",
  1094. ff_mpeg2_aspect[s->aspect_ratio_info].num, ff_mpeg2_aspect[s->aspect_ratio_info].den);
  1095. av_dlog(avctx, "B %d/%d\n", s->avctx->sample_aspect_ratio.num,
  1096. s->avctx->sample_aspect_ratio.den);
  1097. }
  1098. } else {
  1099. s->avctx->sample_aspect_ratio =
  1100. ff_mpeg2_aspect[s->aspect_ratio_info];
  1101. }
  1102. } // MPEG-2
  1103. avctx->pix_fmt = mpeg_get_pixelformat(avctx);
  1104. avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
  1105. // until then pix_fmt may be changed right after codec init
  1106. if (avctx->pix_fmt == AV_PIX_FMT_XVMC_MPEG2_IDCT ||
  1107. avctx->hwaccel)
  1108. if (avctx->idct_algo == FF_IDCT_AUTO)
  1109. avctx->idct_algo = FF_IDCT_SIMPLE;
  1110. /* Quantization matrices may need reordering
  1111. * if DCT permutation is changed. */
  1112. memcpy(old_permutation, s->dsp.idct_permutation, 64 * sizeof(uint8_t));
  1113. if (ff_MPV_common_init(s) < 0)
  1114. return -2;
  1115. quant_matrix_rebuild(s->intra_matrix, old_permutation, s->dsp.idct_permutation);
  1116. quant_matrix_rebuild(s->inter_matrix, old_permutation, s->dsp.idct_permutation);
  1117. quant_matrix_rebuild(s->chroma_intra_matrix, old_permutation, s->dsp.idct_permutation);
  1118. quant_matrix_rebuild(s->chroma_inter_matrix, old_permutation, s->dsp.idct_permutation);
  1119. s1->mpeg_enc_ctx_allocated = 1;
  1120. }
  1121. return 0;
  1122. }
  1123. static int mpeg1_decode_picture(AVCodecContext *avctx,
  1124. const uint8_t *buf, int buf_size)
  1125. {
  1126. Mpeg1Context *s1 = avctx->priv_data;
  1127. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1128. int ref, f_code, vbv_delay;
  1129. init_get_bits(&s->gb, buf, buf_size*8);
  1130. ref = get_bits(&s->gb, 10); /* temporal ref */
  1131. s->pict_type = get_bits(&s->gb, 3);
  1132. if (s->pict_type == 0 || s->pict_type > 3)
  1133. return -1;
  1134. vbv_delay = get_bits(&s->gb, 16);
  1135. if (s->pict_type == AV_PICTURE_TYPE_P || s->pict_type == AV_PICTURE_TYPE_B) {
  1136. s->full_pel[0] = get_bits1(&s->gb);
  1137. f_code = get_bits(&s->gb, 3);
  1138. if (f_code == 0 && (avctx->err_recognition & AV_EF_BITSTREAM))
  1139. return -1;
  1140. s->mpeg_f_code[0][0] = f_code;
  1141. s->mpeg_f_code[0][1] = f_code;
  1142. }
  1143. if (s->pict_type == AV_PICTURE_TYPE_B) {
  1144. s->full_pel[1] = get_bits1(&s->gb);
  1145. f_code = get_bits(&s->gb, 3);
  1146. if (f_code == 0 && (avctx->err_recognition & AV_EF_BITSTREAM))
  1147. return -1;
  1148. s->mpeg_f_code[1][0] = f_code;
  1149. s->mpeg_f_code[1][1] = f_code;
  1150. }
  1151. s->current_picture.f.pict_type = s->pict_type;
  1152. s->current_picture.f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
  1153. if (avctx->debug & FF_DEBUG_PICT_INFO)
  1154. av_log(avctx, AV_LOG_DEBUG, "vbv_delay %d, ref %d type:%d\n", vbv_delay, ref, s->pict_type);
  1155. s->y_dc_scale = 8;
  1156. s->c_dc_scale = 8;
  1157. return 0;
  1158. }
  1159. static void mpeg_decode_sequence_extension(Mpeg1Context *s1)
  1160. {
  1161. MpegEncContext *s= &s1->mpeg_enc_ctx;
  1162. int horiz_size_ext, vert_size_ext;
  1163. int bit_rate_ext;
  1164. skip_bits(&s->gb, 1); /* profile and level esc*/
  1165. s->avctx->profile = get_bits(&s->gb, 3);
  1166. s->avctx->level = get_bits(&s->gb, 4);
  1167. s->progressive_sequence = get_bits1(&s->gb); /* progressive_sequence */
  1168. s->chroma_format = get_bits(&s->gb, 2); /* chroma_format 1=420, 2=422, 3=444 */
  1169. horiz_size_ext = get_bits(&s->gb, 2);
  1170. vert_size_ext = get_bits(&s->gb, 2);
  1171. s->width |= (horiz_size_ext << 12);
  1172. s->height |= (vert_size_ext << 12);
  1173. bit_rate_ext = get_bits(&s->gb, 12); /* XXX: handle it */
  1174. s->bit_rate += (bit_rate_ext << 18) * 400;
  1175. skip_bits1(&s->gb); /* marker */
  1176. s->avctx->rc_buffer_size += get_bits(&s->gb, 8) * 1024 * 16 << 10;
  1177. s->low_delay = get_bits1(&s->gb);
  1178. if (s->flags & CODEC_FLAG_LOW_DELAY)
  1179. s->low_delay = 1;
  1180. s1->frame_rate_ext.num = get_bits(&s->gb, 2) + 1;
  1181. s1->frame_rate_ext.den = get_bits(&s->gb, 5) + 1;
  1182. av_dlog(s->avctx, "sequence extension\n");
  1183. s->codec_id = s->avctx->codec_id = AV_CODEC_ID_MPEG2VIDEO;
  1184. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  1185. av_log(s->avctx, AV_LOG_DEBUG, "profile: %d, level: %d vbv buffer: %d, bitrate:%d\n",
  1186. s->avctx->profile, s->avctx->level, s->avctx->rc_buffer_size, s->bit_rate);
  1187. }
  1188. static void mpeg_decode_sequence_display_extension(Mpeg1Context *s1)
  1189. {
  1190. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1191. int color_description, w, h;
  1192. skip_bits(&s->gb, 3); /* video format */
  1193. color_description = get_bits1(&s->gb);
  1194. if (color_description) {
  1195. s->avctx->color_primaries = get_bits(&s->gb, 8);
  1196. s->avctx->color_trc = get_bits(&s->gb, 8);
  1197. s->avctx->colorspace = get_bits(&s->gb, 8);
  1198. }
  1199. w = get_bits(&s->gb, 14);
  1200. skip_bits(&s->gb, 1); //marker
  1201. h = get_bits(&s->gb, 14);
  1202. // remaining 3 bits are zero padding
  1203. s1->pan_scan.width = 16 * w;
  1204. s1->pan_scan.height = 16 * h;
  1205. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  1206. av_log(s->avctx, AV_LOG_DEBUG, "sde w:%d, h:%d\n", w, h);
  1207. }
  1208. static void mpeg_decode_picture_display_extension(Mpeg1Context *s1)
  1209. {
  1210. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1211. int i, nofco;
  1212. nofco = 1;
  1213. if (s->progressive_sequence) {
  1214. if (s->repeat_first_field) {
  1215. nofco++;
  1216. if (s->top_field_first)
  1217. nofco++;
  1218. }
  1219. } else {
  1220. if (s->picture_structure == PICT_FRAME) {
  1221. nofco++;
  1222. if (s->repeat_first_field)
  1223. nofco++;
  1224. }
  1225. }
  1226. for (i = 0; i < nofco; i++) {
  1227. s1->pan_scan.position[i][0] = get_sbits(&s->gb, 16);
  1228. skip_bits(&s->gb, 1); // marker
  1229. s1->pan_scan.position[i][1] = get_sbits(&s->gb, 16);
  1230. skip_bits(&s->gb, 1); // marker
  1231. }
  1232. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  1233. av_log(s->avctx, AV_LOG_DEBUG, "pde (%d,%d) (%d,%d) (%d,%d)\n",
  1234. s1->pan_scan.position[0][0], s1->pan_scan.position[0][1],
  1235. s1->pan_scan.position[1][0], s1->pan_scan.position[1][1],
  1236. s1->pan_scan.position[2][0], s1->pan_scan.position[2][1]);
  1237. }
  1238. static int load_matrix(MpegEncContext *s, uint16_t matrix0[64], uint16_t matrix1[64], int intra)
  1239. {
  1240. int i;
  1241. for (i = 0; i < 64; i++) {
  1242. int j = s->dsp.idct_permutation[ff_zigzag_direct[i]];
  1243. int v = get_bits(&s->gb, 8);
  1244. if (v == 0) {
  1245. av_log(s->avctx, AV_LOG_ERROR, "matrix damaged\n");
  1246. return -1;
  1247. }
  1248. if (intra && i == 0 && v != 8) {
  1249. av_log(s->avctx, AV_LOG_ERROR, "intra matrix invalid, ignoring\n");
  1250. v = 8; // needed by pink.mpg / issue1046
  1251. }
  1252. matrix0[j] = v;
  1253. if (matrix1)
  1254. matrix1[j] = v;
  1255. }
  1256. return 0;
  1257. }
  1258. static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
  1259. {
  1260. av_dlog(s->avctx, "matrix extension\n");
  1261. if (get_bits1(&s->gb)) load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
  1262. if (get_bits1(&s->gb)) load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
  1263. if (get_bits1(&s->gb)) load_matrix(s, s->chroma_intra_matrix, NULL , 1);
  1264. if (get_bits1(&s->gb)) load_matrix(s, s->chroma_inter_matrix, NULL , 0);
  1265. }
  1266. static void mpeg_decode_picture_coding_extension(Mpeg1Context *s1)
  1267. {
  1268. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1269. s->full_pel[0] = s->full_pel[1] = 0;
  1270. s->mpeg_f_code[0][0] = get_bits(&s->gb, 4);
  1271. s->mpeg_f_code[0][1] = get_bits(&s->gb, 4);
  1272. s->mpeg_f_code[1][0] = get_bits(&s->gb, 4);
  1273. s->mpeg_f_code[1][1] = get_bits(&s->gb, 4);
  1274. if (!s->pict_type && s1->mpeg_enc_ctx_allocated) {
  1275. av_log(s->avctx, AV_LOG_ERROR, "Missing picture start code, guessing missing values\n");
  1276. if (s->mpeg_f_code[1][0] == 15 && s->mpeg_f_code[1][1] == 15) {
  1277. if (s->mpeg_f_code[0][0] == 15 && s->mpeg_f_code[0][1] == 15)
  1278. s->pict_type = AV_PICTURE_TYPE_I;
  1279. else
  1280. s->pict_type = AV_PICTURE_TYPE_P;
  1281. } else
  1282. s->pict_type = AV_PICTURE_TYPE_B;
  1283. s->current_picture.f.pict_type = s->pict_type;
  1284. s->current_picture.f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
  1285. }
  1286. s->intra_dc_precision = get_bits(&s->gb, 2);
  1287. s->picture_structure = get_bits(&s->gb, 2);
  1288. s->top_field_first = get_bits1(&s->gb);
  1289. s->frame_pred_frame_dct = get_bits1(&s->gb);
  1290. s->concealment_motion_vectors = get_bits1(&s->gb);
  1291. s->q_scale_type = get_bits1(&s->gb);
  1292. s->intra_vlc_format = get_bits1(&s->gb);
  1293. s->alternate_scan = get_bits1(&s->gb);
  1294. s->repeat_first_field = get_bits1(&s->gb);
  1295. s->chroma_420_type = get_bits1(&s->gb);
  1296. s->progressive_frame = get_bits1(&s->gb);
  1297. if (s->progressive_sequence && !s->progressive_frame) {
  1298. s->progressive_frame = 1;
  1299. av_log(s->avctx, AV_LOG_ERROR, "interlaced frame in progressive sequence, ignoring\n");
  1300. }
  1301. if (s->picture_structure == 0 || (s->progressive_frame && s->picture_structure != PICT_FRAME)) {
  1302. av_log(s->avctx, AV_LOG_ERROR, "picture_structure %d invalid, ignoring\n", s->picture_structure);
  1303. s->picture_structure = PICT_FRAME;
  1304. }
  1305. if (s->progressive_sequence && !s->frame_pred_frame_dct) {
  1306. av_log(s->avctx, AV_LOG_WARNING, "invalid frame_pred_frame_dct\n");
  1307. }
  1308. if (s->picture_structure == PICT_FRAME) {
  1309. s->first_field = 0;
  1310. s->v_edge_pos = 16 * s->mb_height;
  1311. } else {
  1312. s->first_field ^= 1;
  1313. s->v_edge_pos = 8 * s->mb_height;
  1314. memset(s->mbskip_table, 0, s->mb_stride * s->mb_height);
  1315. }
  1316. if (s->alternate_scan) {
  1317. ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable, ff_alternate_vertical_scan);
  1318. ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable, ff_alternate_vertical_scan);
  1319. } else {
  1320. ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable, ff_zigzag_direct);
  1321. ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable, ff_zigzag_direct);
  1322. }
  1323. /* composite display not parsed */
  1324. av_dlog(s->avctx, "intra_dc_precision=%d\n", s->intra_dc_precision);
  1325. av_dlog(s->avctx, "picture_structure=%d\n", s->picture_structure);
  1326. av_dlog(s->avctx, "top field first=%d\n", s->top_field_first);
  1327. av_dlog(s->avctx, "repeat first field=%d\n", s->repeat_first_field);
  1328. av_dlog(s->avctx, "conceal=%d\n", s->concealment_motion_vectors);
  1329. av_dlog(s->avctx, "intra_vlc_format=%d\n", s->intra_vlc_format);
  1330. av_dlog(s->avctx, "alternate_scan=%d\n", s->alternate_scan);
  1331. av_dlog(s->avctx, "frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
  1332. av_dlog(s->avctx, "progressive_frame=%d\n", s->progressive_frame);
  1333. }
  1334. static int mpeg_field_start(MpegEncContext *s, const uint8_t *buf, int buf_size)
  1335. {
  1336. AVCodecContext *avctx = s->avctx;
  1337. Mpeg1Context *s1 = (Mpeg1Context*)s;
  1338. /* start frame decoding */
  1339. if (s->first_field || s->picture_structure == PICT_FRAME) {
  1340. AVFrameSideData *pan_scan;
  1341. if (ff_MPV_frame_start(s, avctx) < 0)
  1342. return -1;
  1343. ff_mpeg_er_frame_start(s);
  1344. /* first check if we must repeat the frame */
  1345. s->current_picture_ptr->f.repeat_pict = 0;
  1346. if (s->repeat_first_field) {
  1347. if (s->progressive_sequence) {
  1348. if (s->top_field_first)
  1349. s->current_picture_ptr->f.repeat_pict = 4;
  1350. else
  1351. s->current_picture_ptr->f.repeat_pict = 2;
  1352. } else if (s->progressive_frame) {
  1353. s->current_picture_ptr->f.repeat_pict = 1;
  1354. }
  1355. }
  1356. pan_scan = av_frame_new_side_data(&s->current_picture_ptr->f,
  1357. AV_FRAME_DATA_PANSCAN,
  1358. sizeof(s1->pan_scan));
  1359. if (!pan_scan)
  1360. return AVERROR(ENOMEM);
  1361. memcpy(pan_scan->data, &s1->pan_scan, sizeof(s1->pan_scan));
  1362. if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_FRAME))
  1363. ff_thread_finish_setup(avctx);
  1364. } else { // second field
  1365. int i;
  1366. if (!s->current_picture_ptr) {
  1367. av_log(s->avctx, AV_LOG_ERROR, "first field missing\n");
  1368. return -1;
  1369. }
  1370. if (s->avctx->hwaccel &&
  1371. (s->avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD)) {
  1372. if (s->avctx->hwaccel->end_frame(s->avctx) < 0)
  1373. av_log(avctx, AV_LOG_ERROR, "hardware accelerator failed to decode first field\n");
  1374. }
  1375. for (i = 0; i < 4; i++) {
  1376. s->current_picture.f.data[i] = s->current_picture_ptr->f.data[i];
  1377. if (s->picture_structure == PICT_BOTTOM_FIELD) {
  1378. s->current_picture.f.data[i] += s->current_picture_ptr->f.linesize[i];
  1379. }
  1380. }
  1381. }
  1382. if (avctx->hwaccel) {
  1383. if (avctx->hwaccel->start_frame(avctx, buf, buf_size) < 0)
  1384. return -1;
  1385. }
  1386. // MPV_frame_start will call this function too,
  1387. // but we need to call it on every field
  1388. if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
  1389. if (ff_xvmc_field_start(s, avctx) < 0)
  1390. return -1;
  1391. return 0;
  1392. }
  1393. #define DECODE_SLICE_ERROR -1
  1394. #define DECODE_SLICE_OK 0
  1395. /**
  1396. * Decode a slice.
  1397. * MpegEncContext.mb_y must be set to the MB row from the startcode.
  1398. * @return DECODE_SLICE_ERROR if the slice is damaged,
  1399. * DECODE_SLICE_OK if this slice is OK
  1400. */
  1401. static int mpeg_decode_slice(MpegEncContext *s, int mb_y,
  1402. const uint8_t **buf, int buf_size)
  1403. {
  1404. AVCodecContext *avctx = s->avctx;
  1405. const int field_pic = s->picture_structure != PICT_FRAME;
  1406. s->resync_mb_x =
  1407. s->resync_mb_y = -1;
  1408. assert(mb_y < s->mb_height);
  1409. init_get_bits(&s->gb, *buf, buf_size * 8);
  1410. ff_mpeg1_clean_buffers(s);
  1411. s->interlaced_dct = 0;
  1412. s->qscale = get_qscale(s);
  1413. if (s->qscale == 0) {
  1414. av_log(s->avctx, AV_LOG_ERROR, "qscale == 0\n");
  1415. return -1;
  1416. }
  1417. /* extra slice info */
  1418. while (get_bits1(&s->gb) != 0) {
  1419. skip_bits(&s->gb, 8);
  1420. }
  1421. s->mb_x = 0;
  1422. if (mb_y == 0 && s->codec_tag == AV_RL32("SLIF")) {
  1423. skip_bits1(&s->gb);
  1424. } else {
  1425. while (get_bits_left(&s->gb) > 0) {
  1426. int code = get_vlc2(&s->gb, ff_mbincr_vlc.table,
  1427. MBINCR_VLC_BITS, 2);
  1428. if (code < 0) {
  1429. av_log(s->avctx, AV_LOG_ERROR, "first mb_incr damaged\n");
  1430. return -1;
  1431. }
  1432. if (code >= 33) {
  1433. if (code == 33) {
  1434. s->mb_x += 33;
  1435. }
  1436. /* otherwise, stuffing, nothing to do */
  1437. } else {
  1438. s->mb_x += code;
  1439. break;
  1440. }
  1441. }
  1442. }
  1443. if (s->mb_x >= (unsigned)s->mb_width) {
  1444. av_log(s->avctx, AV_LOG_ERROR, "initial skip overflow\n");
  1445. return -1;
  1446. }
  1447. if (avctx->hwaccel) {
  1448. const uint8_t *buf_end, *buf_start = *buf - 4; /* include start_code */
  1449. int start_code = -1;
  1450. buf_end = avpriv_find_start_code(buf_start + 2, *buf + buf_size, &start_code);
  1451. if (buf_end < *buf + buf_size)
  1452. buf_end -= 4;
  1453. s->mb_y = mb_y;
  1454. if (avctx->hwaccel->decode_slice(avctx, buf_start, buf_end - buf_start) < 0)
  1455. return DECODE_SLICE_ERROR;
  1456. *buf = buf_end;
  1457. return DECODE_SLICE_OK;
  1458. }
  1459. s->resync_mb_x = s->mb_x;
  1460. s->resync_mb_y = s->mb_y = mb_y;
  1461. s->mb_skip_run = 0;
  1462. ff_init_block_index(s);
  1463. if (s->mb_y == 0 && s->mb_x == 0 && (s->first_field || s->picture_structure == PICT_FRAME)) {
  1464. if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
  1465. 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",
  1466. 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],
  1467. 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")),
  1468. s->progressive_sequence ? "ps" :"", s->progressive_frame ? "pf" : "", s->alternate_scan ? "alt" :"", s->top_field_first ? "top" :"",
  1469. s->intra_dc_precision, s->picture_structure, s->frame_pred_frame_dct, s->concealment_motion_vectors,
  1470. s->q_scale_type, s->intra_vlc_format, s->repeat_first_field, s->chroma_420_type ? "420" :"");
  1471. }
  1472. }
  1473. for (;;) {
  1474. // If 1, we memcpy blocks in xvmcvideo.
  1475. if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1)
  1476. ff_xvmc_init_block(s); // set s->block
  1477. if (mpeg_decode_mb(s, s->block) < 0)
  1478. return -1;
  1479. if (s->current_picture.motion_val[0] && !s->encoding) { // note motion_val is normally NULL unless we want to extract the MVs
  1480. const int wrap = s->b8_stride;
  1481. int xy = s->mb_x * 2 + s->mb_y * 2 * wrap;
  1482. int b8_xy = 4 * (s->mb_x + s->mb_y * s->mb_stride);
  1483. int motion_x, motion_y, dir, i;
  1484. for (i = 0; i < 2; i++) {
  1485. for (dir = 0; dir < 2; dir++) {
  1486. if (s->mb_intra || (dir == 1 && s->pict_type != AV_PICTURE_TYPE_B)) {
  1487. motion_x = motion_y = 0;
  1488. } else if (s->mv_type == MV_TYPE_16X16 || (s->mv_type == MV_TYPE_FIELD && field_pic)) {
  1489. motion_x = s->mv[dir][0][0];
  1490. motion_y = s->mv[dir][0][1];
  1491. } else /*if ((s->mv_type == MV_TYPE_FIELD) || (s->mv_type == MV_TYPE_16X8))*/ {
  1492. motion_x = s->mv[dir][i][0];
  1493. motion_y = s->mv[dir][i][1];
  1494. }
  1495. s->current_picture.motion_val[dir][xy ][0] = motion_x;
  1496. s->current_picture.motion_val[dir][xy ][1] = motion_y;
  1497. s->current_picture.motion_val[dir][xy + 1][0] = motion_x;
  1498. s->current_picture.motion_val[dir][xy + 1][1] = motion_y;
  1499. s->current_picture.ref_index [dir][b8_xy ] =
  1500. s->current_picture.ref_index [dir][b8_xy + 1] = s->field_select[dir][i];
  1501. assert(s->field_select[dir][i] == 0 || s->field_select[dir][i] == 1);
  1502. }
  1503. xy += wrap;
  1504. b8_xy +=2;
  1505. }
  1506. }
  1507. s->dest[0] += 16;
  1508. s->dest[1] += 16 >> s->chroma_x_shift;
  1509. s->dest[2] += 16 >> s->chroma_x_shift;
  1510. ff_MPV_decode_mb(s, s->block);
  1511. if (++s->mb_x >= s->mb_width) {
  1512. const int mb_size = 16;
  1513. ff_mpeg_draw_horiz_band(s, mb_size*(s->mb_y >> field_pic), mb_size);
  1514. ff_MPV_report_decode_progress(s);
  1515. s->mb_x = 0;
  1516. s->mb_y += 1 << field_pic;
  1517. if (s->mb_y >= s->mb_height) {
  1518. int left = get_bits_left(&s->gb);
  1519. int is_d10 = s->chroma_format == 2 && s->pict_type == AV_PICTURE_TYPE_I && avctx->profile == 0 && avctx->level == 5
  1520. && s->intra_dc_precision == 2 && s->q_scale_type == 1 && s->alternate_scan == 0
  1521. && s->progressive_frame == 0 /* vbv_delay == 0xBBB || 0xE10*/;
  1522. if (left < 0 || (left && show_bits(&s->gb, FFMIN(left, 23)) && !is_d10)
  1523. || ((avctx->err_recognition & AV_EF_BUFFER) && left > 8)) {
  1524. av_log(avctx, AV_LOG_ERROR, "end mismatch left=%d %0X\n", left, show_bits(&s->gb, FFMIN(left, 23)));
  1525. return -1;
  1526. } else
  1527. goto eos;
  1528. }
  1529. ff_init_block_index(s);
  1530. }
  1531. /* skip mb handling */
  1532. if (s->mb_skip_run == -1) {
  1533. /* read increment again */
  1534. s->mb_skip_run = 0;
  1535. for (;;) {
  1536. int code = get_vlc2(&s->gb, ff_mbincr_vlc.table,
  1537. MBINCR_VLC_BITS, 2);
  1538. if (code < 0) {
  1539. av_log(s->avctx, AV_LOG_ERROR, "mb incr damaged\n");
  1540. return -1;
  1541. }
  1542. if (code >= 33) {
  1543. if (code == 33) {
  1544. s->mb_skip_run += 33;
  1545. } else if (code == 35) {
  1546. if (s->mb_skip_run != 0 || show_bits(&s->gb, 15) != 0) {
  1547. av_log(s->avctx, AV_LOG_ERROR, "slice mismatch\n");
  1548. return -1;
  1549. }
  1550. goto eos; /* end of slice */
  1551. }
  1552. /* otherwise, stuffing, nothing to do */
  1553. } else {
  1554. s->mb_skip_run += code;
  1555. break;
  1556. }
  1557. }
  1558. if (s->mb_skip_run) {
  1559. int i;
  1560. if (s->pict_type == AV_PICTURE_TYPE_I) {
  1561. av_log(s->avctx, AV_LOG_ERROR, "skipped MB in I frame at %d %d\n", s->mb_x, s->mb_y);
  1562. return -1;
  1563. }
  1564. /* skip mb */
  1565. s->mb_intra = 0;
  1566. for (i = 0; i < 12; i++)
  1567. s->block_last_index[i] = -1;
  1568. if (s->picture_structure == PICT_FRAME)
  1569. s->mv_type = MV_TYPE_16X16;
  1570. else
  1571. s->mv_type = MV_TYPE_FIELD;
  1572. if (s->pict_type == AV_PICTURE_TYPE_P) {
  1573. /* if P type, zero motion vector is implied */
  1574. s->mv_dir = MV_DIR_FORWARD;
  1575. s->mv[0][0][0] = s->mv[0][0][1] = 0;
  1576. s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
  1577. s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
  1578. s->field_select[0][0] = (s->picture_structure - 1) & 1;
  1579. } else {
  1580. /* if B type, reuse previous vectors and directions */
  1581. s->mv[0][0][0] = s->last_mv[0][0][0];
  1582. s->mv[0][0][1] = s->last_mv[0][0][1];
  1583. s->mv[1][0][0] = s->last_mv[1][0][0];
  1584. s->mv[1][0][1] = s->last_mv[1][0][1];
  1585. }
  1586. }
  1587. }
  1588. }
  1589. eos: // end of slice
  1590. *buf += (get_bits_count(&s->gb)-1)/8;
  1591. av_dlog(s, "y %d %d %d %d\n", s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y);
  1592. return 0;
  1593. }
  1594. static int slice_decode_thread(AVCodecContext *c, void *arg)
  1595. {
  1596. MpegEncContext *s = *(void**)arg;
  1597. const uint8_t *buf = s->gb.buffer;
  1598. int mb_y = s->start_mb_y;
  1599. const int field_pic = s->picture_structure != PICT_FRAME;
  1600. s->er.error_count = (3 * (s->end_mb_y - s->start_mb_y) * s->mb_width) >> field_pic;
  1601. for (;;) {
  1602. uint32_t start_code;
  1603. int ret;
  1604. ret = mpeg_decode_slice(s, mb_y, &buf, s->gb.buffer_end - buf);
  1605. emms_c();
  1606. av_dlog(c, "ret:%d resync:%d/%d mb:%d/%d ts:%d/%d ec:%d\n",
  1607. ret, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y,
  1608. s->start_mb_y, s->end_mb_y, s->er.error_count);
  1609. if (ret < 0) {
  1610. if (c->err_recognition & AV_EF_EXPLODE)
  1611. return ret;
  1612. if (s->resync_mb_x >= 0 && s->resync_mb_y >= 0)
  1613. 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);
  1614. } else {
  1615. 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);
  1616. }
  1617. if (s->mb_y == s->end_mb_y)
  1618. return 0;
  1619. start_code = -1;
  1620. buf = avpriv_find_start_code(buf, s->gb.buffer_end, &start_code);
  1621. mb_y= (start_code - SLICE_MIN_START_CODE) << field_pic;
  1622. if (s->picture_structure == PICT_BOTTOM_FIELD)
  1623. mb_y++;
  1624. if (mb_y < 0 || mb_y >= s->end_mb_y)
  1625. return -1;
  1626. }
  1627. }
  1628. /**
  1629. * Handle slice ends.
  1630. * @return 1 if it seems to be the last slice
  1631. */
  1632. static int slice_end(AVCodecContext *avctx, AVFrame *pict)
  1633. {
  1634. Mpeg1Context *s1 = avctx->priv_data;
  1635. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1636. if (!s1->mpeg_enc_ctx_allocated || !s->current_picture_ptr)
  1637. return 0;
  1638. if (s->avctx->hwaccel) {
  1639. if (s->avctx->hwaccel->end_frame(s->avctx) < 0)
  1640. av_log(avctx, AV_LOG_ERROR, "hardware accelerator failed to decode picture\n");
  1641. }
  1642. if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
  1643. ff_xvmc_field_end(s);
  1644. /* end of slice reached */
  1645. if (/*s->mb_y << field_pic == s->mb_height &&*/ !s->first_field) {
  1646. /* end of image */
  1647. ff_er_frame_end(&s->er);
  1648. ff_MPV_frame_end(s);
  1649. if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
  1650. int ret = av_frame_ref(pict, &s->current_picture_ptr->f);
  1651. if (ret < 0)
  1652. return ret;
  1653. ff_print_debug_info(s, s->current_picture_ptr);
  1654. } else {
  1655. if (avctx->active_thread_type & FF_THREAD_FRAME)
  1656. s->picture_number++;
  1657. /* latency of 1 frame for I- and P-frames */
  1658. /* XXX: use another variable than picture_number */
  1659. if (s->last_picture_ptr != NULL) {
  1660. int ret = av_frame_ref(pict, &s->last_picture_ptr->f);
  1661. if (ret < 0)
  1662. return ret;
  1663. ff_print_debug_info(s, s->last_picture_ptr);
  1664. }
  1665. }
  1666. return 1;
  1667. } else {
  1668. return 0;
  1669. }
  1670. }
  1671. static int mpeg1_decode_sequence(AVCodecContext *avctx,
  1672. const uint8_t *buf, int buf_size)
  1673. {
  1674. Mpeg1Context *s1 = avctx->priv_data;
  1675. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1676. int width, height;
  1677. int i, v, j;
  1678. init_get_bits(&s->gb, buf, buf_size*8);
  1679. width = get_bits(&s->gb, 12);
  1680. height = get_bits(&s->gb, 12);
  1681. if (width == 0 || height == 0) {
  1682. av_log(avctx, AV_LOG_WARNING, "Invalid horizontal or vertical size "
  1683. "value.\n");
  1684. if (avctx->err_recognition & AV_EF_BITSTREAM)
  1685. return AVERROR_INVALIDDATA;
  1686. }
  1687. s->aspect_ratio_info = get_bits(&s->gb, 4);
  1688. if (s->aspect_ratio_info == 0) {
  1689. av_log(avctx, AV_LOG_ERROR, "aspect ratio has forbidden 0 value\n");
  1690. if (avctx->err_recognition & AV_EF_BITSTREAM)
  1691. return -1;
  1692. }
  1693. s->frame_rate_index = get_bits(&s->gb, 4);
  1694. if (s->frame_rate_index == 0 || s->frame_rate_index > 13)
  1695. return -1;
  1696. s->bit_rate = get_bits(&s->gb, 18) * 400;
  1697. if (get_bits1(&s->gb) == 0) /* marker */
  1698. return -1;
  1699. s->width = width;
  1700. s->height = height;
  1701. s->avctx->rc_buffer_size = get_bits(&s->gb, 10) * 1024 * 16;
  1702. skip_bits(&s->gb, 1);
  1703. /* get matrix */
  1704. if (get_bits1(&s->gb)) {
  1705. load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
  1706. } else {
  1707. for (i = 0; i < 64; i++) {
  1708. j = s->dsp.idct_permutation[i];
  1709. v = ff_mpeg1_default_intra_matrix[i];
  1710. s->intra_matrix[j] = v;
  1711. s->chroma_intra_matrix[j] = v;
  1712. }
  1713. }
  1714. if (get_bits1(&s->gb)) {
  1715. load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
  1716. } else {
  1717. for (i = 0; i < 64; i++) {
  1718. int j = s->dsp.idct_permutation[i];
  1719. v = ff_mpeg1_default_non_intra_matrix[i];
  1720. s->inter_matrix[j] = v;
  1721. s->chroma_inter_matrix[j] = v;
  1722. }
  1723. }
  1724. if (show_bits(&s->gb, 23) != 0) {
  1725. av_log(s->avctx, AV_LOG_ERROR, "sequence header damaged\n");
  1726. return -1;
  1727. }
  1728. /* we set MPEG-2 parameters so that it emulates MPEG-1 */
  1729. s->progressive_sequence = 1;
  1730. s->progressive_frame = 1;
  1731. s->picture_structure = PICT_FRAME;
  1732. s->frame_pred_frame_dct = 1;
  1733. s->chroma_format = 1;
  1734. s->codec_id = s->avctx->codec_id = AV_CODEC_ID_MPEG1VIDEO;
  1735. s->out_format = FMT_MPEG1;
  1736. s->swap_uv = 0; // AFAIK VCR2 does not have SEQ_HEADER
  1737. if (s->flags & CODEC_FLAG_LOW_DELAY)
  1738. s->low_delay = 1;
  1739. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  1740. av_log(s->avctx, AV_LOG_DEBUG, "vbv buffer: %d, bitrate:%d\n",
  1741. s->avctx->rc_buffer_size, s->bit_rate);
  1742. return 0;
  1743. }
  1744. static int vcr2_init_sequence(AVCodecContext *avctx)
  1745. {
  1746. Mpeg1Context *s1 = avctx->priv_data;
  1747. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1748. int i, v;
  1749. /* start new MPEG-1 context decoding */
  1750. s->out_format = FMT_MPEG1;
  1751. if (s1->mpeg_enc_ctx_allocated) {
  1752. ff_MPV_common_end(s);
  1753. }
  1754. s->width = avctx->coded_width;
  1755. s->height = avctx->coded_height;
  1756. avctx->has_b_frames = 0; // true?
  1757. s->low_delay = 1;
  1758. avctx->pix_fmt = mpeg_get_pixelformat(avctx);
  1759. avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
  1760. if (avctx->pix_fmt == AV_PIX_FMT_XVMC_MPEG2_IDCT || avctx->hwaccel)
  1761. if (avctx->idct_algo == FF_IDCT_AUTO)
  1762. avctx->idct_algo = FF_IDCT_SIMPLE;
  1763. if (ff_MPV_common_init(s) < 0)
  1764. return -1;
  1765. exchange_uv(s); // common init reset pblocks, so we swap them here
  1766. s->swap_uv = 1; // in case of xvmc we need to swap uv for each MB
  1767. s1->mpeg_enc_ctx_allocated = 1;
  1768. for (i = 0; i < 64; i++) {
  1769. int j = s->dsp.idct_permutation[i];
  1770. v = ff_mpeg1_default_intra_matrix[i];
  1771. s->intra_matrix[j] = v;
  1772. s->chroma_intra_matrix[j] = v;
  1773. v = ff_mpeg1_default_non_intra_matrix[i];
  1774. s->inter_matrix[j] = v;
  1775. s->chroma_inter_matrix[j] = v;
  1776. }
  1777. s->progressive_sequence = 1;
  1778. s->progressive_frame = 1;
  1779. s->picture_structure = PICT_FRAME;
  1780. s->frame_pred_frame_dct = 1;
  1781. s->chroma_format = 1;
  1782. s->codec_id = s->avctx->codec_id = AV_CODEC_ID_MPEG2VIDEO;
  1783. s1->save_width = s->width;
  1784. s1->save_height = s->height;
  1785. s1->save_progressive_seq = s->progressive_sequence;
  1786. return 0;
  1787. }
  1788. static void mpeg_decode_user_data(AVCodecContext *avctx,
  1789. const uint8_t *p, int buf_size)
  1790. {
  1791. const uint8_t *buf_end = p + buf_size;
  1792. /* we parse the DTG active format information */
  1793. if (buf_end - p >= 5 &&
  1794. p[0] == 'D' && p[1] == 'T' && p[2] == 'G' && p[3] == '1') {
  1795. int flags = p[4];
  1796. p += 5;
  1797. if (flags & 0x80) {
  1798. /* skip event id */
  1799. p += 2;
  1800. }
  1801. if (flags & 0x40) {
  1802. if (buf_end - p < 1)
  1803. return;
  1804. avctx->dtg_active_format = p[0] & 0x0f;
  1805. }
  1806. }
  1807. }
  1808. static void mpeg_decode_gop(AVCodecContext *avctx,
  1809. const uint8_t *buf, int buf_size)
  1810. {
  1811. Mpeg1Context *s1 = avctx->priv_data;
  1812. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1813. int time_code_hours, time_code_minutes;
  1814. int time_code_seconds, time_code_pictures;
  1815. int broken_link;
  1816. init_get_bits(&s->gb, buf, buf_size*8);
  1817. skip_bits1(&s->gb); /* drop_frame_flag */
  1818. time_code_hours = get_bits(&s->gb, 5);
  1819. time_code_minutes = get_bits(&s->gb, 6);
  1820. skip_bits1(&s->gb); // marker bit
  1821. time_code_seconds = get_bits(&s->gb, 6);
  1822. time_code_pictures = get_bits(&s->gb, 6);
  1823. s1->closed_gop = get_bits1(&s->gb);
  1824. /*broken_link indicate that after editing the
  1825. reference frames of the first B-Frames after GOP I-Frame
  1826. are missing (open gop)*/
  1827. broken_link = get_bits1(&s->gb);
  1828. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  1829. av_log(s->avctx, AV_LOG_DEBUG, "GOP (%2d:%02d:%02d.[%02d]) closed_gop=%d broken_link=%d\n",
  1830. time_code_hours, time_code_minutes, time_code_seconds,
  1831. time_code_pictures, s1->closed_gop, broken_link);
  1832. }
  1833. static int decode_chunks(AVCodecContext *avctx,
  1834. AVFrame *picture, int *got_output,
  1835. const uint8_t *buf, int buf_size)
  1836. {
  1837. Mpeg1Context *s = avctx->priv_data;
  1838. MpegEncContext *s2 = &s->mpeg_enc_ctx;
  1839. const uint8_t *buf_ptr = buf;
  1840. const uint8_t *buf_end = buf + buf_size;
  1841. int ret, input_size;
  1842. int last_code = 0, skip_frame = 0;
  1843. for (;;) {
  1844. /* find next start code */
  1845. uint32_t start_code = -1;
  1846. buf_ptr = avpriv_find_start_code(buf_ptr, buf_end, &start_code);
  1847. if (start_code > 0x1ff) {
  1848. if (!skip_frame) {
  1849. if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE) &&
  1850. !avctx->hwaccel) {
  1851. int i;
  1852. avctx->execute(avctx, slice_decode_thread, &s2->thread_context[0], NULL, s->slice_count, sizeof(void*));
  1853. for (i = 0; i < s->slice_count; i++)
  1854. s2->er.error_count += s2->thread_context[i]->er.error_count;
  1855. }
  1856. ret = slice_end(avctx, picture);
  1857. if (ret < 0)
  1858. return ret;
  1859. else if (ret) {
  1860. if (s2->last_picture_ptr || s2->low_delay) //FIXME merge with the stuff in mpeg_decode_slice
  1861. *got_output = 1;
  1862. }
  1863. }
  1864. s2->pict_type = 0;
  1865. return FFMAX(0, buf_ptr - buf - s2->parse_context.last_index);
  1866. }
  1867. input_size = buf_end - buf_ptr;
  1868. if (avctx->debug & FF_DEBUG_STARTCODE) {
  1869. av_log(avctx, AV_LOG_DEBUG, "%3X at %td left %d\n", start_code, buf_ptr-buf, input_size);
  1870. }
  1871. /* prepare data for next start code */
  1872. switch (start_code) {
  1873. case SEQ_START_CODE:
  1874. if (last_code == 0) {
  1875. mpeg1_decode_sequence(avctx, buf_ptr, input_size);
  1876. s->sync=1;
  1877. } else {
  1878. av_log(avctx, AV_LOG_ERROR, "ignoring SEQ_START_CODE after %X\n", last_code);
  1879. if (avctx->err_recognition & AV_EF_EXPLODE)
  1880. return AVERROR_INVALIDDATA;
  1881. }
  1882. break;
  1883. case PICTURE_START_CODE:
  1884. if (s2->width <= 0 || s2->height <= 0) {
  1885. av_log(avctx, AV_LOG_ERROR, "Invalid frame dimensions %dx%d.\n",
  1886. s2->width, s2->height);
  1887. return AVERROR_INVALIDDATA;
  1888. }
  1889. if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE) &&
  1890. !avctx->hwaccel && s->slice_count) {
  1891. int i;
  1892. avctx->execute(avctx, slice_decode_thread,
  1893. s2->thread_context, NULL,
  1894. s->slice_count, sizeof(void*));
  1895. for (i = 0; i < s->slice_count; i++)
  1896. s2->er.error_count += s2->thread_context[i]->er.error_count;
  1897. s->slice_count = 0;
  1898. }
  1899. if (last_code == 0 || last_code == SLICE_MIN_START_CODE) {
  1900. ret = mpeg_decode_postinit(avctx);
  1901. if (ret < 0) {
  1902. av_log(avctx, AV_LOG_ERROR, "mpeg_decode_postinit() failure\n");
  1903. return ret;
  1904. }
  1905. /* we have a complete image: we try to decompress it */
  1906. if (mpeg1_decode_picture(avctx, buf_ptr, input_size) < 0)
  1907. s2->pict_type = 0;
  1908. s2->first_slice = 1;
  1909. last_code = PICTURE_START_CODE;
  1910. } else {
  1911. av_log(avctx, AV_LOG_ERROR, "ignoring pic after %X\n", last_code);
  1912. if (avctx->err_recognition & AV_EF_EXPLODE)
  1913. return AVERROR_INVALIDDATA;
  1914. }
  1915. break;
  1916. case EXT_START_CODE:
  1917. init_get_bits(&s2->gb, buf_ptr, input_size*8);
  1918. switch (get_bits(&s2->gb, 4)) {
  1919. case 0x1:
  1920. if (last_code == 0) {
  1921. mpeg_decode_sequence_extension(s);
  1922. } else {
  1923. av_log(avctx, AV_LOG_ERROR, "ignoring seq ext after %X\n", last_code);
  1924. if (avctx->err_recognition & AV_EF_EXPLODE)
  1925. return AVERROR_INVALIDDATA;
  1926. }
  1927. break;
  1928. case 0x2:
  1929. mpeg_decode_sequence_display_extension(s);
  1930. break;
  1931. case 0x3:
  1932. mpeg_decode_quant_matrix_extension(s2);
  1933. break;
  1934. case 0x7:
  1935. mpeg_decode_picture_display_extension(s);
  1936. break;
  1937. case 0x8:
  1938. if (last_code == PICTURE_START_CODE) {
  1939. mpeg_decode_picture_coding_extension(s);
  1940. } else {
  1941. av_log(avctx, AV_LOG_ERROR, "ignoring pic cod ext after %X\n", last_code);
  1942. if (avctx->err_recognition & AV_EF_EXPLODE)
  1943. return AVERROR_INVALIDDATA;
  1944. }
  1945. break;
  1946. }
  1947. break;
  1948. case USER_START_CODE:
  1949. mpeg_decode_user_data(avctx, buf_ptr, input_size);
  1950. break;
  1951. case GOP_START_CODE:
  1952. if (last_code == 0) {
  1953. s2->first_field=0;
  1954. mpeg_decode_gop(avctx, buf_ptr, input_size);
  1955. s->sync=1;
  1956. } else {
  1957. av_log(avctx, AV_LOG_ERROR, "ignoring GOP_START_CODE after %X\n", last_code);
  1958. if (avctx->err_recognition & AV_EF_EXPLODE)
  1959. return AVERROR_INVALIDDATA;
  1960. }
  1961. break;
  1962. default:
  1963. if (start_code >= SLICE_MIN_START_CODE &&
  1964. start_code <= SLICE_MAX_START_CODE && last_code != 0) {
  1965. const int field_pic = s2->picture_structure != PICT_FRAME;
  1966. int mb_y = (start_code - SLICE_MIN_START_CODE) << field_pic;
  1967. last_code = SLICE_MIN_START_CODE;
  1968. if (s2->picture_structure == PICT_BOTTOM_FIELD)
  1969. mb_y++;
  1970. if (mb_y >= s2->mb_height) {
  1971. av_log(s2->avctx, AV_LOG_ERROR, "slice below image (%d >= %d)\n", mb_y, s2->mb_height);
  1972. return -1;
  1973. }
  1974. if (s2->last_picture_ptr == NULL) {
  1975. /* Skip B-frames if we do not have reference frames and gop is not closed */
  1976. if (s2->pict_type == AV_PICTURE_TYPE_B) {
  1977. if (!s->closed_gop) {
  1978. skip_frame = 1;
  1979. break;
  1980. }
  1981. }
  1982. }
  1983. if (s2->pict_type == AV_PICTURE_TYPE_I)
  1984. s->sync=1;
  1985. if (s2->next_picture_ptr == NULL) {
  1986. /* Skip P-frames if we do not have a reference frame or we have an invalid header. */
  1987. if (s2->pict_type == AV_PICTURE_TYPE_P && !s->sync) {
  1988. skip_frame = 1;
  1989. break;
  1990. }
  1991. }
  1992. if ((avctx->skip_frame >= AVDISCARD_NONREF && s2->pict_type == AV_PICTURE_TYPE_B) ||
  1993. (avctx->skip_frame >= AVDISCARD_NONKEY && s2->pict_type != AV_PICTURE_TYPE_I) ||
  1994. avctx->skip_frame >= AVDISCARD_ALL) {
  1995. skip_frame = 1;
  1996. break;
  1997. }
  1998. if (!s->mpeg_enc_ctx_allocated)
  1999. break;
  2000. if (s2->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
  2001. if (mb_y < avctx->skip_top || mb_y >= s2->mb_height - avctx->skip_bottom)
  2002. break;
  2003. }
  2004. if (!s2->pict_type) {
  2005. av_log(avctx, AV_LOG_ERROR, "Missing picture start code\n");
  2006. if (avctx->err_recognition & AV_EF_EXPLODE)
  2007. return AVERROR_INVALIDDATA;
  2008. break;
  2009. }
  2010. if (s2->first_slice) {
  2011. skip_frame = 0;
  2012. s2->first_slice = 0;
  2013. if (mpeg_field_start(s2, buf, buf_size) < 0)
  2014. return -1;
  2015. }
  2016. if (!s2->current_picture_ptr) {
  2017. av_log(avctx, AV_LOG_ERROR, "current_picture not initialized\n");
  2018. return AVERROR_INVALIDDATA;
  2019. }
  2020. if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE) &&
  2021. !avctx->hwaccel) {
  2022. int threshold = (s2->mb_height * s->slice_count +
  2023. s2->slice_context_count / 2) /
  2024. s2->slice_context_count;
  2025. if (threshold <= mb_y) {
  2026. MpegEncContext *thread_context = s2->thread_context[s->slice_count];
  2027. thread_context->start_mb_y = mb_y;
  2028. thread_context->end_mb_y = s2->mb_height;
  2029. if (s->slice_count) {
  2030. s2->thread_context[s->slice_count-1]->end_mb_y = mb_y;
  2031. ret = ff_update_duplicate_context(thread_context,
  2032. s2);
  2033. if (ret < 0)
  2034. return ret;
  2035. }
  2036. init_get_bits(&thread_context->gb, buf_ptr, input_size*8);
  2037. s->slice_count++;
  2038. }
  2039. buf_ptr += 2; // FIXME add minimum number of bytes per slice
  2040. } else {
  2041. ret = mpeg_decode_slice(s2, mb_y, &buf_ptr, input_size);
  2042. emms_c();
  2043. if (ret < 0) {
  2044. if (avctx->err_recognition & AV_EF_EXPLODE)
  2045. return ret;
  2046. if (s2->resync_mb_x >= 0 && s2->resync_mb_y >= 0)
  2047. 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);
  2048. } else {
  2049. 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);
  2050. }
  2051. }
  2052. }
  2053. break;
  2054. }
  2055. }
  2056. }
  2057. static int mpeg_decode_frame(AVCodecContext *avctx,
  2058. void *data, int *got_output,
  2059. AVPacket *avpkt)
  2060. {
  2061. const uint8_t *buf = avpkt->data;
  2062. int buf_size = avpkt->size;
  2063. Mpeg1Context *s = avctx->priv_data;
  2064. AVFrame *picture = data;
  2065. MpegEncContext *s2 = &s->mpeg_enc_ctx;
  2066. av_dlog(avctx, "fill_buffer\n");
  2067. if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == SEQ_END_CODE)) {
  2068. /* special case for last picture */
  2069. if (s2->low_delay == 0 && s2->next_picture_ptr) {
  2070. int ret = av_frame_ref(picture, &s2->next_picture_ptr->f);
  2071. if (ret < 0)
  2072. return ret;
  2073. s2->next_picture_ptr = NULL;
  2074. *got_output = 1;
  2075. }
  2076. return buf_size;
  2077. }
  2078. if (s2->flags & CODEC_FLAG_TRUNCATED) {
  2079. int next = ff_mpeg1_find_frame_end(&s2->parse_context, buf, buf_size, NULL);
  2080. if (ff_combine_frame(&s2->parse_context, next, (const uint8_t **)&buf, &buf_size) < 0)
  2081. return buf_size;
  2082. }
  2083. if (s->mpeg_enc_ctx_allocated == 0 && avctx->codec_tag == AV_RL32("VCR2"))
  2084. vcr2_init_sequence(avctx);
  2085. s->slice_count = 0;
  2086. if (avctx->extradata && !s->extradata_decoded) {
  2087. int ret = decode_chunks(avctx, picture, got_output, avctx->extradata, avctx->extradata_size);
  2088. s->extradata_decoded = 1;
  2089. if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE))
  2090. return ret;
  2091. }
  2092. return decode_chunks(avctx, picture, got_output, buf, buf_size);
  2093. }
  2094. static void flush(AVCodecContext *avctx)
  2095. {
  2096. Mpeg1Context *s = avctx->priv_data;
  2097. s->sync=0;
  2098. s->closed_gop = 0;
  2099. ff_mpeg_flush(avctx);
  2100. }
  2101. static av_cold int mpeg_decode_end(AVCodecContext *avctx)
  2102. {
  2103. Mpeg1Context *s = avctx->priv_data;
  2104. if (s->mpeg_enc_ctx_allocated)
  2105. ff_MPV_common_end(&s->mpeg_enc_ctx);
  2106. return 0;
  2107. }
  2108. static const AVProfile mpeg2_video_profiles[] = {
  2109. { FF_PROFILE_MPEG2_422, "4:2:2" },
  2110. { FF_PROFILE_MPEG2_HIGH, "High" },
  2111. { FF_PROFILE_MPEG2_SS, "Spatially Scalable" },
  2112. { FF_PROFILE_MPEG2_SNR_SCALABLE, "SNR Scalable" },
  2113. { FF_PROFILE_MPEG2_MAIN, "Main" },
  2114. { FF_PROFILE_MPEG2_SIMPLE, "Simple" },
  2115. { FF_PROFILE_RESERVED, "Reserved" },
  2116. { FF_PROFILE_RESERVED, "Reserved" },
  2117. { FF_PROFILE_UNKNOWN },
  2118. };
  2119. AVCodec ff_mpeg1video_decoder = {
  2120. .name = "mpeg1video",
  2121. .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video"),
  2122. .type = AVMEDIA_TYPE_VIDEO,
  2123. .id = AV_CODEC_ID_MPEG1VIDEO,
  2124. .priv_data_size = sizeof(Mpeg1Context),
  2125. .init = mpeg_decode_init,
  2126. .close = mpeg_decode_end,
  2127. .decode = mpeg_decode_frame,
  2128. .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
  2129. CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY |
  2130. CODEC_CAP_SLICE_THREADS,
  2131. .flush = flush,
  2132. .update_thread_context = ONLY_IF_THREADS_ENABLED(mpeg_decode_update_thread_context)
  2133. };
  2134. AVCodec ff_mpeg2video_decoder = {
  2135. .name = "mpeg2video",
  2136. .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 video"),
  2137. .type = AVMEDIA_TYPE_VIDEO,
  2138. .id = AV_CODEC_ID_MPEG2VIDEO,
  2139. .priv_data_size = sizeof(Mpeg1Context),
  2140. .init = mpeg_decode_init,
  2141. .close = mpeg_decode_end,
  2142. .decode = mpeg_decode_frame,
  2143. .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
  2144. CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY |
  2145. CODEC_CAP_SLICE_THREADS,
  2146. .flush = flush,
  2147. .profiles = NULL_IF_CONFIG_SMALL(mpeg2_video_profiles),
  2148. };
  2149. #if CONFIG_MPEG_XVMC_DECODER
  2150. static av_cold int mpeg_mc_decode_init(AVCodecContext *avctx)
  2151. {
  2152. if (avctx->active_thread_type & FF_THREAD_SLICE)
  2153. return -1;
  2154. if (!(avctx->slice_flags & SLICE_FLAG_CODED_ORDER))
  2155. return -1;
  2156. if (!(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD)) {
  2157. av_dlog(avctx, "mpeg12.c: XvMC decoder will work better if SLICE_FLAG_ALLOW_FIELD is set\n");
  2158. }
  2159. mpeg_decode_init(avctx);
  2160. avctx->pix_fmt = AV_PIX_FMT_XVMC_MPEG2_IDCT;
  2161. avctx->xvmc_acceleration = 2; // 2 - the blocks are packed!
  2162. return 0;
  2163. }
  2164. AVCodec ff_mpeg_xvmc_decoder = {
  2165. .name = "mpegvideo_xvmc",
  2166. .long_name = NULL_IF_CONFIG_SMALL("MPEG-1/2 video XvMC (X-Video Motion Compensation)"),
  2167. .type = AVMEDIA_TYPE_VIDEO,
  2168. .id = AV_CODEC_ID_MPEG2VIDEO_XVMC,
  2169. .priv_data_size = sizeof(Mpeg1Context),
  2170. .init = mpeg_mc_decode_init,
  2171. .close = mpeg_decode_end,
  2172. .decode = mpeg_decode_frame,
  2173. .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
  2174. CODEC_CAP_TRUNCATED| CODEC_CAP_HWACCEL | CODEC_CAP_DELAY,
  2175. .flush = flush,
  2176. };
  2177. #endif