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.

2555 lines
92KB

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