FFmpeg
Loading...
Searching...
No Matches
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
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
35
36static 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;
64 int i, coef_off, ret = 0;
65 uint32_t key, data;
66 uint8_t header[HEADER_SIZE];
67 AVStream *st;
68
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);
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_INVALIDDATA;
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};
#define HEADER_SIZE
Definition adxenc.c:99
const FFInputFormat ff_redspark_demuxer
Definition redspark.c:154
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:834
Main libavformat public API header.
#define AVPROBE_SCORE_MAX
maximum score
Definition avformat.h:483
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:98
Buffered I/O operations.
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition aviobuf.c:349
unsigned int avio_rb32(AVIOContext *s)
Definition aviobuf.c:764
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
static av_always_inline void bytestream2_skipu(GetByteContext *g, unsigned int size)
Definition bytestream.h:174
static av_always_inline unsigned int bytestream2_get_bufferu(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition bytestream.h:277
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition bytestream.h:137
static av_always_inline int bytestream2_seek(GetByteContext *g, int offset, int whence)
Definition bytestream.h:212
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
static int read_probe(const AVProbeData *p)
Definition cdg.c:30
#define NULL
Definition coverity.c:32
static AVPacket * pkt
const char * key
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:578
@ AV_CODEC_ID_ADPCM_THP
Definition codec_id.h:388
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
#define AV_WB32(p, v)
#define AV_RB32(p)
#define AV_RB64(p)
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:237
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
const char data[16]
Definition mxf.c:149
static int redspark_read_header(AVFormatContext *s)
Definition redspark.c:58
static int redspark_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition redspark.c:132
static int redspark_probe(const AVProbeData *p)
Definition redspark.c:36
#define rol(value, bits)
Definition redspark.c:30
static const uint8_t header[24]
Definition sdr2.c:68
int nb_channels
Number of channels in this layout.
This struct describes the properties of an encoded stream.
Definition codec_par.h:49
AVChannelLayout ch_layout
The channel layout and number of channels.
Definition codec_par.h:207
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition codec_par.h:71
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
int sample_rate
The number of audio samples per second.
Definition codec_par.h:213
Format I/O context.
Definition avformat.h:1333
Bytestream IO Context.
Definition avio.h:160
This structure stores compressed data.
Definition packet.h:580
This structure contains the data a format has to probe a file.
Definition avformat.h:471
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition avformat.h:825
#define av_log(a,...)
int size