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