xa.c 3.8 KB

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