Debian Codegraph @

Xpjproject/pjmedia/src/pjmedia-codec/vpx_packetizer.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2020 Teluu Inc. (http://www.teluu.com)
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 #include <pjmedia-codec/vpx_packetizer.h>
19 #include <pjmedia/errno.h>
20 #include <pjmedia/types.h>
21 #include <pjmedia/vid_codec_util.h>
22 #include <pj/assert.h>
23 #include <pj/errno.h>
24 #include <pj/log.h>
25 #include <pj/pool.h>
26 #include <pj/string.h>
27
28
29 #if defined(PJMEDIA_HAS_VIDEO) && (PJMEDIA_HAS_VIDEO != 0)
30
31 #define THIS_FILE "vpx_packetizer.c"
32
33 /* VPX packetizer definition */
34 struct pjmedia_vpx_packetizer
35 {
36 /* Current settings */
37 pjmedia_vpx_packetizer_cfg cfg;
38 unsigned int picture_id;
39 };
40
41 /*
42 * Initialize VPX packetizer.
43 */
44 PJ_DEF(void) pjmedia_vpx_packetizer_cfg_default(pjmedia_vpx_packetizer_cfg *cfg)
45 {
46 pj_bzero(cfg, sizeof(*cfg));
47
48 cfg->fmt_id = PJMEDIA_FORMAT_VP8;
49 cfg->mtu =PJMEDIA_MAX_VID_PAYLOAD_SIZE;
50 }
51
52 /*
53 * Create vpx packetizer.
54 */
55 PJ_DEF(pj_status_t) pjmedia_vpx_packetizer_create(
56 pj_pool_t *pool,
57 const pjmedia_vpx_packetizer_cfg *cfg,
58 pjmedia_vpx_packetizer **p)
59 {
60 pjmedia_vpx_packetizer *p_;
61
62 PJ_ASSERT_RETURN(pool && p, PJ_EINVAL);
63
64 if (cfg && cfg->fmt_id != PJMEDIA_FORMAT_VP8 &&
65 cfg->fmt_id != PJMEDIA_FORMAT_VP9)
66 {
67 return PJ_ENOTSUP;
68 }
69
70 p_ = PJ_POOL_ZALLOC_T(pool, pjmedia_vpx_packetizer);
71 if (cfg) {
72 pj_memcpy(&p_->cfg, cfg, sizeof(*cfg));
73 } else {
74 pjmedia_vpx_packetizer_cfg_default(&p_->cfg);
75 }
76 *p = p_;
77
78 return PJ_SUCCESS;
79 }
80
81 /*
82 * Generate an RTP payload from vpx frame bitstream, in-place processing.
83 */
84 PJ_DEF(pj_status_t) pjmedia_vpx_packetize(const pjmedia_vpx_packetizer *pktz,
85 pj_size_t bits_len,
86 unsigned *bits_pos,
87 pj_bool_t is_keyframe,
88 pj_uint8_t **payload,
89 pj_size_t *payload_len)
90 {
91 unsigned payload_desc_size = (pktz->cfg.fmt_id == PJMEDIA_FORMAT_VP8? 4: 1);
92 unsigned max_size = pktz->cfg.mtu - payload_desc_size;
93 unsigned remaining_size = (unsigned)bits_len - *bits_pos;
94 unsigned out_size = (unsigned)*payload_len;
95 pj_uint8_t *bits = *payload;
96
97 *payload_len = PJ_MIN(remaining_size, max_size);
98 if (*payload_len + payload_desc_size > out_size)
99 return PJMEDIA_CODEC_EFRMTOOSHORT;
100
101 /* Set payload header */
102 bits[0] = 0;
103 if (pktz->cfg.fmt_id == PJMEDIA_FORMAT_VP8) {
104 /* For VP8, use 4 bytes payload desc, see #4659 for more info */
105 bits[0] = 0x80;
106
107 /* Set S: Start of VP8 partition. */
108 if (*bits_pos == 0) {
109 bits[0] |= 0x10;
110 /* Increment the picture_id when the S-bit is present */
111 ((pjmedia_vpx_packetizer *)pktz)->picture_id++;
112 }
113
114 /* Set Extended Control Bits ILTK */
115 bits[1] = 0x80;
116 /* Add dual-octet picture_id */
117 bits[2] = ((pktz->picture_id & 0x7f00) >> 8) | 0x80;
118 bits[3] = pktz->picture_id & 0xff;
119
120 /* Set N: Non-reference frame */
121 if (!is_keyframe) bits[0] |= 0x20;
122 } else if (pktz->cfg.fmt_id == PJMEDIA_FORMAT_VP9) {
123 /* Set P: Inter-picture predicted frame */
124 if (!is_keyframe) bits[0] |= 0x40;
125 /* Set B: Start of a frame */
126 if (*bits_pos == 0) bits[0] |= 0x8;
127 /* Set E: End of a frame */
128 if (*bits_pos + *payload_len == bits_len) {
129 bits[0] |= 0x4;
130 }
131 }
132 return PJ_SUCCESS;
133 }
134
135
136 /*
137 * Append RTP payload to a VPX picture bitstream
138 */
139 PJ_DEF(pj_status_t) pjmedia_vpx_unpacketize(pjmedia_vpx_packetizer *pktz,
140 const pj_uint8_t *payload,
141 pj_size_t payload_len,
142 unsigned *payload_desc_len)
143 {
144 unsigned desc_len = 1;
145 pj_uint8_t *p = (pj_uint8_t *)payload;
146
147 #define INC_DESC_LEN() {if (++desc_len >= payload_len) return PJ_ETOOSMALL;}
148
149 if (payload_len <= desc_len) return PJ_ETOOSMALL;
150
151 if (pktz->cfg.fmt_id == PJMEDIA_FORMAT_VP8) {
152 /* 0 1 2 3 4 5 6 7
153 * +-+-+-+-+-+-+-+-+
154 * |X|R|N|S|R| PID | (REQUIRED)
155 */
156 /* X: Extended control bits present. */
157 if (p[0] & 0x80) {
158 INC_DESC_LEN();
159 /* |I|L|T|K| RSV | */
160 /* I: PictureID present. */
161 if (p[1] & 0x80) {
162 INC_DESC_LEN();
163 /* If M bit is set, the PID field MUST contain 15 bits. */
164 if (p[2] & 0x80) INC_DESC_LEN();
165 }
166 /* L: TL0PICIDX present. */
167 if (p[1] & 0x40) INC_DESC_LEN();
168 /* T: TID present or K: KEYIDX present. */
169 if ((p[1] & 0x20) || (p[1] & 0x10)) INC_DESC_LEN();
170 }
171
172 } else if (pktz->cfg.fmt_id == PJMEDIA_FORMAT_VP9) {
173 /* 0 1 2 3 4 5 6 7
174 * +-+-+-+-+-+-+-+-+
175 * |I|P|L|F|B|E|V|-| (REQUIRED)
176 */
177 /* I: Picture ID (PID) present. */
178 if (p[0] & 0x80) {
179 INC_DESC_LEN();
180 /* If M bit is set, the PID field MUST contain 15 bits. */
181 if (p[1] & 0x80) INC_DESC_LEN();
182 }
183 /* L: Layer indices present. */
184 if (p[0] & 0x20) {
185 INC_DESC_LEN();
186 if (!(p[0] & 0x10)) INC_DESC_LEN();
187 }
188 /* F: Flexible mode.
189 * I must also be set to 1, and if P is set, there's up to 3
190 * reference index.
191 */
192 if ((p[0] & 0x10) && (p[0] & 0x80) && (p[0] & 0x40)) {
193 unsigned char *q = p + desc_len;
194
195 INC_DESC_LEN();
196 if (*q & 0x1) {
197 q++;
198 INC_DESC_LEN();
199 if (*q & 0x1) {
200 q++;
201 INC_DESC_LEN();
202 }
203 }
204 }
205 /* V: Scalability structure (SS) data present. */
206 if (p[0] & 0x2) {
207 unsigned char *q = p + desc_len;
208 unsigned N_S = (*q >> 5) + 1;
209
210 INC_DESC_LEN();
211 /* Y: Each spatial layer's frame resolution present. */
212 if (*q & 0x10) {
213 desc_len += N_S * 4;
214 if (desc_len >= payload_len)
215 return PJ_ETOOSMALL;
216 }
217
218 /* G: PG description present flag. */
219 if (*q & 0x8) {
220 unsigned j;
221 unsigned N_G = *(p + desc_len);
222
223 INC_DESC_LEN();
224 for (j = 0; j< N_G; j++) {
225 unsigned R;
226
227 q = p + desc_len;
228 INC_DESC_LEN();
229 R = (*q & 0x0F) >> 2;
230 desc_len += R;
231 if (desc_len >= payload_len)
232 return PJ_ETOOSMALL;
233 }
234 }
235 }
236 }
237 #undef INC_DESC_LEN
238
239 *payload_desc_len = desc_len;
240 return PJ_SUCCESS;
241 }
242
243
244 #endif /* PJMEDIA_HAS_VIDEO */