h263.c 43.5 KB
Newer Older
F
Fabrice Bellard 已提交
1 2 3
/*
 * H263/MPEG4 backend for ffmpeg encoder and decoder
 * Copyright (c) 2000,2001 Gerard Lantau.
4
 * H263+ support.
F
Fabrice Bellard 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
 * Copyright (c) 2001 Juan J. Sierralta P.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
#include "common.h"
#include "dsputil.h"
#include "avcodec.h"
#include "mpegvideo.h"
#include "h263data.h"
#include "mpeg4data.h"

static void h263_encode_block(MpegEncContext * s, DCTELEM * block,
			      int n);
static void h263_encode_motion(MpegEncContext * s, int val);
31
static void h263p_encode_umotion(MpegEncContext * s, int val);
F
Fabrice Bellard 已提交
32 33 34
static void mpeg4_encode_block(MpegEncContext * s, DCTELEM * block,
			       int n);
static int h263_decode_motion(MpegEncContext * s, int pred);
35
static int h263p_decode_umotion(MpegEncContext * s, int pred);
F
Fabrice Bellard 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
static int h263_decode_block(MpegEncContext * s, DCTELEM * block,
                             int n, int coded);
static int mpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
                              int n, int coded);

int h263_get_picture_format(int width, int height)
{
    int format;

    if (width == 128 && height == 96)
	format = 1;
    else if (width == 176 && height == 144)
	format = 2;
    else if (width == 352 && height == 288)
	format = 3;
    else if (width == 704 && height == 576)
	format = 4;
    else if (width == 1408 && height == 1152)
	format = 5;
    else
        format = 7;
    return format;
}

void h263_encode_picture_header(MpegEncContext * s, int picture_number)
{
62
    int format;
F
Fabrice Bellard 已提交
63 64 65 66 67 68 69 70 71 72 73

    align_put_bits(&s->pb);
    put_bits(&s->pb, 22, 0x20);
    put_bits(&s->pb, 8, ((s->picture_number * 30 * FRAME_RATE_BASE) / 
                         s->frame_rate) & 0xff);

    put_bits(&s->pb, 1, 1);	/* marker */
    put_bits(&s->pb, 1, 0);	/* h263 id */
    put_bits(&s->pb, 1, 0);	/* split screen off */
    put_bits(&s->pb, 1, 0);	/* camera  off */
    put_bits(&s->pb, 1, 0);	/* freeze picture release off */
74 75
    
    format = h263_get_picture_format(s->width, s->height);
F
Fabrice Bellard 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
    if (!s->h263_plus) {
        /* H.263v1 */
        put_bits(&s->pb, 3, format);
        put_bits(&s->pb, 1, (s->pict_type == P_TYPE));
        /* By now UMV IS DISABLED ON H.263v1, since the restrictions
        of H.263v1 UMV implies to check the predicted MV after
        calculation of the current MB to see if we're on the limits */
        put_bits(&s->pb, 1, 0);	/* unrestricted motion vector: off */
        put_bits(&s->pb, 1, 0);	/* SAC: off */
        put_bits(&s->pb, 1, 0);	/* advanced prediction mode: off */
        put_bits(&s->pb, 1, 0);	/* not PB frame */
        put_bits(&s->pb, 5, s->qscale);
        put_bits(&s->pb, 1, 0);	/* Continuous Presence Multipoint mode: off */
    } else {
        /* H.263v2 */
        /* H.263 Plus PTYPE */
        put_bits(&s->pb, 3, 7);
        put_bits(&s->pb,3,1); /* Update Full Extended PTYPE */
94 95 96 97 98
        if (format == 7)
            put_bits(&s->pb,3,6); /* Custom Source Format */
        else
            put_bits(&s->pb, 3, format);
            
F
Fabrice Bellard 已提交
99
        put_bits(&s->pb,1,0); /* Custom PCF: off */
100 101
        s->umvplus = (s->pict_type == P_TYPE) && s->unrestricted_mv;
        put_bits(&s->pb, 1, s->umvplus); /* Unrestricted Motion Vector */
F
Fabrice Bellard 已提交
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
        put_bits(&s->pb,1,0); /* SAC: off */
        put_bits(&s->pb,1,0); /* Advanced Prediction Mode: off */
        put_bits(&s->pb,1,0); /* Advanced Intra Coding: off */
        put_bits(&s->pb,1,0); /* Deblocking Filter: off */
        put_bits(&s->pb,1,0); /* Slice Structured: off */
        put_bits(&s->pb,1,0); /* Reference Picture Selection: off */
        put_bits(&s->pb,1,0); /* Independent Segment Decoding: off */
        put_bits(&s->pb,1,0); /* Alternative Inter VLC: off */
        put_bits(&s->pb,1,0); /* Modified Quantization: off */
        put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */
        put_bits(&s->pb,3,0); /* Reserved */
		
        put_bits(&s->pb, 3, s->pict_type == P_TYPE);
		
        put_bits(&s->pb,1,0); /* Reference Picture Resampling: off */
        put_bits(&s->pb,1,0); /* Reduced-Resolution Update: off */
        put_bits(&s->pb,1,0); /* Rounding Type */
        put_bits(&s->pb,2,0); /* Reserved */
        put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */
		
        /* This should be here if PLUSPTYPE */
        put_bits(&s->pb, 1, 0);	/* Continuous Presence Multipoint mode: off */
		
125 126
		if (format == 7) {
            /* Custom Picture Format (CPFMT) */
F
Fabrice Bellard 已提交
127
		
128 129 130 131 132 133
            put_bits(&s->pb,4,2); /* Aspect ratio: CIF 12:11 (4:3) picture */
            put_bits(&s->pb,9,(s->width >> 2) - 1);
            put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */
            put_bits(&s->pb,9,(s->height >> 2));
        }
        
F
Fabrice Bellard 已提交
134
        /* Unlimited Unrestricted Motion Vectors Indicator (UUI) */
135
        if (s->umvplus)
F
Fabrice Bellard 已提交
136 137 138 139 140 141 142 143 144 145 146 147 148 149
            put_bits(&s->pb,1,1); /* Limited according tables of Annex D */
        put_bits(&s->pb, 5, s->qscale);
    }

    put_bits(&s->pb, 1, 0);	/* no PEI */
}

