rtpdec_xiph.c 13.1 KB
Newer Older
1
/*
2
 * Xiph RTP Protocols
3
 * Copyright (c) 2009 Colin McQuillian
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 * Copyright (c) 2010 Josh Allmann
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * 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.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * 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
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/**
24
 * @file
25
 * @brief Xiph / RTP Code
26
 * @author Colin McQuillan <m.niloc@gmail.com>
27 28 29
 * @author Josh Allmann <joshua.allmann@gmail.com>
 */

30
#include "libavutil/attributes.h"
31
#include "libavutil/avassert.h"
32 33 34 35
#include "libavutil/avstring.h"
#include "libavutil/base64.h"
#include "libavcodec/bytestream.h"

36
#include "internal.h"
37
#include "rtpdec.h"
38
#include "rtpdec_formats.h"
39 40

/**
41
 * RTP/Xiph specific private data.
42 43 44 45
 */
struct PayloadContext {
    unsigned ident;             ///< 24-bit stream configuration identifier
    uint32_t timestamp;
46
    AVIOContext* fragment;    ///< buffer for split payloads
47 48 49
    uint8_t *split_buf;
    int split_pos, split_buf_len, split_buf_size;
    int split_pkts;
50 51
};

52
static PayloadContext *xiph_new_context(void)
53 54 55 56 57 58 59 60
{
    return av_mallocz(sizeof(PayloadContext));
}

static inline void free_fragment_if_needed(PayloadContext * data)
{
    if (data->fragment) {
        uint8_t* p;
61
        avio_close_dyn_buf(data->fragment, &p);
62 63 64 65 66
        av_free(p);
        data->fragment = NULL;
    }
}

67
static void xiph_free_context(PayloadContext * data)
68 69
{
    free_fragment_if_needed(data);
70
    av_free(data->split_buf);
71 72 73
    av_free(data);
}

74 75
static av_cold int xiph_vorbis_init(AVFormatContext *ctx, int st_index,
                                    PayloadContext *data)
76 77 78 79 80 81 82 83
{
    if (st_index < 0)
        return 0;
    ctx->streams[st_index]->need_parsing = AVSTREAM_PARSE_HEADERS;
    return 0;
}


84 85 86 87
static int xiph_handle_packet(AVFormatContext *ctx, PayloadContext *data,
                              AVStream *st, AVPacket *pkt, uint32_t *timestamp,
                              const uint8_t *buf, int len, uint16_t seq,
                              int flags)
88 89 90 91
{

    int ident, fragmented, tdt, num_pkts, pkt_len;

92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
    if (!buf) {
        if (!data->split_buf || data->split_pos + 2 > data->split_buf_len ||
            data->split_pkts <= 0) {
            av_log(ctx, AV_LOG_ERROR, "No more data to return\n");
            return AVERROR_INVALIDDATA;
        }
        pkt_len = AV_RB16(data->split_buf + data->split_pos);
        data->split_pos += 2;
        if (data->split_pos + pkt_len > data->split_buf_len) {
            av_log(ctx, AV_LOG_ERROR, "Not enough data to return\n");
            return AVERROR_INVALIDDATA;
        }
        if (av_new_packet(pkt, pkt_len)) {
            av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
            return AVERROR(ENOMEM);
        }
        pkt->stream_index = st->index;
        memcpy(pkt->data, data->split_buf + data->split_pos, pkt_len);
        data->split_pos += pkt_len;
        data->split_pkts--;
        return data->split_pkts > 0;
    }

115 116 117 118 119
    if (len < 6) {
        av_log(ctx, AV_LOG_ERROR, "Invalid %d byte packet\n", len);
        return AVERROR_INVALIDDATA;
    }

120
    // read xiph rtp headers
121 122 123
    ident       = AV_RB24(buf);
    fragmented  = buf[3] >> 6;
    tdt         = (buf[3] >> 4) & 3;
124
    num_pkts    = buf[3] & 0xf;
125 126 127 128 129 130 131 132 133 134 135
    pkt_len     = AV_RB16(buf + 4);

    if (pkt_len > len - 6) {
        av_log(ctx, AV_LOG_ERROR,
               "Invalid packet length %d in %d byte packet\n", pkt_len,
               len);
        return AVERROR_INVALIDDATA;
    }

    if (ident != data->ident) {
        av_log(ctx, AV_LOG_ERROR,
136
               "Unimplemented Xiph SDP configuration change detected\n");
137 138 139 140 141
        return AVERROR_PATCHWELCOME;
    }

    if (tdt) {
        av_log(ctx, AV_LOG_ERROR,
142
               "Unimplemented RTP Xiph packet settings (%d,%d,%d)\n",
143 144 145 146 147 148 149 150
               fragmented, tdt, num_pkts);
        return AVERROR_PATCHWELCOME;
    }

    buf += 6; // move past header bits
    len -= 6;

    if (fragmented == 0) {
151
        if (av_new_packet(pkt, pkt_len)) {
152
            av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
153
            return AVERROR(ENOMEM);
154 155
        }
        pkt->stream_index = st->index;
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
        memcpy(pkt->data, buf, pkt_len);
        buf += pkt_len;
        len -= pkt_len;
        num_pkts--;

        if (num_pkts > 0) {
            if (len > data->split_buf_size || !data->split_buf) {
                av_freep(&data->split_buf);
                data->split_buf_size = 2 * len;
                data->split_buf = av_malloc(data->split_buf_size);
                if (!data->split_buf) {
                    av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
                    av_free_packet(pkt);
                    return AVERROR(ENOMEM);
                }
            }
            memcpy(data->split_buf, buf, len);
            data->split_buf_len = len;
            data->split_pos = 0;
            data->split_pkts = num_pkts;
            return 1;
177 178 179 180 181
        }

        return 0;

    } else if (fragmented == 1) {
182
        // start of xiph data fragment
183 184 185 186 187
        int res;

        // end packet has been lost somewhere, so drop buffered data
        free_fragment_if_needed(data);

188
        if((res = avio_open_dyn_buf(&data->fragment)) < 0)
189 190
            return res;

191
        avio_write(data->fragment, buf, pkt_len);
192 193 194
        data->timestamp = *timestamp;

    } else {
195
        av_assert1(fragmented < 4);
196 197 198 199 200 201 202
        if (data->timestamp != *timestamp) {
            // skip if fragmented timestamp is incorrect;
            // a start packet has been lost somewhere
            free_fragment_if_needed(data);
            av_log(ctx, AV_LOG_ERROR, "RTP timestamps don't match!\n");
            return AVERROR_INVALIDDATA;
        }
203 204 205 206 207
        if (!data->fragment) {
            av_log(ctx, AV_LOG_WARNING,
                   "Received packet without a start fragment; dropping.\n");
            return AVERROR(EAGAIN);
        }
208 209

        // copy data to fragment buffer
210
        avio_write(data->fragment, buf, pkt_len);
211 212

        if (fragmented == 3) {
213
            // end of xiph data packet
214 215
            int ret = ff_rtp_finalize_packet(pkt, &data->fragment, st->index);
            if (ret < 0) {
216 217
                av_log(ctx, AV_LOG_ERROR,
                       "Error occurred when getting fragment buffer.");
218
                return ret;
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
            }

            return 0;
        }
    }

   return AVERROR(EAGAIN);
}

