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.

3146 lines
114KB

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