vlc.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*****************************************************************************
  2. * vlc.c : vlc tables
  3. *****************************************************************************
  4. * Copyright (C) 2003-2018 x264 project
  5. *
  6. * Authors: Laurent Aimar <fenrir@via.ecp.fr>
  7. * Fiona Glaser <fiona@x264.com>
  8. * Henrik Gramner <henrik@gramner.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program 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
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
  23. *
  24. * This program is also available under a commercial proprietary license.
  25. * For more information, contact us at licensing@x264.com.
  26. *****************************************************************************/
  27. #include "common.h"
  28. vlc_large_t x264_level_token[7][LEVEL_TABLE_SIZE];
  29. uint32_t x264_run_before[1<<16];
  30. void x264_cavlc_init( x264_t *h )
  31. {
  32. for( int i_suffix = 0; i_suffix < 7; i_suffix++ )
  33. for( int16_t level = -LEVEL_TABLE_SIZE/2; level < LEVEL_TABLE_SIZE/2; level++ )
  34. {
  35. int mask = level >> 15;
  36. int abs_level = (level^mask)-mask;
  37. int i_level_code = abs_level*2-mask-2;
  38. int i_next = i_suffix;
  39. vlc_large_t *vlc = &x264_level_token[i_suffix][level+LEVEL_TABLE_SIZE/2];
  40. if( ( i_level_code >> i_suffix ) < 14 )
  41. {
  42. vlc->i_size = (i_level_code >> i_suffix) + 1 + i_suffix;
  43. vlc->i_bits = (1<<i_suffix) + (i_level_code & ((1<<i_suffix)-1));
  44. }
  45. else if( i_suffix == 0 && i_level_code < 30 )
  46. {
  47. vlc->i_size = 19;
  48. vlc->i_bits = (1<<4) + (i_level_code - 14);
  49. }
  50. else if( i_suffix > 0 && ( i_level_code >> i_suffix ) == 14 )
  51. {
  52. vlc->i_size = 15 + i_suffix;
  53. vlc->i_bits = (1<<i_suffix) + (i_level_code & ((1<<i_suffix)-1));
  54. }
  55. else
  56. {
  57. i_level_code -= 15 << i_suffix;
  58. if( i_suffix == 0 )
  59. i_level_code -= 15;
  60. vlc->i_size = 28;
  61. vlc->i_bits = (1<<12) + i_level_code;
  62. }
  63. if( i_next == 0 )
  64. i_next++;
  65. if( abs_level > (3 << (i_next-1)) && i_next < 6 )
  66. i_next++;
  67. vlc->i_next = i_next;
  68. }
  69. x264_run_before[0] = 0;
  70. x264_run_before[1] = 0;
  71. for( uint32_t i = 2; i < (1<<16); i++ )
  72. {
  73. x264_run_level_t runlevel;
  74. ALIGNED_ARRAY_16( dctcoef, dct, [16] );
  75. int size = 0;
  76. int bits = 0;
  77. for( int j = 0; j < 16; j++ )
  78. dct[j] = i&(1<<j);
  79. int total = h->quantf.coeff_level_run[DCT_LUMA_4x4]( dct, &runlevel );
  80. int zeros = runlevel.last + 1 - total;
  81. uint32_t mask = i << (x264_clz( i ) + 1);
  82. for( int j = 0; j < total-1 && zeros > 0; j++ )
  83. {
  84. int idx = X264_MIN(zeros, 7) - 1;
  85. int run = x264_clz( mask );
  86. int len = x264_run_before_init[idx][run].i_size;
  87. size += len;
  88. bits <<= len;
  89. bits |= x264_run_before_init[idx][run].i_bits;
  90. zeros -= run;
  91. mask <<= run + 1;
  92. }
  93. x264_run_before[i] = (bits << 5) + size;
  94. }
  95. }