void h263_encode_mb(MpegEncContext * s,
		    DCTELEM block[6][64],
		    int motion_x, int motion_y)
{
    int cbpc, cbpy, i, cbp, pred_x, pred_y;

    //    printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y);
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
   if (!s->mb_intra) {
	   /* compute cbp */
	   cbp = 0;
	   for (i = 0; i < 6; i++) {
	      if (s->block_last_index[i] >= 0)
		   cbp |= 1 << (5 - i);
	   }
	   if ((cbp | motion_x | motion_y) == 0) {
	      /* skip macroblock */
	      put_bits(&s->pb, 1, 1);
	      return;
	   }
	   put_bits(&s->pb, 1, 0);	/* mb coded */
	   cbpc = cbp & 3;
	   put_bits(&s->pb,
		inter_MCBPC_bits[cbpc],
		inter_MCBPC_code[cbpc]);
	   cbpy = cbp >> 2;
	   cbpy ^= 0xf;
	   put_bits(&s->pb, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]);

	   /* motion vectors: 16x16 mode only now */
      h263_pred_motion(s, 0, &pred_x, &pred_y);
      
174
      if (!s->umvplus) {  
175 176 177 178 179 180 181 182 183 184 185
         h263_encode_motion(s, motion_x - pred_x);
         h263_encode_motion(s, motion_y - pred_y);
      }
      else {
         h263p_encode_umotion(s, motion_x - pred_x);
         h263p_encode_umotion(s, motion_y - pred_y);
         if (((motion_x - pred_x) == 1) && ((motion_y - pred_y) == 1))
            /* To prevent Start Code emulation */
            put_bits(&s->pb,1,1);
      }
   } else {
F
Fabrice Bellard 已提交
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
	/* compute cbp */
	cbp = 0;
	for (i = 0; i < 6; i++) {
	    if (s->block_last_index[i] >= 1)
		cbp |= 1 << (5 - i);
	}

	cbpc = cbp & 3;
	if (s->pict_type == I_TYPE) {
	    put_bits(&s->pb,
		     intra_MCBPC_bits[cbpc],
		     intra_MCBPC_code[cbpc]);
	} else {
	    put_bits(&s->pb, 1, 0);	/* mb coded */
	    put_bits(&s->pb,
		     inter_MCBPC_bits[cbpc + 4],
		     inter_MCBPC_code[cbpc + 4]);
	}
	if (s->h263_pred) {
	    /* XXX: currently, we do not try to use ac prediction */
	    put_bits(&s->pb, 1, 0);	/* no ac prediction */
	}
	cbpy = cbp >> 2;
	put_bits(&s->pb, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]);
    }

    /* encode each block */
    if (s->h263_pred) {
	for (i = 0; i < 6; i++) {
	    mpeg4_encode_block(s, block[i], i);
	}
    } else {
	for (i = 0; i < 6; i++) {
	    h263_encode_block(s, block[i], i);
	}
    }
}

static inline int mid_pred(int a, int b, int c)
{
    int vmin, vmax;
    vmin = a;
    if (b < vmin)
        vmin = b;
    if (c < vmin)
        vmin = c;

    vmax = a;
    if (b > vmax)
        vmax = b;
    if (c > vmax)
        vmax = c;

    return a + b + c - vmin - vmax;
}

INT16 *h263_pred_motion(MpegEncContext * s, int block, 
                        int *px, int *py)
{
    int x, y, wrap;
    INT16 *A, *B, *C, *mot_val;

    x = 2 * s->mb_x + 1 + (block & 1);
    y = 2 * s->mb_y + 1 + ((block >> 1) & 1);
    wrap = 2 * s->mb_width + 2;

    mot_val = s->motion_val[(x) + (y) * wrap];

    /* special case for first line */
255
    if (y == 1 || s->first_slice_line || s->first_gob_line) {
F
Fabrice Bellard 已提交
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
        A = s->motion_val[(x-1) + (y) * wrap];
        *px = A[0];
        *py = A[1];
    } else {
        switch(block) {
        default:
        case 0:
            A = s->motion_val[(x-1) + (y) * wrap];
            B = s->motion_val[(x) + (y-1) * wrap];
            C = s->motion_val[(x+2) + (y-1) * wrap];
            break;
        case 1:
        case 2:
            A = s->motion_val[(x-1) + (y) * wrap];
            B = s->motion_val[(x) + (y-1) * wrap];
            C = s->motion_val[(x+1) + (y-1) * wrap];
            break;
        case 3:
            A = s->motion_val[(x-1) + (y) * wrap];
            B = s->motion_val[(x-1) + (y-1) * wrap];
            C = s->motion_val[(x) + (y-1) * wrap];
            break;
        }
        *px = mid_pred(A[0], B[0], C[0]);
        *py = mid_pred(A[1], B[1], C[1]);
    }
    return mot_val;
}


static void h263_encode_motion(MpegEncContext * s, int val)
{
    int range, l, m, bit_size, sign, code, bits;

    if (val == 0) {
        /* zero vector */
        code = 0;
        put_bits(&s->pb, mvtab[code][1], mvtab[code][0]);
    } else {
        bit_size = s->f_code - 1;
        range = 1 << bit_size;
        /* modulo encoding */
        l = range * 32;
        m = 2 * l;
        if (val < -l) {
            val += m;
        } else if (val >= l) {
            val -= m;
        }

        if (val >= 0) {
            val--;
            code = (val >> bit_size) + 1;
            bits = val & (range - 1);
            sign = 0;
        } else {
            val = -val;
            val--;
            code = (val >> bit_size) + 1;
            bits = val & (range - 1);
            sign = 1;
        }

        put_bits(&s->pb, mvtab[code][1] + 1, (mvtab[code][0] << 1) | sign); 
        if (bit_size > 0) {
            put_bits(&s->pb, bit_size, bits);
        }
    }
}

326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
/* Encode MV differences on H.263+ with Unrestricted MV mode */
static void h263p_encode_umotion(MpegEncContext * s, int val)
{
    short sval = 0; 
    short i = 0;
    short n_bits = 0;
    short temp_val;
    int code = 0;
    int tcode;
    
    if ( val == 0)
        put_bits(&s->pb, 1, 1);
    else if (val == 1)
        put_bits(&s->pb, 3, 0);
    else if (val == -1)
        put_bits(&s->pb, 3, 2);
    else {
        
        sval = ((val < 0) ? (short)(-val):(short)val);
        temp_val = sval;
        
        while (temp_val != 0) {
            temp_val = temp_val >> 1;
            n_bits++;
        }
        
        i = n_bits - 1;
        while (i > 0) {
            tcode = (sval & (1 << (i-1))) >> (i-1);
            tcode = (tcode << 1) | 1;
            code = (code << 2) | tcode;
            i--;
        }
        code = ((code << 1) | (val < 0)) << 1;
        put_bits(&s->pb, (2*n_bits)+1, code);
        //printf("\nVal = %d\tCode = %d", sval, code);
    }
}

F
Fabrice Bellard 已提交
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752
void h263_encode_init_vlc(MpegEncContext *s)
{
    static int done = 0;

    if (!done) {
        done = 1;
        init_rl(&rl_inter);
        init_rl(&rl_intra);
    }
}

