xa.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Maxis XA (.xa) File Demuxer
  3. * Copyright (c) 2008 Robert Marston
  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. /**
  22. * @file
  23. * Maxis XA File Demuxer
  24. * by Robert Marston (rmarston@gmail.com)
  25. * for more information on the XA audio format see
  26. * http://wiki.multimedia.cx/index.php?title=Maxis_XA
  27. */
  28. #include "libavutil/intreadwrite.h"
  29. #include "avformat.h"
  30. #include "internal.h"
  31. #define XA00_TAG MKTAG('X', 'A', 0, 0)
  32. #define XAI0_TAG MKTAG('X', 'A', 'I', 0)
  33. #define XAJ0_TAG MKTAG('X', 'A', 'J', 0)
  34. typedef struct MaxisXADemuxContext {
  35. uint32_t out_size;
  36. uint32_t sent_bytes;
  37. uint32_t audio_frame_counter;
  38. } MaxisXADemuxContext;
  39. static int xa_probe(AVProbeData *p)
  40. {
  41. int channels, srate, bits_per_sample;
  42. if (p->buf_size < 24)
  43. return 0;
  44. switch(AV_RL32(p->buf)) {
  45. case XA00_TAG:
  46. case XAI0_TAG:
  47. case XAJ0_TAG:
  48. break;
  49. default:
  50. return 0;
  51. }
  52. channels = AV_RL16(p->buf + 10);
  53. srate = AV_RL32(p->buf + 12);
  54. bits_per_sample = AV_RL16(p->buf + 22);
  55. if (!channels || channels > 8 || !srate || srate > 192000 ||
  56. bits_per_sample < 4 || bits_per_sample > 32)
  57. return 0;
  58. return AVPROBE_SCORE_MAX/2;
  59. }
  60. static int xa_read_header(AVFormatContext *s,
  61. AVFormatParameters *ap)
  62. {
  63. MaxisXADemuxContext *xa = s->priv_data;
  64. AVIOContext *pb = s->pb;
  65. AVStream *st;
  66. /*Set up the XA Audio Decoder*/
  67. st = avformat_new_stream(s, NULL);
  68. if (!st)
  69. return AVERROR(ENOMEM);
  70. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  71. st->codec->codec_id = CODEC_ID_ADPCM_EA_MAXIS_XA;
  72. avio_skip(pb, 4); /* Skip the XA ID */
  73. xa->out_size = avio_rl32(pb);
  74. avio_skip(pb, 2); /* Skip the tag */
  75. st->codec->channels = avio_rl16(pb);
  76. st->codec->sample_rate = avio_rl32(pb);
  77. /* Value in file is average byte rate*/
  78. st->codec->bit_rate = avio_rl32(pb) * 8;
  79. st->codec->block_align = avio_rl16(pb);
  80. st->codec->bits_per_coded_sample = avio_rl16(pb);
  81. avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
  82. return 0;
  83. }
  84. static int xa_read_packet(AVFormatContext *s,
  85. AVPacket *pkt)
  86. {
  87. MaxisXADemuxContext *xa = s->priv_data;
  88. AVStream *st = s->streams[0];
  89. AVIOContext *pb = s->pb;
  90. unsigned int packet_size;
  91. int ret;
  92. if(xa->sent_bytes > xa->out_size)
  93. return AVERROR(EIO);
  94. /* 1 byte header and 14 bytes worth of samples * number channels per block */
  95. packet_size = 15*st->codec->channels;
  96. ret = av_get_packet(pb, pkt, packet_size);
  97. if(ret < 0)
  98. return ret;
  99. pkt->stream_index = st->index;
  100. xa->sent_bytes += packet_size;
  101. pkt->pts = xa->audio_frame_counter;
  102. /* 14 bytes Samples per channel with 2 samples per byte */
  103. xa->audio_frame_counter += 28 * st->codec->channels;
  104. return ret;
  105. }
  106. AVInputFormat ff_xa_demuxer = {
  107. .name = "xa",
  108. .long_name = NULL_IF_CONFIG_SMALL("Maxis XA File Format"),
  109. .priv_data_size = sizeof(MaxisXADemuxContext),
  110. .read_probe = xa_probe,
  111. .read_header = xa_read_header,
  112. .read_packet = xa_read_packet,
  113. };