2
0

aacenc_ltp.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * AAC encoder long term prediction extension
  3. * Copyright (C) 2015 Rostislav Pehlivanov
  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 long term prediction extension
  24. * @author Rostislav Pehlivanov ( atomnuker gmail com )
  25. */
  26. #include "aacenc_ltp.h"
  27. #include "aacenc_quantization.h"
  28. #include "aacenc_utils.h"
  29. /**
  30. * Encode LTP data.
  31. */
  32. void ff_aac_encode_ltp_info(AACEncContext *s, SingleChannelElement *sce,
  33. int common_window)
  34. {
  35. int i;
  36. IndividualChannelStream *ics = &sce->ics;
  37. if (s->profile != FF_PROFILE_AAC_LTP || !ics->predictor_present)
  38. return;
  39. if (common_window)
  40. put_bits(&s->pb, 1, 0);
  41. put_bits(&s->pb, 1, ics->ltp.present);
  42. if (!ics->ltp.present)
  43. return;
  44. put_bits(&s->pb, 11, ics->ltp.lag);
  45. put_bits(&s->pb, 3, ics->ltp.coef_idx);
  46. for (i = 0; i < FFMIN(ics->max_sfb, MAX_LTP_LONG_SFB); i++)
  47. put_bits(&s->pb, 1, ics->ltp.used[i]);
  48. }
  49. void ff_aac_ltp_insert_new_frame(AACEncContext *s)
  50. {
  51. int i, ch, tag, chans, cur_channel, start_ch = 0;
  52. ChannelElement *cpe;
  53. SingleChannelElement *sce;
  54. for (i = 0; i < s->chan_map[0]; i++) {
  55. cpe = &s->cpe[i];
  56. tag = s->chan_map[i+1];
  57. chans = tag == TYPE_CPE ? 2 : 1;
  58. for (ch = 0; ch < chans; ch++) {
  59. sce = &cpe->ch[ch];
  60. cur_channel = start_ch + ch;
  61. /* New sample + overlap */
  62. memcpy(&sce->ltp_state[0], &sce->ltp_state[1024], 1024*sizeof(sce->ltp_state[0]));
  63. memcpy(&sce->ltp_state[1024], &s->planar_samples[cur_channel][2048], 1024*sizeof(sce->ltp_state[0]));
  64. memcpy(&sce->ltp_state[2048], &sce->ret_buf[0], 1024*sizeof(sce->ltp_state[0]));
  65. sce->ics.ltp.lag = 0;
  66. }
  67. start_ch += chans;
  68. }
  69. }
  70. static void get_lag(float *buf, const float *new, LongTermPrediction *ltp)
  71. {
  72. int i, j, lag = 0, max_corr = 0;
  73. float max_ratio = 0.0f;
  74. for (i = 0; i < 2048; i++) {
  75. float corr, s0 = 0.0f, s1 = 0.0f;
  76. const int start = FFMAX(0, i - 1024);
  77. for (j = start; j < 2048; j++) {
  78. const int idx = j - i + 1024;
  79. s0 += new[j]*buf[idx];
  80. s1 += buf[idx]*buf[idx];
  81. }
  82. corr = s1 > 0.0f ? s0/sqrt(s1) : 0.0f;
  83. if (corr > max_corr) {
  84. max_corr = corr;
  85. lag = i;
  86. max_ratio = corr/(2048-start);
  87. }
  88. }
  89. ltp->lag = FFMAX(av_clip_uintp2(lag, 11), 0);
  90. ltp->coef_idx = quant_array_idx(max_ratio, ltp_coef, 8);
  91. ltp->coef = ltp_coef[ltp->coef_idx];
  92. }
  93. static void generate_samples(float *buf, LongTermPrediction *ltp)
  94. {
  95. int i, samples_num = 2048;
  96. if (!ltp->lag) {
  97. ltp->present = 0;
  98. return;
  99. } else if (ltp->lag < 1024) {
  100. samples_num = ltp->lag + 1024;
  101. }
  102. for (i = 0; i < samples_num; i++)
  103. buf[i] = ltp->coef*buf[i + 2048 - ltp->lag];
  104. memset(&buf[i], 0, (2048 - i)*sizeof(float));
  105. }
  106. /**
  107. * Process LTP parameters
  108. * @see Patent WO2006070265A1
  109. */
  110. void ff_aac_update_ltp(AACEncContext *s, SingleChannelElement *sce)
  111. {
  112. float *pred_signal = &sce->ltp_state[0];
  113. const float *samples = &s->planar_samples[s->cur_channel][1024];
  114. if (s->profile != FF_PROFILE_AAC_LTP)
  115. return;
  116. /* Calculate lag */
  117. get_lag(pred_signal, samples, &sce->ics.ltp);
  118. generate_samples(pred_signal, &sce->ics.ltp);
  119. }
  120. void ff_aac_adjust_common_ltp(AACEncContext *s, ChannelElement *cpe)
  121. {
  122. int sfb, count = 0;
  123. SingleChannelElement *sce0 = &cpe->ch[0];
  124. SingleChannelElement *sce1 = &cpe->ch[1];
  125. if (!cpe->common_window ||
  126. sce0->ics.window_sequence[0] == EIGHT_SHORT_SEQUENCE ||
  127. sce1->ics.window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
  128. sce0->ics.ltp.present = 0;
  129. return;
  130. }
  131. for (sfb = 0; sfb < FFMIN(sce0->ics.max_sfb, MAX_LTP_LONG_SFB); sfb++) {
  132. int sum = sce0->ics.ltp.used[sfb] + sce1->ics.ltp.used[sfb];
  133. if (sum != 2) {
  134. sce0->ics.ltp.used[sfb] = 0;
  135. } else if (sum == 2) {
  136. count++;
  137. }
  138. }
  139. sce0->ics.ltp.present = !!count;
  140. sce0->ics.predictor_present = !!count;
  141. }
  142. /**
  143. * Mark LTP sfb's
  144. */
  145. void ff_aac_search_for_ltp(AACEncContext *s, SingleChannelElement *sce,
  146. int common_window)
  147. {
  148. int w, g, w2, i, start = 0, count = 0;
  149. int saved_bits = -(15 + FFMIN(sce->ics.max_sfb, MAX_LTP_LONG_SFB));
  150. float *C34 = &s->scoefs[128*0], *PCD = &s->scoefs[128*1];
  151. float *PCD34 = &s->scoefs[128*2];
  152. const int max_ltp = FFMIN(sce->ics.max_sfb, MAX_LTP_LONG_SFB);
  153. if (sce->ics.window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
  154. if (sce->ics.ltp.lag) {
  155. memset(&sce->ltp_state[0], 0, 3072*sizeof(sce->ltp_state[0]));
  156. memset(&sce->ics.ltp, 0, sizeof(LongTermPrediction));
  157. }
  158. return;
  159. }
  160. if (!sce->ics.ltp.lag || s->lambda > 120.0f)
  161. return;
  162. for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
  163. start = 0;
  164. for (g = 0; g < sce->ics.num_swb; g++) {
  165. int bits1 = 0, bits2 = 0;
  166. float dist1 = 0.0f, dist2 = 0.0f;
  167. if (w*16+g > max_ltp) {
  168. start += sce->ics.swb_sizes[g];
  169. continue;
  170. }
  171. for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
  172. int bits_tmp1, bits_tmp2;
  173. FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
  174. for (i = 0; i < sce->ics.swb_sizes[g]; i++)
  175. PCD[i] = sce->coeffs[start+(w+w2)*128+i] - sce->lcoeffs[start+(w+w2)*128+i];
  176. s->abs_pow34(C34, &sce->coeffs[start+(w+w2)*128], sce->ics.swb_sizes[g]);
  177. s->abs_pow34(PCD34, PCD, sce->ics.swb_sizes[g]);
  178. dist1 += quantize_band_cost(s, &sce->coeffs[start+(w+w2)*128], C34, sce->ics.swb_sizes[g],
  179. sce->sf_idx[(w+w2)*16+g], sce->band_type[(w+w2)*16+g],
  180. s->lambda/band->threshold, INFINITY, &bits_tmp1, NULL, 0);
  181. dist2 += quantize_band_cost(s, PCD, PCD34, sce->ics.swb_sizes[g],
  182. sce->sf_idx[(w+w2)*16+g],
  183. sce->band_type[(w+w2)*16+g],
  184. s->lambda/band->threshold, INFINITY, &bits_tmp2, NULL, 0);
  185. bits1 += bits_tmp1;
  186. bits2 += bits_tmp2;
  187. }
  188. if (dist2 < dist1 && bits2 < bits1) {
  189. for (w2 = 0; w2 < sce->ics.group_len[w]; w2++)
  190. for (i = 0; i < sce->ics.swb_sizes[g]; i++)
  191. sce->coeffs[start+(w+w2)*128+i] -= sce->lcoeffs[start+(w+w2)*128+i];
  192. sce->ics.ltp.used[w*16+g] = 1;
  193. saved_bits += bits1 - bits2;
  194. count++;
  195. }
  196. start += sce->ics.swb_sizes[g];
  197. }
  198. }
  199. sce->ics.ltp.present = !!count && (saved_bits >= 0);
  200. sce->ics.predictor_present = !!sce->ics.ltp.present;
  201. /* Reset any marked sfbs */
  202. if (!sce->ics.ltp.present && !!count) {
  203. for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
  204. start = 0;
  205. for (g = 0; g < sce->ics.num_swb; g++) {
  206. if (sce->ics.ltp.used[w*16+g]) {
  207. for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
  208. for (i = 0; i < sce->ics.swb_sizes[g]; i++) {
  209. sce->coeffs[start+(w+w2)*128+i] += sce->lcoeffs[start+(w+w2)*128+i];
  210. }
  211. }
  212. }
  213. start += sce->ics.swb_sizes[g];
  214. }
  215. }
  216. }
  217. }