00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00030
00031 #include <limits.h>
00032
00033 #include "dsputil.h"
00034 #include "avcodec.h"
00035 #include "mpegvideo.h"
00036 #include "h263.h"
00037 #include "mathops.h"
00038 #include "unary.h"
00039 #include "flv.h"
00040 #include "mpeg4video.h"
00041 #include "internal.h"
00042
00046 static uint8_t mv_penalty[MAX_FCODE+1][MAX_MV*2+1];
00047
00051 static uint8_t fcode_tab[MAX_MV*2+1];
00052
00057 static uint8_t umv_fcode_tab[MAX_MV*2+1];
00058
00059
00060
00061 static uint8_t uni_h263_intra_aic_rl_len [64*64*2*2];
00062 static uint8_t uni_h263_inter_rl_len [64*64*2*2];
00063
00064
00065 #define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128*64 + (run)*128 + (level))
00066
00067 static const uint8_t wrong_run[102] = {
00068 1, 2, 3, 5, 4, 10, 9, 8,
00069 11, 15, 17, 16, 23, 22, 21, 20,
00070 19, 18, 25, 24, 27, 26, 11, 7,
00071 6, 1, 2, 13, 2, 2, 2, 2,
00072 6, 12, 3, 9, 1, 3, 4, 3,
00073 7, 4, 1, 1, 5, 5, 14, 6,
00074 1, 7, 1, 8, 1, 1, 1, 1,
00075 10, 1, 1, 5, 9, 17, 25, 24,
00076 29, 33, 32, 41, 2, 23, 28, 31,
00077 3, 22, 30, 4, 27, 40, 8, 26,
00078 6, 39, 7, 38, 16, 37, 15, 10,
00079 11, 12, 13, 14, 1, 21, 20, 18,
00080 19, 2, 1, 34, 35, 36
00081 };
00082
00088 av_const int ff_h263_aspect_to_info(AVRational aspect){
00089 int i;
00090
00091 if(aspect.num==0) aspect= (AVRational){1,1};
00092
00093 for(i=1; i<6; i++){
00094 if(av_cmp_q(ff_h263_pixel_aspect[i], aspect) == 0){
00095 return i;
00096 }
00097 }
00098
00099 return FF_ASPECT_EXTENDED;
00100 }
00101
00102 void ff_h263_encode_picture_header(MpegEncContext * s, int picture_number)
00103 {
00104 int format, coded_frame_rate, coded_frame_rate_base, i, temp_ref;
00105 int best_clock_code=1;
00106 int best_divisor=60;
00107 int best_error= INT_MAX;
00108
00109 if(s->h263_plus){
00110 for(i=0; i<2; i++){
00111 int div, error;
00112 div= (s->avctx->time_base.num*1800000LL + 500LL*s->avctx->time_base.den) / ((1000LL+i)*s->avctx->time_base.den);
00113 div= av_clip(div, 1, 127);
00114 error= FFABS(s->avctx->time_base.num*1800000LL - (1000LL+i)*s->avctx->time_base.den*div);
00115 if(error < best_error){
00116 best_error= error;
00117 best_divisor= div;
00118 best_clock_code= i;
00119 }
00120 }
00121 }
00122 s->custom_pcf= best_clock_code!=1 || best_divisor!=60;
00123 coded_frame_rate= 1800000;
00124 coded_frame_rate_base= (1000+best_clock_code)*best_divisor;
00125
00126 avpriv_align_put_bits(&s->pb);
00127
00128
00129 s->ptr_lastgob = put_bits_ptr(&s->pb);
00130 put_bits(&s->pb, 22, 0x20);
00131 temp_ref= s->picture_number * (int64_t)coded_frame_rate * s->avctx->time_base.num /
00132 (coded_frame_rate_base * (int64_t)s->avctx->time_base.den);
00133 put_sbits(&s->pb, 8, temp_ref);
00134
00135 put_bits(&s->pb, 1, 1);
00136 put_bits(&s->pb, 1, 0);
00137 put_bits(&s->pb, 1, 0);
00138 put_bits(&s->pb, 1, 0);
00139 put_bits(&s->pb, 1, 0);
00140
00141 format = ff_match_2uint16(ff_h263_format, FF_ARRAY_ELEMS(ff_h263_format), s->width, s->height);
00142 if (!s->h263_plus) {
00143
00144 put_bits(&s->pb, 3, format);
00145 put_bits(&s->pb, 1, (s->pict_type == AV_PICTURE_TYPE_P));
00146
00147
00148
00149 put_bits(&s->pb, 1, 0);
00150 put_bits(&s->pb, 1, 0);
00151 put_bits(&s->pb, 1, s->obmc);
00152 put_bits(&s->pb, 1, 0);
00153 put_bits(&s->pb, 5, s->qscale);
00154 put_bits(&s->pb, 1, 0);
00155 } else {
00156 int ufep=1;
00157
00158
00159
00160 put_bits(&s->pb, 3, 7);
00161 put_bits(&s->pb,3,ufep);
00162 if (format == 8)
00163 put_bits(&s->pb,3,6);
00164 else
00165 put_bits(&s->pb, 3, format);
00166
00167 put_bits(&s->pb,1, s->custom_pcf);
00168 put_bits(&s->pb,1, s->umvplus);
00169 put_bits(&s->pb,1,0);
00170 put_bits(&s->pb,1,s->obmc);
00171 put_bits(&s->pb,1,s->h263_aic);
00172 put_bits(&s->pb,1,s->loop_filter);
00173 put_bits(&s->pb,1,s->h263_slice_structured);
00174 put_bits(&s->pb,1,0);
00175 put_bits(&s->pb,1,0);
00176 put_bits(&s->pb,1,s->alt_inter_vlc);
00177 put_bits(&s->pb,1,s->modified_quant);
00178 put_bits(&s->pb,1,1);
00179 put_bits(&s->pb,3,0);
00180
00181 put_bits(&s->pb, 3, s->pict_type == AV_PICTURE_TYPE_P);
00182
00183 put_bits(&s->pb,1,0);
00184 put_bits(&s->pb,1,0);
00185 put_bits(&s->pb,1,s->no_rounding);
00186 put_bits(&s->pb,2,0);
00187 put_bits(&s->pb,1,1);
00188
00189
00190 put_bits(&s->pb, 1, 0);
00191
00192 if (format == 8) {
00193
00194 s->aspect_ratio_info= ff_h263_aspect_to_info(s->avctx->sample_aspect_ratio);
00195
00196 put_bits(&s->pb,4,s->aspect_ratio_info);
00197 put_bits(&s->pb,9,(s->width >> 2) - 1);
00198 put_bits(&s->pb,1,1);
00199 put_bits(&s->pb,9,(s->height >> 2));
00200 if (s->aspect_ratio_info == FF_ASPECT_EXTENDED){
00201 put_bits(&s->pb, 8, s->avctx->sample_aspect_ratio.num);
00202 put_bits(&s->pb, 8, s->avctx->sample_aspect_ratio.den);
00203 }
00204 }
00205 if(s->custom_pcf){
00206 if(ufep){
00207 put_bits(&s->pb, 1, best_clock_code);
00208 put_bits(&s->pb, 7, best_divisor);
00209 }
00210 put_sbits(&s->pb, 2, temp_ref>>8);
00211 }
00212
00213
00214 if (s->umvplus)
00215
00216
00217 put_bits(&s->pb,2,1);
00218 if(s->h263_slice_structured)
00219 put_bits(&s->pb,2,0);
00220
00221 put_bits(&s->pb, 5, s->qscale);
00222 }
00223
00224 put_bits(&s->pb, 1, 0);
00225
00226 if(s->h263_slice_structured){
00227 put_bits(&s->pb, 1, 1);
00228
00229 av_assert1(s->mb_x == 0 && s->mb_y == 0);
00230 ff_h263_encode_mba(s);
00231
00232 put_bits(&s->pb, 1, 1);
00233 }
00234
00235 if(s->h263_aic){
00236 s->y_dc_scale_table=
00237 s->c_dc_scale_table= ff_aic_dc_scale_table;
00238 }else{
00239 s->y_dc_scale_table=
00240 s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
00241 }
00242 }
00243
00247 void ff_h263_encode_gob_header(MpegEncContext * s, int mb_line)
00248 {
00249 put_bits(&s->pb, 17, 1);
00250
00251 if(s->h263_slice_structured){
00252 put_bits(&s->pb, 1, 1);
00253
00254 ff_h263_encode_mba(s);
00255
00256 if(s->mb_num > 1583)
00257 put_bits(&s->pb, 1, 1);
00258 put_bits(&s->pb, 5, s->qscale);
00259 put_bits(&s->pb, 1, 1);
00260 put_bits(&s->pb, 2, s->pict_type == AV_PICTURE_TYPE_I);
00261 }else{
00262 int gob_number= mb_line / s->gob_index;
00263
00264 put_bits(&s->pb, 5, gob_number);
00265 put_bits(&s->pb, 2, s->pict_type == AV_PICTURE_TYPE_I);
00266 put_bits(&s->pb, 5, s->qscale);
00267 }
00268 }
00269
00273 void ff_clean_h263_qscales(MpegEncContext *s){
00274 int i;
00275 int8_t * const qscale_table = s->current_picture.f.qscale_table;
00276
00277 ff_init_qscale_tab(s);
00278
00279 for(i=1; i<s->mb_num; i++){
00280 if(qscale_table[ s->mb_index2xy[i] ] - qscale_table[ s->mb_index2xy[i-1] ] >2)
00281 qscale_table[ s->mb_index2xy[i] ]= qscale_table[ s->mb_index2xy[i-1] ]+2;
00282 }
00283 for(i=s->mb_num-2; i>=0; i--){
00284 if(qscale_table[ s->mb_index2xy[i] ] - qscale_table[ s->mb_index2xy[i+1] ] >2)
00285 qscale_table[ s->mb_index2xy[i] ]= qscale_table[ s->mb_index2xy[i+1] ]+2;
00286 }
00287
00288 if(s->codec_id != AV_CODEC_ID_H263P){
00289 for(i=1; i<s->mb_num; i++){
00290 int mb_xy= s->mb_index2xy[i];
00291
00292 if(qscale_table[mb_xy] != qscale_table[s->mb_index2xy[i-1]] && (s->mb_type[mb_xy]&CANDIDATE_MB_TYPE_INTER4V)){
00293 s->mb_type[mb_xy]|= CANDIDATE_MB_TYPE_INTER;
00294 }
00295 }
00296 }
00297 }
00298
00299 static const int dquant_code[5]= {1,0,9,2,3};
00300
00306 static void h263_encode_block(MpegEncContext * s, DCTELEM * block, int n)
00307 {
00308 int level, run, last, i, j, last_index, last_non_zero, sign, slevel, code;
00309 RLTable *rl;
00310
00311 rl = &ff_h263_rl_inter;
00312 if (s->mb_intra && !s->h263_aic) {
00313
00314 level = block[0];
00315
00316 if (level > 254) {
00317 level = 254;
00318 block[0] = 254;
00319 }
00320
00321 else if (level < 1) {
00322 level = 1;
00323 block[0] = 1;
00324 }
00325 if (level == 128)
00326 put_bits(&s->pb, 8, 0xff);
00327 else
00328 put_bits(&s->pb, 8, level);
00329 i = 1;
00330 } else {
00331 i = 0;
00332 if (s->h263_aic && s->mb_intra)
00333 rl = &ff_rl_intra_aic;
00334
00335 if(s->alt_inter_vlc && !s->mb_intra){
00336 int aic_vlc_bits=0;
00337 int inter_vlc_bits=0;
00338 int wrong_pos=-1;
00339 int aic_code;
00340
00341 last_index = s->block_last_index[n];
00342 last_non_zero = i - 1;
00343 for (; i <= last_index; i++) {
00344 j = s->intra_scantable.permutated[i];
00345 level = block[j];
00346 if (level) {
00347 run = i - last_non_zero - 1;
00348 last = (i == last_index);
00349
00350 if(level<0) level= -level;
00351
00352 code = get_rl_index(rl, last, run, level);
00353 aic_code = get_rl_index(&ff_rl_intra_aic, last, run, level);
00354 inter_vlc_bits += rl->table_vlc[code][1]+1;
00355 aic_vlc_bits += ff_rl_intra_aic.table_vlc[aic_code][1]+1;
00356
00357 if (code == rl->n) {
00358 inter_vlc_bits += 1+6+8-1;
00359 }
00360 if (aic_code == ff_rl_intra_aic.n) {
00361 aic_vlc_bits += 1+6+8-1;
00362 wrong_pos += run + 1;
00363 }else
00364 wrong_pos += wrong_run[aic_code];
00365 last_non_zero = i;
00366 }
00367 }
00368 i = 0;
00369 if(aic_vlc_bits < inter_vlc_bits && wrong_pos > 63)
00370 rl = &ff_rl_intra_aic;
00371 }
00372 }
00373
00374
00375 last_index = s->block_last_index[n];
00376 last_non_zero = i - 1;
00377 for (; i <= last_index; i++) {
00378 j = s->intra_scantable.permutated[i];
00379 level = block[j];
00380 if (level) {
00381 run = i - last_non_zero - 1;
00382 last = (i == last_index);
00383 sign = 0;
00384 slevel = level;
00385 if (level < 0) {
00386 sign = 1;
00387 level = -level;
00388 }
00389 code = get_rl_index(rl, last, run, level);
00390 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
00391 if (code == rl->n) {
00392 if(!CONFIG_FLV_ENCODER || s->h263_flv <= 1){
00393 put_bits(&s->pb, 1, last);
00394 put_bits(&s->pb, 6, run);
00395
00396 av_assert2(slevel != 0);
00397
00398 if(level < 128)
00399 put_sbits(&s->pb, 8, slevel);
00400 else{
00401 put_bits(&s->pb, 8, 128);
00402 put_sbits(&s->pb, 5, slevel);
00403 put_sbits(&s->pb, 6, slevel>>5);
00404 }
00405 }else{
00406 ff_flv2_encode_ac_esc(&s->pb, slevel, level, run, last);
00407 }
00408 } else {
00409 put_bits(&s->pb, 1, sign);
00410 }
00411 last_non_zero = i;
00412 }
00413 }
00414 }
00415
00416
00417 static void h263p_encode_umotion(MpegEncContext * s, int val)
00418 {
00419 short sval = 0;
00420 short i = 0;
00421 short n_bits = 0;
00422 short temp_val;
00423 int code = 0;
00424 int tcode;
00425
00426 if ( val == 0)
00427 put_bits(&s->pb, 1, 1);
00428 else if (val == 1)
00429 put_bits(&s->pb, 3, 0);
00430 else if (val == -1)
00431 put_bits(&s->pb, 3, 2);
00432 else {
00433
00434 sval = ((val < 0) ? (short)(-val):(short)val);
00435 temp_val = sval;
00436
00437 while (temp_val != 0) {
00438 temp_val = temp_val >> 1;
00439 n_bits++;
00440 }
00441
00442 i = n_bits - 1;
00443 while (i > 0) {
00444 tcode = (sval & (1 << (i-1))) >> (i-1);
00445 tcode = (tcode << 1) | 1;
00446 code = (code << 2) | tcode;
00447 i--;
00448 }
00449 code = ((code << 1) | (val < 0)) << 1;
00450 put_bits(&s->pb, (2*n_bits)+1, code);
00451 }
00452 }
00453
00454 void ff_h263_encode_mb(MpegEncContext * s,
00455 DCTELEM block[6][64],
00456 int motion_x, int motion_y)
00457 {
00458 int cbpc, cbpy, i, cbp, pred_x, pred_y;
00459 int16_t pred_dc;
00460 int16_t rec_intradc[6];
00461 int16_t *dc_ptr[6];
00462 const int interleaved_stats= (s->flags&CODEC_FLAG_PASS1);
00463
00464 if (!s->mb_intra) {
00465
00466 cbp= get_p_cbp(s, block, motion_x, motion_y);
00467
00468 if ((cbp | motion_x | motion_y | s->dquant | (s->mv_type - MV_TYPE_16X16)) == 0) {
00469
00470 put_bits(&s->pb, 1, 1);
00471 if(interleaved_stats){
00472 s->misc_bits++;
00473 s->last_bits++;
00474 }
00475 s->skip_count++;
00476
00477 return;
00478 }
00479 put_bits(&s->pb, 1, 0);
00480
00481 cbpc = cbp & 3;
00482 cbpy = cbp >> 2;
00483 if(s->alt_inter_vlc==0 || cbpc!=3)
00484 cbpy ^= 0xF;
00485 if(s->dquant) cbpc+= 8;
00486 if(s->mv_type==MV_TYPE_16X16){
00487 put_bits(&s->pb,
00488 ff_h263_inter_MCBPC_bits[cbpc],
00489 ff_h263_inter_MCBPC_code[cbpc]);
00490
00491 put_bits(&s->pb, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]);
00492 if(s->dquant)
00493 put_bits(&s->pb, 2, dquant_code[s->dquant+2]);
00494
00495 if(interleaved_stats){
00496 s->misc_bits+= get_bits_diff(s);
00497 }
00498
00499
00500 ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
00501
00502 if (!s->umvplus) {
00503 ff_h263_encode_motion_vector(s, motion_x - pred_x,
00504 motion_y - pred_y, 1);
00505 }
00506 else {
00507 h263p_encode_umotion(s, motion_x - pred_x);
00508 h263p_encode_umotion(s, motion_y - pred_y);
00509 if (((motion_x - pred_x) == 1) && ((motion_y - pred_y) == 1))
00510
00511 put_bits(&s->pb,1,1);
00512 }
00513 }else{
00514 put_bits(&s->pb,
00515 ff_h263_inter_MCBPC_bits[cbpc+16],
00516 ff_h263_inter_MCBPC_code[cbpc+16]);
00517 put_bits(&s->pb, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]);
00518 if(s->dquant)
00519 put_bits(&s->pb, 2, dquant_code[s->dquant+2]);
00520
00521 if(interleaved_stats){
00522 s->misc_bits+= get_bits_diff(s);
00523 }
00524
00525 for(i=0; i<4; i++){
00526
00527 ff_h263_pred_motion(s, i, 0, &pred_x, &pred_y);
00528
00529 motion_x = s->current_picture.f.motion_val[0][s->block_index[i]][0];
00530 motion_y = s->current_picture.f.motion_val[0][s->block_index[i]][1];
00531 if (!s->umvplus) {
00532 ff_h263_encode_motion_vector(s, motion_x - pred_x,
00533 motion_y - pred_y, 1);
00534 }
00535 else {
00536 h263p_encode_umotion(s, motion_x - pred_x);
00537 h263p_encode_umotion(s, motion_y - pred_y);
00538 if (((motion_x - pred_x) == 1) && ((motion_y - pred_y) == 1))
00539
00540 put_bits(&s->pb,1,1);
00541 }
00542 }
00543 }
00544
00545 if(interleaved_stats){
00546 s->mv_bits+= get_bits_diff(s);
00547 }
00548 } else {
00549 av_assert2(s->mb_intra);
00550
00551 cbp = 0;
00552 if (s->h263_aic) {
00553
00554 for(i=0; i<6; i++) {
00555 int16_t level = block[i][0];
00556 int scale;
00557
00558 if(i<4) scale= s->y_dc_scale;
00559 else scale= s->c_dc_scale;
00560
00561 pred_dc = ff_h263_pred_dc(s, i, &dc_ptr[i]);
00562 level -= pred_dc;
00563
00564 if (level >= 0)
00565 level = (level + (scale>>1))/scale;
00566 else
00567 level = (level - (scale>>1))/scale;
00568
00569
00570 if (level == 0 && s->block_last_index[i] == 0)
00571 s->block_last_index[i] = -1;
00572
00573 if(!s->modified_quant){
00574 if (level < -127)
00575 level = -127;
00576 else if (level > 127)
00577 level = 127;
00578 }
00579
00580 block[i][0] = level;
00581
00582 rec_intradc[i] = scale*level + pred_dc;
00583
00584 rec_intradc[i] |= 1;
00585
00586
00587
00588 if (rec_intradc[i] < 0)
00589 rec_intradc[i] = 0;
00590 else if (rec_intradc[i] > 2047)
00591 rec_intradc[i] = 2047;
00592
00593
00594 *dc_ptr[i] = rec_intradc[i];
00595 if (s->block_last_index[i] >= 0)
00596 cbp |= 1 << (5 - i);
00597 }
00598 }else{
00599 for(i=0; i<6; i++) {
00600
00601 if (s->block_last_index[i] >= 1)
00602 cbp |= 1 << (5 - i);
00603 }
00604 }
00605
00606 cbpc = cbp & 3;
00607 if (s->pict_type == AV_PICTURE_TYPE_I) {
00608 if(s->dquant) cbpc+=4;
00609 put_bits(&s->pb,
00610 ff_h263_intra_MCBPC_bits[cbpc],
00611 ff_h263_intra_MCBPC_code[cbpc]);
00612 } else {
00613 if(s->dquant) cbpc+=8;
00614 put_bits(&s->pb, 1, 0);
00615 put_bits(&s->pb,
00616 ff_h263_inter_MCBPC_bits[cbpc + 4],
00617 ff_h263_inter_MCBPC_code[cbpc + 4]);
00618 }
00619 if (s->h263_aic) {
00620
00621 put_bits(&s->pb, 1, 0);
00622 }
00623 cbpy = cbp >> 2;
00624 put_bits(&s->pb, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]);
00625 if(s->dquant)
00626 put_bits(&s->pb, 2, dquant_code[s->dquant+2]);
00627
00628 if(interleaved_stats){
00629 s->misc_bits+= get_bits_diff(s);
00630 }
00631 }
00632
00633 for(i=0; i<6; i++) {
00634
00635 h263_encode_block(s, block[i], i);
00636
00637
00638 if (s->h263_aic && s->mb_intra) {
00639 block[i][0] = rec_intradc[i];
00640
00641 }
00642 }
00643
00644 if(interleaved_stats){
00645 if (!s->mb_intra) {
00646 s->p_tex_bits+= get_bits_diff(s);
00647 s->f_count++;
00648 }else{
00649 s->i_tex_bits+= get_bits_diff(s);
00650 s->i_count++;
00651 }
00652 }
00653 }
00654
00655 void ff_h263_encode_motion(MpegEncContext * s, int val, int f_code)
00656 {
00657 int range, bit_size, sign, code, bits;
00658
00659 if (val == 0) {
00660
00661 code = 0;
00662 put_bits(&s->pb, ff_mvtab[code][1], ff_mvtab[code][0]);
00663 } else {
00664 bit_size = f_code - 1;
00665 range = 1 << bit_size;
00666
00667 val = sign_extend(val, 6 + bit_size);
00668 sign = val>>31;
00669 val= (val^sign)-sign;
00670 sign&=1;
00671
00672 val--;
00673 code = (val >> bit_size) + 1;
00674 bits = val & (range - 1);
00675
00676 put_bits(&s->pb, ff_mvtab[code][1] + 1, (ff_mvtab[code][0] << 1) | sign);
00677 if (bit_size > 0) {
00678 put_bits(&s->pb, bit_size, bits);
00679 }
00680 }
00681 }
00682
00683 static void init_mv_penalty_and_fcode(MpegEncContext *s)
00684 {
00685 int f_code;
00686 int mv;
00687
00688 for(f_code=1; f_code<=MAX_FCODE; f_code++){
00689 for(mv=-MAX_MV; mv<=MAX_MV; mv++){
00690 int len;
00691
00692 if(mv==0) len= ff_mvtab[0][1];
00693 else{
00694 int val, bit_size, code;
00695
00696 bit_size = f_code - 1;
00697
00698 val=mv;
00699 if (val < 0)
00700 val = -val;
00701 val--;
00702 code = (val >> bit_size) + 1;
00703 if(code<33){
00704 len= ff_mvtab[code][1] + 1 + bit_size;
00705 }else{
00706 len= ff_mvtab[32][1] + av_log2(code>>5) + 2 + bit_size;
00707 }
00708 }
00709
00710 mv_penalty[f_code][mv+MAX_MV]= len;
00711 }
00712 }
00713
00714 for(f_code=MAX_FCODE; f_code>0; f_code--){
00715 for(mv=-(16<<f_code); mv<(16<<f_code); mv++){
00716 fcode_tab[mv+MAX_MV]= f_code;
00717 }
00718 }
00719
00720 for(mv=0; mv<MAX_MV*2+1; mv++){
00721 umv_fcode_tab[mv]= 1;
00722 }
00723 }
00724
00725 static void init_uni_h263_rl_tab(RLTable *rl, uint32_t *bits_tab, uint8_t *len_tab){
00726 int slevel, run, last;
00727
00728 av_assert0(MAX_LEVEL >= 64);
00729 av_assert0(MAX_RUN >= 63);
00730
00731 for(slevel=-64; slevel<64; slevel++){
00732 if(slevel==0) continue;
00733 for(run=0; run<64; run++){
00734 for(last=0; last<=1; last++){
00735 const int index= UNI_MPEG4_ENC_INDEX(last, run, slevel+64);
00736 int level= slevel < 0 ? -slevel : slevel;
00737 int sign= slevel < 0 ? 1 : 0;
00738 int bits, len, code;
00739
00740 len_tab[index]= 100;
00741
00742
00743 code= get_rl_index(rl, last, run, level);
00744 bits= rl->table_vlc[code][0];
00745 len= rl->table_vlc[code][1];
00746 bits=bits*2+sign; len++;
00747
00748 if(code!=rl->n && len < len_tab[index]){
00749 if(bits_tab) bits_tab[index]= bits;
00750 len_tab [index]= len;
00751 }
00752
00753 bits= rl->table_vlc[rl->n][0];
00754 len = rl->table_vlc[rl->n][1];
00755 bits=bits*2+last; len++;
00756 bits=bits*64+run; len+=6;
00757 bits=bits*256+(level&0xff); len+=8;
00758
00759 if(len < len_tab[index]){
00760 if(bits_tab) bits_tab[index]= bits;
00761 len_tab [index]= len;
00762 }
00763 }
00764 }
00765 }
00766 }
00767
00768 void ff_h263_encode_init(MpegEncContext *s)
00769 {
00770 static int done = 0;
00771
00772 if (!done) {
00773 done = 1;
00774
00775 ff_init_rl(&ff_h263_rl_inter, ff_h263_static_rl_table_store[0]);
00776 ff_init_rl(&ff_rl_intra_aic, ff_h263_static_rl_table_store[1]);
00777
00778 init_uni_h263_rl_tab(&ff_rl_intra_aic, NULL, uni_h263_intra_aic_rl_len);
00779 init_uni_h263_rl_tab(&ff_h263_rl_inter , NULL, uni_h263_inter_rl_len);
00780
00781 init_mv_penalty_and_fcode(s);
00782 }
00783 s->me.mv_penalty= mv_penalty;
00784
00785 s->intra_ac_vlc_length =s->inter_ac_vlc_length = uni_h263_inter_rl_len;
00786 s->intra_ac_vlc_last_length=s->inter_ac_vlc_last_length= uni_h263_inter_rl_len + 128*64;
00787 if(s->h263_aic){
00788 s->intra_ac_vlc_length = uni_h263_intra_aic_rl_len;
00789 s->intra_ac_vlc_last_length= uni_h263_intra_aic_rl_len + 128*64;
00790 }
00791 s->ac_esc_length= 7+1+6+8;
00792
00793
00794 switch(s->codec_id){
00795 case AV_CODEC_ID_MPEG4:
00796 s->fcode_tab= fcode_tab;
00797 break;
00798 case AV_CODEC_ID_H263P:
00799 if(s->umvplus)
00800 s->fcode_tab= umv_fcode_tab;
00801 if(s->modified_quant){
00802 s->min_qcoeff= -2047;
00803 s->max_qcoeff= 2047;
00804 }else{
00805 s->min_qcoeff= -127;
00806 s->max_qcoeff= 127;
00807 }
00808 break;
00809
00810 case AV_CODEC_ID_FLV1:
00811 if (s->h263_flv > 1) {
00812 s->min_qcoeff= -1023;
00813 s->max_qcoeff= 1023;
00814 } else {
00815 s->min_qcoeff= -127;
00816 s->max_qcoeff= 127;
00817 }
00818 s->y_dc_scale_table=
00819 s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
00820 break;
00821 default:
00822 s->min_qcoeff= -127;
00823 s->max_qcoeff= 127;
00824 s->y_dc_scale_table=
00825 s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
00826 }
00827 }
00828
00829 void ff_h263_encode_mba(MpegEncContext *s)
00830 {
00831 int i, mb_pos;
00832
00833 for(i=0; i<6; i++){
00834 if(s->mb_num-1 <= ff_mba_max[i]) break;
00835 }
00836 mb_pos= s->mb_x + s->mb_width*s->mb_y;
00837 put_bits(&s->pb, ff_mba_length[i], mb_pos);
00838 }