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.

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