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.

2738 lines
97KB

  1. /*
  2. * MPEG-1/2 decoder
  3. * Copyright (c) 2000, 2001 Fabrice Bellard
  4. * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * This file is part of Libav.
  7. *
  8. * Libav is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * Libav is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with Libav; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * MPEG-1/2 decoder
  25. */
  26. #include <inttypes.h>
  27. #include "libavutil/attributes.h"
  28. #include "libavutil/internal.h"
  29. #include "libavutil/stereo3d.h"
  30. #include "avcodec.h"
  31. #include "bytestream.h"
  32. #include "error_resilience.h"
  33. #include "idctdsp.h"
  34. #include "internal.h"
  35. #include "mpeg_er.h"
  36. #include "mpeg12.h"
  37. #include "mpeg12data.h"
  38. #include "mpegutils.h"
  39. #include "mpegvideo.h"
  40. #include "thread.h"
  41. #include "version.h"
  42. #include "xvmc_internal.h"
  43. typedef struct Mpeg1Context {
  44. MpegEncContext mpeg_enc_ctx;
  45. int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
  46. int repeat_field; /* true if we must repeat the field */
  47. AVPanScan pan_scan; /* some temporary storage for the panscan */
  48. AVStereo3D stereo3d;
  49. int has_stereo3d;
  50. uint8_t *a53_caption;
  51. int a53_caption_size;
  52. 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. int i;
  1006. /* we need some permutation to store matrices,
  1007. * until MPV_common_init() sets the real permutation. */
  1008. for (i = 0; i < 64; i++)
  1009. s2->idsp.idct_permutation[i] = i;
  1010. ff_MPV_decode_defaults(s2);
  1011. s->mpeg_enc_ctx.avctx = avctx;
  1012. s->mpeg_enc_ctx.flags = avctx->flags;
  1013. s->mpeg_enc_ctx.flags2 = avctx->flags2;
  1014. ff_mpeg12_common_init(&s->mpeg_enc_ctx);
  1015. ff_mpeg12_init_vlcs();
  1016. s->mpeg_enc_ctx_allocated = 0;
  1017. s->mpeg_enc_ctx.picture_number = 0;
  1018. s->repeat_field = 0;
  1019. s->mpeg_enc_ctx.codec_id = avctx->codec->id;
  1020. avctx->color_range = AVCOL_RANGE_MPEG;
  1021. if (avctx->codec->id == AV_CODEC_ID_MPEG1VIDEO)
  1022. avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
  1023. else
  1024. avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
  1025. return 0;
  1026. }
  1027. static int mpeg_decode_update_thread_context(AVCodecContext *avctx,
  1028. const AVCodecContext *avctx_from)
  1029. {
  1030. Mpeg1Context *ctx = avctx->priv_data, *ctx_from = avctx_from->priv_data;
  1031. MpegEncContext *s = &ctx->mpeg_enc_ctx, *s1 = &ctx_from->mpeg_enc_ctx;
  1032. int err;
  1033. if (avctx == avctx_from ||
  1034. !ctx_from->mpeg_enc_ctx_allocated ||
  1035. !s1->context_initialized)
  1036. return 0;
  1037. err = ff_mpeg_update_thread_context(avctx, avctx_from);
  1038. if (err)
  1039. return err;
  1040. if (!ctx->mpeg_enc_ctx_allocated)
  1041. memcpy(s + 1, s1 + 1, sizeof(Mpeg1Context) - sizeof(MpegEncContext));
  1042. if (!(s->pict_type == AV_PICTURE_TYPE_B || s->low_delay))
  1043. s->picture_number++;
  1044. return 0;
  1045. }
  1046. static void quant_matrix_rebuild(uint16_t *matrix, const uint8_t *old_perm,
  1047. const uint8_t *new_perm)
  1048. {
  1049. uint16_t temp_matrix[64];
  1050. int i;
  1051. memcpy(temp_matrix, matrix, 64 * sizeof(uint16_t));
  1052. for (i = 0; i < 64; i++)
  1053. matrix[new_perm[i]] = temp_matrix[old_perm[i]];
  1054. }
  1055. #if FF_API_XVMC
  1056. static const enum AVPixelFormat pixfmt_xvmc_mpg2_420[] = {
  1057. AV_PIX_FMT_XVMC_MPEG2_IDCT,
  1058. AV_PIX_FMT_XVMC_MPEG2_MC,
  1059. AV_PIX_FMT_NONE
  1060. };
  1061. #endif /* FF_API_XVMC */
  1062. static const enum AVPixelFormat mpeg12_hwaccel_pixfmt_list_420[] = {
  1063. #if CONFIG_MPEG2_DXVA2_HWACCEL
  1064. AV_PIX_FMT_DXVA2_VLD,
  1065. #endif
  1066. #if CONFIG_MPEG2_VAAPI_HWACCEL
  1067. AV_PIX_FMT_VAAPI_VLD,
  1068. #endif
  1069. #if CONFIG_MPEG1_VDPAU_HWACCEL | CONFIG_MPEG2_VDPAU_HWACCEL
  1070. AV_PIX_FMT_VDPAU,
  1071. #endif
  1072. AV_PIX_FMT_YUV420P,
  1073. AV_PIX_FMT_NONE
  1074. };
  1075. static enum AVPixelFormat mpeg_get_pixelformat(AVCodecContext *avctx)
  1076. {
  1077. Mpeg1Context *s1 = avctx->priv_data;
  1078. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1079. #if FF_API_XVMC
  1080. FF_DISABLE_DEPRECATION_WARNINGS
  1081. if (avctx->xvmc_acceleration)
  1082. return ff_get_format(avctx, pixfmt_xvmc_mpg2_420);
  1083. FF_ENABLE_DEPRECATION_WARNINGS
  1084. #endif /* FF_API_XVMC */
  1085. if (s->chroma_format < 2)
  1086. return ff_get_format(avctx, mpeg12_hwaccel_pixfmt_list_420);
  1087. else if (s->chroma_format == 2)
  1088. return AV_PIX_FMT_YUV422P;
  1089. else
  1090. return AV_PIX_FMT_YUV444P;
  1091. }
  1092. /* Call this function when we know all parameters.
  1093. * It may be called in different places for MPEG-1 and MPEG-2. */
  1094. static int mpeg_decode_postinit(AVCodecContext *avctx)
  1095. {
  1096. Mpeg1Context *s1 = avctx->priv_data;
  1097. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1098. uint8_t old_permutation[64];
  1099. int ret;
  1100. if ((s1->mpeg_enc_ctx_allocated == 0) ||
  1101. avctx->coded_width != s->width ||
  1102. avctx->coded_height != s->height ||
  1103. s1->save_width != s->width ||
  1104. s1->save_height != s->height ||
  1105. s1->save_aspect_info != s->aspect_ratio_info ||
  1106. s1->save_progressive_seq != s->progressive_sequence ||
  1107. 0) {
  1108. if (s1->mpeg_enc_ctx_allocated) {
  1109. ParseContext pc = s->parse_context;
  1110. s->parse_context.buffer = 0;
  1111. ff_MPV_common_end(s);
  1112. s->parse_context = pc;
  1113. }
  1114. if ((s->width == 0) || (s->height == 0))
  1115. return -2;
  1116. ret = ff_set_dimensions(avctx, s->width, s->height);
  1117. if (ret < 0)
  1118. return ret;
  1119. avctx->bit_rate = s->bit_rate;
  1120. s1->save_aspect_info = s->aspect_ratio_info;
  1121. s1->save_width = s->width;
  1122. s1->save_height = s->height;
  1123. s1->save_progressive_seq = s->progressive_sequence;
  1124. /* low_delay may be forced, in this case we will have B-frames
  1125. * that behave like P-frames. */
  1126. avctx->has_b_frames = !s->low_delay;
  1127. if (avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
  1128. // MPEG-1 fps
  1129. avctx->time_base.den = ff_mpeg12_frame_rate_tab[s->frame_rate_index].num;
  1130. avctx->time_base.num = ff_mpeg12_frame_rate_tab[s->frame_rate_index].den;
  1131. // MPEG-1 aspect
  1132. avctx->sample_aspect_ratio = av_d2q(1.0 / ff_mpeg1_aspect[s->aspect_ratio_info], 255);
  1133. avctx->ticks_per_frame = 1;
  1134. } else { // MPEG-2
  1135. // MPEG-2 fps
  1136. av_reduce(&s->avctx->time_base.den,
  1137. &s->avctx->time_base.num,
  1138. ff_mpeg12_frame_rate_tab[s->frame_rate_index].num * s1->frame_rate_ext.num * 2,
  1139. ff_mpeg12_frame_rate_tab[s->frame_rate_index].den * s1->frame_rate_ext.den,
  1140. 1 << 30);
  1141. avctx->ticks_per_frame = 2;
  1142. // MPEG-2 aspect
  1143. if (s->aspect_ratio_info > 1) {
  1144. AVRational dar =
  1145. av_mul_q(av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
  1146. (AVRational) { s1->pan_scan.width,
  1147. s1->pan_scan.height }),
  1148. (AVRational) { s->width, s->height });
  1149. /* We ignore the spec here and guess a bit as reality does not
  1150. * match the spec, see for example res_change_ffmpeg_aspect.ts
  1151. * and sequence-display-aspect.mpg.
  1152. * issue1613, 621, 562 */
  1153. if ((s1->pan_scan.width == 0) || (s1->pan_scan.height == 0) ||
  1154. (av_cmp_q(dar, (AVRational) { 4, 3 }) &&
  1155. av_cmp_q(dar, (AVRational) { 16, 9 }))) {
  1156. s->avctx->sample_aspect_ratio =
  1157. av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
  1158. (AVRational) { s->width, s->height });
  1159. } else {
  1160. s->avctx->sample_aspect_ratio =
  1161. av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
  1162. (AVRational) { s1->pan_scan.width, s1->pan_scan.height });
  1163. // issue1613 4/3 16/9 -> 16/9
  1164. // res_change_ffmpeg_aspect.ts 4/3 225/44 ->4/3
  1165. // widescreen-issue562.mpg 4/3 16/9 -> 16/9
  1166. // s->avctx->sample_aspect_ratio = av_mul_q(s->avctx->sample_aspect_ratio, (AVRational) {s->width, s->height});
  1167. av_dlog(avctx, "A %d/%d\n",
  1168. ff_mpeg2_aspect[s->aspect_ratio_info].num,
  1169. ff_mpeg2_aspect[s->aspect_ratio_info].den);
  1170. av_dlog(avctx, "B %d/%d\n", s->avctx->sample_aspect_ratio.num,
  1171. s->avctx->sample_aspect_ratio.den);
  1172. }
  1173. } else {
  1174. s->avctx->sample_aspect_ratio =
  1175. ff_mpeg2_aspect[s->aspect_ratio_info];
  1176. }
  1177. } // MPEG-2
  1178. ff_set_sar(s->avctx, s->avctx->sample_aspect_ratio);
  1179. avctx->pix_fmt = mpeg_get_pixelformat(avctx);
  1180. // until then pix_fmt may be changed right after codec init
  1181. #if FF_API_XVMC
  1182. if ((avctx->pix_fmt == AV_PIX_FMT_XVMC_MPEG2_IDCT ||
  1183. avctx->hwaccel) && avctx->idct_algo == FF_IDCT_AUTO)
  1184. #else
  1185. if (avctx->hwaccel && avctx->idct_algo == FF_IDCT_AUTO)
  1186. #endif /* FF_API_XVMC */
  1187. avctx->idct_algo = FF_IDCT_SIMPLE;
  1188. /* Quantization matrices may need reordering
  1189. * if DCT permutation is changed. */
  1190. memcpy(old_permutation, s->idsp.idct_permutation, 64 * sizeof(uint8_t));
  1191. if (ff_MPV_common_init(s) < 0)
  1192. return -2;
  1193. quant_matrix_rebuild(s->intra_matrix, old_permutation, s->idsp.idct_permutation);
  1194. quant_matrix_rebuild(s->inter_matrix, old_permutation, s->idsp.idct_permutation);
  1195. quant_matrix_rebuild(s->chroma_intra_matrix, old_permutation, s->idsp.idct_permutation);
  1196. quant_matrix_rebuild(s->chroma_inter_matrix, old_permutation, s->idsp.idct_permutation);
  1197. s1->mpeg_enc_ctx_allocated = 1;
  1198. }
  1199. return 0;
  1200. }
  1201. static int mpeg1_decode_picture(AVCodecContext *avctx, const uint8_t *buf,
  1202. int buf_size)
  1203. {
  1204. Mpeg1Context *s1 = avctx->priv_data;
  1205. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1206. int ref, f_code, vbv_delay;
  1207. init_get_bits(&s->gb, buf, buf_size * 8);
  1208. ref = get_bits(&s->gb, 10); /* temporal ref */
  1209. s->pict_type = get_bits(&s->gb, 3);
  1210. if (s->pict_type == 0 || s->pict_type > 3)
  1211. return -1;
  1212. vbv_delay = get_bits(&s->gb, 16);
  1213. if (s->pict_type == AV_PICTURE_TYPE_P ||
  1214. s->pict_type == AV_PICTURE_TYPE_B) {
  1215. s->full_pel[0] = get_bits1(&s->gb);
  1216. f_code = get_bits(&s->gb, 3);
  1217. if (f_code == 0 && (avctx->err_recognition & AV_EF_BITSTREAM))
  1218. return -1;
  1219. s->mpeg_f_code[0][0] = f_code;
  1220. s->mpeg_f_code[0][1] = f_code;
  1221. }
  1222. if (s->pict_type == AV_PICTURE_TYPE_B) {
  1223. s->full_pel[1] = get_bits1(&s->gb);
  1224. f_code = get_bits(&s->gb, 3);
  1225. if (f_code == 0 && (avctx->err_recognition & AV_EF_BITSTREAM))
  1226. return -1;
  1227. s->mpeg_f_code[1][0] = f_code;
  1228. s->mpeg_f_code[1][1] = f_code;
  1229. }
  1230. s->current_picture.f->pict_type = s->pict_type;
  1231. s->current_picture.f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
  1232. if (avctx->debug & FF_DEBUG_PICT_INFO)
  1233. av_log(avctx, AV_LOG_DEBUG,
  1234. "vbv_delay %d, ref %d type:%d\n", vbv_delay, ref, s->pict_type);
  1235. s->y_dc_scale = 8;
  1236. s->c_dc_scale = 8;
  1237. return 0;
  1238. }
  1239. static void mpeg_decode_sequence_extension(Mpeg1Context *s1)
  1240. {
  1241. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1242. int horiz_size_ext, vert_size_ext;
  1243. int bit_rate_ext;
  1244. skip_bits(&s->gb, 1); /* profile and level esc*/
  1245. s->avctx->profile = get_bits(&s->gb, 3);
  1246. s->avctx->level = get_bits(&s->gb, 4);
  1247. s->progressive_sequence = get_bits1(&s->gb); /* progressive_sequence */
  1248. s->chroma_format = get_bits(&s->gb, 2); /* chroma_format 1=420, 2=422, 3=444 */
  1249. horiz_size_ext = get_bits(&s->gb, 2);
  1250. vert_size_ext = get_bits(&s->gb, 2);
  1251. s->width |= (horiz_size_ext << 12);
  1252. s->height |= (vert_size_ext << 12);
  1253. bit_rate_ext = get_bits(&s->gb, 12); /* XXX: handle it */
  1254. s->bit_rate += (bit_rate_ext << 18) * 400;
  1255. skip_bits1(&s->gb); /* marker */
  1256. s->avctx->rc_buffer_size += get_bits(&s->gb, 8) * 1024 * 16 << 10;
  1257. s->low_delay = get_bits1(&s->gb);
  1258. if (s->flags & CODEC_FLAG_LOW_DELAY)
  1259. s->low_delay = 1;
  1260. s1->frame_rate_ext.num = get_bits(&s->gb, 2) + 1;
  1261. s1->frame_rate_ext.den = get_bits(&s->gb, 5) + 1;
  1262. av_dlog(s->avctx, "sequence extension\n");
  1263. s->codec_id = s->avctx->codec_id = AV_CODEC_ID_MPEG2VIDEO;
  1264. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  1265. av_log(s->avctx, AV_LOG_DEBUG,
  1266. "profile: %d, level: %d vbv buffer: %d, bitrate:%d\n",
  1267. s->avctx->profile, s->avctx->level,
  1268. s->avctx->rc_buffer_size, s->bit_rate);
  1269. }
  1270. static void mpeg_decode_sequence_display_extension(Mpeg1Context *s1)
  1271. {
  1272. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1273. int color_description, w, h;
  1274. skip_bits(&s->gb, 3); /* video format */
  1275. color_description = get_bits1(&s->gb);
  1276. if (color_description) {
  1277. s->avctx->color_primaries = get_bits(&s->gb, 8);
  1278. s->avctx->color_trc = get_bits(&s->gb, 8);
  1279. s->avctx->colorspace = get_bits(&s->gb, 8);
  1280. }
  1281. w = get_bits(&s->gb, 14);
  1282. skip_bits(&s->gb, 1); // marker
  1283. h = get_bits(&s->gb, 14);
  1284. // remaining 3 bits are zero padding
  1285. s1->pan_scan.width = 16 * w;
  1286. s1->pan_scan.height = 16 * h;
  1287. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  1288. av_log(s->avctx, AV_LOG_DEBUG, "sde w:%d, h:%d\n", w, h);
  1289. }
  1290. static void mpeg_decode_picture_display_extension(Mpeg1Context *s1)
  1291. {
  1292. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1293. int i, nofco;
  1294. nofco = 1;
  1295. if (s->progressive_sequence) {
  1296. if (s->repeat_first_field) {
  1297. nofco++;
  1298. if (s->top_field_first)
  1299. nofco++;
  1300. }
  1301. } else {
  1302. if (s->picture_structure == PICT_FRAME) {
  1303. nofco++;
  1304. if (s->repeat_first_field)
  1305. nofco++;
  1306. }
  1307. }
  1308. for (i = 0; i < nofco; i++) {
  1309. s1->pan_scan.position[i][0] = get_sbits(&s->gb, 16);
  1310. skip_bits(&s->gb, 1); // marker
  1311. s1->pan_scan.position[i][1] = get_sbits(&s->gb, 16);
  1312. skip_bits(&s->gb, 1); // marker
  1313. }
  1314. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  1315. av_log(s->avctx, AV_LOG_DEBUG,
  1316. "pde (%"PRId16",%"PRId16") (%"PRId16",%"PRId16") (%"PRId16",%"PRId16")\n",
  1317. s1->pan_scan.position[0][0], s1->pan_scan.position[0][1],
  1318. s1->pan_scan.position[1][0], s1->pan_scan.position[1][1],
  1319. s1->pan_scan.position[2][0], s1->pan_scan.position[2][1]);
  1320. }
  1321. static int load_matrix(MpegEncContext *s, uint16_t matrix0[64],
  1322. uint16_t matrix1[64], int intra)
  1323. {
  1324. int i;
  1325. for (i = 0; i < 64; i++) {
  1326. int j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
  1327. int v = get_bits(&s->gb, 8);
  1328. if (v == 0) {
  1329. av_log(s->avctx, AV_LOG_ERROR, "matrix damaged\n");
  1330. return -1;
  1331. }
  1332. if (intra && i == 0 && v != 8) {
  1333. av_log(s->avctx, AV_LOG_ERROR, "intra matrix invalid, ignoring\n");
  1334. v = 8; // needed by pink.mpg / issue1046
  1335. }
  1336. matrix0[j] = v;
  1337. if (matrix1)
  1338. matrix1[j] = v;
  1339. }
  1340. return 0;
  1341. }
  1342. static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
  1343. {
  1344. av_dlog(s->avctx, "matrix extension\n");
  1345. if (get_bits1(&s->gb))
  1346. load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
  1347. if (get_bits1(&s->gb))
  1348. load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
  1349. if (get_bits1(&s->gb))
  1350. load_matrix(s, s->chroma_intra_matrix, NULL, 1);
  1351. if (get_bits1(&s->gb))
  1352. load_matrix(s, s->chroma_inter_matrix, NULL, 0);
  1353. }
  1354. static void mpeg_decode_picture_coding_extension(Mpeg1Context *s1)
  1355. {
  1356. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1357. s->full_pel[0] = s->full_pel[1] = 0;
  1358. s->mpeg_f_code[0][0] = get_bits(&s->gb, 4);
  1359. s->mpeg_f_code[0][1] = get_bits(&s->gb, 4);
  1360. s->mpeg_f_code[1][0] = get_bits(&s->gb, 4);
  1361. s->mpeg_f_code[1][1] = get_bits(&s->gb, 4);
  1362. if (!s->pict_type && s1->mpeg_enc_ctx_allocated) {
  1363. av_log(s->avctx, AV_LOG_ERROR,
  1364. "Missing picture start code, guessing missing values\n");
  1365. if (s->mpeg_f_code[1][0] == 15 && s->mpeg_f_code[1][1] == 15) {
  1366. if (s->mpeg_f_code[0][0] == 15 && s->mpeg_f_code[0][1] == 15)
  1367. s->pict_type = AV_PICTURE_TYPE_I;
  1368. else
  1369. s->pict_type = AV_PICTURE_TYPE_P;
  1370. } else
  1371. s->pict_type = AV_PICTURE_TYPE_B;
  1372. s->current_picture.f->pict_type = s->pict_type;
  1373. s->current_picture.f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
  1374. }
  1375. s->intra_dc_precision = get_bits(&s->gb, 2);
  1376. s->picture_structure = get_bits(&s->gb, 2);
  1377. s->top_field_first = get_bits1(&s->gb);
  1378. s->frame_pred_frame_dct = get_bits1(&s->gb);
  1379. s->concealment_motion_vectors = get_bits1(&s->gb);
  1380. s->q_scale_type = get_bits1(&s->gb);
  1381. s->intra_vlc_format = get_bits1(&s->gb);
  1382. s->alternate_scan = get_bits1(&s->gb);
  1383. s->repeat_first_field = get_bits1(&s->gb);
  1384. s->chroma_420_type = get_bits1(&s->gb);
  1385. s->progressive_frame = get_bits1(&s->gb);
  1386. if (s->progressive_sequence && !s->progressive_frame) {
  1387. s->progressive_frame = 1;
  1388. av_log(s->avctx, AV_LOG_ERROR,
  1389. "interlaced frame in progressive sequence, ignoring\n");
  1390. }
  1391. if (s->picture_structure == 0 ||
  1392. (s->progressive_frame && s->picture_structure != PICT_FRAME)) {
  1393. av_log(s->avctx, AV_LOG_ERROR,
  1394. "picture_structure %d invalid, ignoring\n",
  1395. s->picture_structure);
  1396. s->picture_structure = PICT_FRAME;
  1397. }
  1398. if (s->progressive_sequence && !s->frame_pred_frame_dct)
  1399. av_log(s->avctx, AV_LOG_WARNING, "invalid frame_pred_frame_dct\n");
  1400. if (s->picture_structure == PICT_FRAME) {
  1401. s->first_field = 0;
  1402. s->v_edge_pos = 16 * s->mb_height;
  1403. } else {
  1404. s->first_field ^= 1;
  1405. s->v_edge_pos = 8 * s->mb_height;
  1406. memset(s->mbskip_table, 0, s->mb_stride * s->mb_height);
  1407. }
  1408. if (s->alternate_scan) {
  1409. ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_alternate_vertical_scan);
  1410. ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_alternate_vertical_scan);
  1411. } else {
  1412. ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_zigzag_direct);
  1413. ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_zigzag_direct);
  1414. }
  1415. /* composite display not parsed */
  1416. av_dlog(s->avctx, "intra_dc_precision=%d\n", s->intra_dc_precision);
  1417. av_dlog(s->avctx, "picture_structure=%d\n", s->picture_structure);
  1418. av_dlog(s->avctx, "top field first=%d\n", s->top_field_first);
  1419. av_dlog(s->avctx, "repeat first field=%d\n", s->repeat_first_field);
  1420. av_dlog(s->avctx, "conceal=%d\n", s->concealment_motion_vectors);
  1421. av_dlog(s->avctx, "intra_vlc_format=%d\n", s->intra_vlc_format);
  1422. av_dlog(s->avctx, "alternate_scan=%d\n", s->alternate_scan);
  1423. av_dlog(s->avctx, "frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
  1424. av_dlog(s->avctx, "progressive_frame=%d\n", s->progressive_frame);
  1425. }
  1426. static int mpeg_field_start(MpegEncContext *s, const uint8_t *buf, int buf_size)
  1427. {
  1428. AVCodecContext *avctx = s->avctx;
  1429. Mpeg1Context *s1 = (Mpeg1Context *) s;
  1430. /* start frame decoding */
  1431. if (s->first_field || s->picture_structure == PICT_FRAME) {
  1432. AVFrameSideData *pan_scan;
  1433. if (ff_MPV_frame_start(s, avctx) < 0)
  1434. return -1;
  1435. ff_mpeg_er_frame_start(s);
  1436. /* first check if we must repeat the frame */
  1437. s->current_picture_ptr->f->repeat_pict = 0;
  1438. if (s->repeat_first_field) {
  1439. if (s->progressive_sequence) {
  1440. if (s->top_field_first)
  1441. s->current_picture_ptr->f->repeat_pict = 4;
  1442. else
  1443. s->current_picture_ptr->f->repeat_pict = 2;
  1444. } else if (s->progressive_frame) {
  1445. s->current_picture_ptr->f->repeat_pict = 1;
  1446. }
  1447. }
  1448. pan_scan = av_frame_new_side_data(s->current_picture_ptr->f,
  1449. AV_FRAME_DATA_PANSCAN,
  1450. sizeof(s1->pan_scan));
  1451. if (!pan_scan)
  1452. return AVERROR(ENOMEM);
  1453. memcpy(pan_scan->data, &s1->pan_scan, sizeof(s1->pan_scan));
  1454. if (s1->a53_caption) {
  1455. AVFrameSideData *sd = av_frame_new_side_data(
  1456. s->current_picture_ptr->f, AV_FRAME_DATA_A53_CC,
  1457. s1->a53_caption_size);
  1458. if (sd)
  1459. memcpy(sd->data, s1->a53_caption, s1->a53_caption_size);
  1460. av_freep(&s1->a53_caption);
  1461. }
  1462. if (s1->has_stereo3d) {
  1463. AVStereo3D *stereo = av_stereo3d_create_side_data(s->current_picture_ptr->f);
  1464. if (!stereo)
  1465. return AVERROR(ENOMEM);
  1466. *stereo = s1->stereo3d;
  1467. s1->has_stereo3d = 0;
  1468. }
  1469. if (s1->has_afd) {
  1470. AVFrameSideData *sd =
  1471. av_frame_new_side_data(s->current_picture_ptr->f,
  1472. AV_FRAME_DATA_AFD, 1);
  1473. if (!sd)
  1474. return AVERROR(ENOMEM);
  1475. *sd->data = s1->afd;
  1476. s1->has_afd = 0;
  1477. }
  1478. if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_FRAME))
  1479. ff_thread_finish_setup(avctx);
  1480. } else { // second field
  1481. int i;
  1482. if (!s->current_picture_ptr) {
  1483. av_log(s->avctx, AV_LOG_ERROR, "first field missing\n");
  1484. return -1;
  1485. }
  1486. if (s->avctx->hwaccel &&
  1487. (s->avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD)) {
  1488. if (s->avctx->hwaccel->end_frame(s->avctx) < 0)
  1489. av_log(avctx, AV_LOG_ERROR,
  1490. "hardware accelerator failed to decode first field\n");
  1491. }
  1492. for (i = 0; i < 4; i++) {
  1493. s->current_picture.f->data[i] = s->current_picture_ptr->f->data[i];
  1494. if (s->picture_structure == PICT_BOTTOM_FIELD)
  1495. s->current_picture.f->data[i] +=
  1496. s->current_picture_ptr->f->linesize[i];
  1497. }
  1498. }
  1499. if (avctx->hwaccel) {
  1500. if (avctx->hwaccel->start_frame(avctx, buf, buf_size) < 0)
  1501. return -1;
  1502. }
  1503. #if FF_API_XVMC
  1504. FF_DISABLE_DEPRECATION_WARNINGS
  1505. // MPV_frame_start will call this function too,
  1506. // but we need to call it on every field
  1507. if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
  1508. if (ff_xvmc_field_start(s, avctx) < 0)
  1509. return -1;
  1510. FF_ENABLE_DEPRECATION_WARNINGS
  1511. #endif /* FF_API_XVMC */
  1512. return 0;
  1513. }
  1514. #define DECODE_SLICE_ERROR -1
  1515. #define DECODE_SLICE_OK 0
  1516. /**
  1517. * Decode a slice.
  1518. * MpegEncContext.mb_y must be set to the MB row from the startcode.
  1519. * @return DECODE_SLICE_ERROR if the slice is damaged,
  1520. * DECODE_SLICE_OK if this slice is OK
  1521. */
  1522. static int mpeg_decode_slice(MpegEncContext *s, int mb_y,
  1523. const uint8_t **buf, int buf_size)
  1524. {
  1525. AVCodecContext *avctx = s->avctx;
  1526. const int field_pic = s->picture_structure != PICT_FRAME;
  1527. s->resync_mb_x =
  1528. s->resync_mb_y = -1;
  1529. assert(mb_y < s->mb_height);
  1530. init_get_bits(&s->gb, *buf, buf_size * 8);
  1531. ff_mpeg1_clean_buffers(s);
  1532. s->interlaced_dct = 0;
  1533. s->qscale = get_qscale(s);
  1534. if (s->qscale == 0) {
  1535. av_log(s->avctx, AV_LOG_ERROR, "qscale == 0\n");
  1536. return -1;
  1537. }
  1538. /* extra slice info */
  1539. while (get_bits1(&s->gb) != 0)
  1540. skip_bits(&s->gb, 8);
  1541. s->mb_x = 0;
  1542. if (mb_y == 0 && s->codec_tag == AV_RL32("SLIF")) {
  1543. skip_bits1(&s->gb);
  1544. } else {
  1545. while (get_bits_left(&s->gb) > 0) {
  1546. int code = get_vlc2(&s->gb, ff_mbincr_vlc.table,
  1547. MBINCR_VLC_BITS, 2);
  1548. if (code < 0) {
  1549. av_log(s->avctx, AV_LOG_ERROR, "first mb_incr damaged\n");
  1550. return -1;
  1551. }
  1552. if (code >= 33) {
  1553. if (code == 33)
  1554. s->mb_x += 33;
  1555. /* otherwise, stuffing, nothing to do */
  1556. } else {
  1557. s->mb_x += code;
  1558. break;
  1559. }
  1560. }
  1561. }
  1562. if (s->mb_x >= (unsigned) s->mb_width) {
  1563. av_log(s->avctx, AV_LOG_ERROR, "initial skip overflow\n");
  1564. return -1;
  1565. }
  1566. if (avctx->hwaccel) {
  1567. const uint8_t *buf_end, *buf_start = *buf - 4; /* include start_code */
  1568. int start_code = -1;
  1569. buf_end = avpriv_find_start_code(buf_start + 2, *buf + buf_size, &start_code);
  1570. if (buf_end < *buf + buf_size)
  1571. buf_end -= 4;
  1572. s->mb_y = mb_y;
  1573. if (avctx->hwaccel->decode_slice(avctx, buf_start, buf_end - buf_start) < 0)
  1574. return DECODE_SLICE_ERROR;
  1575. *buf = buf_end;
  1576. return DECODE_SLICE_OK;
  1577. }
  1578. s->resync_mb_x = s->mb_x;
  1579. s->resync_mb_y = s->mb_y = mb_y;
  1580. s->mb_skip_run = 0;
  1581. ff_init_block_index(s);
  1582. if (s->mb_y == 0 && s->mb_x == 0 && (s->first_field || s->picture_structure == PICT_FRAME)) {
  1583. if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
  1584. av_log(s->avctx, AV_LOG_DEBUG,
  1585. "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",
  1586. s->qscale,
  1587. s->mpeg_f_code[0][0], s->mpeg_f_code[0][1],
  1588. s->mpeg_f_code[1][0], s->mpeg_f_code[1][1],
  1589. s->pict_type == AV_PICTURE_TYPE_I ? "I" :
  1590. (s->pict_type == AV_PICTURE_TYPE_P ? "P" :
  1591. (s->pict_type == AV_PICTURE_TYPE_B ? "B" : "S")),
  1592. s->progressive_sequence ? "ps" : "",
  1593. s->progressive_frame ? "pf" : "",
  1594. s->alternate_scan ? "alt" : "",
  1595. s->top_field_first ? "top" : "",
  1596. s->intra_dc_precision, s->picture_structure,
  1597. s->frame_pred_frame_dct, s->concealment_motion_vectors,
  1598. s->q_scale_type, s->intra_vlc_format,
  1599. s->repeat_first_field, s->chroma_420_type ? "420" : "");
  1600. }
  1601. }
  1602. for (;;) {
  1603. #if FF_API_XVMC
  1604. FF_DISABLE_DEPRECATION_WARNINGS
  1605. // If 1, we memcpy blocks in xvmcvideo.
  1606. if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1)
  1607. ff_xvmc_init_block(s); // set s->block
  1608. FF_ENABLE_DEPRECATION_WARNINGS
  1609. #endif /* FF_API_XVMC */
  1610. if (mpeg_decode_mb(s, s->block) < 0)
  1611. return -1;
  1612. // Note motion_val is normally NULL unless we want to extract the MVs.
  1613. if (s->current_picture.motion_val[0] && !s->encoding) {
  1614. const int wrap = s->b8_stride;
  1615. int xy = s->mb_x * 2 + s->mb_y * 2 * wrap;
  1616. int b8_xy = 4 * (s->mb_x + s->mb_y * s->mb_stride);
  1617. int motion_x, motion_y, dir, i;
  1618. for (i = 0; i < 2; i++) {
  1619. for (dir = 0; dir < 2; dir++) {
  1620. if (s->mb_intra ||
  1621. (dir == 1 && s->pict_type != AV_PICTURE_TYPE_B)) {
  1622. motion_x = motion_y = 0;
  1623. } else if (s->mv_type == MV_TYPE_16X16 ||
  1624. (s->mv_type == MV_TYPE_FIELD && field_pic)) {
  1625. motion_x = s->mv[dir][0][0];
  1626. motion_y = s->mv[dir][0][1];
  1627. } else { /* if ((s->mv_type == MV_TYPE_FIELD) || (s->mv_type == MV_TYPE_16X8)) */
  1628. motion_x = s->mv[dir][i][0];
  1629. motion_y = s->mv[dir][i][1];
  1630. }
  1631. s->current_picture.motion_val[dir][xy][0] = motion_x;
  1632. s->current_picture.motion_val[dir][xy][1] = motion_y;
  1633. s->current_picture.motion_val[dir][xy + 1][0] = motion_x;
  1634. s->current_picture.motion_val[dir][xy + 1][1] = motion_y;
  1635. s->current_picture.ref_index [dir][b8_xy] =
  1636. s->current_picture.ref_index [dir][b8_xy + 1] = s->field_select[dir][i];
  1637. assert(s->field_select[dir][i] == 0 ||
  1638. s->field_select[dir][i] == 1);
  1639. }
  1640. xy += wrap;
  1641. b8_xy += 2;
  1642. }
  1643. }
  1644. s->dest[0] += 16;
  1645. s->dest[1] += 16 >> s->chroma_x_shift;
  1646. s->dest[2] += 16 >> s->chroma_x_shift;
  1647. ff_MPV_decode_mb(s, s->block);
  1648. if (++s->mb_x >= s->mb_width) {
  1649. const int mb_size = 16;
  1650. ff_mpeg_draw_horiz_band(s, mb_size * (s->mb_y >> field_pic), mb_size);
  1651. ff_MPV_report_decode_progress(s);
  1652. s->mb_x = 0;
  1653. s->mb_y += 1 << field_pic;
  1654. if (s->mb_y >= s->mb_height) {
  1655. int left = get_bits_left(&s->gb);
  1656. int is_d10 = s->chroma_format == 2 &&
  1657. s->pict_type == AV_PICTURE_TYPE_I &&
  1658. avctx->profile == 0 && avctx->level == 5 &&
  1659. s->intra_dc_precision == 2 &&
  1660. s->q_scale_type == 1 && s->alternate_scan == 0 &&
  1661. s->progressive_frame == 0
  1662. /* vbv_delay == 0xBBB || 0xE10 */;
  1663. if (left < 0 ||
  1664. (left && show_bits(&s->gb, FFMIN(left, 23)) && !is_d10) ||
  1665. ((avctx->err_recognition & AV_EF_BUFFER) && left > 8)) {
  1666. av_log(avctx, AV_LOG_ERROR, "end mismatch left=%d %0X\n",
  1667. left, show_bits(&s->gb, FFMIN(left, 23)));
  1668. return -1;
  1669. } else
  1670. goto eos;
  1671. }
  1672. ff_init_block_index(s);
  1673. }
  1674. /* skip mb handling */
  1675. if (s->mb_skip_run == -1) {
  1676. /* read increment again */
  1677. s->mb_skip_run = 0;
  1678. for (;;) {
  1679. int code = get_vlc2(&s->gb, ff_mbincr_vlc.table,
  1680. MBINCR_VLC_BITS, 2);
  1681. if (code < 0) {
  1682. av_log(s->avctx, AV_LOG_ERROR, "mb incr damaged\n");
  1683. return -1;
  1684. }
  1685. if (code >= 33) {
  1686. if (code == 33) {
  1687. s->mb_skip_run += 33;
  1688. } else if (code == 35) {
  1689. if (s->mb_skip_run != 0 || show_bits(&s->gb, 15) != 0) {
  1690. av_log(s->avctx, AV_LOG_ERROR, "slice mismatch\n");
  1691. return -1;
  1692. }
  1693. goto eos; /* end of slice */
  1694. }
  1695. /* otherwise, stuffing, nothing to do */
  1696. } else {
  1697. s->mb_skip_run += code;
  1698. break;
  1699. }
  1700. }
  1701. if (s->mb_skip_run) {
  1702. int i;
  1703. if (s->pict_type == AV_PICTURE_TYPE_I) {
  1704. av_log(s->avctx, AV_LOG_ERROR,
  1705. "skipped MB in I frame at %d %d\n", s->mb_x, s->mb_y);
  1706. return -1;
  1707. }
  1708. /* skip mb */
  1709. s->mb_intra = 0;
  1710. for (i = 0; i < 12; i++)
  1711. s->block_last_index[i] = -1;
  1712. if (s->picture_structure == PICT_FRAME)
  1713. s->mv_type = MV_TYPE_16X16;
  1714. else
  1715. s->mv_type = MV_TYPE_FIELD;
  1716. if (s->pict_type == AV_PICTURE_TYPE_P) {
  1717. /* if P type, zero motion vector is implied */
  1718. s->mv_dir = MV_DIR_FORWARD;
  1719. s->mv[0][0][0] = s->mv[0][0][1] = 0;
  1720. s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
  1721. s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
  1722. s->field_select[0][0] = (s->picture_structure - 1) & 1;
  1723. } else {
  1724. /* if B type, reuse previous vectors and directions */
  1725. s->mv[0][0][0] = s->last_mv[0][0][0];
  1726. s->mv[0][0][1] = s->last_mv[0][0][1];
  1727. s->mv[1][0][0] = s->last_mv[1][0][0];
  1728. s->mv[1][0][1] = s->last_mv[1][0][1];
  1729. }
  1730. }
  1731. }
  1732. }
  1733. eos: // end of slice
  1734. *buf += (get_bits_count(&s->gb) - 1) / 8;
  1735. av_dlog(s, "y %d %d %d %d\n", s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y);
  1736. return 0;
  1737. }
  1738. static int slice_decode_thread(AVCodecContext *c, void *arg)
  1739. {
  1740. MpegEncContext *s = *(void **) arg;
  1741. const uint8_t *buf = s->gb.buffer;
  1742. int mb_y = s->start_mb_y;
  1743. const int field_pic = s->picture_structure != PICT_FRAME;
  1744. s->er.error_count = (3 * (s->end_mb_y - s->start_mb_y) * s->mb_width) >> field_pic;
  1745. for (;;) {
  1746. uint32_t start_code;
  1747. int ret;
  1748. ret = mpeg_decode_slice(s, mb_y, &buf, s->gb.buffer_end - buf);
  1749. emms_c();
  1750. av_dlog(c, "ret:%d resync:%d/%d mb:%d/%d ts:%d/%d ec:%d\n",
  1751. ret, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y,
  1752. s->start_mb_y, s->end_mb_y, s->er.error_count);
  1753. if (ret < 0) {
  1754. if (c->err_recognition & AV_EF_EXPLODE)
  1755. return ret;
  1756. if (s->resync_mb_x >= 0 && s->resync_mb_y >= 0)
  1757. ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
  1758. s->mb_x, s->mb_y,
  1759. ER_AC_ERROR | ER_DC_ERROR | ER_MV_ERROR);
  1760. } else {
  1761. ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
  1762. s->mb_x - 1, s->mb_y,
  1763. ER_AC_END | ER_DC_END | ER_MV_END);
  1764. }
  1765. if (s->mb_y == s->end_mb_y)
  1766. return 0;
  1767. start_code = -1;
  1768. buf = avpriv_find_start_code(buf, s->gb.buffer_end, &start_code);
  1769. mb_y = (start_code - SLICE_MIN_START_CODE) << field_pic;
  1770. if (s->picture_structure == PICT_BOTTOM_FIELD)
  1771. mb_y++;
  1772. if (mb_y < 0 || mb_y >= s->end_mb_y)
  1773. return -1;
  1774. }
  1775. }
  1776. /**
  1777. * Handle slice ends.
  1778. * @return 1 if it seems to be the last slice
  1779. */
  1780. static int slice_end(AVCodecContext *avctx, AVFrame *pict)
  1781. {
  1782. Mpeg1Context *s1 = avctx->priv_data;
  1783. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1784. if (!s1->mpeg_enc_ctx_allocated || !s->current_picture_ptr)
  1785. return 0;
  1786. if (s->avctx->hwaccel) {
  1787. if (s->avctx->hwaccel->end_frame(s->avctx) < 0)
  1788. av_log(avctx, AV_LOG_ERROR,
  1789. "hardware accelerator failed to decode picture\n");
  1790. }
  1791. #if FF_API_XVMC
  1792. FF_DISABLE_DEPRECATION_WARNINGS
  1793. if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
  1794. ff_xvmc_field_end(s);
  1795. FF_ENABLE_DEPRECATION_WARNINGS
  1796. #endif /* FF_API_XVMC */
  1797. /* end of slice reached */
  1798. if (/* s->mb_y << field_pic == s->mb_height && */ !s->first_field) {
  1799. /* end of image */
  1800. ff_er_frame_end(&s->er);
  1801. ff_MPV_frame_end(s);
  1802. if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
  1803. int ret = av_frame_ref(pict, s->current_picture_ptr->f);
  1804. if (ret < 0)
  1805. return ret;
  1806. ff_print_debug_info(s, s->current_picture_ptr);
  1807. } else {
  1808. if (avctx->active_thread_type & FF_THREAD_FRAME)
  1809. s->picture_number++;
  1810. /* latency of 1 frame for I- and P-frames */
  1811. /* XXX: use another variable than picture_number */
  1812. if (s->last_picture_ptr != NULL) {
  1813. int ret = av_frame_ref(pict, s->last_picture_ptr->f);
  1814. if (ret < 0)
  1815. return ret;
  1816. ff_print_debug_info(s, s->last_picture_ptr);
  1817. }
  1818. }
  1819. return 1;
  1820. } else {
  1821. return 0;
  1822. }
  1823. }
  1824. static int mpeg1_decode_sequence(AVCodecContext *avctx,
  1825. const uint8_t *buf, int buf_size)
  1826. {
  1827. Mpeg1Context *s1 = avctx->priv_data;
  1828. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1829. int width, height;
  1830. int i, v, j;
  1831. init_get_bits(&s->gb, buf, buf_size * 8);
  1832. width = get_bits(&s->gb, 12);
  1833. height = get_bits(&s->gb, 12);
  1834. if (width == 0 || height == 0) {
  1835. av_log(avctx, AV_LOG_WARNING,
  1836. "Invalid horizontal or vertical size value.\n");
  1837. if (avctx->err_recognition & AV_EF_BITSTREAM)
  1838. return AVERROR_INVALIDDATA;
  1839. }
  1840. s->aspect_ratio_info = get_bits(&s->gb, 4);
  1841. if (s->aspect_ratio_info == 0) {
  1842. av_log(avctx, AV_LOG_ERROR, "aspect ratio has forbidden 0 value\n");
  1843. if (avctx->err_recognition & AV_EF_BITSTREAM)
  1844. return -1;
  1845. }
  1846. s->frame_rate_index = get_bits(&s->gb, 4);
  1847. if (s->frame_rate_index == 0 || s->frame_rate_index > 13)
  1848. return -1;
  1849. s->bit_rate = get_bits(&s->gb, 18) * 400;
  1850. if (get_bits1(&s->gb) == 0) /* marker */
  1851. return -1;
  1852. s->width = width;
  1853. s->height = height;
  1854. s->avctx->rc_buffer_size = get_bits(&s->gb, 10) * 1024 * 16;
  1855. skip_bits(&s->gb, 1);
  1856. /* get matrix */
  1857. if (get_bits1(&s->gb)) {
  1858. load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
  1859. } else {
  1860. for (i = 0; i < 64; i++) {
  1861. j = s->idsp.idct_permutation[i];
  1862. v = ff_mpeg1_default_intra_matrix[i];
  1863. s->intra_matrix[j] = v;
  1864. s->chroma_intra_matrix[j] = v;
  1865. }
  1866. }
  1867. if (get_bits1(&s->gb)) {
  1868. load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
  1869. } else {
  1870. for (i = 0; i < 64; i++) {
  1871. int j = s->idsp.idct_permutation[i];
  1872. v = ff_mpeg1_default_non_intra_matrix[i];
  1873. s->inter_matrix[j] = v;
  1874. s->chroma_inter_matrix[j] = v;
  1875. }
  1876. }
  1877. if (show_bits(&s->gb, 23) != 0) {
  1878. av_log(s->avctx, AV_LOG_ERROR, "sequence header damaged\n");
  1879. return -1;
  1880. }
  1881. /* We set MPEG-2 parameters so that it emulates MPEG-1. */
  1882. s->progressive_sequence = 1;
  1883. s->progressive_frame = 1;
  1884. s->picture_structure = PICT_FRAME;
  1885. s->frame_pred_frame_dct = 1;
  1886. s->chroma_format = 1;
  1887. s->codec_id =
  1888. s->avctx->codec_id = AV_CODEC_ID_MPEG1VIDEO;
  1889. s->out_format = FMT_MPEG1;
  1890. if (s->flags & CODEC_FLAG_LOW_DELAY)
  1891. s->low_delay = 1;
  1892. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  1893. av_log(s->avctx, AV_LOG_DEBUG, "vbv buffer: %d, bitrate:%d\n",
  1894. s->avctx->rc_buffer_size, s->bit_rate);
  1895. return 0;
  1896. }
  1897. static int vcr2_init_sequence(AVCodecContext *avctx)
  1898. {
  1899. Mpeg1Context *s1 = avctx->priv_data;
  1900. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1901. int i, v;
  1902. /* start new MPEG-1 context decoding */
  1903. s->out_format = FMT_MPEG1;
  1904. if (s1->mpeg_enc_ctx_allocated) {
  1905. ff_MPV_common_end(s);
  1906. }
  1907. s->width = avctx->coded_width;
  1908. s->height = avctx->coded_height;
  1909. avctx->has_b_frames = 0; // true?
  1910. s->low_delay = 1;
  1911. avctx->pix_fmt = mpeg_get_pixelformat(avctx);
  1912. #if FF_API_XVMC
  1913. if ((avctx->pix_fmt == AV_PIX_FMT_XVMC_MPEG2_IDCT || avctx->hwaccel) &&
  1914. avctx->idct_algo == FF_IDCT_AUTO)
  1915. #else
  1916. if (avctx->hwaccel && avctx->idct_algo == FF_IDCT_AUTO)
  1917. #endif /* FF_API_XVMC */
  1918. avctx->idct_algo = FF_IDCT_SIMPLE;
  1919. if (ff_MPV_common_init(s) < 0)
  1920. return -1;
  1921. s1->mpeg_enc_ctx_allocated = 1;
  1922. for (i = 0; i < 64; i++) {
  1923. int j = s->idsp.idct_permutation[i];
  1924. v = ff_mpeg1_default_intra_matrix[i];
  1925. s->intra_matrix[j] = v;
  1926. s->chroma_intra_matrix[j] = v;
  1927. v = ff_mpeg1_default_non_intra_matrix[i];
  1928. s->inter_matrix[j] = v;
  1929. s->chroma_inter_matrix[j] = v;
  1930. }
  1931. s->progressive_sequence = 1;
  1932. s->progressive_frame = 1;
  1933. s->picture_structure = PICT_FRAME;
  1934. s->frame_pred_frame_dct = 1;
  1935. s->chroma_format = 1;
  1936. s->codec_id = s->avctx->codec_id = AV_CODEC_ID_MPEG2VIDEO;
  1937. s1->save_width = s->width;
  1938. s1->save_height = s->height;
  1939. s1->save_progressive_seq = s->progressive_sequence;
  1940. return 0;
  1941. }
  1942. static int mpeg_decode_a53_cc(AVCodecContext *avctx,
  1943. const uint8_t *p, int buf_size)
  1944. {
  1945. Mpeg1Context *s1 = avctx->priv_data;
  1946. if (buf_size >= 6 &&
  1947. p[0] == 'G' && p[1] == 'A' && p[2] == '9' && p[3] == '4' &&
  1948. p[4] == 3 && (p[5] & 0x40)) {
  1949. /* extract A53 Part 4 CC data */
  1950. int cc_count = p[5] & 0x1f;
  1951. if (cc_count > 0 && buf_size >= 7 + cc_count * 3) {
  1952. av_freep(&s1->a53_caption);
  1953. s1->a53_caption_size = cc_count * 3;
  1954. s1->a53_caption = av_malloc(s1->a53_caption_size);
  1955. if (s1->a53_caption)
  1956. memcpy(s1->a53_caption, p + 7, s1->a53_caption_size);
  1957. }
  1958. return 1;
  1959. } else if (buf_size >= 11 &&
  1960. p[0] == 'C' && p[1] == 'C' && p[2] == 0x01 && p[3] == 0xf8) {
  1961. /* extract DVD CC data */
  1962. int cc_count = 0;
  1963. int i;
  1964. // There is a caption count field in the data, but it is often
  1965. // incorect. So count the number of captions present.
  1966. for (i = 5; i + 6 <= buf_size && ((p[i] & 0xfe) == 0xfe); i += 6)
  1967. cc_count++;
  1968. // Transform the DVD format into A53 Part 4 format
  1969. if (cc_count > 0) {
  1970. av_freep(&s1->a53_caption);
  1971. s1->a53_caption_size = cc_count * 6;
  1972. s1->a53_caption = av_malloc(s1->a53_caption_size);
  1973. if (s1->a53_caption) {
  1974. uint8_t field1 = !!(p[4] & 0x80);
  1975. uint8_t *cap = s1->a53_caption;
  1976. p += 5;
  1977. for (i = 0; i < cc_count; i++) {
  1978. cap[0] = (p[0] == 0xff && field1) ? 0xfc : 0xfd;
  1979. cap[1] = p[1];
  1980. cap[2] = p[2];
  1981. cap[3] = (p[3] == 0xff && !field1) ? 0xfc : 0xfd;
  1982. cap[4] = p[4];
  1983. cap[5] = p[5];
  1984. cap += 6;
  1985. p += 6;
  1986. }
  1987. }
  1988. }
  1989. return 1;
  1990. }
  1991. return 0;
  1992. }
  1993. static void mpeg_decode_user_data(AVCodecContext *avctx,
  1994. const uint8_t *p, int buf_size)
  1995. {
  1996. const uint8_t *buf_end = p + buf_size;
  1997. Mpeg1Context *s1 = avctx->priv_data;
  1998. /* we parse the DTG active format information */
  1999. if (buf_end - p >= 5 &&
  2000. p[0] == 'D' && p[1] == 'T' && p[2] == 'G' && p[3] == '1') {
  2001. int flags = p[4];
  2002. p += 5;
  2003. if (flags & 0x80) {
  2004. /* skip event id */
  2005. p += 2;
  2006. }
  2007. if (flags & 0x40) {
  2008. if (buf_end - p < 1)
  2009. return;
  2010. #if FF_API_AFD
  2011. avctx->dtg_active_format = p[0] & 0x0f;
  2012. #endif /* FF_API_AFD */
  2013. s1->has_afd = 1;
  2014. s1->afd = p[0] & 0x0f;
  2015. }
  2016. } else if (buf_end - p >= 6 &&
  2017. p[0] == 'J' && p[1] == 'P' && p[2] == '3' && p[3] == 'D' &&
  2018. p[4] == 0x03) { // S3D_video_format_length
  2019. // the 0x7F mask ignores the reserved_bit value
  2020. const uint8_t S3D_video_format_type = p[5] & 0x7F;
  2021. if (S3D_video_format_type == 0x03 ||
  2022. S3D_video_format_type == 0x04 ||
  2023. S3D_video_format_type == 0x08 ||
  2024. S3D_video_format_type == 0x23) {
  2025. s1->has_stereo3d = 1;
  2026. switch (S3D_video_format_type) {
  2027. case 0x03:
  2028. s1->stereo3d.type = AV_STEREO3D_SIDEBYSIDE;
  2029. break;
  2030. case 0x04:
  2031. s1->stereo3d.type = AV_STEREO3D_TOPBOTTOM;
  2032. break;
  2033. case 0x08:
  2034. s1->stereo3d.type = AV_STEREO3D_2D;
  2035. break;
  2036. case 0x23:
  2037. s1->stereo3d.type = AV_STEREO3D_SIDEBYSIDE_QUINCUNX;
  2038. break;
  2039. }
  2040. }
  2041. } else if (mpeg_decode_a53_cc(avctx, p, buf_size)) {
  2042. return;
  2043. }
  2044. }
  2045. static void mpeg_decode_gop(AVCodecContext *avctx,
  2046. const uint8_t *buf, int buf_size)
  2047. {
  2048. Mpeg1Context *s1 = avctx->priv_data;
  2049. MpegEncContext *s = &s1->mpeg_enc_ctx;
  2050. int time_code_hours, time_code_minutes;
  2051. int time_code_seconds, time_code_pictures;
  2052. int broken_link;
  2053. init_get_bits(&s->gb, buf, buf_size * 8);
  2054. skip_bits1(&s->gb); /* drop_frame_flag */
  2055. time_code_hours = get_bits(&s->gb, 5);
  2056. time_code_minutes = get_bits(&s->gb, 6);
  2057. skip_bits1(&s->gb); // marker bit
  2058. time_code_seconds = get_bits(&s->gb, 6);
  2059. time_code_pictures = get_bits(&s->gb, 6);
  2060. s1->closed_gop = get_bits1(&s->gb);
  2061. /* broken_link indicate that after editing the
  2062. * reference frames of the first B-Frames after GOP I-Frame
  2063. * are missing (open gop) */
  2064. broken_link = get_bits1(&s->gb);
  2065. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  2066. av_log(s->avctx, AV_LOG_DEBUG,
  2067. "GOP (%2d:%02d:%02d.[%02d]) closed_gop=%d broken_link=%d\n",
  2068. time_code_hours, time_code_minutes, time_code_seconds,
  2069. time_code_pictures, s1->closed_gop, broken_link);
  2070. }
  2071. static int decode_chunks(AVCodecContext *avctx, AVFrame *picture,
  2072. int *got_output, const uint8_t *buf, int buf_size)
  2073. {
  2074. Mpeg1Context *s = avctx->priv_data;
  2075. MpegEncContext *s2 = &s->mpeg_enc_ctx;
  2076. const uint8_t *buf_ptr = buf;
  2077. const uint8_t *buf_end = buf + buf_size;
  2078. int ret, input_size;
  2079. int last_code = 0, skip_frame = 0;
  2080. for (;;) {
  2081. /* find next start code */
  2082. uint32_t start_code = -1;
  2083. buf_ptr = avpriv_find_start_code(buf_ptr, buf_end, &start_code);
  2084. if (start_code > 0x1ff) {
  2085. if (!skip_frame) {
  2086. if (HAVE_THREADS &&
  2087. (avctx->active_thread_type & FF_THREAD_SLICE) &&
  2088. !avctx->hwaccel) {
  2089. int i;
  2090. avctx->execute(avctx, slice_decode_thread,
  2091. &s2->thread_context[0], NULL,
  2092. s->slice_count, sizeof(void *));
  2093. for (i = 0; i < s->slice_count; i++)
  2094. s2->er.error_count += s2->thread_context[i]->er.error_count;
  2095. }
  2096. ret = slice_end(avctx, picture);
  2097. if (ret < 0)
  2098. return ret;
  2099. else if (ret) {
  2100. // FIXME: merge with the stuff in mpeg_decode_slice
  2101. if (s2->last_picture_ptr || s2->low_delay)
  2102. *got_output = 1;
  2103. }
  2104. }
  2105. s2->pict_type = 0;
  2106. return FFMAX(0, buf_ptr - buf - s2->parse_context.last_index);
  2107. }
  2108. input_size = buf_end - buf_ptr;
  2109. if (avctx->debug & FF_DEBUG_STARTCODE)
  2110. av_log(avctx, AV_LOG_DEBUG, "%3"PRIX32" at %td left %d\n",
  2111. start_code, buf_ptr - buf, input_size);
  2112. /* prepare data for next start code */
  2113. switch (start_code) {
  2114. case SEQ_START_CODE:
  2115. if (last_code == 0) {
  2116. mpeg1_decode_sequence(avctx, buf_ptr, input_size);
  2117. s->sync = 1;
  2118. } else {
  2119. av_log(avctx, AV_LOG_ERROR,
  2120. "ignoring SEQ_START_CODE after %X\n", last_code);
  2121. if (avctx->err_recognition & AV_EF_EXPLODE)
  2122. return AVERROR_INVALIDDATA;
  2123. }
  2124. break;
  2125. case PICTURE_START_CODE:
  2126. if (s2->width <= 0 || s2->height <= 0) {
  2127. av_log(avctx, AV_LOG_ERROR, "Invalid frame dimensions %dx%d.\n",
  2128. s2->width, s2->height);
  2129. return AVERROR_INVALIDDATA;
  2130. }
  2131. if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE) &&
  2132. !avctx->hwaccel && s->slice_count) {
  2133. int i;
  2134. avctx->execute(avctx, slice_decode_thread,
  2135. s2->thread_context, NULL,
  2136. s->slice_count, sizeof(void *));
  2137. for (i = 0; i < s->slice_count; i++)
  2138. s2->er.error_count += s2->thread_context[i]->er.error_count;
  2139. s->slice_count = 0;
  2140. }
  2141. if (last_code == 0 || last_code == SLICE_MIN_START_CODE) {
  2142. ret = mpeg_decode_postinit(avctx);
  2143. if (ret < 0) {
  2144. av_log(avctx, AV_LOG_ERROR,
  2145. "mpeg_decode_postinit() failure\n");
  2146. return ret;
  2147. }
  2148. /* We have a complete image: we try to decompress it. */
  2149. if (mpeg1_decode_picture(avctx, buf_ptr, input_size) < 0)
  2150. s2->pict_type = 0;
  2151. s->first_slice = 1;
  2152. last_code = PICTURE_START_CODE;
  2153. } else {
  2154. av_log(avctx, AV_LOG_ERROR,
  2155. "ignoring pic after %X\n", last_code);
  2156. if (avctx->err_recognition & AV_EF_EXPLODE)
  2157. return AVERROR_INVALIDDATA;
  2158. }
  2159. break;
  2160. case EXT_START_CODE:
  2161. init_get_bits(&s2->gb, buf_ptr, input_size * 8);
  2162. switch (get_bits(&s2->gb, 4)) {
  2163. case 0x1:
  2164. if (last_code == 0) {
  2165. mpeg_decode_sequence_extension(s);
  2166. } else {
  2167. av_log(avctx, AV_LOG_ERROR,
  2168. "ignoring seq ext after %X\n", last_code);
  2169. if (avctx->err_recognition & AV_EF_EXPLODE)
  2170. return AVERROR_INVALIDDATA;
  2171. }
  2172. break;
  2173. case 0x2:
  2174. mpeg_decode_sequence_display_extension(s);
  2175. break;
  2176. case 0x3:
  2177. mpeg_decode_quant_matrix_extension(s2);
  2178. break;
  2179. case 0x7:
  2180. mpeg_decode_picture_display_extension(s);
  2181. break;
  2182. case 0x8:
  2183. if (last_code == PICTURE_START_CODE) {
  2184. mpeg_decode_picture_coding_extension(s);
  2185. } else {
  2186. av_log(avctx, AV_LOG_ERROR,
  2187. "ignoring pic cod ext after %X\n", last_code);
  2188. if (avctx->err_recognition & AV_EF_EXPLODE)
  2189. return AVERROR_INVALIDDATA;
  2190. }
  2191. break;
  2192. }
  2193. break;
  2194. case USER_START_CODE:
  2195. mpeg_decode_user_data(avctx, buf_ptr, input_size);
  2196. break;
  2197. case GOP_START_CODE:
  2198. if (last_code == 0) {
  2199. s2->first_field = 0;
  2200. mpeg_decode_gop(avctx, buf_ptr, input_size);
  2201. s->sync = 1;
  2202. } else {
  2203. av_log(avctx, AV_LOG_ERROR,
  2204. "ignoring GOP_START_CODE after %X\n", last_code);
  2205. if (avctx->err_recognition & AV_EF_EXPLODE)
  2206. return AVERROR_INVALIDDATA;
  2207. }
  2208. break;
  2209. default:
  2210. if (start_code >= SLICE_MIN_START_CODE &&
  2211. start_code <= SLICE_MAX_START_CODE && last_code != 0) {
  2212. const int field_pic = s2->picture_structure != PICT_FRAME;
  2213. int mb_y = (start_code - SLICE_MIN_START_CODE) << field_pic;
  2214. last_code = SLICE_MIN_START_CODE;
  2215. if (s2->picture_structure == PICT_BOTTOM_FIELD)
  2216. mb_y++;
  2217. if (mb_y >= s2->mb_height) {
  2218. av_log(s2->avctx, AV_LOG_ERROR,
  2219. "slice below image (%d >= %d)\n", mb_y, s2->mb_height);
  2220. return -1;
  2221. }
  2222. if (s2->last_picture_ptr == NULL) {
  2223. /* Skip B-frames if we do not have reference frames and
  2224. * GOP is not closed. */
  2225. if (s2->pict_type == AV_PICTURE_TYPE_B) {
  2226. if (!s->closed_gop) {
  2227. skip_frame = 1;
  2228. break;
  2229. }
  2230. }
  2231. }
  2232. if (s2->pict_type == AV_PICTURE_TYPE_I)
  2233. s->sync = 1;
  2234. if (s2->next_picture_ptr == NULL) {
  2235. /* Skip P-frames if we do not have a reference frame or
  2236. * we have an invalid header. */
  2237. if (s2->pict_type == AV_PICTURE_TYPE_P && !s->sync) {
  2238. skip_frame = 1;
  2239. break;
  2240. }
  2241. }
  2242. if ((avctx->skip_frame >= AVDISCARD_NONREF &&
  2243. s2->pict_type == AV_PICTURE_TYPE_B) ||
  2244. (avctx->skip_frame >= AVDISCARD_NONKEY &&
  2245. s2->pict_type != AV_PICTURE_TYPE_I) ||
  2246. avctx->skip_frame >= AVDISCARD_ALL) {
  2247. skip_frame = 1;
  2248. break;
  2249. }
  2250. if (!s->mpeg_enc_ctx_allocated)
  2251. break;
  2252. if (s2->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
  2253. if (mb_y < avctx->skip_top ||
  2254. mb_y >= s2->mb_height - avctx->skip_bottom)
  2255. break;
  2256. }
  2257. if (!s2->pict_type) {
  2258. av_log(avctx, AV_LOG_ERROR, "Missing picture start code\n");
  2259. if (avctx->err_recognition & AV_EF_EXPLODE)
  2260. return AVERROR_INVALIDDATA;
  2261. break;
  2262. }
  2263. if (s->first_slice) {
  2264. skip_frame = 0;
  2265. s->first_slice = 0;
  2266. if (mpeg_field_start(s2, buf, buf_size) < 0)
  2267. return -1;
  2268. }
  2269. if (!s2->current_picture_ptr) {
  2270. av_log(avctx, AV_LOG_ERROR,
  2271. "current_picture not initialized\n");
  2272. return AVERROR_INVALIDDATA;
  2273. }
  2274. if (HAVE_THREADS &&
  2275. (avctx->active_thread_type & FF_THREAD_SLICE) &&
  2276. !avctx->hwaccel) {
  2277. int threshold = (s2->mb_height * s->slice_count +
  2278. s2->slice_context_count / 2) /
  2279. s2->slice_context_count;
  2280. if (threshold <= mb_y) {
  2281. MpegEncContext *thread_context = s2->thread_context[s->slice_count];
  2282. thread_context->start_mb_y = mb_y;
  2283. thread_context->end_mb_y = s2->mb_height;
  2284. if (s->slice_count) {
  2285. s2->thread_context[s->slice_count - 1]->end_mb_y = mb_y;
  2286. ret = ff_update_duplicate_context(thread_context, s2);
  2287. if (ret < 0)
  2288. return ret;
  2289. }
  2290. init_get_bits(&thread_context->gb, buf_ptr, input_size * 8);
  2291. s->slice_count++;
  2292. }
  2293. buf_ptr += 2; // FIXME add minimum number of bytes per slice
  2294. } else {
  2295. ret = mpeg_decode_slice(s2, mb_y, &buf_ptr, input_size);
  2296. emms_c();
  2297. if (ret < 0) {
  2298. if (avctx->err_recognition & AV_EF_EXPLODE)
  2299. return ret;
  2300. if (s2->resync_mb_x >= 0 && s2->resync_mb_y >= 0)
  2301. ff_er_add_slice(&s2->er, s2->resync_mb_x,
  2302. s2->resync_mb_y, s2->mb_x, s2->mb_y,
  2303. ER_AC_ERROR | ER_DC_ERROR | ER_MV_ERROR);
  2304. } else {
  2305. ff_er_add_slice(&s2->er, s2->resync_mb_x,
  2306. s2->resync_mb_y, s2->mb_x - 1, s2->mb_y,
  2307. ER_AC_END | ER_DC_END | ER_MV_END);
  2308. }
  2309. }
  2310. }
  2311. break;
  2312. }
  2313. }
  2314. }
  2315. static int mpeg_decode_frame(AVCodecContext *avctx, void *data,
  2316. int *got_output, AVPacket *avpkt)
  2317. {
  2318. const uint8_t *buf = avpkt->data;
  2319. int buf_size = avpkt->size;
  2320. Mpeg1Context *s = avctx->priv_data;
  2321. AVFrame *picture = data;
  2322. MpegEncContext *s2 = &s->mpeg_enc_ctx;
  2323. av_dlog(avctx, "fill_buffer\n");
  2324. if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == SEQ_END_CODE)) {
  2325. /* special case for last picture */
  2326. if (s2->low_delay == 0 && s2->next_picture_ptr) {
  2327. int ret = av_frame_ref(picture, s2->next_picture_ptr->f);
  2328. if (ret < 0)
  2329. return ret;
  2330. s2->next_picture_ptr = NULL;
  2331. *got_output = 1;
  2332. }
  2333. return buf_size;
  2334. }
  2335. if (s2->flags & CODEC_FLAG_TRUNCATED) {
  2336. int next = ff_mpeg1_find_frame_end(&s2->parse_context, buf,
  2337. buf_size, NULL);
  2338. if (ff_combine_frame(&s2->parse_context, next,
  2339. (const uint8_t **) &buf, &buf_size) < 0)
  2340. return buf_size;
  2341. }
  2342. if (s->mpeg_enc_ctx_allocated == 0 && avctx->codec_tag == AV_RL32("VCR2"))
  2343. vcr2_init_sequence(avctx);
  2344. s->slice_count = 0;
  2345. if (avctx->extradata && !s->extradata_decoded) {
  2346. int ret = decode_chunks(avctx, picture, got_output,
  2347. avctx->extradata, avctx->extradata_size);
  2348. s->extradata_decoded = 1;
  2349. if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE))
  2350. return ret;
  2351. }
  2352. return decode_chunks(avctx, picture, got_output, buf, buf_size);
  2353. }
  2354. static void flush(AVCodecContext *avctx)
  2355. {
  2356. Mpeg1Context *s = avctx->priv_data;
  2357. s->sync = 0;
  2358. s->closed_gop = 0;
  2359. ff_mpeg_flush(avctx);
  2360. }
  2361. static av_cold int mpeg_decode_end(AVCodecContext *avctx)
  2362. {
  2363. Mpeg1Context *s = avctx->priv_data;
  2364. if (s->mpeg_enc_ctx_allocated)
  2365. ff_MPV_common_end(&s->mpeg_enc_ctx);
  2366. av_freep(&s->a53_caption);
  2367. return 0;
  2368. }
  2369. static const AVProfile mpeg2_video_profiles[] = {
  2370. { FF_PROFILE_MPEG2_422, "4:2:2" },
  2371. { FF_PROFILE_MPEG2_HIGH, "High" },
  2372. { FF_PROFILE_MPEG2_SS, "Spatially Scalable" },
  2373. { FF_PROFILE_MPEG2_SNR_SCALABLE, "SNR Scalable" },
  2374. { FF_PROFILE_MPEG2_MAIN, "Main" },
  2375. { FF_PROFILE_MPEG2_SIMPLE, "Simple" },
  2376. { FF_PROFILE_RESERVED, "Reserved" },
  2377. { FF_PROFILE_RESERVED, "Reserved" },
  2378. { FF_PROFILE_UNKNOWN },
  2379. };
  2380. AVCodec ff_mpeg1video_decoder = {
  2381. .name = "mpeg1video",
  2382. .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video"),
  2383. .type = AVMEDIA_TYPE_VIDEO,
  2384. .id = AV_CODEC_ID_MPEG1VIDEO,
  2385. .priv_data_size = sizeof(Mpeg1Context),
  2386. .init = mpeg_decode_init,
  2387. .close = mpeg_decode_end,
  2388. .decode = mpeg_decode_frame,
  2389. .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
  2390. CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY |
  2391. CODEC_CAP_SLICE_THREADS,
  2392. .flush = flush,
  2393. .update_thread_context = ONLY_IF_THREADS_ENABLED(mpeg_decode_update_thread_context)
  2394. };
  2395. AVCodec ff_mpeg2video_decoder = {
  2396. .name = "mpeg2video",
  2397. .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 video"),
  2398. .type = AVMEDIA_TYPE_VIDEO,
  2399. .id = AV_CODEC_ID_MPEG2VIDEO,
  2400. .priv_data_size = sizeof(Mpeg1Context),
  2401. .init = mpeg_decode_init,
  2402. .close = mpeg_decode_end,
  2403. .decode = mpeg_decode_frame,
  2404. .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
  2405. CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY |
  2406. CODEC_CAP_SLICE_THREADS,
  2407. .flush = flush,
  2408. .profiles = NULL_IF_CONFIG_SMALL(mpeg2_video_profiles),
  2409. };
  2410. #if FF_API_XVMC
  2411. #if CONFIG_MPEG_XVMC_DECODER
  2412. static av_cold int mpeg_mc_decode_init(AVCodecContext *avctx)
  2413. {
  2414. if (avctx->active_thread_type & FF_THREAD_SLICE)
  2415. return -1;
  2416. if (!(avctx->slice_flags & SLICE_FLAG_CODED_ORDER))
  2417. return -1;
  2418. if (!(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD)) {
  2419. av_dlog(avctx, "mpeg12.c: XvMC decoder will work better if SLICE_FLAG_ALLOW_FIELD is set\n");
  2420. }
  2421. mpeg_decode_init(avctx);
  2422. avctx->pix_fmt = AV_PIX_FMT_XVMC_MPEG2_IDCT;
  2423. avctx->xvmc_acceleration = 2; // 2 - the blocks are packed!
  2424. return 0;
  2425. }
  2426. AVCodec ff_mpeg_xvmc_decoder = {
  2427. .name = "mpegvideo_xvmc",
  2428. .long_name = NULL_IF_CONFIG_SMALL("MPEG-1/2 video XvMC (X-Video Motion Compensation)"),
  2429. .type = AVMEDIA_TYPE_VIDEO,
  2430. .id = AV_CODEC_ID_MPEG2VIDEO_XVMC,
  2431. .priv_data_size = sizeof(Mpeg1Context),
  2432. .init = mpeg_mc_decode_init,
  2433. .close = mpeg_decode_end,
  2434. .decode = mpeg_decode_frame,
  2435. .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
  2436. CODEC_CAP_TRUNCATED | CODEC_CAP_HWACCEL | CODEC_CAP_DELAY,
  2437. .flush = flush,
  2438. };
  2439. #endif
  2440. #endif /* FF_API_XVMC */