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.

2450 lines
82KB

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