You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

395 lines
9.9KB

  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. #include "libavutil/avstring.h"
  22. #include "libavutil/opt.h"
  23. #include "avformat.h"
  24. #include "internal.h"
  25. #include "avio_internal.h"
  26. typedef struct MPJPEGDemuxContext {
  27. const AVClass *class;
  28. char *boundary;
  29. char *searchstr;
  30. int searchstr_len;
  31. int strict_mime_boundary;
  32. } MPJPEGDemuxContext;
  33. static void trim_right(char *p)
  34. {
  35. char *end;
  36. if (!p || !*p)
  37. return;
  38. end = p + strlen(p);
  39. while (end > p && av_isspace(*(end-1)))
  40. *(--end) = '\0';
  41. }
  42. static int get_line(AVIOContext *pb, char *line, int line_size)
  43. {
  44. ff_get_line(pb, line, line_size);
  45. if (pb->error)
  46. return pb->error;
  47. if (pb->eof_reached)
  48. return AVERROR_EOF;
  49. trim_right(line);
  50. return 0;
  51. }
  52. static int split_tag_value(char **tag, char **value, char *line)
  53. {
  54. char *p = line;
  55. int foundData = 0;
  56. *tag = NULL;
  57. *value = NULL;
  58. while (*p != '\0' && *p != ':') {
  59. if (!av_isspace(*p)) {
  60. foundData = 1;
  61. }
  62. p++;
  63. }
  64. if (*p != ':')
  65. return foundData ? AVERROR_INVALIDDATA : 0;
  66. *p = '\0';
  67. *tag = line;
  68. trim_right(*tag);
  69. p++;
  70. while (av_isspace(*p))
  71. p++;
  72. *value = p;
  73. trim_right(*value);
  74. return 0;
  75. }
  76. static int parse_multipart_header(AVIOContext *pb,
  77. int* size,
  78. const char* expected_boundary,
  79. void *log_ctx);
  80. static int mpjpeg_read_close(AVFormatContext *s)
  81. {
  82. MPJPEGDemuxContext *mpjpeg = s->priv_data;
  83. av_freep(&mpjpeg->boundary);
  84. av_freep(&mpjpeg->searchstr);
  85. return 0;
  86. }
  87. static int mpjpeg_read_probe(AVProbeData *p)
  88. {
  89. AVIOContext *pb;
  90. int ret = 0;
  91. int size = 0;
  92. if (p->buf_size < 2 || p->buf[0] != '-' || p->buf[1] != '-')
  93. return 0;
  94. pb = avio_alloc_context(p->buf, p->buf_size, 0, NULL, NULL, NULL, NULL);
  95. if (!pb)
  96. return 0;
  97. ret = (parse_multipart_header(pb, &size, "--", NULL) > 0) ? AVPROBE_SCORE_MAX : 0;
  98. av_free(pb);
  99. return ret;
  100. }
  101. static int mpjpeg_read_header(AVFormatContext *s)
  102. {
  103. AVStream *st;
  104. char boundary[70 + 2 + 1] = {0};
  105. int64_t pos = avio_tell(s->pb);
  106. int ret;
  107. do {
  108. ret = get_line(s->pb, boundary, sizeof(boundary));
  109. if (ret < 0)
  110. return ret;
  111. } while (!boundary[0]);
  112. if (strncmp(boundary, "--", 2))
  113. return AVERROR_INVALIDDATA;
  114. st = avformat_new_stream(s, NULL);
  115. if (!st)
  116. return AVERROR(ENOMEM);
  117. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  118. st->codec->codec_id = AV_CODEC_ID_MJPEG;
  119. avpriv_set_pts_info(st, 60, 1, 25);
  120. avio_seek(s->pb, pos, SEEK_SET);
  121. return 0;
  122. }
  123. static int parse_content_length(const char *value)
  124. {
  125. long int val = strtol(value, NULL, 10);
  126. if (val == LONG_MIN || val == LONG_MAX)
  127. return AVERROR(errno);
  128. if (val > INT_MAX)
  129. return AVERROR(ERANGE);
  130. return val;
  131. }
  132. static int parse_multipart_header(AVIOContext *pb,
  133. int* size,
  134. const char* expected_boundary,
  135. void *log_ctx)
  136. {
  137. char line[128];
  138. int found_content_type = 0;
  139. int ret;
  140. *size = -1;
  141. // get the CRLF as empty string
  142. ret = get_line(pb, line, sizeof(line));
  143. if (ret < 0)
  144. return ret;
  145. /* some implementation do not provide the required
  146. * initial CRLF (see rfc1341 7.2.1)
  147. */
  148. while (!line[0]) {
  149. ret = get_line(pb, line, sizeof(line));
  150. if (ret < 0)
  151. return ret;
  152. }
  153. if (!av_strstart(line, expected_boundary, NULL)) {
  154. av_log(log_ctx,
  155. AV_LOG_ERROR,
  156. "Expected boundary '%s' not found, instead found a line of %zu bytes\n",
  157. expected_boundary,
  158. strlen(line));
  159. return AVERROR_INVALIDDATA;
  160. }
  161. while (!pb->eof_reached) {
  162. char *tag, *value;
  163. ret = get_line(pb, line, sizeof(line));
  164. if (ret < 0) {
  165. if (ret == AVERROR_EOF)
  166. break;
  167. return ret;
  168. }
  169. if (line[0] == '\0')
  170. break;
  171. ret = split_tag_value(&tag, &value, line);
  172. if (ret < 0)
  173. return ret;
  174. if (value==NULL || tag==NULL)
  175. break;
  176. if (!av_strcasecmp(tag, "Content-type")) {
  177. if (av_strcasecmp(value, "image/jpeg")) {
  178. av_log(log_ctx, AV_LOG_ERROR,
  179. "Unexpected %s : %s\n",
  180. tag, value);
  181. return AVERROR_INVALIDDATA;
  182. } else
  183. found_content_type = 1;
  184. } else if (!av_strcasecmp(tag, "Content-Length")) {
  185. *size = parse_content_length(value);
  186. if ( *size < 0 )
  187. av_log(log_ctx, AV_LOG_WARNING,
  188. "Invalid Content-Length value : %s\n",
  189. value);
  190. }
  191. }
  192. return found_content_type ? 0 : AVERROR_INVALIDDATA;
  193. }
  194. static char* mpjpeg_get_boundary(AVIOContext* pb)
  195. {
  196. uint8_t *mime_type = NULL;
  197. const char *start;
  198. const char *end;
  199. uint8_t *res = NULL;
  200. int len;
  201. /* get MIME type, and skip to the first parameter */
  202. av_opt_get(pb, "mime_type", AV_OPT_SEARCH_CHILDREN, &mime_type);
  203. start = mime_type;
  204. while (start != NULL && *start != '\0') {
  205. start = strchr(start, ';');
  206. if (!start)
  207. break;
  208. start = start+1;
  209. while (av_isspace(*start))
  210. start++;
  211. if (!av_stristart(start, "boundary=", &start)) {
  212. end = strchr(start, ';');
  213. if (end)
  214. len = end - start - 1;
  215. else
  216. len = strlen(start);
  217. res = av_strndup(start, len);
  218. break;
  219. }
  220. }
  221. av_freep(&mime_type);
  222. return res;
  223. }
  224. static int mpjpeg_read_packet(AVFormatContext *s, AVPacket *pkt)
  225. {
  226. int size;
  227. int ret;
  228. MPJPEGDemuxContext *mpjpeg = s->priv_data;
  229. if (mpjpeg->boundary == NULL) {
  230. uint8_t* boundary = NULL;
  231. if (mpjpeg->strict_mime_boundary) {
  232. boundary = mpjpeg_get_boundary(s->pb);
  233. }
  234. if (boundary != NULL) {
  235. mpjpeg->boundary = boundary;
  236. mpjpeg->searchstr = av_asprintf( "\r\n%s\r\n", boundary );
  237. } else {
  238. mpjpeg->boundary = av_strdup("--");
  239. mpjpeg->searchstr = av_strdup("\r\n--");
  240. }
  241. if (!mpjpeg->boundary || !mpjpeg->searchstr) {
  242. av_freep(&mpjpeg->boundary);
  243. av_freep(&mpjpeg->searchstr);
  244. return AVERROR(ENOMEM);
  245. }
  246. mpjpeg->searchstr_len = strlen(mpjpeg->searchstr);
  247. }
  248. ret = parse_multipart_header(s->pb, &size, mpjpeg->boundary, s);
  249. if (ret < 0)
  250. return ret;
  251. if (size > 0) {
  252. /* size has been provided to us in MIME header */
  253. ret = av_get_packet(s->pb, pkt, size);
  254. } else {
  255. /* no size was given -- we read until the next boundary or end-of-file */
  256. int remaining = 0, len;
  257. const int read_chunk = 2048;
  258. av_init_packet(pkt);
  259. pkt->data = NULL;
  260. pkt->size = 0;
  261. pkt->pos = avio_tell(s->pb);
  262. /* we may need to return as much as all we've read back to the buffer */
  263. ffio_ensure_seekback(s->pb, read_chunk);
  264. while ((ret = av_append_packet(s->pb, pkt, read_chunk - remaining)) >= 0) {
  265. /* scan the new data */
  266. char *start;
  267. len = ret + remaining;
  268. start = pkt->data + pkt->size - len;
  269. do {
  270. if (!memcmp(start, mpjpeg->searchstr, mpjpeg->searchstr_len)) {
  271. // got the boundary! rewind the stream
  272. avio_seek(s->pb, -(len-2), SEEK_CUR);
  273. pkt->size -= (len-2);
  274. return pkt->size;
  275. }
  276. len--;
  277. start++;
  278. } while (len >= mpjpeg->searchstr_len);
  279. remaining = len;
  280. }
  281. /* error or EOF occurred */
  282. if (ret == AVERROR_EOF) {
  283. ret = pkt->size > 0 ? pkt->size : AVERROR_EOF;
  284. } else {
  285. av_packet_unref(pkt);
  286. }
  287. }
  288. return ret;
  289. }
  290. #define OFFSET(x) offsetof(MPJPEGDemuxContext, x)
  291. #define DEC AV_OPT_FLAG_DECODING_PARAM
  292. const AVOption mpjpeg_options[] = {
  293. { "strict_mime_boundary", "require MIME boundaries match", OFFSET(strict_mime_boundary), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, DEC },
  294. { NULL }
  295. };
  296. static const AVClass mpjpeg_demuxer_class = {
  297. .class_name = "MPJPEG demuxer",
  298. .item_name = av_default_item_name,
  299. .option = mpjpeg_options,
  300. .version = LIBAVUTIL_VERSION_INT,
  301. };
  302. AVInputFormat ff_mpjpeg_demuxer = {
  303. .name = "mpjpeg",
  304. .long_name = NULL_IF_CONFIG_SMALL("MIME multipart JPEG"),
  305. .mime_type = "multipart/x-mixed-replace",
  306. .extensions = "mjpg",
  307. .priv_data_size = sizeof(MPJPEGDemuxContext),
  308. .read_probe = mpjpeg_read_probe,
  309. .read_header = mpjpeg_read_header,
  310. .read_packet = mpjpeg_read_packet,
  311. .read_close = mpjpeg_read_close,
  312. .priv_class = &mpjpeg_demuxer_class
  313. };