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.

162 lines
4.4KB

  1. /*
  2. * RedSpark demuxer
  3. * Copyright (c) 2013 James Almer
  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. #include "libavcodec/bytestream.h"
  22. #include "libavutil/intreadwrite.h"
  23. #include "avformat.h"
  24. #include "avio.h"
  25. #include "internal.h"
  26. #define HEADER_SIZE 4096
  27. #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
  28. typedef struct RedSparkContext {
  29. int samples_count;
  30. } RedSparkContext;
  31. static int redspark_probe(const AVProbeData *p)
  32. {
  33. uint32_t key, data;
  34. uint8_t header[8];
  35. /* Decrypt first 8 bytes of the header */
  36. data = AV_RB32(p->buf);
  37. key = data ^ 0x52656453;
  38. data ^= key;
  39. AV_WB32(header, data);
  40. key = rol(key, 11);
  41. key += rol(key, 3);
  42. data = AV_RB32(p->buf + 4) ^ key;
  43. AV_WB32(header + 4, data);
  44. if (AV_RB64(header) == AV_RB64("RedSpark"))
  45. return AVPROBE_SCORE_MAX;
  46. return 0;
  47. }
  48. static int redspark_read_header(AVFormatContext *s)
  49. {
  50. AVIOContext *pb = s->pb;
  51. RedSparkContext *redspark = s->priv_data;
  52. AVCodecParameters *par;
  53. GetByteContext gbc;
  54. int i, coef_off, ret = 0;
  55. uint32_t key, data;
  56. uint8_t header[HEADER_SIZE];
  57. AVStream *st;
  58. st = avformat_new_stream(s, NULL);
  59. if (!st)
  60. return AVERROR(ENOMEM);
  61. par = st->codecpar;
  62. /* Decrypt header */
  63. data = avio_rb32(pb);
  64. key = data ^ 0x52656453;
  65. data ^= key;
  66. AV_WB32(header, data);
  67. key = rol(key, 11);
  68. for (i = 4; i < HEADER_SIZE; i += 4) {
  69. key += rol(key, 3);
  70. data = avio_rb32(pb) ^ key;
  71. AV_WB32(header + i, data);
  72. }
  73. par->codec_id = AV_CODEC_ID_ADPCM_THP;
  74. par->codec_type = AVMEDIA_TYPE_AUDIO;
  75. bytestream2_init(&gbc, header, HEADER_SIZE);
  76. bytestream2_seek(&gbc, 0x3c, SEEK_SET);
  77. par->sample_rate = bytestream2_get_be32u(&gbc);
  78. if (par->sample_rate <= 0 || par->sample_rate > 96000) {
  79. av_log(s, AV_LOG_ERROR, "Invalid sample rate: %d\n", par->sample_rate);
  80. return AVERROR_INVALIDDATA;
  81. }
  82. st->duration = bytestream2_get_be32u(&gbc) * 14;
  83. redspark->samples_count = 0;
  84. bytestream2_skipu(&gbc, 10);
  85. par->channels = bytestream2_get_byteu(&gbc);
  86. if (!par->channels) {
  87. return AVERROR_INVALIDDATA;
  88. }
  89. coef_off = 0x54 + par->channels * 8;
  90. if (bytestream2_get_byteu(&gbc)) // Loop flag
  91. coef_off += 16;
  92. if (coef_off + par->channels * (32 + 14) > HEADER_SIZE) {
  93. return AVERROR_INVALIDDATA;
  94. }
  95. if (ff_alloc_extradata(par, 32 * par->channels)) {
  96. return AVERROR_INVALIDDATA;
  97. }
  98. /* Get the ADPCM table */
  99. bytestream2_seek(&gbc, coef_off, SEEK_SET);
  100. for (i = 0; i < par->channels; i++) {
  101. if (bytestream2_get_bufferu(&gbc, par->extradata + i * 32, 32) != 32) {
  102. return AVERROR_INVALIDDATA;
  103. }
  104. bytestream2_skipu(&gbc, 14);
  105. }
  106. avpriv_set_pts_info(st, 64, 1, par->sample_rate);
  107. return ret;
  108. }
  109. static int redspark_read_packet(AVFormatContext *s, AVPacket *pkt)
  110. {
  111. AVCodecParameters *par = s->streams[0]->codecpar;
  112. RedSparkContext *redspark = s->priv_data;
  113. uint32_t size = 8 * par->channels;
  114. int ret;
  115. if (avio_feof(s->pb) || redspark->samples_count == s->streams[0]->duration)
  116. return AVERROR_EOF;
  117. ret = av_get_packet(s->pb, pkt, size);
  118. if (ret != size) {
  119. return AVERROR(EIO);
  120. }
  121. pkt->duration = 14;
  122. redspark->samples_count += pkt->duration;
  123. pkt->stream_index = 0;
  124. return ret;
  125. }
  126. AVInputFormat ff_redspark_demuxer = {
  127. .name = "redspark",
  128. .long_name = NULL_IF_CONFIG_SMALL("RedSpark"),
  129. .priv_data_size = sizeof(RedSparkContext),
  130. .read_probe = redspark_probe,
  131. .read_header = redspark_read_header,
  132. .read_packet = redspark_read_packet,
  133. .extensions = "rsd",
  134. };