presets.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /*
  2. * presets.c -- Apply presets
  3. *
  4. * Copyright (c) 2002-2008 Gabriel Bouvigne
  5. * Copyright (c) 2007-2012 Robert Hegemann
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Library General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2 of the License, or (at your option) any later version.
  11. *
  12. * This library 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. * Library General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Library General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. # include <config.h>
  23. #endif
  24. #include "lame.h"
  25. #include "machine.h"
  26. #include "set_get.h"
  27. #include "encoder.h"
  28. #include "util.h"
  29. #include "lame_global_flags.h"
  30. #define SET_OPTION(opt, val, def) if (enforce) \
  31. (void) lame_set_##opt(gfp, val); \
  32. else if (!(fabs(lame_get_##opt(gfp) - def) > 0)) \
  33. (void) lame_set_##opt(gfp, val);
  34. #define SET__OPTION(opt, val, def) if (enforce) \
  35. lame_set_##opt(gfp, val); \
  36. else if (!(fabs(lame_get_##opt(gfp) - def) > 0)) \
  37. lame_set_##opt(gfp, val);
  38. #undef Min
  39. #undef Max
  40. static inline int
  41. min_int(int a, int b)
  42. {
  43. if (a < b) {
  44. return a;
  45. }
  46. return b;
  47. }
  48. static inline int
  49. max_int(int a, int b)
  50. {
  51. if (a > b) {
  52. return a;
  53. }
  54. return b;
  55. }
  56. typedef struct {
  57. int vbr_q;
  58. int quant_comp;
  59. int quant_comp_s;
  60. int expY;
  61. FLOAT st_lrm; /*short threshold */
  62. FLOAT st_s;
  63. FLOAT masking_adj;
  64. FLOAT masking_adj_short;
  65. FLOAT ath_lower;
  66. FLOAT ath_curve;
  67. FLOAT ath_sensitivity;
  68. FLOAT interch;
  69. int safejoint;
  70. int sfb21mod;
  71. FLOAT msfix;
  72. FLOAT minval;
  73. FLOAT ath_fixpoint;
  74. } vbr_presets_t;
  75. /* *INDENT-OFF* */
  76. /* Switch mappings for VBR mode VBR_RH */
  77. static const vbr_presets_t vbr_old_switch_map[] = {
  78. /*vbr_q qcomp_l qcomp_s expY st_lrm st_s mask adj_l adj_s ath_lower ath_curve ath_sens interChR safejoint sfb21mod msfix */
  79. {0, 9, 9, 0, 5.20, 125.0, -4.2, -6.3, 4.8, 1, 0, 0, 2, 21, 0.97, 5, 100},
  80. {1, 9, 9, 0, 5.30, 125.0, -3.6, -5.6, 4.5, 1.5, 0, 0, 2, 21, 1.35, 5, 100},
  81. {2, 9, 9, 0, 5.60, 125.0, -2.2, -3.5, 2.8, 2, 0, 0, 2, 21, 1.49, 5, 100},
  82. {3, 9, 9, 1, 5.80, 130.0, -1.8, -2.8, 2.6, 3, -4, 0, 2, 20, 1.64, 5, 100},
  83. {4, 9, 9, 1, 6.00, 135.0, -0.7, -1.1, 1.1, 3.5, -8, 0, 2, 0, 1.79, 5, 100},
  84. {5, 9, 9, 1, 6.40, 140.0, 0.5, 0.4, -7.5, 4, -12, 0.0002, 0, 0, 1.95, 5, 100},
  85. {6, 9, 9, 1, 6.60, 145.0, 0.67, 0.65, -14.7, 6.5, -19, 0.0004, 0, 0, 2.30, 5, 100},
  86. {7, 9, 9, 1, 6.60, 145.0, 0.8, 0.75, -19.7, 8, -22, 0.0006, 0, 0, 2.70, 5, 100},
  87. {8, 9, 9, 1, 6.60, 145.0, 1.2, 1.15, -27.5, 10, -23, 0.0007, 0, 0, 0, 5, 100},
  88. {9, 9, 9, 1, 6.60, 145.0, 1.6, 1.6, -36, 11, -25, 0.0008, 0, 0, 0, 5, 100},
  89. {10, 9, 9, 1, 6.60, 145.0, 2.0, 2.0, -36, 12, -25, 0.0008, 0, 0, 0, 5, 100}
  90. };
  91. static const vbr_presets_t vbr_mt_psy_switch_map[] = {
  92. /*vbr_q qcomp_l qcomp_s expY st_lrm st_s mask adj_l adj_s ath_lower ath_curve ath_sens --- safejoint sfb21mod msfix */
  93. {0, 9, 9, 0, 4.20, 25.0, -6.8, -6.8, 7.1, 1, 0, 0, 2, 31, 1.000, 5, 100},
  94. {1, 9, 9, 0, 4.20, 25.0, -4.8, -4.8, 5.4, 1.4, -1, 0, 2, 27, 1.122, 5, 98},
  95. {2, 9, 9, 0, 4.20, 25.0, -2.6, -2.6, 3.7, 2.0, -3, 0, 2, 23, 1.288, 5, 97},
  96. {3, 9, 9, 1, 4.20, 25.0, -1.6, -1.6, 2.0, 2.0, -5, 0, 2, 18, 1.479, 5, 96},
  97. {4, 9, 9, 1, 4.20, 25.0, -0.0, -0.0, 0.0, 2.0, -8, 0, 2, 12, 1.698, 5, 95},
  98. {5, 9, 9, 1, 4.20, 25.0, 1.3, 1.3, -6, 3.5, -11, 0, 2, 8, 1.950, 5, 94.2},
  99. #if 0
  100. {6, 9, 9, 1, 4.50, 100.0, 1.5, 1.5, -24.0, 6.0, -14, 0, 2, 4, 2.239, 3, 93.9},
  101. {7, 9, 9, 1, 4.80, 200.0, 1.7, 1.7, -28.0, 9.0, -20, 0, 2, 0, 2.570, 1, 93.6},
  102. #else
  103. {6, 9, 9, 1, 4.50, 100.0, 2.2, 2.3, -12.0, 6.0, -14, 0, 2, 4, 2.239, 3, 93.9},
  104. {7, 9, 9, 1, 4.80, 200.0, 2.7, 2.7, -18.0, 9.0, -17, 0, 2, 0, 2.570, 1, 93.6},
  105. #endif
  106. {8, 9, 9, 1, 5.30, 300.0, 2.8, 2.8, -21.0, 10.0, -23, 0.0002, 0, 0, 2.951, 0, 93.3},
  107. {9, 9, 9, 1, 6.60, 300.0, 2.8, 2.8, -23.0, 11.0, -25, 0.0006, 0, 0, 3.388, 0, 93.3},
  108. {10, 9, 9, 1, 25.00, 300.0, 2.8, 2.8, -25.0, 12.0, -27, 0.0025, 0, 0, 3.500, 0, 93.3}
  109. };
  110. /* *INDENT-ON* */
  111. static vbr_presets_t const*
  112. get_vbr_preset(int v)
  113. {
  114. switch (v) {
  115. case vbr_mtrh:
  116. case vbr_mt:
  117. return &vbr_mt_psy_switch_map[0];
  118. default:
  119. return &vbr_old_switch_map[0];
  120. }
  121. }
  122. #define NOOP(m) (void)p.m
  123. #define LERP(m) (p.m = p.m + x * (q.m - p.m))
  124. static void
  125. apply_vbr_preset(lame_global_flags * gfp, int a, int enforce)
  126. {
  127. vbr_presets_t const *vbr_preset = get_vbr_preset(lame_get_VBR(gfp));
  128. float x = gfp->VBR_q_frac;
  129. vbr_presets_t p = vbr_preset[a];
  130. vbr_presets_t q = vbr_preset[a + 1];
  131. vbr_presets_t const *set = &p;
  132. NOOP(vbr_q);
  133. NOOP(quant_comp);
  134. NOOP(quant_comp_s);
  135. NOOP(expY);
  136. LERP(st_lrm);
  137. LERP(st_s);
  138. LERP(masking_adj);
  139. LERP(masking_adj_short);
  140. LERP(ath_lower);
  141. LERP(ath_curve);
  142. LERP(ath_sensitivity);
  143. LERP(interch);
  144. NOOP(safejoint);
  145. LERP(sfb21mod);
  146. LERP(msfix);
  147. LERP(minval);
  148. LERP(ath_fixpoint);
  149. (void) lame_set_VBR_q(gfp, set->vbr_q);
  150. SET_OPTION(quant_comp, set->quant_comp, -1);
  151. SET_OPTION(quant_comp_short, set->quant_comp_s, -1);
  152. if (set->expY) {
  153. (void) lame_set_experimentalY(gfp, set->expY);
  154. }
  155. SET_OPTION(short_threshold_lrm, set->st_lrm, -1);
  156. SET_OPTION(short_threshold_s, set->st_s, -1);
  157. SET_OPTION(maskingadjust, set->masking_adj, 0);
  158. SET_OPTION(maskingadjust_short, set->masking_adj_short, 0);
  159. if (lame_get_VBR(gfp) == vbr_mt || lame_get_VBR(gfp) == vbr_mtrh) {
  160. lame_set_ATHtype(gfp, 5);
  161. }
  162. SET_OPTION(ATHlower, set->ath_lower, 0);
  163. SET_OPTION(ATHcurve, set->ath_curve, -1);
  164. SET_OPTION(athaa_sensitivity, set->ath_sensitivity, 0);
  165. if (set->interch > 0) {
  166. SET_OPTION(interChRatio, set->interch, -1);
  167. }
  168. /* parameters for which there is no proper set/get interface */
  169. if (set->safejoint > 0) {
  170. (void) lame_set_exp_nspsytune(gfp, lame_get_exp_nspsytune(gfp) | 2);
  171. }
  172. if (set->sfb21mod > 0) {
  173. int const nsp = lame_get_exp_nspsytune(gfp);
  174. int const val = (nsp >> 20) & 63;
  175. if (val == 0) {
  176. int const sf21mod = (set->sfb21mod << 20) | nsp;
  177. (void) lame_set_exp_nspsytune(gfp, sf21mod);
  178. }
  179. }
  180. SET__OPTION(msfix, set->msfix, -1);
  181. if (enforce == 0) {
  182. gfp->VBR_q = a;
  183. gfp->VBR_q_frac = x;
  184. }
  185. gfp->internal_flags->cfg.minval = set->minval;
  186. { /* take care of gain adjustments */
  187. double const x = fabs(gfp->scale);
  188. double const y = (x > 0.f) ? (10.f * log10(x)) : 0.f;
  189. gfp->internal_flags->cfg.ATHfixpoint = set->ath_fixpoint - y;
  190. }
  191. }
  192. static int
  193. apply_abr_preset(lame_global_flags * gfp, int preset, int enforce)
  194. {
  195. typedef struct {
  196. int abr_kbps;
  197. int quant_comp;
  198. int quant_comp_s;
  199. int safejoint;
  200. FLOAT nsmsfix;
  201. FLOAT st_lrm; /*short threshold */
  202. FLOAT st_s;
  203. FLOAT scale;
  204. FLOAT masking_adj;
  205. FLOAT ath_lower;
  206. FLOAT ath_curve;
  207. FLOAT interch;
  208. int sfscale;
  209. } abr_presets_t;
  210. /* *INDENT-OFF* */
  211. /*
  212. * Switch mappings for ABR mode
  213. */
  214. const abr_presets_t abr_switch_map[] = {
  215. /* kbps quant q_s safejoint nsmsfix st_lrm st_s scale msk ath_lwr ath_curve interch , sfscale */
  216. { 8, 9, 9, 0, 0, 6.60, 145, 0.95, 0, -30.0, 11, 0.0012, 1}, /* 8, impossible to use in stereo */
  217. { 16, 9, 9, 0, 0, 6.60, 145, 0.95, 0, -25.0, 11, 0.0010, 1}, /* 16 */
  218. { 24, 9, 9, 0, 0, 6.60, 145, 0.95, 0, -20.0, 11, 0.0010, 1}, /* 24 */
  219. { 32, 9, 9, 0, 0, 6.60, 145, 0.95, 0, -15.0, 11, 0.0010, 1}, /* 32 */
  220. { 40, 9, 9, 0, 0, 6.60, 145, 0.95, 0, -10.0, 11, 0.0009, 1}, /* 40 */
  221. { 48, 9, 9, 0, 0, 6.60, 145, 0.95, 0, -10.0, 11, 0.0009, 1}, /* 48 */
  222. { 56, 9, 9, 0, 0, 6.60, 145, 0.95, 0, -6.0, 11, 0.0008, 1}, /* 56 */
  223. { 64, 9, 9, 0, 0, 6.60, 145, 0.95, 0, -2.0, 11, 0.0008, 1}, /* 64 */
  224. { 80, 9, 9, 0, 0, 6.60, 145, 0.95, 0, .0, 8, 0.0007, 1}, /* 80 */
  225. { 96, 9, 9, 0, 2.50, 6.60, 145, 0.95, 0, 1.0, 5.5, 0.0006, 1}, /* 96 */
  226. {112, 9, 9, 0, 2.25, 6.60, 145, 0.95, 0, 2.0, 4.5, 0.0005, 1}, /* 112 */
  227. {128, 9, 9, 0, 1.95, 6.40, 140, 0.95, 0, 3.0, 4, 0.0002, 1}, /* 128 */
  228. {160, 9, 9, 1, 1.79, 6.00, 135, 0.95, -2, 5.0, 3.5, 0, 1}, /* 160 */
  229. {192, 9, 9, 1, 1.49, 5.60, 125, 0.97, -4, 7.0, 3, 0, 0}, /* 192 */
  230. {224, 9, 9, 1, 1.25, 5.20, 125, 0.98, -6, 9.0, 2, 0, 0}, /* 224 */
  231. {256, 9, 9, 1, 0.97, 5.20, 125, 1.00, -8, 10.0, 1, 0, 0}, /* 256 */
  232. {320, 9, 9, 1, 0.90, 5.20, 125, 1.00, -10, 12.0, 0, 0, 0} /* 320 */
  233. };
  234. /* *INDENT-ON* */
  235. /* Variables for the ABR stuff */
  236. int r;
  237. int actual_bitrate = preset;
  238. r = nearestBitrateFullIndex(preset);
  239. (void) lame_set_VBR(gfp, vbr_abr);
  240. (void) lame_set_VBR_mean_bitrate_kbps(gfp, (actual_bitrate));
  241. (void) lame_set_VBR_mean_bitrate_kbps(gfp, min_int(lame_get_VBR_mean_bitrate_kbps(gfp), 320));
  242. (void) lame_set_VBR_mean_bitrate_kbps(gfp, max_int(lame_get_VBR_mean_bitrate_kbps(gfp), 8));
  243. (void) lame_set_brate(gfp, lame_get_VBR_mean_bitrate_kbps(gfp));
  244. /* parameters for which there is no proper set/get interface */
  245. if (abr_switch_map[r].safejoint > 0)
  246. (void) lame_set_exp_nspsytune(gfp, lame_get_exp_nspsytune(gfp) | 2); /* safejoint */
  247. if (abr_switch_map[r].sfscale > 0)
  248. (void) lame_set_sfscale(gfp, 1);
  249. SET_OPTION(quant_comp, abr_switch_map[r].quant_comp, -1);
  250. SET_OPTION(quant_comp_short, abr_switch_map[r].quant_comp_s, -1);
  251. SET__OPTION(msfix, abr_switch_map[r].nsmsfix, -1);
  252. SET_OPTION(short_threshold_lrm, abr_switch_map[r].st_lrm, -1);
  253. SET_OPTION(short_threshold_s, abr_switch_map[r].st_s, -1);
  254. /* ABR seems to have big problems with clipping, especially at low bitrates */
  255. /* so we compensate for that here by using a scale value depending on bitrate */
  256. lame_set_scale(gfp, lame_get_scale(gfp) * abr_switch_map[r].scale);
  257. SET_OPTION(maskingadjust, abr_switch_map[r].masking_adj, 0);
  258. if (abr_switch_map[r].masking_adj > 0) {
  259. SET_OPTION(maskingadjust_short, abr_switch_map[r].masking_adj * .9, 0);
  260. }
  261. else {
  262. SET_OPTION(maskingadjust_short, abr_switch_map[r].masking_adj * 1.1, 0);
  263. }
  264. SET_OPTION(ATHlower, abr_switch_map[r].ath_lower, 0);
  265. SET_OPTION(ATHcurve, abr_switch_map[r].ath_curve, -1);
  266. SET_OPTION(interChRatio, abr_switch_map[r].interch, -1);
  267. (void) abr_switch_map[r].abr_kbps;
  268. gfp->internal_flags->cfg.minval = 5. * (abr_switch_map[r].abr_kbps / 320.);
  269. return preset;
  270. }
  271. int
  272. apply_preset(lame_global_flags * gfp, int preset, int enforce)
  273. {
  274. /*translate legacy presets */
  275. switch (preset) {
  276. case R3MIX:
  277. {
  278. preset = V3;
  279. (void) lame_set_VBR(gfp, vbr_mtrh);
  280. break;
  281. }
  282. case MEDIUM:
  283. case MEDIUM_FAST:
  284. {
  285. preset = V4;
  286. (void) lame_set_VBR(gfp, vbr_mtrh);
  287. break;
  288. }
  289. case STANDARD:
  290. case STANDARD_FAST:
  291. {
  292. preset = V2;
  293. (void) lame_set_VBR(gfp, vbr_mtrh);
  294. break;
  295. }
  296. case EXTREME:
  297. case EXTREME_FAST:
  298. {
  299. preset = V0;
  300. (void) lame_set_VBR(gfp, vbr_mtrh);
  301. break;
  302. }
  303. case INSANE:
  304. {
  305. preset = 320;
  306. gfp->preset = preset;
  307. (void) apply_abr_preset(gfp, preset, enforce);
  308. lame_set_VBR(gfp, vbr_off);
  309. return preset;
  310. }
  311. }
  312. gfp->preset = preset;
  313. {
  314. switch (preset) {
  315. case V9:
  316. apply_vbr_preset(gfp, 9, enforce);
  317. return preset;
  318. case V8:
  319. apply_vbr_preset(gfp, 8, enforce);
  320. return preset;
  321. case V7:
  322. apply_vbr_preset(gfp, 7, enforce);
  323. return preset;
  324. case V6:
  325. apply_vbr_preset(gfp, 6, enforce);
  326. return preset;
  327. case V5:
  328. apply_vbr_preset(gfp, 5, enforce);
  329. return preset;
  330. case V4:
  331. apply_vbr_preset(gfp, 4, enforce);
  332. return preset;
  333. case V3:
  334. apply_vbr_preset(gfp, 3, enforce);
  335. return preset;
  336. case V2:
  337. apply_vbr_preset(gfp, 2, enforce);
  338. return preset;
  339. case V1:
  340. apply_vbr_preset(gfp, 1, enforce);
  341. return preset;
  342. case V0:
  343. apply_vbr_preset(gfp, 0, enforce);
  344. return preset;
  345. default:
  346. break;
  347. }
  348. }
  349. if (8 <= preset && preset <= 320) {
  350. return apply_abr_preset(gfp, preset, enforce);
  351. }
  352. gfp->preset = 0; /*no corresponding preset found */
  353. return preset;
  354. }