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.

2762 lines
99KB

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