• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 */
init_mv_table(MVTable * tab,uint16_t table_mv_index[4096])48 static av_cold void init_mv_table(MVTable *tab, uint16_t table_mv_index[4096])
49 {
50     int i, x, y;
51 
52     tab->table_mv_index = table_mv_index;
53 
54     /* mark all entries as not used */
55     for(i=0;i<4096;i++)
56         tab->table_mv_index[i] = MSMPEG4_MV_TABLES_NB_ELEMS;
57 
58     for (i = 0; i < MSMPEG4_MV_TABLES_NB_ELEMS; i++) {
59         x = tab->table_mvx[i];
60         y = tab->table_mvy[i];
61         tab->table_mv_index[(x << 6) | y] = i;
62     }
63 }
64 
ff_msmpeg4_code012(PutBitContext * pb,int n)65 void ff_msmpeg4_code012(PutBitContext *pb, int n)
66 {
67     if (n == 0) {
68         put_bits(pb, 1, 0);
69     } else {
70         put_bits(pb, 1, 1);
71         put_bits(pb, 1, (n >= 2));
72     }
73 }
74 
get_size_of_code(MpegEncContext * s,RLTable * rl,int last,int run,int level,int intra)75 static int get_size_of_code(MpegEncContext * s, RLTable *rl, int last, int run, int level, int intra){
76     int size=0;
77     int code;
78     int run_diff= intra ? 0 : 1;
79 
80     code = get_rl_index(rl, last, run, level);
81     size+= rl->table_vlc[code][1];
82     if (code == rl->n) {
83         int level1, run1;
84 
85         level1 = level - rl->max_level[last][run];
86         if (level1 < 1)
87             goto esc2;
88         code = get_rl_index(rl, last, run, level1);
89         if (code == rl->n) {
90             esc2:
91             size++;
92             if (level > MAX_LEVEL)
93                 goto esc3;
94             run1 = run - rl->max_run[last][level] - run_diff;
95             if (run1 < 0)
96                 goto esc3;
97             code = get_rl_index(rl, last, run1, level);
98             if (code == rl->n) {
99             esc3:
100                 /* third escape */
101                 size+=1+1+6+8;
102             } else {
103                 /* second escape */
104                 size+= 1+1+ rl->table_vlc[code][1];
105             }
106         } else {
107             /* first escape */
108             size+= 1+1+ rl->table_vlc[code][1];
109         }
110     } else {
111         size++;
112     }
113     return size;
114 }
115 
ff_msmpeg4_encode_init(MpegEncContext * s)116 av_cold void ff_msmpeg4_encode_init(MpegEncContext *s)
117 {
118     static int init_done=0;
119     int i;
120 
121     ff_msmpeg4_common_init(s);
122     if(s->msmpeg4_version>=4){
123         s->min_qcoeff= -255;
124         s->max_qcoeff=  255;
125     }
126 
127     if (!init_done) {
128         static uint16_t mv_index_tables[2][4096];
129         /* init various encoding tables */
130         init_done = 1;
131         init_mv_table(&ff_mv_tables[0], mv_index_tables[0]);
132         init_mv_table(&ff_mv_tables[1], mv_index_tables[1]);
133 
134         for(i=0;i<NB_RL_TABLES;i++)
135             ff_rl_init(&ff_rl_table[i], ff_static_rl_table_store[i]);
136 
137         for(i=0; i<NB_RL_TABLES; i++){
138             int level;
139             for (level = 1; level <= MAX_LEVEL; level++) {
140                 int run;
141                 for(run=0; run<=MAX_RUN; run++){
142                     int last;
143                     for(last=0; last<2; last++){
144                         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 }
151 
find_best_tables(MpegEncContext * s)152 static void find_best_tables(MpegEncContext * s)
153 {
154     int i;
155     int best        = 0, best_size        = INT_MAX;
156     int chroma_best = 0, best_chroma_size = INT_MAX;
157 
158     for(i=0; i<3; i++){
159         int level;
160         int chroma_size=0;
161         int size=0;
162 
163         if(i>0){// ;)
164             size++;
165             chroma_size++;
166         }
167         for(level=0; level<=MAX_LEVEL; level++){
168             int run;
169             for(run=0; run<=MAX_RUN; run++){
170                 int last;
171                 const int last_size= size + chroma_size;
172                 for(last=0; last<2; last++){
173                     int inter_count       = s->ac_stats[0][0][level][run][last] + s->ac_stats[0][1][level][run][last];
174                     int intra_luma_count  = s->ac_stats[1][0][level][run][last];
175                     int intra_chroma_count= s->ac_stats[1][1][level][run][last];
176 
177                     if(s->pict_type==AV_PICTURE_TYPE_I){
178                         size       += intra_luma_count  *rl_length[i  ][level][run][last];
179                         chroma_size+= intra_chroma_count*rl_length[i+3][level][run][last];
180                     }else{
181                         size+=        intra_luma_count  *rl_length[i  ][level][run][last]
182                                      +intra_chroma_count*rl_length[i+3][level][run][last]
183                                      +inter_count       *rl_length[i+3][level][run][last];
184                     }
185                 }
186                 if(last_size == size+chroma_size) break;
187             }
188         }
189         if(size<best_size){
190             best_size= size;
191             best= i;
192         }
193         if(chroma_size<best_chroma_size){
194             best_chroma_size= chroma_size;
195             chroma_best= i;
196         }
197     }
198 
199     if(s->pict_type==AV_PICTURE_TYPE_P) chroma_best= best;
200 
201     memset(s->ac_stats, 0, sizeof(int)*(MAX_LEVEL+1)*(MAX_RUN+1)*2*2*2);
202 
203     s->rl_table_index       =        best;
204     s->rl_chroma_table_index= chroma_best;
205 
206     if(s->pict_type != s->last_non_b_pict_type){
207         s->rl_table_index= 2;
208         if(s->pict_type==AV_PICTURE_TYPE_I)
209             s->rl_chroma_table_index= 1;
210         else
211             s->rl_chroma_table_index= 2;
212     }
213 
214 }
215 
216 /* write MSMPEG4 compatible frame header */
ff_msmpeg4_encode_picture_header(MpegEncContext * s,int picture_number)217 void ff_msmpeg4_encode_picture_header(MpegEncContext * s, int picture_number)
218 {
219     find_best_tables(s);
220 
221     align_put_bits(&s->pb);
222     put_bits(&s->pb, 2, s->pict_type - 1);
223 
224     put_bits(&s->pb, 5, s->qscale);
225     if(s->msmpeg4_version<=2){
226         s->rl_table_index = 2;
227         s->rl_chroma_table_index = 2;
228     }
229 
230     s->dc_table_index = 1;
231     s->mv_table_index = 1; /* only if P-frame */
232     s->use_skip_mb_code = 1; /* only if P-frame */
233     s->per_mb_rl_table = 0;
234     if(s->msmpeg4_version==4)
235         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     if (s->pict_type == AV_PICTURE_TYPE_I) {
240         s->slice_height= s->mb_height/1;
241         put_bits(&s->pb, 5, 0x16 + s->mb_height/s->slice_height);
242 
243         if(s->msmpeg4_version==4){
244             ff_msmpeg4_encode_ext_header(s);
245             if(s->bit_rate>MBAC_BITRATE)
246                 put_bits(&s->pb, 1, s->per_mb_rl_table);
247         }
248 
249         if(s->msmpeg4_version>2){
250             if(!s->per_mb_rl_table){
251                 ff_msmpeg4_code012(&s->pb, s->rl_chroma_table_index);
252                 ff_msmpeg4_code012(&s->pb, s->rl_table_index);
253             }
254 
255             put_bits(&s->pb, 1, s->dc_table_index);
256         }
257     } else {
258         put_bits(&s->pb, 1, s->use_skip_mb_code);
259 
260         if(s->msmpeg4_version==4 && s->bit_rate>MBAC_BITRATE)
261             put_bits(&s->pb, 1, s->per_mb_rl_table);
262 
263         if(s->msmpeg4_version>2){
264             if(!s->per_mb_rl_table)
265                 ff_msmpeg4_code012(&s->pb, s->rl_table_index);
266 
267             put_bits(&s->pb, 1, s->dc_table_index);
268 
269             put_bits(&s->pb, 1, s->mv_table_index);
270         }
271     }
272 
273     s->esc3_level_length= 0;
274     s->esc3_run_length= 0;
275 }
276 
ff_msmpeg4_encode_ext_header(MpegEncContext * s)277 void ff_msmpeg4_encode_ext_header(MpegEncContext * s)
278 {
279         unsigned fps = s->avctx->time_base.den / s->avctx->time_base.num / FFMAX(s->avctx->ticks_per_frame, 1);
280         put_bits(&s->pb, 5, FFMIN(fps, 31)); //yes 29.97 -> 29
281 
282         put_bits(&s->pb, 11, FFMIN(s->bit_rate/1024, 2047));
283 
284         if(s->msmpeg4_version>=3)
285             put_bits(&s->pb, 1, s->flipflop_rounding);
286         else
287             av_assert0(s->flipflop_rounding==0);
288 }
289 
ff_msmpeg4_encode_motion(MpegEncContext * s,int mx,int my)290 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     if (mx <= -64)
300         mx += 64;
301     else if (mx >= 64)
302         mx -= 64;
303     if (my <= -64)
304         my += 64;
305     else if (my >= 64)
306         my -= 64;
307 
308     mx += 32;
309     my += 32;
310     mv = &ff_mv_tables[s->mv_table_index];
311 
312     code = mv->table_mv_index[(mx << 6) | my];
313     put_bits(&s->pb,
314              mv->table_mv_bits[code],
315              mv->table_mv_code[code]);
316     if (code == MSMPEG4_MV_TABLES_NB_ELEMS) {
317         /* escape : code literally */
318         put_bits(&s->pb, 6, mx);
319         put_bits(&s->pb, 6, my);
320     }
321 }
322 
ff_msmpeg4_handle_slices(MpegEncContext * s)323 void ff_msmpeg4_handle_slices(MpegEncContext *s){
324     if (s->mb_x == 0) {
325         if (s->slice_height && (s->mb_y % s->slice_height) == 0) {
326             if(s->msmpeg4_version < 4){
327                 ff_mpeg4_clean_buffers(s);
328             }
329             s->first_slice_line = 1;
330         } else {
331             s->first_slice_line = 0;
332         }
333     }
334 }
335 
msmpeg4v2_encode_motion(MpegEncContext * s,int val)336 static void msmpeg4v2_encode_motion(MpegEncContext * s, int val)
337 {
338     int range, bit_size, sign, code, bits;
339 
340     if (val == 0) {
341         /* zero vector */
342         code = 0;
343         put_bits(&s->pb, ff_mvtab[code][1], ff_mvtab[code][0]);
344     } else {
345         bit_size = s->f_code - 1;
346         range = 1 << bit_size;
347         if (val <= -64)
348             val += 64;
349         else if (val >= 64)
350             val -= 64;
351 
352         if (val >= 0) {
353             sign = 0;
354         } else {
355             val = -val;
356             sign = 1;
357         }
358         val--;
359         code = (val >> bit_size) + 1;
360         bits = val & (range - 1);
361 
362         put_bits(&s->pb, ff_mvtab[code][1] + 1, (ff_mvtab[code][0] << 1) | sign);
363         if (bit_size > 0) {
364             put_bits(&s->pb, bit_size, bits);
365         }
366     }
367 }
368 
ff_msmpeg4_encode_mb(MpegEncContext * s,int16_t block[6][64],int motion_x,int motion_y)369 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     ff_msmpeg4_handle_slices(s);
378 
379     if (!s->mb_intra) {
380         /* compute cbp */
381         cbp = 0;
382         for (i = 0; i < 6; i++) {
383             if (s->block_last_index[i] >= 0)
384                 cbp |= 1 << (5 - i);
385         }
386         if (s->use_skip_mb_code && (cbp | motion_x | motion_y) == 0) {
387             /* skip macroblock */
388             put_bits(&s->pb, 1, 1);
389             s->last_bits++;
390             s->misc_bits++;
391             s->skip_count++;
392 
393             return;
394         }
395         if (s->use_skip_mb_code)
396             put_bits(&s->pb, 1, 0);     /* mb coded */
397 
398         if(s->msmpeg4_version<=2){
399             put_bits(&s->pb,
400                      ff_v2_mb_type[cbp&3][1],
401                      ff_v2_mb_type[cbp&3][0]);
402             if((cbp&3) != 3) coded_cbp= cbp ^ 0x3C;
403             else             coded_cbp= cbp;
404 
405             put_bits(&s->pb,
406                      ff_h263_cbpy_tab[coded_cbp>>2][1],
407                      ff_h263_cbpy_tab[coded_cbp>>2][0]);
408 
409             s->misc_bits += get_bits_diff(s);
410 
411             ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
412             msmpeg4v2_encode_motion(s, motion_x - pred_x);
413             msmpeg4v2_encode_motion(s, motion_y - pred_y);
414         }else{
415             put_bits(&s->pb,
416                      ff_table_mb_non_intra[cbp + 64][1],
417                      ff_table_mb_non_intra[cbp + 64][0]);
418 
419             s->misc_bits += get_bits_diff(s);
420 
421             /* motion vector */
422             ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
423             ff_msmpeg4_encode_motion(s, motion_x - pred_x,
424                                   motion_y - pred_y);
425         }
426 
427         s->mv_bits += get_bits_diff(s);
428 
429         for (i = 0; i < 6; i++) {
430             ff_msmpeg4_encode_block(s, block[i], i);
431         }
432         s->p_tex_bits += get_bits_diff(s);
433     } else {
434         /* compute cbp */
435         cbp = 0;
436         coded_cbp = 0;
437         for (i = 0; i < 6; i++) {
438             int val, pred;
439             val = (s->block_last_index[i] >= 1);
440             cbp |= val << (5 - i);
441             if (i < 4) {
442                 /* predict value for close blocks only for luma */
443                 pred = ff_msmpeg4_coded_block_pred(s, i, &coded_block);
444                 *coded_block = val;
445                 val = val ^ pred;
446             }
447             coded_cbp |= val << (5 - i);
448         }
449 
450         if(s->msmpeg4_version<=2){
451             if (s->pict_type == AV_PICTURE_TYPE_I) {
452                 put_bits(&s->pb,
453                          ff_v2_intra_cbpc[cbp&3][1], ff_v2_intra_cbpc[cbp&3][0]);
454             } else {
455                 if (s->use_skip_mb_code)
456                     put_bits(&s->pb, 1, 0);     /* mb coded */
457                 put_bits(&s->pb,
458                          ff_v2_mb_type[(cbp&3) + 4][1],
459                          ff_v2_mb_type[(cbp&3) + 4][0]);
460             }
461             put_bits(&s->pb, 1, 0);             /* no AC prediction yet */
462             put_bits(&s->pb,
463                      ff_h263_cbpy_tab[cbp>>2][1],
464                      ff_h263_cbpy_tab[cbp>>2][0]);
465         }else{
466             if (s->pict_type == AV_PICTURE_TYPE_I) {
467                 put_bits(&s->pb,
468                          ff_msmp4_mb_i_table[coded_cbp][1], ff_msmp4_mb_i_table[coded_cbp][0]);
469             } else {
470                 if (s->use_skip_mb_code)
471                     put_bits(&s->pb, 1, 0);     /* mb coded */
472                 put_bits(&s->pb,
473                          ff_table_mb_non_intra[cbp][1],
474                          ff_table_mb_non_intra[cbp][0]);
475             }
476             put_bits(&s->pb, 1, 0);             /* no AC prediction yet */
477             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         s->misc_bits += get_bits_diff(s);
483 
484         for (i = 0; i < 6; i++) {
485             ff_msmpeg4_encode_block(s, block[i], i);
486         }
487         s->i_tex_bits += get_bits_diff(s);
488         s->i_count++;
489     }
490 }
491 
msmpeg4_encode_dc(MpegEncContext * s,int level,int n,int * dir_ptr)492 static void msmpeg4_encode_dc(MpegEncContext * s, int level, int n, int *dir_ptr)
493 {
494     int sign, code;
495     int pred, av_uninit(extquant);
496     int extrabits = 0;
497 
498     int16_t *dc_val;
499     pred = ff_msmpeg4_pred_dc(s, n, &dc_val, dir_ptr);
500 
501     /* update predictor */
502     if (n < 4) {
503         *dc_val = level * s->y_dc_scale;
504     } else {
505         *dc_val = level * s->c_dc_scale;
506     }
507 
508     /* do the prediction */
509     level -= pred;
510 
511     if(s->msmpeg4_version<=2){
512         if (n < 4) {
513             put_bits(&s->pb,
514                      ff_v2_dc_lum_table[level + 256][1],
515                      ff_v2_dc_lum_table[level + 256][0]);
516         }else{
517             put_bits(&s->pb,
518                      ff_v2_dc_chroma_table[level + 256][1],
519                      ff_v2_dc_chroma_table[level + 256][0]);
520         }
521     }else{
522         sign = 0;
523         if (level < 0) {
524             level = -level;
525             sign = 1;
526         }
527         code = level;
528         if (code > DC_MAX)
529             code = DC_MAX;
530         else if( s->msmpeg4_version>=6 ) {
531             if( s->qscale == 1 ) {
532                 extquant = (level + 3) & 0x3;
533                 code  = ((level+3)>>2);
534             } else if( s->qscale == 2 ) {
535                 extquant = (level + 1) & 0x1;
536                 code  = ((level+1)>>1);
537             }
538         }
539 
540         if (s->dc_table_index == 0) {
541             if (n < 4) {
542                 put_bits(&s->pb, ff_table0_dc_lum[code][1], ff_table0_dc_lum[code][0]);
543             } else {
544                 put_bits(&s->pb, ff_table0_dc_chroma[code][1], ff_table0_dc_chroma[code][0]);
545             }
546         } else {
547             if (n < 4) {
548                 put_bits(&s->pb, ff_table1_dc_lum[code][1], ff_table1_dc_lum[code][0]);
549             } else {
550                 put_bits(&s->pb, ff_table1_dc_chroma[code][1], ff_table1_dc_chroma[code][0]);
551             }
552         }
553 
554         if(s->msmpeg4_version>=6 && s->qscale<=2)
555             extrabits = 3 - s->qscale;
556 
557         if (code == DC_MAX)
558             put_bits(&s->pb, 8 + extrabits, level);
559         else if(extrabits > 0)//== VC1 && s->qscale<=2
560             put_bits(&s->pb, extrabits, extquant);
561 
562         if (level != 0) {
563             put_bits(&s->pb, 1, sign);
564         }
565     }
566 }
567 
568 /* Encoding of a block; very similar to MPEG-4 except for a different
569  * escape coding (same as H.263) and more VLC tables. */
ff_msmpeg4_encode_block(MpegEncContext * s,int16_t * block,int n)570 void ff_msmpeg4_encode_block(MpegEncContext * s, int16_t * block, int n)
571 {
572     int level, run, last, i, j, last_index;
573     int last_non_zero, sign, slevel;
574     int code, run_diff, dc_pred_dir;
575     const RLTable *rl;
576     const uint8_t *scantable;
577 
578     if (s->mb_intra) {
579         msmpeg4_encode_dc(s, block[0], n, &dc_pred_dir);
580         i = 1;
581         if (n < 4) {
582             rl = &ff_rl_table[s->rl_table_index];
583         } else {
584             rl = &ff_rl_table[3 + s->rl_chroma_table_index];
585         }
586         run_diff = s->msmpeg4_version>=4;
587         scantable= s->intra_scantable.permutated;
588     } else {
589         i = 0;
590         rl = &ff_rl_table[3 + s->rl_table_index];
591         if(s->msmpeg4_version<=2)
592             run_diff = 0;
593         else
594             run_diff = 1;
595         scantable= s->inter_scantable.permutated;
596     }
597 
598     /* recalculate block_last_index for M$ wmv1 */
599     if(s->msmpeg4_version>=4 && s->msmpeg4_version<6 && s->block_last_index[n]>0){
600         for(last_index=63; last_index>=0; last_index--){
601             if(block[scantable[last_index]]) break;
602         }
603         s->block_last_index[n]= last_index;
604     }else
605         last_index = s->block_last_index[n];
606     /* AC coefs */
607     last_non_zero = i - 1;
608     for (; i <= last_index; i++) {
609         j = scantable[i];
610         level = block[j];
611         if (level) {
612             run = i - last_non_zero - 1;
613             last = (i == last_index);
614             sign = 0;
615             slevel = level;
616             if (level < 0) {
617                 sign = 1;
618                 level = -level;
619             }
620 
621             if(level<=MAX_LEVEL && run<=MAX_RUN){
622                 s->ac_stats[s->mb_intra][n>3][level][run][last]++;
623             }
624 
625             s->ac_stats[s->mb_intra][n > 3][40][63][0]++; //esc3 like
626 
627             code = get_rl_index(rl, last, run, level);
628             put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
629             if (code == rl->n) {
630                 int level1, run1;
631 
632                 level1 = level - rl->max_level[last][run];
633                 if (level1 < 1)
634                     goto esc2;
635                 code = get_rl_index(rl, last, run, level1);
636                 if (code == rl->n) {
637                 esc2:
638                     put_bits(&s->pb, 1, 0);
639                     if (level > MAX_LEVEL)
640                         goto esc3;
641                     run1 = run - rl->max_run[last][level] - run_diff;
642                     if (run1 < 0)
643                         goto esc3;
644                     code = get_rl_index(rl, last, run1+1, level);
645                     if (s->msmpeg4_version == 4 && code == rl->n)
646                         goto esc3;
647                     code = get_rl_index(rl, last, run1, level);
648                     if (code == rl->n) {
649                     esc3:
650                         /* third escape */
651                         put_bits(&s->pb, 1, 0);
652                         put_bits(&s->pb, 1, last);
653                         if(s->msmpeg4_version>=4){
654                             if(s->esc3_level_length==0){
655                                 s->esc3_level_length=8;
656                                 s->esc3_run_length= 6;
657                                 //ESCLVLSZ + ESCRUNSZ
658                                 if(s->qscale<8)
659                                     put_bits(&s->pb, 6 + (s->msmpeg4_version>=6), 3);
660                                 else
661                                     put_bits(&s->pb, 8, 3);
662                             }
663                             put_bits(&s->pb, s->esc3_run_length, run);
664                             put_bits(&s->pb, 1, sign);
665                             put_bits(&s->pb, s->esc3_level_length, level);
666                         }else{
667                             put_bits(&s->pb, 6, run);
668                             put_sbits(&s->pb, 8, slevel);
669                         }
670                     } else {
671                         /* second escape */
672                         put_bits(&s->pb, 1, 1);
673                         put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
674                         put_bits(&s->pb, 1, sign);
675                     }
676                 } else {
677                     /* first escape */
678                     put_bits(&s->pb, 1, 1);
679                     put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
680                     put_bits(&s->pb, 1, sign);
681                 }
682             } else {
683                 put_bits(&s->pb, 1, sign);
684             }
685             last_non_zero = i;
686         }
687     }
688 }
689