1
0

FhGAACEncoder.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #include "FhGAACEncoder.h"
  2. #include "mp4FastAAClib.h"
  3. #include <malloc.h>
  4. #include "config.h"
  5. #include "../nsutil/pcm.h"
  6. FhGAACEncoder *FhGAACEncoder::CreateDecoder(const AACConfiguration *cfg, int nch, int srate, int bps)
  7. {
  8. MPEG4ENC_ERROR err;
  9. MPEG4ENC_SETUP setup;
  10. setup.aot = AACConfig_GetAOT(cfg);
  11. setup.nBitRate = AACConfig_GetBitrate(cfg, nch);
  12. setup.bitrateMode = AACConfig_GetBitrateMode(cfg);
  13. setup.quality = MP4_QUAL_HIGH;
  14. setup.chMode = AACConfig_GetChannelMode(cfg, nch);
  15. setup.sbrSignaling = MP4_SBRSIG_EXPL_BC;
  16. setup.nSampleRateIn = srate;
  17. setup.transportFormat = MP4_TT_RAW;
  18. setup.nGranuleLength = MP4_GRANULE_1024;
  19. setup.metadataMode = MP4_METADATA_NONE;
  20. HANDLE_MPEG4ENC_ENCODER encoder=0;
  21. err = MPEG4ENC_Configure(&encoder, &setup);
  22. if (err != MPEG4ENC_NO_ERROR)
  23. {
  24. return 0;
  25. }
  26. unsigned int first_samples;
  27. err = MPEG4ENC_Open(&encoder, &first_samples);
  28. if (err != MPEG4ENC_NO_ERROR)
  29. {
  30. MPEG4ENC_Close(&encoder);
  31. return 0;
  32. }
  33. float *sample_buffer = (float *)malloc(first_samples * sizeof(float));
  34. if (!sample_buffer)
  35. {
  36. MPEG4ENC_Close(&encoder);
  37. return 0;
  38. }
  39. FhGAACEncoder *fhg_enc = new FhGAACEncoder(encoder, &setup, nch, srate, bps, sample_buffer, first_samples);
  40. if (!fhg_enc)
  41. {
  42. free(sample_buffer);
  43. MPEG4ENC_Close(&encoder);
  44. }
  45. AACConfig_GetToolString(&setup, fhg_enc->tool, sizeof(fhg_enc->tool));
  46. return fhg_enc;
  47. }
  48. FhGAACEncoder::FhGAACEncoder(HANDLE_MPEG4ENC_ENCODER encoder, const MPEG4ENC_SETUP *setup, int nch, int srate, int bps, float *sample_buffer, unsigned int next_samples)
  49. : encoder(encoder), channels(nch), sample_rate(srate), bits_per_sample(bps), sample_buffer(sample_buffer), next_samples(next_samples)
  50. {
  51. MPEG4ENC_INFO info;
  52. MPEG4ENC_GetInfo(encoder, &info);
  53. samples_per_frame = info.nSamplesFrame[0];
  54. if (info.nSamplingRate[0] != srate)
  55. resampling = true;
  56. else
  57. resampling = false;
  58. finishing=false;
  59. total_samples=0;
  60. decodable_samples=0;
  61. // TODO: move this somewhere where we can error-check
  62. mp4_writer.AddAudioTrack(encoder, setup);
  63. }
  64. FhGAACEncoder::~FhGAACEncoder()
  65. {
  66. free(sample_buffer);
  67. MPEG4ENC_Close(&encoder);
  68. }
  69. int FhGAACEncoder::Encode(int framepos, void *in, int in_avail, int *in_used, void *out, int out_avail)
  70. {
  71. if (!in_avail && !finishing)
  72. return 0;
  73. size_t num_samples = in_avail / (bits_per_sample/8);
  74. num_samples = min(num_samples, next_samples);
  75. nsutil_pcm_IntToFloat_Interleaved(sample_buffer, in, bits_per_sample, num_samples);
  76. int samples_consumed=0;
  77. int out_used = 0;
  78. MPEG4ENC_AUINFO *info;
  79. MPEG4ENC_ERROR err = MPEG4ENC_Encode(encoder, sample_buffer, num_samples, &samples_consumed, &next_samples, (unsigned char * const)out, out_avail, &out_used, &info);
  80. if (err == MPEG4ENC_NO_ERROR && out_used)
  81. {
  82. decodable_samples += samples_per_frame;
  83. mp4_writer.Write(out, out_used, samples_per_frame);
  84. }
  85. else if (err != MPEG4ENC_NO_ERROR)
  86. {
  87. return 0;
  88. }
  89. if (!finishing)
  90. {
  91. *in_used = samples_consumed * (bits_per_sample/8);
  92. total_samples += samples_consumed / channels;
  93. }
  94. return out_used;
  95. }
  96. void FhGAACEncoder::PrepareToFinish()
  97. {
  98. finishing=true;
  99. }
  100. void FhGAACEncoder::Finish(const wchar_t *filename)
  101. {
  102. MPEG4ENC_INFO info;
  103. MPEG4ENC_GetInfo(encoder, &info);
  104. if (!resampling)
  105. mp4_writer.WriteGaps(info.nDelay, decodable_samples-total_samples-info.nDelay, total_samples);
  106. mp4_writer.WriteTool(tool);
  107. mp4_writer.CloseTo(filename);
  108. }