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.

2588 lines
93KB

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