FFmpeg
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 "demux.h"
27 #include "internal.h"
28 
29 #define HEADER_SIZE 4096
30 #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
31 
32 typedef struct RedSparkContext {
35 
36 static int redspark_probe(const AVProbeData *p)
37 {
38  uint32_t key, data;
39  uint8_t header[8];
40 
41  /* Decrypt first 8 bytes of the header */
42  data = AV_RB32(p->buf);
43  key = data ^ 0x52656453;
44  data ^= key;
46  key = rol(key, 11);
47 
48  key += rol(key, 3);
49  data = AV_RB32(p->buf + 4) ^ key;
50  AV_WB32(header + 4, data);
51 
52  if (AV_RB64(header) == AV_RB64("RedSpark"))
53  return AVPROBE_SCORE_MAX;
54 
55  return 0;
56 }
57 
59 {
60  AVIOContext *pb = s->pb;
61  RedSparkContext *redspark = s->priv_data;
62  AVCodecParameters *par;
63  GetByteContext gbc;
64  int i, coef_off, ret = 0;
65  uint32_t key, data;
66  uint8_t header[HEADER_SIZE];
67  AVStream *st;
68 
69  st = avformat_new_stream(s, NULL);
70  if (!st)
71  return AVERROR(ENOMEM);
72  par = st->codecpar;
73 
74  /* Decrypt header */
75  data = avio_rb32(pb);
76  key = data ^ 0x52656453;
77  data ^= key;
79  key = rol(key, 11);
80 
81  for (i = 4; i < HEADER_SIZE; i += 4) {
82  key += rol(key, 3);
83  data = avio_rb32(pb) ^ key;
84  AV_WB32(header + i, data);
85  }
86 
89 
91  bytestream2_seek(&gbc, 0x3c, SEEK_SET);
92  par->sample_rate = bytestream2_get_be32u(&gbc);
93  if (par->sample_rate <= 0 || par->sample_rate > 96000) {
94  av_log(s, AV_LOG_ERROR, "Invalid sample rate: %d\n", par->sample_rate);
95  return AVERROR_INVALIDDATA;
96  }
97 
98  st->duration = bytestream2_get_be32u(&gbc) * 14;
99  redspark->samples_count = 0;
100  bytestream2_skipu(&gbc, 10);
101  par->ch_layout.nb_channels = bytestream2_get_byteu(&gbc);
102  if (!par->ch_layout.nb_channels) {
103  return AVERROR_INVALIDDATA;
104  }
105 
106  coef_off = 0x54 + par->ch_layout.nb_channels * 8;
107  if (bytestream2_get_byteu(&gbc)) // Loop flag
108  coef_off += 16;
109 
110  if (coef_off + par->ch_layout.nb_channels * (32 + 14) > HEADER_SIZE) {
111  return AVERROR_INVALIDDATA;
112  }
113 
114  if (ff_alloc_extradata(par, 32 * par->ch_layout.nb_channels)) {
115  return AVERROR_INVALIDDATA;
116  }
117 
118  /* Get the ADPCM table */
119  bytestream2_seek(&gbc, coef_off, SEEK_SET);
120  for (i = 0; i < par->ch_layout.nb_channels; i++) {
121  if (bytestream2_get_bufferu(&gbc, par->extradata + i * 32, 32) != 32) {
122  return AVERROR_INVALIDDATA;
123  }
124  bytestream2_skipu(&gbc, 14);
125  }
126 
127  avpriv_set_pts_info(st, 64, 1, par->sample_rate);
128 
129  return ret;
130 }
131 
133 {
134  AVCodecParameters *par = s->streams[0]->codecpar;
135  RedSparkContext *redspark = s->priv_data;
136  uint32_t size = 8 * par->ch_layout.nb_channels;
137  int ret;
138 
139  if (avio_feof(s->pb) || redspark->samples_count == s->streams[0]->duration)
140  return AVERROR_EOF;
141 
142  ret = av_get_packet(s->pb, pkt, size);
143  if (ret != size) {
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  .p.name = "redspark",
156  .p.long_name = NULL_IF_CONFIG_SMALL("RedSpark"),
157  .p.extensions = "rsd",
158  .priv_data_size = sizeof(RedSparkContext),
162 };
AVCodecParameters::extradata
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: codec_par.h:69
redspark_probe
static int redspark_probe(const AVProbeData *p)
Definition: redspark.c:36
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:51
GetByteContext
Definition: bytestream.h:33
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: codec_par.h:47
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
bytestream2_skipu
static av_always_inline void bytestream2_skipu(GetByteContext *g, unsigned int size)
Definition: bytestream.h:174
bytestream2_seek
static av_always_inline int bytestream2_seek(GetByteContext *g, int offset, int whence)
Definition: bytestream.h:212
data
const char data[16]
Definition: mxf.c:148
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:540
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:313
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:463
avpriv_set_pts_info
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: avformat.c:853
AVStream::duration
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:802
avio_rb32
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:760
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_read_callback.c:41
redspark_read_packet
static int redspark_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: redspark.c:132
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:198
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:553
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:453
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
RedSparkContext
Definition: redspark.c:32
key
const char * key
Definition: hwcontext_opencl.c:189
AVFormatContext
Format I/O context.
Definition: avformat.h:1255
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:766
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:550
NULL
#define NULL
Definition: coverity.c:32
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:451
AVCodecParameters::ch_layout
AVChannelLayout ch_layout
Audio only.
Definition: codec_par.h:180
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:184
AV_WB32
#define AV_WB32(p, v)
Definition: intreadwrite.h:417
AVIOContext
Bytestream IO Context.
Definition: avio.h:160
redspark_read_header
static int redspark_read_header(AVFormatContext *s)
Definition: redspark.c:58
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:106
size
int size
Definition: twinvq_data.h:10344
avio.h
AV_RB32
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:96
FFInputFormat::p
AVInputFormat p
The public AVInputFormat.
Definition: demux.h:41
header
static const uint8_t header[24]
Definition: sdr2.c:68
ff_redspark_demuxer
const FFInputFormat ff_redspark_demuxer
Definition: redspark.c:154
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
RedSparkContext::samples_count
int samples_count
Definition: redspark.c:33
demux.h
av_get_packet
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:103
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:743
avformat.h
AVPacket::stream_index
int stream_index
Definition: packet.h:524
AV_CODEC_ID_ADPCM_THP
@ AV_CODEC_ID_ADPCM_THP
Definition: codec_id.h:385
read_probe
static int read_probe(const AVProbeData *p)
Definition: cdg.c:30
bytestream2_get_bufferu
static av_always_inline unsigned int bytestream2_get_bufferu(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition: bytestream.h:277
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:55
AVPacket
This structure stores compressed data.
Definition: packet.h:499
FFInputFormat
Definition: demux.h:37
bytestream.h
bytestream2_init
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:137
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
rol
#define rol(value, bits)
Definition: redspark.c:30
AV_RB64
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:95
HEADER_SIZE
#define HEADER_SIZE
Definition: redspark.c:29
ff_alloc_extradata
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:239
avio_feof
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:345