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
31
43
44 /**
45 * Table of number of bits a motion vector component needs.
46 */
48
49 /**
50 * Minimal fcode that a motion vector component would need.
51 */
53
54 /**
55 * Minimal fcode that a motion vector component would need in umv.
56 * All entries in this table are 1.
57 */
59
60 //unified encoding tables for run length encoding of coefficients
61 //unified in the sense that the specification specifies the encoding in several steps.
64 //#define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128 + (run)*256 + (level))
65 //#define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128*64 + (run) + (level)*64)
66 #define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128*64 + (run)*128 + (level))
67
69 1, 2, 3, 5, 4, 10, 9, 8,
70 11, 15, 17, 16, 23, 22, 21, 20,
71 19, 18, 25, 24, 27, 26, 11, 7,
72 6, 1, 2, 13, 2, 2, 2, 2,
73 6, 12, 3, 9, 1, 3, 4, 3,
74 7, 4, 1, 1, 5, 5, 14, 6,
75 1, 7, 1, 8, 1, 1, 1, 1,
76 10, 1, 1, 5, 9, 17, 25, 24,
77 29, 33, 32, 41, 2, 23, 28, 31,
78 3, 22, 30, 4, 27, 40, 8, 26,
79 6, 39, 7, 38, 16, 37, 15, 10,
80 11, 12, 13, 14, 1, 21, 20, 18,
81 19, 2, 1, 34, 35, 36
82 };
83
84 /**
85 * Return the 4 bit value that specifies the given aspect ratio.
86 * This may be one of the standard aspect ratios or it specifies
87 * that the aspect will be stored explicitly later.
88 */
90 int i;
91
93
94 for(i=1; i<6; i++){
96 return i;
97 }
98 }
99
101 }
102
104 {
105 int format, coded_frame_rate, coded_frame_rate_base, i, temp_ref;
106 int best_clock_code=1;
107 int best_divisor=60;
108 int best_error= INT_MAX;
109
111 for(i=0; i<2; i++){
114 div= av_clip(div, 1, 127);
116 if(error < best_error){
118 best_divisor= div;
119 best_clock_code= i;
120 }
121 }
122 }
123 s->
custom_pcf= best_clock_code!=1 || best_divisor!=60;
124 coded_frame_rate= 1800000;
125 coded_frame_rate_base= (1000+best_clock_code)*best_divisor;
126
128
129 /* Update the pointer to last GOB */
134 put_sbits(&s->
pb, 8, temp_ref);
/* TemporalReference */
135
138 put_bits(&s->
pb, 1, 0);
/* split screen off */
140 put_bits(&s->
pb, 1, 0);
/* freeze picture release off */
141
144 /* H.263v1 */
147 /* By now UMV IS DISABLED ON H.263v1, since the restrictions
148 of H.263v1 UMV implies to check the predicted MV after
149 calculation of the current MB to see if we're on the limits */
150 put_bits(&s->
pb, 1, 0);
/* Unrestricted Motion Vector: off */
153 put_bits(&s->
pb, 1, 0);
/* only I/P-frames, no PB-frame */
155 put_bits(&s->
pb, 1, 0);
/* Continuous Presence Multipoint mode: off */
156 } else {
157 int ufep=1;
158 /* H.263v2 */
159 /* H.263 Plus PTYPE */
160
162 put_bits(&s->
pb,3,ufep);
/* Update Full Extended PTYPE */
163 if (format == 8)
164 put_bits(&s->
pb,3,6);
/* Custom Source Format */
165 else
167
175 put_bits(&s->
pb,1,0);
/* Reference Picture Selection: off */
176 put_bits(&s->
pb,1,0);
/* Independent Segment Decoding: off */
179 put_bits(&s->
pb,1,1);
/* "1" to prevent start code emulation */
181
183
184 put_bits(&s->
pb,1,0);
/* Reference Picture Resampling: off */
185 put_bits(&s->
pb,1,0);
/* Reduced-Resolution Update: off */
188 put_bits(&s->
pb,1,1);
/* "1" to prevent start code emulation */
189
190 /* This should be here if PLUSPTYPE */
191 put_bits(&s->
pb, 1, 0);
/* Continuous Presence Multipoint mode: off */
192
193 if (format == 8) {
194 /* Custom Picture Format (CPFMT) */
196
199 put_bits(&s->
pb,1,1);
/* "1" to prevent start code emulation */
204 }
205 }
207 if(ufep){
210 }
212 }
213
214 /* Unlimited Unrestricted Motion Vectors Indicator (UUI) */
216 // put_bits(&s->pb,1,1); /* Limited according tables of Annex D */
217 //FIXME check actual requested range
221
223 }
224
226
229
232
234 }
235 }
236
237 /**
238 * Encode a group of blocks header.
239 */
241 {
243
246
248
254 }else{
256
260 }
261 }
262
263 /**
264 * modify qscale so that encoding is actually possible in H.263 (limit difference to -2..2)
265 */
267 int i;
269
271
272 for(i=1; i<s->
mb_num; i++){
275 }
276 for(i=s->
mb_num-2; i>=0; i--){
279 }
280
282 for(i=1; i<s->
mb_num; i++){
284
287 }
288 }
289 }
290 }
291
293
294 /**
295 * Encode an 8x8 block.
296 * @param block the 8x8 block
297 * @param n block index (0-3 are luma, 4-5 are chroma)
298 */
300 {
301 int level,
run, last, i, j, last_index, last_non_zero, sign, slevel, code;
303
306 /* DC coef */
307 level = block[0];
308 /* 255 cannot be represented, so we clamp */
309 if (level > 254) {
310 level = 254;
311 block[0] = 254;
312 }
313 /* 0 cannot be represented also */
314 else if (level < 1) {
315 level = 1;
316 block[0] = 1;
317 }
318 if (level == 128) //FIXME check rv10
320 else
322 i = 1;
323 } else {
324 i = 0;
327
329 int aic_vlc_bits=0;
330 int inter_vlc_bits=0;
331 int wrong_pos=-1;
332 int aic_code;
333
335 last_non_zero = i - 1;
336 for (; i <= last_index; i++) {
338 level = block[j];
339 if (level) {
340 run = i - last_non_zero - 1;
341 last = (i == last_index);
342
343 if(level<0) level= -
level;
344
347 inter_vlc_bits += rl->
table_vlc[code][1]+1;
349
351 inter_vlc_bits += 1+6+8-1;
352 }
354 aic_vlc_bits += 1+6+8-1;
355 wrong_pos += run + 1;
356 }else
358 last_non_zero = i;
359 }
360 }
361 i = 0;
362 if(aic_vlc_bits < inter_vlc_bits && wrong_pos > 63)
364 }
365 }
366
367 /* AC coefs */
369 last_non_zero = i - 1;
370 for (; i <= last_index; i++) {
372 level = block[j];
373 if (level) {
374 run = i - last_non_zero - 1;
375 last = (i == last_index);
376 sign = 0;
378 if (level < 0) {
379 sign = 1;
381 }
385 if(!CONFIG_FLV_ENCODER || s->
h263_flv <= 1){
388
390
391 if(level < 128)
393 else{
397 }
398 }else{
400 }
401 } else {
403 }
404 last_non_zero = i;
405 }
406 }
407 }
408
409 /* Encode MV differences on H.263+ with Unrestricted MV mode */
411 {
412 short sval = 0;
413 short i = 0;
414 short n_bits = 0;
415 short temp_val;
416 int code = 0;
417 int tcode;
418
419 if ( val == 0)
421 else if (val == 1)
423 else if (val == -1)
425 else {
426
427 sval = ((val < 0) ? (
short)(-
val):(
short)
val);
428 temp_val = sval;
429
430 while (temp_val != 0) {
431 temp_val = temp_val >> 1;
432 n_bits++;
433 }
434
435 i = n_bits - 1;
436 while (i > 0) {
437 tcode = (sval & (1 << (i-1))) >> (i-1);
438 tcode = (tcode << 1) | 1;
439 code = (code << 2) | tcode;
440 i--;
441 }
442 code = ((code << 1) | (val < 0)) << 1;
444 }
445 }
446
448 int16_t
block[6][64],
449 int motion_x, int motion_y)
450 {
451 int cbpc, cbpy, i, cbp, pred_x, pred_y;
453 int16_t rec_intradc[6];
454 int16_t *dc_ptr[6];
456
458 /* compute cbp */
459 cbp=
get_p_cbp(s, block, motion_x, motion_y);
460
462 /* skip macroblock */
464 if(interleaved_stats){
467 }
469
470 return;
471 }
473
474 cbpc = cbp & 3;
475 cbpy = cbp >> 2;
477 cbpy ^= 0xF;
483
487
488 if(interleaved_stats){
490 }
491
492 /* motion vectors: 16x16 mode */
494
497 motion_y - pred_y, 1);
498 }
499 else {
502 if (((motion_x - pred_x) == 1) && ((motion_y - pred_y) == 1))
503 /* To prevent Start Code emulation */
505 }
506 }else{
513
514 if(interleaved_stats){
516 }
517
518 for(i=0; i<4; i++){
519 /* motion vectors: 8x8 mode*/
521
526 motion_y - pred_y, 1);
527 }
528 else {
531 if (((motion_x - pred_x) == 1) && ((motion_y - pred_y) == 1))
532 /* To prevent Start Code emulation */
534 }
535 }
536 }
537
538 if(interleaved_stats){
540 }
541 } else {
543
544 cbp = 0;
546 /* Predict DC */
547 for(i=0; i<6; i++) {
548 int16_t
level = block[i][0];
549 int scale;
550
553
556 /* Quant */
557 if (level >= 0)
558 level = (level + (scale>>1))/scale;
559 else
560 level = (level - (scale>>1))/scale;
561
563 if (level < -127)
564 level = -127;
565 else if (level > 127)
566 level = 127;
567 }
568
570 /* Reconstruction */
571 rec_intradc[i] = scale*level +
pred_dc;
572 /* Oddify */
573 rec_intradc[i] |= 1;
574 //if ((rec_intradc[i] % 2) == 0)
575 // rec_intradc[i]++;
576 /* Clipping */
577 if (rec_intradc[i] < 0)
578 rec_intradc[i] = 0;
579 else if (rec_intradc[i] > 2047)
580 rec_intradc[i] = 2047;
581
582 /* Update AC/DC tables */
583 *dc_ptr[i] = rec_intradc[i];
584 /* AIC can change CBP */
587 cbp |= 1 << (5 - i);
588 }
589 }else{
590 for(i=0; i<6; i++) {
591 /* compute cbp */
593 cbp |= 1 << (5 - i);
594 }
595 }
596
597 cbpc = cbp & 3;
603 } else {
609 }
611 /* XXX: currently, we do not try to use ac prediction */
612 put_bits(&s->
pb, 1, 0);
/* no AC prediction */
613 }
614 cbpy = cbp >> 2;
618
619 if(interleaved_stats){
621 }
622 }
623
624 for(i=0; i<6; i++) {
625 /* encode each block */
627
628 /* Update INTRADC for decoding */
630 block[i][0] = rec_intradc[i];
631
632 }
633 }
634
635 if(interleaved_stats){
639 }else{
642 }
643 }
644 }
645
647 {
648 int range, bit_size, sign, code, bits;
649
650 if (val == 0) {
651 /* zero vector */
652 code = 0;
654 } else {
655 bit_size = f_code - 1;
656 range = 1 << bit_size;
657 /* modulo encoding */
659 sign = val>>31;
660 val= (val^sign)-sign;
661 sign&=1;
662
663 val--;
664 code = (val >> bit_size) + 1;
665 bits = val & (range - 1);
666
668 if (bit_size > 0) {
670 }
671 }
672 }
673
675 {
676 int f_code;
678
679 for(f_code=1; f_code<=
MAX_FCODE; f_code++){
682
684 else{
685 int val, bit_size, code;
686
687 bit_size = f_code - 1;
688
690 if (val < 0)
692 val--;
693 code = (val >> bit_size) + 1;
694 if(code<33){
695 len=
ff_mvtab[code][1] + 1 + bit_size;
696 }else{
698 }
699 }
700
702 }
703 }
704
705 for(f_code=
MAX_FCODE; f_code>0; f_code--){
706 for(mv=-(16<<f_code); mv<(16<<f_code); mv++){
708 }
709 }
710
711 for(mv=0; mv<
MAX_MV*2+1; mv++){
713 }
714 }
715
718 {
719 int slevel,
run, last;
720
723
724 for(slevel=-64; slevel<64; slevel++){
725 if(slevel==0) continue;
726 for(run=0; run<64; run++){
727 for(last=0; last<=1; last++){
729 int level= slevel < 0 ? -slevel : slevel;
730 int sign= slevel < 0 ? 1 : 0;
732
734
735 /* ESC0 */
739 bits=bits*2+sign; len++;
740
741 if(code!=rl->
n && len < len_tab[index]){
742 if(bits_tab) bits_tab[
index]= bits;
744 }
745 /* ESC */
748 bits=bits*2+last; len++;
749 bits=bits*64+
run; len+=6;
750 bits=bits*256+(level&0xff); len+=8;
751
752 if(len < len_tab[index]){
753 if(bits_tab) bits_tab[
index]= bits;
755 }
756 }
757 }
758 }
759 }
760
762 {
763 static int done = 0;
764
765 if (!done) {
766 done = 1;
767
770
773
775 }
777
783 }
785
786 // use fcodes >1 only for MPEG-4 & H.263 & H.263+ FIXME
790 break;
797 }else{
800 }
801 break;
802 // Note for MPEG-4 & H.263 the dc-scale table will be set per frame as needed later
807 } else {
810 }
811 break;
812 default: //nothing needed - default table already set in mpegvideo.c
815 }
819 }else{
822 }
823 }
824
826 {
827 int i, mb_pos;
828
829 for(i=0; i<6; i++){
831 }
834 }
static uint8_t mv_penalty[MAX_FCODE+1][MAX_DMV *2+1]
Table of number of bits a motion vector component needs.
static const int dquant_code[5]
const char const char void * val
static const char * format[]
av_cold void ff_h263_encode_init(MpegEncContext *s)
uint8_t * fcode_tab
smallest fcode needed for each MV
const uint8_t * y_dc_scale_table
qscale -> y_dc_scale table
const uint16_t ff_mba_max[6]
static void put_sbits(PutBitContext *pb, int n, int32_t value)
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
int obmc
overlapped block motion compensation
void avpriv_align_put_bits(PutBitContext *s)
Pad the bitstream with zeros up to the next byte boundary.
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel...
int min_qcoeff
minimum encodable coefficient
#define UNI_MPEG4_ENC_INDEX(last, run, level)
const uint16_t ff_h263_format[8][2]
uint8_t * intra_ac_vlc_length
int mb_num
number of MBs of a picture
void ff_h263_encode_gob_header(MpegEncContext *s, int mb_line)
Encode a group of blocks header.
uint8_t(* mv_penalty)[MAX_DMV *2+1]
bit amount needed to encode a MV
void ff_h263_encode_mba(MpegEncContext *s)
int h263_aic
Advanced INTRA Coding (AIC)
int16_t * ff_h263_pred_motion(MpegEncContext *s, int block, int dir, int *px, int *py)
Macro definitions for various function/variable attributes.
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
static uint8_t uni_h263_inter_rl_len[64 *64 *2 *2]
#define CANDIDATE_MB_TYPE_INTER
#define av_assert0(cond)
assert() equivalent, that is always enabled.
int alt_inter_vlc
alternative inter vlc
const uint8_t ff_mpeg1_dc_scale_table[128]
static av_cold void init_uni_h263_rl_tab(RLTable *rl, uint32_t *bits_tab, uint8_t *len_tab)
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
#define CANDIDATE_MB_TYPE_INTER4V
int misc_bits
cbp, mb_type
int no_rounding
apply no rounding to motion compensation (MPEG-4, msmpeg4, ...) for B-frames rounding mode is always ...
Picture current_picture
copy of the current picture structure.
void ff_init_qscale_tab(MpegEncContext *s)
init s->current_picture.qscale_table from s->lambda_table
const uint8_t ff_h263_intra_MCBPC_bits[9]
int ff_h263_pred_dc(MpegEncContext *s, int n, int16_t **dc_val_ptr)
const uint8_t ff_mba_length[7]
int max_qcoeff
maximum encodable coefficient
int dquant
qscale difference to prev qscale
static int get_bits_diff(MpegEncContext *s)
int h263_plus
H.263+ headers.
uint8_t ff_h263_static_rl_table_store[2][2][2 *MAX_RUN+MAX_LEVEL+3]
static uint8_t * put_bits_ptr(PutBitContext *s)
Return the pointer to the byte where the bitstream writer will put the next bit.
const uint8_t ff_h263_inter_MCBPC_code[28]
uint8_t * inter_ac_vlc_last_length
void ff_flv2_encode_ac_esc(PutBitContext *pb, int slevel, int level, int run, int last)
static uint8_t fcode_tab[MAX_MV *2+1]
Minimal fcode that a motion vector component would need.
int flags
AV_CODEC_FLAG_*.
int h263_slice_structured
uint16_t * mb_type
Table for candidate MB types for encoding (defines in mpegutils.h)
static void h263_encode_block(MpegEncContext *s, int16_t *block, int n)
Encode an 8x8 block.
uint8_t * intra_ac_vlc_last_length
int n
number of entries of table_vlc minus 1
const uint8_t ff_h263_inter_MCBPC_bits[28]
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
const uint16_t(* table_vlc)[2]
int umvplus
== H.263+ && unrestricted_mv
int16_t(*[2] motion_val)[2]
static void FUNC() pred_dc(uint8_t *_src, const uint8_t *_top, const uint8_t *_left, ptrdiff_t stride, int log2_size, int c_idx)
void ff_h263_encode_motion(PutBitContext *pb, int val, int f_code)
#define AV_CODEC_FLAG_PASS1
Use internal 2pass ratecontrol in first pass mode.
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
int block_last_index[12]
last non zero coefficient in block
int ac_esc_length
num of bits needed to encode the longest esc
static void error(const char *err)
#define FF_ARRAY_ELEMS(a)
int block_index[6]
index to current MB in block based arrays with edges
static av_cold void init_mv_penalty_and_fcode(MpegEncContext *s)
int * mb_index2xy
mb_index -> mb_x + mb_y*mb_stride
static const int8_t mv[256][2]
#define MV_TYPE_16X16
1 vector for the whole mb
const uint8_t ff_aic_dc_scale_table[32]
#define FF_ASPECT_EXTENDED
Libavcodec external API header.
int h263_flv
use flv H.263 header
static uint8_t umv_fcode_tab[MAX_MV *2+1]
Minimal fcode that a motion vector component would need in umv.
const uint8_t ff_h263_intra_MCBPC_code[9]
void ff_h263_encode_picture_header(MpegEncContext *s, int picture_number)
ScanTable intra_scantable
int height
picture size. must be a multiple of 16
av_cold int ff_rl_init(RLTable *rl, uint8_t static_store[2][2 *MAX_RUN+MAX_LEVEL+3])
uint8_t * inter_ac_vlc_length
static const uint8_t wrong_run[102]
Rational number (pair of numerator and denominator).
static void ff_h263_encode_motion_vector(MpegEncContext *s, int x, int y, int f_code)
static uint8_t uni_h263_intra_aic_rl_len[64 *64 *2 *2]
const uint8_t ff_h263_cbpy_tab[16][2]
int pict_type
AV_PICTURE_TYPE_I, AV_PICTURE_TYPE_P, AV_PICTURE_TYPE_B, ...
static av_const int sign_extend(int val, unsigned bits)
const AVRational ff_h263_pixel_aspect[16]
const uint8_t * c_dc_scale_table
qscale -> c_dc_scale table
struct AVCodecContext * avctx
PutBitContext pb
bit output
static int av_cmp_q(AVRational a, AVRational b)
Compare two rationals.
common internal api header.
static void h263p_encode_umotion(PutBitContext *pb, int val)
av_const int ff_h263_aspect_to_info(AVRational aspect)
Return the 4 bit value that specifies the given aspect ratio.
void ff_h263_encode_mb(MpegEncContext *s, int16_t block[6][64], int motion_x, int motion_y)
static int get_rl_index(const RLTable *rl, int last, int run, int level)
void ff_clean_h263_qscales(MpegEncContext *s)
modify qscale so that encoding is actually possible in H.263 (limit difference to -2...
int last_bits
temp var used for calculating the above vars
static int get_p_cbp(MpegEncContext *s, int16_t block[6][64], int motion_x, int motion_y)
int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
Return the index into tab at which {a,b} match elements {[0],[1]} of tab.
const uint8_t ff_mvtab[33][2]