1 /*
2 * ITU H.263 bitstream encoder
3 * Copyright (c) 2000,2001 Fabrice Bellard
4 * H.263+ 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 * H.263 bitstream encoder.
28 */
29
30 #include "config_components.h"
31
33
48
49 /**
50 * Table of number of bits a motion vector component needs.
51 */
53
54 /**
55 * Minimal fcode that a motion vector component would need.
56 */
58
59 /**
60 * Minimal fcode that a motion vector component would need in umv.
61 * All entries in this table are 1.
62 */
64
65 //unified encoding tables for run length encoding of coefficients
66 //unified in the sense that the specification specifies the encoding in several steps.
69 //#define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128 + (run)*256 + (level))
70 //#define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128*64 + (run) + (level)*64)
71 #define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128*64 + (run)*128 + (level))
72
74 1, 2, 3, 5, 4, 10, 9, 8,
75 11, 15, 17, 16, 23, 22, 21, 20,
76 19, 18, 25, 24, 27, 26, 11, 7,
77 6, 1, 2, 13, 2, 2, 2, 2,
78 6, 12, 3, 9, 1, 3, 4, 3,
79 7, 4, 1, 1, 5, 5, 14, 6,
80 1, 7, 1, 8, 1, 1, 1, 1,
81 10, 1, 1, 5, 9, 17, 25, 24,
82 29, 33, 32, 41, 2, 23, 28, 31,
83 3, 22, 30, 4, 27, 40, 8, 26,
84 6, 39, 7, 38, 16, 37, 15, 10,
85 11, 12, 13, 14, 1, 21, 20, 18,
86 19, 2, 1, 34, 35, 36
87 };
88
89 /**
90 * Return the 4 bit value that specifies the given aspect ratio.
91 * This may be one of the standard aspect ratios or it specifies
92 * that the aspect will be stored explicitly later.
93 */
96
98
102 }
103 }
104
106 }
107
109 {
110 int format, coded_frame_rate, coded_frame_rate_base,
i, temp_ref;
111 int best_clock_code=1;
112 int best_divisor=60;
113 int best_error= INT_MAX;
114 int custom_pcf;
115
119 div= (
s->avctx->time_base.num*1800000LL + 500LL*
s->avctx->time_base.den) / ((1000LL+
i)*
s->avctx->time_base.den);
121 error=
FFABS(
s->avctx->time_base.num*1800000LL - (1000LL+
i)*
s->avctx->time_base.den*div);
122 if(
error < best_error){
124 best_divisor= div;
126 }
127 }
128 }
129 custom_pcf = best_clock_code != 1 || best_divisor != 60;
130 coded_frame_rate= 1800000;
131 coded_frame_rate_base= (1000+best_clock_code)*best_divisor;
132
134
135 /* Update the pointer to last GOB */
138 temp_ref=
s->picture_number * (
int64_t)coded_frame_rate *
s->avctx->time_base.num /
//FIXME use timestamp
139 (coded_frame_rate_base * (
int64_t)
s->avctx->time_base.den);
140 put_sbits(&
s->pb, 8, temp_ref);
/* TemporalReference */
141
144 put_bits(&
s->pb, 1, 0);
/* split screen off */
146 put_bits(&
s->pb, 1, 0);
/* freeze picture release off */
147
150 /* H.263v1 */
153 /* By now UMV IS DISABLED ON H.263v1, since the restrictions
154 of H.263v1 UMV implies to check the predicted MV after
155 calculation of the current MB to see if we're on the limits */
156 put_bits(&
s->pb, 1, 0);
/* Unrestricted Motion Vector: off */
158 put_bits(&
s->pb, 1,
s->obmc);
/* Advanced Prediction */
159 put_bits(&
s->pb, 1, 0);
/* only I/P-frames, no PB-frame */
161 put_bits(&
s->pb, 1, 0);
/* Continuous Presence Multipoint mode: off */
162 } else {
163 int ufep=1;
164 /* H.263v2 */
165 /* H.263 Plus PTYPE */
166
168 put_bits(&
s->pb,3,ufep);
/* Update Full Extended PTYPE */
170 put_bits(&
s->pb,3,6);
/* Custom Source Format */
171 else
173
175 put_bits(&
s->pb,1,
s->umvplus);
/* Unrestricted Motion Vector */
177 put_bits(&
s->pb,1,
s->obmc);
/* Advanced Prediction Mode */
178 put_bits(&
s->pb,1,
s->h263_aic);
/* Advanced Intra Coding */
179 put_bits(&
s->pb,1,
s->loop_filter);
/* Deblocking Filter */
180 put_bits(&
s->pb,1,
s->h263_slice_structured);
/* Slice Structured */
181 put_bits(&
s->pb,1,0);
/* Reference Picture Selection: off */
182 put_bits(&
s->pb,1,0);
/* Independent Segment Decoding: off */
183 put_bits(&
s->pb,1,
s->alt_inter_vlc);
/* Alternative Inter VLC */
184 put_bits(&
s->pb,1,
s->modified_quant);
/* Modified Quantization: */
185 put_bits(&
s->pb,1,1);
/* "1" to prevent start code emulation */
187
189
190 put_bits(&
s->pb,1,0);
/* Reference Picture Resampling: off */
191 put_bits(&
s->pb,1,0);
/* Reduced-Resolution Update: off */
192 put_bits(&
s->pb,1,
s->no_rounding);
/* Rounding Type */
194 put_bits(&
s->pb,1,1);
/* "1" to prevent start code emulation */
195
196 /* This should be here if PLUSPTYPE */
197 put_bits(&
s->pb, 1, 0);
/* Continuous Presence Multipoint mode: off */
198
200 /* Custom Picture Format (CPFMT) */
202
205 put_bits(&
s->pb,1,1);
/* "1" to prevent start code emulation */
208 put_bits(&
s->pb, 8,
s->avctx->sample_aspect_ratio.num);
209 put_bits(&
s->pb, 8,
s->avctx->sample_aspect_ratio.den);
210 }
211 }
212 if (custom_pcf) {
213 if(ufep){
216 }
218 }
219
220 /* Unlimited Unrestricted Motion Vectors Indicator (UUI) */
222 // put_bits(&s->pb,1,1); /* Limited according tables of Annex D */
223 //FIXME check actual requested range
225 if(
s->h263_slice_structured)
226 put_bits(&
s->pb,2,0);
/* no weird submodes */
227
229 }
230
232
233 if(
s->h263_slice_structured){
235
238
240 }
241 }
242
243 /**
244 * Encode a group of blocks header.
245 */
247 {
249
250 if(
s->h263_slice_structured){
252
254
260 }else{
261 int gob_number= mb_line /
s->gob_index;
262
263 put_bits(&
s->pb, 5, gob_number);
/* GN */
266 }
267 }
268
269 /**
270 * modify qscale so that encoding is actually possible in H.263 (limit difference to -2..2)
271 */
274 int8_t *
const qscale_table =
s->current_picture.qscale_table;
275
277
278 for(
i=1;
i<
s->mb_num;
i++){
279 if(qscale_table[
s->mb_index2xy[
i] ] - qscale_table[
s->mb_index2xy[
i-1] ] >2)
280 qscale_table[
s->mb_index2xy[
i] ]= qscale_table[
s->mb_index2xy[
i-1] ]+2;
281 }
282 for(
i=
s->mb_num-2;
i>=0;
i--){
283 if(qscale_table[
s->mb_index2xy[
i] ] - qscale_table[
s->mb_index2xy[
i+1] ] >2)
284 qscale_table[
s->mb_index2xy[
i] ]= qscale_table[
s->mb_index2xy[
i+1] ]+2;
285 }
286
288 for(
i=1;
i<
s->mb_num;
i++){
289 int mb_xy=
s->mb_index2xy[
i];
290
293 }
294 }
295 }
296 }
297
299
300 /**
301 * Encode an 8x8 block.
302 * @param block the 8x8 block
303 * @param n block index (0-3 are luma, 4-5 are chroma)
304 */
306 {
307 int level,
run, last,
i, j, last_index, last_non_zero, sign, slevel,
code;
309
311 if (
s->mb_intra && !
s->h263_aic) {
312 /* DC coef */
314 /* 255 cannot be represented, so we clamp */
318 }
319 /* 0 cannot be represented also */
320 else if (
level < 1) {
323 }
324 if (
level == 128)
//FIXME check rv10
326 else
329 } else {
331 if (
s->h263_aic &&
s->mb_intra)
333
334 if(
s->alt_inter_vlc && !
s->mb_intra){
335 int aic_vlc_bits=0;
336 int inter_vlc_bits=0;
337 int wrong_pos=-1;
338 int aic_code;
339
340 last_index =
s->block_last_index[n];
341 last_non_zero =
i - 1;
342 for (;
i <= last_index;
i++) {
343 j =
s->intra_scantable.permutated[
i];
346 run =
i - last_non_zero - 1;
347 last = (
i == last_index);
348
350
355
357 inter_vlc_bits += 1+6+8-1;
358 }
360 aic_vlc_bits += 1+6+8-1;
361 wrong_pos +=
run + 1;
362 }else
365 }
366 }
368 if(aic_vlc_bits < inter_vlc_bits && wrong_pos > 63)
370 }
371 }
372
373 /* AC coefs */
374 last_index =
s->block_last_index[n];
375 last_non_zero =
i - 1;
376 for (;
i <= last_index;
i++) {
377 j =
s->intra_scantable.permutated[
i];
380 run =
i - last_non_zero - 1;
381 last = (
i == last_index);
382 sign = 0;
385 sign = 1;
387 }
391 if(!CONFIG_FLV_ENCODER ||
s->h263_flv <= 1){
394
396
399 else{
403 }
404 }else{
406 }
407 } else {
409 }
411 }
412 }
413 }
414
415 /* Encode MV differences on H.263+ with Unrestricted MV mode */
417 {
418 short sval = 0;
420 short n_bits = 0;
421 short temp_val;
423 int tcode;
424
431 else {
432
433 sval = ((
val < 0) ? (
short)(-
val):(
short)
val);
434 temp_val = sval;
435
436 while (temp_val != 0) {
437 temp_val = temp_val >> 1;
438 n_bits++;
439 }
440
443 tcode = (sval & (1 << (
i-1))) >> (
i-1);
444 tcode = (tcode << 1) | 1;
447 }
450 }
451 }
452
454 {
456 int16_t *dc_val;
457
458 /* find prediction */
459 if (n < 4) {
460 x = 2 *
s->mb_x + (n & 1);
461 y = 2 *
s->mb_y + ((n & 2) >> 1);
463 dc_val =
s->dc_val[0];
464 } else {
468 dc_val =
s->dc_val[n - 4 + 1];
469 }
470 /* B C
471 * A X
472 */
473 a = dc_val[(x - 1) + (y) *
wrap];
474 c = dc_val[(x) + (y - 1) *
wrap];
475
476 /* No prediction outside GOB boundary */
477 if (
s->first_slice_line && n != 3) {
478 if (n != 2)
c = 1024;
479 if (n != 1 &&
s->mb_x ==
s->resync_mb_x)
a = 1024;
480 }
481 /* just DC prediction */
482 if (
a != 1024 &&
c != 1024)
486 else
488
489 /* we assume pred is positive */
490 *dc_val_ptr = &dc_val[x + y *
wrap];
492 }
493
495 int16_t
block[6][64],
496 int motion_x, int motion_y)
497 {
498 int cbpc, cbpy,
i, cbp, pred_x, pred_y;
500 int16_t rec_intradc[6];
501 int16_t *dc_ptr[6];
503
505 /* compute cbp */
507
508 if ((cbp | motion_x | motion_y |
s->dquant | (
s->mv_type -
MV_TYPE_16X16)) == 0) {
509 /* skip macroblock */
511 if(interleaved_stats){
514 }
516
517 return;
518 }
520
521 cbpc = cbp & 3;
522 cbpy = cbp >> 2;
523 if(
s->alt_inter_vlc==0 || cbpc!=3)
524 cbpy ^= 0xF;
525 if(
s->dquant) cbpc+= 8;
530
534
535 if(interleaved_stats){
537 }
538
539 /* motion vectors: 16x16 mode */
541
544 motion_y - pred_y, 1);
545 }
546 else {
549 if (((motion_x - pred_x) == 1) && ((motion_y - pred_y) == 1))
550 /* To prevent Start Code emulation */
552 }
553 }else{
560
561 if(interleaved_stats){
563 }
564
566 /* motion vectors: 8x8 mode*/
568
569 motion_x =
s->current_picture.motion_val[0][
s->block_index[
i]][0];
570 motion_y =
s->current_picture.motion_val[0][
s->block_index[
i]][1];
573 motion_y - pred_y, 1);
574 }
575 else {
578 if (((motion_x - pred_x) == 1) && ((motion_y - pred_y) == 1))
579 /* To prevent Start Code emulation */
581 }
582 }
583 }
584
585 if(interleaved_stats){
587 }
588 } else {
590
591 cbp = 0;
593 /* Predict DC */
597
599 else scale=
s->c_dc_scale;
600
603 /* Quant */
606 else
608
609 if(!
s->modified_quant){
612 else if (
level > 127)
614 }
615
617 /* Reconstruction */
619 /* Oddify */
621 //if ((rec_intradc[i] % 2) == 0)
622 // rec_intradc[i]++;
623 /* Clipping */
624 if (rec_intradc[
i] < 0)
626 else if (rec_intradc[
i] > 2047)
627 rec_intradc[
i] = 2047;
628
629 /* Update AC/DC tables */
630 *dc_ptr[
i] = rec_intradc[
i];
631 /* AIC can change CBP */
632 if (
s->block_last_index[
i] > 0 ||
633 (
s->block_last_index[
i] == 0 &&
level !=0))
635 }
636 }else{
638 /* compute cbp */
639 if (
s->block_last_index[
i] >= 1)
641 }
642 }
643
644 cbpc = cbp & 3;
646 if(
s->dquant) cbpc+=4;
650 } else {
651 if(
s->dquant) cbpc+=8;
656 }
658 /* XXX: currently, we do not try to use ac prediction */
659 put_bits(&
s->pb, 1, 0);
/* no AC prediction */
660 }
661 cbpy = cbp >> 2;
665
666 if(interleaved_stats){
668 }
669 }
670
672 /* encode each block */
674
675 /* Update INTRADC for decoding */
676 if (
s->h263_aic &&
s->mb_intra) {
678
679 }
680 }
681
682 if(interleaved_stats){
685 }else{
688 }
689 }
690 }
691
693 {
695
697 /* zero vector */
700 } else {
701 bit_size = f_code - 1;
702 range = 1 << bit_size;
703 /* modulo encoding */
707 sign&=1;
708
712
714 if (bit_size > 0) {
716 }
717 }
718 }
719
721 {
722 int f_code;
724
725 for(f_code=1; f_code<=
MAX_FCODE; f_code++){
728
730 else{
732
733 bit_size = f_code - 1;
734
742 }else{
744 }
745 }
746
748 }
749 }
750
751 for(f_code=
MAX_FCODE; f_code>0; f_code--){
752 for(
mv=-(16<<f_code);
mv<(16<<f_code);
mv++){
754 }
755 }
756
759 }
760 }
761
763 {
764 int slevel,
run, last;
765
768
769 for(slevel=-64; slevel<64; slevel++){
770 if(slevel==0) continue;
772 for(last=0; last<=1; last++){
774 int level= slevel < 0 ? -slevel : slevel;
775 int sign= slevel < 0 ? 1 : 0;
777
779
780 /* ESC0 */
785
788
789 /* ESC */
795
798 }
799 }
800 }
801 }
802
804 {
806
809
812
814 }
815
817 {
819
820 s->me.mv_penalty=
mv_penalty;
// FIXME exact table for MSMPEG4 & H.263+
821
827 }
828 s->ac_esc_length= 7+1+6+8;
829
830 // use fcodes >1 only for MPEG-4 & H.263 & H.263+ FIXME
834 break;
838 if(
s->modified_quant){
839 s->min_qcoeff= -2047;
841 }else{
844 }
845 break;
846 // Note for MPEG-4 & H.263 the dc-scale table will be set per frame as needed later
848 if (
s->h263_flv > 1) {
849 s->min_qcoeff= -1023;
851 } else {
854 }
855 break;
856 default: //nothing needed - default table already set in mpegvideo.c
859 }
863 }else{
866 }
867
868 if (
s->lmin >
s->lmax) {
871 }
872
874 }
875
877 {
879
882 }
883 mb_pos=
s->mb_x +
s->mb_width*
s->mb_y;
885 }
886
887 #define OFFSET(x) offsetof(MpegEncContext, x)
888 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
891 {
"mb_info",
"emit macroblock info for RFC 2190 packetization, the parameter value is the maximum payload size",
OFFSET(
mb_info),
AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX,
VE },
895 };
896
902 };
903
917 };
918
923 {
"structured_slices",
"Write slice start position at every GOB header instead of just GOB number.",
OFFSET(h263_slice_structured),
AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1,
VE},
927 };
933 };
934
948 };