00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #define UNCHECKED_BITSTREAM_READER 1
00024
00025 #include "libavutil/opt.h"
00026 #include "mpegvideo.h"
00027 #include "mpeg4video.h"
00028 #include "h263.h"
00029 #include "thread.h"
00030
00031
00032
00033
00034
00035 #define SPRITE_TRAJ_VLC_BITS 6
00036 #define DC_VLC_BITS 9
00037 #define MB_TYPE_B_VLC_BITS 4
00038
00039
00040 static VLC dc_lum, dc_chrom;
00041 static VLC sprite_trajectory;
00042 static VLC mb_type_b_vlc;
00043
00044 static const int mb_type_b_map[4]= {
00045 MB_TYPE_DIRECT2 | MB_TYPE_L0L1,
00046 MB_TYPE_L0L1 | MB_TYPE_16x16,
00047 MB_TYPE_L1 | MB_TYPE_16x16,
00048 MB_TYPE_L0 | MB_TYPE_16x16,
00049 };
00050
00056 void ff_mpeg4_pred_ac(MpegEncContext * s, DCTELEM *block, int n,
00057 int dir)
00058 {
00059 int i;
00060 int16_t *ac_val, *ac_val1;
00061 int8_t * const qscale_table = s->current_picture.f.qscale_table;
00062
00063
00064 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
00065 ac_val1 = ac_val;
00066 if (s->ac_pred) {
00067 if (dir == 0) {
00068 const int xy= s->mb_x-1 + s->mb_y*s->mb_stride;
00069
00070 ac_val -= 16;
00071
00072 if(s->mb_x==0 || s->qscale == qscale_table[xy] || n==1 || n==3){
00073
00074 for(i=1;i<8;i++) {
00075 block[s->dsp.idct_permutation[i<<3]] += ac_val[i];
00076 }
00077 }else{
00078
00079 for(i=1;i<8;i++) {
00080 block[s->dsp.idct_permutation[i<<3]] += ROUNDED_DIV(ac_val[i]*qscale_table[xy], s->qscale);
00081 }
00082 }
00083 } else {
00084 const int xy= s->mb_x + s->mb_y*s->mb_stride - s->mb_stride;
00085
00086 ac_val -= 16 * s->block_wrap[n];
00087
00088 if(s->mb_y==0 || s->qscale == qscale_table[xy] || n==2 || n==3){
00089
00090 for(i=1;i<8;i++) {
00091 block[s->dsp.idct_permutation[i]] += ac_val[i + 8];
00092 }
00093 }else{
00094
00095 for(i=1;i<8;i++) {
00096 block[s->dsp.idct_permutation[i]] += ROUNDED_DIV(ac_val[i + 8]*qscale_table[xy], s->qscale);
00097 }
00098 }
00099 }
00100 }
00101
00102 for(i=1;i<8;i++)
00103 ac_val1[i ] = block[s->dsp.idct_permutation[i<<3]];
00104
00105
00106 for(i=1;i<8;i++)
00107 ac_val1[8 + i] = block[s->dsp.idct_permutation[i ]];
00108
00109 }
00110
00115 static inline int mpeg4_is_resync(MpegEncContext *s){
00116 int bits_count= get_bits_count(&s->gb);
00117 int v= show_bits(&s->gb, 16);
00118
00119 if(s->workaround_bugs&FF_BUG_NO_PADDING && !s->resync_marker){
00120 return 0;
00121 }
00122
00123 while(v<=0xFF){
00124 if(s->pict_type==AV_PICTURE_TYPE_B || (v>>(8-s->pict_type)!=1) || s->partitioned_frame)
00125 break;
00126 skip_bits(&s->gb, 8+s->pict_type);
00127 bits_count+= 8+s->pict_type;
00128 v= show_bits(&s->gb, 16);
00129 }
00130
00131 if(bits_count + 8 >= s->gb.size_in_bits){
00132 v>>=8;
00133 v|= 0x7F >> (7-(bits_count&7));
00134
00135 if(v==0x7F)
00136 return s->mb_num;
00137 }else{
00138 if(v == ff_mpeg4_resync_prefix[bits_count&7]){
00139 int len, mb_num;
00140 int mb_num_bits= av_log2(s->mb_num - 1) + 1;
00141 GetBitContext gb= s->gb;
00142
00143 skip_bits(&s->gb, 1);
00144 align_get_bits(&s->gb);
00145
00146 for(len=0; len<32; len++){
00147 if(get_bits1(&s->gb)) break;
00148 }
00149
00150 mb_num= get_bits(&s->gb, mb_num_bits);
00151 if(!mb_num || mb_num > s->mb_num || get_bits_count(&s->gb)+6 > s->gb.size_in_bits)
00152 mb_num= -1;
00153
00154 s->gb= gb;
00155
00156 if(len>=ff_mpeg4_get_video_packet_prefix_length(s))
00157 return mb_num;
00158 }
00159 }
00160 return 0;
00161 }
00162
00163 static int mpeg4_decode_sprite_trajectory(MpegEncContext * s, GetBitContext *gb)
00164 {
00165 int i;
00166 int a= 2<<s->sprite_warping_accuracy;
00167 int rho= 3-s->sprite_warping_accuracy;
00168 int r=16/a;
00169 const int vop_ref[4][2]= {{0,0}, {s->width,0}, {0, s->height}, {s->width, s->height}};
00170 int d[4][2]={{0,0}, {0,0}, {0,0}, {0,0}};
00171 int sprite_ref[4][2];
00172 int virtual_ref[2][2];
00173 int w2, h2, w3, h3;
00174 int alpha=0, beta=0;
00175 int w= s->width;
00176 int h= s->height;
00177 int min_ab;
00178
00179 if(w<=0 || h<=0)
00180 return -1;
00181
00182 for(i=0; i<s->num_sprite_warping_points; i++){
00183 int length;
00184 int x=0, y=0;
00185
00186 length= get_vlc2(gb, sprite_trajectory.table, SPRITE_TRAJ_VLC_BITS, 3);
00187 if(length){
00188 x= get_xbits(gb, length);
00189 }
00190 if(!(s->divx_version==500 && s->divx_build==413)) skip_bits1(gb);
00191
00192 length= get_vlc2(gb, sprite_trajectory.table, SPRITE_TRAJ_VLC_BITS, 3);
00193 if(length){
00194 y=get_xbits(gb, length);
00195 }
00196 skip_bits1(gb);
00197 s->sprite_traj[i][0]= d[i][0]= x;
00198 s->sprite_traj[i][1]= d[i][1]= y;
00199 }
00200 for(; i<4; i++)
00201 s->sprite_traj[i][0]= s->sprite_traj[i][1]= 0;
00202
00203 while((1<<alpha)<w) alpha++;
00204 while((1<<beta )<h) beta++;
00205 w2= 1<<alpha;
00206 h2= 1<<beta;
00207
00208
00209 if(s->divx_version==500 && s->divx_build==413){
00210 sprite_ref[0][0]= a*vop_ref[0][0] + d[0][0];
00211 sprite_ref[0][1]= a*vop_ref[0][1] + d[0][1];
00212 sprite_ref[1][0]= a*vop_ref[1][0] + d[0][0] + d[1][0];
00213 sprite_ref[1][1]= a*vop_ref[1][1] + d[0][1] + d[1][1];
00214 sprite_ref[2][0]= a*vop_ref[2][0] + d[0][0] + d[2][0];
00215 sprite_ref[2][1]= a*vop_ref[2][1] + d[0][1] + d[2][1];
00216 } else {
00217 sprite_ref[0][0]= (a>>1)*(2*vop_ref[0][0] + d[0][0]);
00218 sprite_ref[0][1]= (a>>1)*(2*vop_ref[0][1] + d[0][1]);
00219 sprite_ref[1][0]= (a>>1)*(2*vop_ref[1][0] + d[0][0] + d[1][0]);
00220 sprite_ref[1][1]= (a>>1)*(2*vop_ref[1][1] + d[0][1] + d[1][1]);
00221 sprite_ref[2][0]= (a>>1)*(2*vop_ref[2][0] + d[0][0] + d[2][0]);
00222 sprite_ref[2][1]= (a>>1)*(2*vop_ref[2][1] + d[0][1] + d[2][1]);
00223 }
00224
00225
00226
00227
00228
00229
00230
00231 virtual_ref[0][0]= 16*(vop_ref[0][0] + w2)
00232 + ROUNDED_DIV(((w - w2)*(r*sprite_ref[0][0] - 16*vop_ref[0][0]) + w2*(r*sprite_ref[1][0] - 16*vop_ref[1][0])),w);
00233 virtual_ref[0][1]= 16*vop_ref[0][1]
00234 + ROUNDED_DIV(((w - w2)*(r*sprite_ref[0][1] - 16*vop_ref[0][1]) + w2*(r*sprite_ref[1][1] - 16*vop_ref[1][1])),w);
00235 virtual_ref[1][0]= 16*vop_ref[0][0]
00236 + ROUNDED_DIV(((h - h2)*(r*sprite_ref[0][0] - 16*vop_ref[0][0]) + h2*(r*sprite_ref[2][0] - 16*vop_ref[2][0])),h);
00237 virtual_ref[1][1]= 16*(vop_ref[0][1] + h2)
00238 + ROUNDED_DIV(((h - h2)*(r*sprite_ref[0][1] - 16*vop_ref[0][1]) + h2*(r*sprite_ref[2][1] - 16*vop_ref[2][1])),h);
00239
00240 switch(s->num_sprite_warping_points)
00241 {
00242 case 0:
00243 s->sprite_offset[0][0]= 0;
00244 s->sprite_offset[0][1]= 0;
00245 s->sprite_offset[1][0]= 0;
00246 s->sprite_offset[1][1]= 0;
00247 s->sprite_delta[0][0]= a;
00248 s->sprite_delta[0][1]= 0;
00249 s->sprite_delta[1][0]= 0;
00250 s->sprite_delta[1][1]= a;
00251 s->sprite_shift[0]= 0;
00252 s->sprite_shift[1]= 0;
00253 break;
00254 case 1:
00255 s->sprite_offset[0][0]= sprite_ref[0][0] - a*vop_ref[0][0];
00256 s->sprite_offset[0][1]= sprite_ref[0][1] - a*vop_ref[0][1];
00257 s->sprite_offset[1][0]= ((sprite_ref[0][0]>>1)|(sprite_ref[0][0]&1)) - a*(vop_ref[0][0]/2);
00258 s->sprite_offset[1][1]= ((sprite_ref[0][1]>>1)|(sprite_ref[0][1]&1)) - a*(vop_ref[0][1]/2);
00259 s->sprite_delta[0][0]= a;
00260 s->sprite_delta[0][1]= 0;
00261 s->sprite_delta[1][0]= 0;
00262 s->sprite_delta[1][1]= a;
00263 s->sprite_shift[0]= 0;
00264 s->sprite_shift[1]= 0;
00265 break;
00266 case 2:
00267 s->sprite_offset[0][0]= (sprite_ref[0][0]<<(alpha+rho))
00268 + (-r*sprite_ref[0][0] + virtual_ref[0][0])*(-vop_ref[0][0])
00269 + ( r*sprite_ref[0][1] - virtual_ref[0][1])*(-vop_ref[0][1])
00270 + (1<<(alpha+rho-1));
00271 s->sprite_offset[0][1]= (sprite_ref[0][1]<<(alpha+rho))
00272 + (-r*sprite_ref[0][1] + virtual_ref[0][1])*(-vop_ref[0][0])
00273 + (-r*sprite_ref[0][0] + virtual_ref[0][0])*(-vop_ref[0][1])
00274 + (1<<(alpha+rho-1));
00275 s->sprite_offset[1][0]= ( (-r*sprite_ref[0][0] + virtual_ref[0][0])*(-2*vop_ref[0][0] + 1)
00276 +( r*sprite_ref[0][1] - virtual_ref[0][1])*(-2*vop_ref[0][1] + 1)
00277 +2*w2*r*sprite_ref[0][0]
00278 - 16*w2
00279 + (1<<(alpha+rho+1)));
00280 s->sprite_offset[1][1]= ( (-r*sprite_ref[0][1] + virtual_ref[0][1])*(-2*vop_ref[0][0] + 1)
00281 +(-r*sprite_ref[0][0] + virtual_ref[0][0])*(-2*vop_ref[0][1] + 1)
00282 +2*w2*r*sprite_ref[0][1]
00283 - 16*w2
00284 + (1<<(alpha+rho+1)));
00285 s->sprite_delta[0][0]= (-r*sprite_ref[0][0] + virtual_ref[0][0]);
00286 s->sprite_delta[0][1]= (+r*sprite_ref[0][1] - virtual_ref[0][1]);
00287 s->sprite_delta[1][0]= (-r*sprite_ref[0][1] + virtual_ref[0][1]);
00288 s->sprite_delta[1][1]= (-r*sprite_ref[0][0] + virtual_ref[0][0]);
00289
00290 s->sprite_shift[0]= alpha+rho;
00291 s->sprite_shift[1]= alpha+rho+2;
00292 break;
00293 case 3:
00294 min_ab= FFMIN(alpha, beta);
00295 w3= w2>>min_ab;
00296 h3= h2>>min_ab;
00297 s->sprite_offset[0][0]= (sprite_ref[0][0]<<(alpha+beta+rho-min_ab))
00298 + (-r*sprite_ref[0][0] + virtual_ref[0][0])*h3*(-vop_ref[0][0])
00299 + (-r*sprite_ref[0][0] + virtual_ref[1][0])*w3*(-vop_ref[0][1])
00300 + (1<<(alpha+beta+rho-min_ab-1));
00301 s->sprite_offset[0][1]= (sprite_ref[0][1]<<(alpha+beta+rho-min_ab))
00302 + (-r*sprite_ref[0][1] + virtual_ref[0][1])*h3*(-vop_ref[0][0])
00303 + (-r*sprite_ref[0][1] + virtual_ref[1][1])*w3*(-vop_ref[0][1])
00304 + (1<<(alpha+beta+rho-min_ab-1));
00305 s->sprite_offset[1][0]= (-r*sprite_ref[0][0] + virtual_ref[0][0])*h3*(-2*vop_ref[0][0] + 1)
00306 + (-r*sprite_ref[0][0] + virtual_ref[1][0])*w3*(-2*vop_ref[0][1] + 1)
00307 + 2*w2*h3*r*sprite_ref[0][0]
00308 - 16*w2*h3
00309 + (1<<(alpha+beta+rho-min_ab+1));
00310 s->sprite_offset[1][1]= (-r*sprite_ref[0][1] + virtual_ref[0][1])*h3*(-2*vop_ref[0][0] + 1)
00311 + (-r*sprite_ref[0][1] + virtual_ref[1][1])*w3*(-2*vop_ref[0][1] + 1)
00312 + 2*w2*h3*r*sprite_ref[0][1]
00313 - 16*w2*h3
00314 + (1<<(alpha+beta+rho-min_ab+1));
00315 s->sprite_delta[0][0]= (-r*sprite_ref[0][0] + virtual_ref[0][0])*h3;
00316 s->sprite_delta[0][1]= (-r*sprite_ref[0][0] + virtual_ref[1][0])*w3;
00317 s->sprite_delta[1][0]= (-r*sprite_ref[0][1] + virtual_ref[0][1])*h3;
00318 s->sprite_delta[1][1]= (-r*sprite_ref[0][1] + virtual_ref[1][1])*w3;
00319
00320 s->sprite_shift[0]= alpha + beta + rho - min_ab;
00321 s->sprite_shift[1]= alpha + beta + rho - min_ab + 2;
00322 break;
00323 }
00324
00325 if( s->sprite_delta[0][0] == a<<s->sprite_shift[0]
00326 && s->sprite_delta[0][1] == 0
00327 && s->sprite_delta[1][0] == 0
00328 && s->sprite_delta[1][1] == a<<s->sprite_shift[0])
00329 {
00330 s->sprite_offset[0][0]>>=s->sprite_shift[0];
00331 s->sprite_offset[0][1]>>=s->sprite_shift[0];
00332 s->sprite_offset[1][0]>>=s->sprite_shift[1];
00333 s->sprite_offset[1][1]>>=s->sprite_shift[1];
00334 s->sprite_delta[0][0]= a;
00335 s->sprite_delta[0][1]= 0;
00336 s->sprite_delta[1][0]= 0;
00337 s->sprite_delta[1][1]= a;
00338 s->sprite_shift[0]= 0;
00339 s->sprite_shift[1]= 0;
00340 s->real_sprite_warping_points=1;
00341 }
00342 else{
00343 int shift_y= 16 - s->sprite_shift[0];
00344 int shift_c= 16 - s->sprite_shift[1];
00345 for(i=0; i<2; i++){
00346 s->sprite_offset[0][i]<<= shift_y;
00347 s->sprite_offset[1][i]<<= shift_c;
00348 s->sprite_delta[0][i]<<= shift_y;
00349 s->sprite_delta[1][i]<<= shift_y;
00350 s->sprite_shift[i]= 16;
00351 }
00352 s->real_sprite_warping_points= s->num_sprite_warping_points;
00353 }
00354 return 0;
00355 }
00356
00361 int ff_mpeg4_decode_video_packet_header(MpegEncContext *s)
00362 {
00363 int mb_num_bits= av_log2(s->mb_num - 1) + 1;
00364 int header_extension=0, mb_num, len;
00365
00366
00367 if( get_bits_count(&s->gb) > s->gb.size_in_bits-20) return -1;
00368
00369 for(len=0; len<32; len++){
00370 if(get_bits1(&s->gb)) break;
00371 }
00372
00373 if(len!=ff_mpeg4_get_video_packet_prefix_length(s)){
00374 av_log(s->avctx, AV_LOG_ERROR, "marker does not match f_code\n");
00375 return -1;
00376 }
00377
00378 if(s->shape != RECT_SHAPE){
00379 header_extension= get_bits1(&s->gb);
00380
00381 }
00382
00383 mb_num= get_bits(&s->gb, mb_num_bits);
00384 if(mb_num>=s->mb_num){
00385 av_log(s->avctx, AV_LOG_ERROR, "illegal mb_num in video packet (%d %d) \n", mb_num, s->mb_num);
00386 return -1;
00387 }
00388
00389 s->mb_x= mb_num % s->mb_width;
00390 s->mb_y= mb_num / s->mb_width;
00391
00392 if(s->shape != BIN_ONLY_SHAPE){
00393 int qscale= get_bits(&s->gb, s->quant_precision);
00394 if(qscale)
00395 s->chroma_qscale=s->qscale= qscale;
00396 }
00397
00398 if(s->shape == RECT_SHAPE){
00399 header_extension= get_bits1(&s->gb);
00400 }
00401 if(header_extension){
00402 int time_incr=0;
00403
00404 while (get_bits1(&s->gb) != 0)
00405 time_incr++;
00406
00407 check_marker(&s->gb, "before time_increment in video packed header");
00408 skip_bits(&s->gb, s->time_increment_bits);
00409 check_marker(&s->gb, "before vop_coding_type in video packed header");
00410
00411 skip_bits(&s->gb, 2);
00412
00413
00414 if(s->shape != BIN_ONLY_SHAPE){
00415 skip_bits(&s->gb, 3);
00416
00417 if(s->pict_type == AV_PICTURE_TYPE_S && s->vol_sprite_usage==GMC_SPRITE){
00418 if(mpeg4_decode_sprite_trajectory(s, &s->gb) < 0)
00419 return -1;
00420 av_log(s->avctx, AV_LOG_ERROR, "untested\n");
00421 }
00422
00423
00424
00425 if (s->pict_type != AV_PICTURE_TYPE_I) {
00426 int f_code = get_bits(&s->gb, 3);
00427 if(f_code==0){
00428 av_log(s->avctx, AV_LOG_ERROR, "Error, video packet header damaged (f_code=0)\n");
00429 }
00430 }
00431 if (s->pict_type == AV_PICTURE_TYPE_B) {
00432 int b_code = get_bits(&s->gb, 3);
00433 if(b_code==0){
00434 av_log(s->avctx, AV_LOG_ERROR, "Error, video packet header damaged (b_code=0)\n");
00435 }
00436 }
00437 }
00438 }
00439
00440
00441 return 0;
00442 }
00443
00449 static inline int get_amv(MpegEncContext *s, int n){
00450 int x, y, mb_v, sum, dx, dy, shift;
00451 int len = 1 << (s->f_code + 4);
00452 const int a= s->sprite_warping_accuracy;
00453
00454 if(s->workaround_bugs & FF_BUG_AMV)
00455 len >>= s->quarter_sample;
00456
00457 if(s->real_sprite_warping_points==1){
00458 if(s->divx_version==500 && s->divx_build==413)
00459 sum= s->sprite_offset[0][n] / (1<<(a - s->quarter_sample));
00460 else
00461 sum= RSHIFT(s->sprite_offset[0][n]<<s->quarter_sample, a);
00462 }else{
00463 dx= s->sprite_delta[n][0];
00464 dy= s->sprite_delta[n][1];
00465 shift= s->sprite_shift[0];
00466 if(n) dy -= 1<<(shift + a + 1);
00467 else dx -= 1<<(shift + a + 1);
00468 mb_v= s->sprite_offset[0][n] + dx*s->mb_x*16 + dy*s->mb_y*16;
00469
00470 sum=0;
00471 for(y=0; y<16; y++){
00472 int v;
00473
00474 v= mb_v + dy*y;
00475
00476 for(x=0; x<16; x++){
00477 sum+= v>>shift;
00478 v+= dx;
00479 }
00480 }
00481 sum= RSHIFT(sum, a+8-s->quarter_sample);
00482 }
00483
00484 if (sum < -len) sum= -len;
00485 else if (sum >= len) sum= len-1;
00486
00487 return sum;
00488 }
00489
00496 static inline int mpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr)
00497 {
00498 int level, code;
00499
00500 if (n < 4)
00501 code = get_vlc2(&s->gb, dc_lum.table, DC_VLC_BITS, 1);
00502 else
00503 code = get_vlc2(&s->gb, dc_chrom.table, DC_VLC_BITS, 1);
00504 if (code < 0 || code > 9 ){
00505 av_log(s->avctx, AV_LOG_ERROR, "illegal dc vlc\n");
00506 return -1;
00507 }
00508 if (code == 0) {
00509 level = 0;
00510 } else {
00511 if(IS_3IV1){
00512 if(code==1)
00513 level= 2*get_bits1(&s->gb)-1;
00514 else{
00515 if(get_bits1(&s->gb))
00516 level = get_bits(&s->gb, code-1) + (1<<(code-1));
00517 else
00518 level = -get_bits(&s->gb, code-1) - (1<<(code-1));
00519 }
00520 }else{
00521 level = get_xbits(&s->gb, code);
00522 }
00523
00524 if (code > 8){
00525 if(get_bits1(&s->gb)==0){
00526 if(s->err_recognition&(AV_EF_BITSTREAM|AV_EF_COMPLIANT)){
00527 av_log(s->avctx, AV_LOG_ERROR, "dc marker bit missing\n");
00528 return -1;
00529 }
00530 }
00531 }
00532 }
00533
00534 return ff_mpeg4_pred_dc(s, n, level, dir_ptr, 0);
00535 }
00536
00541 static int mpeg4_decode_partition_a(MpegEncContext *s){
00542 int mb_num;
00543 static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
00544
00545
00546 mb_num=0;
00547 s->first_slice_line=1;
00548 for(; s->mb_y<s->mb_height; s->mb_y++){
00549 ff_init_block_index(s);
00550 for(; s->mb_x<s->mb_width; s->mb_x++){
00551 const int xy= s->mb_x + s->mb_y*s->mb_stride;
00552 int cbpc;
00553 int dir=0;
00554
00555 mb_num++;
00556 ff_update_block_index(s);
00557 if(s->mb_x == s->resync_mb_x && s->mb_y == s->resync_mb_y+1)
00558 s->first_slice_line=0;
00559
00560 if(s->pict_type==AV_PICTURE_TYPE_I){
00561 int i;
00562
00563 do{
00564 if(show_bits_long(&s->gb, 19)==DC_MARKER){
00565 return mb_num-1;
00566 }
00567
00568 cbpc = get_vlc2(&s->gb, ff_h263_intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 2);
00569 if (cbpc < 0){
00570 av_log(s->avctx, AV_LOG_ERROR, "cbpc corrupted at %d %d\n", s->mb_x, s->mb_y);
00571 return -1;
00572 }
00573 }while(cbpc == 8);
00574
00575 s->cbp_table[xy]= cbpc & 3;
00576 s->current_picture.f.mb_type[xy] = MB_TYPE_INTRA;
00577 s->mb_intra = 1;
00578
00579 if(cbpc & 4) {
00580 ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
00581 }
00582 s->current_picture.f.qscale_table[xy]= s->qscale;
00583
00584 s->mbintra_table[xy]= 1;
00585 for(i=0; i<6; i++){
00586 int dc_pred_dir;
00587 int dc= mpeg4_decode_dc(s, i, &dc_pred_dir);
00588 if(dc < 0){
00589 av_log(s->avctx, AV_LOG_ERROR, "DC corrupted at %d %d\n", s->mb_x, s->mb_y);
00590 return -1;
00591 }
00592 dir<<=1;
00593 if(dc_pred_dir) dir|=1;
00594 }
00595 s->pred_dir_table[xy]= dir;
00596 }else{
00597 int mx, my, pred_x, pred_y, bits;
00598 int16_t * const mot_val = s->current_picture.f.motion_val[0][s->block_index[0]];
00599 const int stride= s->b8_stride*2;
00600
00601 try_again:
00602 bits= show_bits(&s->gb, 17);
00603 if(bits==MOTION_MARKER){
00604 return mb_num-1;
00605 }
00606 skip_bits1(&s->gb);
00607 if(bits&0x10000){
00608
00609 if(s->pict_type==AV_PICTURE_TYPE_S && s->vol_sprite_usage==GMC_SPRITE){
00610 s->current_picture.f.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_GMC | MB_TYPE_L0;
00611 mx= get_amv(s, 0);
00612 my= get_amv(s, 1);
00613 }else{
00614 s->current_picture.f.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
00615 mx=my=0;
00616 }
00617 mot_val[0 ]= mot_val[2 ]=
00618 mot_val[0+stride]= mot_val[2+stride]= mx;
00619 mot_val[1 ]= mot_val[3 ]=
00620 mot_val[1+stride]= mot_val[3+stride]= my;
00621
00622 if(s->mbintra_table[xy])
00623 ff_clean_intra_table_entries(s);
00624 continue;
00625 }
00626
00627 cbpc = get_vlc2(&s->gb, ff_h263_inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
00628 if (cbpc < 0){
00629 av_log(s->avctx, AV_LOG_ERROR, "cbpc corrupted at %d %d\n", s->mb_x, s->mb_y);
00630 return -1;
00631 }
00632 if(cbpc == 20)
00633 goto try_again;
00634
00635 s->cbp_table[xy]= cbpc&(8+3);
00636
00637 s->mb_intra = ((cbpc & 4) != 0);
00638
00639 if(s->mb_intra){
00640 s->current_picture.f.mb_type[xy] = MB_TYPE_INTRA;
00641 s->mbintra_table[xy]= 1;
00642 mot_val[0 ]= mot_val[2 ]=
00643 mot_val[0+stride]= mot_val[2+stride]= 0;
00644 mot_val[1 ]= mot_val[3 ]=
00645 mot_val[1+stride]= mot_val[3+stride]= 0;
00646 }else{
00647 if(s->mbintra_table[xy])
00648 ff_clean_intra_table_entries(s);
00649
00650 if(s->pict_type==AV_PICTURE_TYPE_S && s->vol_sprite_usage==GMC_SPRITE && (cbpc & 16) == 0)
00651 s->mcsel= get_bits1(&s->gb);
00652 else s->mcsel= 0;
00653
00654 if ((cbpc & 16) == 0) {
00655
00656
00657 ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
00658 if(!s->mcsel){
00659 mx = ff_h263_decode_motion(s, pred_x, s->f_code);
00660 if (mx >= 0xffff)
00661 return -1;
00662
00663 my = ff_h263_decode_motion(s, pred_y, s->f_code);
00664 if (my >= 0xffff)
00665 return -1;
00666 s->current_picture.f.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_L0;
00667 } else {
00668 mx = get_amv(s, 0);
00669 my = get_amv(s, 1);
00670 s->current_picture.f.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_GMC | MB_TYPE_L0;
00671 }
00672
00673 mot_val[0 ]= mot_val[2 ] =
00674 mot_val[0+stride]= mot_val[2+stride]= mx;
00675 mot_val[1 ]= mot_val[3 ]=
00676 mot_val[1+stride]= mot_val[3+stride]= my;
00677 } else {
00678 int i;
00679 s->current_picture.f.mb_type[xy] = MB_TYPE_8x8 | MB_TYPE_L0;
00680 for(i=0;i<4;i++) {
00681 int16_t *mot_val= ff_h263_pred_motion(s, i, 0, &pred_x, &pred_y);
00682 mx = ff_h263_decode_motion(s, pred_x, s->f_code);
00683 if (mx >= 0xffff)
00684 return -1;
00685
00686 my = ff_h263_decode_motion(s, pred_y, s->f_code);
00687 if (my >= 0xffff)
00688 return -1;
00689 mot_val[0] = mx;
00690 mot_val[1] = my;
00691 }
00692 }
00693 }
00694 }
00695 }
00696 s->mb_x= 0;
00697 }
00698
00699 return mb_num;
00700 }
00701
00706 static int mpeg4_decode_partition_b(MpegEncContext *s, int mb_count){
00707 int mb_num=0;
00708 static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
00709
00710 s->mb_x= s->resync_mb_x;
00711 s->first_slice_line=1;
00712 for(s->mb_y= s->resync_mb_y; mb_num < mb_count; s->mb_y++){
00713 ff_init_block_index(s);
00714 for(; mb_num < mb_count && s->mb_x<s->mb_width; s->mb_x++){
00715 const int xy= s->mb_x + s->mb_y*s->mb_stride;
00716
00717 mb_num++;
00718 ff_update_block_index(s);
00719 if(s->mb_x == s->resync_mb_x && s->mb_y == s->resync_mb_y+1)
00720 s->first_slice_line=0;
00721
00722 if(s->pict_type==AV_PICTURE_TYPE_I){
00723 int ac_pred= get_bits1(&s->gb);
00724 int cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
00725 if(cbpy<0){
00726 av_log(s->avctx, AV_LOG_ERROR, "cbpy corrupted at %d %d\n", s->mb_x, s->mb_y);
00727 return -1;
00728 }
00729
00730 s->cbp_table[xy]|= cbpy<<2;
00731 s->current_picture.f.mb_type[xy] |= ac_pred*MB_TYPE_ACPRED;
00732 }else{
00733 if (IS_INTRA(s->current_picture.f.mb_type[xy])) {
00734 int dir=0,i;
00735 int ac_pred = get_bits1(&s->gb);
00736 int cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
00737
00738 if(cbpy<0){
00739 av_log(s->avctx, AV_LOG_ERROR, "I cbpy corrupted at %d %d\n", s->mb_x, s->mb_y);
00740 return -1;
00741 }
00742
00743 if(s->cbp_table[xy] & 8) {
00744 ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
00745 }
00746 s->current_picture.f.qscale_table[xy] = s->qscale;
00747
00748 for(i=0; i<6; i++){
00749 int dc_pred_dir;
00750 int dc= mpeg4_decode_dc(s, i, &dc_pred_dir);
00751 if(dc < 0){
00752 av_log(s->avctx, AV_LOG_ERROR, "DC corrupted at %d %d\n", s->mb_x, s->mb_y);
00753 return -1;
00754 }
00755 dir<<=1;
00756 if(dc_pred_dir) dir|=1;
00757 }
00758 s->cbp_table[xy]&= 3;
00759 s->cbp_table[xy]|= cbpy<<2;
00760 s->current_picture.f.mb_type[xy] |= ac_pred*MB_TYPE_ACPRED;
00761 s->pred_dir_table[xy]= dir;
00762 } else if (IS_SKIP(s->current_picture.f.mb_type[xy])) {
00763 s->current_picture.f.qscale_table[xy] = s->qscale;
00764 s->cbp_table[xy]= 0;
00765 }else{
00766 int cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
00767
00768 if(cbpy<0){
00769 av_log(s->avctx, AV_LOG_ERROR, "P cbpy corrupted at %d %d\n", s->mb_x, s->mb_y);
00770 return -1;
00771 }
00772
00773 if(s->cbp_table[xy] & 8) {
00774 ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
00775 }
00776 s->current_picture.f.qscale_table[xy] = s->qscale;
00777
00778 s->cbp_table[xy]&= 3;
00779 s->cbp_table[xy]|= (cbpy^0xf)<<2;
00780 }
00781 }
00782 }
00783 if(mb_num >= mb_count) return 0;
00784 s->mb_x= 0;
00785 }
00786 return 0;
00787 }
00788
00793 int ff_mpeg4_decode_partitions(MpegEncContext *s)
00794 {
00795 int mb_num;
00796 const int part_a_error= s->pict_type==AV_PICTURE_TYPE_I ? (ER_DC_ERROR|ER_MV_ERROR) : ER_MV_ERROR;
00797 const int part_a_end = s->pict_type==AV_PICTURE_TYPE_I ? (ER_DC_END |ER_MV_END) : ER_MV_END;
00798
00799 mb_num= mpeg4_decode_partition_a(s);
00800 if(mb_num<0){
00801 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, part_a_error);
00802 return -1;
00803 }
00804
00805 if(s->resync_mb_x + s->resync_mb_y*s->mb_width + mb_num > s->mb_num){
00806 av_log(s->avctx, AV_LOG_ERROR, "slice below monitor ...\n");
00807 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, part_a_error);
00808 return -1;
00809 }
00810
00811 s->mb_num_left= mb_num;
00812
00813 if(s->pict_type==AV_PICTURE_TYPE_I){
00814 while(show_bits(&s->gb, 9) == 1)
00815 skip_bits(&s->gb, 9);
00816 if(get_bits_long(&s->gb, 19)!=DC_MARKER){
00817 av_log(s->avctx, AV_LOG_ERROR, "marker missing after first I partition at %d %d\n", s->mb_x, s->mb_y);
00818 return -1;
00819 }
00820 }else{
00821 while(show_bits(&s->gb, 10) == 1)
00822 skip_bits(&s->gb, 10);
00823 if(get_bits(&s->gb, 17)!=MOTION_MARKER){
00824 av_log(s->avctx, AV_LOG_ERROR, "marker missing after first P partition at %d %d\n", s->mb_x, s->mb_y);
00825 return -1;
00826 }
00827 }
00828 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, part_a_end);
00829
00830 if( mpeg4_decode_partition_b(s, mb_num) < 0){
00831 if(s->pict_type==AV_PICTURE_TYPE_P)
00832 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, ER_DC_ERROR);
00833 return -1;
00834 }else{
00835 if(s->pict_type==AV_PICTURE_TYPE_P)
00836 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, ER_DC_END);
00837 }
00838
00839 return 0;
00840 }
00841
00846 static inline int mpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
00847 int n, int coded, int intra, int rvlc)
00848 {
00849 int level, i, last, run;
00850 int av_uninit(dc_pred_dir);
00851 RLTable * rl;
00852 RL_VLC_ELEM * rl_vlc;
00853 const uint8_t * scan_table;
00854 int qmul, qadd;
00855
00856
00857
00858 if(intra) {
00859 if(s->use_intra_dc_vlc){
00860
00861 if(s->partitioned_frame){
00862 level = s->dc_val[0][ s->block_index[n] ];
00863 if(n<4) level= FASTDIV((level + (s->y_dc_scale>>1)), s->y_dc_scale);
00864 else level= FASTDIV((level + (s->c_dc_scale>>1)), s->c_dc_scale);
00865 dc_pred_dir= (s->pred_dir_table[s->mb_x + s->mb_y*s->mb_stride]<<n)&32;
00866 }else{
00867 level = mpeg4_decode_dc(s, n, &dc_pred_dir);
00868 if (level < 0)
00869 return -1;
00870 }
00871 block[0] = level;
00872 i = 0;
00873 }else{
00874 i = -1;
00875 ff_mpeg4_pred_dc(s, n, 0, &dc_pred_dir, 0);
00876 }
00877 if (!coded)
00878 goto not_coded;
00879
00880 if(rvlc){
00881 rl = &ff_rvlc_rl_intra;
00882 rl_vlc = ff_rvlc_rl_intra.rl_vlc[0];
00883 }else{
00884 rl = &ff_mpeg4_rl_intra;
00885 rl_vlc = ff_mpeg4_rl_intra.rl_vlc[0];
00886 }
00887 if (s->ac_pred) {
00888 if (dc_pred_dir == 0)
00889 scan_table = s->intra_v_scantable.permutated;
00890 else
00891 scan_table = s->intra_h_scantable.permutated;
00892 } else {
00893 scan_table = s->intra_scantable.permutated;
00894 }
00895 qmul=1;
00896 qadd=0;
00897 } else {
00898 i = -1;
00899 if (!coded) {
00900 s->block_last_index[n] = i;
00901 return 0;
00902 }
00903 if(rvlc) rl = &ff_rvlc_rl_inter;
00904 else rl = &ff_h263_rl_inter;
00905
00906 scan_table = s->intra_scantable.permutated;
00907
00908 if(s->mpeg_quant){
00909 qmul=1;
00910 qadd=0;
00911 if(rvlc){
00912 rl_vlc = ff_rvlc_rl_inter.rl_vlc[0];
00913 }else{
00914 rl_vlc = ff_h263_rl_inter.rl_vlc[0];
00915 }
00916 }else{
00917 qmul = s->qscale << 1;
00918 qadd = (s->qscale - 1) | 1;
00919 if(rvlc){
00920 rl_vlc = ff_rvlc_rl_inter.rl_vlc[s->qscale];
00921 }else{
00922 rl_vlc = ff_h263_rl_inter.rl_vlc[s->qscale];
00923 }
00924 }
00925 }
00926 {
00927 OPEN_READER(re, &s->gb);
00928 for(;;) {
00929 UPDATE_CACHE(re, &s->gb);
00930 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 0);
00931 if (level==0) {
00932
00933 if(rvlc){
00934 if(SHOW_UBITS(re, &s->gb, 1)==0){
00935 av_log(s->avctx, AV_LOG_ERROR, "1. marker bit missing in rvlc esc\n");
00936 return -1;
00937 }; SKIP_CACHE(re, &s->gb, 1);
00938
00939 last= SHOW_UBITS(re, &s->gb, 1); SKIP_CACHE(re, &s->gb, 1);
00940 run= SHOW_UBITS(re, &s->gb, 6);
00941 SKIP_COUNTER(re, &s->gb, 1+1+6);
00942 UPDATE_CACHE(re, &s->gb);
00943
00944 if(SHOW_UBITS(re, &s->gb, 1)==0){
00945 av_log(s->avctx, AV_LOG_ERROR, "2. marker bit missing in rvlc esc\n");
00946 return -1;
00947 }; SKIP_CACHE(re, &s->gb, 1);
00948
00949 level= SHOW_UBITS(re, &s->gb, 11); SKIP_CACHE(re, &s->gb, 11);
00950
00951 if(SHOW_UBITS(re, &s->gb, 5)!=0x10){
00952 av_log(s->avctx, AV_LOG_ERROR, "reverse esc missing\n");
00953 return -1;
00954 }; SKIP_CACHE(re, &s->gb, 5);
00955
00956 level= level * qmul + qadd;
00957 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
00958 SKIP_COUNTER(re, &s->gb, 1+11+5+1);
00959
00960 i+= run + 1;
00961 if(last) i+=192;
00962 }else{
00963 int cache;
00964 cache= GET_CACHE(re, &s->gb);
00965
00966 if(IS_3IV1)
00967 cache ^= 0xC0000000;
00968
00969 if (cache&0x80000000) {
00970 if (cache&0x40000000) {
00971
00972 SKIP_CACHE(re, &s->gb, 2);
00973 last= SHOW_UBITS(re, &s->gb, 1); SKIP_CACHE(re, &s->gb, 1);
00974 run= SHOW_UBITS(re, &s->gb, 6);
00975 SKIP_COUNTER(re, &s->gb, 2+1+6);
00976 UPDATE_CACHE(re, &s->gb);
00977
00978 if(IS_3IV1){
00979 level= SHOW_SBITS(re, &s->gb, 12); LAST_SKIP_BITS(re, &s->gb, 12);
00980 }else{
00981 if(SHOW_UBITS(re, &s->gb, 1)==0){
00982 av_log(s->avctx, AV_LOG_ERROR, "1. marker bit missing in 3. esc\n");
00983 return -1;
00984 }; SKIP_CACHE(re, &s->gb, 1);
00985
00986 level= SHOW_SBITS(re, &s->gb, 12); SKIP_CACHE(re, &s->gb, 12);
00987
00988 if(SHOW_UBITS(re, &s->gb, 1)==0){
00989 av_log(s->avctx, AV_LOG_ERROR, "2. marker bit missing in 3. esc\n");
00990 return -1;
00991 }
00992
00993 SKIP_COUNTER(re, &s->gb, 1+12+1);
00994 }
00995
00996 #if 0
00997 if(s->error_recognition >= FF_ER_COMPLIANT){
00998 const int abs_level= FFABS(level);
00999 if(abs_level<=MAX_LEVEL && run<=MAX_RUN){
01000 const int run1= run - rl->max_run[last][abs_level] - 1;
01001 if(abs_level <= rl->max_level[last][run]){
01002 av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, vlc encoding possible\n");
01003 return -1;
01004 }
01005 if(s->error_recognition > FF_ER_COMPLIANT){
01006 if(abs_level <= rl->max_level[last][run]*2){
01007 av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 1 encoding possible\n");
01008 return -1;
01009 }
01010 if(run1 >= 0 && abs_level <= rl->max_level[last][run1]){
01011 av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 2 encoding possible\n");
01012 return -1;
01013 }
01014 }
01015 }
01016 }
01017 #endif
01018 if (level>0) level= level * qmul + qadd;
01019 else level= level * qmul - qadd;
01020
01021 if((unsigned)(level + 2048) > 4095){
01022 if(s->err_recognition & (AV_EF_BITSTREAM|AV_EF_AGGRESSIVE)){
01023 if(level > 2560 || level<-2560){
01024 av_log(s->avctx, AV_LOG_ERROR, "|level| overflow in 3. esc, qp=%d\n", s->qscale);
01025 return -1;
01026 }
01027 }
01028 level= level<0 ? -2048 : 2047;
01029 }
01030
01031 i+= run + 1;
01032 if(last) i+=192;
01033 } else {
01034
01035 SKIP_BITS(re, &s->gb, 2);
01036 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
01037 i+= run + rl->max_run[run>>7][level/qmul] +1;
01038 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
01039 LAST_SKIP_BITS(re, &s->gb, 1);
01040 }
01041 } else {
01042
01043 SKIP_BITS(re, &s->gb, 1);
01044 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
01045 i+= run;
01046 level = level + rl->max_level[run>>7][(run-1)&63] * qmul;
01047 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
01048 LAST_SKIP_BITS(re, &s->gb, 1);
01049 }
01050 }
01051 } else {
01052 i+= run;
01053 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
01054 LAST_SKIP_BITS(re, &s->gb, 1);
01055 }
01056 if (i > 62){
01057 i-= 192;
01058 if(i&(~63)){
01059 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
01060 return -1;
01061 }
01062
01063 block[scan_table[i]] = level;
01064 break;
01065 }
01066
01067 block[scan_table[i]] = level;
01068 }
01069 CLOSE_READER(re, &s->gb);
01070 }
01071 not_coded:
01072 if (intra) {
01073 if(!s->use_intra_dc_vlc){
01074 block[0] = ff_mpeg4_pred_dc(s, n, block[0], &dc_pred_dir, 0);
01075
01076 i -= i>>31;
01077 }
01078
01079 ff_mpeg4_pred_ac(s, block, n, dc_pred_dir);
01080 if (s->ac_pred) {
01081 i = 63;
01082 }
01083 }
01084 s->block_last_index[n] = i;
01085 return 0;
01086 }
01087
01092 static int mpeg4_decode_partitioned_mb(MpegEncContext *s, DCTELEM block[6][64])
01093 {
01094 int cbp, mb_type;
01095 const int xy= s->mb_x + s->mb_y*s->mb_stride;
01096
01097 mb_type = s->current_picture.f.mb_type[xy];
01098 cbp = s->cbp_table[xy];
01099
01100 s->use_intra_dc_vlc= s->qscale < s->intra_dc_threshold;
01101
01102 if (s->current_picture.f.qscale_table[xy] != s->qscale) {
01103 ff_set_qscale(s, s->current_picture.f.qscale_table[xy]);
01104 }
01105
01106 if (s->pict_type == AV_PICTURE_TYPE_P || s->pict_type==AV_PICTURE_TYPE_S) {
01107 int i;
01108 for(i=0; i<4; i++){
01109 s->mv[0][i][0] = s->current_picture.f.motion_val[0][s->block_index[i]][0];
01110 s->mv[0][i][1] = s->current_picture.f.motion_val[0][s->block_index[i]][1];
01111 }
01112 s->mb_intra = IS_INTRA(mb_type);
01113
01114 if (IS_SKIP(mb_type)) {
01115
01116 for(i=0;i<6;i++)
01117 s->block_last_index[i] = -1;
01118 s->mv_dir = MV_DIR_FORWARD;
01119 s->mv_type = MV_TYPE_16X16;
01120 if(s->pict_type==AV_PICTURE_TYPE_S && s->vol_sprite_usage==GMC_SPRITE){
01121 s->mcsel=1;
01122 s->mb_skipped = 0;
01123 }else{
01124 s->mcsel=0;
01125 s->mb_skipped = 1;
01126 }
01127 }else if(s->mb_intra){
01128 s->ac_pred = IS_ACPRED(s->current_picture.f.mb_type[xy]);
01129 }else if(!s->mb_intra){
01130
01131
01132 s->mv_dir = MV_DIR_FORWARD;
01133 if (IS_8X8(mb_type)) {
01134 s->mv_type = MV_TYPE_8X8;
01135 } else {
01136 s->mv_type = MV_TYPE_16X16;
01137 }
01138 }
01139 } else {
01140 s->mb_intra = 1;
01141 s->ac_pred = IS_ACPRED(s->current_picture.f.mb_type[xy]);
01142 }
01143
01144 if (!IS_SKIP(mb_type)) {
01145 int i;
01146 s->dsp.clear_blocks(s->block[0]);
01147
01148 for (i = 0; i < 6; i++) {
01149 if(mpeg4_decode_block(s, block[i], i, cbp&32, s->mb_intra, s->rvlc) < 0){
01150 av_log(s->avctx, AV_LOG_ERROR, "texture corrupted at %d %d %d\n", s->mb_x, s->mb_y, s->mb_intra);
01151 return -1;
01152 }
01153 cbp+=cbp;
01154 }
01155 }
01156
01157
01158
01159 if(--s->mb_num_left <= 0){
01160 if(mpeg4_is_resync(s))
01161 return SLICE_END;
01162 else
01163 return SLICE_NOEND;
01164 }else{
01165 if(mpeg4_is_resync(s)){
01166 const int delta= s->mb_x + 1 == s->mb_width ? 2 : 1;
01167 if(s->cbp_table[xy+delta])
01168 return SLICE_END;
01169 }
01170 return SLICE_OK;
01171 }
01172 }
01173
01174 static int mpeg4_decode_mb(MpegEncContext *s,
01175 DCTELEM block[6][64])
01176 {
01177 int cbpc, cbpy, i, cbp, pred_x, pred_y, mx, my, dquant;
01178 int16_t *mot_val;
01179 static int8_t quant_tab[4] = { -1, -2, 1, 2 };
01180 const int xy= s->mb_x + s->mb_y * s->mb_stride;
01181
01182 av_assert2(s->h263_pred);
01183
01184 if (s->pict_type == AV_PICTURE_TYPE_P || s->pict_type==AV_PICTURE_TYPE_S) {
01185 do{
01186 if (get_bits1(&s->gb)) {
01187
01188 s->mb_intra = 0;
01189 for(i=0;i<6;i++)
01190 s->block_last_index[i] = -1;
01191 s->mv_dir = MV_DIR_FORWARD;
01192 s->mv_type = MV_TYPE_16X16;
01193 if(s->pict_type==AV_PICTURE_TYPE_S && s->vol_sprite_usage==GMC_SPRITE){
01194 s->current_picture.f.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_GMC | MB_TYPE_16x16 | MB_TYPE_L0;
01195 s->mcsel=1;
01196 s->mv[0][0][0]= get_amv(s, 0);
01197 s->mv[0][0][1]= get_amv(s, 1);
01198
01199 s->mb_skipped = 0;
01200 }else{
01201 s->current_picture.f.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
01202 s->mcsel=0;
01203 s->mv[0][0][0] = 0;
01204 s->mv[0][0][1] = 0;
01205 s->mb_skipped = 1;
01206 }
01207 goto end;
01208 }
01209 cbpc = get_vlc2(&s->gb, ff_h263_inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
01210 if (cbpc < 0){
01211 av_log(s->avctx, AV_LOG_ERROR, "cbpc damaged at %d %d\n", s->mb_x, s->mb_y);
01212 return -1;
01213 }
01214 }while(cbpc == 20);
01215
01216 s->dsp.clear_blocks(s->block[0]);
01217 dquant = cbpc & 8;
01218 s->mb_intra = ((cbpc & 4) != 0);
01219 if (s->mb_intra) goto intra;
01220
01221 if(s->pict_type==AV_PICTURE_TYPE_S && s->vol_sprite_usage==GMC_SPRITE && (cbpc & 16) == 0)
01222 s->mcsel= get_bits1(&s->gb);
01223 else s->mcsel= 0;
01224 cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1) ^ 0x0F;
01225
01226 cbp = (cbpc & 3) | (cbpy << 2);
01227 if (dquant) {
01228 ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
01229 }
01230 if((!s->progressive_sequence) && (cbp || (s->workaround_bugs&FF_BUG_XVID_ILACE)))
01231 s->interlaced_dct= get_bits1(&s->gb);
01232
01233 s->mv_dir = MV_DIR_FORWARD;
01234 if ((cbpc & 16) == 0) {
01235 if(s->mcsel){
01236 s->current_picture.f.mb_type[xy] = MB_TYPE_GMC | MB_TYPE_16x16 | MB_TYPE_L0;
01237
01238 s->mv_type = MV_TYPE_16X16;
01239 mx= get_amv(s, 0);
01240 my= get_amv(s, 1);
01241 s->mv[0][0][0] = mx;
01242 s->mv[0][0][1] = my;
01243 }else if((!s->progressive_sequence) && get_bits1(&s->gb)){
01244 s->current_picture.f.mb_type[xy] = MB_TYPE_16x8 | MB_TYPE_L0 | MB_TYPE_INTERLACED;
01245
01246 s->mv_type= MV_TYPE_FIELD;
01247
01248 s->field_select[0][0]= get_bits1(&s->gb);
01249 s->field_select[0][1]= get_bits1(&s->gb);
01250
01251 ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
01252
01253 for(i=0; i<2; i++){
01254 mx = ff_h263_decode_motion(s, pred_x, s->f_code);
01255 if (mx >= 0xffff)
01256 return -1;
01257
01258 my = ff_h263_decode_motion(s, pred_y/2, s->f_code);
01259 if (my >= 0xffff)
01260 return -1;
01261
01262 s->mv[0][i][0] = mx;
01263 s->mv[0][i][1] = my;
01264 }
01265 }else{
01266 s->current_picture.f.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_L0;
01267
01268 s->mv_type = MV_TYPE_16X16;
01269 ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
01270 mx = ff_h263_decode_motion(s, pred_x, s->f_code);
01271
01272 if (mx >= 0xffff)
01273 return -1;
01274
01275 my = ff_h263_decode_motion(s, pred_y, s->f_code);
01276
01277 if (my >= 0xffff)
01278 return -1;
01279 s->mv[0][0][0] = mx;
01280 s->mv[0][0][1] = my;
01281 }
01282 } else {
01283 s->current_picture.f.mb_type[xy] = MB_TYPE_8x8 | MB_TYPE_L0;
01284 s->mv_type = MV_TYPE_8X8;
01285 for(i=0;i<4;i++) {
01286 mot_val = ff_h263_pred_motion(s, i, 0, &pred_x, &pred_y);
01287 mx = ff_h263_decode_motion(s, pred_x, s->f_code);
01288 if (mx >= 0xffff)
01289 return -1;
01290
01291 my = ff_h263_decode_motion(s, pred_y, s->f_code);
01292 if (my >= 0xffff)
01293 return -1;
01294 s->mv[0][i][0] = mx;
01295 s->mv[0][i][1] = my;
01296 mot_val[0] = mx;
01297 mot_val[1] = my;
01298 }
01299 }
01300 } else if(s->pict_type==AV_PICTURE_TYPE_B) {
01301 int modb1;
01302 int modb2;
01303 int mb_type;
01304
01305 s->mb_intra = 0;
01306 s->mcsel=0;
01307
01308 if(s->mb_x==0){
01309 for(i=0; i<2; i++){
01310 s->last_mv[i][0][0]=
01311 s->last_mv[i][0][1]=
01312 s->last_mv[i][1][0]=
01313 s->last_mv[i][1][1]= 0;
01314 }
01315
01316 ff_thread_await_progress(&s->next_picture_ptr->f, s->mb_y, 0);
01317 }
01318
01319
01320 s->mb_skipped = s->next_picture.f.mbskip_table[s->mb_y * s->mb_stride + s->mb_x];
01321
01322 if(s->mb_skipped){
01323
01324 for(i=0;i<6;i++)
01325 s->block_last_index[i] = -1;
01326
01327 s->mv_dir = MV_DIR_FORWARD;
01328 s->mv_type = MV_TYPE_16X16;
01329 s->mv[0][0][0] = 0;
01330 s->mv[0][0][1] = 0;
01331 s->mv[1][0][0] = 0;
01332 s->mv[1][0][1] = 0;
01333 s->current_picture.f.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
01334 goto end;
01335 }
01336
01337 modb1= get_bits1(&s->gb);
01338 if(modb1){
01339 mb_type= MB_TYPE_DIRECT2 | MB_TYPE_SKIP | MB_TYPE_L0L1;
01340 cbp=0;
01341 }else{
01342 modb2= get_bits1(&s->gb);
01343 mb_type= get_vlc2(&s->gb, mb_type_b_vlc.table, MB_TYPE_B_VLC_BITS, 1);
01344 if(mb_type<0){
01345 av_log(s->avctx, AV_LOG_ERROR, "illegal MB_type\n");
01346 return -1;
01347 }
01348 mb_type= mb_type_b_map[ mb_type ];
01349 if(modb2) cbp= 0;
01350 else{
01351 s->dsp.clear_blocks(s->block[0]);
01352 cbp= get_bits(&s->gb, 6);
01353 }
01354
01355 if ((!IS_DIRECT(mb_type)) && cbp) {
01356 if(get_bits1(&s->gb)){
01357 ff_set_qscale(s, s->qscale + get_bits1(&s->gb)*4 - 2);
01358 }
01359 }
01360
01361 if(!s->progressive_sequence){
01362 if(cbp)
01363 s->interlaced_dct= get_bits1(&s->gb);
01364
01365 if(!IS_DIRECT(mb_type) && get_bits1(&s->gb)){
01366 mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
01367 mb_type &= ~MB_TYPE_16x16;
01368
01369 if(USES_LIST(mb_type, 0)){
01370 s->field_select[0][0]= get_bits1(&s->gb);
01371 s->field_select[0][1]= get_bits1(&s->gb);
01372 }
01373 if(USES_LIST(mb_type, 1)){
01374 s->field_select[1][0]= get_bits1(&s->gb);
01375 s->field_select[1][1]= get_bits1(&s->gb);
01376 }
01377 }
01378 }
01379
01380 s->mv_dir = 0;
01381 if((mb_type & (MB_TYPE_DIRECT2|MB_TYPE_INTERLACED)) == 0){
01382 s->mv_type= MV_TYPE_16X16;
01383
01384 if(USES_LIST(mb_type, 0)){
01385 s->mv_dir = MV_DIR_FORWARD;
01386
01387 mx = ff_h263_decode_motion(s, s->last_mv[0][0][0], s->f_code);
01388 my = ff_h263_decode_motion(s, s->last_mv[0][0][1], s->f_code);
01389 s->last_mv[0][1][0]= s->last_mv[0][0][0]= s->mv[0][0][0] = mx;
01390 s->last_mv[0][1][1]= s->last_mv[0][0][1]= s->mv[0][0][1] = my;
01391 }
01392
01393 if(USES_LIST(mb_type, 1)){
01394 s->mv_dir |= MV_DIR_BACKWARD;
01395
01396 mx = ff_h263_decode_motion(s, s->last_mv[1][0][0], s->b_code);
01397 my = ff_h263_decode_motion(s, s->last_mv[1][0][1], s->b_code);
01398 s->last_mv[1][1][0]= s->last_mv[1][0][0]= s->mv[1][0][0] = mx;
01399 s->last_mv[1][1][1]= s->last_mv[1][0][1]= s->mv[1][0][1] = my;
01400 }
01401 }else if(!IS_DIRECT(mb_type)){
01402 s->mv_type= MV_TYPE_FIELD;
01403
01404 if(USES_LIST(mb_type, 0)){
01405 s->mv_dir = MV_DIR_FORWARD;
01406
01407 for(i=0; i<2; i++){
01408 mx = ff_h263_decode_motion(s, s->last_mv[0][i][0] , s->f_code);
01409 my = ff_h263_decode_motion(s, s->last_mv[0][i][1]/2, s->f_code);
01410 s->last_mv[0][i][0]= s->mv[0][i][0] = mx;
01411 s->last_mv[0][i][1]= (s->mv[0][i][1] = my)*2;
01412 }
01413 }
01414
01415 if(USES_LIST(mb_type, 1)){
01416 s->mv_dir |= MV_DIR_BACKWARD;
01417
01418 for(i=0; i<2; i++){
01419 mx = ff_h263_decode_motion(s, s->last_mv[1][i][0] , s->b_code);
01420 my = ff_h263_decode_motion(s, s->last_mv[1][i][1]/2, s->b_code);
01421 s->last_mv[1][i][0]= s->mv[1][i][0] = mx;
01422 s->last_mv[1][i][1]= (s->mv[1][i][1] = my)*2;
01423 }
01424 }
01425 }
01426 }
01427
01428 if(IS_DIRECT(mb_type)){
01429 if(IS_SKIP(mb_type))
01430 mx=my=0;
01431 else{
01432 mx = ff_h263_decode_motion(s, 0, 1);
01433 my = ff_h263_decode_motion(s, 0, 1);
01434 }
01435
01436 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
01437 mb_type |= ff_mpeg4_set_direct_mv(s, mx, my);
01438 }
01439 s->current_picture.f.mb_type[xy] = mb_type;
01440 } else {
01441 do{
01442 cbpc = get_vlc2(&s->gb, ff_h263_intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 2);
01443 if (cbpc < 0){
01444 av_log(s->avctx, AV_LOG_ERROR, "I cbpc damaged at %d %d\n", s->mb_x, s->mb_y);
01445 return -1;
01446 }
01447 }while(cbpc == 8);
01448
01449 dquant = cbpc & 4;
01450 s->mb_intra = 1;
01451 intra:
01452 s->ac_pred = get_bits1(&s->gb);
01453 if(s->ac_pred)
01454 s->current_picture.f.mb_type[xy] = MB_TYPE_INTRA | MB_TYPE_ACPRED;
01455 else
01456 s->current_picture.f.mb_type[xy] = MB_TYPE_INTRA;
01457
01458 cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
01459 if(cbpy<0){
01460 av_log(s->avctx, AV_LOG_ERROR, "I cbpy damaged at %d %d\n", s->mb_x, s->mb_y);
01461 return -1;
01462 }
01463 cbp = (cbpc & 3) | (cbpy << 2);
01464
01465 s->use_intra_dc_vlc= s->qscale < s->intra_dc_threshold;
01466
01467 if (dquant) {
01468 ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
01469 }
01470
01471 if(!s->progressive_sequence)
01472 s->interlaced_dct= get_bits1(&s->gb);
01473
01474 s->dsp.clear_blocks(s->block[0]);
01475
01476 for (i = 0; i < 6; i++) {
01477 if (mpeg4_decode_block(s, block[i], i, cbp&32, 1, 0) < 0)
01478 return -1;
01479 cbp+=cbp;
01480 }
01481 goto end;
01482 }
01483
01484
01485 for (i = 0; i < 6; i++) {
01486 if (mpeg4_decode_block(s, block[i], i, cbp&32, 0, 0) < 0)
01487 return -1;
01488 cbp+=cbp;
01489 }
01490 end:
01491
01492
01493 if(s->codec_id==AV_CODEC_ID_MPEG4){
01494 int next= mpeg4_is_resync(s);
01495 if(next) {
01496 if (s->mb_x + s->mb_y*s->mb_width + 1 > next && (s->avctx->err_recognition & AV_EF_AGGRESSIVE)) {
01497 return -1;
01498 } else if (s->mb_x + s->mb_y*s->mb_width + 1 >= next)
01499 return SLICE_END;
01500
01501 if(s->pict_type==AV_PICTURE_TYPE_B){
01502 const int delta= s->mb_x + 1 == s->mb_width ? 2 : 1;
01503 ff_thread_await_progress(&s->next_picture_ptr->f,
01504 (s->mb_x + delta >= s->mb_width) ? FFMIN(s->mb_y+1, s->mb_height-1) : s->mb_y, 0);
01505 if (s->next_picture.f.mbskip_table[xy + delta])
01506 return SLICE_OK;
01507 }
01508
01509 return SLICE_END;
01510 }
01511 }
01512
01513 return SLICE_OK;
01514 }
01515
01516
01517 static int mpeg4_decode_gop_header(MpegEncContext * s, GetBitContext *gb){
01518 int hours, minutes, seconds;
01519
01520 if(!show_bits(gb, 23)){
01521 av_log(s->avctx, AV_LOG_WARNING, "GOP header invalid\n");
01522 return -1;
01523 }
01524
01525 hours= get_bits(gb, 5);
01526 minutes= get_bits(gb, 6);
01527 skip_bits1(gb);
01528 seconds= get_bits(gb, 6);
01529
01530 s->time_base= seconds + 60*(minutes + 60*hours);
01531
01532 skip_bits1(gb);
01533 skip_bits1(gb);
01534
01535 return 0;
01536 }
01537
01538 static int mpeg4_decode_profile_level(MpegEncContext * s, GetBitContext *gb){
01539
01540 s->avctx->profile = get_bits(gb, 4);
01541 s->avctx->level = get_bits(gb, 4);
01542
01543
01544 if (s->avctx->profile == 0 && s->avctx->level == 8) {
01545 s->avctx->level = 0;
01546 }
01547
01548 return 0;
01549 }
01550
01551 static int decode_vol_header(MpegEncContext *s, GetBitContext *gb){
01552 int width, height, vo_ver_id;
01553
01554
01555 skip_bits(gb, 1);
01556 s->vo_type= get_bits(gb, 8);
01557 if (get_bits1(gb) != 0) {
01558 vo_ver_id = get_bits(gb, 4);
01559 skip_bits(gb, 3);
01560 } else {
01561 vo_ver_id = 1;
01562 }
01563 s->aspect_ratio_info= get_bits(gb, 4);
01564 if(s->aspect_ratio_info == FF_ASPECT_EXTENDED){
01565 s->avctx->sample_aspect_ratio.num= get_bits(gb, 8);
01566 s->avctx->sample_aspect_ratio.den= get_bits(gb, 8);
01567 }else{
01568 s->avctx->sample_aspect_ratio= ff_h263_pixel_aspect[s->aspect_ratio_info];
01569 }
01570
01571 if ((s->vol_control_parameters=get_bits1(gb))) {
01572 int chroma_format= get_bits(gb, 2);
01573 if(chroma_format!=CHROMA_420){
01574 av_log(s->avctx, AV_LOG_ERROR, "illegal chroma format\n");
01575 }
01576 s->low_delay= get_bits1(gb);
01577 if(get_bits1(gb)){
01578 get_bits(gb, 15);
01579 skip_bits1(gb);
01580 get_bits(gb, 15);
01581 skip_bits1(gb);
01582 get_bits(gb, 15);
01583 skip_bits1(gb);
01584 get_bits(gb, 3);
01585 get_bits(gb, 11);
01586 skip_bits1(gb);
01587 get_bits(gb, 15);
01588 skip_bits1(gb);
01589 }
01590 }else{
01591
01592 if(s->picture_number==0)
01593 s->low_delay=0;
01594 }
01595
01596 s->shape = get_bits(gb, 2);
01597 if(s->shape != RECT_SHAPE) av_log(s->avctx, AV_LOG_ERROR, "only rectangular vol supported\n");
01598 if(s->shape == GRAY_SHAPE && vo_ver_id != 1){
01599 av_log(s->avctx, AV_LOG_ERROR, "Gray shape not supported\n");
01600 skip_bits(gb, 4);
01601 }
01602
01603 check_marker(gb, "before time_increment_resolution");
01604
01605 s->avctx->time_base.den = get_bits(gb, 16);
01606 if(!s->avctx->time_base.den){
01607 av_log(s->avctx, AV_LOG_ERROR, "time_base.den==0\n");
01608 s->avctx->time_base.num = 0;
01609 return -1;
01610 }
01611
01612 s->time_increment_bits = av_log2(s->avctx->time_base.den - 1) + 1;
01613 if (s->time_increment_bits < 1)
01614 s->time_increment_bits = 1;
01615
01616 check_marker(gb, "before fixed_vop_rate");
01617
01618 if (get_bits1(gb) != 0) {
01619 s->avctx->time_base.num = get_bits(gb, s->time_increment_bits);
01620 }else
01621 s->avctx->time_base.num = 1;
01622
01623 s->t_frame=0;
01624
01625 if (s->shape != BIN_ONLY_SHAPE) {
01626 if (s->shape == RECT_SHAPE) {
01627 skip_bits1(gb);
01628 width = get_bits(gb, 13);
01629 skip_bits1(gb);
01630 height = get_bits(gb, 13);
01631 skip_bits1(gb);
01632 if(width && height && !(s->width && s->codec_tag == AV_RL32("MP4S"))){
01633 if (s->width && s->height &&
01634 (s->width != width || s->height != height))
01635 s->context_reinit = 1;
01636 s->width = width;
01637 s->height = height;
01638 }
01639 }
01640
01641 s->progressive_sequence=
01642 s->progressive_frame= get_bits1(gb)^1;
01643 s->interlaced_dct=0;
01644 if(!get_bits1(gb) && (s->avctx->debug & FF_DEBUG_PICT_INFO))
01645 av_log(s->avctx, AV_LOG_INFO, "MPEG4 OBMC not supported (very likely buggy encoder)\n");
01646 if (vo_ver_id == 1) {
01647 s->vol_sprite_usage = get_bits1(gb);
01648 } else {
01649 s->vol_sprite_usage = get_bits(gb, 2);
01650 }
01651 if(s->vol_sprite_usage==STATIC_SPRITE) av_log(s->avctx, AV_LOG_ERROR, "Static Sprites not supported\n");
01652 if(s->vol_sprite_usage==STATIC_SPRITE || s->vol_sprite_usage==GMC_SPRITE){
01653 if(s->vol_sprite_usage==STATIC_SPRITE){
01654 s->sprite_width = get_bits(gb, 13);
01655 skip_bits1(gb);
01656 s->sprite_height= get_bits(gb, 13);
01657 skip_bits1(gb);
01658 s->sprite_left = get_bits(gb, 13);
01659 skip_bits1(gb);
01660 s->sprite_top = get_bits(gb, 13);
01661 skip_bits1(gb);
01662 }
01663 s->num_sprite_warping_points= get_bits(gb, 6);
01664 if(s->num_sprite_warping_points > 3){
01665 av_log(s->avctx, AV_LOG_ERROR, "%d sprite_warping_points\n", s->num_sprite_warping_points);
01666 s->num_sprite_warping_points= 0;
01667 return -1;
01668 }
01669 s->sprite_warping_accuracy = get_bits(gb, 2);
01670 s->sprite_brightness_change= get_bits1(gb);
01671 if(s->vol_sprite_usage==STATIC_SPRITE)
01672 s->low_latency_sprite= get_bits1(gb);
01673 }
01674
01675
01676 if (get_bits1(gb) == 1) {
01677 s->quant_precision = get_bits(gb, 4);
01678 if(get_bits(gb, 4)!=8) av_log(s->avctx, AV_LOG_ERROR, "N-bit not supported\n");
01679 if(s->quant_precision!=5) av_log(s->avctx, AV_LOG_ERROR, "quant precision %d\n", s->quant_precision);
01680 if(s->quant_precision<3 || s->quant_precision>9) {
01681 s->quant_precision = 5;
01682 }
01683 } else {
01684 s->quant_precision = 5;
01685 }
01686
01687
01688
01689 if((s->mpeg_quant=get_bits1(gb))){
01690 int i, v;
01691
01692
01693 for(i=0; i<64; i++){
01694 int j= s->dsp.idct_permutation[i];
01695 v= ff_mpeg4_default_intra_matrix[i];
01696 s->intra_matrix[j]= v;
01697 s->chroma_intra_matrix[j]= v;
01698
01699 v= ff_mpeg4_default_non_intra_matrix[i];
01700 s->inter_matrix[j]= v;
01701 s->chroma_inter_matrix[j]= v;
01702 }
01703
01704
01705 if(get_bits1(gb)){
01706 int last=0;
01707 for(i=0; i<64; i++){
01708 int j;
01709 v= get_bits(gb, 8);
01710 if(v==0) break;
01711
01712 last= v;
01713 j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
01714 s->intra_matrix[j]= v;
01715 s->chroma_intra_matrix[j]= v;
01716 }
01717
01718
01719 for(; i<64; i++){
01720 int j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
01721 s->intra_matrix[j]= last;
01722 s->chroma_intra_matrix[j]= last;
01723 }
01724 }
01725
01726
01727 if(get_bits1(gb)){
01728 int last=0;
01729 for(i=0; i<64; i++){
01730 int j;
01731 v= get_bits(gb, 8);
01732 if(v==0) break;
01733
01734 last= v;
01735 j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
01736 s->inter_matrix[j]= v;
01737 s->chroma_inter_matrix[j]= v;
01738 }
01739
01740
01741 for(; i<64; i++){
01742 int j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
01743 s->inter_matrix[j]= last;
01744 s->chroma_inter_matrix[j]= last;
01745 }
01746 }
01747
01748
01749 }
01750
01751 if(vo_ver_id != 1)
01752 s->quarter_sample= get_bits1(gb);
01753 else s->quarter_sample=0;
01754
01755 if(!get_bits1(gb)){
01756 int pos= get_bits_count(gb);
01757 int estimation_method= get_bits(gb, 2);
01758 if(estimation_method<2){
01759 if(!get_bits1(gb)){
01760 s->cplx_estimation_trash_i += 8*get_bits1(gb);
01761 s->cplx_estimation_trash_i += 8*get_bits1(gb);
01762 s->cplx_estimation_trash_i += 8*get_bits1(gb);
01763 s->cplx_estimation_trash_i += 8*get_bits1(gb);
01764 s->cplx_estimation_trash_i += 8*get_bits1(gb);
01765 s->cplx_estimation_trash_i += 8*get_bits1(gb);
01766 }
01767 if(!get_bits1(gb)){
01768 s->cplx_estimation_trash_i += 8*get_bits1(gb);
01769 s->cplx_estimation_trash_p += 8*get_bits1(gb);
01770 s->cplx_estimation_trash_p += 8*get_bits1(gb);
01771 s->cplx_estimation_trash_i += 8*get_bits1(gb);
01772 }
01773 if(!check_marker(gb, "in complexity estimation part 1")){
01774 skip_bits_long(gb, pos - get_bits_count(gb));
01775 goto no_cplx_est;
01776 }
01777 if(!get_bits1(gb)){
01778 s->cplx_estimation_trash_i += 8*get_bits1(gb);
01779 s->cplx_estimation_trash_i += 8*get_bits1(gb);
01780 s->cplx_estimation_trash_i += 8*get_bits1(gb);
01781 s->cplx_estimation_trash_i += 4*get_bits1(gb);
01782 }
01783 if(!get_bits1(gb)){
01784 s->cplx_estimation_trash_p += 8*get_bits1(gb);
01785 s->cplx_estimation_trash_p += 8*get_bits1(gb);
01786 s->cplx_estimation_trash_b += 8*get_bits1(gb);
01787 s->cplx_estimation_trash_p += 8*get_bits1(gb);
01788 s->cplx_estimation_trash_p += 8*get_bits1(gb);
01789 s->cplx_estimation_trash_p += 8*get_bits1(gb);
01790 }
01791 if(!check_marker(gb, "in complexity estimation part 2")){
01792 skip_bits_long(gb, pos - get_bits_count(gb));
01793 goto no_cplx_est;
01794 }
01795 if(estimation_method==1){
01796 s->cplx_estimation_trash_i += 8*get_bits1(gb);
01797 s->cplx_estimation_trash_p += 8*get_bits1(gb);
01798 }
01799 }else
01800 av_log(s->avctx, AV_LOG_ERROR, "Invalid Complexity estimation method %d\n", estimation_method);
01801 }else{
01802 no_cplx_est:
01803 s->cplx_estimation_trash_i=
01804 s->cplx_estimation_trash_p=
01805 s->cplx_estimation_trash_b= 0;
01806 }
01807
01808 s->resync_marker= !get_bits1(gb);
01809
01810 s->data_partitioning= get_bits1(gb);
01811 if(s->data_partitioning){
01812 s->rvlc= get_bits1(gb);
01813 }
01814
01815 if(vo_ver_id != 1) {
01816 s->new_pred= get_bits1(gb);
01817 if(s->new_pred){
01818 av_log(s->avctx, AV_LOG_ERROR, "new pred not supported\n");
01819 skip_bits(gb, 2);
01820 skip_bits1(gb);
01821 }
01822 s->reduced_res_vop= get_bits1(gb);
01823 if(s->reduced_res_vop) av_log(s->avctx, AV_LOG_ERROR, "reduced resolution VOP not supported\n");
01824 }
01825 else{
01826 s->new_pred=0;
01827 s->reduced_res_vop= 0;
01828 }
01829
01830 s->scalability= get_bits1(gb);
01831
01832 if (s->scalability) {
01833 GetBitContext bak= *gb;
01834 int h_sampling_factor_n;
01835 int h_sampling_factor_m;
01836 int v_sampling_factor_n;
01837 int v_sampling_factor_m;
01838
01839 s->hierachy_type= get_bits1(gb);
01840 skip_bits(gb, 4);
01841 skip_bits1(gb);
01842 h_sampling_factor_n= get_bits(gb, 5);
01843 h_sampling_factor_m= get_bits(gb, 5);
01844 v_sampling_factor_n= get_bits(gb, 5);
01845 v_sampling_factor_m= get_bits(gb, 5);
01846 s->enhancement_type= get_bits1(gb);
01847
01848 if( h_sampling_factor_n==0 || h_sampling_factor_m==0
01849 || v_sampling_factor_n==0 || v_sampling_factor_m==0){
01850
01851
01852 s->scalability=0;
01853 *gb= bak;
01854 }else
01855 av_log(s->avctx, AV_LOG_ERROR, "scalability not supported\n");
01856
01857
01858 }
01859 }
01860
01861 if(s->avctx->debug&FF_DEBUG_PICT_INFO) {
01862 av_log(s->avctx, AV_LOG_DEBUG, "tb %d/%d, tincrbits:%d, qp_prec:%d, ps:%d, %s%s%s%s\n",
01863 s->avctx->time_base.num, s->avctx->time_base.den,
01864 s->time_increment_bits,
01865 s->quant_precision,
01866 s->progressive_sequence,
01867 s->scalability ? "scalability " :"" , s->quarter_sample ? "qpel " : "",
01868 s->data_partitioning ? "partition " : "", s->rvlc ? "rvlc " : ""
01869 );
01870 }
01871
01872 return 0;
01873 }
01874
01879 static int decode_user_data(MpegEncContext *s, GetBitContext *gb){
01880 char buf[256];
01881 int i;
01882 int e;
01883 int ver = 0, build = 0, ver2 = 0, ver3 = 0;
01884 char last;
01885
01886 for(i=0; i<255 && get_bits_count(gb) < gb->size_in_bits; i++){
01887 if(show_bits(gb, 23) == 0) break;
01888 buf[i]= get_bits(gb, 8);
01889 }
01890 buf[i]=0;
01891
01892
01893 e=sscanf(buf, "DivX%dBuild%d%c", &ver, &build, &last);
01894 if(e<2)
01895 e=sscanf(buf, "DivX%db%d%c", &ver, &build, &last);
01896 if(e>=2){
01897 s->divx_version= ver;
01898 s->divx_build= build;
01899 s->divx_packed= e==3 && last=='p';
01900 if(s->divx_packed && !s->showed_packed_warning) {
01901 av_log(s->avctx, AV_LOG_WARNING, "Invalid and inefficient vfw-avi packed B frames detected\n");
01902 s->showed_packed_warning=1;
01903 }
01904 }
01905
01906
01907 e=sscanf(buf, "FFmpe%*[^b]b%d", &build)+3;
01908 if(e!=4)
01909 e=sscanf(buf, "FFmpeg v%d.%d.%d / libavcodec build: %d", &ver, &ver2, &ver3, &build);
01910 if(e!=4){
01911 e=sscanf(buf, "Lavc%d.%d.%d", &ver, &ver2, &ver3)+1;
01912 if (e>1)
01913 build= (ver<<16) + (ver2<<8) + ver3;
01914 }
01915 if(e!=4){
01916 if(strcmp(buf, "ffmpeg")==0){
01917 s->lavc_build= 4600;
01918 }
01919 }
01920 if(e==4){
01921 s->lavc_build= build;
01922 }
01923
01924
01925 e=sscanf(buf, "XviD%d", &build);
01926 if(e==1){
01927 s->xvid_build= build;
01928 }
01929
01930 return 0;
01931 }
01932
01933 static int decode_vop_header(MpegEncContext *s, GetBitContext *gb){
01934 int time_incr, time_increment;
01935
01936 s->pict_type = get_bits(gb, 2) + AV_PICTURE_TYPE_I;
01937 if(s->pict_type==AV_PICTURE_TYPE_B && s->low_delay && s->vol_control_parameters==0 && !(s->flags & CODEC_FLAG_LOW_DELAY)){
01938 av_log(s->avctx, AV_LOG_ERROR, "low_delay flag incorrectly, clearing it\n");
01939 s->low_delay=0;
01940 }
01941
01942 s->partitioned_frame= s->data_partitioning && s->pict_type!=AV_PICTURE_TYPE_B;
01943 if(s->partitioned_frame)
01944 s->decode_mb= mpeg4_decode_partitioned_mb;
01945 else
01946 s->decode_mb= mpeg4_decode_mb;
01947
01948 time_incr=0;
01949 while (get_bits1(gb) != 0)
01950 time_incr++;
01951
01952 check_marker(gb, "before time_increment");
01953
01954 if(s->time_increment_bits==0 || !(show_bits(gb, s->time_increment_bits+1)&1)){
01955 av_log(s->avctx, AV_LOG_ERROR, "hmm, seems the headers are not complete, trying to guess time_increment_bits\n");
01956
01957 for(s->time_increment_bits=1 ;s->time_increment_bits<16; s->time_increment_bits++){
01958 if ( s->pict_type == AV_PICTURE_TYPE_P
01959 || (s->pict_type == AV_PICTURE_TYPE_S && s->vol_sprite_usage==GMC_SPRITE)) {
01960 if((show_bits(gb, s->time_increment_bits+6)&0x37) == 0x30) break;
01961 }else
01962 if((show_bits(gb, s->time_increment_bits+5)&0x1F) == 0x18) break;
01963 }
01964
01965 av_log(s->avctx, AV_LOG_ERROR, "my guess is %d bits ;)\n",s->time_increment_bits);
01966 }
01967
01968 if(IS_3IV1) time_increment= get_bits1(gb);
01969 else time_increment= get_bits(gb, s->time_increment_bits);
01970
01971 if(s->pict_type!=AV_PICTURE_TYPE_B){
01972 s->last_time_base= s->time_base;
01973 s->time_base+= time_incr;
01974 s->time= s->time_base*s->avctx->time_base.den + time_increment;
01975 if(s->workaround_bugs&FF_BUG_UMP4){
01976 if(s->time < s->last_non_b_time){
01977
01978
01979 s->time_base++;
01980 s->time+= s->avctx->time_base.den;
01981 }
01982 }
01983 s->pp_time= s->time - s->last_non_b_time;
01984 s->last_non_b_time= s->time;
01985 }else{
01986 s->time= (s->last_time_base + time_incr)*s->avctx->time_base.den + time_increment;
01987 s->pb_time= s->pp_time - (s->last_non_b_time - s->time);
01988 if(s->pp_time <=s->pb_time || s->pp_time <= s->pp_time - s->pb_time || s->pp_time<=0){
01989
01990 return FRAME_SKIPPED;
01991 }
01992 ff_mpeg4_init_direct_mv(s);
01993
01994 if(s->t_frame==0) s->t_frame= s->pb_time;
01995 if(s->t_frame==0) s->t_frame=1;
01996 s->pp_field_time= ( ROUNDED_DIV(s->last_non_b_time, s->t_frame)
01997 - ROUNDED_DIV(s->last_non_b_time - s->pp_time, s->t_frame))*2;
01998 s->pb_field_time= ( ROUNDED_DIV(s->time, s->t_frame)
01999 - ROUNDED_DIV(s->last_non_b_time - s->pp_time, s->t_frame))*2;
02000 if(!s->progressive_sequence){
02001 if(s->pp_field_time <= s->pb_field_time || s->pb_field_time <= 1)
02002 return FRAME_SKIPPED;
02003 }
02004 }
02005
02006 if(s->avctx->time_base.num)
02007 s->current_picture_ptr->f.pts = (s->time + s->avctx->time_base.num / 2) / s->avctx->time_base.num;
02008 else
02009 s->current_picture_ptr->f.pts = AV_NOPTS_VALUE;
02010 if(s->avctx->debug&FF_DEBUG_PTS)
02011 av_log(s->avctx, AV_LOG_DEBUG, "MPEG4 PTS: %"PRId64"\n",
02012 s->current_picture_ptr->f.pts);
02013
02014 check_marker(gb, "before vop_coded");
02015
02016
02017 if (get_bits1(gb) != 1){
02018 if(s->avctx->debug&FF_DEBUG_PICT_INFO)
02019 av_log(s->avctx, AV_LOG_ERROR, "vop not coded\n");
02020 return FRAME_SKIPPED;
02021 }
02022 if (s->shape != BIN_ONLY_SHAPE && ( s->pict_type == AV_PICTURE_TYPE_P
02023 || (s->pict_type == AV_PICTURE_TYPE_S && s->vol_sprite_usage==GMC_SPRITE))) {
02024
02025 s->no_rounding = get_bits1(gb);
02026 } else {
02027 s->no_rounding = 0;
02028 }
02029
02030
02031 if (s->shape != RECT_SHAPE) {
02032 if (s->vol_sprite_usage != 1 || s->pict_type != AV_PICTURE_TYPE_I) {
02033 skip_bits(gb, 13);
02034 skip_bits1(gb);
02035 skip_bits(gb, 13);
02036 skip_bits1(gb);
02037 skip_bits(gb, 13);
02038 skip_bits1(gb);
02039 skip_bits(gb, 13);
02040 }
02041 skip_bits1(gb);
02042
02043 if (get_bits1(gb) != 0) {
02044 skip_bits(gb, 8);
02045 }
02046 }
02047
02048
02049 if (s->shape != BIN_ONLY_SHAPE) {
02050 skip_bits_long(gb, s->cplx_estimation_trash_i);
02051 if(s->pict_type != AV_PICTURE_TYPE_I)
02052 skip_bits_long(gb, s->cplx_estimation_trash_p);
02053 if(s->pict_type == AV_PICTURE_TYPE_B)
02054 skip_bits_long(gb, s->cplx_estimation_trash_b);
02055
02056 if(get_bits_left(gb) < 3) {
02057 av_log(s->avctx, AV_LOG_ERROR, "Header truncated\n");
02058 return -1;
02059 }
02060 s->intra_dc_threshold= ff_mpeg4_dc_threshold[ get_bits(gb, 3) ];
02061 if(!s->progressive_sequence){
02062 s->top_field_first= get_bits1(gb);
02063 s->alternate_scan= get_bits1(gb);
02064 }else
02065 s->alternate_scan= 0;
02066 }
02067
02068 if(s->alternate_scan){
02069 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_alternate_vertical_scan);
02070 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_alternate_vertical_scan);
02071 ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_alternate_vertical_scan);
02072 ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
02073 } else{
02074 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_zigzag_direct);
02075 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_zigzag_direct);
02076 ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
02077 ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
02078 }
02079
02080 if(s->pict_type == AV_PICTURE_TYPE_S && (s->vol_sprite_usage==STATIC_SPRITE || s->vol_sprite_usage==GMC_SPRITE)){
02081 if(mpeg4_decode_sprite_trajectory(s, gb) < 0)
02082 return -1;
02083 if(s->sprite_brightness_change) av_log(s->avctx, AV_LOG_ERROR, "sprite_brightness_change not supported\n");
02084 if(s->vol_sprite_usage==STATIC_SPRITE) av_log(s->avctx, AV_LOG_ERROR, "static sprite not supported\n");
02085 }
02086
02087 if (s->shape != BIN_ONLY_SHAPE) {
02088 s->chroma_qscale= s->qscale = get_bits(gb, s->quant_precision);
02089 if(s->qscale==0){
02090 av_log(s->avctx, AV_LOG_ERROR, "Error, header damaged or not MPEG4 header (qscale=0)\n");
02091 return -1;
02092 }
02093
02094 if (s->pict_type != AV_PICTURE_TYPE_I) {
02095 s->f_code = get_bits(gb, 3);
02096 if(s->f_code==0){
02097 av_log(s->avctx, AV_LOG_ERROR, "Error, header damaged or not MPEG4 header (f_code=0)\n");
02098 s->f_code=1;
02099 return -1;
02100 }
02101 }else
02102 s->f_code=1;
02103
02104 if (s->pict_type == AV_PICTURE_TYPE_B) {
02105 s->b_code = get_bits(gb, 3);
02106 if(s->b_code==0){
02107 av_log(s->avctx, AV_LOG_ERROR, "Error, header damaged or not MPEG4 header (b_code=0)\n");
02108 s->b_code=1;
02109 return -1;
02110 }
02111 }else
02112 s->b_code=1;
02113
02114 if(s->avctx->debug&FF_DEBUG_PICT_INFO){
02115 av_log(s->avctx, AV_LOG_DEBUG, "qp:%d fc:%d,%d %s size:%d pro:%d alt:%d top:%d %spel part:%d resync:%d w:%d a:%d rnd:%d vot:%d%s dc:%d ce:%d/%d/%d time:%"PRId64" tincr:%d\n",
02116 s->qscale, s->f_code, s->b_code,
02117 s->pict_type == AV_PICTURE_TYPE_I ? "I" : (s->pict_type == AV_PICTURE_TYPE_P ? "P" : (s->pict_type == AV_PICTURE_TYPE_B ? "B" : "S")),
02118 gb->size_in_bits,s->progressive_sequence, s->alternate_scan, s->top_field_first,
02119 s->quarter_sample ? "q" : "h", s->data_partitioning, s->resync_marker, s->num_sprite_warping_points,
02120 s->sprite_warping_accuracy, 1-s->no_rounding, s->vo_type, s->vol_control_parameters ? " VOLC" : " ", s->intra_dc_threshold,
02121 s->cplx_estimation_trash_i, s->cplx_estimation_trash_p, s->cplx_estimation_trash_b,
02122 s->time,
02123 time_increment
02124 );
02125 }
02126
02127 if(!s->scalability){
02128 if (s->shape!=RECT_SHAPE && s->pict_type!=AV_PICTURE_TYPE_I) {
02129 skip_bits1(gb);
02130 }
02131 }else{
02132 if(s->enhancement_type){
02133 int load_backward_shape= get_bits1(gb);
02134 if(load_backward_shape){
02135 av_log(s->avctx, AV_LOG_ERROR, "load backward shape isn't supported\n");
02136 }
02137 }
02138 skip_bits(gb, 2);
02139 }
02140 }
02141
02142
02143 if(s->vo_type==0 && s->vol_control_parameters==0 && s->divx_version==-1 && s->picture_number==0){
02144 av_log(s->avctx, AV_LOG_WARNING, "looks like this file was encoded with (divx4/(old)xvid/opendivx) -> forcing low_delay flag\n");
02145 s->low_delay=1;
02146 }
02147
02148 s->picture_number++;
02149
02150 s->y_dc_scale_table= ff_mpeg4_y_dc_scale_table;
02151 s->c_dc_scale_table= ff_mpeg4_c_dc_scale_table;
02152
02153 if(s->workaround_bugs&FF_BUG_EDGE){
02154 s->h_edge_pos= s->width;
02155 s->v_edge_pos= s->height;
02156 }
02157 return 0;
02158 }
02159
02166 int ff_mpeg4_decode_picture_header(MpegEncContext * s, GetBitContext *gb)
02167 {
02168 unsigned startcode, v;
02169
02170
02171 align_get_bits(gb);
02172
02173 if(s->codec_tag == AV_RL32("WV1F") && show_bits(gb, 24) == 0x575630){
02174 skip_bits(gb, 24);
02175 if(get_bits(gb, 8) == 0xF0)
02176 goto end;
02177 }
02178
02179 startcode = 0xff;
02180 for(;;) {
02181 if(get_bits_count(gb) >= gb->size_in_bits){
02182 if(gb->size_in_bits==8 && (s->divx_version>=0 || s->xvid_build>=0) || s->codec_tag == AV_RL32("QMP4")){
02183 av_log(s->avctx, AV_LOG_VERBOSE, "frame skip %d\n", gb->size_in_bits);
02184 return FRAME_SKIPPED;
02185 }else
02186 return -1;
02187 }
02188
02189
02190 v = get_bits(gb, 8);
02191 startcode = ((startcode << 8) | v) & 0xffffffff;
02192
02193 if((startcode&0xFFFFFF00) != 0x100)
02194 continue;
02195
02196 if(s->avctx->debug&FF_DEBUG_STARTCODE){
02197 av_log(s->avctx, AV_LOG_DEBUG, "startcode: %3X ", startcode);
02198 if (startcode<=0x11F) av_log(s->avctx, AV_LOG_DEBUG, "Video Object Start");
02199 else if(startcode<=0x12F) av_log(s->avctx, AV_LOG_DEBUG, "Video Object Layer Start");
02200 else if(startcode<=0x13F) av_log(s->avctx, AV_LOG_DEBUG, "Reserved");
02201 else if(startcode<=0x15F) av_log(s->avctx, AV_LOG_DEBUG, "FGS bp start");
02202 else if(startcode<=0x1AF) av_log(s->avctx, AV_LOG_DEBUG, "Reserved");
02203 else if(startcode==0x1B0) av_log(s->avctx, AV_LOG_DEBUG, "Visual Object Seq Start");
02204 else if(startcode==0x1B1) av_log(s->avctx, AV_LOG_DEBUG, "Visual Object Seq End");
02205 else if(startcode==0x1B2) av_log(s->avctx, AV_LOG_DEBUG, "User Data");
02206 else if(startcode==0x1B3) av_log(s->avctx, AV_LOG_DEBUG, "Group of VOP start");
02207 else if(startcode==0x1B4) av_log(s->avctx, AV_LOG_DEBUG, "Video Session Error");
02208 else if(startcode==0x1B5) av_log(s->avctx, AV_LOG_DEBUG, "Visual Object Start");
02209 else if(startcode==0x1B6) av_log(s->avctx, AV_LOG_DEBUG, "Video Object Plane start");
02210 else if(startcode==0x1B7) av_log(s->avctx, AV_LOG_DEBUG, "slice start");
02211 else if(startcode==0x1B8) av_log(s->avctx, AV_LOG_DEBUG, "extension start");
02212 else if(startcode==0x1B9) av_log(s->avctx, AV_LOG_DEBUG, "fgs start");
02213 else if(startcode==0x1BA) av_log(s->avctx, AV_LOG_DEBUG, "FBA Object start");
02214 else if(startcode==0x1BB) av_log(s->avctx, AV_LOG_DEBUG, "FBA Object Plane start");
02215 else if(startcode==0x1BC) av_log(s->avctx, AV_LOG_DEBUG, "Mesh Object start");
02216 else if(startcode==0x1BD) av_log(s->avctx, AV_LOG_DEBUG, "Mesh Object Plane start");
02217 else if(startcode==0x1BE) av_log(s->avctx, AV_LOG_DEBUG, "Still Texture Object start");
02218 else if(startcode==0x1BF) av_log(s->avctx, AV_LOG_DEBUG, "Texture Spatial Layer start");
02219 else if(startcode==0x1C0) av_log(s->avctx, AV_LOG_DEBUG, "Texture SNR Layer start");
02220 else if(startcode==0x1C1) av_log(s->avctx, AV_LOG_DEBUG, "Texture Tile start");
02221 else if(startcode==0x1C2) av_log(s->avctx, AV_LOG_DEBUG, "Texture Shape Layer start");
02222 else if(startcode==0x1C3) av_log(s->avctx, AV_LOG_DEBUG, "stuffing start");
02223 else if(startcode<=0x1C5) av_log(s->avctx, AV_LOG_DEBUG, "reserved");
02224 else if(startcode<=0x1FF) av_log(s->avctx, AV_LOG_DEBUG, "System start");
02225 av_log(s->avctx, AV_LOG_DEBUG, " at %d\n", get_bits_count(gb));
02226 }
02227
02228 if(startcode >= 0x120 && startcode <= 0x12F){
02229 if(decode_vol_header(s, gb) < 0)
02230 return -1;
02231 }
02232 else if(startcode == USER_DATA_STARTCODE){
02233 decode_user_data(s, gb);
02234 }
02235 else if(startcode == GOP_STARTCODE){
02236 mpeg4_decode_gop_header(s, gb);
02237 }
02238 else if(startcode == VOS_STARTCODE){
02239 mpeg4_decode_profile_level(s, gb);
02240 }
02241 else if(startcode == VOP_STARTCODE){
02242 break;
02243 }
02244
02245 align_get_bits(gb);
02246 startcode = 0xff;
02247 }
02248 end:
02249 if(s->flags& CODEC_FLAG_LOW_DELAY)
02250 s->low_delay=1;
02251 s->avctx->has_b_frames= !s->low_delay;
02252 return decode_vop_header(s, gb);
02253 }
02254
02255 static av_cold int decode_init(AVCodecContext *avctx)
02256 {
02257 MpegEncContext *s = avctx->priv_data;
02258 int ret;
02259 static int done = 0;
02260
02261 s->divx_version=
02262 s->divx_build=
02263 s->xvid_build=
02264 s->lavc_build= -1;
02265
02266 if((ret=ff_h263_decode_init(avctx)) < 0)
02267 return ret;
02268
02269 if (!done) {
02270 done = 1;
02271
02272 ff_init_rl(&ff_mpeg4_rl_intra, ff_mpeg4_static_rl_table_store[0]);
02273 ff_init_rl(&ff_rvlc_rl_inter, ff_mpeg4_static_rl_table_store[1]);
02274 ff_init_rl(&ff_rvlc_rl_intra, ff_mpeg4_static_rl_table_store[2]);
02275 INIT_VLC_RL(ff_mpeg4_rl_intra, 554);
02276 INIT_VLC_RL(ff_rvlc_rl_inter, 1072);
02277 INIT_VLC_RL(ff_rvlc_rl_intra, 1072);
02278 INIT_VLC_STATIC(&dc_lum, DC_VLC_BITS, 10 ,
02279 &ff_mpeg4_DCtab_lum[0][1], 2, 1,
02280 &ff_mpeg4_DCtab_lum[0][0], 2, 1, 512);
02281 INIT_VLC_STATIC(&dc_chrom, DC_VLC_BITS, 10 ,
02282 &ff_mpeg4_DCtab_chrom[0][1], 2, 1,
02283 &ff_mpeg4_DCtab_chrom[0][0], 2, 1, 512);
02284 INIT_VLC_STATIC(&sprite_trajectory, SPRITE_TRAJ_VLC_BITS, 15,
02285 &ff_sprite_trajectory_tab[0][1], 4, 2,
02286 &ff_sprite_trajectory_tab[0][0], 4, 2, 128);
02287 INIT_VLC_STATIC(&mb_type_b_vlc, MB_TYPE_B_VLC_BITS, 4,
02288 &ff_mb_type_b_tab[0][1], 2, 1,
02289 &ff_mb_type_b_tab[0][0], 2, 1, 16);
02290 }
02291
02292 s->h263_pred = 1;
02293 s->low_delay = 0;
02294 s->decode_mb= mpeg4_decode_mb;
02295 s->time_increment_bits = 4;
02296 avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
02297
02298 return 0;
02299 }
02300
02301 static const AVProfile mpeg4_video_profiles[] = {
02302 { FF_PROFILE_MPEG4_SIMPLE, "Simple Profile" },
02303 { FF_PROFILE_MPEG4_SIMPLE_SCALABLE, "Simple Scalable Profile" },
02304 { FF_PROFILE_MPEG4_CORE, "Core Profile" },
02305 { FF_PROFILE_MPEG4_MAIN, "Main Profile" },
02306 { FF_PROFILE_MPEG4_N_BIT, "N-bit Profile" },
02307 { FF_PROFILE_MPEG4_SCALABLE_TEXTURE, "Scalable Texture Profile" },
02308 { FF_PROFILE_MPEG4_SIMPLE_FACE_ANIMATION, "Simple Face Animation Profile" },
02309 { FF_PROFILE_MPEG4_BASIC_ANIMATED_TEXTURE, "Basic Animated Texture Profile" },
02310 { FF_PROFILE_MPEG4_HYBRID, "Hybrid Profile" },
02311 { FF_PROFILE_MPEG4_ADVANCED_REAL_TIME, "Advanced Real Time Simple Profile" },
02312 { FF_PROFILE_MPEG4_CORE_SCALABLE, "Code Scalable Profile" },
02313 { FF_PROFILE_MPEG4_ADVANCED_CODING, "Advanced Coding Profile" },
02314 { FF_PROFILE_MPEG4_ADVANCED_CORE, "Advanced Core Profile" },
02315 { FF_PROFILE_MPEG4_ADVANCED_SCALABLE_TEXTURE, "Advanced Scalable Texture Profile" },
02316 { FF_PROFILE_MPEG4_SIMPLE_STUDIO, "Simple Studio Profile" },
02317 { FF_PROFILE_MPEG4_ADVANCED_SIMPLE, "Advanced Simple Profile" },
02318 { FF_PROFILE_UNKNOWN },
02319 };
02320
02321 static const AVOption mpeg4_options[] = {
02322 {"quarter_sample", "1/4 subpel MC", offsetof(MpegEncContext, quarter_sample), FF_OPT_TYPE_INT, {.dbl = 0}, 0, 1, 0},
02323 {"divx_packed", "divx style packed b frames", offsetof(MpegEncContext, divx_packed), FF_OPT_TYPE_INT, {.dbl = 0}, 0, 1, 0},
02324 {NULL}
02325 };
02326
02327 static const AVClass mpeg4_class = {
02328 "MPEG4 Video Decoder",
02329 av_default_item_name,
02330 mpeg4_options,
02331 LIBAVUTIL_VERSION_INT,
02332 };
02333
02334 static const AVClass mpeg4_vdpau_class = {
02335 "MPEG4 Video VDPAU Decoder",
02336 av_default_item_name,
02337 mpeg4_options,
02338 LIBAVUTIL_VERSION_INT,
02339 };
02340
02341 AVCodec ff_mpeg4_decoder = {
02342 .name = "mpeg4",
02343 .type = AVMEDIA_TYPE_VIDEO,
02344 .id = AV_CODEC_ID_MPEG4,
02345 .priv_data_size = sizeof(MpegEncContext),
02346 .init = decode_init,
02347 .close = ff_h263_decode_end,
02348 .decode = ff_h263_decode_frame,
02349 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
02350 CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY |
02351 CODEC_CAP_FRAME_THREADS,
02352 .flush = ff_mpeg_flush,
02353 .max_lowres = 3,
02354 .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2"),
02355 .pix_fmts = ff_hwaccel_pixfmt_list_420,
02356 .profiles = NULL_IF_CONFIG_SMALL(mpeg4_video_profiles),
02357 .update_thread_context = ONLY_IF_THREADS_ENABLED(ff_mpeg_update_thread_context),
02358 .priv_class = &mpeg4_class,
02359 };
02360
02361
02362 #if CONFIG_MPEG4_VDPAU_DECODER
02363 AVCodec ff_mpeg4_vdpau_decoder = {
02364 .name = "mpeg4_vdpau",
02365 .type = AVMEDIA_TYPE_VIDEO,
02366 .id = AV_CODEC_ID_MPEG4,
02367 .priv_data_size = sizeof(MpegEncContext),
02368 .init = decode_init,
02369 .close = ff_h263_decode_end,
02370 .decode = ff_h263_decode_frame,
02371 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY |
02372 CODEC_CAP_HWACCEL_VDPAU,
02373 .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 (VDPAU)"),
02374 .pix_fmts = (const enum PixelFormat[]){ PIX_FMT_VDPAU_MPEG4,
02375 PIX_FMT_NONE },
02376 .priv_class = &mpeg4_vdpau_class,
02377 };
02378 #endif