/src/ffmpeg/libavformat/redspark.c
Line | Count | Source |
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 | 999k | #define HEADER_SIZE 4096 |
30 | 2.92M | #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits)))) |
31 | | |
32 | | typedef struct RedSparkContext { |
33 | | int samples_count; |
34 | | } RedSparkContext; |
35 | | |
36 | | static int redspark_probe(const AVProbeData *p) |
37 | 964k | { |
38 | 964k | uint32_t key, data; |
39 | 964k | uint8_t header[8]; |
40 | | |
41 | | /* Decrypt first 8 bytes of the header */ |
42 | 964k | data = AV_RB32(p->buf); |
43 | 964k | key = data ^ 0x52656453; |
44 | 964k | data ^= key; |
45 | 964k | AV_WB32(header, data); |
46 | 964k | key = rol(key, 11); |
47 | | |
48 | 964k | key += rol(key, 3); |
49 | 964k | data = AV_RB32(p->buf + 4) ^ key; |
50 | 964k | AV_WB32(header + 4, data); |
51 | | |
52 | 964k | if (AV_RB64(header) == AV_RB64("RedSpark")) |
53 | 203 | return AVPROBE_SCORE_MAX; |
54 | | |
55 | 964k | return 0; |
56 | 964k | } |
57 | | |
58 | | static int redspark_read_header(AVFormatContext *s) |
59 | 974 | { |
60 | 974 | AVIOContext *pb = s->pb; |
61 | 974 | RedSparkContext *redspark = s->priv_data; |
62 | 974 | AVCodecParameters *par; |
63 | 974 | GetByteContext gbc; |
64 | 974 | int i, coef_off, ret = 0; |
65 | 974 | uint32_t key, data; |
66 | 974 | uint8_t header[HEADER_SIZE]; |
67 | 974 | AVStream *st; |
68 | | |
69 | 974 | st = avformat_new_stream(s, NULL); |
70 | 974 | if (!st) |
71 | 0 | return AVERROR(ENOMEM); |
72 | 974 | par = st->codecpar; |
73 | | |
74 | | /* Decrypt header */ |
75 | 974 | data = avio_rb32(pb); |
76 | 974 | key = data ^ 0x52656453; |
77 | 974 | data ^= key; |
78 | 974 | AV_WB32(header, data); |
79 | 974 | key = rol(key, 11); |
80 | | |
81 | 997k | for (i = 4; i < HEADER_SIZE; i += 4) { |
82 | 996k | key += rol(key, 3); |
83 | 996k | data = avio_rb32(pb) ^ key; |
84 | 996k | AV_WB32(header + i, data); |
85 | 996k | } |
86 | | |
87 | 974 | par->codec_id = AV_CODEC_ID_ADPCM_THP; |
88 | 974 | par->codec_type = AVMEDIA_TYPE_AUDIO; |
89 | | |
90 | 974 | bytestream2_init(&gbc, header, HEADER_SIZE); |
91 | 974 | bytestream2_seek(&gbc, 0x3c, SEEK_SET); |
92 | 974 | par->sample_rate = bytestream2_get_be32u(&gbc); |
93 | 974 | if (par->sample_rate <= 0 || par->sample_rate > 96000) { |
94 | 308 | av_log(s, AV_LOG_ERROR, "Invalid sample rate: %d\n", par->sample_rate); |
95 | 308 | return AVERROR_INVALIDDATA; |
96 | 308 | } |
97 | | |
98 | 666 | st->duration = bytestream2_get_be32u(&gbc) * 14; |
99 | 666 | redspark->samples_count = 0; |
100 | 666 | bytestream2_skipu(&gbc, 10); |
101 | 666 | par->ch_layout.nb_channels = bytestream2_get_byteu(&gbc); |
102 | 666 | if (!par->ch_layout.nb_channels) { |
103 | 2 | return AVERROR_INVALIDDATA; |
104 | 2 | } |
105 | | |
106 | 664 | coef_off = 0x54 + par->ch_layout.nb_channels * 8; |
107 | 664 | if (bytestream2_get_byteu(&gbc)) // Loop flag |
108 | 649 | coef_off += 16; |
109 | | |
110 | 664 | if (coef_off + par->ch_layout.nb_channels * (32 + 14) > HEADER_SIZE) { |
111 | 20 | return AVERROR_INVALIDDATA; |
112 | 20 | } |
113 | | |
114 | 644 | if (ff_alloc_extradata(par, 32 * par->ch_layout.nb_channels)) { |
115 | 0 | return AVERROR_INVALIDDATA; |
116 | 0 | } |
117 | | |
118 | | /* Get the ADPCM table */ |
119 | 644 | bytestream2_seek(&gbc, coef_off, SEEK_SET); |
120 | 14.0k | for (i = 0; i < par->ch_layout.nb_channels; i++) { |
121 | 13.4k | if (bytestream2_get_bufferu(&gbc, par->extradata + i * 32, 32) != 32) { |
122 | 0 | return AVERROR_INVALIDDATA; |
123 | 0 | } |
124 | 13.4k | bytestream2_skipu(&gbc, 14); |
125 | 13.4k | } |
126 | | |
127 | 644 | avpriv_set_pts_info(st, 64, 1, par->sample_rate); |
128 | | |
129 | 644 | return ret; |
130 | 644 | } |
131 | | |
132 | | static int redspark_read_packet(AVFormatContext *s, AVPacket *pkt) |
133 | 158k | { |
134 | 158k | AVCodecParameters *par = s->streams[0]->codecpar; |
135 | 158k | RedSparkContext *redspark = s->priv_data; |
136 | 158k | uint32_t size = 8 * par->ch_layout.nb_channels; |
137 | 158k | int ret; |
138 | | |
139 | 158k | if (avio_feof(s->pb) || redspark->samples_count == s->streams[0]->duration) |
140 | 788 | return AVERROR_EOF; |
141 | | |
142 | 157k | ret = av_get_packet(s->pb, pkt, size); |
143 | 157k | if (ret != size) { |
144 | 333 | return AVERROR(EIO); |
145 | 333 | } |
146 | | |
147 | 156k | pkt->duration = 14; |
148 | 156k | redspark->samples_count += pkt->duration; |
149 | 156k | pkt->stream_index = 0; |
150 | | |
151 | 156k | return ret; |
152 | 157k | } |
153 | | |
154 | | const FFInputFormat ff_redspark_demuxer = { |
155 | | .p.name = "redspark", |
156 | | .p.long_name = NULL_IF_CONFIG_SMALL("RedSpark"), |
157 | | .p.extensions = "rsd", |
158 | | .priv_data_size = sizeof(RedSparkContext), |
159 | | .read_probe = redspark_probe, |
160 | | .read_header = redspark_read_header, |
161 | | .read_packet = redspark_read_packet, |
162 | | }; |