/src/ffmpeg/libavformat/rtpdec_h264.c
Line | Count | Source |
1 | | /* |
2 | | * RTP H.264 Protocol (RFC3984) |
3 | | * Copyright (c) 2006 Ryan Martell |
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 | | |
22 | | /** |
23 | | * @file |
24 | | * @brief H.264 / RTP Code (RFC3984) |
25 | | * @author Ryan Martell <rdm4@martellventures.com> |
26 | | * |
27 | | * @note Notes: |
28 | | * Notes: |
29 | | * This currently supports packetization mode: |
30 | | * Single Nal Unit Mode (0), or |
31 | | * Non-Interleaved Mode (1). It currently does not support |
32 | | * Interleaved Mode (2). (This requires implementing STAP-B, MTAP16, MTAP24, |
33 | | * FU-B packet types) |
34 | | */ |
35 | | |
36 | | #include "libavutil/attributes.h" |
37 | | #include "libavutil/base64.h" |
38 | | #include "libavutil/intreadwrite.h" |
39 | | #include "libavutil/avstring.h" |
40 | | #include "libavutil/mem.h" |
41 | | #include "avformat.h" |
42 | | |
43 | | #include "rtpdec.h" |
44 | | #include "rtpdec_formats.h" |
45 | | |
46 | | struct PayloadContext { |
47 | | // sdp setup parameters |
48 | | uint8_t profile_idc; |
49 | | uint8_t profile_iop; |
50 | | uint8_t level_idc; |
51 | | int packetization_mode; |
52 | | #ifdef DEBUG |
53 | | int packet_types_received[32]; |
54 | | #endif |
55 | | }; |
56 | | |
57 | | #ifdef DEBUG |
58 | | #define COUNT_NAL_TYPE(data, nal) data->packet_types_received[(nal) & 0x1f]++ |
59 | | #define NAL_COUNTERS data->packet_types_received |
60 | | #else |
61 | 0 | #define COUNT_NAL_TYPE(data, nal) do { } while (0) |
62 | 0 | #define NAL_COUNTERS NULL |
63 | | #endif |
64 | 0 | #define NAL_MASK 0x1f |
65 | | |
66 | | static const uint8_t start_sequence[] = { 0, 0, 0, 1 }; |
67 | | |
68 | | static void parse_profile_level_id(AVFormatContext *s, |
69 | | PayloadContext *h264_data, |
70 | | const char *value) |
71 | 0 | { |
72 | 0 | char buffer[3]; |
73 | | // 6 characters=3 bytes, in hex. |
74 | 0 | uint8_t profile_idc; |
75 | 0 | uint8_t profile_iop; |
76 | 0 | uint8_t level_idc; |
77 | |
|
78 | 0 | buffer[0] = value[0]; |
79 | 0 | buffer[1] = value[1]; |
80 | 0 | buffer[2] = '\0'; |
81 | 0 | profile_idc = strtol(buffer, NULL, 16); |
82 | 0 | buffer[0] = value[2]; |
83 | 0 | buffer[1] = value[3]; |
84 | 0 | profile_iop = strtol(buffer, NULL, 16); |
85 | 0 | buffer[0] = value[4]; |
86 | 0 | buffer[1] = value[5]; |
87 | 0 | level_idc = strtol(buffer, NULL, 16); |
88 | |
|
89 | 0 | av_log(s, AV_LOG_DEBUG, |
90 | 0 | "RTP Profile IDC: %x Profile IOP: %x Level: %x\n", |
91 | 0 | profile_idc, profile_iop, level_idc); |
92 | 0 | h264_data->profile_idc = profile_idc; |
93 | 0 | h264_data->profile_iop = profile_iop; |
94 | 0 | h264_data->level_idc = level_idc; |
95 | 0 | } |
96 | | |
97 | | int ff_h264_parse_sprop_parameter_sets(AVFormatContext *s, |
98 | | uint8_t **data_ptr, int *size_ptr, |
99 | | const char *value) |
100 | 0 | { |
101 | 0 | char base64packet[1024]; |
102 | 0 | uint8_t decoded_packet[1024]; |
103 | 0 | int packet_size; |
104 | |
|
105 | 0 | while (*value) { |
106 | 0 | char *dst = base64packet; |
107 | |
|
108 | 0 | while (*value && *value != ',' |
109 | 0 | && (dst - base64packet) < sizeof(base64packet) - 1) { |
110 | 0 | *dst++ = *value++; |
111 | 0 | } |
112 | 0 | *dst++ = '\0'; |
113 | |
|
114 | 0 | if (*value == ',') |
115 | 0 | value++; |
116 | |
|
117 | 0 | packet_size = av_base64_decode(decoded_packet, base64packet, |
118 | 0 | sizeof(decoded_packet)); |
119 | 0 | if (packet_size > 0) { |
120 | 0 | uint8_t *dest = av_realloc(*data_ptr, |
121 | 0 | packet_size + sizeof(start_sequence) + |
122 | 0 | *size_ptr + |
123 | 0 | AV_INPUT_BUFFER_PADDING_SIZE); |
124 | 0 | if (!dest) { |
125 | 0 | av_log(s, AV_LOG_ERROR, |
126 | 0 | "Unable to allocate memory for extradata!\n"); |
127 | 0 | return AVERROR(ENOMEM); |
128 | 0 | } |
129 | 0 | *data_ptr = dest; |
130 | |
|
131 | 0 | memcpy(dest + *size_ptr, start_sequence, |
132 | 0 | sizeof(start_sequence)); |
133 | 0 | memcpy(dest + *size_ptr + sizeof(start_sequence), |
134 | 0 | decoded_packet, packet_size); |
135 | 0 | memset(dest + *size_ptr + sizeof(start_sequence) + |
136 | 0 | packet_size, 0, AV_INPUT_BUFFER_PADDING_SIZE); |
137 | |
|
138 | 0 | *size_ptr += sizeof(start_sequence) + packet_size; |
139 | 0 | } |
140 | 0 | } |
141 | | |
142 | 0 | return 0; |
143 | 0 | } |
144 | | |
145 | | static int sdp_parse_fmtp_config_h264(AVFormatContext *s, |
146 | | AVStream *stream, |
147 | | PayloadContext *h264_data, |
148 | | const char *attr, const char *value) |
149 | 0 | { |
150 | 0 | AVCodecParameters *par = stream->codecpar; |
151 | |
|
152 | 0 | if (!strcmp(attr, "packetization-mode")) { |
153 | 0 | av_log(s, AV_LOG_DEBUG, "RTP Packetization Mode: %d\n", atoi(value)); |
154 | 0 | h264_data->packetization_mode = atoi(value); |
155 | | /* |
156 | | * Packetization Mode: |
157 | | * 0 or not present: Single NAL mode (Only nals from 1-23 are allowed) |
158 | | * 1: Non-interleaved Mode: 1-23, 24 (STAP-A), 28 (FU-A) are allowed. |
159 | | * 2: Interleaved Mode: 25 (STAP-B), 26 (MTAP16), 27 (MTAP24), 28 (FU-A), |
160 | | * and 29 (FU-B) are allowed. |
161 | | */ |
162 | 0 | if (h264_data->packetization_mode > 1) |
163 | 0 | av_log(s, AV_LOG_ERROR, |
164 | 0 | "Interleaved RTP mode is not supported yet.\n"); |
165 | 0 | } else if (!strcmp(attr, "profile-level-id")) { |
166 | 0 | if (strlen(value) == 6) |
167 | 0 | parse_profile_level_id(s, h264_data, value); |
168 | 0 | } else if (!strcmp(attr, "sprop-parameter-sets")) { |
169 | 0 | int ret; |
170 | 0 | if (*value == 0 || value[strlen(value) - 1] == ',') { |
171 | 0 | av_log(s, AV_LOG_WARNING, "Missing PPS in sprop-parameter-sets, ignoring\n"); |
172 | 0 | return 0; |
173 | 0 | } |
174 | 0 | par->extradata_size = 0; |
175 | 0 | av_freep(&par->extradata); |
176 | 0 | ret = ff_h264_parse_sprop_parameter_sets(s, &par->extradata, |
177 | 0 | &par->extradata_size, value); |
178 | 0 | av_log(s, AV_LOG_DEBUG, "Extradata set to %p (size: %d)\n", |
179 | 0 | par->extradata, par->extradata_size); |
180 | 0 | return ret; |
181 | 0 | } |
182 | 0 | return 0; |
183 | 0 | } |
184 | | |
185 | | void ff_h264_parse_framesize(AVCodecParameters *par, const char *p) |
186 | 0 | { |
187 | 0 | char buf1[50]; |
188 | 0 | char *dst = buf1; |
189 | | |
190 | | // remove the protocol identifier |
191 | 0 | while (*p && *p == ' ') |
192 | 0 | p++; // strip spaces. |
193 | 0 | while (*p && *p != ' ') |
194 | 0 | p++; // eat protocol identifier |
195 | 0 | while (*p && *p == ' ') |
196 | 0 | p++; // strip trailing spaces. |
197 | 0 | while (*p && *p != '-' && (dst - buf1) < sizeof(buf1) - 1) |
198 | 0 | *dst++ = *p++; |
199 | 0 | *dst = '\0'; |
200 | | |
201 | | // a='framesize:96 320-240' |
202 | | // set our parameters |
203 | 0 | par->width = atoi(buf1); |
204 | 0 | par->height = atoi(p + 1); // skip the - |
205 | 0 | } |
206 | | |
207 | | int ff_h264_handle_aggregated_packet(AVFormatContext *ctx, PayloadContext *data, AVPacket *pkt, |
208 | | const uint8_t *buf, int len, |
209 | | int skip_between, int *nal_counters, |
210 | | int nal_mask) |
211 | 0 | { |
212 | 0 | int pass = 0; |
213 | 0 | int total_length = 0; |
214 | 0 | uint8_t *dst = NULL; |
215 | 0 | int ret; |
216 | | |
217 | | // first we are going to figure out the total size |
218 | 0 | for (pass = 0; pass < 2; pass++) { |
219 | 0 | const uint8_t *src = buf; |
220 | 0 | int src_len = len; |
221 | |
|
222 | 0 | while (src_len > 2) { |
223 | 0 | uint16_t nal_size = AV_RB16(src); |
224 | | |
225 | | // consume the length of the aggregate |
226 | 0 | src += 2; |
227 | 0 | src_len -= 2; |
228 | |
|
229 | 0 | if (nal_size <= src_len) { |
230 | 0 | if (pass == 0) { |
231 | | // counting |
232 | 0 | total_length += sizeof(start_sequence) + nal_size; |
233 | 0 | } else { |
234 | | // copying |
235 | 0 | memcpy(dst, start_sequence, sizeof(start_sequence)); |
236 | 0 | dst += sizeof(start_sequence); |
237 | 0 | memcpy(dst, src, nal_size); |
238 | 0 | if (nal_counters) |
239 | 0 | nal_counters[(*src) & nal_mask]++; |
240 | 0 | dst += nal_size; |
241 | 0 | } |
242 | 0 | } else { |
243 | 0 | av_log(ctx, AV_LOG_ERROR, |
244 | 0 | "nal size exceeds length: %d %d\n", nal_size, src_len); |
245 | 0 | return AVERROR_INVALIDDATA; |
246 | 0 | } |
247 | | |
248 | | // eat what we handled |
249 | 0 | src += nal_size + skip_between; |
250 | 0 | src_len -= nal_size + skip_between; |
251 | 0 | } |
252 | | |
253 | 0 | if (pass == 0) { |
254 | | /* now we know the total size of the packet (with the |
255 | | * start sequences added) */ |
256 | 0 | if ((ret = av_new_packet(pkt, total_length)) < 0) |
257 | 0 | return ret; |
258 | 0 | dst = pkt->data; |
259 | 0 | } |
260 | 0 | } |
261 | | |
262 | 0 | return 0; |
263 | 0 | } |
264 | | |
265 | | int ff_h264_handle_frag_packet(AVPacket *pkt, const uint8_t *buf, int len, |
266 | | int start_bit, const uint8_t *nal_header, |
267 | | int nal_header_len) |
268 | 0 | { |
269 | 0 | int ret; |
270 | 0 | int tot_len = len; |
271 | 0 | int pos = 0; |
272 | 0 | if (start_bit) |
273 | 0 | tot_len += sizeof(start_sequence) + nal_header_len; |
274 | 0 | if ((ret = av_new_packet(pkt, tot_len)) < 0) |
275 | 0 | return ret; |
276 | 0 | if (start_bit) { |
277 | 0 | memcpy(pkt->data + pos, start_sequence, sizeof(start_sequence)); |
278 | 0 | pos += sizeof(start_sequence); |
279 | 0 | memcpy(pkt->data + pos, nal_header, nal_header_len); |
280 | 0 | pos += nal_header_len; |
281 | 0 | } |
282 | 0 | memcpy(pkt->data + pos, buf, len); |
283 | 0 | return 0; |
284 | 0 | } |
285 | | |
286 | | static int h264_handle_packet_fu_a(AVFormatContext *ctx, PayloadContext *data, AVPacket *pkt, |
287 | | const uint8_t *buf, int len, |
288 | | int *nal_counters, int nal_mask) |
289 | 0 | { |
290 | 0 | uint8_t fu_indicator, fu_header, start_bit, nal_type, nal; |
291 | |
|
292 | 0 | if (len < 3) { |
293 | 0 | av_log(ctx, AV_LOG_ERROR, "Too short data for FU-A H.264 RTP packet\n"); |
294 | 0 | return AVERROR_INVALIDDATA; |
295 | 0 | } |
296 | | |
297 | 0 | fu_indicator = buf[0]; |
298 | 0 | fu_header = buf[1]; |
299 | 0 | start_bit = fu_header >> 7; |
300 | 0 | nal_type = fu_header & 0x1f; |
301 | 0 | nal = fu_indicator & 0xe0 | nal_type; |
302 | | |
303 | | // skip the fu_indicator and fu_header |
304 | 0 | buf += 2; |
305 | 0 | len -= 2; |
306 | |
|
307 | 0 | if (start_bit && nal_counters) |
308 | 0 | nal_counters[nal_type & nal_mask]++; |
309 | 0 | return ff_h264_handle_frag_packet(pkt, buf, len, start_bit, &nal, 1); |
310 | 0 | } |
311 | | |
312 | | // return 0 on packet, no more left, 1 on packet, 1 on partial packet |
313 | | static int h264_handle_packet(AVFormatContext *ctx, PayloadContext *data, |
314 | | AVStream *st, AVPacket *pkt, uint32_t *timestamp, |
315 | | const uint8_t *buf, int len, uint16_t seq, |
316 | | int flags) |
317 | 0 | { |
318 | 0 | uint8_t nal; |
319 | 0 | uint8_t type; |
320 | 0 | int result = 0; |
321 | |
|
322 | 0 | if (!len) { |
323 | 0 | av_log(ctx, AV_LOG_ERROR, "Empty H.264 RTP packet\n"); |
324 | 0 | return AVERROR_INVALIDDATA; |
325 | 0 | } |
326 | 0 | nal = buf[0]; |
327 | 0 | type = nal & 0x1f; |
328 | | |
329 | | /* Simplify the case (these are all the NAL types used internally by |
330 | | * the H.264 codec). */ |
331 | 0 | if (type >= 1 && type <= 23) |
332 | 0 | type = 1; |
333 | 0 | switch (type) { |
334 | 0 | case 0: // undefined, but pass them through |
335 | 0 | case 1: |
336 | 0 | if ((result = av_new_packet(pkt, len + sizeof(start_sequence))) < 0) |
337 | 0 | return result; |
338 | 0 | memcpy(pkt->data, start_sequence, sizeof(start_sequence)); |
339 | 0 | memcpy(pkt->data + sizeof(start_sequence), buf, len); |
340 | 0 | COUNT_NAL_TYPE(data, nal); |
341 | 0 | break; |
342 | | |
343 | 0 | case 24: // STAP-A (one packet, multiple nals) |
344 | | // consume the STAP-A NAL |
345 | 0 | buf++; |
346 | 0 | len--; |
347 | 0 | result = ff_h264_handle_aggregated_packet(ctx, data, pkt, buf, len, 0, |
348 | 0 | NAL_COUNTERS, NAL_MASK); |
349 | 0 | break; |
350 | | |
351 | 0 | case 25: // STAP-B |
352 | 0 | case 26: // MTAP-16 |
353 | 0 | case 27: // MTAP-24 |
354 | 0 | case 29: // FU-B |
355 | 0 | avpriv_report_missing_feature(ctx, "RTP H.264 NAL unit type %d", type); |
356 | 0 | result = AVERROR_PATCHWELCOME; |
357 | 0 | break; |
358 | | |
359 | 0 | case 28: // FU-A (fragmented nal) |
360 | 0 | result = h264_handle_packet_fu_a(ctx, data, pkt, buf, len, |
361 | 0 | NAL_COUNTERS, NAL_MASK); |
362 | 0 | break; |
363 | | |
364 | 0 | case 30: // undefined |
365 | 0 | case 31: // undefined |
366 | 0 | default: |
367 | 0 | av_log(ctx, AV_LOG_ERROR, "Undefined type (%d)\n", type); |
368 | 0 | result = AVERROR_INVALIDDATA; |
369 | 0 | break; |
370 | 0 | } |
371 | | |
372 | 0 | pkt->stream_index = st->index; |
373 | |
|
374 | 0 | return result; |
375 | 0 | } |
376 | | |
377 | | static void h264_close_context(PayloadContext *data) |
378 | 0 | { |
379 | | #ifdef DEBUG |
380 | | int ii; |
381 | | |
382 | | for (ii = 0; ii < 32; ii++) { |
383 | | if (data->packet_types_received[ii]) |
384 | | av_log(NULL, AV_LOG_DEBUG, "Received %d packets of type %d\n", |
385 | | data->packet_types_received[ii], ii); |
386 | | } |
387 | | #endif |
388 | 0 | } |
389 | | |
390 | | static int parse_h264_sdp_line(AVFormatContext *s, int st_index, |
391 | | PayloadContext *h264_data, const char *line) |
392 | 0 | { |
393 | 0 | AVStream *stream; |
394 | 0 | const char *p = line; |
395 | |
|
396 | 0 | if (st_index < 0) |
397 | 0 | return 0; |
398 | | |
399 | 0 | stream = s->streams[st_index]; |
400 | |
|
401 | 0 | if (av_strstart(p, "framesize:", &p)) { |
402 | 0 | ff_h264_parse_framesize(stream->codecpar, p); |
403 | 0 | } else if (av_strstart(p, "fmtp:", &p)) { |
404 | 0 | return ff_parse_fmtp(s, stream, h264_data, p, sdp_parse_fmtp_config_h264); |
405 | 0 | } else if (av_strstart(p, "cliprect:", &p)) { |
406 | | // could use this if we wanted. |
407 | 0 | } |
408 | | |
409 | 0 | return 0; |
410 | 0 | } |
411 | | |
412 | | const RTPDynamicProtocolHandler ff_h264_dynamic_handler = { |
413 | | .enc_name = "H264", |
414 | | .codec_type = AVMEDIA_TYPE_VIDEO, |
415 | | .codec_id = AV_CODEC_ID_H264, |
416 | | .need_parsing = AVSTREAM_PARSE_FULL, |
417 | | .priv_data_size = sizeof(PayloadContext), |
418 | | .parse_sdp_a_line = parse_h264_sdp_line, |
419 | | .close = h264_close_context, |
420 | | .parse_packet = h264_handle_packet, |
421 | | }; |