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.

2596 lines
94KB

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