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.

2629 lines
95KB

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