rtpdec_latm.c 5.3 KB
Newer Older
1
/*
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * RTP Depacketization of MP4A-LATM, RFC 3016
 * Copyright (c) 2010 Martin Storsjo
 *
 * 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 "avio_internal.h"
23 24 25 26 27 28
#include "rtpdec_formats.h"
#include "internal.h"
#include "libavutil/avstring.h"
#include "libavcodec/get_bits.h"

struct PayloadContext {
29
    AVIOContext *dyn_buf;
30 31 32 33 34
    uint8_t *buf;
    int pos, len;
    uint32_t timestamp;
};

35
static void latm_close_context(PayloadContext *data)
36
{
37
    ffio_free_dyn_buf(&data->dyn_buf);
38
    av_freep(&data->buf);
39 40 41 42
}

static int latm_parse_packet(AVFormatContext *ctx, PayloadContext *data,
                             AVStream *st, AVPacket *pkt, uint32_t *timestamp,
43 44
                             const uint8_t *buf, int len, uint16_t seq,
                             int flags)
45 46 47 48 49 50
{
    int ret, cur_len;

    if (buf) {
        if (!data->dyn_buf || data->timestamp != *timestamp) {
            av_freep(&data->buf);
51
            ffio_free_dyn_buf(&data->dyn_buf);
52 53

            data->timestamp = *timestamp;
54
            if ((ret = avio_open_dyn_buf(&data->dyn_buf)) < 0)
55 56
                return ret;
        }
57
        avio_write(data->dyn_buf, buf, len);
58 59 60

        if (!(flags & RTP_FLAG_MARKER))
            return AVERROR(EAGAIN);
61
        av_freep(&data->buf);
62
        data->len = avio_close_dyn_buf(data->dyn_buf, &data->buf);
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
        data->dyn_buf = NULL;
        data->pos = 0;
    }

    if (!data->buf) {
        av_log(ctx, AV_LOG_ERROR, "No data available yet\n");
        return AVERROR(EIO);
    }

    cur_len = 0;
    while (data->pos < data->len) {
        uint8_t val = data->buf[data->pos++];
        cur_len += val;
        if (val != 0xff)
            break;
    }
    if (data->pos + cur_len > data->len) {
        av_log(ctx, AV_LOG_ERROR, "Malformed LATM packet\n");
        return AVERROR(EIO);
    }

    if ((ret = av_new_packet(pkt, cur_len)) < 0)
        return ret;
    memcpy(pkt->data, data->buf + data->pos, cur_len);
    data->pos += cur_len;
    pkt->stream_index = st->index;
    return data->pos < data->len;
}

92
static int parse_fmtp_config(AVStream *st, const char *value)
93 94 95 96
{
    int len = ff_hex_to_data(NULL, value), i, ret = 0;
    GetBitContext gb;
    uint8_t *config;
Mans Rullgard's avatar
Mans Rullgard committed
97
    int audio_mux_version, same_time_framing, num_programs, num_layers;
98 99 100 101 102 103 104 105 106

    /* Pad this buffer, too, to avoid out of bounds reads with get_bits below */
    config = av_mallocz(len + FF_INPUT_BUFFER_PADDING_SIZE);
    if (!config)
        return AVERROR(ENOMEM);
    ff_hex_to_data(config, value);
    init_get_bits(&gb, config, len*8);
    audio_mux_version = get_bits(&gb, 1);
    same_time_framing = get_bits(&gb, 1);
Mans Rullgard's avatar
Mans Rullgard committed
107
    skip_bits(&gb, 6); /* num_sub_frames */
108 109 110 111 112 113 114 115 116 117 118
    num_programs      = get_bits(&gb, 4);
    num_layers        = get_bits(&gb, 3);
    if (audio_mux_version != 0 || same_time_framing != 1 || num_programs != 0 ||
        num_layers != 0) {
        av_log(NULL, AV_LOG_WARNING, "Unsupported LATM config (%d,%d,%d,%d)\n",
                                     audio_mux_version, same_time_framing,
                                     num_programs, num_layers);
        ret = AVERROR_PATCHWELCOME;
        goto end;
    }
    av_freep(&st->codec->extradata);
119
    if (ff_alloc_extradata(st->codec, (get_bits_left(&gb) + 7)/8)) {
120 121 122 123 124 125 126 127 128 129 130
        ret = AVERROR(ENOMEM);
        goto end;
    }
    for (i = 0; i < st->codec->extradata_size; i++)
        st->codec->extradata[i] = get_bits(&gb, 8);

end:
    av_free(config);
    return ret;
}

131 132
static int parse_fmtp(AVFormatContext *s,
                      AVStream *stream, PayloadContext *data,
133
                      const char *attr, const char *value)
134 135 136 137 138 139 140 141 142 143
{
    int res;

    if (!strcmp(attr, "config")) {
        res = parse_fmtp_config(stream, value);
        if (res < 0)
            return res;
    } else if (!strcmp(attr, "cpresent")) {
        int cpresent = atoi(value);
        if (cpresent != 0)
144
            avpriv_request_sample(s,
145
                                  "RTP MP4A-LATM with in-band configuration");
146 147 148 149 150 151 152 153 154 155
    }

    return 0;
}

static int latm_parse_sdp_line(AVFormatContext *s, int st_index,
                               PayloadContext *data, const char *line)
{
    const char *p;

156 157 158
    if (st_index < 0)
        return 0;

159
    if (av_strstart(line, "fmtp:", &p))
160
        return ff_parse_fmtp(s, s->streams[st_index], data, p, parse_fmtp);
161 162 163 164 165 166 167

    return 0;
}

RTPDynamicProtocolHandler ff_mp4a_latm_dynamic_handler = {
    .enc_name           = "MP4A-LATM",
    .codec_type         = AVMEDIA_TYPE_AUDIO,
168
    .codec_id           = AV_CODEC_ID_AAC,
169
    .priv_data_size     = sizeof(PayloadContext),
170
    .parse_sdp_a_line   = latm_parse_sdp_line,
171
    .close              = latm_close_context,
172
    .parse_packet       = latm_parse_packet,
173
};