1 /*****************************************************************************
2 * rtpfmt.c: RTP payload formats
3 *****************************************************************************
4 * Copyright (C) 2003-2004 VLC authors and VideoLAN
5 * Copyright © 2007 Rémi Denis-Courmont
8 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
29 #include <vlc_common.h>
31 #include <vlc_block.h>
32 #include <vlc_strings.h>
35 #include "../demux/xiph.h"
39 static int rtp_packetize_mpa (sout_stream_id_sys_t *, block_t *);
40 static int rtp_packetize_mpv (sout_stream_id_sys_t *, block_t *);
41 static int rtp_packetize_ac3 (sout_stream_id_sys_t *, block_t *);
42 static int rtp_packetize_simple(sout_stream_id_sys_t *, block_t *);
43 static int rtp_packetize_split(sout_stream_id_sys_t *, block_t *);
44 static int rtp_packetize_pcm(sout_stream_id_sys_t *, block_t *);
45 static int rtp_packetize_swab (sout_stream_id_sys_t *, block_t *);
46 static int rtp_packetize_mp4a (sout_stream_id_sys_t *, block_t *);
47 static int rtp_packetize_mp4a_latm (sout_stream_id_sys_t *, block_t *);
48 static int rtp_packetize_h263 (sout_stream_id_sys_t *, block_t *);
49 static int rtp_packetize_h264 (sout_stream_id_sys_t *, block_t *);
50 static int rtp_packetize_amr (sout_stream_id_sys_t *, block_t *);
51 static int rtp_packetize_spx (sout_stream_id_sys_t *, block_t *);
52 static int rtp_packetize_t140 (sout_stream_id_sys_t *, block_t *);
53 static int rtp_packetize_g726_16 (sout_stream_id_sys_t *, block_t *);
54 static int rtp_packetize_g726_24 (sout_stream_id_sys_t *, block_t *);
55 static int rtp_packetize_g726_32 (sout_stream_id_sys_t *, block_t *);
56 static int rtp_packetize_g726_40 (sout_stream_id_sys_t *, block_t *);
57 static int rtp_packetize_xiph (sout_stream_id_sys_t *, block_t *);
58 static int rtp_packetize_vp8 (sout_stream_id_sys_t *, block_t *);
59 static int rtp_packetize_jpeg (sout_stream_id_sys_t *, block_t *);
61 #define XIPH_IDENT (0)
63 /* Helpers common to xiph codecs (vorbis and theora) */
65 static int rtp_xiph_pack_headers(size_t room, void *p_extra, size_t i_extra,
66 uint8_t **p_buffer, size_t *i_buffer,
67 uint8_t *theora_pixel_fmt)
69 unsigned packet_size[XIPH_MAX_HEADER_COUNT];
70 void *packet[XIPH_MAX_HEADER_COUNT];
71 unsigned packet_count;
72 if (xiph_SplitHeaders(packet_size, packet, &packet_count,
78 if (theora_pixel_fmt != NULL)
80 if (packet_size[0] < 42)
82 *theora_pixel_fmt = (((uint8_t *)packet[0])[41] >> 3) & 0x03;
85 unsigned length_size[2] = { 0, 0 };
86 for (int i = 0; i < 2; i++)
88 unsigned size = packet_size[i];
96 *i_buffer = room + 1 + length_size[0] + length_size[1]
97 + packet_size[0] + packet_size[1] + packet_size[2];
98 *p_buffer = malloc(*i_buffer);
99 if (*p_buffer == NULL)
102 uint8_t *p = *p_buffer + room;
103 /* Number of headers */
106 for (int i = 0; i < 2; i++)
108 unsigned size = length_size[i];
111 *p = (packet_size[i] >> (7 * (size - 1))) & 0x7f;
117 for (int i = 0; i < 3; i++)
119 memcpy(p, packet[i], packet_size[i]);
126 static char *rtp_xiph_b64_oob_config(void *p_extra, size_t i_extra,
127 uint8_t *theora_pixel_fmt)
131 if (rtp_xiph_pack_headers(9, p_extra, i_extra, &p_buffer, &i_buffer,
132 theora_pixel_fmt) != VLC_SUCCESS)
135 /* Number of packed headers */
136 SetDWBE(p_buffer, 1);
138 uint32_t ident = XIPH_IDENT;
139 SetWBE(p_buffer + 4, ident >> 8);
140 p_buffer[6] = ident & 0xff;
142 SetWBE(p_buffer + 7, i_buffer);
144 char *config = vlc_b64_encode_binary(p_buffer, i_buffer);
149 static void sprintf_hexa( char *s, uint8_t *p_data, int i_data )
151 static const char hex[16] = "0123456789abcdef";
153 for( int i = 0; i < i_data; i++ )
155 s[2*i+0] = hex[(p_data[i]>>4)&0xf];
156 s[2*i+1] = hex[(p_data[i] )&0xf];
161 /* TODO: make this into something more clever than a big switch? */
162 int rtp_get_fmt( vlc_object_t *obj, es_format_t *p_fmt, const char *mux,
163 rtp_format_t *rtp_fmt )
165 assert( p_fmt != NULL || mux != NULL );
167 /* Dynamic payload type. Payload types are scoped to the RTP
168 * session, and we put each ES in its own session, so no risk of
170 rtp_fmt->payload_type = 96;
171 rtp_fmt->cat = mux != NULL ? VIDEO_ES : p_fmt->i_cat;
172 if( rtp_fmt->cat == AUDIO_ES )
174 rtp_fmt->clock_rate = p_fmt->audio.i_rate;
175 rtp_fmt->channels = p_fmt->audio.i_channels;
178 rtp_fmt->clock_rate = 90000; /* most common case for video */
179 /* Stream bitrate in kbps */
180 rtp_fmt->bitrate = p_fmt != NULL ? p_fmt->i_bitrate/1000 : 0;
181 rtp_fmt->fmtp = NULL;
185 if( strncmp( mux, "ts", 2 ) == 0 )
187 rtp_fmt->payload_type = 33;
188 rtp_fmt->ptname = "MP2T";
191 rtp_fmt->ptname = "MP2P";
195 switch( p_fmt->i_codec )
197 case VLC_CODEC_MULAW:
198 if( p_fmt->audio.i_channels == 1 && p_fmt->audio.i_rate == 8000 )
199 rtp_fmt->payload_type = 0;
200 rtp_fmt->ptname = "PCMU";
201 rtp_fmt->pf_packetize = rtp_packetize_pcm;
204 if( p_fmt->audio.i_channels == 1 && p_fmt->audio.i_rate == 8000 )
205 rtp_fmt->payload_type = 8;
206 rtp_fmt->ptname = "PCMA";
207 rtp_fmt->pf_packetize = rtp_packetize_pcm;
211 if( p_fmt->audio.i_channels == 1 && p_fmt->audio.i_rate == 44100 )
213 rtp_fmt->payload_type = 11;
215 else if( p_fmt->audio.i_channels == 2 &&
216 p_fmt->audio.i_rate == 44100 )
218 rtp_fmt->payload_type = 10;
220 rtp_fmt->ptname = "L16";
221 if( p_fmt->i_codec == VLC_CODEC_S16B )
222 rtp_fmt->pf_packetize = rtp_packetize_pcm;
224 rtp_fmt->pf_packetize = rtp_packetize_swab;
227 rtp_fmt->ptname = "L8";
228 rtp_fmt->pf_packetize = rtp_packetize_pcm;
231 rtp_fmt->ptname = "L24";
232 rtp_fmt->pf_packetize = rtp_packetize_pcm;
235 rtp_fmt->payload_type = 14;
236 rtp_fmt->ptname = "MPA";
237 rtp_fmt->clock_rate = 90000; /* not 44100 */
238 rtp_fmt->pf_packetize = rtp_packetize_mpa;
241 rtp_fmt->payload_type = 32;
242 rtp_fmt->ptname = "MPV";
243 rtp_fmt->pf_packetize = rtp_packetize_mpv;
245 case VLC_CODEC_ADPCM_G726:
246 switch( p_fmt->i_bitrate / 1000 )
249 rtp_fmt->ptname = "G726-16";
250 rtp_fmt->pf_packetize = rtp_packetize_g726_16;
253 rtp_fmt->ptname = "G726-24";
254 rtp_fmt->pf_packetize = rtp_packetize_g726_24;
257 rtp_fmt->ptname = "G726-32";
258 rtp_fmt->pf_packetize = rtp_packetize_g726_32;
261 rtp_fmt->ptname = "G726-40";
262 rtp_fmt->pf_packetize = rtp_packetize_g726_40;
265 msg_Err( obj, "cannot add this stream (unsupported "
266 "G.726 bit rate: %u)", p_fmt->i_bitrate );
271 rtp_fmt->ptname = "ac3";
272 rtp_fmt->pf_packetize = rtp_packetize_ac3;
275 rtp_fmt->ptname = "H263-1998";
276 rtp_fmt->pf_packetize = rtp_packetize_h263;
279 rtp_fmt->ptname = "H264";
280 rtp_fmt->pf_packetize = rtp_packetize_h264;
281 rtp_fmt->fmtp = NULL;
283 if( p_fmt->i_extra > 0 )
285 uint8_t *p_buffer = p_fmt->p_extra;
286 int i_buffer = p_fmt->i_extra;
287 char *p_64_sps = NULL;
288 char *p_64_pps = NULL;
291 while( i_buffer > 4 )
296 while( p_buffer[0] != 0 || p_buffer[1] != 0 ||
301 if( i_buffer == 0 ) break;
304 if( i_buffer < 4 || memcmp(p_buffer, "\x00\x00\x01", 3 ) )
306 msg_Dbg( obj, "No startcode found..");
312 const int i_nal_type = p_buffer[0]&0x1f;
314 msg_Dbg( obj, "we found a startcode for NAL with TYPE:%d", i_nal_type );
317 for( i_offset = 0; i_offset+2 < i_buffer ; i_offset++)
319 if( !memcmp(p_buffer + i_offset, "\x00\x00\x01", 3 ) )
321 /* we found another startcode */
322 while( i_offset > 0 && 0 == p_buffer[ i_offset - 1 ] )
331 msg_Dbg( obj, "No-info found in nal ");
335 if( i_nal_type == 7 )
338 p_64_sps = vlc_b64_encode_binary( p_buffer, i_size );
339 /* XXX: nothing ensures that i_size >= 4 ?? */
340 sprintf_hexa( hexa, &p_buffer[1], 3 );
342 else if( i_nal_type == 8 )
345 p_64_pps = vlc_b64_encode_binary( p_buffer, i_size );
351 if( p_64_sps && p_64_pps &&
352 ( asprintf( &rtp_fmt->fmtp,
353 "packetization-mode=1;profile-level-id=%s;"
354 "sprop-parameter-sets=%s,%s;", hexa, p_64_sps,
356 rtp_fmt->fmtp = NULL;
360 if( rtp_fmt->fmtp == NULL )
361 rtp_fmt->fmtp = strdup( "packetization-mode=1" );
366 rtp_fmt->ptname = "MP4V-ES";
367 rtp_fmt->pf_packetize = rtp_packetize_split;
368 if( p_fmt->i_extra > 0 )
370 char hexa[2*p_fmt->i_extra +1];
371 sprintf_hexa( hexa, p_fmt->p_extra, p_fmt->i_extra );
372 if( asprintf( &rtp_fmt->fmtp,
373 "profile-level-id=3; config=%s;", hexa ) == -1 )
374 rtp_fmt->fmtp = NULL;
380 if( ! var_InheritBool( obj, "sout-rtp-mp4a-latm" ) )
382 char hexa[2*p_fmt->i_extra +1];
384 rtp_fmt->ptname = "mpeg4-generic";
385 rtp_fmt->pf_packetize = rtp_packetize_mp4a;
386 sprintf_hexa( hexa, p_fmt->p_extra, p_fmt->i_extra );
387 if( asprintf( &rtp_fmt->fmtp,
388 "streamtype=5; profile-level-id=15; "
389 "mode=AAC-hbr; config=%s; SizeLength=13; "
390 "IndexLength=3; IndexDeltaLength=3; Profile=1;",
392 rtp_fmt->fmtp = NULL;
398 unsigned char config[6];
399 unsigned int aacsrates[15] = {
400 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
401 16000, 12000, 11025, 8000, 7350, 0, 0 };
403 for( i = 0; i < 15; i++ )
404 if( p_fmt->audio.i_rate == aacsrates[i] )
410 config[3]=p_fmt->audio.i_channels<<4;
414 rtp_fmt->ptname = "MP4A-LATM";
415 rtp_fmt->pf_packetize = rtp_packetize_mp4a_latm;
416 sprintf_hexa( hexa, config, 6 );
417 if( asprintf( &rtp_fmt->fmtp, "profile-level-id=15; "
418 "object=2; cpresent=0; config=%s", hexa ) == -1 )
419 rtp_fmt->fmtp = NULL;
423 case VLC_CODEC_AMR_NB:
424 rtp_fmt->ptname = "AMR";
425 rtp_fmt->fmtp = strdup( "octet-align=1" );
426 rtp_fmt->pf_packetize = rtp_packetize_amr;
428 case VLC_CODEC_AMR_WB:
429 rtp_fmt->ptname = "AMR-WB";
430 rtp_fmt->fmtp = strdup( "octet-align=1" );
431 rtp_fmt->pf_packetize = rtp_packetize_amr;
433 case VLC_CODEC_SPEEX:
434 rtp_fmt->ptname = "SPEEX";
435 rtp_fmt->pf_packetize = rtp_packetize_spx;
437 case VLC_CODEC_VORBIS:
438 rtp_fmt->ptname = "vorbis";
439 rtp_fmt->pf_packetize = rtp_packetize_xiph;
440 if( p_fmt->i_extra > 0 )
442 rtp_fmt->fmtp = NULL;
443 char *config = rtp_xiph_b64_oob_config(p_fmt->p_extra,
444 p_fmt->i_extra, NULL);
447 if( asprintf( &rtp_fmt->fmtp,
448 "configuration=%s;", config ) == -1 )
449 rtp_fmt->fmtp = NULL;
453 case VLC_CODEC_THEORA:
454 rtp_fmt->ptname = "theora";
455 rtp_fmt->pf_packetize = rtp_packetize_xiph;
456 if( p_fmt->i_extra > 0 )
458 rtp_fmt->fmtp = NULL;
459 uint8_t pixel_fmt, c1, c2;
460 char *config = rtp_xiph_b64_oob_config(p_fmt->p_extra,
488 if( asprintf( &rtp_fmt->fmtp,
489 "sampling=YCbCr-4:%d:%d; width=%d; height=%d; "
490 "delivery-method=inline; configuration=%s; "
491 "delivery-method=in_band;", c1, c2,
492 p_fmt->video.i_width, p_fmt->video.i_height,
494 rtp_fmt->fmtp = NULL;
498 case VLC_CODEC_ITU_T140:
499 rtp_fmt->ptname = "t140" ;
500 rtp_fmt->clock_rate = 1000;
501 rtp_fmt->pf_packetize = rtp_packetize_t140;
504 rtp_fmt->payload_type = 3;
505 rtp_fmt->ptname = "GSM";
506 rtp_fmt->pf_packetize = rtp_packetize_split;
509 if (p_fmt->audio.i_channels > 2)
511 msg_Err( obj, "Multistream opus not supported in RTP"
512 " (having %d channels input)",
513 p_fmt->audio.i_channels );
516 rtp_fmt->ptname = "opus";
517 rtp_fmt->pf_packetize = rtp_packetize_simple;
518 rtp_fmt->clock_rate = 48000;
519 rtp_fmt->channels = 2;
520 if (p_fmt->audio.i_channels == 2)
521 rtp_fmt->fmtp = strdup( "sprop-stereo=1" );
524 rtp_fmt->ptname = "VP8";
525 rtp_fmt->pf_packetize = rtp_packetize_vp8;
529 rtp_fmt->ptname = "JPEG";
530 rtp_fmt->payload_type = 26;
531 rtp_fmt->pf_packetize = rtp_packetize_jpeg;
535 msg_Err( obj, "cannot add this stream (unsupported "
536 "codec: %4.4s)", (char*)&p_fmt->i_codec );
545 rtp_packetize_h264_nal( sout_stream_id_sys_t *id,
546 const uint8_t *p_data, int i_data, int64_t i_pts,
547 int64_t i_dts, bool b_last, int64_t i_length );
549 int rtp_packetize_xiph_config( sout_stream_id_sys_t *id, const char *fmtp,
555 /* extract base64 configuration from fmtp */
556 char *start = strstr(fmtp, "configuration=");
557 assert(start != NULL);
558 start += sizeof("configuration=") - 1;
559 char *end = strchr(start, ';');
561 size_t len = end - start;
563 memcpy(b64, start, len);
566 int i_max = rtp_mtu (id) - 6; /* payload max in one packet */
568 uint8_t *p_orig, *p_data;
571 i_data = vlc_b64_decode_binary(&p_orig, b64);
580 int i_count = ( i_data + i_max - 1 ) / i_max;
582 for( int i = 0; i < i_count; i++ )
584 int i_payload = __MIN( i_max, i_data );
585 block_t *out = block_Alloc( 18 + i_payload );
587 unsigned fragtype, numpkts;
598 else if (i == i_count - 1)
603 /* Ident:24, Fragment type:2, Vorbis/Theora Data Type:2, # of pkts:4 */
604 uint32_t header = ((XIPH_IDENT & 0xffffff) << 8) |
605 (fragtype << 6) | (1 << 4) | numpkts;
607 /* rtp common header */
608 rtp_packetize_common( id, out, 0, i_pts );
610 SetDWBE( out->p_buffer + 12, header);
611 SetWBE( out->p_buffer + 16, i_payload);
612 memcpy( &out->p_buffer[18], p_data, i_payload );
616 rtp_packetize_send( id, out );
628 static int rtp_packetize_xiph( sout_stream_id_sys_t *id, block_t *in )
630 int i_max = rtp_mtu (id) - 6; /* payload max in one packet */
631 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
633 uint8_t *p_data = in->p_buffer;
634 int i_data = in->i_buffer;
636 for( int i = 0; i < i_count; i++ )
638 int i_payload = __MIN( i_max, i_data );
639 block_t *out = block_Alloc( 18 + i_payload );
641 unsigned fragtype, numpkts;
644 /* No fragmentation */
654 else if (i == i_count - 1)
659 /* Ident:24, Fragment type:2, Vorbis/Theora Data Type:2, # of pkts:4 */
660 uint32_t header = ((XIPH_IDENT & 0xffffff) << 8) |
661 (fragtype << 6) | (0 << 4) | numpkts;
663 /* rtp common header */
664 rtp_packetize_common( id, out, 0, in->i_pts);
666 SetDWBE( out->p_buffer + 12, header);
667 SetWBE( out->p_buffer + 16, i_payload);
668 memcpy( &out->p_buffer[18], p_data, i_payload );
670 out->i_dts = in->i_dts + i * in->i_length / i_count;
671 out->i_length = in->i_length / i_count;
673 rtp_packetize_send( id, out );
683 static int rtp_packetize_mpa( sout_stream_id_sys_t *id, block_t *in )
685 int i_max = rtp_mtu (id) - 4; /* payload max in one packet */
686 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
688 uint8_t *p_data = in->p_buffer;
689 int i_data = in->i_buffer;
692 for( i = 0; i < i_count; i++ )
694 int i_payload = __MIN( i_max, i_data );
695 block_t *out = block_Alloc( 16 + i_payload );
697 /* rtp common header */
698 rtp_packetize_common( id, out, (i == i_count - 1)?1:0, in->i_pts );
700 SetWBE( out->p_buffer + 12, 0 );
701 /* fragment offset in the current frame */
702 SetWBE( out->p_buffer + 14, i * i_max );
703 memcpy( &out->p_buffer[16], p_data, i_payload );
705 out->i_dts = in->i_dts + i * in->i_length / i_count;
706 out->i_length = in->i_length / i_count;
708 rtp_packetize_send( id, out );
719 static int rtp_packetize_mpv( sout_stream_id_sys_t *id, block_t *in )
721 int i_max = rtp_mtu (id) - 4; /* payload max in one packet */
722 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
724 uint8_t *p_data = in->p_buffer;
725 int i_data = in->i_buffer;
727 int b_sequence_start = 0;
728 int i_temporal_ref = 0;
729 int i_picture_coding_type = 0;
730 int i_fbv = 0, i_bfc = 0, i_ffv = 0, i_ffc = 0;
731 int b_start_slice = 0;
733 /* preparse this packet to get some info */
734 if( in->i_buffer > 4 )
737 int i_rest = in->i_buffer;
742 ( p[0] != 0x00 || p[1] != 0x00 || p[2] != 0x01 ) )
756 /* sequence start code */
757 b_sequence_start = 1;
759 else if( *p == 0x00 && i_rest >= 4 )
762 i_temporal_ref = ( p[1] << 2) |((p[2]>>6)&0x03);
763 i_picture_coding_type = (p[2] >> 3)&0x07;
765 if( i_rest >= 4 && ( i_picture_coding_type == 2 ||
766 i_picture_coding_type == 3 ) )
768 i_ffv = (p[3] >> 2)&0x01;
769 i_ffc = ((p[3]&0x03) << 1)|((p[4]>>7)&0x01);
770 if( i_rest > 4 && i_picture_coding_type == 3 )
772 i_fbv = (p[4]>>6)&0x01;
773 i_bfc = (p[4]>>3)&0x07;
777 else if( *p <= 0xaf )
784 for( i = 0; i < i_count; i++ )
786 int i_payload = __MIN( i_max, i_data );
787 block_t *out = block_Alloc( 16 + i_payload );
788 /* MBZ:5 T:1 TR:10 AN:1 N:1 S:1 B:1 E:1 P:3 FBV:1 BFC:3 FFV:1 FFC:3 */
789 uint32_t h = ( i_temporal_ref << 16 )|
790 ( b_sequence_start << 13 )|
791 ( b_start_slice << 12 )|
792 ( i == i_count - 1 ? 1 << 11 : 0 )|
793 ( i_picture_coding_type << 8 )|
794 ( i_fbv << 7 )|( i_bfc << 4 )|( i_ffv << 3 )|i_ffc;
796 /* rtp common header */
797 rtp_packetize_common( id, out, (i == i_count - 1)?1:0,
798 in->i_pts > VLC_TS_INVALID ? in->i_pts : in->i_dts );
800 SetDWBE( out->p_buffer + 12, h );
802 memcpy( &out->p_buffer[16], p_data, i_payload );
804 out->i_dts = in->i_dts + i * in->i_length / i_count;
805 out->i_length = in->i_length / i_count;
807 rtp_packetize_send( id, out );
817 static int rtp_packetize_ac3( sout_stream_id_sys_t *id, block_t *in )
819 int i_max = rtp_mtu (id) - 2; /* payload max in one packet */
820 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
822 uint8_t *p_data = in->p_buffer;
823 int i_data = in->i_buffer;
826 for( i = 0; i < i_count; i++ )
828 int i_payload = __MIN( i_max, i_data );
829 block_t *out = block_Alloc( 14 + i_payload );
831 /* rtp common header */
832 rtp_packetize_common( id, out, (i == i_count - 1)?1:0, in->i_pts );
834 out->p_buffer[12] = 1;
836 out->p_buffer[13] = 0x00;
838 memcpy( &out->p_buffer[14], p_data, i_payload );
840 out->i_dts = in->i_dts + i * in->i_length / i_count;
841 out->i_length = in->i_length / i_count;
843 rtp_packetize_send( id, out );
853 static int rtp_packetize_simple(sout_stream_id_sys_t *id, block_t *block)
855 bool marker = (block->i_flags & BLOCK_FLAG_DISCONTINUITY) != 0;
857 block = block_Realloc(block, 12, block->i_buffer);
858 if (unlikely(block == NULL))
861 rtp_packetize_common(id, block, marker, block->i_pts);
862 rtp_packetize_send(id, block);
866 static int rtp_packetize_split( sout_stream_id_sys_t *id, block_t *in )
868 int i_max = rtp_mtu (id); /* payload max in one packet */
869 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
871 uint8_t *p_data = in->p_buffer;
872 int i_data = in->i_buffer;
875 for( i = 0; i < i_count; i++ )
877 int i_payload = __MIN( i_max, i_data );
878 block_t *out = block_Alloc( 12 + i_payload );
880 /* rtp common header */
881 rtp_packetize_common( id, out, (i == i_count - 1),
882 (in->i_pts > VLC_TS_INVALID ? in->i_pts : in->i_dts) );
883 memcpy( &out->p_buffer[12], p_data, i_payload );
885 out->i_dts = in->i_dts + i * in->i_length / i_count;
886 out->i_length = in->i_length / i_count;
888 rtp_packetize_send( id, out );
898 static int rtp_packetize_pcm(sout_stream_id_sys_t *id, block_t *in)
900 unsigned max = rtp_mtu(id);
902 while (in->i_buffer > max)
904 unsigned duration = (in->i_length * max) / in->i_buffer;
905 bool marker = (in->i_flags & BLOCK_FLAG_DISCONTINUITY) != 0;
907 block_t *out = block_Alloc(12 + max);
908 if (unlikely(out == NULL))
914 rtp_packetize_common(id, out, marker, in->i_pts);
915 memcpy(out->p_buffer + 12, in->p_buffer, max);
916 rtp_packetize_send(id, out);
920 in->i_pts += duration;
921 in->i_length -= duration;
922 in->i_flags &= ~BLOCK_FLAG_DISCONTINUITY;
925 return rtp_packetize_simple(id, in); /* zero copy for the last frame */
928 /* split and convert from little endian to network byte order */
929 static int rtp_packetize_swab(sout_stream_id_sys_t *id, block_t *in)
931 unsigned max = rtp_mtu(id);
933 while (in->i_buffer > 0)
935 unsigned payload = (max < in->i_buffer) ? max : in->i_buffer;
936 unsigned duration = (in->i_length * payload) / in->i_buffer;
937 bool marker = (in->i_flags & BLOCK_FLAG_DISCONTINUITY) != 0;
939 block_t *out = block_Alloc(12 + payload);
940 if (unlikely(out == NULL))
946 rtp_packetize_common(id, out, marker, in->i_pts);
947 swab(in->p_buffer, out->p_buffer + 12, payload);
948 rtp_packetize_send(id, out);
950 in->p_buffer += payload;
951 in->i_buffer -= payload;
952 in->i_pts += duration;
953 in->i_length -= duration;
954 in->i_flags &= ~BLOCK_FLAG_DISCONTINUITY;
962 static int rtp_packetize_mp4a_latm( sout_stream_id_sys_t *id, block_t *in )
964 int i_max = rtp_mtu (id) - 2; /* payload max in one packet */
965 int latmhdrsize = in->i_buffer / 0xff + 1;
966 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
968 uint8_t *p_data = in->p_buffer, *p_header = NULL;
969 int i_data = in->i_buffer;
972 for( i = 0; i < i_count; i++ )
974 int i_payload = __MIN( i_max, i_data );
979 out = block_Alloc( 12 + latmhdrsize + i_payload );
981 /* rtp common header */
982 rtp_packetize_common( id, out, ((i == i_count - 1) ? 1 : 0),
983 (in->i_pts > VLC_TS_INVALID ? in->i_pts : in->i_dts) );
987 int tmp = in->i_buffer;
989 p_header=out->p_buffer+12;
999 memcpy( &out->p_buffer[12+latmhdrsize], p_data, i_payload );
1001 out->i_dts = in->i_dts + i * in->i_length / i_count;
1002 out->i_length = in->i_length / i_count;
1004 rtp_packetize_send( id, out );
1006 p_data += i_payload;
1007 i_data -= i_payload;
1014 static int rtp_packetize_mp4a( sout_stream_id_sys_t *id, block_t *in )
1016 int i_max = rtp_mtu (id) - 4; /* payload max in one packet */
1017 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1019 uint8_t *p_data = in->p_buffer;
1020 int i_data = in->i_buffer;
1023 for( i = 0; i < i_count; i++ )
1025 int i_payload = __MIN( i_max, i_data );
1026 block_t *out = block_Alloc( 16 + i_payload );
1028 /* rtp common header */
1029 rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
1030 (in->i_pts > VLC_TS_INVALID ? in->i_pts : in->i_dts) );
1032 /* AU headers length (bits) */
1033 out->p_buffer[12] = 0;
1034 out->p_buffer[13] = 2*8;
1035 /* for each AU length 13 bits + idx 3bits, */
1036 SetWBE( out->p_buffer + 14, (in->i_buffer << 3) | 0 );
1038 memcpy( &out->p_buffer[16], p_data, i_payload );
1040 out->i_dts = in->i_dts + i * in->i_length / i_count;
1041 out->i_length = in->i_length / i_count;
1043 rtp_packetize_send( id, out );
1045 p_data += i_payload;
1046 i_data -= i_payload;
1055 #define RTP_H263_HEADER_SIZE (2) // plen = 0
1056 #define RTP_H263_PAYLOAD_START (14) // plen = 0
1057 static int rtp_packetize_h263( sout_stream_id_sys_t *id, block_t *in )
1059 uint8_t *p_data = in->p_buffer;
1060 int i_data = in->i_buffer;
1062 int i_max = rtp_mtu (id) - RTP_H263_HEADER_SIZE; /* payload max in one packet */
1065 int b_v_bit = 0; // no pesky error resilience
1066 int i_plen = 0; // normally plen=0 for PSC packet
1067 int i_pebit = 0; // because plen=0
1073 return VLC_EGENERIC;
1075 if( p_data[0] || p_data[1] )
1078 return VLC_EGENERIC;
1080 /* remove 2 leading 0 bytes */
1083 i_count = ( i_data + i_max - 1 ) / i_max;
1085 for( i = 0; i < i_count; i++ )
1087 int i_payload = __MIN( i_max, i_data );
1088 block_t *out = block_Alloc( RTP_H263_PAYLOAD_START + i_payload );
1089 b_p_bit = (i == 0) ? 1 : 0;
1090 h = ( b_p_bit << 10 )|
1095 /* rtp common header */
1096 //b_m_bit = 1; // always contains end of frame
1097 rtp_packetize_common( id, out, (i == i_count - 1)?1:0,
1098 in->i_pts > VLC_TS_INVALID ? in->i_pts : in->i_dts );
1101 SetWBE( out->p_buffer + 12, h );
1102 memcpy( &out->p_buffer[RTP_H263_PAYLOAD_START], p_data, i_payload );
1104 out->i_dts = in->i_dts + i * in->i_length / i_count;
1105 out->i_length = in->i_length / i_count;
1107 rtp_packetize_send( id, out );
1109 p_data += i_payload;
1110 i_data -= i_payload;
1119 rtp_packetize_h264_nal( sout_stream_id_sys_t *id,
1120 const uint8_t *p_data, int i_data, int64_t i_pts,
1121 int64_t i_dts, bool b_last, int64_t i_length )
1123 const int i_max = rtp_mtu (id); /* payload max in one packet */
1130 i_nal_hdr = p_data[3];
1131 i_nal_type = i_nal_hdr&0x1f;
1133 /* Skip start code */
1138 if( i_data <= i_max )
1140 /* Single NAL unit packet */
1141 block_t *out = block_Alloc( 12 + i_data );
1143 out->i_length = i_length;
1146 rtp_packetize_common( id, out, b_last, i_pts );
1148 memcpy( &out->p_buffer[12], p_data, i_data );
1150 rtp_packetize_send( id, out );
1154 /* FU-A Fragmentation Unit without interleaving */
1155 const int i_count = ( i_data-1 + i_max-2 - 1 ) / (i_max-2);
1161 for( i = 0; i < i_count; i++ )
1163 const int i_payload = __MIN( i_data, i_max-2 );
1164 block_t *out = block_Alloc( 12 + 2 + i_payload );
1165 out->i_dts = i_dts + i * i_length / i_count;
1166 out->i_length = i_length / i_count;
1169 rtp_packetize_common( id, out, (b_last && i_payload == i_data),
1172 out->p_buffer[12] = 0x00 | (i_nal_hdr & 0x60) | 28;
1174 out->p_buffer[13] = ( i == 0 ? 0x80 : 0x00 ) | ( (i == i_count-1) ? 0x40 : 0x00 ) | i_nal_type;
1175 memcpy( &out->p_buffer[14], p_data, i_payload );
1177 rtp_packetize_send( id, out );
1179 i_data -= i_payload;
1180 p_data += i_payload;
1186 static int rtp_packetize_h264( sout_stream_id_sys_t *id, block_t *in )
1188 const uint8_t *p_buffer = in->p_buffer;
1189 int i_buffer = in->i_buffer;
1191 while( i_buffer > 4 && ( p_buffer[0] != 0 || p_buffer[1] != 0 || p_buffer[2] != 1 ) )
1197 /* Split nal units */
1198 while( i_buffer > 4 )
1201 int i_size = i_buffer;
1202 int i_skip = i_buffer;
1204 /* search nal end */
1205 for( i_offset = 4; i_offset+2 < i_buffer ; i_offset++)
1207 if( p_buffer[i_offset] == 0 && p_buffer[i_offset+1] == 0 && p_buffer[i_offset+2] == 1 )
1209 /* we found another startcode */
1210 i_size = i_offset - ( p_buffer[i_offset-1] == 0 ? 1 : 0);
1215 /* TODO add STAP-A to remove a lot of overhead with small slice/sei/... */
1216 rtp_packetize_h264_nal( id, p_buffer, i_size,
1217 (in->i_pts > VLC_TS_INVALID ? in->i_pts : in->i_dts), in->i_dts,
1218 (i_size >= i_buffer), in->i_length * i_size / in->i_buffer );
1228 static int rtp_packetize_amr( sout_stream_id_sys_t *id, block_t *in )
1230 int i_max = rtp_mtu (id) - 2; /* payload max in one packet */
1231 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1233 uint8_t *p_data = in->p_buffer;
1234 int i_data = in->i_buffer;
1237 /* Only supports octet-aligned mode */
1238 for( i = 0; i < i_count; i++ )
1240 int i_payload = __MIN( i_max, i_data );
1241 block_t *out = block_Alloc( 14 + i_payload );
1243 /* rtp common header */
1244 rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
1245 (in->i_pts > VLC_TS_INVALID ? in->i_pts : in->i_dts) );
1246 /* Payload header */
1247 out->p_buffer[12] = 0xF0; /* CMR */
1248 out->p_buffer[13] = p_data[0]&0x7C; /* ToC */ /* FIXME: frame type */
1250 /* FIXME: are we fed multiple frames ? */
1251 memcpy( &out->p_buffer[14], p_data+1, i_payload-1 );
1253 out->i_buffer--; /* FIXME? */
1254 out->i_dts = in->i_dts + i * in->i_length / i_count;
1255 out->i_length = in->i_length / i_count;
1257 rtp_packetize_send( id, out );
1259 p_data += i_payload;
1260 i_data -= i_payload;
1267 static int rtp_packetize_t140( sout_stream_id_sys_t *id, block_t *in )
1269 const size_t i_max = rtp_mtu (id);
1270 const uint8_t *p_data = in->p_buffer;
1271 size_t i_data = in->i_buffer;
1273 for( unsigned i_packet = 0; i_data > 0; i_packet++ )
1275 size_t i_payload = i_data;
1277 /* Make sure we stop on an UTF-8 character boundary
1278 * (assuming the input is valid UTF-8) */
1279 if( i_data > i_max )
1283 while( ( p_data[i_payload] & 0xC0 ) == 0x80 )
1285 if( i_payload == 0 )
1288 return VLC_SUCCESS; /* fishy input! */
1294 block_t *out = block_Alloc( 12 + i_payload );
1301 rtp_packetize_common( id, out, 0, in->i_pts + i_packet );
1302 memcpy( out->p_buffer + 12, p_data, i_payload );
1304 out->i_dts = in->i_pts;
1307 rtp_packetize_send( id, out );
1309 p_data += i_payload;
1310 i_data -= i_payload;
1318 static int rtp_packetize_spx( sout_stream_id_sys_t *id, block_t *in )
1320 uint8_t *p_buffer = in->p_buffer;
1321 int i_data_size, i_payload_size, i_payload_padding;
1322 i_data_size = i_payload_size = in->i_buffer;
1323 i_payload_padding = 0;
1326 if ( in->i_buffer > rtp_mtu (id) )
1333 RFC for Speex in RTP says that each packet must end on an octet
1334 boundary. So, we check to see if the number of bytes % 4 is zero.
1335 If not, we have to add some padding.
1337 This MAY be overkill since packetization is handled elsewhere and
1338 appears to ensure the octet boundary. However, better safe than
1341 if ( i_payload_size % 4 )
1343 i_payload_padding = 4 - ( i_payload_size % 4 );
1344 i_payload_size += i_payload_padding;
1348 Allocate a new RTP p_output block of the appropriate size.
1349 Allow for 12 extra bytes of RTP header.
1351 p_out = block_Alloc( 12 + i_payload_size );
1353 if ( i_payload_padding )
1356 The padding is required to be a zero followed by all 1s.
1358 char c_first_pad, c_remaining_pad;
1360 c_remaining_pad = 0xFF;
1363 Allow for 12 bytes before the i_data_size because
1364 of the expected RTP header added during
1365 rtp_packetize_common.
1367 p_out->p_buffer[12 + i_data_size] = c_first_pad;
1368 switch (i_payload_padding)
1371 p_out->p_buffer[12 + i_data_size + 1] = c_remaining_pad;
1374 p_out->p_buffer[12 + i_data_size + 1] = c_remaining_pad;
1375 p_out->p_buffer[12 + i_data_size + 2] = c_remaining_pad;
1380 /* Add the RTP header to our p_output buffer. */
1381 rtp_packetize_common( id, p_out, 0,
1382 (in->i_pts > VLC_TS_INVALID ? in->i_pts : in->i_dts) );
1383 /* Copy the Speex payload to the p_output buffer */
1384 memcpy( &p_out->p_buffer[12], p_buffer, i_data_size );
1386 p_out->i_dts = in->i_dts;
1387 p_out->i_length = in->i_length;
1390 /* Queue the buffer for actual transmission. */
1391 rtp_packetize_send( id, p_out );
1395 static int rtp_packetize_g726( sout_stream_id_sys_t *id, block_t *in, int i_pad )
1397 int i_max = (rtp_mtu( id )- 12 + i_pad - 1) & ~i_pad;
1398 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1400 uint8_t *p_data = in->p_buffer;
1401 int i_data = in->i_buffer;
1406 int i_payload = __MIN( i_max, i_data );
1407 block_t *out = block_Alloc( 12 + i_payload );
1409 /* rtp common header */
1410 rtp_packetize_common( id, out, 0,
1411 (in->i_pts > VLC_TS_INVALID ? in->i_pts : in->i_dts) );
1413 memcpy( &out->p_buffer[12], p_data, i_payload );
1415 out->i_dts = in->i_dts + i_packet++ * in->i_length / i_count;
1416 out->i_length = in->i_length / i_count;
1418 rtp_packetize_send( id, out );
1420 p_data += i_payload;
1421 i_data -= i_payload;
1428 static int rtp_packetize_g726_16( sout_stream_id_sys_t *id, block_t *in )
1430 return rtp_packetize_g726( id, in, 4 );
1433 static int rtp_packetize_g726_24( sout_stream_id_sys_t *id, block_t *in )
1435 return rtp_packetize_g726( id, in, 8 );
1438 static int rtp_packetize_g726_32( sout_stream_id_sys_t *id, block_t *in )
1440 return rtp_packetize_g726( id, in, 2 );
1443 static int rtp_packetize_g726_40( sout_stream_id_sys_t *id, block_t *in )
1445 return rtp_packetize_g726( id, in, 8 );
1448 #define RTP_VP8_HEADER_SIZE 1
1449 #define RTP_VP8_PAYLOAD_START (12 + RTP_VP8_HEADER_SIZE)
1451 static int rtp_packetize_vp8( sout_stream_id_sys_t *id, block_t *in )
1453 int i_max = rtp_mtu (id) - RTP_VP8_HEADER_SIZE;
1454 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1456 uint8_t *p_data = in->p_buffer;
1457 int i_data = in->i_buffer;
1462 return VLC_EGENERIC;
1465 for( int i = 0; i < i_count; i++ )
1467 int i_payload = __MIN( i_max, i_data );
1468 block_t *out = block_Alloc( RTP_VP8_PAYLOAD_START + i_payload );
1475 /* VP8 payload header */
1476 /* All frames are marked as reference ones */
1478 out->p_buffer[12] = 0x10; // partition start
1480 out->p_buffer[12] = 0;
1482 /* rtp common header */
1483 rtp_packetize_common( id, out, (i == i_count - 1),
1484 (in->i_pts > VLC_TS_INVALID ? in->i_pts : in->i_dts) );
1485 memcpy( &out->p_buffer[RTP_VP8_PAYLOAD_START], p_data, i_payload );
1487 out->i_dts = in->i_dts + i * in->i_length / i_count;
1488 out->i_length = in->i_length / i_count;
1490 rtp_packetize_send( id, out );
1492 p_data += i_payload;
1493 i_data -= i_payload;
1500 static int rtp_packetize_jpeg( sout_stream_id_sys_t *id, block_t *in )
1502 uint8_t *p_data = in->p_buffer;
1503 int i_data = in->i_buffer;
1504 uint8_t *bufend = p_data + i_data;
1506 const uint8_t *qtables = NULL;
1508 int off = 0; // fragment offset in frame
1509 int y_sampling_factor;
1510 // type is set by pixel format (determined by y_sampling_factor):
1513 // += 64 if DRI present
1515 int w = 0; // Width in multiples of 8
1516 int h = 0; // Height in multiples of 8
1517 int restart_interval;
1521 if (GetWBE(p_data) != 0xffd8)
1526 /* parse the header to get necessary info */
1527 int header_finished = 0;
1528 while (!header_finished && p_data + 4 <= bufend) {
1529 uint16_t marker = GetWBE(p_data);
1530 uint8_t *section = p_data + 2;
1531 int section_size = GetWBE(section);
1532 uint8_t *section_body = p_data + 4;
1533 if (section + section_size > bufend)
1536 assert((marker & 0xff00) == 0xff00);
1539 case 0xffdb /*DQT*/:
1540 if (section_body[0])
1541 goto error; // Only 8-bit precision is supported
1543 /* a quantization table is 64 bytes long */
1544 nb_qtables = section_size / 65;
1545 qtables = section_body;
1547 case 0xffc0 /*SOF0*/:
1549 int height = GetWBE(§ion_body[1]);
1550 int width = GetWBE(§ion_body[3]);
1551 if (width > 2040 || height > 2040)
1553 // larger than limit supported by RFC 2435
1556 // Round up by 8, divide by 8
1557 w = ((width+7)&~7) >> 3;
1558 h = ((height+7)&~7) >> 3;
1560 // Get components sampling to determine type
1561 // Y has component ID 1
1562 // Possible configurations of sampling factors:
1563 // Y - 0x22, Cb - 0x11, Cr - 0x11 => yuvj420p
1564 // Y - 0x21, Cb - 0x11, Cr - 0x11 => yuvj422p
1566 // Only 3 components are supported by RFC 2435
1567 if (section_body[5] != 3) // Number of components
1569 for (int j = 0; j < 3; j++)
1571 if (section_body[6 + j * 3] == 1 /* Y */)
1573 y_sampling_factor = section_body[6 + j * 3 + 1];
1575 else if (section_body[6 + j * 3 + 1] != 0x11)
1577 // Sampling factor is unsupported by RFC 2435
1583 case 0xffdd /*DRI*/:
1584 restart_interval = GetWBE(section_body);
1587 case 0xffda /*SOS*/:
1588 /* SOS is last marker in the header */
1589 header_finished = 1;
1592 // Step over marker, 16bit length and section body
1593 p_data += 2 + section_size;
1594 i_data -= 2 + section_size;
1596 if (!header_finished)
1601 switch (y_sampling_factor)
1603 case 0x22: // yuvj420p
1606 case 0x21: // yuvj422p
1610 goto error; // Sampling format unsupported by RFC 2435
1618 int hdr_size = 8 + dri_found * 4;
1619 if (off == 0 && nb_qtables)
1620 hdr_size += 4 + 64 * nb_qtables;
1622 int i_payload = __MIN( i_data, (int)(rtp_mtu (id) - hdr_size) );
1623 if ( i_payload <= 0 )
1624 return VLC_EGENERIC;
1626 block_t *out = block_Alloc( 12 + hdr_size + i_payload );
1630 uint8_t *p = out->p_buffer + 12;
1631 /* set main header */
1632 /* set type-specific=0, set offset in following 24 bits: */
1633 SetDWBE(p, off & 0x00ffffff);
1636 *p++ = 255; // Quant value
1640 // Write restart_marker_hdr if needed
1643 SetWBE(p, restart_interval);
1645 // restart_count. Hardcoded same value as in gstreamer implementation
1650 if (off == 0 && nb_qtables)
1652 /* set quantization tables header */
1655 SetWBE (p, 64 * nb_qtables);
1657 for (int i = 0; i < nb_qtables; i++)
1659 memcpy (p, &qtables[65 * i + 1], 64);
1664 /* rtp common header */
1665 rtp_packetize_common( id, out, (i_payload == i_data),
1666 (in->i_pts > VLC_TS_INVALID ? in->i_pts : in->i_dts) );
1667 memcpy( p, p_data, i_payload );
1669 out->i_dts = in->i_dts;
1670 out->i_length = in->i_length;
1672 rtp_packetize_send( id, out );
1674 p_data += i_payload;
1675 i_data -= i_payload;
1683 return VLC_EGENERIC;