static void h263_encode_block(MpegEncContext * s, DCTELEM * block, int n)
{
    int level, run, last, i, j, last_index, last_non_zero, sign, slevel;
    int code;
    RLTable *rl = &rl_inter;

    if (s->mb_intra) {
	/* DC coef */
	level = block[0];
        /* 255 cannot be represented, so we clamp */
        if (level > 254) {
            level = 254;
            block[0] = 254;
        }
	if (level == 128)
	    put_bits(&s->pb, 8, 0xff);
	else
	    put_bits(&s->pb, 8, level & 0xff);
	i = 1;
    } else {
	i = 0;
    }

    /* AC coefs */
    last_index = s->block_last_index[n];
    last_non_zero = i - 1;
    for (; i <= last_index; i++) {
	j = zigzag_direct[i];
	level = block[j];
	if (level) {
	    run = i - last_non_zero - 1;
	    last = (i == last_index);
	    sign = 0;
	    slevel = level;
	    if (level < 0) {
		sign = 1;
		level = -level;
	    }
            code = get_rl_index(rl, last, run, level);
            put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
            if (code == rl->n) {
                put_bits(&s->pb, 1, last);
                put_bits(&s->pb, 6, run);
                put_bits(&s->pb, 8, slevel & 0xff);
            } else {
                put_bits(&s->pb, 1, sign);
            }
	    last_non_zero = i;
	}
    }
}

/***************************************************/

/* write mpeg4 VOP header */
void mpeg4_encode_picture_header(MpegEncContext * s, int picture_number)
{
    align_put_bits(&s->pb);

    put_bits(&s->pb, 32, 0x1B6);	/* vop header */
    put_bits(&s->pb, 2, s->pict_type - 1);	/* pict type: I = 0 , P = 1 */
    /* XXX: time base + 1 not always correct */
    put_bits(&s->pb, 1, 1);
    put_bits(&s->pb, 1, 0);

    put_bits(&s->pb, 1, 1);	/* marker */
    put_bits(&s->pb, 4, 1);	/* XXX: correct time increment */
    put_bits(&s->pb, 1, 1);	/* marker */
    put_bits(&s->pb, 1, 1);	/* vop coded */
    if (s->pict_type == P_TYPE) {
        s->no_rounding = 0;
	put_bits(&s->pb, 1, s->no_rounding);	/* rounding type */
    }
    put_bits(&s->pb, 3, 0);	/* intra dc VLC threshold */

    put_bits(&s->pb, 5, s->qscale);

    if (s->pict_type != I_TYPE)
	put_bits(&s->pb, 3, s->f_code);	/* fcode_for */
    //    printf("****frame %d\n", picture_number);
}

void h263_dc_scale(MpegEncContext * s)
{
    int quant;

    quant = s->qscale;
    /* luminance */
    if (quant < 5)
	s->y_dc_scale = 8;
    else if (quant > 4 && quant < 9)
	s->y_dc_scale = (2 * quant);
    else if (quant > 8 && quant < 25)
	s->y_dc_scale = (quant + 8);
    else
	s->y_dc_scale = (2 * quant - 16);
    /* chrominance */
    if (quant < 5)
	s->c_dc_scale = 8;
    else if (quant > 4 && quant < 25)
	s->c_dc_scale = ((quant + 13) / 2);
    else
	s->c_dc_scale = (quant - 6);
}

static int mpeg4_pred_dc(MpegEncContext * s, int n, UINT16 **dc_val_ptr, int *dir_ptr)
{
    int a, b, c, x, y, wrap, pred, scale;
    UINT16 *dc_val;

    /* find prediction */
    if (n < 4) {
	x = 2 * s->mb_x + 1 + (n & 1);
	y = 2 * s->mb_y + 1 + ((n & 2) >> 1);
	wrap = s->mb_width * 2 + 2;
	dc_val = s->dc_val[0];
	scale = s->y_dc_scale;
    } else {
	x = s->mb_x + 1;
	y = s->mb_y + 1;
	wrap = s->mb_width + 2;
	dc_val = s->dc_val[n - 4 + 1];
	scale = s->c_dc_scale;
    }

    /* B C
     * A X 
     */
    a = dc_val[(x - 1) + (y) * wrap];
    b = dc_val[(x - 1) + (y - 1) * wrap];
    c = dc_val[(x) + (y - 1) * wrap];

    if (abs(a - b) < abs(b - c)) {
	pred = c;
        *dir_ptr = 1; /* top */
    } else {
	pred = a;
        *dir_ptr = 0; /* left */
    }
    /* we assume pred is positive */
    pred = (pred + (scale >> 1)) / scale;

    /* prepare address for prediction update */
    *dc_val_ptr = &dc_val[(x) + (y) * wrap];

    return pred;
}

void mpeg4_pred_ac(MpegEncContext * s, INT16 *block, int n, 
                   int dir)
{
    int x, y, wrap, i;
    INT16 *ac_val, *ac_val1;

    /* find prediction */
    if (n < 4) {
	x = 2 * s->mb_x + 1 + (n & 1);
	y = 2 * s->mb_y + 1 + ((n & 2) >> 1);
	wrap = s->mb_width * 2 + 2;
	ac_val = s->ac_val[0][0];
    } else {
	x = s->mb_x + 1;
	y = s->mb_y + 1;
	wrap = s->mb_width + 2;
	ac_val = s->ac_val[n - 4 + 1][0];
    }
    ac_val += ((y) * wrap + (x)) * 16;
    ac_val1 = ac_val;
    if (s->ac_pred) {
        if (dir == 0) {
            /* left prediction */
            ac_val -= 16;
            for(i=1;i<8;i++) {
                block[i*8] += ac_val[i];
            }
        } else {
            /* top prediction */
            ac_val -= 16 * wrap;
            for(i=1;i<8;i++) {
                block[i] += ac_val[i + 8];
            }
        }
    }
    /* left copy */
    for(i=1;i<8;i++)
        ac_val1[i] = block[i * 8];
    /* top copy */
    for(i=1;i<8;i++)
        ac_val1[8 + i] = block[i];
}

static inline void mpeg4_encode_dc(MpegEncContext * s, int level, int n, int *dir_ptr)
{
    int size, v, pred;
    UINT16 *dc_val;

    pred = mpeg4_pred_dc(s, n, &dc_val, dir_ptr);
    if (n < 4) {
        *dc_val = level * s->y_dc_scale;
    } else {
        *dc_val = level * s->c_dc_scale;
    }

    /* do the prediction */
    level -= pred;
    /* find number of bits */
    size = 0;
    v = abs(level);
    while (v) {
	v >>= 1;
	size++;
    }

    if (n < 4) {
	/* luminance */
	put_bits(&s->pb, DCtab_lum[size][1], DCtab_lum[size][0]);
    } else {
	/* chrominance */
	put_bits(&s->pb, DCtab_chrom[size][1], DCtab_chrom[size][0]);
    }

    /* encode remaining bits */
    if (size > 0) {
	if (level < 0)
	    level = (-level) ^ ((1 << size) - 1);
	put_bits(&s->pb, size, level);
	if (size > 8)
	    put_bits(&s->pb, 1, 1);
    }
}

