GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/msmpeg4enc.c Lines: 364 381 95.5 %
Date: 2021-04-20 04:37:23 Branches: 185 220 84.1 %

Line Branch Exec Source
1
/*
2
 * MSMPEG4 encoder backend
3
 * Copyright (c) 2001 Fabrice Bellard
4
 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
5
 *
6
 * msmpeg4v1 & v2 stuff by 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
 * MSMPEG4 encoder backend
28
 */
29
30
#include <stdint.h>
31
#include <string.h>
32
33
#include "libavutil/attributes.h"
34
#include "libavutil/avutil.h"
35
#include "mpegvideo.h"
36
#include "h263.h"
37
#include "internal.h"
38
#include "mpeg4video.h"
39
#include "msmpeg4.h"
40
#include "msmpeg4data.h"
41
#include "put_bits.h"
42
#include "rl.h"
43
#include "vc1data.h"
44
45
static uint8_t rl_length[NB_RL_TABLES][MAX_LEVEL+1][MAX_RUN+1][2];
46
47
/* build the table which associate a (x,y) motion vector to a vlc */
48
34
static av_cold void init_mv_table(MVTable *tab, uint16_t table_mv_index[4096])
49
{
50
    int i, x, y;
51
52
34
    tab->table_mv_index = table_mv_index;
53
54
    /* mark all entries as not used */
55
139298
    for(i=0;i<4096;i++)
56
139264
        tab->table_mv_index[i] = MSMPEG4_MV_TABLES_NB_ELEMS;
57
58
37400
    for (i = 0; i < MSMPEG4_MV_TABLES_NB_ELEMS; i++) {
59
37366
        x = tab->table_mvx[i];
60
37366
        y = tab->table_mvy[i];
61
37366
        tab->table_mv_index[(x << 6) | y] = i;
62
    }
63
34
}
64
65
1048
void ff_msmpeg4_code012(PutBitContext *pb, int n)
66
{
67
1048
    if (n == 0) {
68
587
        put_bits(pb, 1, 0);
69
    } else {
70
461
        put_bits(pb, 1, 1);
71
461
        put_bits(pb, 1, (n >= 2));
72
    }
73
1048
}
74
75
848640
static int get_size_of_code(MpegEncContext * s, RLTable *rl, int last, int run, int level, int intra){
76
848640
    int size=0;
77
    int code;
78
848640
    int run_diff= intra ? 0 : 1;
79
80
848640
    code = get_rl_index(rl, last, run, level);
81
848640
    size+= rl->table_vlc[code][1];
82
848640
    if (code == rl->n) {
83
        int level1, run1;
84
85
834411
        level1 = level - rl->max_level[last][run];
86
834411
        if (level1 < 1)
87
            goto esc2;
88
834411
        code = get_rl_index(rl, last, run, level1);
89
834411
        if (code == rl->n) {
90
820182
            esc2:
91
820182
            size++;
92
820182
            if (level > MAX_LEVEL)
93
                goto esc3;
94
820182
            run1 = run - rl->max_run[last][level] - run_diff;
95
820182
            if (run1 < 0)
96
8092
                goto esc3;
97
812090
            code = get_rl_index(rl, last, run1, level);
98
812090
            if (code == rl->n) {
99
805188
            esc3:
100
                /* third escape */
101
813280
                size+=1+1+6+8;
102
            } else {
103
                /* second escape */
104
6902
                size+= 1+1+ rl->table_vlc[code][1];
105
            }
106
        } else {
107
            /* first escape */
108
14229
            size+= 1+1+ rl->table_vlc[code][1];
109
        }
110
    } else {
111
14229
        size++;
112
    }
113
848640
    return size;
114
}
115
116
17
av_cold void ff_msmpeg4_encode_init(MpegEncContext *s)
117
{
118
    static int init_done=0;
119
    int i;
120
121
17
    ff_msmpeg4_common_init(s);
122
17
    if(s->msmpeg4_version>=4){
123
8
        s->min_qcoeff= -255;
124
8
        s->max_qcoeff=  255;
125
    }
126
127
17
    if (!init_done) {
128
        static uint16_t mv_index_tables[2][4096];
129
        /* init various encoding tables */
130
17
        init_done = 1;
131
17
        init_mv_table(&ff_mv_tables[0], mv_index_tables[0]);
132
17
        init_mv_table(&ff_mv_tables[1], mv_index_tables[1]);
133
134
119
        for(i=0;i<NB_RL_TABLES;i++)
135
102
            ff_rl_init(&ff_rl_table[i], ff_static_rl_table_store[i]);
136
137
119
        for(i=0; i<NB_RL_TABLES; i++){
138
            int level;
139
6630
            for (level = 1; level <= MAX_LEVEL; level++) {
140
                int run;
141
430848
                for(run=0; run<=MAX_RUN; run++){
142
                    int last;
143
1272960
                    for(last=0; last<2; last++){
144
848640
                        rl_length[i][level][run][last]= get_size_of_code(s, &ff_rl_table[  i], last, run, level, 0);
145
                    }
146
                }
147
            }
148
        }
149
    }
150
17
}
151
152
625
static void find_best_tables(MpegEncContext * s)
153
{
154
    int i;
155
625
    int best        = 0, best_size        = INT_MAX;
156
625
    int chroma_best = 0, best_chroma_size = INT_MAX;
157
158
2500
    for(i=0; i<3; i++){
159
        int level;
160
1875
        int chroma_size=0;
161
1875
        int size=0;
162
163
1875
        if(i>0){// ;)
164
1250
            size++;
165
1250
            chroma_size++;
166
        }
167
123750
        for(level=0; level<=MAX_LEVEL; level++){
168
            int run;
169
255678
            for(run=0; run<=MAX_RUN; run++){
170
                int last;
171
255678
                const int last_size= size + chroma_size;
172
767034
                for(last=0; last<2; last++){
173
511356
                    int inter_count       = s->ac_stats[0][0][level][run][last] + s->ac_stats[0][1][level][run][last];
174
511356
                    int intra_luma_count  = s->ac_stats[1][0][level][run][last];
175
511356
                    int intra_chroma_count= s->ac_stats[1][1][level][run][last];
176
177
511356
                    if(s->pict_type==AV_PICTURE_TYPE_I){
178
31794
                        size       += intra_luma_count  *rl_length[i  ][level][run][last];
179
31794
                        chroma_size+= intra_chroma_count*rl_length[i+3][level][run][last];
180
                    }else{
181
479562
                        size+=        intra_luma_count  *rl_length[i  ][level][run][last]
182
479562
                                     +intra_chroma_count*rl_length[i+3][level][run][last]
183
479562
                                     +inter_count       *rl_length[i+3][level][run][last];
184
                    }
185
                }
186
255678
                if(last_size == size+chroma_size) break;
187
            }
188
        }
189
1875
        if(size<best_size){
190
1435
            best_size= size;
191
1435
            best= i;
192
        }
193
1875
        if(chroma_size<best_chroma_size){
194
667
            best_chroma_size= chroma_size;
195
667
            chroma_best= i;
196
        }
197
    }
198
199
625
    if(s->pict_type==AV_PICTURE_TYPE_P) chroma_best= best;
200
201
625
    memset(s->ac_stats, 0, sizeof(int)*(MAX_LEVEL+1)*(MAX_RUN+1)*2*2*2);
202
203
625
    s->rl_table_index       =        best;
204
625
    s->rl_chroma_table_index= chroma_best;
205
206
625
    if(s->pict_type != s->last_non_b_pict_type){
207
125
        s->rl_table_index= 2;
208
125
        if(s->pict_type==AV_PICTURE_TYPE_I)
209
63
            s->rl_chroma_table_index= 1;
210
        else
211
62
            s->rl_chroma_table_index= 2;
212
    }
213
214
625
}
215
216
/* write MSMPEG4 compatible frame header */
217
625
void ff_msmpeg4_encode_picture_header(MpegEncContext * s, int picture_number)
218
{
219
625
    find_best_tables(s);
220
221
625
    align_put_bits(&s->pb);
222
625
    put_bits(&s->pb, 2, s->pict_type - 1);
223
224
625
    put_bits(&s->pb, 5, s->qscale);
225
625
    if(s->msmpeg4_version<=2){
226
200
        s->rl_table_index = 2;
227
200
        s->rl_chroma_table_index = 2;
228
    }
229
230
625
    s->dc_table_index = 1;
231
625
    s->mv_table_index = 1; /* only if P-frame */
232
625
    s->use_skip_mb_code = 1; /* only if P-frame */
233
625
    s->per_mb_rl_table = 0;
234
625
    if(s->msmpeg4_version==4)
235

200
        s->inter_intra_pred= (s->width*s->height < 320*240 && s->bit_rate<=II_BITRATE && s->pict_type==AV_PICTURE_TYPE_P);
236
    ff_dlog(s, "%d %"PRId64" %d %d %d\n", s->pict_type, s->bit_rate,
237
            s->inter_intra_pred, s->width, s->height);
238
239
625
    if (s->pict_type == AV_PICTURE_TYPE_I) {
240
63
        s->slice_height= s->mb_height/1;
241
63
        put_bits(&s->pb, 5, 0x16 + s->mb_height/s->slice_height);
242
243
63
        if(s->msmpeg4_version==4){
244
20
            ff_msmpeg4_encode_ext_header(s);
245
20
            if(s->bit_rate>MBAC_BITRATE)
246
20
                put_bits(&s->pb, 1, s->per_mb_rl_table);
247
        }
248
249
63
        if(s->msmpeg4_version>2){
250
43
            if(!s->per_mb_rl_table){
251
43
                ff_msmpeg4_code012(&s->pb, s->rl_chroma_table_index);
252
43
                ff_msmpeg4_code012(&s->pb, s->rl_table_index);
253
            }
254
255
43
            put_bits(&s->pb, 1, s->dc_table_index);
256
        }
257
    } else {
258
562
        put_bits(&s->pb, 1, s->use_skip_mb_code);
259
260

562
        if(s->msmpeg4_version==4 && s->bit_rate>MBAC_BITRATE)
261
180
            put_bits(&s->pb, 1, s->per_mb_rl_table);
262
263
562
        if(s->msmpeg4_version>2){
264
382
            if(!s->per_mb_rl_table)
265
382
                ff_msmpeg4_code012(&s->pb, s->rl_table_index);
266
267
382
            put_bits(&s->pb, 1, s->dc_table_index);
268
269
382
            put_bits(&s->pb, 1, s->mv_table_index);
270
        }
271
    }
272
273
625
    s->esc3_level_length= 0;
274
625
    s->esc3_run_length= 0;
275
625
}
276
277
63
void ff_msmpeg4_encode_ext_header(MpegEncContext * s)
278
{
279
63
        unsigned fps = s->avctx->time_base.den / s->avctx->time_base.num / FFMAX(s->avctx->ticks_per_frame, 1);
280
63
        put_bits(&s->pb, 5, FFMIN(fps, 31)); //yes 29.97 -> 29
281
282
63
        put_bits(&s->pb, 11, FFMIN(s->bit_rate/1024, 2047));
283
284
63
        if(s->msmpeg4_version>=3)
285
43
            put_bits(&s->pb, 1, s->flipflop_rounding);
286
        else
287
20
            av_assert0(s->flipflop_rounding==0);
288
63
}
289
290
161363
void ff_msmpeg4_encode_motion(MpegEncContext * s,
291
                                  int mx, int my)
