Statistics
| Branch: | Revision:

ffmpeg / libavcodec / ituh263enc.c @ c46eeae2

History | View | Annotate | Download (26.6 KB)

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 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 libavcodec/ituh263enc.c
27
 * h263 encoder.
28
 */
29

    
30
//#define DEBUG
31
#include <limits.h>
32

    
33
#include "dsputil.h"
34
#include "avcodec.h"
35
#include "mpegvideo.h"
36
#include "h263.h"
37
#include "mathops.h"
38
#include "unary.h"
39
#include "flv.h"
40
#include "mpeg4video.h"
41

    
42
//#undef NDEBUG
43
//#include <assert.h>
44

    
45
/**
46
 * Table of number of bits a motion vector component needs.
47
 */
48
static uint8_t mv_penalty[MAX_FCODE+1][MAX_MV*2+1];
49

    
50
/**
51
 * Minimal fcode that a motion vector component would need.
52
 */
53
static 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
 */
59
static 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.
63
static uint8_t  uni_h263_intra_aic_rl_len [64*64*2*2];
64
static 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

    
69
static const uint8_t wrong_run[102] = {
70
 1,  2,  3,  5,  4, 10,  9,  8,
71
11, 15, 17, 16, 23, 22, 21, 20,
72
19, 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,
77
10,  1,  1,  5,  9, 17, 25, 24,
78
29, 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,
81
11, 12, 13, 14,  1, 21, 20, 18,
82
19,  2,  1, 34, 35, 36
83
};
84

    
85
/**
86
 * Returns the 4 bit value that specifies the given aspect ratio.
87
 * This may be one of the standard aspect ratios or it specifies
88
 * that the aspect will be stored explicitly later.
89
 */
90
av_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

    
104
void h263_encode_picture_header(MpegEncContext * s, int picture_number)
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

    
128
    align_put_bits(&s->pb);
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

    
143
    format = ff_match_2uint16(h263_format, FF_ARRAY_ELEMS(h263_format), s->width, s->height);
144
    if (!s->h263_plus) {
145
        /* H.263v1 */
146
        put_bits(&s->pb, 3, format);
147
        put_bits(&s->pb, 1, (s->pict_type == FF_P_TYPE));
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 */
164
        if (format == 7)
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

    
183
        put_bits(&s->pb, 3, s->pict_type == FF_P_TYPE);
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

    
194
                if (format == 7) {
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
/**
247
 * Encodes a group of blocks header.
248
 */
249
void h263_encode_gob_header(MpegEncContext * s, int mb_line)
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);
262
        put_bits(&s->pb, 2, s->pict_type == FF_I_TYPE); /* GFID */
263
    }else{
264
        int gob_number= mb_line / s->gob_index;
265

    
266
        put_bits(&s->pb, 5, gob_number); /* GN */
267
        put_bits(&s->pb, 2, s->pict_type == FF_I_TYPE); /* GFID */
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
 */
275
void ff_clean_h263_qscales(MpegEncContext *s){
276
    int i;
277
    int8_t * const qscale_table= s->current_picture.qscale_table;
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

    
290
    if(s->codec_id != CODEC_ID_H263P){
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

    
301
static const int dquant_code[5]= {1,0,9,2,3};
302

    
303
/**
304
 * encodes a 8x8 block.
305
 * @param block the 8x8 block
306
 * @param n block index (0-3 are luma, 4-5 are chroma)
307
 */
308
static void h263_encode_block(MpegEncContext * s, DCTELEM * block, int n)
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)
335
            rl = &rl_intra_aic;
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);
355
                    aic_code = get_rl_index(&rl_intra_aic, last, run, level);
356
                    inter_vlc_bits += rl->table_vlc[code][1]+1;
357
                    aic_vlc_bits   += rl_intra_aic.table_vlc[aic_code][1]+1;
358

    
359
                    if (code == rl->n) {
360
                        inter_vlc_bits += 1+6+8-1;
361
                    }
362
                    if (aic_code == rl_intra_aic.n) {
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)
372
                rl = &rl_intra_aic;
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 */
419
static 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

    
456
void h263_encode_mb(MpegEncContext * s,
457
                    DCTELEM block[6][64],
458
                    int motion_x, int motion_y)
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];
464
    const int interleaved_stats= (s->flags&CODEC_FLAG_PASS1);
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 */
502
            h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
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*/
529
                h263_pred_motion(s, i, 0, &pred_x, &pred_y);
530

    
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];
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

    
563
                pred_dc = h263_pred_dc(s, i, &dc_ptr[i]);
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

    
571
                /* AIC can change CBP */
572
                if (level == 0 && s->block_last_index[i] == 0)
573
                    s->block_last_index[i] = -1;
574

    
575
                if(!s->modified_quant){
576
                    if (level < -127)
577
                        level = -127;
578
                    else if (level > 127)
579
                        level = 127;
580
                }
581

    
582
                block[i][0] = level;
583
                /* Reconstruction */
