+/* rfc3984 */
+static int
+rtp_packetize_h264_nal( sout_stream_t *p_stream, sout_stream_id_t *id,
+ const uint8_t *p_data, int i_data, int64_t i_pts,
+ int64_t i_dts, vlc_bool_t b_last, int64_t i_length )
+{
+ const int i_max = id->i_mtu - 12; /* payload max in one packet */
+ int i_nal_hdr;
+ int i_nal_type;
+
+ if( i_data < 5 )
+ return VLC_SUCCESS;
+
+ i_nal_hdr = p_data[3];
+ i_nal_type = i_nal_hdr&0x1f;
+
+ /* Skip start code */
+ p_data += 3;
+ i_data -= 3;
+
+ /* */
+ if( i_data <= i_max )
+ {
+ /* Single NAL unit packet */
+ block_t *out = block_New( p_stream, 12 + i_data );
+ out->i_dts = i_dts;
+ out->i_length = i_length;
+
+ /* */
+ rtp_packetize_common( id, out, b_last, i_pts );
+ out->i_buffer = 12 + i_data;
+
+ memcpy( &out->p_buffer[12], p_data, i_data );
+
+ rtp_packetize_send( id, out );
+ }
+ else
+ {
+ /* FU-A Fragmentation Unit without interleaving */
+ const int i_count = ( i_data-1 + i_max-2 - 1 ) / (i_max-2);
+ int i;
+
+ p_data++;
+ i_data--;
+
+ for( i = 0; i < i_count; i++ )
+ {
+ const int i_payload = __MIN( i_data, i_max-2 );
+ block_t *out = block_New( p_stream, 12 + 2 + i_payload );
+ out->i_dts = i_dts + i * i_length / i_count;
+ out->i_length = i_length / i_count;
+
+ /* */
+ rtp_packetize_common( id, out, (b_last && i_payload == i_data), i_pts );
+ out->i_buffer = 14 + i_payload;
+
+ /* FU indicator */
+ out->p_buffer[12] = 0x00 | (i_nal_hdr & 0x60) | 28;
+ /* FU header */
+ out->p_buffer[13] = ( i == 0 ? 0x80 : 0x00 ) | ( (i == i_count-1) ? 0x40 : 0x00 ) | i_nal_type;
+ memcpy( &out->p_buffer[14], p_data, i_payload );
+
+ rtp_packetize_send( id, out );
+
+ i_data -= i_payload;
+ p_data += i_payload;
+ }
+ }
+ return VLC_SUCCESS;
+}
+
+static int rtp_packetize_h264( sout_stream_t *p_stream, sout_stream_id_t *id,
+ block_t *in )
+{
+ const uint8_t *p_buffer = in->p_buffer;
+ int i_buffer = in->i_buffer;
+
+ while( i_buffer > 4 && ( p_buffer[0] != 0 || p_buffer[1] != 0 || p_buffer[2] != 1 ) )
+ {
+ i_buffer--;
+ p_buffer++;
+ }
+
+ /* Split nal units */
+ while( i_buffer > 4 )
+ {
+ int i_offset;
+ int i_size = i_buffer;
+ int i_skip = i_buffer;
+
+ /* search nal end */
+ for( i_offset = 4; i_offset+2 < i_buffer ; i_offset++)
+ {
+ if( p_buffer[i_offset] == 0 && p_buffer[i_offset+1] == 0 && p_buffer[i_offset+2] == 1 )
+ {
+ /* we found another startcode */
+ i_size = i_offset - ( p_buffer[i_offset-1] == 0 ? 1 : 0);
+ i_skip = i_offset;
+ break;
+ }
+ }
+ /* TODO add STAP-A to remove a lot of overhead with small slice/sei/... */
+ rtp_packetize_h264_nal( p_stream, id, p_buffer, i_size,
+ (in->i_pts > 0 ? in->i_pts : in->i_dts), in->i_dts,
+ (i_size >= i_buffer), in->i_length * i_size / in->i_buffer );
+
+ i_buffer -= i_skip;
+ p_buffer += i_skip;
+ }
+ return VLC_SUCCESS;
+}
+
+static int rtp_packetize_amr( sout_stream_t *p_stream, sout_stream_id_t *id,
+ block_t *in )
+{
+ int i_max = id->i_mtu - 14; /* payload max in one packet */
+ int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
+
+ uint8_t *p_data = in->p_buffer;
+ int i_data = in->i_buffer;
+ int i;
+
+ /* Only supports octet-aligned mode */
+ for( i = 0; i < i_count; i++ )
+ {
+ int i_payload = __MIN( i_max, i_data );
+ block_t *out = block_New( p_stream, 14 + i_payload );
+
+ /* rtp common header */
+ rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
+ (in->i_pts > 0 ? in->i_pts : in->i_dts) );
+ /* Payload header */
+ out->p_buffer[12] = 0xF0; /* CMR */
+ out->p_buffer[13] = p_data[0]&0x7C; /* ToC */ /* FIXME: frame type */
+
+ /* FIXME: are we fed multiple frames ? */
+ memcpy( &out->p_buffer[14], p_data+1, i_payload-1 );
+
+ out->i_buffer = 14 + i_payload-1;
+ out->i_dts = in->i_dts + i * in->i_length / i_count;
+ out->i_length = in->i_length / i_count;
+
+ rtp_packetize_send( id, out );
+
+ p_data += i_payload;
+ i_data -= i_payload;
+ }
+
+ return VLC_SUCCESS;
+}
+
+static int rtp_packetize_t140( sout_stream_t *p_stream, sout_stream_id_t *id,
+ block_t *in )
+{
+ const size_t i_max = id->i_mtu - 12;
+ const uint8_t *p_data = in->p_buffer;
+ size_t i_data = in->i_buffer;
+
+ for( unsigned i_packet = 0; i_data > 0; i_packet++ )
+ {
+ size_t i_payload = i_data;
+
+ /* Make sure we stop on an UTF-8 character boundary
+ * (assuming the input is valid UTF-8) */
+ if( i_data > i_max )
+ {
+ i_payload = i_max;
+
+ while( ( p_data[i_payload] & 0xC0 ) == 0x80 )
+ {
+ if( i_payload == 0 )
+ return VLC_SUCCESS; /* fishy input! */
+
+ i_payload--;
+ }
+ }
+
+ block_t *out = block_New( p_stream, 12 + i_payload );
+ if( out == NULL )
+ return VLC_SUCCESS;
+
+ rtp_packetize_common( id, out, 0, in->i_pts + i_packet );
+ memcpy( out->p_buffer + 12, p_data, i_payload );
+
+ out->i_buffer = 12 + i_payload;
+ out->i_dts = out->i_pts;
+ out->i_length = 0;
+
+ rtp_packetize_send( id, out );
+
+ p_data += i_payload;
+ i_data -= i_payload;
+ }
+
+ return VLC_SUCCESS;
+}
+
+/*****************************************************************************
+ * Non-RTP mux
+ *****************************************************************************/
+
+/** Add an ES to a non-RTP muxed stream */
+static sout_stream_id_t *MuxAdd( sout_stream_t *p_stream, es_format_t *p_fmt )
+{
+ sout_input_t *p_input;
+ sout_mux_t *p_mux = p_stream->p_sys->p_mux;
+ assert( p_mux != NULL );
+
+ p_input = sout_MuxAddStream( p_mux, p_fmt );
+ if( p_input == NULL )
+ {
+ msg_Err( p_stream, "cannot add this stream to the muxer" );
+ return NULL;
+ }
+
+ return (sout_stream_id_t *)p_input;
+}
+
+
+static int MuxSend( sout_stream_t *p_stream, sout_stream_id_t *id,
+ block_t *p_buffer )
+{
+ sout_mux_t *p_mux = p_stream->p_sys->p_mux;
+ assert( p_mux != NULL );
+
+ sout_MuxSendBuffer( p_mux, (sout_input_t *)id, p_buffer );
+ return VLC_SUCCESS;
+}
+
+
+/** Remove an ES from a non-RTP muxed stream */
+static int MuxDel( sout_stream_t *p_stream, sout_stream_id_t *id )
+{
+ sout_mux_t *p_mux = p_stream->p_sys->p_mux;
+ assert( p_mux != NULL );
+
+ sout_MuxDeleteStream( p_mux, (sout_input_t *)id );
+ return VLC_SUCCESS;
+}
+
+
+static int AccessOutGrabberWriteBuffer( sout_stream_t *p_stream,
+ const block_t *p_buffer )
+{
+ sout_stream_sys_t *p_sys = p_stream->p_sys;
+ sout_stream_id_t *id = p_sys->es[0];
+
+ int64_t i_dts = p_buffer->i_dts;
+
+ uint8_t *p_data = p_buffer->p_buffer;
+ unsigned int i_data = p_buffer->i_buffer;
+ unsigned int i_max = id->i_mtu - 12;
+
+ unsigned i_packet = ( p_buffer->i_buffer + i_max - 1 ) / i_max;
+
+ while( i_data > 0 )
+ {
+ unsigned int i_size;
+
+ /* output complete packet */
+ if( p_sys->packet &&
+ p_sys->packet->i_buffer + i_data > i_max )
+ {
+ rtp_packetize_send( id, p_sys->packet );
+ p_sys->packet = NULL;
+ }
+
+ if( p_sys->packet == NULL )
+ {
+ /* allocate a new packet */
+ p_sys->packet = block_New( p_stream, id->i_mtu );
+ rtp_packetize_common( id, p_sys->packet, 1, i_dts );
+ p_sys->packet->i_dts = i_dts;
+ p_sys->packet->i_length = p_buffer->i_length / i_packet;
+ i_dts += p_sys->packet->i_length;
+ }
+
+ i_size = __MIN( i_data,
+ (unsigned)(id->i_mtu - p_sys->packet->i_buffer) );
+
+ memcpy( &p_sys->packet->p_buffer[p_sys->packet->i_buffer],
+ p_data, i_size );
+
+ p_sys->packet->i_buffer += i_size;
+ p_data += i_size;
+ i_data -= i_size;
+ }
+
+ return VLC_SUCCESS;
+}
+
+
+static int AccessOutGrabberWrite( sout_access_out_t *p_access,
+ block_t *p_buffer )
+{
+ sout_stream_t *p_stream = (sout_stream_t*)p_access->p_sys;
+
+ while( p_buffer )
+ {
+ block_t *p_next;
+
+ AccessOutGrabberWriteBuffer( p_stream, p_buffer );
+
+ p_next = p_buffer->p_next;
+ block_Release( p_buffer );
+ p_buffer = p_next;
+ }
+
+ return VLC_SUCCESS;
+}
+
+
+static sout_access_out_t *GrabberCreate( sout_stream_t *p_stream )
+{
+ sout_access_out_t *p_grab;
+
+ p_grab = vlc_object_create( p_stream->p_sout, sizeof( *p_grab ) );
+ if( p_grab == NULL )
+ return NULL;
+
+ p_grab->p_module = NULL;
+ p_grab->p_sout = p_stream->p_sout;
+ p_grab->psz_access = strdup( "grab" );
+ p_grab->p_cfg = NULL;
+ p_grab->psz_path = strdup( "" );
+ p_grab->p_sys = (sout_access_out_sys_t *)p_stream;
+ p_grab->pf_seek = NULL;
+ p_grab->pf_write = AccessOutGrabberWrite;
+ return p_grab;
+}