2 * MSMPEG4 encoder backend
3 * Copyright (c) 2001 Fabrice Bellard
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
6 * msmpeg4v1 & v2 stuff by Michael Niedermayer <michaelni@gmx.at>
8 * This file is part of FFmpeg.
10 * FFmpeg is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * FFmpeg is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with FFmpeg; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 * MSMPEG4 encoder backend
33 #include "libavutil/attributes.h"
34 #include "libavutil/avutil.h"
35 #include "libavutil/mem.h"
36 #include "mpegvideo.h"
38 #include "mpeg4video.h"
40 #include "msmpeg4data.h"
45 static uint8_t rl_length[NB_RL_TABLES][MAX_LEVEL+1][MAX_RUN+1][2];
47 /* build the table which associate a (x,y) motion vector to a vlc */
48 static av_cold int init_mv_table(MVTable *tab)
52 tab->table_mv_index = av_malloc(sizeof(uint16_t) * 4096);
53 if (!tab->table_mv_index)
54 return AVERROR(ENOMEM);
56 /* mark all entries as not used */
58 tab->table_mv_index[i] = tab->n;
60 for(i=0;i<tab->n;i++) {
61 x = tab->table_mvx[i];
62 y = tab->table_mvy[i];
63 tab->table_mv_index[(x << 6) | y] = i;
69 void ff_msmpeg4_code012(PutBitContext *pb, int n)
75 put_bits(pb, 1, (n >= 2));
79 static int get_size_of_code(MpegEncContext * s, RLTable *rl, int last, int run, int level, int intra){
82 int run_diff= intra ? 0 : 1;
84 code = get_rl_index(rl, last, run, level);
85 size+= rl->table_vlc[code][1];
89 level1 = level - rl->max_level[last][run];
92 code = get_rl_index(rl, last, run, level1);
96 if (level > MAX_LEVEL)
98 run1 = run - rl->max_run[last][level] - run_diff;
101 code = get_rl_index(rl, last, run1, level);
108 size+= 1+1+ rl->table_vlc[code][1];
112 size+= 1+1+ rl->table_vlc[code][1];
120 av_cold int ff_msmpeg4_encode_init(MpegEncContext *s)
122 static int init_done=0;
125 ff_msmpeg4_common_init(s);
126 if(s->msmpeg4_version>=4){
132 /* init various encoding tables */
134 if ((ret = init_mv_table(&ff_mv_tables[0])) < 0)
136 if ((ret = init_mv_table(&ff_mv_tables[1])) < 0)
138 for(i=0;i<NB_RL_TABLES;i++)
139 ff_init_rl(&ff_rl_table[i], ff_static_rl_table_store[i]);
141 for(i=0; i<NB_RL_TABLES; i++){
143 for (level = 1; level <= MAX_LEVEL; level++) {
145 for(run=0; run<=MAX_RUN; run++){
147 for(last=0; last<2; last++){
148 rl_length[i][level][run][last]= get_size_of_code(s, &ff_rl_table[ i], last, run, level, 0);
158 static void find_best_tables(MpegEncContext * s)
161 int best = 0, best_size = INT_MAX;
162 int chroma_best = 0, best_chroma_size = INT_MAX;
173 for(level=0; level<=MAX_LEVEL; level++){
175 for(run=0; run<=MAX_RUN; run++){
177 const int last_size= size + chroma_size;
178 for(last=0; last<2; last++){
179 int inter_count = s->ac_stats[0][0][level][run][last] + s->ac_stats[0][1][level][run][last];
180 int intra_luma_count = s->ac_stats[1][0][level][run][last];
181 int intra_chroma_count= s->ac_stats[1][1][level][run][last];
183 if(s->pict_type==AV_PICTURE_TYPE_I){
184 size += intra_luma_count *rl_length[i ][level][run][last];
185 chroma_size+= intra_chroma_count*rl_length[i+3][level][run][last];
187 size+= intra_luma_count *rl_length[i ][level][run][last]
188 +intra_chroma_count*rl_length[i+3][level][run][last]
189 +inter_count *rl_length[i+3][level][run][last];
192 if(last_size == size+chroma_size) break;
199 if(chroma_size<best_chroma_size){
200 best_chroma_size= chroma_size;
205 if(s->pict_type==AV_PICTURE_TYPE_P) chroma_best= best;
207 memset(s->ac_stats, 0, sizeof(int)*(MAX_LEVEL+1)*(MAX_RUN+1)*2*2*2);
209 s->rl_table_index = best;
210 s->rl_chroma_table_index= chroma_best;
212 if(s->pict_type != s->last_non_b_pict_type){
213 s->rl_table_index= 2;
214 if(s->pict_type==AV_PICTURE_TYPE_I)
215 s->rl_chroma_table_index= 1;
217 s->rl_chroma_table_index= 2;
222 /* write MSMPEG4 compatible frame header */
223 void ff_msmpeg4_encode_picture_header(MpegEncContext * s, int picture_number)
227 avpriv_align_put_bits(&s->pb);
228 put_bits(&s->pb, 2, s->pict_type - 1);
230 put_bits(&s->pb, 5, s->qscale);
231 if(s->msmpeg4_version<=2){
232 s->rl_table_index = 2;
233 s->rl_chroma_table_index = 2;
236 s->dc_table_index = 1;
237 s->mv_table_index = 1; /* only if P frame */
238 s->use_skip_mb_code = 1; /* only if P frame */
239 s->per_mb_rl_table = 0;
240 if(s->msmpeg4_version==4)
241 s->inter_intra_pred= (s->width*s->height < 320*240 && s->bit_rate<=II_BITRATE && s->pict_type==AV_PICTURE_TYPE_P);
242 av_dlog(s, "%d %d %d %d %d\n", s->pict_type, s->bit_rate,
243 s->inter_intra_pred, s->width, s->height);
245 if (s->pict_type == AV_PICTURE_TYPE_I) {
246 s->slice_height= s->mb_height/1;
247 put_bits(&s->pb, 5, 0x16 + s->mb_height/s->slice_height);
249 if(s->msmpeg4_version==4){
250 ff_msmpeg4_encode_ext_header(s);
251 if(s->bit_rate>MBAC_BITRATE)
252 put_bits(&s->pb, 1, s->per_mb_rl_table);
255 if(s->msmpeg4_version>2){
256 if(!s->per_mb_rl_table){
257 ff_msmpeg4_code012(&s->pb, s->rl_chroma_table_index);
258 ff_msmpeg4_code012(&s->pb, s->rl_table_index);
261 put_bits(&s->pb, 1, s->dc_table_index);
264 put_bits(&s->pb, 1, s->use_skip_mb_code);
266 if(s->msmpeg4_version==4 && s->bit_rate>MBAC_BITRATE)
267 put_bits(&s->pb, 1, s->per_mb_rl_table);
269 if(s->msmpeg4_version>2){
270 if(!s->per_mb_rl_table)
271 ff_msmpeg4_code012(&s->pb, s->rl_table_index);
273 put_bits(&s->pb, 1, s->dc_table_index);
275 put_bits(&s->pb, 1, s->mv_table_index);
279 s->esc3_level_length= 0;
280 s->esc3_run_length= 0;
283 void ff_msmpeg4_encode_ext_header(MpegEncContext * s)
285 unsigned fps = s->avctx->time_base.den / s->avctx->time_base.num / FFMAX(s->avctx->ticks_per_frame, 1);
286 put_bits(&s->pb, 5, FFMIN(fps, 31)); //yes 29.97 -> 29
288 put_bits(&s->pb, 11, FFMIN(s->bit_rate/1024, 2047));
290 if(s->msmpeg4_version>=3)
291 put_bits(&s->pb, 1, s->flipflop_rounding);
293 av_assert0(s->flipflop_rounding==0);
296 void ff_msmpeg4_encode_motion(MpegEncContext * s,
302 /* modulo encoding */
303 /* WARNING : you cannot reach all the MVs even with the modulo
304 encoding. This is a somewhat strange compromise they took !!! */
317 if ((unsigned)mx >= 64 ||
319 av_log(s->avctx, AV_LOG_ERROR, "error mx=%d my=%d\n", mx, my);
321 mv = &ff_mv_tables[s->mv_table_index];
323 code = mv->table_mv_index[(mx << 6) | my];
325 mv->table_mv_bits[code],
326 mv->table_mv_code[code]);
328 /* escape : code literally */
329 put_bits(&s->pb, 6, mx);
330 put_bits(&s->pb, 6, my);
334 void ff_msmpeg4_handle_slices(MpegEncContext *s){
336 if (s->slice_height && (s->mb_y % s->slice_height) == 0) {
337 if(s->msmpeg4_version < 4){
338 ff_mpeg4_clean_buffers(s);
340 s->first_slice_line = 1;
342 s->first_slice_line = 0;
347 static void msmpeg4v2_encode_motion(MpegEncContext * s, int val)
349 int range, bit_size, sign, code, bits;
354 put_bits(&s->pb, ff_mvtab[code][1], ff_mvtab[code][0]);
356 bit_size = s->f_code - 1;
357 range = 1 << bit_size;
370 code = (val >> bit_size) + 1;
371 bits = val & (range - 1);
373 put_bits(&s->pb, ff_mvtab[code][1] + 1, (ff_mvtab[code][0] << 1) | sign);
375 put_bits(&s->pb, bit_size, bits);
380 void ff_msmpeg4_encode_mb(MpegEncContext * s,
381 int16_t block[6][64],
382 int motion_x, int motion_y)
384 int cbp, coded_cbp, i;
386 uint8_t *coded_block;
388 ff_msmpeg4_handle_slices(s);
393 for (i = 0; i < 6; i++) {
394 if (s->block_last_index[i] >= 0)
397 if (s->use_skip_mb_code && (cbp | motion_x | motion_y) == 0) {
398 /* skip macroblock */
399 put_bits(&s->pb, 1, 1);
406 if (s->use_skip_mb_code)
407 put_bits(&s->pb, 1, 0); /* mb coded */
409 if(s->msmpeg4_version<=2){
411 ff_v2_mb_type[cbp&3][1],
412 ff_v2_mb_type[cbp&3][0]);
413 if((cbp&3) != 3) coded_cbp= cbp ^ 0x3C;
417 ff_h263_cbpy_tab[coded_cbp>>2][1],
418 ff_h263_cbpy_tab[coded_cbp>>2][0]);
420 s->misc_bits += get_bits_diff(s);
422 ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
423 msmpeg4v2_encode_motion(s, motion_x - pred_x);
424 msmpeg4v2_encode_motion(s, motion_y - pred_y);
427 ff_table_mb_non_intra[cbp + 64][1],
428 ff_table_mb_non_intra[cbp + 64][0]);
430 s->misc_bits += get_bits_diff(s);
433 ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
434 ff_msmpeg4_encode_motion(s, motion_x - pred_x,
438 s->mv_bits += get_bits_diff(s);
440 for (i = 0; i < 6; i++) {
441 ff_msmpeg4_encode_block(s, block[i], i);
443 s->p_tex_bits += get_bits_diff(s);
448 for (i = 0; i < 6; i++) {
450 val = (s->block_last_index[i] >= 1);
451 cbp |= val << (5 - i);
453 /* predict value for close blocks only for luma */
454 pred = ff_msmpeg4_coded_block_pred(s, i, &coded_block);
458 coded_cbp |= val << (5 - i);
461 if(s->msmpeg4_version<=2){
462 if (s->pict_type == AV_PICTURE_TYPE_I) {
464 ff_v2_intra_cbpc[cbp&3][1], ff_v2_intra_cbpc[cbp&3][0]);
466 if (s->use_skip_mb_code)
467 put_bits(&s->pb, 1, 0); /* mb coded */
469 ff_v2_mb_type[(cbp&3) + 4][1],
470 ff_v2_mb_type[(cbp&3) + 4][0]);
472 put_bits(&s->pb, 1, 0); /* no AC prediction yet */
474 ff_h263_cbpy_tab[cbp>>2][1],
475 ff_h263_cbpy_tab[cbp>>2][0]);
477 if (s->pict_type == AV_PICTURE_TYPE_I) {
479 ff_msmp4_mb_i_table[coded_cbp][1], ff_msmp4_mb_i_table[coded_cbp][0]);
481 if (s->use_skip_mb_code)
482 put_bits(&s->pb, 1, 0); /* mb coded */
484 ff_table_mb_non_intra[cbp][1],
485 ff_table_mb_non_intra[cbp][0]);
487 put_bits(&s->pb, 1, 0); /* no AC prediction yet */
488 if(s->inter_intra_pred){
490 put_bits(&s->pb, ff_table_inter_intra[s->h263_aic_dir][1], ff_table_inter_intra[s->h263_aic_dir][0]);
493 s->misc_bits += get_bits_diff(s);
495 for (i = 0; i < 6; i++) {
496 ff_msmpeg4_encode_block(s, block[i], i);
498 s->i_tex_bits += get_bits_diff(s);
503 static void msmpeg4_encode_dc(MpegEncContext * s, int level, int n, int *dir_ptr)
506 int pred, av_uninit(extquant);
510 pred = ff_msmpeg4_pred_dc(s, n, &dc_val, dir_ptr);
512 /* update predictor */
514 *dc_val = level * s->y_dc_scale;
516 *dc_val = level * s->c_dc_scale;
519 /* do the prediction */
522 if(s->msmpeg4_version<=2){
525 ff_v2_dc_lum_table[level + 256][1],
526 ff_v2_dc_lum_table[level + 256][0]);
529 ff_v2_dc_chroma_table[level + 256][1],
530 ff_v2_dc_chroma_table[level + 256][0]);
541 else if( s->msmpeg4_version>=6 ) {
542 if( s->qscale == 1 ) {
543 extquant = (level + 3) & 0x3;
544 code = ((level+3)>>2);
545 } else if( s->qscale == 2 ) {
546 extquant = (level + 1) & 0x1;
547 code = ((level+1)>>1);
551 if (s->dc_table_index == 0) {
553 put_bits(&s->pb, ff_table0_dc_lum[code][1], ff_table0_dc_lum[code][0]);
555 put_bits(&s->pb, ff_table0_dc_chroma[code][1], ff_table0_dc_chroma[code][0]);
559 put_bits(&s->pb, ff_table1_dc_lum[code][1], ff_table1_dc_lum[code][0]);
561 put_bits(&s->pb, ff_table1_dc_chroma[code][1], ff_table1_dc_chroma[code][0]);
565 if(s->msmpeg4_version>=6 && s->qscale<=2)
566 extrabits = 3 - s->qscale;
569 put_bits(&s->pb, 8 + extrabits, level);
570 else if(extrabits > 0)//== VC1 && s->qscale<=2
571 put_bits(&s->pb, extrabits, extquant);
574 put_bits(&s->pb, 1, sign);
579 /* Encoding of a block. Very similar to MPEG4 except for a different
580 escape coding (same as H263) and more vlc tables.
582 void ff_msmpeg4_encode_block(MpegEncContext * s, int16_t * block, int n)
584 int level, run, last, i, j, last_index;
585 int last_non_zero, sign, slevel;
586 int code, run_diff, dc_pred_dir;
588 const uint8_t *scantable;
591 msmpeg4_encode_dc(s, block[0], n, &dc_pred_dir);
594 rl = &ff_rl_table[s->rl_table_index];
596 rl = &ff_rl_table[3 + s->rl_chroma_table_index];
598 run_diff = s->msmpeg4_version>=4;
599 scantable= s->intra_scantable.permutated;
602 rl = &ff_rl_table[3 + s->rl_table_index];
603 if(s->msmpeg4_version<=2)
607 scantable= s->inter_scantable.permutated;
610 /* recalculate block_last_index for M$ wmv1 */
611 if(s->msmpeg4_version>=4 && s->msmpeg4_version<6 && s->block_last_index[n]>0){
612 for(last_index=63; last_index>=0; last_index--){
613 if(block[scantable[last_index]]) break;
615 s->block_last_index[n]= last_index;
617 last_index = s->block_last_index[n];
619 last_non_zero = i - 1;
620 for (; i <= last_index; i++) {
624 run = i - last_non_zero - 1;
625 last = (i == last_index);
633 if(level<=MAX_LEVEL && run<=MAX_RUN){
634 s->ac_stats[s->mb_intra][n>3][level][run][last]++;
637 s->ac_stats[s->mb_intra][n > 3][40][63][0]++; //esc3 like
639 code = get_rl_index(rl, last, run, level);
640 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
644 level1 = level - rl->max_level[last][run];
647 code = get_rl_index(rl, last, run, level1);
650 put_bits(&s->pb, 1, 0);
651 if (level > MAX_LEVEL)
653 run1 = run - rl->max_run[last][level] - run_diff;
656 code = get_rl_index(rl, last, run1+1, level);
657 if (s->msmpeg4_version == 4 && code == rl->n)
659 code = get_rl_index(rl, last, run1, level);
663 put_bits(&s->pb, 1, 0);
664 put_bits(&s->pb, 1, last);
665 if(s->msmpeg4_version>=4){
666 if(s->esc3_level_length==0){
667 s->esc3_level_length=8;
668 s->esc3_run_length= 6;
669 //ESCLVLSZ + ESCRUNSZ
671 put_bits(&s->pb, 6 + (s->msmpeg4_version>=6), 3);
673 put_bits(&s->pb, 8, 3);
675 put_bits(&s->pb, s->esc3_run_length, run);
676 put_bits(&s->pb, 1, sign);
677 put_bits(&s->pb, s->esc3_level_length, level);
679 put_bits(&s->pb, 6, run);
680 put_sbits(&s->pb, 8, slevel);
684 put_bits(&s->pb, 1, 1);
685 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
686 put_bits(&s->pb, 1, sign);
690 put_bits(&s->pb, 1, 1);
691 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
692 put_bits(&s->pb, 1, sign);
695 put_bits(&s->pb, 1, sign);