static void mpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n)
{
    int level, run, last, i, j, last_index, last_non_zero, sign, slevel;
    int code, dc_pred_dir;
    const RLTable *rl;

    if (s->mb_intra) {
	/* mpeg4 based DC predictor */
	mpeg4_encode_dc(s, block[0], n, &dc_pred_dir);
	i = 1;
        rl = &rl_intra;
    } else {
	i = 0;
        rl = &rl_inter;
    }

    /* AC coefs */
    last_index = s->block_last_index[n];
    last_non_zero = i - 1;
    for (; i <= last_index; i++) {
	j = zigzag_direct[i];
	level = block[j];
	if (level) {
	    run = i - last_non_zero - 1;
	    last = (i == last_index);
	    sign = 0;
	    slevel = level;
	    if (level < 0) {
		sign = 1;
		level = -level;
	    }
            code = get_rl_index(rl, last, run, level);
            put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
            if (code == rl->n) {
                int level1, run1;
                level1 = level - rl->max_level[last][run];
                if (level1 < 1) 
                    goto esc2;
                code = get_rl_index(rl, last, run, level1);
                if (code == rl->n) {
                esc2:
                    put_bits(&s->pb, 1, 1);
                    if (level > MAX_LEVEL)
                        goto esc3;
                    run1 = run - rl->max_run[last][level] - 1;
                    if (run1 < 0)
                        goto esc3;
                    code = get_rl_index(rl, last, run1, level);
                    if (code == rl->n) {
                    esc3:
                        /* third escape */
                        put_bits(&s->pb, 1, 1);
                        put_bits(&s->pb, 1, last);
                        put_bits(&s->pb, 6, run);
                        put_bits(&s->pb, 1, 1);
                        put_bits(&s->pb, 12, slevel & 0xfff);
                        put_bits(&s->pb, 1, 1);
                    } else {
                        /* second escape */
                        put_bits(&s->pb, 1, 0);
                        put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
                        put_bits(&s->pb, 1, sign);
                    }
                } else {
                    /* first escape */
                    put_bits(&s->pb, 1, 0);
                    put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
                    put_bits(&s->pb, 1, sign);
                }
            } else {
                put_bits(&s->pb, 1, sign);
            }
	    last_non_zero = i;
	}
    }
}



/***********************************************/
/* decoding */

static VLC intra_MCBPC_vlc;
static VLC inter_MCBPC_vlc;
static VLC cbpy_vlc;
static VLC mv_vlc;
static VLC dc_lum, dc_chrom;

void init_rl(RLTable *rl)
{
    INT8 max_level[MAX_RUN+1], max_run[MAX_LEVEL+1];
    UINT8 index_run[MAX_RUN+1];
    int last, run, level, start, end, i;

    /* compute max_level[], max_run[] and index_run[] */
    for(last=0;last<2;last++) {
        if (last == 0) {
            start = 0;
            end = rl->last;
        } else {
            start = rl->last;
            end = rl->n;
        }

        memset(max_level, 0, MAX_RUN + 1);
        memset(max_run, 0, MAX_LEVEL + 1);
        memset(index_run, rl->n, MAX_RUN + 1);
        for(i=start;i<end;i++) {
            run = rl->table_run[i];
            level = rl->table_level[i];
            if (index_run[run] == rl->n)
                index_run[run] = i;
            if (level > max_level[run])
                max_level[run] = level;
            if (run > max_run[level])
                max_run[level] = run;
        }
        rl->max_level[last] = malloc(MAX_RUN + 1);
        memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
        rl->max_run[last] = malloc(MAX_LEVEL + 1);
        memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
        rl->index_run[last] = malloc(MAX_RUN + 1);
        memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
    }
}

void init_vlc_rl(RLTable *rl)
{
    init_vlc(&rl->vlc, 9, rl->n + 1, 
             &rl->table_vlc[0][1], 4, 2,
             &rl->table_vlc[0][0], 4, 2);
}

/* init vlcs */

/* XXX: find a better solution to handle static init */
void h263_decode_init_vlc(MpegEncContext *s)
{
    static int done = 0;

    if (!done) {
        done = 1;

        init_vlc(&intra_MCBPC_vlc, 6, 8, 
                 intra_MCBPC_bits, 1, 1,
                 intra_MCBPC_code, 1, 1);
753
        init_vlc(&inter_MCBPC_vlc, 9, 25, 
F
Fabrice Bellard 已提交
754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778
                 inter_MCBPC_bits, 1, 1,
                 inter_MCBPC_code, 1, 1);
        init_vlc(&cbpy_vlc, 6, 16,
                 &cbpy_tab[0][1], 2, 1,
                 &cbpy_tab[0][0], 2, 1);
        init_vlc(&mv_vlc, 9, 33,
                 &mvtab[0][1], 2, 1,
                 &mvtab[0][0], 2, 1);
        init_rl(&rl_inter);
        init_rl(&rl_intra);
        init_vlc_rl(&rl_inter);
        init_vlc_rl(&rl_intra);
        init_vlc(&dc_lum, 9, 13,
                 &DCtab_lum[0][1], 2, 1,
                 &DCtab_lum[0][0], 2, 1);
        init_vlc(&dc_chrom, 9, 13,
                 &DCtab_chrom[0][1], 2, 1,
                 &DCtab_chrom[0][0], 2, 1);
    }
}