584
                rec_intradc[i] = scale*level + pred_dc;
585
                /* Oddify */
586
                rec_intradc[i] |= 1;
587
                //if ((rec_intradc[i] % 2) == 0)
588
                //    rec_intradc[i]++;
589
                /* Clipping */
590
                if (rec_intradc[i] < 0)
591
                    rec_intradc[i] = 0;
592
                else if (rec_intradc[i] > 2047)
593
                    rec_intradc[i] = 2047;
594

    
595
                /* Update AC/DC tables */
596
                *dc_ptr[i] = rec_intradc[i];
597
                if (s->block_last_index[i] >= 0)
598
                    cbp |= 1 << (5 - i);
599
            }
600
        }else{
601
            for(i=0; i<6; i++) {
602
                /* compute cbp */
603
                if (s->block_last_index[i] >= 1)
604
                    cbp |= 1 << (5 - i);
605
            }
606
        }
607

    
608
        cbpc = cbp & 3;
609
        if (s->pict_type == FF_I_TYPE) {
610
            if(s->dquant) cbpc+=4;
611
            put_bits(&s->pb,
612
                ff_h263_intra_MCBPC_bits[cbpc],
613
                ff_h263_intra_MCBPC_code[cbpc]);
614
        } else {
615
            if(s->dquant) cbpc+=8;
616
            put_bits(&s->pb, 1, 0);     /* mb coded */
617
            put_bits(&s->pb,
618
                ff_h263_inter_MCBPC_bits[cbpc + 4],
619
                ff_h263_inter_MCBPC_code[cbpc + 4]);
620
        }
621
        if (s->h263_aic) {
622
            /* XXX: currently, we do not try to use ac prediction */
623
            put_bits(&s->pb, 1, 0);     /* no AC prediction */
624
        }
625
        cbpy = cbp >> 2;
626
        put_bits(&s->pb, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]);
627
        if(s->dquant)
628
            put_bits(&s->pb, 2, dquant_code[s->dquant+2]);
629

    
630
        if(interleaved_stats){
631
            s->misc_bits+= get_bits_diff(s);
632
        }
633
    }
634

    
635
    for(i=0; i<6; i++) {
636
        /* encode each block */
637
        h263_encode_block(s, block[i], i);
638

    
639
        /* Update INTRADC for decoding */
640
        if (s->h263_aic && s->mb_intra) {
641
            block[i][0] = rec_intradc[i];
642

    
643
        }
644
    }
645

    
646
    if(interleaved_stats){
647
        if (!s->mb_intra) {
648
            s->p_tex_bits+= get_bits_diff(s);
649
            s->f_count++;
650
        }else{
651
            s->i_tex_bits+= get_bits_diff(s);
652
            s->i_count++;
653
        }
654
    }
