00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "mpegvideo.h"
00024 #include "h263.h"
00025 #include "mpeg4video.h"
00026
00027
00028
00029
00030 static uint8_t uni_DCtab_lum_len[512];
00031 static uint8_t uni_DCtab_chrom_len[512];
00032 static uint16_t uni_DCtab_lum_bits[512];
00033 static uint16_t uni_DCtab_chrom_bits[512];
00034
00035
00036
00037 static uint32_t uni_mpeg4_intra_rl_bits[64*64*2*2];
00038 static uint8_t uni_mpeg4_intra_rl_len [64*64*2*2];
00039 static uint32_t uni_mpeg4_inter_rl_bits[64*64*2*2];
00040 static uint8_t uni_mpeg4_inter_rl_len [64*64*2*2];
00041
00042
00043 #define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128*64 + (run)*128 + (level))
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00060 static inline int get_block_rate(MpegEncContext * s, DCTELEM block[64], int block_last_index, uint8_t scantable[64]){
00061 int last=0;
00062 int j;
00063 int rate=0;
00064
00065 for(j=1; j<=block_last_index; j++){
00066 const int index= scantable[j];
00067 int level= block[index];
00068 if(level){
00069 level+= 64;
00070 if((level&(~127)) == 0){
00071 if(j<block_last_index) rate+= s->intra_ac_vlc_length [UNI_AC_ENC_INDEX(j-last-1, level)];
00072 else rate+= s->intra_ac_vlc_last_length[UNI_AC_ENC_INDEX(j-last-1, level)];
00073 }else
00074 rate += s->ac_esc_length;
00075
00076 last= j;
00077 }
00078 }
00079
00080 return rate;
00081 }
00082
00083
00092 static inline void restore_ac_coeffs(MpegEncContext * s, DCTELEM block[6][64], const int dir[6], uint8_t *st[6], const int zigzag_last_index[6])
00093 {
00094 int i, n;
00095 memcpy(s->block_last_index, zigzag_last_index, sizeof(int)*6);
00096
00097 for(n=0; n<6; n++){
00098 int16_t *ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
00099
00100 st[n]= s->intra_scantable.permutated;
00101 if(dir[n]){
00102
00103 for(i=1; i<8; i++){
00104 block[n][s->dsp.idct_permutation[i ]] = ac_val[i+8];
00105 }
00106 }else{
00107
00108 for(i=1; i<8; i++){
00109 block[n][s->dsp.idct_permutation[i<<3]]= ac_val[i ];
00110 }
00111 }
00112 }
00113 }
00114
00123 static inline int decide_ac_pred(MpegEncContext * s, DCTELEM block[6][64], const int dir[6], uint8_t *st[6], int zigzag_last_index[6])
00124 {
00125 int score= 0;
00126 int i, n;
00127 int8_t * const qscale_table= s->current_picture.qscale_table;
00128
00129 memcpy(zigzag_last_index, s->block_last_index, sizeof(int)*6);
00130
00131 for(n=0; n<6; n++){
00132 int16_t *ac_val, *ac_val1;
00133
00134 score -= get_block_rate(s, block[n], s->block_last_index[n], s->intra_scantable.permutated);
00135
00136 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
00137 ac_val1= ac_val;
00138 if(dir[n]){
00139 const int xy= s->mb_x + s->mb_y*s->mb_stride - s->mb_stride;
00140
00141 ac_val-= s->block_wrap[n]*16;
00142 if(s->mb_y==0 || s->qscale == qscale_table[xy] || n==2 || n==3){
00143
00144 for(i=1; i<8; i++){
00145 const int level= block[n][s->dsp.idct_permutation[i ]];
00146 block[n][s->dsp.idct_permutation[i ]] = level - ac_val[i+8];
00147 ac_val1[i ]= block[n][s->dsp.idct_permutation[i<<3]];
00148 ac_val1[i+8]= level;
00149 }
00150 }else{
00151
00152 for(i=1; i<8; i++){
00153 const int level= block[n][s->dsp.idct_permutation[i ]];
00154 block[n][s->dsp.idct_permutation[i ]] = level - ROUNDED_DIV(ac_val[i + 8]*qscale_table[xy], s->qscale);
00155 ac_val1[i ]= block[n][s->dsp.idct_permutation[i<<3]];
00156 ac_val1[i+8]= level;
00157 }
00158 }
00159 st[n]= s->intra_h_scantable.permutated;
00160 }else{
00161 const int xy= s->mb_x-1 + s->mb_y*s->mb_stride;
00162
00163 ac_val-= 16;
00164 if(s->mb_x==0 || s->qscale == qscale_table[xy] || n==1 || n==3){
00165
00166 for(i=1; i<8; i++){
00167 const int level= block[n][s->dsp.idct_permutation[i<<3]];
00168 block[n][s->dsp.idct_permutation[i<<3]]= level - ac_val[i];
00169 ac_val1[i ]= level;
00170 ac_val1[i+8]= block[n][s->dsp.idct_permutation[i ]];
00171 }
00172 }else{
00173
00174 for(i=1; i<8; i++){
00175 const int level= block[n][s->dsp.idct_permutation[i<<3]];
00176 block[n][s->dsp.idct_permutation[i<<3]]= level - ROUNDED_DIV(ac_val[i]*qscale_table[xy], s->qscale);
00177 ac_val1[i ]= level;
00178 ac_val1[i+8]= block[n][s->dsp.idct_permutation[i ]];
00179 }
00180 }
00181 st[n]= s->intra_v_scantable.permutated;
00182 }
00183
00184 for(i=63; i>0; i--)
00185 if(block[n][ st[n][i] ]) break;
00186 s->block_last_index[n]= i;
00187
00188 score += get_block_rate(s, block[n], s->block_last_index[n], st[n]);
00189 }
00190
00191 if(score < 0){
00192 return 1;
00193 }else{
00194 restore_ac_coeffs(s, block, dir, st, zigzag_last_index);
00195 return 0;
00196 }
00197 }
00198
00202 void ff_clean_mpeg4_qscales(MpegEncContext *s){
00203 int i;
00204 int8_t * const qscale_table= s->current_picture.qscale_table;
00205
00206 ff_clean_h263_qscales(s);
00207
00208 if(s->pict_type== AV_PICTURE_TYPE_B){
00209 int odd=0;
00210
00211
00212 for(i=0; i<s->mb_num; i++){
00213 int mb_xy= s->mb_index2xy[i];
00214 odd += qscale_table[mb_xy]&1;
00215 }
00216
00217 if(2*odd > s->mb_num) odd=1;
00218 else odd=0;
00219
00220 for(i=0; i<s->mb_num; i++){
00221 int mb_xy= s->mb_index2xy[i];
00222 if((qscale_table[mb_xy]&1) != odd)
00223 qscale_table[mb_xy]++;
00224 if(qscale_table[mb_xy] > 31)
00225 qscale_table[mb_xy]= 31;
00226 }
00227
00228 for(i=1; i<s->mb_num; i++){
00229 int mb_xy= s->mb_index2xy[i];
00230 if(qscale_table[mb_xy] != qscale_table[s->mb_index2xy[i-1]] && (s->mb_type[mb_xy]&CANDIDATE_MB_TYPE_DIRECT)){
00231 s->mb_type[mb_xy]|= CANDIDATE_MB_TYPE_BIDIR;
00232 }
00233 }
00234 }
00235 }
00236
00237
00242 static inline void mpeg4_encode_dc(PutBitContext * s, int level, int n)
00243 {
00244 #if 1
00245
00246 level+=256;
00247 if (n < 4) {
00248
00249 put_bits(s, uni_DCtab_lum_len[level], uni_DCtab_lum_bits[level]);
00250 } else {
00251
00252 put_bits(s, uni_DCtab_chrom_len[level], uni_DCtab_chrom_bits[level]);
00253 }
00254 #else
00255 int size, v;
00256
00257 size = 0;
00258 v = abs(level);
00259 while (v) {
00260 v >>= 1;
00261 size++;
00262 }
00263
00264 if (n < 4) {
00265
00266 put_bits(&s->pb, ff_mpeg4_DCtab_lum[size][1], ff_mpeg4_DCtab_lum[size][0]);
00267 } else {
00268
00269 put_bits(&s->pb, ff_mpeg4_DCtab_chrom[size][1], ff_mpeg4_DCtab_chrom[size][0]);
00270 }
00271
00272
00273 if (size > 0) {
00274 if (level < 0)
00275 level = (-level) ^ ((1 << size) - 1);
00276 put_bits(&s->pb, size, level);
00277 if (size > 8)
00278 put_bits(&s->pb, 1, 1);
00279 }
00280 #endif
00281 }
00282
00283 static inline int mpeg4_get_dc_length(int level, int n){
00284 if (n < 4) {
00285 return uni_DCtab_lum_len[level + 256];
00286 } else {
00287 return uni_DCtab_chrom_len[level + 256];
00288 }
00289 }
00290
00295 static inline void mpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n, int intra_dc,
00296 uint8_t *scan_table, PutBitContext *dc_pb, PutBitContext *ac_pb)
00297 {
00298 int i, last_non_zero;
00299 #if 0 //variables for the outcommented version
00300 int code, sign, last;
00301 #endif
00302 const RLTable *rl;
00303 uint32_t *bits_tab;
00304 uint8_t *len_tab;
00305 const int last_index = s->block_last_index[n];
00306
00307 if (s->mb_intra) {
00308
00309 mpeg4_encode_dc(dc_pb, intra_dc, n);
00310 if(last_index<1) return;
00311 i = 1;
00312 rl = &ff_mpeg4_rl_intra;
00313 bits_tab= uni_mpeg4_intra_rl_bits;
00314 len_tab = uni_mpeg4_intra_rl_len;
00315 } else {
00316 if(last_index<0) return;
00317 i = 0;
00318 rl = &ff_h263_rl_inter;
00319 bits_tab= uni_mpeg4_inter_rl_bits;
00320 len_tab = uni_mpeg4_inter_rl_len;
00321 }
00322
00323
00324 last_non_zero = i - 1;
00325 #if 1
00326 for (; i < last_index; i++) {
00327 int level = block[ scan_table[i] ];
00328 if (level) {
00329 int run = i - last_non_zero - 1;
00330 level+=64;
00331 if((level&(~127)) == 0){
00332 const int index= UNI_MPEG4_ENC_INDEX(0, run, level);
00333 put_bits(ac_pb, len_tab[index], bits_tab[index]);
00334 }else{
00335 put_bits(ac_pb, 7+2+1+6+1+12+1, (3<<23)+(3<<21)+(0<<20)+(run<<14)+(1<<13)+(((level-64)&0xfff)<<1)+1);
00336 }
00337 last_non_zero = i;
00338 }
00339 }
00340 {
00341 int level = block[ scan_table[i] ];
00342 int run = i - last_non_zero - 1;
00343 level+=64;
00344 if((level&(~127)) == 0){
00345 const int index= UNI_MPEG4_ENC_INDEX(1, run, level);
00346 put_bits(ac_pb, len_tab[index], bits_tab[index]);
00347 }else{
00348 put_bits(ac_pb, 7+2+1+6+1+12+1, (3<<23)+(3<<21)+(1<<20)+(run<<14)+(1<<13)+(((level-64)&0xfff)<<1)+1);
00349 }
00350 }
00351 #else
00352 for (; i <= last_index; i++) {
00353 const int slevel = block[ scan_table[i] ];
00354 if (slevel) {
00355 int level;
00356 int run = i - last_non_zero - 1;
00357 last = (i == last_index);
00358 sign = 0;
00359 level = slevel;
00360 if (level < 0) {
00361 sign = 1;
00362 level = -level;
00363 }
00364 code = get_rl_index(rl, last, run, level);
00365 put_bits(ac_pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
00366 if (code == rl->n) {
00367 int level1, run1;
00368 level1 = level - rl->max_level[last][run];
00369 if (level1 < 1)
00370 goto esc2;
00371 code = get_rl_index(rl, last, run, level1);
00372 if (code == rl->n) {
00373 esc2:
00374 put_bits(ac_pb, 1, 1);
00375 if (level > MAX_LEVEL)
00376 goto esc3;
00377 run1 = run - rl->max_run[last][level] - 1;
00378 if (run1 < 0)
00379 goto esc3;
00380 code = get_rl_index(rl, last, run1, level);
00381 if (code == rl->n) {
00382 esc3:
00383
00384 put_bits(ac_pb, 1, 1);
00385 put_bits(ac_pb, 1, last);
00386 put_bits(ac_pb, 6, run);
00387 put_bits(ac_pb, 1, 1);
00388 put_sbits(ac_pb, 12, slevel);
00389 put_bits(ac_pb, 1, 1);
00390 } else {
00391
00392 put_bits(ac_pb, 1, 0);
00393 put_bits(ac_pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
00394 put_bits(ac_pb, 1, sign);
00395 }
00396 } else {
00397
00398 put_bits(ac_pb, 1, 0);
00399 put_bits(ac_pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
00400 put_bits(ac_pb, 1, sign);
00401 }
00402 } else {
00403 put_bits(ac_pb, 1, sign);
00404 }
00405 last_non_zero = i;
00406 }
00407 }
00408 #endif
00409 }
00410
00411 static int mpeg4_get_block_length(MpegEncContext * s, DCTELEM * block, int n, int intra_dc,
00412 uint8_t *scan_table)
00413 {
00414 int i, last_non_zero;
00415 uint8_t *len_tab;
00416 const int last_index = s->block_last_index[n];
00417 int len=0;
00418
00419 if (s->mb_intra) {
00420
00421 len += mpeg4_get_dc_length(intra_dc, n);
00422 if(last_index<1) return len;
00423 i = 1;
00424 len_tab = uni_mpeg4_intra_rl_len;
00425 } else {
00426 if(last_index<0) return 0;
00427 i = 0;
00428 len_tab = uni_mpeg4_inter_rl_len;
00429 }
00430
00431
00432 last_non_zero = i - 1;
00433 for (; i < last_index; i++) {
00434 int level = block[ scan_table[i] ];
00435 if (level) {
00436 int run = i - last_non_zero - 1;
00437 level+=64;
00438 if((level&(~127)) == 0){
00439 const int index= UNI_MPEG4_ENC_INDEX(0, run, level);
00440 len += len_tab[index];
00441 }else{
00442 len += 7+2+1+6+1+12+1;
00443 }
00444 last_non_zero = i;
00445 }
00446 }
00447 {
00448 int level = block[ scan_table[i] ];
00449 int run = i - last_non_zero - 1;
00450 level+=64;
00451 if((level&(~127)) == 0){
00452 const int index= UNI_MPEG4_ENC_INDEX(1, run, level);
00453 len += len_tab[index];
00454 }else{
00455 len += 7+2+1+6+1+12+1;
00456 }
00457 }
00458
00459 return len;
00460 }
00461
00462 static inline void mpeg4_encode_blocks(MpegEncContext * s, DCTELEM block[6][64], int intra_dc[6],
00463 uint8_t **scan_table, PutBitContext *dc_pb, PutBitContext *ac_pb){
00464 int i;
00465
00466 if(scan_table){
00467 if(s->flags2 & CODEC_FLAG2_NO_OUTPUT){
00468 for (i = 0; i < 6; i++) {
00469 skip_put_bits(&s->pb, mpeg4_get_block_length(s, block[i], i, intra_dc[i], scan_table[i]));
00470 }
00471 }else{
00472
00473 for (i = 0; i < 6; i++) {
00474 mpeg4_encode_block(s, block[i], i, intra_dc[i], scan_table[i], dc_pb, ac_pb);
00475 }
00476 }
00477 }else{
00478 if(s->flags2 & CODEC_FLAG2_NO_OUTPUT){
00479 for (i = 0; i < 6; i++) {
00480 skip_put_bits(&s->pb, mpeg4_get_block_length(s, block[i], i, 0, s->intra_scantable.permutated));
00481 }
00482 }else{
00483
00484 for (i = 0; i < 6; i++) {
00485 mpeg4_encode_block(s, block[i], i, 0, s->intra_scantable.permutated, dc_pb, ac_pb);
00486 }
00487 }
00488 }
00489 }
00490
00491
00492 static const int dquant_code[5]= {1,0,9,2,3};
00493
00494 void mpeg4_encode_mb(MpegEncContext * s,
00495 DCTELEM block[6][64],
00496 int motion_x, int motion_y)
00497 {
00498 int cbpc, cbpy, pred_x, pred_y;
00499 PutBitContext * const pb2 = s->data_partitioning ? &s->pb2 : &s->pb;
00500 PutBitContext * const tex_pb = s->data_partitioning && s->pict_type!=AV_PICTURE_TYPE_B ? &s->tex_pb : &s->pb;
00501 PutBitContext * const dc_pb = s->data_partitioning && s->pict_type!=AV_PICTURE_TYPE_I ? &s->pb2 : &s->pb;
00502 const int interleaved_stats= (s->flags&CODEC_FLAG_PASS1) && !s->data_partitioning ? 1 : 0;
00503
00504 if (!s->mb_intra) {
00505 int i, cbp;
00506
00507 if(s->pict_type==AV_PICTURE_TYPE_B){
00508 static const int mb_type_table[8]= {-1, 3, 2, 1,-1,-1,-1, 0};
00509 int mb_type= mb_type_table[s->mv_dir];
00510
00511 if(s->mb_x==0){
00512 for(i=0; i<2; i++){
00513 s->last_mv[i][0][0]=
00514 s->last_mv[i][0][1]=
00515 s->last_mv[i][1][0]=
00516 s->last_mv[i][1][1]= 0;
00517 }
00518 }
00519
00520 assert(s->dquant>=-2 && s->dquant<=2);
00521 assert((s->dquant&1)==0);
00522 assert(mb_type>=0);
00523
00524
00525 if(s->next_picture.mbskip_table[s->mb_y * s->mb_stride + s->mb_x]){
00526 s->skip_count++;
00527 s->mv[0][0][0]=
00528 s->mv[0][0][1]=
00529 s->mv[1][0][0]=
00530 s->mv[1][0][1]= 0;
00531 s->mv_dir= MV_DIR_FORWARD;
00532 s->qscale -= s->dquant;
00533
00534
00535 return;
00536 }
00537
00538 cbp= get_b_cbp(s, block, motion_x, motion_y, mb_type);
00539
00540 if ((cbp | motion_x | motion_y | mb_type) ==0) {
00541
00542 assert(s->dquant==0);
00543
00544 put_bits(&s->pb, 1, 1);
00545
00546 if(interleaved_stats){
00547 s->misc_bits++;
00548 s->last_bits++;
00549 }
00550 s->skip_count++;
00551 return;
00552 }
00553
00554 put_bits(&s->pb, 1, 0);
00555 put_bits(&s->pb, 1, cbp ? 0 : 1);
00556 put_bits(&s->pb, mb_type+1, 1);
00557 if(cbp) put_bits(&s->pb, 6, cbp);
00558
00559 if(cbp && mb_type){
00560 if(s->dquant)
00561 put_bits(&s->pb, 2, (s->dquant>>2)+3);
00562 else
00563 put_bits(&s->pb, 1, 0);
00564 }else
00565 s->qscale -= s->dquant;
00566
00567 if(!s->progressive_sequence){
00568 if(cbp)
00569 put_bits(&s->pb, 1, s->interlaced_dct);
00570 if(mb_type)
00571 put_bits(&s->pb, 1, s->mv_type == MV_TYPE_FIELD);
00572 }
00573
00574 if(interleaved_stats){
00575 s->misc_bits+= get_bits_diff(s);
00576 }
00577
00578 if(mb_type == 0){
00579 assert(s->mv_dir & MV_DIRECT);
00580 ff_h263_encode_motion_vector(s, motion_x, motion_y, 1);
00581 s->b_count++;
00582 s->f_count++;
00583 }else{
00584 assert(mb_type > 0 && mb_type < 4);
00585 if(s->mv_type != MV_TYPE_FIELD){
00586 if(s->mv_dir & MV_DIR_FORWARD){
00587 ff_h263_encode_motion_vector(s, s->mv[0][0][0] - s->last_mv[0][0][0],
00588 s->mv[0][0][1] - s->last_mv[0][0][1], s->f_code);
00589 s->last_mv[0][0][0]= s->last_mv[0][1][0]= s->mv[0][0][0];
00590 s->last_mv[0][0][1]= s->last_mv[0][1][1]= s->mv[0][0][1];
00591 s->f_count++;
00592 }
00593 if(s->mv_dir & MV_DIR_BACKWARD){
00594 ff_h263_encode_motion_vector(s, s->mv[1][0][0] - s->last_mv[1][0][0],
00595 s->mv[1][0][1] - s->last_mv[1][0][1], s->b_code);
00596 s->last_mv[1][0][0]= s->last_mv[1][1][0]= s->mv[1][0][0];
00597 s->last_mv[1][0][1]= s->last_mv[1][1][1]= s->mv[1][0][1];
00598 s->b_count++;
00599 }
00600 }else{
00601 if(s->mv_dir & MV_DIR_FORWARD){
00602 put_bits(&s->pb, 1, s->field_select[0][0]);
00603 put_bits(&s->pb, 1, s->field_select[0][1]);
00604 }
00605 if(s->mv_dir & MV_DIR_BACKWARD){
00606 put_bits(&s->pb, 1, s->field_select[1][0]);
00607 put_bits(&s->pb, 1, s->field_select[1][1]);
00608 }
00609 if(s->mv_dir & MV_DIR_FORWARD){
00610 for(i=0; i<2; i++){
00611 ff_h263_encode_motion_vector(s, s->mv[0][i][0] - s->last_mv[0][i][0] ,
00612 s->mv[0][i][1] - s->last_mv[0][i][1]/2, s->f_code);
00613 s->last_mv[0][i][0]= s->mv[0][i][0];
00614 s->last_mv[0][i][1]= s->mv[0][i][1]*2;
00615 }
00616 s->f_count++;
00617 }
00618 if(s->mv_dir & MV_DIR_BACKWARD){
00619 for(i=0; i<2; i++){
00620 ff_h263_encode_motion_vector(s, s->mv[1][i][0] - s->last_mv[1][i][0] ,
00621 s->mv[1][i][1] - s->last_mv[1][i][1]/2, s->b_code);
00622 s->last_mv[1][i][0]= s->mv[1][i][0];
00623 s->last_mv[1][i][1]= s->mv[1][i][1]*2;
00624 }
00625 s->b_count++;
00626 }
00627 }
00628 }
00629
00630 if(interleaved_stats){
00631 s->mv_bits+= get_bits_diff(s);
00632 }
00633
00634 mpeg4_encode_blocks(s, block, NULL, NULL, NULL, &s->pb);
00635
00636 if(interleaved_stats){
00637 s->p_tex_bits+= get_bits_diff(s);
00638 }
00639
00640 }else{
00641 cbp= get_p_cbp(s, block, motion_x, motion_y);
00642
00643 if ((cbp | motion_x | motion_y | s->dquant) == 0 && s->mv_type==MV_TYPE_16X16) {
00644
00645
00646 if(s->max_b_frames>0){
00647 int i;
00648 int x,y, offset;
00649 uint8_t *p_pic;
00650
00651 x= s->mb_x*16;
00652 y= s->mb_y*16;
00653
00654 offset= x + y*s->linesize;
00655 p_pic= s->new_picture.data[0] + offset;
00656
00657 s->mb_skipped=1;
00658 for(i=0; i<s->max_b_frames; i++){
00659 uint8_t *b_pic;
00660 int diff;
00661 Picture *pic= s->reordered_input_picture[i+1];
00662
00663 if(pic==NULL || pic->pict_type!=AV_PICTURE_TYPE_B) break;
00664
00665 b_pic= pic->data[0] + offset;
00666 if(pic->type != FF_BUFFER_TYPE_SHARED)
00667 b_pic+= INPLACE_OFFSET;
00668
00669 if(x+16 > s->width || y+16 > s->height){
00670 int x1,y1;
00671 int xe= FFMIN(16, s->width - x);
00672 int ye= FFMIN(16, s->height- y);
00673 diff=0;
00674 for(y1=0; y1<ye; y1++){
00675 for(x1=0; x1<xe; x1++){
00676 diff+= FFABS(p_pic[x1+y1*s->linesize] - b_pic[x1+y1*s->linesize]);
00677 }
00678 }
00679 diff= diff*256/(xe*ye);
00680 }else{
00681 diff= s->dsp.sad[0](NULL, p_pic, b_pic, s->linesize, 16);
00682 }
00683 if(diff>s->qscale*70){
00684 s->mb_skipped=0;
00685 break;
00686 }
00687 }
00688 }else
00689 s->mb_skipped=1;
00690
00691 if(s->mb_skipped==1){
00692
00693 put_bits(&s->pb, 1, 1);
00694
00695 if(interleaved_stats){
00696 s->misc_bits++;
00697 s->last_bits++;
00698 }
00699 s->skip_count++;
00700
00701 return;
00702 }
00703 }
00704
00705 put_bits(&s->pb, 1, 0);
00706 cbpc = cbp & 3;
00707 cbpy = cbp >> 2;
00708 cbpy ^= 0xf;
00709 if(s->mv_type==MV_TYPE_16X16){
00710 if(s->dquant) cbpc+= 8;
00711 put_bits(&s->pb,
00712 ff_h263_inter_MCBPC_bits[cbpc],
00713 ff_h263_inter_MCBPC_code[cbpc]);
00714
00715 put_bits(pb2, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]);
00716 if(s->dquant)
00717 put_bits(pb2, 2, dquant_code[s->dquant+2]);
00718
00719 if(!s->progressive_sequence){
00720 if(cbp)
00721 put_bits(pb2, 1, s->interlaced_dct);
00722 put_bits(pb2, 1, 0);
00723 }
00724
00725 if(interleaved_stats){
00726 s->misc_bits+= get_bits_diff(s);
00727 }
00728
00729
00730 h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
00731
00732 ff_h263_encode_motion_vector(s, motion_x - pred_x,
00733 motion_y - pred_y, s->f_code);
00734 }else if(s->mv_type==MV_TYPE_FIELD){
00735 if(s->dquant) cbpc+= 8;
00736 put_bits(&s->pb,
00737 ff_h263_inter_MCBPC_bits[cbpc],
00738 ff_h263_inter_MCBPC_code[cbpc]);
00739
00740 put_bits(pb2, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]);
00741 if(s->dquant)
00742 put_bits(pb2, 2, dquant_code[s->dquant+2]);
00743
00744 assert(!s->progressive_sequence);
00745 if(cbp)
00746 put_bits(pb2, 1, s->interlaced_dct);
00747 put_bits(pb2, 1, 1);
00748
00749 if(interleaved_stats){
00750 s->misc_bits+= get_bits_diff(s);
00751 }
00752
00753
00754 h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
00755 pred_y /=2;
00756
00757 put_bits(&s->pb, 1, s->field_select[0][0]);
00758 put_bits(&s->pb, 1, s->field_select[0][1]);
00759
00760 ff_h263_encode_motion_vector(s, s->mv[0][0][0] - pred_x,
00761 s->mv[0][0][1] - pred_y, s->f_code);
00762 ff_h263_encode_motion_vector(s, s->mv[0][1][0] - pred_x,
00763 s->mv[0][1][1] - pred_y, s->f_code);
00764 }else{
00765 assert(s->mv_type==MV_TYPE_8X8);
00766 put_bits(&s->pb,
00767 ff_h263_inter_MCBPC_bits[cbpc+16],
00768 ff_h263_inter_MCBPC_code[cbpc+16]);
00769 put_bits(pb2, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]);
00770
00771 if(!s->progressive_sequence){
00772 if(cbp)
00773 put_bits(pb2, 1, s->interlaced_dct);
00774 }
00775
00776 if(interleaved_stats){
00777 s->misc_bits+= get_bits_diff(s);
00778 }
00779
00780 for(i=0; i<4; i++){
00781
00782 h263_pred_motion(s, i, 0, &pred_x, &pred_y);
00783
00784 ff_h263_encode_motion_vector(s, s->current_picture.motion_val[0][ s->block_index[i] ][0] - pred_x,
00785 s->current_picture.motion_val[0][ s->block_index[i] ][1] - pred_y, s->f_code);
00786 }
00787 }
00788
00789 if(interleaved_stats){
00790 s->mv_bits+= get_bits_diff(s);
00791 }
00792
00793 mpeg4_encode_blocks(s, block, NULL, NULL, NULL, tex_pb);
00794
00795 if(interleaved_stats){
00796 s->p_tex_bits+= get_bits_diff(s);
00797 }
00798 s->f_count++;
00799 }
00800 } else {
00801 int cbp;
00802 int dc_diff[6];
00803 int dir[6];
00804 int zigzag_last_index[6];
00805 uint8_t *scan_table[6];
00806 int i;
00807
00808 for(i=0; i<6; i++){
00809 dc_diff[i]= ff_mpeg4_pred_dc(s, i, block[i][0], &dir[i], 1);
00810 }
00811
00812 if(s->flags & CODEC_FLAG_AC_PRED){
00813 s->ac_pred= decide_ac_pred(s, block, dir, scan_table, zigzag_last_index);
00814 }else{
00815 for(i=0; i<6; i++)
00816 scan_table[i]= s->intra_scantable.permutated;
00817 }
00818
00819
00820 cbp = 0;
00821 for (i = 0; i < 6; i++) {
00822 if (s->block_last_index[i] >= 1)
00823 cbp |= 1 << (5 - i);
00824 }
00825
00826 cbpc = cbp & 3;
00827 if (s->pict_type == AV_PICTURE_TYPE_I) {
00828 if(s->dquant) cbpc+=4;
00829 put_bits(&s->pb,
00830 ff_h263_intra_MCBPC_bits[cbpc],
00831 ff_h263_intra_MCBPC_code[cbpc]);
00832 } else {
00833 if(s->dquant) cbpc+=8;
00834 put_bits(&s->pb, 1, 0);
00835 put_bits(&s->pb,
00836 ff_h263_inter_MCBPC_bits[cbpc + 4],
00837 ff_h263_inter_MCBPC_code[cbpc + 4]);
00838 }
00839 put_bits(pb2, 1, s->ac_pred);
00840 cbpy = cbp >> 2;
00841 put_bits(pb2, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]);
00842 if(s->dquant)
00843 put_bits(dc_pb, 2, dquant_code[s->dquant+2]);
00844
00845 if(!s->progressive_sequence){
00846 put_bits(dc_pb, 1, s->interlaced_dct);
00847 }
00848
00849 if(interleaved_stats){
00850 s->misc_bits+= get_bits_diff(s);
00851 }
00852
00853 mpeg4_encode_blocks(s, block, dc_diff, scan_table, dc_pb, tex_pb);
00854
00855 if(interleaved_stats){
00856 s->i_tex_bits+= get_bits_diff(s);
00857 }
00858 s->i_count++;
00859
00860
00861 if(s->ac_pred)
00862 restore_ac_coeffs(s, block, dir, scan_table, zigzag_last_index);
00863 }
00864 }
00865
00869 void ff_mpeg4_stuffing(PutBitContext * pbc)
00870 {
00871 int length;
00872 put_bits(pbc, 1, 0);
00873 length= (-put_bits_count(pbc))&7;
00874 if(length) put_bits(pbc, length, (1<<length)-1);
00875 }
00876
00877
00878 void ff_set_mpeg4_time(MpegEncContext * s){
00879 if(s->pict_type==AV_PICTURE_TYPE_B){
00880 ff_mpeg4_init_direct_mv(s);
00881 }else{
00882 s->last_time_base= s->time_base;
00883 s->time_base= FFUDIV(s->time, s->avctx->time_base.den);
00884 }
00885 }
00886
00887 static void mpeg4_encode_gop_header(MpegEncContext * s){
00888 int hours, minutes, seconds;
00889 int64_t time;
00890
00891 put_bits(&s->pb, 16, 0);
00892 put_bits(&s->pb, 16, GOP_STARTCODE);
00893
00894 time= s->current_picture_ptr->pts;
00895 if(s->reordered_input_picture[1])
00896 time= FFMIN(time, s->reordered_input_picture[1]->pts);
00897 time= time*s->avctx->time_base.num;
00898 s->last_time_base= FFUDIV(time, s->avctx->time_base.den);
00899
00900 seconds= FFUDIV(time, s->avctx->time_base.den);
00901 minutes= FFUDIV(seconds, 60); seconds = FFUMOD(seconds, 60);
00902 hours = FFUDIV(minutes, 60); minutes = FFUMOD(minutes, 60);
00903 hours = FFUMOD(hours , 24);
00904
00905 put_bits(&s->pb, 5, hours);
00906 put_bits(&s->pb, 6, minutes);
00907 put_bits(&s->pb, 1, 1);
00908 put_bits(&s->pb, 6, seconds);
00909
00910 put_bits(&s->pb, 1, !!(s->flags&CODEC_FLAG_CLOSED_GOP));
00911 put_bits(&s->pb, 1, 0);
00912
00913 ff_mpeg4_stuffing(&s->pb);
00914 }
00915
00916 static void mpeg4_encode_visual_object_header(MpegEncContext * s){
00917 int profile_and_level_indication;
00918 int vo_ver_id;
00919
00920 if(s->avctx->profile != FF_PROFILE_UNKNOWN){
00921 profile_and_level_indication = s->avctx->profile << 4;
00922 }else if(s->max_b_frames || s->quarter_sample){
00923 profile_and_level_indication= 0xF0;
00924 }else{
00925 profile_and_level_indication= 0x00;
00926 }
00927
00928 if(s->avctx->level != FF_LEVEL_UNKNOWN){
00929 profile_and_level_indication |= s->avctx->level;
00930 }else{
00931 profile_and_level_indication |= 1;
00932 }
00933
00934 if(profile_and_level_indication>>4 == 0xF){
00935 vo_ver_id= 5;
00936 }else{
00937 vo_ver_id= 1;
00938 }
00939
00940
00941
00942 put_bits(&s->pb, 16, 0);
00943 put_bits(&s->pb, 16, VOS_STARTCODE);
00944
00945 put_bits(&s->pb, 8, profile_and_level_indication);
00946
00947 put_bits(&s->pb, 16, 0);
00948 put_bits(&s->pb, 16, VISUAL_OBJ_STARTCODE);
00949
00950 put_bits(&s->pb, 1, 1);
00951 put_bits(&s->pb, 4, vo_ver_id);
00952 put_bits(&s->pb, 3, 1);
00953
00954 put_bits(&s->pb, 4, 1);
00955
00956 put_bits(&s->pb, 1, 0);
00957
00958 ff_mpeg4_stuffing(&s->pb);
00959 }
00960
00961 static void mpeg4_encode_vol_header(MpegEncContext * s, int vo_number, int vol_number)
00962 {
00963 int vo_ver_id;
00964
00965 if (!CONFIG_MPEG4_ENCODER) return;
00966
00967 if(s->max_b_frames || s->quarter_sample){
00968 vo_ver_id= 5;
00969 s->vo_type= ADV_SIMPLE_VO_TYPE;
00970 }else{
00971 vo_ver_id= 1;
00972 s->vo_type= SIMPLE_VO_TYPE;
00973 }
00974
00975 put_bits(&s->pb, 16, 0);
00976 put_bits(&s->pb, 16, 0x100 + vo_number);
00977 put_bits(&s->pb, 16, 0);
00978 put_bits(&s->pb, 16, 0x120 + vol_number);
00979
00980 put_bits(&s->pb, 1, 0);
00981 put_bits(&s->pb, 8, s->vo_type);
00982 if(s->workaround_bugs & FF_BUG_MS) {
00983 put_bits(&s->pb, 1, 0);
00984 } else {
00985 put_bits(&s->pb, 1, 1);
00986 put_bits(&s->pb, 4, vo_ver_id);
00987 put_bits(&s->pb, 3, 1);
00988 }
00989
00990 s->aspect_ratio_info= ff_h263_aspect_to_info(s->avctx->sample_aspect_ratio);
00991
00992 put_bits(&s->pb, 4, s->aspect_ratio_info);
00993 if (s->aspect_ratio_info == FF_ASPECT_EXTENDED){
00994 put_bits(&s->pb, 8, s->avctx->sample_aspect_ratio.num);
00995 put_bits(&s->pb, 8, s->avctx->sample_aspect_ratio.den);
00996 }
00997
00998 if(s->workaround_bugs & FF_BUG_MS) {
00999 put_bits(&s->pb, 1, 0);
01000 } else {
01001 put_bits(&s->pb, 1, 1);
01002 put_bits(&s->pb, 2, 1);
01003 put_bits(&s->pb, 1, s->low_delay);
01004 put_bits(&s->pb, 1, 0);
01005 }
01006
01007 put_bits(&s->pb, 2, RECT_SHAPE);
01008 put_bits(&s->pb, 1, 1);
01009
01010 put_bits(&s->pb, 16, s->avctx->time_base.den);
01011 if (s->time_increment_bits < 1)
01012 s->time_increment_bits = 1;
01013 put_bits(&s->pb, 1, 1);
01014 put_bits(&s->pb, 1, 0);
01015 put_bits(&s->pb, 1, 1);
01016 put_bits(&s->pb, 13, s->width);
01017 put_bits(&s->pb, 1, 1);
01018 put_bits(&s->pb, 13, s->height);
01019 put_bits(&s->pb, 1, 1);
01020 put_bits(&s->pb, 1, s->progressive_sequence ? 0 : 1);
01021 put_bits(&s->pb, 1, 1);
01022 if (vo_ver_id == 1) {
01023 put_bits(&s->pb, 1, s->vol_sprite_usage);
01024 }else{
01025 put_bits(&s->pb, 2, s->vol_sprite_usage);
01026 }
01027
01028 put_bits(&s->pb, 1, 0);
01029 put_bits(&s->pb, 1, s->mpeg_quant);
01030
01031 if(s->mpeg_quant){
01032 ff_write_quant_matrix(&s->pb, s->avctx->intra_matrix);
01033 ff_write_quant_matrix(&s->pb, s->avctx->inter_matrix);
01034 }
01035
01036 if (vo_ver_id != 1)
01037 put_bits(&s->pb, 1, s->quarter_sample);
01038 put_bits(&s->pb, 1, 1);
01039 s->resync_marker= s->rtp_mode;
01040 put_bits(&s->pb, 1, s->resync_marker ? 0 : 1);
01041 put_bits(&s->pb, 1, s->data_partitioning ? 1 : 0);
01042 if(s->data_partitioning){
01043 put_bits(&s->pb, 1, 0);
01044 }
01045
01046 if (vo_ver_id != 1){
01047 put_bits(&s->pb, 1, 0);
01048 put_bits(&s->pb, 1, 0);
01049 }
01050 put_bits(&s->pb, 1, 0);
01051
01052 ff_mpeg4_stuffing(&s->pb);
01053
01054
01055 if(!(s->flags & CODEC_FLAG_BITEXACT)){
01056 put_bits(&s->pb, 16, 0);
01057 put_bits(&s->pb, 16, 0x1B2);
01058 ff_put_string(&s->pb, LIBAVCODEC_IDENT, 0);
01059 }
01060 }
01061
01062
01063 void mpeg4_encode_picture_header(MpegEncContext * s, int picture_number)
01064 {
01065 int time_incr;
01066 int time_div, time_mod;
01067
01068 if(s->pict_type==AV_PICTURE_TYPE_I){
01069 if(!(s->flags&CODEC_FLAG_GLOBAL_HEADER)){
01070 if(s->strict_std_compliance < FF_COMPLIANCE_VERY_STRICT)
01071 mpeg4_encode_visual_object_header(s);
01072 if(s->strict_std_compliance < FF_COMPLIANCE_VERY_STRICT || picture_number==0)
01073 mpeg4_encode_vol_header(s, 0, 0);
01074 }
01075 if(!(s->workaround_bugs & FF_BUG_MS))
01076 mpeg4_encode_gop_header(s);
01077 }
01078
01079 s->partitioned_frame= s->data_partitioning && s->pict_type!=AV_PICTURE_TYPE_B;
01080
01081 put_bits(&s->pb, 16, 0);
01082 put_bits(&s->pb, 16, VOP_STARTCODE);
01083 put_bits(&s->pb, 2, s->pict_type - 1);
01084
01085 time_div= FFUDIV(s->time, s->avctx->time_base.den);
01086 time_mod= FFUMOD(s->time, s->avctx->time_base.den);
01087 time_incr= time_div - s->last_time_base;
01088 assert(time_incr >= 0);
01089 while(time_incr--)
01090 put_bits(&s->pb, 1, 1);
01091
01092 put_bits(&s->pb, 1, 0);
01093
01094 put_bits(&s->pb, 1, 1);
01095 put_bits(&s->pb, s->time_increment_bits, time_mod);
01096 put_bits(&s->pb, 1, 1);
01097 put_bits(&s->pb, 1, 1);
01098 if ( s->pict_type == AV_PICTURE_TYPE_P
01099 || (s->pict_type == AV_PICTURE_TYPE_S && s->vol_sprite_usage==GMC_SPRITE)) {
01100 put_bits(&s->pb, 1, s->no_rounding);
01101 }
01102 put_bits(&s->pb, 3, 0);
01103 if(!s->progressive_sequence){
01104 put_bits(&s->pb, 1, s->current_picture_ptr->top_field_first);
01105 put_bits(&s->pb, 1, s->alternate_scan);
01106 }
01107
01108
01109 put_bits(&s->pb, 5, s->qscale);
01110
01111 if (s->pict_type != AV_PICTURE_TYPE_I)
01112 put_bits(&s->pb, 3, s->f_code);
01113 if (s->pict_type == AV_PICTURE_TYPE_B)
01114 put_bits(&s->pb, 3, s->b_code);
01115 }
01116
01117
01118 static void init_uni_dc_tab(void)
01119 {
01120 int level, uni_code, uni_len;
01121
01122 for(level=-256; level<256; level++){
01123 int size, v, l;
01124
01125 size = 0;
01126 v = abs(level);
01127 while (v) {
01128 v >>= 1;
01129 size++;
01130 }
01131
01132 if (level < 0)
01133 l= (-level) ^ ((1 << size) - 1);
01134 else
01135 l= level;
01136
01137
01138 uni_code= ff_mpeg4_DCtab_lum[size][0];
01139 uni_len = ff_mpeg4_DCtab_lum[size][1];
01140
01141 if (size > 0) {
01142 uni_code<<=size; uni_code|=l;
01143 uni_len+=size;
01144 if (size > 8){
01145 uni_code<<=1; uni_code|=1;
01146 uni_len++;
01147 }
01148 }
01149 uni_DCtab_lum_bits[level+256]= uni_code;
01150 uni_DCtab_lum_len [level+256]= uni_len;
01151
01152
01153 uni_code= ff_mpeg4_DCtab_chrom[size][0];
01154 uni_len = ff_mpeg4_DCtab_chrom[size][1];
01155
01156 if (size > 0) {
01157 uni_code<<=size; uni_code|=l;
01158 uni_len+=size;
01159 if (size > 8){
01160 uni_code<<=1; uni_code|=1;
01161 uni_len++;
01162 }
01163 }
01164 uni_DCtab_chrom_bits[level+256]= uni_code;
01165 uni_DCtab_chrom_len [level+256]= uni_len;
01166
01167 }
01168 }
01169
01170 static void init_uni_mpeg4_rl_tab(RLTable *rl, uint32_t *bits_tab, uint8_t *len_tab){
01171 int slevel, run, last;
01172
01173 assert(MAX_LEVEL >= 64);
01174 assert(MAX_RUN >= 63);
01175
01176 for(slevel=-64; slevel<64; slevel++){
01177 if(slevel==0) continue;
01178 for(run=0; run<64; run++){
01179 for(last=0; last<=1; last++){
01180 const int index= UNI_MPEG4_ENC_INDEX(last, run, slevel+64);
01181 int level= slevel < 0 ? -slevel : slevel;
01182 int sign= slevel < 0 ? 1 : 0;
01183 int bits, len, code;
01184 int level1, run1;
01185
01186 len_tab[index]= 100;
01187
01188
01189 code= get_rl_index(rl, last, run, level);
01190 bits= rl->table_vlc[code][0];
01191 len= rl->table_vlc[code][1];
01192 bits=bits*2+sign; len++;
01193
01194 if(code!=rl->n && len < len_tab[index]){
01195 bits_tab[index]= bits;
01196 len_tab [index]= len;
01197 }
01198
01199 bits= rl->table_vlc[rl->n][0];
01200 len= rl->table_vlc[rl->n][1];
01201 bits=bits*2; len++;
01202 level1= level - rl->max_level[last][run];
01203 if(level1>0){
01204 code= get_rl_index(rl, last, run, level1);
01205 bits<<= rl->table_vlc[code][1];
01206 len += rl->table_vlc[code][1];
01207 bits += rl->table_vlc[code][0];
01208 bits=bits*2+sign; len++;
01209
01210 if(code!=rl->n && len < len_tab[index]){
01211 bits_tab[index]= bits;
01212 len_tab [index]= len;
01213 }
01214 }
01215
01216 bits= rl->table_vlc[rl->n][0];
01217 len= rl->table_vlc[rl->n][1];
01218 bits=bits*4+2; len+=2;
01219 run1 = run - rl->max_run[last][level] - 1;
01220 if(run1>=0){
01221 code= get_rl_index(rl, last, run1, level);
01222 bits<<= rl->table_vlc[code][1];
01223 len += rl->table_vlc[code][1];
01224 bits += rl->table_vlc[code][0];
01225 bits=bits*2+sign; len++;
01226
01227 if(code!=rl->n && len < len_tab[index]){
01228 bits_tab[index]= bits;
01229 len_tab [index]= len;
01230 }
01231 }
01232
01233 bits= rl->table_vlc[rl->n][0];
01234 len = rl->table_vlc[rl->n][1];
01235 bits=bits*4+3; len+=2;
01236 bits=bits*2+last; len++;
01237 bits=bits*64+run; len+=6;
01238 bits=bits*2+1; len++;
01239 bits=bits*4096+(slevel&0xfff); len+=12;
01240 bits=bits*2+1; len++;
01241
01242 if(len < len_tab[index]){
01243 bits_tab[index]= bits;
01244 len_tab [index]= len;
01245 }
01246 }
01247 }
01248 }
01249 }
01250
01251 static av_cold int encode_init(AVCodecContext *avctx)
01252 {
01253 MpegEncContext *s = avctx->priv_data;
01254 int ret;
01255 static int done = 0;
01256
01257 if((ret=MPV_encode_init(avctx)) < 0)
01258 return ret;
01259
01260 if (!done) {
01261 done = 1;
01262
01263 init_uni_dc_tab();
01264
01265 init_rl(&ff_mpeg4_rl_intra, ff_mpeg4_static_rl_table_store[0]);
01266
01267 init_uni_mpeg4_rl_tab(&ff_mpeg4_rl_intra, uni_mpeg4_intra_rl_bits, uni_mpeg4_intra_rl_len);
01268 init_uni_mpeg4_rl_tab(&ff_h263_rl_inter, uni_mpeg4_inter_rl_bits, uni_mpeg4_inter_rl_len);
01269 }
01270
01271 s->min_qcoeff= -2048;
01272 s->max_qcoeff= 2047;
01273 s->intra_ac_vlc_length = uni_mpeg4_intra_rl_len;
01274 s->intra_ac_vlc_last_length= uni_mpeg4_intra_rl_len + 128*64;
01275 s->inter_ac_vlc_length = uni_mpeg4_inter_rl_len;
01276 s->inter_ac_vlc_last_length= uni_mpeg4_inter_rl_len + 128*64;
01277 s->luma_dc_vlc_length= uni_DCtab_lum_len;
01278 s->chroma_dc_vlc_length= uni_DCtab_chrom_len;
01279 s->ac_esc_length= 7+2+1+6+1+12+1;
01280 s->y_dc_scale_table= ff_mpeg4_y_dc_scale_table;
01281 s->c_dc_scale_table= ff_mpeg4_c_dc_scale_table;
01282
01283 if(s->flags & CODEC_FLAG_GLOBAL_HEADER){
01284
01285 s->avctx->extradata= av_malloc(1024);
01286 init_put_bits(&s->pb, s->avctx->extradata, 1024);
01287
01288 if(!(s->workaround_bugs & FF_BUG_MS))
01289 mpeg4_encode_visual_object_header(s);
01290 mpeg4_encode_vol_header(s, 0, 0);
01291
01292
01293 flush_put_bits(&s->pb);
01294 s->avctx->extradata_size= (put_bits_count(&s->pb)+7)>>3;
01295 }
01296 return 0;
01297 }
01298
01299 void ff_mpeg4_init_partitions(MpegEncContext *s)
01300 {
01301 uint8_t *start= put_bits_ptr(&s->pb);
01302 uint8_t *end= s->pb.buf_end;
01303 int size= end - start;
01304 int pb_size = (((intptr_t)start + size/3)&(~3)) - (intptr_t)start;
01305 int tex_size= (size - 2*pb_size)&(~3);
01306
01307 set_put_bits_buffer_size(&s->pb, pb_size);
01308 init_put_bits(&s->tex_pb, start + pb_size , tex_size);
01309 init_put_bits(&s->pb2 , start + pb_size + tex_size, pb_size);
01310 }
01311
01312 void ff_mpeg4_merge_partitions(MpegEncContext *s)
01313 {
01314 const int pb2_len = put_bits_count(&s->pb2 );
01315 const int tex_pb_len= put_bits_count(&s->tex_pb);
01316 const int bits= put_bits_count(&s->pb);
01317
01318 if(s->pict_type==AV_PICTURE_TYPE_I){
01319 put_bits(&s->pb, 19, DC_MARKER);
01320 s->misc_bits+=19 + pb2_len + bits - s->last_bits;
01321 s->i_tex_bits+= tex_pb_len;
01322 }else{
01323 put_bits(&s->pb, 17, MOTION_MARKER);
01324 s->misc_bits+=17 + pb2_len;
01325 s->mv_bits+= bits - s->last_bits;
01326 s->p_tex_bits+= tex_pb_len;
01327 }
01328
01329 flush_put_bits(&s->pb2);
01330 flush_put_bits(&s->tex_pb);
01331
01332 set_put_bits_buffer_size(&s->pb, s->pb2.buf_end - s->pb.buf);
01333 ff_copy_bits(&s->pb, s->pb2.buf , pb2_len);
01334 ff_copy_bits(&s->pb, s->tex_pb.buf, tex_pb_len);
01335 s->last_bits= put_bits_count(&s->pb);
01336 }
01337
01338
01339 void ff_mpeg4_encode_video_packet_header(MpegEncContext *s)
01340 {
01341 int mb_num_bits= av_log2(s->mb_num - 1) + 1;
01342
01343 put_bits(&s->pb, ff_mpeg4_get_video_packet_prefix_length(s), 0);
01344 put_bits(&s->pb, 1, 1);
01345
01346 put_bits(&s->pb, mb_num_bits, s->mb_x + s->mb_y*s->mb_width);
01347 put_bits(&s->pb, s->quant_precision, s->qscale);
01348 put_bits(&s->pb, 1, 0);
01349 }
01350
01351 AVCodec ff_mpeg4_encoder = {
01352 "mpeg4",
01353 AVMEDIA_TYPE_VIDEO,
01354 CODEC_ID_MPEG4,
01355 sizeof(MpegEncContext),
01356 encode_init,
01357 MPV_encode_picture,
01358 MPV_encode_end,
01359 .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
01360 .capabilities= CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS,
01361 .long_name= NULL_IF_CONFIG_SMALL("MPEG-4 part 2"),
01362 };