00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00026 #include "common.h"
00027 #include "dsputil.h"
00028 #include "avcodec.h"
00029 #include "mpegvideo.h"
00030 #include "h261data.h"
00031
00032
00033 #define H261_MBA_VLC_BITS 9
00034 #define H261_MTYPE_VLC_BITS 6
00035 #define H261_MV_VLC_BITS 7
00036 #define H261_CBP_VLC_BITS 9
00037 #define TCOEFF_VLC_BITS 9
00038
00039 #define MBA_STUFFING 33
00040 #define MBA_STARTCODE 34
00041 #define IS_FIL(a) ((a)&MB_TYPE_H261_FIL)
00042
00046 typedef struct H261Context{
00047 MpegEncContext s;
00048
00049 int current_mba;
00050 int previous_mba;
00051 int mba_diff;
00052 int mtype;
00053 int current_mv_x;
00054 int current_mv_y;
00055 int gob_number;
00056 int gob_start_code_skipped;
00057 }H261Context;
00058
00059 void ff_h261_loop_filter(MpegEncContext *s){
00060 H261Context * h= (H261Context*)s;
00061 const int linesize = s->linesize;
00062 const int uvlinesize= s->uvlinesize;
00063 uint8_t *dest_y = s->dest[0];
00064 uint8_t *dest_cb= s->dest[1];
00065 uint8_t *dest_cr= s->dest[2];
00066
00067 if(!(IS_FIL (h->mtype)))
00068 return;
00069
00070 s->dsp.h261_loop_filter(dest_y , linesize);
00071 s->dsp.h261_loop_filter(dest_y + 8, linesize);
00072 s->dsp.h261_loop_filter(dest_y + 8 * linesize , linesize);
00073 s->dsp.h261_loop_filter(dest_y + 8 * linesize + 8, linesize);
00074 s->dsp.h261_loop_filter(dest_cb, uvlinesize);
00075 s->dsp.h261_loop_filter(dest_cr, uvlinesize);
00076 }
00077
00078 static int ff_h261_get_picture_format(int width, int height){
00079
00080 if (width == 176 && height == 144)
00081 return 0;
00082
00083 else if (width == 352 && height == 288)
00084 return 1;
00085
00086 else
00087 return -1;
00088 }
00089
00090 static void h261_encode_block(H261Context * h, DCTELEM * block,
00091 int n);
00092 static int h261_decode_block(H261Context *h, DCTELEM *block,
00093 int n, int coded);
00094
00095 void ff_h261_encode_picture_header(MpegEncContext * s, int picture_number){
00096 H261Context * h = (H261Context *) s;
00097 int format, temp_ref;
00098
00099 align_put_bits(&s->pb);
00100
00101
00102 s->ptr_lastgob = pbBufPtr(&s->pb);
00103
00104 put_bits(&s->pb, 20, 0x10);
00105
00106 temp_ref= s->picture_number * (int64_t)30000 * s->avctx->time_base.num /
00107 (1001 * (int64_t)s->avctx->time_base.den);
00108 put_bits(&s->pb, 5, temp_ref & 0x1f);
00109
00110 put_bits(&s->pb, 1, 0);
00111 put_bits(&s->pb, 1, 0);
00112 put_bits(&s->pb, 1, 0);
00113
00114 format = ff_h261_get_picture_format(s->width, s->height);
00115
00116 put_bits(&s->pb, 1, format);
00117
00118 put_bits(&s->pb, 1, 0);
00119 put_bits(&s->pb, 1, 0);
00120
00121 put_bits(&s->pb, 1, 0);
00122 if(format == 0)
00123 h->gob_number = -1;
00124 else
00125 h->gob_number = 0;
00126 h->current_mba = 0;
00127 }
00128
00132 static void h261_encode_gob_header(MpegEncContext * s, int mb_line){
00133 H261Context * h = (H261Context *)s;
00134 if(ff_h261_get_picture_format(s->width, s->height) == 0){
00135 h->gob_number+=2;
00136 }
00137 else{
00138 h->gob_number++;
00139 }
00140 put_bits(&s->pb, 16, 1);
00141 put_bits(&s->pb, 4, h->gob_number);
00142 put_bits(&s->pb, 5, s->qscale);
00143 put_bits(&s->pb, 1, 0);
00144 h->current_mba = 0;
00145 h->previous_mba = 0;
00146 h->current_mv_x=0;
00147 h->current_mv_y=0;
00148 }
00149
00150 void ff_h261_reorder_mb_index(MpegEncContext* s){
00151 int index= s->mb_x + s->mb_y*s->mb_width;
00152
00153 if(index % 33 == 0)
00154 h261_encode_gob_header(s,0);
00155
00156
00157
00158 if(ff_h261_get_picture_format(s->width,s->height) == 1){
00159 s->mb_x = index % 11 ; index /= 11;
00160 s->mb_y = index % 3 ; index /= 3;
00161 s->mb_x+= 11*(index % 2); index /= 2;
00162 s->mb_y+= 3*index;
00163
00164 ff_init_block_index(s);
00165 ff_update_block_index(s);
00166 }
00167 }
00168
00169 static void h261_encode_motion(H261Context * h, int val){
00170 MpegEncContext * const s = &h->s;
00171 int sign, code;
00172 if(val==0){
00173 code = 0;
00174 put_bits(&s->pb,h261_mv_tab[code][1],h261_mv_tab[code][0]);
00175 }
00176 else{
00177 if(val > 15)
00178 val -=32;
00179 if(val < -16)
00180 val+=32;
00181 sign = val < 0;
00182 code = sign ? -val : val;
00183 put_bits(&s->pb,h261_mv_tab[code][1],h261_mv_tab[code][0]);
00184 put_bits(&s->pb,1,sign);
00185 }
00186 }
00187
00188 static inline int get_cbp(MpegEncContext * s,
00189 DCTELEM block[6][64])
00190 {
00191 int i, cbp;
00192 cbp= 0;
00193 for (i = 0; i < 6; i++) {
00194 if (s->block_last_index[i] >= 0)
00195 cbp |= 1 << (5 - i);
00196 }
00197 return cbp;
00198 }
00199 void ff_h261_encode_mb(MpegEncContext * s,
00200 DCTELEM block[6][64],
00201 int motion_x, int motion_y)
00202 {
00203 H261Context * h = (H261Context *)s;
00204 int mvd, mv_diff_x, mv_diff_y, i, cbp;
00205 cbp = 63;
00206 mvd = 0;
00207
00208 h->current_mba++;
00209 h->mtype = 0;
00210
00211 if (!s->mb_intra){
00212
00213 cbp= get_cbp(s, block);
00214
00215
00216 mvd = motion_x | motion_y;
00217
00218 if((cbp | mvd | s->dquant ) == 0) {
00219
00220 s->skip_count++;
00221 h->current_mv_x=0;
00222 h->current_mv_y=0;
00223 return;
00224 }
00225 }
00226
00227
00228 put_bits(&s->pb, h261_mba_bits[(h->current_mba-h->previous_mba)-1], h261_mba_code[(h->current_mba-h->previous_mba)-1]);
00229
00230
00231 if(!s->mb_intra){
00232 h->mtype++;
00233
00234 if(mvd || s->loop_filter)
00235 h->mtype+=3;
00236 if(s->loop_filter)
00237 h->mtype+=3;
00238 if(cbp || s->dquant)
00239 h->mtype++;
00240 assert(h->mtype > 1);
00241 }
00242
00243 if(s->dquant)
00244 h->mtype++;
00245
00246 put_bits(&s->pb, h261_mtype_bits[h->mtype], h261_mtype_code[h->mtype]);
00247
00248 h->mtype = h261_mtype_map[h->mtype];
00249
00250 if(IS_QUANT(h->mtype)){
00251 ff_set_qscale(s,s->qscale+s->dquant);
00252 put_bits(&s->pb, 5, s->qscale);
00253 }
00254
00255 if(IS_16X16(h->mtype)){
00256 mv_diff_x = (motion_x >> 1) - h->current_mv_x;
00257 mv_diff_y = (motion_y >> 1) - h->current_mv_y;
00258 h->current_mv_x = (motion_x >> 1);
00259 h->current_mv_y = (motion_y >> 1);
00260 h261_encode_motion(h,mv_diff_x);
00261 h261_encode_motion(h,mv_diff_y);
00262 }
00263
00264 h->previous_mba = h->current_mba;
00265
00266 if(HAS_CBP(h->mtype)){
00267 put_bits(&s->pb,h261_cbp_tab[cbp-1][1],h261_cbp_tab[cbp-1][0]);
00268 }
00269 for(i=0; i<6; i++) {
00270
00271 h261_encode_block(h, block[i], i);
00272 }
00273
00274 if ( ( h->current_mba == 11 ) || ( h->current_mba == 22 ) || ( h->current_mba == 33 ) || ( !IS_16X16 ( h->mtype ) )){
00275 h->current_mv_x=0;
00276 h->current_mv_y=0;
00277 }
00278 }
00279
00280 void ff_h261_encode_init(MpegEncContext *s){
00281 static int done = 0;
00282
00283 if (!done) {
00284 done = 1;
00285 init_rl(&h261_rl_tcoeff, 1);
00286 }
00287
00288 s->min_qcoeff= -127;
00289 s->max_qcoeff= 127;
00290 s->y_dc_scale_table=
00291 s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
00292 }
00293
00294
00300 static void h261_encode_block(H261Context * h, DCTELEM * block, int n){
00301 MpegEncContext * const s = &h->s;
00302 int level, run, last, i, j, last_index, last_non_zero, sign, slevel, code;
00303 RLTable *rl;
00304
00305 rl = &h261_rl_tcoeff;
00306 if (s->mb_intra) {
00307
00308 level = block[0];
00309
00310 if (level > 254) {
00311 level = 254;
00312 block[0] = 254;
00313 }
00314
00315 else if (level < 1) {
00316 level = 1;
00317 block[0] = 1;
00318 }
00319 if (level == 128)
00320 put_bits(&s->pb, 8, 0xff);
00321 else
00322 put_bits(&s->pb, 8, level);
00323 i = 1;
00324 } else if((block[0]==1 || block[0] == -1) && (s->block_last_index[n] > -1)){
00325
00326 put_bits(&s->pb,2,block[0]>0 ? 2 : 3 );
00327 i = 1;
00328 } else {
00329 i = 0;
00330 }
00331
00332
00333 last_index = s->block_last_index[n];
00334 last_non_zero = i - 1;
00335 for (; i <= last_index; i++) {
00336 j = s->intra_scantable.permutated[i];
00337 level = block[j];
00338 if (level) {
00339 run = i - last_non_zero - 1;
00340 last = (i == last_index);
00341 sign = 0;
00342 slevel = level;
00343 if (level < 0) {
00344 sign = 1;
00345 level = -level;
00346 }
00347 code = get_rl_index(rl, 0 , run, level);
00348 if(run==0 && level < 16)
00349 code+=1;
00350 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
00351 if (code == rl->n) {
00352 put_bits(&s->pb, 6, run);
00353 assert(slevel != 0);
00354 assert(level <= 127);
00355 put_bits(&s->pb, 8, slevel & 0xff);
00356 } else {
00357 put_bits(&s->pb, 1, sign);
00358 }
00359 last_non_zero = i;
00360 }
00361 }
00362 if(last_index > -1){
00363 put_bits(&s->pb, rl->table_vlc[0][1], rl->table_vlc[0][0]);
00364 }
00365 }
00366
00367
00368
00369
00370 static VLC h261_mba_vlc;
00371 static VLC h261_mtype_vlc;
00372 static VLC h261_mv_vlc;
00373 static VLC h261_cbp_vlc;
00374
00375 void init_vlc_rl(RLTable *rl, int use_static);
00376
00377 static void h261_decode_init_vlc(H261Context *h){
00378 static int done = 0;
00379
00380 if(!done){
00381 done = 1;
00382 init_vlc(&h261_mba_vlc, H261_MBA_VLC_BITS, 35,
00383 h261_mba_bits, 1, 1,
00384 h261_mba_code, 1, 1, 1);
00385 init_vlc(&h261_mtype_vlc, H261_MTYPE_VLC_BITS, 10,
00386 h261_mtype_bits, 1, 1,
00387 h261_mtype_code, 1, 1, 1);
00388 init_vlc(&h261_mv_vlc, H261_MV_VLC_BITS, 17,
00389 &h261_mv_tab[0][1], 2, 1,
00390 &h261_mv_tab[0][0], 2, 1, 1);
00391 init_vlc(&h261_cbp_vlc, H261_CBP_VLC_BITS, 63,
00392 &h261_cbp_tab[0][1], 2, 1,
00393 &h261_cbp_tab[0][0], 2, 1, 1);
00394 init_rl(&h261_rl_tcoeff, 1);
00395 init_vlc_rl(&h261_rl_tcoeff, 1);
00396 }
00397 }
00398
00399 static int h261_decode_init(AVCodecContext *avctx){
00400 H261Context *h= avctx->priv_data;
00401 MpegEncContext * const s = &h->s;
00402
00403
00404 MPV_decode_defaults(s);
00405 s->avctx = avctx;
00406
00407 s->width = s->avctx->coded_width;
00408 s->height = s->avctx->coded_height;
00409 s->codec_id = s->avctx->codec->id;
00410
00411 s->out_format = FMT_H261;
00412 s->low_delay= 1;
00413 avctx->pix_fmt= PIX_FMT_YUV420P;
00414
00415 s->codec_id= avctx->codec->id;
00416
00417 h261_decode_init_vlc(h);
00418
00419 h->gob_start_code_skipped = 0;
00420
00421 return 0;
00422 }
00423
00428 static int h261_decode_gob_header(H261Context *h){
00429 unsigned int val;
00430 MpegEncContext * const s = &h->s;
00431
00432 if ( !h->gob_start_code_skipped ){
00433
00434 val = show_bits(&s->gb, 15);
00435 if(val)
00436 return -1;
00437
00438
00439 skip_bits(&s->gb, 16);
00440 }
00441
00442 h->gob_start_code_skipped = 0;
00443
00444 h->gob_number = get_bits(&s->gb, 4);
00445 s->qscale = get_bits(&s->gb, 5);
00446
00447
00448 if (s->mb_height==18){
00449 if ((h->gob_number<=0) || (h->gob_number>12))
00450 return -1;
00451 }
00452 else{
00453 if ((h->gob_number!=1) && (h->gob_number!=3) && (h->gob_number!=5))
00454 return -1;
00455 }
00456
00457
00458 while (get_bits1(&s->gb) != 0) {
00459 skip_bits(&s->gb, 8);
00460 }
00461
00462 if(s->qscale==0)
00463 return -1;
00464
00465
00466
00467
00468 h->current_mba = 0;
00469 h->mba_diff = 0;
00470
00471 return 0;
00472 }
00473
00478 static int ff_h261_resync(H261Context *h){
00479 MpegEncContext * const s = &h->s;
00480 int left, ret;
00481
00482 if ( h->gob_start_code_skipped ){
00483 ret= h261_decode_gob_header(h);
00484 if(ret>=0)
00485 return 0;
00486 }
00487 else{
00488 if(show_bits(&s->gb, 15)==0){
00489 ret= h261_decode_gob_header(h);
00490 if(ret>=0)
00491 return 0;
00492 }
00493
00494 s->gb= s->last_resync_gb;
00495 align_get_bits(&s->gb);
00496 left= s->gb.size_in_bits - get_bits_count(&s->gb);
00497
00498 for(;left>15+1+4+5; left-=8){
00499 if(show_bits(&s->gb, 15)==0){
00500 GetBitContext bak= s->gb;
00501
00502 ret= h261_decode_gob_header(h);
00503 if(ret>=0)
00504 return 0;
00505
00506 s->gb= bak;
00507 }
00508 skip_bits(&s->gb, 8);
00509 }
00510 }
00511
00512 return -1;
00513 }
00514
00519 static int h261_decode_mb_skipped(H261Context *h, int mba1, int mba2 )
00520 {
00521 MpegEncContext * const s = &h->s;
00522 int i;
00523
00524 s->mb_intra = 0;
00525
00526 for(i=mba1; i<mba2; i++){
00527 int j, xy;
00528
00529 s->mb_x= ((h->gob_number-1) % 2) * 11 + i % 11;
00530 s->mb_y= ((h->gob_number-1) / 2) * 3 + i / 11;
00531 xy = s->mb_x + s->mb_y * s->mb_stride;
00532 ff_init_block_index(s);
00533 ff_update_block_index(s);
00534
00535 for(j=0;j<6;j++)
00536 s->block_last_index[j] = -1;
00537
00538 s->mv_dir = MV_DIR_FORWARD;
00539 s->mv_type = MV_TYPE_16X16;
00540 s->current_picture.mb_type[xy]= MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
00541 s->mv[0][0][0] = 0;
00542 s->mv[0][0][1] = 0;
00543 s->mb_skipped = 1;
00544 h->mtype &= ~MB_TYPE_H261_FIL;
00545
00546 MPV_decode_mb(s, s->block);
00547 }
00548
00549 return 0;
00550 }
00551
00552 static int decode_mv_component(GetBitContext *gb, int v){
00553 int mv_diff = get_vlc2(gb, h261_mv_vlc.table, H261_MV_VLC_BITS, 2);
00554
00555
00556 if ( mv_diff < 0 )
00557 return v;
00558
00559 mv_diff = mvmap[mv_diff];
00560
00561 if(mv_diff && !get_bits1(gb))
00562 mv_diff= -mv_diff;
00563
00564 v += mv_diff;
00565 if (v <=-16) v+= 32;
00566 else if(v >= 16) v-= 32;
00567
00568 return v;
00569 }
00570
00571 static int h261_decode_mb(H261Context *h){
00572 MpegEncContext * const s = &h->s;
00573 int i, cbp, xy;
00574
00575 cbp = 63;
00576
00577 do{
00578 h->mba_diff = get_vlc2(&s->gb, h261_mba_vlc.table, H261_MBA_VLC_BITS, 2);
00579
00580
00581
00582 if (h->mba_diff == MBA_STARTCODE){
00583 h->gob_start_code_skipped = 1;
00584 return SLICE_END;
00585 }
00586 }
00587 while( h->mba_diff == MBA_STUFFING );
00588
00589 if ( h->mba_diff < 0 ){
00590 if ( get_bits_count(&s->gb) + 7 >= s->gb.size_in_bits )
00591 return SLICE_END;
00592
00593 av_log(s->avctx, AV_LOG_ERROR, "illegal mba at %d %d\n", s->mb_x, s->mb_y);
00594 return SLICE_ERROR;
00595 }
00596
00597 h->mba_diff += 1;
00598 h->current_mba += h->mba_diff;
00599
00600 if ( h->current_mba > MBA_STUFFING )
00601 return SLICE_ERROR;
00602
00603 s->mb_x= ((h->gob_number-1) % 2) * 11 + ((h->current_mba-1) % 11);
00604 s->mb_y= ((h->gob_number-1) / 2) * 3 + ((h->current_mba-1) / 11);
00605 xy = s->mb_x + s->mb_y * s->mb_stride;
00606 ff_init_block_index(s);
00607 ff_update_block_index(s);
00608
00609
00610 h->mtype = get_vlc2(&s->gb, h261_mtype_vlc.table, H261_MTYPE_VLC_BITS, 2);
00611 h->mtype = h261_mtype_map[h->mtype];
00612
00613
00614 if ( IS_QUANT ( h->mtype ) ){
00615 ff_set_qscale(s, get_bits(&s->gb, 5));
00616 }
00617
00618 s->mb_intra = IS_INTRA4x4(h->mtype);
00619
00620
00621 if ( IS_16X16 ( h->mtype ) ){
00622
00623
00624
00625
00626
00627
00628 if ( ( h->current_mba == 1 ) || ( h->current_mba == 12 ) || ( h->current_mba == 23 ) ||
00629 ( h->mba_diff != 1))
00630 {
00631 h->current_mv_x = 0;
00632 h->current_mv_y = 0;
00633 }
00634
00635 h->current_mv_x= decode_mv_component(&s->gb, h->current_mv_x);
00636 h->current_mv_y= decode_mv_component(&s->gb, h->current_mv_y);
00637 }else{
00638 h->current_mv_x = 0;
00639 h->current_mv_y = 0;
00640 }
00641
00642
00643 if ( HAS_CBP( h->mtype ) ){
00644 cbp = get_vlc2(&s->gb, h261_cbp_vlc.table, H261_CBP_VLC_BITS, 2) + 1;
00645 }
00646
00647 if(s->mb_intra){
00648 s->current_picture.mb_type[xy]= MB_TYPE_INTRA;
00649 goto intra;
00650 }
00651
00652
00653 s->mv_dir = MV_DIR_FORWARD;
00654 s->mv_type = MV_TYPE_16X16;
00655 s->current_picture.mb_type[xy]= MB_TYPE_16x16 | MB_TYPE_L0;
00656 s->mv[0][0][0] = h->current_mv_x * 2;
00657 s->mv[0][0][1] = h->current_mv_y * 2;
00658
00659 intra:
00660
00661 if(s->mb_intra || HAS_CBP(h->mtype)){
00662 s->dsp.clear_blocks(s->block[0]);
00663 for (i = 0; i < 6; i++) {
00664 if (h261_decode_block(h, s->block[i], i, cbp&32) < 0){
00665 return SLICE_ERROR;
00666 }
00667 cbp+=cbp;
00668 }
00669 }else{
00670 for (i = 0; i < 6; i++)
00671 s->block_last_index[i]= -1;
00672 }
00673
00674 MPV_decode_mb(s, s->block);
00675
00676 return SLICE_OK;
00677 }
00678
00683 static int h261_decode_block(H261Context * h, DCTELEM * block,
00684 int n, int coded)
00685 {
00686 MpegEncContext * const s = &h->s;
00687 int code, level, i, j, run;
00688 RLTable *rl = &h261_rl_tcoeff;
00689 const uint8_t *scan_table;
00690
00691
00692
00693
00694
00695
00696 scan_table = s->intra_scantable.permutated;
00697 if (s->mb_intra){
00698
00699 level = get_bits(&s->gb, 8);
00700
00701 if((level&0x7F) == 0){
00702 av_log(s->avctx, AV_LOG_ERROR, "illegal dc %d at %d %d\n", level, s->mb_x, s->mb_y);
00703 return -1;
00704 }
00705
00706 if (level == 255)
00707 level = 128;
00708 block[0] = level;
00709 i = 1;
00710 }else if(coded){
00711
00712
00713
00714
00715 int check = show_bits(&s->gb, 2);
00716 i = 0;
00717 if ( check & 0x2 ){
00718 skip_bits(&s->gb, 2);
00719 block[0] = ( check & 0x1 ) ? -1 : 1;
00720 i = 1;
00721 }
00722 }else{
00723 i = 0;
00724 }
00725 if(!coded){
00726 s->block_last_index[n] = i - 1;
00727 return 0;
00728 }
00729 for(;;){
00730 code = get_vlc2(&s->gb, rl->vlc.table, TCOEFF_VLC_BITS, 2);
00731 if (code < 0){
00732 av_log(s->avctx, AV_LOG_ERROR, "illegal ac vlc code at %dx%d\n", s->mb_x, s->mb_y);
00733 return -1;
00734 }
00735 if (code == rl->n) {
00736
00737
00738 run = get_bits(&s->gb, 6);
00739 level = get_sbits(&s->gb, 8);
00740 }else if(code == 0){
00741 break;
00742 }else{
00743 run = rl->table_run[code];
00744 level = rl->table_level[code];
00745 if (get_bits1(&s->gb))
00746 level = -level;
00747 }
00748 i += run;
00749 if (i >= 64){
00750 av_log(s->avctx, AV_LOG_ERROR, "run overflow at %dx%d\n", s->mb_x, s->mb_y);
00751 return -1;
00752 }
00753 j = scan_table[i];
00754 block[j] = level;
00755 i++;
00756 }
00757 s->block_last_index[n] = i-1;
00758 return 0;
00759 }
00760
00765 int h261_decode_picture_header(H261Context *h){
00766 MpegEncContext * const s = &h->s;
00767 int format, i;
00768 uint32_t startcode= 0;
00769
00770 for(i= s->gb.size_in_bits - get_bits_count(&s->gb); i>24; i-=1){
00771 startcode = ((startcode << 1) | get_bits(&s->gb, 1)) & 0x000FFFFF;
00772
00773 if(startcode == 0x10)
00774 break;
00775 }
00776
00777 if (startcode != 0x10){
00778 av_log(s->avctx, AV_LOG_ERROR, "Bad picture start code\n");
00779 return -1;
00780 }
00781
00782
00783 s->picture_number = get_bits(&s->gb, 5);
00784
00785
00786 skip_bits1(&s->gb);
00787 skip_bits1(&s->gb);
00788 skip_bits1(&s->gb);
00789
00790 format = get_bits1(&s->gb);
00791
00792
00793 if (format == 0){
00794 s->width = 176;
00795 s->height = 144;
00796 s->mb_width = 11;
00797 s->mb_height = 9;
00798 }else{
00799 s->width = 352;
00800 s->height = 288;
00801 s->mb_width = 22;
00802 s->mb_height = 18;
00803 }
00804
00805 s->mb_num = s->mb_width * s->mb_height;
00806
00807 skip_bits1(&s->gb);
00808 skip_bits1(&s->gb);
00809
00810
00811 while (get_bits1(&s->gb) != 0){
00812 skip_bits(&s->gb, 8);
00813 }
00814
00815
00816
00817 s->pict_type = P_TYPE;
00818
00819 h->gob_number = 0;
00820 return 0;
00821 }
00822
00823 static int h261_decode_gob(H261Context *h){
00824 MpegEncContext * const s = &h->s;
00825
00826 ff_set_qscale(s, s->qscale);
00827
00828
00829 while(h->current_mba <= MBA_STUFFING)
00830 {
00831 int ret;
00832
00833 ret= h261_decode_mb(h);
00834 if(ret<0){
00835 if(ret==SLICE_END){
00836 h261_decode_mb_skipped(h, h->current_mba, 33);
00837 return 0;
00838 }
00839 av_log(s->avctx, AV_LOG_ERROR, "Error at MB: %d\n", s->mb_x + s->mb_y*s->mb_stride);
00840 return -1;
00841 }
00842
00843 h261_decode_mb_skipped(h, h->current_mba-h->mba_diff, h->current_mba-1);
00844 }
00845
00846 return -1;
00847 }
00848
00849 static int h261_find_frame_end(ParseContext *pc, AVCodecContext* avctx, const uint8_t *buf, int buf_size){
00850 int vop_found, i, j;
00851 uint32_t state;
00852
00853 vop_found= pc->frame_start_found;
00854 state= pc->state;
00855
00856 for(i=0; i<buf_size && !vop_found; i++){
00857 state= (state<<8) | buf[i];
00858 for(j=0; j<8; j++){
00859 if(((state>>j)&0xFFFFF) == 0x00010){
00860 i++;
00861 vop_found=1;
00862 break;
00863 }
00864 }
00865 }
00866 if(vop_found){
00867 for(; i<buf_size; i++){
00868 state= (state<<8) | buf[i];
00869 for(j=0; j<8; j++){
00870 if(((state>>j)&0xFFFFF) == 0x00010){
00871 pc->frame_start_found=0;
00872 pc->state= state>>(2*8);
00873 return i-1;
00874 }
00875 }
00876 }
00877 }
00878
00879 pc->frame_start_found= vop_found;
00880 pc->state= state;
00881 return END_NOT_FOUND;
00882 }
00883
00884 static int h261_parse(AVCodecParserContext *s,
00885 AVCodecContext *avctx,
00886 uint8_t **poutbuf, int *poutbuf_size,
00887 const uint8_t *buf, int buf_size)
00888 {
00889 ParseContext *pc = s->priv_data;
00890 int next;
00891
00892 next= h261_find_frame_end(pc,avctx, buf, buf_size);
00893 if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
00894 *poutbuf = NULL;
00895 *poutbuf_size = 0;
00896 return buf_size;
00897 }
00898 *poutbuf = (uint8_t *)buf;
00899 *poutbuf_size = buf_size;
00900 return next;
00901 }
00902
00906 static int get_consumed_bytes(MpegEncContext *s, int buf_size){
00907 int pos= get_bits_count(&s->gb)>>3;
00908 if(pos==0) pos=1;
00909 if(pos+10>buf_size) pos=buf_size;
00910
00911 return pos;
00912 }
00913
00914 static int h261_decode_frame(AVCodecContext *avctx,
00915 void *data, int *data_size,
00916 uint8_t *buf, int buf_size)
00917 {
00918 H261Context *h= avctx->priv_data;
00919 MpegEncContext *s = &h->s;
00920 int ret;
00921 AVFrame *pict = data;
00922
00923 #ifdef DEBUG
00924 printf("*****frame %d size=%d\n", avctx->frame_number, buf_size);
00925 printf("bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
00926 #endif
00927 s->flags= avctx->flags;
00928 s->flags2= avctx->flags2;
00929
00930 h->gob_start_code_skipped=0;
00931
00932 retry:
00933
00934 init_get_bits(&s->gb, buf, buf_size*8);
00935
00936 if(!s->context_initialized){
00937 if (MPV_common_init(s) < 0)
00938 return -1;
00939 }
00940
00941
00942 if(s->current_picture_ptr==NULL || s->current_picture_ptr->data[0]){
00943 int i= ff_find_unused_picture(s, 0);
00944 s->current_picture_ptr= &s->picture[i];
00945 }
00946
00947 ret = h261_decode_picture_header(h);
00948
00949
00950 if (ret < 0){
00951 av_log(s->avctx, AV_LOG_ERROR, "header damaged\n");
00952 return -1;
00953 }
00954
00955 if (s->width != avctx->coded_width || s->height != avctx->coded_height){
00956 ParseContext pc= s->parse_context;
00957 s->parse_context.buffer=0;
00958 MPV_common_end(s);
00959 s->parse_context= pc;
00960 }
00961 if (!s->context_initialized) {
00962 avcodec_set_dimensions(avctx, s->width, s->height);
00963
00964 goto retry;
00965 }
00966
00967
00968 s->current_picture.pict_type= s->pict_type;
00969 s->current_picture.key_frame= s->pict_type == I_TYPE;
00970
00971
00972 if(avctx->hurry_up>=5) return get_consumed_bytes(s, buf_size);
00973 if( (avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type==B_TYPE)
00974 ||(avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type!=I_TYPE)
00975 || avctx->skip_frame >= AVDISCARD_ALL)
00976 return get_consumed_bytes(s, buf_size);
00977
00978 if(MPV_frame_start(s, avctx) < 0)
00979 return -1;
00980
00981 ff_er_frame_start(s);
00982
00983
00984 s->mb_x=0;
00985 s->mb_y=0;
00986
00987 while(h->gob_number < (s->mb_height==18 ? 12 : 5)){
00988 if(ff_h261_resync(h)<0)
00989 break;
00990 h261_decode_gob(h);
00991 }
00992 MPV_frame_end(s);
00993
00994 assert(s->current_picture.pict_type == s->current_picture_ptr->pict_type);
00995 assert(s->current_picture.pict_type == s->pict_type);
00996 *pict= *(AVFrame*)s->current_picture_ptr;
00997 ff_print_debug_info(s, pict);
00998
00999
01000
01001 avctx->frame_number = s->picture_number - 1;
01002
01003 *data_size = sizeof(AVFrame);
01004
01005 return get_consumed_bytes(s, buf_size);
01006 }
01007
01008 static int h261_decode_end(AVCodecContext *avctx)
01009 {
01010 H261Context *h= avctx->priv_data;
01011 MpegEncContext *s = &h->s;
01012
01013 MPV_common_end(s);
01014 return 0;
01015 }
01016
01017 #ifdef CONFIG_ENCODERS
01018 AVCodec h261_encoder = {
01019 "h261",
01020 CODEC_TYPE_VIDEO,
01021 CODEC_ID_H261,
01022 sizeof(H261Context),
01023 MPV_encode_init,
01024 MPV_encode_picture,
01025 MPV_encode_end,
01026 };
01027 #endif
01028
01029 AVCodec h261_decoder = {
01030 "h261",
01031 CODEC_TYPE_VIDEO,
01032 CODEC_ID_H261,
01033 sizeof(H261Context),
01034 h261_decode_init,
01035 NULL,
01036 h261_decode_end,
01037 h261_decode_frame,
01038 CODEC_CAP_DR1,
01039 };
01040
01041 AVCodecParser h261_parser = {
01042 { CODEC_ID_H261 },
01043 sizeof(ParseContext),
01044 NULL,
01045 h261_parse,
01046 ff_parse_close,
01047 };