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.

142 lines
4.2KB

  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 "libavutil/mem.h"
  21. #include "rl.h"
  22. void ff_rl_free(RLTable *rl)
  23. {
  24. int i;
  25. for (i = 0; i < 2; i++) {
  26. av_freep(&rl->max_run[i]);
  27. av_freep(&rl->max_level[i]);
  28. av_freep(&rl->index_run[i]);
  29. }
  30. }
  31. av_cold int ff_rl_init(RLTable *rl,
  32. uint8_t static_store[2][2 * MAX_RUN + MAX_LEVEL + 3])
  33. {
  34. int8_t max_level[MAX_RUN + 1], max_run[MAX_LEVEL + 1];
  35. uint8_t index_run[MAX_RUN + 1];
  36. int last, run, level, start, end, i;
  37. /* If table is static, we can quit if rl->max_level[0] is not NULL */
  38. if (static_store && rl->max_level[0])
  39. return 0;
  40. /* compute max_level[], max_run[] and index_run[] */
  41. for (last = 0; last < 2; last++) {
  42. if (last == 0) {
  43. start = 0;
  44. end = rl->last;
  45. } else {
  46. start = rl->last;
  47. end = rl->n;
  48. }
  49. memset(max_level, 0, MAX_RUN + 1);
  50. memset(max_run, 0, MAX_LEVEL + 1);
  51. memset(index_run, rl->n, MAX_RUN + 1);
  52. for (i = start; i < end; i++) {
  53. run = rl->table_run[i];
  54. level = rl->table_level[i];
  55. if (index_run[run] == rl->n)
  56. index_run[run] = i;
  57. if (level > max_level[run])
  58. max_level[run] = level;
  59. if (run > max_run[level])
  60. max_run[level] = run;
  61. }
  62. if (static_store)
  63. rl->max_level[last] = static_store[last];
  64. else {
  65. rl->max_level[last] = av_malloc(MAX_RUN + 1);
  66. if (!rl->max_level[last])
  67. goto fail;
  68. }
  69. memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
  70. if (static_store)
  71. rl->max_run[last] = static_store[last] + MAX_RUN + 1;
  72. else {
  73. rl->max_run[last] = av_malloc(MAX_LEVEL + 1);
  74. if (!rl->max_run[last])
  75. goto fail;
  76. }
  77. memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
  78. if (static_store)
  79. rl->index_run[last] = static_store[last] + MAX_RUN + MAX_LEVEL + 2;
  80. else {
  81. rl->index_run[last] = av_malloc(MAX_RUN + 1);
  82. if (!rl->index_run[last])
  83. goto fail;
  84. }
  85. memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
  86. }
  87. return 0;
  88. fail:
  89. ff_rl_free(rl);
  90. return AVERROR(ENOMEM);
  91. }
  92. av_cold void ff_rl_init_vlc(RLTable *rl)
  93. {
  94. int i, q;
  95. for (q = 0; q < 32; q++) {
  96. int qmul = q * 2;
  97. int qadd = (q - 1) | 1;
  98. if (q == 0) {
  99. qmul = 1;
  100. qadd = 0;
  101. }
  102. for (i = 0; i < rl->vlc.table_size; i++) {
  103. int code = rl->vlc.table[i][0];
  104. int len = rl->vlc.table[i][1];
  105. int level, run;
  106. if (len == 0) { // illegal code
  107. run = 66;
  108. level = MAX_LEVEL;
  109. } else if (len < 0) { // more bits needed
  110. run = 0;
  111. level = code;
  112. } else {
  113. if (code == rl->n) { // esc
  114. run = 66;
  115. level = 0;
  116. } else {
  117. run = rl->table_run[code] + 1;
  118. level = rl->table_level[code] * qmul + qadd;
  119. if (code >= rl->last) run += 192;
  120. }
  121. }
  122. rl->rl_vlc[q][i].len = len;
  123. rl->rl_vlc[q][i].level = level;
  124. rl->rl_vlc[q][i].run = run;
  125. }
  126. }
  127. }