Browse code

rtp: Initial H.261 support

The packetizer only supports splitting at GOB headers - if
such aren't available frequently enough, it splits at any
random byte offset (not at a macroblock boundary either, which
would be allowed by the spec) and sends a payload header pretend
that it starts with a GOB header.

As long as a receiver doesn't try to handle such cases cleverly
but just drops broken frames, this shouldn't matter too much
in practice.

Signed-off-by: Martin Storsjö <martin@martin.st>

Thomas Volkert authored on 2014/12/07 03:54:07
Showing 10 changed files
... ...
@@ -7,6 +7,7 @@ version <next>:
7 7
 - avplay now exits by default at the end of playback
8 8
 - XCB-based screen-grabber
9 9
 - creating DASH compatible fragmented MP4, MPEG-DASH segmenting muxer
10
+- H.261 RTP payload format (RFC 4587) depacketizer and experimental packetizer
10 11
 
11 12
 
12 13
 version 11:
... ...
@@ -32,6 +32,7 @@ OBJS-$(CONFIG_RTPDEC)                    += rdt.o                       \
32 32
                                             rtpdec_amr.o                \
33 33
                                             rtpdec_asf.o                \
34 34
                                             rtpdec_g726.o               \
35
+                                            rtpdec_h261.o               \
35 36
                                             rtpdec_h263.o               \
36 37
                                             rtpdec_h263_rfc2190.o       \
37 38
                                             rtpdec_h264.o               \
... ...
@@ -290,6 +291,7 @@ OBJS-$(CONFIG_RTP_MUXER)                 += rtp.o         \
290 290
                                             rtpenc_aac.o     \
291 291
                                             rtpenc_latm.o    \
292 292
                                             rtpenc_amr.o     \
293
+                                            rtpenc_h261.o    \
293 294
                                             rtpenc_h263.o    \
294 295
                                             rtpenc_h263_rfc2190.o \
295 296
                                             rtpenc_hevc.o    \
... ...
@@ -66,6 +66,7 @@ void ff_register_rtp_dynamic_payload_handlers(void)
66 66
     ff_register_dynamic_payload_handler(&ff_g726_24_dynamic_handler);
67 67
     ff_register_dynamic_payload_handler(&ff_g726_32_dynamic_handler);
68 68
     ff_register_dynamic_payload_handler(&ff_g726_40_dynamic_handler);
69
+    ff_register_dynamic_payload_handler(&ff_h261_dynamic_handler);
69 70
     ff_register_dynamic_payload_handler(&ff_h263_1998_dynamic_handler);
70 71
     ff_register_dynamic_payload_handler(&ff_h263_2000_dynamic_handler);
71 72
     ff_register_dynamic_payload_handler(&ff_h263_rfc2190_dynamic_handler);
... ...
@@ -41,6 +41,7 @@ extern RTPDynamicProtocolHandler ff_g726_16_dynamic_handler;
41 41
 extern RTPDynamicProtocolHandler ff_g726_24_dynamic_handler;
42 42
 extern RTPDynamicProtocolHandler ff_g726_32_dynamic_handler;
43 43
 extern RTPDynamicProtocolHandler ff_g726_40_dynamic_handler;
44
+extern RTPDynamicProtocolHandler ff_h261_dynamic_handler;
44 45
 extern RTPDynamicProtocolHandler ff_h263_1998_dynamic_handler;
45 46
 extern RTPDynamicProtocolHandler ff_h263_2000_dynamic_handler;
46 47
 extern RTPDynamicProtocolHandler ff_h263_rfc2190_dynamic_handler;