int h263_decode_mb(MpegEncContext *s,
                   DCTELEM block[6][64])
{
    int cbpc, cbpy, i, cbp, pred_x, pred_y, mx, my, dquant;
779
    unsigned int val;
F
Fabrice Bellard 已提交
780
    INT16 *mot_val;
781
    static INT8 quant_tab[4] = { -1, -2, 1, 2 };
782 783
    unsigned int gfid;        
    
784
    /* Check for GOB Start Code */
785 786 787 788 789 790
    if (s->mb_x == 0) {
        val = show_bits(&s->gb, 16);
        if (val == 0) {
            /* We have a GBSC probably with GSTUFF */
            skip_bits(&s->gb, 16); /* Drop the zeros */
            while (get_bits1(&s->gb) == 0); /* Seek the '1' bit */
791
#ifdef DEBUG
792 793
            fprintf(stderr,"\nGOB Start Code at MB %d\n", 
                (s->mb_y * s->mb_width) + s->mb_x);
794
#endif
795 796 797
            s->gob_number = get_bits(&s->gb, 5); /* GN */
            gfid = get_bits(&s->gb, 2); /* GFID */
            s->qscale = get_bits(&s->gb, 5); /* GQUANT */
798
#ifdef DEBUG
799
            fprintf(stderr, "\nGN: %u GFID: %u Quant: %u\n", gn, gfid, s->qscale);
800
#endif
801
        }
802
    }
803 804 805 806 807 808 809 810
    /* FIXME: In the future H.263+ will have intra prediction */
    /* and we are gonna need another way to detect MPEG4      */
    if (!s->h263_pred) {
        if (s->mb_y == s->gob_number)
            s->first_gob_line = 1;
        else
            s->first_gob_line = 0;
    }        
F
Fabrice Bellard 已提交
811
    if (s->pict_type == P_TYPE) {
812
        if (get_bits1(&s->gb)) {
F
Fabrice Bellard 已提交
813 814 815 816 817 818 819 820
            /* skip mb */
            s->mb_intra = 0;
            for(i=0;i<6;i++)
                s->block_last_index[i] = -1;
            s->mv_dir = MV_DIR_FORWARD;
            s->mv_type = MV_TYPE_16X16;
            s->mv[0][0][0] = 0;
            s->mv[0][0][1] = 0;
821
            s->mb_skiped = 1;
F
Fabrice Bellard 已提交
822 823 824
            return 0;
        }
        cbpc = get_vlc(&s->gb, &inter_MCBPC_vlc);
825
        //fprintf(stderr, "\tCBPC: %d", cbpc);
F
Fabrice Bellard 已提交
826 827
        if (cbpc < 0)
            return -1;
828 829 830 831
        if (cbpc > 20)
            cbpc+=3;
        else if (cbpc == 20)
            fprintf(stderr, "Stuffing !");
832
        
F
Fabrice Bellard 已提交
833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857
        dquant = cbpc & 8;
        s->mb_intra = ((cbpc & 4) != 0);
    } else {
        cbpc = get_vlc(&s->gb, &intra_MCBPC_vlc);
        if (cbpc < 0)
            return -1;
        dquant = cbpc & 4;
        s->mb_intra = 1;
    }

    if (!s->mb_intra) {
        cbpy = get_vlc(&s->gb, &cbpy_vlc);
        cbp = (cbpc & 3) | ((cbpy ^ 0xf) << 2);
        if (dquant) {
            s->qscale += quant_tab[get_bits(&s->gb, 2)];
            if (s->qscale < 1)
                s->qscale = 1;
            else if (s->qscale > 31)
                s->qscale = 31;
        }
        s->mv_dir = MV_DIR_FORWARD;
        if ((cbpc & 16) == 0) {
            /* 16x16 motion prediction */
            s->mv_type = MV_TYPE_16X16;
            h263_pred_motion(s, 0, &pred_x, &pred_y);
858
            if (s->umvplus_dec)
859 860 861
               mx = h263p_decode_umotion(s, pred_x);
            else
               mx = h263_decode_motion(s, pred_x);
F
Fabrice Bellard 已提交
862 863
            if (mx >= 0xffff)
                return -1;
864
            
865
            if (s->umvplus_dec)
866 867 868
               my = h263p_decode_umotion(s, pred_y);
            else    
               my = h263_decode_motion(s, pred_y);
F
Fabrice Bellard 已提交
869 870 871 872
            if (my >= 0xffff)
                return -1;
            s->mv[0][0][0] = mx;
            s->mv[0][0][1] = my;
873 874 875
            /*fprintf(stderr, "\n MB %d", (s->mb_y * s->mb_width) + s->mb_x);
            fprintf(stderr, "\n\tmvx: %d\t\tpredx: %d", mx, pred_x);
            fprintf(stderr, "\n\tmvy: %d\t\tpredy: %d", my, pred_y);*/
876
            if (s->umvplus_dec && (mx - pred_x) == 1 && (my - pred_y) == 1)
877 878
               skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */
                           
F
Fabrice Bellard 已提交
879 880 881 882
        } else {
            s->mv_type = MV_TYPE_8X8;
            for(i=0;i<4;i++) {
                mot_val = h263_pred_motion(s, i, &pred_x, &pred_y);
883
                if (s->umvplus_dec)
884 885 886
                  mx = h263p_decode_umotion(s, pred_x);
                else
                  mx = h263_decode_motion(s, pred_x);
F
Fabrice Bellard 已提交
887 888
                if (mx >= 0xffff)
                    return -1;
889
                
890
                if (s->umvplus_dec)
891 892 893
                  my = h263p_decode_umotion(s, pred_y);
                else    
                  my = h263_decode_motion(s, pred_y);
F
Fabrice Bellard 已提交
894 895 896 897
                if (my >= 0xffff)
                    return -1;
                s->mv[0][i][0] = mx;
                s->mv[0][i][1] = my;
898
                if (s->umvplus_dec && (mx - pred_x) == 1 && (my - pred_y) == 1)
899
                  skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */
F
Fabrice Bellard 已提交
900 901 902 903 904 905
                mot_val[0] = mx;
                mot_val[1] = my;
            }
        }
    } else {
        s->ac_pred = 0;
906
	    if (s->h263_pred) {
907
            s->ac_pred = get_bits1(&s->gb);
F
Fabrice Bellard 已提交
908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944
        }
        cbpy = get_vlc(&s->gb, &cbpy_vlc);
        cbp = (cbpc & 3) | (cbpy << 2);
        if (dquant) {
            s->qscale += quant_tab[get_bits(&s->gb, 2)];
            if (s->qscale < 1)
                s->qscale = 1;
            else if (s->qscale > 31)
                s->qscale = 31;
        }
    }

    /* decode each block */
    if (s->h263_pred) {
	for (i = 0; i < 6; i++) {
	    if (mpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1) < 0)
                return -1;
	}
    } else {
	for (i = 0; i < 6; i++) {
	    if (h263_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1) < 0)
                return -1;
	}
    }
    return 0;
}

static int h263_decode_motion(MpegEncContext * s, int pred)
{
    int code, val, sign, shift, l, m;

    code = get_vlc(&s->gb, &mv_vlc);
    if (code < 0)
        return 0xffff;

    if (code == 0)
        return pred;
945
    sign = get_bits1(&s->gb);
F
Fabrice Bellard 已提交
946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969
    shift = s->f_code - 1;
    val = (code - 1) << shift;
    if (shift > 0)
        val |= get_bits(&s->gb, shift);
    val++;
    if (sign)
        val = -val;
    val += pred;
    
    /* modulo decoding */
    if (!s->h263_long_vectors) {
        l = (1 << (s->f_code - 1)) * 32;
        m = 2 * l;
        if (val < -l) {
            val += m;
        } else if (val >= l) {
            val -= m;
        }
    } else {
        /* horrible h263 long vector mode */
        if (pred < -31 && val < -63)
            val += 64;
        if (pred > 32 && val > 63)
            val -= 64;
970
        
F
Fabrice Bellard 已提交
971 972 973 974
    }
    return val;
}

