GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/msmpeg4enc.c Lines: 369 397 92.9 %
Date: 2019-11-20 04:07:19 Branches: 192 242 79.3 %

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

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

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

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

162914
        if (s->use_skip_mb_code && (cbp | motion_x | motion_y) == 0) {
394
            /* skip macroblock */
395
2259
            put_bits(&s->pb, 1, 1);
396
2259
            s->last_bits++;
397
2259
            s->misc_bits++;
398
2259
            s->skip_count++;
399
400
2259
            return;
401
        }
402
160655
        if (s->use_skip_mb_code)
403
160655
            put_bits(&s->pb, 1, 0);     /* mb coded */
404
405
160655
        if(s->msmpeg4_version<=2){
406
51003
            put_bits(&s->pb,
407
51003
                     ff_v2_mb_type[cbp&3][1],
408
51003
                     ff_v2_mb_type[cbp&3][0]);
409
51003
            if((cbp&3) != 3) coded_cbp= cbp ^ 0x3C;
410
10410
            else             coded_cbp= cbp;
411
412
51003
            put_bits(&s->pb,
413
51003
                     ff_h263_cbpy_tab[coded_cbp>>2][1],
414
51003
                     ff_h263_cbpy_tab[coded_cbp>>2][0]);
415
416
51003
            s->misc_bits += get_bits_diff(s);
417
418
51003
            ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
419
51003
            msmpeg4v2_encode_motion(s, motion_x - pred_x);
420
51003
            msmpeg4v2_encode_motion(s, motion_y - pred_y);
421
        }else{
422
109652
            put_bits(&s->pb,
423
109652
                     ff_table_mb_non_intra[cbp + 64][1],
424
109652
                     ff_table_mb_non_intra[cbp + 64][0]);
425
426
109652
            s->misc_bits += get_bits_diff(s);
427
428
            /* motion vector */
429
109652
            ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
430
109652
            ff_msmpeg4_encode_motion(s, motion_x - pred_x,
431
                                  motion_y - pred_y);
432
        }
433
434
160655
        s->mv_bits += get_bits_diff(s);
435
436
1124585
        for (i = 0; i < 6; i++) {
437
963930
            ff_msmpeg4_encode_block(s, block[i], i);
438
        }
439
160655
        s->p_tex_bits += get_bits_diff(s);
440
    } else {
441
        /* compute cbp */
442
26536
        cbp = 0;
443
26536
        coded_cbp = 0;
444
185752
        for (i = 0; i < 6; i++) {
445
            int val, pred;
446
159216
            val = (s->block_last_index[i] >= 1);
447
159216
            cbp |= val << (5 - i);
448
159216
            if (i < 4) {
449
                /* predict value for close blocks only for luma */
450
106144
                pred = ff_msmpeg4_coded_block_pred(s, i, &coded_block);
451
106144
                *coded_block = val;
452
106144
                val = val ^ pred;
453
            }
454
159216
            coded_cbp |= val << (5 - i);
455
        }
456
457
26536
        if(s->msmpeg4_version<=2){
458
8142
            if (s->pict_type == AV_PICTURE_TYPE_I) {
459
5985
                put_bits(&s->pb,
460
5985
                         ff_v2_intra_cbpc[cbp&3][1], ff_v2_intra_cbpc[cbp&3][0]);
461
            } else {
462
2157
                if (s->use_skip_mb_code)
463
2157
                    put_bits(&s->pb, 1, 0);     /* mb coded */
464
2157
                put_bits(&s->pb,
465
2157
                         ff_v2_mb_type[(cbp&3) + 4][1],
466
2157
                         ff_v2_mb_type[(cbp&3) + 4][0]);
467
            }
468
8142
            put_bits(&s->pb, 1, 0);             /* no AC prediction yet */
469
8142
            put_bits(&s->pb,
470
8142
                     ff_h263_cbpy_tab[cbp>>2][1],
471
8142
                     ff_h263_cbpy_tab[cbp>>2][0]);
472
        }else{
473
18394
            if (s->pict_type == AV_PICTURE_TYPE_I) {
474
13158
                put_bits(&s->pb,
475
13158
                         ff_msmp4_mb_i_table[coded_cbp][1], ff_msmp4_mb_i_table[coded_cbp][0]);
476
            } else {
477
5236
                if (s->use_skip_mb_code)
478
5236
                    put_bits(&s->pb, 1, 0);     /* mb coded */
479
5236
                put_bits(&s->pb,
480
5236
                         ff_table_mb_non_intra[cbp][1],
481
                         ff_table_mb_non_intra[cbp][0]);
482
            }
483
18394
            put_bits(&s->pb, 1, 0);             /* no AC prediction yet */
484
18394
            if(s->inter_intra_pred){
485
                s->h263_aic_dir=0;
486
                put_bits(&s->pb, ff_table_inter_intra[s->h263_aic_dir][1], ff_table_inter_intra[s->h263_aic_dir][0]);
487
            }
488
        }
489
26536
        s->misc_bits += get_bits_diff(s);
490
491
185752
        for (i = 0; i < 6; i++) {
492
159216
            ff_msmpeg4_encode_block(s, block[i], i);
493
        }
494
26536
        s->i_tex_bits += get_bits_diff(s);
495
26536
        s->i_count++;
496
    }