655
}
656

    
657
void ff_h263_encode_motion(MpegEncContext * s, int val, int f_code)
658
{
659
    int range, l, bit_size, sign, code, bits;
660

    
661
    if (val == 0) {
662
        /* zero vector */
663
        code = 0;
664
        put_bits(&s->pb, mvtab[code][1], mvtab[code][0]);
665
    } else {
666
        bit_size = f_code - 1;
667
        range = 1 << bit_size;
668
        /* modulo encoding */
669
        l= INT_BIT - 6 - bit_size;
670
        val = (val<<l)>>l;
671
        sign = val>>31;
672
        val= (val^sign)-sign;
673
        sign&=1;
674

    
675
        val--;
676
        code = (val >> bit_size) + 1;
677
        bits = val & (range - 1);
678

    
679
        put_bits(&s->pb, mvtab[code][1] + 1, (mvtab[code][0] << 1) | sign);
680
        if (bit_size > 0) {
681
            put_bits(&s->pb, bit_size, bits);
682
        }
683
    }
684
}
685

    
686
static void init_mv_penalty_and_fcode(MpegEncContext *s)
687
{
688
    int f_code;
689
    int mv;
690

    
691
    for(f_code=1; f_code<=MAX_FCODE; f_code++){
692
        for(mv=-MAX_MV; mv<=MAX_MV; mv++){
693
            int len;
694

    
695
            if(mv==0) len= mvtab[0][1];
696
            else{
697
                int val, bit_size, code;
698

    
699
                bit_size = f_code - 1;
700

    
701
                val=mv;
702
                if (val < 0)
703
                    val = -val;
704
                val--;
705
                code = (val >> bit_size) + 1;
706
                if(code<33){
707
                    len= mvtab[code][1] + 1 + bit_size;
708
                }else{
709
                    len= mvtab[32][1] + av_log2(code>>5) + 2 + bit_size;
710
                }
711
            }
712

    
713
            mv_penalty[f_code][mv+MAX_MV]= len;
714
        }
715
    }
716

    
717
    for(f_code=MAX_FCODE; f_code>0; f_code--){
718
        for(mv=-(16<<f_code); mv<(16<<f_code); mv++){
719
            fcode_tab[mv+MAX_MV]= f_code;
720
        }
721
    }
722

    
723
    for(mv=0; mv<MAX_MV*2+1; mv++){
724
        umv_fcode_tab[mv]= 1;
725
    }
726
}
727

    
728
static void init_uni_h263_rl_tab(RLTable *rl, uint32_t *bits_tab, uint8_t *len_tab){
729
    int slevel, run, last;
730

    
731
    assert(MAX_LEVEL >= 64);
732
    assert(MAX_RUN   >= 63);
733

    
734
    for(slevel=-64; slevel<64; slevel++){
735
        if(slevel==0) continue;
736
        for(run=0; run<64; run++){
737
            for(last=0; last<=1; last++){
738
                const int index= UNI_MPEG4_ENC_INDEX(last, run, slevel+64);
739
                int level= slevel < 0 ? -slevel : slevel;
740
                int sign= slevel < 0 ? 1 : 0;
741
                int bits, len, code;
742

    
743
                len_tab[index]= 100;
744

    
745
                /* ESC0 */
746
                code= get_rl_index(rl, last, run, level);
747
                bits= rl->table_vlc[code][0];
748
                len=  rl->table_vlc[code][1];
749
                bits=bits*2+sign; len++;
750

    
751
                if(code!=rl->n && len < len_tab[index]){
752
                    if(bits_tab) bits_tab[index]= bits;
753
                    len_tab [index]= len;
754
                }
755
                /* ESC */
756
                bits= rl->table_vlc[rl->n][0];
757
                len = rl->table_vlc[rl->n][1];
758
                bits=bits*2+last; len++;
759
                bits=bits*64+run; len+=6;
760
                bits=bits*256+(level&0xff); len+=8;
761

    
762
                if(len < len_tab[index]){
763
                    if(bits_tab) bits_tab[index]= bits;
764
                    len_tab [index]= len;
765
                }
766
            }
767
        }
768
    }
769
}
770

    
771
void h263_encode_init(MpegEncContext *s)
772
{
773
    static int done = 0;
774

    
775
    if (!done) {
776
        done = 1;
777

    
778
        init_rl(&ff_h263_rl_inter, ff_h263_static_rl_table_store[0]);
779
        init_rl(&rl_intra_aic, ff_h263_static_rl_table_store[1]);
780

    
781
        init_uni_h263_rl_tab(&rl_intra_aic, NULL, uni_h263_intra_aic_rl_len);
782
        init_uni_h263_rl_tab(&ff_h263_rl_inter    , NULL, uni_h263_inter_rl_len);
783

    
784
        init_mv_penalty_and_fcode(s);
785
    }
786
    s->me.mv_penalty= mv_penalty; //FIXME exact table for msmpeg4 & h263p
787

    
788
    s->intra_ac_vlc_length     =s->inter_ac_vlc_length     = uni_h263_inter_rl_len;
789
    s->intra_ac_vlc_last_length=s->inter_ac_vlc_last_length= uni_h263_inter_rl_len + 128*64;
790
    if(s->h263_aic){
791
        s->intra_ac_vlc_length     = uni_h263_intra_aic_rl_len;
792
        s->intra_ac_vlc_last_length= uni_h263_intra_aic_rl_len + 128*64;
793
    }
794
    s->ac_esc_length= 7+1+6+8;
795

    
796
    // use fcodes >1 only for mpeg4 & h263 & h263p FIXME
797
    switch(s->codec_id){
798
    case CODEC_ID_MPEG4:
799
        s->fcode_tab= fcode_tab;
800
        break;
801
    case CODEC_ID_H263P:
802
        if(s->umvplus)
803
            s->fcode_tab= umv_fcode_tab;
804
        if(s->modified_quant){
805
            s->min_qcoeff= -2047;
806
            s->max_qcoeff=  2047;
807
        }else{
808
            s->min_qcoeff= -127;
809
            s->max_qcoeff=  127;
810
        }
811
        break;
812
        //Note for mpeg4 & h263 the dc-scale table will be set per frame as needed later
813
    case CODEC_ID_FLV1:
814
        if (s->h263_flv > 1) {
815
            s->min_qcoeff= -1023;
816
            s->max_qcoeff=  1023;
817
        } else {
818
            s->min_qcoeff= -127;
819
            s->max_qcoeff=  127;
820
        }
821
        s->y_dc_scale_table=
822
        s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
823
        break;
824
    default: //nothing needed - default table already set in mpegvideo.c
825
        s->min_qcoeff= -127;
826
        s->max_qcoeff=  127;
827
        s->y_dc_scale_table=
828
        s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
829
    }
830
}
831

    
832
void ff_h263_encode_mba(MpegEncContext *s)
833
{
834
    int i, mb_pos;
835

    
836
    for(i=0; i<6; i++){
837
        if(s->mb_num-1 <= ff_mba_max[i]) break;
838
    }
839
    mb_pos= s->mb_x + s->mb_width*s->mb_y;
840
    put_bits(&s->pb, ff_mba_length[i], mb_pos);
841
}