config.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. #include "config.h"
  2. #include <windows.h>
  3. #include <strsafe.h>
  4. #include <assert.h>
  5. #include "mp4FastAAClib.h"
  6. #include "preferences.h"
  7. AACConfigurationFile *AACConfig_Create(unsigned int type, const char *filename)
  8. {
  9. AACConfigurationFile *cfg = (AACConfigurationFile*)calloc(1, sizeof(AACConfigurationFile));
  10. if (cfg)
  11. {
  12. cfg->type = type;
  13. if (filename)
  14. lstrcpynA(cfg->config_file, filename, MAX_PATH);
  15. else
  16. cfg->config_file[0] = 0;
  17. AACConfig_Load(cfg);
  18. }
  19. return cfg;
  20. }
  21. void AACConfig_Load(AACConfigurationFile *cfg)
  22. {
  23. if (cfg->type == ENCODER_TYPE_MPEG4)
  24. {
  25. cfg->config.mode = GetPrivateProfileIntA("audio_fhgaac", "mode", AAC_DEFAULT_MODE, cfg->config_file);
  26. cfg->config.profile = GetPrivateProfileIntA("audio_fhgaac", "profile", AAC_DEFAULT_PROFILE, cfg->config_file);
  27. cfg->config.bitrate = GetPrivateProfileIntA("audio_fhgaac", "bitrate", AAC_DEFAULT_BITRATE, cfg->config_file);
  28. cfg->config.preset = GetPrivateProfileIntA("audio_fhgaac", "preset", AAC_DEFAULT_PRESET, cfg->config_file);
  29. cfg->config.surround = GetPrivateProfileIntA("audio_fhgaac", "surround", AAC_DEFAULT_SURROUND, cfg->config_file);
  30. cfg->shoutcast = 0;
  31. }
  32. else
  33. {
  34. cfg->config.mode = AAC_MODE_CBR;
  35. cfg->config.profile = GetPrivateProfileIntA("audio_adtsaac", "profile", AAC_PROFILE_HE, cfg->config_file);
  36. cfg->config.bitrate = GetPrivateProfileIntA("audio_adtsaac", "bitrate", 64, cfg->config_file);
  37. cfg->config.surround = GetPrivateProfileIntA("audio_adtsaac", "surround", AAC_DEFAULT_SURROUND, cfg->config_file);
  38. cfg->shoutcast = GetPrivateProfileIntA("audio_adtsaac", "shoutcast", 0, cfg->config_file);
  39. }
  40. }
  41. void AACConfig_Save(const AACConfigurationFile *cfg)
  42. {
  43. char temp[128] = {0};
  44. if (cfg->type == ENCODER_TYPE_MPEG4)
  45. {
  46. StringCbPrintfA(temp, sizeof(temp), "%u", cfg->config.mode);
  47. WritePrivateProfileStringA("audio_fhgaac", "mode", temp, cfg->config_file);
  48. StringCbPrintfA(temp, sizeof(temp), "%u", cfg->config.profile);
  49. WritePrivateProfileStringA("audio_fhgaac", "profile", temp, cfg->config_file);
  50. StringCbPrintfA(temp, sizeof(temp), "%u", cfg->config.bitrate);
  51. WritePrivateProfileStringA("audio_fhgaac", "bitrate", temp, cfg->config_file);
  52. StringCbPrintfA(temp, sizeof(temp), "%u", cfg->config.preset);
  53. WritePrivateProfileStringA("audio_fhgaac", "preset", temp, cfg->config_file);
  54. StringCbPrintfA(temp, sizeof(temp), "%u", cfg->config.surround);
  55. WritePrivateProfileStringA("audio_fhgaac", "surround", temp, cfg->config_file);
  56. }
  57. else
  58. {
  59. StringCbPrintfA(temp, sizeof(temp), "%u", cfg->config.profile);
  60. WritePrivateProfileStringA("audio_adtsaac", "profile", temp, cfg->config_file);
  61. StringCbPrintfA(temp, sizeof(temp), "%u", cfg->config.bitrate);
  62. WritePrivateProfileStringA("audio_adtsaac", "bitrate", temp, cfg->config_file);
  63. StringCbPrintfA(temp, sizeof(temp), "%u", cfg->config.surround);
  64. WritePrivateProfileStringA("audio_adtsaac", "surround", temp, cfg->config_file);
  65. }
  66. }
  67. void AACConfig_GetBitrateRange(const AACConfiguration *cfg, int *low, int *high)
  68. {
  69. switch(cfg->profile)
  70. {
  71. case AAC_PROFILE_AUTOMATIC:
  72. *low = 12000;
  73. *high = 448000;
  74. break;
  75. case AAC_PROFILE_LC:
  76. *low = 16000;
  77. *high = 448000;
  78. break;
  79. case AAC_PROFILE_HE:
  80. *low = 16000;
  81. *high = 128000;
  82. break;
  83. case AAC_PROFILE_HE_V2:
  84. *low = 12000;
  85. *high = 56000;
  86. break;
  87. }
  88. }
  89. AUD_OBJ_TYP AACConfig_GetAOT(const AACConfiguration *cfg)
  90. {
  91. if (cfg->mode == AAC_MODE_VBR)
  92. {
  93. switch(cfg->preset)
  94. {
  95. case 1:
  96. return AUD_OBJ_TYP_PS;
  97. case 2:
  98. return AUD_OBJ_TYP_HEAAC;
  99. default:
  100. return AUD_OBJ_TYP_LC;
  101. }
  102. }
  103. else switch (cfg->profile) /* CBR */
  104. {
  105. case AAC_PROFILE_AUTOMATIC:
  106. if (cfg->bitrate <= 40)
  107. return AUD_OBJ_TYP_PS;
  108. else if (cfg->bitrate <= 80)
  109. return AUD_OBJ_TYP_HEAAC;
  110. else
  111. return AUD_OBJ_TYP_LC;
  112. case AAC_PROFILE_LC:
  113. return AUD_OBJ_TYP_LC;
  114. case AAC_PROFILE_HE:
  115. return AUD_OBJ_TYP_HEAAC;
  116. case AAC_PROFILE_HE_V2:
  117. return AUD_OBJ_TYP_PS;
  118. }
  119. return AUD_OBJ_TYP_LC;
  120. }
  121. int AACConfig_GetBitrate(const AACConfiguration *cfg, unsigned int channels)
  122. {
  123. if (cfg->mode == AAC_MODE_VBR)
  124. {
  125. switch(cfg->preset)
  126. {
  127. case 1:
  128. return 16000*channels;
  129. case 2:
  130. return 32000*channels;
  131. case 3:
  132. return 48000*channels;
  133. case 4:
  134. return 64000*channels;
  135. case 5:
  136. return 96000*channels;
  137. case 6:
  138. return 128000*channels;
  139. default:
  140. return 0;
  141. }
  142. }
  143. else
  144. return cfg->bitrate * 1000;
  145. }
  146. MPEG4ENC_BITRATE_MODE AACConfig_GetBitrateMode(const AACConfiguration *cfg)
  147. {
  148. if (cfg->mode == AAC_MODE_VBR)
  149. {
  150. /* by coincidence, these match
  151. to help future maintainers, let's assert this fact */
  152. assert(MP4_BR_MODE_VBR_1 == (MPEG4ENC_BITRATE_MODE)1);
  153. assert(MP4_BR_MODE_VBR_2 == (MPEG4ENC_BITRATE_MODE)2);
  154. assert(MP4_BR_MODE_VBR_3 == (MPEG4ENC_BITRATE_MODE)3);
  155. assert(MP4_BR_MODE_VBR_4 == (MPEG4ENC_BITRATE_MODE)4);
  156. assert(MP4_BR_MODE_VBR_5 == (MPEG4ENC_BITRATE_MODE)5);
  157. assert(MP4_BR_MODE_VBR_6 == (MPEG4ENC_BITRATE_MODE)6);
  158. return (MPEG4ENC_BITRATE_MODE)cfg->preset;
  159. }
  160. else /* CBR */
  161. {
  162. return MP4_BR_MODE_CBR;
  163. }
  164. }
  165. MPEG4ENC_CH_MODE AACConfig_GetChannelMode(const AACConfiguration *cfg, unsigned int channels)
  166. {
  167. switch(channels)
  168. {
  169. case 1:
  170. return MP4_CH_MODE_MONO;
  171. case 2:
  172. if (cfg->mode == AAC_MODE_VBR)
  173. {
  174. if (cfg->preset == 1)
  175. return MP4_CH_MODE_PARAMETRIC_STEREO;
  176. else
  177. return MP4_CH_MODE_STEREO;
  178. }
  179. else /* CBR */
  180. {
  181. if (AACConfig_GetAOT(cfg) == AUD_OBJ_TYP_PS)
  182. return MP4_CH_MODE_PARAMETRIC_STEREO;
  183. else
  184. return MP4_CH_MODE_STEREO;
  185. }
  186. case 3: return MP4_CH_MODE_3;
  187. case 4: return MP4_CH_MODE_4;
  188. case 5: return MP4_CH_MODE_5;
  189. case 6: return MP4_CH_MODE_5_1;
  190. case 8: return MP4_CH_MODE_7_1;
  191. default:
  192. return MP4_CH_MODE_INVALID;
  193. }
  194. }
  195. void AACConfig_GetToolString(const MPEG4ENC_SETUP *setup, char tool[], size_t cch)
  196. {
  197. char version[128] = {0};
  198. MPEG4ENC_GetVersionInfo(version, sizeof(version)/sizeof(*version));
  199. char *p = version;
  200. while (p && *p)
  201. {
  202. if (*p != '.' && (*p < '0' || *p > '9'))
  203. {
  204. *p = 0;
  205. break;
  206. }
  207. p++;
  208. }
  209. switch(setup->bitrateMode)
  210. {
  211. case MP4_BR_MODE_CBR:
  212. StringCchPrintfA(tool, cch, "fhgaac v%s;CBR=%d", version, setup->nBitRate);
  213. break;
  214. case MP4_BR_MODE_VBR_1:
  215. StringCchPrintfA(tool, cch, "fhgaac v%s;VBR=1", version);
  216. break;
  217. case MP4_BR_MODE_VBR_2:
  218. StringCchPrintfA(tool, cch, "fhgaac v%s;VBR=2", version);
  219. break;
  220. case MP4_BR_MODE_VBR_3:
  221. StringCchPrintfA(tool, cch, "fhgaac v%s;VBR=3", version);
  222. break;
  223. case MP4_BR_MODE_VBR_4:
  224. StringCchPrintfA(tool, cch, "fhgaac v%s;VBR=4", version);
  225. break;
  226. case MP4_BR_MODE_VBR_5:
  227. StringCchPrintfA(tool, cch, "fhgaac v%s;VBR=5", version);
  228. break;
  229. case MP4_BR_MODE_VBR_6:
  230. StringCchPrintfA(tool, cch, "fhgaac v%s;VBR=6", version);
  231. break;
  232. }
  233. }