3 * Copyright (c) 2011 Paul B Mahol
5 * This file is part of FFmpeg.
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.
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.
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
24 * This is a demuxer for Loki SDL MJPEG files
31 static const AVCodecTag codec_smjpeg_tags[] = {
32 { CODEC_ID_ADPCM_IMA_SMJPEG, MKTAG('A', 'P', 'C', 'M') },
33 { CODEC_ID_PCM_S16LE, MKTAG('N', 'O', 'N', 'E') },
34 { CODEC_ID_MJPEG, MKTAG('J', 'F', 'I', 'F') },
38 typedef struct SMJPEGContext {
39 int audio_stream_index;
40 int video_stream_index;
43 static int smjpeg_probe(AVProbeData *p)
45 if (!memcmp(p->buf, "\x0\xaSMJPEG", 8))
46 return AVPROBE_SCORE_MAX;
50 static int smjpeg_read_header(AVFormatContext *s, AVFormatParameters *ap)
52 SMJPEGContext *sc = s->priv_data;
53 AVStream *ast = NULL, *vst = NULL;
54 AVIOContext *pb = s->pb;
55 uint32_t version, htype, hlength, duration;
58 avio_skip(pb, 8); // magic
59 version = avio_rb32(pb);
61 av_log_ask_for_sample(s, "unknown version %d\n", version);
63 duration = avio_rb32(pb); // in msec
65 while (!pb->eof_reached) {
66 htype = avio_rl32(pb);
68 case MKTAG('_', 'T', 'X', 'T'):
69 hlength = avio_rb32(pb);
70 if (!hlength || hlength > 512)
71 return AVERROR_INVALIDDATA;
72 comment = av_malloc(hlength + 1);
74 return AVERROR(ENOMEM);
75 if (avio_read(pb, comment, hlength) != hlength) {
77 av_log(s, AV_LOG_ERROR, "error when reading comment\n");
78 return AVERROR_INVALIDDATA;
81 av_dict_set(&s->metadata, "comment", comment,
82 AV_DICT_DONT_STRDUP_VAL);
84 case MKTAG('_', 'S', 'N', 'D'):
86 av_log_ask_for_sample(s, "multiple audio streams not supported\n");
87 return AVERROR_INVALIDDATA;
89 hlength = avio_rb32(pb);
91 return AVERROR_INVALIDDATA;
92 ast = avformat_new_stream(s, 0);
94 return AVERROR(ENOMEM);
95 ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
96 ast->codec->sample_rate = avio_rb16(pb);
97 ast->codec->bits_per_coded_sample = avio_r8(pb);
98 ast->codec->channels = avio_r8(pb);
99 ast->codec->codec_tag = avio_rl32(pb);
100 ast->codec->codec_id = ff_codec_get_id(codec_smjpeg_tags,
101 ast->codec->codec_tag);
102 ast->duration = duration;
103 sc->audio_stream_index = ast->index;
104 avpriv_set_pts_info(ast, 32, 1, 1000);
105 avio_skip(pb, hlength - 8);
107 case MKTAG('_', 'V', 'I', 'D'):
109 av_log_ask_for_sample(s, "multiple video streams not supported\n");
110 return AVERROR_INVALIDDATA;
112 hlength = avio_rb32(pb);
114 return AVERROR_INVALIDDATA;
115 avio_skip(pb, 4); // number of frames
116 vst = avformat_new_stream(s, 0);
118 return AVERROR(ENOMEM);
119 vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
120 vst->codec->width = avio_rb16(pb);
121 vst->codec->height = avio_rb16(pb);
122 vst->codec->codec_tag = avio_rl32(pb);
123 vst->codec->codec_id = ff_codec_get_id(codec_smjpeg_tags,
124 vst->codec->codec_tag);
125 vst->duration = duration;
126 sc->video_stream_index = vst->index;
127 avpriv_set_pts_info(vst, 32, 1, 1000);
128 avio_skip(pb, hlength - 12);
130 case MKTAG('H', 'E', 'N', 'D'):
133 av_log(s, AV_LOG_ERROR, "unknown header %x\n", htype);
134 return AVERROR_INVALIDDATA;
141 static int smjpeg_read_packet(AVFormatContext *s, AVPacket *pkt)
143 SMJPEGContext *sc = s->priv_data;
144 uint32_t dtype, ret, size, timestamp;
146 if (s->pb->eof_reached)
148 dtype = avio_rl32(s->pb);
150 case MKTAG('s', 'n', 'd', 'D'):
151 timestamp = avio_rb32(s->pb);
152 size = avio_rb32(s->pb);
153 ret = av_get_packet(s->pb, pkt, size);
154 pkt->stream_index = sc->audio_stream_index;
155 pkt->pts = timestamp;
157 case MKTAG('v', 'i', 'd', 'D'):
158 timestamp = avio_rb32(s->pb);
159 size = avio_rb32(s->pb);
160 ret = av_get_packet(s->pb, pkt, size);
161 pkt->stream_index = sc->video_stream_index;
162 pkt->pts = timestamp;
164 case MKTAG('D', 'O', 'N', 'E'):
168 av_log(s, AV_LOG_ERROR, "unknown chunk %x\n", dtype);
169 ret = AVERROR_INVALIDDATA;
175 AVInputFormat ff_smjpeg_demuxer = {
177 .long_name = NULL_IF_CONFIG_SMALL("Loki SDL MJPEG"),
178 .priv_data_size = sizeof(SMJPEGContext),
179 .read_probe = smjpeg_probe,
180 .read_header = smjpeg_read_header,
181 .read_packet = smjpeg_read_packet,
182 .extensions = "mjpg",