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 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 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 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 = &rvlc_rl_intra;
00882 rl_vlc = 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 = &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 = 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 = 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 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 assert(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((AVFrame*)s->next_picture_ptr, 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==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->error_recognition >= FF_ER_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((AVFrame*)s->next_picture_ptr,
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 s->width = width;
01634 s->height = height;
01635 }
01636 }
01637
01638 s->progressive_sequence=
01639 s->progressive_frame= get_bits1(gb)^1;
01640 s->interlaced_dct=0;
01641 if(!get_bits1(gb) && (s->avctx->debug & FF_DEBUG_PICT_INFO))
01642 av_log(s->avctx, AV_LOG_INFO, "MPEG4 OBMC not supported (very likely buggy encoder)\n");
01643 if (vo_ver_id == 1) {
01644 s->vol_sprite_usage = get_bits1(gb);
01645 } else {
01646 s->vol_sprite_usage = get_bits(gb, 2);
01647 }
01648 if(s->vol_sprite_usage==STATIC_SPRITE) av_log(s->avctx, AV_LOG_ERROR, "Static Sprites not supported\n");
01649 if(s->vol_sprite_usage==STATIC_SPRITE || s->vol_sprite_usage==GMC_SPRITE){
01650 if(s->vol_sprite_usage==STATIC_SPRITE){
01651 s->sprite_width = get_bits(gb, 13);
01652 skip_bits1(gb);
01653 s->sprite_height= get_bits(gb, 13);
01654 skip_bits1(gb);
01655 s->sprite_left = get_bits(gb, 13);
01656 skip_bits1(gb);
01657 s->sprite_top = get_bits(gb, 13);
01658 skip_bits1(gb);
01659 }
01660 s->num_sprite_warping_points= get_bits(gb, 6);
01661 if(s->num_sprite_warping_points > 3){
01662 av_log(s->avctx, AV_LOG_ERROR, "%d sprite_warping_points\n", s->num_sprite_warping_points);
01663 s->num_sprite_warping_points= 0;
01664 return -1;
01665 }
01666 s->sprite_warping_accuracy = get_bits(gb, 2);
01667 s->sprite_brightness_change= get_bits1(gb);
01668 if(s->vol_sprite_usage==STATIC_SPRITE)
01669 s->low_latency_sprite= get_bits1(gb);
01670 }
01671
01672
01673 if (get_bits1(gb) == 1) {
01674 s->quant_precision = get_bits(gb, 4);
01675 if(get_bits(gb, 4)!=8) av_log(s->avctx, AV_LOG_ERROR, "N-bit not supported\n");
01676 if(s->quant_precision!=5) av_log(s->avctx, AV_LOG_ERROR, "quant precision %d\n", s->quant_precision);
01677 } else {
01678 s->quant_precision = 5;
01679 }
01680
01681
01682
01683 if((s->mpeg_quant=get_bits1(gb))){
01684 int i, v;
01685
01686
01687 for(i=0; i<64; i++){
01688 int j= s->dsp.idct_permutation[i];
01689 v= ff_mpeg4_default_intra_matrix[i];
01690 s->intra_matrix[j]= v;
01691 s->chroma_intra_matrix[j]= v;
01692
01693 v= ff_mpeg4_default_non_intra_matrix[i];
01694 s->inter_matrix[j]= v;
01695 s->chroma_inter_matrix[j]= v;
01696 }
01697
01698
01699 if(get_bits1(gb)){
01700 int last=0;
01701 for(i=0; i<64; i++){
01702 int j;
01703 v= get_bits(gb, 8);
01704 if(v==0) break;
01705
01706 last= v;
01707 j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
01708 s->intra_matrix[j]= v;
01709 s->chroma_intra_matrix[j]= v;
01710 }
01711
01712
01713 for(; i<64; i++){
01714 int j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
01715 s->intra_matrix[j]= last;
01716 s->chroma_intra_matrix[j]= last;
01717 }
01718 }
01719
01720
01721 if(get_bits1(gb)){
01722 int last=0;
01723 for(i=0; i<64; i++){
01724 int j;
01725 v= get_bits(gb, 8);
01726 if(v==0) break;
01727
01728 last= v;
01729 j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
01730 s->inter_matrix[j]= v;
01731 s->chroma_inter_matrix[j]= v;
01732 }
01733
01734
01735 for(; i<64; i++){
01736 int j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
01737 s->inter_matrix[j]= last;
01738 s->chroma_inter_matrix[j]= last;
01739 }
01740 }
01741
01742
01743 }
01744
01745 if(vo_ver_id != 1)
01746 s->quarter_sample= get_bits1(gb);
01747 else s->quarter_sample=0;
01748
01749 if(!get_bits1(gb)){
01750 int pos= get_bits_count(gb);
01751 int estimation_method= get_bits(gb, 2);
01752 if(estimation_method<2){
01753 if(!get_bits1(gb)){
01754 s->cplx_estimation_trash_i += 8*get_bits1(gb);
01755 s->cplx_estimation_trash_i += 8*get_bits1(gb);
01756 s->cplx_estimation_trash_i += 8*get_bits1(gb);
01757 s->cplx_estimation_trash_i += 8*get_bits1(gb);
01758 s->cplx_estimation_trash_i += 8*get_bits1(gb);
01759 s->cplx_estimation_trash_i += 8*get_bits1(gb);
01760 }
01761 if(!get_bits1(gb)){
01762 s->cplx_estimation_trash_i += 8*get_bits1(gb);
01763 s->cplx_estimation_trash_p += 8*get_bits1(gb);
01764 s->cplx_estimation_trash_p += 8*get_bits1(gb);
01765 s->cplx_estimation_trash_i += 8*get_bits1(gb);
01766 }
01767 if(!check_marker(gb, "in complexity estimation part 1")){
01768 skip_bits_long(gb, pos - get_bits_count(gb));
01769 goto no_cplx_est;
01770 }
01771 if(!get_bits1(gb)){
01772 s->cplx_estimation_trash_i += 8*get_bits1(gb);
01773 s->cplx_estimation_trash_i += 8*get_bits1(gb);
01774 s->cplx_estimation_trash_i += 8*get_bits1(gb);
01775 s->cplx_estimation_trash_i += 4*get_bits1(gb);
01776 }
01777 if(!get_bits1(gb)){
01778 s->cplx_estimation_trash_p += 8*get_bits1(gb);
01779 s->cplx_estimation_trash_p += 8*get_bits1(gb);
01780 s->cplx_estimation_trash_b += 8*get_bits1(gb);
01781 s->cplx_estimation_trash_p += 8*get_bits1(gb);
01782 s->cplx_estimation_trash_p += 8*get_bits1(gb);
01783 s->cplx_estimation_trash_p += 8*get_bits1(gb);
01784 }
01785 if(!check_marker(gb, "in complexity estimation part 2")){
01786 skip_bits_long(gb, pos - get_bits_count(gb));
01787 goto no_cplx_est;
01788 }
01789 if(estimation_method==1){
01790 s->cplx_estimation_trash_i += 8*get_bits1(gb);
01791 s->cplx_estimation_trash_p += 8*get_bits1(gb);
01792 }
01793 }else
01794 av_log(s->avctx, AV_LOG_ERROR, "Invalid Complexity estimation method %d\n", estimation_method);
01795 }else{
01796 no_cplx_est:
01797 s->cplx_estimation_trash_i=
01798 s->cplx_estimation_trash_p=
01799 s->cplx_estimation_trash_b= 0;
01800 }
01801
01802 s->resync_marker= !get_bits1(gb);
01803
01804 s->data_partitioning= get_bits1(gb);
01805 if(s->data_partitioning){
01806 s->rvlc= get_bits1(gb);
01807 }
01808
01809 if(vo_ver_id != 1) {
01810 s->new_pred= get_bits1(gb);
01811 if(s->new_pred){
01812 av_log(s->avctx, AV_LOG_ERROR, "new pred not supported\n");
01813 skip_bits(gb, 2);
01814 skip_bits1(gb);
01815 }
01816 s->reduced_res_vop= get_bits1(gb);
01817 if(s->reduced_res_vop) av_log(s->avctx, AV_LOG_ERROR, "reduced resolution VOP not supported\n");
01818 }
01819 else{
01820 s->new_pred=0;
01821 s->reduced_res_vop= 0;
01822 }
01823
01824 s->scalability= get_bits1(gb);
01825
01826 if (s->scalability) {
01827 GetBitContext bak= *gb;
01828 int h_sampling_factor_n;
01829 int h_sampling_factor_m;
01830 int v_sampling_factor_n;
01831 int v_sampling_factor_m;
01832
01833 s->hierachy_type= get_bits1(gb);
01834 skip_bits(gb, 4);
01835 skip_bits1(gb);
01836 h_sampling_factor_n= get_bits(gb, 5);
01837 h_sampling_factor_m= get_bits(gb, 5);
01838 v_sampling_factor_n= get_bits(gb, 5);
01839 v_sampling_factor_m= get_bits(gb, 5);
01840 s->enhancement_type= get_bits1(gb);
01841
01842 if( h_sampling_factor_n==0 || h_sampling_factor_m==0
01843 || v_sampling_factor_n==0 || v_sampling_factor_m==0){
01844
01845
01846 s->scalability=0;
01847 *gb= bak;
01848 }else
01849 av_log(s->avctx, AV_LOG_ERROR, "scalability not supported\n");
01850
01851
01852 }
01853 }
01854 return 0;
01855 }
01856
01861 static int decode_user_data(MpegEncContext *s, GetBitContext *gb){
01862 char buf[256];
01863 int i;
01864 int e;
01865 int ver = 0, build = 0, ver2 = 0, ver3 = 0;
01866 char last;
01867
01868 for(i=0; i<255 && get_bits_count(gb) < gb->size_in_bits; i++){
01869 if(show_bits(gb, 23) == 0) break;
01870 buf[i]= get_bits(gb, 8);
01871 }
01872 buf[i]=0;
01873
01874
01875 e=sscanf(buf, "DivX%dBuild%d%c", &ver, &build, &last);
01876 if(e<2)
01877 e=sscanf(buf, "DivX%db%d%c", &ver, &build, &last);
01878 if(e>=2){
01879 s->divx_version= ver;
01880 s->divx_build= build;
01881 s->divx_packed= e==3 && last=='p';
01882 if(s->divx_packed && !s->showed_packed_warning) {
01883 av_log(s->avctx, AV_LOG_WARNING, "Invalid and inefficient vfw-avi packed B frames detected\n");
01884 s->showed_packed_warning=1;
01885 }
01886 }
01887
01888
01889 e=sscanf(buf, "FFmpe%*[^b]b%d", &build)+3;
01890 if(e!=4)
01891 e=sscanf(buf, "FFmpeg v%d.%d.%d / libavcodec build: %d", &ver, &ver2, &ver3, &build);
01892 if(e!=4){
01893 e=sscanf(buf, "Lavc%d.%d.%d", &ver, &ver2, &ver3)+1;
01894 if (e>1)
01895 build= (ver<<16) + (ver2<<8) + ver3;
01896 }
01897 if(e!=4){
01898 if(strcmp(buf, "ffmpeg")==0){
01899 s->lavc_build= 4600;
01900 }
01901 }
01902 if(e==4){
01903 s->lavc_build= build;
01904 }
01905
01906
01907 e=sscanf(buf, "XviD%d", &build);
01908 if(e==1){
01909 s->xvid_build= build;
01910 }
01911
01912 return 0;
01913 }
01914
01915 static int decode_vop_header(MpegEncContext *s, GetBitContext *gb){
01916 int time_incr, time_increment;
01917
01918 s->pict_type = get_bits(gb, 2) + AV_PICTURE_TYPE_I;
01919 if(s->pict_type==AV_PICTURE_TYPE_B && s->low_delay && s->vol_control_parameters==0 && !(s->flags & CODEC_FLAG_LOW_DELAY)){
01920 av_log(s->avctx, AV_LOG_ERROR, "low_delay flag incorrectly, clearing it\n");
01921 s->low_delay=0;
01922 }
01923
01924 s->partitioned_frame= s->data_partitioning && s->pict_type!=AV_PICTURE_TYPE_B;
01925 if(s->partitioned_frame)
01926 s->decode_mb= mpeg4_decode_partitioned_mb;
01927 else
01928 s->decode_mb= mpeg4_decode_mb;
01929
01930 time_incr=0;
01931 while (get_bits1(gb) != 0)
01932 time_incr++;
01933
01934 check_marker(gb, "before time_increment");
01935
01936 if(s->time_increment_bits==0 || !(show_bits(gb, s->time_increment_bits+1)&1)){
01937 av_log(s->avctx, AV_LOG_ERROR, "hmm, seems the headers are not complete, trying to guess time_increment_bits\n");
01938
01939 for(s->time_increment_bits=1 ;s->time_increment_bits<16; s->time_increment_bits++){
01940 if ( s->pict_type == AV_PICTURE_TYPE_P
01941 || (s->pict_type == AV_PICTURE_TYPE_S && s->vol_sprite_usage==GMC_SPRITE)) {
01942 if((show_bits(gb, s->time_increment_bits+6)&0x37) == 0x30) break;
01943 }else
01944 if((show_bits(gb, s->time_increment_bits+5)&0x1F) == 0x18) break;
01945 }
01946
01947 av_log(s->avctx, AV_LOG_ERROR, "my guess is %d bits ;)\n",s->time_increment_bits);
01948 }
01949
01950 if(IS_3IV1) time_increment= get_bits1(gb);
01951 else time_increment= get_bits(gb, s->time_increment_bits);
01952
01953 if(s->pict_type!=AV_PICTURE_TYPE_B){
01954 s->last_time_base= s->time_base;
01955 s->time_base+= time_incr;
01956 s->time= s->time_base*s->avctx->time_base.den + time_increment;
01957 if(s->workaround_bugs&FF_BUG_UMP4){
01958 if(s->time < s->last_non_b_time){
01959
01960
01961 s->time_base++;
01962 s->time+= s->avctx->time_base.den;
01963 }
01964 }
01965 s->pp_time= s->time - s->last_non_b_time;
01966 s->last_non_b_time= s->time;
01967 }else{
01968 s->time= (s->last_time_base + time_incr)*s->avctx->time_base.den + time_increment;
01969 s->pb_time= s->pp_time - (s->last_non_b_time - s->time);
01970 if(s->pp_time <=s->pb_time || s->pp_time <= s->pp_time - s->pb_time || s->pp_time<=0){
01971
01972 return FRAME_SKIPPED;
01973 }
01974 ff_mpeg4_init_direct_mv(s);
01975
01976 if(s->t_frame==0) s->t_frame= s->pb_time;
01977 if(s->t_frame==0) s->t_frame=1;
01978 s->pp_field_time= ( ROUNDED_DIV(s->last_non_b_time, s->t_frame)
01979 - ROUNDED_DIV(s->last_non_b_time - s->pp_time, s->t_frame))*2;
01980 s->pb_field_time= ( ROUNDED_DIV(s->time, s->t_frame)
01981 - ROUNDED_DIV(s->last_non_b_time - s->pp_time, s->t_frame))*2;
01982 if(!s->progressive_sequence){
01983 if(s->pp_field_time <= s->pb_field_time || s->pb_field_time <= 1)
01984 return FRAME_SKIPPED;
01985 }
01986 }
01987
01988 if(s->avctx->time_base.num)
01989 s->current_picture_ptr->f.pts = (s->time + s->avctx->time_base.num / 2) / s->avctx->time_base.num;
01990 else
01991 s->current_picture_ptr->f.pts = AV_NOPTS_VALUE;
01992 if(s->avctx->debug&FF_DEBUG_PTS)
01993 av_log(s->avctx, AV_LOG_DEBUG, "MPEG4 PTS: %"PRId64"\n",
01994 s->current_picture_ptr->f.pts);
01995
01996 check_marker(gb, "before vop_coded");
01997
01998
01999 if (get_bits1(gb) != 1){
02000 if(s->avctx->debug&FF_DEBUG_PICT_INFO)
02001 av_log(s->avctx, AV_LOG_ERROR, "vop not coded\n");
02002 return FRAME_SKIPPED;
02003 }
02004 if (s->shape != BIN_ONLY_SHAPE && ( s->pict_type == AV_PICTURE_TYPE_P
02005 || (s->pict_type == AV_PICTURE_TYPE_S && s->vol_sprite_usage==GMC_SPRITE))) {
02006
02007 s->no_rounding = get_bits1(gb);
02008 } else {
02009 s->no_rounding = 0;
02010 }
02011
02012
02013 if (s->shape != RECT_SHAPE) {
02014 if (s->vol_sprite_usage != 1 || s->pict_type != AV_PICTURE_TYPE_I) {
02015 skip_bits(gb, 13);
02016 skip_bits1(gb);
02017 skip_bits(gb, 13);
02018 skip_bits1(gb);
02019 skip_bits(gb, 13);
02020 skip_bits1(gb);
02021 skip_bits(gb, 13);
02022 }
02023 skip_bits1(gb);
02024
02025 if (get_bits1(gb) != 0) {
02026 skip_bits(gb, 8);
02027 }
02028 }
02029
02030
02031 if (s->shape != BIN_ONLY_SHAPE) {
02032 skip_bits_long(gb, s->cplx_estimation_trash_i);
02033 if(s->pict_type != AV_PICTURE_TYPE_I)
02034 skip_bits_long(gb, s->cplx_estimation_trash_p);
02035 if(s->pict_type == AV_PICTURE_TYPE_B)
02036 skip_bits_long(gb, s->cplx_estimation_trash_b);
02037
02038 s->intra_dc_threshold= mpeg4_dc_threshold[ get_bits(gb, 3) ];
02039 if(!s->progressive_sequence){
02040 s->top_field_first= get_bits1(gb);
02041 s->alternate_scan= get_bits1(gb);
02042 }else
02043 s->alternate_scan= 0;
02044 }
02045
02046 if(s->alternate_scan){
02047 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_alternate_vertical_scan);
02048 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_alternate_vertical_scan);
02049 ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_alternate_vertical_scan);
02050 ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
02051 } else{
02052 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_zigzag_direct);
02053 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_zigzag_direct);
02054 ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
02055 ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
02056 }
02057
02058 if(s->pict_type == AV_PICTURE_TYPE_S && (s->vol_sprite_usage==STATIC_SPRITE || s->vol_sprite_usage==GMC_SPRITE)){
02059 if(mpeg4_decode_sprite_trajectory(s, gb) < 0)
02060 return -1;
02061 if(s->sprite_brightness_change) av_log(s->avctx, AV_LOG_ERROR, "sprite_brightness_change not supported\n");
02062 if(s->vol_sprite_usage==STATIC_SPRITE) av_log(s->avctx, AV_LOG_ERROR, "static sprite not supported\n");
02063 }
02064
02065 if (s->shape != BIN_ONLY_SHAPE) {
02066 s->chroma_qscale= s->qscale = get_bits(gb, s->quant_precision);
02067 if(s->qscale==0){
02068 av_log(s->avctx, AV_LOG_ERROR, "Error, header damaged or not MPEG4 header (qscale=0)\n");
02069 return -1;
02070 }
02071
02072 if (s->pict_type != AV_PICTURE_TYPE_I) {
02073 s->f_code = get_bits(gb, 3);
02074 if(s->f_code==0){
02075 av_log(s->avctx, AV_LOG_ERROR, "Error, header damaged or not MPEG4 header (f_code=0)\n");
02076 return -1;
02077 }
02078 }else
02079 s->f_code=1;
02080
02081 if (s->pict_type == AV_PICTURE_TYPE_B) {
02082 s->b_code = get_bits(gb, 3);
02083 }else
02084 s->b_code=1;
02085
02086 if(s->avctx->debug&FF_DEBUG_PICT_INFO){
02087 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\n",
02088 s->qscale, s->f_code, s->b_code,
02089 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")),
02090 gb->size_in_bits,s->progressive_sequence, s->alternate_scan, s->top_field_first,
02091 s->quarter_sample ? "q" : "h", s->data_partitioning, s->resync_marker, s->num_sprite_warping_points,
02092 s->sprite_warping_accuracy, 1-s->no_rounding, s->vo_type, s->vol_control_parameters ? " VOLC" : " ", s->intra_dc_threshold, s->cplx_estimation_trash_i, s->cplx_estimation_trash_p, s->cplx_estimation_trash_b);
02093 }
02094
02095 if(!s->scalability){
02096 if (s->shape!=RECT_SHAPE && s->pict_type!=AV_PICTURE_TYPE_I) {
02097 skip_bits1(gb);
02098 }
02099 }else{
02100 if(s->enhancement_type){
02101 int load_backward_shape= get_bits1(gb);
02102 if(load_backward_shape){
02103 av_log(s->avctx, AV_LOG_ERROR, "load backward shape isn't supported\n");
02104 }
02105 }
02106 skip_bits(gb, 2);
02107 }
02108 }
02109
02110
02111 if(s->vo_type==0 && s->vol_control_parameters==0 && s->divx_version==-1 && s->picture_number==0){
02112 av_log(s->avctx, AV_LOG_WARNING, "looks like this file was encoded with (divx4/(old)xvid/opendivx) -> forcing low_delay flag\n");
02113 s->low_delay=1;
02114 }
02115
02116 s->picture_number++;
02117
02118 s->y_dc_scale_table= ff_mpeg4_y_dc_scale_table;
02119 s->c_dc_scale_table= ff_mpeg4_c_dc_scale_table;
02120
02121 if(s->workaround_bugs&FF_BUG_EDGE){
02122 s->h_edge_pos= s->width;
02123 s->v_edge_pos= s->height;
02124 }
02125 return 0;
02126 }
02127
02134 int ff_mpeg4_decode_picture_header(MpegEncContext * s, GetBitContext *gb)
02135 {
02136 unsigned startcode, v;
02137
02138
02139 align_get_bits(gb);
02140
02141 if(s->codec_tag == AV_RL32("WV1F") && show_bits(gb, 24) == 0x575630){
02142 skip_bits(gb, 24);
02143 if(get_bits(gb, 8) == 0xF0)
02144 goto end;
02145 }
02146
02147 startcode = 0xff;
02148 for(;;) {
02149 if(get_bits_count(gb) >= gb->size_in_bits){
02150 if(gb->size_in_bits==8 && (s->divx_version>=0 || s->xvid_build>=0) || s->codec_tag == AV_RL32("QMP4")){
02151 av_log(s->avctx, AV_LOG_VERBOSE, "frame skip %d\n", gb->size_in_bits);
02152 return FRAME_SKIPPED;
02153 }else
02154 return -1;
02155 }
02156
02157
02158 v = get_bits(gb, 8);
02159 startcode = ((startcode << 8) | v) & 0xffffffff;
02160
02161 if((startcode&0xFFFFFF00) != 0x100)
02162 continue;
02163
02164 if(s->avctx->debug&FF_DEBUG_STARTCODE){
02165 av_log(s->avctx, AV_LOG_DEBUG, "startcode: %3X ", startcode);
02166 if (startcode<=0x11F) av_log(s->avctx, AV_LOG_DEBUG, "Video Object Start");
02167 else if(startcode<=0x12F) av_log(s->avctx, AV_LOG_DEBUG, "Video Object Layer Start");
02168 else if(startcode<=0x13F) av_log(s->avctx, AV_LOG_DEBUG, "Reserved");
02169 else if(startcode<=0x15F) av_log(s->avctx, AV_LOG_DEBUG, "FGS bp start");
02170 else if(startcode<=0x1AF) av_log(s->avctx, AV_LOG_DEBUG, "Reserved");
02171 else if(startcode==0x1B0) av_log(s->avctx, AV_LOG_DEBUG, "Visual Object Seq Start");
02172 else if(startcode==0x1B1) av_log(s->avctx, AV_LOG_DEBUG, "Visual Object Seq End");
02173 else if(startcode==0x1B2) av_log(s->avctx, AV_LOG_DEBUG, "User Data");
02174 else if(startcode==0x1B3) av_log(s->avctx, AV_LOG_DEBUG, "Group of VOP start");
02175 else if(startcode==0x1B4) av_log(s->avctx, AV_LOG_DEBUG, "Video Session Error");
02176 else if(startcode==0x1B5) av_log(s->avctx, AV_LOG_DEBUG, "Visual Object Start");
02177 else if(startcode==0x1B6) av_log(s->avctx, AV_LOG_DEBUG, "Video Object Plane start");
02178 else if(startcode==0x1B7) av_log(s->avctx, AV_LOG_DEBUG, "slice start");
02179 else if(startcode==0x1B8) av_log(s->avctx, AV_LOG_DEBUG, "extension start");
02180 else if(startcode==0x1B9) av_log(s->avctx, AV_LOG_DEBUG, "fgs start");
02181 else if(startcode==0x1BA) av_log(s->avctx, AV_LOG_DEBUG, "FBA Object start");
02182 else if(startcode==0x1BB) av_log(s->avctx, AV_LOG_DEBUG, "FBA Object Plane start");
02183 else if(startcode==0x1BC) av_log(s->avctx, AV_LOG_DEBUG, "Mesh Object start");
02184 else if(startcode==0x1BD) av_log(s->avctx, AV_LOG_DEBUG, "Mesh Object Plane start");
02185 else if(startcode==0x1BE) av_log(s->avctx, AV_LOG_DEBUG, "Still Texture Object start");
02186 else if(startcode==0x1BF) av_log(s->avctx, AV_LOG_DEBUG, "Texture Spatial Layer start");
02187 else if(startcode==0x1C0) av_log(s->avctx, AV_LOG_DEBUG, "Texture SNR Layer start");
02188 else if(startcode==0x1C1) av_log(s->avctx, AV_LOG_DEBUG, "Texture Tile start");
02189 else if(startcode==0x1C2) av_log(s->avctx, AV_LOG_DEBUG, "Texture Shape Layer start");
02190 else if(startcode==0x1C3) av_log(s->avctx, AV_LOG_DEBUG, "stuffing start");
02191 else if(startcode<=0x1C5) av_log(s->avctx, AV_LOG_DEBUG, "reserved");
02192 else if(startcode<=0x1FF) av_log(s->avctx, AV_LOG_DEBUG, "System start");
02193 av_log(s->avctx, AV_LOG_DEBUG, " at %d\n", get_bits_count(gb));
02194 }
02195
02196 if(startcode >= 0x120 && startcode <= 0x12F){
02197 if(decode_vol_header(s, gb) < 0)
02198 return -1;
02199 }
02200 else if(startcode == USER_DATA_STARTCODE){
02201 decode_user_data(s, gb);
02202 }
02203 else if(startcode == GOP_STARTCODE){
02204 mpeg4_decode_gop_header(s, gb);
02205 }
02206 else if(startcode == VOS_STARTCODE){
02207 mpeg4_decode_profile_level(s, gb);
02208 }
02209 else if(startcode == VOP_STARTCODE){
02210 break;
02211 }
02212
02213 align_get_bits(gb);
02214 startcode = 0xff;
02215 }
02216 end:
02217 if(s->flags& CODEC_FLAG_LOW_DELAY)
02218 s->low_delay=1;
02219 s->avctx->has_b_frames= !s->low_delay;
02220 return decode_vop_header(s, gb);
02221 }
02222
02223 static av_cold int decode_init(AVCodecContext *avctx)
02224 {
02225 MpegEncContext *s = avctx->priv_data;
02226 int ret;
02227 static int done = 0;
02228
02229 s->divx_version=
02230 s->divx_build=
02231 s->xvid_build=
02232 s->lavc_build= -1;
02233
02234 if((ret=ff_h263_decode_init(avctx)) < 0)
02235 return ret;
02236
02237 if (!done) {
02238 done = 1;
02239
02240 ff_init_rl(&ff_mpeg4_rl_intra, ff_mpeg4_static_rl_table_store[0]);
02241 ff_init_rl(&rvlc_rl_inter, ff_mpeg4_static_rl_table_store[1]);
02242 ff_init_rl(&rvlc_rl_intra, ff_mpeg4_static_rl_table_store[2]);
02243 INIT_VLC_RL(ff_mpeg4_rl_intra, 554);
02244 INIT_VLC_RL(rvlc_rl_inter, 1072);
02245 INIT_VLC_RL(rvlc_rl_intra, 1072);
02246 INIT_VLC_STATIC(&dc_lum, DC_VLC_BITS, 10 ,
02247 &ff_mpeg4_DCtab_lum[0][1], 2, 1,
02248 &ff_mpeg4_DCtab_lum[0][0], 2, 1, 512);
02249 INIT_VLC_STATIC(&dc_chrom, DC_VLC_BITS, 10 ,
02250 &ff_mpeg4_DCtab_chrom[0][1], 2, 1,
02251 &ff_mpeg4_DCtab_chrom[0][0], 2, 1, 512);
02252 INIT_VLC_STATIC(&sprite_trajectory, SPRITE_TRAJ_VLC_BITS, 15,
02253 &sprite_trajectory_tab[0][1], 4, 2,
02254 &sprite_trajectory_tab[0][0], 4, 2, 128);
02255 INIT_VLC_STATIC(&mb_type_b_vlc, MB_TYPE_B_VLC_BITS, 4,
02256 &mb_type_b_tab[0][1], 2, 1,
02257 &mb_type_b_tab[0][0], 2, 1, 16);
02258 }
02259
02260 s->h263_pred = 1;
02261 s->low_delay = 0;
02262 s->decode_mb= mpeg4_decode_mb;
02263 s->time_increment_bits = 4;
02264 avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
02265
02266 return 0;
02267 }
02268
02269 static const AVProfile mpeg4_video_profiles[] = {
02270 { FF_PROFILE_MPEG4_SIMPLE, "Simple Profile" },
02271 { FF_PROFILE_MPEG4_SIMPLE_SCALABLE, "Simple Scalable Profile" },
02272 { FF_PROFILE_MPEG4_CORE, "Core Profile" },
02273 { FF_PROFILE_MPEG4_MAIN, "Main Profile" },
02274 { FF_PROFILE_MPEG4_N_BIT, "N-bit Profile" },
02275 { FF_PROFILE_MPEG4_SCALABLE_TEXTURE, "Scalable Texture Profile" },
02276 { FF_PROFILE_MPEG4_SIMPLE_FACE_ANIMATION, "Simple Face Animation Profile" },
02277 { FF_PROFILE_MPEG4_BASIC_ANIMATED_TEXTURE, "Basic Animated Texture Profile" },
02278 { FF_PROFILE_MPEG4_HYBRID, "Hybrid Profile" },
02279 { FF_PROFILE_MPEG4_ADVANCED_REAL_TIME, "Advanced Real Time Simple Profile" },
02280 { FF_PROFILE_MPEG4_CORE_SCALABLE, "Code Scalable Profile" },
02281 { FF_PROFILE_MPEG4_ADVANCED_CODING, "Advanced Coding Profile" },
02282 { FF_PROFILE_MPEG4_ADVANCED_CORE, "Advanced Core Profile" },
02283 { FF_PROFILE_MPEG4_ADVANCED_SCALABLE_TEXTURE, "Advanced Scalable Texture Profile" },
02284 { FF_PROFILE_MPEG4_SIMPLE_STUDIO, "Simple Studio Profile" },
02285 { FF_PROFILE_MPEG4_ADVANCED_SIMPLE, "Advanced Simple Profile" },
02286 };
02287
02288 static const AVOption mpeg4_options[] = {
02289 {"quarter_sample", "1/4 subpel MC", offsetof(MpegEncContext, quarter_sample), FF_OPT_TYPE_INT, {.dbl = 0}, 0, 1, 0},
02290 {"divx_packed", "divx style packed b frames", offsetof(MpegEncContext, divx_packed), FF_OPT_TYPE_INT, {.dbl = 0}, 0, 1, 0},
02291 {NULL}
02292 };
02293
02294 static const AVClass mpeg4_class = {
02295 "MPEG4 Video Decoder",
02296 av_default_item_name,
02297 mpeg4_options,
02298 LIBAVUTIL_VERSION_INT,
02299 };
02300
02301 static const AVClass mpeg4_vdpau_class = {
02302 "MPEG4 Video VDPAU Decoder",
02303 av_default_item_name,
02304 mpeg4_options,
02305 LIBAVUTIL_VERSION_INT,
02306 };
02307
02308 AVCodec ff_mpeg4_decoder = {
02309 .name = "mpeg4",
02310 .type = AVMEDIA_TYPE_VIDEO,
02311 .id = CODEC_ID_MPEG4,
02312 .priv_data_size = sizeof(MpegEncContext),
02313 .init = decode_init,
02314 .close = ff_h263_decode_end,
02315 .decode = ff_h263_decode_frame,
02316 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY | CODEC_CAP_FRAME_THREADS,
02317 .flush= ff_mpeg_flush,
02318 .max_lowres= 3,
02319 .long_name= NULL_IF_CONFIG_SMALL("MPEG-4 part 2"),
02320 .pix_fmts= ff_hwaccel_pixfmt_list_420,
02321 .profiles = NULL_IF_CONFIG_SMALL(mpeg4_video_profiles),
02322 .update_thread_context= ONLY_IF_THREADS_ENABLED(ff_mpeg_update_thread_context),
02323 .priv_class = &mpeg4_class,
02324 };
02325
02326
02327 #if CONFIG_MPEG4_VDPAU_DECODER
02328 AVCodec ff_mpeg4_vdpau_decoder = {
02329 .name = "mpeg4_vdpau",
02330 .type = AVMEDIA_TYPE_VIDEO,
02331 .id = CODEC_ID_MPEG4,
02332 .priv_data_size = sizeof(MpegEncContext),
02333 .init = decode_init,
02334 .close = ff_h263_decode_end,
02335 .decode = ff_h263_decode_frame,
02336 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
02337 .long_name= NULL_IF_CONFIG_SMALL("MPEG-4 part 2 (VDPAU)"),
02338 .pix_fmts= (const enum PixelFormat[]){PIX_FMT_VDPAU_MPEG4, PIX_FMT_NONE},
02339 .priv_class = &mpeg4_vdpau_class,
02340 };
02341 #endif