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 #include <string.h>
34
50
51 /**
52 * Table of number of bits a motion vector component needs.
53 */
55
57 {
58 for (
int f_code = 1; f_code <=
MAX_FCODE; f_code++) {
61
62 if (
mv == 0)
len = 1;
// ff_mvtab[0][1]
63 else {
65
66 bit_size = f_code - 1;
67
75 } else {
77 }
78 }
79
81 }
82 }
83 }
84
85 #if CONFIG_H263_ENCODER
86 /**
87 * Minimal fcode that a motion vector component would need in umv.
88 * All entries in this table are 1.
89 */
90 static uint8_t umv_fcode_tab[
MAX_MV*2+1];
91
92 //unified encoding tables for run length encoding of coefficients
93 //unified in the sense that the specification specifies the encoding in several steps.
94 static uint8_t uni_h263_intra_aic_rl_len [64*64*2*2];
95 static uint8_t uni_h263_inter_rl_len [64*64*2*2];
96 //#define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128 + (run)*256 + (level))
97 //#define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128*64 + (run) + (level)*64)
98 #define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128*64 + (run)*128 + (level))
99
100 static av_cold void init_uni_h263_rl_tab(
const RLTable *rl, uint8_t *len_tab)
101 {
102 const uint16_t (*table_vlc)[2] = rl->
table_vlc;
103 const uint8_t *table_run = rl->
table_run;
105
108
109 // Note: The LUT only covers level values for which the escape value
110 // is eight bits (not 8 + 5 + 6)
112 sizeof(uni_h263_intra_aic_rl_len));
113
114 len_tab += 64; // simplifies addressing
116 int run = table_run[
i];
117 int level = table_level[
i];
119 int len = table_vlc[
i][1];
120
123 }
127 }
128 }
129 #endif
130
132 {
133 #if CONFIG_H263_ENCODER
137
140
141 memset(umv_fcode_tab, 1, sizeof(umv_fcode_tab));
142 #endif
143
145 }
146
148 {
150
152
154 }
155
157 {
159 /* zero vector -- corresponds to ff_mvtab[0] */
161 } else {
163 int bit_size = f_code - 1;
164 int range = 1 << bit_size;
165 /* modulo encoding */
169 sign&=1;
170
174
176 if (bit_size > 0) {
178 }
179 }
180 }
181
182 #if CONFIG_H263_ENCODER // Snow and SVQ1 need the above
183 static const uint8_t wrong_run[102] = {
184 1, 2, 3, 5, 4, 10, 9, 8,
185 11, 15, 17, 16, 23, 22, 21, 20,
186 19, 18, 25, 24, 27, 26, 11, 7,
187 6, 1, 2, 13, 2, 2, 2, 2,
188 6, 12, 3, 9, 1, 3, 4, 3,
189 7, 4, 1, 1, 5, 5, 14, 6,
190 1, 7, 1, 8, 1, 1, 1, 1,
191 10, 1, 1, 5, 9, 17, 25, 24,
192 29, 33, 32, 41, 2, 23, 28, 31,
193 3, 22, 30, 4, 27, 40, 8, 26,
194 6, 39, 7, 38, 16, 37, 15, 10,
195 11, 12, 13, 14, 1, 21, 20, 18,
196 19, 2, 1, 34, 35, 36
197 };
198
199 /**
200 * Return the 4 bit value that specifies the given aspect ratio.
201 * This may be one of the standard aspect ratios or it specifies
202 * that the aspect will be stored explicitly later.
203 */
206
208
212 }
213 }
214
216 }
217
219 {
221 int format, coded_frame_rate, coded_frame_rate_base,
i, temp_ref;
222 int best_clock_code=1;
223 int best_divisor=60;
224 int best_error= INT_MAX;
225 int custom_pcf;
226
228
232 div= (
s->c.avctx->time_base.num*1800000LL + 500LL*
s->c.avctx->time_base.den) / ((1000LL+
i)*
s->c.avctx->time_base.den);
234 error=
FFABS(
s->c.avctx->time_base.num*1800000LL - (1000LL+
i)*
s->c.avctx->time_base.den*div);
235 if(
error < best_error){
237 best_divisor= div;
239 }
240 }
241 }
242 custom_pcf = best_clock_code != 1 || best_divisor != 60;
243 coded_frame_rate= 1800000;
244 coded_frame_rate_base= (1000+best_clock_code)*best_divisor;
245
247 temp_ref =
s->picture_number * (
int64_t)coded_frame_rate *
s->c.avctx->time_base.num /
//FIXME use timestamp
248 (coded_frame_rate_base * (
int64_t)
s->c.avctx->time_base.den);
249 put_sbits(&
s->pb, 8, temp_ref);
/* TemporalReference */
250
253 put_bits(&
s->pb, 1, 0);
/* split screen off */
255 put_bits(&
s->pb, 1, 0);
/* freeze picture release off */
256
259 /* H.263v1 */
262 /* By now UMV IS DISABLED ON H.263v1, since the restrictions
263 of H.263v1 UMV implies to check the predicted MV after
264 calculation of the current MB to see if we're on the limits */
265 put_bits(&
s->pb, 1, 0);
/* Unrestricted Motion Vector: off */
267 put_bits(&
s->pb, 1,
s->c.obmc);
/* Advanced Prediction */
268 put_bits(&
s->pb, 1, 0);
/* only I/P-frames, no PB-frame */
270 put_bits(&
s->pb, 1, 0);
/* Continuous Presence Multipoint mode: off */
271 } else {
272 int ufep=1;
273 /* H.263v2 */
274 /* H.263 Plus PTYPE */
275
277 put_bits(&
s->pb,3,ufep);
/* Update Full Extended PTYPE */
279 put_bits(&
s->pb,3,6);
/* Custom Source Format */
280 else
282
284 put_bits(&
s->pb,1,
s->umvplus);
/* Unrestricted Motion Vector */
286 put_bits(&
s->pb,1,
s->c.obmc);
/* Advanced Prediction Mode */
287 put_bits(&
s->pb,1,
s->c.h263_aic);
/* Advanced Intra Coding */
288 put_bits(&
s->pb,1,
s->loop_filter);
/* Deblocking Filter */
289 put_bits(&
s->pb,1,
s->h263_slice_structured);
/* Slice Structured */
290 put_bits(&
s->pb,1,0);
/* Reference Picture Selection: off */
291 put_bits(&
s->pb,1,0);
/* Independent Segment Decoding: off */
292 put_bits(&
s->pb,1,
s->alt_inter_vlc);
/* Alternative Inter VLC */
293 put_bits(&
s->pb,1,
s->modified_quant);
/* Modified Quantization: */
294 put_bits(&
s->pb,1,1);
/* "1" to prevent start code emulation */
296
298
299 put_bits(&
s->pb,1,0);
/* Reference Picture Resampling: off */
300 put_bits(&
s->pb,1,0);
/* Reduced-Resolution Update: off */
301 put_bits(&
s->pb,1,
s->c.no_rounding);
/* Rounding Type */
303 put_bits(&
s->pb,1,1);
/* "1" to prevent start code emulation */
304
305 /* This should be here if PLUSPTYPE */
306 put_bits(&
s->pb, 1, 0);
/* Continuous Presence Multipoint mode: off */
307
309 /* Custom Picture Format (CPFMT) */
311
314 put_bits(&
s->pb,1,1);
/* "1" to prevent start code emulation */
317 put_bits(&
s->pb, 8,
s->c.avctx->sample_aspect_ratio.num);
318 put_bits(&
s->pb, 8,
s->c.avctx->sample_aspect_ratio.den);
319 }
320 }
321 if (custom_pcf) {
322 if(ufep){
325 }
327 }
328
329 /* Unlimited Unrestricted Motion Vectors Indicator (UUI) */
331 // put_bits(&s->pb,1,1); /* Limited according tables of Annex D */
332 //FIXME check actual requested range
334 if (
s->h263_slice_structured)
335 put_bits(&
s->pb,2,0);
/* no weird submodes */
336
338 }
339
341
342 if (
s->h263_slice_structured) {
344
347
349 }
350
351 return 0;
352 }
353
355 {
356 int16_t *
dc =
s->c.dc_val;
357
358 // The "- 1" is for the top-left entry
359 const int l_xy =
s->c.block_index[2];
360 for (
int i = l_xy - 2 *
s->c.b8_stride - 1;
i < l_xy;
i += 2)
362
363 const int u_xy =
s->c.block_index[4];
364 const int v_xy =
s->c.block_index[5];
365 int16_t *dc2 =
dc + v_xy - u_xy;
366 for (
int i = u_xy -
s->c.mb_stride - 1;
i < u_xy; ++
i)
367 dc[
i] = dc2[
i] = 1024;
368 }
369
370 /**
371 * Encode a group of blocks header.
372 */
374 {
376
377 if (
s->h263_slice_structured) {
379
381
382 if(
s->c.mb_num > 1583)
384 put_bits(&
s->pb, 5,
s->c.qscale);
/* GQUANT */
387 }else{
388 int gob_number = mb_line /
s->gob_index;
389
390 put_bits(&
s->pb, 5, gob_number);
/* GN */
392 put_bits(&
s->pb, 5,
s->c.qscale);
/* GQUANT */
393 }
394 }
395
396 /**
397 * modify qscale so that encoding is actually possible in H.263 (limit difference to -2..2)
398 */
400 {
401 int8_t *
const qscale_table =
s->c.cur_pic.qscale_table;
402
403 for (
int i = 1;
i <
s->c.mb_num;
i++) {
404 if (qscale_table[
s->c.mb_index2xy[
i] ] - qscale_table[
s->c.mb_index2xy[
i-1] ] > 2)
405 qscale_table[
s->c.mb_index2xy[
i] ] = qscale_table[
s->c.mb_index2xy[
i-1] ] + 2;
406 }
407 for(
int i =
s->c.mb_num - 2;
i >= 0;
i--) {
408 if (qscale_table[
s->c.mb_index2xy[
i] ] - qscale_table[
s->c.mb_index2xy[
i+1] ] > 2)
409 qscale_table[
s->c.mb_index2xy[
i] ] = qscale_table[
s->c.mb_index2xy[
i+1] ] + 2;
410 }
411
413 for (
int i = 1;
i <
s->c.mb_num;
i++) {
414 int mb_xy =
s->c.mb_index2xy[
i];
415
416 if (qscale_table[mb_xy] != qscale_table[
s->c.mb_index2xy[
i - 1]] &&
419 }
420 }
421 }
422 }
423
425
428 {
431 if (
level < 64) {
// 7-bit level
432 bits = 1 + 1 + 6 + 7;
433 code = (0 << (1 + 6 + 7)) |
434 (last << (6 + 7)) |
436 (slevel & 0x7f);
437 } else {
438 /* 11-bit level */
439 bits = 1 + 1 + 6 + 11;
440 code = (1 << (1 + 6 + 11)) |
441 (last << (6 + 11)) |
443 (slevel & 0x7ff);
444 }
446 }
447
448 /**
449 * Encode an 8x8 block.
450 * @param block the 8x8 block
451 * @param n block index (0-3 are luma, 4-5 are chroma)
452 */
454 {
455 int level,
run, last,
i, j, last_index, last_non_zero, sign, slevel,
code;
457
459 if (
s->c.mb_intra && !
s->c.h263_aic) {
460 /* DC coef */
462 /* 255 cannot be represented, so we clamp */
466 }
467 /* 0 cannot be represented also */
468 else if (
level < 1) {
471 }
472 if (
level == 128)
//FIXME check rv10
474 else
477 } else {
479 if (
s->c.h263_aic &&
s->c.mb_intra)
481
482 if (
s->alt_inter_vlc && !
s->c.mb_intra) {
483 int aic_vlc_bits=0;
484 int inter_vlc_bits=0;
485 int wrong_pos=-1;
486 int aic_code;
487
488 last_index =
s->c.block_last_index[n];
489 last_non_zero =
i - 1;
490 for (;
i <= last_index;
i++) {
491 j =
s->c.intra_scantable.permutated[
i];
494 run =
i - last_non_zero - 1;
495 last = (
i == last_index);
496
498
503
505 inter_vlc_bits += 1+6+8-1;
506 }
508 aic_vlc_bits += 1+6+8-1;
509 wrong_pos +=
run + 1;
510 }else
511 wrong_pos += wrong_run[aic_code];
513 }
514 }
516 if(aic_vlc_bits < inter_vlc_bits && wrong_pos > 63)
518 }
519 }
520
521 /* AC coefs */
522 last_index =
s->c.block_last_index[n];
523 last_non_zero =
i - 1;
524 for (;
i <= last_index;
i++) {
525 j =
s->c.intra_scantable.permutated[
i];
528 run =
i - last_non_zero - 1;
529 last = (
i == last_index);
530 sign = 0;
533 sign = 1;
535 }
542
544
547 } else {
551 }
552 } else {
553 flv2_encode_ac_esc(&
s->pb, slevel,
level,
run, last);
554 }
555 } else {
557 }
559 }
560 }
561 }
562
563 /* Encode MV differences on H.263+ with Unrestricted MV mode */
565 {
568 else {
569 unsigned code = (
val < 0) << 1;
571 unsigned n_bits = 2;
572
573 while (aval != 1) { // The leading digit is implicitly coded via length
574 unsigned tmp = (aval & 1) << 1 | 1;
575 aval >>= 1;
577 n_bits += 2;
578 }
580 }
581 }
582
583 static int h263_pred_dc(
MPVEncContext *
const s,
int n, int16_t **dc_val_ptr)
584 {
585 const int wrap =
s->c.block_wrap[n];
586 const int xy =
s->c.block_index[n];
587 int16_t *
const dc_val =
s->c.dc_val + xy;
589
590 /* find prediction */
591 /* B C
592 * A X
593 */
595 int c = dc_val[-
wrap];
596
597 /* just DC prediction */
598 if (
a != 1024 &&
c != 1024)
602 else
604
605 /* we assume pred is positive */
606 *dc_val_ptr = dc_val;
608 }
609
612 int motion_x, int motion_y)
613 {
614 int cbpc, cbpy,
i, cbp, pred_x, pred_y;
616 int16_t rec_intradc[6];
618
619 if (!
s->c.mb_intra) {
620 /* compute cbp */
622
623 if ((cbp | motion_x | motion_y |
s->dquant | (
s->c.mv_type -
MV_TYPE_16X16)) == 0) {
624 /* skip macroblock */
626 if(interleaved_stats){
629 }
630
631 return;
632 }
634
635 cbpc = cbp & 3;
636 cbpy = cbp >> 2;
637 if (!
s->alt_inter_vlc || cbpc!=3)
638 cbpy ^= 0xF;
639 if(
s->dquant) cbpc+= 8;
644
648
649 if(interleaved_stats){
651 }
652
653 /* motion vectors: 16x16 mode */
655
658 motion_y - pred_y, 1);
659 }
660 else {
661 h263p_encode_umotion(&
s->pb, motion_x - pred_x);
662 h263p_encode_umotion(&
s->pb, motion_y - pred_y);
663 if (((motion_x - pred_x) == 1) && ((motion_y - pred_y) == 1))
664 /* To prevent Start Code emulation */
666 }
667 }else{
674
675 if(interleaved_stats){
677 }
678
680 /* motion vectors: 8x8 mode*/
682
683 motion_x =
s->c.cur_pic.motion_val[0][
s->c.block_index[
i]][0];
684 motion_y =
s->c.cur_pic.motion_val[0][
s->c.block_index[
i]][1];
687 motion_y - pred_y, 1);
688 }
689 else {
690 h263p_encode_umotion(&
s->pb, motion_x - pred_x);
691 h263p_encode_umotion(&
s->pb, motion_y - pred_y);
692 if (((motion_x - pred_x) == 1) && ((motion_y - pred_y) == 1))
693 /* To prevent Start Code emulation */
695 }
696 }
697 }
698
699 if(interleaved_stats){
701 }
702 } else {
704
705 cbp = 0;
707 /* Predict DC */
710 int16_t *dc_ptr;
711 int scale =
i < 4 ?
s->c.y_dc_scale :
s->c.c_dc_scale;
712
715 /* Quant */
718 else
720
721 if (!
s->modified_quant) {
724 else if (
level > 127)
726 }
727
729 /* Reconstruction */
731 /* Oddify */
733 //if ((rec_intradc[i] % 2) == 0)
734 // rec_intradc[i]++;
735 /* Clipping */
736 if (rec_intradc[
i] < 0)
738 else if (rec_intradc[
i] > 2047)
739 rec_intradc[
i] = 2047;
740
741 /* Update AC/DC tables */
742 *dc_ptr = rec_intradc[
i];
743 /* AIC can change CBP */
744 if (
s->c.block_last_index[
i] > 0 ||
745 (
s->c.block_last_index[
i] == 0 &&
level !=0))
747 }
748 }else{
750 /* compute cbp */
751 if (
s->c.block_last_index[
i] >= 1)
753 }
754 }
755
756 cbpc = cbp & 3;
758 if(
s->dquant) cbpc+=4;
762 } else {
763 if(
s->dquant) cbpc+=8;
768 }
770 /* XXX: currently, we do not try to use ac prediction */
771 put_bits(&
s->pb, 1, 0);
/* no AC prediction */
772 }
773 cbpy = cbp >> 2;
777
778 if(interleaved_stats){
780 }
781 }
782
784 /* encode each block */
786
787 /* Update INTRADC for decoding */
788 if (
s->c.h263_aic &&
s->c.mb_intra)
790 }
791
792 if(interleaved_stats){
793 if (!
s->c.mb_intra) {
795 }else{
798 }
799 }
800 }
801
803 {
804 const int mb_xy =
s->c.mb_y *
s->c.mb_stride +
s->c.mb_x;
805
806 if (
s->c.cur_pic.mbskip_table)
807 s->c.cur_pic.mbskip_table[mb_xy] =
s->c.mb_skipped;
808
811 else if(
s->c.mb_intra)
813 else
815
817 }
818
820 {
822
824
826
828 return;
829
830 s->intra_ac_vlc_length =
s->inter_ac_vlc_length = uni_h263_inter_rl_len;
831 s->intra_ac_vlc_last_length=
s->inter_ac_vlc_last_length= uni_h263_inter_rl_len + 128*64;
833 s->intra_ac_vlc_length = uni_h263_intra_aic_rl_len;
834 s->intra_ac_vlc_last_length= uni_h263_intra_aic_rl_len + 128*64;
835
836 s->c.y_dc_scale_table =
838 }
839 s->ac_esc_length= 7+1+6+8;
840
841 if (
s->modified_quant)
843
844 // Only used for H.263 and H.263+
846
847 // use fcodes >1 only for MPEG-4 & H.263 & H.263+ FIXME
848 switch(
s->c.codec_id){
852 if (
s->modified_quant) {
853 s->min_qcoeff= -2047;
855 }else{
858 }
859 break;
860 // Note for MPEG-4 & H.263 the dc-scale table will be set per frame as needed later
861 #if CONFIG_FLV_ENCODER
864 /* format = 1; 11-bit codes */
865 s->min_qcoeff = -1023;
866 s->max_qcoeff = 1023;
867 break;
868 #endif
869 default: //nothing needed - default table already set in mpegvideo.c
872 }
873 // H.263, H.263+; will be overwritten for MSMPEG-4 later
877 s->encode_mb = h263_encode_mb;
878 }
879
881 {
883
886 }
887 mb_pos=
s->c.mb_x +
s->c.mb_width*
s->c.mb_y;
889 }
890
891 #define OFFSET(x) offsetof(MpegEncContext, x)
892 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
893 static const AVOption h263_options[] = {
895 {
"mb_info",
"emit macroblock info for RFC 2190 packetization, the parameter value is the maximum payload size",
FF_MPV_OFFSET(
mb_info),
AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX,
VE },
899 };
900
901 static const AVClass h263_class = {
904 .option = h263_options,
906 };
907
915 .p.priv_class = &h263_class,
922 };
923
924 static const AVOption h263p_options[] = {
928 {
"structured_slices",
"Write slice start position at every GOB header instead of just GOB number.",
FF_MPV_OFFSET(h263_slice_structured),
AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1,
VE},
932 };
933 static const AVClass h263p_class = {
936 .option = h263p_options,
938 };
939
947 .p.priv_class = &h263p_class,
955 };
956 #endif