You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
2.5KB

  1. /*
  2. * Packed Animation File audio decoder
  3. * Copyright (c) 2012 Paul B Mahol
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/intreadwrite.h"
  22. #include "avcodec.h"
  23. #include "internal.h"
  24. #include "mathops.h"
  25. #include "paf.h"
  26. static av_cold int paf_audio_init(AVCodecContext *avctx)
  27. {
  28. if (avctx->channels != 2) {
  29. av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
  30. return AVERROR_INVALIDDATA;
  31. }
  32. avctx->channel_layout = AV_CH_LAYOUT_STEREO;
  33. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  34. return 0;
  35. }
  36. static int paf_audio_decode(AVCodecContext *avctx, void *data,
  37. int *got_frame, AVPacket *pkt)
  38. {
  39. AVFrame *frame = data;
  40. int16_t *output_samples;
  41. const uint8_t *src = pkt->data;
  42. int frames, ret, i, j;
  43. int16_t cb[256];
  44. frames = pkt->size / PAF_SOUND_FRAME_SIZE;
  45. if (frames < 1)
  46. return AVERROR_INVALIDDATA;
  47. frame->nb_samples = PAF_SOUND_SAMPLES * frames;
  48. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  49. return ret;
  50. output_samples = (int16_t *)frame->data[0];
  51. // codebook of 256 16-bit samples and 8-bit indices to it
  52. for (j = 0; j < frames; j++) {
  53. for (i = 0; i < 256; i++)
  54. cb[i] = sign_extend(AV_RL16(src + i * 2), 16);
  55. src += 256 * 2;
  56. // always 2 channels
  57. for (i = 0; i < PAF_SOUND_SAMPLES * 2; i++)
  58. *output_samples++ = cb[*src++];
  59. }
  60. *got_frame = 1;
  61. return pkt->size;
  62. }
  63. AVCodec ff_paf_audio_decoder = {
  64. .name = "paf_audio",
  65. .long_name = NULL_IF_CONFIG_SMALL("Amazing Studio Packed Animation File Audio"),
  66. .type = AVMEDIA_TYPE_AUDIO,
  67. .id = AV_CODEC_ID_PAF_AUDIO,
  68. .init = paf_audio_init,
  69. .decode = paf_audio_decode,
  70. .capabilities = AV_CODEC_CAP_DR1,
  71. };