xa.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 libavformat/xa.c
  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. switch(AV_RL32(p->buf)) {
  41. case XA00_TAG:
  42. case XAI0_TAG:
  43. case XAJ0_TAG:
  44. return AVPROBE_SCORE_MAX;
  45. }
  46. return 0;
  47. }
  48. static int xa_read_header(AVFormatContext *s,
  49. AVFormatParameters *ap)
  50. {
  51. MaxisXADemuxContext *xa = s->priv_data;
  52. ByteIOContext *pb = s->pb;
  53. AVStream *st;
  54. /*Set up the XA Audio Decoder*/
  55. st = av_new_stream(s, 0);
  56. if (!st)
  57. return AVERROR(ENOMEM);
  58. st->codec->codec_type = CODEC_TYPE_AUDIO;
  59. st->codec->codec_id = CODEC_ID_ADPCM_EA_MAXIS_XA;
  60. url_fskip(pb, 4); /* Skip the XA ID */
  61. xa->out_size = get_le32(pb);
  62. url_fskip(pb, 2); /* Skip the tag */
  63. st->codec->channels = get_le16(pb);
  64. st->codec->sample_rate = get_le32(pb);
  65. /* Value in file is average byte rate*/
  66. st->codec->bit_rate = get_le32(pb) * 8;
  67. st->codec->block_align = get_le16(pb);
  68. st->codec->bits_per_coded_sample = get_le16(pb);
  69. av_set_pts_info(st, 64, 1, st->codec->sample_rate);
  70. return 0;
  71. }
  72. static int xa_read_packet(AVFormatContext *s,
  73. AVPacket *pkt)
  74. {
  75. MaxisXADemuxContext *xa = s->priv_data;
  76. AVStream *st = s->streams[0];
  77. ByteIOContext *pb = s->pb;
  78. unsigned int packet_size;
  79. int ret;
  80. if(xa->sent_bytes > xa->out_size)
  81. return AVERROR(EIO);
  82. /* 1 byte header and 14 bytes worth of samples * number channels per block */
  83. packet_size = 15*st->codec->channels;
  84. ret = av_get_packet(pb, pkt, packet_size);
  85. if(ret != packet_size)
  86. return AVERROR(EIO);
  87. pkt->stream_index = st->index;
  88. xa->sent_bytes += packet_size;
  89. pkt->pts = xa->audio_frame_counter;
  90. /* 14 bytes Samples per channel with 2 samples per byte */
  91. xa->audio_frame_counter += 28 * st->codec->channels;
  92. return ret;
  93. }
  94. AVInputFormat xa_demuxer = {
  95. "xa",
  96. NULL_IF_CONFIG_SMALL("Maxis XA File Format"),
  97. sizeof(MaxisXADemuxContext),
  98. xa_probe,
  99. xa_read_header,
  100. xa_read_packet,
  101. };