rl.c 4.5 KB

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