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.

2755 lines
98KB

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