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.

2964 lines
108KB

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