975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
/* Decodes RVLC of H.263+ UMV */
static int h263p_decode_umotion(MpegEncContext * s, int pred)
{
   int code = 0, sign;
   
   if (get_bits1(&s->gb)) /* Motion difference = 0 */
      return pred;
   
   code = 2 + get_bits1(&s->gb);
   
   while (get_bits1(&s->gb))
   {
      code <<= 1;
      code += get_bits1(&s->gb);
   }
   sign = code & 1;
   code >>= 1;
   
   code = (sign) ? (pred - code) : (pred + code);
#ifdef DEBUG
   fprintf(stderr,"H.263+ UMV Motion = %d\n", code);
#endif
   return code;   

}

F
Fabrice Bellard 已提交
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
static int h263_decode_block(MpegEncContext * s, DCTELEM * block,
                             int n, int coded)
{
    int code, level, i, j, last, run;
    RLTable *rl = &rl_inter;

    if (s->mb_intra) {
	/* DC coef */
        if (s->h263_rv10 && s->rv10_version == 3 && s->pict_type == I_TYPE) {
            int component, diff;
            component = (n <= 3 ? 0 : n - 4 + 1);
            level = s->last_dc[component];
            if (s->rv10_first_dc_coded[component]) {
                diff = rv_decode_dc(s, n);
                if (diff == 0xffff)
                    return -1;
                level += diff;
                level = level & 0xff; /* handle wrap round */
                s->last_dc[component] = level;
            } else {
                s->rv10_first_dc_coded[component] = 1;
            }
        } else {
            level = get_bits(&s->gb, 8);
            if (level == 255)
                level = 128;
        }
        block[0] = level;
	i = 1;
    } else {
	i = 0;
    }
    if (!coded) {
        s->block_last_index[n] = i - 1;
        return 0;
    }

    for(;;) {
        code = get_vlc(&s->gb, &rl->vlc);
        if (code < 0)
            return -1;
        if (code == rl->n) {
            /* escape */
1044
            last = get_bits1(&s->gb);
F
Fabrice Bellard 已提交
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055
            run = get_bits(&s->gb, 6);
            level = (INT8)get_bits(&s->gb, 8);
            if (s->h263_rv10 && level == -128) {
                /* XXX: should patch encoder too */
                level = get_bits(&s->gb, 12);
                level = (level << 20) >> 20;
            }
        } else {
            run = rl->table_run[code];
            level = rl->table_level[code];
            last = code >= rl->last;
1056
            if (get_bits1(&s->gb))
F
Fabrice Bellard 已提交
1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089
                level = -level;
        }
        i += run;
        if (i >= 64)
            return -1;
	j = zigzag_direct[i];
        block[j] = level;
        if (last)
            break;
        i++;
    }
    s->block_last_index[n] = i;
    return 0;
}

static int mpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr)
{
    int level, pred, code;
    UINT16 *dc_val;

    if (n < 4) 
        code = get_vlc(&s->gb, &dc_lum);
    else 
        code = get_vlc(&s->gb, &dc_chrom);
    if (code < 0)
        return -1;
    if (code == 0) {
        level = 0;
    } else {
        level = get_bits(&s->gb, code);
        if ((level >> (code - 1)) == 0) /* if MSB not set it is negative*/
            level = - (level ^ ((1 << code) - 1));
        if (code > 8)
1090
            skip_bits1(&s->gb); /* marker */
F
Fabrice Bellard 已提交
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146
    }

    pred = mpeg4_pred_dc(s, n, &dc_val, dir_ptr);
    level += pred;
    if (level < 0)
        level = 0;
    if (n < 4) {
        *dc_val = level * s->y_dc_scale;
    } else {
        *dc_val = level * s->c_dc_scale;
    }
    return level;
}

static int mpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
                              int n, int coded)
{
    int code, level, i, j, last, run;
    int dc_pred_dir;
    RLTable *rl;
    const UINT8 *scan_table;

    if (s->mb_intra) {
	/* DC coef */
        level = mpeg4_decode_dc(s, n, &dc_pred_dir);
        if (level < 0)
            return -1;
        block[0] = level;
	i = 1;
        if (!coded) 
            goto not_coded;
        rl = &rl_intra;
        if (s->ac_pred) {
            if (dc_pred_dir == 0) 
                scan_table = ff_alternate_vertical_scan; /* left */
            else
                scan_table = ff_alternate_horizontal_scan; /* top */
        } else {
            scan_table = zigzag_direct;
        }
    } else {
	i = 0;
        if (!coded) {
            s->block_last_index[n] = i - 1;
            return 0;
        }
        rl = &rl_inter;
        scan_table = zigzag_direct;
    }

    for(;;) {
        code = get_vlc(&s->gb, &rl->vlc);
        if (code < 0)
            return -1;
        if (code == rl->n) {
            /* escape */
1147 1148
            if (get_bits1(&s->gb) != 0) {
                if (get_bits1(&s->gb) != 0) {
F
Fabrice Bellard 已提交
1149
                    /* third escape */
1150
                    last = get_bits1(&s->gb);
F
Fabrice Bellard 已提交
1151
                    run = get_bits(&s->gb, 6);
1152
                    get_bits1(&s->gb); /* marker */
F
Fabrice Bellard 已提交
1153 1154
                    level = get_bits(&s->gb, 12);
                    level = (level << 20) >> 20; /* sign extend */
1155
                    skip_bits1(&s->gb); /* marker */
F
Fabrice Bellard 已提交
1156 1157 1158 1159 1160 1161 1162 1163 1164
                } else {
                    /* second escape */
                    code = get_vlc(&s->gb, &rl->vlc);
                    if (code < 0 || code >= rl->n)
                        return -1;
                    run = rl->table_run[code];
                    level = rl->table_level[code];
                    last = code >= rl->last;
                    run += rl->max_run[last][level] + 1;
1165
                    if (get_bits1(&s->gb))
F
Fabrice Bellard 已提交
1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176
                        level = -level;
                }
            } else {
                /* first escape */
                code = get_vlc(&s->gb, &rl->vlc);
                if (code < 0 || code >= rl->n)
                    return -1;
                run = rl->table_run[code];
                level = rl->table_level[code];
                last = code >= rl->last;
                level += rl->max_level[last][run];
1177
                if (get_bits1(&s->gb))
F
Fabrice Bellard 已提交
1178 1179 1180 1181 1182 1183
                    level = -level;
            }
        } else {
            run = rl->table_run[code];
            level = rl->table_level[code];
            last = code >= rl->last;
1184
            if (get_bits1(&s->gb))
F
Fabrice Bellard 已提交
1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214
                level = -level;
        }
        i += run;
        if (i >= 64)
            return -1;
	j = scan_table[i];
        block[j] = level;
        i++;
        if (last)
            break;
    }
 not_coded:
    if (s->mb_intra) {
        mpeg4_pred_ac(s, block, n, dc_pred_dir);
        if (s->ac_pred) {
            i = 64; /* XXX: not optimal */
        }
    }
    s->block_last_index[n] = i - 1;
    return 0;
}

