]> git.sesse.net Git - ffmpeg/blob - libavformat/mpjpegdec.c
Merge commit '3efd71b4d0b4a73ccbbbdc092e6bbd54d92633f4'
[ffmpeg] / libavformat / mpjpegdec.c
1 /*
2  * Multipart JPEG format
3  * Copyright (c) 2015 Luca Barbato
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 #include "libavutil/avstring.h"
23
24 #include "avformat.h"
25 #include "internal.h"
26
27 static int get_line(AVIOContext *pb, char *line, int line_size)
28 {
29     int i = ff_get_line(pb, line, line_size);
30
31     if (i > 1 && line[i - 2] == '\r')
32         line[i - 2] = '\0';
33
34     if (pb->error)
35         return pb->error;
36
37     if (pb->eof_reached)
38         return AVERROR_EOF;
39
40     return 0;
41 }
42
43
44 static void trim_right(char* p)
45 {
46     char *end;
47     if (!p || !*p)
48         return;
49     end = p + strlen(p) - 1;
50     while (end != p && av_isspace(*end)) {
51         *end = '\0';
52         end--;
53     }
54 }
55
56 static int split_tag_value(char **tag, char **value, char *line)
57 {
58     char *p = line;
59     int  foundData = 0;
60
61     *tag = NULL;
62     *value = NULL;
63
64
65     while (*p != '\0' && *p != ':') {
66         if (!av_isspace(*p)) {
67             foundData = 1;
68         }
69         p++;
70     }
71     if (*p != ':')
72         return foundData ? AVERROR_INVALIDDATA : 0;
73
74     *p   = '\0';
75     *tag = line;
76     trim_right(*tag);
77
78     p++;
79
80     while (av_isspace(*p))
81         p++;
82
83     *value = p;
84     trim_right(*value);
85
86     return 0;
87 }
88
89 static int parse_multipart_header(AVIOContext *pb, void *log_ctx);
90
91 static int mpjpeg_read_probe(AVProbeData *p)
92 {
93     AVIOContext *pb;
94     int ret = 0;
95
96     if (p->buf_size < 2 || p->buf[0] != '-' || p->buf[1] != '-')
97         return 0;
98
99     pb = avio_alloc_context(p->buf, p->buf_size, 0, NULL, NULL, NULL, NULL);
100     if (!pb)
101         return 0;
102
103     ret = (parse_multipart_header(pb, NULL)>0)?AVPROBE_SCORE_MAX:0;
104
105     av_free(pb);
106
107     return ret;
108 }
109
110 static int mpjpeg_read_header(AVFormatContext *s)
111 {
112     AVStream *st;
113     char boundary[70 + 2 + 1];
114     int64_t pos = avio_tell(s->pb);
115     int ret;
116
117
118     ret = get_line(s->pb, boundary, sizeof(boundary));
119     if (ret < 0)
120         return ret;
121
122     if (strncmp(boundary, "--", 2))
123         return AVERROR_INVALIDDATA;
124
125     st = avformat_new_stream(s, NULL);
126     if (!st)
127         return AVERROR(ENOMEM);
128
129     st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
130     st->codec->codec_id   = AV_CODEC_ID_MJPEG;
131
132     avpriv_set_pts_info(st, 60, 1, 25);
133
134     avio_seek(s->pb, pos, SEEK_SET);
135
136     return 0;
137 }
138
139 static int parse_content_length(const char *value)
140 {
141     long int val = strtol(value, NULL, 10);
142
143     if (val == LONG_MIN || val == LONG_MAX)
144         return AVERROR(errno);
145     if (val > INT_MAX)
146         return AVERROR(ERANGE);
147     return val;
148 }
149
150 static int parse_multipart_header(AVIOContext *pb, void *log_ctx)
151 {
152     char line[128];
153     int found_content_type = 0;
154     int ret, size = -1;
155
156     // get the CRLF as empty string
157     ret = get_line(pb, line, sizeof(line));
158     if (ret < 0)
159         return ret;
160
161     /* some implementation do not provide the required
162      * initial CRLF (see rfc1341 7.2.1)
163      */
164     if (!line[0]) {
165         ret = get_line(pb, line, sizeof(line));
166         if (ret < 0)
167             return ret;
168     }
169
170     if (strncmp(line, "--", 2))
171         return AVERROR_INVALIDDATA;
172
173     while (!pb->eof_reached) {
174         char *tag, *value;
175
176         ret = get_line(pb, line, sizeof(line));
177         if (ret < 0) {
178             if (ret == AVERROR_EOF)
179                 break;
180             return ret;
181         }
182
183         if (line[0] == '\0')
184             break;
185
186         ret = split_tag_value(&tag, &value, line);
187         if (ret < 0)
188             return ret;
189         if (value==NULL || tag==NULL)
190             break;
191
192         if (!av_strcasecmp(tag, "Content-type")) {
193             if (av_strcasecmp(value, "image/jpeg")) {
194                 if (log_ctx) {
195                     av_log(log_ctx, AV_LOG_ERROR,
196                            "Unexpected %s : %s\n",
197                            tag, value);
198                 }
199
200                 return AVERROR_INVALIDDATA;
201             } else
202                 found_content_type = 1;
203         } else if (!av_strcasecmp(tag, "Content-Length")) {
204             size = parse_content_length(value);
205             if (size < 0)
206                 return size;
207         }
208     }
209
210     if (!found_content_type || size < 0) {
211         return AVERROR_INVALIDDATA;
212     }
213
214     return size;
215 }
216
217 static int mpjpeg_read_packet(AVFormatContext *s, AVPacket *pkt)
218 {
219     int ret;
220     int size = parse_multipart_header(s->pb, s);
221
222     if (size < 0)
223         return size;
224
225     ret = av_get_packet(s->pb, pkt, size);
226     if (ret < 0)
227         return ret;
228
229     return 0;
230 }
231
232 AVInputFormat ff_mpjpeg_demuxer = {
233     .name              = "mpjpeg",
234     .long_name         = NULL_IF_CONFIG_SMALL("MIME multipart JPEG"),
235     .mime_type         = "multipart/x-mixed-replace",
236     .extensions        = "mjpg",
237     .read_probe        = mpjpeg_read_probe,
238     .read_header       = mpjpeg_read_header,
239     .read_packet       = mpjpeg_read_packet,
240 };