* asf: more stuff (bitrate and time).
[vlc.git] / modules / mux / asf.c
1 /*****************************************************************************
2  * asf.c
3  *****************************************************************************
4  * Copyright (C) 2003 VideoLAN
5  * $Id: asf.c,v 1.2 2003/08/24 22:20:43 fenrir Exp $
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>
28
29 #include <vlc/vlc.h>
30 #include <vlc/input.h>
31 #include <vlc/sout.h>
32
33 #include "codecs.h"
34
35 /*****************************************************************************
36  * Module descriptor
37  *****************************************************************************/
38 static int  Open   ( vlc_object_t * );
39 static void Close  ( vlc_object_t * );
40
41 vlc_module_begin();
42     set_description( _("Asf muxer") );
43     set_capability( "sout mux", 5 );
44     add_shortcut( "asf" );
45     set_callbacks( Open, Close );
46 vlc_module_end();
47
48 /*****************************************************************************
49  * Locales prototypes
50  *****************************************************************************/
51 static int  Capability(sout_mux_t *, int, void *, void * );
52 static int  AddStream( sout_mux_t *, sout_input_t * );
53 static int  DelStream( sout_mux_t *, sout_input_t * );
54 static int  Mux      ( sout_mux_t * );
55
56 typedef struct
57 {
58     uint32_t v1; /* le */
59     uint16_t v2; /* le */
60     uint16_t v3; /* le */
61     uint8_t  v4[8];
62 } guid_t;
63
64 typedef struct
65 {
66     int     i_id;
67     int     i_cat;
68     int     i_sequence;
69
70     int     i_extra;
71     uint8_t *p_extra;
72 } asf_track_t;
73
74 struct sout_mux_sys_t
75 {
76     guid_t          fid;    /* file id */
77     int             i_packet_size;
78     int64_t         i_packet_count;
79     mtime_t         i_dts_first;
80     mtime_t         i_dts_last;
81     int64_t         i_bitrate;
82
83     int             i_track;
84     asf_track_t     track[128];
85
86     vlc_bool_t      b_write_header;
87
88     sout_buffer_t   *pk;
89     int             i_pk_used;
90     int             i_pk_frame;
91     mtime_t         i_pk_dts;
92 };
93
94 static int MuxGetStream( sout_mux_t *, int *pi_stream, mtime_t *pi_dts );
95
96 static sout_buffer_t *asf_header_create( sout_mux_t *, vlc_bool_t b_broadcast );
97 static sout_buffer_t *asf_packet_create( sout_mux_t *,
98                                          asf_track_t *, sout_buffer_t * );
99
100 typedef struct
101 {
102     int      i_buffer_size;
103     int      i_buffer;
104     uint8_t  *p_buffer;
105 } bo_t;
106
107 static void bo_init     ( bo_t *, uint8_t *, int  );
108 static void bo_add_u8   ( bo_t *, uint8_t  );
109 static void bo_addle_u16( bo_t *, uint16_t );
110 static void bo_addle_u32( bo_t *, uint32_t );
111 static void bo_addle_u64( bo_t *, uint64_t );
112 static void bo_add_mem  ( bo_t *, uint8_t *, int );
113
114
115 /*****************************************************************************
116  * Open:
117  *****************************************************************************/
118 static int Open( vlc_object_t *p_this )
119 {
120     sout_mux_t     *p_mux = (sout_mux_t*)p_this;
121     sout_mux_sys_t *p_sys;
122     int i;
123
124     msg_Dbg( p_mux, "Asf muxer opened" );
125
126     p_mux->pf_capacity  = Capability;
127     p_mux->pf_addstream = AddStream;
128     p_mux->pf_delstream = DelStream;
129     p_mux->pf_mux       = Mux;
130
131     p_mux->p_sys = p_sys = malloc( sizeof( sout_mux_sys_t ) );
132     p_sys->pk = NULL;
133     p_sys->i_pk_used    = 0;
134     p_sys->i_pk_frame   = 0;
135     p_sys->i_dts_first  = -1;
136     p_sys->i_dts_last   = 0;
137     p_sys->i_bitrate    = 0;
138
139     p_sys->b_write_header = VLC_TRUE;
140     p_sys->i_track = 1;
141     p_sys->i_packet_size = 4096;
142     p_sys->i_packet_count= 0;
143     /* generate a random fid */
144     srand( mdate() & 0xffffffff );
145     p_sys->fid.v1 = 0xbabac001;
146     p_sys->fid.v2 = ( (uint64_t)rand() << 16 ) / RAND_MAX;
147     p_sys->fid.v3 = ( (uint64_t)rand() << 16 ) / RAND_MAX;
148     for( i = 0; i < 8; i++ )
149     {
150         p_sys->fid.v4[i] = ( (uint64_t)rand() << 8 ) / RAND_MAX;
151     }
152
153     return VLC_SUCCESS;
154 }
155
156 /*****************************************************************************
157  * Close:
158  *****************************************************************************/
159 static void Close( vlc_object_t * p_this )
160 {
161     sout_mux_t     *p_mux = (sout_mux_t*)p_this;
162     sout_mux_sys_t *p_sys = p_mux->p_sys;
163     int i;
164
165     msg_Dbg( p_mux, "Asf muxer closed" );
166
167     /* rewrite header */
168     if( !sout_AccessOutSeek( p_mux->p_access, 0 ) )
169     {
170         sout_buffer_t *h = asf_header_create( p_mux, VLC_FALSE );
171         sout_AccessOutWrite( p_mux->p_access, h );
172     }
173
174     for( i = 1; i < p_sys->i_track; i++ )
175     {
176         free( p_sys->track[i].p_extra );
177     }
178     free( p_sys );
179 }
180
181 /*****************************************************************************
182  * Capability:
183  *****************************************************************************/
184 static int Capability( sout_mux_t *p_mux, int i_query,
185                        void *p_args, void *p_answer )
186 {
187    switch( i_query )
188    {
189         case SOUT_MUX_CAP_GET_ADD_STREAM_ANY_TIME:
190             *(vlc_bool_t*)p_answer = VLC_FALSE;
191             return( SOUT_MUX_CAP_ERR_OK );
192         default:
193             return( SOUT_MUX_CAP_ERR_UNIMPLEMENTED );
194    }
195 }
196
197 /*****************************************************************************
198  * AddStream:
199  *****************************************************************************/
200 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
201 {
202     sout_mux_sys_t   *p_sys = p_mux->p_sys;
203     asf_track_t      *tk;
204     bo_t             bo;
205
206     msg_Dbg( p_mux, "adding input" );
207     if( p_sys->i_track > 127 )
208     {
209         msg_Dbg( p_mux, "cannot add this track (too much track)" );
210         return VLC_EGENERIC;
211     }
212
213     tk = p_input->p_sys = &p_sys->track[p_sys->i_track];
214     tk->i_id  = p_sys->i_track;
215     tk->i_cat = p_input->p_fmt->i_cat;
216     tk->i_sequence = 0;
217
218     switch( tk->i_cat )
219     {
220         case AUDIO_ES:
221         {
222             uint16_t i_tag;
223             int      i_blockalign = p_input->p_fmt->i_block_align;
224             int      i_bitspersample = 0;
225
226             switch( p_input->p_fmt->i_fourcc )
227             {
228                 case VLC_FOURCC( 'a', '5', '2', ' ' ):
229                     i_tag = WAVE_FORMAT_A52;
230                     break;
231                 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
232                     i_tag = WAVE_FORMAT_MPEGLAYER3;
233                     break;
234                 case VLC_FOURCC( 'w', 'm', 'a', '1' ):
235                     i_tag = WAVE_FORMAT_WMA1;
236                     break;
237                 case VLC_FOURCC( 'w', 'm', 'a', '2' ):
238                     i_tag = WAVE_FORMAT_WMA2;
239                     break;
240                 case VLC_FOURCC( 'w', 'm', 'a', '3' ):
241                     i_tag = WAVE_FORMAT_WMA3;
242                     break;
243                     /* raw codec */
244                 case VLC_FOURCC( 'u', '8', ' ', ' ' ):
245                     i_tag = WAVE_FORMAT_PCM;
246                     i_blockalign= p_input->p_fmt->i_channels;
247                     i_bitspersample = 8;
248                     break;
249                 case VLC_FOURCC( 's', '1', '6', 'l' ):
250                     i_tag = WAVE_FORMAT_PCM;
251                     i_blockalign= 2 * p_input->p_fmt->i_channels;
252                     i_bitspersample = 16;
253                     break;
254                 case VLC_FOURCC( 's', '2', '4', 'l' ):
255                     i_tag = WAVE_FORMAT_PCM;
256                     i_blockalign= 3 * p_input->p_fmt->i_channels;
257                     i_bitspersample = 24;
258                     break;
259                 case VLC_FOURCC( 's', '3', '2', 'l' ):
260                     i_tag = WAVE_FORMAT_PCM;
261                     i_blockalign= 4 * p_input->p_fmt->i_channels;
262                     i_bitspersample = 32;
263                     break;
264                 default:
265                     return VLC_EGENERIC;
266             }
267
268             tk->i_extra = sizeof( WAVEFORMATEX ) + p_input->p_fmt->i_extra_data;
269             tk->p_extra = malloc( tk->i_extra );
270             bo_init( &bo, tk->p_extra, tk->i_extra );
271             bo_addle_u16( &bo, i_tag );
272             bo_addle_u16( &bo, p_input->p_fmt->i_channels );
273             bo_addle_u32( &bo, p_input->p_fmt->i_sample_rate );
274             bo_addle_u32( &bo, p_input->p_fmt->i_bitrate / 8 );
275             bo_addle_u16( &bo, i_blockalign );
276             bo_addle_u16( &bo, i_bitspersample );
277             bo_addle_u16( &bo, p_input->p_fmt->i_extra_data );
278             if( p_input->p_fmt->i_extra_data > 0 )
279             {
280                 bo_add_mem  ( &bo, p_input->p_fmt->p_extra_data,
281                               p_input->p_fmt->i_extra_data );
282             }
283             if( p_input->p_fmt->i_bitrate > 24000 )
284             {
285                 p_sys->i_bitrate += p_input->p_fmt->i_bitrate;
286             }
287             else
288             {
289                 p_sys->i_bitrate += 512000;
290             }
291             break;
292         }
293         case VIDEO_ES:
294         {
295             tk->i_extra = 11 + sizeof( BITMAPINFOHEADER ) +
296                           p_input->p_fmt->i_extra_data;
297             tk->p_extra = malloc( tk->i_extra );
298             bo_init( &bo, tk->p_extra, tk->i_extra );
299             bo_addle_u32( &bo, p_input->p_fmt->i_width );
300             bo_addle_u32( &bo, p_input->p_fmt->i_height );
301             bo_add_u8   ( &bo, 0x02 );  /* flags */
302             bo_addle_u16( &bo, sizeof( BITMAPINFOHEADER ) +
303                                p_input->p_fmt->i_extra_data );
304             bo_addle_u32( &bo, sizeof( BITMAPINFOHEADER ) +
305                                p_input->p_fmt->i_extra_data );
306             bo_addle_u32( &bo, p_input->p_fmt->i_width );
307             bo_addle_u32( &bo, p_input->p_fmt->i_height );
308             bo_addle_u16( &bo, 1 );
309             bo_addle_u16( &bo, 24 );
310             if( p_input->p_fmt->i_fourcc == VLC_FOURCC('m','p','4','v') )
311             {
312                 bo_add_mem( &bo, "MP4S", 4 );
313             }
314             else if( p_input->p_fmt->i_fourcc == VLC_FOURCC('D','I','V','3') )
315             {
316                 bo_add_mem( &bo, "MP43", 4 );
317             }
318             else
319             {
320                 bo_add_mem( &bo, (uint8_t*)&p_input->p_fmt->i_fourcc, 4 );
321             }
322             bo_addle_u32( &bo, 0 );
323             bo_addle_u32( &bo, 0 );
324             bo_addle_u32( &bo, 0 );
325             bo_addle_u32( &bo, 0 );
326             bo_addle_u32( &bo, 0 );
327             if( p_input->p_fmt->i_extra_data > 0 )
328             {
329                 bo_add_mem  ( &bo, p_input->p_fmt->p_extra_data,
330                               p_input->p_fmt->i_extra_data );
331             }
332
333             p_sys->i_bitrate += 2000000;
334             break;
335         }
336         default:
337             msg_Err(p_mux, "unhandled track type" );
338             return VLC_EGENERIC;
339     }
340
341     p_sys->i_track++;
342     return VLC_SUCCESS;
343 }
344
345 /*****************************************************************************
346  * DelStream:
347  *****************************************************************************/
348 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
349 {
350     msg_Dbg( p_mux, "removing input" );
351     return VLC_SUCCESS;
352 }
353
354 /*****************************************************************************
355  * Mux:
356  *****************************************************************************/
357 static int Mux      ( sout_mux_t *p_mux )
358 {
359     sout_mux_sys_t *p_sys = p_mux->p_sys;
360
361     if( p_sys->b_write_header )
362     {
363         sout_buffer_t *out = asf_header_create( p_mux, VLC_TRUE );
364
365         out->i_flags |= SOUT_BUFFER_FLAGS_HEADER;
366         sout_AccessOutWrite( p_mux->p_access, out );
367
368         p_sys->b_write_header = VLC_FALSE;
369     }
370
371     for( ;; )
372     {
373         sout_input_t  *p_input;
374         asf_track_t   *tk;
375         int           i_stream;
376         mtime_t       i_dts;
377         sout_buffer_t *data;
378         sout_buffer_t *pk;
379
380         if( MuxGetStream( p_mux, &i_stream, &i_dts ) )
381         {
382             /* not enough data */
383             return VLC_SUCCESS;
384         }
385
386         if( p_sys->i_dts_first < 0 )
387         {
388             p_sys->i_dts_first = i_dts;
389         }
390         if( p_sys->i_dts_last < i_dts )
391         {
392             p_sys->i_dts_last = i_dts;
393         }
394
395         p_input = p_mux->pp_inputs[i_stream];
396         tk      = (asf_track_t*)p_input->p_sys;
397
398         data = sout_FifoGet( p_input->p_fifo );
399
400         if( ( pk = asf_packet_create( p_mux, tk, data ) ) )
401         {
402             sout_AccessOutWrite( p_mux->p_access, pk );
403         }
404     }
405
406     return VLC_SUCCESS;
407 }
408
409
410 static int MuxGetStream( sout_mux_t *p_mux,
411                          int        *pi_stream,
412                          mtime_t    *pi_dts )
413 {
414     mtime_t i_dts;
415     int     i_stream;
416     int     i;
417
418     for( i = 0, i_dts = 0, i_stream = -1; i < p_mux->i_nb_inputs; i++ )
419     {
420         sout_input_t  *p_input = p_mux->pp_inputs[i];
421         sout_buffer_t *p_data;
422
423         if( p_input->p_fifo->i_depth <= 0 )
424         {
425             if( p_input->p_fmt->i_cat == AUDIO_ES ||
426                 p_input->p_fmt->i_cat == VIDEO_ES )
427             {
428                 /* We need that audio+video fifo contain at least 1 packet */
429                 return VLC_EGENERIC;
430             }
431             /* SPU */
432             continue;
433         }
434
435         p_data = sout_FifoShow( p_input->p_fifo );
436         if( i_stream == -1 ||
437             p_data->i_dts < i_dts )
438         {
439             i_stream = i;
440             i_dts    = p_data->i_dts;
441         }
442     }
443
444     *pi_stream = i_stream;
445     *pi_dts = i_dts;
446
447     return VLC_SUCCESS;
448 }
449
450 /****************************************************************************
451  * Asf header construction
452  ****************************************************************************/
453
454 /****************************************************************************
455  * Buffer out
456  ****************************************************************************/
457 static void bo_init( bo_t *p_bo, uint8_t *p_buffer, int i_size )
458 {
459     p_bo->i_buffer_size = i_size;
460     p_bo->i_buffer = 0;
461     p_bo->p_buffer = p_buffer;
462 }
463 static void bo_add_u8( bo_t *p_bo, uint8_t i )
464 {
465     if( p_bo->i_buffer < p_bo->i_buffer_size )
466     {
467         p_bo->p_buffer[p_bo->i_buffer] = i;
468     }
469     p_bo->i_buffer++;
470 }
471 static void bo_addle_u16( bo_t *p_bo, uint16_t i )
472 {
473     bo_add_u8( p_bo, i &0xff );
474     bo_add_u8( p_bo, ( ( i >> 8) &0xff ) );
475 }
476 static void bo_addle_u32( bo_t *p_bo, uint32_t i )
477 {
478     bo_addle_u16( p_bo, i &0xffff );
479     bo_addle_u16( p_bo, ( ( i >> 16) &0xffff ) );
480 }
481 static void bo_addle_u64( bo_t *p_bo, uint64_t i )
482 {
483     bo_addle_u32( p_bo, i &0xffffffff );
484     bo_addle_u32( p_bo, ( ( i >> 32) &0xffffffff ) );
485 }
486
487 static void bo_add_mem( bo_t *p_bo, uint8_t *p_mem, int i_size )
488 {
489     int i_copy = __MIN( i_size, p_bo->i_buffer_size - p_bo->i_buffer );
490
491     if( i_copy > 0 )
492     {
493         memcpy( &p_bo->p_buffer[p_bo->i_buffer],
494                 p_mem,
495                 i_copy );
496     }
497     p_bo->i_buffer += i_size;
498 }
499
500 /****************************************************************************
501  * guid
502  ****************************************************************************/
503 static void bo_add_guid( bo_t *p_bo, const guid_t *id )
504 {
505     int i;
506     bo_addle_u32( p_bo, id->v1 );
507     bo_addle_u16( p_bo, id->v2 );
508     bo_addle_u16( p_bo, id->v3 );
509     for( i = 0; i < 8; i++ )
510     {
511         bo_add_u8( p_bo, id->v4[i] );
512     }
513 }
514
515 static const guid_t asf_object_header_guid =
516 {
517     0x75B22630,
518     0x668E,
519     0x11CF,
520     { 0xA6,0xD9, 0x00,0xAA,0x00,0x62,0xCE,0x6C }
521 };
522 static const guid_t asf_object_data_guid =
523 {
524     0x75B22636,
525     0x668E,
526     0x11CF,
527     { 0xA6,0xD9, 0x00,0xAA,0x00,0x62,0xCE,0x6C }
528 };
529
530 static const guid_t asf_object_file_properties_guid =
531 {
532     0x8cabdca1,
533     0xa947,
534     0x11cf,
535     { 0x8e,0xe4, 0x00,0xC0,0x0C,0x20,0x53,0x65 }
536
537 };
538 static const guid_t asf_object_stream_properties_guid =
539 {
540     0xB7DC0791,
541     0xA9B7,
542     0x11CF,
543     { 0x8E,0xE6, 0x00,0xC0,0x0C,0x20,0x53,0x65 }
544
545 };
546 static const guid_t asf_object_header_extention_guid =
547 {
548    0x5FBF03B5,
549    0xA92E,
550    0x11CF,
551    { 0x8E,0xE3, 0x00,0xC0,0x0C,0x20,0x53,0x65 }
552 };
553
554 static const guid_t asf_object_stream_type_audio =
555 {
556     0xF8699E40,
557     0x5B4D,
558     0x11CF,
559     { 0xA8,0xFD, 0x00,0x80,0x5F,0x5C,0x44,0x2B }
560 };
561
562 static const guid_t asf_object_stream_type_video =
563 {
564     0xbc19efc0,
565     0x5B4D,
566     0x11CF,
567     { 0xA8,0xFD, 0x00,0x80,0x5F,0x5C,0x44,0x2B }
568 };
569
570 static const guid_t asf_guid_audio_conceal_none =
571 {
572     0x20FB5700,
573     0x5B55,
574     0x11CF,
575     { 0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B }
576 };
577 static const guid_t asf_guid_video_conceal_none =
578 {
579     0x20FB5700,
580     0x5B55,
581     0x11CF,
582     { 0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B }
583 };
584 static const guid_t asf_guid_reserved_1 =
585 {
586     0xABD3D211,
587     0xA9BA,
588     0x11cf,
589     { 0x8E, 0xE6,0x00, 0xC0, 0x0C ,0x20, 0x53, 0x65 }
590 };
591
592 static sout_buffer_t *asf_header_create( sout_mux_t *p_mux,
593                                          vlc_bool_t b_broadcast )
594 {
595     sout_mux_sys_t *p_sys = p_mux->p_sys;
596     asf_track_t    *tk;
597
598     mtime_t        i_duration = 0;
599     int i_size;
600     sout_buffer_t *out;
601     bo_t          bo;
602     int           i;
603
604     if( p_sys->i_dts_first > 0 )
605     {
606         i_duration = p_sys->i_dts_last - p_sys->i_dts_first;
607         if( i_duration < 0 )
608         {
609             i_duration = 0;
610         }
611     }
612
613     /* calculate header size */
614     i_size = 30 + 104 + 46;
615     for( i = 1; i < p_sys->i_track; i++ )
616     {
617         i_size += 78 + p_sys->track[i].i_extra;
618     }
619     out = sout_BufferNew( p_mux->p_sout, i_size + 50 );
620     bo_init( &bo, out->p_buffer, i_size + 50 );
621
622     /* header object */
623     bo_add_guid ( &bo, &asf_object_header_guid );
624     bo_addle_u64( &bo, i_size );
625     bo_addle_u32( &bo, 2 + p_sys->i_track );
626     bo_add_u8   ( &bo, 1 );
627     bo_add_u8   ( &bo, 2 );
628
629     /* sub object */
630
631     /* file properties */
632     bo_add_guid ( &bo, &asf_object_file_properties_guid );
633     bo_addle_u64( &bo, 104 );
634     bo_add_guid ( &bo, &p_sys->fid );
635     bo_addle_u64( &bo, i_size + p_sys->i_packet_count *
636                                 p_sys->i_packet_size ); /* file size */
637     bo_addle_u64( &bo, 0 );                 /* creation date */
638     bo_addle_u64( &bo, p_sys->i_packet_count );
639     bo_addle_u64( &bo, i_duration * 10 );   /* play duration (100ns) */
640     bo_addle_u64( &bo, i_duration * 10 );   /* send duration (100ns) */
641     bo_addle_u64( &bo, 10000 );             /* preroll duration (ms) */
642     bo_addle_u32( &bo, b_broadcast ? 0x01 : 0x00);      /* flags */
643     bo_addle_u32( &bo, p_sys->i_packet_size );  /* packet size min */
644     bo_addle_u32( &bo, p_sys->i_packet_size );  /* packet size max */
645     bo_addle_u32( &bo, p_sys->i_bitrate );      /* maxbitrate */
646
647     /* header extention */
648     bo_add_guid ( &bo, &asf_object_header_extention_guid );
649     bo_addle_u64( &bo, 46 );
650     bo_add_guid ( &bo, &asf_guid_reserved_1 );
651     bo_addle_u16( &bo, 6 );
652     bo_addle_u32( &bo, 0 );
653
654     /* stream properties */
655     for( i = 1; i < p_sys->i_track; i++ )
656     {
657         tk = &p_sys->track[i];
658
659         bo_add_guid ( &bo, &asf_object_stream_properties_guid );
660         bo_addle_u64( &bo, 78 + tk->i_extra );
661         if( tk->i_cat == AUDIO_ES )
662         {
663             bo_add_guid( &bo, &asf_object_stream_type_audio );
664             bo_add_guid( &bo, &asf_guid_audio_conceal_none );
665         }
666         else if( tk->i_cat == VIDEO_ES )
667         {
668             bo_add_guid( &bo, &asf_object_stream_type_video );
669             bo_add_guid( &bo, &asf_guid_video_conceal_none );
670         }
671         bo_addle_u64( &bo, 0 );         /* time offset */
672         bo_addle_u32( &bo, tk->i_extra );
673         bo_addle_u32( &bo, 0 );         /* 0 */
674         bo_addle_u16( &bo, tk->i_id );  /* stream number */
675         bo_addle_u32( &bo, 0 );
676         bo_add_mem  ( &bo, tk->p_extra, tk->i_extra );
677     }
678
679     /* data object */
680     bo_add_guid ( &bo, &asf_object_data_guid );
681     bo_addle_u64( &bo, 50 + p_sys->i_packet_count * p_sys->i_packet_size );
682     bo_add_guid ( &bo, &p_sys->fid );
683     bo_addle_u64( &bo, p_sys->i_packet_count );
684     bo_addle_u16( &bo, 0x101 );
685
686     return out;
687 }
688
689 /****************************************************************************
690  *
691  ****************************************************************************/
692 static sout_buffer_t *asf_packet_create( sout_mux_t *p_mux,
693                                          asf_track_t *tk, sout_buffer_t *data )
694 {
695     sout_mux_sys_t *p_sys = p_mux->p_sys;
696
697     int     i_data = data->i_size;
698     int     i_pos  = 0;
699     uint8_t *p_data= data->p_buffer;
700     sout_buffer_t *first = NULL, **last = &first;
701
702     while( i_pos < i_data )
703     {
704         bo_t          bo;
705         int           i_payload;
706
707         if( p_sys->pk == NULL )
708         {
709             p_sys->pk = sout_BufferNew( p_mux->p_sout, p_sys->i_packet_size );
710             /* reserve 14 bytes for the packet header */
711             p_sys->i_pk_used = 14;
712             p_sys->i_pk_frame = 0;
713             p_sys->i_pk_dts = data->i_dts;
714         }
715
716
717         bo_init( &bo, &p_sys->pk->p_buffer[p_sys->i_pk_used],
718                       p_sys->i_packet_size - p_sys->i_pk_used );
719
720         /* add payload (header size = 17) */
721         i_payload = __MIN( i_data - i_pos,
722                            p_sys->i_packet_size - p_sys->i_pk_used - 17 );
723         bo_add_u8   ( &bo, 0x80 | tk->i_id );
724         bo_add_u8   ( &bo, tk->i_sequence );
725         bo_addle_u32( &bo, i_pos );
726         bo_add_u8   ( &bo, 0x08 );  /* flags */
727         bo_addle_u32( &bo, i_data );
728         bo_addle_u32( &bo, ( data->i_dts - p_sys->i_dts_first )/ 1000 );
729         bo_addle_u16( &bo, i_payload );
730         bo_add_mem  ( &bo, &p_data[i_pos], i_payload );
731         i_pos += i_payload;
732         p_sys->i_pk_used += 17 + i_payload;
733
734         p_sys->i_pk_frame++;
735
736         if( p_sys->i_pk_used + 14 >= p_sys->i_packet_size )
737         {
738             /* not enough data for another payload, flush the packet */
739             int i_pad = p_sys->i_packet_size - p_sys->i_pk_used;
740
741             bo_init( &bo, p_sys->pk->p_buffer, 14 );
742             bo_add_u8   ( &bo, 0x82 );
743             bo_addle_u16( &bo, 0 );
744             bo_add_u8( &bo, 0x11 );
745             bo_add_u8( &bo, 0x5d );
746             bo_addle_u16( &bo, i_pad );
747             bo_addle_u32( &bo, ( p_sys->i_pk_dts - p_sys->i_dts_first )/ 1000 );
748             bo_addle_u16( &bo, 0 * data->i_length / 1000 );
749             bo_add_u8( &bo, 0x80 | p_sys->i_pk_frame );
750
751             /* append the packet */
752             *last = p_sys->pk;
753             last  = &p_sys->pk->p_next;
754
755             p_sys->pk = NULL;
756
757             p_sys->i_packet_count++;
758         }
759     }
760
761     tk->i_sequence++;
762     sout_BufferDelete( p_mux->p_sout, data );
763
764     return first;
765 }
766
767