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
24
28
29
30
38
39
41 {
43
44 if (!p || !*p)
45 return;
46
47 end = p + strlen(p);
50 }
51
53 {
55
58
61
63 return 0;
64 }
65
66
67
69 {
70 char *p = line;
71 int foundData = 0;
72
75
76
77 while (*p != '0円' && *p != ':') {
79 foundData = 1;
80 }
81 p++;
82 }
83 if (*p != ':')
85
86 *p = '0円';
87 *tag = line;
89
90 p++;
91
93 p++;
94
95 *value = p;
97
98 return 0;
99 }
100
103 const char* expected_boundary,
104 void *log_ctx);
105
107 {
111 return 0;
112 }
113
115 {
117 int ret = 0;
119
121 return 0;
122
124 if (!pb)
125 return 0;
126
128
130
131 return ret;
132 }
133
135 {
137 char boundary[70 + 2 + 1] = {0};
139 int ret;
140
141 do {
142 ret =
get_line(s->
pb, boundary,
sizeof(boundary));
143 if (ret < 0)
144 return ret;
145 } while (!boundary[0]);
146
147 if (strncmp(boundary, "--", 2))
149
151 if (!st)
153
156
158
160
161 return 0;
162 }
163
165 {
166 long int val = strtol(value,
NULL, 10);
167
168 if (val == LONG_MIN || val == LONG_MAX)
170 if (val > INT_MAX)
173 }
174
177 const char* expected_boundary,
178 void *log_ctx)
179 {
181 int found_content_type = 0;
182 int ret;
183
184 *size = -1;
185
186 // get the CRLF as empty string
187 ret =
get_line(pb, line,
sizeof(line));
188 if (ret < 0)
189 return ret;
190
191 /* some implementation do not provide the required
192 * initial CRLF (see rfc1341 7.2.1)
193 */
194 while (!line[0]) {
195 ret =
get_line(pb, line,
sizeof(line));
196 if (ret < 0)
197 return ret;
198 }
199
201 if (log_ctx)
204 "Expected boundary '%s' not found, instead found a line of %zu bytes\n",
205 expected_boundary,
206 strlen(line));
207
209 }
210
213
214 ret =
get_line(pb, line,
sizeof(line));
215 if (ret < 0) {
217 break;
218 return ret;
219 }
220
221 if (line[0] == '0円')
222 break;
223
225 if (ret < 0)
226 return ret;
228 break;
229
232 if (log_ctx)
234 "Unexpected %s : %s\n",
235 tag, value);
237 } else
238 found_content_type = 1;
241 if ( *size < 0 )
242 if (log_ctx)
244 "Invalid Content-Length value : %s\n",
245 value);
246 }
247 }
248
250 }
251
252
254 {
260
261 /* get MIME type, and skip to the first parameter */
263 start = mime_type;
264 while (start !=
NULL && *start !=
'0円') {
265 start = strchr(start, ';');
266 if (!start)
267 break;
268
269 start = start+1;
270
272 start++;
273
275 end = strchr(start, ';');
276 if (end)
277 len = end - start - 1;
278 else
279 len = strlen(start);
280
281 /* some endpoints may enclose the boundary
282 in Content-Type in quotes */
283 if ( len>2 && *start == '"' && start[len-1] == '"' ) {
284 start++;
285 len -= 2;
286 }
288 break;
289 }
290 }
291
293 return res;
294 }
295
296
298 {
300 int ret;
301
307 }
308 if (boundary !=
NULL) {
311 } else {
314 }
319 }
321 }
322
324
325
326 if (ret < 0)
327 return ret;
328
329 if (size > 0) {
330 /* size has been provided to us in MIME header */
332 } else {
333 /* no size was given -- we read until the next boundary or end-of-file */
334 int remaining = 0,
len;
335
336 const int read_chunk = 2048;
341
342 /* we may need to return as much as all we've read back to the buffer */
344
346 /* scan the new data */
348
349 len = ret + remaining;
351 do {
353 // got the boundary! rewind the stream
357 }
359 start++;
362 }
363
364 /* error or EOF occurred */
367 } else {
369 }
370 }
371
372 return ret;
373 }
374
375 #define OFFSET(x) offsetof(MPJPEGDemuxContext, x)
376
377 #define DEC AV_OPT_FLAG_DECODING_PARAM
379 {
"strict_mime_boundary",
"require MIME boundaries match",
OFFSET(strict_mime_boundary),
AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1,
DEC },
381 };
382
383
389 };
390
394 .mime_type = "multipart/x-mixed-replace",
395 .extensions = "mjpg",
401 .priv_class = &mpjpeg_demuxer_class
402 };
403
404
const char const char void * val
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
#define AV_LOG_WARNING
Something somehow does not look correct.
#define LIBAVUTIL_VERSION_INT
int64_t pos
byte position in stream, -1 if unknown
static int get_line(AVIOContext *pb, char *line, int line_size)
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
static av_const int av_isspace(int c)
Locale-independent conversion of ASCII isspace.
static int split_tag_value(char **tag, char **value, char *line)
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
int av_stristart(const char *str, const char *pfx, const char **ptr)
Return non-zero if pfx is a prefix of str independent of case.
static av_cold int end(AVCodecContext *avctx)
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
char * av_strndup(const char *s, size_t len)
Duplicate a substring of the string s.
#define AVERROR_EOF
End of file.
static av_cold int read_close(AVFormatContext *ctx)
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
static int mpjpeg_read_header(AVFormatContext *s)
AVIOContext * avio_alloc_context(unsigned char *buffer, int buffer_size, int write_flag, void *opaque, int(*read_packet)(void *opaque, uint8_t *buf, int buf_size), int(*write_packet)(void *opaque, uint8_t *buf, int buf_size), int64_t(*seek)(void *opaque, int64_t offset, int whence))
Allocate and initialize an AVIOContext for buffered I/O.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
static int mpjpeg_read_close(AVFormatContext *s)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
static const AVClass mpjpeg_demuxer_class
AVCodecContext * codec
Codec context associated with this stream.
int buf_size
Size of buf except extra allocated bytes.
char * av_asprintf(const char *fmt,...)
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
int av_strcasecmp(const char *a, const char *b)
Locale-independent case-insensitive compare.
#define AV_OPT_SEARCH_CHILDREN
Search in possible children of the given object first.
GLsizei GLboolean const GLfloat * value
AVInputFormat ff_mpjpeg_demuxer
static int mpjpeg_read_probe(AVProbeData *p)
int ff_get_line(AVIOContext *s, char *buf, int maxlen)
Read a whole line of text from AVIOContext.
static int read_header(FFV1Context *f)
static int parse_multipart_header(AVIOContext *pb, int *size, const char *expected_boundary, void *log_ctx)
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
enum AVMediaType codec_type
char * av_strdup(const char *s)
Duplicate the string s.
AVIOContext * pb
I/O context.
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Describe the class of an AVClass context structure.
int error
contains the error code or 0 if no error happened
This structure contains the data a format has to probe a file.
static void trim_right(char *p)
const AVOption mpjpeg_options[]
int ffio_ensure_seekback(AVIOContext *s, int64_t buf_size)
Ensures that the requested seekback buffer size will be available.
int av_strstart(const char *str, const char *pfx, const char **ptr)
Return non-zero if pfx is a prefix of str.
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
static int mpjpeg_read_packet(AVFormatContext *s, AVPacket *pkt)
int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
int eof_reached
true if eof reached
void * priv_data
Format private data.
static char * mpjpeg_get_boundary(AVIOContext *pb)
static int parse_content_length(const char *value)
This structure stores compressed data.