You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2444 lines
82KB

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