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 int val = xiph_SplitHeaders(packet_size, packet, &packet_count,
70 if (val != VLC_SUCCESS)
78 if (theora_pixel_fmt != NULL)
80 if (packet_size[0] < 42)
85 *theora_pixel_fmt = (((uint8_t *)packet[0])[41] >> 3) & 0x03;
88 unsigned length_size[2] = { 0, 0 };
89 for (int i = 0; i < 2; i++)
91 unsigned size = packet_size[i];
99 *i_buffer = room + 1 + length_size[0] + length_size[1]
100 + packet_size[0] + packet_size[1] + packet_size[2];
101 *p_buffer = malloc(*i_buffer);
102 if (*p_buffer == NULL)
108 uint8_t *p = *p_buffer + room;
109 /* Number of headers */
112 for (int i = 0; i < 2; i++)
114 unsigned size = length_size[i];
117 *p = (packet_size[i] >> (7 * (size - 1))) & 0x7f;
123 for (int i = 0; i < 3; i++)
125 memcpy(p, packet[i], packet_size[i]);
131 for (unsigned i = 0; i < packet_count; i++)
137 static char *rtp_xiph_b64_oob_config(void *p_extra, size_t i_extra,
138 uint8_t *theora_pixel_fmt)
142 if (rtp_xiph_pack_headers(9, p_extra, i_extra, &p_buffer, &i_buffer,
143 theora_pixel_fmt) != VLC_SUCCESS)
146 /* Number of packed headers */
147 SetDWBE(p_buffer, 1);
149 uint32_t ident = XIPH_IDENT;
150 SetWBE(p_buffer + 4, ident >> 8);
151 p_buffer[6] = ident & 0xff;
153 SetWBE(p_buffer + 7, i_buffer);
155 char *config = vlc_b64_encode_binary(p_buffer, i_buffer);
160 static void sprintf_hexa( char *s, uint8_t *p_data, int i_data )
162 static const char hex[16] = "0123456789abcdef";
164 for( int i = 0; i < i_data; i++ )
166 s[2*i+0] = hex[(p_data[i]>>4)&0xf];
167 s[2*i+1] = hex[(p_data[i] )&0xf];
172 /* TODO: make this into something more clever than a big switch? */
173 int rtp_get_fmt( vlc_object_t *obj, es_format_t *p_fmt, const char *mux,
174 rtp_format_t *rtp_fmt )
176 assert( p_fmt != NULL || mux != NULL );
178 /* Dynamic payload type. Payload types are scoped to the RTP
179 * session, and we put each ES in its own session, so no risk of
181 rtp_fmt->payload_type = 96;
182 rtp_fmt->cat = mux != NULL ? VIDEO_ES : p_fmt->i_cat;
183 if( rtp_fmt->cat == AUDIO_ES )
185 rtp_fmt->clock_rate = p_fmt->audio.i_rate;
186 rtp_fmt->channels = p_fmt->audio.i_channels;
189 rtp_fmt->clock_rate = 90000; /* most common case for video */
190 /* Stream bitrate in kbps */
191 rtp_fmt->bitrate = p_fmt != NULL ? p_fmt->i_bitrate/1000 : 0;
192 rtp_fmt->fmtp = NULL;
196 if( strncmp( mux, "ts", 2 ) == 0 )
198 rtp_fmt->payload_type = 33;
199 rtp_fmt->ptname = "MP2T";
202 rtp_fmt->ptname = "MP2P";
206 switch( p_fmt->i_codec )
208 case VLC_CODEC_MULAW:
209 if( p_fmt->audio.i_channels == 1 && p_fmt->audio.i_rate == 8000 )
210 rtp_fmt->payload_type = 0;
211 rtp_fmt->ptname = "PCMU";
212 rtp_fmt->pf_packetize = rtp_packetize_split;
215 if( p_fmt->audio.i_channels == 1 && p_fmt->audio.i_rate == 8000 )
216 rtp_fmt->payload_type = 8;
217 rtp_fmt->ptname = "PCMA";
218 rtp_fmt->pf_packetize = rtp_packetize_split;
222 if( p_fmt->audio.i_channels == 1 && p_fmt->audio.i_rate == 44100 )
224 rtp_fmt->payload_type = 11;
226 else if( p_fmt->audio.i_channels == 2 &&
227 p_fmt->audio.i_rate == 44100 )
229 rtp_fmt->payload_type = 10;
231 rtp_fmt->ptname = "L16";
232 if( p_fmt->i_codec == VLC_CODEC_S16B )
233 rtp_fmt->pf_packetize = rtp_packetize_split;
235 rtp_fmt->pf_packetize = rtp_packetize_swab;
238 rtp_fmt->ptname = "L8";
239 rtp_fmt->pf_packetize = rtp_packetize_split;
242 rtp_fmt->payload_type = 14;
243 rtp_fmt->ptname = "MPA";
244 rtp_fmt->clock_rate = 90000; /* not 44100 */
245 rtp_fmt->pf_packetize = rtp_packetize_mpa;
248 rtp_fmt->payload_type = 32;
249 rtp_fmt->ptname = "MPV";
250 rtp_fmt->pf_packetize = rtp_packetize_mpv;
252 case VLC_CODEC_ADPCM_G726:
253 switch( p_fmt->i_bitrate / 1000 )
256 rtp_fmt->ptname = "G726-16";
257 rtp_fmt->pf_packetize = rtp_packetize_g726_16;
260 rtp_fmt->ptname = "G726-24";
261 rtp_fmt->pf_packetize = rtp_packetize_g726_24;
264 rtp_fmt->ptname = "G726-32";
265 rtp_fmt->pf_packetize = rtp_packetize_g726_32;
268 rtp_fmt->ptname = "G726-40";
269 rtp_fmt->pf_packetize = rtp_packetize_g726_40;
272 msg_Err( obj, "cannot add this stream (unsupported "
273 "G.726 bit rate: %u)", p_fmt->i_bitrate );
278 rtp_fmt->ptname = "ac3";
279 rtp_fmt->pf_packetize = rtp_packetize_ac3;
282 rtp_fmt->ptname = "H263-1998";
283 rtp_fmt->pf_packetize = rtp_packetize_h263;
286 rtp_fmt->ptname = "H264";
287 rtp_fmt->pf_packetize = rtp_packetize_h264;
288 rtp_fmt->fmtp = NULL;
290 if( p_fmt->i_extra > 0 )
292 uint8_t *p_buffer = p_fmt->p_extra;
293 int i_buffer = p_fmt->i_extra;
294 char *p_64_sps = NULL;
295 char *p_64_pps = NULL;
298 while( i_buffer > 4 )
303 while( p_buffer[0] != 0 || p_buffer[1] != 0 ||
308 if( i_buffer == 0 ) break;
311 if( i_buffer < 4 || memcmp(p_buffer, "\x00\x00\x01", 3 ) )
313 msg_Dbg( obj, "No startcode found..");
319 const int i_nal_type = p_buffer[0]&0x1f;
321 msg_Dbg( obj, "we found a startcode for NAL with TYPE:%d", i_nal_type );
324 for( i_offset = 0; i_offset+2 < i_buffer ; i_offset++)
326 if( !memcmp(p_buffer + i_offset, "\x00\x00\x01", 3 ) )
328 /* we found another startcode */
329 while( i_offset > 0 && 0 == p_buffer[ i_offset - 1 ] )
338 msg_Dbg( obj, "No-info found in nal ");
342 if( i_nal_type == 7 )
345 p_64_sps = vlc_b64_encode_binary( p_buffer, i_size );
346 /* XXX: nothing ensures that i_size >= 4 ?? */
347 sprintf_hexa( hexa, &p_buffer[1], 3 );
349 else if( i_nal_type == 8 )
352 p_64_pps = vlc_b64_encode_binary( p_buffer, i_size );
358 if( p_64_sps && p_64_pps &&
359 ( asprintf( &rtp_fmt->fmtp,
360 "packetization-mode=1;profile-level-id=%s;"
361 "sprop-parameter-sets=%s,%s;", hexa, p_64_sps,
363 rtp_fmt->fmtp = NULL;
367 if( rtp_fmt->fmtp == NULL )
368 rtp_fmt->fmtp = strdup( "packetization-mode=1" );
373 rtp_fmt->ptname = "MP4V-ES";
374 rtp_fmt->pf_packetize = rtp_packetize_split;
375 if( p_fmt->i_extra > 0 )
377 char hexa[2*p_fmt->i_extra +1];
378 sprintf_hexa( hexa, p_fmt->p_extra, p_fmt->i_extra );
379 if( asprintf( &rtp_fmt->fmtp,
380 "profile-level-id=3; config=%s;", hexa ) == -1 )
381 rtp_fmt->fmtp = NULL;
387 if( ! var_InheritBool( obj, "sout-rtp-mp4a-latm" ) )
389 char hexa[2*p_fmt->i_extra +1];
391 rtp_fmt->ptname = "mpeg4-generic";
392 rtp_fmt->pf_packetize = rtp_packetize_mp4a;
393 sprintf_hexa( hexa, p_fmt->p_extra, p_fmt->i_extra );
394 if( asprintf( &rtp_fmt->fmtp,
395 "streamtype=5; profile-level-id=15; "
396 "mode=AAC-hbr; config=%s; SizeLength=13; "
397 "IndexLength=3; IndexDeltaLength=3; Profile=1;",
399 rtp_fmt->fmtp = NULL;
405 unsigned char config[6];
406 unsigned int aacsrates[15] = {
407 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
408 16000, 12000, 11025, 8000, 7350, 0, 0 };
410 for( i = 0; i < 15; i++ )
411 if( p_fmt->audio.i_rate == aacsrates[i] )
417 config[3]=p_fmt->audio.i_channels<<4;
421 rtp_fmt->ptname = "MP4A-LATM";
422 rtp_fmt->pf_packetize = rtp_packetize_mp4a_latm;
423 sprintf_hexa( hexa, config, 6 );
424 if( asprintf( &rtp_fmt->fmtp, "profile-level-id=15; "
425 "object=2; cpresent=0; config=%s", hexa ) == -1 )
426 rtp_fmt->fmtp = NULL;
430 case VLC_CODEC_AMR_NB:
431 rtp_fmt->ptname = "AMR";
432 rtp_fmt->fmtp = strdup( "octet-align=1" );
433 rtp_fmt->pf_packetize = rtp_packetize_amr;
435 case VLC_CODEC_AMR_WB:
436 rtp_fmt->ptname = "AMR-WB";
437 rtp_fmt->fmtp = strdup( "octet-align=1" );
438 rtp_fmt->pf_packetize = rtp_packetize_amr;
440 case VLC_CODEC_SPEEX:
441 rtp_fmt->ptname = "SPEEX";
442 rtp_fmt->pf_packetize = rtp_packetize_spx;
444 case VLC_CODEC_VORBIS:
445 rtp_fmt->ptname = "vorbis";
446 rtp_fmt->pf_packetize = rtp_packetize_xiph;
447 if( p_fmt->i_extra > 0 )
449 rtp_fmt->fmtp = NULL;
450 char *config = rtp_xiph_b64_oob_config(p_fmt->p_extra,
451 p_fmt->i_extra, NULL);
454 if( asprintf( &rtp_fmt->fmtp,
455 "configuration=%s;", config ) == -1 )
456 rtp_fmt->fmtp = NULL;
460 case VLC_CODEC_THEORA:
461 rtp_fmt->ptname = "theora";
462 rtp_fmt->pf_packetize = rtp_packetize_xiph;
463 if( p_fmt->i_extra > 0 )
465 rtp_fmt->fmtp = NULL;
466 uint8_t pixel_fmt, c1, c2;
467 char *config = rtp_xiph_b64_oob_config(p_fmt->p_extra,
495 if( asprintf( &rtp_fmt->fmtp,
496 "sampling=YCbCr-4:%d:%d; width=%d; height=%d; "
497 "delivery-method=inline; configuration=%s; "
498 "delivery-method=in_band;", c1, c2,
499 p_fmt->video.i_width, p_fmt->video.i_height,
501 rtp_fmt->fmtp = NULL;
505 case VLC_CODEC_ITU_T140:
506 rtp_fmt->ptname = "t140" ;
507 rtp_fmt->clock_rate = 1000;
508 rtp_fmt->pf_packetize = rtp_packetize_t140;
512 msg_Err( obj, "cannot add this stream (unsupported "
513 "codec: %4.4s)", (char*)&p_fmt->i_codec );
522 rtp_packetize_h264_nal( sout_stream_id_t *id,
523 const uint8_t *p_data, int i_data, int64_t i_pts,
524 int64_t i_dts, bool b_last, int64_t i_length );
526 int rtp_packetize_xiph_config( sout_stream_id_t *id, const char *fmtp,
532 /* extract base64 configuration from fmtp */
533 char *start = strstr(fmtp, "configuration=");
534 assert(start != NULL);
535 start += sizeof("configuration=") - 1;
536 char *end = strchr(start, ';');
538 size_t len = end - start;
540 memcpy(b64, start, len);
543 int i_max = rtp_mtu (id) - 6; /* payload max in one packet */
545 uint8_t *p_orig, *p_data;
548 i_data = vlc_b64_decode_binary(&p_orig, b64);
555 int i_count = ( i_data + i_max - 1 ) / i_max;
557 for( int i = 0; i < i_count; i++ )
559 int i_payload = __MIN( i_max, i_data );
560 block_t *out = block_Alloc( 18 + i_payload );
562 unsigned fragtype, numpkts;
573 else if (i == i_count - 1)
578 /* Ident:24, Fragment type:2, Vorbis/Theora Data Type:2, # of pkts:4 */
579 uint32_t header = ((XIPH_IDENT & 0xffffff) << 8) |
580 (fragtype << 6) | (1 << 4) | numpkts;
582 /* rtp common header */
583 rtp_packetize_common( id, out, 0, i_pts );
585 SetDWBE( out->p_buffer + 12, header);
586 SetWBE( out->p_buffer + 16, i_payload);
587 memcpy( &out->p_buffer[18], p_data, i_payload );
589 out->i_buffer = 18 + i_payload;
592 rtp_packetize_send( id, out );
604 static int rtp_packetize_xiph( sout_stream_id_t *id, block_t *in )
606 int i_max = rtp_mtu (id) - 6; /* payload max in one packet */
607 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
609 uint8_t *p_data = in->p_buffer;
610 int i_data = in->i_buffer;
612 for( int i = 0; i < i_count; i++ )
614 int i_payload = __MIN( i_max, i_data );
615 block_t *out = block_Alloc( 18 + i_payload );
617 unsigned fragtype, numpkts;
620 /* No fragmentation */
630 else if (i == i_count - 1)
635 /* Ident:24, Fragment type:2, Vorbis/Theora Data Type:2, # of pkts:4 */
636 uint32_t header = ((XIPH_IDENT & 0xffffff) << 8) |
637 (fragtype << 6) | (0 << 4) | numpkts;
639 /* rtp common header */
640 rtp_packetize_common( id, out, 0, in->i_pts);
642 SetDWBE( out->p_buffer + 12, header);
643 SetWBE( out->p_buffer + 16, i_payload);
644 memcpy( &out->p_buffer[18], p_data, i_payload );
646 out->i_buffer = 18 + i_payload;
647 out->i_dts = in->i_dts + i * in->i_length / i_count;
648 out->i_length = in->i_length / i_count;
650 rtp_packetize_send( id, out );
659 static int rtp_packetize_mpa( sout_stream_id_t *id, block_t *in )
661 int i_max = rtp_mtu (id) - 4; /* payload max in one packet */
662 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
664 uint8_t *p_data = in->p_buffer;
665 int i_data = in->i_buffer;
668 for( i = 0; i < i_count; i++ )
670 int i_payload = __MIN( i_max, i_data );
671 block_t *out = block_Alloc( 16 + i_payload );
673 /* rtp common header */
674 rtp_packetize_common( id, out, (i == i_count - 1)?1:0, in->i_pts );
676 SetWBE( out->p_buffer + 12, 0 );
677 /* fragment offset in the current frame */
678 SetWBE( out->p_buffer + 14, i * i_max );
679 memcpy( &out->p_buffer[16], p_data, i_payload );
681 out->i_buffer = 16 + i_payload;
682 out->i_dts = in->i_dts + i * in->i_length / i_count;
683 out->i_length = in->i_length / i_count;
685 rtp_packetize_send( id, out );
695 static int rtp_packetize_mpv( sout_stream_id_t *id, block_t *in )
697 int i_max = rtp_mtu (id) - 4; /* payload max in one packet */
698 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
700 uint8_t *p_data = in->p_buffer;
701 int i_data = in->i_buffer;
703 int b_sequence_start = 0;
704 int i_temporal_ref = 0;
705 int i_picture_coding_type = 0;
706 int i_fbv = 0, i_bfc = 0, i_ffv = 0, i_ffc = 0;
707 int b_start_slice = 0;
709 /* preparse this packet to get some info */
710 if( in->i_buffer > 4 )
713 int i_rest = in->i_buffer;
718 ( p[0] != 0x00 || p[1] != 0x00 || p[2] != 0x01 ) )
732 /* sequence start code */
733 b_sequence_start = 1;
735 else if( *p == 0x00 && i_rest >= 4 )
738 i_temporal_ref = ( p[1] << 2) |((p[2]>>6)&0x03);
739 i_picture_coding_type = (p[2] >> 3)&0x07;
741 if( i_rest >= 4 && ( i_picture_coding_type == 2 ||
742 i_picture_coding_type == 3 ) )
744 i_ffv = (p[3] >> 2)&0x01;
745 i_ffc = ((p[3]&0x03) << 1)|((p[4]>>7)&0x01);
746 if( i_rest > 4 && i_picture_coding_type == 3 )
748 i_fbv = (p[4]>>6)&0x01;
749 i_bfc = (p[4]>>3)&0x07;
753 else if( *p <= 0xaf )
760 for( i = 0; i < i_count; i++ )
762 int i_payload = __MIN( i_max, i_data );
763 block_t *out = block_Alloc( 16 + i_payload );
764 /* 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 */
765 uint32_t h = ( i_temporal_ref << 16 )|
766 ( b_sequence_start << 13 )|
767 ( b_start_slice << 12 )|
768 ( i == i_count - 1 ? 1 << 11 : 0 )|
769 ( i_picture_coding_type << 8 )|
770 ( i_fbv << 7 )|( i_bfc << 4 )|( i_ffv << 3 )|i_ffc;
772 /* rtp common header */
773 rtp_packetize_common( id, out, (i == i_count - 1)?1:0,
774 in->i_pts > VLC_TS_INVALID ? in->i_pts : in->i_dts );
776 SetDWBE( out->p_buffer + 12, h );
778 memcpy( &out->p_buffer[16], p_data, i_payload );
780 out->i_buffer = 16 + i_payload;
781 out->i_dts = in->i_dts + i * in->i_length / i_count;
782 out->i_length = in->i_length / i_count;
784 rtp_packetize_send( id, out );
793 static int rtp_packetize_ac3( sout_stream_id_t *id, block_t *in )
795 int i_max = rtp_mtu (id) - 2; /* payload max in one packet */
796 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
798 uint8_t *p_data = in->p_buffer;
799 int i_data = in->i_buffer;
802 for( i = 0; i < i_count; i++ )
804 int i_payload = __MIN( i_max, i_data );
805 block_t *out = block_Alloc( 14 + i_payload );
807 /* rtp common header */
808 rtp_packetize_common( id, out, (i == i_count - 1)?1:0, in->i_pts );
810 out->p_buffer[12] = 1;
812 out->p_buffer[13] = 0x00;
814 memcpy( &out->p_buffer[14], p_data, i_payload );
816 out->i_buffer = 14 + i_payload;
817 out->i_dts = in->i_dts + i * in->i_length / i_count;
818 out->i_length = in->i_length / i_count;
820 rtp_packetize_send( id, out );
829 static int rtp_packetize_split( sout_stream_id_t *id, block_t *in )
831 int i_max = rtp_mtu (id); /* payload max in one packet */
832 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
834 uint8_t *p_data = in->p_buffer;
835 int i_data = in->i_buffer;
838 for( i = 0; i < i_count; i++ )
840 int i_payload = __MIN( i_max, i_data );
841 block_t *out = block_Alloc( 12 + i_payload );
843 /* rtp common header */
844 rtp_packetize_common( id, out, (i == i_count - 1),
845 (in->i_pts > VLC_TS_INVALID ? in->i_pts : in->i_dts) );
846 memcpy( &out->p_buffer[12], p_data, i_payload );
848 out->i_buffer = 12 + i_payload;
849 out->i_dts = in->i_dts + i * in->i_length / i_count;
850 out->i_length = in->i_length / i_count;
852 rtp_packetize_send( id, out );
861 /* split and convert from little endian to network byte order */
862 static int rtp_packetize_swab( sout_stream_id_t *id, block_t *in )
864 int i_max = rtp_mtu (id); /* payload max in one packet */
865 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
867 uint8_t *p_data = in->p_buffer;
868 int i_data = in->i_buffer;
871 for( i = 0; i < i_count; i++ )
873 int i_payload = __MIN( i_max, i_data );
874 block_t *out = block_Alloc( 12 + i_payload );
876 /* rtp common header */
877 rtp_packetize_common( id, out, (i == i_count - 1),
878 (in->i_pts > VLC_TS_INVALID ? in->i_pts : in->i_dts) );
879 swab( p_data, out->p_buffer + 12, i_payload );
881 out->i_buffer = 12 + i_payload;
882 out->i_dts = in->i_dts + i * in->i_length / i_count;
883 out->i_length = in->i_length / i_count;
885 rtp_packetize_send( id, out );
895 static int rtp_packetize_mp4a_latm( sout_stream_id_t *id, block_t *in )
897 int i_max = rtp_mtu (id) - 2; /* payload max in one packet */
898 int latmhdrsize = in->i_buffer / 0xff + 1;
899 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
901 uint8_t *p_data = in->p_buffer, *p_header = NULL;
902 int i_data = in->i_buffer;
905 for( i = 0; i < i_count; i++ )
907 int i_payload = __MIN( i_max, i_data );
912 out = block_Alloc( 12 + latmhdrsize + i_payload );
914 /* rtp common header */
915 rtp_packetize_common( id, out, ((i == i_count - 1) ? 1 : 0),
916 (in->i_pts > VLC_TS_INVALID ? in->i_pts : in->i_dts) );
920 int tmp = in->i_buffer;
922 p_header=out->p_buffer+12;
932 memcpy( &out->p_buffer[12+latmhdrsize], p_data, i_payload );
934 out->i_buffer = 12 + latmhdrsize + i_payload;
935 out->i_dts = in->i_dts + i * in->i_length / i_count;
936 out->i_length = in->i_length / i_count;
938 rtp_packetize_send( id, out );
947 static int rtp_packetize_mp4a( sout_stream_id_t *id, block_t *in )
949 int i_max = rtp_mtu (id) - 4; /* payload max in one packet */
950 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
952 uint8_t *p_data = in->p_buffer;
953 int i_data = in->i_buffer;
956 for( i = 0; i < i_count; i++ )
958 int i_payload = __MIN( i_max, i_data );
959 block_t *out = block_Alloc( 16 + i_payload );
961 /* rtp common header */
962 rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
963 (in->i_pts > VLC_TS_INVALID ? in->i_pts : in->i_dts) );
965 /* AU headers length (bits) */
966 out->p_buffer[12] = 0;
967 out->p_buffer[13] = 2*8;
968 /* for each AU length 13 bits + idx 3bits, */
969 SetWBE( out->p_buffer + 14, (in->i_buffer << 3) | 0 );
971 memcpy( &out->p_buffer[16], p_data, i_payload );
973 out->i_buffer = 16 + i_payload;
974 out->i_dts = in->i_dts + i * in->i_length / i_count;
975 out->i_length = in->i_length / i_count;
977 rtp_packetize_send( id, out );
988 #define RTP_H263_HEADER_SIZE (2) // plen = 0
989 #define RTP_H263_PAYLOAD_START (14) // plen = 0
990 static int rtp_packetize_h263( sout_stream_id_t *id, block_t *in )
992 uint8_t *p_data = in->p_buffer;
993 int i_data = in->i_buffer;
995 int i_max = rtp_mtu (id) - RTP_H263_HEADER_SIZE; /* payload max in one packet */
998 int b_v_bit = 0; // no pesky error resilience
999 int i_plen = 0; // normally plen=0 for PSC packet
1000 int i_pebit = 0; // because plen=0
1005 return VLC_EGENERIC;
1007 if( p_data[0] || p_data[1] )
1009 return VLC_EGENERIC;
1011 /* remove 2 leading 0 bytes */
1014 i_count = ( i_data + i_max - 1 ) / i_max;
1016 for( i = 0; i < i_count; i++ )
1018 int i_payload = __MIN( i_max, i_data );
1019 block_t *out = block_Alloc( RTP_H263_PAYLOAD_START + i_payload );
1020 b_p_bit = (i == 0) ? 1 : 0;
1021 h = ( b_p_bit << 10 )|
1026 /* rtp common header */
1027 //b_m_bit = 1; // always contains end of frame
1028 rtp_packetize_common( id, out, (i == i_count - 1)?1:0,
1029 in->i_pts > VLC_TS_INVALID ? in->i_pts : in->i_dts );
1032 SetWBE( out->p_buffer + 12, h );
1033 memcpy( &out->p_buffer[RTP_H263_PAYLOAD_START], p_data, i_payload );
1035 out->i_buffer = RTP_H263_PAYLOAD_START + i_payload;
1036 out->i_dts = in->i_dts + i * in->i_length / i_count;
1037 out->i_length = in->i_length / i_count;
1039 rtp_packetize_send( id, out );
1041 p_data += i_payload;
1042 i_data -= i_payload;
1050 rtp_packetize_h264_nal( sout_stream_id_t *id,
1051 const uint8_t *p_data, int i_data, int64_t i_pts,
1052 int64_t i_dts, bool b_last, int64_t i_length )
1054 const int i_max = rtp_mtu (id); /* payload max in one packet */
1061 i_nal_hdr = p_data[3];
1062 i_nal_type = i_nal_hdr&0x1f;
1064 /* Skip start code */
1069 if( i_data <= i_max )
1071 /* Single NAL unit packet */
1072 block_t *out = block_Alloc( 12 + i_data );
1074 out->i_length = i_length;
1077 rtp_packetize_common( id, out, b_last, i_pts );
1078 out->i_buffer = 12 + i_data;
1080 memcpy( &out->p_buffer[12], p_data, i_data );
1082 rtp_packetize_send( id, out );
1086 /* FU-A Fragmentation Unit without interleaving */
1087 const int i_count = ( i_data-1 + i_max-2 - 1 ) / (i_max-2);
1093 for( i = 0; i < i_count; i++ )
1095 const int i_payload = __MIN( i_data, i_max-2 );
1096 block_t *out = block_Alloc( 12 + 2 + i_payload );
1097 out->i_dts = i_dts + i * i_length / i_count;
1098 out->i_length = i_length / i_count;
1101 rtp_packetize_common( id, out, (b_last && i_payload == i_data),
1103 out->i_buffer = 14 + i_payload;
1106 out->p_buffer[12] = 0x00 | (i_nal_hdr & 0x60) | 28;
1108 out->p_buffer[13] = ( i == 0 ? 0x80 : 0x00 ) | ( (i == i_count-1) ? 0x40 : 0x00 ) | i_nal_type;
1109 memcpy( &out->p_buffer[14], p_data, i_payload );
1111 rtp_packetize_send( id, out );
1113 i_data -= i_payload;
1114 p_data += i_payload;
1120 static int rtp_packetize_h264( sout_stream_id_t *id, block_t *in )
1122 const uint8_t *p_buffer = in->p_buffer;
1123 int i_buffer = in->i_buffer;
1125 while( i_buffer > 4 && ( p_buffer[0] != 0 || p_buffer[1] != 0 || p_buffer[2] != 1 ) )
1131 /* Split nal units */
1132 while( i_buffer > 4 )
1135 int i_size = i_buffer;
1136 int i_skip = i_buffer;
1138 /* search nal end */
1139 for( i_offset = 4; i_offset+2 < i_buffer ; i_offset++)
1141 if( p_buffer[i_offset] == 0 && p_buffer[i_offset+1] == 0 && p_buffer[i_offset+2] == 1 )
1143 /* we found another startcode */
1144 i_size = i_offset - ( p_buffer[i_offset-1] == 0 ? 1 : 0);
1149 /* TODO add STAP-A to remove a lot of overhead with small slice/sei/... */
1150 rtp_packetize_h264_nal( id, p_buffer, i_size,
1151 (in->i_pts > VLC_TS_INVALID ? in->i_pts : in->i_dts), in->i_dts,
1152 (i_size >= i_buffer), in->i_length * i_size / in->i_buffer );
1160 static int rtp_packetize_amr( sout_stream_id_t *id, block_t *in )
1162 int i_max = rtp_mtu (id) - 2; /* payload max in one packet */
1163 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1165 uint8_t *p_data = in->p_buffer;
1166 int i_data = in->i_buffer;
1169 /* Only supports octet-aligned mode */
1170 for( i = 0; i < i_count; i++ )
1172 int i_payload = __MIN( i_max, i_data );
1173 block_t *out = block_Alloc( 14 + i_payload );
1175 /* rtp common header */
1176 rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
1177 (in->i_pts > VLC_TS_INVALID ? in->i_pts : in->i_dts) );
1178 /* Payload header */
1179 out->p_buffer[12] = 0xF0; /* CMR */
1180 out->p_buffer[13] = p_data[0]&0x7C; /* ToC */ /* FIXME: frame type */
1182 /* FIXME: are we fed multiple frames ? */
1183 memcpy( &out->p_buffer[14], p_data+1, i_payload-1 );
1185 out->i_buffer = 14 + i_payload-1;
1186 out->i_dts = in->i_dts + i * in->i_length / i_count;
1187 out->i_length = in->i_length / i_count;
1189 rtp_packetize_send( id, out );
1191 p_data += i_payload;
1192 i_data -= i_payload;
1198 static int rtp_packetize_t140( sout_stream_id_t *id, block_t *in )
1200 const size_t i_max = rtp_mtu (id);
1201 const uint8_t *p_data = in->p_buffer;
1202 size_t i_data = in->i_buffer;
1204 for( unsigned i_packet = 0; i_data > 0; i_packet++ )
1206 size_t i_payload = i_data;
1208 /* Make sure we stop on an UTF-8 character boundary
1209 * (assuming the input is valid UTF-8) */
1210 if( i_data > i_max )
1214 while( ( p_data[i_payload] & 0xC0 ) == 0x80 )
1216 if( i_payload == 0 )
1217 return VLC_SUCCESS; /* fishy input! */
1223 block_t *out = block_Alloc( 12 + i_payload );
1227 rtp_packetize_common( id, out, 0, in->i_pts + i_packet );
1228 memcpy( out->p_buffer + 12, p_data, i_payload );
1230 out->i_buffer = 12 + i_payload;
1231 out->i_dts = out->i_pts;
1234 rtp_packetize_send( id, out );
1236 p_data += i_payload;
1237 i_data -= i_payload;
1244 static int rtp_packetize_spx( sout_stream_id_t *id, block_t *in )
1246 uint8_t *p_buffer = in->p_buffer;
1247 int i_data_size, i_payload_size, i_payload_padding;
1248 i_data_size = i_payload_size = in->i_buffer;
1249 i_payload_padding = 0;
1252 if ( in->i_buffer > rtp_mtu (id) )
1256 RFC for Speex in RTP says that each packet must end on an octet
1257 boundary. So, we check to see if the number of bytes % 4 is zero.
1258 If not, we have to add some padding.
1260 This MAY be overkill since packetization is handled elsewhere and
1261 appears to ensure the octet boundary. However, better safe than
1264 if ( i_payload_size % 4 )
1266 i_payload_padding = 4 - ( i_payload_size % 4 );
1267 i_payload_size += i_payload_padding;
1271 Allocate a new RTP p_output block of the appropriate size.
1272 Allow for 12 extra bytes of RTP header.
1274 p_out = block_Alloc( 12 + i_payload_size );
1276 if ( i_payload_padding )
1279 The padding is required to be a zero followed by all 1s.
1281 char c_first_pad, c_remaining_pad;
1283 c_remaining_pad = 0xFF;
1286 Allow for 12 bytes before the i_data_size because
1287 of the expected RTP header added during
1288 rtp_packetize_common.
1290 p_out->p_buffer[12 + i_data_size] = c_first_pad;
1291 switch (i_payload_padding)
1294 p_out->p_buffer[12 + i_data_size + 1] = c_remaining_pad;
1297 p_out->p_buffer[12 + i_data_size + 1] = c_remaining_pad;
1298 p_out->p_buffer[12 + i_data_size + 2] = c_remaining_pad;
1303 /* Add the RTP header to our p_output buffer. */
1304 rtp_packetize_common( id, p_out, 0,
1305 (in->i_pts > VLC_TS_INVALID ? in->i_pts : in->i_dts) );
1306 /* Copy the Speex payload to the p_output buffer */
1307 memcpy( &p_out->p_buffer[12], p_buffer, i_data_size );
1309 p_out->i_buffer = 12 + i_payload_size;
1310 p_out->i_dts = in->i_dts;
1311 p_out->i_length = in->i_length;
1313 /* Queue the buffer for actual transmission. */
1314 rtp_packetize_send( id, p_out );
1318 static int rtp_packetize_g726( sout_stream_id_t *id, block_t *in, int i_pad )
1320 int i_max = (rtp_mtu( id )- 12 + i_pad - 1) & ~i_pad;
1321 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1323 uint8_t *p_data = in->p_buffer;
1324 int i_data = in->i_buffer;
1329 int i_payload = __MIN( i_max, i_data );
1330 block_t *out = block_New( p_stream, 12 + i_payload );
1332 /* rtp common header */
1333 rtp_packetize_common( id, out, 0,
1334 (in->i_pts > VLC_TS_INVALID ? in->i_pts : in->i_dts) );
1336 memcpy( &out->p_buffer[12], p_data, i_payload );
1338 out->i_buffer = 12 + i_payload;
1339 out->i_dts = in->i_dts + i_packet++ * in->i_length / i_count;
1340 out->i_length = in->i_length / i_count;
1342 rtp_packetize_send( id, out );
1344 p_data += i_payload;
1345 i_data -= i_payload;
1350 static int rtp_packetize_g726_16( sout_stream_id_t *id, block_t *in )
1352 return rtp_packetize_g726( id, in, 4 );
1355 static int rtp_packetize_g726_24( sout_stream_id_t *id, block_t *in )
1357 return rtp_packetize_g726( id, in, 8 );
1360 static int rtp_packetize_g726_32( sout_stream_id_t *id, block_t *in )
1362 return rtp_packetize_g726( id, in, 2 );
1365 static int rtp_packetize_g726_40( sout_stream_id_t *id, block_t *in )
1367 return rtp_packetize_g726( id, in, 8 );