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"
39 #include "mpeg4video.h"
41 #include "msmpeg4data.h"
46 static uint8_t rl_length[NB_RL_TABLES][MAX_LEVEL+1][MAX_RUN+1][2];
48 /* build the table which associate a (x,y) motion vector to a vlc */
49 static av_cold int init_mv_table(MVTable *tab)
53 tab->table_mv_index = av_malloc(sizeof(uint16_t) * 4096);
54 if (!tab->table_mv_index)
55 return AVERROR(ENOMEM);
57 /* mark all entries as not used */
59 tab->table_mv_index[i] = tab->n;
61 for(i=0;i<tab->n;i++) {
62 x = tab->table_mvx[i];
63 y = tab->table_mvy[i];
64 tab->table_mv_index[(x << 6) | y] = i;
70 void ff_msmpeg4_code012(PutBitContext *pb, int n)
76 put_bits(pb, 1, (n >= 2));
80 static int get_size_of_code(MpegEncContext * s, RLTable *rl, int last, int run, int level, int intra){
83 int run_diff= intra ? 0 : 1;
85 code = get_rl_index(rl, last, run, level);
86 size+= rl->table_vlc[code][1];
90 level1 = level - rl->max_level[last][run];
93 code = get_rl_index(rl, last, run, level1);
97 if (level > MAX_LEVEL)
99 run1 = run - rl->max_run[last][level] - run_diff;
102 code = get_rl_index(rl, last, run1, level);
109 size+= 1+1+ rl->table_vlc[code][1];
113 size+= 1+1+ rl->table_vlc[code][1];
121 av_cold int ff_msmpeg4_encode_init(MpegEncContext *s)
123 static int init_done=0;
126 ff_msmpeg4_common_init(s);
127 if(s->msmpeg4_version>=4){
133 /* init various encoding tables */
135 if ((ret = init_mv_table(&ff_mv_tables[0])) < 0)
137 if ((ret = init_mv_table(&ff_mv_tables[1])) < 0)
139 for(i=0;i<NB_RL_TABLES;i++)
140 ff_rl_init(&ff_rl_table[i], ff_static_rl_table_store[i]);
142 for(i=0; i<NB_RL_TABLES; i++){
144 for (level = 1; level <= MAX_LEVEL; level++) {
146 for(run=0; run<=MAX_RUN; run++){
148 for(last=0; last<2; last++){
149 rl_length[i][level][run][last]= get_size_of_code(s, &ff_rl_table[ i], last, run, level, 0);
159 static void find_best_tables(MpegEncContext * s)
162 int best = 0, best_size = INT_MAX;
163 int chroma_best = 0, best_chroma_size = INT_MAX;
174 for(level=0; level<=MAX_LEVEL; level++){
176 for(run=0; run<=MAX_RUN; run++){
178 const int last_size= size + chroma_size;
179 for(last=0; last<2; last++){
180 int inter_count = s->ac_stats[0][0][level][run][last] + s->ac_stats[0][1][level][run][last];
181 int intra_luma_count = s->ac_stats[1][0][level][run][last];
182 int intra_chroma_count= s->ac_stats[1][1][level][run][last];
184 if(s->pict_type==AV_PICTURE_TYPE_I){
185 size += intra_luma_count *rl_length[i ][level][run][last];
186 chroma_size+= intra_chroma_count*rl_length[i+3][level][run][last];
188 size+= intra_luma_count *rl_length[i ][level][run][last]
189 +intra_chroma_count*rl_length[i+3][level][run][last]
190 +inter_count *rl_length[i+3][level][run][last];
193 if(last_size == size+chroma_size) break;
200 if(chroma_size<best_chroma_size){
201 best_chroma_size= chroma_size;
206 if(s->pict_type==AV_PICTURE_TYPE_P) chroma_best= best;
208 memset(s->ac_stats, 0, sizeof(int)*(MAX_LEVEL+1)*(MAX_RUN+1)*2*2*2);
210 s->rl_table_index = best;
211 s->rl_chroma_table_index= chroma_best;
213 if(s->pict_type != s->last_non_b_pict_type){
214 s->rl_table_index= 2;
215 if(s->pict_type==AV_PICTURE_TYPE_I)
216 s->rl_chroma_table_index= 1;
218 s->rl_chroma_table_index= 2;
223 /* write MSMPEG4 compatible frame header */
224 void ff_msmpeg4_encode_picture_header(MpegEncContext * s, int picture_number)
228 avpriv_align_put_bits(&s->pb);
229 put_bits(&s->pb, 2, s->pict_type - 1);
231 put_bits(&s->pb, 5, s->qscale);
232 if(s->msmpeg4_version<=2){
233 s->rl_table_index = 2;
234 s->rl_chroma_table_index = 2;
237 s->dc_table_index = 1;
238 s->mv_table_index = 1; /* only if P-frame */
239 s->use_skip_mb_code = 1; /* only if P-frame */
240 s->per_mb_rl_table = 0;
241 if(s->msmpeg4_version==4)
242 s->inter_intra_pred= (s->width*s->height < 320*240 && s->bit_rate<=II_BITRATE && s->pict_type==AV_PICTURE_TYPE_P);
243 ff_dlog(s, "%d %"PRId64" %d %d %d\n", s->pict_type, s->bit_rate,
244 s->inter_intra_pred, s->width, s->height);
246 if (s->pict_type == AV_PICTURE_TYPE_I) {
247 s->slice_height= s->mb_height/1;
248 put_bits(&s->pb, 5, 0x16 + s->mb_height/s->slice_height);
250 if(s->msmpeg4_version==4){
251 ff_msmpeg4_encode_ext_header(s);
252 if(s->bit_rate>MBAC_BITRATE)
253 put_bits(&s->pb, 1, s->per_mb_rl_table);
256 if(s->msmpeg4_version>2){
257 if(!s->per_mb_rl_table){
258 ff_msmpeg4_code012(&s->pb, s->rl_chroma_table_index);
259 ff_msmpeg4_code012(&s->pb, s->rl_table_index);
262 put_bits(&s->pb, 1, s->dc_table_index);
265 put_bits(&s->pb, 1, s->use_skip_mb_code);
267 if(s->msmpeg4_version==4 && s->bit_rate>MBAC_BITRATE)
268 put_bits(&s->pb, 1, s->per_mb_rl_table);
270 if(s->msmpeg4_version>2){
271 if(!s->per_mb_rl_table)
272 ff_msmpeg4_code012(&s->pb, s->rl_table_index);
274 put_bits(&s->pb, 1, s->dc_table_index);
276 put_bits(&s->pb, 1, s->mv_table_index);
280 s->esc3_level_length= 0;
281 s->esc3_run_length= 0;
284 void ff_msmpeg4_encode_ext_header(MpegEncContext * s)
286 unsigned fps = s->avctx->time_base.den / s->avctx->time_base.num / FFMAX(s->avctx->ticks_per_frame, 1);
287 put_bits(&s->pb, 5, FFMIN(fps, 31)); //yes 29.97 -> 29
289 put_bits(&s->pb, 11, FFMIN(s->bit_rate/1024, 2047));
291 if(s->msmpeg4_version>=3)
292 put_bits(&s->pb, 1, s->flipflop_rounding);
294 av_assert0(s->flipflop_rounding==0);
297 void ff_msmpeg4_encode_motion(MpegEncContext * s,
303 /* modulo encoding */
304 /* WARNING : you cannot reach all the MVs even with the modulo
305 encoding. This is a somewhat strange compromise they took !!! */
318 if ((unsigned)mx >= 64 ||
320 av_log(s->avctx, AV_LOG_ERROR, "error mx=%d my=%d\n", mx, my);
322 mv = &ff_mv_tables[s->mv_table_index];
324 code = mv->table_mv_index[(mx << 6) | my];
326 mv->table_mv_bits[code],
327 mv->table_mv_code[code]);
329 /* escape : code literally */
330 put_bits(&s->pb, 6, mx);
331 put_bits(&s->pb, 6, my);
335 void ff_msmpeg4_handle_slices(MpegEncContext *s){
337 if (s->slice_height && (s->mb_y % s->slice_height) == 0) {
338 if(s->msmpeg4_version < 4){
339 ff_mpeg4_clean_buffers(s);
341 s->first_slice_line = 1;
343 s->first_slice_line = 0;
348 static void msmpeg4v2_encode_motion(MpegEncContext * s, int val)
350 int range, bit_size, sign, code, bits;
355 put_bits(&s->pb, ff_mvtab[code][1], ff_mvtab[code][0]);
357 bit_size = s->f_code - 1;
358 range = 1 << bit_size;
371 code = (val >> bit_size) + 1;
372 bits = val & (range - 1);
374 put_bits(&s->pb, ff_mvtab[code][1] + 1, (ff_mvtab[code][0] << 1) | sign);
376 put_bits(&s->pb, bit_size, bits);
381 void ff_msmpeg4_encode_mb(MpegEncContext * s,
382 int16_t block[6][64],
383 int motion_x, int motion_y)
385 int cbp, coded_cbp, i;
387 uint8_t *coded_block;
389 ff_msmpeg4_handle_slices(s);
394 for (i = 0; i < 6; i++) {
395 if (s->block_last_index[i] >= 0)
398 if (s->use_skip_mb_code && (cbp | motion_x | motion_y) == 0) {
399 /* skip macroblock */
400 put_bits(&s->pb, 1, 1);
407 if (s->use_skip_mb_code)
408 put_bits(&s->pb, 1, 0); /* mb coded */
410 if(s->msmpeg4_version<=2){
412 ff_v2_mb_type[cbp&3][1],
413 ff_v2_mb_type[cbp&3][0]);
414 if((cbp&3) != 3) coded_cbp= cbp ^ 0x3C;
418 ff_h263_cbpy_tab[coded_cbp>>2][1],
419 ff_h263_cbpy_tab[coded_cbp>>2][0]);
421 s->misc_bits += get_bits_diff(s);
423 ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
424 msmpeg4v2_encode_motion(s, motion_x - pred_x);
425 msmpeg4v2_encode_motion(s, motion_y - pred_y);
428 ff_table_mb_non_intra[cbp + 64][1],
429 ff_table_mb_non_intra[cbp + 64][0]);
431 s->misc_bits += get_bits_diff(s);
434 ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
435 ff_msmpeg4_encode_motion(s, motion_x - pred_x,
439 s->mv_bits += get_bits_diff(s);
441 for (i = 0; i < 6; i++) {
442 ff_msmpeg4_encode_block(s, block[i], i);
444 s->p_tex_bits += get_bits_diff(s);
449 for (i = 0; i < 6; i++) {
451 val = (s->block_last_index[i] >= 1);
452 cbp |= val << (5 - i);
454 /* predict value for close blocks only for luma */
455 pred = ff_msmpeg4_coded_block_pred(s, i, &coded_block);
459 coded_cbp |= val << (5 - i);
462 if(s->msmpeg4_version<=2){
463 if (s->pict_type == AV_PICTURE_TYPE_I) {
465 ff_v2_intra_cbpc[cbp&3][1], ff_v2_intra_cbpc[cbp&3][0]);
467 if (s->use_skip_mb_code)
468 put_bits(&s->pb, 1, 0); /* mb coded */
470 ff_v2_mb_type[(cbp&3) + 4][1],
471 ff_v2_mb_type[(cbp&3) + 4][0]);
473 put_bits(&s->pb, 1, 0); /* no AC prediction yet */
475 ff_h263_cbpy_tab[cbp>>2][1],
476 ff_h263_cbpy_tab[cbp>>2][0]);
478 if (s->pict_type == AV_PICTURE_TYPE_I) {
480 ff_msmp4_mb_i_table[coded_cbp][1], ff_msmp4_mb_i_table[coded_cbp][0]);
482 if (s->use_skip_mb_code)
483 put_bits(&s->pb, 1, 0); /* mb coded */
485 ff_table_mb_non_intra[cbp][1],
486 ff_table_mb_non_intra[cbp][0]);
488 put_bits(&s->pb, 1, 0); /* no AC prediction yet */
489 if(s->inter_intra_pred){
491 put_bits(&s->pb, ff_table_inter_intra[s->h263_aic_dir][1], ff_table_inter_intra[s->h263_aic_dir][0]);
494 s->misc_bits += get_bits_diff(s);
496 for (i = 0; i < 6; i++) {
497 ff_msmpeg4_encode_block(s, block[i], i);
499 s->i_tex_bits += get_bits_diff(s);
504 static void msmpeg4_encode_dc(MpegEncContext * s, int level, int n, int *dir_ptr)
507 int pred, av_uninit(extquant);
511 pred = ff_msmpeg4_pred_dc(s, n, &dc_val, dir_ptr);
513 /* update predictor */
515 *dc_val = level * s->y_dc_scale;
517 *dc_val = level * s->c_dc_scale;
520 /* do the prediction */
523 if(s->msmpeg4_version<=2){
526 ff_v2_dc_lum_table[level + 256][1],
527 ff_v2_dc_lum_table[level + 256][0]);
530 ff_v2_dc_chroma_table[level + 256][1],
531 ff_v2_dc_chroma_table[level + 256][0]);
542 else if( s->msmpeg4_version>=6 ) {
543 if( s->qscale == 1 ) {
544 extquant = (level + 3) & 0x3;
545 code = ((level+3)>>2);
546 } else if( s->qscale == 2 ) {
547 extquant = (level + 1) & 0x1;
548 code = ((level+1)>>1);
552 if (s->dc_table_index == 0) {
554 put_bits(&s->pb, ff_table0_dc_lum[code][1], ff_table0_dc_lum[code][0]);
556 put_bits(&s->pb, ff_table0_dc_chroma[code][1], ff_table0_dc_chroma[code][0]);
560 put_bits(&s->pb, ff_table1_dc_lum[code][1], ff_table1_dc_lum[code][0]);
562 put_bits(&s->pb, ff_table1_dc_chroma[code][1], ff_table1_dc_chroma[code][0]);
566 if(s->msmpeg4_version>=6 && s->qscale<=2)
567 extrabits = 3 - s->qscale;
570 put_bits(&s->pb, 8 + extrabits, level);
571 else if(extrabits > 0)//== VC1 && s->qscale<=2
572 put_bits(&s->pb, extrabits, extquant);
575 put_bits(&s->pb, 1, sign);
580 /* Encoding of a block; very similar to MPEG-4 except for a different
581 * escape coding (same as H.263) 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);