FFmpeg
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 "demux.h"
27 #include "internal.h"
28 #include "avio_internal.h"
29 
30 typedef struct MPJPEGDemuxContext {
31  const AVClass *class;
32  char *boundary;
33  char *searchstr;
37 
38 static void trim_right(char *p)
39 {
40  char *end;
41 
42  if (!p || !*p)
43  return;
44 
45  end = p + strlen(p);
46  while (end > p && av_isspace(*(end-1)))
47  *(--end) = '\0';
48 }
49 
50 static int get_line(AVIOContext *pb, char *line, int line_size)
51 {
52  ff_get_line(pb, line, line_size);
53 
54  if (pb->error)
55  return pb->error;
56 
57  if (pb->eof_reached)
58  return AVERROR_EOF;
59 
61  return 0;
62 }
63 
64 static int split_tag_value(char **tag, char **value, char *line)
65 {
66  char *p = line;
67  int foundData = 0;
68 
69  *tag = NULL;
70  *value = NULL;
71 
72  while (*p != '\0' && *p != ':') {
73  if (!av_isspace(*p)) {
74  foundData = 1;
75  }
76  p++;
77  }
78  if (*p != ':')
79  return foundData ? AVERROR_INVALIDDATA : 0;
80 
81  *p = '\0';
82  *tag = line;
83  trim_right(*tag);
84 
85  p++;
86 
87  while (av_isspace(*p))
88  p++;
89 
90  *value = p;
91  trim_right(*value);
92 
93  return 0;
94 }
95 
96 static int parse_multipart_header(AVIOContext *pb,
97  int* size,
98  const char* expected_boundary,
99  void *log_ctx);
100 
102 {
103  MPJPEGDemuxContext *mpjpeg = s->priv_data;
104  av_freep(&mpjpeg->boundary);
105  av_freep(&mpjpeg->searchstr);
106  return 0;
107 }
108 
109 static int mpjpeg_read_probe(const AVProbeData *p)
110 {
111  FFIOContext pb;
112  int ret = 0;
113  int size = 0;
114 
115  if (p->buf_size < 2 || p->buf[0] != '-' || p->buf[1] != '-')
116  return 0;
117 
118  ffio_init_read_context(&pb, p->buf, p->buf_size);
119 
120  ret = (parse_multipart_header(&pb.pub, &size, "--", NULL) >= 0) ? AVPROBE_SCORE_MAX : 0;
121 
122  return ret;
123 }
124 
126 {
127  AVStream *st;
128  char boundary[70 + 2 + 1] = {0};
129  int64_t pos = avio_tell(s->pb);
130  int ret;
131 
132  do {
133  ret = get_line(s->pb, boundary, sizeof(boundary));
134  if (ret < 0)
135  return ret;
136  } while (!boundary[0]);
137 
138  if (strncmp(boundary, "--", 2))
139  return AVERROR_INVALIDDATA;
140 
141  st = avformat_new_stream(s, NULL);
142  if (!st)
143  return AVERROR(ENOMEM);
144 
147 
148  avpriv_set_pts_info(st, 60, 1, 25);
149 
150  avio_seek(s->pb, pos, SEEK_SET);
151 
152  return 0;
153 }
154 
155 static int parse_content_length(const char *value)
156 {
157  long int val = strtol(value, NULL, 10);
158 
159  if (val == LONG_MIN || val == LONG_MAX)
160  return AVERROR(errno);
161  if (val > INT_MAX)
162  return AVERROR(ERANGE);
163  return val;
164 }
165 
167  int* size,
168  const char* expected_boundary,
169  void *log_ctx)
170 {
171  char line[128];
172  int found_content_type = 0;
173  int ret;
174 
175  *size = -1;
176 
177  // get the CRLF as empty string
178  ret = get_line(pb, line, sizeof(line));
179  if (ret < 0)
180  return ret;
181 
182  /* some implementation do not provide the required
183  * initial CRLF (see rfc1341 7.2.1)
184  */
185  while (!line[0]) {
186  ret = get_line(pb, line, sizeof(line));
187  if (ret < 0)
188  return ret;
189  }
190 
191  if (!av_strstart(line, expected_boundary, NULL)) {
192  if (log_ctx)
193  av_log(log_ctx,
194  AV_LOG_ERROR,
195  "Expected boundary '%s' not found, instead found a line of %"SIZE_SPECIFIER" bytes\n",
196  expected_boundary,
197  strlen(line));
198 
199  return AVERROR_INVALIDDATA;
200  }
201 
202  while (!pb->eof_reached) {
203  char *tag, *value;
204 
205  ret = get_line(pb, line, sizeof(line));
206  if (ret < 0) {
207  if (ret == AVERROR_EOF)
208  break;
209  return ret;
210  }
211 
212  if (line[0] == '\0')
213  break;
214 
216  if (ret < 0)
217  return ret;
218  if (value==NULL || tag==NULL)
219  break;
220 
221  if (!av_strcasecmp(tag, "Content-type")) {
222  if (av_strcasecmp(value, "image/jpeg")) {
223  if (log_ctx)
224  av_log(log_ctx, AV_LOG_ERROR,
225  "Unexpected %s : %s\n",
226  tag, value);
227  return AVERROR_INVALIDDATA;
228  } else
229  found_content_type = 1;
230  } else if (!av_strcasecmp(tag, "Content-Length")) {
232  if ( *size < 0 )
233  if (log_ctx)
234  av_log(log_ctx, AV_LOG_WARNING,
235  "Invalid Content-Length value : %s\n",
236  value);
237  }
238  }
239 
240  return found_content_type ? 0 : AVERROR_INVALIDDATA;
241 }
242 
244 {
245  uint8_t *mime_type = NULL;
246  const char *start;
247  const char *end;
248  uint8_t *res = NULL;
249  int len;
250 
251  /* get MIME type, and skip to the first parameter */
252  av_opt_get(pb, "mime_type", AV_OPT_SEARCH_CHILDREN, &mime_type);
253  start = mime_type;
254  while (start != NULL && *start != '\0') {
255  start = strchr(start, ';');
256  if (!start)
257  break;
258 
259  start = start+1;
260 
261  while (av_isspace(*start))
262  start++;
263 
264  if (av_stristart(start, "boundary=", &start)) {
265  end = strchr(start, ';');
266  if (end)
267  len = end - start - 1;
268  else
269  len = strlen(start);
270 
271  /* some endpoints may enclose the boundary
272  in Content-Type in quotes */
273  if ( len>2 && *start == '"' && start[len-1] == '"' ) {
274  start++;
275  len -= 2;
276  }
277  res = av_strndup(start, len);
278  break;
279  }
280  }
281 
282  av_freep(&mime_type);
283  return res;
284 }
285 
287 {
288  int size;
289  int ret;
290 
291  MPJPEGDemuxContext *mpjpeg = s->priv_data;
292  if (mpjpeg->boundary == NULL) {
293  uint8_t* boundary = NULL;
294  if (mpjpeg->strict_mime_boundary) {
295  boundary = mpjpeg_get_boundary(s->pb);
296  }
297  if (boundary != NULL) {
298  mpjpeg->boundary = av_asprintf("--%s", boundary);
299  mpjpeg->searchstr = av_asprintf("\r\n--%s\r\n", boundary);
300  av_freep(&boundary);
301  } else {
302  mpjpeg->boundary = av_strdup("--");
303  mpjpeg->searchstr = av_strdup("\r\n--");
304  }
305  if (!mpjpeg->boundary || !mpjpeg->searchstr) {
306  av_freep(&mpjpeg->boundary);
307  av_freep(&mpjpeg->searchstr);
308  return AVERROR(ENOMEM);
309  }
310  mpjpeg->searchstr_len = strlen(mpjpeg->searchstr);
311  }
312 
313  ret = parse_multipart_header(s->pb, &size, mpjpeg->boundary, s);
314  if (ret < 0)
315  return ret;
316 
317  if (size > 0) {
318  /* size has been provided to us in MIME header */
319  ret = av_get_packet(s->pb, pkt, size);
320  } else {
321  /* no size was given -- we read until the next boundary or end-of-file */
322  int len;
323 
324  const int read_chunk = 2048;
325 
326  pkt->pos = avio_tell(s->pb);
327 
328  while ((ret = ffio_ensure_seekback(s->pb, read_chunk)) >= 0 && /* we may need to return as much as all we've read back to the buffer */
329  (ret = av_append_packet(s->pb, pkt, read_chunk)) >= 0) {
330  /* scan the new data */
331  char *start;
332 
333  len = ret;
334  start = pkt->data + pkt->size - len;
335  do {
336  if (!memcmp(start, mpjpeg->searchstr, mpjpeg->searchstr_len)) {
337  // got the boundary! rewind the stream
338  avio_seek(s->pb, -len, SEEK_CUR);
339  pkt->size -= len;
340  return pkt->size;
341  }
342  len--;
343  start++;
344  } while (len >= mpjpeg->searchstr_len);
345  avio_seek(s->pb, -len, SEEK_CUR);
346  pkt->size -= len;
347  }
348 
349  /* error or EOF occurred */
350  if (ret == AVERROR_EOF) {
351  ret = pkt->size > 0 ? pkt->size : AVERROR_EOF;
352  }
353  }
354 
355  return ret;
356 }
357 
358 #define OFFSET(x) offsetof(MPJPEGDemuxContext, x)
359 #define DEC AV_OPT_FLAG_DECODING_PARAM
360 static const AVOption mpjpeg_options[] = {
361  { "strict_mime_boundary", "require MIME boundaries match", OFFSET(strict_mime_boundary), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, DEC },
362  { NULL }
363 };
364 
366  .class_name = "MPJPEG demuxer",
367  .item_name = av_default_item_name,
368  .option = mpjpeg_options,
369  .version = LIBAVUTIL_VERSION_INT,
370 };
371 
373  .p.name = "mpjpeg",
374  .p.long_name = NULL_IF_CONFIG_SMALL("MIME multipart JPEG"),
375  .p.mime_type = "multipart/x-mixed-replace",
376  .p.extensions = "mjpg",
377  .p.priv_class = &mpjpeg_demuxer_class,
378  .p.flags = AVFMT_NOTIMESTAMPS,
379  .priv_data_size = sizeof(MPJPEGDemuxContext),
384 };
AV_OPT_SEARCH_CHILDREN
#define AV_OPT_SEARCH_CHILDREN
Search in possible children of the given object first.
Definition: opt.h:522
MPJPEGDemuxContext::boundary
char * boundary
Definition: mpjpegdec.c:32
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:51
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
AVFMT_NOTIMESTAMPS
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition: avformat.h:479
av_asprintf
char * av_asprintf(const char *fmt,...)
Definition: avstring.c:115
av_strcasecmp
int av_strcasecmp(const char *a, const char *b)
Locale-independent case-insensitive compare.
Definition: avstring.c:207
av_isspace
static av_const int av_isspace(int c)
Locale-independent conversion of ASCII isspace.
Definition: avstring.h:218
AVPacket::data
uint8_t * data
Definition: packet.h:522
AVOption
AVOption.
Definition: opt.h:346
AVIOContext::error
int error
contains the error code or 0 if no error happened
Definition: avio.h:239
ffio_init_read_context
void ffio_init_read_context(FFIOContext *s, const uint8_t *buffer, int buffer_size)
Wrap a buffer in an AVIOContext for reading.
Definition: aviobuf.c:98
AVProbeData::buf_size
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:454
FFIOContext
Definition: avio_internal.h:28
DEC
#define DEC
Definition: mpjpegdec.c:359
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:463
avpriv_set_pts_info
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: avformat.c:853
read_close
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:143
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:494
val
static double val(void *priv, double ch)
Definition: aeval.c:78
OFFSET
#define OFFSET(x)
Definition: mpjpegdec.c:358
get_line
static int get_line(AVIOContext *pb, char *line, int line_size)
Definition: mpjpegdec.c:50
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_read_callback.c:41
mpjpeg_get_boundary
static char * mpjpeg_get_boundary(AVIOContext *pb)
Definition: mpjpegdec.c:243
s
#define s(width, name)
Definition: cbs_vp9.c:198
trim_right
static void trim_right(char *p)
Definition: mpjpegdec.c:38
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:553
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:453
read_chunk
static int read_chunk(AVFormatContext *s)
Definition: dhav.c:171
av_stristart
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:47
parse_multipart_header
static int parse_multipart_header(AVIOContext *pb, int *size, const char *expected_boundary, void *log_ctx)
Definition: mpjpegdec.c:166
MPJPEGDemuxContext::searchstr
char * searchstr
Definition: mpjpegdec.c:33
AVFormatContext
Format I/O context.
Definition: avformat.h:1255
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:766
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:550
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
MPJPEGDemuxContext::searchstr_len
int searchstr_len
Definition: mpjpegdec.c:34
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:451
ff_mpjpeg_demuxer
const FFInputFormat ff_mpjpeg_demuxer
Definition: mpjpegdec.c:372
mpjpeg_demuxer_class
static const AVClass mpjpeg_demuxer_class
Definition: mpjpegdec.c:365
AVIOContext
Bytestream IO Context.
Definition: avio.h:160
AVPacket::size
int size
Definition: packet.h:523
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:106
MPJPEGDemuxContext::strict_mime_boundary
int strict_mime_boundary
Definition: mpjpegdec.c:35
FFIOContext::pub
AVIOContext pub
Definition: avio_internal.h:29
size
int size
Definition: twinvq_data.h:10344
FFInputFormat::p
AVInputFormat p
The public AVInputFormat.
Definition: demux.h:41
ffio_ensure_seekback
int ffio_ensure_seekback(AVIOContext *s, int64_t buf_size)
Ensures that the requested seekback buffer size will be available.
Definition: aviobuf.c:1022
mpjpeg_read_probe
static int mpjpeg_read_probe(const AVProbeData *p)
Definition: mpjpegdec.c:109
line
Definition: graph2dot.c:48
mpjpeg_read_close
static int mpjpeg_read_close(AVFormatContext *s)
Definition: mpjpegdec.c:101
mpjpeg_read_packet
static int mpjpeg_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: mpjpegdec.c:286
mpjpeg_options
static const AVOption mpjpeg_options[]
Definition: mpjpegdec.c:360
av_strstart
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:36
MPJPEGDemuxContext
Definition: mpjpegdec.c:30
AV_CODEC_ID_MJPEG
@ AV_CODEC_ID_MJPEG
Definition: codec_id.h:59
avio_internal.h
value
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default value
Definition: writing_filters.txt:86
ff_get_line
int ff_get_line(AVIOContext *s, char *buf, int maxlen)
Read a whole line of text from AVIOContext.
Definition: aviobuf.c:768
demux.h
len
int len
Definition: vorbis_enc_data.h:426
split_tag_value
static int split_tag_value(char **tag, char **value, char *line)
Definition: mpjpegdec.c:64
parse_content_length
static int parse_content_length(const char *value)
Definition: mpjpegdec.c:155
av_get_packet
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:103
tag
uint32_t tag
Definition: movenc.c:1786
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:743
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:230
AVClass::class_name
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:71
av_append_packet
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:119
pos
unsigned int pos
Definition: spdifenc.c:413
avformat.h
SIZE_SPECIFIER
#define SIZE_SPECIFIER
Definition: internal.h:141
AVIOContext::eof_reached
int eof_reached
true if was unable to read due to error or eof
Definition: avio.h:238
mpjpeg_read_header
static int mpjpeg_read_header(AVFormatContext *s)
Definition: mpjpegdec.c:125
av_strdup
char * av_strdup(const char *s)
Duplicate a string.
Definition: mem.c:270
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
read_probe
static int read_probe(const AVProbeData *p)
Definition: cdg.c:30
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:55
AVPacket
This structure stores compressed data.
Definition: packet.h:499
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Definition: opt.h:251
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
AVPacket::pos
int64_t pos
byte position in stream, -1 if unknown
Definition: packet.h:542
FFInputFormat
Definition: demux.h:37
av_opt_get
int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
Definition: opt.c:1145
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
avstring.h
av_strndup
char * av_strndup(const char *s, size_t len)
Duplicate a substring of a string.
Definition: mem.c:282
line
The official guide to swscale for confused that consecutive non overlapping rectangles of slice_bottom special converter These generally are unscaled converters of common like for each output line the vertical scaler pulls lines from a ring buffer When the ring buffer does not contain the wanted line
Definition: swscale.txt:40