comparison h263.c @ 324:9c6f056f0e41 libavcodec

fixed mpeg4 time stuff on encoding mpeg4 b-frame enoding support removed old, out-commented ratecontrol reuse motion compensation code between encoding & decoding prefix newly added global functions with ff_ to reduce namespace polution b-frame ME (unfinished, but working) added some comments to mpegvideo.h do MC on encoding only once if possible bugs? ;)
author michaelni
date Wed, 17 Apr 2002 04:32:12 +0000
parents ce35fd27bbb0
children 15efd80cf51e
comparison
equal deleted inserted replaced
323:68cc7650c645 324:9c6f056f0e41
34 #define MAX(a,b) ((a) > (b) ? (a) : (b)) 34 #define MAX(a,b) ((a) > (b) ? (a) : (b))
35 #define MIN(a,b) ((a) < (b) ? (a) : (b)) 35 #define MIN(a,b) ((a) < (b) ? (a) : (b))
36 36
37 static void h263_encode_block(MpegEncContext * s, DCTELEM * block, 37 static void h263_encode_block(MpegEncContext * s, DCTELEM * block,
38 int n); 38 int n);
39 static void h263_encode_motion(MpegEncContext * s, int val); 39 static void h263_encode_motion(MpegEncContext * s, int val, int fcode);
40 static void h263p_encode_umotion(MpegEncContext * s, int val); 40 static void h263p_encode_umotion(MpegEncContext * s, int val);
41 static void mpeg4_encode_block(MpegEncContext * s, DCTELEM * block, 41 static void mpeg4_encode_block(MpegEncContext * s, DCTELEM * block,
42 int n, int dc, UINT8 *scan_table); 42 int n, int dc, UINT8 *scan_table);
43 static int h263_decode_motion(MpegEncContext * s, int pred, int fcode); 43 static int h263_decode_motion(MpegEncContext * s, int pred, int fcode);
44 static int h263p_decode_umotion(MpegEncContext * s, int pred); 44 static int h263p_decode_umotion(MpegEncContext * s, int pred);
250 250
251 void mpeg4_encode_mb(MpegEncContext * s, 251 void mpeg4_encode_mb(MpegEncContext * s,
252 DCTELEM block[6][64], 252 DCTELEM block[6][64],
253 int motion_x, int motion_y) 253 int motion_x, int motion_y)
254 { 254 {
255 int cbpc, cbpy, i, cbp, pred_x, pred_y; 255 int cbpc, cbpy, i, pred_x, pred_y;
256 int bits; 256 int bits;
257 257
258 // printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y); 258 // printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y);
259 if (!s->mb_intra) { 259 if (!s->mb_intra) {
260 /* compute cbp */ 260 /* compute cbp */
261 cbp = 0; 261 int cbp = 0;
262 for (i = 0; i < 6; i++) { 262 for (i = 0; i < 6; i++) {
263 if (s->block_last_index[i] >= 0) 263 if (s->block_last_index[i] >= 0)
264 cbp |= 1 << (5 - i); 264 cbp |= 1 << (5 - i);
265 } 265 }
266 if ((cbp | motion_x | motion_y) == 0 && s->mv_type==MV_TYPE_16X16) { 266
267 /* skip macroblock */ 267 if(s->pict_type==B_TYPE){
268 put_bits(&s->pb, 1, 1); 268 static const int mb_type_table[8]= {-1, 2, 3, 1,-1,-1,-1, 0}; /* convert from mv_dir to type */
269 s->misc_bits++; 269 int mb_type= mb_type_table[s->mv_dir];
270 s->last_bits++; 270
271 s->skip_count++; 271 if(s->mb_x==0){
272 return; 272 s->last_mv[0][0][0]=
273 } 273 s->last_mv[0][0][1]=
274 put_bits(&s->pb, 1, 0); /* mb coded */ 274 s->last_mv[1][0][0]=
275 if(s->mv_type==MV_TYPE_16X16){ 275 s->last_mv[1][0][1]= 0;
276 cbpc = cbp & 3; 276 }
277 put_bits(&s->pb, 277
278 inter_MCBPC_bits[cbpc], 278 /* nothing to do if this MB was skiped in the next P Frame */
279 inter_MCBPC_code[cbpc]); 279 if(s->mbskip_table[s->mb_y * s->mb_width + s->mb_x]){
280 cbpy = cbp >> 2; 280 s->skip_count++;
281 cbpy ^= 0xf; 281 s->mv[0][0][0]=
282 put_bits(&s->pb, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]); 282 s->mv[0][0][1]=
283 283 s->mv[1][0][0]=
284 s->mv[1][0][1]= 0;
285 // s->mv_dir= MV_DIR_FORWARD; //doesnt matter
286 return;
287 }
288
289 if ((cbp | motion_x | motion_y | mb_type) ==0) {
290 /* direct MB with MV={0,0} */
291 put_bits(&s->pb, 1, 1); /* mb not coded modb1=1 */
292 s->misc_bits++;
293 s->last_bits++;
294 s->skip_count++;
295 return;
296 }
297 put_bits(&s->pb, 1, 0); /* mb coded modb1=0 */
298 put_bits(&s->pb, 1, cbp ? 0 : 1); /* modb2 */ //FIXME merge
299 put_bits(&s->pb, mb_type+1, 1); // this table is so simple that we dont need it :)
300 if(cbp) put_bits(&s->pb, 6, cbp);
301
302 if(cbp && mb_type)
303 put_bits(&s->pb, 1, 0); /* no q-scale change */
304
284 bits= get_bit_count(&s->pb); 305 bits= get_bit_count(&s->pb);
285 s->misc_bits+= bits - s->last_bits; 306 s->misc_bits+= bits - s->last_bits;
286 s->last_bits=bits; 307 s->last_bits=bits;
287 308
288 /* motion vectors: 16x16 mode */ 309 switch(mb_type)
289 h263_pred_motion(s, 0, &pred_x, &pred_y); 310 {
290 311 case 0: /* direct */
291 h263_encode_motion(s, motion_x - pred_x); 312 h263_encode_motion(s, motion_x, 1);
292 h263_encode_motion(s, motion_y - pred_y); 313 h263_encode_motion(s, motion_y, 1);
293 }else{ 314 break;
294 cbpc = (cbp & 3)+16; 315 case 1: /* bidir */
295 put_bits(&s->pb, 316 h263_encode_motion(s, s->mv[0][0][0] - s->last_mv[0][0][0], s->f_code);
296 inter_MCBPC_bits[cbpc], 317 h263_encode_motion(s, s->mv[0][0][1] - s->last_mv[0][0][1], s->f_code);
297 inter_MCBPC_code[cbpc]); 318 h263_encode_motion(s, s->mv[1][0][0] - s->last_mv[1][0][0], s->b_code);
298 cbpy = cbp >> 2; 319 h263_encode_motion(s, s->mv[1][0][1] - s->last_mv[1][0][1], s->b_code);
299 cbpy ^= 0xf; 320 s->last_mv[0][0][0]= s->mv[0][0][0];
300 put_bits(&s->pb, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]); 321 s->last_mv[0][0][1]= s->mv[0][0][1];
301 322 s->last_mv[1][0][0]= s->mv[1][0][0];
323 s->last_mv[1][0][1]= s->mv[1][0][1];
324 break;
325 case 2: /* backward */
326 h263_encode_motion(s, motion_x - s->last_mv[1][0][0], s->b_code);
327 h263_encode_motion(s, motion_y - s->last_mv[1][0][1], s->b_code);
328 s->last_mv[1][0][0]= motion_x;
329 s->last_mv[1][0][1]= motion_y;
330 break;
331 case 3: /* forward */
332 h263_encode_motion(s, motion_x - s->last_mv[0][0][0], s->f_code);
333 h263_encode_motion(s, motion_y - s->last_mv[0][0][1], s->f_code);
334 s->last_mv[0][0][0]= motion_x;
335 s->last_mv[0][0][1]= motion_y;
336 break;
337 default:
338 return;
339 }
302 bits= get_bit_count(&s->pb); 340 bits= get_bit_count(&s->pb);
303 s->misc_bits+= bits - s->last_bits; 341 s->mv_bits+= bits - s->last_bits;
304 s->last_bits=bits; 342 s->last_bits=bits;
305 343
306 for(i=0; i<4; i++){ 344 /* encode each block */
307 /* motion vectors: 8x8 mode*/ 345 for (i = 0; i < 6; i++) {
308 h263_pred_motion(s, i, &pred_x, &pred_y); 346 mpeg4_encode_block(s, block[i], i, 0, zigzag_direct);
309 347 }
310 h263_encode_motion(s, s->motion_val[ s->block_index[i] ][0] - pred_x); 348 bits= get_bit_count(&s->pb);
311 h263_encode_motion(s, s->motion_val[ s->block_index[i] ][1] - pred_y); 349 s->p_tex_bits+= bits - s->last_bits;
312 } 350 s->last_bits=bits;
313 } 351 }else{ /* s->pict_type==B_TYPE */
314 bits= get_bit_count(&s->pb); 352 if ((cbp | motion_x | motion_y) == 0 && s->mv_type==MV_TYPE_16X16) {
315 s->mv_bits+= bits - s->last_bits; 353 /* skip macroblock */
316 s->last_bits=bits; 354 put_bits(&s->pb, 1, 1);
317 355 s->misc_bits++;
318 /* encode each block */ 356 s->last_bits++;
319 for (i = 0; i < 6; i++) { 357 s->skip_count++;
320 mpeg4_encode_block(s, block[i], i, 0, zigzag_direct); 358 s->mb_skiped=1; // we need that for b-frames
321 } 359 return;
322 bits= get_bit_count(&s->pb); 360 }
323 s->p_tex_bits+= bits - s->last_bits; 361 put_bits(&s->pb, 1, 0); /* mb coded */
324 s->last_bits=bits; 362 if(s->mv_type==MV_TYPE_16X16){
325 s->p_count++; 363 cbpc = cbp & 3;
364 put_bits(&s->pb,
365 inter_MCBPC_bits[cbpc],
366 inter_MCBPC_code[cbpc]);
367 cbpy = cbp >> 2;
368 cbpy ^= 0xf;
369 put_bits(&s->pb, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]);
370
371 bits= get_bit_count(&s->pb);
372 s->misc_bits+= bits - s->last_bits;
373 s->last_bits=bits;
374
375 /* motion vectors: 16x16 mode */
376 h263_pred_motion(s, 0, &pred_x, &pred_y);
377
378 h263_encode_motion(s, motion_x - pred_x, s->f_code);
379 h263_encode_motion(s, motion_y - pred_y, s->f_code);
380 }else{
381 cbpc = (cbp & 3)+16;
382 put_bits(&s->pb,
383 inter_MCBPC_bits[cbpc],
384 inter_MCBPC_code[cbpc]);
385 cbpy = cbp >> 2;
386 cbpy ^= 0xf;
387 put_bits(&s->pb, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]);
388
389 bits= get_bit_count(&s->pb);
390 s->misc_bits+= bits - s->last_bits;
391 s->last_bits=bits;
392
393 for(i=0; i<4; i++){
394 /* motion vectors: 8x8 mode*/
395 h263_pred_motion(s, i, &pred_x, &pred_y);
396
397 h263_encode_motion(s, s->motion_val[ s->block_index[i] ][0] - pred_x, s->f_code);
398 h263_encode_motion(s, s->motion_val[ s->block_index[i] ][1] - pred_y, s->f_code);
399 }
400 }
401 bits= get_bit_count(&s->pb);
402 s->mv_bits+= bits - s->last_bits;
403 s->last_bits=bits;
404
405 /* encode each block */
406 for (i = 0; i < 6; i++) {
407 mpeg4_encode_block(s, block[i], i, 0, zigzag_direct);
408 }
409 bits= get_bit_count(&s->pb);
410 s->p_tex_bits+= bits - s->last_bits;
411 s->last_bits=bits;
412 s->p_count++;
413 }
326 } else { 414 } else {
415 int cbp;
327 int dc_diff[6]; //dc values with the dc prediction subtracted 416 int dc_diff[6]; //dc values with the dc prediction subtracted
328 int dir[6]; //prediction direction 417 int dir[6]; //prediction direction
329 int zigzag_last_index[6]; 418 int zigzag_last_index[6];
330 UINT8 *scan_table[6]; 419 UINT8 *scan_table[6];
331 420
425 int motion_x, int motion_y) 514 int motion_x, int motion_y)
426 { 515 {
427 int cbpc, cbpy, i, cbp, pred_x, pred_y; 516 int cbpc, cbpy, i, cbp, pred_x, pred_y;
428 517
429 // printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y); 518 // printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y);
430 if (!s->mb_intra) { 519 if (!s->mb_intra) {
431 /* compute cbp */ 520 /* compute cbp */
432 cbp = 0; 521 cbp = 0;
433 for (i = 0; i < 6; i++) { 522 for (i = 0; i < 6; i++) {
434 if (s->block_last_index[i] >= 0) 523 if (s->block_last_index[i] >= 0)
435 cbp |= 1 << (5 - i); 524 cbp |= 1 << (5 - i);
436 } 525 }
437 if ((cbp | motion_x | motion_y) == 0) { 526 if ((cbp | motion_x | motion_y) == 0) {
438 /* skip macroblock */ 527 /* skip macroblock */
439 put_bits(&s->pb, 1, 1); 528 put_bits(&s->pb, 1, 1);
440 return; 529 return;
441 } 530 }
442 put_bits(&s->pb, 1, 0); /* mb coded */ 531 put_bits(&s->pb, 1, 0); /* mb coded */
443 cbpc = cbp & 3; 532 cbpc = cbp & 3;
444 put_bits(&s->pb, 533 put_bits(&s->pb,
445 inter_MCBPC_bits[cbpc], 534 inter_MCBPC_bits[cbpc],
446 inter_MCBPC_code[cbpc]); 535 inter_MCBPC_code[cbpc]);
447 cbpy = cbp >> 2; 536 cbpy = cbp >> 2;
448 cbpy ^= 0xf; 537 cbpy ^= 0xf;
449 put_bits(&s->pb, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]); 538 put_bits(&s->pb, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]);
450 539
451 /* motion vectors: 16x16 mode only now */ 540 /* motion vectors: 16x16 mode only now */
452 h263_pred_motion(s, 0, &pred_x, &pred_y); 541 h263_pred_motion(s, 0, &pred_x, &pred_y);
453 542
454 if (!s->umvplus) { 543 if (!s->umvplus) {
455 h263_encode_motion(s, motion_x - pred_x); 544 h263_encode_motion(s, motion_x - pred_x, s->f_code);
456 h263_encode_motion(s, motion_y - pred_y); 545 h263_encode_motion(s, motion_y - pred_y, s->f_code);
457 } 546 }
458 else { 547 else {
459 h263p_encode_umotion(s, motion_x - pred_x); 548 h263p_encode_umotion(s, motion_x - pred_x);
460 h263p_encode_umotion(s, motion_y - pred_y); 549 h263p_encode_umotion(s, motion_y - pred_y);
461 if (((motion_x - pred_x) == 1) && ((motion_y - pred_y) == 1)) 550 if (((motion_x - pred_x) == 1) && ((motion_y - pred_y) == 1))
462 /* To prevent Start Code emulation */ 551 /* To prevent Start Code emulation */
463 put_bits(&s->pb,1,1); 552 put_bits(&s->pb,1,1);
464 } 553 }
465 } else { 554 } else {
466 /* compute cbp */ 555 /* compute cbp */
467 cbp = 0; 556 cbp = 0;
468 for (i = 0; i < 6; i++) { 557 for (i = 0; i < 6; i++) {
469 if (s->block_last_index[i] >= 1) 558 if (s->block_last_index[i] >= 1)
601 *py = mid_pred(A[1], B[1], C[1]); 690 *py = mid_pred(A[1], B[1], C[1]);
602 } 691 }
603 return mot_val; 692 return mot_val;
604 } 693 }
605 694
606 static void h263_encode_motion(MpegEncContext * s, int val) 695 static void h263_encode_motion(MpegEncContext * s, int val, int f_code)
607 { 696 {
608 int range, l, m, bit_size, sign, code, bits; 697 int range, l, m, bit_size, sign, code, bits;
609 698
610 if (val == 0) { 699 if (val == 0) {
611 /* zero vector */ 700 /* zero vector */
612 code = 0; 701 code = 0;
613 put_bits(&s->pb, mvtab[code][1], mvtab[code][0]); 702 put_bits(&s->pb, mvtab[code][1], mvtab[code][0]);
614 } else { 703 } else {
615 bit_size = s->f_code - 1; 704 bit_size = f_code - 1;
616 range = 1 << bit_size; 705 range = 1 << bit_size;
617 /* modulo encoding */ 706 /* modulo encoding */
618 l = range * 32; 707 l = range * 32;
619 m = 2 * l; 708 m = 2 * l;
620 if (val < -l) { 709 if (val < -l) {
891 else 980 else
892 put_bits(&s->pb, 4, 1); /* aspect ratio info= sqare pixel */ 981 put_bits(&s->pb, 4, 1); /* aspect ratio info= sqare pixel */
893 put_bits(&s->pb, 1, 0); /* vol control parameters= no */ 982 put_bits(&s->pb, 1, 0); /* vol control parameters= no */
894 put_bits(&s->pb, 2, RECT_SHAPE); /* vol shape= rectangle */ 983 put_bits(&s->pb, 2, RECT_SHAPE); /* vol shape= rectangle */
895 put_bits(&s->pb, 1, 1); /* marker bit */ 984 put_bits(&s->pb, 1, 1); /* marker bit */
896 put_bits(&s->pb, 16, s->time_increment_resolution=30000); 985
986 s->time_increment_resolution= s->frame_rate/ff_gcd(s->frame_rate, FRAME_RATE_BASE);
987 if(s->time_increment_resolution>=256*256) s->time_increment_resolution= 256*128;
988
989 put_bits(&s->pb, 16, s->time_increment_resolution);
897 s->time_increment_bits = av_log2(s->time_increment_resolution - 1) + 1; 990 s->time_increment_bits = av_log2(s->time_increment_resolution - 1) + 1;
898 if (s->time_increment_bits < 1) 991 if (s->time_increment_bits < 1)
899 s->time_increment_bits = 1; 992 s->time_increment_bits = 1;
900 put_bits(&s->pb, 1, 1); /* marker bit */ 993 put_bits(&s->pb, 1, 1); /* marker bit */
901 put_bits(&s->pb, 1, 0); /* fixed vop rate=no */ 994 put_bits(&s->pb, 1, 0); /* fixed vop rate=no */
934 } 1027 }
935 1028
936 /* write mpeg4 VOP header */ 1029 /* write mpeg4 VOP header */
937 void mpeg4_encode_picture_header(MpegEncContext * s, int picture_number) 1030 void mpeg4_encode_picture_header(MpegEncContext * s, int picture_number)
938 { 1031 {
1032 int time_incr;
1033 int time_div, time_mod;
1034
939 if(s->pict_type==I_TYPE) mpeg4_encode_vol_header(s); 1035 if(s->pict_type==I_TYPE) mpeg4_encode_vol_header(s);
940 1036
1037 s->time= s->picture_number*(int64_t)FRAME_RATE_BASE*s->time_increment_resolution/s->frame_rate;
1038 time_div= s->time/s->time_increment_resolution;
1039 time_mod= s->time%s->time_increment_resolution;
1040 //printf("num:%d rate:%d base:%d\n", s->picture_number, s->frame_rate, FRAME_RATE_BASE);
1041
941 if(get_bit_count(&s->pb)!=0) mpeg4_stuffing(&s->pb); 1042 if(get_bit_count(&s->pb)!=0) mpeg4_stuffing(&s->pb);
942 put_bits(&s->pb, 16, 0); /* vop header */ 1043 put_bits(&s->pb, 16, 0); /* vop header */
943 put_bits(&s->pb, 16, 0x1B6); /* vop header */ 1044 put_bits(&s->pb, 16, 0x1B6); /* vop header */
944 put_bits(&s->pb, 2, s->pict_type - 1); /* pict type: I = 0 , P = 1 */ 1045 put_bits(&s->pb, 2, s->pict_type - 1); /* pict type: I = 0 , P = 1 */
945 /* XXX: time base + 1 not always correct */ 1046
946 put_bits(&s->pb, 1, 1); 1047 if(s->pict_type==B_TYPE){
1048 s->bp_time= s->last_non_b_time - s->time;
1049 }else{
1050 s->last_time_base= s->time_base;
1051 s->time_base= time_div;
1052 s->pp_time= s->time - s->last_non_b_time;
1053 s->last_non_b_time= s->time;
1054 }
1055
1056 time_incr= time_div - s->last_time_base;
1057 while(time_incr--)
1058 put_bits(&s->pb, 1, 1);
1059
947 put_bits(&s->pb, 1, 0); 1060 put_bits(&s->pb, 1, 0);
948 1061
949 put_bits(&s->pb, 1, 1); /* marker */ 1062 put_bits(&s->pb, 1, 1); /* marker */
950 put_bits(&s->pb, s->time_increment_bits, 1); /* XXX: correct time increment */ 1063 put_bits(&s->pb, s->time_increment_bits, time_mod); /* time increment */
951 put_bits(&s->pb, 1, 1); /* marker */ 1064 put_bits(&s->pb, 1, 1); /* marker */
952 put_bits(&s->pb, 1, 1); /* vop coded */ 1065 put_bits(&s->pb, 1, 1); /* vop coded */
953 if ( s->pict_type == P_TYPE 1066 if ( s->pict_type == P_TYPE
954 || (s->pict_type == S_TYPE && s->vol_sprite_usage==GMC_SPRITE)) { 1067 || (s->pict_type == S_TYPE && s->vol_sprite_usage==GMC_SPRITE)) {
955 s->no_rounding ^= 1; 1068 s->no_rounding ^= 1;
1359 { 1472 {
1360 int state, v, bits; 1473 int state, v, bits;
1361 int mb_num_bits= av_log2(s->mb_num - 1) + 1; 1474 int mb_num_bits= av_log2(s->mb_num - 1) + 1;
1362 int header_extension=0, mb_num; 1475 int header_extension=0, mb_num;
1363 int c_wrap, c_xy, l_wrap, l_xy; 1476 int c_wrap, c_xy, l_wrap, l_xy;
1477 int time_increment;
1364 //printf("resync at %d %d\n", s->mb_x, s->mb_y); 1478 //printf("resync at %d %d\n", s->mb_x, s->mb_y);
1365 //printf("%X\n", show_bits(&s->gb, 24)); 1479 //printf("%X\n", show_bits(&s->gb, 24));
1366 1480
1367 if( get_bits_count(&s->gb) > s->gb.size*8-32) 1481 if( get_bits_count(&s->gb) > s->gb.size*8-32)
1368 return 0; 1482 return 0;
1413 printf("header extension not really supported\n"); 1527 printf("header extension not really supported\n");
1414 while (get_bits1(&s->gb) != 0) 1528 while (get_bits1(&s->gb) != 0)
1415 time_incr++; 1529 time_incr++;
1416 1530
1417 check_marker(&s->gb, "before time_increment in video packed header"); 1531 check_marker(&s->gb, "before time_increment in video packed header");
1418 s->time_increment= get_bits(&s->gb, s->time_increment_bits); 1532 time_increment= get_bits(&s->gb, s->time_increment_bits);
1419 if(s->pict_type!=B_TYPE){ 1533 if(s->pict_type!=B_TYPE){
1534 s->last_time_base= s->time_base;
1420 s->time_base+= time_incr; 1535 s->time_base+= time_incr;
1421 s->last_non_b_time[1]= s->last_non_b_time[0]; 1536 s->time= s->time_base*s->time_increment_resolution + time_increment;
1422 s->last_non_b_time[0]= s->time_base*s->time_increment_resolution + s->time_increment; 1537 s->pp_time= s->time - s->last_non_b_time;
1538 s->last_non_b_time= s->time;
1423 }else{ 1539 }else{
1424 s->time= (s->last_non_b_time[1]/s->time_increment_resolution + time_incr)*s->time_increment_resolution; 1540 s->time= (s->last_time_base + time_incr)*s->time_increment_resolution + time_increment;
1425 s->time+= s->time_increment; 1541 s->bp_time= s->last_non_b_time - s->time;
1426 } 1542 }
1427 check_marker(&s->gb, "before vop_coding_type in video packed header"); 1543 check_marker(&s->gb, "before vop_coding_type in video packed header");
1428 1544
1429 skip_bits(&s->gb, 2); /* vop coding type */ 1545 skip_bits(&s->gb, 2); /* vop coding type */
1430 //FIXME not rect stuff here 1546 //FIXME not rect stuff here
1640 } 1756 }
1641 } else if(s->pict_type==B_TYPE) { 1757 } else if(s->pict_type==B_TYPE) {
1642 int modb1; // first bit of modb 1758 int modb1; // first bit of modb
1643 int modb2; // second bit of modb 1759 int modb2; // second bit of modb
1644 int mb_type; 1760 int mb_type;
1645 int time_pp; 1761 uint16_t time_pp;
1646 int time_pb; 1762 uint16_t time_pb;
1647 int xy; 1763 int xy;
1648 1764
1649 s->mb_intra = 0; //B-frames never contain intra blocks 1765 s->mb_intra = 0; //B-frames never contain intra blocks
1650 s->mcsel=0; // ... true gmc blocks 1766 s->mcsel=0; // ... true gmc blocks
1651 1767
1671 s->mv[1][0][0] = 0; 1787 s->mv[1][0][0] = 0;
1672 s->mv[1][0][1] = 0; 1788 s->mv[1][0][1] = 0;
1673 //FIXME is this correct? 1789 //FIXME is this correct?
1674 /* s->last_mv[0][0][0]= 1790 /* s->last_mv[0][0][0]=
1675 s->last_mv[0][0][1]=0;*/ 1791 s->last_mv[0][0][1]=0;*/
1676 s->mb_skiped = 1;
1677 return 0; 1792 return 0;
1678 } 1793 }
1679 1794
1680 modb1= get_bits1(&s->gb); 1795 modb1= get_bits1(&s->gb);
1681 if(modb1==0){ 1796 if(modb1==0){
1699 } 1814 }
1700 s->mv_type = MV_TYPE_16X16; // we'll switch to 8x8 only if the last P frame had 8x8 for this MB and mb_type=0 here 1815 s->mv_type = MV_TYPE_16X16; // we'll switch to 8x8 only if the last P frame had 8x8 for this MB and mb_type=0 here
1701 mx=my=0; //for case 4, we could put this to the mb_type=4 but than gcc compains about uninitalized mx/my 1816 mx=my=0; //for case 4, we could put this to the mb_type=4 but than gcc compains about uninitalized mx/my
1702 switch(mb_type) 1817 switch(mb_type)
1703 { 1818 {
1704 case 0: 1819 case 0: /* direct */
1705 mx = h263_decode_motion(s, 0, 1); 1820 mx = h263_decode_motion(s, 0, 1);
1706 my = h263_decode_motion(s, 0, 1); 1821 my = h263_decode_motion(s, 0, 1);
1707 case 4: 1822 case 4: /* direct with mx=my=0 */
1708 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT; 1823 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
1709 xy= s->block_index[0]; 1824 xy= s->block_index[0];
1710 time_pp= s->last_non_b_time[0] - s->last_non_b_time[1]; 1825 time_pp= s->pp_time;
1711 time_pb= s->time - s->last_non_b_time[1]; 1826 time_pb= time_pp - s->bp_time;
1712 //if(time_pp>3000 )printf("%d %d ", time_pp, time_pb); 1827 //if(time_pp>3000 )printf("%d %d ", time_pp, time_pb);
1713 //FIXME 4MV 1828 //FIXME 4MV
1714 //FIXME avoid divides 1829 //FIXME avoid divides
1715 s->mv[0][0][0] = s->motion_val[xy][0]*time_pb/time_pp + mx; 1830 s->mv[0][0][0] = s->motion_val[xy][0]*time_pb/time_pp + mx;
1716 s->mv[0][0][1] = s->motion_val[xy][1]*time_pb/time_pp + my; 1831 s->mv[0][0][1] = s->motion_val[xy][1]*time_pb/time_pp + my;
2395 2510
2396 /* decode mpeg4 VOP header */ 2511 /* decode mpeg4 VOP header */
2397 int mpeg4_decode_picture_header(MpegEncContext * s) 2512 int mpeg4_decode_picture_header(MpegEncContext * s)
2398 { 2513 {
2399 int time_incr, startcode, state, v; 2514 int time_incr, startcode, state, v;
2515 int time_increment;
2400 2516
2401 redo: 2517 redo:
2402 /* search next start code */ 2518 /* search next start code */
2403 align_get_bits(&s->gb); 2519 align_get_bits(&s->gb);
2404 state = 0xff; 2520 state = 0xff;
2628 } else if (startcode != 0x1b6) { //VOP 2744 } else if (startcode != 0x1b6) { //VOP
2629 goto redo; 2745 goto redo;
2630 } 2746 }
2631 2747
2632 s->pict_type = get_bits(&s->gb, 2) + 1; /* pict type: I = 0 , P = 1 */ 2748 s->pict_type = get_bits(&s->gb, 2) + 1; /* pict type: I = 0 , P = 1 */
2633 //printf("pic: %d, qpel:%d\n", s->pict_type, s->quarter_sample); 2749 // printf("pic: %d, qpel:%d\n", s->pict_type, s->quarter_sample);
2634 time_incr=0; 2750 time_incr=0;
2635 while (get_bits1(&s->gb) != 0) 2751 while (get_bits1(&s->gb) != 0)
2636 time_incr++; 2752 time_incr++;
2637 2753
2638 check_marker(&s->gb, "before time_increment"); 2754 check_marker(&s->gb, "before time_increment");
2639 s->time_increment= get_bits(&s->gb, s->time_increment_bits); 2755 time_increment= get_bits(&s->gb, s->time_increment_bits);
2756 //printf(" type:%d incr:%d increment:%d\n", s->pict_type, time_incr, time_increment);
2640 if(s->pict_type!=B_TYPE){ 2757 if(s->pict_type!=B_TYPE){
2758 s->last_time_base= s->time_base;
2641 s->time_base+= time_incr; 2759 s->time_base+= time_incr;
2642 s->last_non_b_time[1]= s->last_non_b_time[0]; 2760 s->time= s->time_base*s->time_increment_resolution + time_increment;
2643 s->last_non_b_time[0]= s->time_base*s->time_increment_resolution + s->time_increment; 2761 s->pp_time= s->time - s->last_non_b_time;
2762 s->last_non_b_time= s->time;
2644 }else{ 2763 }else{
2645 s->time= (s->last_non_b_time[1]/s->time_increment_resolution + time_incr)*s->time_increment_resolution; 2764 s->time= (s->last_time_base + time_incr)*s->time_increment_resolution + time_increment;
2646 s->time+= s->time_increment; 2765 s->bp_time= s->last_non_b_time - s->time;
2647 } 2766 }
2648 2767
2649 if(check_marker(&s->gb, "before vop_coded")==0 && s->picture_number==0){ 2768 if(check_marker(&s->gb, "before vop_coded")==0 && s->picture_number==0){
2650 printf("hmm, seems the headers arnt complete, trying to guess time_increment_bits\n"); 2769 printf("hmm, seems the headers arnt complete, trying to guess time_increment_bits\n");
2651 for(s->time_increment_bits++ ;s->time_increment_bits<16; s->time_increment_bits++){ 2770 for(s->time_increment_bits++ ;s->time_increment_bits<16; s->time_increment_bits++){