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.

2923 lines
106KB

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