aaccoder_trellis.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * AAC encoder trellis codebook selector
  3. * Copyright (C) 2008-2009 Konstantin Shishkov
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * AAC encoder trellis codebook selector
  24. * @author Konstantin Shishkov
  25. */
  26. /**
  27. * This file contains a template for the codebook_trellis_rate selector function.
  28. * It needs to be provided, externally, as an already included declaration,
  29. * the following functions from aacenc_quantization/util.h. They're not included
  30. * explicitly here to make it possible to provide alternative implementations:
  31. * - quantize_band_cost_bits
  32. * - abs_pow34_v
  33. */
  34. #ifndef AVCODEC_AACCODER_TRELLIS_H
  35. #define AVCODEC_AACCODER_TRELLIS_H
  36. #include <float.h>
  37. #include "libavutil/mathematics.h"
  38. #include "avcodec.h"
  39. #include "put_bits.h"
  40. #include "aac.h"
  41. #include "aacenc.h"
  42. #include "aactab.h"
  43. #include "aacenctab.h"
  44. /**
  45. * structure used in optimal codebook search
  46. */
  47. typedef struct TrellisBandCodingPath {
  48. int prev_idx; ///< pointer to the previous path point
  49. float cost; ///< path cost
  50. int run;
  51. } TrellisBandCodingPath;
  52. static void codebook_trellis_rate(AACEncContext *s, SingleChannelElement *sce,
  53. int win, int group_len, const float lambda)
  54. {
  55. TrellisBandCodingPath path[120][CB_TOT_ALL];
  56. int w, swb, cb, start, size;
  57. int i, j;
  58. const int max_sfb = sce->ics.max_sfb;
  59. const int run_bits = sce->ics.num_windows == 1 ? 5 : 3;
  60. const int run_esc = (1 << run_bits) - 1;
  61. int idx, ppos, count;
  62. int stackrun[120], stackcb[120], stack_len;
  63. float next_minbits = INFINITY;
  64. int next_mincb = 0;
  65. s->abs_pow34(s->scoefs, sce->coeffs, 1024);
  66. start = win*128;
  67. for (cb = 0; cb < CB_TOT_ALL; cb++) {
  68. path[0][cb].cost = run_bits+4;
  69. path[0][cb].prev_idx = -1;
  70. path[0][cb].run = 0;
  71. }
  72. for (swb = 0; swb < max_sfb; swb++) {
  73. size = sce->ics.swb_sizes[swb];
  74. if (sce->zeroes[win*16 + swb]) {
  75. float cost_stay_here = path[swb][0].cost;
  76. float cost_get_here = next_minbits + run_bits + 4;
  77. if ( run_value_bits[sce->ics.num_windows == 8][path[swb][0].run]
  78. != run_value_bits[sce->ics.num_windows == 8][path[swb][0].run+1])
  79. cost_stay_here += run_bits;
  80. if (cost_get_here < cost_stay_here) {
  81. path[swb+1][0].prev_idx = next_mincb;
  82. path[swb+1][0].cost = cost_get_here;
  83. path[swb+1][0].run = 1;
  84. } else {
  85. path[swb+1][0].prev_idx = 0;
  86. path[swb+1][0].cost = cost_stay_here;
  87. path[swb+1][0].run = path[swb][0].run + 1;
  88. }
  89. next_minbits = path[swb+1][0].cost;
  90. next_mincb = 0;
  91. for (cb = 1; cb < CB_TOT_ALL; cb++) {
  92. path[swb+1][cb].cost = 61450;
  93. path[swb+1][cb].prev_idx = -1;
  94. path[swb+1][cb].run = 0;
  95. }
  96. } else {
  97. float minbits = next_minbits;
  98. int mincb = next_mincb;
  99. int startcb = sce->band_type[win*16+swb];
  100. startcb = aac_cb_in_map[startcb];
  101. next_minbits = INFINITY;
  102. next_mincb = 0;
  103. for (cb = 0; cb < startcb; cb++) {
  104. path[swb+1][cb].cost = 61450;
  105. path[swb+1][cb].prev_idx = -1;
  106. path[swb+1][cb].run = 0;
  107. }
  108. for (cb = startcb; cb < CB_TOT_ALL; cb++) {
  109. float cost_stay_here, cost_get_here;
  110. float bits = 0.0f;
  111. if (cb >= 12 && sce->band_type[win*16+swb] != aac_cb_out_map[cb]) {
  112. path[swb+1][cb].cost = 61450;
  113. path[swb+1][cb].prev_idx = -1;
  114. path[swb+1][cb].run = 0;
  115. continue;
  116. }
  117. for (w = 0; w < group_len; w++) {
  118. bits += quantize_band_cost_bits(s, &sce->coeffs[start + w*128],
  119. &s->scoefs[start + w*128], size,
  120. sce->sf_idx[win*16+swb],
  121. aac_cb_out_map[cb],
  122. 0, INFINITY, NULL, NULL, 0);
  123. }
  124. cost_stay_here = path[swb][cb].cost + bits;
  125. cost_get_here = minbits + bits + run_bits + 4;
  126. if ( run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run]
  127. != run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run+1])
  128. cost_stay_here += run_bits;
  129. if (cost_get_here < cost_stay_here) {
  130. path[swb+1][cb].prev_idx = mincb;
  131. path[swb+1][cb].cost = cost_get_here;
  132. path[swb+1][cb].run = 1;
  133. } else {
  134. path[swb+1][cb].prev_idx = cb;
  135. path[swb+1][cb].cost = cost_stay_here;
  136. path[swb+1][cb].run = path[swb][cb].run + 1;
  137. }
  138. if (path[swb+1][cb].cost < next_minbits) {
  139. next_minbits = path[swb+1][cb].cost;
  140. next_mincb = cb;
  141. }
  142. }
  143. }
  144. start += sce->ics.swb_sizes[swb];
  145. }
  146. //convert resulting path from backward-linked list
  147. stack_len = 0;
  148. idx = 0;
  149. for (cb = 1; cb < CB_TOT_ALL; cb++)
  150. if (path[max_sfb][cb].cost < path[max_sfb][idx].cost)
  151. idx = cb;
  152. ppos = max_sfb;
  153. while (ppos > 0) {
  154. av_assert1(idx >= 0);
  155. cb = idx;
  156. stackrun[stack_len] = path[ppos][cb].run;
  157. stackcb [stack_len] = cb;
  158. idx = path[ppos-path[ppos][cb].run+1][cb].prev_idx;
  159. ppos -= path[ppos][cb].run;
  160. stack_len++;
  161. }
  162. //perform actual band info encoding
  163. start = 0;
  164. for (i = stack_len - 1; i >= 0; i--) {
  165. cb = aac_cb_out_map[stackcb[i]];
  166. put_bits(&s->pb, 4, cb);
  167. count = stackrun[i];
  168. memset(sce->zeroes + win*16 + start, !cb, count);
  169. //XXX: memset when band_type is also uint8_t
  170. for (j = 0; j < count; j++) {
  171. sce->band_type[win*16 + start] = cb;
  172. start++;
  173. }
  174. while (count >= run_esc) {
  175. put_bits(&s->pb, run_bits, run_esc);
  176. count -= run_esc;
  177. }
  178. put_bits(&s->pb, run_bits, count);
  179. }
  180. }
  181. #endif /* AVCODEC_AACCODER_TRELLIS_H */