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.

2707 lines
97KB

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