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.

148 lines
4.2KB

  1. /*
  2. * Copyright (c) 2012 Laurent Aimar
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "libavutil/intreadwrite.h"
  21. #include "avcodec.h"
  22. #include "internal.h"
  23. typedef struct DVAudioContext {
  24. int block_size;
  25. int is_12bit;
  26. int is_pal;
  27. int16_t shuffle[2000];
  28. } DVAudioContext;
  29. static av_cold int decode_init(AVCodecContext *avctx)
  30. {
  31. DVAudioContext *s = avctx->priv_data;
  32. int i;
  33. if (avctx->channels != 2) {
  34. av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
  35. return AVERROR(EINVAL);
  36. }
  37. if (avctx->codec_tag == 0x0215) {
  38. s->block_size = 7200;
  39. } else if (avctx->codec_tag == 0x0216) {
  40. s->block_size = 8640;
  41. } else if (avctx->block_align == 7200 ||
  42. avctx->block_align == 8640) {
  43. s->block_size = avctx->block_align;
  44. } else {
  45. return AVERROR(EINVAL);
  46. }
  47. s->is_pal = s->block_size == 8640;
  48. s->is_12bit = avctx->bits_per_raw_sample == 12;
  49. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  50. avctx->channel_layout = AV_CH_LAYOUT_STEREO;
  51. for (i = 0; i < FF_ARRAY_ELEMS(s->shuffle); i++) {
  52. const unsigned a = s->is_pal ? 18 : 15;
  53. const unsigned b = 3 * a;
  54. s->shuffle[i] = 80 * ((21 * (i % 3) + 9 * (i / 3) + ((i / a) % 3)) % b) +
  55. (2 + s->is_12bit) * (i / b) + 8;
  56. }
  57. return 0;
  58. }
  59. static inline int dv_get_audio_sample_count(const uint8_t *buffer, int dsf)
  60. {
  61. int samples = buffer[0] & 0x3f; /* samples in this frame - min samples */
  62. switch ((buffer[3] >> 3) & 0x07) {
  63. case 0:
  64. return samples + (dsf ? 1896 : 1580);
  65. case 1:
  66. return samples + (dsf ? 1742 : 1452);
  67. case 2:
  68. default:
  69. return samples + (dsf ? 1264 : 1053);
  70. }
  71. }
  72. static inline uint16_t dv_audio_12to16(uint16_t sample)
  73. {
  74. uint16_t shift, result;
  75. sample = (sample < 0x800) ? sample : sample | 0xf000;
  76. shift = (sample & 0xf00) >> 8;
  77. if (shift < 0x2 || shift > 0xd) {
  78. result = sample;
  79. } else if (shift < 0x8) {
  80. shift--;
  81. result = (sample - (256 * shift)) << shift;
  82. } else {
  83. shift = 0xe - shift;
  84. result = ((sample + ((256 * shift) + 1)) << shift) - 1;
  85. }
  86. return result;
  87. }
  88. static int decode_frame(AVCodecContext *avctx, void *data,
  89. int *got_frame_ptr, AVPacket *pkt)
  90. {
  91. DVAudioContext *s = avctx->priv_data;
  92. AVFrame *frame = data;
  93. const uint8_t *src = pkt->data;
  94. int16_t *dst;
  95. int ret, i;
  96. if (pkt->size < s->block_size)
  97. return AVERROR_INVALIDDATA;
  98. frame->nb_samples = dv_get_audio_sample_count(pkt->data + 244, s->is_pal);
  99. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  100. return ret;
  101. dst = (int16_t *)frame->data[0];
  102. for (i = 0; i < frame->nb_samples; i++) {
  103. const uint8_t *v = &src[s->shuffle[i]];
  104. if (s->is_12bit) {
  105. *dst++ = dv_audio_12to16((v[0] << 4) | ((v[2] >> 4) & 0x0f));
  106. *dst++ = dv_audio_12to16((v[1] << 4) | ((v[2] >> 0) & 0x0f));
  107. } else {
  108. *dst++ = AV_RB16(&v[0]);
  109. *dst++ = AV_RB16(&v[s->is_pal ? 4320 : 3600]);
  110. }
  111. }
  112. *got_frame_ptr = 1;
  113. return s->block_size;
  114. }
  115. AVCodec ff_dvaudio_decoder = {
  116. .name = "dvaudio",
  117. .long_name = NULL_IF_CONFIG_SMALL("Ulead DV Audio"),
  118. .type = AVMEDIA_TYPE_AUDIO,
  119. .id = AV_CODEC_ID_DVAUDIO,
  120. .init = decode_init,
  121. .decode = decode_frame,
  122. .capabilities = AV_CODEC_CAP_DR1,
  123. .priv_data_size = sizeof(DVAudioContext),
  124. };