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.

323 lines
8.1KB

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