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.

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