47 48
new file mode 100644
... ...
@@ -0,0 +1,202 @@
0
+/*
1
+ * RTP parser for H.261 payload format (RFC 4587)
2
+ * Copyright (c) 2014 Thomas Volkert <thomas@homer-conferencing.com>
3
+ *
4
+ * This file is part of Libav.
5
+ *
6
+ * Libav is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * Libav is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with Libav; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+#include "libavcodec/get_bits.h"
22
+#include "avformat.h"
23
+#include "rtpdec_formats.h"
24
+
25
+#define RTP_H261_PAYLOAD_HEADER_SIZE 4
26
+
27
+struct PayloadContext {
28
+    AVIOContext *buf;
29
+    uint8_t      endbyte;
30
+    int          endbyte_bits;
31
+    uint32_t     timestamp;
32
+};
33
+
34
+static av_cold PayloadContext *h261_new_context(void)
35
+{
36
+    return av_mallocz(sizeof(PayloadContext));
37
+}
38
+
39
+static void h261_free_dyn_buffer(AVIOContext **dyn_buf)
40
+{
41
+    uint8_t *ptr_dyn_buffer;
42
+    avio_close_dyn_buf(*dyn_buf, &ptr_dyn_buffer);
43
+    av_free(ptr_dyn_buffer);
44
+    *dyn_buf = NULL;
45
+}
46
+
47
+static av_cold void h261_free_context(PayloadContext *pl_ctx)
48
+{
49
+    /* return if context is invalid */
50
+    if (!pl_ctx)
51
+        return;
52
+
53
+    /* free buffer if it is valid */
54
+    if (pl_ctx->buf) {
55
+        h261_free_dyn_buffer(&pl_ctx->buf);
56
+    }
57
+
58
+    /* free context */
59
+    av_free(pl_ctx);
60
+}
61
+
62
+static av_cold int h261_init(AVFormatContext *ctx, int st_index,
63
+                             PayloadContext *data)
64
+{
65
+    if (st_index < 0)
66
+        return 0;
67
+
68
+    ctx->streams[st_index]->need_parsing = AVSTREAM_PARSE_FULL;
69
+
70
+    return 0;
71
+}
72
+
73
+static int h261_handle_packet(AVFormatContext *ctx, PayloadContext *rtp_h261_ctx,
74
+                              AVStream *st, AVPacket *pkt, uint32_t *timestamp,
75
+                              const uint8_t *buf, int len, uint16_t seq,
76
+                              int flags)
77
+{
78
+    int sbit, ebit, gobn, mbap, quant;
79
+    int res;
80
+
81
+    /* drop data of previous packets in case of non-continuous (lossy) packet stream */
82
+    if (rtp_h261_ctx->buf && rtp_h261_ctx->timestamp != *timestamp) {
83
+        h261_free_dyn_buffer(&rtp_h261_ctx->buf);
84
+        rtp_h261_ctx->endbyte_bits = 0;
85
+    }
86
+
87
+    /* sanity check for size of input packet: 1 byte payload at least */
88
+    if (len < RTP_H261_PAYLOAD_HEADER_SIZE + 1) {
89
+        av_log(ctx, AV_LOG_ERROR, "Too short RTP/H.261 packet, got %d bytes\n", len);
90
+        return AVERROR_INVALIDDATA;
91
+    }
92
+
93
+    /*
94
+     * decode the H.261 payload header according to section 4.1 of RFC 4587:
95
+     * (uses 4 bytes between RTP header and H.261 stream per packet)
96
+     *
97
+     *    0                   1                   2                   3
98
+     *    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
99
+     *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
100
+     *   |SBIT |EBIT |I|V| GOBN  |   MBAP  |  QUANT  |  HMVD   |  VMVD   |
101
+     *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
102
+     *
103
+     *      Start bit position (SBIT): 3 bits
104
+     *      End bit position (EBIT): 3 bits
105
+     *      INTRA-frame encoded data (I): 1 bit
106
+     *      Motion Vector flag (V): 1 bit
107
+     *      GOB number (GOBN): 4 bits
108
+     *      Macroblock address predictor (MBAP): 5 bits
109
+     *      Quantizer (QUANT): 5 bits
110
+     *      Horizontal motion vector data (HMVD): 5 bits
111
+     *      Vertical motion vector data (VMVD): 5 bits
112
+     */
113
+    sbit  =  (buf[0] >> 5) & 0x07;
114
+    ebit  =  (buf[0] >> 2) & 0x07;
115
+    gobn  =  (buf[1] >> 4) & 0x0f;
116
+    mbap  = ((buf[1] << 1) & 0x1e) | ((buf[2] >> 7) & 0x01);
117
+    quant =  (buf[2] >> 2) & 0x1f;
118
+
119
+    /* pass the H.261 payload header and continue with the actual payload */
120
+    buf += RTP_H261_PAYLOAD_HEADER_SIZE;
121
+    len -= RTP_H261_PAYLOAD_HEADER_SIZE;
122
+
123
+    /* start frame buffering with new dynamic buffer */
124
+    if (!rtp_h261_ctx->buf) {
125
+        /* sanity check: a new frame starts with gobn=0, sbit=0, mbap=0, quant=0 */
126
+        if (!gobn && !sbit && !mbap && !quant) {
127
+            res = avio_open_dyn_buf(&rtp_h261_ctx->buf);
128
+            if (res < 0)
129
+                return res;
130
+            /* update the timestamp in the frame packet with the one from the RTP packet */
131
+            rtp_h261_ctx->timestamp = *timestamp;
132
+        } else {
133
+            /* frame not started yet, need more packets */
134
+            return AVERROR(EAGAIN);
135
+        }
136
+    }
137
+
138
+    /* do the "byte merging" at the boundaries of two consecutive frame fragments */
139
+    if (rtp_h261_ctx->endbyte_bits || sbit) {
140
+        if (rtp_h261_ctx->endbyte_bits == sbit) {
141
+            rtp_h261_ctx->endbyte     |= buf[0] & (0xff >> sbit);
142
+            rtp_h261_ctx->endbyte_bits = 0;
143
+            buf++;
144
+            len--;
145
+            avio_w8(rtp_h261_ctx->buf, rtp_h261_ctx->endbyte);
146
+        } else {
147
+            /* ebit/sbit values inconsistent, assuming packet loss */
148
+            GetBitContext gb;
149
+            init_get_bits(&gb, buf, len*8 - ebit);
150
+            skip_bits(&gb, sbit);
151
+            if (rtp_h261_ctx->endbyte_bits) {
152
+                rtp_h261_ctx->endbyte |= get_bits(&gb, 8 - rtp_h261_ctx->endbyte_bits);
153
+                avio_w8(rtp_h261_ctx->buf, rtp_h261_ctx->endbyte);
154
+            }
155
+            while (get_bits_left(&gb) >= 8)
156
+                avio_w8(rtp_h261_ctx->buf, get_bits(&gb, 8));
157
+            rtp_h261_ctx->endbyte_bits = get_bits_left(&gb);
158
+            if (rtp_h261_ctx->endbyte_bits)
159
+                rtp_h261_ctx->endbyte = get_bits(&gb, rtp_h261_ctx->endbyte_bits) <<
160
+                                        (8 - rtp_h261_ctx->endbyte_bits);
161
+            ebit = 0;
162
+            len  = 0;
163
+        }
164
+    }
165
+    if (ebit) {
166
+        if (len > 0)
167
+            avio_write(rtp_h261_ctx->buf, buf, len - 1);
168
+        rtp_h261_ctx->endbyte_bits = 8 - ebit;
169
+        rtp_h261_ctx->endbyte      = buf[len - 1] & (0xff << ebit);
170
+    } else {
171
+        avio_write(rtp_h261_ctx->buf, buf, len);
172
+    }
173
+
174
+    /* RTP marker bit means: last fragment of current frame was received;
175
+       otherwise, an additional fragment is needed for the current frame */
176
+    if (!(flags & RTP_FLAG_MARKER))
177
+        return AVERROR(EAGAIN);
178
+
179
+    /* write the completed last byte from the "byte merging" */
180
+    if (rtp_h261_ctx->endbyte_bits)
181
+        avio_w8(rtp_h261_ctx->buf, rtp_h261_ctx->endbyte);
182
+    rtp_h261_ctx->endbyte_bits = 0;
183
+
184
+    /* close frame buffering and create resulting A/V packet */
185
+    res = ff_rtp_finalize_packet(pkt, &rtp_h261_ctx->buf, st->index);
186
+    if (res < 0)
187
+        return res;
188
+
189
+    return 0;
190
+}
191
+
192
+RTPDynamicProtocolHandler ff_h261_dynamic_handler = {
193
+    .enc_name          = "H261",
194
+    .codec_type        = AVMEDIA_TYPE_VIDEO,
195
+    .codec_id          = AV_CODEC_ID_H261,
196
+    .init              = h261_init,
197
+    .alloc             = h261_new_context,
198
+    .free              = h261_free_context,
199
+    .parse_packet      = h261_handle_packet,
200
+    .static_payload_id = 31,
201
+};
... ...
@@ -49,6 +49,7 @@ static const AVClass rtp_muxer_class = {
49 49
 static int is_supported(enum AVCodecID id)
50 50
 {
51 51
     switch(id) {
52
+    case AV_CODEC_ID_H261:
52 53
     case AV_CODEC_ID_H263:
53 54
     case AV_CODEC_ID_H263P:
54 55
     case AV_CODEC_ID_H264:
... ...
@@ -88,7 +89,7 @@ static int is_supported(enum AVCodecID id)
88 88
 static int rtp_write_header(AVFormatContext *s1)
89 89
 {
90 90
     RTPMuxContext *s = s1->priv_data;
91
-    int n;
91
+    int n, ret = AVERROR(EINVAL);
92 92
     AVStream *st;
93 93
 
94 94
     if (s1->nb_streams != 1) {
... ...
@@ -191,6 +192,17 @@ static int rtp_write_header(AVFormatContext *s1)
191 191
         s->max_payload_size = n * TS_PACKET_SIZE;
192 192
         s->buf_ptr = s->buf;
193 193
         break;
194
+    case AV_CODEC_ID_H261:
195
+        if (s1->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
196
+            av_log(s, AV_LOG_ERROR,
197
+                   "Packetizing H261 is experimental and produces incorrect "
198
+                   "packetization for cases where GOBs don't fit into packets "
199
+                   "(even though most receivers may handle it just fine). "
200
+                   "Please set -f_strict experimental in order to enable it.\n");
201
+            ret = AVERROR_EXPERIMENTAL;
202
+            goto fail;
203
+        }
204
+        break;
194 205
     case AV_CODEC_ID_H264:
195 206
         /* check for H.264 MP4 syntax */
196 207
         if (st->codec->extradata_size > 4 && st->codec->extradata[0] == 1) {
... ...
@@ -273,7 +285,7 @@ defaultcase:
273 273
 
274 274
 fail:
275 275
     av_freep(&s->buf);
276
-    return AVERROR(EINVAL);
276
+    return ret;
277 277
 }
278 278
 
279 279
 /* send an rtcp sender report packet */
... ...
@@ -567,6 +579,9 @@ static int rtp_write_packet(AVFormatContext *s1, AVPacket *pkt)
567 567
     case AV_CODEC_ID_H264:
568 568
         ff_rtp_send_h264(s1, pkt->data, size);
569 569
         break;
570
+    case AV_CODEC_ID_H261:
571
+        ff_rtp_send_h261(s1, pkt->data, size);
572
+        break;
570 573
     case AV_CODEC_ID_H263:
571 574
         if (s->flags & FF_RTP_FLAG_RFC2190) {
572 575
             int mb_info_size = 0;
... ...
@@ -81,6 +81,7 @@ typedef struct RTPMuxContext RTPMuxContext;
81 81
 void ff_rtp_send_data(AVFormatContext *s1, const uint8_t *buf1, int len, int m);
82 82
 
83 83
 void ff_rtp_send_h264(AVFormatContext *s1, const uint8_t *buf1, int size);
84
+void ff_rtp_send_h261(AVFormatContext *s1, const uint8_t *buf1, int size);
84 85
 void ff_rtp_send_h263(AVFormatContext *s1, const uint8_t *buf1, int size);
85 86
 void ff_rtp_send_h263_rfc2190(AVFormatContext *s1, const uint8_t *buf1, int size,
86 87
                               const uint8_t *mb_info, int mb_info_size);
87 88
new file mode 100644
... ...
@@ -0,0 +1,102 @@
0
+/*
1
+ * RTP packetization for H.261 video (RFC 4587)
2
+ * Copyright (c) 2014 Thomas Volkert <thomas@homer-conferencing.com>
3
+ *
4
+ * This file is part of Libav.
5
+ *
6
+ * Libav is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * Libav is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with Libav; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+#include "avformat.h"
22
+#include "rtpenc.h"
23
+
24
+#define RTP_H261_HEADER_SIZE 4
25
+
26
+static const uint8_t *find_resync_marker_reverse(const uint8_t *restrict start,
27
+                                                 const uint8_t *restrict end)
28
+{
29
+    const uint8_t *p = end - 1;
30
+    start += 1; /* Make sure we never return the original start. */
31
+    for (; p > start; p--) {
32
+        if (p[0] == 0 && p[1] == 1)
33
+            return p;
34
+    }
35
+    return end;
36
+}
37
+
38
+void ff_rtp_send_h261(AVFormatContext *ctx, const uint8_t *frame_buf, int frame_size)
39
+{
40
+    int cur_frame_size;
41
+    int last_packet_of_frame;
42
+    RTPMuxContext *rtp_ctx = ctx->priv_data;
43
+
44
+    /* use the default 90 KHz time stamp */
45
+    rtp_ctx->timestamp = rtp_ctx->cur_timestamp;
46
+
47
+    /* continue as long as not all frame data is processed */
48
+    while (frame_size > 0) {
49
+        /*
50
+         * encode the H.261 payload header according to section 4.1 of RFC 4587:
51
+         * (uses 4 bytes between RTP header and H.261 stream per packet)
52
+         *
53
+         *    0                   1                   2                   3
54
+         *    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
55
+         *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
56
+         *   |SBIT |EBIT |I|V| GOBN  |   MBAP  |  QUANT  |  HMVD   |  VMVD   |
57
+         *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
58
+         *
59
+         *      Start bit position (SBIT): 3 bits
60
+         *      End bit position (EBIT): 3 bits
61
+         *      INTRA-frame encoded data (I): 1 bit
62
+         *      Motion Vector flag (V): 1 bit
63
+         *      GOB number (GOBN): 4 bits
64
+         *      Macroblock address predictor (MBAP): 5 bits
65
+         *      Quantizer (QUANT): 5 bits
66
+         *      Horizontal motion vector data (HMVD): 5 bits
67
+         *      Vertical motion vector data (VMVD): 5 bits
68
+         */
69
+        rtp_ctx->buf[0] = 1; /* sbit=0, ebit=0, i=0, v=1 */
70
+        rtp_ctx->buf[1] = 0; /* gobn=0, mbap=0 */
71
+        rtp_ctx->buf[2] = 0; /* quant=0, hmvd=5 */
72
+        rtp_ctx->buf[3] = 0; /* vmvd=0 */
73
+        if (frame_size < 2 || frame_buf[0] != 0 || frame_buf[1] != 1) {
74
+            /* A full, correct fix for this would be to make the H261 encoder
75
+             * support inserting extra GOB headers (triggered by setting e.g.
76
+             * "-ps 1"), and including information about macroblock boundaries
77
+             * (such as for h263_rfc2190). */
78
+            av_log(ctx, AV_LOG_WARNING,
79
+                   "RTP/H261 packet not cut at a GOB boundary, not signaled correctly\n");
80
+        }
81
+
82
+        cur_frame_size = FFMIN(rtp_ctx->max_payload_size - RTP_H261_HEADER_SIZE, frame_size);
83
+
84
+        /* look for a better place to split the frame into packets */
85
+        if (cur_frame_size < frame_size) {
86
+            const uint8_t *packet_end = find_resync_marker_reverse(frame_buf,
87
+                                                                   frame_buf + cur_frame_size);
88
+            cur_frame_size = packet_end - frame_buf;
89
+        }
90
+
91
+        /* calculate the "marker" bit for the RTP header */
92
+        last_packet_of_frame = cur_frame_size == frame_size;
93
+
94
+        /* complete and send RTP packet */
95
+        memcpy(&rtp_ctx->buf[RTP_H261_HEADER_SIZE], frame_buf, cur_frame_size);
96
+        ff_rtp_send_data(ctx, rtp_ctx->buf, RTP_H261_HEADER_SIZE + cur_frame_size, last_packet_of_frame);
97
+
98
+        frame_buf  += cur_frame_size;
99
+        frame_size -= cur_frame_size;
100
+    }
101
+}
... ...
@@ -498,6 +498,20 @@ static char *sdp_write_media_attributes(char *buff, int size, AVCodecContext *c,
498 498
                                      payload_type, mode, config ? config : "");
499 499
             break;
500 500
         }
501
+        case AV_CODEC_ID_H261:
502
+        {
503
+            const char *pic_fmt = NULL;
504
+            /* only QCIF and CIF are specified as supported in RFC 4587 */
505
+            if (c->width == 176 && c->height == 144)
506
+                pic_fmt = "QCIF=1";
507
+            else if (c->width == 352 && c->height == 288)
508
+                pic_fmt = "CIF=1";
509
+            if (payload_type >= RTP_PT_PRIVATE)
510
+                av_strlcatf(buff, size, "a=rtpmap:%d H261/90000\r\n", payload_type);
511
+            if (pic_fmt)
512
+                av_strlcatf(buff, size, "a=fmtp:%d %s\r\n", payload_type, pic_fmt);
513
+            break;
514
+        }
501 515
         case AV_CODEC_ID_H263:
502 516
         case AV_CODEC_ID_H263P:
503 517
             /* a=framesize is required by 3GPP TS 26.234 (PSS). It
... ...
@@ -30,8 +30,8 @@
30 30
 #include "libavutil/version.h"
31 31
 
32 32
 #define LIBAVFORMAT_VERSION_MAJOR 56
33
-#define LIBAVFORMAT_VERSION_MINOR  7
34
-#define LIBAVFORMAT_VERSION_MICRO  2
33
+#define LIBAVFORMAT_VERSION_MINOR  8
34
+#define LIBAVFORMAT_VERSION_MICRO  0
35 35
 
36 36
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
37 37
                                                LIBAVFORMAT_VERSION_MINOR, \