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  if (coef_off + codec->channels * (32 + 14) > HEADER_SIZE) {
112  ret = AVERROR_INVALIDDATA;
113  goto fail;
114  }
115 
116  if (ff_alloc_extradata(codec, 32 * codec->channels)) {
117  ret = AVERROR(ENOMEM);
118  goto fail;
119  }
120 
121  /* Get the ADPCM table */
122  bytestream2_seek(&gbc, coef_off, SEEK_SET);
123  for (i = 0; i < codec->channels; i++) {
124  if (bytestream2_get_bufferu(&gbc, codec->extradata + i * 32, 32) != 32) {
125  ret = AVERROR_INVALIDDATA;
126  goto fail;
127  }
128  bytestream2_skipu(&gbc, 14);
129  }
130 
131  avpriv_set_pts_info(st, 64, 1, codec->sample_rate);
132 
133 fail:
134  av_free(header);
135 
136  return ret;
137 }
138 
140 {
141  AVCodecContext *codec = s->streams[0]->codec;
142  RedSparkContext *redspark = s->priv_data;
143  uint32_t size = 8 * codec->channels;
144  int ret;
145 
146  if (avio_feof(s->pb) || redspark->samples_count == s->streams[0]->duration)
147  return AVERROR_EOF;
148 
149  ret = av_get_packet(s->pb, pkt, size);
150  if (ret != size) {
151  av_free_packet(pkt);
152  return AVERROR(EIO);
153  }
154 
155  pkt->duration = 14;
156  redspark->samples_count += pkt->duration;
157  pkt->stream_index = 0;
158 
159  return ret;
160 }
161 
163  .name = "redspark",
164  .long_name = NULL_IF_CONFIG_SMALL("RedSpark"),
165  .priv_data_size = sizeof(RedSparkContext),
169  .extensions = "rsd",
170 };
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:631
Bytestream IO Context.
Definition: avio.h:111
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
Buffered I/O operations.
void av_free_packet(AVPacket *pkt)
Free a packet.
Definition: avpacket.c:280
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:4083
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:34
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:1273
uint8_t
#define av_malloc(s)
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:690
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1617
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:3749
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:1341
static int redspark_read_header(AVFormatContext *s)
Definition: redspark.c:54
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:244
ptrdiff_t size
Definition: opengl_enc.c:101
static const uint8_t header[24]
Definition: sdr2.c:67
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1441
#define av_log(a,...)
int samples_count
Definition: redspark.c:31
#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:175
#define fail()
Definition: checkasm.h:57
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:861
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:450
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:623
Stream structure.
Definition: avformat.h:842
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
enum AVMediaType codec_type
Definition: avcodec.h:1510
enum AVCodecID codec_id
Definition: avcodec.h:1519
int sample_rate
samples per second
Definition: avcodec.h:2262
AVIOContext * pb
I/O context.
Definition: avformat.h:1315
main external API structure.
Definition: avcodec.h:1502
int ff_alloc_extradata(AVCodecContext *avctx, int size)
Allocate extradata with additional AV_INPUT_BUFFER_PADDING_SIZE at end which is always set to 0...
Definition: utils.c:2924
#define AV_WB32(p, v)
Definition: intreadwrite.h:419
This structure contains the data a format has to probe a file.
Definition: avformat.h:448
AVInputFormat ff_redspark_demuxer
Definition: redspark.c:162
static int redspark_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: redspark.c:139
#define HEADER_SIZE
Definition: redspark.c:28
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:901
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:460
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.
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:636
#define av_free(p)
int channels
number of audio channels
Definition: avcodec.h:2263
void * priv_data
Format private data.
Definition: avformat.h:1301
static av_always_inline int bytestream2_seek(GetByteContext *g, int offset, int whence)
Definition: bytestream.h:208
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:628
int avio_feof(AVIOContext *s)
feof() equivalent for AVIOContext.
Definition: aviobuf.c:301
int stream_index
Definition: avcodec.h:1425
This structure stores compressed data.
Definition: avcodec.h:1400