/* most is hardcoded. should extend to handle all h263 streams */
int h263_decode_picture_header(MpegEncContext *s)
{
    int format, width, height;

    /* picture header */
    if (get_bits(&s->gb, 22) != 0x20)
        return -1;
1215
    skip_bits(&s->gb, 8); /* picture timestamp */
F
Fabrice Bellard 已提交
1216

1217
    if (get_bits1(&s->gb) != 1)
F
Fabrice Bellard 已提交
1218
        return -1;	/* marker */
1219
    if (get_bits1(&s->gb) != 0)
F
Fabrice Bellard 已提交
1220
        return -1;	/* h263 id */
1221 1222 1223
    skip_bits1(&s->gb);	/* split screen off */
    skip_bits1(&s->gb);	/* camera  off */
    skip_bits1(&s->gb);	/* freeze picture release off */
F
Fabrice Bellard 已提交
1224

1225
    /* Reset GOB number */
1226
    s->gob_number = 0;
1227
        
F
Fabrice Bellard 已提交
1228 1229
    format = get_bits(&s->gb, 3);

1230
    if (format != 7 && format != 6) {
F
Fabrice Bellard 已提交
1231 1232 1233 1234 1235 1236
        s->h263_plus = 0;
        /* H.263v1 */
        width = h263_format[format][0];
        height = h263_format[format][1];
        if (!width)
            return -1;
1237 1238 1239
        
        s->width = width;
        s->height = height;
1240
        s->pict_type = I_TYPE + get_bits1(&s->gb);
F
Fabrice Bellard 已提交
1241

1242
        s->unrestricted_mv = get_bits1(&s->gb); 
F
Fabrice Bellard 已提交
1243 1244
        s->h263_long_vectors = s->unrestricted_mv;

1245
        if (get_bits1(&s->gb) != 0)
F
Fabrice Bellard 已提交
1246
            return -1;	/* SAC: off */
1247 1248 1249 1250
        if (get_bits1(&s->gb) != 0) {
            s->mv_type = MV_TYPE_8X8; /* Advanced prediction mode */
        }   
        
1251
        if (get_bits1(&s->gb) != 0)
F
Fabrice Bellard 已提交
1252 1253 1254
            return -1;	/* not PB frame */

        s->qscale = get_bits(&s->gb, 5);
1255
        skip_bits1(&s->gb);	/* Continuous Presence Multipoint mode: off */
F
Fabrice Bellard 已提交
1256
    } else {
1257 1258
        int ufep;
        
F
Fabrice Bellard 已提交
1259
        /* H.263v2 */
1260 1261
        s->h263_plus = 1;
        ufep = get_bits(&s->gb, 3); /* Update Full Extended PTYPE */
1262
        
1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276
        if (ufep == 1) {
            /* OPPTYPE */       
            format = get_bits(&s->gb, 3);
            skip_bits(&s->gb,1); /* Custom PCF */
            s->umvplus_dec = get_bits(&s->gb, 1); /* Unrestricted Motion Vector */
            skip_bits1(&s->gb); /* Syntax-based Arithmetic Coding (SAC) */
            if (get_bits1(&s->gb) != 0) {
                s->mv_type = MV_TYPE_8X8; /* Advanced prediction mode */
            }
            skip_bits(&s->gb, 8);
            skip_bits(&s->gb, 3); /* Reserved */
        } else if (ufep != 0)
            return -1;
            
1277
        /* MPPTYPE */
F
Fabrice Bellard 已提交
1278 1279 1280 1281
        s->pict_type = get_bits(&s->gb, 3) + 1;
        if (s->pict_type != I_TYPE &&
            s->pict_type != P_TYPE)
            return -1;
1282
        skip_bits(&s->gb, 7);
1283 1284
        
        /* Get the picture dimensions */
1285 1286 1287 1288 1289 1290 1291
        if (ufep) {
            if (format == 6) {
                /* Custom Picture Format (CPFMT) */
                skip_bits(&s->gb, 4); /* aspect ratio */
                width = (get_bits(&s->gb, 9) + 1) * 4;
                skip_bits1(&s->gb);
                height = get_bits(&s->gb, 9) * 4;
1292
#ifdef DEBUG 
1293
                fprintf(stderr,"\nH.263+ Custom picture: %dx%d\n",width,height);
1294
#endif            
1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306
            }
            else {
                width = h263_format[format][0];
                height = h263_format[format][1];
            }
            if ((width == 0) || (height == 0))
                return -1;
            s->width = width;
            s->height = height;
            if (s->umvplus_dec) {
                skip_bits1(&s->gb); /* Unlimited Unrestricted Motion Vectors Indicator (UUI) */
            }
1307 1308
        }
            
F
Fabrice Bellard 已提交
1309 1310 1311
        s->qscale = get_bits(&s->gb, 5);
    }
    /* PEI */
1312 1313
    while (get_bits1(&s->gb) != 0) {
        skip_bits(&s->gb, 8);
F
Fabrice Bellard 已提交
1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341
    }
    s->f_code = 1;
    return 0;
}

