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.

2481 lines
83KB

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