1
0

avi_pcm_decoder.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #include "avi_pcm_decoder.h"
  2. #include "avi_alaw_decoder.h"
  3. #include "avi_ulaw_decoder.h"
  4. int AVIDecoder::CreateAudioDecoder(const nsavi::AVIH *avi_header,
  5. const nsavi::STRH *stream_header, const nsavi::STRF *stream_format, const nsavi::STRD *stream_data,
  6. unsigned int preferred_bits, unsigned int max_channels, bool floating_point,
  7. ifc_aviaudiodecoder **decoder)
  8. {
  9. nsavi::audio_format *waveformat = (nsavi::audio_format *)stream_format;
  10. if (waveformat->format == nsavi::audio_format_pcm || waveformat->format == nsavi::audio_format_extensible)
  11. {
  12. *decoder = new AVIPCMDecoder(waveformat);
  13. return CREATEDECODER_SUCCESS;
  14. }
  15. else if (waveformat->format == nsavi::audio_format_alaw)
  16. {
  17. *decoder = new AVIALawDecoder(waveformat);
  18. return CREATEDECODER_SUCCESS;
  19. }
  20. else if (waveformat->format == nsavi::audio_format_ulaw)
  21. {
  22. *decoder = new AVIULawDecoder(waveformat);
  23. return CREATEDECODER_SUCCESS;
  24. }
  25. return CREATEDECODER_NOT_MINE;
  26. }
  27. #define CBCLASS AVIDecoder
  28. START_DISPATCH;
  29. CB(CREATE_AUDIO_DECODER, CreateAudioDecoder)
  30. END_DISPATCH;
  31. #undef CBCLASS
  32. AVIPCMDecoder::AVIPCMDecoder(const nsavi::audio_format *waveformat) : waveformat(waveformat)
  33. {
  34. }
  35. int AVIPCMDecoder::OutputFrameSize(size_t *frame_size)
  36. {
  37. *frame_size = waveformat->block_align; // TODO
  38. return AVI_SUCCESS;
  39. }
  40. int AVIPCMDecoder::GetOutputProperties(unsigned int *sampleRate, unsigned int *channels, unsigned int *bitsPerSample, bool *isFloat)
  41. {
  42. if (waveformat)
  43. {
  44. *sampleRate = waveformat->sample_rate;
  45. *channels = waveformat->channels;
  46. *bitsPerSample = waveformat->bits_per_sample;
  47. *isFloat = false; // TODO
  48. return AVI_SUCCESS;
  49. }
  50. else
  51. {
  52. return AVI_FAILURE;
  53. }
  54. }
  55. int AVIPCMDecoder::DecodeChunk(uint16_t type, void **inputBuffer, size_t *inputBufferBytes, void *outputBuffer, size_t *outputBufferBytes)
  56. {
  57. size_t copy_size = min(*inputBufferBytes, *outputBufferBytes);
  58. int remainder = copy_size % waveformat->block_align;
  59. copy_size -= remainder;
  60. memcpy(outputBuffer, *inputBuffer, copy_size);
  61. *inputBuffer = (uint8_t *)(*inputBuffer) + copy_size;
  62. *inputBufferBytes = *inputBufferBytes - copy_size;
  63. *outputBufferBytes = copy_size;
  64. return AVI_SUCCESS;
  65. }
  66. void AVIPCMDecoder::Close()
  67. {
  68. delete this;
  69. }
  70. #define CBCLASS AVIPCMDecoder
  71. START_DISPATCH;
  72. CB(OUTPUT_FRAME_SIZE, OutputFrameSize)
  73. CB(GET_OUTPUT_PROPERTIES, GetOutputProperties)
  74. CB(DECODE_CHUNK, DecodeChunk)
  75. VCB(CLOSE, Close)
  76. END_DISPATCH;
  77. #undef CBCLASS