FFmpeg
Loading...
Searching...
No Matches
smacker.c
Go to the documentation of this file.
1/*
2 * Smacker demuxer
3 * Copyright (c) 2006 Konstantin Shishkov
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/*
23 * Based on http://wiki.multimedia.cx/index.php?title=Smacker
24 */
25
26#include <inttypes.h>
27
30#include "libavutil/mem.h"
31#include "avformat.h"
32#include "avio_internal.h"
33#include "demux.h"
34#include "internal.h"
35
36#define SMACKER_PAL 0x01
37#define SMACKER_FLAG_RING_FRAME 0x01
38#define SMACKER_FLAG_Y_INTERLACE (1 << 1)
39#define SMACKER_FLAG_Y_DOUBLE (1 << 2)
40
48
49typedef struct SmackerContext {
50 uint32_t frames;
51 /* frame info */
52 uint32_t *frm_size;
53 uint8_t *frm_flags;
54 /* internal variables */
58 int indexes[7];
60 /* current frame for demuxing */
61 uint32_t frame_size;
62 int flags;
65 uint8_t pal[768];
68
69/* palette used in Smacker */
70static const uint8_t smk_pal[64] = {
71 0x00, 0x04, 0x08, 0x0C, 0x10, 0x14, 0x18, 0x1C,
72 0x20, 0x24, 0x28, 0x2C, 0x30, 0x34, 0x38, 0x3C,
73 0x41, 0x45, 0x49, 0x4D, 0x51, 0x55, 0x59, 0x5D,
74 0x61, 0x65, 0x69, 0x6D, 0x71, 0x75, 0x79, 0x7D,
75 0x82, 0x86, 0x8A, 0x8E, 0x92, 0x96, 0x9A, 0x9E,
76 0xA2, 0xA6, 0xAA, 0xAE, 0xB2, 0xB6, 0xBA, 0xBE,
77 0xC3, 0xC7, 0xCB, 0xCF, 0xD3, 0xD7, 0xDB, 0xDF,
78 0xE3, 0xE7, 0xEB, 0xEF, 0xF3, 0xF7, 0xFB, 0xFF
79};
80
81
82static int smacker_probe(const AVProbeData *p)
83{
84 if ( AV_RL32(p->buf) != MKTAG('S', 'M', 'K', '2')
85 && AV_RL32(p->buf) != MKTAG('S', 'M', 'K', '4'))
86 return 0;
87
88 if (AV_RL32(p->buf+4) > 32768U || AV_RL32(p->buf+8) > 32768U)
89 return AVPROBE_SCORE_MAX/4;
90
91 return AVPROBE_SCORE_MAX;
92}
93
95{
96 AVIOContext *pb = s->pb;
97 SmackerContext *smk = s->priv_data;
98 AVStream *st;
100 uint32_t magic, width, height, flags, treesize;
101 int64_t pos;
102 int i, ret, pts_inc;
103 int tbase;
104
105 /* read and check header */
106 magic = avio_rl32(pb);
107 if (magic != MKTAG('S', 'M', 'K', '2') && magic != MKTAG('S', 'M', 'K', '4'))
108 return AVERROR_INVALIDDATA;
109 width = avio_rl32(pb);
110 height = avio_rl32(pb);
111 smk->frames = avio_rl32(pb);
112 pts_inc = avio_rl32(pb);
113 if (pts_inc > INT_MAX / 100 || pts_inc == INT_MIN) {
114 av_log(s, AV_LOG_ERROR, "pts_inc %d is invalid\n", pts_inc);
115 return AVERROR_INVALIDDATA;
116 }
117
118 flags = avio_rl32(pb);
120 smk->frames++;
121 if (smk->frames > 0xFFFFFF) {
122 av_log(s, AV_LOG_ERROR, "Too many frames: %"PRIu32"\n", smk->frames);
123 return AVERROR_INVALIDDATA;
124 }
125
126 avio_skip(pb, 28); /* Unused audio related data */
127
128 treesize = avio_rl32(pb);
129 if (treesize >= UINT_MAX/4) {
130 // treesize + 16 must not overflow (this check is probably redundant)
131 av_log(s, AV_LOG_ERROR, "treesize too large\n");
132 return AVERROR_INVALIDDATA;
133 }
134
136 if (!st)
137 return AVERROR(ENOMEM);
138
139 smk->videoindex = st->index;
140 /* Smacker uses 100000 as internal timebase */
141 if (pts_inc < 0)
142 pts_inc = -pts_inc;
143 else
144 pts_inc *= 100;
145 tbase = 100000;
146 av_reduce(&tbase, &pts_inc, tbase, pts_inc, (1UL << 31) - 1);
147 avpriv_set_pts_info(st, 33, pts_inc, tbase);
148 st->duration = smk->frames;
149
150 st->sample_aspect_ratio = (AVRational){ 1, 1 +
152
153 /* init video codec */
154 par = st->codecpar;
155 par->width = width;
156 par->height = height;
157 par->format = AV_PIX_FMT_PAL8;
160 par->codec_tag = magic;
161
162 if ((ret = ff_alloc_extradata(par, treesize + 16)) < 0) {
164 "Cannot allocate %"PRIu32" bytes of extradata\n",
165 treesize + 16);
166 return ret;
167 }
168 if ((ret = ffio_read_size(pb, par->extradata, 16)) < 0)
169 return ret;
170
171 /* handle possible audio streams */
172 for (i = 0; i < 7; i++) {
173 uint32_t rate = avio_rl24(pb);
174 uint8_t aflag = avio_r8(pb);
175
176 smk->indexes[i] = -1;
177
178 if (rate) {
180 if (!ast)
181 return AVERROR(ENOMEM);
182
183 AVCodecParameters *const apar = ast->codecpar;
184
185 smk->indexes[i] = ast->index;
187 if (aflag & SMK_AUD_BINKAUD) {
189 } else if (aflag & SMK_AUD_USEDCT) {
191 } else if (aflag & SMK_AUD_PACKED) {
193 apar->codec_tag = MKTAG('S', 'M', 'K', 'A');
194 } else {
196 }
198 !!(aflag & SMK_AUD_STEREO) + 1);
199 apar->sample_rate = rate;
200 apar->bits_per_coded_sample = (aflag & SMK_AUD_16BITS) ? 16 : 8;
201 if (apar->bits_per_coded_sample == 16 &&
204 else
205 smk->duration_size[i] = 4;
206 avpriv_set_pts_info(ast, 64, 1, apar->sample_rate * apar->ch_layout.nb_channels
207 * apar->bits_per_coded_sample / 8);
208 }
209 }
210
211 avio_rl32(pb); /* padding */
212
213 /* setup data */
214 st->priv_data = av_malloc_array(smk->frames, sizeof(*smk->frm_size) +
215 sizeof(*smk->frm_flags));
216 if (!st->priv_data)
217 return AVERROR(ENOMEM);
218 smk->frm_size = st->priv_data;
219 smk->frm_flags = (void*)(smk->frm_size + smk->frames);
220
221 /* read frame info */
222 pos = 0;
223 for (i = 0; i < smk->frames; i++) {
224 smk->frm_size[i] = avio_rl32(pb);
225 if ((ret = av_add_index_entry(st, pos, i, smk->frm_size[i], 0,
226 (i == 0 || (smk->frm_size[i] & 1)) ? AVINDEX_KEYFRAME : 0)) < 0)
227 return ret;
228 pos += smk->frm_size[i];
229 }
230 if ((ret = ffio_read_size(pb, smk->frm_flags, smk->frames)) < 0 ||
231 /* load trees to extradata, they will be unpacked by decoder */
232 (ret = ffio_read_size(pb, par->extradata + 16,
233 par->extradata_size - 16)) < 0) {
234 return ret;
235 }
236
237 return 0;
238}
239
241{
242 SmackerContext *smk = s->priv_data;
243 int flags;
244 int ret;
245
246 if (avio_feof(s->pb) || smk->cur_frame >= smk->frames)
247 return AVERROR_EOF;
248
249 /* if we demuxed all streams, pass another frame */
250 if (!smk->next_audio_index) {
251 smk->frame_size = smk->frm_size[smk->cur_frame] & (~3);
252 smk->next_frame_pos = avio_tell(s->pb) + smk->frame_size;
253 flags = smk->frm_flags[smk->cur_frame];
254 smk->flags = flags >> 1;
255 /* handle palette change event */
256 if (flags & SMACKER_PAL) {
257 int size, sz, t, off, j, pos;
258 uint8_t *pal = smk->pal;
259 uint8_t oldpal[768];
260
261 memcpy(oldpal, pal, 768);
262 size = avio_r8(s->pb);
263 size = size * 4;
264 if (size > smk->frame_size) {
266 goto next_frame;
267 }
268 smk->frame_size -= size--;
269 sz = 0;
270 pos = avio_tell(s->pb) + size;
271 while (sz < 256) {
272 t = avio_r8(s->pb);
273 if (t & 0x80) { /* skip palette entries */
274 sz += (t & 0x7F) + 1;
275 pal += ((t & 0x7F) + 1) * 3;
276 } else if (t & 0x40) { /* copy with offset */
277 off = avio_r8(s->pb);
278 j = (t & 0x3F) + 1;
279 if (off + j > 0x100) {
281 "Invalid palette update, offset=%d length=%d extends beyond palette size\n",
282 off, j);
284 goto next_frame;
285 }
286 off *= 3;
287 while (j-- && sz < 256) {
288 *pal++ = oldpal[off + 0];
289 *pal++ = oldpal[off + 1];
290 *pal++ = oldpal[off + 2];
291 sz++;
292 off += 3;
293 }
294 } else { /* new entries */
295 *pal++ = smk_pal[t];
296 *pal++ = smk_pal[avio_r8(s->pb) & 0x3F];
297 *pal++ = smk_pal[avio_r8(s->pb) & 0x3F];
298 sz++;
299 }
300 }
301 avio_seek(s->pb, pos, 0);
302 smk->new_palette = 1;
303 }
304 }
305
306 for (int i = smk->next_audio_index; i < 7; i++) {
307 if (smk->flags & (1 << i)) {
308 uint32_t size;
309
310 size = avio_rl32(s->pb);
311 if ((int)size < 4 + smk->duration_size[i] || size > smk->frame_size) {
312 av_log(s, AV_LOG_ERROR, "Invalid audio part size\n");
314 goto next_frame;
315 }
316 smk->frame_size -= size;
317 size -= 4;
318
319 if (smk->indexes[i] < 0 ||
320 s->streams[smk->indexes[i]]->discard >= AVDISCARD_ALL) {
321 smk->aud_pts[i] += smk->duration_size[i] ? avio_rl32(s->pb)
322 : size;
323 avio_skip(s->pb, size - smk->duration_size[i]);
324 continue;
325 }
326 if ((ret = av_get_packet(s->pb, pkt, size)) != size) {
327 ret = ret < 0 ? ret : AVERROR_INVALIDDATA;
328 goto next_frame;
329 }
330 pkt->stream_index = smk->indexes[i];
331 pkt->pts = smk->aud_pts[i];
332 pkt->duration = smk->duration_size[i] ? AV_RL32(pkt->data)
333 : size;
334 smk->aud_pts[i] += pkt->duration;
335 smk->next_audio_index = i + 1;
336 return 0;
337 }
338 }
339
340 if (s->streams[smk->videoindex]->discard >= AVDISCARD_ALL) {
341 ret = FFERROR_REDO;
342 goto next_frame;
343 }
344 if (smk->frame_size >= INT_MAX/2) {
346 goto next_frame;
347 }
348 if ((ret = av_new_packet(pkt, smk->frame_size + 769)) < 0)
349 goto next_frame;
350 flags = smk->new_palette;
351 if ((smk->frm_size[smk->cur_frame] & 1) || smk->cur_frame == 0)
352 flags |= 2;
353 pkt->data[0] = flags;
354 memcpy(pkt->data + 1, smk->pal, 768);
355 ret = ffio_read_size(s->pb, pkt->data + 769, smk->frame_size);
356 if (ret < 0)
357 goto next_frame;
358 pkt->stream_index = smk->videoindex;
359 pkt->pts = smk->cur_frame;
360 pkt->duration = 1;
361 if (flags & 2)
362 pkt->flags |= AV_PKT_FLAG_KEY;
363 smk->next_audio_index = 0;
364 smk->new_palette = 0;
365 smk->cur_frame++;
366
367 return 0;
368next_frame:
369 avio_seek(s->pb, smk->next_frame_pos, SEEK_SET);
370 smk->next_audio_index = 0;
371 smk->cur_frame++;
372 return ret;
373}
374
375static int smacker_read_seek(AVFormatContext *s, int stream_index,
376 int64_t timestamp, int flags)
377{
378 AVStream *st = s->streams[stream_index];
379 SmackerContext *smk = s->priv_data;
380 int64_t pos;
381 int ret;
382
383 if (!(s->pb->seekable & AVIO_SEEKABLE_NORMAL))
384 return -1;
385
386 if (timestamp < 0 || timestamp >= smk->frames)
387 return AVERROR(EINVAL);
388
389 ret = av_index_search_timestamp(st, timestamp, flags);
390 if (ret < 0)
391 return ret;
392
394 pos += ffstream(st)->index_entries[ret].pos;
395 pos = avio_seek(s->pb, pos, SEEK_SET);
396 if (pos < 0)
397 return pos;
398
399 smk->cur_frame = ret;
400 smk->next_audio_index = 0;
401 smk->new_palette = 0;
402 memset(smk->pal, 0, sizeof(smk->pal));
403 memset(smk->aud_pts, 0, sizeof(smk->aud_pts));
404
405 return 0;
406}
407
409 .p.name = "smk",
410 .p.long_name = NULL_IF_CONFIG_SMALL("Smacker"),
411 .priv_data_size = sizeof(SmackerContext),
416};
const FFInputFormat ff_smacker_demuxer
Definition smacker.c:408
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 AVINDEX_KEYFRAME
Definition avformat.h:628
#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
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition aviobuf.c:236
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition avio.h:41
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition aviobuf.c:349
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition avio.h:494
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition aviobuf.c:321
unsigned int avio_rl32(AVIOContext *s)
Definition aviobuf.c:733
unsigned int avio_rl24(AVIOContext *s)
Definition aviobuf.c:725
int avio_r8(AVIOContext *s)
Definition aviobuf.c:606
int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition aviobuf.c:665
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
#define flags(name, subs,...)
Definition cbs_h264.c:74
#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
Public libavutil channel layout APIs header.
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
#define FFERROR_REDO
Returned by demuxers to indicate that data was consumed but discarded (ignored streams or junk data).
Definition demux.h:224
static AVPacket * pkt
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:578
@ AV_CODEC_ID_PCM_U8
Definition codec_id.h:335
@ AV_CODEC_ID_SMACKVIDEO
Definition codec_id.h:133
@ AV_CODEC_ID_PCM_S16LE
Definition codec_id.h:330
@ AV_CODEC_ID_BINKAUDIO_DCT
Definition codec_id.h:501
@ AV_CODEC_ID_SMACKAUDIO
Definition codec_id.h:476
@ AV_CODEC_ID_BINKAUDIO_RDFT
Definition codec_id.h:500
@ AVDISCARD_ALL
discard all
Definition defs.h:232
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition packet.h:650
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition packet.c:98
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
int av_add_index_entry(AVStream *st, int64_t pos, int64_t timestamp, int size, int distance, int flags)
Add an index entry into a sorted list.
Definition seek.c:122
int av_index_search_timestamp(AVStream *st, int64_t timestamp, int flags)
Get the index for a specific timestamp.
Definition seek.c:245
void av_channel_layout_default(AVChannelLayout *ch_layout, int nb_channels)
Get the default channel layout for a given number of channels.
#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
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition rational.c:35
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
#define AV_RL32(p)
static av_always_inline FFStream * ffstream(AVStream *st)
Definition internal.h:365
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
static av_always_inline FFFormatContext * ffformatcontext(AVFormatContext *s)
Definition internal.h:130
#define SMACKER_FLAG_RING_FRAME
Definition smacker.c:37
#define SMACKER_FLAG_Y_DOUBLE
Definition smacker.c:39
#define SMACKER_PAL
Definition smacker.c:36
static const uint8_t smk_pal[64]
Definition smacker.c:70
static int smacker_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition smacker.c:240
SAudFlags
Definition smacker.c:41
@ SMK_AUD_USEDCT
Definition smacker.c:46
@ SMK_AUD_BINKAUD
Definition smacker.c:45
@ SMK_AUD_PACKED
Definition smacker.c:42
@ SMK_AUD_16BITS
Definition smacker.c:43
@ SMK_AUD_STEREO
Definition smacker.c:44
#define SMACKER_FLAG_Y_INTERLACE
Definition smacker.c:38
static int smacker_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Definition smacker.c:375
static int smacker_read_header(AVFormatContext *s)
Definition smacker.c:94
static int smacker_probe(const AVProbeData *p)
Definition smacker.c:82
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition libcdio.c:151
#define MKTAG(a, b, c, d)
Definition macros.h:55
Memory handling functions.
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition pixfmt.h:84
unsigned int pos
Definition spdifenc.c:431
int nb_channels
Number of channels in this layout.
This struct describes the properties of an encoded stream.
Definition codec_par.h:49
int extradata_size
Size of the extradata content in bytes.
Definition codec_par.h:75
int height
The height of the video frame in pixels.
Definition codec_par.h:150
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition codec_par.h:113
AVChannelLayout ch_layout
The channel layout and number of channels.
Definition codec_par.h:207
int width
The width of the video frame in pixels.
Definition codec_par.h:143
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition codec_par.h:61
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
int64_t pos
Definition avformat.h:621
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
Rational number (pair of numerator and denominator).
Definition rational.h:58
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition avformat.h:844
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition avformat.h:825
void * priv_data
Definition avformat.h:791
int index
stream index in AVFormatContext
Definition avformat.h:772
int64_t data_offset
offset of the first packet
Definition internal.h:89
AVIndexEntry * index_entries
Only used if the format does not support seeking natively.
Definition internal.h:191
uint32_t frames
Definition smacker.c:50
uint8_t * frm_flags
Definition smacker.c:53
int duration_size[7]
Definition smacker.c:59
uint32_t * frm_size
Definition smacker.c:52
int indexes[7]
Definition smacker.c:58
int new_palette
Definition smacker.c:64
int64_t aud_pts[7]
Definition smacker.c:66
uint8_t pal[768]
Definition smacker.c:65
uint32_t frame_size
Definition smacker.c:61
int64_t next_frame_pos
Definition smacker.c:55
int next_audio_index
Definition smacker.c:63
#define av_malloc_array(a, b)
#define av_log(a,...)
#define height
Definition dsp.h:89
#define width
Definition dsp.h:89
int size