xa.c 3.3 KB

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