292
{
293
    int code;
294
    MVTable *mv;
295
296
    /* modulo encoding */
297
    /* WARNING : you cannot reach all the MVs even with the modulo
298
       encoding. This is a somewhat strange compromise they took !!!  */
299
161363
    if (mx <= -64)
300
        mx += 64;
301
161363
    else if (mx >= 64)
302
        mx -= 64;
303
161363
    if (my <= -64)
304
        my += 64;
305
161363
    else if (my >= 64)
306
        my -= 64;
307
308
161363
    mx += 32;
309
161363
    my += 32;
310
161363
    mv = &ff_mv_tables[s->mv_table_index];
311
312
161363
    code = mv->table_mv_index[(mx << 6) | my];
313
161363
    put_bits(&s->pb,
314
161363
             mv->table_mv_bits[code],
315
161363
             mv->table_mv_code[code]);
316
161363
    if (code == MSMPEG4_MV_TABLES_NB_ELEMS) {
317
        /* escape : code literally */
318
859
        put_bits(&s->pb, 6, mx);
319
859
        put_bits(&s->pb, 6, my);
320
    }
321
161363
}
322
323
249300
void ff_msmpeg4_handle_slices(MpegEncContext *s){
324
249300
    if (s->mb_x == 0) {
325

11850
        if (s->slice_height && (s->mb_y % s->slice_height) == 0) {
326
825
            if(s->msmpeg4_version < 4){
327
425
                ff_mpeg4_clean_buffers(s);
328
            }
329
825
            s->first_slice_line = 1;
330
        } else {
331
11025
            s->first_slice_line = 0;
332
        }
333
    }
334
249300
}
335
336
102006
static void msmpeg4v2_encode_motion(MpegEncContext * s, int val)
337
{
338
    int range, bit_size, sign, code, bits;
339
340
102006
    if (val == 0) {
341
        /* zero vector */
342
52036
        code = 0;
343
52036
        put_bits(&s->pb, ff_mvtab[code][1], ff_mvtab[code][0]);
344
    } else {
345
49970
        bit_size = s->f_code - 1;
346
49970
        range = 1 << bit_size;
347
49970
        if (val <= -64)
348
            val += 64;
349
49970
        else if (val >= 64)
350
            val -= 64;
351
352
49970
        if (val >= 0) {
353
31351
            sign = 0;
354
        } else {
355
18619
            val = -val;
356
18619
            sign = 1;
357
        }
358
49970
        val--;
359
49970
        code = (val >> bit_size) + 1;
360
49970
        bits = val & (range - 1);
361
362
49970
        put_bits(&s->pb, ff_mvtab[code][1] + 1, (ff_mvtab[code][0] << 1) | sign);
363
49970
        if (bit_size > 0) {
364
            put_bits(&s->pb, bit_size, bits);
365
        }
366
    }
367
102006
}
368
369
189450
void ff_msmpeg4_encode_mb(MpegEncContext * s,
370
                          int16_t block[6][64],
371
                          int motion_x, int motion_y)
