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.

2716 lines
97KB

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