497
}
498
499
208050
static void msmpeg4_encode_dc(MpegEncContext * s, int level, int n, int *dir_ptr)
500
{
501
    int sign, code;
502
208050
    int pred, av_uninit(extquant);
503
208050
    int extrabits = 0;
504
505
    int16_t *dc_val;
506
208050
    pred = ff_msmpeg4_pred_dc(s, n, &dc_val, dir_ptr);
507
508
    /* update predictor */
509
208050
    if (n < 4) {
510
138700
        *dc_val = level * s->y_dc_scale;
511
    } else {
512
69350
        *dc_val = level * s->c_dc_scale;
513
    }
514
515
    /* do the prediction */
516
208050
    level -= pred;
517
518
208050
    if(s->msmpeg4_version<=2){
519
48852
        if (n < 4) {
520
32568
            put_bits(&s->pb,
521
32568
                     ff_v2_dc_lum_table[level + 256][1],
522
32568
                     ff_v2_dc_lum_table[level + 256][0]);
523
        }else{
524
16284
            put_bits(&s->pb,
525
16284
                     ff_v2_dc_chroma_table[level + 256][1],
526
16284
                     ff_v2_dc_chroma_table[level + 256][0]);
527
        }
528
    }else{
529
159198
        sign = 0;
530
159198
        if (level < 0) {
531
69483
            level = -level;
532
69483
            sign = 1;
533
        }
534
159198
        code = level;
535
159198
        if (code > DC_MAX)
536
46
            code = DC_MAX;
537
159152
        else if( s->msmpeg4_version>=6 ) {
538
            if( s->qscale == 1 ) {
539
                extquant = (level + 3) & 0x3;
540
                code  = ((level+3)>>2);
541
            } else if( s->qscale == 2 ) {
542
                extquant = (level + 1) & 0x1;
543
                code  = ((level+1)>>1);
544
            }
545
        }
546
547
159198
        if (s->dc_table_index == 0) {
548
            if (n < 4) {
549
                put_bits(&s->pb, ff_table0_dc_lum[code][1], ff_table0_dc_lum[code][0]);
550
            } else {
551
                put_bits(&s->pb, ff_table0_dc_chroma[code][1], ff_table0_dc_chroma[code][0]);
552
            }
553
        } else {
554
159198
            if (n < 4) {
555
106132
                put_bits(&s->pb, ff_table1_dc_lum[code][1], ff_table1_dc_lum[code][0]);
556
            } else {
557
53066
                put_bits(&s->pb, ff_table1_dc_chroma[code][1], ff_table1_dc_chroma[code][0]);
558
            }
559
        }
560
561

159198
        if(s->msmpeg4_version>=6 && s->qscale<=2)
562
            extrabits = 3 - s->qscale;
563
564
159198
        if (code == DC_MAX)
565
46
            put_bits(&s->pb, 8 + extrabits, level);
566
159152
        else if(extrabits > 0)//== VC1 && s->qscale<=2
567
            put_bits(&s->pb, extrabits, extquant);
568
569
159198
        if (level != 0) {
570
144586
            put_bits(&s->pb, 1, sign);
571
        }
572
    }
573
208050
}
574
575
/* Encoding of a block; very similar to MPEG-4 except for a different
576
 * escape coding (same as H.263) and more VLC tables. */
