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.

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