rtpdec_vp9.c 10.9 KB
Newer Older
1
/*
2
 * RTP parser for VP9 payload format (draft version 02) - experimental
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * Copyright (c) 2015 Thomas Volkert <thomas@homer-conferencing.com>
 *
 * 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
 */

22
#include "libavutil/intreadwrite.h"
23

24
#include "avio_internal.h"
25 26 27 28 29 30 31 32 33 34
#include "rtpdec_formats.h"

#define RTP_VP9_DESC_REQUIRED_SIZE 1

struct PayloadContext {
    AVIOContext *buf;
    uint32_t     timestamp;
};

static av_cold int vp9_init(AVFormatContext *ctx, int st_index,
35
                            PayloadContext *data)
36 37 38 39 40 41 42 43 44 45 46 47
{
    av_log(ctx, AV_LOG_WARNING,
           "RTP/VP9 support is still experimental\n");

    return 0;
}

static int vp9_handle_packet(AVFormatContext *ctx, PayloadContext *rtp_vp9_ctx,
                             AVStream *st, AVPacket *pkt, uint32_t *timestamp,
                             const uint8_t *buf, int len, uint16_t seq,
                             int flags)
{
48 49
    int has_pic_id, has_layer_idc, has_ref_idc, has_ss_data;
    av_unused int pic_id = 0, non_key_frame = 0, inter_picture_layer_frame;
50 51 52
    av_unused int layer_temporal = -1, layer_spatial = -1, layer_quality = -1;
    int ref_fields = 0, has_ref_field_ext_pic_id = 0;
    int first_fragment, last_fragment;
53
    int rtp_m;
54 55 56
    int res = 0;

    /* drop data of previous packets in case of non-continuous (lossy) packet stream */
57
    if (rtp_vp9_ctx->buf && rtp_vp9_ctx->timestamp != *timestamp)
58
        ffio_free_dyn_buf(&rtp_vp9_ctx->buf);
59 60 61 62 63 64 65 66

    /* sanity check for size of input packet: 1 byte payload at least */
    if (len < RTP_VP9_DESC_REQUIRED_SIZE + 1) {
        av_log(ctx, AV_LOG_ERROR, "Too short RTP/VP9 packet, got %d bytes\n", len);
        return AVERROR_INVALIDDATA;
    }

    /*
67 68 69 70
     *     decode the required VP9 payload descriptor according to section 4.2 of the spec.:
     *
     *      0 1 2 3 4 5 6 7
     *     +-+-+-+-+-+-+-+-+
71
     *     |I|P|L|F|B|E|V|-| (REQUIRED)
72 73 74
     *     +-+-+-+-+-+-+-+-+
     *
     *     I: PictureID present
75
     *     P: Inter-picture predicted layer frame
76
     *     L: Layer indices present
77
     *     F: Flexible mode
78 79 80 81 82
     *     B: Start of VP9 frame
     *     E: End of picture
     *     V: Scalability Structure (SS) present
     */
    has_pic_id     = !!(buf[0] & 0x80);
83 84 85 86 87 88
    inter_picture_layer_frame = !!(buf[0] & 0x40);
    has_layer_idc  = !!(buf[0] & 0x20);
    has_ref_idc    = !!(buf[0] & 0x10);
    first_fragment = !!(buf[0] & 0x08);
    last_fragment  = !!(buf[0] & 0x04);
    has_ss_data    = !!(buf[0] & 0x02);
89 90

    rtp_m = !!(flags & RTP_FLAG_MARKER);
91 92

    /* sanity check for markers: B should always be equal to the RTP M marker */
93 94
    if (last_fragment != rtp_m) {
        av_log(ctx, AV_LOG_ERROR, "Invalid combination of B and M marker (%d != %d)\n", last_fragment, rtp_m);
95 96 97 98 99 100 101 102
        return AVERROR_INVALIDDATA;
    }

    /* pass the extensions field */
    buf += RTP_VP9_DESC_REQUIRED_SIZE;
    len -= RTP_VP9_DESC_REQUIRED_SIZE;

    /*
103 104 105 106 107 108 109 110 111 112 113
     *         decode the 1-byte/2-byte picture ID:
     *
     *          0 1 2 3 4 5 6 7
     *         +-+-+-+-+-+-+-+-+
     *   I:    |M|PICTURE ID   | (RECOMMENDED)
     *         +-+-+-+-+-+-+-+-+
     *   M:    | EXTENDED PID  | (RECOMMENDED)
     *         +-+-+-+-+-+-+-+-+
     *
     *   M: The most significant bit of the first octet is an extension flag.
     *   PictureID:  8 or 16 bits including the M bit.
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
     */
    if (has_pic_id) {
        /* check for 1-byte or 2-byte picture index */
        if (buf[0] & 0x80) {
            if (len < 2) {
                av_log(ctx, AV_LOG_ERROR, "Too short RTP/VP9 packet\n");
                return AVERROR_INVALIDDATA;
            }
            pic_id = AV_RB16(buf) & 0x7fff;
            buf += 2;
            len -= 2;
        } else {
            pic_id = buf[0] & 0x7f;
            buf++;
            len--;
        }
    }

    /*
133 134 135 136 137 138 139 140 141 142
     *         decode layer indices
     *
     *          0 1 2 3 4 5 6 7
     *         +-+-+-+-+-+-+-+-+
     *   L:    | T | S | Q | R | (CONDITIONALLY RECOMMENDED)
     *         +-+-+-+-+-+-+-+-+
     *
     *   T, S and Q are 2-bit indices for temporal, spatial, and quality layers.
     *   If "F" is set in the initial octet, R is 2 bits representing the number
     *   of reference fields this frame refers to.
143 144 145
     */
    if (has_layer_idc) {
        if (len < 1) {
146
            av_log(ctx, AV_LOG_ERROR, "Too short RTP/VP9 packet\n");
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
            return AVERROR_INVALIDDATA;
        }
        layer_temporal = buf[0] & 0xC0;
        layer_spatial  = buf[0] & 0x30;
        layer_quality  = buf[0] & 0x0C;
        if (has_ref_idc) {
            ref_fields = buf[0] & 0x03;
            if (ref_fields)
                non_key_frame = 1;
        }
        buf++;
        len--;
    }

    /*
162 163 164 165 166 167 168 169 170 171 172 173
     *         decode the reference fields
     *
     *          0 1 2 3 4 5 6 7
     *         +-+-+-+-+-+-+-+-+              -\
     *   F:    | PID |X| RS| RQ| (OPTIONAL)    .
     *         +-+-+-+-+-+-+-+-+               . - R times
     *   X:    | EXTENDED PID  | (OPTIONAL)    .
     *         +-+-+-+-+-+-+-+-+              -/
     *
     *   PID:  The relative Picture ID referred to by this frame.
     *   RS and RQ:  The spatial and quality layer IDs.
     *   X: 1 if this layer index has an extended relative Picture ID.
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
     */
    if (has_ref_idc) {
        while (ref_fields) {
            if (len < 1) {
                av_log(ctx, AV_LOG_ERROR, "Too short RTP/VP9 packet\n");
                return AVERROR_INVALIDDATA;
            }

            has_ref_field_ext_pic_id = buf[0] & 0x10;

            /* pass ref. field */
            if (has_ref_field_ext_pic_id) {
                if (len < 2) {
                    av_log(ctx, AV_LOG_ERROR, "Too short RTP/VP9 packet\n");
                    return AVERROR_INVALIDDATA;
                }

                /* ignore ref. data */

                buf += 2;
                len -= 2;
            } else {

                /* ignore ref. data */

                buf++;
                len--;
            }
            ref_fields--;
        }
    }

    /*
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
     *         decode the scalability structure (SS)
     *
     *          0 1 2 3 4 5 6 7
     *         +-+-+-+-+-+-+-+-+
     *   V:    | PATTERN LENGTH|
     *         +-+-+-+-+-+-+-+-+                           -\
     *         | T | S | Q | R | (OPTIONAL)                 .
     *         +-+-+-+-+-+-+-+-+              -\            .
     *         | PID |X| RS| RQ| (OPTIONAL)    .            . - PAT. LEN. times
     *         +-+-+-+-+-+-+-+-+               . - R times  .
     *   X:    | EXTENDED PID  | (OPTIONAL)    .            .
     *         +-+-+-+-+-+-+-+-+              -/           -/
     *
     *   PID:  The relative Picture ID referred to by this frame.
     *   RS and RQ:  The spatial and quality layer IDs.
     *   X: 1 if this layer index has an extended relative Picture ID.
223 224
     */
    if (has_ss_data) {
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
        int n_s, y, g, i;
        if (len < 1) {
            av_log(ctx, AV_LOG_ERROR, "Too short RTP/VP9 packet\n");
            return AVERROR_INVALIDDATA;
        }
        n_s = buf[0] >> 5;
        y = !!(buf[0] & 0x10);
        g = !!(buf[0] & 0x08);
        buf++;
        len--;
        if (n_s > 0) {
            avpriv_report_missing_feature(ctx, "VP9 scalability structure with multiple layers");
            return AVERROR_PATCHWELCOME;
        }
        if (y) {
            if (len < 4 * (n_s + 1)) {
                av_log(ctx, AV_LOG_ERROR, "Too short RTP/VP9 packet\n");
                return AVERROR_INVALIDDATA;
            }
            for (i = 0; i < n_s + 1; i++) {
                av_unused int w, h;
                w = AV_RB16(buf);
                h = AV_RB16(buf + 2);
                buf += 4;
                len -= 4;
            }
        }
        if (g) {
            int n_g;
            if (len < 1) {
                av_log(ctx, AV_LOG_ERROR, "Too short RTP/VP9 packet\n");
                return AVERROR_INVALIDDATA;
            }
            n_g = buf[0];
            buf++;
            len--;
            for (i = 0; i < n_g; i++) {
                av_unused int t, u, r, j;
                if (len < 1) {
                    av_log(ctx, AV_LOG_ERROR, "Too short RTP/VP9 packet\n");
                    return AVERROR_INVALIDDATA;
                }
                t = buf[0] >> 5;
                u = !!(buf[0] & 0x10);
                r = (buf[0] >> 2) & 0x03;
                buf++;
                len--;
                if (len < r) {
                    av_log(ctx, AV_LOG_ERROR, "Too short RTP/VP9 packet\n");
                    return AVERROR_INVALIDDATA;
                }
                for (j = 0; j < r; j++) {
                    av_unused int p_diff = buf[0];
                    buf++;
                    len--;
                }
            }
        }
283 284 285
    }

    /*
286 287 288
     * decode the VP9 payload header
     *
     *  spec. is tbd
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
     */
    //XXX: implement when specified

    /* sanity check: 1 byte payload as minimum */
    if (len < 1) {
        av_log(ctx, AV_LOG_ERROR, "Too short RTP/VP9 packet\n");
        return AVERROR_INVALIDDATA;
    }

    /* start frame buffering with new dynamic buffer */
    if (!rtp_vp9_ctx->buf) {
        /* sanity check: a new frame should have started */
        if (first_fragment) {
            res = avio_open_dyn_buf(&rtp_vp9_ctx->buf);
            if (res < 0)
                return res;
            /* update the timestamp in the frame packet with the one from the RTP packet */
            rtp_vp9_ctx->timestamp = *timestamp;
        } else {
            /* frame not started yet, need more packets */
            return AVERROR(EAGAIN);
        }
    }

    /* write the fragment to the dyn. buffer */
    avio_write(rtp_vp9_ctx->buf, buf, len);

    /* do we need more fragments? */
    if (!last_fragment)
        return AVERROR(EAGAIN);

    /* close frame buffering and create resulting A/V packet */
    res = ff_rtp_finalize_packet(pkt, &rtp_vp9_ctx->buf, st->index);
    if (res < 0)
        return res;

    return 0;
}

328 329 330 331 332
static void vp9_close_context(PayloadContext *vp9)
{
    ffio_free_dyn_buf(&vp9->buf);
}

333
const RTPDynamicProtocolHandler ff_vp9_dynamic_handler = {
334 335 336
    .enc_name         = "VP9",
    .codec_type       = AVMEDIA_TYPE_VIDEO,
    .codec_id         = AV_CODEC_ID_VP9,
337
    .priv_data_size   = sizeof(PayloadContext),
338
    .init             = vp9_init,
339
    .close            = vp9_close_context,
340 341
    .parse_packet     = vp9_handle_packet
};