mpjpegdec.c 5.3 KB
Newer Older
L
Luca Barbato 已提交
1 2 3 4
/*
 * Multipart JPEG format
 * Copyright (c) 2015 Luca Barbato
 *
5
 * This file is part of FFmpeg.
L
Luca Barbato 已提交
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
L
Luca Barbato 已提交
8 9 10 11
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
L
Luca Barbato 已提交
13 14 15 16 17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
L
Luca Barbato 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include "libavutil/avstring.h"

#include "avformat.h"
#include "internal.h"

static int get_line(AVIOContext *pb, char *line, int line_size)
{
    int i = ff_get_line(pb, line, line_size);

    if (i > 1 && line[i - 2] == '\r')
        line[i - 2] = '\0';

    if (pb->error)
        return pb->error;

    if (pb->eof_reached)
        return AVERROR_EOF;

    return 0;
}

43 44 45 46 47 48 49 50 51 52 53 54 55

static void trim_right(char* p)
{
    char* end;
    if (!p || !*p)
        return;
    end=p+strlen(p)-1;
    while (end!=p && av_isspace(*end)) {
        *end='\0';
        end--;
    }
}

L
Luca Barbato 已提交
56 57 58 59 60 61 62 63 64 65 66
static int split_tag_value(char **tag, char **value, char *line)
{
    char *p = line;

    while (*p != '\0' && *p != ':')
        p++;
    if (*p != ':')
        return AVERROR_INVALIDDATA;

    *p   = '\0';
    *tag = line;
67
    trim_right(*tag);
L
Luca Barbato 已提交
68 69 70 71 72 73 74

    p++;

    while (av_isspace(*p))
        p++;

    *value = p;
75
    trim_right(*value);
L
Luca Barbato 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

    return 0;
}

static int check_content_type(char *line)
{
    char *tag, *value;
    int ret = split_tag_value(&tag, &value, line);

    if (ret < 0)
        return ret;

    if (av_strcasecmp(tag, "Content-type") ||
        av_strcasecmp(value, "image/jpeg"))
        return AVERROR_INVALIDDATA;

    return 0;
}

95 96
static int parse_multipart_header(AVIOContext *pb, void *log_ctx);

L
Luca Barbato 已提交
97 98 99 100 101 102
static int mpjpeg_read_probe(AVProbeData *p)
{
    AVIOContext *pb;
    char line[128] = { 0 };
    int ret = 0;

103 104 105
    if (p->buf_size < 2 || p->buf[0] != '-' || p->buf[1] != '-')
        return 0;

L
Luca Barbato 已提交
106 107 108 109
    pb = avio_alloc_context(p->buf, p->buf_size, 0, NULL, NULL, NULL, NULL);
    if (!pb)
        return AVERROR(ENOMEM);

110
    ret = (parse_multipart_header(pb, NULL)>0)?AVPROBE_SCORE_MAX:0;
111

L
Luca Barbato 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
    av_free(pb);

    return ret;
}

static int mpjpeg_read_header(AVFormatContext *s)
{
    AVStream *st;
    char boundary[70 + 2 + 1];
    int64_t pos = avio_tell(s->pb);
    int ret;


    ret = get_line(s->pb, boundary, sizeof(boundary));
    if (ret < 0)
        return ret;

    if (strncmp(boundary, "--", 2))
        return AVERROR_INVALIDDATA;

    st = avformat_new_stream(s, NULL);
L
Luca Barbato 已提交
133 134
    if (!st)
        return AVERROR(ENOMEM);
L
Luca Barbato 已提交
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156

    st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
    st->codec->codec_id   = AV_CODEC_ID_MJPEG;

    avpriv_set_pts_info(st, 60, 1, 25);

    avio_seek(s->pb, pos, SEEK_SET);

    return 0;
}

static int parse_content_length(const char *value)
{
    long int val = strtol(value, NULL, 10);

    if (val == LONG_MIN || val == LONG_MAX)
        return AVERROR(errno);
    if (val > INT_MAX)
        return AVERROR(ERANGE);
    return val;
}

157
static int parse_multipart_header(AVIOContext *pb, void *log_ctx)
L
Luca Barbato 已提交
158 159 160 161 162
{
    char line[128];
    int found_content_type = 0;
    int ret, size = -1;

163
    ret = get_line(pb, line, sizeof(line));
L
Luca Barbato 已提交
164 165 166 167 168 169
    if (ret < 0)
        return ret;

    if (strncmp(line, "--", 2))
        return AVERROR_INVALIDDATA;

170
    while (!pb->eof_reached) {
L
Luca Barbato 已提交
171 172
        char *tag, *value;

173
        ret = get_line(pb, line, sizeof(line));
174 175 176
        if (ret < 0) {
            if (ret==AVERROR_EOF)
                break;
L
Luca Barbato 已提交
177
            return ret;
178
        }
L
Luca Barbato 已提交
179 180 181 182 183 184 185 186 187 188

        if (line[0] == '\0')
            break;

        ret = split_tag_value(&tag, &value, line);
        if (ret < 0)
            return ret;

        if (!av_strcasecmp(tag, "Content-type")) {
            if (av_strcasecmp(value, "image/jpeg")) {
189 190 191 192 193 194
                if (log_ctx) {
                    av_log(log_ctx, AV_LOG_ERROR,
                           "Unexpected %s : %s\n",
                           tag, value);
                }

L
Luca Barbato 已提交
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
                return AVERROR_INVALIDDATA;
            } else
                found_content_type = 1;
        } else if (!av_strcasecmp(tag, "Content-Length")) {
            size = parse_content_length(value);
            if (size < 0)
                return size;
        }
    }

    if (!found_content_type || size < 0) {
        return AVERROR_INVALIDDATA;
    }

    return size;
}

static int mpjpeg_read_packet(AVFormatContext *s, AVPacket *pkt)
{
    int ret;
215
    int size = parse_multipart_header(s->pb, s);
L
Luca Barbato 已提交
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238

    if (size < 0)
        return size;

    ret = av_get_packet(s->pb, pkt, size);
    if (ret < 0)
        return ret;

    // trailing empty line
    avio_skip(s->pb, 2);

    return 0;
}

AVInputFormat ff_mpjpeg_demuxer = {
    .name              = "mpjpeg",
    .long_name         = NULL_IF_CONFIG_SMALL("MIME multipart JPEG"),
    .mime_type         = "multipart/x-mixed-replace",
    .extensions        = "mjpg",
    .read_probe        = mpjpeg_read_probe,
    .read_header       = mpjpeg_read_header,
    .read_packet       = mpjpeg_read_packet,
};