372
{
373
    int cbp, coded_cbp, i;
374
    int pred_x, pred_y;
375
    uint8_t *coded_block;
376
377
189450
    ff_msmpeg4_handle_slices(s);
378
379
189450
    if (!s->mb_intra) {
380
        /* compute cbp */
381
162914
        cbp = 0;
382
1140398
        for (i = 0; i < 6; i++) {
383
977484
            if (s->block_last_index[i] >= 0)
384
354169
                cbp |= 1 << (5 - i);
385
        }
386

162914
        if (s->use_skip_mb_code && (cbp | motion_x | motion_y) == 0) {
387
            /* skip macroblock */
388
2259
            put_bits(&s->pb, 1, 1);
389
2259
            s->last_bits++;
390
2259
            s->misc_bits++;
391
2259
            s->skip_count++;
392
393
2259
            return;
394
        }
395
160655
        if (s->use_skip_mb_code)
396
160655
            put_bits(&s->pb, 1, 0);     /* mb coded */
397
398
160655
        if(s->msmpeg4_version<=2){
399
51003
            put_bits(&s->pb,
400
51003
                     ff_v2_mb_type[cbp&3][1],
401
51003
                     ff_v2_mb_type[cbp&3][0]);
402
51003
            if((cbp&3) != 3) coded_cbp= cbp ^ 0x3C;
403
10410
            else             coded_cbp= cbp;
404
405
51003
            put_bits(&s->pb,
406
51003
                     ff_h263_cbpy_tab[coded_cbp>>2][1],
407
51003
                     ff_h263_cbpy_tab[coded_cbp>>2][0]);
408
409
51003
            s->misc_bits += get_bits_diff(s);
410
411
51003
            ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
412
51003
            msmpeg4v2_encode_motion(s, motion_x - pred_x);
413
51003
            msmpeg4v2_encode_motion(s, motion_y - pred_y);
414
        }else{
415
109652
            put_bits(&s->pb,
416
109652
                     ff_table_mb_non_intra[cbp + 64][1],
417
109652
                     ff_table_mb_non_intra[cbp + 64][0]);
418
419
109652
            s->misc_bits += get_bits_diff(s);
420
421
            /* motion vector */
422
109652
            ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
423
109652
            ff_msmpeg4_encode_motion(s, motion_x - pred_x,
424
                                  motion_y - pred_y);
425
        }
426
427
160655
        s->mv_bits += get_bits_diff(s);
428
429
1124585
        for (i = 0; i < 6; i++) {
430
963930
            ff_msmpeg4_encode_block(s, block[i], i);
431
        }
432
160655
        s->p_tex_bits += get_bits_diff(s);
433
    } else {
434
        /* compute cbp */
435
26536
        cbp = 0;
436
26536
        coded_cbp = 0;
437
185752
        for (i = 0; i < 6; i++) {
438
            int val, pred;
439
159216
            val = (s->block_last_index[i] >= 1);
440
159216
            cbp |= val << (5 - i);
441
159216
            if (i < 4) {
442
                /* predict value for close blocks only for luma */
443
106144
                pred = ff_msmpeg4_coded_block_pred(s, i, &coded_block);
444
106144
                *coded_block = val;
445
106144
                val = val ^ pred;
446
            }
447
159216
            coded_cbp |= val << (5 - i);
448
        }
449
450
26536
        if(s->msmpeg4_version<=2){
451
8142
            if (s->pict_type == AV_PICTURE_TYPE_I) {
452
5985
                put_bits(&s->pb,
453
5985
                         ff_v2_intra_cbpc[cbp&3][1], ff_v2_intra_cbpc[cbp&3][0]);
454
            } else {
455
2157
                if (s->use_skip_mb_code)
456
2157
                    put_bits(&s->pb, 1, 0);     /* mb coded */
457
2157
                put_bits(&s->pb,
458
2157
                         ff_v2_mb_type[(cbp&3) + 4][1],
459
2157
                         ff_v2_mb_type[(cbp&3) + 4][0]);
460
            }
461
8142
            put_bits(&s->pb, 1, 0);             /* no AC prediction yet */
462
8142
            put_bits(&s->pb,
463
8142
                     ff_h263_cbpy_tab[cbp>>2][1],
464
8142
                     ff_h263_cbpy_tab[cbp>>2][0]);
465
        }else{
466
18394
            if (s->pict_type == AV_PICTURE_TYPE_I) {
467
13158
                put_bits(&s->pb,
468
13158
                         ff_msmp4_mb_i_table[coded_cbp][1], ff_msmp4_mb_i_table[coded_cbp][0]);
469
            } else {
470
5236
                if (s->use_skip_mb_code)
471
5236
                    put_bits(&s->pb, 1, 0);     /* mb coded */
472
5236
                put_bits(&s->pb,
473
5236
                         ff_table_mb_non_intra[cbp][1],
474
5236
                         ff_table_mb_non_intra[cbp][0]);
475
            }
476
18394
            put_bits(&s->pb, 1, 0);             /* no AC prediction yet */
477
18394
            if(s->inter_intra_pred){
478
                s->h263_aic_dir=0;
479
                put_bits(&s->pb, ff_table_inter_intra[s->h263_aic_dir][1], ff_table_inter_intra[s->h263_aic_dir][0]);
480
            }
481
        }
482
26536
        s->misc_bits += get_bits_diff(s);
483
484
185752
        for (i = 0; i < 6; i++) {
485
159216
            ff_msmpeg4_encode_block(s, block[i], i);
486
        }
487
26536
        s->i_tex_bits += get_bits_diff(s);
488
26536
        s->i_count++;
489
    }
490
}
491
492
208050
static void msmpeg4_encode_dc(MpegEncContext * s, int level, int n, int *dir_ptr)
493
{
494
    int sign, code;
495
    int pred;
496
497
    int16_t *dc_val;
498
208050
    pred = ff_msmpeg4_pred_dc(s, n, &dc_val, dir_ptr);
499
500
    /* update predictor */
501
208050
    if (n < 4) {
502
138700
        *dc_val = level * s->y_dc_scale;
503
    } else {
504
69350
        *dc_val = level * s->c_dc_scale;
505
    }
506
507
    /* do the prediction */
508
208050
    level -= pred;
509
510
208050
    if(s->msmpeg4_version<=2){
511
48852
        if (n < 4) {
512
32568
            put_bits(&s->pb,
513
32568
                     ff_v2_dc_lum_table[level + 256][1],
514
32568
                     ff_v2_dc_lum_table[level + 256][0]);
515
        }else{
516
16284
            put_bits(&s->pb,
517
16284
                     ff_v2_dc_chroma_table[level + 256][1],
518
16284
                     ff_v2_dc_chroma_table[level + 256][0]);
519
        }
520
    }else{
521
159198
        sign = 0;
522
159198
        if (level < 0) {
523
69483
            level = -level;
524
69483
            sign = 1;
525
        }
526
159198
        code = level;
527
159198
        if (code > DC_MAX)
528
46
            code = DC_MAX;
529
530
159198
        if (s->dc_table_index == 0) {
531
            if (n < 4) {
532
                put_bits(&s->pb, ff_table0_dc_lum[code][1], ff_table0_dc_lum[code][0]);
533
            } else {
534
                put_bits(&s->pb, ff_table0_dc_chroma[code][1], ff_table0_dc_chroma[code][0]);
535
            }
536
        } else {
537
159198
            if (n < 4) {
538
106132
                put_bits(&s->pb, ff_table1_dc_lum[code][1], ff_table1_dc_lum[code][0]);
539
            } else {
540
53066
                put_bits(&s->pb, ff_table1_dc_chroma[code][1], ff_table1_dc_chroma[code][0]);
541
            }
542
        }
543
544
159198
        if (code == DC_MAX)
545
46
            put_bits(&s->pb, 8, level);
546
547
159198
        if (level != 0) {
548
144586
            put_bits(&s->pb, 1, sign);
549
        }
550
    }
551
208050
}
552
553
/* Encoding of a block; very similar to MPEG-4 except for a different
554
 * escape coding (same as H.263) and more VLC tables. */
