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.

389 lines
9.8KB

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