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.

2482 lines
90KB

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