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