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.

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