1
0

ADTSAACEncoder.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include "ADTSAACEncoder.h"
  2. #include "mp4FastAAClib.h"
  3. #include <malloc.h>
  4. #include "config.h"
  5. #include "../nsutil/pcm.h"
  6. ADTSAACEncoder *ADTSAACEncoder::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_IMPLICIT;
  16. setup.nSampleRateIn = srate;
  17. setup.transportFormat = MP4_TT_ADTS;
  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. ADTSAACEncoder *fhg_enc = new ADTSAACEncoder(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. ADTSAACEncoder::ADTSAACEncoder(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. finishing=false;
  55. }
  56. ADTSAACEncoder::~ADTSAACEncoder()
  57. {
  58. free(sample_buffer);
  59. MPEG4ENC_Close(&encoder);
  60. }
  61. int ADTSAACEncoder::Encode(int framepos, void *in, int in_avail, int *in_used, void *out, int out_avail)
  62. {
  63. if (!in_avail && !finishing)
  64. return 0;
  65. size_t num_samples = in_avail / (bits_per_sample/8);
  66. num_samples = min(num_samples, next_samples);
  67. nsutil_pcm_IntToFloat_Interleaved(sample_buffer, in, bits_per_sample, num_samples);
  68. int samples_consumed=0;
  69. int out_used = 0;
  70. MPEG4ENC_AUINFO *info;
  71. MPEG4ENC_ERROR err = MPEG4ENC_Encode(encoder, sample_buffer, num_samples, &samples_consumed, &next_samples, (unsigned char * const)out, out_avail, &out_used, &info);
  72. if (err != MPEG4ENC_NO_ERROR)
  73. {
  74. out_used = 0; /* in case it didn't get set */
  75. }
  76. else if (err != MPEG4ENC_NO_ERROR)
  77. {
  78. return 0;
  79. }
  80. if (!finishing)
  81. {
  82. *in_used = samples_consumed * (bits_per_sample/8);
  83. }
  84. return out_used;
  85. }
  86. void ADTSAACEncoder::PrepareToFinish()
  87. {
  88. finishing=true;
  89. }
  90. void ADTSAACEncoder::Finish(const wchar_t *filename)
  91. {
  92. /* TODO:
  93. MPEG4ENC_INFO info;
  94. MPEG4ENC_GetInfo(encoder, &info);
  95. if (!resampling)
  96. mp4_writer.WriteGaps(info.nDelay, decodable_samples-total_samples-info.nDelay, total_samples);
  97. mp4_writer.WriteTool(tool);
  98. */
  99. }