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.

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