Freetype: mark as _rgb the appropriate options
[vlc.git] / modules / mux / avi.c
blobd2d1a70d57ac7ccee129c3fb4ac5d13b80980b03
1 /*****************************************************************************
2 * avi.c
3 *****************************************************************************
4 * Copyright (C) 2001-2009 the VideoLAN team
5 * $Id$
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
25 * Preamble
26 *****************************************************************************/
27 /* TODO: add OpenDML write support */
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_sout.h>
37 #include <vlc_block.h>
38 #include <vlc_codecs.h>
40 /*****************************************************************************
41 * Module descriptor
42 *****************************************************************************/
43 static int Open ( vlc_object_t * );
44 static void Close ( vlc_object_t * );
46 vlc_module_begin ()
47 set_description( N_("AVI muxer") )
48 set_category( CAT_SOUT )
49 set_subcategory( SUBCAT_SOUT_MUX )
50 set_capability( "sout mux", 5 )
51 add_shortcut( "avi" )
52 set_callbacks( Open, Close )
53 vlc_module_end ()
56 /*****************************************************************************
57 * Local prototypes
58 *****************************************************************************/
59 static int Control( sout_mux_t *, int, va_list );
60 static int AddStream( sout_mux_t *, sout_input_t * );
61 static int DelStream( sout_mux_t *, sout_input_t * );
62 static int Mux ( sout_mux_t * );
64 typedef struct avi_stream_s
66 int i_cat;
68 char fcc[4];
70 mtime_t i_duration; // in µs
72 int i_frames; // total frame count
73 int64_t i_totalsize; // total stream size
75 float f_fps;
76 int i_bitrate;
78 BITMAPINFOHEADER *p_bih;
79 WAVEFORMATEX *p_wf;
81 } avi_stream_t;
83 typedef struct avi_idx1_entry_s
85 char fcc[4];
86 uint32_t i_flags;
87 uint32_t i_pos;
88 uint32_t i_length;
90 } avi_idx1_entry_t;
92 typedef struct avi_idx1_s
94 unsigned int i_entry_count;
95 unsigned int i_entry_max;
97 avi_idx1_entry_t *entry;
98 } avi_idx1_t;
100 struct sout_mux_sys_t
102 bool b_write_header;
104 int i_streams;
105 int i_stream_video;
107 off_t i_movi_size;
108 avi_stream_t stream[100];
110 avi_idx1_t idx1;
111 off_t i_idx1_size;
115 // FIXME FIXME
116 #define HDR_SIZE 10240
118 /* Flags in avih */
119 #define AVIF_HASINDEX 0x00000010 // Index at end of file?
120 #define AVIF_ISINTERLEAVED 0x00000100
121 #define AVIF_TRUSTCKTYPE 0x00000800 // Use CKType to find key frames?
123 /* Flags for index */
124 #define AVIIF_KEYFRAME 0x00000010L /* this frame is a key frame.*/
127 static block_t *avi_HeaderCreateRIFF( sout_mux_t * );
128 static block_t *avi_HeaderCreateidx1( sout_mux_t * );
130 static void SetFCC( uint8_t *p, char *fcc )
132 memcpy( p, fcc, 4 );
135 /*****************************************************************************
136 * Open:
137 *****************************************************************************/
138 static int Open( vlc_object_t *p_this )
140 sout_mux_t *p_mux = (sout_mux_t*)p_this;
141 sout_mux_sys_t *p_sys;
143 msg_Dbg( p_mux, "AVI muxer opened" );
145 p_sys = malloc( sizeof( sout_mux_sys_t ) );
146 if( !p_sys )
147 return VLC_ENOMEM;
148 p_sys->i_streams = 0;
149 p_sys->i_stream_video = -1;
150 p_sys->i_movi_size = 0;
152 p_sys->idx1.i_entry_count = 0;
153 p_sys->idx1.i_entry_max = 10000;
154 p_sys->idx1.entry = calloc( p_sys->idx1.i_entry_max,
155 sizeof( avi_idx1_entry_t ) );
156 if( !p_sys->idx1.entry )
158 free( p_sys );
159 return VLC_ENOMEM;
161 p_sys->b_write_header = true;
164 p_mux->pf_control = Control;
165 p_mux->pf_addstream = AddStream;
166 p_mux->pf_delstream = DelStream;
167 p_mux->pf_mux = Mux;
168 p_mux->p_sys = p_sys;
170 return VLC_SUCCESS;
173 /*****************************************************************************
174 * Close:
175 *****************************************************************************/
176 static void Close( vlc_object_t * p_this )
178 sout_mux_t *p_mux = (sout_mux_t*)p_this;
179 sout_mux_sys_t *p_sys = p_mux->p_sys;
181 block_t *p_hdr, *p_idx1;
182 int i_stream;
184 msg_Dbg( p_mux, "AVI muxer closed" );
186 /* first create idx1 chunk (write at the end of the stream */
187 p_idx1 = avi_HeaderCreateidx1( p_mux );
188 p_sys->i_idx1_size = p_idx1->i_buffer;
189 sout_AccessOutWrite( p_mux->p_access, p_idx1 );
191 /* calculate some value for headers creations */
192 for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
194 avi_stream_t *p_stream;
196 p_stream = &p_sys->stream[i_stream];
198 p_stream->f_fps = 25;
199 if( p_stream->i_duration > 0 )
201 p_stream->f_fps = (float)p_stream->i_frames /
202 ( (float)p_stream->i_duration /
203 (float)1000000 );
205 p_stream->i_bitrate = 128 * 1024;
206 if( p_stream->i_duration > 0 )
208 p_stream->i_bitrate =
209 8 * (uint64_t)1000000 *
210 (uint64_t)p_stream->i_totalsize /
211 (uint64_t)p_stream->i_duration;
213 msg_Info( p_mux, "stream[%d] duration:%"PRId64" totalsize:%"PRId64
214 " frames:%d fps:%f KiB/s:%d",
215 i_stream,
216 (int64_t)p_stream->i_duration / (int64_t)1000000,
217 p_stream->i_totalsize,
218 p_stream->i_frames,
219 p_stream->f_fps, p_stream->i_bitrate/1024 );
222 p_hdr = avi_HeaderCreateRIFF( p_mux );
223 sout_AccessOutSeek( p_mux->p_access, 0 );
224 sout_AccessOutWrite( p_mux->p_access, p_hdr );
227 static int Control( sout_mux_t *p_mux, int i_query, va_list args )
229 VLC_UNUSED(p_mux);
230 bool *pb_bool;
231 char **ppsz;
233 switch( i_query )
235 case MUX_CAN_ADD_STREAM_WHILE_MUXING:
236 pb_bool = (bool*)va_arg( args, bool * );
237 *pb_bool = false;
238 return VLC_SUCCESS;
240 case MUX_GET_ADD_STREAM_WAIT:
241 pb_bool = (bool*)va_arg( args, bool * );
242 *pb_bool = true;
243 return VLC_SUCCESS;
245 case MUX_GET_MIME:
246 ppsz = (char**)va_arg( args, char ** );
247 *ppsz = strdup( "video/avi" );
248 return VLC_SUCCESS;
250 default:
251 return VLC_EGENERIC;
255 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
257 sout_mux_sys_t *p_sys = p_mux->p_sys;
258 avi_stream_t *p_stream;
260 if( p_sys->i_streams >= 100 )
262 msg_Err( p_mux, "too many streams" );
263 return VLC_EGENERIC;
266 msg_Dbg( p_mux, "adding input" );
267 p_input->p_sys = malloc( sizeof( int ) );
268 if( !p_input->p_sys )
269 return VLC_ENOMEM;
271 *((int*)p_input->p_sys) = p_sys->i_streams;
272 p_stream = &p_sys->stream[p_sys->i_streams];
274 switch( p_input->p_fmt->i_cat )
276 case AUDIO_ES:
277 p_stream->i_cat = AUDIO_ES;
278 p_stream->fcc[0] = '0' + p_sys->i_streams / 10;
279 p_stream->fcc[1] = '0' + p_sys->i_streams % 10;
280 p_stream->fcc[2] = 'w';
281 p_stream->fcc[3] = 'b';
283 p_stream->p_bih = NULL;
285 p_stream->p_wf = malloc( sizeof( WAVEFORMATEX ) +
286 p_input->p_fmt->i_extra );
287 if( !p_stream->p_wf )
289 free( p_input->p_sys );
290 return VLC_ENOMEM;
292 #define p_wf p_stream->p_wf
293 p_wf->cbSize = p_input->p_fmt->i_extra;
294 if( p_wf->cbSize > 0 )
296 memcpy( &p_wf[1],
297 p_input->p_fmt->p_extra,
298 p_input->p_fmt->i_extra );
300 p_wf->nChannels = p_input->p_fmt->audio.i_channels;
301 p_wf->nSamplesPerSec = p_input->p_fmt->audio.i_rate;
302 p_wf->nBlockAlign = p_input->p_fmt->audio.i_blockalign;
303 p_wf->nAvgBytesPerSec= p_input->p_fmt->i_bitrate / 8;
304 p_wf->wBitsPerSample = 0;
306 switch( p_input->p_fmt->i_codec )
308 case VLC_CODEC_A52:
309 p_wf->wFormatTag = WAVE_FORMAT_A52;
310 p_wf->nBlockAlign= 1;
311 break;
312 case VLC_CODEC_MPGA:
313 p_wf->wFormatTag = WAVE_FORMAT_MPEGLAYER3;
314 p_wf->nBlockAlign= 1;
315 break;
316 case VLC_CODEC_WMA1:
317 p_wf->wFormatTag = WAVE_FORMAT_WMA1;
318 break;
319 case VLC_CODEC_WMA2:
320 p_wf->wFormatTag = WAVE_FORMAT_WMA2;
321 break;
322 case VLC_CODEC_WMAP:
323 p_wf->wFormatTag = WAVE_FORMAT_WMAP;
324 break;
325 case VLC_CODEC_WMAL:
326 p_wf->wFormatTag = WAVE_FORMAT_WMAL;
327 break;
328 /* raw codec */
329 case VLC_CODEC_U8:
330 p_wf->wFormatTag = WAVE_FORMAT_PCM;
331 p_wf->nBlockAlign= p_wf->nChannels;
332 p_wf->wBitsPerSample = 8;
333 p_wf->nAvgBytesPerSec = (p_wf->wBitsPerSample/8) *
334 p_wf->nSamplesPerSec * p_wf->nChannels;
335 break;
336 case VLC_CODEC_S16L:
337 p_wf->wFormatTag = WAVE_FORMAT_PCM;
338 p_wf->nBlockAlign= 2 * p_wf->nChannels;
339 p_wf->wBitsPerSample = 16;
340 p_wf->nAvgBytesPerSec = (p_wf->wBitsPerSample/8) *
341 p_wf->nSamplesPerSec * p_wf->nChannels;
342 break;
343 case VLC_CODEC_S24L:
344 p_wf->wFormatTag = WAVE_FORMAT_PCM;
345 p_wf->nBlockAlign= 3 * p_wf->nChannels;
346 p_wf->wBitsPerSample = 24;
347 p_wf->nAvgBytesPerSec = (p_wf->wBitsPerSample/8) *
348 p_wf->nSamplesPerSec * p_wf->nChannels;
349 break;
350 case VLC_CODEC_S32L:
351 p_wf->wFormatTag = WAVE_FORMAT_PCM;
352 p_wf->nBlockAlign= 4 * p_wf->nChannels;
353 p_wf->wBitsPerSample = 32;
354 p_wf->nAvgBytesPerSec = (p_wf->wBitsPerSample/8) *
355 p_wf->nSamplesPerSec * p_wf->nChannels;
356 break;
357 default:
358 return VLC_EGENERIC;
360 #undef p_wf
361 break;
362 case VIDEO_ES:
363 p_stream->i_cat = VIDEO_ES;
364 p_stream->fcc[0] = '0' + p_sys->i_streams / 10;
365 p_stream->fcc[1] = '0' + p_sys->i_streams % 10;
366 p_stream->fcc[2] = 'd';
367 p_stream->fcc[3] = 'c';
368 if( p_sys->i_stream_video < 0 )
370 p_sys->i_stream_video = p_sys->i_streams;
372 p_stream->p_wf = NULL;
373 p_stream->p_bih = malloc( sizeof( BITMAPINFOHEADER ) +
374 p_input->p_fmt->i_extra );
375 if( !p_stream->p_bih )
377 free( p_input->p_sys );
378 return VLC_ENOMEM;
380 #define p_bih p_stream->p_bih
381 p_bih->biSize = sizeof( BITMAPINFOHEADER ) +
382 p_input->p_fmt->i_extra;
383 if( p_input->p_fmt->i_extra > 0 )
385 memcpy( &p_bih[1],
386 p_input->p_fmt->p_extra,
387 p_input->p_fmt->i_extra );
389 p_bih->biWidth = p_input->p_fmt->video.i_width;
390 p_bih->biHeight= p_input->p_fmt->video.i_height;
391 p_bih->biPlanes= 1;
392 p_bih->biBitCount = 24;
393 p_bih->biSizeImage = 0;
394 p_bih->biXPelsPerMeter = 0;
395 p_bih->biYPelsPerMeter = 0;
396 p_bih->biClrUsed = 0;
397 p_bih->biClrImportant = 0;
398 switch( p_input->p_fmt->i_codec )
400 case VLC_CODEC_MP4V:
401 p_bih->biCompression = VLC_FOURCC( 'X', 'V', 'I', 'D' );
402 break;
403 default:
404 p_bih->biCompression = p_input->p_fmt->i_original_fourcc ?: p_input->p_fmt->i_codec;
405 break;
407 #undef p_bih
408 break;
409 default:
410 return( VLC_EGENERIC );
412 p_stream->i_totalsize = 0;
413 p_stream->i_frames = 0;
414 p_stream->i_duration = 0;
416 /* fixed later */
417 p_stream->f_fps = 25;
418 p_stream->i_bitrate = 128 * 1024;
420 p_sys->i_streams++;
421 return( VLC_SUCCESS );
424 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
426 msg_Dbg( p_mux, "removing input" );
428 free( p_input->p_sys );
429 return 0;
432 static int Mux ( sout_mux_t *p_mux )
434 sout_mux_sys_t *p_sys = p_mux->p_sys;
435 avi_stream_t *p_stream;
436 int i_stream, i;
438 if( p_sys->b_write_header )
440 block_t *p_hdr;
442 msg_Dbg( p_mux, "writing header" );
444 p_hdr = avi_HeaderCreateRIFF( p_mux );
445 sout_AccessOutWrite( p_mux->p_access, p_hdr );
447 p_sys->b_write_header = false;
450 for( i = 0; i < p_mux->i_nb_inputs; i++ )
452 int i_count;
453 block_fifo_t *p_fifo;
455 i_stream = *((int*)p_mux->pp_inputs[i]->p_sys );
456 p_stream = &p_sys->stream[i_stream];
458 p_fifo = p_mux->pp_inputs[i]->p_fifo;
459 i_count = block_FifoCount( p_fifo );
460 while( i_count > 1 )
462 avi_idx1_entry_t *p_idx;
463 block_t *p_data;
465 p_data = block_FifoGet( p_fifo );
466 if( block_FifoCount( p_fifo ) > 0 )
468 block_t *p_next = block_FifoShow( p_fifo );
469 p_data->i_length = p_next->i_dts - p_data->i_dts;
473 if( p_stream->i_frames == 0 &&p_stream->i_cat == VIDEO_ES )
475 /* Add header present at the end of BITMAP info header
476 to first frame in case of XVID */
477 if( p_stream->p_bih->biCompression
478 == VLC_FOURCC( 'X', 'V', 'I', 'D' ) )
480 int i_header_length =
481 p_stream->p_bih->biSize - sizeof(BITMAPINFOHEADER);
482 p_data = block_Realloc( p_data,
483 i_header_length, p_data->i_buffer );
484 if( !p_data)
485 return VLC_ENOMEM;
486 memcpy(p_data->p_buffer,&p_stream->p_bih[1], i_header_length);
490 p_stream->i_frames++;
491 if( p_data->i_length < 0 )
493 msg_Warn( p_mux, "argg length < 0 l" );
494 block_Release( p_data );
495 i_count--;
496 continue;
498 p_stream->i_duration += p_data->i_length;
499 p_stream->i_totalsize += p_data->i_buffer;
501 /* add idx1 entry for this frame */
502 p_idx = &p_sys->idx1.entry[p_sys->idx1.i_entry_count];
503 memcpy( p_idx->fcc, p_stream->fcc, 4 );
504 p_idx->i_flags = 0;
505 if( ( p_data->i_flags & BLOCK_FLAG_TYPE_MASK ) == 0 || ( p_data->i_flags & BLOCK_FLAG_TYPE_I ) )
506 p_idx->i_flags = AVIIF_KEYFRAME;
507 p_idx->i_pos = p_sys->i_movi_size + 4;
508 p_idx->i_length= p_data->i_buffer;
509 p_sys->idx1.i_entry_count++;
510 if( p_sys->idx1.i_entry_count >= p_sys->idx1.i_entry_max )
512 p_sys->idx1.i_entry_max += 10000;
513 p_sys->idx1.entry = xrealloc( p_sys->idx1.entry,
514 p_sys->idx1.i_entry_max * sizeof( avi_idx1_entry_t ) );
517 p_data = block_Realloc( p_data, 8, p_data->i_buffer );
518 if( p_data )
520 SetFCC( p_data->p_buffer, p_stream->fcc );
521 SetDWLE( p_data->p_buffer + 4, p_data->i_buffer - 8 );
523 if( p_data->i_buffer & 0x01 )
525 p_data = block_Realloc( p_data, 0, p_data->i_buffer + 1 );
526 p_data->p_buffer[ p_data->i_buffer - 1 ] = '\0';
529 p_sys->i_movi_size += p_data->i_buffer;
530 sout_AccessOutWrite( p_mux->p_access, p_data );
533 i_count--;
537 return( 0 );
540 /****************************************************************************/
541 /****************************************************************************/
542 /****************************************************************************/
543 /****************************************************************************/
545 typedef struct buffer_out_s
547 int i_buffer_size;
548 int i_buffer;
549 uint8_t *p_buffer;
551 } buffer_out_t;
553 static void bo_Init( buffer_out_t *p_bo, int i_size, uint8_t *p_buffer )
555 p_bo->i_buffer_size = i_size;
556 p_bo->i_buffer = 0;
557 p_bo->p_buffer = p_buffer;
559 static void bo_AddByte( buffer_out_t *p_bo, uint8_t i )
561 if( p_bo->i_buffer < p_bo->i_buffer_size )
563 p_bo->p_buffer[p_bo->i_buffer] = i;
565 p_bo->i_buffer++;
567 static void bo_AddWordLE( buffer_out_t *p_bo, uint16_t i )
569 bo_AddByte( p_bo, i &0xff );
570 bo_AddByte( p_bo, ( ( i >> 8) &0xff ) );
572 static void bo_AddWordBE( buffer_out_t *p_bo, uint16_t i )
574 bo_AddByte( p_bo, ( ( i >> 8) &0xff ) );
575 bo_AddByte( p_bo, i &0xff );
577 static void bo_AddDWordLE( buffer_out_t *p_bo, uint32_t i )
579 bo_AddWordLE( p_bo, i &0xffff );
580 bo_AddWordLE( p_bo, ( ( i >> 16) &0xffff ) );
582 static void bo_AddDWordBE( buffer_out_t *p_bo, uint32_t i )
584 bo_AddWordBE( p_bo, ( ( i >> 16) &0xffff ) );
585 bo_AddWordBE( p_bo, i &0xffff );
587 #if 0
588 static void bo_AddLWordLE( buffer_out_t *p_bo, uint64_t i )
590 bo_AddDWordLE( p_bo, i &0xffffffff );
591 bo_AddDWordLE( p_bo, ( ( i >> 32) &0xffffffff ) );
593 static void bo_AddLWordBE( buffer_out_t *p_bo, uint64_t i )
595 bo_AddDWordBE( p_bo, ( ( i >> 32) &0xffffffff ) );
596 bo_AddDWordBE( p_bo, i &0xffffffff );
598 #endif
600 static void bo_AddFCC( buffer_out_t *p_bo, const char *fcc )
602 bo_AddByte( p_bo, fcc[0] );
603 bo_AddByte( p_bo, fcc[1] );
604 bo_AddByte( p_bo, fcc[2] );
605 bo_AddByte( p_bo, fcc[3] );
608 static void bo_AddMem( buffer_out_t *p_bo, int i_size, uint8_t *p_mem )
610 int i;
612 for( i = 0; i < i_size; i++ )
614 bo_AddByte( p_bo, p_mem[i] );
618 /****************************************************************************
619 ****************************************************************************
621 ** avi header generation
623 ****************************************************************************
624 ****************************************************************************/
625 #define AVI_BOX_ENTER( fcc ) \
626 buffer_out_t _bo_sav_; \
627 bo_AddFCC( p_bo, fcc ); \
628 _bo_sav_ = *p_bo; \
629 bo_AddDWordLE( p_bo, 0 )
631 #define AVI_BOX_ENTER_LIST( fcc ) \
632 AVI_BOX_ENTER( "LIST" ); \
633 bo_AddFCC( p_bo, fcc )
635 #define AVI_BOX_EXIT( i_err ) \
636 if( p_bo->i_buffer&0x01 ) bo_AddByte( p_bo, 0 ); \
637 bo_AddDWordLE( &_bo_sav_, p_bo->i_buffer - _bo_sav_.i_buffer - 4 ); \
638 return( i_err );
640 static int avi_HeaderAdd_avih( sout_mux_t *p_mux,
641 buffer_out_t *p_bo )
643 sout_mux_sys_t *p_sys = p_mux->p_sys;
644 avi_stream_t *p_video = NULL;
645 int i_stream;
646 uint32_t i_microsecperframe;
647 int i_maxbytespersec;
648 int i_totalframes;
649 AVI_BOX_ENTER( "avih" );
651 if( p_sys->i_stream_video >= 0 )
653 p_video = &p_sys->stream[p_sys->i_stream_video];
654 if( p_video->i_frames <= 0 )
656 // p_video = NULL;
660 if( p_video )
662 i_microsecperframe =
663 (uint32_t)( (float)1000000 /
664 (float)p_sys->stream[p_sys->i_stream_video].f_fps );
665 i_totalframes = p_sys->stream[p_sys->i_stream_video].i_frames;
667 else
669 msg_Warn( p_mux, "avi file without video track isn't a good idea..." );
670 i_microsecperframe = 0;
671 i_totalframes = 0;
674 for( i_stream = 0,i_maxbytespersec = 0; i_stream < p_sys->i_streams; i_stream++ )
676 if( p_sys->stream[i_stream].i_duration > 0 )
678 i_maxbytespersec +=
679 p_sys->stream[i_stream].i_totalsize /
680 p_sys->stream[i_stream].i_duration;
684 bo_AddDWordLE( p_bo, i_microsecperframe );
685 bo_AddDWordLE( p_bo, i_maxbytespersec );
686 bo_AddDWordLE( p_bo, 0 ); /* padding */
687 bo_AddDWordLE( p_bo, AVIF_TRUSTCKTYPE |
688 AVIF_HASINDEX |
689 AVIF_ISINTERLEAVED ); /* flags */
690 bo_AddDWordLE( p_bo, i_totalframes );
691 bo_AddDWordLE( p_bo, 0 ); /* initial frame */
692 bo_AddDWordLE( p_bo, p_sys->i_streams ); /* streams count */
693 bo_AddDWordLE( p_bo, 1024 * 1024 ); /* suggested buffer size */
694 if( p_video )
696 bo_AddDWordLE( p_bo, p_video->p_bih->biWidth );
697 bo_AddDWordLE( p_bo, p_video->p_bih->biHeight );
699 else
701 bo_AddDWordLE( p_bo, 0 );
702 bo_AddDWordLE( p_bo, 0 );
704 bo_AddDWordLE( p_bo, 0 ); /* ???? */
705 bo_AddDWordLE( p_bo, 0 ); /* ???? */
706 bo_AddDWordLE( p_bo, 0 ); /* ???? */
707 bo_AddDWordLE( p_bo, 0 ); /* ???? */
709 AVI_BOX_EXIT( 0 );
711 static int avi_HeaderAdd_strh( buffer_out_t *p_bo, avi_stream_t *p_stream )
713 AVI_BOX_ENTER( "strh" );
715 switch( p_stream->i_cat )
717 case VIDEO_ES:
719 bo_AddFCC( p_bo, "vids" );
720 bo_AddDWordBE( p_bo, p_stream->p_bih->biCompression );
721 bo_AddDWordLE( p_bo, 0 ); /* flags */
722 bo_AddWordLE( p_bo, 0 ); /* priority */
723 bo_AddWordLE( p_bo, 0 ); /* langage */
724 bo_AddDWordLE( p_bo, 0 ); /* initial frame */
725 bo_AddDWordLE( p_bo, 1000 );/* scale */
726 bo_AddDWordLE( p_bo, (uint32_t)( 1000 * p_stream->f_fps ));
727 bo_AddDWordLE( p_bo, 0 ); /* start */
728 bo_AddDWordLE( p_bo, p_stream->i_frames );
729 bo_AddDWordLE( p_bo, 1024 * 1024 );
730 bo_AddDWordLE( p_bo, -1 ); /* quality */
731 bo_AddDWordLE( p_bo, 0 ); /* samplesize */
732 bo_AddWordLE( p_bo, 0 ); /* ??? */
733 bo_AddWordLE( p_bo, 0 ); /* ??? */
734 bo_AddWordLE( p_bo, p_stream->p_bih->biWidth );
735 bo_AddWordLE( p_bo, p_stream->p_bih->biHeight );
737 break;
738 case AUDIO_ES:
740 int i_rate, i_scale, i_samplesize;
742 i_samplesize = p_stream->p_wf->nBlockAlign;
743 if( i_samplesize > 1 )
745 i_scale = i_samplesize;
746 i_rate = /*i_scale **/ p_stream->i_bitrate / 8;
748 else
750 i_samplesize = 1;
751 i_scale = 1000;
752 i_rate = 1000 * p_stream->i_bitrate / 8;
754 bo_AddFCC( p_bo, "auds" );
755 bo_AddDWordLE( p_bo, 0 ); /* tag */
756 bo_AddDWordLE( p_bo, 0 ); /* flags */
757 bo_AddWordLE( p_bo, 0 ); /* priority */
758 bo_AddWordLE( p_bo, 0 ); /* langage */
759 bo_AddDWordLE( p_bo, 0 ); /* initial frame */
760 bo_AddDWordLE( p_bo, i_scale );/* scale */
761 bo_AddDWordLE( p_bo, i_rate );
762 bo_AddDWordLE( p_bo, 0 ); /* start */
763 bo_AddDWordLE( p_bo, p_stream->i_frames );
764 bo_AddDWordLE( p_bo, 10 * 1024 );
765 bo_AddDWordLE( p_bo, -1 ); /* quality */
766 bo_AddDWordLE( p_bo, i_samplesize );
767 bo_AddWordLE( p_bo, 0 ); /* ??? */
768 bo_AddWordLE( p_bo, 0 ); /* ??? */
769 bo_AddWordLE( p_bo, 0 );
770 bo_AddWordLE( p_bo, 0 );
772 break;
775 AVI_BOX_EXIT( 0 );
778 static int avi_HeaderAdd_strf( buffer_out_t *p_bo, avi_stream_t *p_stream )
780 AVI_BOX_ENTER( "strf" );
782 switch( p_stream->i_cat )
784 case AUDIO_ES:
785 bo_AddWordLE( p_bo, p_stream->p_wf->wFormatTag );
786 bo_AddWordLE( p_bo, p_stream->p_wf->nChannels );
787 bo_AddDWordLE( p_bo, p_stream->p_wf->nSamplesPerSec );
788 bo_AddDWordLE( p_bo, p_stream->p_wf->nAvgBytesPerSec );
789 bo_AddWordLE( p_bo, p_stream->p_wf->nBlockAlign );
790 bo_AddWordLE( p_bo, p_stream->p_wf->wBitsPerSample );
791 bo_AddWordLE( p_bo, p_stream->p_wf->cbSize );
792 bo_AddMem( p_bo, p_stream->p_wf->cbSize, (uint8_t*)&p_stream->p_wf[1] );
793 break;
794 case VIDEO_ES:
795 bo_AddDWordLE( p_bo, p_stream->p_bih->biSize );
796 bo_AddDWordLE( p_bo, p_stream->p_bih->biWidth );
797 bo_AddDWordLE( p_bo, p_stream->p_bih->biHeight );
798 bo_AddWordLE( p_bo, p_stream->p_bih->biPlanes );
799 bo_AddWordLE( p_bo, p_stream->p_bih->biBitCount );
800 if( VLC_FOURCC( 0, 0, 0, 1 ) == 0x00000001 )
802 bo_AddDWordBE( p_bo, p_stream->p_bih->biCompression );
804 else
806 bo_AddDWordLE( p_bo, p_stream->p_bih->biCompression );
808 bo_AddDWordLE( p_bo, p_stream->p_bih->biSizeImage );
809 bo_AddDWordLE( p_bo, p_stream->p_bih->biXPelsPerMeter );
810 bo_AddDWordLE( p_bo, p_stream->p_bih->biYPelsPerMeter );
811 bo_AddDWordLE( p_bo, p_stream->p_bih->biClrUsed );
812 bo_AddDWordLE( p_bo, p_stream->p_bih->biClrImportant );
813 bo_AddMem( p_bo,
814 p_stream->p_bih->biSize - sizeof( BITMAPINFOHEADER ),
815 (uint8_t*)&p_stream->p_bih[1] );
816 break;
819 AVI_BOX_EXIT( 0 );
822 static int avi_HeaderAdd_strl( buffer_out_t *p_bo, avi_stream_t *p_stream )
824 AVI_BOX_ENTER_LIST( "strl" );
826 avi_HeaderAdd_strh( p_bo, p_stream );
827 avi_HeaderAdd_strf( p_bo, p_stream );
829 AVI_BOX_EXIT( 0 );
832 static block_t *avi_HeaderCreateRIFF( sout_mux_t *p_mux )
834 sout_mux_sys_t *p_sys = p_mux->p_sys;
835 block_t *p_hdr;
836 int i_stream;
837 int i_junk;
838 buffer_out_t bo;
840 p_hdr = block_New( p_mux, HDR_SIZE );
841 memset( p_hdr->p_buffer, 0, HDR_SIZE );
843 bo_Init( &bo, HDR_SIZE, p_hdr->p_buffer );
845 bo_AddFCC( &bo, "RIFF" );
846 bo_AddDWordLE( &bo, p_sys->i_movi_size + HDR_SIZE - 8 + p_sys->i_idx1_size );
847 bo_AddFCC( &bo, "AVI " );
849 bo_AddFCC( &bo, "LIST" );
850 /* HDRL List size should exclude following data in HDR buffer
851 * -12 (RIFF, RIFF size, 'AVI ' tag),
852 * - 8 (hdr1 LIST tag and its size)
853 * - 12 (movi LIST tag, size, 'movi' listType )
855 bo_AddDWordLE( &bo, HDR_SIZE - 12 - 8 - 12);
856 bo_AddFCC( &bo, "hdrl" );
858 avi_HeaderAdd_avih( p_mux, &bo );
859 for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
861 avi_HeaderAdd_strl( &bo, &p_sys->stream[i_stream] );
864 i_junk = HDR_SIZE - bo.i_buffer - 8 - 12;
865 bo_AddFCC( &bo, "JUNK" );
866 bo_AddDWordLE( &bo, i_junk );
868 bo.i_buffer += i_junk;
869 bo_AddFCC( &bo, "LIST" );
870 bo_AddDWordLE( &bo, p_sys->i_movi_size + 4 );
871 bo_AddFCC( &bo, "movi" );
873 return( p_hdr );
876 static block_t * avi_HeaderCreateidx1( sout_mux_t *p_mux )
878 sout_mux_sys_t *p_sys = p_mux->p_sys;
879 block_t *p_idx1;
880 uint32_t i_idx1_size;
881 buffer_out_t bo;
883 i_idx1_size = 16 * p_sys->idx1.i_entry_count + 8;
885 p_idx1 = block_New( p_mux, i_idx1_size);
886 memset( p_idx1->p_buffer, 0, i_idx1_size);
888 bo_Init( &bo, i_idx1_size, p_idx1->p_buffer );
889 bo_AddFCC( &bo, "idx1" );
890 bo_AddDWordLE( &bo, i_idx1_size - 8);
892 for( unsigned i = 0; i < p_sys->idx1.i_entry_count; i++ )
894 bo_AddFCC( &bo, p_sys->idx1.entry[i].fcc );
895 bo_AddDWordLE( &bo, p_sys->idx1.entry[i].i_flags );
896 bo_AddDWordLE( &bo, p_sys->idx1.entry[i].i_pos );
897 bo_AddDWordLE( &bo, p_sys->idx1.entry[i].i_length );
900 return( p_idx1 );