1 /*
2 * ITU H263 bitstream encoder
3 * Copyright (c) 2000,2001 Fabrice Bellard
4 * H263+ support.
5 * Copyright (c) 2001 Juan J. Sierralta P
6 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
7 *
8 * This file is part of FFmpeg.
9 *
10 * FFmpeg is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * FFmpeg is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with FFmpeg; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25 /**
26 * @file
27 * h263 bitstream encoder.
28 */
29
31
42
43 /**
44 * Table of number of bits a motion vector component needs.
45 */
47
48 /**
49 * Minimal fcode that a motion vector component would need.
50 */
52
53 /**
54 * Minimal fcode that a motion vector component would need in umv.
55 * All entries in this table are 1.
56 */
58
59 //unified encoding tables for run length encoding of coefficients
60 //unified in the sense that the specification specifies the encoding in several steps.
63 //#define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128 + (run)*256 + (level))
64 //#define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128*64 + (run) + (level)*64)
65 #define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128*64 + (run)*128 + (level))
66
68 1, 2, 3, 5, 4, 10, 9, 8,
69 11, 15, 17, 16, 23, 22, 21, 20,
70 19, 18, 25, 24, 27, 26, 11, 7,
71 6, 1, 2, 13, 2, 2, 2, 2,
72 6, 12, 3, 9, 1, 3, 4, 3,
73 7, 4, 1, 1, 5, 5, 14, 6,
74 1, 7, 1, 8, 1, 1, 1, 1,
75 10, 1, 1, 5, 9, 17, 25, 24,
76 29, 33, 32, 41, 2, 23, 28, 31,
77 3, 22, 30, 4, 27, 40, 8, 26,
78 6, 39, 7, 38, 16, 37, 15, 10,
79 11, 12, 13, 14, 1, 21, 20, 18,
80 19, 2, 1, 34, 35, 36
81 };
82
83 /**
84 * Return the 4 bit value that specifies the given aspect ratio.
85 * This may be one of the standard aspect ratios or it specifies
86 * that the aspect will be stored explicitly later.
87 */
89 int i;
90
92
93 for(i=1; i<6; i++){
95 return i;
96 }
97 }
98
100 }
101
103 {
104 int format, coded_frame_rate, coded_frame_rate_base, i, temp_ref;
105 int best_clock_code=1;
106 int best_divisor=60;
107 int best_error= INT_MAX;
108
110 for(i=0; i<2; i++){
111 int div, error;
113 div= av_clip(div, 1, 127);
115 if(error < best_error){
116 best_error= error;
117 best_divisor= div;
118 best_clock_code= i;
119 }
120 }
121 }
122 s->
custom_pcf= best_clock_code!=1 || best_divisor!=60;
123 coded_frame_rate= 1800000;
124 coded_frame_rate_base= (1000+best_clock_code)*best_divisor;
125
127
128 /* Update the pointer to last GOB */
133 put_sbits(&s->
pb, 8, temp_ref);
/* TemporalReference */
134
137 put_bits(&s->
pb, 1, 0);
/* split screen off */
139 put_bits(&s->
pb, 1, 0);
/* freeze picture release off */
140
143 /* H.263v1 */
146 /* By now UMV IS DISABLED ON H.263v1, since the restrictions
147 of H.263v1 UMV implies to check the predicted MV after
148 calculation of the current MB to see if we're on the limits */
149 put_bits(&s->
pb, 1, 0);
/* Unrestricted Motion Vector: off */
152 put_bits(&s->
pb, 1, 0);
/* only I/P frames, no PB frame */
154 put_bits(&s->
pb, 1, 0);
/* Continuous Presence Multipoint mode: off */
155 } else {
156 int ufep=1;
157 /* H.263v2 */
158 /* H.263 Plus PTYPE */
159
161 put_bits(&s->
pb,3,ufep);
/* Update Full Extended PTYPE */
162 if (format == 8)
163 put_bits(&s->
pb,3,6);
/* Custom Source Format */
164 else
166
174 put_bits(&s->
pb,1,0);
/* Reference Picture Selection: off */
175 put_bits(&s->
pb,1,0);
/* Independent Segment Decoding: off */
178 put_bits(&s->
pb,1,1);
/* "1" to prevent start code emulation */
180
182
183 put_bits(&s->
pb,1,0);
/* Reference Picture Resampling: off */
184 put_bits(&s->
pb,1,0);
/* Reduced-Resolution Update: off */
187 put_bits(&s->
pb,1,1);
/* "1" to prevent start code emulation */
188
189 /* This should be here if PLUSPTYPE */
190 put_bits(&s->
pb, 1, 0);
/* Continuous Presence Multipoint mode: off */
191
192 if (format == 8) {
193 /* Custom Picture Format (CPFMT) */
195
198 put_bits(&s->
pb,1,1);
/* "1" to prevent start code emulation */
203 }
204 }
206 if(ufep){
209 }
211 }
212
213 /* Unlimited Unrestricted Motion Vectors Indicator (UUI) */
215 // put_bits(&s->pb,1,1); /* Limited according tables of Annex D */
216 //FIXME check actual requested range
220
222 }
223
225
228
231
233 }
234 }
235
236 /**
237 * Encode a group of blocks header.
238 */
240 {
242
245
247
253 }else{
255
259 }
260 }
261
262 /**
263 * modify qscale so that encoding is actually possible in h263 (limit difference to -2..2)
264 */
266 int i;
268
270
271 for(i=1; i<s->
mb_num; i++){
274 }
275 for(i=s->
mb_num-2; i>=0; i--){
278 }
279
281 for(i=1; i<s->
mb_num; i++){
283
286 }
287 }
288 }
289 }
290
292
293 /**
294 * Encode an 8x8 block.
295 * @param block the 8x8 block
296 * @param n block index (0-3 are luma, 4-5 are chroma)
297 */
299 {
300 int level,
run, last, i, j, last_index, last_non_zero, sign, slevel, code;
302
305 /* DC coef */
306 level = block[0];
307 /* 255 cannot be represented, so we clamp */
308 if (level > 254) {
309 level = 254;
310 block[0] = 254;
311 }
312 /* 0 cannot be represented also */
313 else if (level < 1) {
314 level = 1;
315 block[0] = 1;
316 }
317 if (level == 128) //FIXME check rv10
319 else
321 i = 1;
322 } else {
323 i = 0;
326
328 int aic_vlc_bits=0;
329 int inter_vlc_bits=0;
330 int wrong_pos=-1;
331 int aic_code;
332
334 last_non_zero = i - 1;
335 for (; i <= last_index; i++) {
337 level = block[j];
338 if (level) {
339 run = i - last_non_zero - 1;
340 last = (i == last_index);
341
342 if(level<0) level= -
level;
343
346 inter_vlc_bits += rl->
table_vlc[code][1]+1;
348
350 inter_vlc_bits += 1+6+8-1;
351 }
353 aic_vlc_bits += 1+6+8-1;
354 wrong_pos += run + 1;
355 }else
357 last_non_zero = i;
358 }
359 }
360 i = 0;
361 if(aic_vlc_bits < inter_vlc_bits && wrong_pos > 63)
363 }
364 }
365
366 /* AC coefs */
368 last_non_zero = i - 1;
369 for (; i <= last_index; i++) {
371 level = block[j];
372 if (level) {
373 run = i - last_non_zero - 1;
374 last = (i == last_index);
375 sign = 0;
377 if (level < 0) {
378 sign = 1;
380 }
384 if(!CONFIG_FLV_ENCODER || s->
h263_flv <= 1){
387
389
390 if(level < 128)
392 else{
396 }
397 }else{
399 }
400 } else {
402 }
403 last_non_zero = i;
404 }
405 }
406 }
407
408 /* Encode MV differences on H.263+ with Unrestricted MV mode */
410 {
411 short sval = 0;
412 short i = 0;
413 short n_bits = 0;
414 short temp_val;
415 int code = 0;
416 int tcode;
417
418 if ( val == 0)
420 else if (val == 1)
422 else if (val == -1)
424 else {
425
426 sval = ((val < 0) ? (
short)(-
val):(
short)
val);
427 temp_val = sval;
428
429 while (temp_val != 0) {
430 temp_val = temp_val >> 1;
431 n_bits++;
432 }
433
434 i = n_bits - 1;
435 while (i > 0) {
436 tcode = (sval & (1 << (i-1))) >> (i-1);
437 tcode = (tcode << 1) | 1;
438 code = (code << 2) | tcode;
439 i--;
440 }
441 code = ((code << 1) | (val < 0)) << 1;
443 }
444 }
445
447 int16_t
block[6][64],
448 int motion_x, int motion_y)
449 {
450 int cbpc, cbpy, i, cbp, pred_x, pred_y;
452 int16_t rec_intradc[6];
453 int16_t *dc_ptr[6];
455
457 /* compute cbp */
458 cbp=
get_p_cbp(s, block, motion_x, motion_y);
459
461 /* skip macroblock */
463 if(interleaved_stats){
466 }
468
469 return;
470 }
472
473 cbpc = cbp & 3;
474 cbpy = cbp >> 2;
476 cbpy ^= 0xF;
482
486
487 if(interleaved_stats){
489 }
490
491 /* motion vectors: 16x16 mode */
493
496 motion_y - pred_y, 1);
497 }
498 else {
501 if (((motion_x - pred_x) == 1) && ((motion_y - pred_y) == 1))
502 /* To prevent Start Code emulation */
504 }
505 }else{
512
513 if(interleaved_stats){
515 }
516
517 for(i=0; i<4; i++){
518 /* motion vectors: 8x8 mode*/
520
525 motion_y - pred_y, 1);
526 }
527 else {
530 if (((motion_x - pred_x) == 1) && ((motion_y - pred_y) == 1))
531 /* To prevent Start Code emulation */
533 }
534 }
535 }
536
537 if(interleaved_stats){
539 }
540 } else {
542
543 cbp = 0;
545 /* Predict DC */
546 for(i=0; i<6; i++) {
547 int16_t
level = block[i][0];
548 int scale;
549
552
555 /* Quant */
556 if (level >= 0)
557 level = (level + (scale>>1))/scale;
558 else
559 level = (level - (scale>>1))/scale;
560
562 if (level < -127)
563 level = -127;
564 else if (level > 127)
565 level = 127;
566 }
567
569 /* Reconstruction */
570 rec_intradc[i] = scale*level +
pred_dc;
571 /* Oddify */
572 rec_intradc[i] |= 1;
573 //if ((rec_intradc[i] % 2) == 0)
574 // rec_intradc[i]++;
575 /* Clipping */
576 if (rec_intradc[i] < 0)
577 rec_intradc[i] = 0;
578 else if (rec_intradc[i] > 2047)
579 rec_intradc[i] = 2047;
580
581 /* Update AC/DC tables */
582 *dc_ptr[i] = rec_intradc[i];
583 /* AIC can change CBP */
586 cbp |= 1 << (5 - i);
587 }
588 }else{
589 for(i=0; i<6; i++) {
590 /* compute cbp */
592 cbp |= 1 << (5 - i);
593 }
594 }
595
596 cbpc = cbp & 3;
602 } else {
608 }
610 /* XXX: currently, we do not try to use ac prediction */
611 put_bits(&s->
pb, 1, 0);
/* no AC prediction */
612 }
613 cbpy = cbp >> 2;
617
618 if(interleaved_stats){
620 }
621 }
622
623 for(i=0; i<6; i++) {
624 /* encode each block */
626
627 /* Update INTRADC for decoding */
629 block[i][0] = rec_intradc[i];
630
631 }
632 }
633
634 if(interleaved_stats){
638 }else{
641 }
642 }
643 }
644
646 {
647 int range, bit_size, sign, code,
bits;
648
649 if (val == 0) {
650 /* zero vector */
651 code = 0;
653 } else {
654 bit_size = f_code - 1;
655 range = 1 << bit_size;
656 /* modulo encoding */
658 sign = val>>31;
659 val= (val^sign)-sign;
660 sign&=1;
661
662 val--;
663 code = (val >> bit_size) + 1;
664 bits = val & (range - 1);
665
667 if (bit_size > 0) {
669 }
670 }
671 }
672
674 {
675 int f_code;
677
678 for(f_code=1; f_code<=
MAX_FCODE; f_code++){
681
683 else{
684 int val, bit_size, code;
685
686 bit_size = f_code - 1;
687
689 if (val < 0)
691 val--;
692 code = (val >> bit_size) + 1;
693 if(code<33){
694 len=
ff_mvtab[code][1] + 1 + bit_size;
695 }else{
697 }
698 }
699
701 }
702 }
703
704 for(f_code=
MAX_FCODE; f_code>0; f_code--){
705 for(mv=-(16<<f_code); mv<(16<<f_code); mv++){
707 }
708 }
709
710 for(mv=0; mv<
MAX_MV*2+1; mv++){
712 }
713 }
714
717 {
718 int slevel,
run, last;
719
722
723 for(slevel=-64; slevel<64; slevel++){
724 if(slevel==0) continue;
725 for(run=0; run<64; run++){
726 for(last=0; last<=1; last++){
728 int level= slevel < 0 ? -slevel : slevel;
729 int sign= slevel < 0 ? 1 : 0;
731
733
734 /* ESC0 */
738 bits=bits*2+sign; len++;
739
740 if(code!=rl->
n && len < len_tab[index]){
743 }
744 /* ESC */
747 bits=bits*2+last; len++;
748 bits=bits*64+
run; len+=6;
749 bits=bits*256+(level&0xff); len+=8;
750
751 if(len < len_tab[index]){
754 }
755 }
756 }
757 }
758 }
759
761 {
762 static int done = 0;
763
764 if (!done) {
765 done = 1;
766
769
772
774 }
776
782 }
784
785 // use fcodes >1 only for mpeg4 & h263 & h263p FIXME
789 break;
796 }else{
799 }
800 break;
801 //Note for mpeg4 & h263 the dc-scale table will be set per frame as needed later
806 } else {
809 }
810 break;
811 default: //nothing needed - default table already set in mpegvideo.c
814 }
818 }else{
821 }
822 }
823
825 {
826 int i, mb_pos;
827
828 for(i=0; i<6; i++){
830 }
833 }