1 /*****************************************************************************
2 * rtpfmt.c: RTP payload formats
3 *****************************************************************************
4 * Copyright (C) 2003-2004 the VideoLAN team
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
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 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 General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, 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_t *, block_t *);
40 static int rtp_packetize_mpv (sout_stream_id_t *, block_t *);
41 static int rtp_packetize_ac3 (sout_stream_id_t *, block_t *);
42 static int rtp_packetize_split(sout_stream_id_t *, block_t *);
43 static int rtp_packetize_swab (sout_stream_id_t *, block_t *);
44 static int rtp_packetize_mp4a (sout_stream_id_t *, block_t *);
45 static int rtp_packetize_mp4a_latm (sout_stream_id_t *, block_t *);
46 static int rtp_packetize_h263 (sout_stream_id_t *, block_t *);
47 static int rtp_packetize_h264 (sout_stream_id_t *, block_t *);
48 static int rtp_packetize_amr (sout_stream_id_t *, block_t *);
49 static int rtp_packetize_spx (sout_stream_id_t *, block_t *);
50 static int rtp_packetize_t140 (sout_stream_id_t *, block_t *);
51 static int rtp_packetize_g726_16 (sout_stream_id_t *, block_t *);
52 static int rtp_packetize_g726_24 (sout_stream_id_t *, block_t *);
53 static int rtp_packetize_g726_32 (sout_stream_id_t *, block_t *);
54 static int rtp_packetize_g726_40 (sout_stream_id_t *, block_t *);
55 static int rtp_packetize_xiph (sout_stream_id_t *, block_t *);
57 #define XIPH_IDENT (0)
59 /* Helpers common to xiph codecs (vorbis and theora) */
61 static int rtp_xiph_pack_headers(size_t room, void *p_extra, size_t i_extra,
62 uint8_t **p_buffer, size_t *i_buffer,
63 uint8_t *theora_pixel_fmt)
65 unsigned packet_size[XIPH_MAX_HEADER_COUNT];
66 void *packet[XIPH_MAX_HEADER_COUNT];
67 unsigned packet_count;
68 if (xiph_SplitHeaders(packet_size, packet, &packet_count,
74 if (theora_pixel_fmt != NULL)
76 if (packet_size[0] < 42)
78 *theora_pixel_fmt = (((uint8_t *)packet[0])[41] >> 3) & 0x03;
81 unsigned length_size[2] = { 0, 0 };
82 for (int i = 0; i < 2; i++)
84 unsigned size = packet_size[i];
92 *i_buffer = room + 1 + length_size[0] + length_size[1]
93 + packet_size[0] + packet_size[1] + packet_size[2];
94 *p_buffer = malloc(*i_buffer);
95 if (*p_buffer == NULL)
98 uint8_t *p = *p_buffer + room;
99 /* Number of headers */
102 for (int i = 0; i < 2; i++)
104 unsigned size = length_size[i];
107 *p = (packet_size[i] >> (7 * (size - 1))) & 0x7f;
113 for (int i = 0; i < 3; i++)
115 memcpy(p, packet[i], packet_size[i]);
122 static char *rtp_xiph_b64_oob_config(void *p_extra, size_t i_extra,
123 uint8_t *theora_pixel_fmt)
127 if (rtp_xiph_pack_headers(9, p_extra, i_extra, &p_buffer, &i_buffer,
128 theora_pixel_fmt) != VLC_SUCCESS)
131 /* Number of packed headers */
132 SetDWBE(p_buffer, 1);
134 uint32_t ident = XIPH_IDENT;
135 SetWBE(p_buffer + 4, ident >> 8);
136 p_buffer[6] = ident & 0xff;
138 SetWBE(p_buffer + 7, i_buffer);
140 char *config = vlc_b64_encode_binary(p_buffer, i_buffer);
145 static void sprintf_hexa( char *s, uint8_t *p_data, int i_data )
147 static const char hex[16] = "0123456789abcdef";
149 for( int i = 0; i < i_data; i++ )
151 s[2*i+0] = hex[(p_data[i]>>4)&0xf];
152 s[2*i+1] = hex[(p_data[i] )&0xf];
157 /* TODO: make this into something more clever than a big switch? */
158 int rtp_get_fmt( vlc_object_t *obj, es_format_t *p_fmt, const char *mux,
159 rtp_format_t *rtp_fmt )
161 assert( p_fmt != NULL || mux != NULL );
163 /* Dynamic payload type. Payload types are scoped to the RTP
164 * session, and we put each ES in its own session, so no risk of
166 rtp_fmt->payload_type = 96;
167 rtp_fmt->cat = mux != NULL ? VIDEO_ES : p_fmt->i_cat;
168 if( rtp_fmt->cat == AUDIO_ES )
170 rtp_fmt->clock_rate = p_fmt->audio.i_rate;
171 rtp_fmt->channels = p_fmt->audio.i_channels;
174 rtp_fmt->clock_rate = 90000; /* most common case for video */
175 /* Stream bitrate in kbps */
176 rtp_fmt->bitrate = p_fmt != NULL ? p_fmt->i_bitrate/1000 : 0;
177 rtp_fmt->fmtp = NULL;
181 if( strncmp( mux, "ts", 2 ) == 0 )
183 rtp_fmt->payload_type = 33;
184 rtp_fmt->ptname = "MP2T";
187 rtp_fmt->ptname = "MP2P";
191 switch( p_fmt->i_codec )
193 case VLC_CODEC_MULAW:
194 if( p_fmt->audio.i_channels == 1 && p_fmt->audio.i_rate == 8000 )
195 rtp_fmt->payload_type = 0;
196 rtp_fmt->ptname = "PCMU";
197 rtp_fmt->pf_packetize = rtp_packetize_split;
200 if( p_fmt->audio.i_channels == 1 && p_fmt->audio.i_rate == 8000 )
201 rtp_fmt->payload_type = 8;
202 rtp_fmt->ptname = "PCMA";
203 rtp_fmt->pf_packetize = rtp_packetize_split;
207 if( p_fmt->audio.i_channels == 1 && p_fmt->audio.i_rate == 44100 )
209 rtp_fmt->payload_type = 11;
211 else if( p_fmt->audio.i_channels == 2 &&
212 p_fmt->audio.i_rate == 44100 )
214 rtp_fmt->payload_type = 10;
216 rtp_fmt->ptname = "L16";
217 if( p_fmt->i_codec == VLC_CODEC_S16B )
218 rtp_fmt->pf_packetize = rtp_packetize_split;
220 rtp_fmt->pf_packetize = rtp_packetize_swab;
223 rtp_fmt->ptname = "L8";
224 rtp_fmt->pf_packetize = rtp_packetize_split;
227 rtp_fmt->ptname = "L24";
228 rtp_fmt->pf_packetize = rtp_packetize_split;
231 rtp_fmt->payload_type = 14;
232 rtp_fmt->ptname = "MPA";
233 rtp_fmt->clock_rate = 90000; /* not 44100 */
234 rtp_fmt->pf_packetize = rtp_packetize_mpa;
237 rtp_fmt->payload_type = 32;
238 rtp_fmt->ptname = "MPV";
239 rtp_fmt->pf_packetize = rtp_packetize_mpv;
241 case VLC_CODEC_ADPCM_G726:
242 switch( p_fmt->i_bitrate / 1000 )
245 rtp_fmt->ptname = "G726-16";
246 rtp_fmt->pf_packetize = rtp_packetize_g726_16;
249 rtp_fmt->ptname = "G726-24";
250 rtp_fmt->pf_packetize = rtp_packetize_g726_24;
253 rtp_fmt->ptname = "G726-32";
254 rtp_fmt->pf_packetize = rtp_packetize_g726_32;
257 rtp_fmt->ptname = "G726-40";
258 rtp_fmt->pf_packetize = rtp_packetize_g726_40;
261 msg_Err( obj, "cannot add this stream (unsupported "
262 "G.726 bit rate: %u)", p_fmt->i_bitrate );
267 rtp_fmt->ptname = "ac3";
268 rtp_fmt->pf_packetize = rtp_packetize_ac3;
271 rtp_fmt->ptname = "H263-1998";
272 rtp_fmt->pf_packetize = rtp_packetize_h263;
275 rtp_fmt->ptname = "H264";
276 rtp_fmt->pf_packetize = rtp_packetize_h264;
277 rtp_fmt->fmtp = NULL;
279 if( p_fmt->i_extra > 0 )
281 uint8_t *p_buffer = p_fmt->p_extra;
282 int i_buffer = p_fmt->i_extra;
283 char *p_64_sps = NULL;
284 char *p_64_pps = NULL;
287 while( i_buffer > 4 )
292 while( p_buffer[0] != 0 || p_buffer[1] != 0 ||
297 if( i_buffer == 0 ) break;
300 if( i_buffer < 4 || memcmp(p_buffer, "\x00\x00\x01", 3 ) )
302 msg_Dbg( obj, "No startcode found..");
308 const int i_nal_type = p_buffer[0]&0x1f;
310 msg_Dbg( obj, "we found a startcode for NAL with TYPE:%d", i_nal_type );
313 for( i_offset = 0; i_offset+2 < i_buffer ; i_offset++)
315 if( !memcmp(p_buffer + i_offset, "\x00\x00\x01", 3 ) )
317 /* we found another startcode */
318 while( i_offset > 0 && 0 == p_buffer[ i_offset - 1 ] )
327 msg_Dbg( obj, "No-info found in nal ");
331 if( i_nal_type == 7 )
334 p_64_sps = vlc_b64_encode_binary( p_buffer, i_size );
335 /* XXX: nothing ensures that i_size >= 4 ?? */
336 sprintf_hexa( hexa, &p_buffer[1], 3 );
338 else if( i_nal_type == 8 )
341 p_64_pps = vlc_b64_encode_binary( p_buffer, i_size );
347 if( p_64_sps && p_64_pps &&
348 ( asprintf( &rtp_fmt->fmtp,
349 "packetization-mode=1;profile-level-id=%s;"
350 "sprop-parameter-sets=%s,%s;", hexa, p_64_sps,
352 rtp_fmt->fmtp = NULL;
356 if( rtp_fmt->fmtp == NULL )
357 rtp_fmt->fmtp = strdup( "packetization-mode=1" );
362 rtp_fmt->ptname = "MP4V-ES";
363 rtp_fmt->pf_packetize = rtp_packetize_split;
364 if( p_fmt->i_extra > 0 )
366 char hexa[2*p_fmt->i_extra +1];
367 sprintf_hexa( hexa, p_fmt->p_extra, p_fmt->i_extra );
368 if( asprintf( &rtp_fmt->fmtp,
369 "profile-level-id=3; config=%s;", hexa ) == -1 )
370 rtp_fmt->fmtp = NULL;
376 if( ! var_InheritBool( obj, "sout-rtp-mp4a-latm" ) )
378 char hexa[2*p_fmt->i_extra +1];
380 rtp_fmt->ptname = "mpeg4-generic";
381 rtp_fmt->pf_packetize = rtp_packetize_mp4a;
382 sprintf_hexa( hexa, p_fmt->p_extra, p_fmt->i_extra );
383 if( asprintf( &rtp_fmt->fmtp,
384 "streamtype=5; profile-level-id=15; "
385 "mode=AAC-hbr; config=%s; SizeLength=13; "
386 "IndexLength=3; IndexDeltaLength=3; Profile=1;",
388 rtp_fmt->fmtp = NULL;
394 unsigned char config[6];
395 unsigned int aacsrates[15] = {
396 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
397 16000, 12000, 11025, 8000, 7350, 0, 0 };
399 for( i = 0; i < 15; i++ )
400 if( p_fmt->audio.i_rate == aacsrates[i] )
406 config[3]=p_fmt->audio.i_channels<<4;
410 rtp_fmt->ptname = "MP4A-LATM";
411 rtp_fmt->pf_packetize = rtp_packetize_mp4a_latm;
412 sprintf_hexa( hexa, config, 6 );
413 if( asprintf( &rtp_fmt->fmtp, "profile-level-id=15; "
414 "object=2; cpresent=0; config=%s", hexa ) == -1 )
415 rtp_fmt->fmtp = NULL;
419 case VLC_CODEC_AMR_NB:
420 rtp_fmt->ptname = "AMR";
421 rtp_fmt->fmtp = strdup( "octet-align=1" );
422 rtp_fmt->pf_packetize = rtp_packetize_amr;
424 case VLC_CODEC_AMR_WB:
425 rtp_fmt->ptname = "AMR-WB";
426 rtp_fmt->fmtp = strdup( "octet-align=1" );
427 rtp_fmt->pf_packetize = rtp_packetize_amr;
429 case VLC_CODEC_SPEEX:
430 rtp_fmt->ptname = "SPEEX";
431 rtp_fmt->pf_packetize = rtp_packetize_spx;
433 case VLC_CODEC_VORBIS:
434 rtp_fmt->ptname = "vorbis";
435 rtp_fmt->pf_packetize = rtp_packetize_xiph;
436 if( p_fmt->i_extra > 0 )
438 rtp_fmt->fmtp = NULL;
439 char *config = rtp_xiph_b64_oob_config(p_fmt->p_extra,
440 p_fmt->i_extra, NULL);
443 if( asprintf( &rtp_fmt->fmtp,
444 "configuration=%s;", config ) == -1 )
445 rtp_fmt->fmtp = NULL;
449 case VLC_CODEC_THEORA:
450 rtp_fmt->ptname = "theora";
451 rtp_fmt->pf_packetize = rtp_packetize_xiph;
452 if( p_fmt->i_extra > 0 )
454 rtp_fmt->fmtp = NULL;
455 uint8_t pixel_fmt, c1, c2;
456 char *config = rtp_xiph_b64_oob_config(p_fmt->p_extra,
484 if( asprintf( &rtp_fmt->fmtp,
485 "sampling=YCbCr-4:%d:%d; width=%d; height=%d; "
486 "delivery-method=inline; configuration=%s; "
487 "delivery-method=in_band;", c1, c2,
488 p_fmt->video.i_width, p_fmt->video.i_height,
490 rtp_fmt->fmtp = NULL;
494 case VLC_CODEC_ITU_T140:
495 rtp_fmt->ptname = "t140" ;
496 rtp_fmt->clock_rate = 1000;
497 rtp_fmt->pf_packetize = rtp_packetize_t140;
501 msg_Err( obj, "cannot add this stream (unsupported "
502 "codec: %4.4s)", (char*)&p_fmt->i_codec );
511 rtp_packetize_h264_nal( sout_stream_id_t *id,
512 const uint8_t *p_data, int i_data, int64_t i_pts,
513 int64_t i_dts, bool b_last, int64_t i_length );
515 int rtp_packetize_xiph_config( sout_stream_id_t *id, const char *fmtp,
521 /* extract base64 configuration from fmtp */
522 char *start = strstr(fmtp, "configuration=");
523 assert(start != NULL);
524 start += sizeof("configuration=") - 1;
525 char *end = strchr(start, ';');
527 size_t len = end - start;
529 memcpy(b64, start, len);
532 int i_max = rtp_mtu (id) - 6; /* payload max in one packet */
534 uint8_t *p_orig, *p_data;
537 i_data = vlc_b64_decode_binary(&p_orig, b64);
544 int i_count = ( i_data + i_max - 1 ) / i_max;
546 for( int i = 0; i < i_count; i++ )
548 int i_payload = __MIN( i_max, i_data );
549 block_t *out = block_Alloc( 18 + i_payload );
551 unsigned fragtype, numpkts;
562 else if (i == i_count - 1)
567 /* Ident:24, Fragment type:2, Vorbis/Theora Data Type:2, # of pkts:4 */
568 uint32_t header = ((XIPH_IDENT & 0xffffff) << 8) |
569 (fragtype << 6) | (1 << 4) | numpkts;
571 /* rtp common header */
572 rtp_packetize_common( id, out, 0, i_pts );
574 SetDWBE( out->p_buffer + 12, header);
575 SetWBE( out->p_buffer + 16, i_payload);
576 memcpy( &out->p_buffer[18], p_data, i_payload );
578 out->i_buffer = 18 + i_payload;
581 rtp_packetize_send( id, out );
593 static int rtp_packetize_xiph( sout_stream_id_t *id, block_t *in )
595 int i_max = rtp_mtu (id) - 6; /* payload max in one packet */
596 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
598 uint8_t *p_data = in->p_buffer;
599 int i_data = in->i_buffer;
601 for( int i = 0; i < i_count; i++ )
603 int i_payload = __MIN( i_max, i_data );
604 block_t *out = block_Alloc( 18 + i_payload );
606 unsigned fragtype, numpkts;
609 /* No fragmentation */
619 else if (i == i_count - 1)
624 /* Ident:24, Fragment type:2, Vorbis/Theora Data Type:2, # of pkts:4 */
625 uint32_t header = ((XIPH_IDENT & 0xffffff) << 8) |
626 (fragtype << 6) | (0 << 4) | numpkts;
628 /* rtp common header */
629 rtp_packetize_common( id, out, 0, in->i_pts);
631 SetDWBE( out->p_buffer + 12, header);
632 SetWBE( out->p_buffer + 16, i_payload);
633 memcpy( &out->p_buffer[18], p_data, i_payload );
635 out->i_buffer = 18 + i_payload;
636 out->i_dts = in->i_dts + i * in->i_length / i_count;
637 out->i_length = in->i_length / i_count;
639 rtp_packetize_send( id, out );
648 static int rtp_packetize_mpa( sout_stream_id_t *id, block_t *in )
650 int i_max = rtp_mtu (id) - 4; /* payload max in one packet */
651 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
653 uint8_t *p_data = in->p_buffer;
654 int i_data = in->i_buffer;
657 for( i = 0; i < i_count; i++ )
659 int i_payload = __MIN( i_max, i_data );
660 block_t *out = block_Alloc( 16 + i_payload );
662 /* rtp common header */
663 rtp_packetize_common( id, out, (i == i_count - 1)?1:0, in->i_pts );
665 SetWBE( out->p_buffer + 12, 0 );
666 /* fragment offset in the current frame */
667 SetWBE( out->p_buffer + 14, i * i_max );
668 memcpy( &out->p_buffer[16], p_data, i_payload );
670 out->i_buffer = 16 + i_payload;
671 out->i_dts = in->i_dts + i * in->i_length / i_count;
672 out->i_length = in->i_length / i_count;
674 rtp_packetize_send( id, out );
684 static int rtp_packetize_mpv( sout_stream_id_t *id, block_t *in )
686 int i_max = rtp_mtu (id) - 4; /* payload max in one packet */
687 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
689 uint8_t *p_data = in->p_buffer;
690 int i_data = in->i_buffer;
692 int b_sequence_start = 0;
693 int i_temporal_ref = 0;
694 int i_picture_coding_type = 0;
695 int i_fbv = 0, i_bfc = 0, i_ffv = 0, i_ffc = 0;
696 int b_start_slice = 0;
698 /* preparse this packet to get some info */
699 if( in->i_buffer > 4 )
702 int i_rest = in->i_buffer;
707 ( p[0] != 0x00 || p[1] != 0x00 || p[2] != 0x01 ) )
721 /* sequence start code */
722 b_sequence_start = 1;
724 else if( *p == 0x00 && i_rest >= 4 )
727 i_temporal_ref = ( p[1] << 2) |((p[2]>>6)&0x03);
728 i_picture_coding_type = (p[2] >> 3)&0x07;
730 if( i_rest >= 4 && ( i_picture_coding_type == 2 ||
731 i_picture_coding_type == 3 ) )
733 i_ffv = (p[3] >> 2)&0x01;
734 i_ffc = ((p[3]&0x03) << 1)|((p[4]>>7)&0x01);
735 if( i_rest > 4 && i_picture_coding_type == 3 )
737 i_fbv = (p[4]>>6)&0x01;
738 i_bfc = (p[4]>>3)&0x07;
742 else if( *p <= 0xaf )
749 for( i = 0; i < i_count; i++ )
751 int i_payload = __MIN( i_max, i_data );
752 block_t *out = block_Alloc( 16 + i_payload );
753 /* 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 */
754 uint32_t h = ( i_temporal_ref << 16 )|
755 ( b_sequence_start << 13 )|
756 ( b_start_slice << 12 )|
757 ( i == i_count - 1 ? 1 << 11 : 0 )|
758 ( i_picture_coding_type << 8 )|
759 ( i_fbv << 7 )|( i_bfc << 4 )|( i_ffv << 3 )|i_ffc;
761 /* rtp common header */
762 rtp_packetize_common( id, out, (i == i_count - 1)?1:0,
763 in->i_pts > VLC_TS_INVALID ? in->i_pts : in->i_dts );
765 SetDWBE( out->p_buffer + 12, h );
767 memcpy( &out->p_buffer[16], p_data, i_payload );
769 out->i_buffer = 16 + i_payload;
770 out->i_dts = in->i_dts + i * in->i_length / i_count;
771 out->i_length = in->i_length / i_count;
773 rtp_packetize_send( id, out );
782 static int rtp_packetize_ac3( sout_stream_id_t *id, block_t *in )
784 int i_max = rtp_mtu (id) - 2; /* payload max in one packet */
785 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
787 uint8_t *p_data = in->p_buffer;
788 int i_data = in->i_buffer;
791 for( i = 0; i < i_count; i++ )
793 int i_payload = __MIN( i_max, i_data );
794 block_t *out = block_Alloc( 14 + i_payload );
796 /* rtp common header */
797 rtp_packetize_common( id, out, (i == i_count - 1)?1:0, in->i_pts );
799 out->p_buffer[12] = 1;
801 out->p_buffer[13] = 0x00;
803 memcpy( &out->p_buffer[14], p_data, i_payload );
805 out->i_buffer = 14 + i_payload;
806 out->i_dts = in->i_dts + i * in->i_length / i_count;
807 out->i_length = in->i_length / i_count;
809 rtp_packetize_send( id, out );
818 static int rtp_packetize_split( sout_stream_id_t *id, block_t *in )
820 int i_max = rtp_mtu (id); /* payload max in one packet */
821 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
823 uint8_t *p_data = in->p_buffer;
824 int i_data = in->i_buffer;
827 for( i = 0; i < i_count; i++ )
829 int i_payload = __MIN( i_max, i_data );
830 block_t *out = block_Alloc( 12 + i_payload );
832 /* rtp common header */
833 rtp_packetize_common( id, out, (i == i_count - 1),
834 (in->i_pts > VLC_TS_INVALID ? in->i_pts : in->i_dts) );
835 memcpy( &out->p_buffer[12], p_data, i_payload );
837 out->i_buffer = 12 + i_payload;
838 out->i_dts = in->i_dts + i * in->i_length / i_count;
839 out->i_length = in->i_length / i_count;
841 rtp_packetize_send( id, out );
850 /* split and convert from little endian to network byte order */
851 static int rtp_packetize_swab( sout_stream_id_t *id, block_t *in )
853 int i_max = rtp_mtu (id); /* payload max in one packet */
854 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
856 uint8_t *p_data = in->p_buffer;
857 int i_data = in->i_buffer;
860 for( i = 0; i < i_count; i++ )
862 int i_payload = __MIN( i_max, i_data );
863 block_t *out = block_Alloc( 12 + i_payload );
865 /* rtp common header */
866 rtp_packetize_common( id, out, (i == i_count - 1),
867 (in->i_pts > VLC_TS_INVALID ? in->i_pts : in->i_dts) );
868 swab( p_data, out->p_buffer + 12, i_payload );
870 out->i_buffer = 12 + i_payload;
871 out->i_dts = in->i_dts + i * in->i_length / i_count;
872 out->i_length = in->i_length / i_count;
874 rtp_packetize_send( id, out );
884 static int rtp_packetize_mp4a_latm( sout_stream_id_t *id, block_t *in )
886 int i_max = rtp_mtu (id) - 2; /* payload max in one packet */
887 int latmhdrsize = in->i_buffer / 0xff + 1;
888 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
890 uint8_t *p_data = in->p_buffer, *p_header = NULL;
891 int i_data = in->i_buffer;
894 for( i = 0; i < i_count; i++ )
896 int i_payload = __MIN( i_max, i_data );
901 out = block_Alloc( 12 + latmhdrsize + i_payload );
903 /* rtp common header */
904 rtp_packetize_common( id, out, ((i == i_count - 1) ? 1 : 0),
905 (in->i_pts > VLC_TS_INVALID ? in->i_pts : in->i_dts) );
909 int tmp = in->i_buffer;
911 p_header=out->p_buffer+12;
921 memcpy( &out->p_buffer[12+latmhdrsize], p_data, i_payload );
923 out->i_buffer = 12 + latmhdrsize + i_payload;
924 out->i_dts = in->i_dts + i * in->i_length / i_count;
925 out->i_length = in->i_length / i_count;
927 rtp_packetize_send( id, out );
936 static int rtp_packetize_mp4a( sout_stream_id_t *id, block_t *in )
938 int i_max = rtp_mtu (id) - 4; /* payload max in one packet */
939 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
941 uint8_t *p_data = in->p_buffer;
942 int i_data = in->i_buffer;
945 for( i = 0; i < i_count; i++ )
947 int i_payload = __MIN( i_max, i_data );
948 block_t *out = block_Alloc( 16 + i_payload );
950 /* rtp common header */
951 rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
952 (in->i_pts > VLC_TS_INVALID ? in->i_pts : in->i_dts) );
954 /* AU headers length (bits) */
955 out->p_buffer[12] = 0;
956 out->p_buffer[13] = 2*8;
957 /* for each AU length 13 bits + idx 3bits, */
958 SetWBE( out->p_buffer + 14, (in->i_buffer << 3) | 0 );
960 memcpy( &out->p_buffer[16], p_data, i_payload );
962 out->i_buffer = 16 + i_payload;
963 out->i_dts = in->i_dts + i * in->i_length / i_count;
964 out->i_length = in->i_length / i_count;
966 rtp_packetize_send( id, out );
977 #define RTP_H263_HEADER_SIZE (2) // plen = 0
978 #define RTP_H263_PAYLOAD_START (14) // plen = 0
979 static int rtp_packetize_h263( sout_stream_id_t *id, block_t *in )
981 uint8_t *p_data = in->p_buffer;
982 int i_data = in->i_buffer;
984 int i_max = rtp_mtu (id) - RTP_H263_HEADER_SIZE; /* payload max in one packet */
987 int b_v_bit = 0; // no pesky error resilience
988 int i_plen = 0; // normally plen=0 for PSC packet
989 int i_pebit = 0; // because plen=0
996 if( p_data[0] || p_data[1] )
1000 /* remove 2 leading 0 bytes */
1003 i_count = ( i_data + i_max - 1 ) / i_max;
1005 for( i = 0; i < i_count; i++ )
1007 int i_payload = __MIN( i_max, i_data );
1008 block_t *out = block_Alloc( RTP_H263_PAYLOAD_START + i_payload );
1009 b_p_bit = (i == 0) ? 1 : 0;
1010 h = ( b_p_bit << 10 )|
1015 /* rtp common header */
1016 //b_m_bit = 1; // always contains end of frame
1017 rtp_packetize_common( id, out, (i == i_count - 1)?1:0,
1018 in->i_pts > VLC_TS_INVALID ? in->i_pts : in->i_dts );
1021 SetWBE( out->p_buffer + 12, h );
1022 memcpy( &out->p_buffer[RTP_H263_PAYLOAD_START], p_data, i_payload );
1024 out->i_buffer = RTP_H263_PAYLOAD_START + i_payload;
1025 out->i_dts = in->i_dts + i * in->i_length / i_count;
1026 out->i_length = in->i_length / i_count;
1028 rtp_packetize_send( id, out );
1030 p_data += i_payload;
1031 i_data -= i_payload;
1039 rtp_packetize_h264_nal( sout_stream_id_t *id,
1040 const uint8_t *p_data, int i_data, int64_t i_pts,
1041 int64_t i_dts, bool b_last, int64_t i_length )
1043 const int i_max = rtp_mtu (id); /* payload max in one packet */
1050 i_nal_hdr = p_data[3];
1051 i_nal_type = i_nal_hdr&0x1f;
1053 /* Skip start code */
1058 if( i_data <= i_max )
1060 /* Single NAL unit packet */
1061 block_t *out = block_Alloc( 12 + i_data );
1063 out->i_length = i_length;
1066 rtp_packetize_common( id, out, b_last, i_pts );
1067 out->i_buffer = 12 + i_data;
1069 memcpy( &out->p_buffer[12], p_data, i_data );
1071 rtp_packetize_send( id, out );
1075 /* FU-A Fragmentation Unit without interleaving */
1076 const int i_count = ( i_data-1 + i_max-2 - 1 ) / (i_max-2);
1082 for( i = 0; i < i_count; i++ )
1084 const int i_payload = __MIN( i_data, i_max-2 );
1085 block_t *out = block_Alloc( 12 + 2 + i_payload );
1086 out->i_dts = i_dts + i * i_length / i_count;
1087 out->i_length = i_length / i_count;
1090 rtp_packetize_common( id, out, (b_last && i_payload == i_data),
1092 out->i_buffer = 14 + i_payload;
1095 out->p_buffer[12] = 0x00 | (i_nal_hdr & 0x60) | 28;
1097 out->p_buffer[13] = ( i == 0 ? 0x80 : 0x00 ) | ( (i == i_count-1) ? 0x40 : 0x00 ) | i_nal_type;
1098 memcpy( &out->p_buffer[14], p_data, i_payload );
1100 rtp_packetize_send( id, out );
1102 i_data -= i_payload;
1103 p_data += i_payload;
1109 static int rtp_packetize_h264( sout_stream_id_t *id, block_t *in )
1111 const uint8_t *p_buffer = in->p_buffer;
1112 int i_buffer = in->i_buffer;
1114 while( i_buffer > 4 && ( p_buffer[0] != 0 || p_buffer[1] != 0 || p_buffer[2] != 1 ) )
1120 /* Split nal units */
1121 while( i_buffer > 4 )
1124 int i_size = i_buffer;
1125 int i_skip = i_buffer;
1127 /* search nal end */
1128 for( i_offset = 4; i_offset+2 < i_buffer ; i_offset++)
1130 if( p_buffer[i_offset] == 0 && p_buffer[i_offset+1] == 0 && p_buffer[i_offset+2] == 1 )
1132 /* we found another startcode */
1133 i_size = i_offset - ( p_buffer[i_offset-1] == 0 ? 1 : 0);
1138 /* TODO add STAP-A to remove a lot of overhead with small slice/sei/... */
1139 rtp_packetize_h264_nal( id, p_buffer, i_size,
1140 (in->i_pts > VLC_TS_INVALID ? in->i_pts : in->i_dts), in->i_dts,
1141 (i_size >= i_buffer), in->i_length * i_size / in->i_buffer );
1149 static int rtp_packetize_amr( sout_stream_id_t *id, block_t *in )
1151 int i_max = rtp_mtu (id) - 2; /* payload max in one packet */
1152 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1154 uint8_t *p_data = in->p_buffer;
1155 int i_data = in->i_buffer;
1158 /* Only supports octet-aligned mode */
1159 for( i = 0; i < i_count; i++ )
1161 int i_payload = __MIN( i_max, i_data );
1162 block_t *out = block_Alloc( 14 + i_payload );
1164 /* rtp common header */
1165 rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
1166 (in->i_pts > VLC_TS_INVALID ? in->i_pts : in->i_dts) );
1167 /* Payload header */
1168 out->p_buffer[12] = 0xF0; /* CMR */
1169 out->p_buffer[13] = p_data[0]&0x7C; /* ToC */ /* FIXME: frame type */
1171 /* FIXME: are we fed multiple frames ? */
1172 memcpy( &out->p_buffer[14], p_data+1, i_payload-1 );
1174 out->i_buffer = 14 + i_payload-1;
1175 out->i_dts = in->i_dts + i * in->i_length / i_count;
1176 out->i_length = in->i_length / i_count;
1178 rtp_packetize_send( id, out );
1180 p_data += i_payload;
1181 i_data -= i_payload;
1187 static int rtp_packetize_t140( sout_stream_id_t *id, block_t *in )
1189 const size_t i_max = rtp_mtu (id);
1190 const uint8_t *p_data = in->p_buffer;
1191 size_t i_data = in->i_buffer;
1193 for( unsigned i_packet = 0; i_data > 0; i_packet++ )
1195 size_t i_payload = i_data;
1197 /* Make sure we stop on an UTF-8 character boundary
1198 * (assuming the input is valid UTF-8) */
1199 if( i_data > i_max )
1203 while( ( p_data[i_payload] & 0xC0 ) == 0x80 )
1205 if( i_payload == 0 )
1206 return VLC_SUCCESS; /* fishy input! */
1212 block_t *out = block_Alloc( 12 + i_payload );
1216 rtp_packetize_common( id, out, 0, in->i_pts + i_packet );
1217 memcpy( out->p_buffer + 12, p_data, i_payload );
1219 out->i_buffer = 12 + i_payload;
1220 out->i_dts = in->i_pts;
1223 rtp_packetize_send( id, out );
1225 p_data += i_payload;
1226 i_data -= i_payload;
1233 static int rtp_packetize_spx( sout_stream_id_t *id, block_t *in )
1235 uint8_t *p_buffer = in->p_buffer;
1236 int i_data_size, i_payload_size, i_payload_padding;
1237 i_data_size = i_payload_size = in->i_buffer;
1238 i_payload_padding = 0;
1241 if ( in->i_buffer > rtp_mtu (id) )
1245 RFC for Speex in RTP says that each packet must end on an octet
1246 boundary. So, we check to see if the number of bytes % 4 is zero.
1247 If not, we have to add some padding.
1249 This MAY be overkill since packetization is handled elsewhere and
1250 appears to ensure the octet boundary. However, better safe than
1253 if ( i_payload_size % 4 )
1255 i_payload_padding = 4 - ( i_payload_size % 4 );
1256 i_payload_size += i_payload_padding;
1260 Allocate a new RTP p_output block of the appropriate size.
1261 Allow for 12 extra bytes of RTP header.
1263 p_out = block_Alloc( 12 + i_payload_size );
1265 if ( i_payload_padding )
1268 The padding is required to be a zero followed by all 1s.
1270 char c_first_pad, c_remaining_pad;
1272 c_remaining_pad = 0xFF;
1275 Allow for 12 bytes before the i_data_size because
1276 of the expected RTP header added during
1277 rtp_packetize_common.
1279 p_out->p_buffer[12 + i_data_size] = c_first_pad;
1280 switch (i_payload_padding)
1283 p_out->p_buffer[12 + i_data_size + 1] = c_remaining_pad;
1286 p_out->p_buffer[12 + i_data_size + 1] = c_remaining_pad;
1287 p_out->p_buffer[12 + i_data_size + 2] = c_remaining_pad;
1292 /* Add the RTP header to our p_output buffer. */
1293 rtp_packetize_common( id, p_out, 0,
1294 (in->i_pts > VLC_TS_INVALID ? in->i_pts : in->i_dts) );
1295 /* Copy the Speex payload to the p_output buffer */
1296 memcpy( &p_out->p_buffer[12], p_buffer, i_data_size );
1298 p_out->i_buffer = 12 + i_payload_size;
1299 p_out->i_dts = in->i_dts;
1300 p_out->i_length = in->i_length;
1302 /* Queue the buffer for actual transmission. */
1303 rtp_packetize_send( id, p_out );
1307 static int rtp_packetize_g726( sout_stream_id_t *id, block_t *in, int i_pad )
1309 int i_max = (rtp_mtu( id )- 12 + i_pad - 1) & ~i_pad;
1310 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1312 uint8_t *p_data = in->p_buffer;
1313 int i_data = in->i_buffer;
1318 int i_payload = __MIN( i_max, i_data );
1319 block_t *out = block_Alloc( 12 + i_payload );
1321 /* rtp common header */
1322 rtp_packetize_common( id, out, 0,
1323 (in->i_pts > VLC_TS_INVALID ? in->i_pts : in->i_dts) );
1325 memcpy( &out->p_buffer[12], p_data, i_payload );
1327 out->i_buffer = 12 + i_payload;
1328 out->i_dts = in->i_dts + i_packet++ * in->i_length / i_count;
1329 out->i_length = in->i_length / i_count;
1331 rtp_packetize_send( id, out );
1333 p_data += i_payload;
1334 i_data -= i_payload;
1339 static int rtp_packetize_g726_16( sout_stream_id_t *id, block_t *in )
1341 return rtp_packetize_g726( id, in, 4 );
1344 static int rtp_packetize_g726_24( sout_stream_id_t *id, block_t *in )
1346 return rtp_packetize_g726( id, in, 8 );
1349 static int rtp_packetize_g726_32( sout_stream_id_t *id, block_t *in )
1351 return rtp_packetize_g726( id, in, 2 );
1354 static int rtp_packetize_g726_40( sout_stream_id_t *id, block_t *in )
1356 return rtp_packetize_g726( id, in, 8 );