aboutsummaryrefslogtreecommitdiffstats
path: root/libavformat/rtpdec_h261.c
blob: c1d79a8ace7aaf2ff5869f3a42f2c8430eee1526 (plain) (blame)
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
74
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
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
/*
 * RTP parser for H.261 payload format (RFC 4587)
 * Copyright (c) 2014 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
 */

#include "avformat.h"
#include "rtpdec_formats.h"
#include "libavcodec/get_bits.h"

#define RTP_H261_PAYLOAD_HEADER_SIZE                    4

struct PayloadContext {
    AVIOContext *buf;
    uint8_t      endbyte;
    int          endbyte_bits;
    uint32_t     timestamp;
};

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

static void h261_free_dyn_buffer(AVIOContext **dyn_buf)
{
    uint8_t *ptr_dyn_buffer;
    avio_close_dyn_buf(*dyn_buf, &ptr_dyn_buffer);
    av_free(ptr_dyn_buffer);
    *dyn_buf = NULL;
}

static void h261_free_context(PayloadContext *pl_ctx)
{
    /* return if context is invalid */
    if (!pl_ctx)
        return;

    /* free buffer if it is valid */
    if (pl_ctx->buf) {
        h261_free_dyn_buffer(&pl_ctx->buf);
    }

    /* free context */
    av_free(pl_ctx);
}

static av_cold int h261_init(AVFormatContext *ctx, int st_index,
                             PayloadContext *data)
{
    //av_log(ctx, AV_LOG_DEBUG, "h261_init() for stream %d\n", st_index);

    if (st_index < 0)
        return 0;

    ctx->streams[st_index]->need_parsing = AVSTREAM_PARSE_FULL;

    return 0;
}

int ff_h261_handle_packet(AVFormatContext *ctx, PayloadContext *data,
                          AVStream *st, AVPacket *pkt, uint32_t *timestamp,
                          const uint8_t *buf, int len, uint16_t seq, int flags)
{
    int sbit, ebit, gobn, mbap, quant;
    int res;

    //av_log(ctx, AV_LOG_DEBUG, "got h261 RTP packet with time: %u\n", timestamp);

    /* drop data of previous packets in case of non-continuous (loss) packet stream */
    if (data->buf && data->timestamp != *timestamp) {
        h261_free_dyn_buffer(&data->buf);
    }

    /* sanity check for size of input packet */
    if (len < 5 /* 4 bytes header and 1 byte payload at least */) {
        av_log(ctx, AV_LOG_ERROR, "Too short H.261 RTP packet\n");
        return AVERROR_INVALIDDATA;
    }

    /*
      decode the H.261 payload header according to section 4.1 of RFC 4587:
      (uses 4 bytes between RTP header and H.261 stream per packet)

         0                   1                   2                   3
         0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
        |SBIT |EBIT |I|V| GOBN  |   MBAP  |  QUANT  |  HMVD   |  VMVD   |
        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

           Start bit position (SBIT): 3 bits
           End bit position (EBIT): 3 bits
           INTRA-frame encoded data (I): 1 bit
           Motion Vector flag (V): 1 bit
           GOB number (GOBN): 4 bits
           Macroblock address predictor (MBAP): 5 bits
           Quantizer (QUANT): 5 bits
           Horizontal motion vector data (HMVD): 5 bits
           Vertical motion vector data (VMVD): 5 bits

    */
    sbit  =  (buf[0] >> 5) & 0x07;
    ebit  =  (buf[0] >> 2) & 0x07;
    gobn  =  (buf[1] >> 4) & 0x0f;
    mbap  = ((buf[1] << 1) & 0x1e) | ((buf[1] >> 7) & 0x01);
    quant =  (buf[1] >> 4) & 0x0f;

    /* pass the H.261 payload header and continue with the actual payload */
    buf += RTP_H261_PAYLOAD_HEADER_SIZE;
    len -= RTP_H261_PAYLOAD_HEADER_SIZE;

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

    /* do the "byte merging" at the boundaries of two consecutive frame fragments */
    if (data->endbyte_bits || sbit) {
        if (data->endbyte_bits == sbit) {
            data->endbyte |= buf[0] & (0xff >> sbit);
            data->endbyte_bits = 0;
            buf++;
            len--;
            avio_w8(data->buf, data->endbyte);
        } else {
            /* ebit/sbit values inconsistent, assuming packet loss */
            GetBitContext gb;
            init_get_bits(&gb, buf, len*8 - ebit);
            skip_bits(&gb, sbit);
            if (data->endbyte_bits) {
                data->endbyte |= get_bits(&gb, 8 - data->endbyte_bits);
                avio_w8(data->buf, data->endbyte);
            }
            while (get_bits_left(&gb) >= 8)
                avio_w8(data->buf, get_bits(&gb, 8));
            data->endbyte_bits = get_bits_left(&gb);
            if (data->endbyte_bits)
                data->endbyte = get_bits(&gb, data->endbyte_bits) <<
                                (8 - data->endbyte_bits);
            ebit = 0;
            len = 0;
        }
    }
    if (ebit) {
        if (len > 0)
            avio_write(data->buf, buf, len - 1);
        data->endbyte_bits = 8 - ebit;
        data->endbyte = buf[len - 1] & (0xff << ebit);
    } else {
        avio_write(data->buf, buf, len);
    }

    /* RTP marker bit means: last fragment of current frame was received;
       otherwise, an additional fragment is needed for the current frame */
    if (!(flags & RTP_FLAG_MARKER))
        return AVERROR(EAGAIN);

    /* write the completed last byte from the "byte merging" */
    if (data->endbyte_bits)
        avio_w8(data->buf, data->endbyte);
    data->endbyte_bits = 0;

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

    return 0;
}

RTPDynamicProtocolHandler ff_h261_dynamic_handler = {
    .enc_name          = "H261",
    .codec_type        = AVMEDIA_TYPE_VIDEO,
    .codec_id          = AV_CODEC_ID_H261,
    .init              = h261_init,
    .parse_packet      = ff_h261_handle_packet,
    .alloc             = h261_new_context,
    .free              = h261_free_context,
    .static_payload_id = 31,
};