j= s->intra_scantable.permutated[i]; s->inter_matrix[j]= v; s->chroma_inter_matrix[j]= v;
j= s->idct_permutation[ ff_zigzag_direct[i] ]; s->inter_matrix[j]= last; s->chroma_inter_matrix[j]= last;
int mpeg4_decode_picture_header(MpegEncContext * s){    int time_incr, startcode, state, v;    int time_increment; redo:    /* search next start code */    align_get_bits(&s->gb);    state = 0xff;    for(;;) {        v = get_bits(&s->gb, 8);        if (state == 0x000001) {            state = ((state << 8) | v) & 0xffffff;            startcode = state;            break;        }        state = ((state << 8) | v) & 0xffffff;        if( get_bits_count(&s->gb) > s->gb.size*8-32){            if(s->gb.size>50){                printf("no VOP startcode found, frame size was=%d\n", s->gb.size);                return -1;            }else{                printf("frame skip\n");                return FRAME_SKIPED;            }        }    }//printf("startcode %X %d\n", startcode, get_bits_count(&s->gb));    if (startcode == 0x120) { // Video Object Layer        int width, height, vo_ver_id;        /* vol header */        skip_bits(&s->gb, 1); /* random access */        s->vo_type= get_bits(&s->gb, 8);        if (get_bits1(&s->gb) != 0) { /* is_ol_id */            vo_ver_id = get_bits(&s->gb, 4); /* vo_ver_id */            skip_bits(&s->gb, 3); /* vo_priority */        } else {            vo_ver_id = 1;        }//printf("vo type:%d\n",s->vo_type);        s->aspect_ratio_info= get_bits(&s->gb, 4);	if(s->aspect_ratio_info == FF_ASPECT_EXTENDED){	    	    s->aspected_width = get_bits(&s->gb, 8); // par_width	    s->aspected_height = get_bits(&s->gb, 8); // par_height        }        if ((s->vol_control_parameters=get_bits1(&s->gb))) { /* vol control parameter */            int chroma_format= get_bits(&s->gb, 2);            if(chroma_format!=1){                printf("illegal chroma format\n");            }            s->low_delay= get_bits1(&s->gb);            if(get_bits1(&s->gb)){ /* vbv parameters */                get_bits(&s->gb, 15);	/* first_half_bitrate */                skip_bits1(&s->gb);	/* marker */                get_bits(&s->gb, 15);	/* latter_half_bitrate */                skip_bits1(&s->gb);	/* marker */                get_bits(&s->gb, 15);	/* first_half_vbv_buffer_size */                skip_bits1(&s->gb);	/* marker */                get_bits(&s->gb, 3);	/* latter_half_vbv_buffer_size */                get_bits(&s->gb, 11);	/* first_half_vbv_occupancy */                skip_bits1(&s->gb);	/* marker */                get_bits(&s->gb, 15);	/* latter_half_vbv_occupancy */                skip_bits1(&s->gb);	/* marker */                           }        }else{            // set low delay flag only once so the smart? low delay detection wont be overriden            if(s->picture_number==0)                s->low_delay=0;        }        s->shape = get_bits(&s->gb, 2); /* vol shape */        if(s->shape != RECT_SHAPE) printf("only rectangular vol supported\n");        if(s->shape == GRAY_SHAPE && vo_ver_id != 1){            printf("Gray shape not supported\n");            skip_bits(&s->gb, 4);  //video_object_layer_shape_extension        }        skip_bits1(&s->gb);   /* marker */                s->time_increment_resolution = get_bits(&s->gb, 16);                s->time_increment_bits = av_log2(s->time_increment_resolution - 1) + 1;        if (s->time_increment_bits < 1)            s->time_increment_bits = 1;        skip_bits1(&s->gb);   /* marker */        if (get_bits1(&s->gb) != 0) {   /* fixed_vop_rate  */            skip_bits(&s->gb, s->time_increment_bits);        }        if (s->shape != BIN_ONLY_SHAPE) {            if (s->shape == RECT_SHAPE) {                skip_bits1(&s->gb);   /* marker */                width = get_bits(&s->gb, 13);                skip_bits1(&s->gb);   /* marker */                height = get_bits(&s->gb, 13);                skip_bits1(&s->gb);   /* marker */                if(width && height){ /* they should be non zero but who knows ... */                    s->width = width;                    s->height = height;//                    printf("width/height: %d %d\n", width, height);                }            }                        s->progressive_sequence= get_bits1(&s->gb)^1;            if(!get_bits1(&s->gb)) printf("OBMC not supported (very likely buggy encoder)\n");   /* OBMC Disable */            if (vo_ver_id == 1) {                s->vol_sprite_usage = get_bits1(&s->gb); /* vol_sprite_usage */            } else {                s->vol_sprite_usage = get_bits(&s->gb, 2); /* vol_sprite_usage */            }            if(s->vol_sprite_usage==STATIC_SPRITE) printf("Static Sprites not supported\n");            if(s->vol_sprite_usage==STATIC_SPRITE || s->vol_sprite_usage==GMC_SPRITE){                if(s->vol_sprite_usage==STATIC_SPRITE){                    s->sprite_width = get_bits(&s->gb, 13);                    skip_bits1(&s->gb); /* marker */                    s->sprite_height= get_bits(&s->gb, 13);                    skip_bits1(&s->gb); /* marker */                    s->sprite_left  = get_bits(&s->gb, 13);                    skip_bits1(&s->gb); /* marker */                    s->sprite_top   = get_bits(&s->gb, 13);                    skip_bits1(&s->gb); /* marker */                }                s->num_sprite_warping_points= get_bits(&s->gb, 6);                s->sprite_warping_accuracy = get_bits(&s->gb, 2);                s->sprite_brightness_change= get_bits1(&s->gb);                if(s->vol_sprite_usage==STATIC_SPRITE)                    s->low_latency_sprite= get_bits1(&s->gb);                        }            // FIXME sadct disable bit if verid!=1 && shape not rect                        if (get_bits1(&s->gb) == 1) {   /* not_8_bit */                s->quant_precision = get_bits(&s->gb, 4); /* quant_precision */                if(get_bits(&s->gb, 4)!=8) printf("N-bit not supported\n"); /* bits_per_pixel */                if(s->quant_precision!=5) printf("quant precission %d\n", s->quant_precision);            } else {                s->quant_precision = 5;            }                        // FIXME a bunch of grayscale shape things            if((s->mpeg_quant=get_bits1(&s->gb))){ /* vol_quant_type */                int i, j, v;                                /* load default matrixes */                for(i=0; i<64; i++){                    int j= s->idct_permutation[i];                    v= ff_mpeg4_default_intra_matrix[i];                    s->intra_matrix[j]= v;                    s->chroma_intra_matrix[j]= v;                                        v= ff_mpeg4_default_non_intra_matrix[i];                    s->inter_matrix[j]= v;                    s->chroma_inter_matrix[j]= v;                }                /* load custom intra matrix */                if(get_bits1(&s->gb)){                    for(i=0; i<64; i++){                        v= get_bits(&s->gb, 8);                        if(v==0) break;                        j= s->intra_scantable.permutated[i];                        s->intra_matrix[j]= v;                        s->chroma_intra_matrix[j]= v;                    }                }                /* load custom non intra matrix */                if(get_bits1(&s->gb)){                    for(i=0; i<64; i++){                        v= get_bits(&s->gb, 8);                        if(v==0) break;                        j= s->intra_scantable.permutated[i];                        s->inter_matrix[j]= v;                        s->chroma_inter_matrix[j]= v;                    }                    /* replicate last value */                    for(; i<64; i++){                        j= s->intra_scantable.permutated[i];                        s->inter_matrix[j]= v;                        s->chroma_inter_matrix[j]= v;                    }                }                // FIXME a bunch of grayscale shape things            }            if(vo_ver_id != 1)                 s->quarter_sample= get_bits1(&s->gb);            else s->quarter_sample=0;            if(!get_bits1(&s->gb)) printf("Complexity estimation not supported\n");            s->resync_marker= !get_bits1(&s->gb); /* resync_marker_disabled */            s->data_partitioning= get_bits1(&s->gb);            if(s->data_partitioning){                s->rvlc= get_bits1(&s->gb);                if(s->rvlc){                    printf("reversible vlc not supported\n");                }            }                        if(vo_ver_id != 1) {                s->new_pred= get_bits1(&s->gb);                if(s->new_pred){                    printf("new pred not supported\n");                    skip_bits(&s->gb, 2); /* requested upstream message type */                    skip_bits1(&s->gb); /* newpred segment type */                }                s->reduced_res_vop= get_bits1(&s->gb);                if(s->reduced_res_vop) printf("reduced resolution VOP not supported\n");            }            else{                s->new_pred=0;                s->reduced_res_vop= 0;            }            s->scalability= get_bits1(&s->gb);            if (s->scalability) {                GetBitContext bak= s->gb;                int dummy= s->hierachy_type= get_bits1(&s->gb);                int ref_layer_id= get_bits(&s->gb, 4);                int ref_layer_sampling_dir= get_bits1(&s->gb);                int h_sampling_factor_n= get_bits(&s->gb, 5);                int h_sampling_factor_m= get_bits(&s->gb, 5);                int v_sampling_factor_n= get_bits(&s->gb, 5);                int v_sampling_factor_m= get_bits(&s->gb, 5);                s->enhancement_type= get_bits1(&s->gb);                                if(   h_sampling_factor_n==0 || h_sampling_factor_m==0                    || v_sampling_factor_n==0 || v_sampling_factor_m==0 || s->workaround_bugs==1){                   //                    fprintf(stderr, "illegal scalability header (VERY broken encoder), trying to workaround\n");                    s->scalability=0;                                       s->gb= bak;                    goto redo;                }                                // bin shape stuff FIXME                printf("scalability not supported\n");            }        }//printf("end Data %X %d\n", show_bits(&s->gb, 32), get_bits_count(&s->gb)&0x7);        goto redo;    } else if (startcode == 0x1b2) { //userdata        char buf[256];        int i;        int e;        int ver, build;//printf("user Data %X\n", show_bits(&s->gb, 32));        buf[0]= show_bits(&s->gb, 8);        for(i=1; i<256; i++){            buf[i]= show_bits(&s->gb, 16)&0xFF;            if(buf[i]==0) break;            skip_bits(&s->gb, 8);        }        buf[255]=0;        e=sscanf(buf, "DivX%dBuild%d", &ver, &build);        if(e!=2)            e=sscanf(buf, "DivX%db%d", &ver, &build);        if(e==2){            s->divx_version= ver;            s->divx_build= build;            if(s->picture_number==0){                printf("This file was encoded with DivX%d Build%d\n", ver, build);                if(ver==500 && build==413){                    printf("WARNING: this version of DivX is not MPEG4 compatible, trying to workaround these bugs...\n");                }            }        }//printf("User Data: %s\n", buf);        goto redo;    } else if (startcode != 0x1b6) { //VOP        goto redo;    }    s->pict_type = get_bits(&s->gb, 2) + I_TYPE;	/* pict type: I = 0 , P = 1 *///if(s->pict_type!=I_TYPE) return FRAME_SKIPED;    if(s->pict_type==B_TYPE && s->low_delay && s->vol_control_parameters==0){        printf("low_delay flag set, but shouldnt, clearing it\n");        s->low_delay=0;    }// printf("pic: %d, qpel:%d part:%d resync:%d\n", s->pict_type, s->quarter_sample, s->data_partitioning, s->resync_marker);         if(s->time_increment_resolution==0){        s->time_increment_resolution=1;//        fprintf(stderr, "time_increment_resolution is illegal\n");    }    time_incr=0;    while (get_bits1(&s->gb) != 0)         time_incr++;    check_marker(&s->gb, "before time_increment");    time_increment= get_bits(&s->gb, s->time_increment_bits);//printf(" type:%d modulo_time_base:%d increment:%d\n", s->pict_type, time_incr, time_increment);    if(s->pict_type!=B_TYPE){        s->last_time_base= s->time_base;        s->time_base+= time_incr;        s->time= s->time_base*s->time_increment_resolution + time_increment;        if(s->workaround_bugs==3 || s->avctx->fourcc == ff_get_fourcc("UMP4")){            if(s->time < s->last_non_b_time){//                fprintf(stderr, "header is not mpeg4 compatible, broken encoder, trying to workaround\n");                s->time_base++;                s->time+= s->time_increment_resolution;            }        }        s->pp_time= s->time - s->last_non_b_time;        s->last_non_b_time= s->time;    }else{        s->time= (s->last_time_base + time_incr)*s->time_increment_resolution + time_increment;        s->pb_time= s->pp_time - (s->last_non_b_time - s->time);        if(s->pp_time <=s->pb_time || s->pp_time <= s->pp_time - s->pb_time || s->pp_time<=0){//            printf("messed up order, seeking?, skiping current b frame\n");            return FRAME_SKIPED;        }                if(s->t_frame==0) s->t_frame= s->time - s->last_time_base;        if(s->t_frame==0) s->t_frame=1; // 1/0 protection//printf("%Ld %Ld %d %d\n", s->last_non_b_time, s->time, s->pp_time, s->t_frame); fflush(stdout);        s->pp_field_time= (  ROUNDED_DIV(s->last_non_b_time, s->t_frame)                            - ROUNDED_DIV(s->last_non_b_time - s->pp_time, s->t_frame))*2;        s->pb_field_time= (  ROUNDED_DIV(s->time, s->t_frame)                            - ROUNDED_DIV(s->last_non_b_time - s->pp_time, s->t_frame))*2;    }        s->avctx->pts= s->time*1000LL*1000LL / s->time_increment_resolution;        if(check_marker(&s->gb, "before vop_coded")==0 && s->picture_number==0){        printf("hmm, seems the headers arnt complete, trying to guess time_increment_bits\n");        for(s->time_increment_bits++ ;s->time_increment_bits<16; s->time_increment_bits++){            if(get_bits1(&s->gb)) break;        }        printf("my guess is %d bits ;)\n",s->time_increment_bits);    }    /* vop coded */    if (get_bits1(&s->gb) != 1)        goto redo;//printf("time %d %d %d || %Ld %Ld %Ld\n", s->time_increment_bits, s->time_increment_resolution, s->time_base,//s->time, s->last_non_b_time, s->last_non_b_time - s->pp_time);      if (s->shape != BIN_ONLY_SHAPE && ( s->pict_type == P_TYPE                          || (s->pict_type == S_TYPE && s->vol_sprite_usage==GMC_SPRITE))) {        /* rounding type for motion estimation */	s->no_rounding = get_bits1(&s->gb);    } else {	s->no_rounding = 0;    }//FIXME reduced res stuff     if (s->shape != RECT_SHAPE) {         if (s->vol_sprite_usage != 1 || s->pict_type != I_TYPE) {             int width, height, hor_spat_ref, ver_spat_ref;              width = get_bits(&s->gb, 13);             skip_bits1(&s->gb);   /* marker */             height = get_bits(&s->gb, 13);             skip_bits1(&s->gb);   /* marker */             hor_spat_ref = get_bits(&s->gb, 13); /* hor_spat_ref */             skip_bits1(&s->gb);   /* marker */             ver_spat_ref = get_bits(&s->gb, 13); /* ver_spat_ref */         }         skip_bits1(&s->gb); /* change_CR_disable */          if (get_bits1(&s->gb) != 0) {             skip_bits(&s->gb, 8); /* constant_alpha_value */         }     }//FIXME complexity estimation stuff          if (s->shape != BIN_ONLY_SHAPE) {         int t;         t=get_bits(&s->gb, 3); /* intra dc VLC threshold *///printf("threshold %d\n", t);         if(!s->progressive_sequence){             s->top_field_first= get_bits1(&s->gb);             s->alternate_scan= get_bits1(&s->gb);         }else             s->alternate_scan= 0;     }     if(s->pict_type == S_TYPE && (s->vol_sprite_usage==STATIC_SPRITE || s->vol_sprite_usage==GMC_SPRITE)){         if(s->num_sprite_warping_points){             mpeg4_decode_sprite_trajectory(s);         }         if(s->sprite_brightness_change) printf("sprite_brightness_change not supported\n");         if(s->vol_sprite_usage==STATIC_SPRITE) printf("static sprite not supported\n");     }     if (s->shape != BIN_ONLY_SHAPE) {         /* note: we do not use quant_precision to avoid problem if no            MPEG4 vol header as it is found on some old opendivx            movies */         s->qscale = get_bits(&s->gb, 5);         if(s->qscale==0){             printf("Error, header damaged or not MPEG4 header (qscale=0)\n");             return -1; // makes no sense to continue, as there is nothing left from the image then         }           if (s->pict_type != I_TYPE) {             s->f_code = get_bits(&s->gb, 3);	/* fcode_for */             if(s->f_code==0){                 printf("Error, header damaged or not MPEG4 header (f_code=0)\n");                 return -1; // makes no sense to continue, as the MV decoding will break very quickly             }         }else             s->f_code=1;              if (s->pict_type == B_TYPE) {             s->b_code = get_bits(&s->gb, 3);         }else             s->b_code=1;#if 0printf("qp:%d fc:%d bc:%d type:%s size:%d pro:%d alt:%d top:%d qpel:%d part:%d resync:%d\n",     s->qscale, s->f_code, s->b_code,     s->pict_type == I_TYPE ? "I" : (s->pict_type == P_TYPE ? "P" : (s->pict_type == B_TYPE ? "B" : "S")),     s->gb.size,s->progressive_sequence, s->alternate_scan, s->top_field_first,     s->quarter_sample, s->data_partitioning, s->resync_marker); #endif         if(!s->scalability){             if (s->shape!=RECT_SHAPE && s->pict_type!=I_TYPE) {                 skip_bits1(&s->gb); // vop shape coding type             }         }else{             if(s->enhancement_type){                 int load_backward_shape= get_bits1(&s->gb);                 if(load_backward_shape){                     printf("load backward shape isnt supported\n");                 }             }             skip_bits(&s->gb, 2); //ref_select_code         }     }     /* detect buggy encoders which dont set the low_delay flag (divx4/xvid/opendivx)*/     // note we cannot detect divx5 without b-frames easyly (allthough its buggy too)     if(s->vo_type==0 && s->vol_control_parameters==0 && s->divx_version==0 && s->picture_number==0){         printf("looks like this file was encoded with (divx4/(old)xvid/opendivx) -> forcing low_delay flag\n");         s->low_delay=1;     }     s->picture_number++; // better than pic number==0 allways ;)     s->y_dc_scale_table= ff_mpeg4_y_dc_scale_table; //FIXME add short header support      s->c_dc_scale_table= ff_mpeg4_c_dc_scale_table;     if(s->divx_version==0 || s->divx_version < 500){         s->h_edge_pos= s->width;         s->v_edge_pos= s->height;     }     return 0;}