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.

168 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. codec->extradata_size = 32 * codec->channels;
  94. codec->extradata = av_malloc(codec->extradata_size);
  95. if (!codec->extradata) {
  96. ret = AVERROR(ENOMEM);
  97. goto fail;
  98. }
  99. /* Get the ADPCM table */
  100. bytestream2_seek(&gbc, coef_off, SEEK_SET);
  101. for (i = 0; i < codec->channels; i++) {
  102. if (bytestream2_get_bufferu(&gbc, codec->extradata + i * 32, 32) != 32) {
  103. ret = AVERROR_INVALIDDATA;
  104. goto fail;
  105. }
  106. bytestream2_skipu(&gbc, 14);
  107. }
  108. avpriv_set_pts_info(st, 64, 1, codec->sample_rate);
  109. fail:
  110. av_free(header);
  111. return ret;
  112. }
  113. static int redspark_read_packet(AVFormatContext *s, AVPacket *pkt)
  114. {
  115. AVCodecContext *codec = s->streams[0]->codec;
  116. RedSparkContext *redspark = s->priv_data;
  117. uint32_t size = 8 * codec->channels;
  118. int ret;
  119. if (url_feof(s->pb) || redspark->samples_count == s->streams[0]->duration)
  120. return AVERROR_EOF;
  121. ret = av_get_packet(s->pb, pkt, size);
  122. if (ret != size) {
  123. av_free_packet(pkt);
  124. return AVERROR(EIO);
  125. }
  126. pkt->duration = 14;
  127. redspark->samples_count += pkt->duration;
  128. pkt->stream_index = 0;
  129. return ret;
  130. }
  131. AVInputFormat ff_redspark_demuxer = {
  132. .name = "redspark",
  133. .long_name = NULL_IF_CONFIG_SMALL("RedSpark"),
  134. .priv_data_size = sizeof(RedSparkContext),
  135. .read_probe = redspark_probe,
  136. .read_header = redspark_read_header,
  137. .read_packet = redspark_read_packet,
  138. .extensions = "rsd",
  139. };