• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * SMJPEG demuxer
3  * Copyright (c) 2011 Paul B Mahol
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  * @file
24  * This is a demuxer for Loki SDL Motion JPEG files
25  */
26 
27 #include <inttypes.h>
28 
29 #include "avformat.h"
30 #include "internal.h"
31 #include "riff.h"
32 #include "smjpeg.h"
33 
34 typedef struct SMJPEGContext {
35     int audio_stream_index;
36     int video_stream_index;
37 } SMJPEGContext;
38 
smjpeg_probe(const AVProbeData * p)39 static int smjpeg_probe(const AVProbeData *p)
40 {
41     if (!memcmp(p->buf, SMJPEG_MAGIC, 8))
42         return AVPROBE_SCORE_MAX;
43     return 0;
44 }
45 
smjpeg_read_header(AVFormatContext * s)46 static int smjpeg_read_header(AVFormatContext *s)
47 {
48     SMJPEGContext *sc = s->priv_data;
49     AVStream *ast = NULL, *vst = NULL;
50     AVIOContext *pb = s->pb;
51     uint32_t version, htype, hlength, duration;
52     char *comment;
53 
54     sc->audio_stream_index =
55     sc->video_stream_index = -1;
56 
57     avio_skip(pb, 8); // magic
58     version = avio_rb32(pb);
59     if (version)
60         avpriv_request_sample(s, "Unknown version %"PRIu32, version);
61 
62     duration = avio_rb32(pb); // in msec
63 
64     while (!avio_feof(pb)) {
65         htype = avio_rl32(pb);
66         switch (htype) {
67         case SMJPEG_TXT:
68             hlength = avio_rb32(pb);
69             if (!hlength || hlength > 512)
70                 return AVERROR_INVALIDDATA;
71             comment = av_malloc(hlength + 1);
72             if (!comment)
73                 return AVERROR(ENOMEM);
74             if (avio_read(pb, comment, hlength) != hlength) {
75                 av_freep(&comment);
76                 av_log(s, AV_LOG_ERROR, "error when reading comment\n");
77                 return AVERROR_INVALIDDATA;
78             }
79             comment[hlength] = 0;
80             av_dict_set(&s->metadata, "comment", comment,
81                         AV_DICT_DONT_STRDUP_VAL);
82             break;
83         case SMJPEG_SND:
84             if (ast) {
85                 avpriv_request_sample(s, "Multiple audio streams");
86                 return AVERROR_PATCHWELCOME;
87             }
88             hlength = avio_rb32(pb);
89             if (hlength < 8)
90                 return AVERROR_INVALIDDATA;
91             ast = avformat_new_stream(s, 0);
92             if (!ast)
93                 return AVERROR(ENOMEM);
94             ast->codecpar->codec_type  = AVMEDIA_TYPE_AUDIO;
95             ast->codecpar->sample_rate = avio_rb16(pb);
96             ast->codecpar->bits_per_coded_sample = avio_r8(pb);
97             ast->codecpar->channels    = avio_r8(pb);
98             ast->codecpar->codec_tag   = avio_rl32(pb);
99             ast->codecpar->codec_id    = ff_codec_get_id(ff_codec_smjpeg_audio_tags,
100                                                          ast->codecpar->codec_tag);
101             ast->duration           = duration;
102             sc->audio_stream_index  = ast->index;
103             avpriv_set_pts_info(ast, 32, 1, 1000);
104             avio_skip(pb, hlength - 8);
105             break;
106         case SMJPEG_VID:
107             if (vst) {
108                 avpriv_request_sample(s, "Multiple video streams");
109                 return AVERROR_INVALIDDATA;
110             }
111             hlength = avio_rb32(pb);
112             if (hlength < 12)
113                 return AVERROR_INVALIDDATA;
114             vst = avformat_new_stream(s, 0);
115             if (!vst)
116                 return AVERROR(ENOMEM);
117             vst->nb_frames            = avio_rb32(pb);
118             vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
119             vst->codecpar->width      = avio_rb16(pb);
120             vst->codecpar->height     = avio_rb16(pb);
121             vst->codecpar->codec_tag  = avio_rl32(pb);
122             vst->codecpar->codec_id   = ff_codec_get_id(ff_codec_smjpeg_video_tags,
123                                                         vst->codecpar->codec_tag);
124             vst->duration          = duration;
125             sc->video_stream_index = vst->index;
126             avpriv_set_pts_info(vst, 32, 1, 1000);
127             avio_skip(pb, hlength - 12);
128             break;
129         case SMJPEG_HEND:
130             return 0;
131         default:
132             av_log(s, AV_LOG_ERROR, "unknown header %"PRIx32"\n", htype);
133             return AVERROR_INVALIDDATA;
134         }
135     }
136 
137     return AVERROR_EOF;
138 }
139 
smjpeg_read_packet(AVFormatContext * s,AVPacket * pkt)140 static int smjpeg_read_packet(AVFormatContext *s, AVPacket *pkt)
141 {
142     SMJPEGContext *sc = s->priv_data;
143     uint32_t dtype, size, timestamp;
144     int64_t pos;
145     int ret;
146 
147     if (avio_feof(s->pb))
148         return AVERROR_EOF;
149     pos   = avio_tell(s->pb);
150     dtype = avio_rl32(s->pb);
151     switch (dtype) {
152     case SMJPEG_SNDD:
153         if (sc->audio_stream_index < 0)
154             return AVERROR_INVALIDDATA;
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;
160         pkt->pos = pos;
161         break;
162     case SMJPEG_VIDD:
163         if (sc->video_stream_index < 0)
164             return AVERROR_INVALIDDATA;
165         timestamp = avio_rb32(s->pb);
166         size = avio_rb32(s->pb);
167         ret = av_get_packet(s->pb, pkt, size);
168         pkt->stream_index = sc->video_stream_index;
169         pkt->pts = timestamp;
170         pkt->pos = pos;
171         break;
172     case SMJPEG_DONE:
173         ret = AVERROR_EOF;
174         break;
175     default:
176         av_log(s, AV_LOG_ERROR, "unknown chunk %"PRIx32"\n", dtype);
177         ret = AVERROR_INVALIDDATA;
178         break;
179     }
180     return ret;
181 }
182 
183 AVInputFormat ff_smjpeg_demuxer = {
184     .name           = "smjpeg",
185     .long_name      = NULL_IF_CONFIG_SMALL("Loki SDL MJPEG"),
186     .priv_data_size = sizeof(SMJPEGContext),
187     .read_probe     = smjpeg_probe,
188     .read_header    = smjpeg_read_header,
189     .read_packet    = smjpeg_read_packet,
190     .extensions     = "mjpg",
191     .flags          = AVFMT_GENERIC_INDEX,
192 };
193