]> git.sesse.net Git - vlc/blob - modules/stream_out/rtpfmt.c
contribs: blind attempt to fix compilation of x264 on win32.
[vlc] / modules / stream_out / rtpfmt.c
1 /*****************************************************************************
2  * rtpfmt.c: RTP payload formats
3  *****************************************************************************
4  * Copyright (C) 2003-2004 the VideoLAN team
5  * Copyright © 2007 Rémi Denis-Courmont
6  * $Id$
7  *
8  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <vlc_common.h>
30 #include <vlc_sout.h>
31 #include <vlc_block.h>
32 #include <vlc_strings.h>
33
34 #include "rtp.h"
35 #include "../demux/xiph.h"
36
37 #include <assert.h>
38
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 *);
56
57 #define XIPH_IDENT (0)
58
59 /* Helpers common to xiph codecs (vorbis and theora) */
60
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)
64 {
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,
69                                 i_extra, p_extra);
70     if (val != VLC_SUCCESS)
71         return val;
72     if (packet_count < 3)
73     {
74         val = VLC_EGENERIC;
75         goto free;
76     }
77
78     if (theora_pixel_fmt != NULL)
79     {
80         if (packet_size[0] < 42)
81         {
82             val = VLC_EGENERIC;
83             goto free;
84         }
85         *theora_pixel_fmt = (((uint8_t *)packet[0])[41] >> 3) & 0x03;
86     }
87
88     unsigned length_size[2] = { 0, 0 };
89     for (int i = 0; i < 2; i++)
90     {
91         unsigned size = packet_size[i];
92         while (size > 0)
93         {
94             length_size[i]++;
95             size >>= 7;
96         }
97     }
98
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)
103     {
104         val = VLC_ENOMEM;
105         goto free;
106     }
107
108     uint8_t *p = *p_buffer + room;
109     /* Number of headers */
110     *p++ = 2;
111
112     for (int i = 0; i < 2; i++)
113     {
114         unsigned size = length_size[i];
115         while (size > 0)
116         {
117             *p = (packet_size[i] >> (7 * (size - 1))) & 0x7f;
118             if (--size > 0)
119                 *p |= 0x80;
120             p++;
121         }
122     }
123     for (int i = 0; i < 3; i++)
124     {
125         memcpy(p, packet[i], packet_size[i]);
126         p += packet_size[i];
127     }
128
129     val = VLC_SUCCESS;
130 free:
131     for (unsigned i = 0; i < packet_count; i++)
132         free(packet[i]);
133
134     return val;
135 }
136
137 static char *rtp_xiph_b64_oob_config(void *p_extra, size_t i_extra,
138                                      uint8_t *theora_pixel_fmt)
139 {
140     uint8_t *p_buffer;
141     size_t i_buffer;
142     if (rtp_xiph_pack_headers(9, p_extra, i_extra, &p_buffer, &i_buffer,
143                               theora_pixel_fmt) != VLC_SUCCESS)
144         return NULL;
145
146     /* Number of packed headers */
147     SetDWBE(p_buffer, 1);
148     /* Ident */
149     uint32_t ident = XIPH_IDENT;
150     SetWBE(p_buffer + 4, ident >> 8);
151     p_buffer[6] = ident & 0xff;
152     /* Length field */
153     SetWBE(p_buffer + 7, i_buffer);
154
155     char *config = vlc_b64_encode_binary(p_buffer, i_buffer);
156     free(p_buffer);
157     return config;
158 }
159
160 static void sprintf_hexa( char *s, uint8_t *p_data, int i_data )
161 {
162     static const char hex[16] = "0123456789abcdef";
163
164     for( int i = 0; i < i_data; i++ )
165     {
166         s[2*i+0] = hex[(p_data[i]>>4)&0xf];
167         s[2*i+1] = hex[(p_data[i]   )&0xf];
168     }
169     s[2*i_data] = '\0';
170 }
171
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 )
175 {
176     assert( p_fmt != NULL || mux != NULL );
177
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
180      * conflict. */
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 )
184     {
185         rtp_fmt->clock_rate = p_fmt->audio.i_rate;
186         rtp_fmt->channels = p_fmt->audio.i_channels;
187     }
188     else
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;
193
194     if( mux != NULL )
195     {
196         if( strncmp( mux, "ts", 2 ) == 0 )
197         {
198             rtp_fmt->payload_type = 33;
199             rtp_fmt->ptname = "MP2T";
200         }
201         else
202             rtp_fmt->ptname = "MP2P";
203         return VLC_SUCCESS;
204     }
205
206     switch( p_fmt->i_codec )
207     {
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;
213             break;
214         case VLC_CODEC_ALAW:
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;
219             break;
220         case VLC_CODEC_S16B:
221         case VLC_CODEC_S16L:
222             if( p_fmt->audio.i_channels == 1 && p_fmt->audio.i_rate == 44100 )
223             {
224                 rtp_fmt->payload_type = 11;
225             }
226             else if( p_fmt->audio.i_channels == 2 &&
227                      p_fmt->audio.i_rate == 44100 )
228             {
229                 rtp_fmt->payload_type = 10;
230             }
231             rtp_fmt->ptname = "L16";
232             if( p_fmt->i_codec == VLC_CODEC_S16B )
233                 rtp_fmt->pf_packetize = rtp_packetize_split;
234             else
235                 rtp_fmt->pf_packetize = rtp_packetize_swab;
236             break;
237         case VLC_CODEC_U8:
238             rtp_fmt->ptname = "L8";
239             rtp_fmt->pf_packetize = rtp_packetize_split;
240             break;
241         case VLC_CODEC_MPGA:
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;
246             break;
247         case VLC_CODEC_MPGV:
248             rtp_fmt->payload_type = 32;
249             rtp_fmt->ptname = "MPV";
250             rtp_fmt->pf_packetize = rtp_packetize_mpv;
251             break;
252         case VLC_CODEC_ADPCM_G726:
253             switch( p_fmt->i_bitrate / 1000 )
254             {
255             case 16:
256                 rtp_fmt->ptname = "G726-16";
257                 rtp_fmt->pf_packetize = rtp_packetize_g726_16;
258                 break;
259             case 24:
260                 rtp_fmt->ptname = "G726-24";
261                 rtp_fmt->pf_packetize = rtp_packetize_g726_24;
262                 break;
263             case 32:
264                 rtp_fmt->ptname = "G726-32";
265                 rtp_fmt->pf_packetize = rtp_packetize_g726_32;
266                 break;
267             case 40:
268                 rtp_fmt->ptname = "G726-40";
269                 rtp_fmt->pf_packetize = rtp_packetize_g726_40;
270                 break;
271             default:
272                 msg_Err( obj, "cannot add this stream (unsupported "
273                          "G.726 bit rate: %u)", p_fmt->i_bitrate );
274                 return VLC_EGENERIC;
275             }
276             break;
277         case VLC_CODEC_A52:
278             rtp_fmt->ptname = "ac3";
279             rtp_fmt->pf_packetize = rtp_packetize_ac3;
280             break;
281         case VLC_CODEC_H263:
282             rtp_fmt->ptname = "H263-1998";
283             rtp_fmt->pf_packetize = rtp_packetize_h263;
284             break;
285         case VLC_CODEC_H264:
286             rtp_fmt->ptname = "H264";
287             rtp_fmt->pf_packetize = rtp_packetize_h264;
288             rtp_fmt->fmtp = NULL;
289
290             if( p_fmt->i_extra > 0 )
291             {
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;
296                 char    hexa[6+1];
297
298                 while( i_buffer > 4 )
299                 {
300                     int i_offset    = 0;
301                     int i_size      = 0;
302
303                     while( p_buffer[0] != 0 || p_buffer[1] != 0 ||
304                            p_buffer[2] != 1 )
305                     {
306                         p_buffer++;
307                         i_buffer--;
308                         if( i_buffer == 0 ) break;
309                     }
310
311                     if( i_buffer < 4 || memcmp(p_buffer, "\x00\x00\x01", 3 ) )
312                     {
313                         msg_Dbg( obj, "No startcode found..");
314                         break;
315                     }
316                     p_buffer += 3;
317                     i_buffer -= 3;
318
319                     const int i_nal_type = p_buffer[0]&0x1f;
320
321                     msg_Dbg( obj, "we found a startcode for NAL with TYPE:%d", i_nal_type );
322
323                     i_size = i_buffer;
324                     for( i_offset = 0; i_offset+2 < i_buffer ; i_offset++)
325                     {
326                         if( !memcmp(p_buffer + i_offset, "\x00\x00\x01", 3 ) )
327                         {
328                             /* we found another startcode */
329                             while( i_offset > 0 && 0 == p_buffer[ i_offset - 1 ] )
330                                 i_offset--;
331                             i_size = i_offset;
332                             break;
333                         }
334                     }
335
336                     if( i_size == 0 )
337                     {
338                         msg_Dbg( obj, "No-info found in nal ");
339                         continue;
340                     }
341
342                     if( i_nal_type == 7 )
343                     {
344                         free( p_64_sps );
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 );
348                     }
349                     else if( i_nal_type == 8 )
350                     {
351                         free( p_64_pps );
352                         p_64_pps = vlc_b64_encode_binary( p_buffer, i_size );
353                     }
354                     i_buffer -= i_size;
355                     p_buffer += i_size;
356                 }
357                 /* */
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,
362                                 p_64_pps ) == -1 ) )
363                     rtp_fmt->fmtp = NULL;
364                 free( p_64_sps );
365                 free( p_64_pps );
366             }
367             if( rtp_fmt->fmtp == NULL )
368                 rtp_fmt->fmtp = strdup( "packetization-mode=1" );
369             break;
370
371         case VLC_CODEC_MP4V:
372         {
373             rtp_fmt->ptname = "MP4V-ES";
374             rtp_fmt->pf_packetize = rtp_packetize_split;
375             if( p_fmt->i_extra > 0 )
376             {
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;
382             }
383             break;
384         }
385         case VLC_CODEC_MP4A:
386         {
387             if( ! var_InheritBool( obj, "sout-rtp-mp4a-latm" ) )
388             {
389                 char hexa[2*p_fmt->i_extra +1];
390
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;",
398                               hexa ) == -1 )
399                     rtp_fmt->fmtp = NULL;
400             }
401             else
402             {
403                 char hexa[13];
404                 int i;
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 };
409
410                 for( i = 0; i < 15; i++ )
411                     if( p_fmt->audio.i_rate == aacsrates[i] )
412                         break;
413
414                 config[0]=0x40;
415                 config[1]=0;
416                 config[2]=0x20|i;
417                 config[3]=p_fmt->audio.i_channels<<4;
418                 config[4]=0x3f;
419                 config[5]=0xc0;
420
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;
427             }
428             break;
429         }
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;
434             break;
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;
439             break;
440         case VLC_CODEC_SPEEX:
441             rtp_fmt->ptname = "SPEEX";
442             rtp_fmt->pf_packetize = rtp_packetize_spx;
443             break;
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 )
448             {
449                 rtp_fmt->fmtp = NULL;
450                 char *config = rtp_xiph_b64_oob_config(p_fmt->p_extra,
451                                                        p_fmt->i_extra, NULL);
452                 if (config == NULL)
453                     break;
454                 if( asprintf( &rtp_fmt->fmtp,
455                               "configuration=%s;", config ) == -1 )
456                     rtp_fmt->fmtp = NULL;
457                 free(config);
458             }
459             break;
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 )
464             {
465                 rtp_fmt->fmtp = NULL;
466                 uint8_t pixel_fmt, c1, c2;
467                 char *config = rtp_xiph_b64_oob_config(p_fmt->p_extra,
468                                                        p_fmt->i_extra,
469                                                        &pixel_fmt);
470                 if (config == NULL)
471                     break;
472
473                 if (pixel_fmt == 1)
474                 {
475                     /* reserved */
476                     free(config);
477                     break;
478                 }
479                 switch (pixel_fmt)
480                 {
481                     case 0:
482                         c1 = 2;
483                         c2 = 0;
484                         break;
485                     case 2:
486                         c1 = c2 = 2;
487                         break;
488                     case 3:
489                         c1 = c2 = 4;
490                         break;
491                     default:
492                         assert(0);
493                 }
494
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,
500                               config ) == -1 )
501                     rtp_fmt->fmtp = NULL;
502                 free(config);
503             }
504             break;
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;
509             break;
510
511         default:
512             msg_Err( obj, "cannot add this stream (unsupported "
513                      "codec: %4.4s)", (char*)&p_fmt->i_codec );
514             return VLC_EGENERIC;
515     }
516
517     return VLC_SUCCESS;
518 }
519
520
521 static int
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 );
525
526 int rtp_packetize_xiph_config( sout_stream_id_t *id, const char *fmtp,
527                                int64_t i_pts )
528 {
529     if (fmtp == NULL)
530         return VLC_EGENERIC;
531
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, ';');
537     assert(end != NULL);
538     size_t len = end - start;
539     char b64[len + 1];
540     memcpy(b64, start, len);
541     b64[len] = '\0';
542
543     int     i_max   = rtp_mtu (id) - 6; /* payload max in one packet */
544
545     uint8_t *p_orig, *p_data;
546     int i_data;
547
548     i_data = vlc_b64_decode_binary(&p_orig, b64);
549     if (i_data == 0)
550         return VLC_EGENERIC;
551     assert(i_data > 9);
552     p_data = p_orig + 9;
553     i_data -= 9;
554
555     int i_count = ( i_data + i_max - 1 ) / i_max;
556
557     for( int i = 0; i < i_count; i++ )
558     {
559         int           i_payload = __MIN( i_max, i_data );
560         block_t *out = block_Alloc( 18 + i_payload );
561
562         unsigned fragtype, numpkts;
563         if (i_count == 1)
564         {
565             fragtype = 0;
566             numpkts = 1;
567         }
568         else
569         {
570             numpkts = 0;
571             if (i == 0)
572                 fragtype = 1;
573             else if (i == i_count - 1)
574                 fragtype = 3;
575             else
576                 fragtype = 2;
577         }
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;
581
582         /* rtp common header */
583         rtp_packetize_common( id, out, 0, i_pts );
584
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 );
588
589         out->i_buffer   = 18 + i_payload;
590         out->i_dts    = i_pts;
591
592         rtp_packetize_send( id, out );
593
594         p_data += i_payload;
595         i_data -= i_payload;
596     }
597
598     free(p_orig);
599
600     return VLC_SUCCESS;
601 }
602
603 /* rfc5215 */
604 static int rtp_packetize_xiph( sout_stream_id_t *id, block_t *in )
605 {
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;
608
609     uint8_t *p_data = in->p_buffer;
610     int     i_data  = in->i_buffer;
611
612     for( int i = 0; i < i_count; i++ )
613     {
614         int           i_payload = __MIN( i_max, i_data );
615         block_t *out = block_Alloc( 18 + i_payload );
616
617         unsigned fragtype, numpkts;
618         if (i_count == 1)
619         {
620             /* No fragmentation */
621             fragtype = 0;
622             numpkts = 1;
623         }
624         else
625         {
626             /* Fragmentation */
627             numpkts = 0;
628             if (i == 0)
629                 fragtype = 1;
630             else if (i == i_count - 1)
631                 fragtype = 3;
632             else
633                 fragtype = 2;
634         }
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;
638
639         /* rtp common header */
640         rtp_packetize_common( id, out, 0, in->i_pts);
641
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 );
645
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;
649
650         rtp_packetize_send( id, out );
651
652         p_data += i_payload;
653         i_data -= i_payload;
654     }
655
656     return VLC_SUCCESS;
657 }
658
659 static int rtp_packetize_mpa( sout_stream_id_t *id, block_t *in )
660 {
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;
663
664     uint8_t *p_data = in->p_buffer;
665     int     i_data  = in->i_buffer;
666     int     i;
667
668     for( i = 0; i < i_count; i++ )
669     {
670         int           i_payload = __MIN( i_max, i_data );
671         block_t *out = block_Alloc( 16 + i_payload );
672
673         /* rtp common header */
674         rtp_packetize_common( id, out, (i == i_count - 1)?1:0, in->i_pts );
675         /* mbz set to 0 */
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 );
680
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;
684
685         rtp_packetize_send( id, out );
686
687         p_data += i_payload;
688         i_data -= i_payload;
689     }
690
691     return VLC_SUCCESS;
692 }
693
694 /* rfc2250 */
695 static int rtp_packetize_mpv( sout_stream_id_t *id, block_t *in )
696 {
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;
699
700     uint8_t *p_data = in->p_buffer;
701     int     i_data  = in->i_buffer;
702     int     i;
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;
708
709     /* preparse this packet to get some info */
710     if( in->i_buffer > 4 )
711     {
712         uint8_t *p = p_data;
713         int      i_rest = in->i_buffer;
714
715         for( ;; )
716         {
717             while( i_rest > 4 &&
718                    ( p[0] != 0x00 || p[1] != 0x00 || p[2] != 0x01 ) )
719             {
720                 p++;
721                 i_rest--;
722             }
723             if( i_rest <= 4 )
724             {
725                 break;
726             }
727             p += 3;
728             i_rest -= 4;
729
730             if( *p == 0xb3 )
731             {
732                 /* sequence start code */
733                 b_sequence_start = 1;
734             }
735             else if( *p == 0x00 && i_rest >= 4 )
736             {
737                 /* picture */
738                 i_temporal_ref = ( p[1] << 2) |((p[2]>>6)&0x03);
739                 i_picture_coding_type = (p[2] >> 3)&0x07;
740
741                 if( i_rest >= 4 && ( i_picture_coding_type == 2 ||
742                                     i_picture_coding_type == 3 ) )
743                 {
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 )
747                     {
748                         i_fbv = (p[4]>>6)&0x01;
749                         i_bfc = (p[4]>>3)&0x07;
750                     }
751                 }
752             }
753             else if( *p <= 0xaf )
754             {
755                 b_start_slice = 1;
756             }
757         }
758     }
759
760     for( i = 0; i < i_count; i++ )
761     {
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;
771
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 );
775
776         SetDWBE( out->p_buffer + 12, h );
777
778         memcpy( &out->p_buffer[16], p_data, i_payload );
779
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;
783
784         rtp_packetize_send( id, out );
785
786         p_data += i_payload;
787         i_data -= i_payload;
788     }
789
790     return VLC_SUCCESS;
791 }
792
793 static int rtp_packetize_ac3( sout_stream_id_t *id, block_t *in )
794 {
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;
797
798     uint8_t *p_data = in->p_buffer;
799     int     i_data  = in->i_buffer;
800     int     i;
801
802     for( i = 0; i < i_count; i++ )
803     {
804         int           i_payload = __MIN( i_max, i_data );
805         block_t *out = block_Alloc( 14 + i_payload );
806
807         /* rtp common header */
808         rtp_packetize_common( id, out, (i == i_count - 1)?1:0, in->i_pts );
809         /* unit count */
810         out->p_buffer[12] = 1;
811         /* unit header */
812         out->p_buffer[13] = 0x00;
813         /* data */
814         memcpy( &out->p_buffer[14], p_data, i_payload );
815
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;
819
820         rtp_packetize_send( id, out );
821
822         p_data += i_payload;
823         i_data -= i_payload;
824     }
825
826     return VLC_SUCCESS;
827 }
828
829 static int rtp_packetize_split( sout_stream_id_t *id, block_t *in )
830 {
831     int     i_max   = rtp_mtu (id); /* payload max in one packet */
832     int     i_count = ( in->i_buffer + i_max - 1 ) / i_max;
833
834     uint8_t *p_data = in->p_buffer;
835     int     i_data  = in->i_buffer;
836     int     i;
837
838     for( i = 0; i < i_count; i++ )
839     {
840         int           i_payload = __MIN( i_max, i_data );
841         block_t *out = block_Alloc( 12 + i_payload );
842
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 );
847
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;
851
852         rtp_packetize_send( id, out );
853
854         p_data += i_payload;
855         i_data -= i_payload;
856     }
857
858     return VLC_SUCCESS;
859 }
860
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 )
863 {
864     int     i_max   = rtp_mtu (id); /* payload max in one packet */
865     int     i_count = ( in->i_buffer + i_max - 1 ) / i_max;
866
867     uint8_t *p_data = in->p_buffer;
868     int     i_data  = in->i_buffer;
869     int     i;
870
871     for( i = 0; i < i_count; i++ )
872     {
873         int           i_payload = __MIN( i_max, i_data );
874         block_t *out = block_Alloc( 12 + i_payload );
875
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 );
880
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;
884
885         rtp_packetize_send( id, out );
886
887         p_data += i_payload;
888         i_data -= i_payload;
889     }
890
891     return VLC_SUCCESS;
892 }
893
894 /* rfc3016 */
895 static int rtp_packetize_mp4a_latm( sout_stream_id_t *id, block_t *in )
896 {
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;
900
901     uint8_t *p_data = in->p_buffer, *p_header = NULL;
902     int     i_data  = in->i_buffer;
903     int     i;
904
905     for( i = 0; i < i_count; i++ )
906     {
907         int     i_payload = __MIN( i_max, i_data );
908         block_t *out;
909
910         if( i != 0 )
911             latmhdrsize = 0;
912         out = block_Alloc( 12 + latmhdrsize + i_payload );
913
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) );
917
918         if( i == 0 )
919         {
920             int tmp = in->i_buffer;
921
922             p_header=out->p_buffer+12;
923             while( tmp > 0xfe )
924             {
925                 *p_header = 0xff;
926                 p_header++;
927                 tmp -= 0xff;
928             }
929             *p_header = tmp;
930         }
931
932         memcpy( &out->p_buffer[12+latmhdrsize], p_data, i_payload );
933
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;
937
938         rtp_packetize_send( id, out );
939
940         p_data += i_payload;
941         i_data -= i_payload;
942     }
943
944     return VLC_SUCCESS;
945 }
946
947 static int rtp_packetize_mp4a( sout_stream_id_t *id, block_t *in )
948 {
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;
951
952     uint8_t *p_data = in->p_buffer;
953     int     i_data  = in->i_buffer;
954     int     i;
955
956     for( i = 0; i < i_count; i++ )
957     {
958         int           i_payload = __MIN( i_max, i_data );
959         block_t *out = block_Alloc( 16 + i_payload );
960
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) );
964         /* AU headers */
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 );
970
971         memcpy( &out->p_buffer[16], p_data, i_payload );
972
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;
976
977         rtp_packetize_send( id, out );
978
979         p_data += i_payload;
980         i_data -= i_payload;
981     }
982
983     return VLC_SUCCESS;
984 }
985
986
987 /* rfc2429 */
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 )
991 {
992     uint8_t *p_data = in->p_buffer;
993     int     i_data  = in->i_buffer;
994     int     i;
995     int     i_max   = rtp_mtu (id) - RTP_H263_HEADER_SIZE; /* payload max in one packet */
996     int     i_count;
997     int     b_p_bit;
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
1001     uint16_t h;
1002
1003     if( i_data < 2 )
1004     {
1005         return VLC_EGENERIC;
1006     }
1007     if( p_data[0] || p_data[1] )
1008     {
1009         return VLC_EGENERIC;
1010     }
1011     /* remove 2 leading 0 bytes */
1012     p_data += 2;
1013     i_data -= 2;
1014     i_count = ( i_data + i_max - 1 ) / i_max;
1015
1016     for( i = 0; i < i_count; i++ )
1017     {
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 )|
1022             ( b_v_bit << 9  )|
1023             ( i_plen  << 3  )|
1024               i_pebit;
1025
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 );
1030
1031         /* h263 header */
1032         SetWBE( out->p_buffer + 12, h );
1033         memcpy( &out->p_buffer[RTP_H263_PAYLOAD_START], p_data, i_payload );
1034
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;
1038
1039         rtp_packetize_send( id, out );
1040
1041         p_data += i_payload;
1042         i_data -= i_payload;
1043     }
1044
1045     return VLC_SUCCESS;
1046 }
1047
1048 /* rfc3984 */
1049 static int
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 )
1053 {
1054     const int i_max = rtp_mtu (id); /* payload max in one packet */
1055     int i_nal_hdr;
1056     int i_nal_type;
1057
1058     if( i_data < 5 )
1059         return VLC_SUCCESS;
1060
1061     i_nal_hdr = p_data[3];
1062     i_nal_type = i_nal_hdr&0x1f;
1063
1064     /* Skip start code */
1065     p_data += 3;
1066     i_data -= 3;
1067
1068     /* */
1069     if( i_data <= i_max )
1070     {
1071         /* Single NAL unit packet */
1072         block_t *out = block_Alloc( 12 + i_data );
1073         out->i_dts    = i_dts;
1074         out->i_length = i_length;
1075
1076         /* */
1077         rtp_packetize_common( id, out, b_last, i_pts );
1078         out->i_buffer = 12 + i_data;
1079
1080         memcpy( &out->p_buffer[12], p_data, i_data );
1081
1082         rtp_packetize_send( id, out );
1083     }
1084     else
1085     {
1086         /* FU-A Fragmentation Unit without interleaving */
1087         const int i_count = ( i_data-1 + i_max-2 - 1 ) / (i_max-2);
1088         int i;
1089
1090         p_data++;
1091         i_data--;
1092
1093         for( i = 0; i < i_count; i++ )
1094         {
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;
1099
1100             /* */
1101             rtp_packetize_common( id, out, (b_last && i_payload == i_data),
1102                                     i_pts );
1103             out->i_buffer = 14 + i_payload;
1104
1105             /* FU indicator */
1106             out->p_buffer[12] = 0x00 | (i_nal_hdr & 0x60) | 28;
1107             /* FU header */
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 );
1110
1111             rtp_packetize_send( id, out );
1112
1113             i_data -= i_payload;
1114             p_data += i_payload;
1115         }
1116     }
1117     return VLC_SUCCESS;
1118 }
1119
1120 static int rtp_packetize_h264( sout_stream_id_t *id, block_t *in )
1121 {
1122     const uint8_t *p_buffer = in->p_buffer;
1123     int i_buffer = in->i_buffer;
1124
1125     while( i_buffer > 4 && ( p_buffer[0] != 0 || p_buffer[1] != 0 || p_buffer[2] != 1 ) )
1126     {
1127         i_buffer--;
1128         p_buffer++;
1129     }
1130
1131     /* Split nal units */
1132     while( i_buffer > 4 )
1133     {
1134         int i_offset;
1135         int i_size = i_buffer;
1136         int i_skip = i_buffer;
1137
1138         /* search nal end */
1139         for( i_offset = 4; i_offset+2 < i_buffer ; i_offset++)
1140         {
1141             if( p_buffer[i_offset] == 0 && p_buffer[i_offset+1] == 0 && p_buffer[i_offset+2] == 1 )
1142             {
1143                 /* we found another startcode */
1144                 i_size = i_offset - ( p_buffer[i_offset-1] == 0 ? 1 : 0);
1145                 i_skip = i_offset;
1146                 break;
1147             }
1148         }
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 );
1153
1154         i_buffer -= i_skip;
1155         p_buffer += i_skip;
1156     }
1157     return VLC_SUCCESS;
1158 }
1159
1160 static int rtp_packetize_amr( sout_stream_id_t *id, block_t *in )
1161 {
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;
1164
1165     uint8_t *p_data = in->p_buffer;
1166     int     i_data  = in->i_buffer;
1167     int     i;
1168
1169     /* Only supports octet-aligned mode */
1170     for( i = 0; i < i_count; i++ )
1171     {
1172         int           i_payload = __MIN( i_max, i_data );
1173         block_t *out = block_Alloc( 14 + i_payload );
1174
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 */
1181
1182         /* FIXME: are we fed multiple frames ? */
1183         memcpy( &out->p_buffer[14], p_data+1, i_payload-1 );
1184
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;
1188
1189         rtp_packetize_send( id, out );
1190
1191         p_data += i_payload;
1192         i_data -= i_payload;
1193     }
1194
1195     return VLC_SUCCESS;
1196 }
1197
1198 static int rtp_packetize_t140( sout_stream_id_t *id, block_t *in )
1199 {
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;
1203
1204     for( unsigned i_packet = 0; i_data > 0; i_packet++ )
1205     {
1206         size_t i_payload = i_data;
1207
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 )
1211         {
1212             i_payload = i_max;
1213
1214             while( ( p_data[i_payload] & 0xC0 ) == 0x80 )
1215             {
1216                 if( i_payload == 0 )
1217                     return VLC_SUCCESS; /* fishy input! */
1218
1219                 i_payload--;
1220             }
1221         }
1222
1223         block_t *out = block_Alloc( 12 + i_payload );
1224         if( out == NULL )
1225             return VLC_SUCCESS;
1226
1227         rtp_packetize_common( id, out, 0, in->i_pts + i_packet );
1228         memcpy( out->p_buffer + 12, p_data, i_payload );
1229
1230         out->i_buffer = 12 + i_payload;
1231         out->i_dts    = out->i_pts;
1232         out->i_length = 0;
1233
1234         rtp_packetize_send( id, out );
1235
1236         p_data += i_payload;
1237         i_data -= i_payload;
1238     }
1239
1240     return VLC_SUCCESS;
1241 }
1242
1243
1244 static int rtp_packetize_spx( sout_stream_id_t *id, block_t *in )
1245 {
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;
1250     block_t *p_out;
1251
1252     if ( in->i_buffer > rtp_mtu (id) )
1253         return VLC_SUCCESS;
1254
1255     /*
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. 
1259
1260       This MAY be overkill since packetization is handled elsewhere and 
1261       appears to ensure the octet boundary. However, better safe than
1262       sorry.
1263     */
1264     if ( i_payload_size % 4 )
1265     {
1266         i_payload_padding = 4 - ( i_payload_size % 4 );
1267         i_payload_size += i_payload_padding;
1268     }
1269
1270     /*
1271       Allocate a new RTP p_output block of the appropriate size. 
1272       Allow for 12 extra bytes of RTP header. 
1273     */
1274     p_out = block_Alloc( 12 + i_payload_size );
1275
1276     if ( i_payload_padding )
1277     {
1278     /*
1279       The padding is required to be a zero followed by all 1s.
1280     */
1281         char c_first_pad, c_remaining_pad;
1282         c_first_pad = 0x7F;
1283         c_remaining_pad = 0xFF;
1284
1285         /*
1286           Allow for 12 bytes before the i_data_size because
1287           of the expected RTP header added during
1288           rtp_packetize_common.
1289         */
1290         p_out->p_buffer[12 + i_data_size] = c_first_pad; 
1291         switch (i_payload_padding)
1292         {
1293           case 2:
1294             p_out->p_buffer[12 + i_data_size + 1] = c_remaining_pad; 
1295             break;
1296           case 3:
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; 
1299             break;
1300         }
1301     }
1302
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 );
1308
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;
1312
1313     /* Queue the buffer for actual transmission. */
1314     rtp_packetize_send( id, p_out );
1315     return VLC_SUCCESS;
1316 }
1317
1318 static int rtp_packetize_g726( sout_stream_id_t *id, block_t *in, int i_pad )
1319 {
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;
1322
1323     uint8_t *p_data = in->p_buffer;
1324     int     i_data  = in->i_buffer;
1325     int     i_packet = 0;
1326
1327     while( i_data > 0 )
1328     {
1329         int           i_payload = __MIN( i_max, i_data );
1330         block_t *out = block_New( p_stream, 12 + i_payload );
1331
1332         /* rtp common header */
1333         rtp_packetize_common( id, out, 0,
1334                       (in->i_pts > VLC_TS_INVALID ? in->i_pts : in->i_dts) );
1335
1336         memcpy( &out->p_buffer[12], p_data, i_payload );
1337
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;
1341
1342         rtp_packetize_send( id, out );
1343
1344         p_data += i_payload;
1345         i_data -= i_payload;
1346     }
1347     return VLC_SUCCESS;
1348 }
1349
1350 static int rtp_packetize_g726_16( sout_stream_id_t *id, block_t *in )
1351 {
1352     return rtp_packetize_g726( id, in, 4 );
1353 }
1354
1355 static int rtp_packetize_g726_24( sout_stream_id_t *id, block_t *in )
1356 {
1357     return rtp_packetize_g726( id, in, 8 );
1358 }
1359
1360 static int rtp_packetize_g726_32( sout_stream_id_t *id, block_t *in )
1361 {
1362     return rtp_packetize_g726( id, in, 2 );
1363 }
1364
1365 static int rtp_packetize_g726_40( sout_stream_id_t *id, block_t *in )
1366 {
1367     return rtp_packetize_g726( id, in, 8 );
1368 }