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.

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