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.

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