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.

2981 lines
108KB

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