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.

171 lines
4.7KB

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