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 #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
30 
31 typedef struct RedSparkContext {
34 
36 {
37  uint32_t key, data;
38  uint8_t header[8];
39 
40  /* Decrypt first 8 bytes of the header */
41  data = AV_RB32(p->buf);
42  key = data ^ 0x52656453;
43  data ^= key;
44  AV_WB32(header, data);
45  key = rol(key, 11);
46 
47  key += rol(key, 3);
48  data = AV_RB32(p->buf + 4) ^ key;
49  AV_WB32(header + 4, data);
50 
51  if (AV_RB64(header) == AV_RB64("RedSpark"))
52  return AVPROBE_SCORE_MAX;
53 
54  return 0;
55 }
56 
58 {
59  AVIOContext *pb = s->pb;
60  RedSparkContext *redspark = s->priv_data;
61  AVCodecParameters *par;
62  GetByteContext gbc;
63  int i, coef_off, ret = 0;
64  uint32_t key, data;
66  AVStream *st;
67 
68  st = avformat_new_stream(s, NULL);
69  if (!st)
70  return AVERROR(ENOMEM);
71  par = st->codecpar;
72 
73  /* Decrypt header */
74  data = avio_rb32(pb);
75  key = data ^ 0x52656453;
76  data ^= key;
77  AV_WB32(header, data);
78  key = rol(key, 11);
79 
80  for (i = 4; i < HEADER_SIZE; i += 4) {
81  key += rol(key, 3);
82  data = avio_rb32(pb) ^ key;
83  AV_WB32(header + i, data);
84  }
85 
88 
89  bytestream2_init(&gbc, header, HEADER_SIZE);
90  bytestream2_seek(&gbc, 0x3c, SEEK_SET);
91  par->sample_rate = bytestream2_get_be32u(&gbc);
92  if (par->sample_rate <= 0 || par->sample_rate > 96000) {
93  av_log(s, AV_LOG_ERROR, "Invalid sample rate: %d\n", par->sample_rate);
94  return AVERROR_INVALIDDATA;
95  }
96 
97  st->duration = bytestream2_get_be32u(&gbc) * 14;
98  redspark->samples_count = 0;
99  bytestream2_skipu(&gbc, 10);
100  par->channels = bytestream2_get_byteu(&gbc);
101  if (!par->channels) {
102  return AVERROR_INVALIDDATA;
103  }
104 
105  coef_off = 0x54 + par->channels * 8;
106  if (bytestream2_get_byteu(&gbc)) // Loop flag
107  coef_off += 16;
108 
109  if (coef_off + par->channels * (32 + 14) > HEADER_SIZE) {
110  return AVERROR_INVALIDDATA;
111  }
112 
113  if (ff_alloc_extradata(par, 32 * par->channels)) {
114  return AVERROR_INVALIDDATA;
115  }
116 
117  /* Get the ADPCM table */
118  bytestream2_seek(&gbc, coef_off, SEEK_SET);
119  for (i = 0; i < par->channels; i++) {
120  if (bytestream2_get_bufferu(&gbc, par->extradata + i * 32, 32) != 32) {
121  return AVERROR_INVALIDDATA;
122  }
123  bytestream2_skipu(&gbc, 14);
124  }
125 
126  avpriv_set_pts_info(st, 64, 1, par->sample_rate);
127 
128  return ret;
129 }
130 
132 {
133  AVCodecParameters *par = s->streams[0]->codecpar;
134  RedSparkContext *redspark = s->priv_data;
135  uint32_t size = 8 * par->channels;
136  int ret;
137 
138  if (avio_feof(s->pb) || redspark->samples_count == s->streams[0]->duration)
139  return AVERROR_EOF;
140 
141  ret = av_get_packet(s->pb, pkt, size);
142  if (ret != size) {
143  av_packet_unref(pkt);
144  return AVERROR(EIO);
145  }
146 
147  pkt->duration = 14;
148  redspark->samples_count += pkt->duration;
149  pkt->stream_index = 0;
150 
151  return ret;
152 }
153 
155  .name = "redspark",
156  .long_name = NULL_IF_CONFIG_SMALL("RedSpark"),
157  .priv_data_size = sizeof(RedSparkContext),
161  .extensions = "rsd",
162 };
#define rol(value, bits)
Definition: redspark.c:29
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:768
Bytestream IO Context.
Definition: avio.h:161
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
Buffered I/O operations.
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:4737
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:4152
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:133
static AVPacket pkt
static int redspark_probe(AVProbeData *p)
Definition: redspark.c:35
This struct describes the properties of an encoded stream.
Definition: avcodec.h:4144
static av_always_inline unsigned int bytestream2_get_bufferu(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition: bytestream.h:273
Format I/O context.
Definition: avformat.h:1349
uint8_t
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:790
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1697
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4367
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
Definition: bytestream.h:87
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1417
static int redspark_read_header(AVFormatContext *s)
Definition: redspark.c:57
static av_always_inline void bytestream2_skipu(GetByteContext *g, unsigned int size)
Definition: bytestream.h:170
#define AVERROR_EOF
End of file.
Definition: error.h:55
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition: utils.c:289
ptrdiff_t size
Definition: opengl_enc.c:101
static const uint8_t header[24]
Definition: sdr2.c:67
#define av_log(a,...)
int samples_count
Definition: redspark.c:32
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:179
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:4148
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:463
int ff_alloc_extradata(AVCodecParameters *par, int size)
Allocate extradata with additional AV_INPUT_BUFFER_PADDING_SIZE at end which is always set to 0...
Definition: utils.c:3216
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:528
Stream structure.
Definition: avformat.h:889
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
AVIOContext * pb
I/O context.
Definition: avformat.h:1391
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:618
#define AV_WB32(p, v)
Definition: intreadwrite.h:424
This structure contains the data a format has to probe a file.
Definition: avformat.h:461
AVInputFormat ff_redspark_demuxer
Definition: redspark.c:154
static int redspark_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: redspark.c:131
#define HEADER_SIZE
Definition: redspark.c:28
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:946
int sample_rate
Audio only.
Definition: avcodec.h:4262
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:473
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_RB64
Definition: bytestream.h:87
Main libavformat public API header.
void * priv_data
Format private data.
Definition: avformat.h:1377
static av_always_inline int bytestream2_seek(GetByteContext *g, int offset, int whence)
Definition: bytestream.h:208
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: avcodec.h:4166
int channels
Audio only.
Definition: avcodec.h:4258
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:664
AVCodecParameters * codecpar
Definition: avformat.h:1252
int avio_feof(AVIOContext *s)
feof() equivalent for AVIOContext.
Definition: aviobuf.c:356
int stream_index
Definition: avcodec.h:1681
This structure stores compressed data.
Definition: avcodec.h:1656