]> git.sesse.net Git - vlc/blob - modules/stream_out/rtpfmt.c
Don't include config.h from the headers - refs #297.
[vlc] / modules / stream_out / rtpfmt.c
1 /*****************************************************************************
2  * rtp.c: rtp stream output module
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/vlc.h>
30 #include <vlc_sout.h>
31 #include <vlc_block.h>
32
33 #include "rtp.h"
34
35 int rtp_packetize_mpa( sout_stream_t *p_stream, sout_stream_id_t *id,
36                        block_t *in )
37 {
38     int     i_max   = rtp_mtu (id) - 4; /* payload max in one packet */
39     int     i_count = ( in->i_buffer + i_max - 1 ) / i_max;
40
41     uint8_t *p_data = in->p_buffer;
42     int     i_data  = in->i_buffer;
43     int     i;
44
45     for( i = 0; i < i_count; i++ )
46     {
47         int           i_payload = __MIN( i_max, i_data );
48         block_t *out = block_New( p_stream, 16 + i_payload );
49
50         /* rtp common header */
51         rtp_packetize_common( id, out, (i == i_count - 1)?1:0, in->i_pts );
52         /* mbz set to 0 */
53         out->p_buffer[12] = 0;
54         out->p_buffer[13] = 0;
55         /* fragment offset in the current frame */
56         out->p_buffer[14] = ( (i*i_max) >> 8 )&0xff;
57         out->p_buffer[15] = ( (i*i_max)      )&0xff;
58         memcpy( &out->p_buffer[16], p_data, i_payload );
59
60         out->i_buffer   = 16 + i_payload;
61         out->i_dts    = in->i_dts + i * in->i_length / i_count;
62         out->i_length = in->i_length / i_count;
63
64         rtp_packetize_send( id, out );
65
66         p_data += i_payload;
67         i_data -= i_payload;
68     }
69
70     return VLC_SUCCESS;
71 }
72
73 /* rfc2250 */
74 int rtp_packetize_mpv( sout_stream_t *p_stream, sout_stream_id_t *id,
75                        block_t *in )
76 {
77     int     i_max   = rtp_mtu (id) - 4; /* payload max in one packet */
78     int     i_count = ( in->i_buffer + i_max - 1 ) / i_max;
79
80     uint8_t *p_data = in->p_buffer;
81     int     i_data  = in->i_buffer;
82     int     i;
83     int     b_sequence_start = 0;
84     int     i_temporal_ref = 0;
85     int     i_picture_coding_type = 0;
86     int     i_fbv = 0, i_bfc = 0, i_ffv = 0, i_ffc = 0;
87     int     b_start_slice = 0;
88
89     /* preparse this packet to get some info */
90     if( in->i_buffer > 4 )
91     {
92         uint8_t *p = p_data;
93         int      i_rest = in->i_buffer;
94
95         for( ;; )
96         {
97             while( i_rest > 4 &&
98                    ( p[0] != 0x00 || p[1] != 0x00 || p[2] != 0x01 ) )
99             {
100                 p++;
101                 i_rest--;
102             }
103             if( i_rest <= 4 )
104             {
105                 break;
106             }
107             p += 3;
108             i_rest -= 4;
109
110             if( *p == 0xb3 )
111             {
112                 /* sequence start code */
113                 b_sequence_start = 1;
114             }
115             else if( *p == 0x00 && i_rest >= 4 )
116             {
117                 /* picture */
118                 i_temporal_ref = ( p[1] << 2) |((p[2]>>6)&0x03);
119                 i_picture_coding_type = (p[2] >> 3)&0x07;
120
121                 if( i_rest >= 4 && ( i_picture_coding_type == 2 ||
122                                     i_picture_coding_type == 3 ) )
123                 {
124                     i_ffv = (p[3] >> 2)&0x01;
125                     i_ffc = ((p[3]&0x03) << 1)|((p[4]>>7)&0x01);
126                     if( i_rest > 4 && i_picture_coding_type == 3 )
127                     {
128                         i_fbv = (p[4]>>6)&0x01;
129                         i_bfc = (p[4]>>3)&0x07;
130                     }
131                 }
132             }
133             else if( *p <= 0xaf )
134             {
135                 b_start_slice = 1;
136             }
137         }
138     }
139
140     for( i = 0; i < i_count; i++ )
141     {
142         int           i_payload = __MIN( i_max, i_data );
143         block_t *out = block_New( p_stream,
144                                              16 + i_payload );
145         uint32_t      h = ( i_temporal_ref << 16 )|
146                           ( b_sequence_start << 13 )|
147                           ( b_start_slice << 12 )|
148                           ( i == i_count - 1 ? 1 << 11 : 0 )|
149                           ( i_picture_coding_type << 8 )|
150                           ( i_fbv << 7 )|( i_bfc << 4 )|( i_ffv << 3 )|i_ffc;
151
152         /* rtp common header */
153         rtp_packetize_common( id, out, (i == i_count - 1)?1:0,
154                               in->i_pts > 0 ? in->i_pts : in->i_dts );
155
156         /* 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 */
157         out->p_buffer[12] = ( h >> 24 )&0xff;
158         out->p_buffer[13] = ( h >> 16 )&0xff;
159         out->p_buffer[14] = ( h >>  8 )&0xff;
160         out->p_buffer[15] = ( h       )&0xff;
161
162         memcpy( &out->p_buffer[16], p_data, i_payload );
163
164         out->i_buffer   = 16 + i_payload;
165         out->i_dts    = in->i_dts + i * in->i_length / i_count;
166         out->i_length = in->i_length / i_count;
167
168         rtp_packetize_send( id, out );
169
170         p_data += i_payload;
171         i_data -= i_payload;
172     }
173
174     return VLC_SUCCESS;
175 }
176
177 int rtp_packetize_ac3( sout_stream_t *p_stream, sout_stream_id_t *id,
178                        block_t *in )
179 {
180     int     i_max   = rtp_mtu (id) - 2; /* payload max in one packet */
181     int     i_count = ( in->i_buffer + i_max - 1 ) / i_max;
182
183     uint8_t *p_data = in->p_buffer;
184     int     i_data  = in->i_buffer;
185     int     i;
186
187     for( i = 0; i < i_count; i++ )
188     {
189         int           i_payload = __MIN( i_max, i_data );
190         block_t *out = block_New( p_stream, 14 + i_payload );
191
192         /* rtp common header */
193         rtp_packetize_common( id, out, (i == i_count - 1)?1:0, in->i_pts );
194         /* unit count */
195         out->p_buffer[12] = 1;
196         /* unit header */
197         out->p_buffer[13] = 0x00;
198         /* data */
199         memcpy( &out->p_buffer[14], p_data, i_payload );
200
201         out->i_buffer   = 14 + i_payload;
202         out->i_dts    = in->i_dts + i * in->i_length / i_count;
203         out->i_length = in->i_length / i_count;
204
205         rtp_packetize_send( id, out );
206
207         p_data += i_payload;
208         i_data -= i_payload;
209     }
210
211     return VLC_SUCCESS;
212 }
213
214 int rtp_packetize_split( sout_stream_t *p_stream, sout_stream_id_t *id,
215                          block_t *in )
216 {
217     int     i_max   = rtp_mtu (id); /* payload max in one packet */
218     int     i_count = ( in->i_buffer + i_max - 1 ) / i_max;
219
220     uint8_t *p_data = in->p_buffer;
221     int     i_data  = in->i_buffer;
222     int     i;
223
224     for( i = 0; i < i_count; i++ )
225     {
226         int           i_payload = __MIN( i_max, i_data );
227         block_t *out = block_New( p_stream, 12 + i_payload );
228
229         /* rtp common header */
230         rtp_packetize_common( id, out, (i == i_count - 1),
231                               (in->i_pts > 0 ? in->i_pts : in->i_dts) );
232         memcpy( &out->p_buffer[12], p_data, i_payload );
233
234         out->i_buffer   = 12 + i_payload;
235         out->i_dts    = in->i_dts + i * in->i_length / i_count;
236         out->i_length = in->i_length / i_count;
237
238         rtp_packetize_send( id, out );
239
240         p_data += i_payload;
241         i_data -= i_payload;
242     }
243
244     return VLC_SUCCESS;
245 }
246
247 /* rfc3016 */
248 int rtp_packetize_mp4a_latm( sout_stream_t *p_stream, sout_stream_id_t *id,
249                              block_t *in )
250 {
251     int     i_max   = rtp_mtu (id) - 2;              /* payload max in one packet */
252     int     latmhdrsize = in->i_buffer / 0xff + 1;
253     int     i_count = ( in->i_buffer + i_max - 1 ) / i_max;
254
255     uint8_t *p_data = in->p_buffer, *p_header = NULL;
256     int     i_data  = in->i_buffer;
257     int     i;
258
259     for( i = 0; i < i_count; i++ )
260     {
261         int     i_payload = __MIN( i_max, i_data );
262         block_t *out;
263
264         if( i != 0 )
265             latmhdrsize = 0;
266         out = block_New( p_stream, 12 + latmhdrsize + i_payload );
267
268         /* rtp common header */
269         rtp_packetize_common( id, out, ((i == i_count - 1) ? 1 : 0),
270                               (in->i_pts > 0 ? in->i_pts : in->i_dts) );
271
272         if( i == 0 )
273         {
274             int tmp = in->i_buffer;
275
276             p_header=out->p_buffer+12;
277             while( tmp > 0xfe )
278             {
279                 *p_header = 0xff;
280                 p_header++;
281                 tmp -= 0xff;
282             }
283             *p_header = tmp;
284         }
285
286         memcpy( &out->p_buffer[12+latmhdrsize], p_data, i_payload );
287
288         out->i_buffer   = 12 + latmhdrsize + i_payload;
289         out->i_dts    = in->i_dts + i * in->i_length / i_count;
290         out->i_length = in->i_length / i_count;
291
292         rtp_packetize_send( id, out );
293
294         p_data += i_payload;
295         i_data -= i_payload;
296     }
297
298     return VLC_SUCCESS;
299 }
300
301 int rtp_packetize_l16( sout_stream_t *p_stream, sout_stream_id_t *id,
302                        block_t *in )
303 {
304     const uint8_t *p_data = in->p_buffer;
305     size_t i_data  = in->i_buffer;
306     size_t i_plen = 2 * rtp_plen (id, 20);
307
308     for( unsigned i_packet = 0; i_data > 0; i_packet++ )
309     {
310         int           i_payload = __MIN( i_plen, i_data );
311         block_t *out = block_New( p_stream, 12 + i_payload );
312
313         /* rtp common header */
314         rtp_packetize_common( id, out, 0,
315                               (in->i_pts > 0 ? in->i_pts : in->i_dts) );
316         memcpy( &out->p_buffer[12], p_data, i_payload );
317
318         out->i_buffer = 12 + i_payload;
319         out->i_dts    = in->i_dts + i_packet * 20000;
320         out->i_length = i_payload * 20000 / i_plen;
321
322         rtp_packetize_send( id, out );
323
324         p_data += i_payload;
325         i_data -= i_payload;
326     }
327
328     return VLC_SUCCESS;
329 }
330
331 int rtp_packetize_l8( sout_stream_t *p_stream, sout_stream_id_t *id,
332                       block_t *in )
333 {
334     const uint8_t *p_data = in->p_buffer;
335     size_t i_data  = in->i_buffer;
336     size_t i_plen = rtp_plen (id, 20);
337
338     for( unsigned i_packet = 0; i_data > 0; i_packet++ )
339     {
340         int           i_payload = __MIN( i_plen, i_data );
341         block_t *out = block_New( p_stream, 12 + i_payload );
342
343         /* rtp common header */
344         rtp_packetize_common( id, out, 0,
345                               (in->i_pts > 0 ? in->i_pts : in->i_dts) );
346         memcpy( &out->p_buffer[12], p_data, i_payload );
347
348         out->i_buffer = 12 + i_payload;
349         out->i_dts    = in->i_dts + i_packet * 20000;
350         out->i_length = i_payload * 20000 / i_plen;
351
352         rtp_packetize_send( id, out );
353
354         p_data += i_payload;
355         i_data -= i_payload;
356     }
357
358     return VLC_SUCCESS;
359 }
360
361 int rtp_packetize_mp4a( sout_stream_t *p_stream, sout_stream_id_t *id,
362                         block_t *in )
363 {
364     int     i_max   = rtp_mtu (id) - 4; /* payload max in one packet */
365     int     i_count = ( in->i_buffer + i_max - 1 ) / i_max;
366
367     uint8_t *p_data = in->p_buffer;
368     int     i_data  = in->i_buffer;
369     int     i;
370
371     for( i = 0; i < i_count; i++ )
372     {
373         int           i_payload = __MIN( i_max, i_data );
374         block_t *out = block_New( p_stream, 16 + i_payload );
375
376         /* rtp common header */
377         rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
378                               (in->i_pts > 0 ? in->i_pts : in->i_dts) );
379         /* AU headers */
380         /* AU headers length (bits) */
381         out->p_buffer[12] = 0;
382         out->p_buffer[13] = 2*8;
383         /* for each AU length 13 bits + idx 3bits, */
384         out->p_buffer[14] = ( in->i_buffer >> 5 )&0xff;
385         out->p_buffer[15] = ( (in->i_buffer&0xff)<<3 )|0;
386
387         memcpy( &out->p_buffer[16], p_data, i_payload );
388
389         out->i_buffer   = 16 + i_payload;
390         out->i_dts    = in->i_dts + i * in->i_length / i_count;
391         out->i_length = in->i_length / i_count;
392
393         rtp_packetize_send( id, out );
394
395         p_data += i_payload;
396         i_data -= i_payload;
397     }
398
399     return VLC_SUCCESS;
400 }
401
402
403 /* rfc2429 */
404 #define RTP_H263_HEADER_SIZE (2)  // plen = 0
405 #define RTP_H263_PAYLOAD_START (14)  // plen = 0
406 int rtp_packetize_h263( sout_stream_t *p_stream, sout_stream_id_t *id,
407                         block_t *in )
408 {
409     uint8_t *p_data = in->p_buffer;
410     int     i_data  = in->i_buffer;
411     int     i;
412     int     i_max   = rtp_mtu (id) - RTP_H263_HEADER_SIZE; /* payload max in one packet */
413     int     i_count;
414     int     b_p_bit;
415     int     b_v_bit = 0; // no pesky error resilience
416     int     i_plen = 0; // normally plen=0 for PSC packet
417     int     i_pebit = 0; // because plen=0
418     uint16_t h;
419
420     if( i_data < 2 )
421     {
422         return VLC_EGENERIC;
423     }
424     if( p_data[0] || p_data[1] )
425     {
426         return VLC_EGENERIC;
427     }
428     /* remove 2 leading 0 bytes */
429     p_data += 2;
430     i_data -= 2;
431     i_count = ( i_data + i_max - 1 ) / i_max;
432
433     for( i = 0; i < i_count; i++ )
434     {
435         int      i_payload = __MIN( i_max, i_data );
436         block_t *out = block_New( p_stream,
437                                   RTP_H263_PAYLOAD_START + i_payload );
438         b_p_bit = (i == 0) ? 1 : 0;
439         h = ( b_p_bit << 10 )|
440             ( b_v_bit << 9  )|
441             ( i_plen  << 3  )|
442               i_pebit;
443
444         /* rtp common header */
445         //b_m_bit = 1; // always contains end of frame
446         rtp_packetize_common( id, out, (i == i_count - 1)?1:0,
447                               in->i_pts > 0 ? in->i_pts : in->i_dts );
448
449         /* h263 header */
450         out->p_buffer[12] = ( h >>  8 )&0xff;
451         out->p_buffer[13] = ( h       )&0xff;
452         memcpy( &out->p_buffer[RTP_H263_PAYLOAD_START], p_data, i_payload );
453
454         out->i_buffer = RTP_H263_PAYLOAD_START + i_payload;
455         out->i_dts    = in->i_dts + i * in->i_length / i_count;
456         out->i_length = in->i_length / i_count;
457
458         rtp_packetize_send( id, out );
459
460         p_data += i_payload;
461         i_data -= i_payload;
462     }
463
464     return VLC_SUCCESS;
465 }
466
467 /* rfc3984 */
468 int
469 rtp_packetize_h264_nal( sout_stream_t *p_stream, sout_stream_id_t *id,
470                         const uint8_t *p_data, int i_data, int64_t i_pts,
471                         int64_t i_dts, vlc_bool_t b_last, int64_t i_length )
472 {
473     const int i_max = rtp_mtu (id); /* payload max in one packet */
474     int i_nal_hdr;
475     int i_nal_type;
476
477     if( i_data < 5 )
478         return VLC_SUCCESS;
479
480     i_nal_hdr = p_data[3];
481     i_nal_type = i_nal_hdr&0x1f;
482
483     /* Skip start code */
484     p_data += 3;
485     i_data -= 3;
486
487     /* */
488     if( i_data <= i_max )
489     {
490         /* Single NAL unit packet */
491         block_t *out = block_New( p_stream, 12 + i_data );
492         out->i_dts    = i_dts;
493         out->i_length = i_length;
494
495         /* */
496         rtp_packetize_common( id, out, b_last, i_pts );
497         out->i_buffer = 12 + i_data;
498
499         memcpy( &out->p_buffer[12], p_data, i_data );
500
501         rtp_packetize_send( id, out );
502     }
503     else
504     {
505         /* FU-A Fragmentation Unit without interleaving */
506         const int i_count = ( i_data-1 + i_max-2 - 1 ) / (i_max-2);
507         int i;
508
509         p_data++;
510         i_data--;
511
512         for( i = 0; i < i_count; i++ )
513         {
514             const int i_payload = __MIN( i_data, i_max-2 );
515             block_t *out = block_New( p_stream, 12 + 2 + i_payload );
516             out->i_dts    = i_dts + i * i_length / i_count;
517             out->i_length = i_length / i_count;
518
519             /* */
520             rtp_packetize_common( id, out, (b_last && i_payload == i_data), i_pts );
521             out->i_buffer = 14 + i_payload;
522
523             /* FU indicator */
524             out->p_buffer[12] = 0x00 | (i_nal_hdr & 0x60) | 28;
525             /* FU header */
526             out->p_buffer[13] = ( i == 0 ? 0x80 : 0x00 ) | ( (i == i_count-1) ? 0x40 : 0x00 )  | i_nal_type;
527             memcpy( &out->p_buffer[14], p_data, i_payload );
528
529             rtp_packetize_send( id, out );
530
531             i_data -= i_payload;
532             p_data += i_payload;
533         }
534     }
535     return VLC_SUCCESS;
536 }
537
538 int rtp_packetize_h264( sout_stream_t *p_stream, sout_stream_id_t *id,
539                         block_t *in )
540 {
541     const uint8_t *p_buffer = in->p_buffer;
542     int i_buffer = in->i_buffer;
543
544     while( i_buffer > 4 && ( p_buffer[0] != 0 || p_buffer[1] != 0 || p_buffer[2] != 1 ) )
545     {
546         i_buffer--;
547         p_buffer++;
548     }
549
550     /* Split nal units */
551     while( i_buffer > 4 )
552     {
553         int i_offset;
554         int i_size = i_buffer;
555         int i_skip = i_buffer;
556
557         /* search nal end */
558         for( i_offset = 4; i_offset+2 < i_buffer ; i_offset++)
559         {
560             if( p_buffer[i_offset] == 0 && p_buffer[i_offset+1] == 0 && p_buffer[i_offset+2] == 1 )
561             {
562                 /* we found another startcode */
563                 i_size = i_offset - ( p_buffer[i_offset-1] == 0 ? 1 : 0);
564                 i_skip = i_offset;
565                 break;
566             }
567         }
568         /* TODO add STAP-A to remove a lot of overhead with small slice/sei/... */
569         rtp_packetize_h264_nal( p_stream, id, p_buffer, i_size,
570                                 (in->i_pts > 0 ? in->i_pts : in->i_dts), in->i_dts,
571                                 (i_size >= i_buffer), in->i_length * i_size / in->i_buffer );
572
573         i_buffer -= i_skip;
574         p_buffer += i_skip;
575     }
576     return VLC_SUCCESS;
577 }
578
579 int rtp_packetize_amr( sout_stream_t *p_stream, sout_stream_id_t *id,
580                        block_t *in )
581 {
582     int     i_max   = rtp_mtu (id) - 2; /* payload max in one packet */
583     int     i_count = ( in->i_buffer + i_max - 1 ) / i_max;
584
585     uint8_t *p_data = in->p_buffer;
586     int     i_data  = in->i_buffer;
587     int     i;
588
589     /* Only supports octet-aligned mode */
590     for( i = 0; i < i_count; i++ )
591     {
592         int           i_payload = __MIN( i_max, i_data );
593         block_t *out = block_New( p_stream, 14 + i_payload );
594
595         /* rtp common header */
596         rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
597                               (in->i_pts > 0 ? in->i_pts : in->i_dts) );
598         /* Payload header */
599         out->p_buffer[12] = 0xF0; /* CMR */
600         out->p_buffer[13] = p_data[0]&0x7C; /* ToC */ /* FIXME: frame type */
601
602         /* FIXME: are we fed multiple frames ? */
603         memcpy( &out->p_buffer[14], p_data+1, i_payload-1 );
604
605         out->i_buffer   = 14 + i_payload-1;
606         out->i_dts    = in->i_dts + i * in->i_length / i_count;
607         out->i_length = in->i_length / i_count;
608
609         rtp_packetize_send( id, out );
610
611         p_data += i_payload;
612         i_data -= i_payload;
613     }
614
615     return VLC_SUCCESS;
616 }
617
618 int rtp_packetize_t140( sout_stream_t *p_stream, sout_stream_id_t *id,
619                         block_t *in )
620 {
621     const size_t   i_max  = rtp_mtu (id);
622     const uint8_t *p_data = in->p_buffer;
623     size_t         i_data = in->i_buffer;
624
625     for( unsigned i_packet = 0; i_data > 0; i_packet++ )
626     {
627         size_t i_payload = i_data;
628
629         /* Make sure we stop on an UTF-8 character boundary
630          * (assuming the input is valid UTF-8) */
631         if( i_data > i_max )
632         {
633             i_payload = i_max;
634
635             while( ( p_data[i_payload] & 0xC0 ) == 0x80 )
636             {
637                 if( i_payload == 0 )
638                     return VLC_SUCCESS; /* fishy input! */
639
640                 i_payload--;
641             }
642         }
643
644         block_t *out = block_New( p_stream, 12 + i_payload );
645         if( out == NULL )
646             return VLC_SUCCESS;
647
648         rtp_packetize_common( id, out, 0, in->i_pts + i_packet );
649         memcpy( out->p_buffer + 12, p_data, i_payload );
650
651         out->i_buffer = 12 + i_payload;
652         out->i_dts    = out->i_pts;
653         out->i_length = 0;
654
655         rtp_packetize_send( id, out );
656
657         p_data += i_payload;
658         i_data -= i_payload;
659     }
660
661     return VLC_SUCCESS;
662 }
663
664
665 int rtp_packetize_spx( sout_stream_t *p_stream, sout_stream_id_t *id,
666                        block_t *in )
667 {
668     uint8_t *p_buffer = in->p_buffer;
669     int i_data_size, i_payload_size, i_payload_padding;
670     i_data_size = i_payload_size = in->i_buffer;
671     i_payload_padding = 0;
672     block_t *p_out;
673
674     if ( in->i_buffer > rtp_mtu (id) )
675     {
676         msg_Warn( p_stream, "Cannot send packet larger than output MTU" );
677         return VLC_SUCCESS;
678     }
679
680     /*
681       RFC for Speex in RTP says that each packet must end on an octet 
682       boundary. So, we check to see if the number of bytes % 4 is zero.
683       If not, we have to add some padding. 
684
685       This MAY be overkill since packetization is handled elsewhere and 
686       appears to ensure the octet boundary. However, better safe than
687       sorry.
688     */
689     if ( i_payload_size % 4 )
690     {
691         i_payload_padding = 4 - ( i_payload_size % 4 );
692         i_payload_size += i_payload_padding;
693     }
694
695     /*
696       Allocate a new RTP p_output block of the appropriate size. 
697       Allow for 12 extra bytes of RTP header. 
698     */
699     p_out = block_New( p_stream, 12 + i_payload_size );
700
701     if ( i_payload_padding )
702     {
703     /*
704       The padding is required to be a zero followed by all 1s.
705     */
706         char c_first_pad, c_remaining_pad;
707         c_first_pad = 0x7F;
708         c_remaining_pad = 0xFF;
709
710         /*
711           Allow for 12 bytes before the i_data_size because
712           of the expected RTP header added during
713           rtp_packetize_common.
714         */
715         p_out->p_buffer[12 + i_data_size] = c_first_pad; 
716         switch (i_payload_padding)
717         {
718           case 2:
719             p_out->p_buffer[12 + i_data_size + 1] = c_remaining_pad; 
720             break;
721           case 3:
722             p_out->p_buffer[12 + i_data_size + 1] = c_remaining_pad; 
723             p_out->p_buffer[12 + i_data_size + 2] = c_remaining_pad; 
724             break;
725         }
726     }
727
728     /* Add the RTP header to our p_output buffer. */
729     rtp_packetize_common( id, p_out, 0, (in->i_pts > 0 ? in->i_pts : in->i_dts) );
730     /* Copy the Speex payload to the p_output buffer */
731     memcpy( &p_out->p_buffer[12], p_buffer, i_data_size );
732
733     p_out->i_buffer = 12 + i_payload_size;
734     p_out->i_dts = in->i_dts;
735     p_out->i_length = in->i_length;
736
737     /* Queue the buffer for actual transmission. */
738     rtp_packetize_send( id, p_out );
739     return VLC_SUCCESS;
740 }