aacsbr: prevent multiple ff_aac_sbr_ctx_init()
[ffmpeg.git] / libavcodec / mpeg4videoenc.c
1 /*
2  * MPEG4 encoder.
3  * Copyright (c) 2000,2001 Fabrice Bellard
4  * Copyright (c) 2002-2010 Michael Niedermayer <michaelni@gmx.at>
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 #include "libavutil/log.h"
24 #include "libavutil/opt.h"
25 #include "mpegvideo.h"
26 #include "h263.h"
27 #include "mpeg4video.h"
28
29 //The uni_DCtab_* tables below contain unified bits+length tables to encode DC
30 //differences in mpeg4. Unified in the sense that the specification specifies
31 //this encoding in several steps.
32 static uint8_t uni_DCtab_lum_len[512];
33 static uint8_t uni_DCtab_chrom_len[512];
34 static uint16_t uni_DCtab_lum_bits[512];
35 static uint16_t uni_DCtab_chrom_bits[512];
36
37 //unified encoding tables for run length encoding of coefficients
38 //unified in the sense that the specification specifies the encoding in several steps.
39 static uint32_t uni_mpeg4_intra_rl_bits[64*64*2*2];
40 static uint8_t  uni_mpeg4_intra_rl_len [64*64*2*2];
41 static uint32_t uni_mpeg4_inter_rl_bits[64*64*2*2];
42 static uint8_t  uni_mpeg4_inter_rl_len [64*64*2*2];
43 //#define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128 + (run)*256 + (level))
44 //#define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128*64 + (run) + (level)*64)
45 #define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128*64 + (run)*128 + (level))
46
47 /* mpeg4
48 inter
49 max level: 24/6
50 max run: 53/63
51
52 intra
53 max level: 53/16
54 max run: 29/41
55 */
56
57
58 /**
59  * Return the number of bits that encoding the 8x8 block in block would need.
60  * @param[in]  block_last_index last index in scantable order that refers to a non zero element in block.
61  */
62 static inline int get_block_rate(MpegEncContext * s, DCTELEM block[64], int block_last_index, uint8_t scantable[64]){
63     int last=0;
64     int j;
65     int rate=0;
66
67     for(j=1; j<=block_last_index; j++){
68         const int index= scantable[j];
69         int level= block[index];
70         if(level){
71             level+= 64;
72             if((level&(~127)) == 0){
73                 if(j<block_last_index) rate+= s->intra_ac_vlc_length     [UNI_AC_ENC_INDEX(j-last-1, level)];
74                 else                   rate+= s->intra_ac_vlc_last_length[UNI_AC_ENC_INDEX(j-last-1, level)];
75             }else
76                 rate += s->ac_esc_length;
77
78             last= j;
79         }
80     }
81
82     return rate;
83 }
84
85
86 /**
87  * Restore the ac coefficients in block that have been changed by decide_ac_pred().
88  * This function also restores s->block_last_index.
89  * @param[in,out] block MB coefficients, these will be restored
90  * @param[in] dir ac prediction direction for each 8x8 block
91  * @param[out] st scantable for each 8x8 block
92  * @param[in] zigzag_last_index index refering to the last non zero coefficient in zigzag order
93  */
94 static inline void restore_ac_coeffs(MpegEncContext * s, DCTELEM block[6][64], const int dir[6], uint8_t *st[6], const int zigzag_last_index[6])
95 {
96     int i, n;
97     memcpy(s->block_last_index, zigzag_last_index, sizeof(int)*6);
98
99     for(n=0; n<6; n++){
100         int16_t *ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
101
102         st[n]= s->intra_scantable.permutated;
103         if(dir[n]){
104             /* top prediction */
105             for(i=1; i<8; i++){
106                 block[n][s->dsp.idct_permutation[i   ]] = ac_val[i+8];
107             }
108         }else{
109             /* left prediction */
110             for(i=1; i<8; i++){
111                 block[n][s->dsp.idct_permutation[i<<3]]= ac_val[i  ];
112             }
113         }
114     }
115 }
116
117 /**
118  * Return the optimal value (0 or 1) for the ac_pred element for the given MB in mpeg4.
119  * This function will also update s->block_last_index and s->ac_val.
120  * @param[in,out] block MB coefficients, these will be updated if 1 is returned
121  * @param[in] dir ac prediction direction for each 8x8 block
122  * @param[out] st scantable for each 8x8 block
123  * @param[out] zigzag_last_index index refering to the last non zero coefficient in zigzag order
124  */
125 static inline int decide_ac_pred(MpegEncContext * s, DCTELEM block[6][64], const int dir[6], uint8_t *st[6], int zigzag_last_index[6])
126 {
127     int score= 0;
128     int i, n;
129     int8_t * const qscale_table = s->current_picture.f.qscale_table;
130
131     memcpy(zigzag_last_index, s->block_last_index, sizeof(int)*6);
132
133     for(n=0; n<6; n++){
134         int16_t *ac_val, *ac_val1;
135
136         score -= get_block_rate(s, block[n], s->block_last_index[n], s->intra_scantable.permutated);
137
138         ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
139         ac_val1= ac_val;
140         if(dir[n]){
141             const int xy= s->mb_x + s->mb_y*s->mb_stride - s->mb_stride;
142             /* top prediction */
143             ac_val-= s->block_wrap[n]*16;
144             if(s->mb_y==0 || s->qscale == qscale_table[xy] || n==2 || n==3){
145                 /* same qscale */
146                 for(i=1; i<8; i++){
147                     const int level= block[n][s->dsp.idct_permutation[i   ]];
148                     block[n][s->dsp.idct_permutation[i   ]] = level - ac_val[i+8];
149                     ac_val1[i  ]=    block[n][s->dsp.idct_permutation[i<<3]];
150                     ac_val1[i+8]= level;
151                 }
152             }else{
153                 /* different qscale, we must rescale */
154                 for(i=1; i<8; i++){
155                     const int level= block[n][s->dsp.idct_permutation[i   ]];
156                     block[n][s->dsp.idct_permutation[i   ]] = level - ROUNDED_DIV(ac_val[i + 8]*qscale_table[xy], s->qscale);
157                     ac_val1[i  ]=    block[n][s->dsp.idct_permutation[i<<3]];
158                     ac_val1[i+8]= level;
159                 }
160             }
161             st[n]= s->intra_h_scantable.permutated;
162         }else{
163             const int xy= s->mb_x-1 + s->mb_y*s->mb_stride;
164             /* left prediction */
165             ac_val-= 16;
166             if(s->mb_x==0 || s->qscale == qscale_table[xy] || n==1 || n==3){
167                 /* same qscale */
168                 for(i=1; i<8; i++){
169                     const int level= block[n][s->dsp.idct_permutation[i<<3]];
170                     block[n][s->dsp.idct_permutation[i<<3]]= level - ac_val[i];
171                     ac_val1[i  ]= level;
172                     ac_val1[i+8]=    block[n][s->dsp.idct_permutation[i   ]];
173                 }
174             }else{
175                 /* different qscale, we must rescale */
176                 for(i=1; i<8; i++){
177                     const int level= block[n][s->dsp.idct_permutation[i<<3]];
178                     block[n][s->dsp.idct_permutation[i<<3]]= level - ROUNDED_DIV(ac_val[i]*qscale_table[xy], s->qscale);
179                     ac_val1[i  ]= level;
180                     ac_val1[i+8]=    block[n][s->dsp.idct_permutation[i   ]];
181                 }
182             }
183             st[n]= s->intra_v_scantable.permutated;
184         }
185
186         for(i=63; i>0; i--) //FIXME optimize
187             if(block[n][ st[n][i] ]) break;
188         s->block_last_index[n]= i;
189
190         score += get_block_rate(s, block[n], s->block_last_index[n], st[n]);
191     }
192
193     if(score < 0){
194         return 1;
195     }else{
196         restore_ac_coeffs(s, block, dir, st, zigzag_last_index);
197         return 0;
198     }
199 }
200
201 /**
202  * modify mb_type & qscale so that encoding is acually possible in mpeg4
203  */
204 void ff_clean_mpeg4_qscales(MpegEncContext *s){
205     int i;
206     int8_t * const qscale_table = s->current_picture.f.qscale_table;
207
208     ff_clean_h263_qscales(s);
209
210     if(s->pict_type== AV_PICTURE_TYPE_B){
211         int odd=0;
212         /* ok, come on, this isn't funny anymore, there's more code for handling this mpeg4 mess than for the actual adaptive quantization */
213
214         for(i=0; i<s->mb_num; i++){
215             int mb_xy= s->mb_index2xy[i];
216             odd += qscale_table[mb_xy]&1;
217         }
218
219         if(2*odd > s->mb_num) odd=1;
220         else                  odd=0;
221
222         for(i=0; i<s->mb_num; i++){
223             int mb_xy= s->mb_index2xy[i];
224             if((qscale_table[mb_xy]&1) != odd)
225                 qscale_table[mb_xy]++;
226             if(qscale_table[mb_xy] > 31)
227                 qscale_table[mb_xy]= 31;
228         }
229
230         for(i=1; i<s->mb_num; i++){
231             int mb_xy= s->mb_index2xy[i];
232             if(qscale_table[mb_xy] != qscale_table[s->mb_index2xy[i-1]] && (s->mb_type[mb_xy]&CANDIDATE_MB_TYPE_DIRECT)){
233                 s->mb_type[mb_xy]|= CANDIDATE_MB_TYPE_BIDIR;
234             }
235         }
236     }
237 }
238
239
240 /**
241  * encodes the dc value.
242  * @param n block index (0-3 are luma, 4-5 are chroma)
243  */
244 static inline void mpeg4_encode_dc(PutBitContext * s, int level, int n)
245 {
246 #if 1
247     /* DC will overflow if level is outside the [-255,255] range. */
248     level+=256;
249     if (n < 4) {
250         /* luminance */
251         put_bits(s, uni_DCtab_lum_len[level], uni_DCtab_lum_bits[level]);
252     } else {
253         /* chrominance */
254         put_bits(s, uni_DCtab_chrom_len[level], uni_DCtab_chrom_bits[level]);
255     }
256 #else
257     int size, v;
258     /* find number of bits */
259     size = 0;
260     v = abs(level);
261     while (v) {
262         v >>= 1;
263         size++;
264     }
265
266     if (n < 4) {
267         /* luminance */
268         put_bits(&s->pb, ff_mpeg4_DCtab_lum[size][1], ff_mpeg4_DCtab_lum[size][0]);
269     } else {
270         /* chrominance */
271         put_bits(&s->pb, ff_mpeg4_DCtab_chrom[size][1], ff_mpeg4_DCtab_chrom[size][0]);
272     }
273
274     /* encode remaining bits */
275     if (size > 0) {
276         if (level < 0)
277             level = (-level) ^ ((1 << size) - 1);
278         put_bits(&s->pb, size, level);
279         if (size > 8)
280             put_bits(&s->pb, 1, 1);
281     }
282 #endif
283 }
284
285 static inline int mpeg4_get_dc_length(int level, int n){
286     if (n < 4) {
287         return uni_DCtab_lum_len[level + 256];
288     } else {
289         return uni_DCtab_chrom_len[level + 256];
290     }
291 }
292
293 /**
294  * encodes a 8x8 block
295  * @param n block index (0-3 are luma, 4-5 are chroma)
296  */
297 static inline void mpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n, int intra_dc,
298                                uint8_t *scan_table, PutBitContext *dc_pb, PutBitContext *ac_pb)
299 {
300     int i, last_non_zero;
301     uint32_t *bits_tab;
302     uint8_t *len_tab;
303     const int last_index = s->block_last_index[n];
304
305     if (s->mb_intra) { //Note gcc (3.2.1 at least) will optimize this away
306         /* mpeg4 based DC predictor */
307         mpeg4_encode_dc(dc_pb, intra_dc, n);
308         if(last_index<1) return;
309         i = 1;
310         bits_tab= uni_mpeg4_intra_rl_bits;
311         len_tab = uni_mpeg4_intra_rl_len;
312     } else {
313         if(last_index<0) return;
314         i = 0;
315         bits_tab= uni_mpeg4_inter_rl_bits;
316         len_tab = uni_mpeg4_inter_rl_len;
317     }
318
319     /* AC coefs */
320     last_non_zero = i - 1;
321     for (; i < last_index; i++) {
322         int level = block[ scan_table[i] ];
323         if (level) {
324             int run = i - last_non_zero - 1;
325             level+=64;
326             if((level&(~127)) == 0){
327                 const int index= UNI_MPEG4_ENC_INDEX(0, run, level);
328                 put_bits(ac_pb, len_tab[index], bits_tab[index]);
329             }else{ //ESC3
330                 put_bits(ac_pb, 7+2+1+6+1+12+1, (3<<23)+(3<<21)+(0<<20)+(run<<14)+(1<<13)+(((level-64)&0xfff)<<1)+1);
331             }
332             last_non_zero = i;
333         }
334     }
335     /*if(i<=last_index)*/{
336         int level = block[ scan_table[i] ];
337         int run = i - last_non_zero - 1;
338         level+=64;
339         if((level&(~127)) == 0){
340             const int index= UNI_MPEG4_ENC_INDEX(1, run, level);
341             put_bits(ac_pb, len_tab[index], bits_tab[index]);
342         }else{ //ESC3
343             put_bits(ac_pb, 7+2+1+6+1+12+1, (3<<23)+(3<<21)+(1<<20)+(run<<14)+(1<<13)+(((level-64)&0xfff)<<1)+1);
344         }
345     }
346 }
347
348 static int mpeg4_get_block_length(MpegEncContext * s, DCTELEM * block, int n, int intra_dc,
349                                uint8_t *scan_table)
350 {
351     int i, last_non_zero;
352     uint8_t *len_tab;
353     const int last_index = s->block_last_index[n];
354     int len=0;
355
356     if (s->mb_intra) { //Note gcc (3.2.1 at least) will optimize this away
357         /* mpeg4 based DC predictor */
358         len += mpeg4_get_dc_length(intra_dc, n);
359         if(last_index<1) return len;
360         i = 1;
361         len_tab = uni_mpeg4_intra_rl_len;
362     } else {
363         if(last_index<0) return 0;
364         i = 0;
365         len_tab = uni_mpeg4_inter_rl_len;
366     }
367
368     /* AC coefs */
369     last_non_zero = i - 1;
370     for (; i < last_index; i++) {
371         int level = block[ scan_table[i] ];
372         if (level) {
373             int run = i - last_non_zero - 1;
374             level+=64;
375             if((level&(~127)) == 0){
376                 const int index= UNI_MPEG4_ENC_INDEX(0, run, level);
377                 len += len_tab[index];
378             }else{ //ESC3
379                 len += 7+2+1+6+1+12+1;
380             }
381             last_non_zero = i;
382         }
383     }
384     /*if(i<=last_index)*/{
385         int level = block[ scan_table[i] ];
386         int run = i - last_non_zero - 1;
387         level+=64;
388         if((level&(~127)) == 0){
389             const int index= UNI_MPEG4_ENC_INDEX(1, run, level);
390             len += len_tab[index];
391         }else{ //ESC3
392             len += 7+2+1+6+1+12+1;
393         }
394     }
395
396     return len;
397 }
398
399 static inline void mpeg4_encode_blocks(MpegEncContext * s, DCTELEM block[6][64], int intra_dc[6],
400                                uint8_t **scan_table, PutBitContext *dc_pb, PutBitContext *ac_pb){
401     int i;
402
403     if(scan_table){
404         if(s->flags2 & CODEC_FLAG2_NO_OUTPUT){
405             for (i = 0; i < 6; i++) {
406                 skip_put_bits(&s->pb, mpeg4_get_block_length(s, block[i], i, intra_dc[i], scan_table[i]));
407             }
408         }else{
409             /* encode each block */
410             for (i = 0; i < 6; i++) {
411                 mpeg4_encode_block(s, block[i], i, intra_dc[i], scan_table[i], dc_pb, ac_pb);
412             }
413         }
414     }else{
415         if(s->flags2 & CODEC_FLAG2_NO_OUTPUT){
416             for (i = 0; i < 6; i++) {
417                 skip_put_bits(&s->pb, mpeg4_get_block_length(s, block[i], i, 0, s->intra_scantable.permutated));
418             }
419         }else{
420             /* encode each block */
421             for (i = 0; i < 6; i++) {
422                 mpeg4_encode_block(s, block[i], i, 0, s->intra_scantable.permutated, dc_pb, ac_pb);
423             }
424         }
425     }
426 }
427
428 //FIXME this is duplicated to h263.c
429 static const int dquant_code[5]= {1,0,9,2,3};
430
431 void mpeg4_encode_mb(MpegEncContext * s,
432                     DCTELEM block[6][64],
433                     int motion_x, int motion_y)
434 {
435     int cbpc, cbpy, pred_x, pred_y;
436     PutBitContext * const pb2    = s->data_partitioning                         ? &s->pb2    : &s->pb;
437     PutBitContext * const tex_pb = s->data_partitioning && s->pict_type!=AV_PICTURE_TYPE_B ? &s->tex_pb : &s->pb;
438     PutBitContext * const dc_pb  = s->data_partitioning && s->pict_type!=AV_PICTURE_TYPE_I ? &s->pb2    : &s->pb;
439     const int interleaved_stats= (s->flags&CODEC_FLAG_PASS1) && !s->data_partitioning ? 1 : 0;
440
441     if (!s->mb_intra) {
442         int i, cbp;
443
444         if(s->pict_type==AV_PICTURE_TYPE_B){
445             static const int mb_type_table[8]= {-1, 3, 2, 1,-1,-1,-1, 0}; /* convert from mv_dir to type */
446             int mb_type=  mb_type_table[s->mv_dir];
447
448             if(s->mb_x==0){
449                 for(i=0; i<2; i++){
450                     s->last_mv[i][0][0]=
451                     s->last_mv[i][0][1]=
452                     s->last_mv[i][1][0]=
453                     s->last_mv[i][1][1]= 0;
454                 }
455             }
456
457             assert(s->dquant>=-2 && s->dquant<=2);
458             assert((s->dquant&1)==0);
459             assert(mb_type>=0);
460
461             /* nothing to do if this MB was skipped in the next P Frame */
462             if (s->next_picture.f.mbskip_table[s->mb_y * s->mb_stride + s->mb_x]) { //FIXME avoid DCT & ...
463                 s->skip_count++;
464                 s->mv[0][0][0]=
465                 s->mv[0][0][1]=
466                 s->mv[1][0][0]=
467                 s->mv[1][0][1]= 0;
468                 s->mv_dir= MV_DIR_FORWARD; //doesn't matter
469                 s->qscale -= s->dquant;
470 //                s->mb_skipped=1;
471
472                 return;
473             }
474
475             cbp= get_b_cbp(s, block, motion_x, motion_y, mb_type);
476
477             if ((cbp | motion_x | motion_y | mb_type) ==0) {
478                 /* direct MB with MV={0,0} */
479                 assert(s->dquant==0);
480
481                 put_bits(&s->pb, 1, 1); /* mb not coded modb1=1 */
482
483                 if(interleaved_stats){
484                     s->misc_bits++;
485                     s->last_bits++;
486                 }
487                 s->skip_count++;
488                 return;
489             }
490
491             put_bits(&s->pb, 1, 0);     /* mb coded modb1=0 */
492             put_bits(&s->pb, 1, cbp ? 0 : 1); /* modb2 */ //FIXME merge
493             put_bits(&s->pb, mb_type+1, 1); // this table is so simple that we don't need it :)
494             if(cbp) put_bits(&s->pb, 6, cbp);
495
496             if(cbp && mb_type){
497                 if(s->dquant)
498                     put_bits(&s->pb, 2, (s->dquant>>2)+3);
499                 else
500                     put_bits(&s->pb, 1, 0);
501             }else
502                 s->qscale -= s->dquant;
503
504             if(!s->progressive_sequence){
505                 if(cbp)
506                     put_bits(&s->pb, 1, s->interlaced_dct);
507                 if(mb_type) // not direct mode
508                     put_bits(&s->pb, 1, s->mv_type == MV_TYPE_FIELD);
509             }
510
511             if(interleaved_stats){
512                 s->misc_bits+= get_bits_diff(s);
513             }
514
515             if(mb_type == 0){
516                 assert(s->mv_dir & MV_DIRECT);
517                 ff_h263_encode_motion_vector(s, motion_x, motion_y, 1);
518                 s->b_count++;
519                 s->f_count++;
520             }else{
521                 assert(mb_type > 0 && mb_type < 4);
522                 if(s->mv_type != MV_TYPE_FIELD){
523                     if(s->mv_dir & MV_DIR_FORWARD){
524                         ff_h263_encode_motion_vector(s, s->mv[0][0][0] - s->last_mv[0][0][0],
525                                                         s->mv[0][0][1] - s->last_mv[0][0][1], s->f_code);
526                         s->last_mv[0][0][0]= s->last_mv[0][1][0]= s->mv[0][0][0];
527                         s->last_mv[0][0][1]= s->last_mv[0][1][1]= s->mv[0][0][1];
528                         s->f_count++;
529                     }
530                     if(s->mv_dir & MV_DIR_BACKWARD){
531                         ff_h263_encode_motion_vector(s, s->mv[1][0][0] - s->last_mv[1][0][0],
532                                                         s->mv[1][0][1] - s->last_mv[1][0][1], s->b_code);
533                         s->last_mv[1][0][0]= s->last_mv[1][1][0]= s->mv[1][0][0];
534                         s->last_mv[1][0][1]= s->last_mv[1][1][1]= s->mv[1][0][1];
535                         s->b_count++;
536                     }
537                 }else{
538                     if(s->mv_dir & MV_DIR_FORWARD){
539                         put_bits(&s->pb, 1, s->field_select[0][0]);
540                         put_bits(&s->pb, 1, s->field_select[0][1]);
541                     }
542                     if(s->mv_dir & MV_DIR_BACKWARD){
543                         put_bits(&s->pb, 1, s->field_select[1][0]);
544                         put_bits(&s->pb, 1, s->field_select[1][1]);
545                     }
546                     if(s->mv_dir & MV_DIR_FORWARD){
547                         for(i=0; i<2; i++){
548                             ff_h263_encode_motion_vector(s, s->mv[0][i][0] - s->last_mv[0][i][0]  ,
549                                                             s->mv[0][i][1] - s->last_mv[0][i][1]/2, s->f_code);
550                             s->last_mv[0][i][0]= s->mv[0][i][0];
551                             s->last_mv[0][i][1]= s->mv[0][i][1]*2;
552                         }
553                         s->f_count++;
554                     }
555                     if(s->mv_dir & MV_DIR_BACKWARD){
556                         for(i=0; i<2; i++){
557                             ff_h263_encode_motion_vector(s, s->mv[1][i][0] - s->last_mv[1][i][0]  ,
558                                                             s->mv[1][i][1] - s->last_mv[1][i][1]/2, s->b_code);
559                             s->last_mv[1][i][0]= s->mv[1][i][0];
560                             s->last_mv[1][i][1]= s->mv[1][i][1]*2;
561                         }
562                         s->b_count++;
563                     }
564                 }
565             }
566
567             if(interleaved_stats){
568                 s->mv_bits+= get_bits_diff(s);
569             }
570
571             mpeg4_encode_blocks(s, block, NULL, NULL, NULL, &s->pb);
572
573             if(interleaved_stats){
574                 s->p_tex_bits+= get_bits_diff(s);
575             }
576
577         }else{ /* s->pict_type==AV_PICTURE_TYPE_B */
578             cbp= get_p_cbp(s, block, motion_x, motion_y);
579
580             if ((cbp | motion_x | motion_y | s->dquant) == 0 && s->mv_type==MV_TYPE_16X16) {
581                 /* check if the B frames can skip it too, as we must skip it if we skip here
582                    why didn't they just compress the skip-mb bits instead of reusing them ?! */
583                 if(s->max_b_frames>0){
584                     int i;
585                     int x,y, offset;
586                     uint8_t *p_pic;
587
588                     x= s->mb_x*16;
589                     y= s->mb_y*16;
590
591                     offset= x + y*s->linesize;
592                     p_pic = s->new_picture.f.data[0] + offset;
593
594                     s->mb_skipped=1;
595                     for(i=0; i<s->max_b_frames; i++){
596                         uint8_t *b_pic;
597                         int diff;
598                         Picture *pic= s->reordered_input_picture[i+1];
599
600                         if (pic == NULL || pic->f.pict_type != AV_PICTURE_TYPE_B)
601                             break;
602
603                         b_pic = pic->f.data[0] + offset;
604                         if (pic->f.type != FF_BUFFER_TYPE_SHARED)
605                             b_pic+= INPLACE_OFFSET;
606
607                         if(x+16 > s->width || y+16 > s->height){
608                             int x1,y1;
609                             int xe= FFMIN(16, s->width - x);
610                             int ye= FFMIN(16, s->height- y);
611                             diff=0;
612                             for(y1=0; y1<ye; y1++){
613                                 for(x1=0; x1<xe; x1++){
614                                     diff+= FFABS(p_pic[x1+y1*s->linesize] - b_pic[x1+y1*s->linesize]);
615                                 }
616                             }
617                             diff= diff*256/(xe*ye);
618                         }else{
619                             diff= s->dsp.sad[0](NULL, p_pic, b_pic, s->linesize, 16);
620                         }
621                         if(diff>s->qscale*70){ //FIXME check that 70 is optimal
622                             s->mb_skipped=0;
623                             break;
624                         }
625                     }
626                 }else
627                     s->mb_skipped=1;
628
629                 if(s->mb_skipped==1){
630                     /* skip macroblock */
631                     put_bits(&s->pb, 1, 1);
632
633                     if(interleaved_stats){
634                         s->misc_bits++;
635                         s->last_bits++;
636                     }
637                     s->skip_count++;
638
639                     return;
640                 }
641             }
642
643             put_bits(&s->pb, 1, 0);     /* mb coded */
644             cbpc = cbp & 3;
645             cbpy = cbp >> 2;
646             cbpy ^= 0xf;
647             if(s->mv_type==MV_TYPE_16X16){
648                 if(s->dquant) cbpc+= 8;
649                 put_bits(&s->pb,
650                         ff_h263_inter_MCBPC_bits[cbpc],
651                         ff_h263_inter_MCBPC_code[cbpc]);
652
653                 put_bits(pb2, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]);
654                 if(s->dquant)
655                     put_bits(pb2, 2, dquant_code[s->dquant+2]);
656
657                 if(!s->progressive_sequence){
658                     if(cbp)
659                         put_bits(pb2, 1, s->interlaced_dct);
660                     put_bits(pb2, 1, 0);
661                 }
662
663                 if(interleaved_stats){
664                     s->misc_bits+= get_bits_diff(s);
665                 }
666
667                 /* motion vectors: 16x16 mode */
668                 h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
669
670                 ff_h263_encode_motion_vector(s, motion_x - pred_x,
671                                                 motion_y - pred_y, s->f_code);
672             }else if(s->mv_type==MV_TYPE_FIELD){
673                 if(s->dquant) cbpc+= 8;
674                 put_bits(&s->pb,
675                         ff_h263_inter_MCBPC_bits[cbpc],
676                         ff_h263_inter_MCBPC_code[cbpc]);
677
678                 put_bits(pb2, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]);
679                 if(s->dquant)
680                     put_bits(pb2, 2, dquant_code[s->dquant+2]);
681
682                 assert(!s->progressive_sequence);
683                 if(cbp)
684                     put_bits(pb2, 1, s->interlaced_dct);
685                 put_bits(pb2, 1, 1);
686
687                 if(interleaved_stats){
688                     s->misc_bits+= get_bits_diff(s);
689                 }
690
691                 /* motion vectors: 16x8 interlaced mode */
692                 h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
693                 pred_y /=2;
694
695                 put_bits(&s->pb, 1, s->field_select[0][0]);
696                 put_bits(&s->pb, 1, s->field_select[0][1]);
697
698                 ff_h263_encode_motion_vector(s, s->mv[0][0][0] - pred_x,
699                                                 s->mv[0][0][1] - pred_y, s->f_code);
700                 ff_h263_encode_motion_vector(s, s->mv[0][1][0] - pred_x,
701                                                 s->mv[0][1][1] - pred_y, s->f_code);
702             }else{
703                 assert(s->mv_type==MV_TYPE_8X8);
704                 put_bits(&s->pb,
705                         ff_h263_inter_MCBPC_bits[cbpc+16],
706                         ff_h263_inter_MCBPC_code[cbpc+16]);
707                 put_bits(pb2, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]);
708
709                 if(!s->progressive_sequence){
710                     if(cbp)
711                         put_bits(pb2, 1, s->interlaced_dct);
712                 }
713
714                 if(interleaved_stats){
715                     s->misc_bits+= get_bits_diff(s);
716                 }
717
718                 for(i=0; i<4; i++){
719                     /* motion vectors: 8x8 mode*/
720                     h263_pred_motion(s, i, 0, &pred_x, &pred_y);
721
722                     ff_h263_encode_motion_vector(s, s->current_picture.f.motion_val[0][ s->block_index[i] ][0] - pred_x,
723                                                     s->current_picture.f.motion_val[0][ s->block_index[i] ][1] - pred_y, s->f_code);
724                 }
725             }
726
727             if(interleaved_stats){
728                 s->mv_bits+= get_bits_diff(s);
729             }
730
731             mpeg4_encode_blocks(s, block, NULL, NULL, NULL, tex_pb);
732
733             if(interleaved_stats){
734                 s->p_tex_bits+= get_bits_diff(s);
735             }
736             s->f_count++;
737         }
738     } else {
739         int cbp;
740         int dc_diff[6];   //dc values with the dc prediction subtracted
741         int dir[6];  //prediction direction
742         int zigzag_last_index[6];
743         uint8_t *scan_table[6];
744         int i;
745
746         for(i=0; i<6; i++){
747             dc_diff[i]= ff_mpeg4_pred_dc(s, i, block[i][0], &dir[i], 1);
748         }
749
750         if(s->flags & CODEC_FLAG_AC_PRED){
751             s->ac_pred= decide_ac_pred(s, block, dir, scan_table, zigzag_last_index);
752         }else{
753             for(i=0; i<6; i++)
754                 scan_table[i]= s->intra_scantable.permutated;
755         }
756
757         /* compute cbp */
758         cbp = 0;
759         for (i = 0; i < 6; i++) {
760             if (s->block_last_index[i] >= 1)
761                 cbp |= 1 << (5 - i);
762         }
763
764         cbpc = cbp & 3;
765         if (s->pict_type == AV_PICTURE_TYPE_I) {
766             if(s->dquant) cbpc+=4;
767             put_bits(&s->pb,
768                 ff_h263_intra_MCBPC_bits[cbpc],
769                 ff_h263_intra_MCBPC_code[cbpc]);
770         } else {
771             if(s->dquant) cbpc+=8;
772             put_bits(&s->pb, 1, 0);     /* mb coded */
773             put_bits(&s->pb,
774                 ff_h263_inter_MCBPC_bits[cbpc + 4],
775                 ff_h263_inter_MCBPC_code[cbpc + 4]);
776         }
777         put_bits(pb2, 1, s->ac_pred);
778         cbpy = cbp >> 2;
779         put_bits(pb2, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]);
780         if(s->dquant)
781             put_bits(dc_pb, 2, dquant_code[s->dquant+2]);
782
783         if(!s->progressive_sequence){
784             put_bits(dc_pb, 1, s->interlaced_dct);
785         }
786
787         if(interleaved_stats){
788             s->misc_bits+= get_bits_diff(s);
789         }
790
791         mpeg4_encode_blocks(s, block, dc_diff, scan_table, dc_pb, tex_pb);
792
793         if(interleaved_stats){
794             s->i_tex_bits+= get_bits_diff(s);
795         }
796         s->i_count++;
797
798         /* restore ac coeffs & last_index stuff if we messed them up with the prediction */
799         if(s->ac_pred)
800             restore_ac_coeffs(s, block, dir, scan_table, zigzag_last_index);
801     }
802 }
803
804 /**
805  * add mpeg4 stuffing bits (01...1)
806  */
807 void ff_mpeg4_stuffing(PutBitContext * pbc)
808 {
809     int length;
810     put_bits(pbc, 1, 0);
811     length= (-put_bits_count(pbc))&7;
812     if(length) put_bits(pbc, length, (1<<length)-1);
813 }
814
815 /* must be called before writing the header */
816 void ff_set_mpeg4_time(MpegEncContext * s){
817     if(s->pict_type==AV_PICTURE_TYPE_B){
818         ff_mpeg4_init_direct_mv(s);
819     }else{
820         s->last_time_base= s->time_base;
821         s->time_base= FFUDIV(s->time, s->avctx->time_base.den);
822     }
823 }
824
825 static void mpeg4_encode_gop_header(MpegEncContext * s){
826     int hours, minutes, seconds;
827     int64_t time;
828
829     put_bits(&s->pb, 16, 0);
830     put_bits(&s->pb, 16, GOP_STARTCODE);
831
832     time = s->current_picture_ptr->f.pts;
833     if(s->reordered_input_picture[1])
834         time = FFMIN(time, s->reordered_input_picture[1]->f.pts);
835     time= time*s->avctx->time_base.num;
836     s->last_time_base= FFUDIV(time, s->avctx->time_base.den);
837
838     seconds= FFUDIV(time, s->avctx->time_base.den);
839     minutes= FFUDIV(seconds, 60); seconds = FFUMOD(seconds, 60);
840     hours  = FFUDIV(minutes, 60); minutes = FFUMOD(minutes, 60);
841     hours  = FFUMOD(hours  , 24);
842
843     put_bits(&s->pb, 5, hours);
844     put_bits(&s->pb, 6, minutes);
845     put_bits(&s->pb, 1, 1);
846     put_bits(&s->pb, 6, seconds);
847
848     put_bits(&s->pb, 1, !!(s->flags&CODEC_FLAG_CLOSED_GOP));
849     put_bits(&s->pb, 1, 0); //broken link == NO
850
851     ff_mpeg4_stuffing(&s->pb);
852 }
853
854 static void mpeg4_encode_visual_object_header(MpegEncContext * s){
855     int profile_and_level_indication;
856     int vo_ver_id;
857
858     if(s->avctx->profile != FF_PROFILE_UNKNOWN){
859         profile_and_level_indication = s->avctx->profile << 4;
860     }else if(s->max_b_frames || s->quarter_sample){
861         profile_and_level_indication= 0xF0; // adv simple
862     }else{
863         profile_and_level_indication= 0x00; // simple
864     }
865
866     if(s->avctx->level != FF_LEVEL_UNKNOWN){
867         profile_and_level_indication |= s->avctx->level;
868     }else{
869         profile_and_level_indication |= 1; //level 1
870     }
871
872     if(profile_and_level_indication>>4 == 0xF){
873         vo_ver_id= 5;
874     }else{
875         vo_ver_id= 1;
876     }
877
878     //FIXME levels
879
880     put_bits(&s->pb, 16, 0);
881     put_bits(&s->pb, 16, VOS_STARTCODE);
882
883     put_bits(&s->pb, 8, profile_and_level_indication);
884
885     put_bits(&s->pb, 16, 0);
886     put_bits(&s->pb, 16, VISUAL_OBJ_STARTCODE);
887
888     put_bits(&s->pb, 1, 1);
889         put_bits(&s->pb, 4, vo_ver_id);
890         put_bits(&s->pb, 3, 1); //priority
891
892     put_bits(&s->pb, 4, 1); //visual obj type== video obj
893
894     put_bits(&s->pb, 1, 0); //video signal type == no clue //FIXME
895
896     ff_mpeg4_stuffing(&s->pb);
897 }
898
899 static void mpeg4_encode_vol_header(MpegEncContext * s, int vo_number, int vol_number)
900 {
901     int vo_ver_id;
902
903     if (!CONFIG_MPEG4_ENCODER)  return;
904
905     if(s->max_b_frames || s->quarter_sample){
906         vo_ver_id= 5;
907         s->vo_type= ADV_SIMPLE_VO_TYPE;
908     }else{
909         vo_ver_id= 1;
910         s->vo_type= SIMPLE_VO_TYPE;
911     }
912
913     put_bits(&s->pb, 16, 0);
914     put_bits(&s->pb, 16, 0x100 + vo_number);        /* video obj */
915     put_bits(&s->pb, 16, 0);
916     put_bits(&s->pb, 16, 0x120 + vol_number);       /* video obj layer */
917
918     put_bits(&s->pb, 1, 0);             /* random access vol */
919     put_bits(&s->pb, 8, s->vo_type);    /* video obj type indication */
920     if(s->workaround_bugs & FF_BUG_MS) {
921         put_bits(&s->pb, 1, 0);         /* is obj layer id= no */
922     } else {
923         put_bits(&s->pb, 1, 1);         /* is obj layer id= yes */
924         put_bits(&s->pb, 4, vo_ver_id); /* is obj layer ver id */
925         put_bits(&s->pb, 3, 1);         /* is obj layer priority */
926     }
927
928     s->aspect_ratio_info= ff_h263_aspect_to_info(s->avctx->sample_aspect_ratio);
929
930     put_bits(&s->pb, 4, s->aspect_ratio_info);/* aspect ratio info */
931     if (s->aspect_ratio_info == FF_ASPECT_EXTENDED){
932         put_bits(&s->pb, 8, s->avctx->sample_aspect_ratio.num);
933         put_bits(&s->pb, 8, s->avctx->sample_aspect_ratio.den);
934     }
935
936     if(s->workaround_bugs & FF_BUG_MS) { //
937         put_bits(&s->pb, 1, 0);         /* vol control parameters= no @@@ */
938     } else {
939         put_bits(&s->pb, 1, 1);         /* vol control parameters= yes */
940         put_bits(&s->pb, 2, 1);         /* chroma format YUV 420/YV12 */
941         put_bits(&s->pb, 1, s->low_delay);
942         put_bits(&s->pb, 1, 0);         /* vbv parameters= no */
943     }
944
945     put_bits(&s->pb, 2, RECT_SHAPE);    /* vol shape= rectangle */
946     put_bits(&s->pb, 1, 1);             /* marker bit */
947
948     put_bits(&s->pb, 16, s->avctx->time_base.den);
949     if (s->time_increment_bits < 1)
950         s->time_increment_bits = 1;
951     put_bits(&s->pb, 1, 1);             /* marker bit */
952     put_bits(&s->pb, 1, 0);             /* fixed vop rate=no */
953     put_bits(&s->pb, 1, 1);             /* marker bit */
954     put_bits(&s->pb, 13, s->width);     /* vol width */
955     put_bits(&s->pb, 1, 1);             /* marker bit */
956     put_bits(&s->pb, 13, s->height);    /* vol height */
957     put_bits(&s->pb, 1, 1);             /* marker bit */
958     put_bits(&s->pb, 1, s->progressive_sequence ? 0 : 1);
959     put_bits(&s->pb, 1, 1);             /* obmc disable */
960     if (vo_ver_id == 1) {
961         put_bits(&s->pb, 1, s->vol_sprite_usage);       /* sprite enable */
962     }else{
963         put_bits(&s->pb, 2, s->vol_sprite_usage);       /* sprite enable */
964     }
965
966     put_bits(&s->pb, 1, 0);             /* not 8 bit == false */
967     put_bits(&s->pb, 1, s->mpeg_quant); /* quant type= (0=h263 style)*/
968
969     if(s->mpeg_quant){
970         ff_write_quant_matrix(&s->pb, s->avctx->intra_matrix);
971         ff_write_quant_matrix(&s->pb, s->avctx->inter_matrix);
972     }
973
974     if (vo_ver_id != 1)
975         put_bits(&s->pb, 1, s->quarter_sample);
976     put_bits(&s->pb, 1, 1);             /* complexity estimation disable */
977     s->resync_marker= s->rtp_mode;
978     put_bits(&s->pb, 1, s->resync_marker ? 0 : 1);/* resync marker disable */
979     put_bits(&s->pb, 1, s->data_partitioning ? 1 : 0);
980     if(s->data_partitioning){
981         put_bits(&s->pb, 1, 0);         /* no rvlc */
982     }
983
984     if (vo_ver_id != 1){
985         put_bits(&s->pb, 1, 0);         /* newpred */
986         put_bits(&s->pb, 1, 0);         /* reduced res vop */
987     }
988     put_bits(&s->pb, 1, 0);             /* scalability */
989
990     ff_mpeg4_stuffing(&s->pb);
991
992     /* user data */
993     if(!(s->flags & CODEC_FLAG_BITEXACT)){
994         put_bits(&s->pb, 16, 0);
995         put_bits(&s->pb, 16, 0x1B2);    /* user_data */
996         ff_put_string(&s->pb, LIBAVCODEC_IDENT, 0);
997     }
998 }
999
1000 /* write mpeg4 VOP header */
1001 void mpeg4_encode_picture_header(MpegEncContext * s, int picture_number)
1002 {
1003     int time_incr;
1004     int time_div, time_mod;
1005
1006     if(s->pict_type==AV_PICTURE_TYPE_I){
1007         if(!(s->flags&CODEC_FLAG_GLOBAL_HEADER)){
1008             if(s->strict_std_compliance < FF_COMPLIANCE_VERY_STRICT) //HACK, the reference sw is buggy
1009                 mpeg4_encode_visual_object_header(s);
1010             if(s->strict_std_compliance < FF_COMPLIANCE_VERY_STRICT || picture_number==0) //HACK, the reference sw is buggy
1011                 mpeg4_encode_vol_header(s, 0, 0);
1012         }
1013         if(!(s->workaround_bugs & FF_BUG_MS))
1014             mpeg4_encode_gop_header(s);
1015     }
1016
1017     s->partitioned_frame= s->data_partitioning && s->pict_type!=AV_PICTURE_TYPE_B;
1018
1019     put_bits(&s->pb, 16, 0);                /* vop header */
1020     put_bits(&s->pb, 16, VOP_STARTCODE);    /* vop header */
1021     put_bits(&s->pb, 2, s->pict_type - 1);  /* pict type: I = 0 , P = 1 */
1022
1023     time_div= FFUDIV(s->time, s->avctx->time_base.den);
1024     time_mod= FFUMOD(s->time, s->avctx->time_base.den);
1025     time_incr= time_div - s->last_time_base;
1026     assert(time_incr >= 0);
1027     while(time_incr--)
1028         put_bits(&s->pb, 1, 1);
1029
1030     put_bits(&s->pb, 1, 0);
1031
1032     put_bits(&s->pb, 1, 1);                             /* marker */
1033     put_bits(&s->pb, s->time_increment_bits, time_mod); /* time increment */
1034     put_bits(&s->pb, 1, 1);                             /* marker */
1035     put_bits(&s->pb, 1, 1);                             /* vop coded */
1036     if (    s->pict_type == AV_PICTURE_TYPE_P
1037         || (s->pict_type == AV_PICTURE_TYPE_S && s->vol_sprite_usage==GMC_SPRITE)) {
1038         put_bits(&s->pb, 1, s->no_rounding);    /* rounding type */
1039     }
1040     put_bits(&s->pb, 3, 0);     /* intra dc VLC threshold */
1041     if(!s->progressive_sequence){
1042          put_bits(&s->pb, 1, s->current_picture_ptr->f.top_field_first);
1043          put_bits(&s->pb, 1, s->alternate_scan);
1044     }
1045     //FIXME sprite stuff
1046
1047     put_bits(&s->pb, 5, s->qscale);
1048
1049     if (s->pict_type != AV_PICTURE_TYPE_I)
1050         put_bits(&s->pb, 3, s->f_code); /* fcode_for */
1051     if (s->pict_type == AV_PICTURE_TYPE_B)
1052         put_bits(&s->pb, 3, s->b_code); /* fcode_back */
1053 }
1054
1055
1056 static void init_uni_dc_tab(void)
1057 {
1058     int level, uni_code, uni_len;
1059
1060     for(level=-256; level<256; level++){
1061         int size, v, l;
1062         /* find number of bits */
1063         size = 0;
1064         v = abs(level);
1065         while (v) {
1066             v >>= 1;
1067             size++;
1068         }
1069
1070         if (level < 0)
1071             l= (-level) ^ ((1 << size) - 1);
1072         else
1073             l= level;
1074
1075         /* luminance */
1076         uni_code= ff_mpeg4_DCtab_lum[size][0];
1077         uni_len = ff_mpeg4_DCtab_lum[size][1];
1078
1079         if (size > 0) {
1080             uni_code<<=size; uni_code|=l;
1081             uni_len+=size;
1082             if (size > 8){
1083                 uni_code<<=1; uni_code|=1;
1084                 uni_len++;
1085             }
1086         }
1087         uni_DCtab_lum_bits[level+256]= uni_code;
1088         uni_DCtab_lum_len [level+256]= uni_len;
1089
1090         /* chrominance */
1091         uni_code= ff_mpeg4_DCtab_chrom[size][0];
1092         uni_len = ff_mpeg4_DCtab_chrom[size][1];
1093
1094         if (size > 0) {
1095             uni_code<<=size; uni_code|=l;
1096             uni_len+=size;
1097             if (size > 8){
1098                 uni_code<<=1; uni_code|=1;
1099                 uni_len++;
1100             }
1101         }
1102         uni_DCtab_chrom_bits[level+256]= uni_code;
1103         uni_DCtab_chrom_len [level+256]= uni_len;
1104
1105     }
1106 }
1107
1108 static void init_uni_mpeg4_rl_tab(RLTable *rl, uint32_t *bits_tab, uint8_t *len_tab){
1109     int slevel, run, last;
1110
1111     assert(MAX_LEVEL >= 64);
1112     assert(MAX_RUN   >= 63);
1113
1114     for(slevel=-64; slevel<64; slevel++){
1115         if(slevel==0) continue;
1116         for(run=0; run<64; run++){
1117             for(last=0; last<=1; last++){
1118                 const int index= UNI_MPEG4_ENC_INDEX(last, run, slevel+64);
1119                 int level= slevel < 0 ? -slevel : slevel;
1120                 int sign= slevel < 0 ? 1 : 0;
1121                 int bits, len, code;
1122                 int level1, run1;
1123
1124                 len_tab[index]= 100;
1125
1126                 /* ESC0 */
1127                 code= get_rl_index(rl, last, run, level);
1128                 bits= rl->table_vlc[code][0];
1129                 len=  rl->table_vlc[code][1];
1130                 bits=bits*2+sign; len++;
1131
1132                 if(code!=rl->n && len < len_tab[index]){
1133                     bits_tab[index]= bits;
1134                     len_tab [index]= len;
1135                 }
1136                 /* ESC1 */
1137                 bits= rl->table_vlc[rl->n][0];
1138                 len=  rl->table_vlc[rl->n][1];
1139                 bits=bits*2;    len++; //esc1
1140                 level1= level - rl->max_level[last][run];
1141                 if(level1>0){
1142                     code= get_rl_index(rl, last, run, level1);
1143                     bits<<= rl->table_vlc[code][1];
1144                     len  += rl->table_vlc[code][1];
1145                     bits += rl->table_vlc[code][0];
1146                     bits=bits*2+sign; len++;
1147
1148                     if(code!=rl->n && len < len_tab[index]){
1149                         bits_tab[index]= bits;
1150                         len_tab [index]= len;
1151                     }
1152                 }
1153                 /* ESC2 */
1154                 bits= rl->table_vlc[rl->n][0];
1155                 len=  rl->table_vlc[rl->n][1];
1156                 bits=bits*4+2;    len+=2; //esc2
1157                 run1 = run - rl->max_run[last][level] - 1;
1158                 if(run1>=0){
1159                     code= get_rl_index(rl, last, run1, level);
1160                     bits<<= rl->table_vlc[code][1];
1161                     len  += rl->table_vlc[code][1];
1162                     bits += rl->table_vlc[code][0];
1163                     bits=bits*2+sign; len++;
1164
1165                     if(code!=rl->n && len < len_tab[index]){
1166                         bits_tab[index]= bits;
1167                         len_tab [index]= len;
1168                     }
1169                 }
1170                 /* ESC3 */
1171                 bits= rl->table_vlc[rl->n][0];
1172                 len = rl->table_vlc[rl->n][1];
1173                 bits=bits*4+3;    len+=2; //esc3
1174                 bits=bits*2+last; len++;
1175                 bits=bits*64+run; len+=6;
1176                 bits=bits*2+1;    len++;  //marker
1177                 bits=bits*4096+(slevel&0xfff); len+=12;
1178                 bits=bits*2+1;    len++;  //marker
1179
1180                 if(len < len_tab[index]){
1181                     bits_tab[index]= bits;
1182                     len_tab [index]= len;
1183                 }
1184             }
1185         }
1186     }
1187 }
1188
1189 static av_cold int encode_init(AVCodecContext *avctx)
1190 {
1191     MpegEncContext *s = avctx->priv_data;
1192     int ret;
1193     static int done = 0;
1194
1195     if((ret=MPV_encode_init(avctx)) < 0)
1196         return ret;
1197
1198     if (!done) {
1199         done = 1;
1200
1201         init_uni_dc_tab();
1202
1203         init_rl(&ff_mpeg4_rl_intra, ff_mpeg4_static_rl_table_store[0]);
1204
1205         init_uni_mpeg4_rl_tab(&ff_mpeg4_rl_intra, uni_mpeg4_intra_rl_bits, uni_mpeg4_intra_rl_len);
1206         init_uni_mpeg4_rl_tab(&ff_h263_rl_inter, uni_mpeg4_inter_rl_bits, uni_mpeg4_inter_rl_len);
1207     }
1208
1209     s->min_qcoeff= -2048;
1210     s->max_qcoeff=  2047;
1211     s->intra_ac_vlc_length     = uni_mpeg4_intra_rl_len;
1212     s->intra_ac_vlc_last_length= uni_mpeg4_intra_rl_len + 128*64;
1213     s->inter_ac_vlc_length     = uni_mpeg4_inter_rl_len;
1214     s->inter_ac_vlc_last_length= uni_mpeg4_inter_rl_len + 128*64;
1215     s->luma_dc_vlc_length= uni_DCtab_lum_len;
1216     s->chroma_dc_vlc_length= uni_DCtab_chrom_len;
1217     s->ac_esc_length= 7+2+1+6+1+12+1;
1218     s->y_dc_scale_table= ff_mpeg4_y_dc_scale_table;
1219     s->c_dc_scale_table= ff_mpeg4_c_dc_scale_table;
1220
1221     if(s->flags & CODEC_FLAG_GLOBAL_HEADER){
1222
1223         s->avctx->extradata= av_malloc(1024);
1224         init_put_bits(&s->pb, s->avctx->extradata, 1024);
1225
1226         if(!(s->workaround_bugs & FF_BUG_MS))
1227             mpeg4_encode_visual_object_header(s);
1228         mpeg4_encode_vol_header(s, 0, 0);
1229
1230 //            ff_mpeg4_stuffing(&s->pb); ?
1231         flush_put_bits(&s->pb);
1232         s->avctx->extradata_size= (put_bits_count(&s->pb)+7)>>3;
1233     }
1234     return 0;
1235 }
1236
1237 void ff_mpeg4_init_partitions(MpegEncContext *s)
1238 {
1239     uint8_t *start= put_bits_ptr(&s->pb);
1240     uint8_t *end= s->pb.buf_end;
1241     int size= end - start;
1242     int pb_size = (((intptr_t)start + size/3)&(~3)) - (intptr_t)start;
1243     int tex_size= (size - 2*pb_size)&(~3);
1244
1245     set_put_bits_buffer_size(&s->pb, pb_size);
1246     init_put_bits(&s->tex_pb, start + pb_size           , tex_size);
1247     init_put_bits(&s->pb2   , start + pb_size + tex_size, pb_size);
1248 }
1249
1250 void ff_mpeg4_merge_partitions(MpegEncContext *s)
1251 {
1252     const int pb2_len   = put_bits_count(&s->pb2   );
1253     const int tex_pb_len= put_bits_count(&s->tex_pb);
1254     const int bits= put_bits_count(&s->pb);
1255
1256     if(s->pict_type==AV_PICTURE_TYPE_I){
1257         put_bits(&s->pb, 19, DC_MARKER);
1258         s->misc_bits+=19 + pb2_len + bits - s->last_bits;
1259         s->i_tex_bits+= tex_pb_len;
1260     }else{
1261         put_bits(&s->pb, 17, MOTION_MARKER);
1262         s->misc_bits+=17 + pb2_len;
1263         s->mv_bits+= bits - s->last_bits;
1264         s->p_tex_bits+= tex_pb_len;
1265     }
1266
1267     flush_put_bits(&s->pb2);
1268     flush_put_bits(&s->tex_pb);
1269
1270     set_put_bits_buffer_size(&s->pb, s->pb2.buf_end - s->pb.buf);
1271     ff_copy_bits(&s->pb, s->pb2.buf   , pb2_len);
1272     ff_copy_bits(&s->pb, s->tex_pb.buf, tex_pb_len);
1273     s->last_bits= put_bits_count(&s->pb);
1274 }
1275
1276
1277 void ff_mpeg4_encode_video_packet_header(MpegEncContext *s)
1278 {
1279     int mb_num_bits= av_log2(s->mb_num - 1) + 1;
1280
1281     put_bits(&s->pb, ff_mpeg4_get_video_packet_prefix_length(s), 0);
1282     put_bits(&s->pb, 1, 1);
1283
1284     put_bits(&s->pb, mb_num_bits, s->mb_x + s->mb_y*s->mb_width);
1285     put_bits(&s->pb, s->quant_precision, s->qscale);
1286     put_bits(&s->pb, 1, 0); /* no HEC */
1287 }
1288
1289 #define OFFSET(x) offsetof(MpegEncContext, x)
1290 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1291 static const AVOption options[] = {
1292     { "data_partitioning",       "Use data partitioning.",      OFFSET(data_partitioning), FF_OPT_TYPE_INT, { 0 }, 0, 1, VE },
1293     { "alternate_scan",          "Enable alternate scantable.", OFFSET(alternate_scan),    FF_OPT_TYPE_INT, { 0 }, 0, 1, VE },
1294     { NULL },
1295 };
1296
1297 static const AVClass mpeg4enc_class = {
1298     .class_name = "MPEG4 encoder",
1299     .item_name  = av_default_item_name,
1300     .option     = options,
1301     .version    = LIBAVUTIL_VERSION_INT,
1302 };
1303
1304 AVCodec ff_mpeg4_encoder = {
1305     .name           = "mpeg4",
1306     .type           = AVMEDIA_TYPE_VIDEO,
1307     .id             = CODEC_ID_MPEG4,
1308     .priv_data_size = sizeof(MpegEncContext),
1309     .init           = encode_init,
1310     .encode         = MPV_encode_picture,
1311     .close          = MPV_encode_end,
1312     .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
1313     .capabilities= CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS,
1314     .long_name= NULL_IF_CONFIG_SMALL("MPEG-4 part 2"),
1315     .priv_class     = &mpeg4enc_class,
1316 };