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 *****************************************************************************/
41 #include <vlc_codec.h>
43 #include "transrate.h"
45 /****************************************************************************
46 * transrater, code from M2VRequantizer http://www.metakine.com/
47 ****************************************************************************/
49 /////---- begin ext mpeg code
54 static inline int saturate( int i_value )
58 if ( i_value < -2048 )
63 static int64_t get_score( const RunLevel *blk, RunLevel *new_blk, int i_qscale, int i_qscale_new )
68 while ( new_blk->level )
70 int new_level = new_blk->level;
71 int level = blk->level;
72 if ( i1 > 64 || i2 > 64 || !blk->run || !new_blk->run ) return score;
73 if ( i1 + blk->run == i2 + new_blk->run )
75 int64_t tmp = saturate(level * i_qscale)
76 - saturate(new_level * i_qscale_new);
85 int64_t tmp = saturate(level * i_qscale);
94 int level = blk->level;
95 int64_t tmp = saturate(level * i_qscale);
104 static void change_qscale( const RunLevel *blk, RunLevel *new_blk, int i_qscale, int i_qscale_new, int intra )
109 rounding = i_qscale_new / 3;
111 rounding = i_qscale_new / 6;
115 int level = blk->level > 0 ? blk->level : -blk->level;
116 int new_level = saturate(level * i_qscale) / i_qscale_new;
121 new_blk->run = i - li;
122 new_blk->level = blk->level > 0 ? new_level : -new_level;
131 static const uint8_t non_linear_mquant_table[32] =
133 0, 1, 2, 3, 4, 5, 6, 7,
134 8,10,12,14,16,18,20,22,
135 24,28,32,36,40,44,48,52,
136 56,64,72,80,88,96,104,112
138 static const uint8_t map_non_linear_mquant[113] =
140 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,
141 16,17,17,17,18,18,18,18,19,19,19,19,20,20,20,20,21,21,21,21,22,22,
142 22,22,23,23,23,23,24,24,24,24,24,24,24,25,25,25,25,25,25,25,26,26,
143 26,26,26,26,26,26,27,27,27,27,27,27,27,27,28,28,28,28,28,28,28,29,
144 29,29,29,29,29,29,29,29,29,30,30,30,30,30,30,30,31,31,31,31,31
147 int scale_quant( transrate_t *tr, double qrate )
149 int i_quant = (int)floor( tr->quantizer_scale * qrate + 0.5 );
151 if ( tr->q_scale_type )
157 i_quant = non_linear_mquant_table[map_non_linear_mquant[i_quant]];
165 i_quant = (i_quant / 2) * 2; // Must be *even*
171 int increment_quant( transrate_t *tr, int i_quant )
173 if ( tr->q_scale_type )
175 assert( i_quant >= 1 && i_quant <= 112 );
176 i_quant = map_non_linear_mquant[i_quant] + 1;
179 i_quant = non_linear_mquant_table[i_quant];
183 assert(!(i_quant & 1));
192 static int decrement_quant( transrate_t *tr, int i_quant )
194 if ( tr->q_scale_type )
196 assert( i_quant >= 1 && i_quant <= 112 );
197 i_quant = map_non_linear_mquant[i_quant] - 1;
200 i_quant = non_linear_mquant_table[i_quant];
204 assert(!(i_quant & 1));
212 static void quantize_block( transrate_t *tr, RunLevel *new_blk, int intra )
214 RunLevel old_blk[65];
215 RunLevel *blk = old_blk;
216 const uint8_t *old_matrix, *new_matrix;
219 memcpy( blk, new_blk, 65 * sizeof(RunLevel) );
222 old_matrix = tr->intra_quantizer_matrix;
223 new_matrix = mpeg4_default_intra_matrix;
227 old_matrix = tr->non_intra_quantizer_matrix;
228 new_matrix = mpeg4_default_non_intra_matrix;
233 int level = blk->level > 0 ? blk->level : -blk->level;
234 int new_level = (level * old_matrix[i] + new_matrix[i]/2)
240 new_blk->run = i - li;
241 new_blk->level = blk->level > 0 ? new_level : -new_level;
250 int transrate_mb( transrate_t *tr, RunLevel blk[6][65], RunLevel new_blk[6][65],
251 int i_cbp, int intra )
253 int i_qscale = tr->quantizer_scale;
254 int i_guessed_qscale = tr->new_quantizer_scale;
255 int64_t i_last_error = 0;
257 int i_last_qscale_same_error = 0;
264 for ( i = 0; i < 6; i++ )
266 if ( i_cbp & (1 << (5 - i)) )
268 RunLevel *cur_blk = blk[i];
270 while ( cur_blk->level )
278 /* See if we can change quantizer scale */
284 for ( i = 0; i < 6; i++ )
286 if ( i_cbp & (1 << (5 - i)) )
288 int64_t i_block_error;
289 change_qscale( blk[i], new_blk[i], i_qscale, i_guessed_qscale,
291 i_block_error = get_score( blk[i], new_blk[i],
292 i_qscale, i_guessed_qscale );
293 if ( i > 3 ) i_block_error *= 4;
294 if ( i_block_error > i_error )
295 i_error = i_block_error;
296 if ( new_blk[i]->level )
297 i_new_cbp |= (1 << (5 - i));
301 if ( i_error >= (int64_t)tr->i_minimum_error
302 && i_error <= (int64_t)tr->i_admissible_error )
306 if ( i_nb_coeffs <= 15 && i_error <= (int64_t)tr->i_admissible_error )
308 /* There is no interest in changing the qscale (takes up 5 bits
309 * we won't regain) */
315 if ( i_error > (int64_t)tr->i_admissible_error )
318 i_last_qscale = i_guessed_qscale;
319 i_guessed_qscale = decrement_quant( tr, i_guessed_qscale );
324 i_last_qscale = i_guessed_qscale;
325 i_guessed_qscale = increment_quant( tr, i_guessed_qscale );
326 i_last_error = i_error;
327 i_last_qscale_same_error = i_last_qscale;
329 if ( i_guessed_qscale == i_last_qscale )
332 else if ( i_direction < 0 )
334 if ( i_error > (int64_t)tr->i_admissible_error )
336 i_last_qscale = i_guessed_qscale;
337 i_guessed_qscale = decrement_quant( tr, i_guessed_qscale );
338 if ( i_guessed_qscale == i_last_qscale )
348 if ( i_error < (int64_t)tr->i_minimum_error )
350 i_last_qscale = i_guessed_qscale;
351 i_guessed_qscale = increment_quant( tr, i_guessed_qscale );
352 if ( i_error > i_last_error )
354 i_last_error = i_error;
355 i_last_qscale_same_error = i_last_qscale;
357 if ( i_guessed_qscale == i_last_qscale )
359 if ( i_last_error == i_error )
361 i_guessed_qscale = i_last_qscale_same_error;
362 if ( i_guessed_qscale == i_qscale )
364 memcpy( new_blk, blk, sizeof(RunLevel)*65*6 );
370 for ( i = 0; i < 6; i++ )
372 if ( i_cbp & (1 << (5 - i)) )
374 change_qscale( blk[i], new_blk[i],
375 i_qscale, i_guessed_qscale,
377 if ( new_blk[i]->level )
378 i_new_cbp |= (1 << (5 - i));
388 if ( i_error > (int64_t)tr->i_admissible_error
389 || i_last_error == i_error )
391 i_guessed_qscale = i_last_qscale_same_error;
392 if ( i_guessed_qscale == i_qscale )
394 memcpy( new_blk, blk, sizeof(RunLevel)*65*6 );
400 for ( i = 0; i < 6; i++ )
402 if ( i_cbp & (1 << (5 - i)) )
404 change_qscale( blk[i], new_blk[i],
405 i_qscale, i_guessed_qscale,
407 if ( new_blk[i]->level )
408 i_new_cbp |= (1 << (5 - i));
418 tr->new_quantizer_scale = i_guessed_qscale;
421 /* Now see if we can drop coeffs */
422 for ( i = 0; i < 6; i++ )
424 if ( i_new_cbp & (1 << (5 - i)) )
428 RunLevel *last_blk = new_blk[i];
431 while ( last_blk[1].level )
433 if ( last_blk == new_blk[i] )
435 old_level = last_blk->level;
437 i_error = get_score( blk[i], new_blk[i],
438 i_qscale, i_guessed_qscale );
439 if ( i_error > tr->i_admissible_error )
441 last_blk->level = old_level;
452 void get_intra_block_B14( transrate_t *tr, RunLevel *blk )
454 bs_transrate_t *bs = &tr->bs;
463 if (bs->i_bit_in_cache >= 0x28000000)
465 tab = DCT_B14AC_5 + (UBITS (bs->i_bit_in_cache, 5) - 5);
468 if (i >= 64) break; /* end of block */
471 bs_flush( bs, tab->len );
473 val = (val ^ SBITS (bs->i_bit_in_cache, 1)) - SBITS (bs->i_bit_in_cache, 1);
475 blk->run = i - li - 1;
482 else if (bs->i_bit_in_cache >= 0x04000000)
484 tab = DCT_B14_8 + (UBITS (bs->i_bit_in_cache, 8) - 4);
487 if (i < 64) goto normal_code;
490 i += (UBITS (bs->i_bit_in_cache, 12) & 0x3F) - 64;
491 if (i >= 64) break; /* illegal, check needed to avoid buffer overflow */
494 val = SBITS (bs->i_bit_in_cache, 12);
496 blk->run = i - li - 1;
504 else if (bs->i_bit_in_cache >= 0x02000000)
506 tab = DCT_B14_10 + (UBITS (bs->i_bit_in_cache, 10) - 8);
508 if (i < 64 ) goto normal_code;
510 else if (bs->i_bit_in_cache >= 0x00800000)
512 tab = DCT_13 + (UBITS (bs->i_bit_in_cache, 13) - 16);
514 if (i < 64 ) goto normal_code;
516 else if (bs->i_bit_in_cache >= 0x00200000)
518 tab = DCT_15 + (UBITS (bs->i_bit_in_cache, 15) - 16);
520 if (i < 64 ) goto normal_code;
524 tab = DCT_16 + UBITS (bs->i_bit_in_cache, 16);
527 if (i < 64 ) goto normal_code;
529 fprintf(stderr, "Err in B14\n");
531 break; /* illegal, check needed to avoid buffer overflow */
533 bs_flush( bs, 2 ); /* dump end of block code */
536 if ( tr->mpeg4_matrix )
537 quantize_block( tr, blk, 1 );
540 void get_intra_block_B15( transrate_t *tr, RunLevel *blk )
542 bs_transrate_t *bs = &tr->bs;
551 if (bs->i_bit_in_cache >= 0x04000000)
553 tab = DCT_B15_8 + (UBITS (bs->i_bit_in_cache, 8) - 4);
559 bs_flush( bs, tab->len );
562 val = (val ^ SBITS (bs->i_bit_in_cache, 1)) - SBITS (bs->i_bit_in_cache, 1);
564 blk->run = i - li - 1;
573 i += (UBITS (bs->i_bit_in_cache, 12) & 0x3F) - 64;
575 if (i >= 64) break; /* illegal, check against buffer overflow */
578 val = SBITS (bs->i_bit_in_cache, 12);
580 blk->run = i - li - 1;
588 else if (bs->i_bit_in_cache >= 0x02000000)
590 tab = DCT_B15_10 + (UBITS (bs->i_bit_in_cache, 10) - 8);
592 if (i < 64) goto normal_code;
594 else if (bs->i_bit_in_cache >= 0x00800000)
596 tab = DCT_13 + (UBITS (bs->i_bit_in_cache, 13) - 16);
598 if (i < 64) goto normal_code;
600 else if (bs->i_bit_in_cache >= 0x00200000)
602 tab = DCT_15 + (UBITS (bs->i_bit_in_cache, 15) - 16);
604 if (i < 64) goto normal_code;
608 tab = DCT_16 + UBITS (bs->i_bit_in_cache, 16);
611 if (i < 64) goto normal_code;
613 fprintf(stderr, "Err in B15\n");
615 break; /* illegal, check needed to avoid buffer overflow */
617 bs_flush( bs, 4 ); /* dump end of block code */
620 if ( tr->mpeg4_matrix )
621 quantize_block( tr, blk, 1 );
625 int get_non_intra_block( transrate_t *tr, RunLevel *blk )
627 bs_transrate_t *bs = &tr->bs;
634 if (bs->i_bit_in_cache >= 0x28000000)
636 tab = DCT_B14DC_5 + (UBITS (bs->i_bit_in_cache, 5) - 5);
643 if (bs->i_bit_in_cache >= 0x28000000)
645 tab = DCT_B14AC_5 + (UBITS (bs->i_bit_in_cache, 5) - 5);
650 break; /* end of block */
654 bs_flush( bs, tab->len );
656 val = (val ^ SBITS (bs->i_bit_in_cache, 1)) - SBITS (bs->i_bit_in_cache, 1);
658 blk->run = i - li - 1;
662 //if ( ((val) && (tab->level < tst)) || ((!val) && (tab->level >= tst)) )
663 // LOGF("level: %i val: %i tst : %i q: %i nq : %i\n", tab->level, val, tst, q, nq);
670 if (bs->i_bit_in_cache >= 0x04000000)
672 tab = DCT_B14_8 + (UBITS (bs->i_bit_in_cache, 8) - 4);
675 if (i < 64) goto normal_code;
679 i += (UBITS (bs->i_bit_in_cache, 12) & 0x3F) - 64;
681 if (i >= 64) break; /* illegal, check needed to avoid buffer overflow */
684 val = SBITS (bs->i_bit_in_cache, 12);
686 blk->run = i - li - 1;
693 else if (bs->i_bit_in_cache >= 0x02000000)
695 tab = DCT_B14_10 + (UBITS (bs->i_bit_in_cache, 10) - 8);
697 if (i < 64) goto normal_code;
699 else if (bs->i_bit_in_cache >= 0x00800000)
701 tab = DCT_13 + (UBITS (bs->i_bit_in_cache, 13) - 16);
703 if (i < 64) goto normal_code;
705 else if (bs->i_bit_in_cache >= 0x00200000)
707 tab = DCT_15 + (UBITS (bs->i_bit_in_cache, 15) - 16);
709 if (i < 64) goto normal_code;
713 tab = DCT_16 + UBITS (bs->i_bit_in_cache, 16);
717 if (i < 64) goto normal_code;
719 fprintf(stderr, "Err in non-intra\n");
721 break; /* illegal, check needed to avoid buffer overflow */
723 bs_flush( bs, 2 ); /* dump end of block code */
726 if ( tr->mpeg4_matrix )
727 quantize_block( tr, blk, 0 );
732 static inline void putAC( bs_transrate_t *bs, int run, int signed_level, int vlcformat)
735 const VLCtable *ptab = NULL;
737 level = (signed_level<0) ? -signed_level : signed_level; /* abs(signed_level) */
739 assert(!(run<0 || run>63 || level==0 || level>2047));
743 if (run<2 && level<41)
745 if (vlcformat) ptab = &dct_code_tab1a[run][level-1];
746 else ptab = &dct_code_tab1[run][level-1];
749 else if (run<32 && level<6)
751 if (vlcformat) ptab = &dct_code_tab2a[run-2][level-1];
752 else ptab = &dct_code_tab2[run-2][level-1];
756 if (len) /* a VLC code exists */
758 bs_write( bs, ptab->code, len);
759 bs_write( bs, signed_level<0, 1); /* sign */
763 bs_write( bs, 1l, 6); /* Escape */
764 bs_write( bs, run, 6); /* 6 bit code for run */
765 bs_write( bs, ((unsigned int)signed_level) & 0xFFF, 12);
770 static inline void putACfirst( bs_transrate_t *bs, int run, int val)
772 if (run==0 && (val==1 || val==-1)) bs_write( bs, 2|(val<0), 2 );
773 else putAC( bs, run, val, 0);
776 void putnonintrablk( bs_transrate_t *bs, RunLevel *blk)
780 putACfirst( bs, blk->run, blk->level );
785 putAC( bs, blk->run, blk->level, 0 );
789 bs_write( bs, 2, 2 );
792 void putintrablk( bs_transrate_t *bs, RunLevel *blk, int vlcformat)
796 putAC( bs, blk->run, blk->level, vlcformat );
801 bs_write( bs, 6, 4 );
803 bs_write( bs, 2, 2 );