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 *****************************************************************************/
42 #include <vlc_common.h>
44 #include <vlc_codec.h>
46 #include "transrate.h"
48 /****************************************************************************
49 * transrater, code from M2VRequantizer http://www.metakine.com/
50 ****************************************************************************/
52 /////---- begin ext mpeg code
57 static inline int saturate( int i_value )
61 if ( i_value < -2048 )
66 static int64_t get_score( const RunLevel *blk, RunLevel *new_blk, int i_qscale, int i_qscale_new )
71 while ( new_blk->level )
73 int new_level = new_blk->level;
74 int level = blk->level;
75 if ( i1 > 64 || i2 > 64 || !blk->run || !new_blk->run ) return score;
76 if ( i1 + blk->run == i2 + new_blk->run )
78 int64_t tmp = saturate(level * i_qscale)
79 - saturate(new_level * i_qscale_new);
88 int64_t tmp = saturate(level * i_qscale);
97 int level = blk->level;
98 int64_t tmp = saturate(level * i_qscale);
107 static void change_qscale( const RunLevel *blk, RunLevel *new_blk, int i_qscale, int i_qscale_new, int intra )
112 rounding = i_qscale_new / 3;
114 rounding = i_qscale_new / 6;
118 int level = blk->level > 0 ? blk->level : -blk->level;
119 int new_level = saturate(level * i_qscale) / i_qscale_new;
124 new_blk->run = i - li;
125 new_blk->level = blk->level > 0 ? new_level : -new_level;
134 static const uint8_t non_linear_mquant_table[32] =
136 0, 1, 2, 3, 4, 5, 6, 7,
137 8,10,12,14,16,18,20,22,
138 24,28,32,36,40,44,48,52,
139 56,64,72,80,88,96,104,112
141 static const uint8_t map_non_linear_mquant[113] =
143 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,
144 16,17,17,17,18,18,18,18,19,19,19,19,20,20,20,20,21,21,21,21,22,22,
145 22,22,23,23,23,23,24,24,24,24,24,24,24,25,25,25,25,25,25,25,26,26,
146 26,26,26,26,26,26,27,27,27,27,27,27,27,27,28,28,28,28,28,28,28,29,
147 29,29,29,29,29,29,29,29,29,30,30,30,30,30,30,30,31,31,31,31,31
150 int scale_quant( transrate_t *tr, double qrate )
152 int i_quant = (int)floor( tr->quantizer_scale * qrate + 0.5 );
154 if ( tr->q_scale_type )
160 i_quant = non_linear_mquant_table[map_non_linear_mquant[i_quant]];
168 i_quant = (i_quant / 2) * 2; // Must be *even*
174 int increment_quant( transrate_t *tr, int i_quant )
176 if ( tr->q_scale_type )
178 assert( i_quant >= 1 && i_quant <= 112 );
179 i_quant = map_non_linear_mquant[i_quant] + 1;
182 i_quant = non_linear_mquant_table[i_quant];
186 assert(!(i_quant & 1));
195 static int decrement_quant( transrate_t *tr, int i_quant )
197 if ( tr->q_scale_type )
199 assert( i_quant >= 1 && i_quant <= 112 );
200 i_quant = map_non_linear_mquant[i_quant] - 1;
203 i_quant = non_linear_mquant_table[i_quant];
207 assert(!(i_quant & 1));
215 static void quantize_block( transrate_t *tr, RunLevel *new_blk, int intra )
217 RunLevel old_blk[65];
218 RunLevel *blk = old_blk;
219 const uint8_t *old_matrix, *new_matrix;
222 memcpy( blk, new_blk, 65 * sizeof(RunLevel) );
225 old_matrix = tr->intra_quantizer_matrix;
226 new_matrix = mpeg4_default_intra_matrix;
230 old_matrix = tr->non_intra_quantizer_matrix;
231 new_matrix = mpeg4_default_non_intra_matrix;
236 int level = blk->level > 0 ? blk->level : -blk->level;
237 int new_level = (level * old_matrix[i] + new_matrix[i]/2)
243 new_blk->run = i - li;
244 new_blk->level = blk->level > 0 ? new_level : -new_level;
253 int transrate_mb( transrate_t *tr, RunLevel blk[6][65], RunLevel new_blk[6][65],
254 int i_cbp, int intra )
256 int i_qscale = tr->quantizer_scale;
257 int i_guessed_qscale = tr->new_quantizer_scale;
258 int64_t i_last_error = 0;
260 int i_last_qscale_same_error = 0;
267 for ( i = 0; i < 6; i++ )
269 if ( i_cbp & (1 << (5 - i)) )
271 RunLevel *cur_blk = blk[i];
273 while ( cur_blk->level )
281 /* See if we can change quantizer scale */
287 for ( i = 0; i < 6; i++ )
289 if ( i_cbp & (1 << (5 - i)) )
291 int64_t i_block_error;
292 change_qscale( blk[i], new_blk[i], i_qscale, i_guessed_qscale,
294 i_block_error = get_score( blk[i], new_blk[i],
295 i_qscale, i_guessed_qscale );
296 if ( i > 3 ) i_block_error *= 4;
297 if ( i_block_error > i_error )
298 i_error = i_block_error;
299 if ( new_blk[i]->level )
300 i_new_cbp |= (1 << (5 - i));
304 if ( i_error >= (int64_t)tr->i_minimum_error
305 && i_error <= (int64_t)tr->i_admissible_error )
309 if ( i_nb_coeffs <= 15 && i_error <= (int64_t)tr->i_admissible_error )
311 /* There is no interest in changing the qscale (takes up 5 bits
312 * we won't regain) */
318 if ( i_error > (int64_t)tr->i_admissible_error )
321 i_last_qscale = i_guessed_qscale;
322 i_guessed_qscale = decrement_quant( tr, i_guessed_qscale );
327 i_last_qscale = i_guessed_qscale;
328 i_guessed_qscale = increment_quant( tr, i_guessed_qscale );
329 i_last_error = i_error;
330 i_last_qscale_same_error = i_last_qscale;
332 if ( i_guessed_qscale == i_last_qscale )
335 else if ( i_direction < 0 )
337 if ( i_error > (int64_t)tr->i_admissible_error )
339 i_last_qscale = i_guessed_qscale;
340 i_guessed_qscale = decrement_quant( tr, i_guessed_qscale );
341 if ( i_guessed_qscale == i_last_qscale )
351 if ( i_error < (int64_t)tr->i_minimum_error )
353 i_last_qscale = i_guessed_qscale;
354 i_guessed_qscale = increment_quant( tr, i_guessed_qscale );
355 if ( i_error > i_last_error )
357 i_last_error = i_error;
358 i_last_qscale_same_error = i_last_qscale;
360 if ( i_guessed_qscale == i_last_qscale )
362 if ( i_last_error == i_error )
364 i_guessed_qscale = i_last_qscale_same_error;
365 if ( i_guessed_qscale == i_qscale )
367 memcpy( new_blk, blk, sizeof(RunLevel)*65*6 );
373 for ( i = 0; i < 6; i++ )
375 if ( i_cbp & (1 << (5 - i)) )
377 change_qscale( blk[i], new_blk[i],
378 i_qscale, i_guessed_qscale,
380 if ( new_blk[i]->level )
381 i_new_cbp |= (1 << (5 - i));
391 if ( i_error > (int64_t)tr->i_admissible_error
392 || i_last_error == i_error )
394 i_guessed_qscale = i_last_qscale_same_error;
395 if ( i_guessed_qscale == i_qscale )
397 memcpy( new_blk, blk, sizeof(RunLevel)*65*6 );
403 for ( i = 0; i < 6; i++ )
405 if ( i_cbp & (1 << (5 - i)) )
407 change_qscale( blk[i], new_blk[i],
408 i_qscale, i_guessed_qscale,
410 if ( new_blk[i]->level )
411 i_new_cbp |= (1 << (5 - i));
421 tr->new_quantizer_scale = i_guessed_qscale;
424 /* Now see if we can drop coeffs */
425 for ( i = 0; i < 6; i++ )
427 if ( i_new_cbp & (1 << (5 - i)) )
431 RunLevel *last_blk = new_blk[i];
434 while ( last_blk[1].level )
436 if ( last_blk == new_blk[i] )
438 old_level = last_blk->level;
440 i_error = get_score( blk[i], new_blk[i],
441 i_qscale, i_guessed_qscale );
442 if ( i_error > tr->i_admissible_error )
444 last_blk->level = old_level;
455 void get_intra_block_B14( transrate_t *tr, RunLevel *blk )
457 bs_transrate_t *bs = &tr->bs;
466 if (bs->i_bit_in_cache >= 0x28000000)
468 tab = DCT_B14AC_5 + (UBITS (bs->i_bit_in_cache, 5) - 5);
471 if (i >= 64) break; /* end of block */
474 bs_flush( bs, tab->len );
476 val = (val ^ SBITS (bs->i_bit_in_cache, 1)) - SBITS (bs->i_bit_in_cache, 1);
478 blk->run = i - li - 1;
485 else if (bs->i_bit_in_cache >= 0x04000000)
487 tab = DCT_B14_8 + (UBITS (bs->i_bit_in_cache, 8) - 4);
490 if (i < 64) goto normal_code;
493 i += (UBITS (bs->i_bit_in_cache, 12) & 0x3F) - 64;
494 if (i >= 64) break; /* illegal, check needed to avoid buffer overflow */
497 val = SBITS (bs->i_bit_in_cache, 12);
499 blk->run = i - li - 1;
507 else if (bs->i_bit_in_cache >= 0x02000000)
509 tab = DCT_B14_10 + (UBITS (bs->i_bit_in_cache, 10) - 8);
511 if (i < 64 ) goto normal_code;
513 else if (bs->i_bit_in_cache >= 0x00800000)
515 tab = DCT_13 + (UBITS (bs->i_bit_in_cache, 13) - 16);
517 if (i < 64 ) goto normal_code;
519 else if (bs->i_bit_in_cache >= 0x00200000)
521 tab = DCT_15 + (UBITS (bs->i_bit_in_cache, 15) - 16);
523 if (i < 64 ) goto normal_code;
527 tab = DCT_16 + UBITS (bs->i_bit_in_cache, 16);
530 if (i < 64 ) goto normal_code;
532 fprintf(stderr, "Err in B14\n");
534 break; /* illegal, check needed to avoid buffer overflow */
536 bs_flush( bs, 2 ); /* dump end of block code */
539 if ( tr->mpeg4_matrix )
540 quantize_block( tr, blk, 1 );
543 void get_intra_block_B15( transrate_t *tr, RunLevel *blk )
545 bs_transrate_t *bs = &tr->bs;
554 if (bs->i_bit_in_cache >= 0x04000000)
556 tab = DCT_B15_8 + (UBITS (bs->i_bit_in_cache, 8) - 4);
562 bs_flush( bs, tab->len );
565 val = (val ^ SBITS (bs->i_bit_in_cache, 1)) - SBITS (bs->i_bit_in_cache, 1);
567 blk->run = i - li - 1;
576 i += (UBITS (bs->i_bit_in_cache, 12) & 0x3F) - 64;
578 if (i >= 64) break; /* illegal, check against buffer overflow */
581 val = SBITS (bs->i_bit_in_cache, 12);
583 blk->run = i - li - 1;
591 else if (bs->i_bit_in_cache >= 0x02000000)
593 tab = DCT_B15_10 + (UBITS (bs->i_bit_in_cache, 10) - 8);
595 if (i < 64) goto normal_code;
597 else if (bs->i_bit_in_cache >= 0x00800000)
599 tab = DCT_13 + (UBITS (bs->i_bit_in_cache, 13) - 16);
601 if (i < 64) goto normal_code;
603 else if (bs->i_bit_in_cache >= 0x00200000)
605 tab = DCT_15 + (UBITS (bs->i_bit_in_cache, 15) - 16);
607 if (i < 64) goto normal_code;
611 tab = DCT_16 + UBITS (bs->i_bit_in_cache, 16);
614 if (i < 64) goto normal_code;
616 fprintf(stderr, "Err in B15\n");
618 break; /* illegal, check needed to avoid buffer overflow */
620 bs_flush( bs, 4 ); /* dump end of block code */
623 if ( tr->mpeg4_matrix )
624 quantize_block( tr, blk, 1 );
628 int get_non_intra_block( transrate_t *tr, RunLevel *blk )
630 bs_transrate_t *bs = &tr->bs;
637 if (bs->i_bit_in_cache >= 0x28000000)
639 tab = DCT_B14DC_5 + (UBITS (bs->i_bit_in_cache, 5) - 5);
646 if (bs->i_bit_in_cache >= 0x28000000)
648 tab = DCT_B14AC_5 + (UBITS (bs->i_bit_in_cache, 5) - 5);
653 break; /* end of block */
657 bs_flush( bs, tab->len );
659 val = (val ^ SBITS (bs->i_bit_in_cache, 1)) - SBITS (bs->i_bit_in_cache, 1);
661 blk->run = i - li - 1;
665 //if ( ((val) && (tab->level < tst)) || ((!val) && (tab->level >= tst)) )
666 // LOGF("level: %i val: %i tst : %i q: %i nq : %i\n", tab->level, val, tst, q, nq);
673 if (bs->i_bit_in_cache >= 0x04000000)
675 tab = DCT_B14_8 + (UBITS (bs->i_bit_in_cache, 8) - 4);
678 if (i < 64) goto normal_code;
682 i += (UBITS (bs->i_bit_in_cache, 12) & 0x3F) - 64;
684 if (i >= 64) break; /* illegal, check needed to avoid buffer overflow */
687 val = SBITS (bs->i_bit_in_cache, 12);
689 blk->run = i - li - 1;
696 else if (bs->i_bit_in_cache >= 0x02000000)
698 tab = DCT_B14_10 + (UBITS (bs->i_bit_in_cache, 10) - 8);
700 if (i < 64) goto normal_code;
702 else if (bs->i_bit_in_cache >= 0x00800000)
704 tab = DCT_13 + (UBITS (bs->i_bit_in_cache, 13) - 16);
706 if (i < 64) goto normal_code;
708 else if (bs->i_bit_in_cache >= 0x00200000)
710 tab = DCT_15 + (UBITS (bs->i_bit_in_cache, 15) - 16);
712 if (i < 64) goto normal_code;
716 tab = DCT_16 + UBITS (bs->i_bit_in_cache, 16);
720 if (i < 64) goto normal_code;
722 fprintf(stderr, "Err in non-intra\n");
724 break; /* illegal, check needed to avoid buffer overflow */
726 bs_flush( bs, 2 ); /* dump end of block code */
729 if ( tr->mpeg4_matrix )
730 quantize_block( tr, blk, 0 );
735 static inline void putAC( bs_transrate_t *bs, int run, int signed_level, int vlcformat)
738 const VLCtable *ptab = NULL;
740 level = (signed_level<0) ? -signed_level : signed_level; /* abs(signed_level) */
742 assert(!(run<0 || run>63 || level==0 || level>2047));
746 if (run<2 && level<41)
748 if (vlcformat) ptab = &dct_code_tab1a[run][level-1];
749 else ptab = &dct_code_tab1[run][level-1];
752 else if (run<32 && level<6)
754 if (vlcformat) ptab = &dct_code_tab2a[run-2][level-1];
755 else ptab = &dct_code_tab2[run-2][level-1];
759 if (len) /* a VLC code exists */
761 bs_write( bs, ptab->code, len);
762 bs_write( bs, signed_level<0, 1); /* sign */
766 bs_write( bs, 1l, 6); /* Escape */
767 bs_write( bs, run, 6); /* 6 bit code for run */
768 bs_write( bs, ((unsigned int)signed_level) & 0xFFF, 12);
773 static inline void putACfirst( bs_transrate_t *bs, int run, int val)
775 if (run==0 && (val==1 || val==-1)) bs_write( bs, 2|(val<0), 2 );
776 else putAC( bs, run, val, 0);
779 void putnonintrablk( bs_transrate_t *bs, RunLevel *blk)
783 putACfirst( bs, blk->run, blk->level );
788 putAC( bs, blk->run, blk->level, 0 );
792 bs_write( bs, 2, 2 );
795 void putintrablk( bs_transrate_t *bs, RunLevel *blk, int vlcformat)
799 putAC( bs, blk->run, blk->level, vlcformat );
804 bs_write( bs, 6, 4 );
806 bs_write( bs, 2, 2 );