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.

2436 lines
88KB

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