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