comparison h263.c @ 355:ac6fc4c7aecb libavcodec

- H263 PAR support by Alex Beregszaszi. - Typo fix in mpeg4data.
author pulento
date Fri, 03 May 2002 18:11:23 +0000
parents 6ebbecc10063
children 2066dc543be4
comparison
equal deleted inserted replaced
354:167aa21aa250 355:ac6fc4c7aecb
18 * along with this program; if not, write to the Free Software 18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 * 20 *
21 * ac prediction encoding & b-frame support by Michael Niedermayer <michaelni@gmx.at> 21 * ac prediction encoding & b-frame support by Michael Niedermayer <michaelni@gmx.at>
22 */ 22 */
23
24 //#define DEBUG
23 #include "common.h" 25 #include "common.h"
24 #include "dsputil.h" 26 #include "dsputil.h"
25 #include "avcodec.h" 27 #include "avcodec.h"
26 #include "mpegvideo.h" 28 #include "mpegvideo.h"
27 #include "h263data.h" 29 #include "h263data.h"
64 int h263_get_picture_format(int width, int height) 66 int h263_get_picture_format(int width, int height)
65 { 67 {
66 int format; 68 int format;
67 69
68 if (width == 128 && height == 96) 70 if (width == 128 && height == 96)
69 format = 1; 71 format = 1;
70 else if (width == 176 && height == 144) 72 else if (width == 176 && height == 144)
71 format = 2; 73 format = 2;
72 else if (width == 352 && height == 288) 74 else if (width == 352 && height == 288)
73 format = 3; 75 format = 3;
74 else if (width == 704 && height == 576) 76 else if (width == 704 && height == 576)
75 format = 4; 77 format = 4;
76 else if (width == 1408 && height == 1152) 78 else if (width == 1408 && height == 1152)
77 format = 5; 79 format = 5;
78 else 80 else
79 format = 7; 81 format = 7;
80 return format; 82 return format;
81 } 83 }
82 84
155 put_bits(&s->pb, 1, 0); /* Continuous Presence Multipoint mode: off */ 157 put_bits(&s->pb, 1, 0); /* Continuous Presence Multipoint mode: off */
156 158
157 if (format == 7) { 159 if (format == 7) {
158 /* Custom Picture Format (CPFMT) */ 160 /* Custom Picture Format (CPFMT) */
159 161
162 if (s->aspect_ratio_info)
163 put_bits(&s->pb,4,s->aspect_ratio_info);
164 else
160 put_bits(&s->pb,4,2); /* Aspect ratio: CIF 12:11 (4:3) picture */ 165 put_bits(&s->pb,4,2); /* Aspect ratio: CIF 12:11 (4:3) picture */
161 put_bits(&s->pb,9,(s->width >> 2) - 1); 166 put_bits(&s->pb,9,(s->width >> 2) - 1);
162 put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */ 167 put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */
163 put_bits(&s->pb,9,(s->height >> 2)); 168 put_bits(&s->pb,9,(s->height >> 2));
164 } 169 }
2378 /* most is hardcoded. should extend to handle all h263 streams */ 2383 /* most is hardcoded. should extend to handle all h263 streams */
2379 int h263_decode_picture_header(MpegEncContext *s) 2384 int h263_decode_picture_header(MpegEncContext *s)
2380 { 2385 {
2381 int format, width, height; 2386 int format, width, height;
2382 2387
2383 /* picture header */ 2388 /* picture start code */
2384 if (get_bits(&s->gb, 22) != 0x20) 2389 if (get_bits(&s->gb, 22) != 0x20) {
2390 fprintf(stderr, "Bad picture start code\n");
2385 return -1; 2391 return -1;
2392 }
2393 /* temporal reference */
2386 s->picture_number = get_bits(&s->gb, 8); /* picture timestamp */ 2394 s->picture_number = get_bits(&s->gb, 8); /* picture timestamp */
2387 2395
2388 if (get_bits1(&s->gb) != 1) 2396 /* PTYPE starts here */
2389 return -1; /* marker */ 2397 if (get_bits1(&s->gb) != 1) {
2390 if (get_bits1(&s->gb) != 0) 2398 /* marker */
2399 fprintf(stderr, "Bad marker\n");
2400 return -1;
2401 }
2402 if (get_bits1(&s->gb) != 0) {
2403 fprintf(stderr, "Bad H263 id\n");
2391 return -1; /* h263 id */ 2404 return -1; /* h263 id */
2405 }
2392 skip_bits1(&s->gb); /* split screen off */ 2406 skip_bits1(&s->gb); /* split screen off */
2393 skip_bits1(&s->gb); /* camera off */ 2407 skip_bits1(&s->gb); /* camera off */
2394 skip_bits1(&s->gb); /* freeze picture release off */ 2408 skip_bits1(&s->gb); /* freeze picture release off */
2395 2409
2396 /* Reset GOB number */ 2410 /* Reset GOB number */
2397 s->gob_number = 0; 2411 s->gob_number = 0;
2398 2412
2399 format = get_bits(&s->gb, 3); 2413 format = get_bits(&s->gb, 3);
2414 /*
2415 0 forbidden
2416 1 sub-QCIF
2417 10 QCIF
2418 7 extended PTYPE (PLUSPTYPE)
2419 */
2400 2420
2401 if (format != 7 && format != 6) { 2421 if (format != 7 && format != 6) {
2402 s->h263_plus = 0; 2422 s->h263_plus = 0;
2403 /* H.263v1 */ 2423 /* H.263v1 */
2404 width = h263_format[format][0]; 2424 width = h263_format[format][0];
2411 s->pict_type = I_TYPE + get_bits1(&s->gb); 2431 s->pict_type = I_TYPE + get_bits1(&s->gb);
2412 2432
2413 s->unrestricted_mv = get_bits1(&s->gb); 2433 s->unrestricted_mv = get_bits1(&s->gb);
2414 s->h263_long_vectors = s->unrestricted_mv; 2434 s->h263_long_vectors = s->unrestricted_mv;
2415 2435
2416 if (get_bits1(&s->gb) != 0) 2436 if (get_bits1(&s->gb) != 0) {
2437 fprintf(stderr, "H263 SAC not supported\n");
2417 return -1; /* SAC: off */ 2438 return -1; /* SAC: off */
2439 }
2418 if (get_bits1(&s->gb) != 0) { 2440 if (get_bits1(&s->gb) != 0) {
2419 s->mv_type = MV_TYPE_8X8; /* Advanced prediction mode */ 2441 s->mv_type = MV_TYPE_8X8; /* Advanced prediction mode */
2420 } 2442 }
2421 2443
2422 if (get_bits1(&s->gb) != 0) 2444 if (get_bits1(&s->gb) != 0) {
2445 fprintf(stderr, "H263 PB frame not supported\n");
2423 return -1; /* not PB frame */ 2446 return -1; /* not PB frame */
2424 2447 }
2425 s->qscale = get_bits(&s->gb, 5); 2448 s->qscale = get_bits(&s->gb, 5);
2426 skip_bits1(&s->gb); /* Continuous Presence Multipoint mode: off */ 2449 skip_bits1(&s->gb); /* Continuous Presence Multipoint mode: off */
2427 } else { 2450 } else {
2428 int ufep; 2451 int ufep;
2429 2452
2430 /* H.263v2 */ 2453 /* H.263v2 */
2431 s->h263_plus = 1; 2454 s->h263_plus = 1;
2432 ufep = get_bits(&s->gb, 3); /* Update Full Extended PTYPE */ 2455 ufep = get_bits(&s->gb, 3); /* Update Full Extended PTYPE */
2433 2456
2457 /* ufep other than 0 and 1 are reserved */
2434 if (ufep == 1) { 2458 if (ufep == 1) {
2435 /* OPPTYPE */ 2459 /* OPPTYPE */
2436 format = get_bits(&s->gb, 3); 2460 format = get_bits(&s->gb, 3);
2461 dprintf("ufep=1, format: %d\n", format);
2437 skip_bits(&s->gb,1); /* Custom PCF */ 2462 skip_bits(&s->gb,1); /* Custom PCF */
2438 s->umvplus_dec = get_bits(&s->gb, 1); /* Unrestricted Motion Vector */ 2463 s->umvplus_dec = get_bits(&s->gb, 1); /* Unrestricted Motion Vector */
2439 skip_bits1(&s->gb); /* Syntax-based Arithmetic Coding (SAC) */ 2464 skip_bits1(&s->gb); /* Syntax-based Arithmetic Coding (SAC) */
2440 if (get_bits1(&s->gb) != 0) { 2465 if (get_bits1(&s->gb) != 0) {
2441 s->mv_type = MV_TYPE_8X8; /* Advanced prediction mode */ 2466 s->mv_type = MV_TYPE_8X8; /* Advanced prediction mode */
2442 } 2467 }
2443 if (get_bits1(&s->gb) != 0) { /* Advanced Intra Coding (AIC) */ 2468 if (get_bits1(&s->gb) != 0) { /* Advanced Intra Coding (AIC) */
2444 s->h263_aic = 1; 2469 s->h263_aic = 1;
2445 } 2470 }
2471
2446 skip_bits(&s->gb, 7); 2472 skip_bits(&s->gb, 7);
2473 /* these are the 7 bits: (in order of appearence */
2474 /* Deblocking Filter */
2475 /* Slice Structured */
2476 /* Reference Picture Selection */
2477 /* Independent Segment Decoding */
2478 /* Alternative Inter VLC */
2479 /* Modified Quantization */
2480 /* Prevent start code emulation */
2481
2447 skip_bits(&s->gb, 3); /* Reserved */ 2482 skip_bits(&s->gb, 3); /* Reserved */
2448 } else if (ufep != 0) 2483 } else if (ufep != 0) {
2484 fprintf(stderr, "Bad UFEP type (%d)\n", ufep);
2449 return -1; 2485 return -1;
2486 }
2450 2487
2451 /* MPPTYPE */ 2488 /* MPPTYPE */
2452 s->pict_type = get_bits(&s->gb, 3) + 1; 2489 s->pict_type = get_bits(&s->gb, 3) + I_TYPE;
2490 dprintf("pict_type: %d\n", s->pict_type);
2453 if (s->pict_type != I_TYPE && 2491 if (s->pict_type != I_TYPE &&
2454 s->pict_type != P_TYPE) 2492 s->pict_type != P_TYPE)
2455 return -1; 2493 return -1;
2456 skip_bits(&s->gb, 2); 2494 skip_bits(&s->gb, 2);
2457 s->no_rounding = get_bits1(&s->gb); 2495 s->no_rounding = get_bits1(&s->gb);
2458 //fprintf(stderr, "\nRTYPE: %d", s->no_rounding); 2496 dprintf("RTYPE: %d\n", s->no_rounding);
2459 skip_bits(&s->gb, 4); 2497 skip_bits(&s->gb, 4);
2460 2498
2461 /* Get the picture dimensions */ 2499 /* Get the picture dimensions */
2462 if (ufep) { 2500 if (ufep) {
2463 if (format == 6) { 2501 if (format == 6) {
2464 /* Custom Picture Format (CPFMT) */ 2502 /* Custom Picture Format (CPFMT) */
2465 skip_bits(&s->gb, 4); /* aspect ratio */ 2503 s->aspect_ratio_info = get_bits(&s->gb, 4);
2504 dprintf("aspect: %d\n", s->aspect_ratio_info);
2505 /* aspect ratios:
2506 0 - forbidden
2507 1 - 1:1
2508 2 - 12:11 (CIF 4:3)
2509 3 - 10:11 (525-type 4:3)
2510 4 - 16:11 (CIF 16:9)
2511 5 - 40:33 (525-type 16:9)
2512 6-14 - reserved
2513 */
2466 width = (get_bits(&s->gb, 9) + 1) * 4; 2514 width = (get_bits(&s->gb, 9) + 1) * 4;
2467 skip_bits1(&s->gb); 2515 skip_bits1(&s->gb);
2468 height = get_bits(&s->gb, 9) * 4; 2516 height = get_bits(&s->gb, 9) * 4;
2469 #ifdef DEBUG 2517 dprintf("\nH.263+ Custom picture: %dx%d\n",width,height);
2470 fprintf(stderr,"\nH.263+ Custom picture: %dx%d\n",width,height); 2518 if (s->aspect_ratio_info == EXTENDED_PAR) {
2471 #endif 2519 /* aspected dimensions */
2472 } 2520 skip_bits(&s->gb, 8); /* width */
2473 else { 2521 skip_bits(&s->gb, 8); /* height */
2522 }
2523 } else {
2474 width = h263_format[format][0]; 2524 width = h263_format[format][0];
2475 height = h263_format[format][1]; 2525 height = h263_format[format][1];
2476 } 2526 }
2477 if ((width == 0) || (height == 0)) 2527 if ((width == 0) || (height == 0))
2478 return -1; 2528 return -1;
2725 } else { 2775 } else {
2726 vo_ver_id = 1; 2776 vo_ver_id = 1;
2727 } 2777 }
2728 //printf("vo type:%d\n",s->vo_type); 2778 //printf("vo type:%d\n",s->vo_type);
2729 s->aspect_ratio_info= get_bits(&s->gb, 4); 2779 s->aspect_ratio_info= get_bits(&s->gb, 4);
2730 if(s->aspect_ratio_info == EXTENDET_PAR){ 2780 if(s->aspect_ratio_info == EXTENDED_PAR){
2731 skip_bits(&s->gb, 8); //par_width 2781 skip_bits(&s->gb, 8); //par_width
2732 skip_bits(&s->gb, 8); // par_height 2782 skip_bits(&s->gb, 8); // par_height
2733 } 2783 }
2734 2784
2735 if ((vol_control=get_bits1(&s->gb))) { /* vol control parameter */ 2785 if ((vol_control=get_bits1(&s->gb))) { /* vol control parameter */
2938 goto redo; 2988 goto redo;
2939 } else if (startcode != 0x1b6) { //VOP 2989 } else if (startcode != 0x1b6) { //VOP
2940 goto redo; 2990 goto redo;
2941 } 2991 }
2942 2992
2943 s->pict_type = get_bits(&s->gb, 2) + 1; /* pict type: I = 0 , P = 1 */ 2993 s->pict_type = get_bits(&s->gb, 2) + I_TYPE; /* pict type: I = 0 , P = 1 */
2944 // printf("pic: %d, qpel:%d\n", s->pict_type, s->quarter_sample); 2994 // printf("pic: %d, qpel:%d\n", s->pict_type, s->quarter_sample);
2945 time_incr=0; 2995 time_incr=0;
2946 while (get_bits1(&s->gb) != 0) 2996 while (get_bits1(&s->gb) != 0)
2947 time_incr++; 2997 time_incr++;
2948 2998
3066 int intel_h263_decode_picture_header(MpegEncContext *s) 3116 int intel_h263_decode_picture_header(MpegEncContext *s)
3067 { 3117 {
3068 int format; 3118 int format;
3069 3119
3070 /* picture header */ 3120 /* picture header */
3071 if (get_bits(&s->gb, 22) != 0x20) 3121 if (get_bits(&s->gb, 22) != 0x20) {
3122 fprintf(stderr, "Bad picture start code\n");
3072 return -1; 3123 return -1;
3073 skip_bits(&s->gb, 8); /* picture timestamp */ 3124 }
3074 3125 s->picture_number = get_bits(&s->gb, 8); /* picture timestamp */
3075 if (get_bits1(&s->gb) != 1) 3126
3127 if (get_bits1(&s->gb) != 1) {
3128 fprintf(stderr, "Bad marker\n");
3076 return -1; /* marker */ 3129 return -1; /* marker */
3077 if (get_bits1(&s->gb) != 0) 3130 }
3131 if (get_bits1(&s->gb) != 0) {
3132 fprintf(stderr, "Bad H263 id\n");
3078 return -1; /* h263 id */ 3133 return -1; /* h263 id */
3134 }
3079 skip_bits1(&s->gb); /* split screen off */ 3135 skip_bits1(&s->gb); /* split screen off */
3080 skip_bits1(&s->gb); /* camera off */ 3136 skip_bits1(&s->gb); /* camera off */
3081 skip_bits1(&s->gb); /* freeze picture release off */ 3137 skip_bits1(&s->gb); /* freeze picture release off */
3082 3138
3083 format = get_bits(&s->gb, 3); 3139 format = get_bits(&s->gb, 3);
3084 if (format != 7) 3140 if (format != 7) {
3141 fprintf(stderr, "Intel H263 free format not supported\n");
3085 return -1; 3142 return -1;
3086 3143 }
3087 s->h263_plus = 0; 3144 s->h263_plus = 0;
3088 3145
3089 s->pict_type = I_TYPE + get_bits1(&s->gb); 3146 s->pict_type = I_TYPE + get_bits1(&s->gb);
3090 3147
3091 s->unrestricted_mv = get_bits1(&s->gb); 3148 s->unrestricted_mv = get_bits1(&s->gb);
3092 s->h263_long_vectors = s->unrestricted_mv; 3149 s->h263_long_vectors = s->unrestricted_mv;
3093 3150
3094 if (get_bits1(&s->gb) != 0) 3151 if (get_bits1(&s->gb) != 0) {
3152 fprintf(stderr, "SAC not supported\n");
3095 return -1; /* SAC: off */ 3153 return -1; /* SAC: off */
3096 if (get_bits1(&s->gb) != 0) 3154 }
3155 if (get_bits1(&s->gb) != 0) {
3156 fprintf(stderr, "Advanced Prediction Mode not supported\n");
3097 return -1; /* advanced prediction mode: off */ 3157 return -1; /* advanced prediction mode: off */
3098 if (get_bits1(&s->gb) != 0) 3158 }
3099 return -1; /* not PB frame */ 3159 if (get_bits1(&s->gb) != 0) {
3160 fprintf(stderr, "PB frame mode no supported\n");
3161 return -1; /* PB frame mode */
3162 }
3100 3163
3101 /* skip unknown header garbage */ 3164 /* skip unknown header garbage */
3102 skip_bits(&s->gb, 41); 3165 skip_bits(&s->gb, 41);
3103 3166
3104 s->qscale = get_bits(&s->gb, 5); 3167 s->qscale = get_bits(&s->gb, 5);