flv_adpcm_decoder.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #include "flv_adpcm_decoder.h"
  2. #include "../f263/BitReader.h"
  3. int FLVDecoderCreator::CreateAudioDecoder(int stereo, int bits, int sample_rate, int format_type, ifc_flvaudiodecoder **decoder)
  4. {
  5. if (format_type == FLV::AUDIO_FORMAT_ADPCM)
  6. {
  7. *decoder = new FLVADPCM(stereo?2:1);
  8. return CREATEDECODER_SUCCESS;
  9. }
  10. return CREATEDECODER_NOT_MINE;
  11. }
  12. int FLVDecoderCreator::HandlesAudio(int format_type)
  13. {
  14. if (format_type == FLV::AUDIO_FORMAT_ADPCM)
  15. {
  16. return CREATEDECODER_SUCCESS;
  17. }
  18. return CREATEDECODER_NOT_MINE;
  19. }
  20. #define CBCLASS FLVDecoderCreator
  21. START_DISPATCH;
  22. CB(CREATE_AUDIO_DECODER, CreateAudioDecoder)
  23. CB(HANDLES_AUDIO, HandlesAudio)
  24. END_DISPATCH;
  25. #undef CBCLASS
  26. /* --- */
  27. FLVADPCM::FLVADPCM(unsigned int channels) : channels(channels)
  28. {
  29. }
  30. int FLVADPCM::GetOutputFormat(unsigned int *sample_rate, unsigned int *channels, unsigned int *bits)
  31. {
  32. *channels = this->channels;
  33. *bits = 16;
  34. return FLV_AUDIO_SUCCESS;
  35. }
  36. // padded to zero where table size is less then 16
  37. static const int swf_index_tables[4][16] = {
  38. /*2*/ { -1, 2 },
  39. /*3*/ { -1, -1, 2, 4 },
  40. /*4*/ { -1, -1, -1, -1, 2, 4, 6, 8 },
  41. /*5*/ { -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 4, 6, 8, 10, 13, 16 }
  42. };
  43. static const int step_table[89] = {
  44. 7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
  45. 19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
  46. 50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
  47. 130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
  48. 337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
  49. 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
  50. 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
  51. 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
  52. 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
  53. };
  54. int FLVADPCM::DecodeSample(const void *input_buffer, size_t input_buffer_bytes, void *_samples, size_t *samples_size_bytes, double *bitrate)
  55. {
  56. int predictors[2] = {0, 0};
  57. int16_t step_indices[2] = {0, 0};
  58. int16_t *samples = (int16_t *)_samples;
  59. const size_t max_samples = *samples_size_bytes / sizeof(int16_t);
  60. BitReader bit_reader;
  61. bit_reader.data = (uint8_t *)input_buffer;
  62. bit_reader.numBits = (uint32_t)input_buffer_bytes * 8;
  63. uint32_t bits = bit_reader.getbits(2);
  64. const int *table = swf_index_tables[bits];
  65. int k0 = 1 << bits++;
  66. int signmask = 1 << bits++;
  67. size_t sample_num=0;
  68. while (bit_reader.size() >= 22*channels)
  69. {
  70. for (size_t i = 0; i != channels; i++)
  71. {
  72. samples[sample_num++] = predictors[i] = ((int32_t)bit_reader.getbits(16) << 16) >> 16;
  73. step_indices[i] = bit_reader.getbits(6);
  74. }
  75. while (bit_reader.size() > bits*channels && sample_num < max_samples)
  76. {
  77. for (size_t i = 0; i != channels; i++)
  78. {
  79. int delta = bit_reader.getbits(bits);
  80. int step = step_table[step_indices[i]];
  81. long vpdiff = 0;
  82. int k = k0;
  83. do {
  84. if (delta & k)
  85. vpdiff += step;
  86. step >>= 1;
  87. k >>= 1;
  88. } while(k);
  89. vpdiff += step;
  90. if (delta & signmask)
  91. predictors[i] -= vpdiff;
  92. else
  93. predictors[i] += vpdiff;
  94. step_indices[i] += table[delta & (~signmask)];
  95. if (step_indices[i] < 0) step_indices[i] = 0;
  96. if (step_indices[i] > 88) step_indices[i] = 88;
  97. if (predictors[i] > 32767) predictors[i] = 32767;
  98. if (predictors[i] < -32768) predictors[i] = -32768;
  99. samples[sample_num++] = predictors[i];
  100. }
  101. }
  102. }
  103. *samples_size_bytes = sample_num * sizeof(int16_t);
  104. return FLV_AUDIO_SUCCESS;
  105. }
  106. void FLVADPCM::Close()
  107. {
  108. delete this;
  109. }
  110. #define CBCLASS FLVADPCM
  111. START_DISPATCH;
  112. CB(FLV_AUDIO_GETOUTPUTFORMAT, GetOutputFormat)
  113. CB(FLV_AUDIO_DECODE, DecodeSample)
  114. VCB(FLV_AUDIO_CLOSE, Close)
  115. END_DISPATCH;
  116. #undef CBCLASS