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