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.

406 lines
10KB

  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. avio_context_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->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  118. st->codecpar->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. if (log_ctx)
  155. av_log(log_ctx,
  156. AV_LOG_ERROR,
  157. "Expected boundary '%s' not found, instead found a line of %"SIZE_SPECIFIER" bytes\n",
  158. expected_boundary,
  159. strlen(line));
  160. return AVERROR_INVALIDDATA;
  161. }
  162. while (!pb->eof_reached) {
  163. char *tag, *value;
  164. ret = get_line(pb, line, sizeof(line));
  165. if (ret < 0) {
  166. if (ret == AVERROR_EOF)
  167. break;
  168. return ret;
  169. }
  170. if (line[0] == '\0')
  171. break;
  172. ret = split_tag_value(&tag, &value, line);
  173. if (ret < 0)
  174. return ret;
  175. if (value==NULL || tag==NULL)
  176. break;
  177. if (!av_strcasecmp(tag, "Content-type")) {
  178. if (av_strcasecmp(value, "image/jpeg")) {
  179. if (log_ctx)
  180. av_log(log_ctx, AV_LOG_ERROR,
  181. "Unexpected %s : %s\n",
  182. tag, value);
  183. return AVERROR_INVALIDDATA;
  184. } else
  185. found_content_type = 1;
  186. } else if (!av_strcasecmp(tag, "Content-Length")) {
  187. *size = parse_content_length(value);
  188. if ( *size < 0 )
  189. if (log_ctx)
  190. av_log(log_ctx, AV_LOG_WARNING,
  191. "Invalid Content-Length value : %s\n",
  192. value);
  193. }
  194. }
  195. return found_content_type ? 0 : AVERROR_INVALIDDATA;
  196. }
  197. static char* mpjpeg_get_boundary(AVIOContext* pb)
  198. {
  199. uint8_t *mime_type = NULL;
  200. const char *start;
  201. const char *end;
  202. uint8_t *res = NULL;
  203. int len;
  204. /* get MIME type, and skip to the first parameter */
  205. av_opt_get(pb, "mime_type", AV_OPT_SEARCH_CHILDREN, &mime_type);
  206. start = mime_type;
  207. while (start != NULL && *start != '\0') {
  208. start = strchr(start, ';');
  209. if (!start)
  210. break;
  211. start = start+1;
  212. while (av_isspace(*start))
  213. start++;
  214. if (!av_stristart(start, "boundary=", &start)) {
  215. end = strchr(start, ';');
  216. if (end)
  217. len = end - start - 1;
  218. else
  219. len = strlen(start);
  220. /* some endpoints may enclose the boundary
  221. in Content-Type in quotes */
  222. if ( len>2 && *start == '"' && start[len-1] == '"' ) {
  223. start++;
  224. len -= 2;
  225. }
  226. res = av_strndup(start, len);
  227. break;
  228. }
  229. }
  230. av_freep(&mime_type);
  231. return res;
  232. }
  233. static int mpjpeg_read_packet(AVFormatContext *s, AVPacket *pkt)
  234. {
  235. int size;
  236. int ret;
  237. MPJPEGDemuxContext *mpjpeg = s->priv_data;
  238. if (mpjpeg->boundary == NULL) {
  239. uint8_t* boundary = NULL;
  240. if (mpjpeg->strict_mime_boundary) {
  241. boundary = mpjpeg_get_boundary(s->pb);
  242. }
  243. if (boundary != NULL) {
  244. mpjpeg->boundary = boundary;
  245. mpjpeg->searchstr = av_asprintf( "\r\n%s\r\n", boundary );
  246. } else {
  247. mpjpeg->boundary = av_strdup("--");
  248. mpjpeg->searchstr = av_strdup("\r\n--");
  249. }
  250. if (!mpjpeg->boundary || !mpjpeg->searchstr) {
  251. av_freep(&mpjpeg->boundary);
  252. av_freep(&mpjpeg->searchstr);
  253. return AVERROR(ENOMEM);
  254. }
  255. mpjpeg->searchstr_len = strlen(mpjpeg->searchstr);
  256. }
  257. ret = parse_multipart_header(s->pb, &size, mpjpeg->boundary, s);
  258. if (ret < 0)
  259. return ret;
  260. if (size > 0) {
  261. /* size has been provided to us in MIME header */
  262. ret = av_get_packet(s->pb, pkt, size);
  263. } else {
  264. /* no size was given -- we read until the next boundary or end-of-file */
  265. int remaining = 0, len;
  266. const int read_chunk = 2048;
  267. av_init_packet(pkt);
  268. pkt->data = NULL;
  269. pkt->size = 0;
  270. pkt->pos = avio_tell(s->pb);
  271. /* we may need to return as much as all we've read back to the buffer */
  272. ffio_ensure_seekback(s->pb, read_chunk);
  273. while ((ret = av_append_packet(s->pb, pkt, read_chunk - remaining)) >= 0) {
  274. /* scan the new data */
  275. char *start;
  276. len = ret + remaining;
  277. start = pkt->data + pkt->size - len;
  278. do {
  279. if (!memcmp(start, mpjpeg->searchstr, mpjpeg->searchstr_len)) {
  280. // got the boundary! rewind the stream
  281. avio_seek(s->pb, -len, SEEK_CUR);
  282. pkt->size -= len;
  283. return pkt->size;
  284. }
  285. len--;
  286. start++;
  287. } while (len >= mpjpeg->searchstr_len);
  288. remaining = len;
  289. }
  290. /* error or EOF occurred */
  291. if (ret == AVERROR_EOF) {
  292. ret = pkt->size > 0 ? pkt->size : AVERROR_EOF;
  293. } else {
  294. av_packet_unref(pkt);
  295. }
  296. }
  297. return ret;
  298. }
  299. #define OFFSET(x) offsetof(MPJPEGDemuxContext, x)
  300. #define DEC AV_OPT_FLAG_DECODING_PARAM
  301. static const AVOption mpjpeg_options[] = {
  302. { "strict_mime_boundary", "require MIME boundaries match", OFFSET(strict_mime_boundary), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, DEC },
  303. { NULL }
  304. };
  305. static const AVClass mpjpeg_demuxer_class = {
  306. .class_name = "MPJPEG demuxer",
  307. .item_name = av_default_item_name,
  308. .option = mpjpeg_options,
  309. .version = LIBAVUTIL_VERSION_INT,
  310. };
  311. AVInputFormat ff_mpjpeg_demuxer = {
  312. .name = "mpjpeg",
  313. .long_name = NULL_IF_CONFIG_SMALL("MIME multipart JPEG"),
  314. .mime_type = "multipart/x-mixed-replace",
  315. .extensions = "mjpg",
  316. .priv_data_size = sizeof(MPJPEGDemuxContext),
  317. .read_probe = mpjpeg_read_probe,
  318. .read_header = mpjpeg_read_header,
  319. .read_packet = mpjpeg_read_packet,
  320. .read_close = mpjpeg_read_close,
  321. .priv_class = &mpjpeg_demuxer_class,
  322. .flags = AVFMT_NOTIMESTAMPS,
  323. };