You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
3.7KB

  1. /*
  2. * This file is part of Libav.
  3. *
  4. * Libav is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * Libav is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with Libav; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include <stdint.h>
  19. #include "libavutil/attributes.h"
  20. #include "rl.h"
  21. av_cold void ff_rl_init(RLTable *rl,
  22. uint8_t static_store[2][2 * MAX_RUN + MAX_LEVEL + 3])
  23. {
  24. int8_t max_level[MAX_RUN + 1], max_run[MAX_LEVEL + 1];
  25. uint8_t index_run[MAX_RUN + 1];
  26. int last, run, level, start, end, i;
  27. /* If table is static, we can quit if rl->max_level[0] is not NULL */
  28. if (static_store && rl->max_level[0])
  29. return;
  30. /* compute max_level[], max_run[] and index_run[] */
  31. for (last = 0; last < 2; last++) {
  32. if (last == 0) {
  33. start = 0;
  34. end = rl->last;
  35. } else {
  36. start = rl->last;
  37. end = rl->n;
  38. }
  39. memset(max_level, 0, MAX_RUN + 1);
  40. memset(max_run, 0, MAX_LEVEL + 1);
  41. memset(index_run, rl->n, MAX_RUN + 1);
  42. for (i = start; i < end; i++) {
  43. run = rl->table_run[i];
  44. level = rl->table_level[i];
  45. if (index_run[run] == rl->n)
  46. index_run[run] = i;
  47. if (level > max_level[run])
  48. max_level[run] = level;
  49. if (run > max_run[level])
  50. max_run[level] = run;
  51. }
  52. if (static_store)
  53. rl->max_level[last] = static_store[last];
  54. else
  55. rl->max_level[last] = av_malloc(MAX_RUN + 1);
  56. memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
  57. if (static_store)
  58. rl->max_run[last] = static_store[last] + MAX_RUN + 1;
  59. else
  60. rl->max_run[last] = av_malloc(MAX_LEVEL + 1);
  61. memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
  62. if (static_store)
  63. rl->index_run[last] = static_store[last] + MAX_RUN + MAX_LEVEL + 2;
  64. else
  65. rl->index_run[last] = av_malloc(MAX_RUN + 1);
  66. memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
  67. }
  68. }
  69. av_cold void ff_rl_init_vlc(RLTable *rl)
  70. {
  71. int i, q;
  72. for (q = 0; q < 32; q++) {
  73. int qmul = q * 2;
  74. int qadd = (q - 1) | 1;
  75. if (q == 0) {
  76. qmul = 1;
  77. qadd = 0;
  78. }
  79. for (i = 0; i < rl->vlc.table_size; i++) {
  80. int code = rl->vlc.table[i][0];
  81. int len = rl->vlc.table[i][1];
  82. int level, run;
  83. if (len == 0) { // illegal code
  84. run = 66;
  85. level = MAX_LEVEL;
  86. } else if (len < 0) { // more bits needed
  87. run = 0;
  88. level = code;
  89. } else {
  90. if (code == rl->n) { // esc
  91. run = 66;
  92. level = 0;
  93. } else {
  94. run = rl->table_run[code] + 1;
  95. level = rl->table_level[code] * qmul + qadd;
  96. if (code >= rl->last) run += 192;
  97. }
  98. }
  99. rl->rl_vlc[q][i].len = len;
  100. rl->rl_vlc[q][i].level = level;
  101. rl->rl_vlc[q][i].run = run;
  102. }
  103. }
  104. }