3afc238e35e9382e9c4bced4ca2e2fe1c513b9d7
[libav.git] / libavcodec / ituh263enc.c
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 Libav.
9 *
10 * Libav 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 * Libav 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 Libav; 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
30 #include <limits.h>
31
32 #include "libavutil/attributes.h"
33 #include "avcodec.h"
34 #include "mpegvideo.h"
35 #include "h263.h"
36 #include "mathops.h"
37 #include "mpegutils.h"
38 #include "unary.h"
39 #include "flv.h"
40 #include "mpeg4video.h"
41 #include "internal.h"
42
43 /**
44 * Table of number of bits a motion vector component needs.
45 */
46 static uint8_t mv_penalty[MAX_FCODE+1][MAX_MV*2+1];
47
48 /**
49 * Minimal fcode that a motion vector component would need.
50 */
51 static uint8_t fcode_tab[MAX_MV*2+1];
52
53 /**
54 * Minimal fcode that a motion vector component would need in umv.
55 * All entries in this table are 1.
56 */
57 static uint8_t umv_fcode_tab[MAX_MV*2+1];
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.
61 static uint8_t uni_h263_intra_aic_rl_len [64*64*2*2];
62 static uint8_t uni_h263_inter_rl_len [64*64*2*2];
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
67 static const uint8_t wrong_run[102] = {
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 */
88 av_const int ff_h263_aspect_to_info(AVRational aspect){
89 int i;
90
91 if(aspect.num==0) aspect= (AVRational){1,1};
92
93 for(i=1; i<6; i++){
94 if(av_cmp_q(ff_h263_pixel_aspect[i], aspect) == 0){
95 return i;
96 }
97 }
98
99 return FF_ASPECT_EXTENDED;
100 }
101
102 void ff_h263_encode_picture_header(MpegEncContext * s, int picture_number)
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
109 if(s->h263_plus){
110 for(i=0; i<2; i++){
111 int div, error;
112 div= (s->avctx->time_base.num*1800000LL + 500LL*s->avctx->time_base.den) / ((1000LL+i)*s->avctx->time_base.den);
113 div= av_clip(div, 1, 127);
114 error= FFABS(s->avctx->time_base.num*1800000LL - (1000LL+i)*s->avctx->time_base.den*div);
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
126 avpriv_align_put_bits(&s->pb);
127
128 /* Update the pointer to last GOB */
129 s->ptr_lastgob = put_bits_ptr(&s->pb);
130 put_bits(&s->pb, 22, 0x20); /* PSC */
131 temp_ref= s->picture_number * (int64_t)coded_frame_rate * s->avctx->time_base.num / //FIXME use timestamp
132 (coded_frame_rate_base * (int64_t)s->avctx->time_base.den);
133 put_sbits(&s->pb, 8, temp_ref); /* TemporalReference */
134
135 put_bits(&s->pb, 1, 1); /* marker */
136 put_bits(&s->pb, 1, 0); /* h263 id */
137 put_bits(&s->pb, 1, 0); /* split screen off */
138 put_bits(&s->pb, 1, 0); /* camera off */
139 put_bits(&s->pb, 1, 0); /* freeze picture release off */
140
141 format = ff_match_2uint16(ff_h263_format, FF_ARRAY_ELEMS(ff_h263_format), s->width, s->height);
142 if (!s->h263_plus) {
143 /* H.263v1 */
144 put_bits(&s->pb, 3, format);
145 put_bits(&s->pb, 1, (s->pict_type == AV_PICTURE_TYPE_P));
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 */
150 put_bits(&s->pb, 1, 0); /* SAC: off */
151 put_bits(&s->pb, 1, s->obmc); /* Advanced Prediction */
152 put_bits(&s->pb, 1, 0); /* only I/P frames, no PB frame */
153 put_bits(&s->pb, 5, s->qscale);
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
160 put_bits(&s->pb, 3, 7);
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
165 put_bits(&s->pb, 3, format);
166
167 put_bits(&s->pb,1, s->custom_pcf);
168 put_bits(&s->pb,1, s->umvplus); /* Unrestricted Motion Vector */
169 put_bits(&s->pb,1,0); /* SAC: off */
170 put_bits(&s->pb,1,s->obmc); /* Advanced Prediction Mode */
171 put_bits(&s->pb,1,s->h263_aic); /* Advanced Intra Coding */
172 put_bits(&s->pb,1,s->loop_filter); /* Deblocking Filter */
173 put_bits(&s->pb,1,s->h263_slice_structured); /* Slice Structured */
174 put_bits(&s->pb,1,0); /* Reference Picture Selection: off */
175 put_bits(&s->pb,1,0); /* Independent Segment Decoding: off */
176 put_bits(&s->pb,1,s->alt_inter_vlc); /* Alternative Inter VLC */
177 put_bits(&s->pb,1,s->modified_quant); /* Modified Quantization: */
178 put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */
179 put_bits(&s->pb,3,0); /* Reserved */
180
181 put_bits(&s->pb, 3, s->pict_type == AV_PICTURE_TYPE_P);
182
183 put_bits(&s->pb,1,0); /* Reference Picture Resampling: off */
184 put_bits(&s->pb,1,0); /* Reduced-Resolution Update: off */
185 put_bits(&s->pb,1,s->no_rounding); /* Rounding Type */
186 put_bits(&s->pb,2,0); /* Reserved */
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) */
194 s->aspect_ratio_info= ff_h263_aspect_to_info(s->avctx->sample_aspect_ratio);
195
196 put_bits(&s->pb,4,s->aspect_ratio_info);
197 put_bits(&s->pb,9,(s->width >> 2) - 1);
198 put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */
199 put_bits(&s->pb,9,(s->height >> 2));
200 if (s->aspect_ratio_info == FF_ASPECT_EXTENDED){
201 put_bits(&s->pb, 8, s->avctx->sample_aspect_ratio.num);
202 put_bits(&s->pb, 8, s->avctx->sample_aspect_ratio.den);
203 }
204 }
205 if(s->custom_pcf){
206 if(ufep){
207 put_bits(&s->pb, 1, best_clock_code);
208 put_bits(&s->pb, 7, best_divisor);
209 }
210 put_sbits(&s->pb, 2, temp_ref>>8);
211 }
212
213 /* Unlimited Unrestricted Motion Vectors Indicator (UUI) */
214 if (s->umvplus)
215 // put_bits(&s->pb,1,1); /* Limited according tables of Annex D */
216 //FIXME check actual requested range
217 put_bits(&s->pb,2,1); /* unlimited */
218 if(s->h263_slice_structured)
219 put_bits(&s->pb,2,0); /* no weird submodes */
220
221 put_bits(&s->pb, 5, s->qscale);
222 }
223
224 put_bits(&s->pb, 1, 0); /* no PEI */
225
226 if(s->h263_slice_structured){
227 put_bits(&s->pb, 1, 1);
228
229 assert(s->mb_x == 0 && s->mb_y == 0);
230 ff_h263_encode_mba(s);
231
232 put_bits(&s->pb, 1, 1);
233 }
234
235 if(s->h263_aic){
236 s->y_dc_scale_table=
237 s->c_dc_scale_table= ff_aic_dc_scale_table;
238 }else{
239 s->y_dc_scale_table=
240 s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
241 }
242 }
243
244 /**
245 * Encode a group of blocks header.
246 */
247 void ff_h263_encode_gob_header(MpegEncContext * s, int mb_line)
248 {
249 put_bits(&s->pb, 17, 1); /* GBSC */
250
251 if(s->h263_slice_structured){
252 put_bits(&s->pb, 1, 1);
253
254 ff_h263_encode_mba(s);
255
256 if(s->mb_num > 1583)
257 put_bits(&s->pb, 1, 1);
258 put_bits(&s->pb, 5, s->qscale); /* GQUANT */
259 put_bits(&s->pb, 1, 1);
260 put_bits(&s->pb, 2, s->pict_type == AV_PICTURE_TYPE_I); /* GFID */
261 }else{
262 int gob_number= mb_line / s->gob_index;
263
264 put_bits(&s->pb, 5, gob_number); /* GN */
265 put_bits(&s->pb, 2, s->pict_type == AV_PICTURE_TYPE_I); /* GFID */
266 put_bits(&s->pb, 5, s->qscale); /* GQUANT */
267 }
268 }
269
270 /**
271 * modify qscale so that encoding is acually possible in h263 (limit difference to -2..2)
272 */
273 void ff_clean_h263_qscales(MpegEncContext *s){
274 int i;
275 int8_t * const qscale_table = s->current_picture.qscale_table;
276
277 ff_init_qscale_tab(s);
278
279 for(i=1; i<s->mb_num; i++){
280 if(qscale_table[ s->mb_index2xy[i] ] - qscale_table[ s->mb_index2xy[i-1] ] >2)
281 qscale_table[ s->mb_index2xy[i] ]= qscale_table[ s->mb_index2xy[i-1] ]+2;
282 }
283 for(i=s->mb_num-2; i>=0; i--){
284 if(qscale_table[ s->mb_index2xy[i] ] - qscale_table[ s->mb_index2xy[i+1] ] >2)
285 qscale_table[ s->mb_index2xy[i] ]= qscale_table[ s->mb_index2xy[i+1] ]+2;
286 }
287
288 if(s->codec_id != AV_CODEC_ID_H263P){
289 for(i=1; i<s->mb_num; i++){
290 int mb_xy= s->mb_index2xy[i];
291
292 if(qscale_table[mb_xy] != qscale_table[s->mb_index2xy[i-1]] && (s->mb_type[mb_xy]&CANDIDATE_MB_TYPE_INTER4V)){
293 s->mb_type[mb_xy]|= CANDIDATE_MB_TYPE_INTER;
294 }
295 }
296 }
297 }
298
299 static const int dquant_code[5]= {1,0,9,2,3};
300
301 /**
302 * Encode an 8x8 block.
303 * @param block the 8x8 block
304 * @param n block index (0-3 are luma, 4-5 are chroma)
305 */
306 static void h263_encode_block(MpegEncContext * s, int16_t * block, int n)
307 {
308 int level, run, last, i, j, last_index, last_non_zero, sign, slevel, code;
309 RLTable *rl;
310
311 rl = &ff_h263_rl_inter;
312 if (s->mb_intra && !s->h263_aic) {
313 /* DC coef */
314 level = block[0];
315 /* 255 cannot be represented, so we clamp */
316 if (level > 254) {
317 level = 254;
318 block[0] = 254;
319 }
320 /* 0 cannot be represented also */
321 else if (level < 1) {
322 level = 1;
323 block[0] = 1;
324 }
325 if (level == 128) //FIXME check rv10
326 put_bits(&s->pb, 8, 0xff);
327 else
328 put_bits(&s->pb, 8, level);
329 i = 1;
330 } else {
331 i = 0;
332 if (s->h263_aic && s->mb_intra)
333 rl = &ff_rl_intra_aic;
334
335 if(s->alt_inter_vlc && !s->mb_intra){
336 int aic_vlc_bits=0;
337 int inter_vlc_bits=0;
338 int wrong_pos=-1;
339 int aic_code;
340
341 last_index = s->block_last_index[n];
342 last_non_zero = i - 1;
343 for (; i <= last_index; i++) {
344 j = s->intra_scantable.permutated[i];
345 level = block[j];
346 if (level) {
347 run = i - last_non_zero - 1;
348 last = (i == last_index);
349
350 if(level<0) level= -level;
351
352 code = get_rl_index(rl, last, run, level);
353 aic_code = get_rl_index(&ff_rl_intra_aic, last, run, level);
354 inter_vlc_bits += rl->table_vlc[code][1]+1;
355 aic_vlc_bits += ff_rl_intra_aic.table_vlc[aic_code][1]+1;
356
357 if (code == rl->n) {
358 inter_vlc_bits += 1+6+8-1;
359 }
360 if (aic_code == ff_rl_intra_aic.n) {
361 aic_vlc_bits += 1+6+8-1;
362 wrong_pos += run + 1;
363 }else
364 wrong_pos += wrong_run[aic_code];
365 last_non_zero = i;
366 }
367 }
368 i = 0;
369 if(aic_vlc_bits < inter_vlc_bits && wrong_pos > 63)
370 rl = &ff_rl_intra_aic;
371 }
372 }
373
374 /* AC coefs */
375 last_index = s->block_last_index[n];
376 last_non_zero = i - 1;
377 for (; i <= last_index; i++) {
378 j = s->intra_scantable.permutated[i];
379 level = block[j];
380 if (level) {
381 run = i - last_non_zero - 1;
382 last = (i == last_index);
383 sign = 0;
384 slevel = level;
385 if (level < 0) {
386 sign = 1;
387 level = -level;
388 }
389 code = get_rl_index(rl, last, run, level);
390 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
391 if (code == rl->n) {
392 if(!CONFIG_FLV_ENCODER || s->h263_flv <= 1){
393 put_bits(&s->pb, 1, last);
394 put_bits(&s->pb, 6, run);
395
396 assert(slevel != 0);
397
398 if(level < 128)
399 put_sbits(&s->pb, 8, slevel);
400 else{
401 put_bits(&s->pb, 8, 128);
402 put_sbits(&s->pb, 5, slevel);
403 put_sbits(&s->pb, 6, slevel>>5);
404 }
405 }else{
406 ff_flv2_encode_ac_esc(&s->pb, slevel, level, run, last);
407 }
408 } else {
409 put_bits(&s->pb, 1, sign);
410 }
411 last_non_zero = i;
412 }
413 }
414 }
415
416 /* Encode MV differences on H.263+ with Unrestricted MV mode */
417 static void h263p_encode_umotion(MpegEncContext * s, int val)
418 {
419 short sval = 0;
420 short i = 0;
421 short n_bits = 0;
422 short temp_val;
423 int code = 0;
424 int tcode;
425
426 if ( val == 0)
427 put_bits(&s->pb, 1, 1);
428 else if (val == 1)
429 put_bits(&s->pb, 3, 0);
430 else if (val == -1)
431 put_bits(&s->pb, 3, 2);
432 else {
433
434 sval = ((val < 0) ? (short)(-val):(short)val);
435 temp_val = sval;
436
437 while (temp_val != 0) {
438 temp_val = temp_val >> 1;
439 n_bits++;
440 }
441
442 i = n_bits - 1;
443 while (i > 0) {
444 tcode = (sval & (1 << (i-1))) >> (i-1);
445 tcode = (tcode << 1) | 1;
446 code = (code << 2) | tcode;
447 i--;
448 }
449 code = ((code << 1) | (val < 0)) << 1;
450 put_bits(&s->pb, (2*n_bits)+1, code);
451 }
452 }
453
454 void ff_h263_encode_mb(MpegEncContext * s,
455 int16_t block[6][64],
456 int motion_x, int motion_y)
457 {
458 int cbpc, cbpy, i, cbp, pred_x, pred_y;
459 int16_t pred_dc;
460 int16_t rec_intradc[6];
461 int16_t *dc_ptr[6];
462 const int interleaved_stats = s->avctx->flags & CODEC_FLAG_PASS1;
463
464 if (!s->mb_intra) {
465 /* compute cbp */
466 cbp= get_p_cbp(s, block, motion_x, motion_y);
467
468 if ((cbp | motion_x | motion_y | s->dquant | (s->mv_type - MV_TYPE_16X16)) == 0) {
469 /* skip macroblock */
470 put_bits(&s->pb, 1, 1);
471 if(interleaved_stats){
472 s->misc_bits++;
473 s->last_bits++;
474 }
475 s->skip_count++;
476
477 return;
478 }
479 put_bits(&s->pb, 1, 0); /* mb coded */
480
481 cbpc = cbp & 3;
482 cbpy = cbp >> 2;
483 if(s->alt_inter_vlc==0 || cbpc!=3)
484 cbpy ^= 0xF;
485 if(s->dquant) cbpc+= 8;
486 if(s->mv_type==MV_TYPE_16X16){
487 put_bits(&s->pb,
488 ff_h263_inter_MCBPC_bits[cbpc],
489 ff_h263_inter_MCBPC_code[cbpc]);
490
491 put_bits(&s->pb, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]);
492 if(s->dquant)
493 put_bits(&s->pb, 2, dquant_code[s->dquant+2]);
494
495 if(interleaved_stats){
496 s->misc_bits+= get_bits_diff(s);
497 }
498
499 /* motion vectors: 16x16 mode */
500 ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
501
502 if (!s->umvplus) {
503 ff_h263_encode_motion_vector(s, motion_x - pred_x,
504 motion_y - pred_y, 1);
505 }
506 else {
507 h263p_encode_umotion(s, motion_x - pred_x);
508 h263p_encode_umotion(s, motion_y - pred_y);
509 if (((motion_x - pred_x) == 1) && ((motion_y - pred_y) == 1))
510 /* To prevent Start Code emulation */
511 put_bits(&s->pb,1,1);
512 }
513 }else{
514 put_bits(&s->pb,
515 ff_h263_inter_MCBPC_bits[cbpc+16],
516 ff_h263_inter_MCBPC_code[cbpc+16]);
517 put_bits(&s->pb, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]);
518 if(s->dquant)
519 put_bits(&s->pb, 2, dquant_code[s->dquant+2]);
520
521 if(interleaved_stats){
522 s->misc_bits+= get_bits_diff(s);
523 }
524
525 for(i=0; i<4; i++){
526 /* motion vectors: 8x8 mode*/
527 ff_h263_pred_motion(s, i, 0, &pred_x, &pred_y);
528
529 motion_x = s->current_picture.motion_val[0][s->block_index[i]][0];
530 motion_y = s->current_picture.motion_val[0][s->block_index[i]][1];
531 if (!s->umvplus) {
532 ff_h263_encode_motion_vector(s, motion_x - pred_x,
533 motion_y - pred_y, 1);
534 }
535 else {
536 h263p_encode_umotion(s, motion_x - pred_x);
537 h263p_encode_umotion(s, motion_y - pred_y);
538 if (((motion_x - pred_x) == 1) && ((motion_y - pred_y) == 1))
539 /* To prevent Start Code emulation */
540 put_bits(&s->pb,1,1);
541 }
542 }
543 }
544
545 if(interleaved_stats){
546 s->mv_bits+= get_bits_diff(s);
547 }
548 } else {
549 assert(s->mb_intra);
550
551 cbp = 0;
552 if (s->h263_aic) {
553 /* Predict DC */
554 for(i=0; i<6; i++) {
555 int16_t level = block[i][0];
556 int scale;
557
558 if(i<4) scale= s->y_dc_scale;
559 else scale= s->c_dc_scale;
560
561 pred_dc = ff_h263_pred_dc(s, i, &dc_ptr[i]);
562 level -= pred_dc;
563 /* Quant */
564 if (level >= 0)
565 level = (level + (scale>>1))/scale;
566 else
567 level = (level - (scale>>1))/scale;
568
569 if(!s->modified_quant){
570 if (level < -127)
571 level = -127;
572 else if (level > 127)
573 level = 127;
574 }
575
576 block[i][0] = level;
577 /* Reconstruction */
578 rec_intradc[i] = scale*level + pred_dc;
579 /* Oddify */
580 rec_intradc[i] |= 1;
581 //if ((rec_intradc[i] % 2) == 0)
582 // rec_intradc[i]++;
583 /* Clipping */
584 if (rec_intradc[i] < 0)
585 rec_intradc[i] = 0;
586 else if (rec_intradc[i] > 2047)
587 rec_intradc[i] = 2047;
588
589 /* Update AC/DC tables */
590 *dc_ptr[i] = rec_intradc[i];
591 /* AIC can change CBP */
592 if (s->block_last_index[i] > 0 ||
593 (s->block_last_index[i] == 0 && level !=0))
594 cbp |= 1 << (5 - i);
595 }
596 }else{
597 for(i=0; i<6; i++) {
598 /* compute cbp */
599 if (s->block_last_index[i] >= 1)
600 cbp |= 1 << (5 - i);
601 }
602 }
603
604 cbpc = cbp & 3;
605 if (s->pict_type == AV_PICTURE_TYPE_I) {
606 if(s->dquant) cbpc+=4;
607 put_bits(&s->pb,
608 ff_h263_intra_MCBPC_bits[cbpc],
609 ff_h263_intra_MCBPC_code[cbpc]);
610 } else {
611 if(s->dquant) cbpc+=8;
612 put_bits(&s->pb, 1, 0); /* mb coded */
613 put_bits(&s->pb,
614 ff_h263_inter_MCBPC_bits[cbpc + 4],
615 ff_h263_inter_MCBPC_code[cbpc + 4]);
616 }
617 if (s->h263_aic) {
618 /* XXX: currently, we do not try to use ac prediction */
619 put_bits(&s->pb, 1, 0); /* no AC prediction */
620 }
621 cbpy = cbp >> 2;
622 put_bits(&s->pb, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]);
623 if(s->dquant)
624 put_bits(&s->pb, 2, dquant_code[s->dquant+2]);
625
626 if(interleaved_stats){
627 s->misc_bits+= get_bits_diff(s);
628 }
629 }
630
631 for(i=0; i<6; i++) {
632 /* encode each block */
633 h263_encode_block(s, block[i], i);
634
635 /* Update INTRADC for decoding */
636 if (s->h263_aic && s->mb_intra) {
637 block[i][0] = rec_intradc[i];
638
639 }
640 }
641
642 if(interleaved_stats){
643 if (!s->mb_intra) {
644 s->p_tex_bits+= get_bits_diff(s);
645 s->f_count++;
646 }else{
647 s->i_tex_bits+= get_bits_diff(s);
648 s->i_count++;
649 }
650 }
651 }
652
653 void ff_h263_encode_motion(MpegEncContext * s, int val, int f_code)
654 {
655 int range, bit_size, sign, code, bits;
656
657 if (val == 0) {
658 /* zero vector */
659 code = 0;
660 put_bits(&s->pb, ff_mvtab[code][1], ff_mvtab[code][0]);
661 } else {
662 bit_size = f_code - 1;
663 range = 1 << bit_size;
664 /* modulo encoding */
665 val = sign_extend(val, 6 + bit_size);
666 sign = val>>31;
667 val= (val^sign)-sign;
668 sign&=1;
669
670 val--;
671 code = (val >> bit_size) + 1;
672 bits = val & (range - 1);
673
674 put_bits(&s->pb, ff_mvtab[code][1] + 1, (ff_mvtab[code][0] << 1) | sign);
675 if (bit_size > 0) {
676 put_bits(&s->pb, bit_size, bits);
677 }
678 }
679 }
680
681 static av_cold void init_mv_penalty_and_fcode(MpegEncContext *s)
682 {
683 int f_code;
684 int mv;
685
686 for(f_code=1; f_code<=MAX_FCODE; f_code++){
687 for(mv=-MAX_MV; mv<=MAX_MV; mv++){
688 int len;
689
690 if(mv==0) len= ff_mvtab[0][1];
691 else{
692 int val, bit_size, code;
693
694 bit_size = f_code - 1;
695
696 val=mv;
697 if (val < 0)
698 val = -val;
699 val--;
700 code = (val >> bit_size) + 1;
701 if(code<33){
702 len= ff_mvtab[code][1] + 1 + bit_size;
703 }else{
704 len= ff_mvtab[32][1] + av_log2(code>>5) + 2 + bit_size;
705 }
706 }
707
708 mv_penalty[f_code][mv+MAX_MV]= len;
709 }
710 }
711
712 for(f_code=MAX_FCODE; f_code>0; f_code--){
713 for(mv=-(16<<f_code); mv<(16<<f_code); mv++){
714 fcode_tab[mv+MAX_MV]= f_code;
715 }
716 }
717
718 for(mv=0; mv<MAX_MV*2+1; mv++){
719 umv_fcode_tab[mv]= 1;
720 }
721 }
722
723 static av_cold void init_uni_h263_rl_tab(RLTable *rl, uint32_t *bits_tab,
724 uint8_t *len_tab)
725 {
726 int slevel, run, last;
727
728 assert(MAX_LEVEL >= 64);
729 assert(MAX_RUN >= 63);
730
731 for(slevel=-64; slevel<64; slevel++){
732 if(slevel==0) continue;
733 for(run=0; run<64; run++){
734 for(last=0; last<=1; last++){
735 const int index= UNI_MPEG4_ENC_INDEX(last, run, slevel+64);
736 int level= slevel < 0 ? -slevel : slevel;
737 int sign= slevel < 0 ? 1 : 0;
738 int bits, len, code;
739
740 len_tab[index]= 100;
741
742 /* ESC0 */
743 code= get_rl_index(rl, last, run, level);
744 bits= rl->table_vlc[code][0];
745 len= rl->table_vlc[code][1];
746 bits=bits*2+sign; len++;
747
748 if(code!=rl->n && len < len_tab[index]){
749 if(bits_tab) bits_tab[index]= bits;
750 len_tab [index]= len;
751 }
752 /* ESC */
753 bits= rl->table_vlc[rl->n][0];
754 len = rl->table_vlc[rl->n][1];
755 bits=bits*2+last; len++;
756 bits=bits*64+run; len+=6;
757 bits=bits*256+(level&0xff); len+=8;
758
759 if(len < len_tab[index]){
760 if(bits_tab) bits_tab[index]= bits;
761 len_tab [index]= len;
762 }
763 }
764 }
765 }
766 }
767
768 av_cold void ff_h263_encode_init(MpegEncContext *s)
769 {
770 static int done = 0;
771
772 if (!done) {
773 done = 1;
774
775 ff_rl_init(&ff_h263_rl_inter, ff_h263_static_rl_table_store[0]);
776 ff_rl_init(&ff_rl_intra_aic, ff_h263_static_rl_table_store[1]);
777
778 init_uni_h263_rl_tab(&ff_rl_intra_aic, NULL, uni_h263_intra_aic_rl_len);
779 init_uni_h263_rl_tab(&ff_h263_rl_inter , NULL, uni_h263_inter_rl_len);
780
781 init_mv_penalty_and_fcode(s);
782 }
783 s->me.mv_penalty= mv_penalty; //FIXME exact table for msmpeg4 & h263p
784
785 s->intra_ac_vlc_length =s->inter_ac_vlc_length = uni_h263_inter_rl_len;
786 s->intra_ac_vlc_last_length=s->inter_ac_vlc_last_length= uni_h263_inter_rl_len + 128*64;
787 if(s->h263_aic){
788 s->intra_ac_vlc_length = uni_h263_intra_aic_rl_len;
789 s->intra_ac_vlc_last_length= uni_h263_intra_aic_rl_len + 128*64;
790 }
791 s->ac_esc_length= 7+1+6+8;
792
793 // use fcodes >1 only for mpeg4 & h263 & h263p FIXME
794 switch(s->codec_id){
795 case AV_CODEC_ID_MPEG4:
796 s->fcode_tab= fcode_tab;
797 break;
798 case AV_CODEC_ID_H263P:
799 if(s->umvplus)
800 s->fcode_tab= umv_fcode_tab;
801 if(s->modified_quant){
802 s->min_qcoeff= -2047;
803 s->max_qcoeff= 2047;
804 }else{
805 s->min_qcoeff= -127;
806 s->max_qcoeff= 127;
807 }
808 break;
809 //Note for mpeg4 & h263 the dc-scale table will be set per frame as needed later
810 case AV_CODEC_ID_FLV1:
811 if (s->h263_flv > 1) {
812 s->min_qcoeff= -1023;
813 s->max_qcoeff= 1023;
814 } else {
815 s->min_qcoeff= -127;
816 s->max_qcoeff= 127;
817 }
818 s->y_dc_scale_table=
819 s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
820 break;
821 default: //nothing needed - default table already set in mpegvideo.c
822 s->min_qcoeff= -127;
823 s->max_qcoeff= 127;
824 s->y_dc_scale_table=
825 s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
826 }
827 }
828
829 void ff_h263_encode_mba(MpegEncContext *s)
830 {
831 int i, mb_pos;
832
833 for(i=0; i<6; i++){
834 if(s->mb_num-1 <= ff_mba_max[i]) break;
835 }
836 mb_pos= s->mb_x + s->mb_width*s->mb_y;
837 put_bits(&s->pb, ff_mba_length[i], mb_pos);
838 }