FFmpeg
Loading...
Searching...
No Matches
sdp.c
Go to the documentation of this file.
1/*
2 * copyright (c) 2007 Luca Abeni
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg 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 * FFmpeg 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 FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include "config_components.h"
22
23#include <string.h>
24#include "libavutil/avstring.h"
25#include "libavutil/base64.h"
26#include "libavutil/dict.h"
27#include "libavutil/mem.h"
29#include "libavutil/opt.h"
30#include "libavcodec/xiph.h"
32#include "avformat.h"
33#include "internal.h"
34#include "av1.h"
35#include "avc.h"
36#include "hevc.h"
37#include "nal.h"
38#include "rtp.h"
39#include "version.h"
40#if CONFIG_NETWORK
41#include "network.h"
42#endif
43
44#if CONFIG_RTP_MUXER
45#define MAX_EXTRADATA_SIZE ((INT_MAX - 10) / 2)
46
47struct sdp_session_level {
48 int sdp_version; /**< protocol version (currently 0) */
49 int id; /**< session ID */
50 int version; /**< session version */
51 int start_time; /**< session start time (NTP time, in seconds),
52 or 0 in case of permanent session */
53 int end_time; /**< session end time (NTP time, in seconds),
54 or 0 if the session is not bounded */
55 int ttl; /**< TTL, in case of multicast stream */
56 const char *user; /**< username of the session's creator */
57 const char *src_addr; /**< IP address of the machine from which the session was created */
58 const char *src_type; /**< address type of src_addr */
59 const char *dst_addr; /**< destination IP address (can be multicast) */
60 const char *dst_type; /**< destination IP address type */
61 const char *name; /**< session name (can be an empty string) */
62};
63
64static void sdp_write_address(char *buff, int size, const char *dest_addr,
65 const char *dest_type, int ttl)
66{
67 if (dest_addr) {
68 if (!dest_type)
69 dest_type = "IP4";
70 if (ttl > 0 && !strcmp(dest_type, "IP4")) {
71 /* The TTL should only be specified for IPv4 multicast addresses,
72 * not for IPv6. */
73 av_strlcatf(buff, size, "c=IN %s %s/%d\r\n", dest_type, dest_addr, ttl);
74 } else {
75 av_strlcatf(buff, size, "c=IN %s %s\r\n", dest_type, dest_addr);
76 }
77 }
78}
79
80static void sdp_write_header(char *buff, int size, struct sdp_session_level *s)
81{
82 av_strlcatf(buff, size, "v=%d\r\n"
83 "o=- %d %d IN %s %s\r\n"
84 "s=%s\r\n",
85 s->sdp_version,
86 s->id, s->version, s->src_type, s->src_addr,
87 s->name);
88 sdp_write_address(buff, size, s->dst_addr, s->dst_type, s->ttl);
89 av_strlcatf(buff, size, "t=%d %d\r\n"
90 "a=tool:libavformat " AV_STRINGIFY(LIBAVFORMAT_VERSION) "\r\n",
91 s->start_time, s->end_time);
92}
93
94#if CONFIG_NETWORK
95static int resolve_destination(char *dest_addr, int size, char *type,
96 int type_size)
97{
98 struct addrinfo hints = { 0 }, *ai;
99 int is_multicast;
100
101 av_strlcpy(type, "IP4", type_size);
102 if (!dest_addr[0])
103 return 0;
104
105 /* Resolve the destination, since it must be written
106 * as a numeric IP address in the SDP. */
107
108 if (getaddrinfo(dest_addr, NULL, &hints, &ai))
109 return 0;
110 getnameinfo(ai->ai_addr, ai->ai_addrlen, dest_addr, size,
111 NULL, 0, NI_NUMERICHOST);
112#ifdef AF_INET6
113 if (ai->ai_family == AF_INET6)
114 av_strlcpy(type, "IP6", type_size);
115#endif
116 is_multicast = ff_is_multicast_address(ai->ai_addr);
117 freeaddrinfo(ai);
118 return is_multicast;
119}
120#else
121static int resolve_destination(char *dest_addr, int size, char *type,
122 int type_size)
123{
124 return 0;
125}
126#endif
127
128static int sdp_get_address(char *dest_addr, int size, int *ttl, const char *url)
129{
130 int port;
131 const char *p;
132 char proto[32];
133
134 av_url_split(proto, sizeof(proto), NULL, 0, dest_addr, size, &port, NULL, 0, url);
135
136 *ttl = 0;
137
138 if (strcmp(proto, "rtp") && strcmp(proto, "srtp")) {
139 /* The url isn't for the actual rtp sessions,
140 * don't parse out anything else than the destination.
141 */
142 return 0;
143 }
144
145 p = strchr(url, '?');
146 if (p) {
147 char buff[64];
148
149 if (av_find_info_tag(buff, sizeof(buff), "ttl", p)) {
150 *ttl = strtol(buff, NULL, 10);
151 } else {
152 *ttl = 5;
153 }
154 }
155
156 return port;
157}
158
159static int extradata2psets_av1(AVFormatContext *s, const AVCodecParameters *par,
160 char **out)
161{
162 char *psets;
164
165 if (ff_av1_parse_seq_header(&seq, par->extradata, par->extradata_size) < 0)
166 return AVERROR_INVALIDDATA;
167
168 psets = av_mallocz(64);
169 if (!psets) {
170 av_log(s, AV_LOG_ERROR, "Cannot allocate memory for the parameter sets.\n");
171 return AVERROR(ENOMEM);
172 }
173 av_strlcatf(psets, 64, "profile=%u;level-idx=%u;tier=%u",
174 seq.profile, seq.level, seq.tier);
175 *out = psets;
176 return 0;
177}
178
179#define MAX_PSET_SIZE 1024
180static int extradata2psets(AVFormatContext *s, const AVCodecParameters *par,
181 char **out)
182{
183 char *psets, *p;
184 const uint8_t *r;
185 static const char pset_string[] = "; sprop-parameter-sets=";
186 static const char profile_string[] = "; profile-level-id=";
187 uint8_t *extradata = par->extradata;
188 int extradata_size = par->extradata_size;
189 uint8_t *tmpbuf = NULL;
190 const uint8_t *sps = NULL, *sps_end;
191
192 *out = NULL;
193
195 av_log(s, AV_LOG_ERROR, "Too much extradata!\n");
196 return AVERROR_INVALIDDATA;
197 }
198 if (par->extradata[0] == 1) {
199 int ret = ff_avc_write_annexb_extradata(par->extradata, &extradata,
200 &extradata_size);
201 if (ret < 0)
202 return ret;
203
204 tmpbuf = extradata;
205 }
206
207 psets = av_mallocz(MAX_PSET_SIZE);
208 if (!psets) {
209 av_log(s, AV_LOG_ERROR, "Cannot allocate memory for the parameter sets.\n");
210 av_free(tmpbuf);
211 return AVERROR(ENOMEM);
212 }
213 memcpy(psets, pset_string, strlen(pset_string));
214 p = psets + strlen(pset_string);
215 r = ff_nal_find_startcode(extradata, extradata + extradata_size);
216 while (r < extradata + extradata_size) {
217 const uint8_t *r1;
218 uint8_t nal_type;
219
220 while (!*(r++));
221 nal_type = *r & 0x1f;
222 r1 = ff_nal_find_startcode(r, extradata + extradata_size);
223 if (nal_type != 7 && nal_type != 8) { /* Only output SPS and PPS */
224 r = r1;
225 continue;
226 }
227 if (p != (psets + strlen(pset_string))) {
228 if (p - psets >= MAX_PSET_SIZE)
229 goto fail_in_loop;
230 *p = ',';
231 p++;
232 }
233 if (!sps) {
234 sps = r;
235 sps_end = r1;
236 }
237 if (!av_base64_encode(p, MAX_PSET_SIZE - (p - psets), r, r1 - r)) {
238 av_log(s, AV_LOG_ERROR, "Cannot Base64-encode %td %td!\n",
239 MAX_PSET_SIZE - (p - psets), r1 - r);
240fail_in_loop:
241 av_free(psets);
242 av_free(tmpbuf);
243
244 return AVERROR_INVALIDDATA;
245 }
246 p += strlen(p);
247 r = r1;
248 }
249 if (sps && sps_end - sps >= 4 && p - psets <= MAX_PSET_SIZE - strlen(profile_string) - 7) {
250 memcpy(p, profile_string, strlen(profile_string));
251 p += strlen(p);
252 ff_data_to_hex(p, sps + 1, 3, 0);
253 }
254 av_free(tmpbuf);
255
256 *out = psets;
257 return 0;
258}
259
260static int extradata2psets_hevc(AVFormatContext *fmt, const AVCodecParameters *par,
261 char **out)
262{
263 char *psets;
264 uint8_t *extradata = par->extradata;
265 int extradata_size = par->extradata_size;
266 uint8_t *tmpbuf = NULL;
267 int ps_pos[3] = { 0 };
268 static const char * const ps_names[3] = { "vps", "sps", "pps" };
269 int num_arrays, num_nalus;
270 int pos, i, j, ret = 0;
271
272 *out = NULL;
273
274 // Convert to hvcc format. Since we need to group multiple NALUs of
275 // the same type, and we might need to convert from one format to the
276 // other anyway, we get away with a little less work by using the hvcc
277 // format.
278 if (par->extradata[0] != 1) {
279 AVIOContext *pb;
280
281 ret = avio_open_dyn_buf(&pb);
282 if (ret < 0)
283 return ret;
284
285 ret = ff_isom_write_hvcc(pb, par->extradata, par->extradata_size, 0, fmt);
286 if (ret < 0) {
287 avio_close_dyn_buf(pb, &tmpbuf);
288 goto err;
289 }
290 extradata_size = avio_close_dyn_buf(pb, &extradata);
291 tmpbuf = extradata;
292 }
293
294 if (extradata_size < 23)
295 goto err;
296
297 num_arrays = extradata[22];
298 pos = 23;
299 for (i = 0; i < num_arrays; i++) {
300 int num_nalus, nalu_type;
301 if (pos + 3 > extradata_size)
302 goto err;
303 nalu_type = extradata[pos] & 0x3f;
304 // Not including libavcodec/hevc.h to avoid confusion between
305 // NAL_* with the same name for both H.264 and HEVC.
306 if (nalu_type == 32) // VPS
307 ps_pos[0] = pos;
308 else if (nalu_type == 33) // SPS
309 ps_pos[1] = pos;
310 else if (nalu_type == 34) // PPS
311 ps_pos[2] = pos;
312 num_nalus = AV_RB16(&extradata[pos + 1]);
313 pos += 3;
314 for (j = 0; j < num_nalus; j++) {
315 int len;
316 if (pos + 2 > extradata_size)
317 goto err;
318 len = AV_RB16(&extradata[pos]);
319 pos += 2;
320 if (pos + len > extradata_size)
321 goto err;
322 pos += len;
323 }
324 }
325 if (!ps_pos[0] || !ps_pos[1] || !ps_pos[2])
326 goto err;
327
328 psets = av_mallocz(MAX_PSET_SIZE);
329 if (!psets) {
330 ret = AVERROR(ENOMEM);
331 goto err;
332 }
333
334 psets[0] = '\0';
335
336 for (i = 0; i < 3; i++) {
337 pos = ps_pos[i];
338
339 if (i > 0)
340 av_strlcat(psets, "; ", MAX_PSET_SIZE);
341 av_strlcatf(psets, MAX_PSET_SIZE, "sprop-%s=", ps_names[i]);
342
343 // Skipping boundary checks in the input here; we've already traversed
344 // the whole hvcc structure above without issues
345 num_nalus = AV_RB16(&extradata[pos + 1]);
346 pos += 3;
347 for (j = 0; j < num_nalus; j++) {
348 int len = AV_RB16(&extradata[pos]);
349 int strpos;
350 pos += 2;
351 if (j > 0)
352 av_strlcat(psets, ",", MAX_PSET_SIZE);
353 strpos = strlen(psets);
354 if (!av_base64_encode(psets + strpos, MAX_PSET_SIZE - strpos,
355 &extradata[pos], len)) {
356 av_free(psets);
357 goto err;
358 }
359 pos += len;
360 }
361 }
362 av_free(tmpbuf);
363
364 *out = psets;
365 return 0;
366err:
367 if (ret >= 0)
369 av_free(tmpbuf);
370 return ret;
371}
372
373static int extradata2config(AVFormatContext *s, const AVCodecParameters *par,
374 char **out)
375{
376 char *config;
377
378 *out = NULL;
379
381 av_log(s, AV_LOG_ERROR, "Too much extradata!\n");
382 return AVERROR_INVALIDDATA;
383 }
384 config = av_malloc(10 + par->extradata_size * 2);
385 if (!config) {
386 av_log(s, AV_LOG_ERROR, "Cannot allocate memory for the config info.\n");
387 return AVERROR(ENOMEM);
388 }
389 memcpy(config, "; config=", 9);
390 ff_data_to_hex(config + 9, par->extradata, par->extradata_size, 0);
391
392 *out = config;
393 return 0;
394}
395
396static int xiph_extradata2config(AVFormatContext *s, const AVCodecParameters *par,
397 char **out)
398{
399 uint8_t *config;
400 char *encoded_config;
401 const uint8_t *header_start[3];
402 int headers_len, header_len[3], config_len;
403 int first_header_size, ret;
404
405 *out = NULL;
406
407 switch (par->codec_id) {
409 first_header_size = 42;
410 break;
412 first_header_size = 30;
413 break;
414 default:
415 av_log(s, AV_LOG_ERROR, "Unsupported Xiph codec ID\n");
416 return AVERROR(ENOSYS);
417 }
418
420 first_header_size, header_start,
421 header_len);
422 if (ret < 0) {
423 av_log(s, AV_LOG_ERROR, "Extradata corrupt.\n");
424 return ret;
425 }
426
427 headers_len = header_len[0] + header_len[2];
428 config_len = 4 + // count
429 3 + // ident
430 2 + // packet size
431 1 + // header count
432 2 + // header size
433 headers_len; // and the rest
434
435 config = av_malloc(config_len);
436 if (!config)
437 goto xiph_fail;
438
439 encoded_config = av_malloc(AV_BASE64_SIZE(config_len));
440 if (!encoded_config) {
441 av_free(config);
442 goto xiph_fail;
443 }
444
445 config[0] = config[1] = config[2] = 0;
446 config[3] = 1;
447 config[4] = (RTP_XIPH_IDENT >> 16) & 0xff;
448 config[5] = (RTP_XIPH_IDENT >> 8) & 0xff;
449 config[6] = (RTP_XIPH_IDENT ) & 0xff;
450 config[7] = (headers_len >> 8) & 0xff;
451 config[8] = headers_len & 0xff;
452 config[9] = 2;
453 config[10] = header_len[0];
454 config[11] = 0; // size of comment header; nonexistent
455 memcpy(config + 12, header_start[0], header_len[0]);
456 memcpy(config + 12 + header_len[0], header_start[2], header_len[2]);
457
458 av_base64_encode(encoded_config, AV_BASE64_SIZE(config_len),
459 config, config_len);
460 av_free(config);
461
462 *out = encoded_config;
463 return 0;
464
465xiph_fail:
467 "Not enough memory for configuration string\n");
468 return AVERROR(ENOMEM);
469}
470
471static int latm_context2profilelevel(const AVCodecParameters *par)
472{
473 /* MP4A-LATM
474 * The RTP payload format specification is described in RFC 3016
475 * The encoding specifications are provided in ISO/IEC 14496-3 */
476
477 int profile_level = 0x2B;
478
479 /* TODO: AAC Profile only supports AAC LC Object Type.
480 * Different Object Types should implement different Profile Levels */
481
482 if (par->sample_rate <= 24000) {
483 if (par->ch_layout.nb_channels <= 2)
484 profile_level = 0x28; // AAC Profile, Level 1
485 } else if (par->sample_rate <= 48000) {
486 if (par->ch_layout.nb_channels <= 2) {
487 profile_level = 0x29; // AAC Profile, Level 2
488 } else if (par->ch_layout.nb_channels <= 5) {
489 profile_level = 0x2A; // AAC Profile, Level 4
490 }
491 } else if (par->sample_rate <= 96000) {
492 if (par->ch_layout.nb_channels <= 5) {
493 profile_level = 0x2B; // AAC Profile, Level 5
494 }
495 }
496
497 return profile_level;
498}
499
500static int latm_context2config(AVFormatContext *s, const AVCodecParameters *par,
501 char **out)
502{
503 /* MP4A-LATM
504 * The RTP payload format specification is described in RFC 3016
505 * The encoding specifications are provided in ISO/IEC 14496-3 */
506
507 uint8_t config_byte[6];
508 int rate_index;
509 char *config;
510
511 *out = NULL;
512
513 for (rate_index = 0; rate_index < 16; rate_index++)
514 if (ff_mpeg4audio_sample_rates[rate_index] == par->sample_rate)
515 break;
516 if (rate_index == 16) {
517 av_log(s, AV_LOG_ERROR, "Unsupported sample rate\n");
518 return AVERROR(ENOSYS);
519 }
520
521 config_byte[0] = 0x40;
522 config_byte[1] = 0;
523 config_byte[2] = 0x20 | rate_index;
524 config_byte[3] = par->ch_layout.nb_channels << 4;
525 config_byte[4] = 0x3f;
526 config_byte[5] = 0xc0;
527
528 config = av_malloc(6*2+1);
529 if (!config) {
530 av_log(s, AV_LOG_ERROR, "Cannot allocate memory for the config info.\n");
531 return AVERROR(ENOMEM);
532 }
533 ff_data_to_hex(config, config_byte, 6, 1);
534
535 *out = config;
536 return 0;
537}
538
539static int sdp_write_media_attributes(char *buff, int size, const AVStream *st,
540 int payload_type, AVFormatContext *fmt)
541{
542 char *config = NULL;
543 const AVCodecParameters *p = st->codecpar;
544 int ret = 0;
545
546 switch (p->codec_id) {
547 case AV_CODEC_ID_AV1:
548 av_strlcatf(buff, size, "a=rtpmap:%d AV1/90000\r\n", payload_type);
549 if (p->extradata_size) {
550 ret = extradata2psets_av1(fmt, p, &config);
551 if (ret < 0)
552 return ret;
553 av_strlcatf(buff, size, "a=fmtp:%d %s\r\n", payload_type, config);
554 }
555 break;
557 av_strlcatf(buff, size, "a=rtpmap:%d VC2/90000\r\n", payload_type);
558 break;
559 case AV_CODEC_ID_H264: {
560 int mode = 1;
561 if (fmt && fmt->oformat && fmt->oformat->priv_class &&
562 av_opt_flag_is_set(fmt->priv_data, "rtpflags", "h264_mode0"))
563 mode = 0;
564 if (p->extradata_size) {
565 ret = extradata2psets(fmt, p, &config);
566 if (ret < 0)
567 return ret;
568 }
569 av_strlcatf(buff, size, "a=rtpmap:%d H264/90000\r\n"
570 "a=fmtp:%d packetization-mode=%d%s\r\n",
571 payload_type,
572 payload_type, mode, config ? config : "");
573 break;
574 }
575 case AV_CODEC_ID_H261:
576 {
577 const char *pic_fmt = NULL;
578 /* only QCIF and CIF are specified as supported in RFC 4587 */
579 if (p->width == 176 && p->height == 144)
580 pic_fmt = "QCIF=1";
581 else if (p->width == 352 && p->height == 288)
582 pic_fmt = "CIF=1";
583 if (payload_type >= RTP_PT_PRIVATE)
584 av_strlcatf(buff, size, "a=rtpmap:%d H261/90000\r\n", payload_type);
585 if (pic_fmt)
586 av_strlcatf(buff, size, "a=fmtp:%d %s\r\n", payload_type, pic_fmt);
587 break;
588 }
589 case AV_CODEC_ID_H263:
591 /* a=framesize is required by 3GPP TS 26.234 (PSS). It
592 * actually specifies the maximum video size, but we only know
593 * the current size. This is required for playback on Android
594 * stagefright and on Samsung bada. */
595 if (!fmt || !fmt->oformat->priv_class ||
596 !av_opt_flag_is_set(fmt->priv_data, "rtpflags", "rfc2190") ||
597 p->codec_id == AV_CODEC_ID_H263P)
598 av_strlcatf(buff, size, "a=rtpmap:%d H263-2000/90000\r\n"
599 "a=framesize:%d %d-%d\r\n",
600 payload_type,
601 payload_type, p->width, p->height);
602 break;
603 case AV_CODEC_ID_HEVC:
604 if (p->extradata_size) {
605 ret = extradata2psets_hevc(fmt, p, &config);
606 if (ret < 0)
607 return ret;
608 }
609 av_strlcatf(buff, size, "a=rtpmap:%d H265/90000\r\n", payload_type);
610 if (config)
611 av_strlcatf(buff, size, "a=fmtp:%d %s\r\n",
612 payload_type, config);
613 break;
615 if (p->extradata_size) {
616 ret = extradata2config(fmt, p, &config);
617 if (ret < 0)
618 return ret;
619 }
620 av_strlcatf(buff, size, "a=rtpmap:%d MP4V-ES/90000\r\n"
621 "a=fmtp:%d profile-level-id=1%s\r\n",
622 payload_type,
623 payload_type, config ? config : "");
624 break;
625 case AV_CODEC_ID_AAC:
626 if (fmt && fmt->oformat && fmt->oformat->priv_class &&
627 av_opt_flag_is_set(fmt->priv_data, "rtpflags", "latm")) {
628 ret = latm_context2config(fmt, p, &config);
629 if (ret < 0)
630 return ret;
631 av_strlcatf(buff, size, "a=rtpmap:%d MP4A-LATM/%d/%d\r\n"
632 "a=fmtp:%d profile-level-id=%d;cpresent=0;config=%s\r\n",
633 payload_type, p->sample_rate, p->ch_layout.nb_channels,
634 payload_type, latm_context2profilelevel(p), config);
635 } else {
636 if (p->extradata_size) {
637 ret = extradata2config(fmt, p, &config);
638 if (ret < 0)
639 return ret;
640 } else {
641 /* FIXME: maybe we can forge config information based on the
642 * codec parameters...
643 */
644 av_log(fmt, AV_LOG_ERROR, "AAC with no global headers is currently not supported.\n");
645 return AVERROR(ENOSYS);
646 }
647 av_strlcatf(buff, size, "a=rtpmap:%d MPEG4-GENERIC/%d/%d\r\n"
648 "a=fmtp:%d profile-level-id=1;"
649 "mode=AAC-hbr;sizelength=13;indexlength=3;"
650 "indexdeltalength=3%s\r\n",
651 payload_type, p->sample_rate, p->ch_layout.nb_channels,
652 payload_type, config);
653 }
654 break;
656 if (payload_type >= RTP_PT_PRIVATE)
657 av_strlcatf(buff, size, "a=rtpmap:%d L16/%d/%d\r\n",
658 payload_type,
659 p->sample_rate, p->ch_layout.nb_channels);
660 break;
662 if (payload_type >= RTP_PT_PRIVATE)
663 av_strlcatf(buff, size, "a=rtpmap:%d L24/%d/%d\r\n",
664 payload_type,
665 p->sample_rate, p->ch_layout.nb_channels);
666 break;
668 if (payload_type >= RTP_PT_PRIVATE)
669 av_strlcatf(buff, size, "a=rtpmap:%d PCMU/%d/%d\r\n",
670 payload_type,
671 p->sample_rate, p->ch_layout.nb_channels);
672 break;
674 if (payload_type >= RTP_PT_PRIVATE)
675 av_strlcatf(buff, size, "a=rtpmap:%d PCMA/%d/%d\r\n",
676 payload_type,
677 p->sample_rate, p->ch_layout.nb_channels);
678 break;
680 av_strlcatf(buff, size, "a=rtpmap:%d AMR/%d/%d\r\n"
681 "a=fmtp:%d octet-align=1\r\n",
682 payload_type, p->sample_rate, p->ch_layout.nb_channels,
683 payload_type);
684 break;
686 av_strlcatf(buff, size, "a=rtpmap:%d AMR-WB/%d/%d\r\n"
687 "a=fmtp:%d octet-align=1\r\n",
688 payload_type, p->sample_rate, p->ch_layout.nb_channels,
689 payload_type);
690 break;
692 if (p->extradata_size)
693 ret = xiph_extradata2config(fmt, p, &config);
694 else {
695 av_log(fmt, AV_LOG_ERROR, "Vorbis configuration info missing\n");
697 }
698 if (ret < 0)
699 return ret;
700
701 av_strlcatf(buff, size, "a=rtpmap:%d vorbis/%d/%d\r\n"
702 "a=fmtp:%d configuration=%s\r\n",
703 payload_type, p->sample_rate, p->ch_layout.nb_channels,
704 payload_type, config);
705 break;
706 case AV_CODEC_ID_THEORA: {
707 const char *pix_fmt;
708 switch (p->format) {
710 pix_fmt = "YCbCr-4:2:0";
711 break;
713 pix_fmt = "YCbCr-4:2:2";
714 break;
716 pix_fmt = "YCbCr-4:4:4";
717 break;
718 default:
719 av_log(fmt, AV_LOG_ERROR, "Unsupported pixel format.\n");
720 return AVERROR(ENOSYS);
721 }
722
723 if (p->extradata_size)
724 ret = xiph_extradata2config(fmt, p, &config);
725 else {
726 av_log(fmt, AV_LOG_ERROR, "Theora configuration info missing\n");
728 }
729 if (ret < 0)
730 return ret;
731
732 av_strlcatf(buff, size, "a=rtpmap:%d theora/90000\r\n"
733 "a=fmtp:%d delivery-method=inline; "
734 "width=%d; height=%d; sampling=%s; "
735 "configuration=%s\r\n",
736 payload_type, payload_type,
737 p->width, p->height, pix_fmt, config);
738 break;
739 }
742 const char *pix_fmt;
743 int bit_depth = 8;
744
745 switch (p->format) {
747 pix_fmt = "YCbCr-4:2:2";
748 break;
750 pix_fmt = "YCbCr-4:2:2";
751 bit_depth = 10;
752 break;
754 pix_fmt = "YCbCr-4:2:0";
755 break;
756 case AV_PIX_FMT_RGB24:
757 pix_fmt = "RGB";
758 break;
759 case AV_PIX_FMT_BGR24:
760 pix_fmt = "BGR";
761 break;
762 default:
763 av_log(fmt, AV_LOG_ERROR, "Unsupported pixel format.\n");
764 return AVERROR(ENOSYS);
765 }
766
767 av_strlcatf(buff, size, "a=rtpmap:%d raw/90000\r\n"
768 "a=fmtp:%d sampling=%s; "
769 "width=%d; height=%d; "
770 "depth=%d",
771 payload_type, payload_type,
772 pix_fmt, p->width, p->height, bit_depth);
773 if (p->field_order != AV_FIELD_PROGRESSIVE)
774 av_strlcatf(buff, size, "; interlace");
775 av_strlcatf(buff, size, "\r\n");
776 break;
777 }
778
779 case AV_CODEC_ID_VP8:
780 av_strlcatf(buff, size, "a=rtpmap:%d VP8/90000\r\n",
781 payload_type);
782 break;
783 case AV_CODEC_ID_VP9:
784 av_strlcatf(buff, size, "a=rtpmap:%d VP9/90000\r\n",
785 payload_type);
786 break;
788 if (payload_type >= RTP_PT_PRIVATE)
789 av_strlcatf(buff, size, "a=rtpmap:%d JPEG/90000\r\n",
790 payload_type);
791 break;
793 if (payload_type >= RTP_PT_PRIVATE)
794 av_strlcatf(buff, size, "a=rtpmap:%d G722/%d/%d\r\n",
795 payload_type,
796 8000, p->ch_layout.nb_channels);
797 break;
799 if (payload_type >= RTP_PT_PRIVATE)
800 av_strlcatf(buff, size, "a=rtpmap:%d AAL2-G726-%d/%d\r\n",
801 payload_type,
802 p->bits_per_coded_sample*8,
803 p->sample_rate);
804 break;
805 }
807 if (payload_type >= RTP_PT_PRIVATE)
808 av_strlcatf(buff, size, "a=rtpmap:%d G726-%d/%d\r\n",
809 payload_type,
810 p->bits_per_coded_sample*8,
811 p->sample_rate);
812 break;
813 }
814 case AV_CODEC_ID_ILBC:
815 av_strlcatf(buff, size, "a=rtpmap:%d iLBC/%d\r\n"
816 "a=fmtp:%d mode=%d\r\n",
817 payload_type, p->sample_rate,
818 payload_type, p->block_align == 38 ? 20 : 30);
819 break;
821 av_strlcatf(buff, size, "a=rtpmap:%d speex/%d\r\n",
822 payload_type, p->sample_rate);
823 break;
824 case AV_CODEC_ID_OPUS:
825 /* The opus RTP draft says that all opus streams MUST be declared
826 as stereo, to avoid negotiation failures. The actual number of
827 channels can change on a packet-by-packet basis. The number of
828 channels a receiver prefers to receive or a sender plans to send
829 can be declared via fmtp parameters (both default to mono), but
830 receivers MUST be able to receive and process stereo packets. */
831 av_strlcatf(buff, size, "a=rtpmap:%d opus/48000/2\r\n",
832 payload_type);
833 if (p->ch_layout.nb_channels == 2) {
834 av_strlcatf(buff, size, "a=fmtp:%d sprop-stereo=1\r\n",
835 payload_type);
836 }
837 break;
838 default:
839 /* Nothing special to do here... */
840 break;
841 }
842
843 av_free(config);
844
845 return 0;
846}
847
848int ff_sdp_write_media(char *buff, int size, const AVStream *st, int idx,
849 const char *dest_addr, const char *dest_type,
850 int port, int ttl, AVFormatContext *fmt)
851{
852 const AVCodecParameters *p = st->codecpar;
853 const char *type;
854 int payload_type;
855
856 payload_type = ff_rtp_get_payload_type(fmt, st->codecpar, idx);
857
858 switch (p->codec_type) {
859 case AVMEDIA_TYPE_VIDEO : type = "video" ; break;
860 case AVMEDIA_TYPE_AUDIO : type = "audio" ; break;
861 case AVMEDIA_TYPE_SUBTITLE: type = "text" ; break;
862 default : type = "application"; break;
863 }
864
865 av_strlcatf(buff, size, "m=%s %d RTP/AVP %d\r\n", type, port, payload_type);
866 sdp_write_address(buff, size, dest_addr, dest_type, ttl);
867 if (p->bit_rate) {
868 av_strlcatf(buff, size, "b=AS:%"PRId64"\r\n", p->bit_rate / 1000);
869 }
870 if (p->framerate.num > 0 && p->framerate.den > 0) {
871 av_strlcatf(buff, size, "a=framerate:%g\r\n", av_q2d(p->framerate));
872 }
873
874 return sdp_write_media_attributes(buff, size, st, payload_type, fmt);
875}
876
877int av_sdp_create(AVFormatContext *ac[], int n_files, char *buf, int size)
878{
879 AVDictionaryEntry *title = av_dict_get(ac[0]->metadata, "title", NULL, 0);
880 struct sdp_session_level s = { 0 };
881 int i, j, port, ttl, is_multicast, index = 0;
882 char dst[32], dst_type[5];
883
884 memset(buf, 0, size);
885 s.user = "-";
886 s.src_addr = "127.0.0.1"; /* FIXME: Properly set this */
887 s.src_type = "IP4";
888 s.name = title ? title->value : "No Name";
889
890 port = 0;
891 ttl = 0;
892 if (n_files == 1) {
893 port = sdp_get_address(dst, sizeof(dst), &ttl, ac[0]->url ? ac[0]->url : "");
894 is_multicast = resolve_destination(dst, sizeof(dst), dst_type,
895 sizeof(dst_type));
896 if (!is_multicast)
897 ttl = 0;
898 if (dst[0]) {
899 s.dst_addr = dst;
900 s.dst_type = dst_type;
901 s.ttl = ttl;
902 if (!strcmp(dst_type, "IP6")) {
903 s.src_addr = "::1";
904 s.src_type = "IP6";
905 }
906 }
907 }
908 sdp_write_header(buf, size, &s);
909
910 dst[0] = 0;
911 for (i = 0; i < n_files; i++) {
912 if (n_files != 1) {
913 port = sdp_get_address(dst, sizeof(dst), &ttl, ac[i]->url ? ac[i]->url : "");
914 is_multicast = resolve_destination(dst, sizeof(dst), dst_type,
915 sizeof(dst_type));
916 if (!is_multicast)
917 ttl = 0;
918 }
919 for (j = 0; j < ac[i]->nb_streams; j++) {
920 int ret = ff_sdp_write_media(buf, size, ac[i]->streams[j], index++,
921 dst[0] ? dst : NULL, dst_type,
922 (port > 0) ? port + j * 2 : 0,
923 ttl, ac[i]);
924 if (ret < 0)
925 return ret;
926
927 if (port <= 0) {
928 av_strlcatf(buf, size,
929 "a=control:streamid=%d\r\n", i + j);
930 }
931 if (ac[i]->pb && ac[i]->pb->av_class) {
932 uint8_t *crypto_suite = NULL, *crypto_params = NULL;
933 av_opt_get(ac[i]->pb, "srtp_out_suite", AV_OPT_SEARCH_CHILDREN,
934 &crypto_suite);
935 av_opt_get(ac[i]->pb, "srtp_out_params", AV_OPT_SEARCH_CHILDREN,
936 &crypto_params);
937 if (crypto_suite && crypto_suite[0])
938 av_strlcatf(buf, size,
939 "a=crypto:1 %s inline:%s\r\n",
940 crypto_suite, crypto_params);
941 av_free(crypto_suite);
942 av_free(crypto_params);
943 }
944 }
945 }
946
947 return 0;
948}
949#else
950int av_sdp_create(AVFormatContext *ac[], int n_files, char *buf, int size)
951{
952 return AVERROR(ENOSYS);
953}
954
955int ff_sdp_write_media(char *buff, int size, const AVStream *st, int idx,
956 const char *dest_addr, const char *dest_type,
957 int port, int ttl, AVFormatContext *fmt)
958{
959 return AVERROR(ENOSYS);
960}
961#endif
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
static void bit_depth(AudioStatsContext *s, const uint64_t *const mask, uint8_t *depth)
Definition af_astats.c:246
int ff_av1_parse_seq_header(AV1SequenceParameters *seq, const uint8_t *buf, int size)
Parses a Sequence Header from the the provided buffer.
Definition av1.c:336
int ff_avc_write_annexb_extradata(const uint8_t *in, uint8_t **buf, int *size)
Definition avc.c:145
Main libavformat public API header.
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition aviobuf.c:1410
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition aviobuf.c:1365
size_t av_strlcatf(char *dst, size_t size, const char *fmt,...)
Definition avstring.c:103
static int FUNC metadata(CodedBitstreamContext *ctx, RWContext *rw, APVRawMetadata *current)
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
static int FUNC sps(CodedBitstreamContext *ctx, RWContext *rw, H264RawSPS *current)
#define s(width, name)
Definition cbs_vp9.c:198
#define NULL
Definition coverity.c:32
@ AV_FIELD_PROGRESSIVE
Definition defs.h:213
static enum AVPixelFormat pix_fmt
Public dictionary API.
enum AVCodecID id
Definition dts2pts.c:607
static int64_t start_time
Definition ffplay.c:329
@ AV_CODEC_ID_DIRAC
Definition codec_id.h:166
@ AV_CODEC_ID_PCM_S24BE
Definition codec_id.h:343
@ AV_CODEC_ID_VORBIS
Definition codec_id.h:458
@ AV_CODEC_ID_H261
Definition codec_id.h:53
@ AV_CODEC_ID_RAWVIDEO
Definition codec_id.h:63
@ AV_CODEC_ID_ADPCM_G722
Definition codec_id.h:398
@ AV_CODEC_ID_H264
Definition codec_id.h:77
@ AV_CODEC_ID_PCM_S16BE
Definition codec_id.h:331
@ AV_CODEC_ID_BITPACKED
Definition codec_id.h:276
@ AV_CODEC_ID_ADPCM_G726LE
Definition codec_id.h:405
@ AV_CODEC_ID_AV1
Definition codec_id.h:275
@ AV_CODEC_ID_VP8
Definition codec_id.h:190
@ AV_CODEC_ID_PCM_ALAW
Definition codec_id.h:337
@ AV_CODEC_ID_AMR_NB
Definition codec_id.h:434
@ AV_CODEC_ID_THEORA
Definition codec_id.h:80
@ AV_CODEC_ID_HEVC
Definition codec_id.h:223
@ AV_CODEC_ID_ADPCM_G726
Definition codec_id.h:381
@ AV_CODEC_ID_AAC
Definition codec_id.h:455
@ AV_CODEC_ID_H263
Definition codec_id.h:54
@ AV_CODEC_ID_AMR_WB
Definition codec_id.h:435
@ AV_CODEC_ID_MPEG4
Definition codec_id.h:62
@ AV_CODEC_ID_MJPEG
Definition codec_id.h:57
@ AV_CODEC_ID_SPEEX
Definition codec_id.h:488
@ AV_CODEC_ID_ILBC
Definition codec_id.h:512
@ AV_CODEC_ID_VP9
Definition codec_id.h:217
@ AV_CODEC_ID_H263P
Definition codec_id.h:69
@ AV_CODEC_ID_OPUS
Definition codec_id.h:513
@ AV_CODEC_ID_PCM_MULAW
Definition codec_id.h:336
void av_url_split(char *proto, int proto_size, char *authorization, int authorization_size, char *hostname, int hostname_size, int *port_ptr, char *path, int path_size, const char *url)
Split a URL string into components.
Definition utils.c:361
int av_sdp_create(AVFormatContext *ac[], int n_files, char *buf, int size)
Generate an SDP for an RTP session.
Definition sdp.c:950
char * av_base64_encode(char *out, int out_size, const uint8_t *in, int in_size)
Encode data to base64 and null-terminate.
Definition base64.c:147
#define AV_BASE64_SIZE(x)
Calculate the output size needed to base64-encode x bytes to a null-terminated string.
Definition base64.h:66
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition dict.c:60
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
static double av_q2d(AVRational a)
Convert an AVRational to a double.
Definition rational.h:104
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AVMEDIA_TYPE_SUBTITLE
Definition avutil.h:203
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
size_t av_strlcat(char *dst, const char *src, size_t size)
Append the string src to the string dst, but to a total length of no more than size - 1 bytes,...
Definition avstring.c:95
size_t av_strlcpy(char *dst, const char *src, size_t size)
Copy the string src to dst, but no more than size - 1 bytes, and null-terminate dst.
Definition avstring.c:85
int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
Definition opt.c:1287
#define AV_OPT_SEARCH_CHILDREN
Search in possible children of the given object first.
Definition opt.h:604
int av_opt_flag_is_set(void *obj, const char *field_name, const char *flag_name)
Check whether a particular flag is set in a flags field.
Definition opt.c:1475
#define AV_STRINGIFY(s)
Definition macros.h:66
int index
Definition gxfenc.c:90
int ff_isom_write_hvcc(AVIOContext *pb, const uint8_t *data, int size, int ps_array_completeness, void *logctx)
Writes HEVC extradata (parameter sets and declarative SEI NAL units with nuh_layer_id == 0,...
Definition hevc.c:1401
cl_device_type type
#define r
Definition input.c:42
#define AV_RB16(p)
#define MAX_EXTRADATA_SIZE
Definition latmenc.c:35
internal header for HEVC (de)muxer utilities
char * ff_data_to_hex(char *buf, const uint8_t *src, int size, int lowercase)
Write hexadecimal string corresponding to given binary data.
Definition utils.c:464
Libavformat version macros.
#define LIBAVFORMAT_VERSION
Definition version.h:40
version
Definition libkvazaar.c:313
Memory handling functions.
const int ff_mpeg4audio_sample_rates[16]
const uint8_t * ff_nal_find_startcode(const uint8_t *p, const uint8_t *end)
Definition nal.c:68
IDirect3DDxgiInterfaceAccess _COM_Outptr_ void ** p
int ff_is_multicast_address(struct sockaddr *addr)
Definition network.c:150
#define NI_NUMERICHOST
Definition network.h:195
#define getaddrinfo
Definition network.h:217
#define getnameinfo
Definition network.h:219
#define freeaddrinfo
Definition network.h:218
#define av_malloc(s)
Definition ops_static.c:52
AVOptions.
int av_find_info_tag(char *arg, int arg_size, const char *tag1, const char *info)
Attempt to find a specific tag in a URL.
Definition parseutils.c:756
misc parsing utilities
#define AV_PIX_FMT_YUV422P10
Definition pixfmt.h:546
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition pixfmt.h:75
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition pixfmt.h:73
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition pixfmt.h:77
@ AV_PIX_FMT_UYVY422
packed YUV 4:2:2, 16bpp, Cb Y0 Cr Y1
Definition pixfmt.h:88
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition pixfmt.h:78
@ AV_PIX_FMT_BGR24
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition pixfmt.h:76
const char * name
Definition qsvenc.c:142
int ff_rtp_get_payload_type(const AVFormatContext *fmt, const AVCodecParameters *par, int idx)
Return the payload type for a given stream used in the given format context.
Definition rtp.c:93
#define RTP_PT_PRIVATE
Definition rtp.h:79
#define RTP_XIPH_IDENT
Definition rtp.h:91
int ff_sdp_write_media(char *buff, int size, const AVStream *st, int idx, const char *dest_addr, const char *dest_type, int port, int ttl, AVFormatContext *fmt)
Append the media-specific SDP fragment for the media stream c to the buffer buff.
Definition sdp.c:955
unsigned int pos
Definition spdifenc.c:431
uint8_t level
Definition av1.h:30
uint8_t profile
Definition av1.h:29
int nb_channels
Number of channels in this layout.
This struct describes the properties of an encoded stream.
Definition codec_par.h:49
int extradata_size
Size of the extradata content in bytes.
Definition codec_par.h:75
AVChannelLayout ch_layout
The channel layout and number of channels.
Definition codec_par.h:207
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition codec_par.h:71
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
int sample_rate
The number of audio samples per second.
Definition codec_par.h:213
char * value
Definition dict.h:92
Format I/O context.
Definition avformat.h:1333
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition avformat.h:1389
const struct AVOutputFormat * oformat
The output container format.
Definition avformat.h:1352
void * priv_data
Format private data.
Definition avformat.h:1361
Bytestream IO Context.
Definition avio.h:160
const AVClass * av_class
A class for private options.
Definition avio.h:173
const AVClass * priv_class
AVClass for the private context.
Definition avformat.h:555
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
Definition swscale.c:71
#define av_free(p)
#define av_mallocz(s)
#define av_log(a,...)
static FILE * out
Definition movenc.c:55
int size
int len
int avpriv_split_xiph_headers(const uint8_t *extradata, int extradata_size, int first_header_size, const uint8_t *header_start[3], int header_len[3])
Split a single extradata buffer into the three headers that most Xiph codecs use.
Definition xiph.c:26