FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
redspark.c
Go to the documentation of this file.
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 
22 #include "libavcodec/bytestream.h"
23 #include "libavutil/intreadwrite.h"
24 #include "avformat.h"
25 #include "avio.h"
26 #include "internal.h"
27 
28 #define HEADER_SIZE 4096
29 
30 typedef struct RedSparkContext {
33 
35 {
36  uint32_t key, data;
37  uint8_t header[8];
38 
39  /* Decrypt first 8 bytes of the header */
40  data = AV_RB32(p->buf);
41  data = data ^ (key = data ^ 0x52656453);
42  AV_WB32(header, data);
43  key = (key << 11) | (key >> 21);
44 
45  data = AV_RB32(p->buf + 4) ^ (((key << 3) | (key >> 29)) + key);
46  AV_WB32(header + 4, data);
47 
48  if (AV_RB64(header) == AV_RB64("RedSpark"))
49  return AVPROBE_SCORE_MAX;
50 
51  return 0;
52 }
53 
55 {
56  AVIOContext *pb = s->pb;
57  RedSparkContext *redspark = s->priv_data;
58  AVCodecContext *codec;
59  GetByteContext gbc;
60  int i, coef_off, ret = 0;
61  uint32_t key, data;
62  uint8_t *header, *pbc;
63  AVStream *st;
64 
65  st = avformat_new_stream(s, NULL);
66  if (!st)
67  return AVERROR(ENOMEM);
68  codec = st->codec;
69 
71  if (!header)
72  return AVERROR(ENOMEM);
73  pbc = header;
74 
75  /* Decrypt header */
76  data = avio_rb32(pb);
77  data = data ^ (key = data ^ 0x52656453);
78  bytestream_put_be32(&pbc, data);
79  key = (key << 11) | (key >> 21);
80 
81  for (i = 4; i < HEADER_SIZE; i += 4) {
82  data = avio_rb32(pb) ^ (key = ((key << 3) | (key >> 29)) + key);
83  bytestream_put_be32(&pbc, data);
84  }
85 
88 
89  bytestream2_init(&gbc, header, HEADER_SIZE);
90  bytestream2_seek(&gbc, 0x3c, SEEK_SET);
91  codec->sample_rate = bytestream2_get_be32u(&gbc);
92  if (codec->sample_rate <= 0 || codec->sample_rate > 96000) {
93  av_log(s, AV_LOG_ERROR, "Invalid sample rate: %d\n", codec->sample_rate);
94  ret = AVERROR_INVALIDDATA;
95  goto fail;
96  }
97 
98  st->duration = bytestream2_get_be32u(&gbc) * 14;
99  redspark->samples_count = 0;
100  bytestream2_skipu(&gbc, 10);
101  codec->channels = bytestream2_get_byteu(&gbc);
102  if (!codec->channels) {
103  ret = AVERROR_INVALIDDATA;
104  goto fail;
105  }
106 
107  coef_off = 0x54 + codec->channels * 8;
108  if (bytestream2_get_byteu(&gbc)) // Loop flag
109  coef_off += 16;
110 
111  codec->extradata_size = 32 * codec->channels;
112  codec->extradata = av_malloc(codec->extradata_size);
113  if (!codec->extradata) {
114  ret = AVERROR(ENOMEM);
115  goto fail;
116  }
117 
118  /* Get the ADPCM table */
119  bytestream2_seek(&gbc, coef_off, SEEK_SET);
120  for (i = 0; i < codec->channels; i++) {
121  if (bytestream2_get_bufferu(&gbc, codec->extradata + i * 32, 32) != 32) {
122  ret = AVERROR_INVALIDDATA;
123  goto fail;
124  }
125  bytestream2_skipu(&gbc, 14);
126  }
127 
128  avpriv_set_pts_info(st, 64, 1, codec->sample_rate);
129 
130 fail:
131  av_free(header);
132 
133  return ret;
134 }
135 
137 {
138  AVCodecContext *codec = s->streams[0]->codec;
139  RedSparkContext *redspark = s->priv_data;
140  uint32_t size = 8 * codec->channels;
141  int ret;
142 
143  if (url_feof(s->pb) || redspark->samples_count == s->streams[0]->duration)
144  return AVERROR_EOF;
145 
146  ret = av_get_packet(s->pb, pkt, size);
147  if (ret != size) {
148  av_free_packet(pkt);
149  return AVERROR(EIO);
150  }
151 
152  pkt->duration = 14;
153  redspark->samples_count += pkt->duration;
154  pkt->stream_index = 0;
155 
156  return ret;
157 }
158 
160  .name = "redspark",
161  .long_name = NULL_IF_CONFIG_SMALL("RedSpark"),
162  .priv_data_size = sizeof(RedSparkContext),
166  .extensions = "rsd",
167 };