/**
 * Length encoding described in RFC5215 section 3.1.1.
 */
static int get_base128(const uint8_t ** buf, const uint8_t * buf_end)
{
    int n = 0;
    for (; *buf < buf_end; ++*buf) {
        n <<= 7;
        n += **buf & 0x7f;
        if (!(**buf & 0x80)) {
            ++*buf;
            return n;
        }
    }
    return 0;
}

/**
 * Based off parse_packed_headers in Vorbis RTP
 */
248
static int
249 250
parse_packed_headers(const uint8_t * packed_headers,
                     const uint8_t * packed_headers_end,
251
                     AVCodecContext * codec, PayloadContext * xiph_data)
252 253 254 255 256 257 258
{

    unsigned num_packed, num_headers, length, length1, length2, extradata_alloc;
    uint8_t *ptr;

    if (packed_headers_end - packed_headers < 9) {
        av_log(codec, AV_LOG_ERROR,
259
               "Invalid %"PTRDIFF_SPECIFIER" byte packed header.",
260 261 262 263 264
               packed_headers_end - packed_headers);
        return AVERROR_INVALIDDATA;
    }

    num_packed         = bytestream_get_be32(&packed_headers);
265
    xiph_data->ident   = bytestream_get_be24(&packed_headers);
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
    length             = bytestream_get_be16(&packed_headers);
    num_headers        = get_base128(&packed_headers, packed_headers_end);
    length1            = get_base128(&packed_headers, packed_headers_end);
    length2            = get_base128(&packed_headers, packed_headers_end);

    if (num_packed != 1 || num_headers > 3) {
        av_log(codec, AV_LOG_ERROR,
               "Unimplemented number of headers: %d packed headers, %d headers\n",
               num_packed, num_headers);
        return AVERROR_PATCHWELCOME;
    }

    if (packed_headers_end - packed_headers != length ||
        length1 > length || length2 > length - length1) {
        av_log(codec, AV_LOG_ERROR,
281
               "Bad packed header lengths (%d,%d,%"PTRDIFF_SPECIFIER",%d)\n", length1,
282 283 284 285 286 287 288 289 290 291
               length2, packed_headers_end - packed_headers, length);
        return AVERROR_INVALIDDATA;
    }

    /* allocate extra space:
     * -- length/255 +2 for xiphlacing
     * -- one for the '2' marker
     * -- FF_INPUT_BUFFER_PADDING_SIZE required */
    extradata_alloc = length + length/255 + 3 + FF_INPUT_BUFFER_PADDING_SIZE;

292
    if (ff_alloc_extradata(codec, extradata_alloc)) {
293
        av_log(codec, AV_LOG_ERROR, "Out of memory\n");
294
        return AVERROR(ENOMEM);
295
    }
296
    ptr = codec->extradata;
297 298 299 300 301 302 303 304 305 306 307 308
    *ptr++ = 2;
    ptr += av_xiphlacing(ptr, length1);
    ptr += av_xiphlacing(ptr, length2);
    memcpy(ptr, packed_headers, length);
    ptr += length;
    codec->extradata_size = ptr - codec->extradata;
    // clear out remaining parts of the buffer
    memset(ptr, 0, extradata_alloc - codec->extradata_size);

    return 0;
}

