+#include "../demux/xiph.h"
+
+#include <assert.h>
+
+static int rtp_packetize_mpa (sout_stream_id_sys_t *, block_t *);
+static int rtp_packetize_mpv (sout_stream_id_sys_t *, block_t *);
+static int rtp_packetize_ac3 (sout_stream_id_sys_t *, block_t *);
+static int rtp_packetize_split(sout_stream_id_sys_t *, block_t *);
+static int rtp_packetize_swab (sout_stream_id_sys_t *, block_t *);
+static int rtp_packetize_mp4a (sout_stream_id_sys_t *, block_t *);
+static int rtp_packetize_mp4a_latm (sout_stream_id_sys_t *, block_t *);
+static int rtp_packetize_h263 (sout_stream_id_sys_t *, block_t *);
+static int rtp_packetize_h264 (sout_stream_id_sys_t *, block_t *);
+static int rtp_packetize_amr (sout_stream_id_sys_t *, block_t *);
+static int rtp_packetize_spx (sout_stream_id_sys_t *, block_t *);
+static int rtp_packetize_t140 (sout_stream_id_sys_t *, block_t *);
+static int rtp_packetize_g726_16 (sout_stream_id_sys_t *, block_t *);
+static int rtp_packetize_g726_24 (sout_stream_id_sys_t *, block_t *);
+static int rtp_packetize_g726_32 (sout_stream_id_sys_t *, block_t *);
+static int rtp_packetize_g726_40 (sout_stream_id_sys_t *, block_t *);
+static int rtp_packetize_xiph (sout_stream_id_sys_t *, block_t *);
+static int rtp_packetize_vp8 (sout_stream_id_sys_t *, block_t *);
+static int rtp_packetize_jpeg (sout_stream_id_sys_t *, block_t *);
+
+#define XIPH_IDENT (0)
+
+/* Helpers common to xiph codecs (vorbis and theora) */
+
+static int rtp_xiph_pack_headers(size_t room, void *p_extra, size_t i_extra,
+ uint8_t **p_buffer, size_t *i_buffer,
+ uint8_t *theora_pixel_fmt)
+{
+ unsigned packet_size[XIPH_MAX_HEADER_COUNT];
+ void *packet[XIPH_MAX_HEADER_COUNT];
+ unsigned packet_count;
+ if (xiph_SplitHeaders(packet_size, packet, &packet_count,
+ i_extra, p_extra))
+ return VLC_EGENERIC;;
+ if (packet_count < 3)
+ return VLC_EGENERIC;;
+
+ if (theora_pixel_fmt != NULL)
+ {
+ if (packet_size[0] < 42)
+ return VLC_EGENERIC;
+ *theora_pixel_fmt = (((uint8_t *)packet[0])[41] >> 3) & 0x03;
+ }
+
+ unsigned length_size[2] = { 0, 0 };
+ for (int i = 0; i < 2; i++)
+ {
+ unsigned size = packet_size[i];
+ while (size > 0)
+ {
+ length_size[i]++;
+ size >>= 7;
+ }
+ }
+
+ *i_buffer = room + 1 + length_size[0] + length_size[1]
+ + packet_size[0] + packet_size[1] + packet_size[2];
+ *p_buffer = malloc(*i_buffer);
+ if (*p_buffer == NULL)
+ return VLC_ENOMEM;
+
+ uint8_t *p = *p_buffer + room;
+ /* Number of headers */
+ *p++ = 2;
+
+ for (int i = 0; i < 2; i++)
+ {
+ unsigned size = length_size[i];
+ while (size > 0)
+ {
+ *p = (packet_size[i] >> (7 * (size - 1))) & 0x7f;
+ if (--size > 0)
+ *p |= 0x80;
+ p++;
+ }
+ }
+ for (int i = 0; i < 3; i++)
+ {
+ memcpy(p, packet[i], packet_size[i]);
+ p += packet_size[i];
+ }
+
+ return VLC_SUCCESS;
+}
+
+static char *rtp_xiph_b64_oob_config(void *p_extra, size_t i_extra,
+ uint8_t *theora_pixel_fmt)
+{
+ uint8_t *p_buffer;
+ size_t i_buffer;
+ if (rtp_xiph_pack_headers(9, p_extra, i_extra, &p_buffer, &i_buffer,
+ theora_pixel_fmt) != VLC_SUCCESS)
+ return NULL;
+
+ /* Number of packed headers */
+ SetDWBE(p_buffer, 1);
+ /* Ident */
+ uint32_t ident = XIPH_IDENT;
+ SetWBE(p_buffer + 4, ident >> 8);
+ p_buffer[6] = ident & 0xff;
+ /* Length field */
+ SetWBE(p_buffer + 7, i_buffer);
+
+ char *config = vlc_b64_encode_binary(p_buffer, i_buffer);
+ free(p_buffer);
+ return config;
+}
+
+static void sprintf_hexa( char *s, uint8_t *p_data, int i_data )
+{
+ static const char hex[16] = "0123456789abcdef";
+
+ for( int i = 0; i < i_data; i++ )
+ {
+ s[2*i+0] = hex[(p_data[i]>>4)&0xf];
+ s[2*i+1] = hex[(p_data[i] )&0xf];
+ }
+ s[2*i_data] = '\0';
+}
+
+/* TODO: make this into something more clever than a big switch? */
+int rtp_get_fmt( vlc_object_t *obj, es_format_t *p_fmt, const char *mux,
+ rtp_format_t *rtp_fmt )
+{
+ assert( p_fmt != NULL || mux != NULL );
+
+ /* Dynamic payload type. Payload types are scoped to the RTP
+ * session, and we put each ES in its own session, so no risk of
+ * conflict. */
+ rtp_fmt->payload_type = 96;
+ rtp_fmt->cat = mux != NULL ? VIDEO_ES : p_fmt->i_cat;
+ if( rtp_fmt->cat == AUDIO_ES )
+ {
+ rtp_fmt->clock_rate = p_fmt->audio.i_rate;
+ rtp_fmt->channels = p_fmt->audio.i_channels;
+ }
+ else
+ rtp_fmt->clock_rate = 90000; /* most common case for video */
+ /* Stream bitrate in kbps */
+ rtp_fmt->bitrate = p_fmt != NULL ? p_fmt->i_bitrate/1000 : 0;
+ rtp_fmt->fmtp = NULL;
+
+ if( mux != NULL )
+ {
+ if( strncmp( mux, "ts", 2 ) == 0 )
+ {
+ rtp_fmt->payload_type = 33;
+ rtp_fmt->ptname = "MP2T";
+ }
+ else
+ rtp_fmt->ptname = "MP2P";
+ return VLC_SUCCESS;
+ }
+
+ switch( p_fmt->i_codec )
+ {
+ case VLC_CODEC_MULAW:
+ if( p_fmt->audio.i_channels == 1 && p_fmt->audio.i_rate == 8000 )
+ rtp_fmt->payload_type = 0;
+ rtp_fmt->ptname = "PCMU";
+ rtp_fmt->pf_packetize = rtp_packetize_split;
+ break;
+ case VLC_CODEC_ALAW:
+ if( p_fmt->audio.i_channels == 1 && p_fmt->audio.i_rate == 8000 )
+ rtp_fmt->payload_type = 8;
+ rtp_fmt->ptname = "PCMA";
+ rtp_fmt->pf_packetize = rtp_packetize_split;
+ break;
+ case VLC_CODEC_S16B:
+ case VLC_CODEC_S16L:
+ if( p_fmt->audio.i_channels == 1 && p_fmt->audio.i_rate == 44100 )
+ {
+ rtp_fmt->payload_type = 11;
+ }
+ else if( p_fmt->audio.i_channels == 2 &&
+ p_fmt->audio.i_rate == 44100 )
+ {
+ rtp_fmt->payload_type = 10;
+ }
+ rtp_fmt->ptname = "L16";
+ if( p_fmt->i_codec == VLC_CODEC_S16B )
+ rtp_fmt->pf_packetize = rtp_packetize_split;
+ else
+ rtp_fmt->pf_packetize = rtp_packetize_swab;
+ break;
+ case VLC_CODEC_U8:
+ rtp_fmt->ptname = "L8";
+ rtp_fmt->pf_packetize = rtp_packetize_split;
+ break;
+ case VLC_CODEC_S24B:
+ rtp_fmt->ptname = "L24";
+ rtp_fmt->pf_packetize = rtp_packetize_split;
+ break;
+ case VLC_CODEC_MPGA:
+ rtp_fmt->payload_type = 14;
+ rtp_fmt->ptname = "MPA";
+ rtp_fmt->clock_rate = 90000; /* not 44100 */
+ rtp_fmt->pf_packetize = rtp_packetize_mpa;
+ break;
+ case VLC_CODEC_MPGV:
+ rtp_fmt->payload_type = 32;
+ rtp_fmt->ptname = "MPV";
+ rtp_fmt->pf_packetize = rtp_packetize_mpv;
+ break;
+ case VLC_CODEC_ADPCM_G726:
+ switch( p_fmt->i_bitrate / 1000 )
+ {
+ case 16:
+ rtp_fmt->ptname = "G726-16";
+ rtp_fmt->pf_packetize = rtp_packetize_g726_16;
+ break;
+ case 24:
+ rtp_fmt->ptname = "G726-24";
+ rtp_fmt->pf_packetize = rtp_packetize_g726_24;
+ break;
+ case 32:
+ rtp_fmt->ptname = "G726-32";
+ rtp_fmt->pf_packetize = rtp_packetize_g726_32;
+ break;
+ case 40:
+ rtp_fmt->ptname = "G726-40";
+ rtp_fmt->pf_packetize = rtp_packetize_g726_40;
+ break;
+ default:
+ msg_Err( obj, "cannot add this stream (unsupported "
+ "G.726 bit rate: %u)", p_fmt->i_bitrate );
+ return VLC_EGENERIC;
+ }
+ break;
+ case VLC_CODEC_A52:
+ rtp_fmt->ptname = "ac3";
+ rtp_fmt->pf_packetize = rtp_packetize_ac3;
+ break;
+ case VLC_CODEC_H263:
+ rtp_fmt->ptname = "H263-1998";
+ rtp_fmt->pf_packetize = rtp_packetize_h263;
+ break;
+ case VLC_CODEC_H264:
+ rtp_fmt->ptname = "H264";
+ rtp_fmt->pf_packetize = rtp_packetize_h264;
+ rtp_fmt->fmtp = NULL;
+
+ if( p_fmt->i_extra > 0 )
+ {
+ uint8_t *p_buffer = p_fmt->p_extra;
+ int i_buffer = p_fmt->i_extra;
+ char *p_64_sps = NULL;
+ char *p_64_pps = NULL;
+ char hexa[6+1];
+
+ while( i_buffer > 4 )
+ {
+ int i_offset = 0;
+ int i_size = 0;
+
+ while( p_buffer[0] != 0 || p_buffer[1] != 0 ||
+ p_buffer[2] != 1 )
+ {
+ p_buffer++;
+ i_buffer--;
+ if( i_buffer == 0 ) break;
+ }
+
+ if( i_buffer < 4 || memcmp(p_buffer, "\x00\x00\x01", 3 ) )
+ {
+ msg_Dbg( obj, "No startcode found..");
+ break;
+ }
+ p_buffer += 3;
+ i_buffer -= 3;
+
+ const int i_nal_type = p_buffer[0]&0x1f;
+
+ msg_Dbg( obj, "we found a startcode for NAL with TYPE:%d", i_nal_type );
+
+ i_size = i_buffer;
+ for( i_offset = 0; i_offset+2 < i_buffer ; i_offset++)
+ {
+ if( !memcmp(p_buffer + i_offset, "\x00\x00\x01", 3 ) )
+ {
+ /* we found another startcode */
+ while( i_offset > 0 && 0 == p_buffer[ i_offset - 1 ] )
+ i_offset--;
+ i_size = i_offset;
+ break;
+ }
+ }