You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
2.9KB

  1. /*
  2. * Electronic Arts .cdata file Demuxer
  3. * Copyright (c) 2007 Peter Ross
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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. * Electronic Arts cdata Format Demuxer
  24. * by Peter Ross (pross@xvid.org)
  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. #include "internal.h"
  31. typedef struct CdataDemuxContext {
  32. unsigned int channels;
  33. unsigned int audio_pts;
  34. } CdataDemuxContext;
  35. static int cdata_probe(AVProbeData *p)
  36. {
  37. const uint8_t *b = p->buf;
  38. if (b[0] == 0x04 && (b[1] == 0x00 || b[1] == 0x04 || b[1] == 0x0C))
  39. return AVPROBE_SCORE_MAX/8;
  40. return 0;
  41. }
  42. static int cdata_read_header(AVFormatContext *s)
  43. {
  44. CdataDemuxContext *cdata = s->priv_data;
  45. AVIOContext *pb = s->pb;
  46. unsigned int sample_rate, header;
  47. AVStream *st;
  48. header = avio_rb16(pb);
  49. switch (header) {
  50. case 0x0400: cdata->channels = 1; break;
  51. case 0x0404: cdata->channels = 2; break;
  52. case 0x040C: cdata->channels = 4; break;
  53. default:
  54. av_log(s, AV_LOG_INFO, "unknown header 0x%04x\n", header);
  55. return -1;
  56. };
  57. sample_rate = avio_rb16(pb);
  58. avio_skip(pb, 12);
  59. st = avformat_new_stream(s, NULL);
  60. if (!st)
  61. return AVERROR(ENOMEM);
  62. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  63. st->codecpar->codec_tag = 0; /* no fourcc */
  64. st->codecpar->codec_id = AV_CODEC_ID_ADPCM_EA_XAS;
  65. st->codecpar->channels = cdata->channels;
  66. st->codecpar->sample_rate = sample_rate;
  67. avpriv_set_pts_info(st, 64, 1, sample_rate);
  68. cdata->audio_pts = 0;
  69. return 0;
  70. }
  71. static int cdata_read_packet(AVFormatContext *s, AVPacket *pkt)
  72. {
  73. CdataDemuxContext *cdata = s->priv_data;
  74. int packet_size = 76*cdata->channels;
  75. int ret = av_get_packet(s->pb, pkt, packet_size);
  76. if (ret < 0)
  77. return ret;
  78. pkt->pts = cdata->audio_pts++;
  79. return 0;
  80. }
  81. AVInputFormat ff_ea_cdata_demuxer = {
  82. .name = "ea_cdata",
  83. .long_name = NULL_IF_CONFIG_SMALL("Electronic Arts cdata"),
  84. .priv_data_size = sizeof(CdataDemuxContext),
  85. .read_probe = cdata_probe,
  86. .read_header = cdata_read_header,
  87. .read_packet = cdata_read_packet,
  88. .extensions = "cdata",
  89. };