Libav 0.7.1
|
00001 /* 00002 * H261 encoder 00003 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at> 00004 * Copyright (c) 2004 Maarten Daniels 00005 * 00006 * This file is part of Libav. 00007 * 00008 * Libav is free software; you can redistribute it and/or 00009 * modify it under the terms of the GNU Lesser General Public 00010 * License as published by the Free Software Foundation; either 00011 * version 2.1 of the License, or (at your option) any later version. 00012 * 00013 * Libav is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 * Lesser General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU Lesser General Public 00019 * License along with Libav; if not, write to the Free Software 00020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 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 // QCIF 00042 if (width == 176 && height == 144) 00043 return 0; 00044 // CIF 00045 else if (width == 352 && height == 288) 00046 return 1; 00047 // ERROR 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 align_put_bits(&s->pb); 00057 00058 /* Update the pointer to last GOB */ 00059 s->ptr_lastgob = put_bits_ptr(&s->pb); 00060 00061 put_bits(&s->pb, 20, 0x10); /* PSC */ 00062 00063 temp_ref= s->picture_number * (int64_t)30000 * s->avctx->time_base.num / 00064 (1001 * (int64_t)s->avctx->time_base.den); //FIXME maybe this should use a timestamp 00065 put_sbits(&s->pb, 5, temp_ref); /* TemporalReference */ 00066 00067 put_bits(&s->pb, 1, 0); /* split screen off */ 00068 put_bits(&s->pb, 1, 0); /* camera off */ 00069 put_bits(&s->pb, 1, 0); /* freeze picture release off */ 00070 00071 format = ff_h261_get_picture_format(s->width, s->height); 00072 00073 put_bits(&s->pb, 1, format); /* 0 == QCIF, 1 == CIF */ 00074 00075 put_bits(&s->pb, 1, 0); /* still image mode */ 00076 put_bits(&s->pb, 1, 0); /* reserved */ 00077 00078 put_bits(&s->pb, 1, 0); /* no PEI */ 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; // QCIF 00093 } 00094 else{ 00095 h->gob_number++; // CIF 00096 } 00097 put_bits(&s->pb, 16, 1); /* GBSC */ 00098 put_bits(&s->pb, 4, h->gob_number); /* GN */ 00099 put_bits(&s->pb, 5, s->qscale); /* GQUANT */ 00100 put_bits(&s->pb, 1, 0); /* no GEI */ 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 /* for CIF the GOB's are fragmented in the middle of a scanline 00114 that's why we need to adjust the x and y index of the macroblocks */ 00115 if(ff_h261_get_picture_format(s->width,s->height) == 1){ // CIF 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; // avoid warning 00163 mvd = 0; 00164 00165 h->current_mba++; 00166 h->mtype = 0; 00167 00168 if (!s->mb_intra){ 00169 /* compute cbp */ 00170 cbp= get_cbp(s, block); 00171 00172 /* mvd indicates if this block is motion compensated */ 00173 mvd = motion_x | motion_y; 00174 00175 if((cbp | mvd | s->dquant ) == 0) { 00176 /* skip macroblock */ 00177 s->skip_count++; 00178 h->current_mv_x=0; 00179 h->current_mv_y=0; 00180 return; 00181 } 00182 } 00183 00184 /* MB is not skipped, encode MBA */ 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 /* calculate MTYPE */ 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 /* encode each block */ 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 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 /* DC coef */ 00266 level = block[0]; 00267 /* 255 cannot be represented, so we clamp */ 00268 if (level > 254) { 00269 level = 254; 00270 block[0] = 254; 00271 } 00272 /* 0 cannot be represented also */ 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 //special case 00284 put_bits(&s->pb,2,block[0]>0 ? 2 : 3 ); 00285 i = 1; 00286 } else { 00287 i = 0; 00288 } 00289 00290 /* AC coefs */ 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 /*no last in H.261, EOB is used*/, 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]);// END OF BLOCK 00321 } 00322 } 00323 00324 AVCodec ff_h261_encoder = { 00325 "h261", 00326 AVMEDIA_TYPE_VIDEO, 00327 CODEC_ID_H261, 00328 sizeof(H261Context), 00329 MPV_encode_init, 00330 MPV_encode_picture, 00331 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