1 /*****************************************************************************
2 * block.c: MPEG2 video transrating module
3 *****************************************************************************
4 * Copyright (C) 2003-2004 the VideoLAN team
5 * Copyright (C) 2003 Antoine Missout
6 * Copyright (C) 2000-2003 Michel Lespinasse <walken@zoy.org>
7 * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
10 * Authors: Christophe Massiot <massiot@via.ecp.fr>
11 * Laurent Aimar <fenrir@via.ecp.fr>
13 * Michel Lespinasse <walken@zoy.org>
14 * Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
29 *****************************************************************************/
31 /*****************************************************************************
33 *****************************************************************************/
40 #include <vlc_codec.h>
42 #include "transrate.h"
44 /****************************************************************************
45 * transrater, code from M2VRequantizer http://www.metakine.com/
46 ****************************************************************************/
48 /////---- begin ext mpeg code
53 static inline int saturate( int i_value )
57 if ( i_value < -2048 )
62 static int64_t get_score( const RunLevel *blk, RunLevel *new_blk, int i_qscale, int i_qscale_new )
67 while ( new_blk->level )
69 int new_level = new_blk->level;
70 int level = blk->level;
71 if ( i1 > 64 || i2 > 64 || !blk->run || !new_blk->run ) return score;
72 if ( i1 + blk->run == i2 + new_blk->run )
74 int64_t tmp = saturate(level * i_qscale)
75 - saturate(new_level * i_qscale_new);
84 int64_t tmp = saturate(level * i_qscale);
93 int level = blk->level;
94 int64_t tmp = saturate(level * i_qscale);
103 static void change_qscale( const RunLevel *blk, RunLevel *new_blk, int i_qscale, int i_qscale_new, int intra )
108 rounding = i_qscale_new / 3;
110 rounding = i_qscale_new / 6;
114 int level = blk->level > 0 ? blk->level : -blk->level;
115 int new_level = saturate(level * i_qscale) / i_qscale_new;
120 new_blk->run = i - li;
121 new_blk->level = blk->level > 0 ? new_level : -new_level;
130 static const uint8_t non_linear_mquant_table[32] =
132 0, 1, 2, 3, 4, 5, 6, 7,
133 8,10,12,14,16,18,20,22,
134 24,28,32,36,40,44,48,52,
135 56,64,72,80,88,96,104,112
137 static const uint8_t map_non_linear_mquant[113] =
139 0,1,2,3,4,5,6,7,8,8,9,9,10,10,11,11,12,12,13,13,14,14,15,15,16,16,
140 16,17,17,17,18,18,18,18,19,19,19,19,20,20,20,20,21,21,21,21,22,22,
141 22,22,23,23,23,23,24,24,24,24,24,24,24,25,25,25,25,25,25,25,26,26,
142 26,26,26,26,26,26,27,27,27,27,27,27,27,27,28,28,28,28,28,28,28,29,
143 29,29,29,29,29,29,29,29,29,30,30,30,30,30,30,30,31,31,31,31,31
146 int scale_quant( transrate_t *tr, double qrate )
148 int i_quant = (int)floor( tr->quantizer_scale * qrate + 0.5 );
150 if ( tr->q_scale_type )
156 i_quant = non_linear_mquant_table[map_non_linear_mquant[i_quant]];
164 i_quant = (i_quant / 2) * 2; // Must be *even*
170 int increment_quant( transrate_t *tr, int i_quant )
172 if ( tr->q_scale_type )
174 assert( i_quant >= 1 && i_quant <= 112 );
175 i_quant = map_non_linear_mquant[i_quant] + 1;
178 i_quant = non_linear_mquant_table[i_quant];
182 assert(!(i_quant & 1));
191 static int decrement_quant( transrate_t *tr, int i_quant )
193 if ( tr->q_scale_type )
195 assert( i_quant >= 1 && i_quant <= 112 );
196 i_quant = map_non_linear_mquant[i_quant] - 1;
199 i_quant = non_linear_mquant_table[i_quant];
203 assert(!(i_quant & 1));
211 static void quantize_block( transrate_t *tr, RunLevel *new_blk, int intra )
213 RunLevel old_blk[65];
214 RunLevel *blk = old_blk;
215 const uint8_t *old_matrix, *new_matrix;
218 memcpy( blk, new_blk, 65 * sizeof(RunLevel) );
221 old_matrix = tr->intra_quantizer_matrix;
222 new_matrix = mpeg4_default_intra_matrix;
226 old_matrix = tr->non_intra_quantizer_matrix;
227 new_matrix = mpeg4_default_non_intra_matrix;
232 int level = blk->level > 0 ? blk->level : -blk->level;
233 int new_level = (level * old_matrix[i] + new_matrix[i]/2)
239 new_blk->run = i - li;
240 new_blk->level = blk->level > 0 ? new_level : -new_level;
249 int transrate_mb( transrate_t *tr, RunLevel blk[6][65], RunLevel new_blk[6][65],
250 int i_cbp, int intra )
252 int i_qscale = tr->quantizer_scale;
253 int i_guessed_qscale = tr->new_quantizer_scale;
254 int64_t i_last_error = 0;
256 int i_last_qscale_same_error = 0;
263 for ( i = 0; i < 6; i++ )
265 if ( i_cbp & (1 << (5 - i)) )
267 RunLevel *cur_blk = blk[i];
269 while ( cur_blk->level )
277 /* See if we can change quantizer scale */
283 for ( i = 0; i < 6; i++ )
285 if ( i_cbp & (1 << (5 - i)) )
287 int64_t i_block_error;
288 change_qscale( blk[i], new_blk[i], i_qscale, i_guessed_qscale,
290 i_block_error = get_score( blk[i], new_blk[i],
291 i_qscale, i_guessed_qscale );
292 if ( i > 3 ) i_block_error *= 4;
293 if ( i_block_error > i_error )
294 i_error = i_block_error;
295 if ( new_blk[i]->level )
296 i_new_cbp |= (1 << (5 - i));
300 if ( i_error >= (int64_t)tr->i_minimum_error
301 && i_error <= (int64_t)tr->i_admissible_error )
305 if ( i_nb_coeffs <= 15 && i_error <= (int64_t)tr->i_admissible_error )
307 /* There is no interest in changing the qscale (takes up 5 bits
308 * we won't regain) */
314 if ( i_error > (int64_t)tr->i_admissible_error )
317 i_last_qscale = i_guessed_qscale;
318 i_guessed_qscale = decrement_quant( tr, i_guessed_qscale );
323 i_last_qscale = i_guessed_qscale;
324 i_guessed_qscale = increment_quant( tr, i_guessed_qscale );
325 i_last_error = i_error;
326 i_last_qscale_same_error = i_last_qscale;
328 if ( i_guessed_qscale == i_last_qscale )
331 else if ( i_direction < 0 )
333 if ( i_error > (int64_t)tr->i_admissible_error )
335 i_last_qscale = i_guessed_qscale;
336 i_guessed_qscale = decrement_quant( tr, i_guessed_qscale );
337 if ( i_guessed_qscale == i_last_qscale )
347 if ( i_error < (int64_t)tr->i_minimum_error )
349 i_last_qscale = i_guessed_qscale;
350 i_guessed_qscale = increment_quant( tr, i_guessed_qscale );
351 if ( i_error > i_last_error )
353 i_last_error = i_error;
354 i_last_qscale_same_error = i_last_qscale;
356 if ( i_guessed_qscale == i_last_qscale )
358 if ( i_last_error == i_error )
360 i_guessed_qscale = i_last_qscale_same_error;
361 if ( i_guessed_qscale == i_qscale )
363 memcpy( new_blk, blk, sizeof(RunLevel)*65*6 );
369 for ( i = 0; i < 6; i++ )
371 if ( i_cbp & (1 << (5 - i)) )
373 change_qscale( blk[i], new_blk[i],
374 i_qscale, i_guessed_qscale,
376 if ( new_blk[i]->level )
377 i_new_cbp |= (1 << (5 - i));
387 if ( i_error > (int64_t)tr->i_admissible_error
388 || i_last_error == i_error )
390 i_guessed_qscale = i_last_qscale_same_error;
391 if ( i_guessed_qscale == i_qscale )
393 memcpy( new_blk, blk, sizeof(RunLevel)*65*6 );
399 for ( i = 0; i < 6; i++ )
401 if ( i_cbp & (1 << (5 - i)) )
403 change_qscale( blk[i], new_blk[i],
404 i_qscale, i_guessed_qscale,
406 if ( new_blk[i]->level )
407 i_new_cbp |= (1 << (5 - i));
417 tr->new_quantizer_scale = i_guessed_qscale;
420 /* Now see if we can drop coeffs */
421 for ( i = 0; i < 6; i++ )
423 if ( i_new_cbp & (1 << (5 - i)) )
427 RunLevel *last_blk = new_blk[i];
430 while ( last_blk[1].level )
432 if ( last_blk == new_blk[i] )
434 old_level = last_blk->level;
436 i_error = get_score( blk[i], new_blk[i],
437 i_qscale, i_guessed_qscale );
438 if ( i_error > tr->i_admissible_error )
440 last_blk->level = old_level;
451 void get_intra_block_B14( transrate_t *tr, RunLevel *blk )
453 bs_transrate_t *bs = &tr->bs;
462 if (bs->i_bit_in_cache >= 0x28000000)
464 tab = DCT_B14AC_5 + (UBITS (bs->i_bit_in_cache, 5) - 5);
467 if (i >= 64) break; /* end of block */
470 bs_flush( bs, tab->len );
472 val = (val ^ SBITS (bs->i_bit_in_cache, 1)) - SBITS (bs->i_bit_in_cache, 1);
474 blk->run = i - li - 1;
481 else if (bs->i_bit_in_cache >= 0x04000000)
483 tab = DCT_B14_8 + (UBITS (bs->i_bit_in_cache, 8) - 4);
486 if (i < 64) goto normal_code;
489 i += (UBITS (bs->i_bit_in_cache, 12) & 0x3F) - 64;
490 if (i >= 64) break; /* illegal, check needed to avoid buffer overflow */
493 val = SBITS (bs->i_bit_in_cache, 12);
495 blk->run = i - li - 1;
503 else if (bs->i_bit_in_cache >= 0x02000000)
505 tab = DCT_B14_10 + (UBITS (bs->i_bit_in_cache, 10) - 8);
507 if (i < 64 ) goto normal_code;
509 else if (bs->i_bit_in_cache >= 0x00800000)
511 tab = DCT_13 + (UBITS (bs->i_bit_in_cache, 13) - 16);
513 if (i < 64 ) goto normal_code;
515 else if (bs->i_bit_in_cache >= 0x00200000)
517 tab = DCT_15 + (UBITS (bs->i_bit_in_cache, 15) - 16);
519 if (i < 64 ) goto normal_code;
523 tab = DCT_16 + UBITS (bs->i_bit_in_cache, 16);
526 if (i < 64 ) goto normal_code;
528 fprintf(stderr, "Err in B14\n");
530 break; /* illegal, check needed to avoid buffer overflow */
532 bs_flush( bs, 2 ); /* dump end of block code */
535 if ( tr->mpeg4_matrix )
536 quantize_block( tr, blk, 1 );
539 void get_intra_block_B15( transrate_t *tr, RunLevel *blk )
541 bs_transrate_t *bs = &tr->bs;
550 if (bs->i_bit_in_cache >= 0x04000000)
552 tab = DCT_B15_8 + (UBITS (bs->i_bit_in_cache, 8) - 4);
558 bs_flush( bs, tab->len );
561 val = (val ^ SBITS (bs->i_bit_in_cache, 1)) - SBITS (bs->i_bit_in_cache, 1);
563 blk->run = i - li - 1;
572 i += (UBITS (bs->i_bit_in_cache, 12) & 0x3F) - 64;
574 if (i >= 64) break; /* illegal, check against buffer overflow */
577 val = SBITS (bs->i_bit_in_cache, 12);
579 blk->run = i - li - 1;
587 else if (bs->i_bit_in_cache >= 0x02000000)
589 tab = DCT_B15_10 + (UBITS (bs->i_bit_in_cache, 10) - 8);
591 if (i < 64) goto normal_code;
593 else if (bs->i_bit_in_cache >= 0x00800000)
595 tab = DCT_13 + (UBITS (bs->i_bit_in_cache, 13) - 16);
597 if (i < 64) goto normal_code;
599 else if (bs->i_bit_in_cache >= 0x00200000)
601 tab = DCT_15 + (UBITS (bs->i_bit_in_cache, 15) - 16);
603 if (i < 64) goto normal_code;
607 tab = DCT_16 + UBITS (bs->i_bit_in_cache, 16);
610 if (i < 64) goto normal_code;
612 fprintf(stderr, "Err in B15\n");
614 break; /* illegal, check needed to avoid buffer overflow */
616 bs_flush( bs, 4 ); /* dump end of block code */
619 if ( tr->mpeg4_matrix )
620 quantize_block( tr, blk, 1 );
624 int get_non_intra_block( transrate_t *tr, RunLevel *blk )
626 bs_transrate_t *bs = &tr->bs;
633 if (bs->i_bit_in_cache >= 0x28000000)
635 tab = DCT_B14DC_5 + (UBITS (bs->i_bit_in_cache, 5) - 5);
642 if (bs->i_bit_in_cache >= 0x28000000)
644 tab = DCT_B14AC_5 + (UBITS (bs->i_bit_in_cache, 5) - 5);
649 break; /* end of block */
653 bs_flush( bs, tab->len );
655 val = (val ^ SBITS (bs->i_bit_in_cache, 1)) - SBITS (bs->i_bit_in_cache, 1);
657 blk->run = i - li - 1;
661 //if ( ((val) && (tab->level < tst)) || ((!val) && (tab->level >= tst)) )
662 // LOGF("level: %i val: %i tst : %i q: %i nq : %i\n", tab->level, val, tst, q, nq);
669 if (bs->i_bit_in_cache >= 0x04000000)
671 tab = DCT_B14_8 + (UBITS (bs->i_bit_in_cache, 8) - 4);
674 if (i < 64) goto normal_code;
678 i += (UBITS (bs->i_bit_in_cache, 12) & 0x3F) - 64;
680 if (i >= 64) break; /* illegal, check needed to avoid buffer overflow */
683 val = SBITS (bs->i_bit_in_cache, 12);
685 blk->run = i - li - 1;
692 else if (bs->i_bit_in_cache >= 0x02000000)
694 tab = DCT_B14_10 + (UBITS (bs->i_bit_in_cache, 10) - 8);
696 if (i < 64) goto normal_code;
698 else if (bs->i_bit_in_cache >= 0x00800000)
700 tab = DCT_13 + (UBITS (bs->i_bit_in_cache, 13) - 16);
702 if (i < 64) goto normal_code;
704 else if (bs->i_bit_in_cache >= 0x00200000)
706 tab = DCT_15 + (UBITS (bs->i_bit_in_cache, 15) - 16);
708 if (i < 64) goto normal_code;
712 tab = DCT_16 + UBITS (bs->i_bit_in_cache, 16);
716 if (i < 64) goto normal_code;
718 fprintf(stderr, "Err in non-intra\n");
720 break; /* illegal, check needed to avoid buffer overflow */
722 bs_flush( bs, 2 ); /* dump end of block code */
725 if ( tr->mpeg4_matrix )
726 quantize_block( tr, blk, 0 );
731 static inline void putAC( bs_transrate_t *bs, int run, int signed_level, int vlcformat)
734 const VLCtable *ptab = NULL;
736 level = (signed_level<0) ? -signed_level : signed_level; /* abs(signed_level) */
738 assert(!(run<0 || run>63 || level==0 || level>2047));
742 if (run<2 && level<41)
744 if (vlcformat) ptab = &dct_code_tab1a[run][level-1];
745 else ptab = &dct_code_tab1[run][level-1];
748 else if (run<32 && level<6)
750 if (vlcformat) ptab = &dct_code_tab2a[run-2][level-1];
751 else ptab = &dct_code_tab2[run-2][level-1];
755 if (len) /* a VLC code exists */
757 bs_write( bs, ptab->code, len);
758 bs_write( bs, signed_level<0, 1); /* sign */
762 bs_write( bs, 1l, 6); /* Escape */
763 bs_write( bs, run, 6); /* 6 bit code for run */
764 bs_write( bs, ((unsigned int)signed_level) & 0xFFF, 12);
769 static inline void putACfirst( bs_transrate_t *bs, int run, int val)
771 if (run==0 && (val==1 || val==-1)) bs_write( bs, 2|(val<0), 2 );
772 else putAC( bs, run, val, 0);
775 void putnonintrablk( bs_transrate_t *bs, RunLevel *blk)
779 putACfirst( bs, blk->run, blk->level );
784 putAC( bs, blk->run, blk->level, 0 );
788 bs_write( bs, 2, 2 );
791 void putintrablk( bs_transrate_t *bs, RunLevel *blk, int vlcformat)
795 putAC( bs, blk->run, blk->level, vlcformat );
800 bs_write( bs, 6, 4 );
802 bs_write( bs, 2, 2 );