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.

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