qt: playlist: use item title if available
[vlc.git] / modules / mux / avi.c
blob844d983b42279257b289eb2717f37eb0ee83687b
1 /*****************************************************************************
2 * avi.c
3 *****************************************************************************
4 * Copyright (C) 2001-2009 VLC authors and VideoLAN
6 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 /*****************************************************************************
24 * Preamble
25 *****************************************************************************/
26 /* TODO: add OpenDML write support */
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_sout.h>
36 #include <vlc_block.h>
37 #include <vlc_codecs.h>
38 #include <vlc_boxes.h>
39 #include "../demux/avi/bitmapinfoheader.h"
41 /*****************************************************************************
42 * Module descriptor
43 *****************************************************************************/
44 static int Open ( vlc_object_t * );
45 static void Close ( vlc_object_t * );
47 #define SOUT_CFG_PREFIX "sout-avi-"
49 #define CFG_ARTIST_TEXT N_("Artist")
50 #define CFG_DATE_TEXT N_("Date")
51 #define CFG_GENRE_TEXT N_("Genre")
52 #define CFG_COPYRIGHT_TEXT N_("Copyright")
53 #define CFG_COMMENT_TEXT N_("Comment")
54 #define CFG_NAME_TEXT N_("Name")
55 #define CFG_SUBJECT_TEXT N_("Subject")
56 #define CFG_ENCODER_TEXT N_("Encoder")
57 #define CFG_KEYWORDS_TEXT N_("Keywords")
59 vlc_module_begin ()
60 set_description( N_("AVI muxer") )
61 set_category( CAT_SOUT )
62 set_subcategory( SUBCAT_SOUT_MUX )
63 set_capability( "sout mux", 5 )
64 add_shortcut( "avi" )
66 add_string( SOUT_CFG_PREFIX "artist", NULL, CFG_ARTIST_TEXT, NULL, true )
67 add_string( SOUT_CFG_PREFIX "date", NULL, CFG_DATE_TEXT, NULL, true )
68 add_string( SOUT_CFG_PREFIX "genre", NULL, CFG_GENRE_TEXT, NULL, true )
69 add_string( SOUT_CFG_PREFIX "copyright", NULL, CFG_COPYRIGHT_TEXT, NULL, true )
70 add_string( SOUT_CFG_PREFIX "comment", NULL, CFG_COMMENT_TEXT, NULL, true )
71 add_string( SOUT_CFG_PREFIX "name", NULL, CFG_NAME_TEXT, NULL, true )
72 add_string( SOUT_CFG_PREFIX "subject", NULL, CFG_SUBJECT_TEXT, NULL, true )
73 add_string( SOUT_CFG_PREFIX "encoder",
74 "VLC Media Player - " VERSION_MESSAGE,
75 CFG_ENCODER_TEXT, NULL, true )
76 add_string( SOUT_CFG_PREFIX "keywords", NULL, CFG_KEYWORDS_TEXT, NULL, true )
78 set_callbacks( Open, Close )
79 vlc_module_end ()
82 /*****************************************************************************
83 * Local prototypes
84 *****************************************************************************/
85 static int Control( sout_mux_t *, int, va_list );
86 static int AddStream( sout_mux_t *, sout_input_t * );
87 static void DelStream( sout_mux_t *, sout_input_t * );
88 static int Mux ( sout_mux_t * );
90 typedef struct avi_stream_s
92 int i_cat;
94 char fcc[4];
96 vlc_tick_t i_duration; // in µs
98 int i_frames; // total frame count
99 int64_t i_totalsize; // total stream size
101 float f_fps;
102 int i_bitrate;
104 VLC_BITMAPINFOHEADER *p_bih;
105 size_t i_bih;
106 WAVEFORMATEX *p_wf;
108 } avi_stream_t;
110 typedef struct avi_idx1_entry_s
112 char fcc[4];
113 uint32_t i_flags;
114 uint32_t i_pos;
115 uint32_t i_length;
117 } avi_idx1_entry_t;
119 typedef struct avi_idx1_s
121 unsigned int i_entry_count;
122 unsigned int i_entry_max;
124 avi_idx1_entry_t *entry;
125 } avi_idx1_t;
127 typedef struct
129 bool b_write_header;
131 int i_streams;
132 int i_stream_video;
134 off_t i_movi_size;
135 avi_stream_t stream[100];
137 avi_idx1_t idx1;
138 off_t i_idx1_size;
140 } sout_mux_sys_t;
142 #define HDR_BASE_SIZE 512 /* single video&audio ~ 400 bytes header */
144 /* Flags in avih */
145 #define AVIF_HASINDEX 0x00000010 // Index at end of file?
146 #define AVIF_ISINTERLEAVED 0x00000100
147 #define AVIF_TRUSTCKTYPE 0x00000800 // Use CKType to find key frames?
149 /* Flags for index */
150 #define AVIIF_KEYFRAME 0x00000010L /* this frame is a key frame.*/
153 static block_t *avi_HeaderCreateRIFF( sout_mux_t * );
154 static block_t *avi_HeaderCreateidx1( sout_mux_t * );
156 static void SetFCC( uint8_t *p, char *fcc )
158 memcpy( p, fcc, 4 );
161 /*****************************************************************************
162 * Open:
163 *****************************************************************************/
164 static int Open( vlc_object_t *p_this )
166 sout_mux_t *p_mux = (sout_mux_t*)p_this;
167 sout_mux_sys_t *p_sys;
169 msg_Dbg( p_mux, "AVI muxer opened" );
171 p_sys = malloc( sizeof( sout_mux_sys_t ) );
172 if( !p_sys )
173 return VLC_ENOMEM;
174 p_sys->i_streams = 0;
175 p_sys->i_stream_video = -1;
176 p_sys->i_movi_size = 0;
178 p_sys->idx1.i_entry_count = 0;
179 p_sys->idx1.i_entry_max = 10000;
180 p_sys->idx1.entry = calloc( p_sys->idx1.i_entry_max,
181 sizeof( avi_idx1_entry_t ) );
182 if( !p_sys->idx1.entry )
184 free( p_sys );
185 return VLC_ENOMEM;
187 p_sys->b_write_header = true;
190 p_mux->pf_control = Control;
191 p_mux->pf_addstream = AddStream;
192 p_mux->pf_delstream = DelStream;
193 p_mux->pf_mux = Mux;
194 p_mux->p_sys = p_sys;
196 return VLC_SUCCESS;
199 /*****************************************************************************
200 * Close:
201 *****************************************************************************/
202 static void Close( vlc_object_t * p_this )
204 sout_mux_t *p_mux = (sout_mux_t*)p_this;
205 sout_mux_sys_t *p_sys = p_mux->p_sys;
207 block_t *p_hdr, *p_idx1;
208 int i_stream;
210 msg_Dbg( p_mux, "AVI muxer closed" );
212 /* first create idx1 chunk (write at the end of the stream */
213 p_idx1 = avi_HeaderCreateidx1( p_mux );
214 if( p_idx1 )
216 p_sys->i_idx1_size = p_idx1->i_buffer;
217 sout_AccessOutWrite( p_mux->p_access, p_idx1 );
219 else p_sys->i_idx1_size = 0;
221 /* calculate some value for headers creations */
222 for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
224 avi_stream_t *p_stream;
226 p_stream = &p_sys->stream[i_stream];
228 if( p_stream->i_duration > 0 )
230 p_stream->f_fps = (float)p_stream->i_frames /
231 ( (float)p_stream->i_duration /
232 (float)CLOCK_FREQ );
233 p_stream->i_bitrate =
234 8 * (uint64_t)1000000 *
235 (uint64_t)p_stream->i_totalsize /
236 (uint64_t)p_stream->i_duration;
238 else
240 p_stream->f_fps = 25;
241 p_stream->i_bitrate = 128 * 1024;
243 msg_Info( p_mux, "stream[%d] duration:%"PRId64" totalsize:%"PRId64
244 " frames:%d fps:%f KiB/s:%d",
245 i_stream,
246 SEC_FROM_VLC_TICK(p_stream->i_duration),
247 p_stream->i_totalsize,
248 p_stream->i_frames,
249 p_stream->f_fps, p_stream->i_bitrate/1024 );
252 p_hdr = avi_HeaderCreateRIFF( p_mux );
253 if ( p_hdr )
255 sout_AccessOutSeek( p_mux->p_access, 0 );
256 sout_AccessOutWrite( p_mux->p_access, p_hdr );
259 for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
261 avi_stream_t *p_stream;
262 p_stream = &p_sys->stream[i_stream];
263 free( p_stream->p_bih );
264 free( p_stream->p_wf );
266 free( p_sys->idx1.entry );
267 free( p_sys );
270 static int Control( sout_mux_t *p_mux, int i_query, va_list args )
272 VLC_UNUSED(p_mux);
273 bool *pb_bool;
274 char **ppsz;
276 switch( i_query )
278 case MUX_CAN_ADD_STREAM_WHILE_MUXING:
279 pb_bool = va_arg( args, bool * );
280 *pb_bool = false;
281 return VLC_SUCCESS;
283 case MUX_GET_MIME:
284 ppsz = va_arg( args, char ** );
285 *ppsz = strdup( "video/avi" );
286 return VLC_SUCCESS;
288 default:
289 return VLC_EGENERIC;
293 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
295 sout_mux_sys_t *p_sys = p_mux->p_sys;
296 avi_stream_t *p_stream;
298 if( p_sys->i_streams >= 100 )
300 msg_Err( p_mux, "too many streams" );
301 return VLC_EGENERIC;
304 msg_Dbg( p_mux, "adding input" );
305 p_input->p_sys = malloc( sizeof( int ) );
306 if( !p_input->p_sys )
307 return VLC_ENOMEM;
309 *((int*)p_input->p_sys) = p_sys->i_streams;
310 p_stream = &p_sys->stream[p_sys->i_streams];
312 switch( p_input->p_fmt->i_cat )
314 case AUDIO_ES:
315 p_stream->i_cat = AUDIO_ES;
316 p_stream->fcc[0] = '0' + p_sys->i_streams / 10;
317 p_stream->fcc[1] = '0' + p_sys->i_streams % 10;
318 p_stream->fcc[2] = 'w';
319 p_stream->fcc[3] = 'b';
321 p_stream->p_bih = NULL;
322 p_stream->i_bih = 0;
324 WAVEFORMATEX *p_wf = malloc( sizeof( WAVEFORMATEX ) +
325 p_input->p_fmt->i_extra );
326 if( !p_wf )
328 free( p_input->p_sys );
329 p_input->p_sys = NULL;
330 return VLC_ENOMEM;
333 p_wf->cbSize = p_input->p_fmt->i_extra;
334 if( p_wf->cbSize > 0 )
336 memcpy( &p_wf[1],
337 p_input->p_fmt->p_extra,
338 p_input->p_fmt->i_extra );
340 p_wf->nChannels = p_input->p_fmt->audio.i_channels;
341 p_wf->nSamplesPerSec = p_input->p_fmt->audio.i_rate;
342 p_wf->nBlockAlign = p_input->p_fmt->audio.i_blockalign;
343 p_wf->nAvgBytesPerSec= p_input->p_fmt->i_bitrate / 8;
344 p_wf->wBitsPerSample = 0;
346 switch( p_input->p_fmt->i_codec )
348 case VLC_CODEC_A52:
349 p_wf->wFormatTag = WAVE_FORMAT_A52;
350 p_wf->nBlockAlign= 1;
351 break;
352 case VLC_CODEC_MP3:
353 p_wf->wFormatTag = WAVE_FORMAT_MPEGLAYER3;
354 p_wf->nBlockAlign= 1;
355 break;
356 case VLC_CODEC_WMA1:
357 p_wf->wFormatTag = WAVE_FORMAT_WMA1;
358 break;
359 case VLC_CODEC_WMA2:
360 p_wf->wFormatTag = WAVE_FORMAT_WMA2;
361 break;
362 case VLC_CODEC_WMAP:
363 p_wf->wFormatTag = WAVE_FORMAT_WMAP;
364 break;
365 case VLC_CODEC_WMAL:
366 p_wf->wFormatTag = WAVE_FORMAT_WMAL;
367 break;
368 case VLC_CODEC_ALAW:
369 p_wf->wFormatTag = WAVE_FORMAT_ALAW;
370 break;
371 case VLC_CODEC_MULAW:
372 p_wf->wFormatTag = WAVE_FORMAT_MULAW;
373 break;
374 /* raw codec */
375 case VLC_CODEC_U8:
376 p_wf->wFormatTag = WAVE_FORMAT_PCM;
377 p_wf->nBlockAlign= p_wf->nChannels;
378 p_wf->wBitsPerSample = 8;
379 p_wf->nAvgBytesPerSec = (p_wf->wBitsPerSample/8) *
380 p_wf->nSamplesPerSec * p_wf->nChannels;
381 break;
382 case VLC_CODEC_S16L:
383 p_wf->wFormatTag = WAVE_FORMAT_PCM;
384 p_wf->nBlockAlign= 2 * p_wf->nChannels;
385 p_wf->wBitsPerSample = 16;
386 p_wf->nAvgBytesPerSec = (p_wf->wBitsPerSample/8) *
387 p_wf->nSamplesPerSec * p_wf->nChannels;
388 break;
389 case VLC_CODEC_S24L:
390 p_wf->wFormatTag = WAVE_FORMAT_PCM;
391 p_wf->nBlockAlign= 3 * p_wf->nChannels;
392 p_wf->wBitsPerSample = 24;
393 p_wf->nAvgBytesPerSec = (p_wf->wBitsPerSample/8) *
394 p_wf->nSamplesPerSec * p_wf->nChannels;
395 break;
396 case VLC_CODEC_S32L:
397 p_wf->wFormatTag = WAVE_FORMAT_PCM;
398 p_wf->nBlockAlign= 4 * p_wf->nChannels;
399 p_wf->wBitsPerSample = 32;
400 p_wf->nAvgBytesPerSec = (p_wf->wBitsPerSample/8) *
401 p_wf->nSamplesPerSec * p_wf->nChannels;
402 break;
403 default:
404 free( p_wf );
405 free( p_input->p_sys );
406 p_input->p_sys = NULL;
407 return VLC_EGENERIC;
409 p_stream->p_wf = p_wf;
410 break;
411 case VIDEO_ES:
412 p_stream->i_cat = VIDEO_ES;
413 p_stream->fcc[0] = '0' + p_sys->i_streams / 10;
414 p_stream->fcc[1] = '0' + p_sys->i_streams % 10;
415 p_stream->fcc[2] = 'd';
416 p_stream->fcc[3] = 'c';
417 if( p_sys->i_stream_video < 0 )
419 p_sys->i_stream_video = p_sys->i_streams;
421 p_stream->p_wf = NULL;
422 p_stream->p_bih = CreateBitmapInfoHeader( &p_input->fmt, &p_stream->i_bih );
423 if( !p_stream->p_bih )
425 free( p_input->p_sys );
426 p_input->p_sys = NULL;
427 return VLC_ENOMEM;
429 break;
430 default:
431 free( p_input->p_sys );
432 p_input->p_sys = NULL;
433 return( VLC_EGENERIC );
435 p_stream->i_totalsize = 0;
436 p_stream->i_frames = 0;
437 p_stream->i_duration = 0;
439 /* fixed later */
440 p_stream->f_fps = 25;
441 p_stream->i_bitrate = 128 * 1024;
443 p_sys->i_streams++;
444 return( VLC_SUCCESS );
447 static void DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
449 msg_Dbg( p_mux, "removing input" );
451 free( p_input->p_sys );
454 static int PrepareSamples( const avi_stream_t *p_stream,
455 const es_format_t *p_fmt,
456 block_t **pp_block )
458 if( p_stream->i_frames == 0 && p_stream->i_cat == VIDEO_ES )
460 /* Add header present at the end of BITMAP info header
461 to first frame in case of XVID */
462 if( p_stream->p_bih->biCompression == VLC_FOURCC( 'X', 'V', 'I', 'D' ) )
464 size_t i_header_length =
465 p_stream->p_bih->biSize - sizeof(VLC_BITMAPINFOHEADER);
466 *pp_block = block_Realloc( *pp_block, i_header_length,
467 (*pp_block)->i_buffer );
468 if( !*pp_block )
469 return VLC_ENOMEM;
470 memcpy((*pp_block)->p_buffer,&p_stream->p_bih[1], i_header_length);
474 /* RV24 is only BGR in AVI, and we can't use BI_BITFIELD */
475 if( p_stream->i_cat == VIDEO_ES &&
476 p_stream->p_bih->biCompression == BI_RGB &&
477 p_stream->p_bih->biBitCount == 24 &&
478 (p_fmt->video.i_bmask != 0xFF0000 ||
479 p_fmt->video.i_rmask != 0x0000FF) )
481 unsigned rshift = ctz(p_fmt->video.i_rmask);
482 unsigned gshift = ctz(p_fmt->video.i_gmask);
483 unsigned bshift = ctz(p_fmt->video.i_bmask);
485 uint8_t *p_data = (*pp_block)->p_buffer;
486 for( size_t i=0; i<(*pp_block)->i_buffer / 3; i++ )
488 uint8_t *p = &p_data[i*3];
489 /* reorder as BGR using shift value (done by FixRGB) */
490 uint32_t v = (p[0] << 16) | (p[1] << 8) | p[2];
491 p[0] = (v & p_fmt->video.i_bmask) >> bshift;
492 p[1] = (v & p_fmt->video.i_gmask) >> gshift;
493 p[2] = (v & p_fmt->video.i_rmask) >> rshift;
497 return VLC_SUCCESS;
500 static int Mux ( sout_mux_t *p_mux )
502 sout_mux_sys_t *p_sys = p_mux->p_sys;
503 avi_stream_t *p_stream;
504 int i_stream, i;
506 if( p_sys->b_write_header )
508 msg_Dbg( p_mux, "writing header" );
510 block_t *p_hdr = avi_HeaderCreateRIFF( p_mux );
511 if ( !p_hdr )
512 return VLC_EGENERIC;
513 sout_AccessOutWrite( p_mux->p_access, p_hdr );
515 p_sys->b_write_header = false;
518 for( i = 0; i < p_mux->i_nb_inputs; i++ )
520 int i_count;
521 block_fifo_t *p_fifo;
523 if (!p_mux->pp_inputs[i]->p_sys)
524 continue;
526 i_stream = *((int*)p_mux->pp_inputs[i]->p_sys );
527 p_stream = &p_sys->stream[i_stream];
529 p_fifo = p_mux->pp_inputs[i]->p_fifo;
530 i_count = block_FifoCount( p_fifo );
531 while( i_count > 1 )
533 avi_idx1_entry_t *p_idx;
534 block_t *p_data;
536 p_data = block_FifoGet( p_fifo );
537 if( block_FifoCount( p_fifo ) > 0 )
539 block_t *p_next = block_FifoShow( p_fifo );
540 p_data->i_length = p_next->i_dts - p_data->i_dts;
543 if( PrepareSamples( p_stream, &p_mux->pp_inputs[i]->fmt,
544 &p_data ) != VLC_SUCCESS )
546 i_count--;
547 continue;
550 p_stream->i_frames++;
551 if( p_data->i_length < 0 )
553 msg_Warn( p_mux, "argg length < 0 l" );
554 block_Release( p_data );
555 i_count--;
556 continue;
558 p_stream->i_duration += p_data->i_length;
559 p_stream->i_totalsize += p_data->i_buffer;
561 /* add idx1 entry for this frame */
562 p_idx = &p_sys->idx1.entry[p_sys->idx1.i_entry_count];
563 memcpy( p_idx->fcc, p_stream->fcc, 4 );
564 p_idx->i_flags = 0;
565 if( ( p_data->i_flags & BLOCK_FLAG_TYPE_MASK ) == 0 || ( p_data->i_flags & BLOCK_FLAG_TYPE_I ) )
566 p_idx->i_flags = AVIIF_KEYFRAME;
567 p_idx->i_pos = p_sys->i_movi_size + 4;
568 p_idx->i_length= p_data->i_buffer;
569 p_sys->idx1.i_entry_count++;
570 if( p_sys->idx1.i_entry_count >= p_sys->idx1.i_entry_max )
572 p_sys->idx1.i_entry_max += 10000;
573 p_sys->idx1.entry = xrealloc( p_sys->idx1.entry,
574 p_sys->idx1.i_entry_max * sizeof( avi_idx1_entry_t ) );
577 p_data = block_Realloc( p_data, 8, p_data->i_buffer );
578 if( p_data )
580 SetFCC( p_data->p_buffer, p_stream->fcc );
581 SetDWLE( p_data->p_buffer + 4, p_data->i_buffer - 8 );
583 if( p_data->i_buffer & 0x01 )
585 p_data = block_Realloc( p_data, 0, p_data->i_buffer + 1 );
586 if ( p_data )
587 p_data->p_buffer[ p_data->i_buffer - 1 ] = '\0';
590 if ( p_data )
592 p_sys->i_movi_size += p_data->i_buffer;
593 sout_AccessOutWrite( p_mux->p_access, p_data );
597 i_count--;
601 return( 0 );
604 /****************************************************************************
605 ****************************************************************************
607 ** avi header generation
609 ****************************************************************************
610 ****************************************************************************/
611 #define AVI_BOX_ENTER( fcc ) \
612 int i_datasize_offset; \
613 bo_add_fourcc( p_bo, fcc ); \
614 i_datasize_offset = p_bo->b->i_buffer; \
615 bo_add_32le( p_bo, 0 )
617 #define AVI_BOX_ENTER_LIST( fcc ) \
618 AVI_BOX_ENTER( "LIST" ); \
619 bo_add_fourcc( p_bo, fcc )
621 #define AVI_BOX_EXIT( i_err ) \
622 if( p_bo->b->i_buffer&0x01 ) bo_add_8( p_bo, 0 ); \
623 bo_set_32le( p_bo, i_datasize_offset, p_bo->b->i_buffer - i_datasize_offset - 4 ); \
624 return( i_err );
626 static int avi_HeaderAdd_avih( sout_mux_t *p_mux,
627 bo_t *p_bo )
629 sout_mux_sys_t *p_sys = p_mux->p_sys;
630 avi_stream_t *p_video = NULL;
631 int i_stream;
632 uint32_t i_microsecperframe;
633 int i_maxbytespersec;
634 int i_totalframes;
635 AVI_BOX_ENTER( "avih" );
637 if( p_sys->i_stream_video >= 0 )
639 p_video = &p_sys->stream[p_sys->i_stream_video];
640 if( p_video->i_frames <= 0 )
642 // p_video = NULL;
646 if( p_video )
648 i_microsecperframe =
649 (uint32_t)( (float)1000000 /
650 (float)p_sys->stream[p_sys->i_stream_video].f_fps );
651 i_totalframes = p_sys->stream[p_sys->i_stream_video].i_frames;
653 else
655 msg_Warn( p_mux, "avi file without video track isn't a good idea..." );
656 i_microsecperframe = 0;
657 i_totalframes = 0;
660 for( i_stream = 0,i_maxbytespersec = 0; i_stream < p_sys->i_streams; i_stream++ )
662 if( p_sys->stream[i_stream].i_duration > 0 )
664 i_maxbytespersec +=
665 p_sys->stream[i_stream].i_totalsize /
666 p_sys->stream[i_stream].i_duration;
670 bo_add_32le( p_bo, i_microsecperframe );
671 bo_add_32le( p_bo, i_maxbytespersec );
672 bo_add_32le( p_bo, 0 ); /* padding */
673 bo_add_32le( p_bo, AVIF_TRUSTCKTYPE |
674 AVIF_HASINDEX |
675 AVIF_ISINTERLEAVED ); /* flags */
676 bo_add_32le( p_bo, i_totalframes );
677 bo_add_32le( p_bo, 0 ); /* initial frame */
678 bo_add_32le( p_bo, p_sys->i_streams ); /* streams count */
679 bo_add_32le( p_bo, 1024 * 1024 ); /* suggested buffer size */
680 if( p_video )
682 bo_add_32le( p_bo, p_video->p_bih->biWidth );
683 bo_add_32le( p_bo, p_video->p_bih->biHeight );
685 else
687 bo_add_32le( p_bo, 0 );
688 bo_add_32le( p_bo, 0 );
690 bo_add_32le( p_bo, 0 ); /* ???? */
691 bo_add_32le( p_bo, 0 ); /* ???? */
692 bo_add_32le( p_bo, 0 ); /* ???? */
693 bo_add_32le( p_bo, 0 ); /* ???? */
695 AVI_BOX_EXIT( 0 );
697 static int avi_HeaderAdd_strh( bo_t *p_bo, avi_stream_t *p_stream )
699 AVI_BOX_ENTER( "strh" );
701 switch( p_stream->i_cat )
703 case VIDEO_ES:
705 bo_add_fourcc( p_bo, "vids" );
706 if( p_stream->p_bih->biBitCount )
707 bo_add_fourcc( p_bo, "DIB " );
708 else
709 #ifdef WORDS_BIGENDIAN
710 bo_add_32be( p_bo, p_stream->p_bih->biCompression );
711 #else
712 bo_add_32le( p_bo, p_stream->p_bih->biCompression );
713 #endif
714 bo_add_32le( p_bo, 0 ); /* flags */
715 bo_add_16le( p_bo, 0 ); /* priority */
716 bo_add_16le( p_bo, 0 ); /* langage */
717 bo_add_32le( p_bo, 0 ); /* initial frame */
718 bo_add_32le( p_bo, 1000 );/* scale */
719 bo_add_32le( p_bo, (uint32_t)( 1000 * p_stream->f_fps ));
720 bo_add_32le( p_bo, 0 ); /* start */
721 bo_add_32le( p_bo, p_stream->i_frames );
722 bo_add_32le( p_bo, 1024 * 1024 );
723 bo_add_32le( p_bo, -1 ); /* quality */
724 bo_add_32le( p_bo, 0 ); /* samplesize */
725 bo_add_16le( p_bo, 0 ); /* ??? */
726 bo_add_16le( p_bo, 0 ); /* ??? */
727 bo_add_16le( p_bo, p_stream->p_bih->biWidth );
728 bo_add_16le( p_bo, p_stream->p_bih->biHeight );
730 break;
731 case AUDIO_ES:
733 int i_rate, i_scale, i_samplesize;
735 i_samplesize = p_stream->p_wf->nBlockAlign;
736 if( i_samplesize > 1 )
738 i_scale = i_samplesize;
739 i_rate = /*i_scale **/ p_stream->i_bitrate / 8;
741 else
743 i_samplesize = 1;
744 i_scale = 1000;
745 i_rate = 1000 * p_stream->i_bitrate / 8;
747 bo_add_fourcc( p_bo, "auds" );
748 bo_add_32le( p_bo, 0 ); /* tag */
749 bo_add_32le( p_bo, 0 ); /* flags */
750 bo_add_16le( p_bo, 0 ); /* priority */
751 bo_add_16le( p_bo, 0 ); /* langage */
752 bo_add_32le( p_bo, 0 ); /* initial frame */
753 bo_add_32le( p_bo, i_scale );/* scale */
754 bo_add_32le( p_bo, i_rate );
755 bo_add_32le( p_bo, 0 ); /* start */
756 bo_add_32le( p_bo, p_stream->i_frames );
757 bo_add_32le( p_bo, 10 * 1024 );
758 bo_add_32le( p_bo, -1 ); /* quality */
759 bo_add_32le( p_bo, i_samplesize );
760 bo_add_16le( p_bo, 0 ); /* ??? */
761 bo_add_16le( p_bo, 0 ); /* ??? */
762 bo_add_16le( p_bo, 0 );
763 bo_add_16le( p_bo, 0 );
765 break;
768 AVI_BOX_EXIT( 0 );
771 static int avi_HeaderAdd_strf( bo_t *p_bo, avi_stream_t *p_stream )
773 AVI_BOX_ENTER( "strf" );
775 switch( p_stream->i_cat )
777 case AUDIO_ES:
778 bo_add_16le( p_bo, p_stream->p_wf->wFormatTag );
779 bo_add_16le( p_bo, p_stream->p_wf->nChannels );
780 bo_add_32le( p_bo, p_stream->p_wf->nSamplesPerSec );
781 bo_add_32le( p_bo, p_stream->p_wf->nAvgBytesPerSec );
782 bo_add_16le( p_bo, p_stream->p_wf->nBlockAlign );
783 bo_add_16le( p_bo, p_stream->p_wf->wBitsPerSample );
784 bo_add_16le( p_bo, p_stream->p_wf->cbSize );
785 bo_add_mem( p_bo, p_stream->p_wf->cbSize, (uint8_t*)&p_stream->p_wf[1] );
786 break;
787 case VIDEO_ES:
788 bo_add_32le( p_bo, p_stream->p_bih->biSize );
789 bo_add_32le( p_bo, p_stream->p_bih->biWidth );
790 bo_add_32le( p_bo, p_stream->p_bih->biHeight );
791 bo_add_16le( p_bo, p_stream->p_bih->biPlanes );
792 bo_add_16le( p_bo, p_stream->p_bih->biBitCount );
793 #ifdef WORDS_BIGENDIAN
794 bo_add_32be( p_bo, p_stream->p_bih->biCompression );
795 #else
796 bo_add_32le( p_bo, p_stream->p_bih->biCompression );
797 #endif
798 bo_add_32le( p_bo, p_stream->p_bih->biSizeImage );
799 bo_add_32le( p_bo, p_stream->p_bih->biXPelsPerMeter );
800 bo_add_32le( p_bo, p_stream->p_bih->biYPelsPerMeter );
801 bo_add_32le( p_bo, p_stream->p_bih->biClrUsed );
802 bo_add_32le( p_bo, p_stream->p_bih->biClrImportant );
803 bo_add_mem( p_bo,
804 p_stream->i_bih - sizeof( VLC_BITMAPINFOHEADER ),
805 (uint8_t*)&p_stream->p_bih[1] );
806 break;
809 AVI_BOX_EXIT( 0 );
812 static int avi_HeaderAdd_strl( bo_t *p_bo, avi_stream_t *p_stream )
814 AVI_BOX_ENTER_LIST( "strl" );
816 avi_HeaderAdd_strh( p_bo, p_stream );
817 avi_HeaderAdd_strf( p_bo, p_stream );
819 AVI_BOX_EXIT( 0 );
822 static int avi_HeaderAdd_meta( bo_t *p_bo, const char psz_meta[4],
823 const char *psz_data )
825 if ( psz_data == NULL ) return 1;
826 const char *psz = psz_data;
827 AVI_BOX_ENTER( psz_meta );
828 while (*psz) bo_add_8( p_bo, *psz++ );
829 bo_add_8( p_bo, 0 );
830 AVI_BOX_EXIT( 0 );
833 static int avi_HeaderAdd_INFO( sout_mux_t *p_mux, bo_t *p_bo )
835 char *psz;
837 #define APPLY_META(var, fourcc) \
838 psz = var_InheritString( p_mux, SOUT_CFG_PREFIX var );\
839 if ( psz )\
841 avi_HeaderAdd_meta( p_bo, fourcc, psz );\
842 free( psz );\
845 AVI_BOX_ENTER_LIST( "INFO" );
847 APPLY_META( "artist", "IART")
848 APPLY_META( "comment", "ICMT")
849 APPLY_META( "copyright","ICOP")
850 APPLY_META( "date", "ICRD")
851 APPLY_META( "genre", "IGNR")
852 APPLY_META( "name", "INAM")
853 APPLY_META( "keywords", "IKEY")
854 APPLY_META( "subject", "ISBJ")
855 APPLY_META( "encoder", "ISFT")
856 /* Some are missing, but are they really useful ?? */
858 #undef APPLY_META
860 AVI_BOX_EXIT( 0 );
863 static block_t *avi_HeaderCreateRIFF( sout_mux_t *p_mux )
865 sout_mux_sys_t *p_sys = p_mux->p_sys;
866 int i_stream;
867 int i_junk;
868 bo_t bo;
870 struct
872 int i_riffsize;
873 int i_hdrllistsize;
874 int i_hdrldatastart;
875 } offsets;
877 if (! bo_init( &bo, HDR_BASE_SIZE ) )
878 return NULL;
880 bo_add_fourcc( &bo, "RIFF" );
881 offsets.i_riffsize = bo.b->i_buffer;
882 bo_add_32le( &bo, 0xEFBEADDE );
883 bo_add_fourcc( &bo, "AVI " );
885 bo_add_fourcc( &bo, "LIST" );
886 /* HDRL List size should exclude following data in HDR buffer
887 * -12 (RIFF, RIFF size, 'AVI ' tag),
888 * - 8 (hdr1 LIST tag and its size)
889 * - 12 (movi LIST tag, size, 'movi' listType )
891 offsets.i_hdrllistsize = bo.b->i_buffer;
892 bo_add_32le( &bo, 0xEFBEADDE );
893 bo_add_fourcc( &bo, "hdrl" );
894 offsets.i_hdrldatastart = bo.b->i_buffer;
896 avi_HeaderAdd_avih( p_mux, &bo );
897 for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
899 avi_HeaderAdd_strl( &bo, &p_sys->stream[i_stream] );
902 /* align on 16 bytes */
903 int i_align = ( ( bo.b->i_buffer + 12 + 0xE ) & ~ 0xF );
904 i_junk = i_align - bo.b->i_buffer;
905 bo_add_fourcc( &bo, "JUNK" );
906 bo_add_32le( &bo, i_junk );
907 for( int i=0; i< i_junk; i++ )
909 bo_add_8( &bo, 0 );
912 /* Now set hdrl size */
913 bo_set_32le( &bo, offsets.i_hdrllistsize,
914 bo.b->i_buffer - offsets.i_hdrldatastart );
916 avi_HeaderAdd_INFO( p_mux, &bo );
918 bo_add_fourcc( &bo, "LIST" );
919 bo_add_32le( &bo, p_sys->i_movi_size + 4 );
920 bo_add_fourcc( &bo, "movi" );
922 /* Now set RIFF size */
923 bo_set_32le( &bo, offsets.i_riffsize, bo.b->i_buffer - 8
924 + p_sys->i_movi_size + p_sys->i_idx1_size );
926 return( bo.b );
929 static block_t * avi_HeaderCreateidx1( sout_mux_t *p_mux )
931 sout_mux_sys_t *p_sys = p_mux->p_sys;
932 uint32_t i_idx1_size;
933 bo_t bo;
935 i_idx1_size = 16 * p_sys->idx1.i_entry_count + 8;
937 if (!i_idx1_size || !bo_init( &bo, i_idx1_size ) )
938 return NULL;
939 memset( bo.b->p_buffer, 0, i_idx1_size);
941 bo_add_fourcc( &bo, "idx1" );
942 bo_add_32le( &bo, i_idx1_size - 8);
944 for( unsigned i = 0; i < p_sys->idx1.i_entry_count; i++ )
946 bo_add_fourcc( &bo, p_sys->idx1.entry[i].fcc );
947 bo_add_32le( &bo, p_sys->idx1.entry[i].i_flags );
948 bo_add_32le( &bo, p_sys->idx1.entry[i].i_pos );
949 bo_add_32le( &bo, p_sys->idx1.entry[i].i_length );
952 return( bo.b );