1 /*
2 * RTP packetization for H.264 (RFC3984)
3 * RTP packetizer for HEVC/H.265 payload format (draft version 6)
4 * Copyright (c) 2008 Luca Abeni
5 * Copyright (c) 2014 Thomas Volkert <thomas@homer-conferencing.com>
6 *
7 * This file is part of FFmpeg.
8 *
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 /**
25 * @file
26 * @brief H.264/HEVC packetization
27 * @author Luca Abeni <lucabe72@email.it>
28 */
29
31
36
38 {
40 if (
s->buf_ptr !=
s->buf) {
41 // If we're only sending one single NAL unit, send it as such, skip
42 // the STAP-A/AP framing
43 if (
s->buffered_nals == 1) {
47 else
49 } else
51 }
54 }
55
57 {
60
62 if (size <= s->max_payload_size) {
63 int buffered_size =
s->buf_ptr -
s->buf;
64 int header_size;
65 int skip_aggregate = 0;
66
68 header_size = 1;
70 } else {
71 header_size = 2;
72 }
73
74 // Flush buffered NAL units if the current unit doesn't fit
75 if (buffered_size + 2 +
size >
s->max_payload_size) {
77 buffered_size = 0;
78 }
79 // If we aren't using mode 0, and the NAL unit fits including the
80 // framing (2 bytes length, plus 1/2 bytes for the STAP-A/AP marker),
81 // write the unit to the buffer as a STAP-A/AP packet, otherwise flush
82 // and send as single NAL.
83 if (buffered_size + 2 + header_size + size <= s->max_payload_size &&
84 !skip_aggregate) {
85 if (buffered_size == 0) {
88 } else {
89 *
s->buf_ptr++ = 48 << 1;
91 }
92 }
95 memcpy(
s->buf_ptr, buf,
size);
98 } else {
101 }
102 } else {
103 int flag_byte, header_size;
107 "NAL size %d > %d, try -slice-max-size %d\n",
size,
108 s->max_payload_size,
s->max_payload_size);
109 return;
110 }
113 uint8_t
type = buf[0] & 0x1F;
114 uint8_t nri = buf[0] & 0x60;
115
116 s->buf[0] = 28;
/* FU Indicator; Type = 28 ---> FU-A */
120 buf += 1;
122
123 flag_byte = 1;
124 header_size = 2;
125 } else {
126 uint8_t nal_type = (buf[0] >> 1) & 0x3F;
127 /*
128 * create the HEVC payload header and transmit the buffer as fragmentation units (FU)
129 *
130 * 0 1
131 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
132 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
133 * |F| Type | LayerId | TID |
134 * +-------------+-----------------+
135 *
136 * F = 0
137 * Type = 49 (fragmentation unit (FU))
138 * LayerId = 0
139 * TID = 1
140 */
143
144 /*
145 * create the FU header
146 *
147 * 0 1 2 3 4 5 6 7
148 * +-+-+-+-+-+-+-+-+
149 * |S|E| FuType |
150 * +---------------+
151 *
152 * S = variable
153 * E = variable
154 * FuType = NAL unit type
155 */
156 s->buf[2] = nal_type;
157 /* set the S bit: mark as start fragment */
159
160 /* pass the original NAL header */
161 buf += 2;
163
164 flag_byte = 2;
165 header_size = 3;
166 }
167
168 while (
size + header_size >
s->max_payload_size) {
169 memcpy(&
s->buf[header_size], buf,
s->max_payload_size - header_size);
171 buf +=
s->max_payload_size - header_size;
172 size -=
s->max_payload_size - header_size;
173 s->buf[flag_byte] &= ~(1 << 7);
174 }
175 s->buf[flag_byte] |= 1 << 6;
176 memcpy(&
s->buf[header_size], buf,
size);
178 }
179 }
180
182 {
183 const uint8_t *
r, *end = buf1 +
size;
185
186 s->timestamp =
s->cur_timestamp;
188 if (
s->nal_length_size)
190 else
193 const uint8_t *r1;
194
195 if (
s->nal_length_size) {
197 if (!r1)
198 r1 = end;
199 // Check that the last is not truncated
200 if (r1 - r < s->nal_length_size)
201 break;
202 r +=
s->nal_length_size;
203 } else {
206 }
209 }
211 }