309 310
static int xiph_parse_fmtp_pair(AVFormatContext *s,
                                AVStream* stream,
311
                                PayloadContext *xiph_data,
Josh Allmann's avatar
Josh Allmann committed
312
                                char *attr, char *value)
313
{
314
    AVCodecContext *codec = stream->codec;
315 316 317
    int result = 0;

    if (!strcmp(attr, "sampling")) {
318
        if (!strcmp(value, "YCbCr-4:2:0")) {
319
            codec->pix_fmt = AV_PIX_FMT_YUV420P;
320
        } else if (!strcmp(value, "YCbCr-4:4:2")) {
321
            codec->pix_fmt = AV_PIX_FMT_YUV422P;
322
        } else if (!strcmp(value, "YCbCr-4:4:4")) {
323
            codec->pix_fmt = AV_PIX_FMT_YUV444P;
324
        } else {
325
            av_log(s, AV_LOG_ERROR,
326 327 328
                   "Unsupported pixel format %s\n", attr);
            return AVERROR_INVALIDDATA;
        }
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
    } else if (!strcmp(attr, "width")) {
        /* This is an integer between 1 and 1048561
         * and MUST be in multiples of 16. */
        codec->width = atoi(value);
        return 0;
    } else if (!strcmp(attr, "height")) {
        /* This is an integer between 1 and 1048561
         * and MUST be in multiples of 16. */
        codec->height = atoi(value);
        return 0;
    } else if (!strcmp(attr, "delivery-method")) {
        /* Possible values are: inline, in_band, out_band/specific_name. */
        return AVERROR_PATCHWELCOME;
    } else if (!strcmp(attr, "configuration-uri")) {
        /* NOTE: configuration-uri is supported only under 2 conditions:
         *--after the delivery-method tag
         * --with a delivery-method value of out_band */
        return AVERROR_PATCHWELCOME;
    } else if (!strcmp(attr, "configuration")) {
        /* NOTE: configuration is supported only AFTER the delivery-method tag
         * The configuration value is a base64 encoded packed header */
        uint8_t *decoded_packet = NULL;
        int packet_size;
        size_t decoded_alloc = strlen(value) / 4 * 3 + 4;

        if (decoded_alloc <= INT_MAX) {
            decoded_packet = av_malloc(decoded_alloc);
            if (decoded_packet) {
                packet_size =
                    av_base64_decode(decoded_packet, value, decoded_alloc);

                result = parse_packed_headers
                    (decoded_packet, decoded_packet + packet_size, codec,
362
                    xiph_data);
363
            } else {
364
                av_log(s, AV_LOG_ERROR,
365
                       "Out of memory while decoding SDP configuration.\n");
366
                result = AVERROR(ENOMEM);
367 368
            }
        } else {
369
            av_log(s, AV_LOG_ERROR, "Packet too large\n");
370 371 372 373 374 375 376
            result = AVERROR_INVALIDDATA;
        }
        av_free(decoded_packet);
    }
    return result;
}

377
static int xiph_parse_sdp_line(AVFormatContext *s, int st_index,
378
                               PayloadContext *data, const char *line)
379 380 381
{
    const char *p;

382 383 384
    if (st_index < 0)
        return 0;

385
    if (av_strstart(line, "fmtp:", &p)) {
386
        return ff_parse_fmtp(s, s->streams[st_index], data, p,
387
                             xiph_parse_fmtp_pair);
388 389 390 391 392 393 394
    }

    return 0;
}

RTPDynamicProtocolHandler ff_theora_dynamic_handler = {
    .enc_name         = "theora",
395
    .codec_type       = AVMEDIA_TYPE_VIDEO,
396
    .codec_id         = AV_CODEC_ID_THEORA,
397
    .parse_sdp_a_line = xiph_parse_sdp_line,
398 399
    .alloc            = xiph_new_context,
    .free             = xiph_free_context,
400
    .parse_packet     = xiph_handle_packet
401
};
402 403 404

RTPDynamicProtocolHandler ff_vorbis_dynamic_handler = {
    .enc_name         = "vorbis",
405
    .codec_type       = AVMEDIA_TYPE_AUDIO,
406
    .codec_id         = AV_CODEC_ID_VORBIS,
407
    .init             = xiph_vorbis_init,
408
    .parse_sdp_a_line = xiph_parse_sdp_line,
409 410
    .alloc            = xiph_new_context,
    .free             = xiph_free_context,
411 412
    .parse_packet     = xiph_handle_packet
};