patch-libavcodec_aaccoder_trellis_h 7.6 KB

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