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