patch-libavcodec_aacenc_ltp_c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. $OpenBSD: patch-libavcodec_aacenc_ltp_c,v 1.4 2015/12/10 06:53:38 ajacoutot Exp $
  2. aacenc: add support for encoding files using Long Term Prediction
  3. aacenc_ltp: fix assertion
  4. aacenc_ltp: replace av_clip() with av_clip_uintp2()
  5. aacenc_ltp: correct header description comment
  6. aacenc_ltp: adjust and speed up autocorrelation calculations
  7. aac_ltp: split, reorder and improve prediction algorithm
  8. aac_ltp: actually signal LTP as off during EIGHT_SHORT windows
  9. aacenc_ltp: fix out of bounds memory access
  10. aacenc_ltp: disable LTP with high lambda values
  11. --- libavcodec/aacenc_ltp.c.orig Wed Dec 9 01:10:22 2015
  12. +++ libavcodec/aacenc_ltp.c Wed Dec 9 01:11:54 2015
  13. @@ -0,0 +1,236 @@
  14. +/*
  15. + * AAC encoder long term prediction extension
  16. + * Copyright (C) 2015 Rostislav Pehlivanov
  17. + *
  18. + * This file is part of FFmpeg.
  19. + *
  20. + * FFmpeg is free software; you can redistribute it and/or
  21. + * modify it under the terms of the GNU Lesser General Public
  22. + * License as published by the Free Software Foundation; either
  23. + * version 2.1 of the License, or (at your option) any later version.
  24. + *
  25. + * FFmpeg is distributed in the hope that it will be useful,
  26. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  28. + * Lesser General Public License for more details.
  29. + *
  30. + * You should have received a copy of the GNU Lesser General Public
  31. + * License along with FFmpeg; if not, write to the Free Software
  32. + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  33. + */
  34. +
  35. +/**
  36. + * @file
  37. + * AAC encoder long term prediction extension
  38. + * @author Rostislav Pehlivanov ( atomnuker gmail com )
  39. + */
  40. +
  41. +#include "aacenc_ltp.h"
  42. +#include "aacenc_quantization.h"
  43. +#include "aacenc_utils.h"
  44. +
  45. +/**
  46. + * Encode LTP data.
  47. + */
  48. +void ff_aac_encode_ltp_info(AACEncContext *s, SingleChannelElement *sce,
  49. + int common_window)
  50. +{
  51. + int i;
  52. + IndividualChannelStream *ics = &sce->ics;
  53. + if (s->profile != FF_PROFILE_AAC_LTP || !ics->predictor_present)
  54. + return;
  55. + if (common_window)
  56. + put_bits(&s->pb, 1, 0);
  57. + put_bits(&s->pb, 1, ics->ltp.present);
  58. + if (!ics->ltp.present)
  59. + return;
  60. + put_bits(&s->pb, 11, ics->ltp.lag);
  61. + put_bits(&s->pb, 3, ics->ltp.coef_idx);
  62. + for (i = 0; i < FFMIN(ics->max_sfb, MAX_LTP_LONG_SFB); i++)
  63. + put_bits(&s->pb, 1, ics->ltp.used[i]);
  64. +}
  65. +
  66. +void ff_aac_ltp_insert_new_frame(AACEncContext *s)
  67. +{
  68. + int i, ch, tag, chans, cur_channel, start_ch = 0;
  69. + ChannelElement *cpe;
  70. + SingleChannelElement *sce;
  71. + for (i = 0; i < s->chan_map[0]; i++) {
  72. + cpe = &s->cpe[i];
  73. + tag = s->chan_map[i+1];
  74. + chans = tag == TYPE_CPE ? 2 : 1;
  75. + for (ch = 0; ch < chans; ch++) {
  76. + sce = &cpe->ch[ch];
  77. + cur_channel = start_ch + ch;
  78. + /* New sample + overlap */
  79. + memcpy(&sce->ltp_state[0], &sce->ltp_state[1024], 1024*sizeof(sce->ltp_state[0]));
  80. + memcpy(&sce->ltp_state[1024], &s->planar_samples[cur_channel][2048], 1024*sizeof(sce->ltp_state[0]));
  81. + memcpy(&sce->ltp_state[2048], &sce->ret_buf[0], 1024*sizeof(sce->ltp_state[0]));
  82. + sce->ics.ltp.lag = 0;
  83. + }
  84. + start_ch += chans;
  85. + }
  86. +}
  87. +
  88. +static void get_lag(float *buf, const float *new, LongTermPrediction *ltp)
  89. +{
  90. + int i, j, lag, max_corr = 0;
  91. + float max_ratio;
  92. + for (i = 0; i < 2048; i++) {
  93. + float corr, s0 = 0.0f, s1 = 0.0f;
  94. + const int start = FFMAX(0, i - 1024);
  95. + for (j = start; j < 2048; j++) {
  96. + const int idx = j - i + 1024;
  97. + s0 += new[j]*buf[idx];
  98. + s1 += buf[idx]*buf[idx];
  99. + }
  100. + corr = s1 > 0.0f ? s0/sqrt(s1) : 0.0f;
  101. + if (corr > max_corr) {
  102. + max_corr = corr;
  103. + lag = i;
  104. + max_ratio = corr/(2048-start);
  105. + }
  106. + }
  107. + ltp->lag = FFMAX(av_clip_uintp2(lag, 11), 0);
  108. + ltp->coef_idx = quant_array_idx(max_ratio, ltp_coef, 8);
  109. + ltp->coef = ltp_coef[ltp->coef_idx];
  110. +}
  111. +
  112. +static void generate_samples(float *buf, LongTermPrediction *ltp)
  113. +{
  114. + int i, samples_num = 2048;
  115. + if (!ltp->lag) {
  116. + ltp->present = 0;
  117. + return;
  118. + } else if (ltp->lag < 1024) {
  119. + samples_num = ltp->lag + 1024;
  120. + }
  121. + for (i = 0; i < samples_num; i++)
  122. + buf[i] = ltp->coef*buf[i + 2048 - ltp->lag];
  123. + memset(&buf[i], 0, (2048 - i)*sizeof(float));
  124. +}
  125. +
  126. +/**
  127. + * Process LTP parameters
  128. + * @see Patent WO2006070265A1
  129. + */
  130. +void ff_aac_update_ltp(AACEncContext *s, SingleChannelElement *sce)
  131. +{
  132. + float *pred_signal = &sce->ltp_state[0];
  133. + const float *samples = &s->planar_samples[s->cur_channel][1024];
  134. +
  135. + if (s->profile != FF_PROFILE_AAC_LTP)
  136. + return;
  137. +
  138. + /* Calculate lag */
  139. + get_lag(pred_signal, samples, &sce->ics.ltp);
  140. + generate_samples(pred_signal, &sce->ics.ltp);
  141. +}
  142. +
  143. +void ff_aac_adjust_common_ltp(AACEncContext *s, ChannelElement *cpe)
  144. +{
  145. + int sfb, count = 0;
  146. + SingleChannelElement *sce0 = &cpe->ch[0];
  147. + SingleChannelElement *sce1 = &cpe->ch[1];
  148. +
  149. + if (!cpe->common_window ||
  150. + sce0->ics.window_sequence[0] == EIGHT_SHORT_SEQUENCE ||
  151. + sce1->ics.window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
  152. + sce0->ics.ltp.present = 0;
  153. + return;
  154. + }
  155. +
  156. + for (sfb = 0; sfb < FFMIN(sce0->ics.max_sfb, MAX_LTP_LONG_SFB); sfb++) {
  157. + int sum = sce0->ics.ltp.used[sfb] + sce1->ics.ltp.used[sfb];
  158. + if (sum != 2) {
  159. + sce0->ics.ltp.used[sfb] = 0;
  160. + } else if (sum == 2) {
  161. + count++;
  162. + }
  163. + }
  164. +
  165. + sce0->ics.ltp.present = !!count;
  166. + sce0->ics.predictor_present = !!count;
  167. +}
  168. +
  169. +/**
  170. + * Mark LTP sfb's
  171. + */
  172. +void ff_aac_search_for_ltp(AACEncContext *s, SingleChannelElement *sce,
  173. + int common_window)
  174. +{
  175. + int w, g, w2, i, start = 0, count = 0;
  176. + int saved_bits = -(15 + FFMIN(sce->ics.max_sfb, MAX_LTP_LONG_SFB));
  177. + float *C34 = &s->scoefs[128*0], *PCD = &s->scoefs[128*1];
  178. + float *PCD34 = &s->scoefs[128*2];
  179. + const int max_ltp = FFMIN(sce->ics.max_sfb, MAX_LTP_LONG_SFB);
  180. +
  181. + if (sce->ics.window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
  182. + if (sce->ics.ltp.lag) {
  183. + memset(&sce->ltp_state[0], 0, 3072*sizeof(sce->ltp_state[0]));
  184. + memset(&sce->ics.ltp, 0, sizeof(LongTermPrediction));
  185. + }
  186. + return;
  187. + }
  188. +
  189. + if (!sce->ics.ltp.lag || s->lambda > 120.0f)
  190. + return;
  191. +
  192. + for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
  193. + start = 0;
  194. + for (g = 0; g < sce->ics.num_swb; g++) {
  195. + int bits1 = 0, bits2 = 0;
  196. + float dist1 = 0.0f, dist2 = 0.0f;
  197. + if (w*16+g > max_ltp) {
  198. + start += sce->ics.swb_sizes[g];
  199. + continue;
  200. + }
  201. + for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
  202. + int bits_tmp1, bits_tmp2;
  203. + FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
  204. + for (i = 0; i < sce->ics.swb_sizes[g]; i++)
  205. + PCD[i] = sce->coeffs[start+(w+w2)*128+i] - sce->lcoeffs[start+(w+w2)*128+i];
  206. + abs_pow34_v(C34, &sce->coeffs[start+(w+w2)*128], sce->ics.swb_sizes[g]);
  207. + abs_pow34_v(PCD34, PCD, sce->ics.swb_sizes[g]);
  208. + dist1 += quantize_band_cost(s, &sce->coeffs[start+(w+w2)*128], C34, sce->ics.swb_sizes[g],
  209. + sce->sf_idx[(w+w2)*16+g], sce->band_type[(w+w2)*16+g],
  210. + s->lambda/band->threshold, INFINITY, &bits_tmp1, NULL, 0);
  211. + dist2 += quantize_band_cost(s, PCD, PCD34, sce->ics.swb_sizes[g],
  212. + sce->sf_idx[(w+w2)*16+g],
  213. + sce->band_type[(w+w2)*16+g],
  214. + s->lambda/band->threshold, INFINITY, &bits_tmp2, NULL, 0);
  215. + bits1 += bits_tmp1;
  216. + bits2 += bits_tmp2;
  217. + }
  218. + if (dist2 < dist1 && bits2 < bits1) {
  219. + for (w2 = 0; w2 < sce->ics.group_len[w]; w2++)
  220. + for (i = 0; i < sce->ics.swb_sizes[g]; i++)
  221. + sce->coeffs[start+(w+w2)*128+i] -= sce->lcoeffs[start+(w+w2)*128+i];
  222. + sce->ics.ltp.used[w*16+g] = 1;
  223. + saved_bits += bits1 - bits2;
  224. + count++;
  225. + }
  226. + start += sce->ics.swb_sizes[g];
  227. + }
  228. + }
  229. +
  230. + sce->ics.ltp.present = !!count && (saved_bits >= 0);
  231. + sce->ics.predictor_present = !!sce->ics.ltp.present;
  232. +
  233. + /* Reset any marked sfbs */
  234. + if (!sce->ics.ltp.present && !!count) {
  235. + for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
  236. + start = 0;
  237. + for (g = 0; g < sce->ics.num_swb; g++) {
  238. + if (sce->ics.ltp.used[w*16+g]) {
  239. + for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
  240. + for (i = 0; i < sce->ics.swb_sizes[g]; i++) {
  241. + sce->coeffs[start+(w+w2)*128+i] += sce->lcoeffs[start+(w+w2)*128+i];
  242. + }
  243. + }
  244. + }
  245. + start += sce->ics.swb_sizes[g];
  246. + }
  247. + }
  248. + }
  249. +}