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.

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