2 * Multipart JPEG format
3 * Copyright (c) 2015 Luca Barbato
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
22 #include "libavutil/avstring.h"
27 static int get_line(AVIOContext *pb, char *line, int line_size)
29 int i = ff_get_line(pb, line, line_size);
31 if (i > 1 && line[i - 2] == '\r')
44 static void trim_right(char* p)
50 while (end!=p && av_isspace(*end)) {
56 static int split_tag_value(char **tag, char **value, char *line)
60 while (*p != '\0' && *p != ':')
63 return AVERROR_INVALIDDATA;
71 while (av_isspace(*p))
80 static int check_content_type(char *line)
83 int ret = split_tag_value(&tag, &value, line);
88 if (av_strcasecmp(tag, "Content-type") ||
89 av_strcasecmp(value, "image/jpeg"))
90 return AVERROR_INVALIDDATA;
95 static int parse_multipart_header(AVIOContext *pb, void *log_ctx);
97 static int mpjpeg_read_probe(AVProbeData *p)
100 char line[128] = { 0 };
103 if (p->buf_size < 2 || p->buf[0] != '-' || p->buf[1] != '-')
106 pb = avio_alloc_context(p->buf, p->buf_size, 0, NULL, NULL, NULL, NULL);
108 return AVERROR(ENOMEM);
110 ret = (parse_multipart_header(pb, NULL)>0)?AVPROBE_SCORE_MAX:0;
117 static int mpjpeg_read_header(AVFormatContext *s)
120 char boundary[70 + 2 + 1];
121 int64_t pos = avio_tell(s->pb);
125 ret = get_line(s->pb, boundary, sizeof(boundary));
129 if (strncmp(boundary, "--", 2))
130 return AVERROR_INVALIDDATA;
132 st = avformat_new_stream(s, NULL);
134 return AVERROR(ENOMEM);
136 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
137 st->codec->codec_id = AV_CODEC_ID_MJPEG;
139 avpriv_set_pts_info(st, 60, 1, 25);
141 avio_seek(s->pb, pos, SEEK_SET);
146 static int parse_content_length(const char *value)
148 long int val = strtol(value, NULL, 10);
150 if (val == LONG_MIN || val == LONG_MAX)
151 return AVERROR(errno);
153 return AVERROR(ERANGE);
157 static int parse_multipart_header(AVIOContext *pb, void *log_ctx)
160 int found_content_type = 0;
163 ret = get_line(pb, line, sizeof(line));
167 if (strncmp(line, "--", 2))
168 return AVERROR_INVALIDDATA;
170 while (!pb->eof_reached) {
173 ret = get_line(pb, line, sizeof(line));
175 if (ret==AVERROR_EOF)
183 ret = split_tag_value(&tag, &value, line);
187 if (!av_strcasecmp(tag, "Content-type")) {
188 if (av_strcasecmp(value, "image/jpeg")) {
190 av_log(log_ctx, AV_LOG_ERROR,
191 "Unexpected %s : %s\n",
195 return AVERROR_INVALIDDATA;
197 found_content_type = 1;
198 } else if (!av_strcasecmp(tag, "Content-Length")) {
199 size = parse_content_length(value);
205 if (!found_content_type || size < 0) {
206 return AVERROR_INVALIDDATA;
212 static int mpjpeg_read_packet(AVFormatContext *s, AVPacket *pkt)
215 int size = parse_multipart_header(s->pb, s);
220 ret = av_get_packet(s->pb, pkt, size);
224 // trailing empty line
230 AVInputFormat ff_mpjpeg_demuxer = {
232 .long_name = NULL_IF_CONFIG_SMALL("MIME multipart JPEG"),
233 .mime_type = "multipart/x-mixed-replace",
234 .extensions = "mjpg",
235 .read_probe = mpjpeg_read_probe,
236 .read_header = mpjpeg_read_header,
237 .read_packet = mpjpeg_read_packet,