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.

2760 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 AVERROR_INVALIDDATA;
  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 AVERROR_INVALIDDATA;
  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 AVERROR_INVALIDDATA;
  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. int ret;
  655. av_dlog(s->avctx, "decode_mb: x=%d y=%d\n", s->mb_x, s->mb_y);
  656. assert(s->mb_skipped == 0);
  657. if (s->mb_skip_run-- != 0) {
  658. if (s->pict_type == AV_PICTURE_TYPE_P) {
  659. s->mb_skipped = 1;
  660. s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride] =
  661. MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;
  662. } else {
  663. int mb_type;
  664. if (s->mb_x)
  665. mb_type = s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride - 1];
  666. else
  667. // FIXME not sure if this is allowed in MPEG at all
  668. mb_type = s->current_picture.mb_type[s->mb_width + (s->mb_y - 1) * s->mb_stride - 1];
  669. if (IS_INTRA(mb_type))
  670. return AVERROR_INVALIDDATA;
  671. s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride] =
  672. mb_type | MB_TYPE_SKIP;
  673. // assert(s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride - 1] & (MB_TYPE_16x16 | MB_TYPE_16x8));
  674. if ((s->mv[0][0][0] | s->mv[0][0][1] | s->mv[1][0][0] | s->mv[1][0][1]) == 0)
  675. s->mb_skipped = 1;
  676. }
  677. return 0;
  678. }
  679. switch (s->pict_type) {
  680. default:
  681. case AV_PICTURE_TYPE_I:
  682. if (get_bits1(&s->gb) == 0) {
  683. if (get_bits1(&s->gb) == 0) {
  684. av_log(s->avctx, AV_LOG_ERROR,
  685. "invalid mb type in I Frame at %d %d\n",
  686. s->mb_x, s->mb_y);
  687. return AVERROR_INVALIDDATA;
  688. }
  689. mb_type = MB_TYPE_QUANT | MB_TYPE_INTRA;
  690. } else {
  691. mb_type = MB_TYPE_INTRA;
  692. }
  693. break;
  694. case AV_PICTURE_TYPE_P:
  695. mb_type = get_vlc2(&s->gb, ff_mb_ptype_vlc.table, MB_PTYPE_VLC_BITS, 1);
  696. if (mb_type < 0) {
  697. av_log(s->avctx, AV_LOG_ERROR,
  698. "invalid mb type in P Frame at %d %d\n", s->mb_x, s->mb_y);
  699. return AVERROR_INVALIDDATA;
  700. }
  701. mb_type = ptype2mb_type[mb_type];
  702. break;
  703. case AV_PICTURE_TYPE_B:
  704. mb_type = get_vlc2(&s->gb, ff_mb_btype_vlc.table, MB_BTYPE_VLC_BITS, 1);
  705. if (mb_type < 0) {
  706. av_log(s->avctx, AV_LOG_ERROR,
  707. "invalid mb type in B Frame at %d %d\n", s->mb_x, s->mb_y);
  708. return AVERROR_INVALIDDATA;
  709. }
  710. mb_type = btype2mb_type[mb_type];
  711. break;
  712. }
  713. av_dlog(s->avctx, "mb_type=%x\n", mb_type);
  714. // motion_type = 0; /* avoid warning */
  715. if (IS_INTRA(mb_type)) {
  716. s->bdsp.clear_blocks(s->block[0]);
  717. if (!s->chroma_y_shift)
  718. s->bdsp.clear_blocks(s->block[6]);
  719. /* compute DCT type */
  720. // FIXME: add an interlaced_dct coded var?
  721. if (s->picture_structure == PICT_FRAME &&
  722. !s->frame_pred_frame_dct)
  723. s->interlaced_dct = get_bits1(&s->gb);
  724. if (IS_QUANT(mb_type))
  725. s->qscale = get_qscale(s);
  726. if (s->concealment_motion_vectors) {
  727. /* just parse them */
  728. if (s->picture_structure != PICT_FRAME)
  729. skip_bits1(&s->gb); /* field select */
  730. s->mv[0][0][0] =
  731. s->last_mv[0][0][0] =
  732. s->last_mv[0][1][0] = mpeg_decode_motion(s, s->mpeg_f_code[0][0],
  733. s->last_mv[0][0][0]);
  734. s->mv[0][0][1] =
  735. s->last_mv[0][0][1] =
  736. s->last_mv[0][1][1] = mpeg_decode_motion(s, s->mpeg_f_code[0][1],
  737. s->last_mv[0][0][1]);
  738. skip_bits1(&s->gb); /* marker */
  739. } else {
  740. /* reset mv prediction */
  741. memset(s->last_mv, 0, sizeof(s->last_mv));
  742. }
  743. s->mb_intra = 1;
  744. #if FF_API_XVMC
  745. FF_DISABLE_DEPRECATION_WARNINGS
  746. // if 1, we memcpy blocks in xvmcvideo
  747. if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1)
  748. ff_xvmc_pack_pblocks(s, -1); // inter are always full blocks
  749. FF_ENABLE_DEPRECATION_WARNINGS
  750. #endif /* FF_API_XVMC */
  751. if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
  752. if (s->flags2 & CODEC_FLAG2_FAST) {
  753. for (i = 0; i < 6; i++)
  754. mpeg2_fast_decode_block_intra(s, *s->pblocks[i], i);
  755. } else {
  756. for (i = 0; i < mb_block_count; i++)
  757. if ((ret = mpeg2_decode_block_intra(s, *s->pblocks[i], i)) < 0)
  758. return ret;
  759. }
  760. } else {
  761. for (i = 0; i < 6; i++)
  762. if ((ret = mpeg1_decode_block_intra(s, *s->pblocks[i], i)) < 0)
  763. return ret;
  764. }
  765. } else {
  766. if (mb_type & MB_TYPE_ZERO_MV) {
  767. assert(mb_type & MB_TYPE_CBP);
  768. s->mv_dir = MV_DIR_FORWARD;
  769. if (s->picture_structure == PICT_FRAME) {
  770. if (!s->frame_pred_frame_dct)
  771. s->interlaced_dct = get_bits1(&s->gb);
  772. s->mv_type = MV_TYPE_16X16;
  773. } else {
  774. s->mv_type = MV_TYPE_FIELD;
  775. mb_type |= MB_TYPE_INTERLACED;
  776. s->field_select[0][0] = s->picture_structure - 1;
  777. }
  778. if (IS_QUANT(mb_type))
  779. s->qscale = get_qscale(s);
  780. s->last_mv[0][0][0] = 0;
  781. s->last_mv[0][0][1] = 0;
  782. s->last_mv[0][1][0] = 0;
  783. s->last_mv[0][1][1] = 0;
  784. s->mv[0][0][0] = 0;
  785. s->mv[0][0][1] = 0;
  786. } else {
  787. assert(mb_type & MB_TYPE_L0L1);
  788. // FIXME decide if MBs in field pictures are MB_TYPE_INTERLACED
  789. /* get additional motion vector type */
  790. if (s->frame_pred_frame_dct) {
  791. motion_type = MT_FRAME;
  792. } else {
  793. motion_type = get_bits(&s->gb, 2);
  794. if (s->picture_structure == PICT_FRAME && HAS_CBP(mb_type))
  795. s->interlaced_dct = get_bits1(&s->gb);
  796. }
  797. if (IS_QUANT(mb_type))
  798. s->qscale = get_qscale(s);
  799. /* motion vectors */
  800. s->mv_dir = (mb_type >> 13) & 3;
  801. av_dlog(s->avctx, "motion_type=%d\n", motion_type);
  802. switch (motion_type) {
  803. case MT_FRAME: /* or MT_16X8 */
  804. if (s->picture_structure == PICT_FRAME) {
  805. mb_type |= MB_TYPE_16x16;
  806. s->mv_type = MV_TYPE_16X16;
  807. for (i = 0; i < 2; i++) {
  808. if (USES_LIST(mb_type, i)) {
  809. /* MT_FRAME */
  810. s->mv[i][0][0] =
  811. s->last_mv[i][0][0] =
  812. s->last_mv[i][1][0] =
  813. mpeg_decode_motion(s, s->mpeg_f_code[i][0],
  814. s->last_mv[i][0][0]);
  815. s->mv[i][0][1] =
  816. s->last_mv[i][0][1] =
  817. s->last_mv[i][1][1] =
  818. mpeg_decode_motion(s, s->mpeg_f_code[i][1],
  819. s->last_mv[i][0][1]);
  820. /* full_pel: only for MPEG-1 */
  821. if (s->full_pel[i]) {
  822. s->mv[i][0][0] <<= 1;
  823. s->mv[i][0][1] <<= 1;
  824. }
  825. }
  826. }
  827. } else {
  828. mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
  829. s->mv_type = MV_TYPE_16X8;
  830. for (i = 0; i < 2; i++) {
  831. if (USES_LIST(mb_type, i)) {
  832. /* MT_16X8 */
  833. for (j = 0; j < 2; j++) {
  834. s->field_select[i][j] = get_bits1(&s->gb);
  835. for (k = 0; k < 2; k++) {
  836. val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
  837. s->last_mv[i][j][k]);
  838. s->last_mv[i][j][k] = val;
  839. s->mv[i][j][k] = val;
  840. }
  841. }
  842. }
  843. }
  844. }
  845. break;
  846. case MT_FIELD:
  847. s->mv_type = MV_TYPE_FIELD;
  848. if (s->picture_structure == PICT_FRAME) {
  849. mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
  850. for (i = 0; i < 2; i++) {
  851. if (USES_LIST(mb_type, i)) {
  852. for (j = 0; j < 2; j++) {
  853. s->field_select[i][j] = get_bits1(&s->gb);
  854. val = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
  855. s->last_mv[i][j][0]);
  856. s->last_mv[i][j][0] = val;
  857. s->mv[i][j][0] = val;
  858. av_dlog(s->avctx, "fmx=%d\n", val);
  859. val = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
  860. s->last_mv[i][j][1] >> 1);
  861. s->last_mv[i][j][1] = val << 1;
  862. s->mv[i][j][1] = val;
  863. av_dlog(s->avctx, "fmy=%d\n", val);
  864. }
  865. }
  866. }
  867. } else {
  868. mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
  869. for (i = 0; i < 2; i++) {
  870. if (USES_LIST(mb_type, i)) {
  871. s->field_select[i][0] = get_bits1(&s->gb);
  872. for (k = 0; k < 2; k++) {
  873. val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
  874. s->last_mv[i][0][k]);
  875. s->last_mv[i][0][k] = val;
  876. s->last_mv[i][1][k] = val;
  877. s->mv[i][0][k] = val;
  878. }
  879. }
  880. }
  881. }
  882. break;
  883. case MT_DMV:
  884. s->mv_type = MV_TYPE_DMV;
  885. for (i = 0; i < 2; i++) {
  886. if (USES_LIST(mb_type, i)) {
  887. int dmx, dmy, mx, my, m;
  888. const int my_shift = s->picture_structure == PICT_FRAME;
  889. mx = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
  890. s->last_mv[i][0][0]);
  891. s->last_mv[i][0][0] = mx;
  892. s->last_mv[i][1][0] = mx;
  893. dmx = get_dmv(s);
  894. my = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
  895. s->last_mv[i][0][1] >> my_shift);
  896. dmy = get_dmv(s);
  897. s->last_mv[i][0][1] = my << my_shift;
  898. s->last_mv[i][1][1] = my << my_shift;
  899. s->mv[i][0][0] = mx;
  900. s->mv[i][0][1] = my;
  901. s->mv[i][1][0] = mx; // not used
  902. s->mv[i][1][1] = my; // not used
  903. if (s->picture_structure == PICT_FRAME) {
  904. mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
  905. // m = 1 + 2 * s->top_field_first;
  906. m = s->top_field_first ? 1 : 3;
  907. /* top -> top pred */
  908. s->mv[i][2][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
  909. s->mv[i][2][1] = ((my * m + (my > 0)) >> 1) + dmy - 1;
  910. m = 4 - m;
  911. s->mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
  912. s->mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1;
  913. } else {
  914. mb_type |= MB_TYPE_16x16;
  915. s->mv[i][2][0] = ((mx + (mx > 0)) >> 1) + dmx;
  916. s->mv[i][2][1] = ((my + (my > 0)) >> 1) + dmy;
  917. if (s->picture_structure == PICT_TOP_FIELD)
  918. s->mv[i][2][1]--;
  919. else
  920. s->mv[i][2][1]++;
  921. }
  922. }
  923. }
  924. break;
  925. default:
  926. av_log(s->avctx, AV_LOG_ERROR,
  927. "00 motion_type at %d %d\n", s->mb_x, s->mb_y);
  928. return AVERROR_INVALIDDATA;
  929. }
  930. }
  931. s->mb_intra = 0;
  932. if (HAS_CBP(mb_type)) {
  933. s->bdsp.clear_blocks(s->block[0]);
  934. cbp = get_vlc2(&s->gb, ff_mb_pat_vlc.table, MB_PAT_VLC_BITS, 1);
  935. if (mb_block_count > 6) {
  936. cbp <<= mb_block_count - 6;
  937. cbp |= get_bits(&s->gb, mb_block_count - 6);
  938. s->bdsp.clear_blocks(s->block[6]);
  939. }
  940. if (cbp <= 0) {
  941. av_log(s->avctx, AV_LOG_ERROR,
  942. "invalid cbp at %d %d\n", s->mb_x, s->mb_y);
  943. return AVERROR_INVALIDDATA;
  944. }
  945. #if FF_API_XVMC
  946. FF_DISABLE_DEPRECATION_WARNINGS
  947. // if 1, we memcpy blocks in xvmcvideo
  948. if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1)
  949. ff_xvmc_pack_pblocks(s, cbp);
  950. FF_ENABLE_DEPRECATION_WARNINGS
  951. #endif /* FF_API_XVMC */
  952. if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
  953. if (s->flags2 & CODEC_FLAG2_FAST) {
  954. for (i = 0; i < 6; i++) {
  955. if (cbp & 32)
  956. mpeg2_fast_decode_block_non_intra(s, *s->pblocks[i], i);
  957. else
  958. s->block_last_index[i] = -1;
  959. cbp += cbp;
  960. }
  961. } else {
  962. cbp <<= 12 - mb_block_count;
  963. for (i = 0; i < mb_block_count; i++) {
  964. if (cbp & (1 << 11)) {
  965. if ((ret = mpeg2_decode_block_non_intra(s, *s->pblocks[i], i)) < 0)
  966. return ret;
  967. } else {
  968. s->block_last_index[i] = -1;
  969. }
  970. cbp += cbp;
  971. }
  972. }
  973. } else {
  974. if (s->flags2 & CODEC_FLAG2_FAST) {
  975. for (i = 0; i < 6; i++) {
  976. if (cbp & 32)
  977. mpeg1_fast_decode_block_inter(s, *s->pblocks[i], i);
  978. else
  979. s->block_last_index[i] = -1;
  980. cbp += cbp;
  981. }
  982. } else {
  983. for (i = 0; i < 6; i++) {
  984. if (cbp & 32) {
  985. if ((ret = mpeg1_decode_block_inter(s, *s->pblocks[i], i)) < 0)
  986. return ret;
  987. } else {
  988. s->block_last_index[i] = -1;
  989. }
  990. cbp += cbp;
  991. }
  992. }
  993. }
  994. } else {
  995. for (i = 0; i < 12; i++)
  996. s->block_last_index[i] = -1;
  997. }
  998. }
  999. s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride] = mb_type;
  1000. return 0;
  1001. }
  1002. static av_cold int mpeg_decode_init(AVCodecContext *avctx)
  1003. {
  1004. Mpeg1Context *s = avctx->priv_data;
  1005. MpegEncContext *s2 = &s->mpeg_enc_ctx;
  1006. ff_mpv_decode_defaults(s2);
  1007. s->mpeg_enc_ctx.avctx = avctx;
  1008. s->mpeg_enc_ctx.flags = avctx->flags;
  1009. s->mpeg_enc_ctx.flags2 = avctx->flags2;
  1010. /* we need some permutation to store matrices,
  1011. * until the decoder sets the real permutation. */
  1012. ff_mpv_idct_init(s2);
  1013. ff_mpeg12_common_init(&s->mpeg_enc_ctx);
  1014. ff_mpeg12_init_vlcs();
  1015. s->mpeg_enc_ctx_allocated = 0;
  1016. s->mpeg_enc_ctx.picture_number = 0;
  1017. s->repeat_field = 0;
  1018. s->mpeg_enc_ctx.codec_id = avctx->codec->id;
  1019. avctx->color_range = AVCOL_RANGE_MPEG;
  1020. if (avctx->codec->id == AV_CODEC_ID_MPEG1VIDEO)
  1021. avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
  1022. else
  1023. avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
  1024. return 0;
  1025. }
  1026. static int mpeg_decode_update_thread_context(AVCodecContext *avctx,
  1027. const AVCodecContext *avctx_from)
  1028. {
  1029. Mpeg1Context *ctx = avctx->priv_data, *ctx_from = avctx_from->priv_data;
  1030. MpegEncContext *s = &ctx->mpeg_enc_ctx, *s1 = &ctx_from->mpeg_enc_ctx;
  1031. int err;
  1032. if (avctx == avctx_from ||
  1033. !ctx_from->mpeg_enc_ctx_allocated ||
  1034. !s1->context_initialized)
  1035. return 0;
  1036. err = ff_mpeg_update_thread_context(avctx, avctx_from);
  1037. if (err)
  1038. return err;
  1039. if (!ctx->mpeg_enc_ctx_allocated) {
  1040. // copy the whole context after the initial MpegEncContext structure
  1041. memcpy(ctx, ctx_from, sizeof(*ctx));
  1042. memset(&ctx->mpeg_enc_ctx, 0, sizeof(ctx->mpeg_enc_ctx));
  1043. }
  1044. if (!(s->pict_type == AV_PICTURE_TYPE_B || s->low_delay))
  1045. s->picture_number++;
  1046. return 0;
  1047. }
  1048. static void quant_matrix_rebuild(uint16_t *matrix, const uint8_t *old_perm,
  1049. const uint8_t *new_perm)
  1050. {
  1051. uint16_t temp_matrix[64];
  1052. int i;
  1053. memcpy(temp_matrix, matrix, 64 * sizeof(uint16_t));
  1054. for (i = 0; i < 64; i++)
  1055. matrix[new_perm[i]] = temp_matrix[old_perm[i]];
  1056. }
  1057. #if FF_API_XVMC
  1058. static const enum AVPixelFormat pixfmt_xvmc_mpg2_420[] = {
  1059. AV_PIX_FMT_XVMC_MPEG2_IDCT,
  1060. AV_PIX_FMT_XVMC_MPEG2_MC,
  1061. AV_PIX_FMT_NONE
  1062. };
  1063. #endif /* FF_API_XVMC */
  1064. static const enum AVPixelFormat mpeg12_hwaccel_pixfmt_list_420[] = {
  1065. #if CONFIG_MPEG2_DXVA2_HWACCEL
  1066. AV_PIX_FMT_DXVA2_VLD,
  1067. #endif
  1068. #if CONFIG_MPEG2_VAAPI_HWACCEL
  1069. AV_PIX_FMT_VAAPI_VLD,
  1070. #endif
  1071. #if CONFIG_MPEG1_VDPAU_HWACCEL | CONFIG_MPEG2_VDPAU_HWACCEL
  1072. AV_PIX_FMT_VDPAU,
  1073. #endif
  1074. AV_PIX_FMT_YUV420P,
  1075. AV_PIX_FMT_NONE
  1076. };
  1077. static const enum AVPixelFormat mpeg12_pixfmt_list_422[] = {
  1078. AV_PIX_FMT_YUV422P,
  1079. AV_PIX_FMT_NONE
  1080. };
  1081. static const enum AVPixelFormat mpeg12_pixfmt_list_444[] = {
  1082. AV_PIX_FMT_YUV444P,
  1083. AV_PIX_FMT_NONE
  1084. };
  1085. static enum AVPixelFormat mpeg_get_pixelformat(AVCodecContext *avctx)
  1086. {
  1087. Mpeg1Context *s1 = avctx->priv_data;
  1088. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1089. const enum AVPixelFormat *pix_fmts;
  1090. #if FF_API_XVMC
  1091. FF_DISABLE_DEPRECATION_WARNINGS
  1092. if (avctx->xvmc_acceleration)
  1093. return ff_get_format(avctx, pixfmt_xvmc_mpg2_420);
  1094. FF_ENABLE_DEPRECATION_WARNINGS
  1095. #endif /* FF_API_XVMC */
  1096. if (s->chroma_format < 2)
  1097. pix_fmts = mpeg12_hwaccel_pixfmt_list_420;
  1098. else if (s->chroma_format == 2)
  1099. pix_fmts = mpeg12_pixfmt_list_422;
  1100. else
  1101. pix_fmts = mpeg12_pixfmt_list_444;
  1102. return ff_get_format(avctx, pix_fmts);
  1103. }
  1104. /* Call this function when we know all parameters.
  1105. * It may be called in different places for MPEG-1 and MPEG-2. */
  1106. static int mpeg_decode_postinit(AVCodecContext *avctx)
  1107. {
  1108. Mpeg1Context *s1 = avctx->priv_data;
  1109. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1110. uint8_t old_permutation[64];
  1111. int ret;
  1112. if ((s1->mpeg_enc_ctx_allocated == 0) ||
  1113. avctx->coded_width != s->width ||
  1114. avctx->coded_height != s->height ||
  1115. s1->save_width != s->width ||
  1116. s1->save_height != s->height ||
  1117. s1->save_aspect_info != s->aspect_ratio_info ||
  1118. s1->save_progressive_seq != s->progressive_sequence ||
  1119. 0) {
  1120. if (s1->mpeg_enc_ctx_allocated) {
  1121. ParseContext pc = s->parse_context;
  1122. s->parse_context.buffer = 0;
  1123. ff_mpv_common_end(s);
  1124. s->parse_context = pc;
  1125. }
  1126. ret = ff_set_dimensions(avctx, s->width, s->height);
  1127. if (ret < 0)
  1128. return ret;
  1129. avctx->bit_rate = s->bit_rate;
  1130. s1->save_aspect_info = s->aspect_ratio_info;
  1131. s1->save_width = s->width;
  1132. s1->save_height = s->height;
  1133. s1->save_progressive_seq = s->progressive_sequence;
  1134. /* low_delay may be forced, in this case we will have B-frames
  1135. * that behave like P-frames. */
  1136. avctx->has_b_frames = !s->low_delay;
  1137. if (avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
  1138. // MPEG-1 fps
  1139. avctx->framerate = ff_mpeg12_frame_rate_tab[s->frame_rate_index];
  1140. // MPEG-1 aspect
  1141. avctx->sample_aspect_ratio = av_d2q(1.0 / ff_mpeg1_aspect[s->aspect_ratio_info], 255);
  1142. avctx->ticks_per_frame = 1;
  1143. } else { // MPEG-2
  1144. // MPEG-2 fps
  1145. av_reduce(&s->avctx->framerate.num,
  1146. &s->avctx->framerate.den,
  1147. ff_mpeg12_frame_rate_tab[s->frame_rate_index].num * s1->frame_rate_ext.num * 2,
  1148. ff_mpeg12_frame_rate_tab[s->frame_rate_index].den * s1->frame_rate_ext.den,
  1149. 1 << 30);
  1150. avctx->ticks_per_frame = 2;
  1151. // MPEG-2 aspect
  1152. if (s->aspect_ratio_info > 1) {
  1153. AVRational dar =
  1154. av_mul_q(av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
  1155. (AVRational) { s1->pan_scan.width,
  1156. s1->pan_scan.height }),
  1157. (AVRational) { s->width, s->height });
  1158. /* We ignore the spec here and guess a bit as reality does not
  1159. * match the spec, see for example res_change_ffmpeg_aspect.ts
  1160. * and sequence-display-aspect.mpg.
  1161. * issue1613, 621, 562 */
  1162. if ((s1->pan_scan.width == 0) || (s1->pan_scan.height == 0) ||
  1163. (av_cmp_q(dar, (AVRational) { 4, 3 }) &&
  1164. av_cmp_q(dar, (AVRational) { 16, 9 }))) {
  1165. s->avctx->sample_aspect_ratio =
  1166. av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
  1167. (AVRational) { s->width, s->height });
  1168. } else {
  1169. s->avctx->sample_aspect_ratio =
  1170. av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
  1171. (AVRational) { s1->pan_scan.width, s1->pan_scan.height });
  1172. // issue1613 4/3 16/9 -> 16/9
  1173. // res_change_ffmpeg_aspect.ts 4/3 225/44 ->4/3
  1174. // widescreen-issue562.mpg 4/3 16/9 -> 16/9
  1175. // s->avctx->sample_aspect_ratio = av_mul_q(s->avctx->sample_aspect_ratio, (AVRational) {s->width, s->height});
  1176. av_dlog(avctx, "A %d/%d\n",
  1177. ff_mpeg2_aspect[s->aspect_ratio_info].num,
  1178. ff_mpeg2_aspect[s->aspect_ratio_info].den);
  1179. av_dlog(avctx, "B %d/%d\n", s->avctx->sample_aspect_ratio.num,
  1180. s->avctx->sample_aspect_ratio.den);
  1181. }
  1182. } else {
  1183. s->avctx->sample_aspect_ratio =
  1184. ff_mpeg2_aspect[s->aspect_ratio_info];
  1185. }
  1186. } // MPEG-2
  1187. ff_set_sar(s->avctx, s->avctx->sample_aspect_ratio);
  1188. avctx->pix_fmt = mpeg_get_pixelformat(avctx);
  1189. // until then pix_fmt may be changed right after codec init
  1190. #if FF_API_XVMC
  1191. if ((avctx->pix_fmt == AV_PIX_FMT_XVMC_MPEG2_IDCT ||
  1192. avctx->hwaccel) && avctx->idct_algo == FF_IDCT_AUTO)
  1193. #else
  1194. if (avctx->hwaccel && avctx->idct_algo == FF_IDCT_AUTO)
  1195. #endif /* FF_API_XVMC */
  1196. avctx->idct_algo = FF_IDCT_SIMPLE;
  1197. /* Quantization matrices may need reordering
  1198. * if DCT permutation is changed. */
  1199. memcpy(old_permutation, s->idsp.idct_permutation, 64 * sizeof(uint8_t));
  1200. ff_mpv_idct_init(s);
  1201. if ((ret = ff_mpv_common_init(s)) < 0)
  1202. return ret;
  1203. quant_matrix_rebuild(s->intra_matrix, old_permutation, s->idsp.idct_permutation);
  1204. quant_matrix_rebuild(s->inter_matrix, old_permutation, s->idsp.idct_permutation);
  1205. quant_matrix_rebuild(s->chroma_intra_matrix, old_permutation, s->idsp.idct_permutation);
  1206. quant_matrix_rebuild(s->chroma_inter_matrix, old_permutation, s->idsp.idct_permutation);
  1207. s1->mpeg_enc_ctx_allocated = 1;
  1208. }
  1209. return 0;
  1210. }
  1211. static int mpeg1_decode_picture(AVCodecContext *avctx, const uint8_t *buf,
  1212. int buf_size)
  1213. {
  1214. Mpeg1Context *s1 = avctx->priv_data;
  1215. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1216. int ref, f_code, vbv_delay;
  1217. init_get_bits(&s->gb, buf, buf_size * 8);
  1218. ref = get_bits(&s->gb, 10); /* temporal ref */
  1219. s->pict_type = get_bits(&s->gb, 3);
  1220. if (s->pict_type == 0 || s->pict_type > 3)
  1221. return AVERROR_INVALIDDATA;
  1222. vbv_delay = get_bits(&s->gb, 16);
  1223. if (s->pict_type == AV_PICTURE_TYPE_P ||
  1224. s->pict_type == AV_PICTURE_TYPE_B) {
  1225. s->full_pel[0] = get_bits1(&s->gb);
  1226. f_code = get_bits(&s->gb, 3);
  1227. if (f_code == 0 && (avctx->err_recognition & AV_EF_BITSTREAM))
  1228. return AVERROR_INVALIDDATA;
  1229. s->mpeg_f_code[0][0] = f_code;
  1230. s->mpeg_f_code[0][1] = f_code;
  1231. }
  1232. if (s->pict_type == AV_PICTURE_TYPE_B) {
  1233. s->full_pel[1] = get_bits1(&s->gb);
  1234. f_code = get_bits(&s->gb, 3);
  1235. if (f_code == 0 && (avctx->err_recognition & AV_EF_BITSTREAM))
  1236. return AVERROR_INVALIDDATA;
  1237. s->mpeg_f_code[1][0] = f_code;
  1238. s->mpeg_f_code[1][1] = f_code;
  1239. }
  1240. s->current_picture.f->pict_type = s->pict_type;
  1241. s->current_picture.f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
  1242. if (avctx->debug & FF_DEBUG_PICT_INFO)
  1243. av_log(avctx, AV_LOG_DEBUG,
  1244. "vbv_delay %d, ref %d type:%d\n", vbv_delay, ref, s->pict_type);
  1245. s->y_dc_scale = 8;
  1246. s->c_dc_scale = 8;
  1247. return 0;
  1248. }
  1249. static void mpeg_decode_sequence_extension(Mpeg1Context *s1)
  1250. {
  1251. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1252. int horiz_size_ext, vert_size_ext;
  1253. int bit_rate_ext;
  1254. skip_bits(&s->gb, 1); /* profile and level esc*/
  1255. s->avctx->profile = get_bits(&s->gb, 3);
  1256. s->avctx->level = get_bits(&s->gb, 4);
  1257. s->progressive_sequence = get_bits1(&s->gb); /* progressive_sequence */
  1258. s->chroma_format = get_bits(&s->gb, 2); /* chroma_format 1=420, 2=422, 3=444 */
  1259. horiz_size_ext = get_bits(&s->gb, 2);
  1260. vert_size_ext = get_bits(&s->gb, 2);
  1261. s->width |= (horiz_size_ext << 12);
  1262. s->height |= (vert_size_ext << 12);
  1263. bit_rate_ext = get_bits(&s->gb, 12); /* XXX: handle it */
  1264. s->bit_rate += (bit_rate_ext << 18) * 400;
  1265. skip_bits1(&s->gb); /* marker */
  1266. s->avctx->rc_buffer_size += get_bits(&s->gb, 8) * 1024 * 16 << 10;
  1267. s->low_delay = get_bits1(&s->gb);
  1268. if (s->flags & CODEC_FLAG_LOW_DELAY)
  1269. s->low_delay = 1;
  1270. s1->frame_rate_ext.num = get_bits(&s->gb, 2) + 1;
  1271. s1->frame_rate_ext.den = get_bits(&s->gb, 5) + 1;
  1272. av_dlog(s->avctx, "sequence extension\n");
  1273. s->codec_id = s->avctx->codec_id = AV_CODEC_ID_MPEG2VIDEO;
  1274. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  1275. av_log(s->avctx, AV_LOG_DEBUG,
  1276. "profile: %d, level: %d vbv buffer: %d, bitrate:%d\n",
  1277. s->avctx->profile, s->avctx->level,
  1278. s->avctx->rc_buffer_size, s->bit_rate);
  1279. }
  1280. static void mpeg_decode_sequence_display_extension(Mpeg1Context *s1)
  1281. {
  1282. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1283. int color_description, w, h;
  1284. skip_bits(&s->gb, 3); /* video format */
  1285. color_description = get_bits1(&s->gb);
  1286. if (color_description) {
  1287. s->avctx->color_primaries = get_bits(&s->gb, 8);
  1288. s->avctx->color_trc = get_bits(&s->gb, 8);
  1289. s->avctx->colorspace = get_bits(&s->gb, 8);
  1290. }
  1291. w = get_bits(&s->gb, 14);
  1292. skip_bits(&s->gb, 1); // marker
  1293. h = get_bits(&s->gb, 14);
  1294. // remaining 3 bits are zero padding
  1295. s1->pan_scan.width = 16 * w;
  1296. s1->pan_scan.height = 16 * h;
  1297. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  1298. av_log(s->avctx, AV_LOG_DEBUG, "sde w:%d, h:%d\n", w, h);
  1299. }
  1300. static void mpeg_decode_picture_display_extension(Mpeg1Context *s1)
  1301. {
  1302. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1303. int i, nofco;
  1304. nofco = 1;
  1305. if (s->progressive_sequence) {
  1306. if (s->repeat_first_field) {
  1307. nofco++;
  1308. if (s->top_field_first)
  1309. nofco++;
  1310. }
  1311. } else {
  1312. if (s->picture_structure == PICT_FRAME) {
  1313. nofco++;
  1314. if (s->repeat_first_field)
  1315. nofco++;
  1316. }
  1317. }
  1318. for (i = 0; i < nofco; i++) {
  1319. s1->pan_scan.position[i][0] = get_sbits(&s->gb, 16);
  1320. skip_bits(&s->gb, 1); // marker
  1321. s1->pan_scan.position[i][1] = get_sbits(&s->gb, 16);
  1322. skip_bits(&s->gb, 1); // marker
  1323. }
  1324. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  1325. av_log(s->avctx, AV_LOG_DEBUG,
  1326. "pde (%"PRId16",%"PRId16") (%"PRId16",%"PRId16") (%"PRId16",%"PRId16")\n",
  1327. s1->pan_scan.position[0][0], s1->pan_scan.position[0][1],
  1328. s1->pan_scan.position[1][0], s1->pan_scan.position[1][1],
  1329. s1->pan_scan.position[2][0], s1->pan_scan.position[2][1]);
  1330. }
  1331. static int load_matrix(MpegEncContext *s, uint16_t matrix0[64],
  1332. uint16_t matrix1[64], int intra)
  1333. {
  1334. int i;
  1335. for (i = 0; i < 64; i++) {
  1336. int j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
  1337. int v = get_bits(&s->gb, 8);
  1338. if (v == 0) {
  1339. av_log(s->avctx, AV_LOG_ERROR, "matrix damaged\n");
  1340. return AVERROR_INVALIDDATA;
  1341. }
  1342. if (intra && i == 0 && v != 8) {
  1343. av_log(s->avctx, AV_LOG_ERROR, "intra matrix invalid, ignoring\n");
  1344. v = 8; // needed by pink.mpg / issue1046
  1345. }
  1346. matrix0[j] = v;
  1347. if (matrix1)
  1348. matrix1[j] = v;
  1349. }
  1350. return 0;
  1351. }
  1352. static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
  1353. {
  1354. av_dlog(s->avctx, "matrix extension\n");
  1355. if (get_bits1(&s->gb))
  1356. load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
  1357. if (get_bits1(&s->gb))
  1358. load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
  1359. if (get_bits1(&s->gb))
  1360. load_matrix(s, s->chroma_intra_matrix, NULL, 1);
  1361. if (get_bits1(&s->gb))
  1362. load_matrix(s, s->chroma_inter_matrix, NULL, 0);
  1363. }
  1364. static void mpeg_decode_picture_coding_extension(Mpeg1Context *s1)
  1365. {
  1366. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1367. s->full_pel[0] = s->full_pel[1] = 0;
  1368. s->mpeg_f_code[0][0] = get_bits(&s->gb, 4);
  1369. s->mpeg_f_code[0][1] = get_bits(&s->gb, 4);
  1370. s->mpeg_f_code[1][0] = get_bits(&s->gb, 4);
  1371. s->mpeg_f_code[1][1] = get_bits(&s->gb, 4);
  1372. if (!s->pict_type && s1->mpeg_enc_ctx_allocated) {
  1373. av_log(s->avctx, AV_LOG_ERROR,
  1374. "Missing picture start code, guessing missing values\n");
  1375. if (s->mpeg_f_code[1][0] == 15 && s->mpeg_f_code[1][1] == 15) {
  1376. if (s->mpeg_f_code[0][0] == 15 && s->mpeg_f_code[0][1] == 15)
  1377. s->pict_type = AV_PICTURE_TYPE_I;
  1378. else
  1379. s->pict_type = AV_PICTURE_TYPE_P;
  1380. } else
  1381. s->pict_type = AV_PICTURE_TYPE_B;
  1382. s->current_picture.f->pict_type = s->pict_type;
  1383. s->current_picture.f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
  1384. }
  1385. s->intra_dc_precision = get_bits(&s->gb, 2);
  1386. s->picture_structure = get_bits(&s->gb, 2);
  1387. s->top_field_first = get_bits1(&s->gb);
  1388. s->frame_pred_frame_dct = get_bits1(&s->gb);
  1389. s->concealment_motion_vectors = get_bits1(&s->gb);
  1390. s->q_scale_type = get_bits1(&s->gb);
  1391. s->intra_vlc_format = get_bits1(&s->gb);
  1392. s->alternate_scan = get_bits1(&s->gb);
  1393. s->repeat_first_field = get_bits1(&s->gb);
  1394. s->chroma_420_type = get_bits1(&s->gb);
  1395. s->progressive_frame = get_bits1(&s->gb);
  1396. if (s->progressive_sequence && !s->progressive_frame) {
  1397. s->progressive_frame = 1;
  1398. av_log(s->avctx, AV_LOG_ERROR,
  1399. "interlaced frame in progressive sequence, ignoring\n");
  1400. }
  1401. if (s->picture_structure == 0 ||
  1402. (s->progressive_frame && s->picture_structure != PICT_FRAME)) {
  1403. av_log(s->avctx, AV_LOG_ERROR,
  1404. "picture_structure %d invalid, ignoring\n",
  1405. s->picture_structure);
  1406. s->picture_structure = PICT_FRAME;
  1407. }
  1408. if (s->progressive_sequence && !s->frame_pred_frame_dct)
  1409. av_log(s->avctx, AV_LOG_WARNING, "invalid frame_pred_frame_dct\n");
  1410. if (s->picture_structure == PICT_FRAME) {
  1411. s->first_field = 0;
  1412. s->v_edge_pos = 16 * s->mb_height;
  1413. } else {
  1414. s->first_field ^= 1;
  1415. s->v_edge_pos = 8 * s->mb_height;
  1416. memset(s->mbskip_table, 0, s->mb_stride * s->mb_height);
  1417. }
  1418. if (s->alternate_scan) {
  1419. ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_alternate_vertical_scan);
  1420. ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_alternate_vertical_scan);
  1421. } else {
  1422. ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_zigzag_direct);
  1423. ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_zigzag_direct);
  1424. }
  1425. /* composite display not parsed */
  1426. av_dlog(s->avctx, "intra_dc_precision=%d\n", s->intra_dc_precision);
  1427. av_dlog(s->avctx, "picture_structure=%d\n", s->picture_structure);
  1428. av_dlog(s->avctx, "top field first=%d\n", s->top_field_first);
  1429. av_dlog(s->avctx, "repeat first field=%d\n", s->repeat_first_field);
  1430. av_dlog(s->avctx, "conceal=%d\n", s->concealment_motion_vectors);
  1431. av_dlog(s->avctx, "intra_vlc_format=%d\n", s->intra_vlc_format);
  1432. av_dlog(s->avctx, "alternate_scan=%d\n", s->alternate_scan);
  1433. av_dlog(s->avctx, "frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
  1434. av_dlog(s->avctx, "progressive_frame=%d\n", s->progressive_frame);
  1435. }
  1436. static int mpeg_field_start(MpegEncContext *s, const uint8_t *buf, int buf_size)
  1437. {
  1438. AVCodecContext *avctx = s->avctx;
  1439. Mpeg1Context *s1 = (Mpeg1Context *) s;
  1440. int ret;
  1441. /* start frame decoding */
  1442. if (s->first_field || s->picture_structure == PICT_FRAME) {
  1443. AVFrameSideData *pan_scan;
  1444. if ((ret = ff_mpv_frame_start(s, avctx)) < 0)
  1445. return ret;
  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 AVERROR_INVALIDDATA;
  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 ((ret = avctx->hwaccel->start_frame(avctx, buf, buf_size)) < 0)
  1512. return ret;
  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. int ret;
  1539. s->resync_mb_x =
  1540. s->resync_mb_y = -1;
  1541. assert(mb_y < s->mb_height);
  1542. init_get_bits(&s->gb, *buf, buf_size * 8);
  1543. ff_mpeg1_clean_buffers(s);
  1544. s->interlaced_dct = 0;
  1545. s->qscale = get_qscale(s);
  1546. if (s->qscale == 0) {
  1547. av_log(s->avctx, AV_LOG_ERROR, "qscale == 0\n");
  1548. return AVERROR_INVALIDDATA;
  1549. }
  1550. /* extra slice info */
  1551. while (get_bits1(&s->gb) != 0)
  1552. skip_bits(&s->gb, 8);
  1553. s->mb_x = 0;
  1554. if (mb_y == 0 && s->codec_tag == AV_RL32("SLIF")) {
  1555. skip_bits1(&s->gb);
  1556. } else {
  1557. while (get_bits_left(&s->gb) > 0) {
  1558. int code = get_vlc2(&s->gb, ff_mbincr_vlc.table,
  1559. MBINCR_VLC_BITS, 2);
  1560. if (code < 0) {
  1561. av_log(s->avctx, AV_LOG_ERROR, "first mb_incr damaged\n");
  1562. return AVERROR_INVALIDDATA;
  1563. }
  1564. if (code >= 33) {
  1565. if (code == 33)
  1566. s->mb_x += 33;
  1567. /* otherwise, stuffing, nothing to do */
  1568. } else {
  1569. s->mb_x += code;
  1570. break;
  1571. }
  1572. }
  1573. }
  1574. if (s->mb_x >= (unsigned) s->mb_width) {
  1575. av_log(s->avctx, AV_LOG_ERROR, "initial skip overflow\n");
  1576. return AVERROR_INVALIDDATA;
  1577. }
  1578. if (avctx->hwaccel) {
  1579. const uint8_t *buf_end, *buf_start = *buf - 4; /* include start_code */
  1580. int start_code = -1;
  1581. buf_end = avpriv_find_start_code(buf_start + 2, *buf + buf_size, &start_code);
  1582. if (buf_end < *buf + buf_size)
  1583. buf_end -= 4;
  1584. s->mb_y = mb_y;
  1585. if (avctx->hwaccel->decode_slice(avctx, buf_start, buf_end - buf_start) < 0)
  1586. return DECODE_SLICE_ERROR;
  1587. *buf = buf_end;
  1588. return DECODE_SLICE_OK;
  1589. }
  1590. s->resync_mb_x = s->mb_x;
  1591. s->resync_mb_y = s->mb_y = mb_y;
  1592. s->mb_skip_run = 0;
  1593. ff_init_block_index(s);
  1594. if (s->mb_y == 0 && s->mb_x == 0 && (s->first_field || s->picture_structure == PICT_FRAME)) {
  1595. if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
  1596. av_log(s->avctx, AV_LOG_DEBUG,
  1597. "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",
  1598. s->qscale,
  1599. s->mpeg_f_code[0][0], s->mpeg_f_code[0][1],
  1600. s->mpeg_f_code[1][0], s->mpeg_f_code[1][1],
  1601. s->pict_type == AV_PICTURE_TYPE_I ? "I" :
  1602. (s->pict_type == AV_PICTURE_TYPE_P ? "P" :
  1603. (s->pict_type == AV_PICTURE_TYPE_B ? "B" : "S")),
  1604. s->progressive_sequence ? "ps" : "",
  1605. s->progressive_frame ? "pf" : "",
  1606. s->alternate_scan ? "alt" : "",
  1607. s->top_field_first ? "top" : "",
  1608. s->intra_dc_precision, s->picture_structure,
  1609. s->frame_pred_frame_dct, s->concealment_motion_vectors,
  1610. s->q_scale_type, s->intra_vlc_format,
  1611. s->repeat_first_field, s->chroma_420_type ? "420" : "");
  1612. }
  1613. }
  1614. for (;;) {
  1615. #if FF_API_XVMC
  1616. FF_DISABLE_DEPRECATION_WARNINGS
  1617. // If 1, we memcpy blocks in xvmcvideo.
  1618. if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1)
  1619. ff_xvmc_init_block(s); // set s->block
  1620. FF_ENABLE_DEPRECATION_WARNINGS
  1621. #endif /* FF_API_XVMC */
  1622. if ((ret = mpeg_decode_mb(s, s->block)) < 0)
  1623. return ret;
  1624. // Note motion_val is normally NULL unless we want to extract the MVs.
  1625. if (s->current_picture.motion_val[0] && !s->encoding) {
  1626. const int wrap = s->b8_stride;
  1627. int xy = s->mb_x * 2 + s->mb_y * 2 * wrap;
  1628. int b8_xy = 4 * (s->mb_x + s->mb_y * s->mb_stride);
  1629. int motion_x, motion_y, dir, i;
  1630. for (i = 0; i < 2; i++) {
  1631. for (dir = 0; dir < 2; dir++) {
  1632. if (s->mb_intra ||
  1633. (dir == 1 && s->pict_type != AV_PICTURE_TYPE_B)) {
  1634. motion_x = motion_y = 0;
  1635. } else if (s->mv_type == MV_TYPE_16X16 ||
  1636. (s->mv_type == MV_TYPE_FIELD && field_pic)) {
  1637. motion_x = s->mv[dir][0][0];
  1638. motion_y = s->mv[dir][0][1];
  1639. } else { /* if ((s->mv_type == MV_TYPE_FIELD) || (s->mv_type == MV_TYPE_16X8)) */
  1640. motion_x = s->mv[dir][i][0];
  1641. motion_y = s->mv[dir][i][1];
  1642. }
  1643. s->current_picture.motion_val[dir][xy][0] = motion_x;
  1644. s->current_picture.motion_val[dir][xy][1] = motion_y;
  1645. s->current_picture.motion_val[dir][xy + 1][0] = motion_x;
  1646. s->current_picture.motion_val[dir][xy + 1][1] = motion_y;
  1647. s->current_picture.ref_index [dir][b8_xy] =
  1648. s->current_picture.ref_index [dir][b8_xy + 1] = s->field_select[dir][i];
  1649. assert(s->field_select[dir][i] == 0 ||
  1650. s->field_select[dir][i] == 1);
  1651. }
  1652. xy += wrap;
  1653. b8_xy += 2;
  1654. }
  1655. }
  1656. s->dest[0] += 16;
  1657. s->dest[1] += 16 >> s->chroma_x_shift;
  1658. s->dest[2] += 16 >> s->chroma_x_shift;
  1659. ff_mpv_decode_mb(s, s->block);
  1660. if (++s->mb_x >= s->mb_width) {
  1661. const int mb_size = 16;
  1662. ff_mpeg_draw_horiz_band(s, mb_size * (s->mb_y >> field_pic), mb_size);
  1663. ff_mpv_report_decode_progress(s);
  1664. s->mb_x = 0;
  1665. s->mb_y += 1 << field_pic;
  1666. if (s->mb_y >= s->mb_height) {
  1667. int left = get_bits_left(&s->gb);
  1668. int is_d10 = s->chroma_format == 2 &&
  1669. s->pict_type == AV_PICTURE_TYPE_I &&
  1670. avctx->profile == 0 && avctx->level == 5 &&
  1671. s->intra_dc_precision == 2 &&
  1672. s->q_scale_type == 1 && s->alternate_scan == 0 &&
  1673. s->progressive_frame == 0
  1674. /* vbv_delay == 0xBBB || 0xE10 */;
  1675. if (left < 0 ||
  1676. (left && show_bits(&s->gb, FFMIN(left, 23)) && !is_d10) ||
  1677. ((avctx->err_recognition & AV_EF_BUFFER) && left > 8)) {
  1678. av_log(avctx, AV_LOG_ERROR, "end mismatch left=%d %0X\n",
  1679. left, show_bits(&s->gb, FFMIN(left, 23)));
  1680. return AVERROR_INVALIDDATA;
  1681. } else
  1682. goto eos;
  1683. }
  1684. ff_init_block_index(s);
  1685. }
  1686. /* skip mb handling */
  1687. if (s->mb_skip_run == -1) {
  1688. /* read increment again */
  1689. s->mb_skip_run = 0;
  1690. for (;;) {
  1691. int code = get_vlc2(&s->gb, ff_mbincr_vlc.table,
  1692. MBINCR_VLC_BITS, 2);
  1693. if (code < 0) {
  1694. av_log(s->avctx, AV_LOG_ERROR, "mb incr damaged\n");
  1695. return AVERROR_INVALIDDATA;
  1696. }
  1697. if (code >= 33) {
  1698. if (code == 33) {
  1699. s->mb_skip_run += 33;
  1700. } else if (code == 35) {
  1701. if (s->mb_skip_run != 0 || show_bits(&s->gb, 15) != 0) {
  1702. av_log(s->avctx, AV_LOG_ERROR, "slice mismatch\n");
  1703. return AVERROR_INVALIDDATA;
  1704. }
  1705. goto eos; /* end of slice */
  1706. }
  1707. /* otherwise, stuffing, nothing to do */
  1708. } else {
  1709. s->mb_skip_run += code;
  1710. break;
  1711. }
  1712. }
  1713. if (s->mb_skip_run) {
  1714. int i;
  1715. if (s->pict_type == AV_PICTURE_TYPE_I) {
  1716. av_log(s->avctx, AV_LOG_ERROR,
  1717. "skipped MB in I frame at %d %d\n", s->mb_x, s->mb_y);
  1718. return AVERROR_INVALIDDATA;
  1719. }
  1720. /* skip mb */
  1721. s->mb_intra = 0;
  1722. for (i = 0; i < 12; i++)
  1723. s->block_last_index[i] = -1;
  1724. if (s->picture_structure == PICT_FRAME)
  1725. s->mv_type = MV_TYPE_16X16;
  1726. else
  1727. s->mv_type = MV_TYPE_FIELD;
  1728. if (s->pict_type == AV_PICTURE_TYPE_P) {
  1729. /* if P type, zero motion vector is implied */
  1730. s->mv_dir = MV_DIR_FORWARD;
  1731. s->mv[0][0][0] = s->mv[0][0][1] = 0;
  1732. s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
  1733. s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
  1734. s->field_select[0][0] = (s->picture_structure - 1) & 1;
  1735. } else {
  1736. /* if B type, reuse previous vectors and directions */
  1737. s->mv[0][0][0] = s->last_mv[0][0][0];
  1738. s->mv[0][0][1] = s->last_mv[0][0][1];
  1739. s->mv[1][0][0] = s->last_mv[1][0][0];
  1740. s->mv[1][0][1] = s->last_mv[1][0][1];
  1741. }
  1742. }
  1743. }
  1744. }
  1745. eos: // end of slice
  1746. *buf += (get_bits_count(&s->gb) - 1) / 8;
  1747. av_dlog(s, "y %d %d %d %d\n", s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y);
  1748. return 0;
  1749. }
  1750. static int slice_decode_thread(AVCodecContext *c, void *arg)
  1751. {
  1752. MpegEncContext *s = *(void **) arg;
  1753. const uint8_t *buf = s->gb.buffer;
  1754. int mb_y = s->start_mb_y;
  1755. const int field_pic = s->picture_structure != PICT_FRAME;
  1756. s->er.error_count = (3 * (s->end_mb_y - s->start_mb_y) * s->mb_width) >> field_pic;
  1757. for (;;) {
  1758. uint32_t start_code;
  1759. int ret;
  1760. ret = mpeg_decode_slice(s, mb_y, &buf, s->gb.buffer_end - buf);
  1761. emms_c();
  1762. av_dlog(c, "ret:%d resync:%d/%d mb:%d/%d ts:%d/%d ec:%d\n",
  1763. ret, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y,
  1764. s->start_mb_y, s->end_mb_y, s->er.error_count);
  1765. if (ret < 0) {
  1766. if (c->err_recognition & AV_EF_EXPLODE)
  1767. return ret;
  1768. if (s->resync_mb_x >= 0 && s->resync_mb_y >= 0)
  1769. ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
  1770. s->mb_x, s->mb_y,
  1771. ER_AC_ERROR | ER_DC_ERROR | ER_MV_ERROR);
  1772. } else {
  1773. ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
  1774. s->mb_x - 1, s->mb_y,
  1775. ER_AC_END | ER_DC_END | ER_MV_END);
  1776. }
  1777. if (s->mb_y == s->end_mb_y)
  1778. return 0;
  1779. start_code = -1;
  1780. buf = avpriv_find_start_code(buf, s->gb.buffer_end, &start_code);
  1781. mb_y = (start_code - SLICE_MIN_START_CODE) << field_pic;
  1782. if (s->picture_structure == PICT_BOTTOM_FIELD)
  1783. mb_y++;
  1784. if (mb_y < 0 || mb_y >= s->end_mb_y)
  1785. return AVERROR_INVALIDDATA;
  1786. }
  1787. }
  1788. /**
  1789. * Handle slice ends.
  1790. * @return 1 if it seems to be the last slice
  1791. */
  1792. static int slice_end(AVCodecContext *avctx, AVFrame *pict)
  1793. {
  1794. Mpeg1Context *s1 = avctx->priv_data;
  1795. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1796. if (!s1->mpeg_enc_ctx_allocated || !s->current_picture_ptr)
  1797. return 0;
  1798. if (s->avctx->hwaccel) {
  1799. if (s->avctx->hwaccel->end_frame(s->avctx) < 0)
  1800. av_log(avctx, AV_LOG_ERROR,
  1801. "hardware accelerator failed to decode picture\n");
  1802. }
  1803. #if FF_API_XVMC
  1804. FF_DISABLE_DEPRECATION_WARNINGS
  1805. if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
  1806. ff_xvmc_field_end(s);
  1807. FF_ENABLE_DEPRECATION_WARNINGS
  1808. #endif /* FF_API_XVMC */
  1809. /* end of slice reached */
  1810. if (/* s->mb_y << field_pic == s->mb_height && */ !s->first_field) {
  1811. /* end of image */
  1812. ff_er_frame_end(&s->er);
  1813. ff_mpv_frame_end(s);
  1814. if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
  1815. int ret = av_frame_ref(pict, s->current_picture_ptr->f);
  1816. if (ret < 0)
  1817. return ret;
  1818. ff_print_debug_info(s, s->current_picture_ptr);
  1819. } else {
  1820. if (avctx->active_thread_type & FF_THREAD_FRAME)
  1821. s->picture_number++;
  1822. /* latency of 1 frame for I- and P-frames */
  1823. /* XXX: use another variable than picture_number */
  1824. if (s->last_picture_ptr) {
  1825. int ret = av_frame_ref(pict, s->last_picture_ptr->f);
  1826. if (ret < 0)
  1827. return ret;
  1828. ff_print_debug_info(s, s->last_picture_ptr);
  1829. }
  1830. }
  1831. return 1;
  1832. } else {
  1833. return 0;
  1834. }
  1835. }
  1836. static int mpeg1_decode_sequence(AVCodecContext *avctx,
  1837. const uint8_t *buf, int buf_size)
  1838. {
  1839. Mpeg1Context *s1 = avctx->priv_data;
  1840. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1841. int width, height;
  1842. int i, v, j;
  1843. init_get_bits(&s->gb, buf, buf_size * 8);
  1844. width = get_bits(&s->gb, 12);
  1845. height = get_bits(&s->gb, 12);
  1846. if (width == 0 || height == 0) {
  1847. av_log(avctx, AV_LOG_WARNING,
  1848. "Invalid horizontal or vertical size value.\n");
  1849. if (avctx->err_recognition & AV_EF_BITSTREAM)
  1850. return AVERROR_INVALIDDATA;
  1851. }
  1852. s->aspect_ratio_info = get_bits(&s->gb, 4);
  1853. if (s->aspect_ratio_info == 0) {
  1854. av_log(avctx, AV_LOG_ERROR, "aspect ratio has forbidden 0 value\n");
  1855. if (avctx->err_recognition & AV_EF_BITSTREAM)
  1856. return AVERROR_INVALIDDATA;
  1857. }
  1858. s->frame_rate_index = get_bits(&s->gb, 4);
  1859. if (s->frame_rate_index == 0 || s->frame_rate_index > 13) {
  1860. av_log(avctx, AV_LOG_WARNING,
  1861. "frame_rate_index %d is invalid\n", s->frame_rate_index);
  1862. return AVERROR_INVALIDDATA;
  1863. }
  1864. s->bit_rate = get_bits(&s->gb, 18) * 400;
  1865. if (get_bits1(&s->gb) == 0) { /* marker */
  1866. av_log(avctx, AV_LOG_ERROR, "Marker in sequence header missing\n");
  1867. return AVERROR_INVALIDDATA;
  1868. }
  1869. s->width = width;
  1870. s->height = height;
  1871. s->avctx->rc_buffer_size = get_bits(&s->gb, 10) * 1024 * 16;
  1872. skip_bits(&s->gb, 1);
  1873. /* get matrix */
  1874. if (get_bits1(&s->gb)) {
  1875. load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
  1876. } else {
  1877. for (i = 0; i < 64; i++) {
  1878. j = s->idsp.idct_permutation[i];
  1879. v = ff_mpeg1_default_intra_matrix[i];
  1880. s->intra_matrix[j] = v;
  1881. s->chroma_intra_matrix[j] = v;
  1882. }
  1883. }
  1884. if (get_bits1(&s->gb)) {
  1885. load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
  1886. } else {
  1887. for (i = 0; i < 64; i++) {
  1888. int j = s->idsp.idct_permutation[i];
  1889. v = ff_mpeg1_default_non_intra_matrix[i];
  1890. s->inter_matrix[j] = v;
  1891. s->chroma_inter_matrix[j] = v;
  1892. }
  1893. }
  1894. if (show_bits(&s->gb, 23) != 0) {
  1895. av_log(s->avctx, AV_LOG_ERROR, "sequence header damaged\n");
  1896. return AVERROR_INVALIDDATA;
  1897. }
  1898. /* We set MPEG-2 parameters so that it emulates MPEG-1. */
  1899. s->progressive_sequence = 1;
  1900. s->progressive_frame = 1;
  1901. s->picture_structure = PICT_FRAME;
  1902. s->frame_pred_frame_dct = 1;
  1903. s->chroma_format = 1;
  1904. s->codec_id =
  1905. s->avctx->codec_id = AV_CODEC_ID_MPEG1VIDEO;
  1906. s->out_format = FMT_MPEG1;
  1907. if (s->flags & CODEC_FLAG_LOW_DELAY)
  1908. s->low_delay = 1;
  1909. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  1910. av_log(s->avctx, AV_LOG_DEBUG, "vbv buffer: %d, bitrate:%d\n",
  1911. s->avctx->rc_buffer_size, s->bit_rate);
  1912. return 0;
  1913. }
  1914. static int vcr2_init_sequence(AVCodecContext *avctx)
  1915. {
  1916. Mpeg1Context *s1 = avctx->priv_data;
  1917. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1918. int i, v, ret;
  1919. /* start new MPEG-1 context decoding */
  1920. s->out_format = FMT_MPEG1;
  1921. if (s1->mpeg_enc_ctx_allocated) {
  1922. ff_mpv_common_end(s);
  1923. }
  1924. s->width = avctx->coded_width;
  1925. s->height = avctx->coded_height;
  1926. avctx->has_b_frames = 0; // true?
  1927. s->low_delay = 1;
  1928. avctx->pix_fmt = mpeg_get_pixelformat(avctx);
  1929. #if FF_API_XVMC
  1930. if ((avctx->pix_fmt == AV_PIX_FMT_XVMC_MPEG2_IDCT || avctx->hwaccel) &&
  1931. avctx->idct_algo == FF_IDCT_AUTO)
  1932. #else
  1933. if (avctx->hwaccel && avctx->idct_algo == FF_IDCT_AUTO)
  1934. #endif /* FF_API_XVMC */
  1935. avctx->idct_algo = FF_IDCT_SIMPLE;
  1936. ff_mpv_idct_init(s);
  1937. if ((ret = ff_mpv_common_init(s)) < 0)
  1938. return ret;
  1939. s1->mpeg_enc_ctx_allocated = 1;
  1940. for (i = 0; i < 64; i++) {
  1941. int j = s->idsp.idct_permutation[i];
  1942. v = ff_mpeg1_default_intra_matrix[i];
  1943. s->intra_matrix[j] = v;
  1944. s->chroma_intra_matrix[j] = v;
  1945. v = ff_mpeg1_default_non_intra_matrix[i];
  1946. s->inter_matrix[j] = v;
  1947. s->chroma_inter_matrix[j] = v;
  1948. }
  1949. s->progressive_sequence = 1;
  1950. s->progressive_frame = 1;
  1951. s->picture_structure = PICT_FRAME;
  1952. s->frame_pred_frame_dct = 1;
  1953. s->chroma_format = 1;
  1954. s->codec_id = s->avctx->codec_id = AV_CODEC_ID_MPEG2VIDEO;
  1955. s1->save_width = s->width;
  1956. s1->save_height = s->height;
  1957. s1->save_progressive_seq = s->progressive_sequence;
  1958. return 0;
  1959. }
  1960. static int mpeg_decode_a53_cc(AVCodecContext *avctx,
  1961. const uint8_t *p, int buf_size)
  1962. {
  1963. Mpeg1Context *s1 = avctx->priv_data;
  1964. if (buf_size >= 6 &&
  1965. p[0] == 'G' && p[1] == 'A' && p[2] == '9' && p[3] == '4' &&
  1966. p[4] == 3 && (p[5] & 0x40)) {
  1967. /* extract A53 Part 4 CC data */
  1968. int cc_count = p[5] & 0x1f;
  1969. if (cc_count > 0 && buf_size >= 7 + cc_count * 3) {
  1970. av_freep(&s1->a53_caption);
  1971. s1->a53_caption_size = cc_count * 3;
  1972. s1->a53_caption = av_malloc(s1->a53_caption_size);
  1973. if (s1->a53_caption)
  1974. memcpy(s1->a53_caption, p + 7, s1->a53_caption_size);
  1975. }
  1976. return 1;
  1977. } else if (buf_size >= 11 &&
  1978. p[0] == 'C' && p[1] == 'C' && p[2] == 0x01 && p[3] == 0xf8) {
  1979. /* extract DVD CC data */
  1980. int cc_count = 0;
  1981. int i;
  1982. // There is a caption count field in the data, but it is often
  1983. // incorect. So count the number of captions present.
  1984. for (i = 5; i + 6 <= buf_size && ((p[i] & 0xfe) == 0xfe); i += 6)
  1985. cc_count++;
  1986. // Transform the DVD format into A53 Part 4 format
  1987. if (cc_count > 0) {
  1988. av_freep(&s1->a53_caption);
  1989. s1->a53_caption_size = cc_count * 6;
  1990. s1->a53_caption = av_malloc(s1->a53_caption_size);
  1991. if (s1->a53_caption) {
  1992. uint8_t field1 = !!(p[4] & 0x80);
  1993. uint8_t *cap = s1->a53_caption;
  1994. p += 5;
  1995. for (i = 0; i < cc_count; i++) {
  1996. cap[0] = (p[0] == 0xff && field1) ? 0xfc : 0xfd;
  1997. cap[1] = p[1];
  1998. cap[2] = p[2];
  1999. cap[3] = (p[3] == 0xff && !field1) ? 0xfc : 0xfd;
  2000. cap[4] = p[4];
  2001. cap[5] = p[5];
  2002. cap += 6;
  2003. p += 6;
  2004. }
  2005. }
  2006. }
  2007. return 1;
  2008. }
  2009. return 0;
  2010. }
  2011. static void mpeg_decode_user_data(AVCodecContext *avctx,
  2012. const uint8_t *p, int buf_size)
  2013. {
  2014. const uint8_t *buf_end = p + buf_size;
  2015. Mpeg1Context *s1 = avctx->priv_data;
  2016. /* we parse the DTG active format information */
  2017. if (buf_end - p >= 5 &&
  2018. p[0] == 'D' && p[1] == 'T' && p[2] == 'G' && p[3] == '1') {
  2019. int flags = p[4];
  2020. p += 5;
  2021. if (flags & 0x80) {
  2022. /* skip event id */
  2023. p += 2;
  2024. }
  2025. if (flags & 0x40) {
  2026. if (buf_end - p < 1)
  2027. return;
  2028. #if FF_API_AFD
  2029. FF_DISABLE_DEPRECATION_WARNINGS
  2030. avctx->dtg_active_format = p[0] & 0x0f;
  2031. FF_ENABLE_DEPRECATION_WARNINGS
  2032. #endif /* FF_API_AFD */
  2033. s1->has_afd = 1;
  2034. s1->afd = p[0] & 0x0f;
  2035. }
  2036. } else if (buf_end - p >= 6 &&
  2037. p[0] == 'J' && p[1] == 'P' && p[2] == '3' && p[3] == 'D' &&
  2038. p[4] == 0x03) { // S3D_video_format_length
  2039. // the 0x7F mask ignores the reserved_bit value
  2040. const uint8_t S3D_video_format_type = p[5] & 0x7F;
  2041. if (S3D_video_format_type == 0x03 ||
  2042. S3D_video_format_type == 0x04 ||
  2043. S3D_video_format_type == 0x08 ||
  2044. S3D_video_format_type == 0x23) {
  2045. s1->has_stereo3d = 1;
  2046. switch (S3D_video_format_type) {
  2047. case 0x03:
  2048. s1->stereo3d.type = AV_STEREO3D_SIDEBYSIDE;
  2049. break;
  2050. case 0x04:
  2051. s1->stereo3d.type = AV_STEREO3D_TOPBOTTOM;
  2052. break;
  2053. case 0x08:
  2054. s1->stereo3d.type = AV_STEREO3D_2D;
  2055. break;
  2056. case 0x23:
  2057. s1->stereo3d.type = AV_STEREO3D_SIDEBYSIDE_QUINCUNX;
  2058. break;
  2059. }
  2060. }
  2061. } else if (mpeg_decode_a53_cc(avctx, p, buf_size)) {
  2062. return;
  2063. }
  2064. }
  2065. static void mpeg_decode_gop(AVCodecContext *avctx,
  2066. const uint8_t *buf, int buf_size)
  2067. {
  2068. Mpeg1Context *s1 = avctx->priv_data;
  2069. MpegEncContext *s = &s1->mpeg_enc_ctx;
  2070. int time_code_hours, time_code_minutes;
  2071. int time_code_seconds, time_code_pictures;
  2072. int broken_link;
  2073. init_get_bits(&s->gb, buf, buf_size * 8);
  2074. skip_bits1(&s->gb); /* drop_frame_flag */
  2075. time_code_hours = get_bits(&s->gb, 5);
  2076. time_code_minutes = get_bits(&s->gb, 6);
  2077. skip_bits1(&s->gb); // marker bit
  2078. time_code_seconds = get_bits(&s->gb, 6);
  2079. time_code_pictures = get_bits(&s->gb, 6);
  2080. s1->closed_gop = get_bits1(&s->gb);
  2081. /* broken_link indicate that after editing the
  2082. * reference frames of the first B-Frames after GOP I-Frame
  2083. * are missing (open gop) */
  2084. broken_link = get_bits1(&s->gb);
  2085. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  2086. av_log(s->avctx, AV_LOG_DEBUG,
  2087. "GOP (%2d:%02d:%02d.[%02d]) closed_gop=%d broken_link=%d\n",
  2088. time_code_hours, time_code_minutes, time_code_seconds,
  2089. time_code_pictures, s1->closed_gop, broken_link);
  2090. }
  2091. static int decode_chunks(AVCodecContext *avctx, AVFrame *picture,
  2092. int *got_output, const uint8_t *buf, int buf_size)
  2093. {
  2094. Mpeg1Context *s = avctx->priv_data;
  2095. MpegEncContext *s2 = &s->mpeg_enc_ctx;
  2096. const uint8_t *buf_ptr = buf;
  2097. const uint8_t *buf_end = buf + buf_size;
  2098. int ret, input_size;
  2099. int last_code = 0, skip_frame = 0;
  2100. for (;;) {
  2101. /* find next start code */
  2102. uint32_t start_code = -1;
  2103. buf_ptr = avpriv_find_start_code(buf_ptr, buf_end, &start_code);
  2104. if (start_code > 0x1ff) {
  2105. if (!skip_frame) {
  2106. if (HAVE_THREADS &&
  2107. (avctx->active_thread_type & FF_THREAD_SLICE) &&
  2108. !avctx->hwaccel) {
  2109. int i;
  2110. avctx->execute(avctx, slice_decode_thread,
  2111. &s2->thread_context[0], NULL,
  2112. s->slice_count, sizeof(void *));
  2113. for (i = 0; i < s->slice_count; i++)
  2114. s2->er.error_count += s2->thread_context[i]->er.error_count;
  2115. }
  2116. ret = slice_end(avctx, picture);
  2117. if (ret < 0)
  2118. return ret;
  2119. else if (ret) {
  2120. // FIXME: merge with the stuff in mpeg_decode_slice
  2121. if (s2->last_picture_ptr || s2->low_delay)
  2122. *got_output = 1;
  2123. }
  2124. }
  2125. s2->pict_type = 0;
  2126. return FFMAX(0, buf_ptr - buf - s2->parse_context.last_index);
  2127. }
  2128. input_size = buf_end - buf_ptr;
  2129. if (avctx->debug & FF_DEBUG_STARTCODE)
  2130. av_log(avctx, AV_LOG_DEBUG, "%3"PRIX32" at %td left %d\n",
  2131. start_code, buf_ptr - buf, input_size);
  2132. /* prepare data for next start code */
  2133. switch (start_code) {
  2134. case SEQ_START_CODE:
  2135. if (last_code == 0) {
  2136. mpeg1_decode_sequence(avctx, buf_ptr, input_size);
  2137. s->sync = 1;
  2138. } else {
  2139. av_log(avctx, AV_LOG_ERROR,
  2140. "ignoring SEQ_START_CODE after %X\n", last_code);
  2141. if (avctx->err_recognition & AV_EF_EXPLODE)
  2142. return AVERROR_INVALIDDATA;
  2143. }
  2144. break;
  2145. case PICTURE_START_CODE:
  2146. if (s2->width <= 0 || s2->height <= 0) {
  2147. av_log(avctx, AV_LOG_ERROR, "Invalid frame dimensions %dx%d.\n",
  2148. s2->width, s2->height);
  2149. return AVERROR_INVALIDDATA;
  2150. }
  2151. if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE) &&
  2152. !avctx->hwaccel && s->slice_count) {
  2153. int i;
  2154. avctx->execute(avctx, slice_decode_thread,
  2155. s2->thread_context, NULL,
  2156. s->slice_count, sizeof(void *));
  2157. for (i = 0; i < s->slice_count; i++)
  2158. s2->er.error_count += s2->thread_context[i]->er.error_count;
  2159. s->slice_count = 0;
  2160. }
  2161. if (last_code == 0 || last_code == SLICE_MIN_START_CODE) {
  2162. ret = mpeg_decode_postinit(avctx);
  2163. if (ret < 0) {
  2164. av_log(avctx, AV_LOG_ERROR,
  2165. "mpeg_decode_postinit() failure\n");
  2166. return ret;
  2167. }
  2168. /* We have a complete image: we try to decompress it. */
  2169. if (mpeg1_decode_picture(avctx, buf_ptr, input_size) < 0)
  2170. s2->pict_type = 0;
  2171. s->first_slice = 1;
  2172. last_code = PICTURE_START_CODE;
  2173. } else {
  2174. av_log(avctx, AV_LOG_ERROR,
  2175. "ignoring pic after %X\n", last_code);
  2176. if (avctx->err_recognition & AV_EF_EXPLODE)
  2177. return AVERROR_INVALIDDATA;
  2178. }
  2179. break;
  2180. case EXT_START_CODE:
  2181. init_get_bits(&s2->gb, buf_ptr, input_size * 8);
  2182. switch (get_bits(&s2->gb, 4)) {
  2183. case 0x1:
  2184. if (last_code == 0) {
  2185. mpeg_decode_sequence_extension(s);
  2186. } else {
  2187. av_log(avctx, AV_LOG_ERROR,
  2188. "ignoring seq ext after %X\n", last_code);
  2189. if (avctx->err_recognition & AV_EF_EXPLODE)
  2190. return AVERROR_INVALIDDATA;
  2191. }
  2192. break;
  2193. case 0x2:
  2194. mpeg_decode_sequence_display_extension(s);
  2195. break;
  2196. case 0x3:
  2197. mpeg_decode_quant_matrix_extension(s2);
  2198. break;
  2199. case 0x7:
  2200. mpeg_decode_picture_display_extension(s);
  2201. break;
  2202. case 0x8:
  2203. if (last_code == PICTURE_START_CODE) {
  2204. mpeg_decode_picture_coding_extension(s);
  2205. } else {
  2206. av_log(avctx, AV_LOG_ERROR,
  2207. "ignoring pic cod ext after %X\n", last_code);
  2208. if (avctx->err_recognition & AV_EF_EXPLODE)
  2209. return AVERROR_INVALIDDATA;
  2210. }
  2211. break;
  2212. }
  2213. break;
  2214. case USER_START_CODE:
  2215. mpeg_decode_user_data(avctx, buf_ptr, input_size);
  2216. break;
  2217. case GOP_START_CODE:
  2218. if (last_code == 0) {
  2219. s2->first_field = 0;
  2220. mpeg_decode_gop(avctx, buf_ptr, input_size);
  2221. s->sync = 1;
  2222. } else {
  2223. av_log(avctx, AV_LOG_ERROR,
  2224. "ignoring GOP_START_CODE after %X\n", last_code);
  2225. if (avctx->err_recognition & AV_EF_EXPLODE)
  2226. return AVERROR_INVALIDDATA;
  2227. }
  2228. break;
  2229. default:
  2230. if (start_code >= SLICE_MIN_START_CODE &&
  2231. start_code <= SLICE_MAX_START_CODE && last_code != 0) {
  2232. const int field_pic = s2->picture_structure != PICT_FRAME;
  2233. int mb_y = (start_code - SLICE_MIN_START_CODE) << field_pic;
  2234. last_code = SLICE_MIN_START_CODE;
  2235. if (s2->picture_structure == PICT_BOTTOM_FIELD)
  2236. mb_y++;
  2237. if (mb_y >= s2->mb_height) {
  2238. av_log(s2->avctx, AV_LOG_ERROR,
  2239. "slice below image (%d >= %d)\n", mb_y, s2->mb_height);
  2240. return AVERROR_INVALIDDATA;
  2241. }
  2242. if (!s2->last_picture_ptr) {
  2243. /* Skip B-frames if we do not have reference frames and
  2244. * GOP is not closed. */
  2245. if (s2->pict_type == AV_PICTURE_TYPE_B) {
  2246. if (!s->closed_gop) {
  2247. skip_frame = 1;
  2248. break;
  2249. }
  2250. }
  2251. }
  2252. if (s2->pict_type == AV_PICTURE_TYPE_I)
  2253. s->sync = 1;
  2254. if (!s2->next_picture_ptr) {
  2255. /* Skip P-frames if we do not have a reference frame or
  2256. * we have an invalid header. */
  2257. if (s2->pict_type == AV_PICTURE_TYPE_P && !s->sync) {
  2258. skip_frame = 1;
  2259. break;
  2260. }
  2261. }
  2262. if ((avctx->skip_frame >= AVDISCARD_NONREF &&
  2263. s2->pict_type == AV_PICTURE_TYPE_B) ||
  2264. (avctx->skip_frame >= AVDISCARD_NONKEY &&
  2265. s2->pict_type != AV_PICTURE_TYPE_I) ||
  2266. avctx->skip_frame >= AVDISCARD_ALL) {
  2267. skip_frame = 1;
  2268. break;
  2269. }
  2270. if (!s->mpeg_enc_ctx_allocated)
  2271. break;
  2272. if (s2->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
  2273. if (mb_y < avctx->skip_top ||
  2274. mb_y >= s2->mb_height - avctx->skip_bottom)
  2275. break;
  2276. }
  2277. if (!s2->pict_type) {
  2278. av_log(avctx, AV_LOG_ERROR, "Missing picture start code\n");
  2279. if (avctx->err_recognition & AV_EF_EXPLODE)
  2280. return AVERROR_INVALIDDATA;
  2281. break;
  2282. }
  2283. if (s->first_slice) {
  2284. skip_frame = 0;
  2285. s->first_slice = 0;
  2286. if ((ret = mpeg_field_start(s2, buf, buf_size)) < 0)
  2287. return ret;
  2288. }
  2289. if (!s2->current_picture_ptr) {
  2290. av_log(avctx, AV_LOG_ERROR,
  2291. "current_picture not initialized\n");
  2292. return AVERROR_INVALIDDATA;
  2293. }
  2294. if (HAVE_THREADS &&
  2295. (avctx->active_thread_type & FF_THREAD_SLICE) &&
  2296. !avctx->hwaccel) {
  2297. int threshold = (s2->mb_height * s->slice_count +
  2298. s2->slice_context_count / 2) /
  2299. s2->slice_context_count;
  2300. if (threshold <= mb_y) {
  2301. MpegEncContext *thread_context = s2->thread_context[s->slice_count];
  2302. thread_context->start_mb_y = mb_y;
  2303. thread_context->end_mb_y = s2->mb_height;
  2304. if (s->slice_count) {
  2305. s2->thread_context[s->slice_count - 1]->end_mb_y = mb_y;
  2306. ret = ff_update_duplicate_context(thread_context, s2);
  2307. if (ret < 0)
  2308. return ret;
  2309. }
  2310. init_get_bits(&thread_context->gb, buf_ptr, input_size * 8);
  2311. s->slice_count++;
  2312. }
  2313. buf_ptr += 2; // FIXME add minimum number of bytes per slice
  2314. } else {
  2315. ret = mpeg_decode_slice(s2, mb_y, &buf_ptr, input_size);
  2316. emms_c();
  2317. if (ret < 0) {
  2318. if (avctx->err_recognition & AV_EF_EXPLODE)
  2319. return ret;
  2320. if (s2->resync_mb_x >= 0 && s2->resync_mb_y >= 0)
  2321. ff_er_add_slice(&s2->er, s2->resync_mb_x,
  2322. s2->resync_mb_y, s2->mb_x, s2->mb_y,
  2323. ER_AC_ERROR | ER_DC_ERROR | ER_MV_ERROR);
  2324. } else {
  2325. ff_er_add_slice(&s2->er, s2->resync_mb_x,
  2326. s2->resync_mb_y, s2->mb_x - 1, s2->mb_y,
  2327. ER_AC_END | ER_DC_END | ER_MV_END);
  2328. }
  2329. }
  2330. }
  2331. break;
  2332. }
  2333. }
  2334. }
  2335. static int mpeg_decode_frame(AVCodecContext *avctx, void *data,
  2336. int *got_output, AVPacket *avpkt)
  2337. {
  2338. const uint8_t *buf = avpkt->data;
  2339. int buf_size = avpkt->size;
  2340. Mpeg1Context *s = avctx->priv_data;
  2341. AVFrame *picture = data;
  2342. MpegEncContext *s2 = &s->mpeg_enc_ctx;
  2343. av_dlog(avctx, "fill_buffer\n");
  2344. if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == SEQ_END_CODE)) {
  2345. /* special case for last picture */
  2346. if (s2->low_delay == 0 && s2->next_picture_ptr) {
  2347. int ret = av_frame_ref(picture, s2->next_picture_ptr->f);
  2348. if (ret < 0)
  2349. return ret;
  2350. s2->next_picture_ptr = NULL;
  2351. *got_output = 1;
  2352. }
  2353. return buf_size;
  2354. }
  2355. if (s2->flags & CODEC_FLAG_TRUNCATED) {
  2356. int next = ff_mpeg1_find_frame_end(&s2->parse_context, buf,
  2357. buf_size, NULL);
  2358. if (ff_combine_frame(&s2->parse_context, next,
  2359. (const uint8_t **) &buf, &buf_size) < 0)
  2360. return buf_size;
  2361. }
  2362. if (s->mpeg_enc_ctx_allocated == 0 && avctx->codec_tag == AV_RL32("VCR2"))
  2363. vcr2_init_sequence(avctx);
  2364. s->slice_count = 0;
  2365. if (avctx->extradata && !s->extradata_decoded) {
  2366. int ret = decode_chunks(avctx, picture, got_output,
  2367. avctx->extradata, avctx->extradata_size);
  2368. s->extradata_decoded = 1;
  2369. if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE))
  2370. return ret;
  2371. }
  2372. return decode_chunks(avctx, picture, got_output, buf, buf_size);
  2373. }
  2374. static void flush(AVCodecContext *avctx)
  2375. {
  2376. Mpeg1Context *s = avctx->priv_data;
  2377. s->sync = 0;
  2378. s->closed_gop = 0;
  2379. ff_mpeg_flush(avctx);
  2380. }
  2381. static av_cold int mpeg_decode_end(AVCodecContext *avctx)
  2382. {
  2383. Mpeg1Context *s = avctx->priv_data;
  2384. if (s->mpeg_enc_ctx_allocated)
  2385. ff_mpv_common_end(&s->mpeg_enc_ctx);
  2386. av_freep(&s->a53_caption);
  2387. return 0;
  2388. }
  2389. static const AVProfile mpeg2_video_profiles[] = {
  2390. { FF_PROFILE_MPEG2_422, "4:2:2" },
  2391. { FF_PROFILE_MPEG2_HIGH, "High" },
  2392. { FF_PROFILE_MPEG2_SS, "Spatially Scalable" },
  2393. { FF_PROFILE_MPEG2_SNR_SCALABLE, "SNR Scalable" },
  2394. { FF_PROFILE_MPEG2_MAIN, "Main" },
  2395. { FF_PROFILE_MPEG2_SIMPLE, "Simple" },
  2396. { FF_PROFILE_RESERVED, "Reserved" },
  2397. { FF_PROFILE_RESERVED, "Reserved" },
  2398. { FF_PROFILE_UNKNOWN },
  2399. };
  2400. AVCodec ff_mpeg1video_decoder = {
  2401. .name = "mpeg1video",
  2402. .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video"),
  2403. .type = AVMEDIA_TYPE_VIDEO,
  2404. .id = AV_CODEC_ID_MPEG1VIDEO,
  2405. .priv_data_size = sizeof(Mpeg1Context),
  2406. .init = mpeg_decode_init,
  2407. .close = mpeg_decode_end,
  2408. .decode = mpeg_decode_frame,
  2409. .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
  2410. CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY |
  2411. CODEC_CAP_SLICE_THREADS,
  2412. .flush = flush,
  2413. .update_thread_context = ONLY_IF_THREADS_ENABLED(mpeg_decode_update_thread_context)
  2414. };
  2415. AVCodec ff_mpeg2video_decoder = {
  2416. .name = "mpeg2video",
  2417. .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 video"),
  2418. .type = AVMEDIA_TYPE_VIDEO,
  2419. .id = AV_CODEC_ID_MPEG2VIDEO,
  2420. .priv_data_size = sizeof(Mpeg1Context),
  2421. .init = mpeg_decode_init,
  2422. .close = mpeg_decode_end,
  2423. .decode = mpeg_decode_frame,
  2424. .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
  2425. CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY |
  2426. CODEC_CAP_SLICE_THREADS,
  2427. .flush = flush,
  2428. .profiles = NULL_IF_CONFIG_SMALL(mpeg2_video_profiles),
  2429. };
  2430. #if FF_API_XVMC
  2431. #if CONFIG_MPEG_XVMC_DECODER
  2432. static av_cold int mpeg_mc_decode_init(AVCodecContext *avctx)
  2433. {
  2434. if (avctx->active_thread_type & FF_THREAD_SLICE)
  2435. return -1;
  2436. if (!(avctx->slice_flags & SLICE_FLAG_CODED_ORDER))
  2437. return -1;
  2438. if (!(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD)) {
  2439. av_dlog(avctx, "mpeg12.c: XvMC decoder will work better if SLICE_FLAG_ALLOW_FIELD is set\n");
  2440. }
  2441. mpeg_decode_init(avctx);
  2442. avctx->pix_fmt = AV_PIX_FMT_XVMC_MPEG2_IDCT;
  2443. avctx->xvmc_acceleration = 2; // 2 - the blocks are packed!
  2444. return 0;
  2445. }
  2446. AVCodec ff_mpeg_xvmc_decoder = {
  2447. .name = "mpegvideo_xvmc",
  2448. .long_name = NULL_IF_CONFIG_SMALL("MPEG-1/2 video XvMC (X-Video Motion Compensation)"),
  2449. .type = AVMEDIA_TYPE_VIDEO,
  2450. .id = AV_CODEC_ID_MPEG2VIDEO_XVMC,
  2451. .priv_data_size = sizeof(Mpeg1Context),
  2452. .init = mpeg_mc_decode_init,
  2453. .close = mpeg_decode_end,
  2454. .decode = mpeg_decode_frame,
  2455. .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
  2456. CODEC_CAP_TRUNCATED | CODEC_CAP_HWACCEL | CODEC_CAP_DELAY,
  2457. .flush = flush,
  2458. };
  2459. #endif
  2460. #endif /* FF_API_XVMC */