/* decode mpeg4 VOP header */
int mpeg4_decode_picture_header(MpegEncContext * s)
{
    int time_incr, startcode, state, v;

 redo:
    /* search next start code */
    align_get_bits(&s->gb);
    state = 0xff;
    for(;;) {
        v = get_bits(&s->gb, 8);
        if (state == 0x000001) {
            state = ((state << 8) | v) & 0xffffff;
            startcode = state;
            break;
        }
        state = ((state << 8) | v) & 0xffffff;
        /* XXX: really detect end of frame */
        if (state == 0)
            return -1;
    }

    if (startcode == 0x120) {
1342
        int time_increment_resolution, width, height, vo_ver_id;
F
Fabrice Bellard 已提交
1343 1344

        /* vol header */
1345 1346
        skip_bits(&s->gb, 1); /* random access */
        skip_bits(&s->gb, 8); /* vo_type */
1347 1348 1349 1350 1351 1352
        if (get_bits1(&s->gb) != 0) { /* is_ol_id */
            vo_ver_id = get_bits(&s->gb, 4); /* vo_ver_id */
            skip_bits(&s->gb, 3); /* vo_priority */
        } else {
            vo_ver_id = 1;
        }
F
Fabrice Bellard 已提交
1353
        
1354
        skip_bits(&s->gb, 4); /* aspect_ratio_info */
1355 1356
        skip_bits1(&s->gb); /* vol control parameter */
        s->shape = get_bits(&s->gb, 2); /* vol shape */
1357
        skip_bits1(&s->gb);   /* marker */
F
Fabrice Bellard 已提交
1358 1359
        
        time_increment_resolution = get_bits(&s->gb, 16);
F
Fabrice Bellard 已提交
1360
        s->time_increment_bits = av_log2(time_increment_resolution - 1) + 1;
1361 1362
        if (s->time_increment_bits < 1)
            s->time_increment_bits = 1;
1363
        skip_bits1(&s->gb);   /* marker */
F
Fabrice Bellard 已提交
1364

1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401
        if (get_bits1(&s->gb) != 0) {   /* fixed_vop_rate  */
            skip_bits(&s->gb, s->time_increment_bits);
        }

        if (s->shape != 2) {
            if (s->shape == 0) {
                skip_bits1(&s->gb);   /* marker */
                width = get_bits(&s->gb, 13);
                skip_bits1(&s->gb);   /* marker */
                height = get_bits(&s->gb, 13);
                skip_bits1(&s->gb);   /* marker */
            }
            
            skip_bits1(&s->gb);   /* interlaced */
            skip_bits1(&s->gb);   /* OBMC */
            if (vo_ver_id == 1) {
                s->vol_sprite_usage = get_bits1(&s->gb); /* vol_sprite_usage */
            } else {
                s->vol_sprite_usage = get_bits(&s->gb, 2); /* vol_sprite_usage */
            }
            if (get_bits1(&s->gb) == 1) {   /* not_8_bit */
                s->quant_precision = get_bits(&s->gb, 4); /* quant_precision */
                skip_bits(&s->gb, 4); /* bits_per_pixel */
            } else {
                s->quant_precision = 5;
            }
            
            skip_bits1(&s->gb);   /* vol_quant_type */
            skip_bits1(&s->gb);   /* vol_quarter_pixel */
            skip_bits1(&s->gb);   /* complexity_estimation_disabled */
            skip_bits1(&s->gb);   /* resync_marker_disabled */
            skip_bits1(&s->gb);   /* data_partioning_enabled */
            if (get_bits1(&s->gb) != 0) { /* scalability */
                printf("bad scalability!!!\n");
                return -1;
            }
        }
F
Fabrice Bellard 已提交
1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413
        goto redo;
    } else if (startcode != 0x1b6) {
        goto redo;
    }

    s->pict_type = get_bits(&s->gb, 2) + 1;	/* pict type: I = 0 , P = 1 */
    if (s->pict_type != I_TYPE &&
        s->pict_type != P_TYPE)
        return -1;
    
    /* XXX: parse time base */
    time_incr = 0;
1414
    while (get_bits1(&s->gb) != 0) 
F
Fabrice Bellard 已提交
1415 1416
        time_incr++;

1417 1418 1419
    skip_bits1(&s->gb);   	/* marker */
    skip_bits(&s->gb, s->time_increment_bits);
    skip_bits1(&s->gb);   	/* marker */
F
Fabrice Bellard 已提交
1420
    /* vop coded */
1421
    if (get_bits1(&s->gb) != 1)
1422
        goto redo;
F
Fabrice Bellard 已提交
1423
    
1424
    if (s->shape != 2 && s->pict_type == P_TYPE) {
F
Fabrice Bellard 已提交
1425
        /* rounding type for motion estimation */
1426
	s->no_rounding = get_bits1(&s->gb);
1427 1428
    } else {
	s->no_rounding = 0;
F
Fabrice Bellard 已提交
1429
    }
1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465
    
     if (s->shape != 0) {
         if (s->vol_sprite_usage != 1 || s->pict_type != I_TYPE) {
             int width, height, hor_spat_ref, ver_spat_ref;
 
             width = get_bits(&s->gb, 13);
             skip_bits1(&s->gb);   /* marker */
             height = get_bits(&s->gb, 13);
             skip_bits1(&s->gb);   /* marker */
             hor_spat_ref = get_bits(&s->gb, 13); /* hor_spat_ref */
             skip_bits1(&s->gb);   /* marker */
             ver_spat_ref = get_bits(&s->gb, 13); /* ver_spat_ref */
         }
         skip_bits1(&s->gb); /* change_CR_disable */
 
         if (get_bits1(&s->gb) != 0) {
             skip_bits(&s->gb, 8); /* constant_alpha_value */
         }
     }

     if (s->shape != 2) {
         skip_bits(&s->gb, 3); /* intra dc VLC threshold */
  
         /* note: we do not use quant_precision to avoid problem if no
            MPEG4 vol header as it is found on some old opendivx
            movies */
         s->qscale = get_bits(&s->gb, 5);
  
         if (s->pict_type != I_TYPE) {
             s->f_code = get_bits(&s->gb, 3);	/* fcode_for */
         }
         if (s->shape && (s->pict_type != I_TYPE)) {
             skip_bits1(&s->gb); // vop shape coding type
         }
     }
     return 0;
F
Fabrice Bellard 已提交
1466 1467 1468 1469 1470 1471 1472 1473 1474 1475
}

/* don't understand why they choose a different header ! */
int intel_h263_decode_picture_header(MpegEncContext *s)
{
    int format;

    /* picture header */
    if (get_bits(&s->gb, 22) != 0x20)
        return -1;
1476
    skip_bits(&s->gb, 8); /* picture timestamp */
F
Fabrice Bellard 已提交
1477

1478
    if (get_bits1(&s->gb) != 1)
F
Fabrice Bellard 已提交
1479
        return -1;	/* marker */
1480
    if (get_bits1(&s->gb) != 0)
F
Fabrice Bellard 已提交
1481
        return -1;	/* h263 id */
1482 1483 1484
    skip_bits1(&s->gb);	/* split screen off */
    skip_bits1(&s->gb);	/* camera  off */
    skip_bits1(&s->gb);	/* freeze picture release off */
F
Fabrice Bellard 已提交
1485 1486 1487 1488 1489 1490 1491

    format = get_bits(&s->gb, 3);
    if (format != 7)
        return -1;

    s->h263_plus = 0;

1492
    s->pict_type = I_TYPE + get_bits1(&s->gb);
F
Fabrice Bellard 已提交
1493
    
1494
    s->unrestricted_mv = get_bits1(&s->gb); 
F
Fabrice Bellard 已提交
1495 1496
    s->h263_long_vectors = s->unrestricted_mv;

1497
    if (get_bits1(&s->gb) != 0)
F
Fabrice Bellard 已提交
1498
        return -1;	/* SAC: off */
1499
    if (get_bits1(&s->gb) != 0)
F
Fabrice Bellard 已提交
1500
        return -1;	/* advanced prediction mode: off */
1501
    if (get_bits1(&s->gb) != 0)
F
Fabrice Bellard 已提交
1502 1503 1504
        return -1;	/* not PB frame */

    /* skip unknown header garbage */
1505
    skip_bits(&s->gb, 41);
F
Fabrice Bellard 已提交
1506 1507

    s->qscale = get_bits(&s->gb, 5);
1508
    skip_bits1(&s->gb);	/* Continuous Presence Multipoint mode: off */
F
Fabrice Bellard 已提交
1509 1510

    /* PEI */
1511 1512
    while (get_bits1(&s->gb) != 0) {
        skip_bits(&s->gb, 8);
F
Fabrice Bellard 已提交
1513 1514 1515 1516
    }
    s->f_code = 1;
    return 0;
}
1517