FFmpeg  4.3.6
mpjpegdec.c
Go to the documentation of this file.
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 #include "libavutil/opt.h"
24 
25 #include "avformat.h"
26 #include "internal.h"
27 #include "avio_internal.h"
28 
29 
30 
31 typedef struct MPJPEGDemuxContext {
32  const AVClass *class;
33  char *boundary;
34  char *searchstr;
38 
39 
40 static void trim_right(char *p)
41 {
42  char *end;
43 
44  if (!p || !*p)
45  return;
46 
47  end = p + strlen(p);
48  while (end > p && av_isspace(*(end-1)))
49  *(--end) = '\0';
50 }
51 
52 static int get_line(AVIOContext *pb, char *line, int line_size)
53 {
54  ff_get_line(pb, line, line_size);
55 
56  if (pb->error)
57  return pb->error;
58 
59  if (pb->eof_reached)
60  return AVERROR_EOF;
61 
62  trim_right(line);
63  return 0;
64 }
65 
66 
67 
68 static int split_tag_value(char **tag, char **value, char *line)
69 {
70  char *p = line;
71  int foundData = 0;
72 
73  *tag = NULL;
74  *value = NULL;
75 
76 
77  while (*p != '\0' && *p != ':') {
78  if (!av_isspace(*p)) {
79  foundData = 1;
80  }
81  p++;
82  }
83  if (*p != ':')
84  return foundData ? AVERROR_INVALIDDATA : 0;
85 
86  *p = '\0';
87  *tag = line;
88  trim_right(*tag);
89 
90  p++;
91 
92  while (av_isspace(*p))
93  p++;
94 
95  *value = p;
96  trim_right(*value);
97 
98  return 0;
99 }
100 
101 static int parse_multipart_header(AVIOContext *pb,
102  int* size,
103  const char* expected_boundary,
104  void *log_ctx);
105 
107 {
108  MPJPEGDemuxContext *mpjpeg = s->priv_data;
109  av_freep(&mpjpeg->boundary);
110  av_freep(&mpjpeg->searchstr);
111  return 0;
112 }
113 
114 static int mpjpeg_read_probe(const AVProbeData *p)
115 {
116  AVIOContext pb;
117  int ret = 0;
118  int size = 0;
119 
120  if (p->buf_size < 2 || p->buf[0] != '-' || p->buf[1] != '-')
121  return 0;
122 
123  ffio_init_context(&pb, p->buf, p->buf_size, 0, NULL, NULL, NULL, NULL);
124 
125  ret = (parse_multipart_header(&pb, &size, "--", NULL) >= 0) ? AVPROBE_SCORE_MAX : 0;
126 
127  return ret;
128 }
129 
131 {
132  AVStream *st;
133  char boundary[70 + 2 + 1] = {0};
134  int64_t pos = avio_tell(s->pb);
135  int ret;
136 
137  do {
138  ret = get_line(s->pb, boundary, sizeof(boundary));
139  if (ret < 0)
140  return ret;
141  } while (!boundary[0]);
142 
143  if (strncmp(boundary, "--", 2))
144  return AVERROR_INVALIDDATA;
145 
146  st = avformat_new_stream(s, NULL);
147  if (!st)
148  return AVERROR(ENOMEM);
149 
152 
153  avpriv_set_pts_info(st, 60, 1, 25);
154 
155  avio_seek(s->pb, pos, SEEK_SET);
156 
157  return 0;
158 }
159 
160 static int parse_content_length(const char *value)
161 {
162  long int val = strtol(value, NULL, 10);
163 
164  if (val == LONG_MIN || val == LONG_MAX)
165  return AVERROR(errno);
166  if (val > INT_MAX)
167  return AVERROR(ERANGE);
168  return val;
169 }
170 
172  int* size,
173  const char* expected_boundary,
174  void *log_ctx)
175 {
176  char line[128];
177  int found_content_type = 0;
178  int ret;
179 
180  *size = -1;
181 
182  // get the CRLF as empty string
183  ret = get_line(pb, line, sizeof(line));
184  if (ret < 0)
185  return ret;
186 
187  /* some implementation do not provide the required
188  * initial CRLF (see rfc1341 7.2.1)
189  */
190  while (!line[0]) {
191  ret = get_line(pb, line, sizeof(line));
192  if (ret < 0)
193  return ret;
194  }
195 
196  if (!av_strstart(line, expected_boundary, NULL)) {
197  if (log_ctx)
198  av_log(log_ctx,
199  AV_LOG_ERROR,
200  "Expected boundary '%s' not found, instead found a line of %"SIZE_SPECIFIER" bytes\n",
201  expected_boundary,
202  strlen(line));
203 
204  return AVERROR_INVALIDDATA;
205  }
206 
207  while (!pb->eof_reached) {
208  char *tag, *value;
209 
210  ret = get_line(pb, line, sizeof(line));
211  if (ret < 0) {
212  if (ret == AVERROR_EOF)
213  break;
214  return ret;
215  }
216 
217  if (line[0] == '\0')
218  break;
219 
220  ret = split_tag_value(&tag, &value, line);
221  if (ret < 0)
222  return ret;
223  if (value==NULL || tag==NULL)
224  break;
225 
226  if (!av_strcasecmp(tag, "Content-type")) {
227  if (av_strcasecmp(value, "image/jpeg")) {
228  if (log_ctx)
229  av_log(log_ctx, AV_LOG_ERROR,
230  "Unexpected %s : %s\n",
231  tag, value);
232  return AVERROR_INVALIDDATA;
233  } else
234  found_content_type = 1;
235  } else if (!av_strcasecmp(tag, "Content-Length")) {
236  *size = parse_content_length(value);
237  if ( *size < 0 )
238  if (log_ctx)
239  av_log(log_ctx, AV_LOG_WARNING,
240  "Invalid Content-Length value : %s\n",
241  value);
242  }
243  }
244 
245  return found_content_type ? 0 : AVERROR_INVALIDDATA;
246 }
247 
248 
250 {
251  uint8_t *mime_type = NULL;
252  const char *start;
253  const char *end;
254  uint8_t *res = NULL;
255  int len;
256 
257  /* get MIME type, and skip to the first parameter */
258  av_opt_get(pb, "mime_type", AV_OPT_SEARCH_CHILDREN, &mime_type);
259  start = mime_type;
260  while (start != NULL && *start != '\0') {
261  start = strchr(start, ';');
262  if (!start)
263  break;
264 
265  start = start+1;
266 
267  while (av_isspace(*start))
268  start++;
269 
270  if (av_stristart(start, "boundary=", &start)) {
271  end = strchr(start, ';');
272  if (end)
273  len = end - start - 1;
274  else
275  len = strlen(start);
276 
277  /* some endpoints may enclose the boundary
278  in Content-Type in quotes */
279  if ( len>2 && *start == '"' && start[len-1] == '"' ) {
280  start++;
281  len -= 2;
282  }
283  res = av_strndup(start, len);
284  break;
285  }
286  }
287 
288  av_freep(&mime_type);
289  return res;
290 }
291 
292 
294 {
295  int size;
296  int ret;
297 
298  MPJPEGDemuxContext *mpjpeg = s->priv_data;
299  if (mpjpeg->boundary == NULL) {
300  uint8_t* boundary = NULL;
301  if (mpjpeg->strict_mime_boundary) {
302  boundary = mpjpeg_get_boundary(s->pb);
303  }
304  if (boundary != NULL) {
305  mpjpeg->boundary = av_asprintf("--%s", boundary);
306  mpjpeg->searchstr = av_asprintf("\r\n--%s\r\n", boundary);
307  av_freep(&boundary);
308  } else {
309  mpjpeg->boundary = av_strdup("--");
310  mpjpeg->searchstr = av_strdup("\r\n--");
311  }
312  if (!mpjpeg->boundary || !mpjpeg->searchstr) {
313  av_freep(&mpjpeg->boundary);
314  av_freep(&mpjpeg->searchstr);
315  return AVERROR(ENOMEM);
316  }
317  mpjpeg->searchstr_len = strlen(mpjpeg->searchstr);
318  }
319 
320  ret = parse_multipart_header(s->pb, &size, mpjpeg->boundary, s);
321 
322 
323  if (ret < 0)
324  return ret;
325 
326  if (size > 0) {
327  /* size has been provided to us in MIME header */
328  ret = av_get_packet(s->pb, pkt, size);
329  } else {
330  /* no size was given -- we read until the next boundary or end-of-file */
331  int remaining = 0, len;
332 
333  const int read_chunk = 2048;
334 
335  pkt->pos = avio_tell(s->pb);
336 
337  while ((ret = ffio_ensure_seekback(s->pb, read_chunk - remaining)) >= 0 && /* we may need to return as much as all we've read back to the buffer */
338  (ret = av_append_packet(s->pb, pkt, read_chunk - remaining)) >= 0) {
339  /* scan the new data */
340  char *start;
341 
342  len = ret + remaining;
343  start = pkt->data + pkt->size - len;
344  do {
345  if (!memcmp(start, mpjpeg->searchstr, mpjpeg->searchstr_len)) {
346  // got the boundary! rewind the stream
347  avio_seek(s->pb, -len, SEEK_CUR);
348  pkt->size -= len;
349  return pkt->size;
350  }
351  len--;
352  start++;
353  } while (len >= mpjpeg->searchstr_len);
354  remaining = len;
355  }
356 
357  /* error or EOF occurred */
358  if (ret == AVERROR_EOF) {
359  ret = pkt->size > 0 ? pkt->size : AVERROR_EOF;
360  }
361  }
362 
363  return ret;
364 }
365 
366 #define OFFSET(x) offsetof(MPJPEGDemuxContext, x)
367 
368 #define DEC AV_OPT_FLAG_DECODING_PARAM
369 static const AVOption mpjpeg_options[] = {
370  { "strict_mime_boundary", "require MIME boundaries match", OFFSET(strict_mime_boundary), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, DEC },
371  { NULL }
372 };
373 
374 
376  .class_name = "MPJPEG demuxer",
377  .item_name = av_default_item_name,
378  .option = mpjpeg_options,
379  .version = LIBAVUTIL_VERSION_INT,
380 };
381 
383  .name = "mpjpeg",
384  .long_name = NULL_IF_CONFIG_SMALL("MIME multipart JPEG"),
385  .mime_type = "multipart/x-mixed-replace",
386  .extensions = "mjpg",
387  .priv_data_size = sizeof(MPJPEGDemuxContext),
392  .priv_class = &mpjpeg_demuxer_class,
394 };
395 
396 
#define NULL
Definition: coverity.c:32
Bytestream IO Context.
Definition: avio.h:161
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
int size
AVOption.
Definition: opt.h:246
#define DEC
Definition: mpjpegdec.c:368
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
int64_t pos
byte position in stream, -1 if unknown
Definition: packet.h:375
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:4948
static int get_line(AVIOContext *pb, char *line, int line_size)
Definition: mpjpegdec.c:52
static int read_chunk(AVFormatContext *s)
Definition: dhav.c:166
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
int size
Definition: packet.h:356
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:241
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
static av_const int av_isspace(int c)
Locale-independent conversion of ASCII isspace.
Definition: avstring.h:222
static int split_tag_value(char **tag, char **value, char *line)
Definition: mpjpegdec.c:68
static AVPacket pkt
Format I/O context.
Definition: avformat.h:1351
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
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.
Definition: avstring.c:45
uint8_t
AVOptions.
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:92
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4526
uint8_t * data
Definition: packet.h:355
uint32_t tag
Definition: movenc.c:1532
#define AVERROR_EOF
End of file.
Definition: error.h:55
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition: utils.c:307
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:557
static int mpjpeg_read_header(AVFormatContext *s)
Definition: mpjpegdec.c:130
#define av_log(a,...)
int av_append_packet(AVIOContext *s, AVPacket *pkt, int size)
Read data and append it to the current content of the AVPacket.
Definition: utils.c:317
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
static int mpjpeg_read_close(AVFormatContext *s)
Definition: mpjpegdec.c:106
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:188
unsigned int pos
Definition: spdifenc.c:412
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:56
Definition: graph2dot.c:48
static const AVClass mpjpeg_demuxer_class
Definition: mpjpegdec.c:375
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:444
char * av_asprintf(const char *fmt,...)
Definition: avstring.c:113
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:443
int av_strcasecmp(const char *a, const char *b)
Locale-independent case-insensitive compare.
Definition: avstring.c:213
#define AV_OPT_SEARCH_CHILDREN
Search in possible children of the given object first.
Definition: opt.h:558
AVInputFormat ff_mpjpeg_demuxer
Definition: mpjpegdec.c:382
#define s(width, name)
Definition: cbs_vp9.c:257
int ff_get_line(AVIOContext *s, char *buf, int maxlen)
Read a whole line of text from AVIOContext.
Definition: aviobuf.c:786
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:545
Stream structure.
Definition: avformat.h:876
static int mpjpeg_read_probe(const AVProbeData *p)
Definition: mpjpegdec.c:114
static int parse_multipart_header(AVIOContext *pb, int *size, const char *expected_boundary, void *log_ctx)
Definition: mpjpegdec.c:171
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition: avformat.h:462
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
char * av_strdup(const char *s)
Duplicate a string.
Definition: mem.c:253
AVIOContext * pb
I/O context.
Definition: avformat.h:1393
double value
Definition: eval.c:98
Describe the class of an AVClass context structure.
Definition: log.h:67
#define OFFSET(x)
Definition: mpjpegdec.c:366
int error
contains the error code or 0 if no error happened
Definition: avio.h:245
This structure contains the data a format has to probe a file.
Definition: avformat.h:441
#define SIZE_SPECIFIER
Definition: internal.h:264
#define flags(name, subs,...)
Definition: cbs_av1.c:565
static void trim_right(char *p)
Definition: mpjpegdec.c:40
int strict_mime_boundary
Definition: mpjpegdec.c:36
static int read_probe(const AVProbeData *pd)
Definition: jvdec.c:55
static const AVOption mpjpeg_options[]
Definition: mpjpegdec.c:369
int ffio_ensure_seekback(AVIOContext *s, int64_t buf_size)
Ensures that the requested seekback buffer size will be available.
Definition: aviobuf.c:982
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:453
int av_strstart(const char *str, const char *pfx, const char **ptr)
Return non-zero if pfx is a prefix of str.
Definition: avstring.c:34
Main libavformat public API header.
int ffio_init_context(AVIOContext *s, 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))
Definition: aviobuf.c:76
static int mpjpeg_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: mpjpegdec.c:293
int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
Definition: opt.c:779
int eof_reached
true if was unable to read due to error or eof
Definition: avio.h:239
int len
void * priv_data
Format private data.
Definition: avformat.h:1379
#define av_freep(p)
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:650
static char * mpjpeg_get_boundary(AVIOContext *pb)
Definition: mpjpegdec.c:249
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1023
static int parse_content_length(const char *value)
Definition: mpjpegdec.c:160
static double val(void *priv, double ch)
Definition: aeval.c:76
This structure stores compressed data.
Definition: packet.h:332
char * av_strndup(const char *s, size_t len)
Duplicate a substring of a string.
Definition: mem.c:265