577
1482246
void ff_msmpeg4_encode_block(MpegEncContext * s, int16_t * block, int n)
578
{
579
    int level, run, last, i, j, last_index;
580
    int last_non_zero, sign, slevel;
581
    int code, run_diff, dc_pred_dir;
582
    const RLTable *rl;
583
    const uint8_t *scantable;
584
585
1482246
    if (s->mb_intra) {
586
208050
        msmpeg4_encode_dc(s, block[0], n, &dc_pred_dir);
587
208050
        i = 1;
588
208050
        if (n < 4) {
589
138700
            rl = &ff_rl_table[s->rl_table_index];
590
        } else {
591
69350
            rl = &ff_rl_table[3 + s->rl_chroma_table_index];
592
        }
593
208050
        run_diff = s->msmpeg4_version>=4;
594
208050
        scantable= s->intra_scantable.permutated;
595
    } else {
596
1274196
        i = 0;
597
1274196
        rl = &ff_rl_table[3 + s->rl_table_index];
598
1274196
        if(s->msmpeg4_version<=2)
599
306018
            run_diff = 0;
600
        else
601
968178
            run_diff = 1;
602
1274196
        scantable= s->inter_scantable.permutated;
603
    }
604
605
    /* recalculate block_last_index for M$ wmv1 */
606

1482246
    if(s->msmpeg4_version>=4 && s->msmpeg4_version<6 && s->block_last_index[n]>0){
607
9923648
        for(last_index=63; last_index>=0; last_index--){
608
9923648
            if(block[scantable[last_index]]) break;
609
        }
610
277524
        s->block_last_index[n]= last_index;
611
    }else
612
1204722
        last_index = s->block_last_index[n];
613
    /* AC coefs */
614
1482246
    last_non_zero = i - 1;
615
18914067
    for (; i <= last_index; i++) {
616
17431821
        j = scantable[i];
617
17431821
        level = block[j];
618
17431821
        if (level) {
619
4396756
            run = i - last_non_zero - 1;
620
4396756
            last = (i == last_index);
621
4396756
            sign = 0;
622
4396756
            slevel = level;
623
4396756
            if (level < 0) {
624
2195832
                sign = 1;
625
2195832
                level = -level;
626
            }
627
628

4396756
            if(level<=MAX_LEVEL && run<=MAX_RUN){
629
4396756
                s->ac_stats[s->mb_intra][n>3][level][run][last]++;
630
            }
631
632
4396756
            s->ac_stats[s->mb_intra][n > 3][40][63][0]++; //esc3 like
633
634
4396756
            code = get_rl_index(rl, last, run, level);
635
4396756
            put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
636
4396756
            if (code == rl->n) {
637
                int level1, run1;
638
639
136660
                level1 = level - rl->max_level[last][run];
640
136660
                if (level1 < 1)
641
                    goto esc2;
642
136660
                code = get_rl_index(rl, last, run, level1);
643
136660
                if (code == rl->n) {
644
74016
                esc2:
645
74016
                    put_bits(&s->pb, 1, 0);
646
74016
                    if (level > MAX_LEVEL)
647
                        goto esc3;
648
74016
                    run1 = run - rl->max_run[last][level] - run_diff;
649
74016
                    if (run1 < 0)
650
403
                        goto esc3;
651
73613
                    code = get_rl_index(rl, last, run1+1, level);
652

73613
                    if (s->msmpeg4_version == 4 && code == rl->n)
653
5317
                        goto esc3;
654
68296
                    code = get_rl_index(rl, last, run1, level);
655
68296
                    if (code == rl->n) {
656
13568
                    esc3:
657
                        /* third escape */
658
19288
                        put_bits(&s->pb, 1, 0);
659
19288
                        put_bits(&s->pb, 1, last);
660
19288
                        if(s->msmpeg4_version>=4){
661
6445
                            if(s->esc3_level_length==0){
662
280
                                s->esc3_level_length=8;
663
280
                                s->esc3_run_length= 6;
664
                                //ESCLVLSZ + ESCRUNSZ
665
280
                                if(s->qscale<8)
666
                                    put_bits(&s->pb, 6 + (s->msmpeg4_version>=6), 3);
667
                                else
668
280
                                    put_bits(&s->pb, 8, 3);
669
                            }
670
6445
                            put_bits(&s->pb, s->esc3_run_length, run);
671
6445
                            put_bits(&s->pb, 1, sign);
672
6445
                            put_bits(&s->pb, s->esc3_level_length, level);
673
                        }else{
674
12843
                            put_bits(&s->pb, 6, run);
675
12843
                            put_sbits(&s->pb, 8, slevel);
676
                        }
677
                    } else {
678
                        /* second escape */
679
54728
                        put_bits(&s->pb, 1, 1);
680
54728
                        put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
681
54728
                        put_bits(&s->pb, 1, sign);
682
                    }
683
                } else {
684
                    /* first escape */
685
62644
                    put_bits(&s->pb, 1, 1);
686
62644
                    put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
687
62644
                    put_bits(&s->pb, 1, sign);
688
                }
689
            } else {
690
4260096
                put_bits(&s->pb, 1, sign);
691
            }
692
4396756
            last_non_zero = i;
693
        }
694
    }
695
1482246
}