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.

2976 lines
108KB

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