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.

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