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.

402 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(const 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. ffio_init_context(&pb, p->buf, p->buf_size, 0, NULL, NULL, NULL, NULL);
  95. ret = (parse_multipart_header(&pb, &size, "--", NULL) >= 0) ? AVPROBE_SCORE_MAX : 0;
  96. return ret;
  97. }
  98. static int mpjpeg_read_header(AVFormatContext *s)
  99. {
  100. AVStream *st;
  101. char boundary[70 + 2 + 1] = {0};
  102. int64_t pos = avio_tell(s->pb);
  103. int ret;
  104. do {
  105. ret = get_line(s->pb, boundary, sizeof(boundary));
  106. if (ret < 0)
  107. return ret;
  108. } while (!boundary[0]);
  109. if (strncmp(boundary, "--", 2))
  110. return AVERROR_INVALIDDATA;
  111. st = avformat_new_stream(s, NULL);
  112. if (!st)
  113. return AVERROR(ENOMEM);
  114. st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  115. st->codecpar->codec_id = AV_CODEC_ID_MJPEG;
  116. avpriv_set_pts_info(st, 60, 1, 25);
  117. avio_seek(s->pb, pos, SEEK_SET);
  118. return 0;
  119. }
  120. static int parse_content_length(const char *value)
  121. {
  122. long int val = strtol(value, NULL, 10);
  123. if (val == LONG_MIN || val == LONG_MAX)
  124. return AVERROR(errno);
  125. if (val > INT_MAX)
  126. return AVERROR(ERANGE);
  127. return val;
  128. }
  129. static int parse_multipart_header(AVIOContext *pb,
  130. int* size,
  131. const char* expected_boundary,
  132. void *log_ctx)
  133. {
  134. char line[128];
  135. int found_content_type = 0;
  136. int ret;
  137. *size = -1;
  138. // get the CRLF as empty string
  139. ret = get_line(pb, line, sizeof(line));
  140. if (ret < 0)
  141. return ret;
  142. /* some implementation do not provide the required
  143. * initial CRLF (see rfc1341 7.2.1)
  144. */
  145. while (!line[0]) {
  146. ret = get_line(pb, line, sizeof(line));
  147. if (ret < 0)
  148. return ret;
  149. }
  150. if (!av_strstart(line, expected_boundary, NULL)) {
  151. if (log_ctx)
  152. av_log(log_ctx,
  153. AV_LOG_ERROR,
  154. "Expected boundary '%s' not found, instead found a line of %"SIZE_SPECIFIER" bytes\n",
  155. expected_boundary,
  156. strlen(line));
  157. return AVERROR_INVALIDDATA;
  158. }
  159. while (!pb->eof_reached) {
  160. char *tag, *value;
  161. ret = get_line(pb, line, sizeof(line));
  162. if (ret < 0) {
  163. if (ret == AVERROR_EOF)
  164. break;
  165. return ret;
  166. }
  167. if (line[0] == '\0')
  168. break;
  169. ret = split_tag_value(&tag, &value, line);
  170. if (ret < 0)
  171. return ret;
  172. if (value==NULL || tag==NULL)
  173. break;
  174. if (!av_strcasecmp(tag, "Content-type")) {
  175. if (av_strcasecmp(value, "image/jpeg")) {
  176. if (log_ctx)
  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. if (log_ctx)
  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. /* some endpoints may enclose the boundary
  218. in Content-Type in quotes */
  219. if ( len>2 && *start == '"' && start[len-1] == '"' ) {
  220. start++;
  221. len -= 2;
  222. }
  223. res = av_strndup(start, len);
  224. break;
  225. }
  226. }
  227. av_freep(&mime_type);
  228. return res;
  229. }
  230. static int mpjpeg_read_packet(AVFormatContext *s, AVPacket *pkt)
  231. {
  232. int size;
  233. int ret;
  234. MPJPEGDemuxContext *mpjpeg = s->priv_data;
  235. if (mpjpeg->boundary == NULL) {
  236. uint8_t* boundary = NULL;
  237. if (mpjpeg->strict_mime_boundary) {
  238. boundary = mpjpeg_get_boundary(s->pb);
  239. }
  240. if (boundary != NULL) {
  241. mpjpeg->boundary = boundary;
  242. mpjpeg->searchstr = av_asprintf( "\r\n%s\r\n", boundary );
  243. } else {
  244. mpjpeg->boundary = av_strdup("--");
  245. mpjpeg->searchstr = av_strdup("\r\n--");
  246. }
  247. if (!mpjpeg->boundary || !mpjpeg->searchstr) {
  248. av_freep(&mpjpeg->boundary);
  249. av_freep(&mpjpeg->searchstr);
  250. return AVERROR(ENOMEM);
  251. }
  252. mpjpeg->searchstr_len = strlen(mpjpeg->searchstr);
  253. }
  254. ret = parse_multipart_header(s->pb, &size, mpjpeg->boundary, s);
  255. if (ret < 0)
  256. return ret;
  257. if (size > 0) {
  258. /* size has been provided to us in MIME header */
  259. ret = av_get_packet(s->pb, pkt, size);
  260. } else {
  261. /* no size was given -- we read until the next boundary or end-of-file */
  262. int remaining = 0, len;
  263. const int read_chunk = 2048;
  264. av_init_packet(pkt);
  265. pkt->data = NULL;
  266. pkt->size = 0;
  267. pkt->pos = avio_tell(s->pb);
  268. /* we may need to return as much as all we've read back to the buffer */
  269. ffio_ensure_seekback(s->pb, read_chunk);
  270. while ((ret = av_append_packet(s->pb, pkt, read_chunk - remaining)) >= 0) {
  271. /* scan the new data */
  272. char *start;
  273. len = ret + remaining;
  274. start = pkt->data + pkt->size - len;
  275. do {
  276. if (!memcmp(start, mpjpeg->searchstr, mpjpeg->searchstr_len)) {
  277. // got the boundary! rewind the stream
  278. avio_seek(s->pb, -len, SEEK_CUR);
  279. pkt->size -= len;
  280. return pkt->size;
  281. }
  282. len--;
  283. start++;
  284. } while (len >= mpjpeg->searchstr_len);
  285. remaining = len;
  286. }
  287. /* error or EOF occurred */
  288. if (ret == AVERROR_EOF) {
  289. ret = pkt->size > 0 ? pkt->size : AVERROR_EOF;
  290. } else {
  291. av_packet_unref(pkt);
  292. }
  293. }
  294. return ret;
  295. }
  296. #define OFFSET(x) offsetof(MPJPEGDemuxContext, x)
  297. #define DEC AV_OPT_FLAG_DECODING_PARAM
  298. static const AVOption mpjpeg_options[] = {
  299. { "strict_mime_boundary", "require MIME boundaries match", OFFSET(strict_mime_boundary), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, DEC },
  300. { NULL }
  301. };
  302. static const AVClass mpjpeg_demuxer_class = {
  303. .class_name = "MPJPEG demuxer",
  304. .item_name = av_default_item_name,
  305. .option = mpjpeg_options,
  306. .version = LIBAVUTIL_VERSION_INT,
  307. };
  308. AVInputFormat ff_mpjpeg_demuxer = {
  309. .name = "mpjpeg",
  310. .long_name = NULL_IF_CONFIG_SMALL("MIME multipart JPEG"),
  311. .mime_type = "multipart/x-mixed-replace",
  312. .extensions = "mjpg",
  313. .priv_data_size = sizeof(MPJPEGDemuxContext),
  314. .read_probe = mpjpeg_read_probe,
  315. .read_header = mpjpeg_read_header,
  316. .read_packet = mpjpeg_read_packet,
  317. .read_close = mpjpeg_read_close,
  318. .priv_class = &mpjpeg_demuxer_class,
  319. .flags = AVFMT_NOTIMESTAMPS,
  320. };