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.

174 lines
5.9KB

  1. /*
  2. * Westwood Studios AUD Format Demuxer
  3. * Copyright (c) 2003 The ffmpeg Project
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * Westwood Studios AUD file demuxer
  24. * by Mike Melanson (melanson@pcisys.net)
  25. * for more information on the Westwood file formats, visit:
  26. * http://www.pcisys.net/~melanson/codecs/
  27. * http://www.geocities.com/SiliconValley/8682/aud3.txt
  28. *
  29. * Implementation note: There is no definite file signature for AUD files.
  30. * The demuxer uses a probabilistic strategy for content detection. This
  31. * entails performing sanity checks on certain header values in order to
  32. * qualify a file. Refer to wsaud_probe() for the precise parameters.
  33. */
  34. #include "libavutil/intreadwrite.h"
  35. #include "avformat.h"
  36. #include "internal.h"
  37. #define AUD_HEADER_SIZE 12
  38. #define AUD_CHUNK_PREAMBLE_SIZE 8
  39. #define AUD_CHUNK_SIGNATURE 0x0000DEAF
  40. typedef struct WsAudDemuxContext {
  41. int audio_samplerate;
  42. int audio_channels;
  43. int audio_bits;
  44. enum CodecID audio_type;
  45. int audio_stream_index;
  46. int64_t audio_frame_counter;
  47. } WsAudDemuxContext;
  48. static int wsaud_probe(AVProbeData *p)
  49. {
  50. int field;
  51. /* Probabilistic content detection strategy: There is no file signature
  52. * so perform sanity checks on various header parameters:
  53. * 8000 <= sample rate (16 bits) <= 48000 ==> 40001 acceptable numbers
  54. * flags <= 0x03 (2 LSBs are used) ==> 4 acceptable numbers
  55. * compression type (8 bits) = 1 or 99 ==> 2 acceptable numbers
  56. * first audio chunk signature (32 bits) ==> 1 acceptable number
  57. * The number space contains 2^64 numbers. There are 40001 * 4 * 2 * 1 =
  58. * 320008 acceptable number combinations.
  59. */
  60. if (p->buf_size < AUD_HEADER_SIZE + AUD_CHUNK_PREAMBLE_SIZE)
  61. return 0;
  62. /* check sample rate */
  63. field = AV_RL16(&p->buf[0]);
  64. if ((field < 8000) || (field > 48000))
  65. return 0;
  66. /* enforce the rule that the top 6 bits of this flags field are reserved (0);
  67. * this might not be true, but enforce it until deemed unnecessary */
  68. if (p->buf[10] & 0xFC)
  69. return 0;
  70. /* note: only check for WS IMA (type 99) right now since there is no
  71. * support for type 1 */
  72. if (p->buf[11] != 99)
  73. return 0;
  74. /* read ahead to the first audio chunk and validate the first header signature */
  75. if (AV_RL32(&p->buf[16]) != AUD_CHUNK_SIGNATURE)
  76. return 0;
  77. /* return 1/2 certainty since this file check is a little sketchy */
  78. return AVPROBE_SCORE_MAX / 2;
  79. }
  80. static int wsaud_read_header(AVFormatContext *s,
  81. AVFormatParameters *ap)
  82. {
  83. WsAudDemuxContext *wsaud = s->priv_data;
  84. AVIOContext *pb = s->pb;
  85. AVStream *st;
  86. unsigned char header[AUD_HEADER_SIZE];
  87. if (avio_read(pb, header, AUD_HEADER_SIZE) != AUD_HEADER_SIZE)
  88. return AVERROR(EIO);
  89. wsaud->audio_samplerate = AV_RL16(&header[0]);
  90. if (header[11] == 99)
  91. wsaud->audio_type = CODEC_ID_ADPCM_IMA_WS;
  92. else
  93. return AVERROR_INVALIDDATA;
  94. /* flag 0 indicates stereo */
  95. wsaud->audio_channels = (header[10] & 0x1) + 1;
  96. /* flag 1 indicates 16 bit audio */
  97. wsaud->audio_bits = (((header[10] & 0x2) >> 1) + 1) * 8;
  98. /* initialize the audio decoder stream */
  99. st = avformat_new_stream(s, NULL);
  100. if (!st)
  101. return AVERROR(ENOMEM);
  102. avpriv_set_pts_info(st, 33, 1, wsaud->audio_samplerate);
  103. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  104. st->codec->codec_id = wsaud->audio_type;
  105. st->codec->codec_tag = 0; /* no tag */
  106. st->codec->channels = wsaud->audio_channels;
  107. st->codec->sample_rate = wsaud->audio_samplerate;
  108. st->codec->bits_per_coded_sample = wsaud->audio_bits;
  109. st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
  110. st->codec->bits_per_coded_sample / 4;
  111. st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample;
  112. wsaud->audio_stream_index = st->index;
  113. wsaud->audio_frame_counter = 0;
  114. return 0;
  115. }
  116. static int wsaud_read_packet(AVFormatContext *s,
  117. AVPacket *pkt)
  118. {
  119. WsAudDemuxContext *wsaud = s->priv_data;
  120. AVIOContext *pb = s->pb;
  121. unsigned char preamble[AUD_CHUNK_PREAMBLE_SIZE];
  122. unsigned int chunk_size;
  123. int ret = 0;
  124. if (avio_read(pb, preamble, AUD_CHUNK_PREAMBLE_SIZE) !=
  125. AUD_CHUNK_PREAMBLE_SIZE)
  126. return AVERROR(EIO);
  127. /* validate the chunk */
  128. if (AV_RL32(&preamble[4]) != AUD_CHUNK_SIGNATURE)
  129. return AVERROR_INVALIDDATA;
  130. chunk_size = AV_RL16(&preamble[0]);
  131. ret= av_get_packet(pb, pkt, chunk_size);
  132. if (ret != chunk_size)
  133. return AVERROR(EIO);
  134. pkt->stream_index = wsaud->audio_stream_index;
  135. pkt->pts = wsaud->audio_frame_counter;
  136. pkt->pts /= wsaud->audio_samplerate;
  137. /* 2 samples/byte, 1 or 2 samples per frame depending on stereo */
  138. wsaud->audio_frame_counter += (chunk_size * 2) / wsaud->audio_channels;
  139. return ret;
  140. }
  141. AVInputFormat ff_wsaud_demuxer = {
  142. .name = "wsaud",
  143. .long_name = NULL_IF_CONFIG_SMALL("Westwood Studios audio format"),
  144. .priv_data_size = sizeof(WsAudDemuxContext),
  145. .read_probe = wsaud_probe,
  146. .read_header = wsaud_read_header,
  147. .read_packet = wsaud_read_packet,
  148. };