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.

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