1 /*
2 * RTP parser for H.261 payload format (RFC 4587)
3 * Copyright (c) 2014 Thomas Volkert <thomas@homer-conferencing.com>
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
26
27 #define RTP_H261_PAYLOAD_HEADER_SIZE 4
28
34 };
35
37 {
38 /* return if context is invalid */
39 if (!pl_ctx)
40 return;
41
42 /* free buffer if it is valid */
44 }
45
48 const uint8_t *buf,
int len, uint16_t seq,
50 {
51 int sbit, ebit, gobn, mbap,
quant;
52 int res;
53
54 /* drop data of previous packets in case of non-continuous (lossy) packet stream */
55 if (rtp_h261_ctx->
buf && rtp_h261_ctx->
timestamp != *timestamp) {
58 }
59
60 /* sanity check for size of input packet: 1 byte payload at least */
64 }
65
66 /*
67 * decode the H.261 payload header according to section 4.1 of RFC 4587:
68 * (uses 4 bytes between RTP header and H.261 stream per packet)
69 *
70 * 0 1 2 3
71 * 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
72 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
73 * |SBIT |EBIT |I|V| GOBN | MBAP | QUANT | HMVD | VMVD |
74 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
75 *
76 * Start bit position (SBIT): 3 bits
77 * End bit position (EBIT): 3 bits
78 * INTRA-frame encoded data (I): 1 bit
79 * Motion Vector flag (V): 1 bit
80 * GOB number (GOBN): 4 bits
81 * Macroblock address predictor (MBAP): 5 bits
82 * Quantizer (QUANT): 5 bits
83 * Horizontal motion vector data (HMVD): 5 bits
84 * Vertical motion vector data (VMVD): 5 bits
85 */
86 sbit = (buf[0] >> 5) & 0x07;
87 ebit = (buf[0] >> 2) & 0x07;
88 gobn = (buf[1] >> 4) & 0x0f;
89 mbap = ((buf[1] << 1) & 0x1e) | ((buf[2] >> 7) & 0x01);
90 quant = (buf[2] >> 2) & 0x1f;
91
92 /* pass the H.261 payload header and continue with the actual payload */
95
96 /* start frame buffering with new dynamic buffer */
97 if (!rtp_h261_ctx->
buf) {
98 /* sanity check: a new frame starts with gobn=0, sbit=0, mbap=0, quant=0 */
99 if (!gobn && !sbit && !mbap && !
quant) {
101 if (res < 0)
102 return res;
103 /* update the timestamp in the frame packet with the one from the RTP packet */
105 } else {
106 /* frame not started yet, need more packets */
108 }
109 }
110
111 /* do the "byte merging" at the boundaries of two consecutive frame fragments */
114 rtp_h261_ctx->
endbyte |= buf[0] & (0xff >> sbit);
116 buf++;
119 } else {
120 /* ebit/sbit values inconsistent, assuming packet loss */
123 if (res < 0)
124 return res;
129 }
136 ebit = 0;
138 }
139 }
140 if (ebit) {
144 rtp_h261_ctx->
endbyte = buf[
len - 1] & (0xff << ebit);
145 } else {
147 }
148
149 /* RTP marker bit means: last fragment of current frame was received;
150 otherwise, an additional fragment is needed for the current frame */
153
154 /* write the completed last byte from the "byte merging" */
158
159 /* close frame buffering and create resulting A/V packet */
161 if (res < 0)
162 return res;
163
164 return 0;
165 }
166
175 .static_payload_id = 31,
176 };