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.

2722 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) << 18;
  1220. if (bit_rate_ext < INT_MAX / 400 &&
  1221. bit_rate_ext * 400 < INT_MAX - s->bit_rate) {
  1222. s->bit_rate += bit_rate_ext * 400;
  1223. } else {
  1224. av_log(s->avctx, AV_LOG_WARNING, "Invalid bit rate extension value: %d\n",
  1225. bit_rate_ext >> 18);
  1226. s->bit_rate = 0;
  1227. }
  1228. skip_bits1(&s->gb); /* marker */
  1229. s->avctx->rc_buffer_size += get_bits(&s->gb, 8) * 1024 * 16 << 10;
  1230. s->low_delay = get_bits1(&s->gb);
  1231. if (s->avctx->flags & AV_CODEC_FLAG_LOW_DELAY)
  1232. s->low_delay = 1;
  1233. s1->frame_rate_ext.num = get_bits(&s->gb, 2) + 1;
  1234. s1->frame_rate_ext.den = get_bits(&s->gb, 5) + 1;
  1235. ff_dlog(s->avctx, "sequence extension\n");
  1236. s->codec_id = s->avctx->codec_id = AV_CODEC_ID_MPEG2VIDEO;
  1237. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  1238. av_log(s->avctx, AV_LOG_DEBUG,
  1239. "profile: %d, level: %d vbv buffer: %d, bitrate:%d\n",
  1240. s->avctx->profile, s->avctx->level,
  1241. s->avctx->rc_buffer_size, s->bit_rate);
  1242. }
  1243. static void mpeg_decode_sequence_display_extension(Mpeg1Context *s1)
  1244. {
  1245. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1246. int color_description, w, h;
  1247. skip_bits(&s->gb, 3); /* video format */
  1248. color_description = get_bits1(&s->gb);
  1249. if (color_description) {
  1250. s->avctx->color_primaries = get_bits(&s->gb, 8);
  1251. s->avctx->color_trc = get_bits(&s->gb, 8);
  1252. s->avctx->colorspace = get_bits(&s->gb, 8);
  1253. }
  1254. w = get_bits(&s->gb, 14);
  1255. skip_bits(&s->gb, 1); // marker
  1256. h = get_bits(&s->gb, 14);
  1257. // remaining 3 bits are zero padding
  1258. s1->pan_scan.width = 16 * w;
  1259. s1->pan_scan.height = 16 * h;
  1260. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  1261. av_log(s->avctx, AV_LOG_DEBUG, "sde w:%d, h:%d\n", w, h);
  1262. }
  1263. static void mpeg_decode_picture_display_extension(Mpeg1Context *s1)
  1264. {
  1265. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1266. int i, nofco;
  1267. nofco = 1;
  1268. if (s->progressive_sequence) {
  1269. if (s->repeat_first_field) {
  1270. nofco++;
  1271. if (s->top_field_first)
  1272. nofco++;
  1273. }
  1274. } else {
  1275. if (s->picture_structure == PICT_FRAME) {
  1276. nofco++;
  1277. if (s->repeat_first_field)
  1278. nofco++;
  1279. }
  1280. }
  1281. for (i = 0; i < nofco; i++) {
  1282. s1->pan_scan.position[i][0] = get_sbits(&s->gb, 16);
  1283. skip_bits(&s->gb, 1); // marker
  1284. s1->pan_scan.position[i][1] = get_sbits(&s->gb, 16);
  1285. skip_bits(&s->gb, 1); // marker
  1286. }
  1287. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  1288. av_log(s->avctx, AV_LOG_DEBUG,
  1289. "pde (%"PRId16",%"PRId16") (%"PRId16",%"PRId16") (%"PRId16",%"PRId16")\n",
  1290. s1->pan_scan.position[0][0], s1->pan_scan.position[0][1],
  1291. s1->pan_scan.position[1][0], s1->pan_scan.position[1][1],
  1292. s1->pan_scan.position[2][0], s1->pan_scan.position[2][1]);
  1293. }
  1294. static int load_matrix(MpegEncContext *s, uint16_t matrix0[64],
  1295. uint16_t matrix1[64], int intra)
  1296. {
  1297. int i;
  1298. for (i = 0; i < 64; i++) {
  1299. int j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
  1300. int v = get_bits(&s->gb, 8);
  1301. if (v == 0) {
  1302. av_log(s->avctx, AV_LOG_ERROR, "matrix damaged\n");
  1303. return AVERROR_INVALIDDATA;
  1304. }
  1305. if (intra && i == 0 && v != 8) {
  1306. av_log(s->avctx, AV_LOG_ERROR, "intra matrix invalid, ignoring\n");
  1307. v = 8; // needed by pink.mpg / issue1046
  1308. }
  1309. matrix0[j] = v;
  1310. if (matrix1)
  1311. matrix1[j] = v;
  1312. }
  1313. return 0;
  1314. }
  1315. static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
  1316. {
  1317. ff_dlog(s->avctx, "matrix extension\n");
  1318. if (get_bits1(&s->gb))
  1319. load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
  1320. if (get_bits1(&s->gb))
  1321. load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
  1322. if (get_bits1(&s->gb))
  1323. load_matrix(s, s->chroma_intra_matrix, NULL, 1);
  1324. if (get_bits1(&s->gb))
  1325. load_matrix(s, s->chroma_inter_matrix, NULL, 0);
  1326. }
  1327. static void mpeg_decode_picture_coding_extension(Mpeg1Context *s1)
  1328. {
  1329. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1330. s->full_pel[0] = s->full_pel[1] = 0;
  1331. s->mpeg_f_code[0][0] = get_bits(&s->gb, 4);
  1332. s->mpeg_f_code[0][1] = get_bits(&s->gb, 4);
  1333. s->mpeg_f_code[1][0] = get_bits(&s->gb, 4);
  1334. s->mpeg_f_code[1][1] = get_bits(&s->gb, 4);
  1335. if (!s->pict_type && s1->mpeg_enc_ctx_allocated) {
  1336. av_log(s->avctx, AV_LOG_ERROR,
  1337. "Missing picture start code, guessing missing values\n");
  1338. if (s->mpeg_f_code[1][0] == 15 && s->mpeg_f_code[1][1] == 15) {
  1339. if (s->mpeg_f_code[0][0] == 15 && s->mpeg_f_code[0][1] == 15)
  1340. s->pict_type = AV_PICTURE_TYPE_I;
  1341. else
  1342. s->pict_type = AV_PICTURE_TYPE_P;
  1343. } else
  1344. s->pict_type = AV_PICTURE_TYPE_B;
  1345. s->current_picture.f->pict_type = s->pict_type;
  1346. s->current_picture.f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
  1347. }
  1348. s->intra_dc_precision = get_bits(&s->gb, 2);
  1349. s->picture_structure = get_bits(&s->gb, 2);
  1350. s->top_field_first = get_bits1(&s->gb);
  1351. s->frame_pred_frame_dct = get_bits1(&s->gb);
  1352. s->concealment_motion_vectors = get_bits1(&s->gb);
  1353. s->q_scale_type = get_bits1(&s->gb);
  1354. s->intra_vlc_format = get_bits1(&s->gb);
  1355. s->alternate_scan = get_bits1(&s->gb);
  1356. s->repeat_first_field = get_bits1(&s->gb);
  1357. s->chroma_420_type = get_bits1(&s->gb);
  1358. s->progressive_frame = get_bits1(&s->gb);
  1359. if (s->progressive_sequence && !s->progressive_frame) {
  1360. s->progressive_frame = 1;
  1361. av_log(s->avctx, AV_LOG_ERROR,
  1362. "interlaced frame in progressive sequence, ignoring\n");
  1363. }
  1364. if (s->picture_structure == 0 ||
  1365. (s->progressive_frame && s->picture_structure != PICT_FRAME)) {
  1366. av_log(s->avctx, AV_LOG_ERROR,
  1367. "picture_structure %d invalid, ignoring\n",
  1368. s->picture_structure);
  1369. s->picture_structure = PICT_FRAME;
  1370. }
  1371. if (s->progressive_sequence && !s->frame_pred_frame_dct)
  1372. av_log(s->avctx, AV_LOG_WARNING, "invalid frame_pred_frame_dct\n");
  1373. if (s->picture_structure == PICT_FRAME) {
  1374. s->v_edge_pos = 16 * s->mb_height;
  1375. } else {
  1376. s->v_edge_pos = 8 * s->mb_height;
  1377. memset(s->mbskip_table, 0, s->mb_stride * s->mb_height);
  1378. }
  1379. if (s->alternate_scan) {
  1380. ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_alternate_vertical_scan);
  1381. ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_alternate_vertical_scan);
  1382. } else {
  1383. ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_zigzag_direct);
  1384. ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_zigzag_direct);
  1385. }
  1386. /* composite display not parsed */
  1387. ff_dlog(s->avctx, "intra_dc_precision=%d\n", s->intra_dc_precision);
  1388. ff_dlog(s->avctx, "picture_structure=%d\n", s->picture_structure);
  1389. ff_dlog(s->avctx, "top field first=%d\n", s->top_field_first);
  1390. ff_dlog(s->avctx, "repeat first field=%d\n", s->repeat_first_field);
  1391. ff_dlog(s->avctx, "conceal=%d\n", s->concealment_motion_vectors);
  1392. ff_dlog(s->avctx, "intra_vlc_format=%d\n", s->intra_vlc_format);
  1393. ff_dlog(s->avctx, "alternate_scan=%d\n", s->alternate_scan);
  1394. ff_dlog(s->avctx, "frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
  1395. ff_dlog(s->avctx, "progressive_frame=%d\n", s->progressive_frame);
  1396. }
  1397. static int mpeg_field_start(MpegEncContext *s, const uint8_t *buf, int buf_size)
  1398. {
  1399. AVCodecContext *avctx = s->avctx;
  1400. Mpeg1Context *s1 = (Mpeg1Context *) s;
  1401. int ret;
  1402. if (s->picture_structure == PICT_FRAME)
  1403. s->first_field = 0;
  1404. else
  1405. s->first_field ^= 1;
  1406. /* start frame decoding */
  1407. if (s->first_field || s->picture_structure == PICT_FRAME) {
  1408. AVFrameSideData *pan_scan;
  1409. if ((ret = ff_mpv_frame_start(s, avctx)) < 0)
  1410. return ret;
  1411. ff_mpeg_er_frame_start(s);
  1412. /* first check if we must repeat the frame */
  1413. s->current_picture_ptr->f->repeat_pict = 0;
  1414. if (s->repeat_first_field) {
  1415. if (s->progressive_sequence) {
  1416. if (s->top_field_first)
  1417. s->current_picture_ptr->f->repeat_pict = 4;
  1418. else
  1419. s->current_picture_ptr->f->repeat_pict = 2;
  1420. } else if (s->progressive_frame) {
  1421. s->current_picture_ptr->f->repeat_pict = 1;
  1422. }
  1423. }
  1424. pan_scan = av_frame_new_side_data(s->current_picture_ptr->f,
  1425. AV_FRAME_DATA_PANSCAN,
  1426. sizeof(s1->pan_scan));
  1427. if (!pan_scan)
  1428. return AVERROR(ENOMEM);
  1429. memcpy(pan_scan->data, &s1->pan_scan, sizeof(s1->pan_scan));
  1430. if (s1->a53_caption) {
  1431. AVFrameSideData *sd = av_frame_new_side_data(
  1432. s->current_picture_ptr->f, AV_FRAME_DATA_A53_CC,
  1433. s1->a53_caption_size);
  1434. if (sd)
  1435. memcpy(sd->data, s1->a53_caption, s1->a53_caption_size);
  1436. av_freep(&s1->a53_caption);
  1437. }
  1438. if (s1->has_stereo3d) {
  1439. AVStereo3D *stereo = av_stereo3d_create_side_data(s->current_picture_ptr->f);
  1440. if (!stereo)
  1441. return AVERROR(ENOMEM);
  1442. *stereo = s1->stereo3d;
  1443. s1->has_stereo3d = 0;
  1444. }
  1445. if (s1->has_afd) {
  1446. AVFrameSideData *sd =
  1447. av_frame_new_side_data(s->current_picture_ptr->f,
  1448. AV_FRAME_DATA_AFD, 1);
  1449. if (!sd)
  1450. return AVERROR(ENOMEM);
  1451. *sd->data = s1->afd;
  1452. s1->has_afd = 0;
  1453. }
  1454. if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_FRAME))
  1455. ff_thread_finish_setup(avctx);
  1456. } else { // second field
  1457. int i;
  1458. if (!s->current_picture_ptr) {
  1459. av_log(s->avctx, AV_LOG_ERROR, "first field missing\n");
  1460. return AVERROR_INVALIDDATA;
  1461. }
  1462. if (s->avctx->hwaccel &&
  1463. (s->avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD)) {
  1464. if (s->avctx->hwaccel->end_frame(s->avctx) < 0)
  1465. av_log(avctx, AV_LOG_ERROR,
  1466. "hardware accelerator failed to decode first field\n");
  1467. }
  1468. for (i = 0; i < 4; i++) {
  1469. s->current_picture.f->data[i] = s->current_picture_ptr->f->data[i];
  1470. if (s->picture_structure == PICT_BOTTOM_FIELD)
  1471. s->current_picture.f->data[i] +=
  1472. s->current_picture_ptr->f->linesize[i];
  1473. }
  1474. }
  1475. if (avctx->hwaccel) {
  1476. if ((ret = avctx->hwaccel->start_frame(avctx, buf, buf_size)) < 0)
  1477. return ret;
  1478. }
  1479. #if FF_API_XVMC
  1480. FF_DISABLE_DEPRECATION_WARNINGS
  1481. // ff_mpv_frame_start will call this function too,
  1482. // but we need to call it on every field
  1483. if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
  1484. if (ff_xvmc_field_start(s, avctx) < 0)
  1485. return -1;
  1486. FF_ENABLE_DEPRECATION_WARNINGS
  1487. #endif /* FF_API_XVMC */
  1488. return 0;
  1489. }
  1490. #define DECODE_SLICE_ERROR -1
  1491. #define DECODE_SLICE_OK 0
  1492. /**
  1493. * Decode a slice.
  1494. * MpegEncContext.mb_y must be set to the MB row from the startcode.
  1495. * @return DECODE_SLICE_ERROR if the slice is damaged,
  1496. * DECODE_SLICE_OK if this slice is OK
  1497. */
  1498. static int mpeg_decode_slice(MpegEncContext *s, int mb_y,
  1499. const uint8_t **buf, int buf_size)
  1500. {
  1501. AVCodecContext *avctx = s->avctx;
  1502. const int field_pic = s->picture_structure != PICT_FRAME;
  1503. int ret;
  1504. s->resync_mb_x =
  1505. s->resync_mb_y = -1;
  1506. assert(mb_y < s->mb_height);
  1507. init_get_bits(&s->gb, *buf, buf_size * 8);
  1508. ff_mpeg1_clean_buffers(s);
  1509. s->interlaced_dct = 0;
  1510. s->qscale = get_qscale(s);
  1511. if (s->qscale == 0) {
  1512. av_log(s->avctx, AV_LOG_ERROR, "qscale == 0\n");
  1513. return AVERROR_INVALIDDATA;
  1514. }
  1515. /* extra slice info */
  1516. while (get_bits1(&s->gb) != 0)
  1517. skip_bits(&s->gb, 8);
  1518. s->mb_x = 0;
  1519. if (mb_y == 0 && s->codec_tag == AV_RL32("SLIF")) {
  1520. skip_bits1(&s->gb);
  1521. } else {
  1522. while (get_bits_left(&s->gb) > 0) {
  1523. int code = get_vlc2(&s->gb, ff_mbincr_vlc.table,
  1524. MBINCR_VLC_BITS, 2);
  1525. if (code < 0) {
  1526. av_log(s->avctx, AV_LOG_ERROR, "first mb_incr damaged\n");
  1527. return AVERROR_INVALIDDATA;
  1528. }
  1529. if (code >= 33) {
  1530. if (code == 33)
  1531. s->mb_x += 33;
  1532. /* otherwise, stuffing, nothing to do */
  1533. } else {
  1534. s->mb_x += code;
  1535. break;
  1536. }
  1537. }
  1538. }
  1539. if (s->mb_x >= (unsigned) s->mb_width) {
  1540. av_log(s->avctx, AV_LOG_ERROR, "initial skip overflow\n");
  1541. return AVERROR_INVALIDDATA;
  1542. }
  1543. if (avctx->hwaccel) {
  1544. const uint8_t *buf_end, *buf_start = *buf - 4; /* include start_code */
  1545. int start_code = -1;
  1546. buf_end = avpriv_find_start_code(buf_start + 2, *buf + buf_size, &start_code);
  1547. if (buf_end < *buf + buf_size)
  1548. buf_end -= 4;
  1549. s->mb_y = mb_y;
  1550. if (avctx->hwaccel->decode_slice(avctx, buf_start, buf_end - buf_start) < 0)
  1551. return DECODE_SLICE_ERROR;
  1552. *buf = buf_end;
  1553. return DECODE_SLICE_OK;
  1554. }
  1555. s->resync_mb_x = s->mb_x;
  1556. s->resync_mb_y = s->mb_y = mb_y;
  1557. s->mb_skip_run = 0;
  1558. ff_init_block_index(s);
  1559. if (s->mb_y == 0 && s->mb_x == 0 && (s->first_field || s->picture_structure == PICT_FRAME)) {
  1560. if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
  1561. av_log(s->avctx, AV_LOG_DEBUG,
  1562. "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",
  1563. s->qscale,
  1564. s->mpeg_f_code[0][0], s->mpeg_f_code[0][1],
  1565. s->mpeg_f_code[1][0], s->mpeg_f_code[1][1],
  1566. s->pict_type == AV_PICTURE_TYPE_I ? "I" :
  1567. (s->pict_type == AV_PICTURE_TYPE_P ? "P" :
  1568. (s->pict_type == AV_PICTURE_TYPE_B ? "B" : "S")),
  1569. s->progressive_sequence ? "ps" : "",
  1570. s->progressive_frame ? "pf" : "",
  1571. s->alternate_scan ? "alt" : "",
  1572. s->top_field_first ? "top" : "",
  1573. s->intra_dc_precision, s->picture_structure,
  1574. s->frame_pred_frame_dct, s->concealment_motion_vectors,
  1575. s->q_scale_type, s->intra_vlc_format,
  1576. s->repeat_first_field, s->chroma_420_type ? "420" : "");
  1577. }
  1578. }
  1579. for (;;) {
  1580. #if FF_API_XVMC
  1581. FF_DISABLE_DEPRECATION_WARNINGS
  1582. // If 1, we memcpy blocks in xvmcvideo.
  1583. if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1)
  1584. ff_xvmc_init_block(s); // set s->block
  1585. FF_ENABLE_DEPRECATION_WARNINGS
  1586. #endif /* FF_API_XVMC */
  1587. if ((ret = mpeg_decode_mb(s, s->block)) < 0)
  1588. return ret;
  1589. // Note motion_val is normally NULL unless we want to extract the MVs.
  1590. if (s->current_picture.motion_val[0] && !s->encoding) {
  1591. const int wrap = s->b8_stride;
  1592. int xy = s->mb_x * 2 + s->mb_y * 2 * wrap;
  1593. int b8_xy = 4 * (s->mb_x + s->mb_y * s->mb_stride);
  1594. int motion_x, motion_y, dir, i;
  1595. for (i = 0; i < 2; i++) {
  1596. for (dir = 0; dir < 2; dir++) {
  1597. if (s->mb_intra ||
  1598. (dir == 1 && s->pict_type != AV_PICTURE_TYPE_B)) {
  1599. motion_x = motion_y = 0;
  1600. } else if (s->mv_type == MV_TYPE_16X16 ||
  1601. (s->mv_type == MV_TYPE_FIELD && field_pic)) {
  1602. motion_x = s->mv[dir][0][0];
  1603. motion_y = s->mv[dir][0][1];
  1604. } else { /* if ((s->mv_type == MV_TYPE_FIELD) || (s->mv_type == MV_TYPE_16X8)) */
  1605. motion_x = s->mv[dir][i][0];
  1606. motion_y = s->mv[dir][i][1];
  1607. }
  1608. s->current_picture.motion_val[dir][xy][0] = motion_x;
  1609. s->current_picture.motion_val[dir][xy][1] = motion_y;
  1610. s->current_picture.motion_val[dir][xy + 1][0] = motion_x;
  1611. s->current_picture.motion_val[dir][xy + 1][1] = motion_y;
  1612. s->current_picture.ref_index [dir][b8_xy] =
  1613. s->current_picture.ref_index [dir][b8_xy + 1] = s->field_select[dir][i];
  1614. assert(s->field_select[dir][i] == 0 ||
  1615. s->field_select[dir][i] == 1);
  1616. }
  1617. xy += wrap;
  1618. b8_xy += 2;
  1619. }
  1620. }
  1621. s->dest[0] += 16;
  1622. s->dest[1] += 16 >> s->chroma_x_shift;
  1623. s->dest[2] += 16 >> s->chroma_x_shift;
  1624. ff_mpv_decode_mb(s, s->block);
  1625. if (++s->mb_x >= s->mb_width) {
  1626. const int mb_size = 16;
  1627. ff_mpeg_draw_horiz_band(s, mb_size * (s->mb_y >> field_pic), mb_size);
  1628. ff_mpv_report_decode_progress(s);
  1629. s->mb_x = 0;
  1630. s->mb_y += 1 << field_pic;
  1631. if (s->mb_y >= s->mb_height) {
  1632. int left = get_bits_left(&s->gb);
  1633. int is_d10 = s->chroma_format == 2 &&
  1634. s->pict_type == AV_PICTURE_TYPE_I &&
  1635. avctx->profile == 0 && avctx->level == 5 &&
  1636. s->intra_dc_precision == 2 &&
  1637. s->q_scale_type == 1 && s->alternate_scan == 0 &&
  1638. s->progressive_frame == 0
  1639. /* vbv_delay == 0xBBB || 0xE10 */;
  1640. if (left < 0 ||
  1641. (left && show_bits(&s->gb, FFMIN(left, 23)) && !is_d10) ||
  1642. ((avctx->err_recognition & AV_EF_BUFFER) && left > 8)) {
  1643. av_log(avctx, AV_LOG_ERROR, "end mismatch left=%d %0X\n",
  1644. left, show_bits(&s->gb, FFMIN(left, 23)));
  1645. return AVERROR_INVALIDDATA;
  1646. } else
  1647. goto eos;
  1648. }
  1649. ff_init_block_index(s);
  1650. }
  1651. /* skip mb handling */
  1652. if (s->mb_skip_run == -1) {
  1653. /* read increment again */
  1654. s->mb_skip_run = 0;
  1655. for (;;) {
  1656. int code = get_vlc2(&s->gb, ff_mbincr_vlc.table,
  1657. MBINCR_VLC_BITS, 2);
  1658. if (code < 0) {
  1659. av_log(s->avctx, AV_LOG_ERROR, "mb incr damaged\n");
  1660. return AVERROR_INVALIDDATA;
  1661. }
  1662. if (code >= 33) {
  1663. if (code == 33) {
  1664. s->mb_skip_run += 33;
  1665. } else if (code == 35) {
  1666. if (s->mb_skip_run != 0 || show_bits(&s->gb, 15) != 0) {
  1667. av_log(s->avctx, AV_LOG_ERROR, "slice mismatch\n");
  1668. return AVERROR_INVALIDDATA;
  1669. }
  1670. goto eos; /* end of slice */
  1671. }
  1672. /* otherwise, stuffing, nothing to do */
  1673. } else {
  1674. s->mb_skip_run += code;
  1675. break;
  1676. }
  1677. }
  1678. if (s->mb_skip_run) {
  1679. int i;
  1680. if (s->pict_type == AV_PICTURE_TYPE_I) {
  1681. av_log(s->avctx, AV_LOG_ERROR,
  1682. "skipped MB in I-frame at %d %d\n", s->mb_x, s->mb_y);
  1683. return AVERROR_INVALIDDATA;
  1684. }
  1685. /* skip mb */
  1686. s->mb_intra = 0;
  1687. for (i = 0; i < 12; i++)
  1688. s->block_last_index[i] = -1;
  1689. if (s->picture_structure == PICT_FRAME)
  1690. s->mv_type = MV_TYPE_16X16;
  1691. else
  1692. s->mv_type = MV_TYPE_FIELD;
  1693. if (s->pict_type == AV_PICTURE_TYPE_P) {
  1694. /* if P type, zero motion vector is implied */
  1695. s->mv_dir = MV_DIR_FORWARD;
  1696. s->mv[0][0][0] = s->mv[0][0][1] = 0;
  1697. s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
  1698. s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
  1699. s->field_select[0][0] = (s->picture_structure - 1) & 1;
  1700. } else {
  1701. /* if B type, reuse previous vectors and directions */
  1702. s->mv[0][0][0] = s->last_mv[0][0][0];
  1703. s->mv[0][0][1] = s->last_mv[0][0][1];
  1704. s->mv[1][0][0] = s->last_mv[1][0][0];
  1705. s->mv[1][0][1] = s->last_mv[1][0][1];
  1706. }
  1707. }
  1708. }
  1709. }
  1710. eos: // end of slice
  1711. *buf += (get_bits_count(&s->gb) - 1) / 8;
  1712. ff_dlog(s, "y %d %d %d %d\n", s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y);
  1713. return 0;
  1714. }
  1715. static int slice_decode_thread(AVCodecContext *c, void *arg)
  1716. {
  1717. MpegEncContext *s = *(void **) arg;
  1718. const uint8_t *buf = s->gb.buffer;
  1719. int mb_y = s->start_mb_y;
  1720. const int field_pic = s->picture_structure != PICT_FRAME;
  1721. s->er.error_count = (3 * (s->end_mb_y - s->start_mb_y) * s->mb_width) >> field_pic;
  1722. for (;;) {
  1723. uint32_t start_code;
  1724. int ret;
  1725. ret = mpeg_decode_slice(s, mb_y, &buf, s->gb.buffer_end - buf);
  1726. emms_c();
  1727. ff_dlog(c, "ret:%d resync:%d/%d mb:%d/%d ts:%d/%d ec:%d\n",
  1728. ret, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y,
  1729. s->start_mb_y, s->end_mb_y, s->er.error_count);
  1730. if (ret < 0) {
  1731. if (c->err_recognition & AV_EF_EXPLODE)
  1732. return ret;
  1733. if (s->resync_mb_x >= 0 && s->resync_mb_y >= 0)
  1734. ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
  1735. s->mb_x, s->mb_y,
  1736. ER_AC_ERROR | ER_DC_ERROR | ER_MV_ERROR);
  1737. } else {
  1738. ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
  1739. s->mb_x - 1, s->mb_y,
  1740. ER_AC_END | ER_DC_END | ER_MV_END);
  1741. }
  1742. if (s->mb_y == s->end_mb_y)
  1743. return 0;
  1744. start_code = -1;
  1745. buf = avpriv_find_start_code(buf, s->gb.buffer_end, &start_code);
  1746. mb_y = (start_code - SLICE_MIN_START_CODE) << field_pic;
  1747. if (s->picture_structure == PICT_BOTTOM_FIELD)
  1748. mb_y++;
  1749. if (mb_y < 0 || mb_y >= s->end_mb_y)
  1750. return AVERROR_INVALIDDATA;
  1751. }
  1752. }
  1753. /**
  1754. * Handle slice ends.
  1755. * @return 1 if it seems to be the last slice
  1756. */
  1757. static int slice_end(AVCodecContext *avctx, AVFrame *pict)
  1758. {
  1759. Mpeg1Context *s1 = avctx->priv_data;
  1760. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1761. if (!s1->mpeg_enc_ctx_allocated || !s->current_picture_ptr)
  1762. return 0;
  1763. if (s->avctx->hwaccel) {
  1764. if (s->avctx->hwaccel->end_frame(s->avctx) < 0)
  1765. av_log(avctx, AV_LOG_ERROR,
  1766. "hardware accelerator failed to decode picture\n");
  1767. }
  1768. #if FF_API_XVMC
  1769. FF_DISABLE_DEPRECATION_WARNINGS
  1770. if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
  1771. ff_xvmc_field_end(s);
  1772. FF_ENABLE_DEPRECATION_WARNINGS
  1773. #endif /* FF_API_XVMC */
  1774. /* end of slice reached */
  1775. if (/* s->mb_y << field_pic == s->mb_height && */ !s->first_field) {
  1776. /* end of image */
  1777. ff_er_frame_end(&s->er);
  1778. ff_mpv_frame_end(s);
  1779. if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
  1780. int ret = av_frame_ref(pict, s->current_picture_ptr->f);
  1781. if (ret < 0)
  1782. return ret;
  1783. ff_print_debug_info(s, s->current_picture_ptr);
  1784. } else {
  1785. if (avctx->active_thread_type & FF_THREAD_FRAME)
  1786. s->picture_number++;
  1787. /* latency of 1 frame for I- and P-frames */
  1788. /* XXX: use another variable than picture_number */
  1789. if (s->last_picture_ptr) {
  1790. int ret = av_frame_ref(pict, s->last_picture_ptr->f);
  1791. if (ret < 0)
  1792. return ret;
  1793. ff_print_debug_info(s, s->last_picture_ptr);
  1794. }
  1795. }
  1796. return 1;
  1797. } else {
  1798. return 0;
  1799. }
  1800. }
  1801. static int mpeg1_decode_sequence(AVCodecContext *avctx,
  1802. const uint8_t *buf, int buf_size)
  1803. {
  1804. Mpeg1Context *s1 = avctx->priv_data;
  1805. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1806. int width, height;
  1807. int i, v, j;
  1808. init_get_bits(&s->gb, buf, buf_size * 8);
  1809. width = get_bits(&s->gb, 12);
  1810. height = get_bits(&s->gb, 12);
  1811. if (width == 0 || height == 0) {
  1812. av_log(avctx, AV_LOG_WARNING,
  1813. "Invalid horizontal or vertical size value.\n");
  1814. if (avctx->err_recognition & AV_EF_BITSTREAM)
  1815. return AVERROR_INVALIDDATA;
  1816. }
  1817. s->aspect_ratio_info = get_bits(&s->gb, 4);
  1818. if (s->aspect_ratio_info == 0) {
  1819. av_log(avctx, AV_LOG_ERROR, "aspect ratio has forbidden 0 value\n");
  1820. if (avctx->err_recognition & AV_EF_BITSTREAM)
  1821. return AVERROR_INVALIDDATA;
  1822. }
  1823. s->frame_rate_index = get_bits(&s->gb, 4);
  1824. if (s->frame_rate_index == 0 || s->frame_rate_index > 13) {
  1825. av_log(avctx, AV_LOG_WARNING,
  1826. "frame_rate_index %d is invalid\n", s->frame_rate_index);
  1827. return AVERROR_INVALIDDATA;
  1828. }
  1829. s->bit_rate = get_bits(&s->gb, 18) * 400;
  1830. if (get_bits1(&s->gb) == 0) { /* marker */
  1831. av_log(avctx, AV_LOG_ERROR, "Marker in sequence header missing\n");
  1832. return AVERROR_INVALIDDATA;
  1833. }
  1834. s->width = width;
  1835. s->height = height;
  1836. s->avctx->rc_buffer_size = get_bits(&s->gb, 10) * 1024 * 16;
  1837. skip_bits(&s->gb, 1);
  1838. /* get matrix */
  1839. if (get_bits1(&s->gb)) {
  1840. load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
  1841. } else {
  1842. for (i = 0; i < 64; i++) {
  1843. j = s->idsp.idct_permutation[i];
  1844. v = ff_mpeg1_default_intra_matrix[i];
  1845. s->intra_matrix[j] = v;
  1846. s->chroma_intra_matrix[j] = v;
  1847. }
  1848. }
  1849. if (get_bits1(&s->gb)) {
  1850. load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
  1851. } else {
  1852. for (i = 0; i < 64; i++) {
  1853. int j = s->idsp.idct_permutation[i];
  1854. v = ff_mpeg1_default_non_intra_matrix[i];
  1855. s->inter_matrix[j] = v;
  1856. s->chroma_inter_matrix[j] = v;
  1857. }
  1858. }
  1859. if (show_bits(&s->gb, 23) != 0) {
  1860. av_log(s->avctx, AV_LOG_ERROR, "sequence header damaged\n");
  1861. return AVERROR_INVALIDDATA;
  1862. }
  1863. /* We set MPEG-2 parameters so that it emulates MPEG-1. */
  1864. s->progressive_sequence = 1;
  1865. s->progressive_frame = 1;
  1866. s->picture_structure = PICT_FRAME;
  1867. s->frame_pred_frame_dct = 1;
  1868. s->chroma_format = 1;
  1869. s->codec_id =
  1870. s->avctx->codec_id = AV_CODEC_ID_MPEG1VIDEO;
  1871. s->out_format = FMT_MPEG1;
  1872. if (s->avctx->flags & AV_CODEC_FLAG_LOW_DELAY)
  1873. s->low_delay = 1;
  1874. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  1875. av_log(s->avctx, AV_LOG_DEBUG, "vbv buffer: %d, bitrate:%d\n",
  1876. s->avctx->rc_buffer_size, s->bit_rate);
  1877. return 0;
  1878. }
  1879. static int vcr2_init_sequence(AVCodecContext *avctx)
  1880. {
  1881. Mpeg1Context *s1 = avctx->priv_data;
  1882. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1883. int i, v, ret;
  1884. /* start new MPEG-1 context decoding */
  1885. s->out_format = FMT_MPEG1;
  1886. if (s1->mpeg_enc_ctx_allocated) {
  1887. ff_mpv_common_end(s);
  1888. }
  1889. s->width = avctx->coded_width;
  1890. s->height = avctx->coded_height;
  1891. avctx->has_b_frames = 0; // true?
  1892. s->low_delay = 1;
  1893. avctx->pix_fmt = mpeg_get_pixelformat(avctx);
  1894. #if FF_API_XVMC
  1895. if ((avctx->pix_fmt == AV_PIX_FMT_XVMC_MPEG2_IDCT || avctx->hwaccel) &&
  1896. avctx->idct_algo == FF_IDCT_AUTO)
  1897. #else
  1898. if (avctx->hwaccel && avctx->idct_algo == FF_IDCT_AUTO)
  1899. #endif /* FF_API_XVMC */
  1900. avctx->idct_algo = FF_IDCT_SIMPLE;
  1901. ff_mpv_idct_init(s);
  1902. if ((ret = ff_mpv_common_init(s)) < 0)
  1903. return ret;
  1904. s1->mpeg_enc_ctx_allocated = 1;
  1905. for (i = 0; i < 64; i++) {
  1906. int j = s->idsp.idct_permutation[i];
  1907. v = ff_mpeg1_default_intra_matrix[i];
  1908. s->intra_matrix[j] = v;
  1909. s->chroma_intra_matrix[j] = v;
  1910. v = ff_mpeg1_default_non_intra_matrix[i];
  1911. s->inter_matrix[j] = v;
  1912. s->chroma_inter_matrix[j] = v;
  1913. }
  1914. s->progressive_sequence = 1;
  1915. s->progressive_frame = 1;
  1916. s->picture_structure = PICT_FRAME;
  1917. s->frame_pred_frame_dct = 1;
  1918. s->chroma_format = 1;
  1919. s->codec_id = s->avctx->codec_id = AV_CODEC_ID_MPEG2VIDEO;
  1920. s1->save_width = s->width;
  1921. s1->save_height = s->height;
  1922. s1->save_progressive_seq = s->progressive_sequence;
  1923. return 0;
  1924. }
  1925. static int mpeg_decode_a53_cc(AVCodecContext *avctx,
  1926. const uint8_t *p, int buf_size)
  1927. {
  1928. Mpeg1Context *s1 = avctx->priv_data;
  1929. if (buf_size >= 6 &&
  1930. p[0] == 'G' && p[1] == 'A' && p[2] == '9' && p[3] == '4' &&
  1931. p[4] == 3 && (p[5] & 0x40)) {
  1932. /* extract A53 Part 4 CC data */
  1933. int cc_count = p[5] & 0x1f;
  1934. if (cc_count > 0 && buf_size >= 7 + cc_count * 3) {
  1935. av_freep(&s1->a53_caption);
  1936. s1->a53_caption_size = cc_count * 3;
  1937. s1->a53_caption = av_malloc(s1->a53_caption_size);
  1938. if (s1->a53_caption)
  1939. memcpy(s1->a53_caption, p + 7, s1->a53_caption_size);
  1940. }
  1941. return 1;
  1942. } else if (buf_size >= 11 &&
  1943. p[0] == 'C' && p[1] == 'C' && p[2] == 0x01 && p[3] == 0xf8) {
  1944. /* extract DVD CC data */
  1945. int cc_count = 0;
  1946. int i;
  1947. // There is a caption count field in the data, but it is often
  1948. // incorrect. So count the number of captions present.
  1949. for (i = 5; i + 6 <= buf_size && ((p[i] & 0xfe) == 0xfe); i += 6)
  1950. cc_count++;
  1951. // Transform the DVD format into A53 Part 4 format
  1952. if (cc_count > 0) {
  1953. av_freep(&s1->a53_caption);
  1954. s1->a53_caption_size = cc_count * 6;
  1955. s1->a53_caption = av_malloc(s1->a53_caption_size);
  1956. if (s1->a53_caption) {
  1957. uint8_t field1 = !!(p[4] & 0x80);
  1958. uint8_t *cap = s1->a53_caption;
  1959. p += 5;
  1960. for (i = 0; i < cc_count; i++) {
  1961. cap[0] = (p[0] == 0xff && field1) ? 0xfc : 0xfd;
  1962. cap[1] = p[1];
  1963. cap[2] = p[2];
  1964. cap[3] = (p[3] == 0xff && !field1) ? 0xfc : 0xfd;
  1965. cap[4] = p[4];
  1966. cap[5] = p[5];
  1967. cap += 6;
  1968. p += 6;
  1969. }
  1970. }
  1971. }
  1972. return 1;
  1973. }
  1974. return 0;
  1975. }
  1976. static void mpeg_decode_user_data(AVCodecContext *avctx,
  1977. const uint8_t *p, int buf_size)
  1978. {
  1979. const uint8_t *buf_end = p + buf_size;
  1980. Mpeg1Context *s1 = avctx->priv_data;
  1981. /* we parse the DTG active format information */
  1982. if (buf_end - p >= 5 &&
  1983. p[0] == 'D' && p[1] == 'T' && p[2] == 'G' && p[3] == '1') {
  1984. int flags = p[4];
  1985. p += 5;
  1986. if (flags & 0x80) {
  1987. /* skip event id */
  1988. p += 2;
  1989. }
  1990. if (flags & 0x40) {
  1991. if (buf_end - p < 1)
  1992. return;
  1993. #if FF_API_AFD
  1994. FF_DISABLE_DEPRECATION_WARNINGS
  1995. avctx->dtg_active_format = p[0] & 0x0f;
  1996. FF_ENABLE_DEPRECATION_WARNINGS
  1997. #endif /* FF_API_AFD */
  1998. s1->has_afd = 1;
  1999. s1->afd = p[0] & 0x0f;
  2000. }
  2001. } else if (buf_end - p >= 6 &&
  2002. p[0] == 'J' && p[1] == 'P' && p[2] == '3' && p[3] == 'D' &&
  2003. p[4] == 0x03) { // S3D_video_format_length
  2004. // the 0x7F mask ignores the reserved_bit value
  2005. const uint8_t S3D_video_format_type = p[5] & 0x7F;
  2006. if (S3D_video_format_type == 0x03 ||
  2007. S3D_video_format_type == 0x04 ||
  2008. S3D_video_format_type == 0x08 ||
  2009. S3D_video_format_type == 0x23) {
  2010. s1->has_stereo3d = 1;
  2011. switch (S3D_video_format_type) {
  2012. case 0x03:
  2013. s1->stereo3d.type = AV_STEREO3D_SIDEBYSIDE;
  2014. break;
  2015. case 0x04:
  2016. s1->stereo3d.type = AV_STEREO3D_TOPBOTTOM;
  2017. break;
  2018. case 0x08:
  2019. s1->stereo3d.type = AV_STEREO3D_2D;
  2020. break;
  2021. case 0x23:
  2022. s1->stereo3d.type = AV_STEREO3D_SIDEBYSIDE_QUINCUNX;
  2023. break;
  2024. }
  2025. }
  2026. } else if (mpeg_decode_a53_cc(avctx, p, buf_size)) {
  2027. return;
  2028. }
  2029. }
  2030. static void mpeg_decode_gop(AVCodecContext *avctx,
  2031. const uint8_t *buf, int buf_size)
  2032. {
  2033. Mpeg1Context *s1 = avctx->priv_data;
  2034. MpegEncContext *s = &s1->mpeg_enc_ctx;
  2035. int time_code_hours, time_code_minutes;
  2036. int time_code_seconds, time_code_pictures;
  2037. int broken_link;
  2038. init_get_bits(&s->gb, buf, buf_size * 8);
  2039. skip_bits1(&s->gb); /* drop_frame_flag */
  2040. time_code_hours = get_bits(&s->gb, 5);
  2041. time_code_minutes = get_bits(&s->gb, 6);
  2042. skip_bits1(&s->gb); // marker bit
  2043. time_code_seconds = get_bits(&s->gb, 6);
  2044. time_code_pictures = get_bits(&s->gb, 6);
  2045. s1->closed_gop = get_bits1(&s->gb);
  2046. /* broken_link indicate that after editing the
  2047. * reference frames of the first B-Frames after GOP I-Frame
  2048. * are missing (open gop) */
  2049. broken_link = get_bits1(&s->gb);
  2050. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  2051. av_log(s->avctx, AV_LOG_DEBUG,
  2052. "GOP (%2d:%02d:%02d.[%02d]) closed_gop=%d broken_link=%d\n",
  2053. time_code_hours, time_code_minutes, time_code_seconds,
  2054. time_code_pictures, s1->closed_gop, broken_link);
  2055. }
  2056. static int decode_chunks(AVCodecContext *avctx, AVFrame *picture,
  2057. int *got_output, const uint8_t *buf, int buf_size)
  2058. {
  2059. Mpeg1Context *s = avctx->priv_data;
  2060. MpegEncContext *s2 = &s->mpeg_enc_ctx;
  2061. const uint8_t *buf_ptr = buf;
  2062. const uint8_t *buf_end = buf + buf_size;
  2063. int ret, input_size;
  2064. int last_code = 0, skip_frame = 0;
  2065. for (;;) {
  2066. /* find next start code */
  2067. uint32_t start_code = -1;
  2068. buf_ptr = avpriv_find_start_code(buf_ptr, buf_end, &start_code);
  2069. if (start_code > 0x1ff) {
  2070. if (!skip_frame) {
  2071. if (HAVE_THREADS &&
  2072. (avctx->active_thread_type & FF_THREAD_SLICE) &&
  2073. !avctx->hwaccel) {
  2074. int i;
  2075. avctx->execute(avctx, slice_decode_thread,
  2076. &s2->thread_context[0], NULL,
  2077. s->slice_count, sizeof(void *));
  2078. for (i = 0; i < s->slice_count; i++)
  2079. s2->er.error_count += s2->thread_context[i]->er.error_count;
  2080. }
  2081. ret = slice_end(avctx, picture);
  2082. if (ret < 0)
  2083. return ret;
  2084. else if (ret) {
  2085. // FIXME: merge with the stuff in mpeg_decode_slice
  2086. if (s2->last_picture_ptr || s2->low_delay)
  2087. *got_output = 1;
  2088. }
  2089. }
  2090. s2->pict_type = 0;
  2091. return FFMAX(0, buf_ptr - buf - s2->parse_context.last_index);
  2092. }
  2093. input_size = buf_end - buf_ptr;
  2094. if (avctx->debug & FF_DEBUG_STARTCODE)
  2095. av_log(avctx, AV_LOG_DEBUG, "%3"PRIX32" at %td left %d\n",
  2096. start_code, buf_ptr - buf, input_size);
  2097. /* prepare data for next start code */
  2098. switch (start_code) {
  2099. case SEQ_START_CODE:
  2100. if (last_code == 0) {
  2101. mpeg1_decode_sequence(avctx, buf_ptr, input_size);
  2102. s->sync = 1;
  2103. } else {
  2104. av_log(avctx, AV_LOG_ERROR,
  2105. "ignoring SEQ_START_CODE after %X\n", last_code);
  2106. if (avctx->err_recognition & AV_EF_EXPLODE)
  2107. return AVERROR_INVALIDDATA;
  2108. }
  2109. break;
  2110. case PICTURE_START_CODE:
  2111. if (s2->width <= 0 || s2->height <= 0) {
  2112. av_log(avctx, AV_LOG_ERROR, "Invalid frame dimensions %dx%d.\n",
  2113. s2->width, s2->height);
  2114. return AVERROR_INVALIDDATA;
  2115. }
  2116. if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE) &&
  2117. !avctx->hwaccel && s->slice_count) {
  2118. int i;
  2119. avctx->execute(avctx, slice_decode_thread,
  2120. s2->thread_context, NULL,
  2121. s->slice_count, sizeof(void *));
  2122. for (i = 0; i < s->slice_count; i++)
  2123. s2->er.error_count += s2->thread_context[i]->er.error_count;
  2124. s->slice_count = 0;
  2125. }
  2126. if (last_code == 0 || last_code == SLICE_MIN_START_CODE) {
  2127. ret = mpeg_decode_postinit(avctx);
  2128. if (ret < 0) {
  2129. av_log(avctx, AV_LOG_ERROR,
  2130. "mpeg_decode_postinit() failure\n");
  2131. return ret;
  2132. }
  2133. /* We have a complete image: we try to decompress it. */
  2134. if (mpeg1_decode_picture(avctx, buf_ptr, input_size) < 0)
  2135. s2->pict_type = 0;
  2136. s->first_slice = 1;
  2137. last_code = PICTURE_START_CODE;
  2138. } else {
  2139. av_log(avctx, AV_LOG_ERROR,
  2140. "ignoring pic after %X\n", last_code);
  2141. if (avctx->err_recognition & AV_EF_EXPLODE)
  2142. return AVERROR_INVALIDDATA;
  2143. }
  2144. break;
  2145. case EXT_START_CODE:
  2146. init_get_bits(&s2->gb, buf_ptr, input_size * 8);
  2147. switch (get_bits(&s2->gb, 4)) {
  2148. case 0x1:
  2149. if (last_code == 0) {
  2150. mpeg_decode_sequence_extension(s);
  2151. } else {
  2152. av_log(avctx, AV_LOG_ERROR,
  2153. "ignoring seq ext after %X\n", last_code);
  2154. if (avctx->err_recognition & AV_EF_EXPLODE)
  2155. return AVERROR_INVALIDDATA;
  2156. }
  2157. break;
  2158. case 0x2:
  2159. mpeg_decode_sequence_display_extension(s);
  2160. break;
  2161. case 0x3:
  2162. mpeg_decode_quant_matrix_extension(s2);
  2163. break;
  2164. case 0x7:
  2165. mpeg_decode_picture_display_extension(s);
  2166. break;
  2167. case 0x8:
  2168. if (last_code == PICTURE_START_CODE) {
  2169. mpeg_decode_picture_coding_extension(s);
  2170. } else {
  2171. av_log(avctx, AV_LOG_ERROR,
  2172. "ignoring pic cod ext after %X\n", last_code);
  2173. if (avctx->err_recognition & AV_EF_EXPLODE)
  2174. return AVERROR_INVALIDDATA;
  2175. }
  2176. break;
  2177. }
  2178. break;
  2179. case USER_START_CODE:
  2180. mpeg_decode_user_data(avctx, buf_ptr, input_size);
  2181. break;
  2182. case GOP_START_CODE:
  2183. if (last_code == 0) {
  2184. s2->first_field = 0;
  2185. mpeg_decode_gop(avctx, buf_ptr, input_size);
  2186. s->sync = 1;
  2187. } else {
  2188. av_log(avctx, AV_LOG_ERROR,
  2189. "ignoring GOP_START_CODE after %X\n", last_code);
  2190. if (avctx->err_recognition & AV_EF_EXPLODE)
  2191. return AVERROR_INVALIDDATA;
  2192. }
  2193. break;
  2194. default:
  2195. if (start_code >= SLICE_MIN_START_CODE &&
  2196. start_code <= SLICE_MAX_START_CODE && last_code != 0) {
  2197. const int field_pic = s2->picture_structure != PICT_FRAME;
  2198. int mb_y = (start_code - SLICE_MIN_START_CODE) << field_pic;
  2199. last_code = SLICE_MIN_START_CODE;
  2200. if (s2->picture_structure == PICT_BOTTOM_FIELD)
  2201. mb_y++;
  2202. if (mb_y >= s2->mb_height) {
  2203. av_log(s2->avctx, AV_LOG_ERROR,
  2204. "slice below image (%d >= %d)\n", mb_y, s2->mb_height);
  2205. return AVERROR_INVALIDDATA;
  2206. }
  2207. if (!s2->last_picture_ptr) {
  2208. /* Skip B-frames if we do not have reference frames and
  2209. * GOP is not closed. */
  2210. if (s2->pict_type == AV_PICTURE_TYPE_B) {
  2211. if (!s->closed_gop) {
  2212. skip_frame = 1;
  2213. break;
  2214. }
  2215. }
  2216. }
  2217. if (s2->pict_type == AV_PICTURE_TYPE_I)
  2218. s->sync = 1;
  2219. if (!s2->next_picture_ptr) {
  2220. /* Skip P-frames if we do not have a reference frame or
  2221. * we have an invalid header. */
  2222. if (s2->pict_type == AV_PICTURE_TYPE_P && !s->sync) {
  2223. skip_frame = 1;
  2224. break;
  2225. }
  2226. }
  2227. if ((avctx->skip_frame >= AVDISCARD_NONREF &&
  2228. s2->pict_type == AV_PICTURE_TYPE_B) ||
  2229. (avctx->skip_frame >= AVDISCARD_NONKEY &&
  2230. s2->pict_type != AV_PICTURE_TYPE_I) ||
  2231. avctx->skip_frame >= AVDISCARD_ALL) {
  2232. skip_frame = 1;
  2233. break;
  2234. }
  2235. if (!s->mpeg_enc_ctx_allocated)
  2236. break;
  2237. if (s2->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
  2238. if (mb_y < avctx->skip_top ||
  2239. mb_y >= s2->mb_height - avctx->skip_bottom)
  2240. break;
  2241. }
  2242. if (!s2->pict_type) {
  2243. av_log(avctx, AV_LOG_ERROR, "Missing picture start code\n");
  2244. if (avctx->err_recognition & AV_EF_EXPLODE)
  2245. return AVERROR_INVALIDDATA;
  2246. break;
  2247. }
  2248. if (s->first_slice) {
  2249. skip_frame = 0;
  2250. s->first_slice = 0;
  2251. if ((ret = mpeg_field_start(s2, buf, buf_size)) < 0)
  2252. return ret;
  2253. }
  2254. if (!s2->current_picture_ptr) {
  2255. av_log(avctx, AV_LOG_ERROR,
  2256. "current_picture not initialized\n");
  2257. return AVERROR_INVALIDDATA;
  2258. }
  2259. if (HAVE_THREADS &&
  2260. (avctx->active_thread_type & FF_THREAD_SLICE) &&
  2261. !avctx->hwaccel) {
  2262. int threshold = (s2->mb_height * s->slice_count +
  2263. s2->slice_context_count / 2) /
  2264. s2->slice_context_count;
  2265. if (threshold <= mb_y) {
  2266. MpegEncContext *thread_context = s2->thread_context[s->slice_count];
  2267. thread_context->start_mb_y = mb_y;
  2268. thread_context->end_mb_y = s2->mb_height;
  2269. if (s->slice_count) {
  2270. s2->thread_context[s->slice_count - 1]->end_mb_y = mb_y;
  2271. ret = ff_update_duplicate_context(thread_context, s2);
  2272. if (ret < 0)
  2273. return ret;
  2274. }
  2275. init_get_bits(&thread_context->gb, buf_ptr, input_size * 8);
  2276. s->slice_count++;
  2277. }
  2278. buf_ptr += 2; // FIXME add minimum number of bytes per slice
  2279. } else {
  2280. ret = mpeg_decode_slice(s2, mb_y, &buf_ptr, input_size);
  2281. emms_c();
  2282. if (ret < 0) {
  2283. if (avctx->err_recognition & AV_EF_EXPLODE)
  2284. return ret;
  2285. if (s2->resync_mb_x >= 0 && s2->resync_mb_y >= 0)
  2286. ff_er_add_slice(&s2->er, s2->resync_mb_x,
  2287. s2->resync_mb_y, s2->mb_x, s2->mb_y,
  2288. ER_AC_ERROR | ER_DC_ERROR | ER_MV_ERROR);
  2289. } else {
  2290. ff_er_add_slice(&s2->er, s2->resync_mb_x,
  2291. s2->resync_mb_y, s2->mb_x - 1, s2->mb_y,
  2292. ER_AC_END | ER_DC_END | ER_MV_END);
  2293. }
  2294. }
  2295. }
  2296. break;
  2297. }
  2298. }
  2299. }
  2300. static int mpeg_decode_frame(AVCodecContext *avctx, void *data,
  2301. int *got_output, AVPacket *avpkt)
  2302. {
  2303. const uint8_t *buf = avpkt->data;
  2304. int buf_size = avpkt->size;
  2305. Mpeg1Context *s = avctx->priv_data;
  2306. AVFrame *picture = data;
  2307. MpegEncContext *s2 = &s->mpeg_enc_ctx;
  2308. ff_dlog(avctx, "fill_buffer\n");
  2309. if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == SEQ_END_CODE)) {
  2310. /* special case for last picture */
  2311. if (s2->low_delay == 0 && s2->next_picture_ptr) {
  2312. int ret = av_frame_ref(picture, s2->next_picture_ptr->f);
  2313. if (ret < 0)
  2314. return ret;
  2315. s2->next_picture_ptr = NULL;
  2316. *got_output = 1;
  2317. }
  2318. return buf_size;
  2319. }
  2320. if (s2->avctx->flags & AV_CODEC_FLAG_TRUNCATED) {
  2321. int next = ff_mpeg1_find_frame_end(&s2->parse_context, buf,
  2322. buf_size, NULL);
  2323. if (ff_combine_frame(&s2->parse_context, next,
  2324. (const uint8_t **) &buf, &buf_size) < 0)
  2325. return buf_size;
  2326. }
  2327. if (s->mpeg_enc_ctx_allocated == 0 && avctx->codec_tag == AV_RL32("VCR2"))
  2328. vcr2_init_sequence(avctx);
  2329. s->slice_count = 0;
  2330. if (avctx->extradata && !s->extradata_decoded) {
  2331. int ret = decode_chunks(avctx, picture, got_output,
  2332. avctx->extradata, avctx->extradata_size);
  2333. s->extradata_decoded = 1;
  2334. if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE))
  2335. return ret;
  2336. }
  2337. return decode_chunks(avctx, picture, got_output, buf, buf_size);
  2338. }
  2339. static void flush(AVCodecContext *avctx)
  2340. {
  2341. Mpeg1Context *s = avctx->priv_data;
  2342. s->sync = 0;
  2343. s->closed_gop = 0;
  2344. ff_mpeg_flush(avctx);
  2345. }
  2346. static av_cold int mpeg_decode_end(AVCodecContext *avctx)
  2347. {
  2348. Mpeg1Context *s = avctx->priv_data;
  2349. if (s->mpeg_enc_ctx_allocated)
  2350. ff_mpv_common_end(&s->mpeg_enc_ctx);
  2351. av_freep(&s->a53_caption);
  2352. return 0;
  2353. }
  2354. AVCodec ff_mpeg1video_decoder = {
  2355. .name = "mpeg1video",
  2356. .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video"),
  2357. .type = AVMEDIA_TYPE_VIDEO,
  2358. .id = AV_CODEC_ID_MPEG1VIDEO,
  2359. .priv_data_size = sizeof(Mpeg1Context),
  2360. .init = mpeg_decode_init,
  2361. .close = mpeg_decode_end,
  2362. .decode = mpeg_decode_frame,
  2363. .capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1 |
  2364. AV_CODEC_CAP_TRUNCATED | AV_CODEC_CAP_DELAY |
  2365. AV_CODEC_CAP_SLICE_THREADS,
  2366. .flush = flush,
  2367. .update_thread_context = ONLY_IF_THREADS_ENABLED(mpeg_decode_update_thread_context)
  2368. };
  2369. AVCodec ff_mpeg2video_decoder = {
  2370. .name = "mpeg2video",
  2371. .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 video"),
  2372. .type = AVMEDIA_TYPE_VIDEO,
  2373. .id = AV_CODEC_ID_MPEG2VIDEO,
  2374. .priv_data_size = sizeof(Mpeg1Context),
  2375. .init = mpeg_decode_init,
  2376. .close = mpeg_decode_end,
  2377. .decode = mpeg_decode_frame,
  2378. .capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1 |
  2379. AV_CODEC_CAP_TRUNCATED | AV_CODEC_CAP_DELAY |
  2380. AV_CODEC_CAP_SLICE_THREADS,
  2381. .flush = flush,
  2382. .profiles = NULL_IF_CONFIG_SMALL(ff_mpeg2_video_profiles),
  2383. };
  2384. #if FF_API_XVMC
  2385. #if CONFIG_MPEG_XVMC_DECODER
  2386. static av_cold int mpeg_mc_decode_init(AVCodecContext *avctx)
  2387. {
  2388. if (avctx->active_thread_type & FF_THREAD_SLICE)
  2389. return -1;
  2390. if (!(avctx->slice_flags & SLICE_FLAG_CODED_ORDER))
  2391. return -1;
  2392. if (!(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD)) {
  2393. ff_dlog(avctx, "mpeg12.c: XvMC decoder will work better if SLICE_FLAG_ALLOW_FIELD is set\n");
  2394. }
  2395. mpeg_decode_init(avctx);
  2396. avctx->pix_fmt = AV_PIX_FMT_XVMC_MPEG2_IDCT;
  2397. avctx->xvmc_acceleration = 2; // 2 - the blocks are packed!
  2398. return 0;
  2399. }
  2400. AVCodec ff_mpeg_xvmc_decoder = {
  2401. .name = "mpegvideo_xvmc",
  2402. .long_name = NULL_IF_CONFIG_SMALL("MPEG-1/2 video XvMC (X-Video Motion Compensation)"),
  2403. .type = AVMEDIA_TYPE_VIDEO,
  2404. .id = AV_CODEC_ID_MPEG2VIDEO_XVMC,
  2405. .priv_data_size = sizeof(Mpeg1Context),
  2406. .init = mpeg_mc_decode_init,
  2407. .close = mpeg_decode_end,
  2408. .decode = mpeg_decode_frame,
  2409. .capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1 |
  2410. AV_CODEC_CAP_TRUNCATED | CODEC_CAP_HWACCEL |
  2411. AV_CODEC_CAP_DELAY,
  2412. .flush = flush,
  2413. };
  2414. #endif
  2415. #endif /* FF_API_XVMC */