summaryrefslogtreecommitdiff
path: root/multimedia
diff options
context:
space:
mode:
authorjoerg <joerg@pkgsrc.org>2006-08-14 12:15:38 +0000
committerjoerg <joerg@pkgsrc.org>2006-08-14 12:15:38 +0000
commitb7245d08cb0604949b1b00bb6e41b56050a5c660 (patch)
tree2bcec59ed24bbc8fd8b16de282ca16d1bf951da5 /multimedia
parentbd5a0361548466893c0b73d676014f11fde92fa1 (diff)
downloadpkgsrc-b7245d08cb0604949b1b00bb6e41b56050a5c660.tar.gz
Disable mutex debugging by default, it results in Xine spinning around
all the time at least on DragonFly. Move some inline functions around so that they exist before they are used, avoiding compilation errors on DragonFly where -fno-unit-at-a-time is disable by default.
Diffstat (limited to 'multimedia')
-rw-r--r--multimedia/xine-lib/Makefile3
-rw-r--r--multimedia/xine-lib/distinfo6
-rw-r--r--multimedia/xine-lib/patches/patch-bh14
-rw-r--r--multimedia/xine-lib/patches/patch-da848
-rw-r--r--multimedia/xine-lib/patches/patch-db1566
-rw-r--r--multimedia/xine-lib/patches/patch-dc382
6 files changed, 2817 insertions, 2 deletions
diff --git a/multimedia/xine-lib/Makefile b/multimedia/xine-lib/Makefile
index caf839f8211..0cd6ebfea99 100644
--- a/multimedia/xine-lib/Makefile
+++ b/multimedia/xine-lib/Makefile
@@ -1,8 +1,9 @@
-# $NetBSD: Makefile,v 1.40 2006/08/06 10:36:50 wiz Exp $
+# $NetBSD: Makefile,v 1.41 2006/08/14 12:15:38 joerg Exp $
.include "Makefile.common"
COMMENT= Multimedia player library
+PKGREVISION= 1
BUILDLINK_API_DEPENDS.vcdimager+= vcdimager>=0.7.20nb1
diff --git a/multimedia/xine-lib/distinfo b/multimedia/xine-lib/distinfo
index 2a2d0fff266..33822c0bb7b 100644
--- a/multimedia/xine-lib/distinfo
+++ b/multimedia/xine-lib/distinfo
@@ -1,4 +1,4 @@
-$NetBSD: distinfo,v 1.35 2006/08/06 10:36:50 wiz Exp $
+$NetBSD: distinfo,v 1.36 2006/08/14 12:15:38 joerg Exp $
SHA1 (xine-lib-1.1.2.tar.bz2) = 07e454e2287e34414b598faf78ae6d8ab8ffbb69
RMD160 (xine-lib-1.1.2.tar.bz2) = 21322d6d7e085237926741495d70f5ce7764910b
@@ -25,5 +25,9 @@ SHA1 (patch-bb) = 23b387a4bd1877081eeddb5a2ecdd2b697eb224f
SHA1 (patch-bd) = 5352293022da25b08598d42655af2524ff99b28a
SHA1 (patch-be) = d076593d15c58762e84f53964fb90c61873e7a45
SHA1 (patch-bg) = aa1a8960d597113825993be59db45644a620e99d
+SHA1 (patch-bh) = 443e5f542c6d3f5162e5c9c34b0d4311f9138a5a
SHA1 (patch-cb) = 08d9920022988d2764d941cfa8b1aa5602a0ec81
SHA1 (patch-cd) = a080c745d08ded46db7c1173fe55350c1eb9ff33
+SHA1 (patch-da) = 7cb3cb60fd47720f081a101f80f9b53da7696c78
+SHA1 (patch-db) = 07214343b63a5e3bad774e8a18b3d8533526b97d
+SHA1 (patch-dc) = 218fbda52fffecb02993695116fcca0fbf65346e
diff --git a/multimedia/xine-lib/patches/patch-bh b/multimedia/xine-lib/patches/patch-bh
new file mode 100644
index 00000000000..b156426c394
--- /dev/null
+++ b/multimedia/xine-lib/patches/patch-bh
@@ -0,0 +1,14 @@
+$NetBSD: patch-bh,v 1.3 2006/08/14 12:15:38 joerg Exp $
+
+--- src/xine-utils/xine_mutex.c.orig 2006-06-23 18:24:22.000000000 +0000
++++ src/xine-utils/xine_mutex.c
+@@ -29,7 +29,9 @@
+ #include <pthread.h>
+ #include "xineutils.h"
+
++#if 0
+ #define DBG_MUTEX
++#endif
+
+ int xine_mutex_init (xine_mutex_t *mutex, const pthread_mutexattr_t *mutexattr,
+ const char *id) {
diff --git a/multimedia/xine-lib/patches/patch-da b/multimedia/xine-lib/patches/patch-da
new file mode 100644
index 00000000000..0cf26afffb2
--- /dev/null
+++ b/multimedia/xine-lib/patches/patch-da
@@ -0,0 +1,848 @@
+$NetBSD: patch-da,v 1.1 2006/08/14 12:15:38 joerg Exp $
+
+Move some inlined functions to a place before they are used,
+otherwise GCC barfs when -fno-unit-a-time is active.
+
+--- src/libffmpeg/libavcodec/h263.c.orig 2006-08-14 11:26:49.000000000 +0000
++++ src/libffmpeg/libavcodec/h263.c
+@@ -3395,6 +3395,53 @@ static inline int get_amv(MpegEncContext
+ }
+
+ /**
++ * decodes the dc value.
++ * @param n block index (0-3 are luma, 4-5 are chroma)
++ * @param dir_ptr the prediction direction will be stored here
++ * @return the quantized dc
++ */
++static inline int mpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr)
++{
++ int level, code;
++
++ if (n < 4)
++ code = get_vlc2(&s->gb, dc_lum.table, DC_VLC_BITS, 1);
++ else
++ code = get_vlc2(&s->gb, dc_chrom.table, DC_VLC_BITS, 1);
++ if (code < 0 || code > 9 /* && s->nbit<9 */){
++ av_log(s->avctx, AV_LOG_ERROR, "illegal dc vlc\n");
++ return -1;
++ }
++ if (code == 0) {
++ level = 0;
++ } else {
++ if(IS_3IV1){
++ if(code==1)
++ level= 2*get_bits1(&s->gb)-1;
++ else{
++ if(get_bits1(&s->gb))
++ level = get_bits(&s->gb, code-1) + (1<<(code-1));
++ else
++ level = -get_bits(&s->gb, code-1) - (1<<(code-1));
++ }
++ }else{
++ level = get_xbits(&s->gb, code);
++ }
++
++ if (code > 8){
++ if(get_bits1(&s->gb)==0){ /* marker */
++ if(s->error_resilience>=2){
++ av_log(s->avctx, AV_LOG_ERROR, "dc marker bit missing\n");
++ return -1;
++ }
++ }
++ }
++ }
++
++ return ff_mpeg4_pred_dc(s, n, level, dir_ptr, 0);
++}
++
++/**
+ * decodes first partition.
+ * @return number of MBs decoded or <0 if an error occured
+ */
+@@ -3700,125 +3747,380 @@ int ff_mpeg4_decode_partitions(MpegEncCo
+ }
+
+ /**
+- * decode partition C of one MB.
++ * decodes a block.
+ * @return <0 if an error occured
+ */
+-static int mpeg4_decode_partitioned_mb(MpegEncContext *s, DCTELEM block[6][64])
++static inline int mpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
++ int n, int coded, int intra, int rvlc)
+ {
+- int cbp, mb_type;
+- const int xy= s->mb_x + s->mb_y*s->mb_stride;
++ int level, i, last, run;
++ int dc_pred_dir;
++ RLTable * rl;
++ RL_VLC_ELEM * rl_vlc;
++ const uint8_t * scan_table;
++ int qmul, qadd;
+
+- mb_type= s->current_picture.mb_type[xy];
+- cbp = s->cbp_table[xy];
++ //Note intra & rvlc should be optimized away if this is inlined
+
+- if(s->current_picture.qscale_table[xy] != s->qscale){
+- ff_set_qscale(s, s->current_picture.qscale_table[xy] );
+- }
++ if(intra) {
++ if(s->qscale < s->intra_dc_threshold){
++ /* DC coef */
++ if(s->partitioned_frame){
++ level = s->dc_val[0][ s->block_index[n] ];
++ if(n<4) level= FASTDIV((level + (s->y_dc_scale>>1)), s->y_dc_scale);
++ else level= FASTDIV((level + (s->c_dc_scale>>1)), s->c_dc_scale);
++ dc_pred_dir= (s->pred_dir_table[s->mb_x + s->mb_y*s->mb_stride]<<n)&32;
++ }else{
++ level = mpeg4_decode_dc(s, n, &dc_pred_dir);
++ if (level < 0)
++ return -1;
++ }
++ block[0] = level;
++ i = 0;
++ }else{
++ i = -1;
++ }
++ if (!coded)
++ goto not_coded;
+
+- if (s->pict_type == P_TYPE || s->pict_type==S_TYPE) {
+- int i;
+- for(i=0; i<4; i++){
+- s->mv[0][i][0] = s->current_picture.motion_val[0][ s->block_index[i] ][0];
+- s->mv[0][i][1] = s->current_picture.motion_val[0][ s->block_index[i] ][1];
++ if(rvlc){
++ rl = &rvlc_rl_intra;
++ rl_vlc = rvlc_rl_intra.rl_vlc[0];
++ }else{
++ rl = &rl_intra;
++ rl_vlc = rl_intra.rl_vlc[0];
+ }
+- s->mb_intra = IS_INTRA(mb_type);
++ if (s->ac_pred) {
++ if (dc_pred_dir == 0)
++ scan_table = s->intra_v_scantable.permutated; /* left */
++ else
++ scan_table = s->intra_h_scantable.permutated; /* top */
++ } else {
++ scan_table = s->intra_scantable.permutated;
++ }
++ qmul=1;
++ qadd=0;
++ } else {
++ i = -1;
++ if (!coded) {
++ s->block_last_index[n] = i;
++ return 0;
++ }
++ if(rvlc) rl = &rvlc_rl_inter;
++ else rl = &rl_inter;
+
+- if (IS_SKIP(mb_type)) {
+- /* skip mb */
+- for(i=0;i<6;i++)
+- s->block_last_index[i] = -1;
+- s->mv_dir = MV_DIR_FORWARD;
+- s->mv_type = MV_TYPE_16X16;
+- if(s->pict_type==S_TYPE && s->vol_sprite_usage==GMC_SPRITE){
+- s->mcsel=1;
+- s->mb_skipped = 0;
+- }else{
+- s->mcsel=0;
+- s->mb_skipped = 1;
+- }
+- }else if(s->mb_intra){
+- s->ac_pred = IS_ACPRED(s->current_picture.mb_type[xy]);
+- }else if(!s->mb_intra){
+-// s->mcsel= 0; //FIXME do we need to init that
++ scan_table = s->intra_scantable.permutated;
+
+- s->mv_dir = MV_DIR_FORWARD;
+- if (IS_8X8(mb_type)) {
+- s->mv_type = MV_TYPE_8X8;
+- } else {
+- s->mv_type = MV_TYPE_16X16;
++ if(s->mpeg_quant){
++ qmul=1;
++ qadd=0;
++ if(rvlc){
++ rl_vlc = rvlc_rl_inter.rl_vlc[0];
++ }else{
++ rl_vlc = rl_inter.rl_vlc[0];
+ }
+- }
+- } else { /* I-Frame */
+- s->mb_intra = 1;
+- s->ac_pred = IS_ACPRED(s->current_picture.mb_type[xy]);
+- }
+-
+- if (!IS_SKIP(mb_type)) {
+- int i;
+- s->dsp.clear_blocks(s->block[0]);
+- /* decode each block */
+- for (i = 0; i < 6; i++) {
+- if(mpeg4_decode_block(s, block[i], i, cbp&32, s->mb_intra, s->rvlc) < 0){
+- av_log(s->avctx, AV_LOG_ERROR, "texture corrupted at %d %d %d\n", s->mb_x, s->mb_y, s->mb_intra);
+- return -1;
++ }else{
++ qmul = s->qscale << 1;
++ qadd = (s->qscale - 1) | 1;
++ if(rvlc){
++ rl_vlc = rvlc_rl_inter.rl_vlc[s->qscale];
++ }else{
++ rl_vlc = rl_inter.rl_vlc[s->qscale];
+ }
+- cbp+=cbp;
+ }
+ }
++ {
++ OPEN_READER(re, &s->gb);
++ for(;;) {
++ UPDATE_CACHE(re, &s->gb);
++ GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 0);
++ if (level==0) {
++ /* escape */
++ if(rvlc){
++ if(SHOW_UBITS(re, &s->gb, 1)==0){
++ av_log(s->avctx, AV_LOG_ERROR, "1. marker bit missing in rvlc esc\n");
++ return -1;
++ }; SKIP_CACHE(re, &s->gb, 1);
+
+- /* per-MB end of slice check */
++ last= SHOW_UBITS(re, &s->gb, 1); SKIP_CACHE(re, &s->gb, 1);
++ run= SHOW_UBITS(re, &s->gb, 6); LAST_SKIP_CACHE(re, &s->gb, 6);
++ SKIP_COUNTER(re, &s->gb, 1+1+6);
++ UPDATE_CACHE(re, &s->gb);
+
+- if(--s->mb_num_left <= 0){
+-//printf("%06X %d\n", show_bits(&s->gb, 24), s->gb.size_in_bits - get_bits_count(&s->gb));
+- if(mpeg4_is_resync(s))
+- return SLICE_END;
+- else
+- return SLICE_NOEND;
+- }else{
+- if(mpeg4_is_resync(s)){
+- const int delta= s->mb_x + 1 == s->mb_width ? 2 : 1;
+- if(s->cbp_table[xy+delta])
+- return SLICE_END;
+- }
+- return SLICE_OK;
+- }
+-}
++ if(SHOW_UBITS(re, &s->gb, 1)==0){
++ av_log(s->avctx, AV_LOG_ERROR, "2. marker bit missing in rvlc esc\n");
++ return -1;
++ }; SKIP_CACHE(re, &s->gb, 1);
+
+-/**
+- * read the next MVs for OBMC. yes this is a ugly hack, feel free to send a patch :)
+- */
+-static void preview_obmc(MpegEncContext *s){
+- GetBitContext gb= s->gb;
++ level= SHOW_UBITS(re, &s->gb, 11); SKIP_CACHE(re, &s->gb, 11);
+
+- int cbpc, i, pred_x, pred_y, mx, my;
+- int16_t *mot_val;
+- const int xy= s->mb_x + 1 + s->mb_y * s->mb_stride;
+- const int stride= s->b8_stride*2;
++ if(SHOW_UBITS(re, &s->gb, 5)!=0x10){
++ av_log(s->avctx, AV_LOG_ERROR, "reverse esc missing\n");
++ return -1;
++ }; SKIP_CACHE(re, &s->gb, 5);
+
+- for(i=0; i<4; i++)
+- s->block_index[i]+= 2;
+- for(i=4; i<6; i++)
+- s->block_index[i]+= 1;
+- s->mb_x++;
++ level= level * qmul + qadd;
++ level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1); LAST_SKIP_CACHE(re, &s->gb, 1);
++ SKIP_COUNTER(re, &s->gb, 1+11+5+1);
+
+- assert(s->pict_type == P_TYPE);
++ i+= run + 1;
++ if(last) i+=192;
++ }else{
++ int cache;
++ cache= GET_CACHE(re, &s->gb);
+
+- do{
+- if (get_bits1(&s->gb)) {
+- /* skip mb */
+- mot_val = s->current_picture.motion_val[0][ s->block_index[0] ];
+- mot_val[0 ]= mot_val[2 ]=
+- mot_val[0+stride]= mot_val[2+stride]= 0;
+- mot_val[1 ]= mot_val[3 ]=
+- mot_val[1+stride]= mot_val[3+stride]= 0;
++ if(IS_3IV1)
++ cache ^= 0xC0000000;
+
+- s->current_picture.mb_type[xy]= MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
+- goto end;
+- }
+- cbpc = get_vlc2(&s->gb, inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
+- }while(cbpc == 20);
++ if (cache&0x80000000) {
++ if (cache&0x40000000) {
++ /* third escape */
++ SKIP_CACHE(re, &s->gb, 2);
++ last= SHOW_UBITS(re, &s->gb, 1); SKIP_CACHE(re, &s->gb, 1);
++ run= SHOW_UBITS(re, &s->gb, 6); LAST_SKIP_CACHE(re, &s->gb, 6);
++ SKIP_COUNTER(re, &s->gb, 2+1+6);
++ UPDATE_CACHE(re, &s->gb);
+
+- if(cbpc & 4){
++ if(IS_3IV1){
++ level= SHOW_SBITS(re, &s->gb, 12); LAST_SKIP_BITS(re, &s->gb, 12);
++ }else{
++ if(SHOW_UBITS(re, &s->gb, 1)==0){
++ av_log(s->avctx, AV_LOG_ERROR, "1. marker bit missing in 3. esc\n");
++ return -1;
++ }; SKIP_CACHE(re, &s->gb, 1);
++
++ level= SHOW_SBITS(re, &s->gb, 12); SKIP_CACHE(re, &s->gb, 12);
++
++ if(SHOW_UBITS(re, &s->gb, 1)==0){
++ av_log(s->avctx, AV_LOG_ERROR, "2. marker bit missing in 3. esc\n");
++ return -1;
++ }; LAST_SKIP_CACHE(re, &s->gb, 1);
++
++ SKIP_COUNTER(re, &s->gb, 1+12+1);
++ }
++
++#if 0
++ if(s->error_resilience >= FF_ER_COMPLIANT){
++ const int abs_level= ABS(level);
++ if(abs_level<=MAX_LEVEL && run<=MAX_RUN){
++ const int run1= run - rl->max_run[last][abs_level] - 1;
++ if(abs_level <= rl->max_level[last][run]){
++ av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, vlc encoding possible\n");
++ return -1;
++ }
++ if(s->error_resilience > FF_ER_COMPLIANT){
++ if(abs_level <= rl->max_level[last][run]*2){
++ av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 1 encoding possible\n");
++ return -1;
++ }
++ if(run1 >= 0 && abs_level <= rl->max_level[last][run1]){
++ av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 2 encoding possible\n");
++ return -1;
++ }
++ }
++ }
++ }
++#endif
++ if (level>0) level= level * qmul + qadd;
++ else level= level * qmul - qadd;
++
++ if((unsigned)(level + 2048) > 4095){
++ if(s->error_resilience > FF_ER_COMPLIANT){
++ if(level > 2560 || level<-2560){
++ av_log(s->avctx, AV_LOG_ERROR, "|level| overflow in 3. esc, qp=%d\n", s->qscale);
++ return -1;
++ }
++ }
++ level= level<0 ? -2048 : 2047;
++ }
++
++ i+= run + 1;
++ if(last) i+=192;
++ } else {
++ /* second escape */
++#if MIN_CACHE_BITS < 20
++ LAST_SKIP_BITS(re, &s->gb, 2);
++ UPDATE_CACHE(re, &s->gb);
++#else
++ SKIP_BITS(re, &s->gb, 2);
++#endif
++ GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
++ i+= run + rl->max_run[run>>7][level/qmul] +1; //FIXME opt indexing
++ level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
++ LAST_SKIP_BITS(re, &s->gb, 1);
++ }
++ } else {
++ /* first escape */
++#if MIN_CACHE_BITS < 19
++ LAST_SKIP_BITS(re, &s->gb, 1);
++ UPDATE_CACHE(re, &s->gb);
++#else
++ SKIP_BITS(re, &s->gb, 1);
++#endif
++ GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
++ i+= run;
++ level = level + rl->max_level[run>>7][(run-1)&63] * qmul;//FIXME opt indexing
++ level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
++ LAST_SKIP_BITS(re, &s->gb, 1);
++ }
++ }
++ } else {
++ i+= run;
++ level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
++ LAST_SKIP_BITS(re, &s->gb, 1);
++ }
++ if (i > 62){
++ i-= 192;
++ if(i&(~63)){
++ av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
++ return -1;
++ }
++
++ block[scan_table[i]] = level;
++ break;
++ }
++
++ block[scan_table[i]] = level;
++ }
++ CLOSE_READER(re, &s->gb);
++ }
++ not_coded:
++ if (intra) {
++ if(s->qscale >= s->intra_dc_threshold){
++ block[0] = ff_mpeg4_pred_dc(s, n, block[0], &dc_pred_dir, 0);
++
++ if(i == -1) i=0;
++ }
++
++ mpeg4_pred_ac(s, block, n, dc_pred_dir);
++ if (s->ac_pred) {
++ i = 63; /* XXX: not optimal */
++ }
++ }
++ s->block_last_index[n] = i;
++ return 0;
++}
++
++/**
++ * decode partition C of one MB.
++ * @return <0 if an error occured
++ */
++static int mpeg4_decode_partitioned_mb(MpegEncContext *s, DCTELEM block[6][64])
++{
++ int cbp, mb_type;
++ const int xy= s->mb_x + s->mb_y*s->mb_stride;
++
++ mb_type= s->current_picture.mb_type[xy];
++ cbp = s->cbp_table[xy];
++
++ if(s->current_picture.qscale_table[xy] != s->qscale){
++ ff_set_qscale(s, s->current_picture.qscale_table[xy] );
++ }
++
++ if (s->pict_type == P_TYPE || s->pict_type==S_TYPE) {
++ int i;
++ for(i=0; i<4; i++){
++ s->mv[0][i][0] = s->current_picture.motion_val[0][ s->block_index[i] ][0];
++ s->mv[0][i][1] = s->current_picture.motion_val[0][ s->block_index[i] ][1];
++ }
++ s->mb_intra = IS_INTRA(mb_type);
++
++ if (IS_SKIP(mb_type)) {
++ /* skip mb */
++ for(i=0;i<6;i++)
++ s->block_last_index[i] = -1;
++ s->mv_dir = MV_DIR_FORWARD;
++ s->mv_type = MV_TYPE_16X16;
++ if(s->pict_type==S_TYPE && s->vol_sprite_usage==GMC_SPRITE){
++ s->mcsel=1;
++ s->mb_skipped = 0;
++ }else{
++ s->mcsel=0;
++ s->mb_skipped = 1;
++ }
++ }else if(s->mb_intra){
++ s->ac_pred = IS_ACPRED(s->current_picture.mb_type[xy]);
++ }else if(!s->mb_intra){
++// s->mcsel= 0; //FIXME do we need to init that
++
++ s->mv_dir = MV_DIR_FORWARD;
++ if (IS_8X8(mb_type)) {
++ s->mv_type = MV_TYPE_8X8;
++ } else {
++ s->mv_type = MV_TYPE_16X16;
++ }
++ }
++ } else { /* I-Frame */
++ s->mb_intra = 1;
++ s->ac_pred = IS_ACPRED(s->current_picture.mb_type[xy]);
++ }
++
++ if (!IS_SKIP(mb_type)) {
++ int i;
++ s->dsp.clear_blocks(s->block[0]);
++ /* decode each block */
++ for (i = 0; i < 6; i++) {
++ if(mpeg4_decode_block(s, block[i], i, cbp&32, s->mb_intra, s->rvlc) < 0){
++ av_log(s->avctx, AV_LOG_ERROR, "texture corrupted at %d %d %d\n", s->mb_x, s->mb_y, s->mb_intra);
++ return -1;
++ }
++ cbp+=cbp;
++ }
++ }
++
++ /* per-MB end of slice check */
++
++ if(--s->mb_num_left <= 0){
++//printf("%06X %d\n", show_bits(&s->gb, 24), s->gb.size_in_bits - get_bits_count(&s->gb));
++ if(mpeg4_is_resync(s))
++ return SLICE_END;
++ else
++ return SLICE_NOEND;
++ }else{
++ if(mpeg4_is_resync(s)){
++ const int delta= s->mb_x + 1 == s->mb_width ? 2 : 1;
++ if(s->cbp_table[xy+delta])
++ return SLICE_END;
++ }
++ return SLICE_OK;
++ }
++}
++
++/**
++ * read the next MVs for OBMC. yes this is a ugly hack, feel free to send a patch :)
++ */
++static void preview_obmc(MpegEncContext *s){
++ GetBitContext gb= s->gb;
++
++ int cbpc, i, pred_x, pred_y, mx, my;
++ int16_t *mot_val;
++ const int xy= s->mb_x + 1 + s->mb_y * s->mb_stride;
++ const int stride= s->b8_stride*2;
++
++ for(i=0; i<4; i++)
++ s->block_index[i]+= 2;
++ for(i=4; i<6; i++)
++ s->block_index[i]+= 1;
++ s->mb_x++;
++
++ assert(s->pict_type == P_TYPE);
++
++ do{
++ if (get_bits1(&s->gb)) {
++ /* skip mb */
++ mot_val = s->current_picture.motion_val[0][ s->block_index[0] ];
++ mot_val[0 ]= mot_val[2 ]=
++ mot_val[0+stride]= mot_val[2+stride]= 0;
++ mot_val[1 ]= mot_val[3 ]=
++ mot_val[1+stride]= mot_val[3+stride]= 0;
++
++ s->current_picture.mb_type[xy]= MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
++ goto end;
++ }
++ cbpc = get_vlc2(&s->gb, inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
++ }while(cbpc == 20);
++
++ if(cbpc & 4){
+ s->current_picture.mb_type[xy]= MB_TYPE_INTRA;
+ }else{
+ get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1);
+@@ -4678,308 +4980,6 @@ not_coded:
+ return 0;
+ }
+
+-/**
+- * decodes the dc value.
+- * @param n block index (0-3 are luma, 4-5 are chroma)
+- * @param dir_ptr the prediction direction will be stored here
+- * @return the quantized dc
+- */
+-static inline int mpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr)
+-{
+- int level, code;
+-
+- if (n < 4)
+- code = get_vlc2(&s->gb, dc_lum.table, DC_VLC_BITS, 1);
+- else
+- code = get_vlc2(&s->gb, dc_chrom.table, DC_VLC_BITS, 1);
+- if (code < 0 || code > 9 /* && s->nbit<9 */){
+- av_log(s->avctx, AV_LOG_ERROR, "illegal dc vlc\n");
+- return -1;
+- }
+- if (code == 0) {
+- level = 0;
+- } else {
+- if(IS_3IV1){
+- if(code==1)
+- level= 2*get_bits1(&s->gb)-1;
+- else{
+- if(get_bits1(&s->gb))
+- level = get_bits(&s->gb, code-1) + (1<<(code-1));
+- else
+- level = -get_bits(&s->gb, code-1) - (1<<(code-1));
+- }
+- }else{
+- level = get_xbits(&s->gb, code);
+- }
+-
+- if (code > 8){
+- if(get_bits1(&s->gb)==0){ /* marker */
+- if(s->error_resilience>=2){
+- av_log(s->avctx, AV_LOG_ERROR, "dc marker bit missing\n");
+- return -1;
+- }
+- }
+- }
+- }
+-
+- return ff_mpeg4_pred_dc(s, n, level, dir_ptr, 0);
+-}
+-
+-/**
+- * decodes a block.
+- * @return <0 if an error occured
+- */
+-static inline int mpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
+- int n, int coded, int intra, int rvlc)
+-{
+- int level, i, last, run;
+- int dc_pred_dir;
+- RLTable * rl;
+- RL_VLC_ELEM * rl_vlc;
+- const uint8_t * scan_table;
+- int qmul, qadd;
+-
+- //Note intra & rvlc should be optimized away if this is inlined
+-
+- if(intra) {
+- if(s->qscale < s->intra_dc_threshold){
+- /* DC coef */
+- if(s->partitioned_frame){
+- level = s->dc_val[0][ s->block_index[n] ];
+- if(n<4) level= FASTDIV((level + (s->y_dc_scale>>1)), s->y_dc_scale);
+- else level= FASTDIV((level + (s->c_dc_scale>>1)), s->c_dc_scale);
+- dc_pred_dir= (s->pred_dir_table[s->mb_x + s->mb_y*s->mb_stride]<<n)&32;
+- }else{
+- level = mpeg4_decode_dc(s, n, &dc_pred_dir);
+- if (level < 0)
+- return -1;
+- }
+- block[0] = level;
+- i = 0;
+- }else{
+- i = -1;
+- }
+- if (!coded)
+- goto not_coded;
+-
+- if(rvlc){
+- rl = &rvlc_rl_intra;
+- rl_vlc = rvlc_rl_intra.rl_vlc[0];
+- }else{
+- rl = &rl_intra;
+- rl_vlc = rl_intra.rl_vlc[0];
+- }
+- if (s->ac_pred) {
+- if (dc_pred_dir == 0)
+- scan_table = s->intra_v_scantable.permutated; /* left */
+- else
+- scan_table = s->intra_h_scantable.permutated; /* top */
+- } else {
+- scan_table = s->intra_scantable.permutated;
+- }
+- qmul=1;
+- qadd=0;
+- } else {
+- i = -1;
+- if (!coded) {
+- s->block_last_index[n] = i;
+- return 0;
+- }
+- if(rvlc) rl = &rvlc_rl_inter;
+- else rl = &rl_inter;
+-
+- scan_table = s->intra_scantable.permutated;
+-
+- if(s->mpeg_quant){
+- qmul=1;
+- qadd=0;
+- if(rvlc){
+- rl_vlc = rvlc_rl_inter.rl_vlc[0];
+- }else{
+- rl_vlc = rl_inter.rl_vlc[0];
+- }
+- }else{
+- qmul = s->qscale << 1;
+- qadd = (s->qscale - 1) | 1;
+- if(rvlc){
+- rl_vlc = rvlc_rl_inter.rl_vlc[s->qscale];
+- }else{
+- rl_vlc = rl_inter.rl_vlc[s->qscale];
+- }
+- }
+- }
+- {
+- OPEN_READER(re, &s->gb);
+- for(;;) {
+- UPDATE_CACHE(re, &s->gb);
+- GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 0);
+- if (level==0) {
+- /* escape */
+- if(rvlc){
+- if(SHOW_UBITS(re, &s->gb, 1)==0){
+- av_log(s->avctx, AV_LOG_ERROR, "1. marker bit missing in rvlc esc\n");
+- return -1;
+- }; SKIP_CACHE(re, &s->gb, 1);
+-
+- last= SHOW_UBITS(re, &s->gb, 1); SKIP_CACHE(re, &s->gb, 1);
+- run= SHOW_UBITS(re, &s->gb, 6); LAST_SKIP_CACHE(re, &s->gb, 6);
+- SKIP_COUNTER(re, &s->gb, 1+1+6);
+- UPDATE_CACHE(re, &s->gb);
+-
+- if(SHOW_UBITS(re, &s->gb, 1)==0){
+- av_log(s->avctx, AV_LOG_ERROR, "2. marker bit missing in rvlc esc\n");
+- return -1;
+- }; SKIP_CACHE(re, &s->gb, 1);
+-
+- level= SHOW_UBITS(re, &s->gb, 11); SKIP_CACHE(re, &s->gb, 11);
+-
+- if(SHOW_UBITS(re, &s->gb, 5)!=0x10){
+- av_log(s->avctx, AV_LOG_ERROR, "reverse esc missing\n");
+- return -1;
+- }; SKIP_CACHE(re, &s->gb, 5);
+-
+- level= level * qmul + qadd;
+- level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1); LAST_SKIP_CACHE(re, &s->gb, 1);
+- SKIP_COUNTER(re, &s->gb, 1+11+5+1);
+-
+- i+= run + 1;
+- if(last) i+=192;
+- }else{
+- int cache;
+- cache= GET_CACHE(re, &s->gb);
+-
+- if(IS_3IV1)
+- cache ^= 0xC0000000;
+-
+- if (cache&0x80000000) {
+- if (cache&0x40000000) {
+- /* third escape */
+- SKIP_CACHE(re, &s->gb, 2);
+- last= SHOW_UBITS(re, &s->gb, 1); SKIP_CACHE(re, &s->gb, 1);
+- run= SHOW_UBITS(re, &s->gb, 6); LAST_SKIP_CACHE(re, &s->gb, 6);
+- SKIP_COUNTER(re, &s->gb, 2+1+6);
+- UPDATE_CACHE(re, &s->gb);
+-
+- if(IS_3IV1){
+- level= SHOW_SBITS(re, &s->gb, 12); LAST_SKIP_BITS(re, &s->gb, 12);
+- }else{
+- if(SHOW_UBITS(re, &s->gb, 1)==0){
+- av_log(s->avctx, AV_LOG_ERROR, "1. marker bit missing in 3. esc\n");
+- return -1;
+- }; SKIP_CACHE(re, &s->gb, 1);
+-
+- level= SHOW_SBITS(re, &s->gb, 12); SKIP_CACHE(re, &s->gb, 12);
+-
+- if(SHOW_UBITS(re, &s->gb, 1)==0){
+- av_log(s->avctx, AV_LOG_ERROR, "2. marker bit missing in 3. esc\n");
+- return -1;
+- }; LAST_SKIP_CACHE(re, &s->gb, 1);
+-
+- SKIP_COUNTER(re, &s->gb, 1+12+1);
+- }
+-
+-#if 0
+- if(s->error_resilience >= FF_ER_COMPLIANT){
+- const int abs_level= ABS(level);
+- if(abs_level<=MAX_LEVEL && run<=MAX_RUN){
+- const int run1= run - rl->max_run[last][abs_level] - 1;
+- if(abs_level <= rl->max_level[last][run]){
+- av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, vlc encoding possible\n");
+- return -1;
+- }
+- if(s->error_resilience > FF_ER_COMPLIANT){
+- if(abs_level <= rl->max_level[last][run]*2){
+- av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 1 encoding possible\n");
+- return -1;
+- }
+- if(run1 >= 0 && abs_level <= rl->max_level[last][run1]){
+- av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 2 encoding possible\n");
+- return -1;
+- }
+- }
+- }
+- }
+-#endif
+- if (level>0) level= level * qmul + qadd;
+- else level= level * qmul - qadd;
+-
+- if((unsigned)(level + 2048) > 4095){
+- if(s->error_resilience > FF_ER_COMPLIANT){
+- if(level > 2560 || level<-2560){
+- av_log(s->avctx, AV_LOG_ERROR, "|level| overflow in 3. esc, qp=%d\n", s->qscale);
+- return -1;
+- }
+- }
+- level= level<0 ? -2048 : 2047;
+- }
+-
+- i+= run + 1;
+- if(last) i+=192;
+- } else {
+- /* second escape */
+-#if MIN_CACHE_BITS < 20
+- LAST_SKIP_BITS(re, &s->gb, 2);
+- UPDATE_CACHE(re, &s->gb);
+-#else
+- SKIP_BITS(re, &s->gb, 2);
+-#endif
+- GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
+- i+= run + rl->max_run[run>>7][level/qmul] +1; //FIXME opt indexing
+- level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
+- LAST_SKIP_BITS(re, &s->gb, 1);
+- }
+- } else {
+- /* first escape */
+-#if MIN_CACHE_BITS < 19
+- LAST_SKIP_BITS(re, &s->gb, 1);
+- UPDATE_CACHE(re, &s->gb);
+-#else
+- SKIP_BITS(re, &s->gb, 1);
+-#endif
+- GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
+- i+= run;
+- level = level + rl->max_level[run>>7][(run-1)&63] * qmul;//FIXME opt indexing
+- level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
+- LAST_SKIP_BITS(re, &s->gb, 1);
+- }
+- }
+- } else {
+- i+= run;
+- level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
+- LAST_SKIP_BITS(re, &s->gb, 1);
+- }
+- if (i > 62){
+- i-= 192;
+- if(i&(~63)){
+- av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
+- return -1;
+- }
+-
+- block[scan_table[i]] = level;
+- break;
+- }
+-
+- block[scan_table[i]] = level;
+- }
+- CLOSE_READER(re, &s->gb);
+- }
+- not_coded:
+- if (intra) {
+- if(s->qscale >= s->intra_dc_threshold){
+- block[0] = ff_mpeg4_pred_dc(s, n, block[0], &dc_pred_dir, 0);
+-
+- if(i == -1) i=0;
+- }
+-
+- mpeg4_pred_ac(s, block, n, dc_pred_dir);
+- if (s->ac_pred) {
+- i = 63; /* XXX: not optimal */
+- }
+- }
+- s->block_last_index[n] = i;
+- return 0;
+-}
+-
+ /* most is hardcoded. should extend to handle all h263 streams */
+ int h263_decode_picture_header(MpegEncContext *s)
+ {
diff --git a/multimedia/xine-lib/patches/patch-db b/multimedia/xine-lib/patches/patch-db
new file mode 100644
index 00000000000..7af0c8988fc
--- /dev/null
+++ b/multimedia/xine-lib/patches/patch-db
@@ -0,0 +1,1566 @@
+$NetBSD: patch-db,v 1.1 2006/08/14 12:15:38 joerg Exp $
+
+Move some inlined functions to a place before they are used,
+otherwise GCC barfs when -fno-unit-a-time is active.
+
+--- src/libffmpeg/libavcodec/mpeg12.c.orig 2006-08-14 11:37:33.000000000 +0000
++++ src/libffmpeg/libavcodec/mpeg12.c
+@@ -1054,515 +1054,370 @@ static inline int get_qscale(MpegEncCont
+ #define MT_16X8 2
+ #define MT_DMV 3
+
+-static int mpeg_decode_mb(MpegEncContext *s,
+- DCTELEM block[12][64])
++static inline int decode_dc(GetBitContext *gb, int component)
+ {
+- int i, j, k, cbp, val, mb_type, motion_type;
+- const int mb_block_count = 4 + (1<< s->chroma_format);
++ int code, diff;
+
+- dprintf("decode_mb: x=%d y=%d\n", s->mb_x, s->mb_y);
++ if (component == 0) {
++ code = get_vlc2(gb, dc_lum_vlc.table, DC_VLC_BITS, 2);
++ } else {
++ code = get_vlc2(gb, dc_chroma_vlc.table, DC_VLC_BITS, 2);
++ }
++ if (code < 0){
++ av_log(NULL, AV_LOG_ERROR, "invalid dc code at\n");
++ return 0xffff;
++ }
++ if (code == 0) {
++ diff = 0;
++ } else {
++ diff = get_xbits(gb, code);
++ }
++ return diff;
++}
+
+- assert(s->mb_skipped==0);
++static inline int mpeg2_fast_decode_block_intra(MpegEncContext *s,
++ DCTELEM *block,
++ int n)
++{
++ int level, dc, diff, j, run;
++ int component;
++ RLTable *rl;
++ uint8_t * scantable= s->intra_scantable.permutated;
++ const uint16_t *quant_matrix;
++ const int qscale= s->qscale;
+
+- if (s->mb_skip_run-- != 0) {
+- if(s->pict_type == I_TYPE){
+- av_log(s->avctx, AV_LOG_ERROR, "skipped MB in I frame at %d %d\n", s->mb_x, s->mb_y);
+- return -1;
+- }
++ /* DC coef */
++ if (n < 4){
++ quant_matrix = s->intra_matrix;
++ component = 0;
++ }else{
++ quant_matrix = s->chroma_intra_matrix;
++ component = (n&1) + 1;
++ }
++ diff = decode_dc(&s->gb, component);
++ if (diff >= 0xffff)
++ return -1;
++ dc = s->last_dc[component];
++ dc += diff;
++ s->last_dc[component] = dc;
++ block[0] = dc << (3 - s->intra_dc_precision);
++ if (s->intra_vlc_format)
++ rl = &rl_mpeg2;
++ else
++ rl = &rl_mpeg1;
+
+- /* skip mb */
+- s->mb_intra = 0;
+- for(i=0;i<12;i++)
+- s->block_last_index[i] = -1;
+- if(s->picture_structure == PICT_FRAME)
+- s->mv_type = MV_TYPE_16X16;
+- else
+- s->mv_type = MV_TYPE_FIELD;
+- if (s->pict_type == P_TYPE) {
+- /* if P type, zero motion vector is implied */
+- s->mv_dir = MV_DIR_FORWARD;
+- s->mv[0][0][0] = s->mv[0][0][1] = 0;
+- s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
+- s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
+- s->field_select[0][0]= s->picture_structure - 1;
+- s->mb_skipped = 1;
+- s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]= MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;
+- } else {
+- int mb_type;
++ {
++ OPEN_READER(re, &s->gb);
++ /* now quantify & encode AC coefs */
++ for(;;) {
++ UPDATE_CACHE(re, &s->gb);
++ GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
+
+- if(s->mb_x)
+- mb_type= s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride - 1];
+- else
+- 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,
+- if(IS_INTRA(mb_type))
+- return -1;
++ if(level == 127){
++ break;
++ } else if(level != 0) {
++ scantable += run;
++ j = *scantable;
++ level= (level*qscale*quant_matrix[j])>>4;
++ level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
++ LAST_SKIP_BITS(re, &s->gb, 1);
++ } else {
++ /* escape */
++ run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
++ UPDATE_CACHE(re, &s->gb);
++ level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
++ scantable += run;
++ j = *scantable;
++ if(level<0){
++ level= (-level*qscale*quant_matrix[j])>>4;
++ level= -level;
++ }else{
++ level= (level*qscale*quant_matrix[j])>>4;
++ }
++ }
+
+- /* if B type, reuse previous vectors and directions */
+- s->mv[0][0][0] = s->last_mv[0][0][0];
+- s->mv[0][0][1] = s->last_mv[0][0][1];
+- s->mv[1][0][0] = s->last_mv[1][0][0];
+- s->mv[1][0][1] = s->last_mv[1][0][1];
++ block[j] = level;
++ }
++ CLOSE_READER(re, &s->gb);
++ }
+
+- s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]=
+- mb_type | MB_TYPE_SKIP;
+-// assert(s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride - 1]&(MB_TYPE_16x16|MB_TYPE_16x8));
++ s->block_last_index[n] = scantable - s->intra_scantable.permutated;
++ return 0;
++}
+
+- if((s->mv[0][0][0]|s->mv[0][0][1]|s->mv[1][0][0]|s->mv[1][0][1])==0)
+- s->mb_skipped = 1;
+- }
++static inline int mpeg2_decode_block_intra(MpegEncContext *s,
++ DCTELEM *block,
++ int n)
++{
++ int level, dc, diff, i, j, run;
++ int component;
++ RLTable *rl;
++ uint8_t * const scantable= s->intra_scantable.permutated;
++ const uint16_t *quant_matrix;
++ const int qscale= s->qscale;
++ int mismatch;
+
+- return 0;
++ /* DC coef */
++ if (n < 4){
++ quant_matrix = s->intra_matrix;
++ component = 0;
++ }else{
++ quant_matrix = s->chroma_intra_matrix;
++ component = (n&1) + 1;
+ }
++ diff = decode_dc(&s->gb, component);
++ if (diff >= 0xffff)
++ return -1;
++ dc = s->last_dc[component];
++ dc += diff;
++ s->last_dc[component] = dc;
++ block[0] = dc << (3 - s->intra_dc_precision);
++ dprintf("dc=%d\n", block[0]);
++ mismatch = block[0] ^ 1;
++ i = 0;
++ if (s->intra_vlc_format)
++ rl = &rl_mpeg2;
++ else
++ rl = &rl_mpeg1;
+
+- switch(s->pict_type) {
+- default:
+- case I_TYPE:
+- if (get_bits1(&s->gb) == 0) {
+- if (get_bits1(&s->gb) == 0){
+- av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in I Frame at %d %d\n", s->mb_x, s->mb_y);
++ {
++ OPEN_READER(re, &s->gb);
++ /* now quantify & encode AC coefs */
++ for(;;) {
++ UPDATE_CACHE(re, &s->gb);
++ GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
++
++ if(level == 127){
++ break;
++ } else if(level != 0) {
++ i += run;
++ j = scantable[i];
++ level= (level*qscale*quant_matrix[j])>>4;
++ level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
++ LAST_SKIP_BITS(re, &s->gb, 1);
++ } else {
++ /* escape */
++ run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
++ UPDATE_CACHE(re, &s->gb);
++ level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
++ i += run;
++ j = scantable[i];
++ if(level<0){
++ level= (-level*qscale*quant_matrix[j])>>4;
++ level= -level;
++ }else{
++ level= (level*qscale*quant_matrix[j])>>4;
++ }
++ }
++ if (i > 63){
++ av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
+ return -1;
+ }
+- mb_type = MB_TYPE_QUANT | MB_TYPE_INTRA;
+- } else {
+- mb_type = MB_TYPE_INTRA;
+- }
+- break;
+- case P_TYPE:
+- mb_type = get_vlc2(&s->gb, mb_ptype_vlc.table, MB_PTYPE_VLC_BITS, 1);
+- if (mb_type < 0){
+- av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in P Frame at %d %d\n", s->mb_x, s->mb_y);
+- return -1;
+- }
+- mb_type = ptype2mb_type[ mb_type ];
+- break;
+- case B_TYPE:
+- mb_type = get_vlc2(&s->gb, mb_btype_vlc.table, MB_BTYPE_VLC_BITS, 1);
+- if (mb_type < 0){
+- av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in B Frame at %d %d\n", s->mb_x, s->mb_y);
+- return -1;
+- }
+- mb_type = btype2mb_type[ mb_type ];
+- break;
+- }
+- dprintf("mb_type=%x\n", mb_type);
+-// motion_type = 0; /* avoid warning */
+- if (IS_INTRA(mb_type)) {
+- s->dsp.clear_blocks(s->block[0]);
+
+- if(!s->chroma_y_shift){
+- s->dsp.clear_blocks(s->block[6]);
++ mismatch^= level;
++ block[j] = level;
+ }
++ CLOSE_READER(re, &s->gb);
++ }
++ block[63]^= mismatch&1;
+
+- /* compute dct type */
+- if (s->picture_structure == PICT_FRAME && //FIXME add a interlaced_dct coded var?
+- !s->frame_pred_frame_dct) {
+- s->interlaced_dct = get_bits1(&s->gb);
+- }
++ s->block_last_index[n] = i;
++ return 0;
++}
+
+- if (IS_QUANT(mb_type))
+- s->qscale = get_qscale(s);
++static inline int mpeg1_decode_block_intra(MpegEncContext *s,
++ DCTELEM *block,
++ int n)
++{
++ int level, dc, diff, i, j, run;
++ int component;
++ RLTable *rl = &rl_mpeg1;
++ uint8_t * const scantable= s->intra_scantable.permutated;
++ const uint16_t *quant_matrix= s->intra_matrix;
++ const int qscale= s->qscale;
+
+- if (s->concealment_motion_vectors) {
+- /* just parse them */
+- if (s->picture_structure != PICT_FRAME)
+- skip_bits1(&s->gb); /* field select */
+-
+- s->mv[0][0][0]= s->last_mv[0][0][0]= s->last_mv[0][1][0] =
+- mpeg_decode_motion(s, s->mpeg_f_code[0][0], s->last_mv[0][0][0]);
+- s->mv[0][0][1]= s->last_mv[0][0][1]= s->last_mv[0][1][1] =
+- mpeg_decode_motion(s, s->mpeg_f_code[0][1], s->last_mv[0][0][1]);
+-
+- skip_bits1(&s->gb); /* marker */
+- }else
+- memset(s->last_mv, 0, sizeof(s->last_mv)); /* reset mv prediction */
+- s->mb_intra = 1;
+-#ifdef HAVE_XVMC
+- //one 1 we memcpy blocks in xvmcvideo
+- if(s->avctx->xvmc_acceleration > 1){
+- XVMC_pack_pblocks(s,-1);//inter are always full blocks
+- if(s->swap_uv){
+- exchange_uv(s);
+- }
+- }
+-#endif
++ /* DC coef */
++ component = (n <= 3 ? 0 : n - 4 + 1);
++ diff = decode_dc(&s->gb, component);
++ if (diff >= 0xffff)
++ return -1;
++ dc = s->last_dc[component];
++ dc += diff;
++ s->last_dc[component] = dc;
++ block[0] = dc<<3;
++ dprintf("dc=%d diff=%d\n", dc, diff);
++ i = 0;
++ {
++ OPEN_READER(re, &s->gb);
++ /* now quantify & encode AC coefs */
++ for(;;) {
++ UPDATE_CACHE(re, &s->gb);
++ GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
+
+- if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
+- if(s->flags2 & CODEC_FLAG2_FAST){
+- for(i=0;i<6;i++) {
+- mpeg2_fast_decode_block_intra(s, s->pblocks[i], i);
+- }
+- }else{
+- for(i=0;i<mb_block_count;i++) {
+- if (mpeg2_decode_block_intra(s, s->pblocks[i], i) < 0)
+- return -1;
++ if(level == 127){
++ break;
++ } else if(level != 0) {
++ i += run;
++ j = scantable[i];
++ level= (level*qscale*quant_matrix[j])>>4;
++ level= (level-1)|1;
++ level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
++ LAST_SKIP_BITS(re, &s->gb, 1);
++ } else {
++ /* escape */
++ run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
++ UPDATE_CACHE(re, &s->gb);
++ level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
++ if (level == -128) {
++ level = SHOW_UBITS(re, &s->gb, 8) - 256; LAST_SKIP_BITS(re, &s->gb, 8);
++ } else if (level == 0) {
++ level = SHOW_UBITS(re, &s->gb, 8) ; LAST_SKIP_BITS(re, &s->gb, 8);
+ }
+- }
+- } else {
+- for(i=0;i<6;i++) {
+- if (mpeg1_decode_block_intra(s, s->pblocks[i], i) < 0)
+- return -1;
+- }
+- }
+- } else {
+- if (mb_type & MB_TYPE_ZERO_MV){
+- assert(mb_type & MB_TYPE_CBP);
+-
+- /* compute dct type */
+- if (s->picture_structure == PICT_FRAME && //FIXME add a interlaced_dct coded var?
+- !s->frame_pred_frame_dct) {
+- s->interlaced_dct = get_bits1(&s->gb);
+- }
+-
+- if (IS_QUANT(mb_type))
+- s->qscale = get_qscale(s);
+-
+- s->mv_dir = MV_DIR_FORWARD;
+- if(s->picture_structure == PICT_FRAME)
+- s->mv_type = MV_TYPE_16X16;
+- else{
+- s->mv_type = MV_TYPE_FIELD;
+- mb_type |= MB_TYPE_INTERLACED;
+- s->field_select[0][0]= s->picture_structure - 1;
+- }
+- s->last_mv[0][0][0] = 0;
+- s->last_mv[0][0][1] = 0;
+- s->last_mv[0][1][0] = 0;
+- s->last_mv[0][1][1] = 0;
+- s->mv[0][0][0] = 0;
+- s->mv[0][0][1] = 0;
+- }else{
+- assert(mb_type & MB_TYPE_L0L1);
+-//FIXME decide if MBs in field pictures are MB_TYPE_INTERLACED
+- /* get additionnal motion vector type */
+- if (s->frame_pred_frame_dct)
+- motion_type = MT_FRAME;
+- else{
+- motion_type = get_bits(&s->gb, 2);
+- }
+-
+- /* compute dct type */
+- if (s->picture_structure == PICT_FRAME && //FIXME add a interlaced_dct coded var?
+- !s->frame_pred_frame_dct && HAS_CBP(mb_type)) {
+- s->interlaced_dct = get_bits1(&s->gb);
+- }
+-
+- if (IS_QUANT(mb_type))
+- s->qscale = get_qscale(s);
+-
+- /* motion vectors */
+- s->mv_dir = 0;
+- for(i=0;i<2;i++) {
+- if (USES_LIST(mb_type, i)) {
+- s->mv_dir |= (MV_DIR_FORWARD >> i);
+- dprintf("motion_type=%d\n", motion_type);
+- switch(motion_type) {
+- case MT_FRAME: /* or MT_16X8 */
+- if (s->picture_structure == PICT_FRAME) {
+- /* MT_FRAME */
+- mb_type |= MB_TYPE_16x16;
+- s->mv_type = MV_TYPE_16X16;
+- s->mv[i][0][0]= s->last_mv[i][0][0]= s->last_mv[i][1][0] =
+- mpeg_decode_motion(s, s->mpeg_f_code[i][0], s->last_mv[i][0][0]);
+- s->mv[i][0][1]= s->last_mv[i][0][1]= s->last_mv[i][1][1] =
+- mpeg_decode_motion(s, s->mpeg_f_code[i][1], s->last_mv[i][0][1]);
+- /* full_pel: only for mpeg1 */
+- if (s->full_pel[i]){
+- s->mv[i][0][0] <<= 1;
+- s->mv[i][0][1] <<= 1;
+- }
+- } else {
+- /* MT_16X8 */
+- mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
+- s->mv_type = MV_TYPE_16X8;
+- for(j=0;j<2;j++) {
+- s->field_select[i][j] = get_bits1(&s->gb);
+- for(k=0;k<2;k++) {
+- val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
+- s->last_mv[i][j][k]);
+- s->last_mv[i][j][k] = val;
+- s->mv[i][j][k] = val;
+- }
+- }
+- }
+- break;
+- case MT_FIELD:
+- s->mv_type = MV_TYPE_FIELD;
+- if (s->picture_structure == PICT_FRAME) {
+- mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
+- for(j=0;j<2;j++) {
+- s->field_select[i][j] = get_bits1(&s->gb);
+- val = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
+- s->last_mv[i][j][0]);
+- s->last_mv[i][j][0] = val;
+- s->mv[i][j][0] = val;
+- dprintf("fmx=%d\n", val);
+- val = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
+- s->last_mv[i][j][1] >> 1);
+- s->last_mv[i][j][1] = val << 1;
+- s->mv[i][j][1] = val;
+- dprintf("fmy=%d\n", val);
+- }
+- } else {
+- mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
+- s->field_select[i][0] = get_bits1(&s->gb);
+- for(k=0;k<2;k++) {
+- val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
+- s->last_mv[i][0][k]);
+- s->last_mv[i][0][k] = val;
+- s->last_mv[i][1][k] = val;
+- s->mv[i][0][k] = val;
+- }
+- }
+- break;
+- case MT_DMV:
+- {
+- int dmx, dmy, mx, my, m;
+-
+- mx = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
+- s->last_mv[i][0][0]);
+- s->last_mv[i][0][0] = mx;
+- s->last_mv[i][1][0] = mx;
+- dmx = get_dmv(s);
+- my = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
+- s->last_mv[i][0][1] >> 1);
+- dmy = get_dmv(s);
+- s->mv_type = MV_TYPE_DMV;
+-
+-
+- s->last_mv[i][0][1] = my<<1;
+- s->last_mv[i][1][1] = my<<1;
+-
+- s->mv[i][0][0] = mx;
+- s->mv[i][0][1] = my;
+- s->mv[i][1][0] = mx;//not used
+- s->mv[i][1][1] = my;//not used
+-
+- if (s->picture_structure == PICT_FRAME) {
+- mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
+-
+- //m = 1 + 2 * s->top_field_first;
+- m = s->top_field_first ? 1 : 3;
+-
+- /* top -> top pred */
+- s->mv[i][2][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
+- s->mv[i][2][1] = ((my * m + (my > 0)) >> 1) + dmy - 1;
+- m = 4 - m;
+- s->mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
+- s->mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1;
+- } else {
+- mb_type |= MB_TYPE_16x16;
+-
+- s->mv[i][2][0] = ((mx + (mx > 0)) >> 1) + dmx;
+- s->mv[i][2][1] = ((my + (my > 0)) >> 1) + dmy;
+- if(s->picture_structure == PICT_TOP_FIELD)
+- s->mv[i][2][1]--;
+- else
+- s->mv[i][2][1]++;
+- }
+- }
+- break;
+- default:
+- av_log(s->avctx, AV_LOG_ERROR, "00 motion_type at %d %d\n", s->mb_x, s->mb_y);
+- return -1;
+- }
++ i += run;
++ j = scantable[i];
++ if(level<0){
++ level= -level;
++ level= (level*qscale*quant_matrix[j])>>4;
++ level= (level-1)|1;
++ level= -level;
++ }else{
++ level= (level*qscale*quant_matrix[j])>>4;
++ level= (level-1)|1;
+ }
+ }
+- }
+-
+- s->mb_intra = 0;
+- if (HAS_CBP(mb_type)) {
+- s->dsp.clear_blocks(s->block[0]);
+-
+- if(!s->chroma_y_shift){
+- s->dsp.clear_blocks(s->block[6]);
+- }
+-
+- cbp = get_vlc2(&s->gb, mb_pat_vlc.table, MB_PAT_VLC_BITS, 1);
+- if (cbp < 0 || ((cbp == 0) && (s->chroma_format < 2)) ){
+- av_log(s->avctx, AV_LOG_ERROR, "invalid cbp at %d %d\n", s->mb_x, s->mb_y);
++ if (i > 63){
++ av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
+ return -1;
+ }
+- if(mb_block_count > 6){
+- cbp<<= mb_block_count-6;
+- cbp |= get_bits(&s->gb, mb_block_count-6);
+- }
+-
+-#ifdef HAVE_XVMC
+- //on 1 we memcpy blocks in xvmcvideo
+- if(s->avctx->xvmc_acceleration > 1){
+- XVMC_pack_pblocks(s,cbp);
+- if(s->swap_uv){
+- exchange_uv(s);
+- }
+- }
+-#endif
+-
+- if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
+- if(s->flags2 & CODEC_FLAG2_FAST){
+- for(i=0;i<6;i++) {
+- if(cbp & 32) {
+- mpeg2_fast_decode_block_non_intra(s, s->pblocks[i], i);
+- } else {
+- s->block_last_index[i] = -1;
+- }
+- cbp+=cbp;
+- }
+- }else{
+- cbp<<= 12-mb_block_count;
+
+- for(i=0;i<mb_block_count;i++) {
+- if ( cbp & (1<<11) ) {
+- if (mpeg2_decode_block_non_intra(s, s->pblocks[i], i) < 0)
+- return -1;
+- } else {
+- s->block_last_index[i] = -1;
+- }
+- cbp+=cbp;
+- }
+- }
+- } else {
+- if(s->flags2 & CODEC_FLAG2_FAST){
+- for(i=0;i<6;i++) {
+- if (cbp & 32) {
+- mpeg1_fast_decode_block_inter(s, s->pblocks[i], i);
+- } else {
+- s->block_last_index[i] = -1;
+- }
+- cbp+=cbp;
+- }
+- }else{
+- for(i=0;i<6;i++) {
+- if (cbp & 32) {
+- if (mpeg1_decode_block_inter(s, s->pblocks[i], i) < 0)
+- return -1;
+- } else {
+- s->block_last_index[i] = -1;
+- }
+- cbp+=cbp;
+- }
+- }
+- }
+- }else{
+- for(i=0;i<12;i++)
+- s->block_last_index[i] = -1;
++ block[j] = level;
+ }
++ CLOSE_READER(re, &s->gb);
+ }
+-
+- s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]= mb_type;
+-
+- return 0;
++ s->block_last_index[n] = i;
++ return 0;
+ }
+
+-/* as h263, but only 17 codes */
+-static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred)
++static inline int mpeg2_fast_decode_block_non_intra(MpegEncContext *s,
++ DCTELEM *block,
++ int n)
+ {
+- int code, sign, val, l, shift;
+-
+- code = get_vlc2(&s->gb, mv_vlc.table, MV_VLC_BITS, 2);
+- if (code == 0) {
+- return pred;
+- }
+- if (code < 0) {
+- return 0xffff;
+- }
++ int level, i, j, run;
++ RLTable *rl = &rl_mpeg1;
++ uint8_t * const scantable= s->intra_scantable.permutated;
++ const int qscale= s->qscale;
++ OPEN_READER(re, &s->gb);
++ i = -1;
+
+- sign = get_bits1(&s->gb);
+- shift = fcode - 1;
+- val = code;
+- if (shift) {
+- val = (val - 1) << shift;
+- val |= get_bits(&s->gb, shift);
+- val++;
++ /* special case for the first coef. no need to add a second vlc table */
++ UPDATE_CACHE(re, &s->gb);
++ if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
++ level= (3*qscale)>>1;
++ if(GET_CACHE(re, &s->gb)&0x40000000)
++ level= -level;
++ block[0] = level;
++ i++;
++ SKIP_BITS(re, &s->gb, 2);
++ if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
++ goto end;
+ }
+- if (sign)
+- val = -val;
+- val += pred;
+
+- /* modulo decoding */
+- l= INT_BIT - 5 - shift;
+- val = (val<<l)>>l;
+- return val;
+-}
++ /* now quantify & encode AC coefs */
++ for(;;) {
++ GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
+
+-static inline int decode_dc(GetBitContext *gb, int component)
+-{
+- int code, diff;
++ if(level != 0) {
++ i += run;
++ j = scantable[i];
++ level= ((level*2+1)*qscale)>>1;
++ level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
++ SKIP_BITS(re, &s->gb, 1);
++ } else {
++ /* escape */
++ run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
++ UPDATE_CACHE(re, &s->gb);
++ level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
+
+- if (component == 0) {
+- code = get_vlc2(gb, dc_lum_vlc.table, DC_VLC_BITS, 2);
+- } else {
+- code = get_vlc2(gb, dc_chroma_vlc.table, DC_VLC_BITS, 2);
+- }
+- if (code < 0){
+- av_log(NULL, AV_LOG_ERROR, "invalid dc code at\n");
+- return 0xffff;
+- }
+- if (code == 0) {
+- diff = 0;
+- } else {
+- diff = get_xbits(gb, code);
++ i += run;
++ j = scantable[i];
++ if(level<0){
++ level= ((-level*2+1)*qscale)>>1;
++ level= -level;
++ }else{
++ level= ((level*2+1)*qscale)>>1;
++ }
++ }
++
++ block[j] = level;
++ if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
++ break;
++ UPDATE_CACHE(re, &s->gb);
+ }
+- return diff;
++end:
++ LAST_SKIP_BITS(re, &s->gb, 2);
++ CLOSE_READER(re, &s->gb);
++ s->block_last_index[n] = i;
++ return 0;
+ }
+
+-static inline int mpeg1_decode_block_intra(MpegEncContext *s,
++static inline int mpeg2_decode_block_non_intra(MpegEncContext *s,
+ DCTELEM *block,
+ int n)
+ {
+- int level, dc, diff, i, j, run;
+- int component;
++ int level, i, j, run;
+ RLTable *rl = &rl_mpeg1;
+ uint8_t * const scantable= s->intra_scantable.permutated;
+- const uint16_t *quant_matrix= s->intra_matrix;
++ const uint16_t *quant_matrix;
+ const int qscale= s->qscale;
++ int mismatch;
++
++ mismatch = 1;
+
+- /* DC coef */
+- component = (n <= 3 ? 0 : n - 4 + 1);
+- diff = decode_dc(&s->gb, component);
+- if (diff >= 0xffff)
+- return -1;
+- dc = s->last_dc[component];
+- dc += diff;
+- s->last_dc[component] = dc;
+- block[0] = dc<<3;
+- dprintf("dc=%d diff=%d\n", dc, diff);
+- i = 0;
+ {
+ OPEN_READER(re, &s->gb);
++ i = -1;
++ if (n < 4)
++ quant_matrix = s->inter_matrix;
++ else
++ quant_matrix = s->chroma_inter_matrix;
++
++ /* special case for the first coef. no need to add a second vlc table */
++ UPDATE_CACHE(re, &s->gb);
++ if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
++ level= (3*qscale*quant_matrix[0])>>5;
++ if(GET_CACHE(re, &s->gb)&0x40000000)
++ level= -level;
++ block[0] = level;
++ mismatch ^= level;
++ i++;
++ SKIP_BITS(re, &s->gb, 2);
++ if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
++ goto end;
++ }
++
+ /* now quantify & encode AC coefs */
+ for(;;) {
+- UPDATE_CACHE(re, &s->gb);
+ GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
+
+- if(level == 127){
+- break;
+- } else if(level != 0) {
++ if(level != 0) {
+ i += run;
+ j = scantable[i];
+- level= (level*qscale*quant_matrix[j])>>4;
+- level= (level-1)|1;
++ level= ((level*2+1)*qscale*quant_matrix[j])>>5;
+ level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
+- LAST_SKIP_BITS(re, &s->gb, 1);
++ SKIP_BITS(re, &s->gb, 1);
+ } else {
+ /* escape */
+ run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
+ UPDATE_CACHE(re, &s->gb);
+- level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
+- if (level == -128) {
+- level = SHOW_UBITS(re, &s->gb, 8) - 256; LAST_SKIP_BITS(re, &s->gb, 8);
+- } else if (level == 0) {
+- level = SHOW_UBITS(re, &s->gb, 8) ; LAST_SKIP_BITS(re, &s->gb, 8);
+- }
++ level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
++
+ i += run;
+ j = scantable[i];
+ if(level<0){
+- level= -level;
+- level= (level*qscale*quant_matrix[j])>>4;
+- level= (level-1)|1;
++ level= ((-level*2+1)*qscale*quant_matrix[j])>>5;
+ level= -level;
+ }else{
+- level= (level*qscale*quant_matrix[j])>>4;
+- level= (level-1)|1;
++ level= ((level*2+1)*qscale*quant_matrix[j])>>5;
+ }
+ }
+ if (i > 63){
+@@ -1570,12 +1425,20 @@ static inline int mpeg1_decode_block_int
+ return -1;
+ }
+
++ mismatch ^= level;
+ block[j] = level;
++ if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
++ break;
++ UPDATE_CACHE(re, &s->gb);
+ }
++end:
++ LAST_SKIP_BITS(re, &s->gb, 2);
+ CLOSE_READER(re, &s->gb);
+ }
++ block[63] ^= (mismatch & 1);
++
+ s->block_last_index[n] = i;
+- return 0;
++ return 0;
+ }
+
+ static inline int mpeg1_decode_block_inter(MpegEncContext *s,
+@@ -1713,315 +1576,452 @@ static inline int mpeg1_fast_decode_bloc
+ level= (level-1)|1;
+ }
+ }
+-
+- block[j] = level;
+- if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
+- break;
+- UPDATE_CACHE(re, &s->gb);
++
++ block[j] = level;
++ if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
++ break;
++ UPDATE_CACHE(re, &s->gb);
++ }
++end:
++ LAST_SKIP_BITS(re, &s->gb, 2);
++ CLOSE_READER(re, &s->gb);
++ }
++ s->block_last_index[n] = i;
++ return 0;
++}
++
++static int mpeg_decode_mb(MpegEncContext *s,
++ DCTELEM block[12][64])
++{
++ int i, j, k, cbp, val, mb_type, motion_type;
++ const int mb_block_count = 4 + (1<< s->chroma_format);
++
++ dprintf("decode_mb: x=%d y=%d\n", s->mb_x, s->mb_y);
++
++ assert(s->mb_skipped==0);
++
++ if (s->mb_skip_run-- != 0) {
++ if(s->pict_type == I_TYPE){
++ av_log(s->avctx, AV_LOG_ERROR, "skipped MB in I frame at %d %d\n", s->mb_x, s->mb_y);
++ return -1;
++ }
++
++ /* skip mb */
++ s->mb_intra = 0;
++ for(i=0;i<12;i++)
++ s->block_last_index[i] = -1;
++ if(s->picture_structure == PICT_FRAME)
++ s->mv_type = MV_TYPE_16X16;
++ else
++ s->mv_type = MV_TYPE_FIELD;
++ if (s->pict_type == P_TYPE) {
++ /* if P type, zero motion vector is implied */
++ s->mv_dir = MV_DIR_FORWARD;
++ s->mv[0][0][0] = s->mv[0][0][1] = 0;
++ s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
++ s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
++ s->field_select[0][0]= s->picture_structure - 1;
++ s->mb_skipped = 1;
++ s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]= MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;
++ } else {
++ int mb_type;
++
++ if(s->mb_x)
++ mb_type= s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride - 1];
++ else
++ 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,
++ if(IS_INTRA(mb_type))
++ return -1;
++
++ /* if B type, reuse previous vectors and directions */
++ s->mv[0][0][0] = s->last_mv[0][0][0];
++ s->mv[0][0][1] = s->last_mv[0][0][1];
++ s->mv[1][0][0] = s->last_mv[1][0][0];
++ s->mv[1][0][1] = s->last_mv[1][0][1];
++
++ s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]=
++ mb_type | MB_TYPE_SKIP;
++// assert(s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride - 1]&(MB_TYPE_16x16|MB_TYPE_16x8));
++
++ if((s->mv[0][0][0]|s->mv[0][0][1]|s->mv[1][0][0]|s->mv[1][0][1])==0)
++ s->mb_skipped = 1;
++ }
++
++ return 0;
++ }
++
++ switch(s->pict_type) {
++ default:
++ case I_TYPE:
++ if (get_bits1(&s->gb) == 0) {
++ if (get_bits1(&s->gb) == 0){
++ av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in I Frame at %d %d\n", s->mb_x, s->mb_y);
++ return -1;
++ }
++ mb_type = MB_TYPE_QUANT | MB_TYPE_INTRA;
++ } else {
++ mb_type = MB_TYPE_INTRA;
++ }
++ break;
++ case P_TYPE:
++ mb_type = get_vlc2(&s->gb, mb_ptype_vlc.table, MB_PTYPE_VLC_BITS, 1);
++ if (mb_type < 0){
++ av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in P Frame at %d %d\n", s->mb_x, s->mb_y);
++ return -1;
++ }
++ mb_type = ptype2mb_type[ mb_type ];
++ break;
++ case B_TYPE:
++ mb_type = get_vlc2(&s->gb, mb_btype_vlc.table, MB_BTYPE_VLC_BITS, 1);
++ if (mb_type < 0){
++ av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in B Frame at %d %d\n", s->mb_x, s->mb_y);
++ return -1;
++ }
++ mb_type = btype2mb_type[ mb_type ];
++ break;
++ }
++ dprintf("mb_type=%x\n", mb_type);
++// motion_type = 0; /* avoid warning */
++ if (IS_INTRA(mb_type)) {
++ s->dsp.clear_blocks(s->block[0]);
++
++ if(!s->chroma_y_shift){
++ s->dsp.clear_blocks(s->block[6]);
++ }
++
++ /* compute dct type */
++ if (s->picture_structure == PICT_FRAME && //FIXME add a interlaced_dct coded var?
++ !s->frame_pred_frame_dct) {
++ s->interlaced_dct = get_bits1(&s->gb);
++ }
++
++ if (IS_QUANT(mb_type))
++ s->qscale = get_qscale(s);
++
++ if (s->concealment_motion_vectors) {
++ /* just parse them */
++ if (s->picture_structure != PICT_FRAME)
++ skip_bits1(&s->gb); /* field select */
++
++ s->mv[0][0][0]= s->last_mv[0][0][0]= s->last_mv[0][1][0] =
++ mpeg_decode_motion(s, s->mpeg_f_code[0][0], s->last_mv[0][0][0]);
++ s->mv[0][0][1]= s->last_mv[0][0][1]= s->last_mv[0][1][1] =
++ mpeg_decode_motion(s, s->mpeg_f_code[0][1], s->last_mv[0][0][1]);
++
++ skip_bits1(&s->gb); /* marker */
++ }else
++ memset(s->last_mv, 0, sizeof(s->last_mv)); /* reset mv prediction */
++ s->mb_intra = 1;
++#ifdef HAVE_XVMC
++ //one 1 we memcpy blocks in xvmcvideo
++ if(s->avctx->xvmc_acceleration > 1){
++ XVMC_pack_pblocks(s,-1);//inter are always full blocks
++ if(s->swap_uv){
++ exchange_uv(s);
++ }
++ }
++#endif
++
++ if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
++ if(s->flags2 & CODEC_FLAG2_FAST){
++ for(i=0;i<6;i++) {
++ mpeg2_fast_decode_block_intra(s, s->pblocks[i], i);
++ }
++ }else{
++ for(i=0;i<mb_block_count;i++) {
++ if (mpeg2_decode_block_intra(s, s->pblocks[i], i) < 0)
++ return -1;
++ }
++ }
++ } else {
++ for(i=0;i<6;i++) {
++ if (mpeg1_decode_block_intra(s, s->pblocks[i], i) < 0)
++ return -1;
++ }
+ }
+-end:
+- LAST_SKIP_BITS(re, &s->gb, 2);
+- CLOSE_READER(re, &s->gb);
+- }
+- s->block_last_index[n] = i;
+- return 0;
+-}
+-
++ } else {
++ if (mb_type & MB_TYPE_ZERO_MV){
++ assert(mb_type & MB_TYPE_CBP);
+
+-static inline int mpeg2_decode_block_non_intra(MpegEncContext *s,
+- DCTELEM *block,
+- int n)
+-{
+- int level, i, j, run;
+- RLTable *rl = &rl_mpeg1;
+- uint8_t * const scantable= s->intra_scantable.permutated;
+- const uint16_t *quant_matrix;
+- const int qscale= s->qscale;
+- int mismatch;
++ /* compute dct type */
++ if (s->picture_structure == PICT_FRAME && //FIXME add a interlaced_dct coded var?
++ !s->frame_pred_frame_dct) {
++ s->interlaced_dct = get_bits1(&s->gb);
++ }
+
+- mismatch = 1;
++ if (IS_QUANT(mb_type))
++ s->qscale = get_qscale(s);
+
+- {
+- OPEN_READER(re, &s->gb);
+- i = -1;
+- if (n < 4)
+- quant_matrix = s->inter_matrix;
+- else
+- quant_matrix = s->chroma_inter_matrix;
++ s->mv_dir = MV_DIR_FORWARD;
++ if(s->picture_structure == PICT_FRAME)
++ s->mv_type = MV_TYPE_16X16;
++ else{
++ s->mv_type = MV_TYPE_FIELD;
++ mb_type |= MB_TYPE_INTERLACED;
++ s->field_select[0][0]= s->picture_structure - 1;
++ }
++ s->last_mv[0][0][0] = 0;
++ s->last_mv[0][0][1] = 0;
++ s->last_mv[0][1][0] = 0;
++ s->last_mv[0][1][1] = 0;
++ s->mv[0][0][0] = 0;
++ s->mv[0][0][1] = 0;
++ }else{
++ assert(mb_type & MB_TYPE_L0L1);
++//FIXME decide if MBs in field pictures are MB_TYPE_INTERLACED
++ /* get additionnal motion vector type */
++ if (s->frame_pred_frame_dct)
++ motion_type = MT_FRAME;
++ else{
++ motion_type = get_bits(&s->gb, 2);
++ }
+
+- /* special case for the first coef. no need to add a second vlc table */
+- UPDATE_CACHE(re, &s->gb);
+- if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
+- level= (3*qscale*quant_matrix[0])>>5;
+- if(GET_CACHE(re, &s->gb)&0x40000000)
+- level= -level;
+- block[0] = level;
+- mismatch ^= level;
+- i++;
+- SKIP_BITS(re, &s->gb, 2);
+- if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
+- goto end;
+- }
++ /* compute dct type */
++ if (s->picture_structure == PICT_FRAME && //FIXME add a interlaced_dct coded var?
++ !s->frame_pred_frame_dct && HAS_CBP(mb_type)) {
++ s->interlaced_dct = get_bits1(&s->gb);
++ }
+
+- /* now quantify & encode AC coefs */
+- for(;;) {
+- GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
++ if (IS_QUANT(mb_type))
++ s->qscale = get_qscale(s);
+
+- if(level != 0) {
+- i += run;
+- j = scantable[i];
+- level= ((level*2+1)*qscale*quant_matrix[j])>>5;
+- level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
+- SKIP_BITS(re, &s->gb, 1);
+- } else {
+- /* escape */
+- run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
+- UPDATE_CACHE(re, &s->gb);
+- level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
++ /* motion vectors */
++ s->mv_dir = 0;
++ for(i=0;i<2;i++) {
++ if (USES_LIST(mb_type, i)) {
++ s->mv_dir |= (MV_DIR_FORWARD >> i);
++ dprintf("motion_type=%d\n", motion_type);
++ switch(motion_type) {
++ case MT_FRAME: /* or MT_16X8 */
++ if (s->picture_structure == PICT_FRAME) {
++ /* MT_FRAME */
++ mb_type |= MB_TYPE_16x16;
++ s->mv_type = MV_TYPE_16X16;
++ s->mv[i][0][0]= s->last_mv[i][0][0]= s->last_mv[i][1][0] =
++ mpeg_decode_motion(s, s->mpeg_f_code[i][0], s->last_mv[i][0][0]);
++ s->mv[i][0][1]= s->last_mv[i][0][1]= s->last_mv[i][1][1] =
++ mpeg_decode_motion(s, s->mpeg_f_code[i][1], s->last_mv[i][0][1]);
++ /* full_pel: only for mpeg1 */
++ if (s->full_pel[i]){
++ s->mv[i][0][0] <<= 1;
++ s->mv[i][0][1] <<= 1;
++ }
++ } else {
++ /* MT_16X8 */
++ mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
++ s->mv_type = MV_TYPE_16X8;
++ for(j=0;j<2;j++) {
++ s->field_select[i][j] = get_bits1(&s->gb);
++ for(k=0;k<2;k++) {
++ val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
++ s->last_mv[i][j][k]);
++ s->last_mv[i][j][k] = val;
++ s->mv[i][j][k] = val;
++ }
++ }
++ }
++ break;
++ case MT_FIELD:
++ s->mv_type = MV_TYPE_FIELD;
++ if (s->picture_structure == PICT_FRAME) {
++ mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
++ for(j=0;j<2;j++) {
++ s->field_select[i][j] = get_bits1(&s->gb);
++ val = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
++ s->last_mv[i][j][0]);
++ s->last_mv[i][j][0] = val;
++ s->mv[i][j][0] = val;
++ dprintf("fmx=%d\n", val);
++ val = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
++ s->last_mv[i][j][1] >> 1);
++ s->last_mv[i][j][1] = val << 1;
++ s->mv[i][j][1] = val;
++ dprintf("fmy=%d\n", val);
++ }
++ } else {
++ mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
++ s->field_select[i][0] = get_bits1(&s->gb);
++ for(k=0;k<2;k++) {
++ val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
++ s->last_mv[i][0][k]);
++ s->last_mv[i][0][k] = val;
++ s->last_mv[i][1][k] = val;
++ s->mv[i][0][k] = val;
++ }
++ }
++ break;
++ case MT_DMV:
++ {
++ int dmx, dmy, mx, my, m;
+
+- i += run;
+- j = scantable[i];
+- if(level<0){
+- level= ((-level*2+1)*qscale*quant_matrix[j])>>5;
+- level= -level;
+- }else{
+- level= ((level*2+1)*qscale*quant_matrix[j])>>5;
+- }
+- }
+- if (i > 63){
+- av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
+- return -1;
+- }
++ mx = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
++ s->last_mv[i][0][0]);
++ s->last_mv[i][0][0] = mx;
++ s->last_mv[i][1][0] = mx;
++ dmx = get_dmv(s);
++ my = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
++ s->last_mv[i][0][1] >> 1);
++ dmy = get_dmv(s);
++ s->mv_type = MV_TYPE_DMV;
+
+- mismatch ^= level;
+- block[j] = level;
+- if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
+- break;
+- UPDATE_CACHE(re, &s->gb);
+- }
+-end:
+- LAST_SKIP_BITS(re, &s->gb, 2);
+- CLOSE_READER(re, &s->gb);
+- }
+- block[63] ^= (mismatch & 1);
+
+- s->block_last_index[n] = i;
+- return 0;
+-}
++ s->last_mv[i][0][1] = my<<1;
++ s->last_mv[i][1][1] = my<<1;
+
+-static inline int mpeg2_fast_decode_block_non_intra(MpegEncContext *s,
+- DCTELEM *block,
+- int n)
+-{
+- int level, i, j, run;
+- RLTable *rl = &rl_mpeg1;
+- uint8_t * const scantable= s->intra_scantable.permutated;
+- const int qscale= s->qscale;
+- OPEN_READER(re, &s->gb);
+- i = -1;
++ s->mv[i][0][0] = mx;
++ s->mv[i][0][1] = my;
++ s->mv[i][1][0] = mx;//not used
++ s->mv[i][1][1] = my;//not used
+
+- /* special case for the first coef. no need to add a second vlc table */
+- UPDATE_CACHE(re, &s->gb);
+- if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
+- level= (3*qscale)>>1;
+- if(GET_CACHE(re, &s->gb)&0x40000000)
+- level= -level;
+- block[0] = level;
+- i++;
+- SKIP_BITS(re, &s->gb, 2);
+- if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
+- goto end;
+- }
++ if (s->picture_structure == PICT_FRAME) {
++ mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
+
+- /* now quantify & encode AC coefs */
+- for(;;) {
+- GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
++ //m = 1 + 2 * s->top_field_first;
++ m = s->top_field_first ? 1 : 3;
+
+- if(level != 0) {
+- i += run;
+- j = scantable[i];
+- level= ((level*2+1)*qscale)>>1;
+- level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
+- SKIP_BITS(re, &s->gb, 1);
+- } else {
+- /* escape */
+- run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
+- UPDATE_CACHE(re, &s->gb);
+- level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
++ /* top -> top pred */
++ s->mv[i][2][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
++ s->mv[i][2][1] = ((my * m + (my > 0)) >> 1) + dmy - 1;
++ m = 4 - m;
++ s->mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
++ s->mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1;
++ } else {
++ mb_type |= MB_TYPE_16x16;
+
+- i += run;
+- j = scantable[i];
+- if(level<0){
+- level= ((-level*2+1)*qscale)>>1;
+- level= -level;
+- }else{
+- level= ((level*2+1)*qscale)>>1;
++ s->mv[i][2][0] = ((mx + (mx > 0)) >> 1) + dmx;
++ s->mv[i][2][1] = ((my + (my > 0)) >> 1) + dmy;
++ if(s->picture_structure == PICT_TOP_FIELD)
++ s->mv[i][2][1]--;
++ else
++ s->mv[i][2][1]++;
++ }
++ }
++ break;
++ default:
++ av_log(s->avctx, AV_LOG_ERROR, "00 motion_type at %d %d\n", s->mb_x, s->mb_y);
++ return -1;
++ }
++ }
+ }
+ }
+
+- block[j] = level;
+- if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
+- break;
+- UPDATE_CACHE(re, &s->gb);
+- }
+-end:
+- LAST_SKIP_BITS(re, &s->gb, 2);
+- CLOSE_READER(re, &s->gb);
+- s->block_last_index[n] = i;
+- return 0;
+-}
++ s->mb_intra = 0;
++ if (HAS_CBP(mb_type)) {
++ s->dsp.clear_blocks(s->block[0]);
+
++ if(!s->chroma_y_shift){
++ s->dsp.clear_blocks(s->block[6]);
++ }
+
+-static inline int mpeg2_decode_block_intra(MpegEncContext *s,
+- DCTELEM *block,
+- int n)
+-{
+- int level, dc, diff, i, j, run;
+- int component;
+- RLTable *rl;
+- uint8_t * const scantable= s->intra_scantable.permutated;
+- const uint16_t *quant_matrix;
+- const int qscale= s->qscale;
+- int mismatch;
++ cbp = get_vlc2(&s->gb, mb_pat_vlc.table, MB_PAT_VLC_BITS, 1);
++ if (cbp < 0 || ((cbp == 0) && (s->chroma_format < 2)) ){
++ av_log(s->avctx, AV_LOG_ERROR, "invalid cbp at %d %d\n", s->mb_x, s->mb_y);
++ return -1;
++ }
++ if(mb_block_count > 6){
++ cbp<<= mb_block_count-6;
++ cbp |= get_bits(&s->gb, mb_block_count-6);
++ }
+
+- /* DC coef */
+- if (n < 4){
+- quant_matrix = s->intra_matrix;
+- component = 0;
+- }else{
+- quant_matrix = s->chroma_intra_matrix;
+- component = (n&1) + 1;
+- }
+- diff = decode_dc(&s->gb, component);
+- if (diff >= 0xffff)
+- return -1;
+- dc = s->last_dc[component];
+- dc += diff;
+- s->last_dc[component] = dc;
+- block[0] = dc << (3 - s->intra_dc_precision);
+- dprintf("dc=%d\n", block[0]);
+- mismatch = block[0] ^ 1;
+- i = 0;
+- if (s->intra_vlc_format)
+- rl = &rl_mpeg2;
+- else
+- rl = &rl_mpeg1;
++#ifdef HAVE_XVMC
++ //on 1 we memcpy blocks in xvmcvideo
++ if(s->avctx->xvmc_acceleration > 1){
++ XVMC_pack_pblocks(s,cbp);
++ if(s->swap_uv){
++ exchange_uv(s);
++ }
++ }
++#endif
+
+- {
+- OPEN_READER(re, &s->gb);
+- /* now quantify & encode AC coefs */
+- for(;;) {
+- UPDATE_CACHE(re, &s->gb);
+- GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
++ if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
++ if(s->flags2 & CODEC_FLAG2_FAST){
++ for(i=0;i<6;i++) {
++ if(cbp & 32) {
++ mpeg2_fast_decode_block_non_intra(s, s->pblocks[i], i);
++ } else {
++ s->block_last_index[i] = -1;
++ }
++ cbp+=cbp;
++ }
++ }else{
++ cbp<<= 12-mb_block_count;
+
+- if(level == 127){
+- break;
+- } else if(level != 0) {
+- i += run;
+- j = scantable[i];
+- level= (level*qscale*quant_matrix[j])>>4;
+- level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
+- LAST_SKIP_BITS(re, &s->gb, 1);
++ for(i=0;i<mb_block_count;i++) {
++ if ( cbp & (1<<11) ) {
++ if (mpeg2_decode_block_non_intra(s, s->pblocks[i], i) < 0)
++ return -1;
++ } else {
++ s->block_last_index[i] = -1;
++ }
++ cbp+=cbp;
++ }
++ }
+ } else {
+- /* escape */
+- run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
+- UPDATE_CACHE(re, &s->gb);
+- level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
+- i += run;
+- j = scantable[i];
+- if(level<0){
+- level= (-level*qscale*quant_matrix[j])>>4;
+- level= -level;
++ if(s->flags2 & CODEC_FLAG2_FAST){
++ for(i=0;i<6;i++) {
++ if (cbp & 32) {
++ mpeg1_fast_decode_block_inter(s, s->pblocks[i], i);
++ } else {
++ s->block_last_index[i] = -1;
++ }
++ cbp+=cbp;
++ }
+ }else{
+- level= (level*qscale*quant_matrix[j])>>4;
++ for(i=0;i<6;i++) {
++ if (cbp & 32) {
++ if (mpeg1_decode_block_inter(s, s->pblocks[i], i) < 0)
++ return -1;
++ } else {
++ s->block_last_index[i] = -1;
++ }
++ cbp+=cbp;
++ }
+ }
+ }
+- if (i > 63){
+- av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
+- return -1;
+- }
+-
+- mismatch^= level;
+- block[j] = level;
++ }else{
++ for(i=0;i<12;i++)
++ s->block_last_index[i] = -1;
+ }
+- CLOSE_READER(re, &s->gb);
+ }
+- block[63]^= mismatch&1;
+
+- s->block_last_index[n] = i;
++ s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]= mb_type;
++
+ return 0;
+ }
+
+-static inline int mpeg2_fast_decode_block_intra(MpegEncContext *s,
+- DCTELEM *block,
+- int n)
++/* as h263, but only 17 codes */
++static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred)
+ {
+- int level, dc, diff, j, run;
+- int component;
+- RLTable *rl;
+- uint8_t * scantable= s->intra_scantable.permutated;
+- const uint16_t *quant_matrix;
+- const int qscale= s->qscale;
++ int code, sign, val, l, shift;
+
+- /* DC coef */
+- if (n < 4){
+- quant_matrix = s->intra_matrix;
+- component = 0;
+- }else{
+- quant_matrix = s->chroma_intra_matrix;
+- component = (n&1) + 1;
++ code = get_vlc2(&s->gb, mv_vlc.table, MV_VLC_BITS, 2);
++ if (code == 0) {
++ return pred;
++ }
++ if (code < 0) {
++ return 0xffff;
+ }
+- diff = decode_dc(&s->gb, component);
+- if (diff >= 0xffff)
+- return -1;
+- dc = s->last_dc[component];
+- dc += diff;
+- s->last_dc[component] = dc;
+- block[0] = dc << (3 - s->intra_dc_precision);
+- if (s->intra_vlc_format)
+- rl = &rl_mpeg2;
+- else
+- rl = &rl_mpeg1;
+-
+- {
+- OPEN_READER(re, &s->gb);
+- /* now quantify & encode AC coefs */
+- for(;;) {
+- UPDATE_CACHE(re, &s->gb);
+- GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
+-
+- if(level == 127){
+- break;
+- } else if(level != 0) {
+- scantable += run;
+- j = *scantable;
+- level= (level*qscale*quant_matrix[j])>>4;
+- level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
+- LAST_SKIP_BITS(re, &s->gb, 1);
+- } else {
+- /* escape */
+- run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
+- UPDATE_CACHE(re, &s->gb);
+- level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
+- scantable += run;
+- j = *scantable;
+- if(level<0){
+- level= (-level*qscale*quant_matrix[j])>>4;
+- level= -level;
+- }else{
+- level= (level*qscale*quant_matrix[j])>>4;
+- }
+- }
+
+- block[j] = level;
+- }
+- CLOSE_READER(re, &s->gb);
++ sign = get_bits1(&s->gb);
++ shift = fcode - 1;
++ val = code;
++ if (shift) {
++ val = (val - 1) << shift;
++ val |= get_bits(&s->gb, shift);
++ val++;
+ }
++ if (sign)
++ val = -val;
++ val += pred;
+
+- s->block_last_index[n] = scantable - s->intra_scantable.permutated;
+- return 0;
++ /* modulo decoding */
++ l= INT_BIT - 5 - shift;
++ val = (val<<l)>>l;
++ return val;
+ }
+
++
++
+ typedef struct Mpeg1Context {
+ MpegEncContext mpeg_enc_ctx;
+ int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
diff --git a/multimedia/xine-lib/patches/patch-dc b/multimedia/xine-lib/patches/patch-dc
new file mode 100644
index 00000000000..c92eb868f19
--- /dev/null
+++ b/multimedia/xine-lib/patches/patch-dc
@@ -0,0 +1,382 @@
+$NetBSD: patch-dc,v 1.1 2006/08/14 12:15:38 joerg Exp $
+
+Move some inlined functions to a place before they are used,
+otherwise GCC barfs when -fno-unit-a-time is active.
+
+--- src/libffmpeg/libavcodec/msmpeg4.c.orig 2006-08-14 11:51:09.000000000 +0000
++++ src/libffmpeg/libavcodec/msmpeg4.c
+@@ -1479,186 +1479,6 @@ static int msmpeg4v2_decode_motion(MpegE
+ return val;
+ }
+
+-static int msmpeg4v12_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
+-{
+- int cbp, code, i;
+-
+- if (s->pict_type == P_TYPE) {
+- if (s->use_skip_mb_code) {
+- if (get_bits1(&s->gb)) {
+- /* skip mb */
+- s->mb_intra = 0;
+- for(i=0;i<6;i++)
+- s->block_last_index[i] = -1;
+- s->mv_dir = MV_DIR_FORWARD;
+- s->mv_type = MV_TYPE_16X16;
+- s->mv[0][0][0] = 0;
+- s->mv[0][0][1] = 0;
+- s->mb_skipped = 1;
+- return 0;
+- }
+- }
+-
+- if(s->msmpeg4_version==2)
+- code = get_vlc2(&s->gb, v2_mb_type_vlc.table, V2_MB_TYPE_VLC_BITS, 1);
+- else
+- code = get_vlc2(&s->gb, v1_inter_cbpc_vlc.table, V1_INTER_CBPC_VLC_BITS, 3);
+- if(code<0 || code>7){
+- av_log(s->avctx, AV_LOG_ERROR, "cbpc %d invalid at %d %d\n", code, s->mb_x, s->mb_y);
+- return -1;
+- }
+-
+- s->mb_intra = code >>2;
+-
+- cbp = code & 0x3;
+- } else {
+- s->mb_intra = 1;
+- if(s->msmpeg4_version==2)
+- cbp= get_vlc2(&s->gb, v2_intra_cbpc_vlc.table, V2_INTRA_CBPC_VLC_BITS, 1);
+- else
+- cbp= get_vlc2(&s->gb, v1_intra_cbpc_vlc.table, V1_INTRA_CBPC_VLC_BITS, 1);
+- if(cbp<0 || cbp>3){
+- av_log(s->avctx, AV_LOG_ERROR, "cbpc %d invalid at %d %d\n", cbp, s->mb_x, s->mb_y);
+- return -1;
+- }
+- }
+-
+- if (!s->mb_intra) {
+- int mx, my, cbpy;
+-
+- cbpy= get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1);
+- if(cbpy<0){
+- av_log(s->avctx, AV_LOG_ERROR, "cbpy %d invalid at %d %d\n", cbp, s->mb_x, s->mb_y);
+- return -1;
+- }
+-
+- cbp|= cbpy<<2;
+- if(s->msmpeg4_version==1 || (cbp&3) != 3) cbp^= 0x3C;
+-
+- h263_pred_motion(s, 0, 0, &mx, &my);
+- mx= msmpeg4v2_decode_motion(s, mx, 1);
+- my= msmpeg4v2_decode_motion(s, my, 1);
+-
+- s->mv_dir = MV_DIR_FORWARD;
+- s->mv_type = MV_TYPE_16X16;
+- s->mv[0][0][0] = mx;
+- s->mv[0][0][1] = my;
+- } else {
+- if(s->msmpeg4_version==2){
+- s->ac_pred = get_bits1(&s->gb);
+- cbp|= get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1)<<2; //FIXME check errors
+- } else{
+- s->ac_pred = 0;
+- cbp|= get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1)<<2; //FIXME check errors
+- if(s->pict_type==P_TYPE) cbp^=0x3C;
+- }
+- }
+-
+- s->dsp.clear_blocks(s->block[0]);
+- for (i = 0; i < 6; i++) {
+- if (msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1, NULL) < 0)
+- {
+- av_log(s->avctx, AV_LOG_ERROR, "\nerror while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
+- return -1;
+- }
+- }
+- return 0;
+-}
+-
+-static int msmpeg4v34_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
+-{
+- int cbp, code, i;
+- uint8_t *coded_val;
+- uint32_t * const mb_type_ptr= &s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ];
+-
+- if (s->pict_type == P_TYPE) {
+- set_stat(ST_INTER_MB);
+- if (s->use_skip_mb_code) {
+- if (get_bits1(&s->gb)) {
+- /* skip mb */
+- s->mb_intra = 0;
+- for(i=0;i<6;i++)
+- s->block_last_index[i] = -1;
+- s->mv_dir = MV_DIR_FORWARD;
+- s->mv_type = MV_TYPE_16X16;
+- s->mv[0][0][0] = 0;
+- s->mv[0][0][1] = 0;
+- s->mb_skipped = 1;
+- *mb_type_ptr = MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;
+-
+- return 0;
+- }
+- }
+-
+- code = get_vlc2(&s->gb, mb_non_intra_vlc[DEFAULT_INTER_INDEX].table, MB_NON_INTRA_VLC_BITS, 3);
+- if (code < 0)
+- return -1;
+- //s->mb_intra = (code & 0x40) ? 0 : 1;
+- s->mb_intra = (~code & 0x40) >> 6;
+-
+- cbp = code & 0x3f;
+- } else {
+- set_stat(ST_INTRA_MB);
+- s->mb_intra = 1;
+- code = get_vlc2(&s->gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
+- if (code < 0)
+- return -1;
+- /* predict coded block pattern */
+- cbp = 0;
+- for(i=0;i<6;i++) {
+- int val = ((code >> (5 - i)) & 1);
+- if (i < 4) {
+- int pred = coded_block_pred(s, i, &coded_val);
+- val = val ^ pred;
+- *coded_val = val;
+- }
+- cbp |= val << (5 - i);
+- }
+- }
+-
+- if (!s->mb_intra) {
+- int mx, my;
+-//printf("P at %d %d\n", s->mb_x, s->mb_y);
+- if(s->per_mb_rl_table && cbp){
+- s->rl_table_index = decode012(&s->gb);
+- s->rl_chroma_table_index = s->rl_table_index;
+- }
+- set_stat(ST_MV);
+- h263_pred_motion(s, 0, 0, &mx, &my);
+- if (msmpeg4_decode_motion(s, &mx, &my) < 0)
+- return -1;
+- s->mv_dir = MV_DIR_FORWARD;
+- s->mv_type = MV_TYPE_16X16;
+- s->mv[0][0][0] = mx;
+- s->mv[0][0][1] = my;
+- *mb_type_ptr = MB_TYPE_L0 | MB_TYPE_16x16;
+- } else {
+-//printf("I at %d %d %d %06X\n", s->mb_x, s->mb_y, ((cbp&3)? 1 : 0) +((cbp&0x3C)? 2 : 0), show_bits(&s->gb, 24));
+- set_stat(ST_INTRA_MB);
+- s->ac_pred = get_bits1(&s->gb);
+- *mb_type_ptr = MB_TYPE_INTRA;
+- if(s->inter_intra_pred){
+- s->h263_aic_dir= get_vlc2(&s->gb, inter_intra_vlc.table, INTER_INTRA_VLC_BITS, 1);
+-// printf("%d%d %d %d/", s->ac_pred, s->h263_aic_dir, s->mb_x, s->mb_y);
+- }
+- if(s->per_mb_rl_table && cbp){
+- s->rl_table_index = decode012(&s->gb);
+- s->rl_chroma_table_index = s->rl_table_index;
+- }
+- }
+-
+- s->dsp.clear_blocks(s->block[0]);
+- for (i = 0; i < 6; i++) {
+- if (msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1, NULL) < 0)
+- {
+- av_log(s->avctx, AV_LOG_ERROR, "\nerror while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
+- return -1;
+- }
+- }
+-
+- return 0;
+-}
+-//#define ERROR_DETAILS
+ static inline int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
+ int n, int coded, const uint8_t *scan_table)
+ {
+@@ -1909,6 +1729,187 @@ static inline int msmpeg4_decode_block(M
+ return 0;
+ }
+
++static int msmpeg4v12_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
++{
++ int cbp, code, i;
++
++ if (s->pict_type == P_TYPE) {
++ if (s->use_skip_mb_code) {
++ if (get_bits1(&s->gb)) {
++ /* skip mb */
++ s->mb_intra = 0;
++ for(i=0;i<6;i++)
++ s->block_last_index[i] = -1;
++ s->mv_dir = MV_DIR_FORWARD;
++ s->mv_type = MV_TYPE_16X16;
++ s->mv[0][0][0] = 0;
++ s->mv[0][0][1] = 0;
++ s->mb_skipped = 1;
++ return 0;
++ }
++ }
++
++ if(s->msmpeg4_version==2)
++ code = get_vlc2(&s->gb, v2_mb_type_vlc.table, V2_MB_TYPE_VLC_BITS, 1);
++ else
++ code = get_vlc2(&s->gb, v1_inter_cbpc_vlc.table, V1_INTER_CBPC_VLC_BITS, 3);
++ if(code<0 || code>7){
++ av_log(s->avctx, AV_LOG_ERROR, "cbpc %d invalid at %d %d\n", code, s->mb_x, s->mb_y);
++ return -1;
++ }
++
++ s->mb_intra = code >>2;
++
++ cbp = code & 0x3;
++ } else {
++ s->mb_intra = 1;
++ if(s->msmpeg4_version==2)
++ cbp= get_vlc2(&s->gb, v2_intra_cbpc_vlc.table, V2_INTRA_CBPC_VLC_BITS, 1);
++ else
++ cbp= get_vlc2(&s->gb, v1_intra_cbpc_vlc.table, V1_INTRA_CBPC_VLC_BITS, 1);
++ if(cbp<0 || cbp>3){
++ av_log(s->avctx, AV_LOG_ERROR, "cbpc %d invalid at %d %d\n", cbp, s->mb_x, s->mb_y);
++ return -1;
++ }
++ }
++
++ if (!s->mb_intra) {
++ int mx, my, cbpy;
++
++ cbpy= get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1);
++ if(cbpy<0){
++ av_log(s->avctx, AV_LOG_ERROR, "cbpy %d invalid at %d %d\n", cbp, s->mb_x, s->mb_y);
++ return -1;
++ }
++
++ cbp|= cbpy<<2;
++ if(s->msmpeg4_version==1 || (cbp&3) != 3) cbp^= 0x3C;
++
++ h263_pred_motion(s, 0, 0, &mx, &my);
++ mx= msmpeg4v2_decode_motion(s, mx, 1);
++ my= msmpeg4v2_decode_motion(s, my, 1);
++
++ s->mv_dir = MV_DIR_FORWARD;
++ s->mv_type = MV_TYPE_16X16;
++ s->mv[0][0][0] = mx;
++ s->mv[0][0][1] = my;
++ } else {
++ if(s->msmpeg4_version==2){
++ s->ac_pred = get_bits1(&s->gb);
++ cbp|= get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1)<<2; //FIXME check errors
++ } else{
++ s->ac_pred = 0;
++ cbp|= get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1)<<2; //FIXME check errors
++ if(s->pict_type==P_TYPE) cbp^=0x3C;
++ }
++ }
++
++ s->dsp.clear_blocks(s->block[0]);
++ for (i = 0; i < 6; i++) {
++ if (msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1, NULL) < 0)
++ {
++ av_log(s->avctx, AV_LOG_ERROR, "\nerror while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
++ return -1;
++ }
++ }
++ return 0;
++}
++
++static int msmpeg4v34_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
++{
++ int cbp, code, i;
++ uint8_t *coded_val;
++ uint32_t * const mb_type_ptr= &s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ];
++
++ if (s->pict_type == P_TYPE) {
++ set_stat(ST_INTER_MB);
++ if (s->use_skip_mb_code) {
++ if (get_bits1(&s->gb)) {
++ /* skip mb */
++ s->mb_intra = 0;
++ for(i=0;i<6;i++)
++ s->block_last_index[i] = -1;
++ s->mv_dir = MV_DIR_FORWARD;
++ s->mv_type = MV_TYPE_16X16;
++ s->mv[0][0][0] = 0;
++ s->mv[0][0][1] = 0;
++ s->mb_skipped = 1;
++ *mb_type_ptr = MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;
++
++ return 0;
++ }
++ }
++
++ code = get_vlc2(&s->gb, mb_non_intra_vlc[DEFAULT_INTER_INDEX].table, MB_NON_INTRA_VLC_BITS, 3);
++ if (code < 0)
++ return -1;
++ //s->mb_intra = (code & 0x40) ? 0 : 1;
++ s->mb_intra = (~code & 0x40) >> 6;
++
++ cbp = code & 0x3f;
++ } else {
++ set_stat(ST_INTRA_MB);
++ s->mb_intra = 1;
++ code = get_vlc2(&s->gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
++ if (code < 0)
++ return -1;
++ /* predict coded block pattern */
++ cbp = 0;
++ for(i=0;i<6;i++) {
++ int val = ((code >> (5 - i)) & 1);
++ if (i < 4) {
++ int pred = coded_block_pred(s, i, &coded_val);
++ val = val ^ pred;
++ *coded_val = val;
++ }
++ cbp |= val << (5 - i);
++ }
++ }
++
++ if (!s->mb_intra) {
++ int mx, my;
++//printf("P at %d %d\n", s->mb_x, s->mb_y);
++ if(s->per_mb_rl_table && cbp){
++ s->rl_table_index = decode012(&s->gb);
++ s->rl_chroma_table_index = s->rl_table_index;
++ }
++ set_stat(ST_MV);
++ h263_pred_motion(s, 0, 0, &mx, &my);
++ if (msmpeg4_decode_motion(s, &mx, &my) < 0)
++ return -1;
++ s->mv_dir = MV_DIR_FORWARD;
++ s->mv_type = MV_TYPE_16X16;
++ s->mv[0][0][0] = mx;
++ s->mv[0][0][1] = my;
++ *mb_type_ptr = MB_TYPE_L0 | MB_TYPE_16x16;
++ } else {
++//printf("I at %d %d %d %06X\n", s->mb_x, s->mb_y, ((cbp&3)? 1 : 0) +((cbp&0x3C)? 2 : 0), show_bits(&s->gb, 24));
++ set_stat(ST_INTRA_MB);
++ s->ac_pred = get_bits1(&s->gb);
++ *mb_type_ptr = MB_TYPE_INTRA;
++ if(s->inter_intra_pred){
++ s->h263_aic_dir= get_vlc2(&s->gb, inter_intra_vlc.table, INTER_INTRA_VLC_BITS, 1);
++// printf("%d%d %d %d/", s->ac_pred, s->h263_aic_dir, s->mb_x, s->mb_y);
++ }
++ if(s->per_mb_rl_table && cbp){
++ s->rl_table_index = decode012(&s->gb);
++ s->rl_chroma_table_index = s->rl_table_index;
++ }
++ }
++
++ s->dsp.clear_blocks(s->block[0]);
++ for (i = 0; i < 6; i++) {
++ if (msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1, NULL) < 0)
++ {
++ av_log(s->avctx, AV_LOG_ERROR, "\nerror while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
++ return -1;
++ }
++ }
++
++ return 0;
++}
++//#define ERROR_DETAILS
++
+ static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr)
+ {
+ int level, pred;