00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00028 #include "dsputil.h"
00029 #include "avcodec.h"
00030 #include "mpegvideo.h"
00031 #include "h263.h"
00032 #include "h261.h"
00033 #include "h261data.h"
00034
00035 extern uint8_t ff_h261_rl_table_store[2][2*MAX_RUN + MAX_LEVEL + 3];
00036
00037 static void h261_encode_block(H261Context * h, DCTELEM * block,
00038 int n);
00039
00040 int ff_h261_get_picture_format(int width, int height){
00041
00042 if (width == 176 && height == 144)
00043 return 0;
00044
00045 else if (width == 352 && height == 288)
00046 return 1;
00047
00048 else
00049 return -1;
00050 }
00051
00052 void ff_h261_encode_picture_header(MpegEncContext * s, int picture_number){
00053 H261Context * h = (H261Context *) s;
00054 int format, temp_ref;
00055
00056 avpriv_align_put_bits(&s->pb);
00057
00058
00059 s->ptr_lastgob = put_bits_ptr(&s->pb);
00060
00061 put_bits(&s->pb, 20, 0x10);
00062
00063 temp_ref= s->picture_number * (int64_t)30000 * s->avctx->time_base.num /
00064 (1001 * (int64_t)s->avctx->time_base.den);
00065 put_sbits(&s->pb, 5, temp_ref);
00066
00067 put_bits(&s->pb, 1, 0);
00068 put_bits(&s->pb, 1, 0);
00069 put_bits(&s->pb, 1, 0);
00070
00071 format = ff_h261_get_picture_format(s->width, s->height);
00072
00073 put_bits(&s->pb, 1, format);
00074
00075 put_bits(&s->pb, 1, 0);
00076 put_bits(&s->pb, 1, 0);
00077
00078 put_bits(&s->pb, 1, 0);
00079 if(format == 0)
00080 h->gob_number = -1;
00081 else
00082 h->gob_number = 0;
00083 h->current_mba = 0;
00084 }
00085
00089 static void h261_encode_gob_header(MpegEncContext * s, int mb_line){
00090 H261Context * h = (H261Context *)s;
00091 if(ff_h261_get_picture_format(s->width, s->height) == 0){
00092 h->gob_number+=2;
00093 }
00094 else{
00095 h->gob_number++;
00096 }
00097 put_bits(&s->pb, 16, 1);
00098 put_bits(&s->pb, 4, h->gob_number);
00099 put_bits(&s->pb, 5, s->qscale);
00100 put_bits(&s->pb, 1, 0);
00101 h->current_mba = 0;
00102 h->previous_mba = 0;
00103 h->current_mv_x=0;
00104 h->current_mv_y=0;
00105 }
00106
00107 void ff_h261_reorder_mb_index(MpegEncContext* s){
00108 int index= s->mb_x + s->mb_y*s->mb_width;
00109
00110 if(index % 33 == 0)
00111 h261_encode_gob_header(s,0);
00112
00113
00114
00115 if(ff_h261_get_picture_format(s->width,s->height) == 1){
00116 s->mb_x = index % 11 ; index /= 11;
00117 s->mb_y = index % 3 ; index /= 3;
00118 s->mb_x+= 11*(index % 2); index /= 2;
00119 s->mb_y+= 3*index;
00120
00121 ff_init_block_index(s);
00122 ff_update_block_index(s);
00123 }
00124 }
00125
00126 static void h261_encode_motion(H261Context * h, int val){
00127 MpegEncContext * const s = &h->s;
00128 int sign, code;
00129 if(val==0){
00130 code = 0;
00131 put_bits(&s->pb,h261_mv_tab[code][1],h261_mv_tab[code][0]);
00132 }
00133 else{
00134 if(val > 15)
00135 val -=32;
00136 if(val < -16)
00137 val+=32;
00138 sign = val < 0;
00139 code = sign ? -val : val;
00140 put_bits(&s->pb,h261_mv_tab[code][1],h261_mv_tab[code][0]);
00141 put_bits(&s->pb,1,sign);
00142 }
00143 }
00144
00145 static inline int get_cbp(MpegEncContext * s,
00146 DCTELEM block[6][64])
00147 {
00148 int i, cbp;
00149 cbp= 0;
00150 for (i = 0; i < 6; i++) {
00151 if (s->block_last_index[i] >= 0)
00152 cbp |= 1 << (5 - i);
00153 }
00154 return cbp;
00155 }
00156 void ff_h261_encode_mb(MpegEncContext * s,
00157 DCTELEM block[6][64],
00158 int motion_x, int motion_y)
00159 {
00160 H261Context * h = (H261Context *)s;
00161 int mvd, mv_diff_x, mv_diff_y, i, cbp;
00162 cbp = 63;
00163 mvd = 0;
00164
00165 h->current_mba++;
00166 h->mtype = 0;
00167
00168 if (!s->mb_intra){
00169
00170 cbp= get_cbp(s, block);
00171
00172
00173 mvd = motion_x | motion_y;
00174
00175 if((cbp | mvd | s->dquant ) == 0) {
00176
00177 s->skip_count++;
00178 h->current_mv_x=0;
00179 h->current_mv_y=0;
00180 return;
00181 }
00182 }
00183
00184
00185 put_bits(&s->pb, h261_mba_bits[(h->current_mba-h->previous_mba)-1], h261_mba_code[(h->current_mba-h->previous_mba)-1]);
00186
00187
00188 if(!s->mb_intra){
00189 h->mtype++;
00190
00191 if(mvd || s->loop_filter)
00192 h->mtype+=3;
00193 if(s->loop_filter)
00194 h->mtype+=3;
00195 if(cbp || s->dquant)
00196 h->mtype++;
00197 assert(h->mtype > 1);
00198 }
00199
00200 if(s->dquant)
00201 h->mtype++;
00202
00203 put_bits(&s->pb, h261_mtype_bits[h->mtype], h261_mtype_code[h->mtype]);
00204
00205 h->mtype = h261_mtype_map[h->mtype];
00206
00207 if(IS_QUANT(h->mtype)){
00208 ff_set_qscale(s,s->qscale+s->dquant);
00209 put_bits(&s->pb, 5, s->qscale);
00210 }
00211
00212 if(IS_16X16(h->mtype)){
00213 mv_diff_x = (motion_x >> 1) - h->current_mv_x;
00214 mv_diff_y = (motion_y >> 1) - h->current_mv_y;
00215 h->current_mv_x = (motion_x >> 1);
00216 h->current_mv_y = (motion_y >> 1);
00217 h261_encode_motion(h,mv_diff_x);
00218 h261_encode_motion(h,mv_diff_y);
00219 }
00220
00221 h->previous_mba = h->current_mba;
00222
00223 if(HAS_CBP(h->mtype)){
00224 assert(cbp>0);
00225 put_bits(&s->pb,h261_cbp_tab[cbp-1][1],h261_cbp_tab[cbp-1][0]);
00226 }
00227 for(i=0; i<6; i++) {
00228
00229 h261_encode_block(h, block[i], i);
00230 }
00231
00232 if ( ( h->current_mba == 11 ) || ( h->current_mba == 22 ) || ( h->current_mba == 33 ) || ( !IS_16X16 ( h->mtype ) )){
00233 h->current_mv_x=0;
00234 h->current_mv_y=0;
00235 }
00236 }
00237
00238 void ff_h261_encode_init(MpegEncContext *s){
00239 static int done = 0;
00240
00241 if (!done) {
00242 done = 1;
00243 ff_init_rl(&h261_rl_tcoeff, ff_h261_rl_table_store);
00244 }
00245
00246 s->min_qcoeff= -127;
00247 s->max_qcoeff= 127;
00248 s->y_dc_scale_table=
00249 s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
00250 }
00251
00252
00258 static void h261_encode_block(H261Context * h, DCTELEM * block, int n){
00259 MpegEncContext * const s = &h->s;
00260 int level, run, i, j, last_index, last_non_zero, sign, slevel, code;
00261 RLTable *rl;
00262
00263 rl = &h261_rl_tcoeff;
00264 if (s->mb_intra) {
00265
00266 level = block[0];
00267
00268 if (level > 254) {
00269 level = 254;
00270 block[0] = 254;
00271 }
00272
00273 else if (level < 1) {
00274 level = 1;
00275 block[0] = 1;
00276 }
00277 if (level == 128)
00278 put_bits(&s->pb, 8, 0xff);
00279 else
00280 put_bits(&s->pb, 8, level);
00281 i = 1;
00282 } else if((block[0]==1 || block[0] == -1) && (s->block_last_index[n] > -1)){
00283
00284 put_bits(&s->pb,2,block[0]>0 ? 2 : 3 );
00285 i = 1;
00286 } else {
00287 i = 0;
00288 }
00289
00290
00291 last_index = s->block_last_index[n];
00292 last_non_zero = i - 1;
00293 for (; i <= last_index; i++) {
00294 j = s->intra_scantable.permutated[i];
00295 level = block[j];
00296 if (level) {
00297 run = i - last_non_zero - 1;
00298 sign = 0;
00299 slevel = level;
00300 if (level < 0) {
00301 sign = 1;
00302 level = -level;
00303 }
00304 code = get_rl_index(rl, 0 , run, level);
00305 if(run==0 && level < 16)
00306 code+=1;
00307 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
00308 if (code == rl->n) {
00309 put_bits(&s->pb, 6, run);
00310 assert(slevel != 0);
00311 assert(level <= 127);
00312 put_sbits(&s->pb, 8, slevel);
00313 } else {
00314 put_bits(&s->pb, 1, sign);
00315 }
00316 last_non_zero = i;
00317 }
00318 }
00319 if(last_index > -1){
00320 put_bits(&s->pb, rl->table_vlc[0][1], rl->table_vlc[0][0]);
00321 }
00322 }
00323
00324 AVCodec ff_h261_encoder = {
00325 .name = "h261",
00326 .type = AVMEDIA_TYPE_VIDEO,
00327 .id = CODEC_ID_H261,
00328 .priv_data_size = sizeof(H261Context),
00329 .init = MPV_encode_init,
00330 .encode = MPV_encode_picture,
00331 .close = MPV_encode_end,
00332 .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
00333 .long_name= NULL_IF_CONFIG_SMALL("H.261"),
00334 };
00335