pcm_encoder.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright (c) 2020, Actions Semi Co., Inc.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <errno.h>
  7. #include <string.h>
  8. #include <as_audio_codec.h>
  9. #include <mem_manager.h>
  10. #define PCM_FRAME_SIZE (128)
  11. struct pcm_encoder {
  12. u16_t frame_bytes;
  13. u8_t channels;
  14. };
  15. static int _as_encoder_ops_pcm_close(void *hnd)
  16. {
  17. if (hnd)
  18. mem_free(hnd);
  19. return AE_RET_OK;
  20. }
  21. static int _as_encoder_ops_pcm_open(void **hnd, as_enc_t *as_enc)
  22. {
  23. struct pcm_encoder *encoder = NULL;
  24. if (as_enc->channels != 1 && as_enc->channels != 2)
  25. return AE_RET_UNEXPECTED;
  26. encoder = mem_malloc(sizeof(*encoder));
  27. if (!encoder)
  28. return AE_RET_OUTOFMEMORY;
  29. as_enc->block_size = PCM_FRAME_SIZE * sizeof(u16_t);
  30. as_enc->block_enc_size_max = as_enc->block_size * as_enc->channels;
  31. encoder->channels = as_enc->channels;
  32. encoder->frame_bytes = as_enc->block_enc_size_max;
  33. *hnd = encoder;
  34. return AE_RET_OK;
  35. }
  36. static int _as_encoder_ops_pcm_encode(void *hnd, as_encode_info_t *encode_info)
  37. {
  38. struct pcm_encoder *encoder = hnd;
  39. u16_t *out16 = encode_info->aout;
  40. u16_t *inl16 = encode_info->ain->pcm[0];
  41. u16_t *inr16 = encode_info->ain->pcm[1];
  42. int i;
  43. encode_info->bytes_used = encoder->frame_bytes;
  44. if (encoder->channels == 1) {
  45. memcpy(out16, inl16, encoder->frame_bytes);
  46. return AE_RET_OK;
  47. }
  48. for (i = 0; i < PCM_FRAME_SIZE; i++) {
  49. *out16++ = *inl16++;
  50. *out16++ = *inr16++;
  51. }
  52. return AE_RET_OK;
  53. }
  54. static int _as_encoder_ops_pcm_get_fhdr(void *hnd, as_enc_fhdr_t *enc_fhdr)
  55. {
  56. enc_fhdr->hdr_buf = NULL;
  57. enc_fhdr->hdr_len = 0;
  58. return AE_RET_OK;
  59. }
  60. static int _as_encoder_ops_pcm_mem_require(void *hnd, as_mem_info_t *mem_info)
  61. {
  62. mem_info->global_buf_len[0] = 0;
  63. mem_info->share_buf_len[0] = 0;
  64. return AE_RET_OK;
  65. }
  66. int as_encoder_ops_pcm(void *hnd, asenc_ex_ops_cmd_t cmd, unsigned int args)
  67. {
  68. switch (cmd) {
  69. case AE_CMD_OPEN:
  70. return _as_encoder_ops_pcm_open((void **)hnd, (as_enc_t *)args);
  71. case AE_CMD_CLOSE:
  72. return _as_encoder_ops_pcm_close(hnd);
  73. case AE_CMD_FRAME_ENCODE:
  74. return _as_encoder_ops_pcm_encode(hnd, (as_encode_info_t *)args);
  75. case AE_CMD_GET_FHDR:
  76. return _as_encoder_ops_pcm_get_fhdr(hnd, (as_enc_fhdr_t *)args);
  77. case AE_CMD_MEM_REQUIRE:
  78. return _as_encoder_ops_pcm_mem_require(hnd, (as_mem_info_t *)args);
  79. default:
  80. return AE_RET_OK;
  81. }
  82. }