rtpdec_qt.c 8.99 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*
 * RTP/Quicktime support.
 * Copyright (c) 2009 Ronald S. Bultje
 *
 * 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
 */

/**
 * @file
 * @brief Quicktime-style RTP support
 * @author Ronald S. Bultje <rbultje@ronald.bitfreak.net>
 */

#include "avformat.h"
29
#include "internal.h"
30
#include "avio_internal.h"
31 32 33 34 35 36 37 38 39 40 41 42 43 44
#include "rtp.h"
#include "rtpdec.h"
#include "isom.h"
#include "libavcodec/get_bits.h"

struct PayloadContext {
    AVPacket pkt;
    int bytes_per_frame, remaining;
    uint32_t timestamp;
};

static int qt_rtp_parse_packet(AVFormatContext *s, PayloadContext *qt,
                               AVStream *st, AVPacket *pkt,
                               uint32_t *timestamp, const uint8_t *buf,
45
                               int len, uint16_t seq, int flags)
46
{
47
    AVIOContext pb;
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
    GetBitContext gb;
    int packing_scheme, has_payload_desc, has_packet_info, alen,
        has_marker_bit = flags & RTP_FLAG_MARKER;

    if (qt->remaining) {
        int num = qt->pkt.size / qt->bytes_per_frame;

        if (av_new_packet(pkt, qt->bytes_per_frame))
            return AVERROR(ENOMEM);
        pkt->stream_index = st->index;
        pkt->flags        = qt->pkt.flags;
        memcpy(pkt->data,
               &qt->pkt.data[(num - qt->remaining) * qt->bytes_per_frame],
               qt->bytes_per_frame);
        if (--qt->remaining == 0) {
            av_freep(&qt->pkt.data);
            qt->pkt.size = 0;
        }
        return qt->remaining > 0;
    }

    /**
     * The RTP payload is described in:
     * http://developer.apple.com/quicktime/icefloe/dispatch026.html
     */
    init_get_bits(&gb, buf, len << 3);
74
    ffio_init_context(&pb, (uint8_t*)buf, len, 0, NULL, NULL, NULL, NULL);
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99

    if (len < 4)
        return AVERROR_INVALIDDATA;

    skip_bits(&gb, 4); // version
    if ((packing_scheme = get_bits(&gb, 2)) == 0)
        return AVERROR_INVALIDDATA;
    if (get_bits1(&gb))
        flags          |= RTP_FLAG_KEY;
    has_payload_desc    = get_bits1(&gb);
    has_packet_info     = get_bits1(&gb);
    skip_bits(&gb, 23); // reserved:7, cache payload info:1, payload ID:15

    if (has_payload_desc) {
        int data_len, pos, is_start, is_finish;
        uint32_t tag;

        pos = get_bits_count(&gb) >> 3;
        if (pos + 12 > len)
            return AVERROR_INVALIDDATA;

        skip_bits(&gb, 2); // has non-I frames:1, is sparse:1
        is_start  = get_bits1(&gb);
        is_finish = get_bits1(&gb);
        if (!is_start || !is_finish) {
100 101
            avpriv_request_sample(s, "RTP-X-QT with payload description "
                                  "split over several packets");
102
            return AVERROR_PATCHWELCOME;
103 104 105 106
        }
        skip_bits(&gb, 12); // reserved
        data_len = get_bits(&gb, 16);

107
        avio_seek(&pb, pos + 4, SEEK_SET);
108
        tag = avio_rl32(&pb);
109
        if ((st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
110
                 tag != MKTAG('v','i','d','e')) ||
111
            (st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
112 113
                 tag != MKTAG('s','o','u','n')))
            return AVERROR_INVALIDDATA;
114
        avpriv_set_pts_info(st, 32, 1, avio_rb32(&pb));
115 116 117 118

        if (pos + data_len > len)
            return AVERROR_INVALIDDATA;
        /* TLVs */
119
        while (avio_tell(&pb) + 4 < pos + data_len) {
120 121
            int tlv_len = avio_rb16(&pb);
            tag = avio_rl16(&pb);
122
            if (avio_tell(&pb) + tlv_len > pos + data_len)
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
                return AVERROR_INVALIDDATA;

#define MKTAG16(a,b) MKTAG(a,b,0,0)
            switch (tag) {
            case MKTAG16('s','d'): {
                MOVStreamContext *msc;
                void *priv_data = st->priv_data;
                int nb_streams = s->nb_streams;
                MOVContext *mc = av_mallocz(sizeof(*mc));
                if (!mc)
                    return AVERROR(ENOMEM);
                mc->fc = s;
                st->priv_data = msc = av_mallocz(sizeof(MOVStreamContext));
                if (!msc) {
                    av_free(mc);
                    st->priv_data = priv_data;
                    return AVERROR(ENOMEM);
                }
                /* ff_mov_read_stsd_entries updates stream s->nb_streams-1,
                 * so set it temporarily to indicate which stream to update. */
                s->nb_streams = st->index + 1;
                ff_mov_read_stsd_entries(mc, &pb, 1);
                qt->bytes_per_frame = msc->bytes_per_frame;
                av_free(msc);
                av_free(mc);
                st->priv_data = priv_data;
                s->nb_streams = nb_streams;
                break;
            }
            default:
153
                avio_skip(&pb, tlv_len);
154 155 156 157 158
                break;
            }
        }

        /* 32-bit alignment */
159
        avio_skip(&pb, ((avio_tell(&pb) + 3) & ~3) - avio_tell(&pb));
160
    } else
161
        avio_seek(&pb, 4, SEEK_SET);
162 163

    if (has_packet_info) {
164
        avpriv_request_sample(s, "RTP-X-QT with packet-specific info");
165
        return AVERROR_PATCHWELCOME;
166 167
    }

168
    alen = len - avio_tell(&pb);
169 170 171 172 173 174
    if (alen <= 0)
        return AVERROR_INVALIDDATA;

    switch (packing_scheme) {
    case 3: /* one data packet spread over 1 or multiple RTP packets */
        if (qt->pkt.size > 0 && qt->timestamp == *timestamp) {
175 176
            int err;
            if ((err = av_reallocp(&qt->pkt.data, qt->pkt.size + alen +
177 178
                                   FF_INPUT_BUFFER_PADDING_SIZE)) < 0) {
                qt->pkt.size = 0;
179
                return err;
180
            }
181 182 183
        } else {
            av_freep(&qt->pkt.data);
            av_init_packet(&qt->pkt);
184
            qt->pkt.data = av_realloc(NULL, alen + FF_INPUT_BUFFER_PADDING_SIZE);
185 186
            if (!qt->pkt.data)
                return AVERROR(ENOMEM);
187 188 189
            qt->pkt.size = 0;
            qt->timestamp = *timestamp;
        }
190
        memcpy(qt->pkt.data + qt->pkt.size, buf + avio_tell(&pb), alen);
191 192
        qt->pkt.size += alen;
        if (has_marker_bit) {
193 194 195 196
            int ret = av_packet_from_data(pkt, qt->pkt.data, qt->pkt.size);
            if (ret < 0)
                return ret;

197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
            qt->pkt.size = 0;
            qt->pkt.data = NULL;
            pkt->flags        = flags & RTP_FLAG_KEY ? AV_PKT_FLAG_KEY : 0;
            pkt->stream_index = st->index;
            memset(pkt->data + pkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
            return 0;
        }
        return AVERROR(EAGAIN);

    case 1: /* constant packet size, multiple packets per RTP packet */
        if (qt->bytes_per_frame == 0 ||
            alen % qt->bytes_per_frame != 0)
            return AVERROR_INVALIDDATA; /* wrongly padded */
        qt->remaining = (alen / qt->bytes_per_frame) - 1;
        if (av_new_packet(pkt, qt->bytes_per_frame))
            return AVERROR(ENOMEM);
213
        memcpy(pkt->data, buf + avio_tell(&pb), qt->bytes_per_frame);
214 215 216 217
        pkt->flags = flags & RTP_FLAG_KEY ? AV_PKT_FLAG_KEY : 0;
        pkt->stream_index = st->index;
        if (qt->remaining > 0) {
            av_freep(&qt->pkt.data);
218
            qt->pkt.data = av_realloc(NULL, qt->remaining * qt->bytes_per_frame);
219 220 221 222 223 224
            if (!qt->pkt.data) {
                av_free_packet(pkt);
                return AVERROR(ENOMEM);
            }
            qt->pkt.size = qt->remaining * qt->bytes_per_frame;
            memcpy(qt->pkt.data,
225
                   buf + avio_tell(&pb) + qt->bytes_per_frame,
226 227 228 229 230 231 232
                   qt->remaining * qt->bytes_per_frame);
            qt->pkt.flags = pkt->flags;
            return 1;
        }
        return 0;

    default:  /* unimplemented */
233
        avpriv_request_sample(NULL, "RTP-X-QT with packing scheme 2");
234
        return AVERROR_PATCHWELCOME;
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
    }
}

static PayloadContext *qt_rtp_new(void)
{
    return av_mallocz(sizeof(PayloadContext));
}

static void qt_rtp_free(PayloadContext *qt)
{
    av_freep(&qt->pkt.data);
    av_free(qt);
}

#define RTP_QT_HANDLER(m, n, s, t) \
RTPDynamicProtocolHandler ff_ ## m ## _rtp_ ## n ## _handler = { \
251 252
    .enc_name         = s, \
    .codec_type       = t, \
253
    .codec_id         = AV_CODEC_ID_NONE, \
254 255
    .alloc            = qt_rtp_new,    \
    .free             = qt_rtp_free,   \
256
    .parse_packet     = qt_rtp_parse_packet, \
257
}
258

259 260 261 262
RTP_QT_HANDLER(qt,        vid, "X-QT",        AVMEDIA_TYPE_VIDEO);
RTP_QT_HANDLER(qt,        aud, "X-QT",        AVMEDIA_TYPE_AUDIO);
RTP_QT_HANDLER(quicktime, vid, "X-QUICKTIME", AVMEDIA_TYPE_VIDEO);
RTP_QT_HANDLER(quicktime, aud, "X-QUICKTIME", AVMEDIA_TYPE_AUDIO);