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.

239 lines
5.3KB

  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. static int get_line(AVIOContext *pb, char *line, int line_size)
  25. {
  26. int i = ff_get_line(pb, line, line_size);
  27. if (i > 1 && line[i - 2] == '\r')
  28. line[i - 2] = '\0';
  29. if (pb->error)
  30. return pb->error;
  31. if (pb->eof_reached)
  32. return AVERROR_EOF;
  33. return 0;
  34. }
  35. static void trim_right(char* p)
  36. {
  37. char *end;
  38. if (!p || !*p)
  39. return;
  40. end = p + strlen(p) - 1;
  41. while (end != p && av_isspace(*end)) {
  42. *end = '\0';
  43. end--;
  44. }
  45. }
  46. static int split_tag_value(char **tag, char **value, char *line)
  47. {
  48. char *p = line;
  49. while (*p != '\0' && *p != ':')
  50. p++;
  51. if (*p != ':')
  52. return AVERROR_INVALIDDATA;
  53. *p = '\0';
  54. *tag = line;
  55. trim_right(*tag);
  56. p++;
  57. while (av_isspace(*p))
  58. p++;
  59. *value = p;
  60. trim_right(*value);
  61. return 0;
  62. }
  63. static int check_content_type(char *line)
  64. {
  65. char *tag, *value;
  66. int ret = split_tag_value(&tag, &value, line);
  67. if (ret < 0)
  68. return ret;
  69. if (av_strcasecmp(tag, "Content-type") ||
  70. av_strcasecmp(value, "image/jpeg"))
  71. return AVERROR_INVALIDDATA;
  72. return 0;
  73. }
  74. static int parse_multipart_header(AVIOContext *pb, void *log_ctx);
  75. static int mpjpeg_read_probe(AVProbeData *p)
  76. {
  77. AVIOContext *pb;
  78. char line[128] = { 0 };
  79. int ret = 0;
  80. if (p->buf_size < 2 || p->buf[0] != '-' || p->buf[1] != '-')
  81. return 0;
  82. pb = avio_alloc_context(p->buf, p->buf_size, 0, NULL, NULL, NULL, NULL);
  83. if (!pb)
  84. return AVERROR(ENOMEM);
  85. ret = (parse_multipart_header(pb, NULL)>0)?AVPROBE_SCORE_MAX:0;
  86. av_free(pb);
  87. return ret;
  88. }
  89. static int mpjpeg_read_header(AVFormatContext *s)
  90. {
  91. AVStream *st;
  92. char boundary[70 + 2 + 1];
  93. int64_t pos = avio_tell(s->pb);
  94. int ret;
  95. ret = get_line(s->pb, boundary, sizeof(boundary));
  96. if (ret < 0)
  97. return ret;
  98. if (strncmp(boundary, "--", 2))
  99. return AVERROR_INVALIDDATA;
  100. st = avformat_new_stream(s, NULL);
  101. if (!st)
  102. return AVERROR(ENOMEM);
  103. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  104. st->codec->codec_id = AV_CODEC_ID_MJPEG;
  105. avpriv_set_pts_info(st, 60, 1, 25);
  106. avio_seek(s->pb, pos, SEEK_SET);
  107. return 0;
  108. }
  109. static int parse_content_length(const char *value)
  110. {
  111. long int val = strtol(value, NULL, 10);
  112. if (val == LONG_MIN || val == LONG_MAX)
  113. return AVERROR(errno);
  114. if (val > INT_MAX)
  115. return AVERROR(ERANGE);
  116. return val;
  117. }
  118. static int parse_multipart_header(AVIOContext *pb, void *log_ctx)
  119. {
  120. char line[128];
  121. int found_content_type = 0;
  122. int ret, size = -1;
  123. ret = get_line(pb, line, sizeof(line));
  124. if (ret < 0)
  125. return ret;
  126. if (strncmp(line, "--", 2))
  127. return AVERROR_INVALIDDATA;
  128. while (!pb->eof_reached) {
  129. char *tag, *value;
  130. ret = get_line(pb, line, sizeof(line));
  131. if (ret < 0) {
  132. if (ret == AVERROR_EOF)
  133. break;
  134. return ret;
  135. }
  136. if (line[0] == '\0')
  137. break;
  138. ret = split_tag_value(&tag, &value, line);
  139. if (ret < 0)
  140. return ret;
  141. if (!av_strcasecmp(tag, "Content-type")) {
  142. if (av_strcasecmp(value, "image/jpeg")) {
  143. if (log_ctx) {
  144. av_log(log_ctx, AV_LOG_ERROR,
  145. "Unexpected %s : %s\n",
  146. tag, value);
  147. }
  148. return AVERROR_INVALIDDATA;
  149. } else
  150. found_content_type = 1;
  151. } else if (!av_strcasecmp(tag, "Content-Length")) {
  152. size = parse_content_length(value);
  153. if (size < 0)
  154. return size;
  155. }
  156. }
  157. if (!found_content_type || size < 0) {
  158. return AVERROR_INVALIDDATA;
  159. }
  160. return size;
  161. }
  162. static int mpjpeg_read_packet(AVFormatContext *s, AVPacket *pkt)
  163. {
  164. int ret;
  165. int size = parse_multipart_header(s->pb, s);
  166. if (size < 0)
  167. return size;
  168. ret = av_get_packet(s->pb, pkt, size);
  169. if (ret < 0)
  170. return ret;
  171. // trailing empty line
  172. avio_skip(s->pb, 2);
  173. return 0;
  174. }
  175. AVInputFormat ff_mpjpeg_demuxer = {
  176. .name = "mpjpeg",
  177. .long_name = NULL_IF_CONFIG_SMALL("MIME multipart JPEG"),
  178. .mime_type = "multipart/x-mixed-replace",
  179. .extensions = "mjpg",
  180. .read_probe = mpjpeg_read_probe,
  181. .read_header = mpjpeg_read_header,
  182. .read_packet = mpjpeg_read_packet,
  183. };