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.

2444 lines
82KB

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