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.

225 lines
5.0KB

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