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