eacdata.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Electronic Arts .cdata file Demuxer
  3. * Copyright (c) 2007 Peter Ross
  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/eacdata.c
  23. * Electronic Arts cdata Format Demuxer
  24. * by Peter Ross (suxen_drol at hotmail dot com)
  25. *
  26. * Technical details here:
  27. * http://wiki.multimedia.cx/index.php?title=EA_Command_And_Conquer_3_Audio_Codec
  28. */
  29. #include "avformat.h"
  30. typedef struct {
  31. unsigned int channels;
  32. unsigned int audio_pts;
  33. } CdataDemuxContext;
  34. static int cdata_probe(AVProbeData *p)
  35. {
  36. const uint8_t *b = p->buf;
  37. if (b[0] == 0x04 && (b[1] == 0x00 || b[1] == 0x04 || b[1] == 0x0C))
  38. return AVPROBE_SCORE_MAX/8;
  39. return 0;
  40. }
  41. static int cdata_read_header(AVFormatContext *s, AVFormatParameters *ap)
  42. {
  43. CdataDemuxContext *cdata = s->priv_data;
  44. ByteIOContext *pb = s->pb;
  45. unsigned int sample_rate, header;
  46. AVStream *st;
  47. header = get_be16(pb);
  48. switch (header) {
  49. case 0x0400: cdata->channels = 1; break;
  50. case 0x0404: cdata->channels = 2; break;
  51. case 0x040C: cdata->channels = 4; break;
  52. default:
  53. av_log(s, AV_LOG_INFO, "unknown header 0x%04x\n", header);
  54. return -1;
  55. };
  56. sample_rate = get_be16(pb);
  57. url_fskip(pb, 12);
  58. st = av_new_stream(s, 0);
  59. if (!st)
  60. return AVERROR(ENOMEM);
  61. st->codec->codec_type = CODEC_TYPE_AUDIO;
  62. st->codec->codec_tag = 0; /* no fourcc */
  63. st->codec->codec_id = CODEC_ID_ADPCM_EA_XAS;
  64. st->codec->channels = cdata->channels;
  65. st->codec->sample_rate = sample_rate;
  66. av_set_pts_info(st, 64, 1, sample_rate);
  67. cdata->audio_pts = 0;
  68. return 0;
  69. }
  70. static int cdata_read_packet(AVFormatContext *s, AVPacket *pkt)
  71. {
  72. CdataDemuxContext *cdata = s->priv_data;
  73. int packet_size = 76*cdata->channels;
  74. if (av_get_packet(s->pb, pkt, packet_size) != packet_size)
  75. return AVERROR(EIO);
  76. pkt->pts = cdata->audio_pts++;
  77. return 1;
  78. }
  79. AVInputFormat ea_cdata_demuxer = {
  80. "ea_cdata",
  81. NULL_IF_CONFIG_SMALL("Electronic Arts cdata"),
  82. sizeof(CdataDemuxContext),
  83. cdata_probe,
  84. cdata_read_header,
  85. cdata_read_packet,
  86. .extensions = "cdata",
  87. };