3
* Copyright (c) 2011 Paul B Mahol
5
* This file is part of Libav.
7
* Libav 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.
12
* Libav 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.
17
* You should have received a copy of the GNU Lesser General Public
18
* License along with Libav; if not, write to the Free Software
19
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24
* This is a demuxer for Loki SDL Motion JPEG files
31
static const AVCodecTag codec_smjpeg_video_tags[] = {
32
{ CODEC_ID_MJPEG, MKTAG('J', 'F', 'I', 'F') },
36
static const AVCodecTag codec_smjpeg_audio_tags[] = {
37
{ CODEC_ID_ADPCM_IMA_SMJPEG, MKTAG('A', 'P', 'C', 'M') },
38
{ CODEC_ID_PCM_S16LE, MKTAG('N', 'O', 'N', 'E') },
42
typedef struct SMJPEGContext {
43
int audio_stream_index;
44
int video_stream_index;
47
static int smjpeg_probe(AVProbeData *p)
49
if (!memcmp(p->buf, "\x0\xaSMJPEG", 8))
50
return AVPROBE_SCORE_MAX;
54
static int smjpeg_read_header(AVFormatContext *s, AVFormatParameters *ap)
56
SMJPEGContext *sc = s->priv_data;
57
AVStream *ast = NULL, *vst = NULL;
58
AVIOContext *pb = s->pb;
59
uint32_t version, htype, hlength, duration;
62
avio_skip(pb, 8); // magic
63
version = avio_rb32(pb);
65
av_log_ask_for_sample(s, "unknown version %d\n", version);
67
duration = avio_rb32(pb); // in msec
69
while (!pb->eof_reached) {
70
htype = avio_rl32(pb);
72
case MKTAG('_', 'T', 'X', 'T'):
73
hlength = avio_rb32(pb);
74
if (!hlength || hlength > 512)
75
return AVERROR_INVALIDDATA;
76
comment = av_malloc(hlength + 1);
78
return AVERROR(ENOMEM);
79
if (avio_read(pb, comment, hlength) != hlength) {
81
av_log(s, AV_LOG_ERROR, "error when reading comment\n");
82
return AVERROR_INVALIDDATA;
85
av_dict_set(&s->metadata, "comment", comment,
86
AV_DICT_DONT_STRDUP_VAL);
88
case MKTAG('_', 'S', 'N', 'D'):
90
av_log_ask_for_sample(s, "multiple audio streams not supported\n");
91
return AVERROR_INVALIDDATA;
93
hlength = avio_rb32(pb);
95
return AVERROR_INVALIDDATA;
96
ast = avformat_new_stream(s, 0);
98
return AVERROR(ENOMEM);
99
ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
100
ast->codec->sample_rate = avio_rb16(pb);
101
ast->codec->bits_per_coded_sample = avio_r8(pb);
102
ast->codec->channels = avio_r8(pb);
103
ast->codec->codec_tag = avio_rl32(pb);
104
ast->codec->codec_id = ff_codec_get_id(codec_smjpeg_audio_tags,
105
ast->codec->codec_tag);
106
ast->duration = duration;
107
sc->audio_stream_index = ast->index;
108
avpriv_set_pts_info(ast, 32, 1, 1000);
109
avio_skip(pb, hlength - 8);
111
case MKTAG('_', 'V', 'I', 'D'):
113
av_log_ask_for_sample(s, "multiple video streams not supported\n");
114
return AVERROR_INVALIDDATA;
116
hlength = avio_rb32(pb);
118
return AVERROR_INVALIDDATA;
119
avio_skip(pb, 4); // number of frames
120
vst = avformat_new_stream(s, 0);
122
return AVERROR(ENOMEM);
123
vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
124
vst->codec->width = avio_rb16(pb);
125
vst->codec->height = avio_rb16(pb);
126
vst->codec->codec_tag = avio_rl32(pb);
127
vst->codec->codec_id = ff_codec_get_id(codec_smjpeg_video_tags,
128
vst->codec->codec_tag);
129
vst->duration = duration;
130
sc->video_stream_index = vst->index;
131
avpriv_set_pts_info(vst, 32, 1, 1000);
132
avio_skip(pb, hlength - 12);
134
case MKTAG('H', 'E', 'N', 'D'):
137
av_log(s, AV_LOG_ERROR, "unknown header %x\n", htype);
138
return AVERROR_INVALIDDATA;
145
static int smjpeg_read_packet(AVFormatContext *s, AVPacket *pkt)
147
SMJPEGContext *sc = s->priv_data;
148
uint32_t dtype, ret, size, timestamp;
150
if (s->pb->eof_reached)
152
dtype = avio_rl32(s->pb);
154
case MKTAG('s', 'n', 'd', 'D'):
155
timestamp = avio_rb32(s->pb);
156
size = avio_rb32(s->pb);
157
ret = av_get_packet(s->pb, pkt, size);
158
pkt->stream_index = sc->audio_stream_index;
159
pkt->pts = timestamp;
161
case MKTAG('v', 'i', 'd', 'D'):
162
timestamp = avio_rb32(s->pb);
163
size = avio_rb32(s->pb);
164
ret = av_get_packet(s->pb, pkt, size);
165
pkt->stream_index = sc->video_stream_index;
166
pkt->pts = timestamp;
168
case MKTAG('D', 'O', 'N', 'E'):
172
av_log(s, AV_LOG_ERROR, "unknown chunk %x\n", dtype);
173
ret = AVERROR_INVALIDDATA;
179
AVInputFormat ff_smjpeg_demuxer = {
181
.long_name = NULL_IF_CONFIG_SMALL("Loki SDL MJPEG"),
182
.priv_data_size = sizeof(SMJPEGContext),
183
.read_probe = smjpeg_probe,
184
.read_header = smjpeg_read_header,
185
.read_packet = smjpeg_read_packet,
186
.extensions = "mjpg",