555
1482246
void ff_msmpeg4_encode_block(MpegEncContext * s, int16_t * block, int n)
556
{
557
    int level, run, last, i, j, last_index;
558
    int last_non_zero, sign, slevel;
559
    int code, run_diff, dc_pred_dir;
560
    const RLTable *rl;
561
    const uint8_t *scantable;
562
563
1482246
    if (s->mb_intra) {
564
208050
        msmpeg4_encode_dc(s, block[0], n, &dc_pred_dir);
565
208050
        i = 1;
566
208050
        if (n < 4) {
567
138700
            rl = &ff_rl_table[s->rl_table_index];
568
        } else {
569
69350
            rl = &ff_rl_table[3 + s->rl_chroma_table_index];
570
        }
571
208050
        run_diff = s->msmpeg4_version>=4;
572
208050
        scantable= s->intra_scantable.permutated;
573
    } else {
574
1274196
        i = 0;
575
1274196
        rl = &ff_rl_table[3 + s->rl_table_index];
576
1274196
        if(s->msmpeg4_version<=2)
577
306018
            run_diff = 0;
578
        else
579
968178
            run_diff = 1;
580
1274196
        scantable= s->inter_scantable.permutated;
581
    }
582
583
    /* recalculate block_last_index for M$ wmv1 */
584

1482246
    if (s->msmpeg4_version >= 4 && s->block_last_index[n] > 0) {
585
9923648
        for(last_index=63; last_index>=0; last_index--){
586
9923648
            if(block[scantable[last_index]]) break;
587
        }
588
277524
        s->block_last_index[n]= last_index;
589
    }else
590
1204722
        last_index = s->block_last_index[n];
591
    /* AC coefs */
592
1482246
    last_non_zero = i - 1;
593
18914067
    for (; i <= last_index; i++) {
594
17431821
        j = scantable[i];
595
17431821
        level = block[j];
596
17431821
        if (level) {
597
4396756
            run = i - last_non_zero - 1;
598
4396756
            last = (i == last_index);
599
4396756
            sign = 0;
600
4396756
            slevel = level;
601
4396756
            if (level < 0) {
602
2195832
                sign = 1;
603
2195832
                level = -level;
604
            }
605
606

4396756
            if(level<=MAX_LEVEL && run<=MAX_RUN){
607
4396756
                s->ac_stats[s->mb_intra][n>3][level][run][last]++;
608
            }
609
610
4396756
            s->ac_stats[s->mb_intra][n > 3][40][63][0]++; //esc3 like
611
612
4396756
            code = get_rl_index(rl, last, run, level);
613
4396756
            put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
614
4396756
            if (code == rl->n) {
615
                int level1, run1;
616
617
136660
                level1 = level - rl->max_level[last][run];
618
136660
                if (level1 < 1)
619
                    goto esc2;
620
136660
                code = get_rl_index(rl, last, run, level1);
621
136660
                if (code == rl->n) {
622
74016
                esc2:
623
74016
                    put_bits(&s->pb, 1, 0);
624
74016
                    if (level > MAX_LEVEL)
625
                        goto esc3;
626
74016
                    run1 = run - rl->max_run[last][level] - run_diff;
627
74016
                    if (run1 < 0)
628
403
                        goto esc3;
629
73613
                    code = get_rl_index(rl, last, run1+1, level);
630

73613
                    if (s->msmpeg4_version == 4 && code == rl->n)
631
5317
                        goto esc3;
632
68296
                    code = get_rl_index(rl, last, run1, level);
633
68296
                    if (code == rl->n) {
634
13568
                    esc3:
635
                        /* third escape */
636
19288
                        put_bits(&s->pb, 1, 0);
637
19288
                        put_bits(&s->pb, 1, last);
638
19288
                        if(s->msmpeg4_version>=4){
639
6445
                            if(s->esc3_level_length==0){
640
280
                                s->esc3_level_length=8;
641
280
                                s->esc3_run_length= 6;
642
                                //ESCLVLSZ + ESCRUNSZ
643
280
                                if(s->qscale<8)
644
                                    put_bits(&s->pb, 6, 3);
645
                                else
646
280
                                    put_bits(&s->pb, 8, 3);
647
                            }
648
6445
                            put_bits(&s->pb, s->esc3_run_length, run);
649
6445
                            put_bits(&s->pb, 1, sign);
650
6445
                            put_bits(&s->pb, s->esc3_level_length, level);
651
                        }else{
652
12843
                            put_bits(&s->pb, 6, run);
653
12843
                            put_sbits(&s->pb, 8, slevel);
654
                        }
655
                    } else {
656
                        /* second escape */
657
54728
                        put_bits(&s->pb, 1, 1);
658
54728
                        put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
659
54728
                        put_bits(&s->pb, 1, sign);
660
                    }
661
                } else {
662
                    /* first escape */
663
62644
                    put_bits(&s->pb, 1, 1);
664
62644
                    put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
665
62644
                    put_bits(&s->pb, 1, sign);
666
                }
667
            } else {
668
4260096
                put_bits(&s->pb, 1, sign);
669
            }
670
4396756
            last_non_zero = i;
671
        }
672
    }
673
1482246
}