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.

2710 lines
96KB

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