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 #include <stdint.h>
00031 #include <string.h>
00032
00033 #include "libavutil/avutil.h"
00034 #include "libavutil/mem.h"
00035 #include "mpegvideo.h"
00036 #include "msmpeg4.h"
00037 #include "h263.h"
00038 #include "mpeg4video.h"
00039 #include "msmpeg4.h"
00040 #include "msmpeg4data.h"
00041 #include "put_bits.h"
00042 #include "rl.h"
00043 #include "vc1data.h"
00044
00045 static uint8_t rl_length[NB_RL_TABLES][MAX_LEVEL+1][MAX_RUN+1][2];
00046
00047
00048 static void init_mv_table(MVTable *tab)
00049 {
00050 int i, x, y;
00051
00052 tab->table_mv_index = av_malloc(sizeof(uint16_t) * 4096);
00053
00054 for(i=0;i<4096;i++)
00055 tab->table_mv_index[i] = tab->n;
00056
00057 for(i=0;i<tab->n;i++) {
00058 x = tab->table_mvx[i];
00059 y = tab->table_mvy[i];
00060 tab->table_mv_index[(x << 6) | y] = i;
00061 }
00062 }
00063
00064 void ff_msmpeg4_code012(PutBitContext *pb, int n)
00065 {
00066 if (n == 0) {
00067 put_bits(pb, 1, 0);
00068 } else {
00069 put_bits(pb, 1, 1);
00070 put_bits(pb, 1, (n >= 2));
00071 }
00072 }
00073
00074 static int get_size_of_code(MpegEncContext * s, RLTable *rl, int last, int run, int level, int intra){
00075 int size=0;
00076 int code;
00077 int run_diff= intra ? 0 : 1;
00078
00079 code = get_rl_index(rl, last, run, level);
00080 size+= rl->table_vlc[code][1];
00081 if (code == rl->n) {
00082 int level1, run1;
00083
00084 level1 = level - rl->max_level[last][run];
00085 if (level1 < 1)
00086 goto esc2;
00087 code = get_rl_index(rl, last, run, level1);
00088 if (code == rl->n) {
00089 esc2:
00090 size++;
00091 if (level > MAX_LEVEL)
00092 goto esc3;
00093 run1 = run - rl->max_run[last][level] - run_diff;
00094 if (run1 < 0)
00095 goto esc3;
00096 code = get_rl_index(rl, last, run1, level);
00097 if (code == rl->n) {
00098 esc3:
00099
00100 size+=1+1+6+8;
00101 } else {
00102
00103 size+= 1+1+ rl->table_vlc[code][1];
00104 }
00105 } else {
00106
00107 size+= 1+1+ rl->table_vlc[code][1];
00108 }
00109 } else {
00110 size++;
00111 }
00112 return size;
00113 }
00114
00115 av_cold void ff_msmpeg4_encode_init(MpegEncContext *s)
00116 {
00117 static int init_done=0;
00118 int i;
00119
00120 ff_msmpeg4_common_init(s);
00121 if(s->msmpeg4_version>=4){
00122 s->min_qcoeff= -255;
00123 s->max_qcoeff= 255;
00124 }
00125
00126 if (!init_done) {
00127
00128 init_done = 1;
00129 init_mv_table(&ff_mv_tables[0]);
00130 init_mv_table(&ff_mv_tables[1]);
00131 for(i=0;i<NB_RL_TABLES;i++)
00132 ff_init_rl(&ff_rl_table[i], ff_static_rl_table_store[i]);
00133
00134 for(i=0; i<NB_RL_TABLES; i++){
00135 int level;
00136 for (level = 1; level <= MAX_LEVEL; level++) {
00137 int run;
00138 for(run=0; run<=MAX_RUN; run++){
00139 int last;
00140 for(last=0; last<2; last++){
00141 rl_length[i][level][run][last]= get_size_of_code(s, &ff_rl_table[ i], last, run, level, 0);
00142 }
00143 }
00144 }
00145 }
00146 }
00147 }
00148
00149 static void find_best_tables(MpegEncContext * s)
00150 {
00151 int i;
00152 int best = 0, best_size = INT_MAX;
00153 int chroma_best = 0, best_chroma_size = INT_MAX;
00154
00155 for(i=0; i<3; i++){
00156 int level;
00157 int chroma_size=0;
00158 int size=0;
00159
00160 if(i>0){
00161 size++;
00162 chroma_size++;
00163 }
00164 for(level=0; level<=MAX_LEVEL; level++){
00165 int run;
00166 for(run=0; run<=MAX_RUN; run++){
00167 int last;
00168 const int last_size= size + chroma_size;
00169 for(last=0; last<2; last++){
00170 int inter_count = s->ac_stats[0][0][level][run][last] + s->ac_stats[0][1][level][run][last];
00171 int intra_luma_count = s->ac_stats[1][0][level][run][last];
00172 int intra_chroma_count= s->ac_stats[1][1][level][run][last];
00173
00174 if(s->pict_type==AV_PICTURE_TYPE_I){
00175 size += intra_luma_count *rl_length[i ][level][run][last];
00176 chroma_size+= intra_chroma_count*rl_length[i+3][level][run][last];
00177 }else{
00178 size+= intra_luma_count *rl_length[i ][level][run][last]
00179 +intra_chroma_count*rl_length[i+3][level][run][last]
00180 +inter_count *rl_length[i+3][level][run][last];
00181 }
00182 }
00183 if(last_size == size+chroma_size) break;
00184 }
00185 }
00186 if(size<best_size){
00187 best_size= size;
00188 best= i;
00189 }
00190 if(chroma_size<best_chroma_size){
00191 best_chroma_size= chroma_size;
00192 chroma_best= i;
00193 }
00194 }
00195
00196
00197
00198
00199 if(s->pict_type==AV_PICTURE_TYPE_P) chroma_best= best;
00200
00201 memset(s->ac_stats, 0, sizeof(int)*(MAX_LEVEL+1)*(MAX_RUN+1)*2*2*2);
00202
00203 s->rl_table_index = best;
00204 s->rl_chroma_table_index= chroma_best;
00205
00206 if(s->pict_type != s->last_non_b_pict_type){
00207 s->rl_table_index= 2;
00208 if(s->pict_type==AV_PICTURE_TYPE_I)
00209 s->rl_chroma_table_index= 1;
00210 else
00211 s->rl_chroma_table_index= 2;
00212 }
00213
00214 }
00215
00216
00217 void ff_msmpeg4_encode_picture_header(MpegEncContext * s, int picture_number)
00218 {
00219 find_best_tables(s);
00220
00221 avpriv_align_put_bits(&s->pb);
00222 put_bits(&s->pb, 2, s->pict_type - 1);
00223
00224 put_bits(&s->pb, 5, s->qscale);
00225 if(s->msmpeg4_version<=2){
00226 s->rl_table_index = 2;
00227 s->rl_chroma_table_index = 2;
00228 }
00229
00230 s->dc_table_index = 1;
00231 s->mv_table_index = 1;
00232 s->use_skip_mb_code = 1;
00233 s->per_mb_rl_table = 0;
00234 if(s->msmpeg4_version==4)
00235 s->inter_intra_pred= (s->width*s->height < 320*240 && s->bit_rate<=II_BITRATE && s->pict_type==AV_PICTURE_TYPE_P);
00236
00237
00238 if (s->pict_type == AV_PICTURE_TYPE_I) {
00239 s->slice_height= s->mb_height/1;
00240 put_bits(&s->pb, 5, 0x16 + s->mb_height/s->slice_height);
00241
00242 if(s->msmpeg4_version==4){
00243 ff_msmpeg4_encode_ext_header(s);
00244 if(s->bit_rate>MBAC_BITRATE)
00245 put_bits(&s->pb, 1, s->per_mb_rl_table);
00246 }
00247
00248 if(s->msmpeg4_version>2){
00249 if(!s->per_mb_rl_table){
00250 ff_msmpeg4_code012(&s->pb, s->rl_chroma_table_index);
00251 ff_msmpeg4_code012(&s->pb, s->rl_table_index);
00252 }
00253
00254 put_bits(&s->pb, 1, s->dc_table_index);
00255 }
00256 } else {
00257 put_bits(&s->pb, 1, s->use_skip_mb_code);
00258
00259 if(s->msmpeg4_version==4 && s->bit_rate>MBAC_BITRATE)
00260 put_bits(&s->pb, 1, s->per_mb_rl_table);
00261
00262 if(s->msmpeg4_version>2){
00263 if(!s->per_mb_rl_table)
00264 ff_msmpeg4_code012(&s->pb, s->rl_table_index);
00265
00266 put_bits(&s->pb, 1, s->dc_table_index);
00267
00268 put_bits(&s->pb, 1, s->mv_table_index);
00269 }
00270 }
00271
00272 s->esc3_level_length= 0;
00273 s->esc3_run_length= 0;
00274 }
00275
00276 void ff_msmpeg4_encode_ext_header(MpegEncContext * s)
00277 {
00278 unsigned fps = s->avctx->time_base.den / s->avctx->time_base.num / FFMAX(s->avctx->ticks_per_frame, 1);
00279 put_bits(&s->pb, 5, FFMIN(fps, 31));
00280
00281 put_bits(&s->pb, 11, FFMIN(s->bit_rate/1024, 2047));
00282
00283 if(s->msmpeg4_version>=3)
00284 put_bits(&s->pb, 1, s->flipflop_rounding);
00285 else
00286 av_assert0(s->flipflop_rounding==0);
00287 }
00288
00289 void ff_msmpeg4_encode_motion(MpegEncContext * s,
00290 int mx, int my)
00291 {
00292 int code;
00293 MVTable *mv;
00294
00295
00296
00297
00298 if (mx <= -64)
00299 mx += 64;
00300 else if (mx >= 64)
00301 mx -= 64;
00302 if (my <= -64)
00303 my += 64;
00304 else if (my >= 64)
00305 my -= 64;
00306
00307 mx += 32;
00308 my += 32;
00309 #if 0
00310 if ((unsigned)mx >= 64 ||
00311 (unsigned)my >= 64)
00312 av_log(s->avctx, AV_LOG_ERROR, "error mx=%d my=%d\n", mx, my);
00313 #endif
00314 mv = &ff_mv_tables[s->mv_table_index];
00315
00316 code = mv->table_mv_index[(mx << 6) | my];
00317 put_bits(&s->pb,
00318 mv->table_mv_bits[code],
00319 mv->table_mv_code[code]);
00320 if (code == mv->n) {
00321
00322 put_bits(&s->pb, 6, mx);
00323 put_bits(&s->pb, 6, my);
00324 }
00325 }
00326
00327 void ff_msmpeg4_handle_slices(MpegEncContext *s){
00328 if (s->mb_x == 0) {
00329 if (s->slice_height && (s->mb_y % s->slice_height) == 0) {
00330 if(s->msmpeg4_version < 4){
00331 ff_mpeg4_clean_buffers(s);
00332 }
00333 s->first_slice_line = 1;
00334 } else {
00335 s->first_slice_line = 0;
00336 }
00337 }
00338 }
00339
00340 static void msmpeg4v2_encode_motion(MpegEncContext * s, int val)
00341 {
00342 int range, bit_size, sign, code, bits;
00343
00344 if (val == 0) {
00345
00346 code = 0;
00347 put_bits(&s->pb, ff_mvtab[code][1], ff_mvtab[code][0]);
00348 } else {
00349 bit_size = s->f_code - 1;
00350 range = 1 << bit_size;
00351 if (val <= -64)
00352 val += 64;
00353 else if (val >= 64)
00354 val -= 64;
00355
00356 if (val >= 0) {
00357 sign = 0;
00358 } else {
00359 val = -val;
00360 sign = 1;
00361 }
00362 val--;
00363 code = (val >> bit_size) + 1;
00364 bits = val & (range - 1);
00365
00366 put_bits(&s->pb, ff_mvtab[code][1] + 1, (ff_mvtab[code][0] << 1) | sign);
00367 if (bit_size > 0) {
00368 put_bits(&s->pb, bit_size, bits);
00369 }
00370 }
00371 }
00372
00373 void ff_msmpeg4_encode_mb(MpegEncContext * s,
00374 DCTELEM block[6][64],
00375 int motion_x, int motion_y)
00376 {
00377 int cbp, coded_cbp, i;
00378 int pred_x, pred_y;
00379 uint8_t *coded_block;
00380
00381 ff_msmpeg4_handle_slices(s);
00382
00383 if (!s->mb_intra) {
00384
00385 cbp = 0;
00386 for (i = 0; i < 6; i++) {
00387 if (s->block_last_index[i] >= 0)
00388 cbp |= 1 << (5 - i);
00389 }
00390 if (s->use_skip_mb_code && (cbp | motion_x | motion_y) == 0) {
00391
00392 put_bits(&s->pb, 1, 1);
00393 s->last_bits++;
00394 s->misc_bits++;
00395 s->skip_count++;
00396
00397 return;
00398 }
00399 if (s->use_skip_mb_code)
00400 put_bits(&s->pb, 1, 0);
00401
00402 if(s->msmpeg4_version<=2){
00403 put_bits(&s->pb,
00404 ff_v2_mb_type[cbp&3][1],
00405 ff_v2_mb_type[cbp&3][0]);
00406 if((cbp&3) != 3) coded_cbp= cbp ^ 0x3C;
00407 else coded_cbp= cbp;
00408
00409 put_bits(&s->pb,
00410 ff_h263_cbpy_tab[coded_cbp>>2][1],
00411 ff_h263_cbpy_tab[coded_cbp>>2][0]);
00412
00413 s->misc_bits += get_bits_diff(s);
00414
00415 ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
00416 msmpeg4v2_encode_motion(s, motion_x - pred_x);
00417 msmpeg4v2_encode_motion(s, motion_y - pred_y);
00418 }else{
00419 put_bits(&s->pb,
00420 ff_table_mb_non_intra[cbp + 64][1],
00421 ff_table_mb_non_intra[cbp + 64][0]);
00422
00423 s->misc_bits += get_bits_diff(s);
00424
00425
00426 ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
00427 ff_msmpeg4_encode_motion(s, motion_x - pred_x,
00428 motion_y - pred_y);
00429 }
00430
00431 s->mv_bits += get_bits_diff(s);
00432
00433 for (i = 0; i < 6; i++) {
00434 ff_msmpeg4_encode_block(s, block[i], i);
00435 }
00436 s->p_tex_bits += get_bits_diff(s);
00437 } else {
00438
00439 cbp = 0;
00440 coded_cbp = 0;
00441 for (i = 0; i < 6; i++) {
00442 int val, pred;
00443 val = (s->block_last_index[i] >= 1);
00444 cbp |= val << (5 - i);
00445 if (i < 4) {
00446
00447 pred = ff_msmpeg4_coded_block_pred(s, i, &coded_block);
00448 *coded_block = val;
00449 val = val ^ pred;
00450 }
00451 coded_cbp |= val << (5 - i);
00452 }
00453
00454 if(s->msmpeg4_version<=2){
00455 if (s->pict_type == AV_PICTURE_TYPE_I) {
00456 put_bits(&s->pb,
00457 ff_v2_intra_cbpc[cbp&3][1], ff_v2_intra_cbpc[cbp&3][0]);
00458 } else {
00459 if (s->use_skip_mb_code)
00460 put_bits(&s->pb, 1, 0);
00461 put_bits(&s->pb,
00462 ff_v2_mb_type[(cbp&3) + 4][1],
00463 ff_v2_mb_type[(cbp&3) + 4][0]);
00464 }
00465 put_bits(&s->pb, 1, 0);
00466 put_bits(&s->pb,
00467 ff_h263_cbpy_tab[cbp>>2][1],
00468 ff_h263_cbpy_tab[cbp>>2][0]);
00469 }else{
00470 if (s->pict_type == AV_PICTURE_TYPE_I) {
00471 put_bits(&s->pb,
00472 ff_msmp4_mb_i_table[coded_cbp][1], ff_msmp4_mb_i_table[coded_cbp][0]);
00473 } else {
00474 if (s->use_skip_mb_code)
00475 put_bits(&s->pb, 1, 0);
00476 put_bits(&s->pb,
00477 ff_table_mb_non_intra[cbp][1],
00478 ff_table_mb_non_intra[cbp][0]);
00479 }
00480 put_bits(&s->pb, 1, 0);
00481 if(s->inter_intra_pred){
00482 s->h263_aic_dir=0;
00483 put_bits(&s->pb, ff_table_inter_intra[s->h263_aic_dir][1], ff_table_inter_intra[s->h263_aic_dir][0]);
00484 }
00485 }
00486 s->misc_bits += get_bits_diff(s);
00487
00488 for (i = 0; i < 6; i++) {
00489 ff_msmpeg4_encode_block(s, block[i], i);
00490 }
00491 s->i_tex_bits += get_bits_diff(s);
00492 s->i_count++;
00493 }
00494 }
00495
00496 static void msmpeg4_encode_dc(MpegEncContext * s, int level, int n, int *dir_ptr)
00497 {
00498 int sign, code;
00499 int pred, extquant;
00500 int extrabits = 0;
00501
00502 int16_t *dc_val;
00503 pred = ff_msmpeg4_pred_dc(s, n, &dc_val, dir_ptr);
00504
00505
00506 if (n < 4) {
00507 *dc_val = level * s->y_dc_scale;
00508 } else {
00509 *dc_val = level * s->c_dc_scale;
00510 }
00511
00512
00513 level -= pred;
00514
00515 if(s->msmpeg4_version<=2){
00516 if (n < 4) {
00517 put_bits(&s->pb,
00518 ff_v2_dc_lum_table[level + 256][1],
00519 ff_v2_dc_lum_table[level + 256][0]);
00520 }else{
00521 put_bits(&s->pb,
00522 ff_v2_dc_chroma_table[level + 256][1],
00523 ff_v2_dc_chroma_table[level + 256][0]);
00524 }
00525 }else{
00526 sign = 0;
00527 if (level < 0) {
00528 level = -level;
00529 sign = 1;
00530 }
00531 code = level;
00532 if (code > DC_MAX)
00533 code = DC_MAX;
00534 else if( s->msmpeg4_version>=6 ) {
00535 if( s->qscale == 1 ) {
00536 extquant = (level + 3) & 0x3;
00537 code = ((level+3)>>2);
00538 } else if( s->qscale == 2 ) {
00539 extquant = (level + 1) & 0x1;
00540 code = ((level+1)>>1);
00541 }
00542 }
00543
00544 if (s->dc_table_index == 0) {
00545 if (n < 4) {
00546 put_bits(&s->pb, ff_table0_dc_lum[code][1], ff_table0_dc_lum[code][0]);
00547 } else {
00548 put_bits(&s->pb, ff_table0_dc_chroma[code][1], ff_table0_dc_chroma[code][0]);
00549 }
00550 } else {
00551 if (n < 4) {
00552 put_bits(&s->pb, ff_table1_dc_lum[code][1], ff_table1_dc_lum[code][0]);
00553 } else {
00554 put_bits(&s->pb, ff_table1_dc_chroma[code][1], ff_table1_dc_chroma[code][0]);
00555 }
00556 }
00557
00558 if(s->msmpeg4_version>=6 && s->qscale<=2)
00559 extrabits = 3 - s->qscale;
00560
00561 if (code == DC_MAX)
00562 put_bits(&s->pb, 8 + extrabits, level);
00563 else if(extrabits > 0)
00564 put_bits(&s->pb, extrabits, extquant);
00565
00566 if (level != 0) {
00567 put_bits(&s->pb, 1, sign);
00568 }
00569 }
00570 }
00571
00572
00573
00574
00575 void ff_msmpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n)
00576 {
00577 int level, run, last, i, j, last_index;
00578 int last_non_zero, sign, slevel;
00579 int code, run_diff, dc_pred_dir;
00580 const RLTable *rl;
00581 const uint8_t *scantable;
00582
00583 if (s->mb_intra) {
00584 msmpeg4_encode_dc(s, block[0], n, &dc_pred_dir);
00585 i = 1;
00586 if (n < 4) {
00587 rl = &ff_rl_table[s->rl_table_index];
00588 } else {
00589 rl = &ff_rl_table[3 + s->rl_chroma_table_index];
00590 }
00591 run_diff = s->msmpeg4_version>=4;
00592 scantable= s->intra_scantable.permutated;
00593 } else {
00594 i = 0;
00595 rl = &ff_rl_table[3 + s->rl_table_index];
00596 if(s->msmpeg4_version<=2)
00597 run_diff = 0;
00598 else
00599 run_diff = 1;
00600 scantable= s->inter_scantable.permutated;
00601 }
00602
00603
00604 if(s->msmpeg4_version>=4 && s->msmpeg4_version<6 && s->block_last_index[n]>0){
00605 for(last_index=63; last_index>=0; last_index--){
00606 if(block[scantable[last_index]]) break;
00607 }
00608 s->block_last_index[n]= last_index;
00609 }else
00610 last_index = s->block_last_index[n];
00611
00612 last_non_zero = i - 1;
00613 for (; i <= last_index; i++) {
00614 j = scantable[i];
00615 level = block[j];
00616 if (level) {
00617 run = i - last_non_zero - 1;
00618 last = (i == last_index);
00619 sign = 0;
00620 slevel = level;
00621 if (level < 0) {
00622 sign = 1;
00623 level = -level;
00624 }
00625
00626 if(level<=MAX_LEVEL && run<=MAX_RUN){
00627 s->ac_stats[s->mb_intra][n>3][level][run][last]++;
00628 }
00629
00630 s->ac_stats[s->mb_intra][n > 3][40][63][0]++;
00631
00632 code = get_rl_index(rl, last, run, level);
00633 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
00634 if (code == rl->n) {
00635 int level1, run1;
00636
00637 level1 = level - rl->max_level[last][run];
00638 if (level1 < 1)
00639 goto esc2;
00640 code = get_rl_index(rl, last, run, level1);
00641 if (code == rl->n) {
00642 esc2:
00643 put_bits(&s->pb, 1, 0);
00644 if (level > MAX_LEVEL)
00645 goto esc3;
00646 run1 = run - rl->max_run[last][level] - run_diff;
00647 if (run1 < 0)
00648 goto esc3;
00649 code = get_rl_index(rl, last, run1+1, level);
00650 if (s->msmpeg4_version == 4 && code == rl->n)
00651 goto esc3;
00652 code = get_rl_index(rl, last, run1, level);
00653 if (code == rl->n) {
00654 esc3:
00655
00656 put_bits(&s->pb, 1, 0);
00657 put_bits(&s->pb, 1, last);
00658 if(s->msmpeg4_version>=4){
00659 if(s->esc3_level_length==0){
00660 s->esc3_level_length=8;
00661 s->esc3_run_length= 6;
00662
00663 if(s->qscale<8)
00664 put_bits(&s->pb, 6 + (s->msmpeg4_version>=6), 3);
00665 else
00666 put_bits(&s->pb, 8, 3);
00667 }
00668 put_bits(&s->pb, s->esc3_run_length, run);
00669 put_bits(&s->pb, 1, sign);
00670 put_bits(&s->pb, s->esc3_level_length, level);
00671 }else{
00672 put_bits(&s->pb, 6, run);
00673 put_sbits(&s->pb, 8, slevel);
00674 }
00675 } else {
00676
00677 put_bits(&s->pb, 1, 1);
00678 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
00679 put_bits(&s->pb, 1, sign);
00680 }
00681 } else {
00682
00683 put_bits(&s->pb, 1, 1);
00684 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
00685 put_bits(&s->pb, 1, sign);
00686 }
00687 } else {
00688 put_bits(&s->pb, 1, sign);
00689 }
00690 last_non_zero = i;
00691 }
00692 }
00693 }