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.

2939 lines
107KB

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