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.

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