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.

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