codec: subsdec: fix variable shadowing
[vlc.git] / modules / mux / avi.c
blob0f964687387c4508533c5929fed407bad78337d3
1 /*****************************************************************************
2 * avi.c
3 *****************************************************************************
4 * Copyright (C) 2001-2009 VLC authors and VideoLAN
5 * $Id$
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * 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>
39 #include <vlc_boxes.h>
40 #include "../demux/avi/bitmapinfoheader.h"
42 /*****************************************************************************
43 * Module descriptor
44 *****************************************************************************/
45 static int Open ( vlc_object_t * );
46 static void Close ( vlc_object_t * );
48 #define SOUT_CFG_PREFIX "sout-avi-"
50 #define CFG_ARTIST_TEXT N_("Artist")
51 #define CFG_DATE_TEXT N_("Date")
52 #define CFG_GENRE_TEXT N_("Genre")
53 #define CFG_COPYRIGHT_TEXT N_("Copyright")
54 #define CFG_COMMENT_TEXT N_("Comment")
55 #define CFG_NAME_TEXT N_("Name")
56 #define CFG_SUBJECT_TEXT N_("Subject")
57 #define CFG_ENCODER_TEXT N_("Encoder")
58 #define CFG_KEYWORDS_TEXT N_("Keywords")
60 vlc_module_begin ()
61 set_description( N_("AVI muxer") )
62 set_category( CAT_SOUT )
63 set_subcategory( SUBCAT_SOUT_MUX )
64 set_capability( "sout mux", 5 )
65 add_shortcut( "avi" )
67 add_string( SOUT_CFG_PREFIX "artist", NULL, CFG_ARTIST_TEXT, NULL, true )
68 add_string( SOUT_CFG_PREFIX "date", NULL, CFG_DATE_TEXT, NULL, true )
69 add_string( SOUT_CFG_PREFIX "genre", NULL, CFG_GENRE_TEXT, NULL, true )
70 add_string( SOUT_CFG_PREFIX "copyright", NULL, CFG_COPYRIGHT_TEXT, NULL, true )
71 add_string( SOUT_CFG_PREFIX "comment", NULL, CFG_COMMENT_TEXT, NULL, true )
72 add_string( SOUT_CFG_PREFIX "name", NULL, CFG_NAME_TEXT, NULL, true )
73 add_string( SOUT_CFG_PREFIX "subject", NULL, CFG_SUBJECT_TEXT, NULL, true )
74 add_string( SOUT_CFG_PREFIX "encoder",
75 "VLC Media Player - " VERSION_MESSAGE,
76 CFG_ENCODER_TEXT, NULL, true )
77 add_string( SOUT_CFG_PREFIX "keywords", NULL, CFG_KEYWORDS_TEXT, NULL, true )
79 set_callbacks( Open, Close )
80 vlc_module_end ()
83 /*****************************************************************************
84 * Local prototypes
85 *****************************************************************************/
86 static int Control( sout_mux_t *, int, va_list );
87 static int AddStream( sout_mux_t *, sout_input_t * );
88 static void DelStream( sout_mux_t *, sout_input_t * );
89 static int Mux ( sout_mux_t * );
91 typedef struct avi_stream_s
93 int i_cat;
95 char fcc[4];
97 vlc_tick_t i_duration; // in µs
99 int i_frames; // total frame count
100 int64_t i_totalsize; // total stream size
102 float f_fps;
103 int i_bitrate;
105 VLC_BITMAPINFOHEADER *p_bih;
106 size_t i_bih;
107 WAVEFORMATEX *p_wf;
109 } avi_stream_t;
111 typedef struct avi_idx1_entry_s
113 char fcc[4];
114 uint32_t i_flags;
115 uint32_t i_pos;
116 uint32_t i_length;
118 } avi_idx1_entry_t;
120 typedef struct avi_idx1_s
122 unsigned int i_entry_count;
123 unsigned int i_entry_max;
125 avi_idx1_entry_t *entry;
126 } avi_idx1_t;
128 typedef struct
130 bool b_write_header;
132 int i_streams;
133 int i_stream_video;
135 off_t i_movi_size;
136 avi_stream_t stream[100];
138 avi_idx1_t idx1;
139 off_t i_idx1_size;
141 } sout_mux_sys_t;
143 #define HDR_BASE_SIZE 512 /* single video&audio ~ 400 bytes header */
145 /* Flags in avih */
146 #define AVIF_HASINDEX 0x00000010 // Index at end of file?
147 #define AVIF_ISINTERLEAVED 0x00000100
148 #define AVIF_TRUSTCKTYPE 0x00000800 // Use CKType to find key frames?
150 /* Flags for index */
151 #define AVIIF_KEYFRAME 0x00000010L /* this frame is a key frame.*/
154 static block_t *avi_HeaderCreateRIFF( sout_mux_t * );
155 static block_t *avi_HeaderCreateidx1( sout_mux_t * );
157 static void SetFCC( uint8_t *p, char *fcc )
159 memcpy( p, fcc, 4 );
162 /*****************************************************************************
163 * Open:
164 *****************************************************************************/
165 static int Open( vlc_object_t *p_this )
167 sout_mux_t *p_mux = (sout_mux_t*)p_this;
168 sout_mux_sys_t *p_sys;
170 msg_Dbg( p_mux, "AVI muxer opened" );
172 p_sys = malloc( sizeof( sout_mux_sys_t ) );
173 if( !p_sys )
174 return VLC_ENOMEM;
175 p_sys->i_streams = 0;
176 p_sys->i_stream_video = -1;
177 p_sys->i_movi_size = 0;
179 p_sys->idx1.i_entry_count = 0;
180 p_sys->idx1.i_entry_max = 10000;
181 p_sys->idx1.entry = calloc( p_sys->idx1.i_entry_max,
182 sizeof( avi_idx1_entry_t ) );
183 if( !p_sys->idx1.entry )
185 free( p_sys );
186 return VLC_ENOMEM;
188 p_sys->b_write_header = true;
191 p_mux->pf_control = Control;
192 p_mux->pf_addstream = AddStream;
193 p_mux->pf_delstream = DelStream;
194 p_mux->pf_mux = Mux;
195 p_mux->p_sys = p_sys;
197 return VLC_SUCCESS;
200 /*****************************************************************************
201 * Close:
202 *****************************************************************************/
203 static void Close( vlc_object_t * p_this )
205 sout_mux_t *p_mux = (sout_mux_t*)p_this;
206 sout_mux_sys_t *p_sys = p_mux->p_sys;
208 block_t *p_hdr, *p_idx1;
209 int i_stream;
211 msg_Dbg( p_mux, "AVI muxer closed" );
213 /* first create idx1 chunk (write at the end of the stream */
214 p_idx1 = avi_HeaderCreateidx1( p_mux );
215 if( p_idx1 )
217 p_sys->i_idx1_size = p_idx1->i_buffer;
218 sout_AccessOutWrite( p_mux->p_access, p_idx1 );
220 else p_sys->i_idx1_size = 0;
222 /* calculate some value for headers creations */
223 for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
225 avi_stream_t *p_stream;
227 p_stream = &p_sys->stream[i_stream];
229 if( p_stream->i_duration > 0 )
231 p_stream->f_fps = (float)p_stream->i_frames /
232 ( (float)p_stream->i_duration /
233 (float)CLOCK_FREQ );
234 p_stream->i_bitrate =
235 8 * (uint64_t)1000000 *
236 (uint64_t)p_stream->i_totalsize /
237 (uint64_t)p_stream->i_duration;
239 else
241 p_stream->f_fps = 25;
242 p_stream->i_bitrate = 128 * 1024;
244 msg_Info( p_mux, "stream[%d] duration:%"PRId64" totalsize:%"PRId64
245 " frames:%d fps:%f KiB/s:%d",
246 i_stream,
247 SEC_FROM_VLC_TICK(p_stream->i_duration),
248 p_stream->i_totalsize,
249 p_stream->i_frames,
250 p_stream->f_fps, p_stream->i_bitrate/1024 );
253 p_hdr = avi_HeaderCreateRIFF( p_mux );
254 if ( p_hdr )
256 sout_AccessOutSeek( p_mux->p_access, 0 );
257 sout_AccessOutWrite( p_mux->p_access, p_hdr );
260 for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
262 avi_stream_t *p_stream;
263 p_stream = &p_sys->stream[i_stream];
264 free( p_stream->p_bih );
265 free( p_stream->p_wf );
267 free( p_sys->idx1.entry );
268 free( p_sys );
271 static int Control( sout_mux_t *p_mux, int i_query, va_list args )
273 VLC_UNUSED(p_mux);
274 bool *pb_bool;
275 char **ppsz;
277 switch( i_query )
279 case MUX_CAN_ADD_STREAM_WHILE_MUXING:
280 pb_bool = va_arg( args, bool * );
281 *pb_bool = false;
282 return VLC_SUCCESS;
284 case MUX_GET_ADD_STREAM_WAIT:
285 pb_bool = va_arg( args, bool * );
286 *pb_bool = true;
287 return VLC_SUCCESS;
289 case MUX_GET_MIME:
290 ppsz = va_arg( args, char ** );
291 *ppsz = strdup( "video/avi" );
292 return VLC_SUCCESS;
294 default:
295 return VLC_EGENERIC;
299 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
301 sout_mux_sys_t *p_sys = p_mux->p_sys;
302 avi_stream_t *p_stream;
304 if( p_sys->i_streams >= 100 )
306 msg_Err( p_mux, "too many streams" );
307 return VLC_EGENERIC;
310 msg_Dbg( p_mux, "adding input" );
311 p_input->p_sys = malloc( sizeof( int ) );
312 if( !p_input->p_sys )
313 return VLC_ENOMEM;
315 *((int*)p_input->p_sys) = p_sys->i_streams;
316 p_stream = &p_sys->stream[p_sys->i_streams];
318 switch( p_input->p_fmt->i_cat )
320 case AUDIO_ES:
321 p_stream->i_cat = AUDIO_ES;
322 p_stream->fcc[0] = '0' + p_sys->i_streams / 10;
323 p_stream->fcc[1] = '0' + p_sys->i_streams % 10;
324 p_stream->fcc[2] = 'w';
325 p_stream->fcc[3] = 'b';
327 p_stream->p_bih = NULL;
328 p_stream->i_bih = 0;
330 WAVEFORMATEX *p_wf = malloc( sizeof( WAVEFORMATEX ) +
331 p_input->p_fmt->i_extra );
332 if( !p_wf )
334 free( p_input->p_sys );
335 p_input->p_sys = NULL;
336 return VLC_ENOMEM;
339 p_wf->cbSize = p_input->p_fmt->i_extra;
340 if( p_wf->cbSize > 0 )
342 memcpy( &p_wf[1],
343 p_input->p_fmt->p_extra,
344 p_input->p_fmt->i_extra );
346 p_wf->nChannels = p_input->p_fmt->audio.i_channels;
347 p_wf->nSamplesPerSec = p_input->p_fmt->audio.i_rate;
348 p_wf->nBlockAlign = p_input->p_fmt->audio.i_blockalign;
349 p_wf->nAvgBytesPerSec= p_input->p_fmt->i_bitrate / 8;
350 p_wf->wBitsPerSample = 0;
352 switch( p_input->p_fmt->i_codec )
354 case VLC_CODEC_A52:
355 p_wf->wFormatTag = WAVE_FORMAT_A52;
356 p_wf->nBlockAlign= 1;
357 break;
358 case VLC_CODEC_MP3:
359 p_wf->wFormatTag = WAVE_FORMAT_MPEGLAYER3;
360 p_wf->nBlockAlign= 1;
361 break;
362 case VLC_CODEC_WMA1:
363 p_wf->wFormatTag = WAVE_FORMAT_WMA1;
364 break;
365 case VLC_CODEC_WMA2:
366 p_wf->wFormatTag = WAVE_FORMAT_WMA2;
367 break;
368 case VLC_CODEC_WMAP:
369 p_wf->wFormatTag = WAVE_FORMAT_WMAP;
370 break;
371 case VLC_CODEC_WMAL:
372 p_wf->wFormatTag = WAVE_FORMAT_WMAL;
373 break;
374 case VLC_CODEC_ALAW:
375 p_wf->wFormatTag = WAVE_FORMAT_ALAW;
376 break;
377 case VLC_CODEC_MULAW:
378 p_wf->wFormatTag = WAVE_FORMAT_MULAW;
379 break;
380 /* raw codec */
381 case VLC_CODEC_U8:
382 p_wf->wFormatTag = WAVE_FORMAT_PCM;
383 p_wf->nBlockAlign= p_wf->nChannels;
384 p_wf->wBitsPerSample = 8;
385 p_wf->nAvgBytesPerSec = (p_wf->wBitsPerSample/8) *
386 p_wf->nSamplesPerSec * p_wf->nChannels;
387 break;
388 case VLC_CODEC_S16L:
389 p_wf->wFormatTag = WAVE_FORMAT_PCM;
390 p_wf->nBlockAlign= 2 * p_wf->nChannels;
391 p_wf->wBitsPerSample = 16;
392 p_wf->nAvgBytesPerSec = (p_wf->wBitsPerSample/8) *
393 p_wf->nSamplesPerSec * p_wf->nChannels;
394 break;
395 case VLC_CODEC_S24L:
396 p_wf->wFormatTag = WAVE_FORMAT_PCM;
397 p_wf->nBlockAlign= 3 * p_wf->nChannels;
398 p_wf->wBitsPerSample = 24;
399 p_wf->nAvgBytesPerSec = (p_wf->wBitsPerSample/8) *
400 p_wf->nSamplesPerSec * p_wf->nChannels;
401 break;
402 case VLC_CODEC_S32L:
403 p_wf->wFormatTag = WAVE_FORMAT_PCM;
404 p_wf->nBlockAlign= 4 * p_wf->nChannels;
405 p_wf->wBitsPerSample = 32;
406 p_wf->nAvgBytesPerSec = (p_wf->wBitsPerSample/8) *
407 p_wf->nSamplesPerSec * p_wf->nChannels;
408 break;
409 default:
410 free( p_wf );
411 free( p_input->p_sys );
412 p_input->p_sys = NULL;
413 return VLC_EGENERIC;
415 p_stream->p_wf = p_wf;
416 break;
417 case VIDEO_ES:
418 p_stream->i_cat = VIDEO_ES;
419 p_stream->fcc[0] = '0' + p_sys->i_streams / 10;
420 p_stream->fcc[1] = '0' + p_sys->i_streams % 10;
421 p_stream->fcc[2] = 'd';
422 p_stream->fcc[3] = 'c';
423 if( p_sys->i_stream_video < 0 )
425 p_sys->i_stream_video = p_sys->i_streams;
427 p_stream->p_wf = NULL;
428 p_stream->p_bih = CreateBitmapInfoHeader( &p_input->fmt, &p_stream->i_bih );
429 if( !p_stream->p_bih )
431 free( p_input->p_sys );
432 p_input->p_sys = NULL;
433 return VLC_ENOMEM;
435 break;
436 default:
437 free( p_input->p_sys );
438 p_input->p_sys = NULL;
439 return( VLC_EGENERIC );
441 p_stream->i_totalsize = 0;
442 p_stream->i_frames = 0;
443 p_stream->i_duration = 0;
445 /* fixed later */
446 p_stream->f_fps = 25;
447 p_stream->i_bitrate = 128 * 1024;
449 p_sys->i_streams++;
450 return( VLC_SUCCESS );
453 static void DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
455 msg_Dbg( p_mux, "removing input" );
457 free( p_input->p_sys );
460 static int PrepareSamples( const avi_stream_t *p_stream,
461 const es_format_t *p_fmt,
462 block_t **pp_block )
464 if( p_stream->i_frames == 0 && p_stream->i_cat == VIDEO_ES )
466 /* Add header present at the end of BITMAP info header
467 to first frame in case of XVID */
468 if( p_stream->p_bih->biCompression == VLC_FOURCC( 'X', 'V', 'I', 'D' ) )
470 size_t i_header_length =
471 p_stream->p_bih->biSize - sizeof(VLC_BITMAPINFOHEADER);
472 *pp_block = block_Realloc( *pp_block, i_header_length,
473 (*pp_block)->i_buffer );
474 if( !*pp_block )
475 return VLC_ENOMEM;
476 memcpy((*pp_block)->p_buffer,&p_stream->p_bih[1], i_header_length);
480 /* RV24 is only BGR in AVI, and we can't use BI_BITFIELD */
481 if( p_stream->i_cat == VIDEO_ES &&
482 p_stream->p_bih->biCompression == BI_RGB &&
483 p_stream->p_bih->biBitCount == 24 &&
484 (p_fmt->video.i_bmask != 0xFF0000 ||
485 p_fmt->video.i_rmask != 0x0000FF) )
487 unsigned rshift = ctz(p_fmt->video.i_rmask);
488 unsigned gshift = ctz(p_fmt->video.i_gmask);
489 unsigned bshift = ctz(p_fmt->video.i_bmask);
491 uint8_t *p_data = (*pp_block)->p_buffer;
492 for( size_t i=0; i<(*pp_block)->i_buffer / 3; i++ )
494 uint8_t *p = &p_data[i*3];
495 /* reorder as BGR using shift value (done by FixRGB) */
496 uint32_t v = (p[0] << 16) | (p[1] << 8) | p[2];
497 p[0] = (v & p_fmt->video.i_bmask) >> bshift;
498 p[1] = (v & p_fmt->video.i_gmask) >> gshift;
499 p[2] = (v & p_fmt->video.i_rmask) >> rshift;
503 return VLC_SUCCESS;
506 static int Mux ( sout_mux_t *p_mux )
508 sout_mux_sys_t *p_sys = p_mux->p_sys;
509 avi_stream_t *p_stream;
510 int i_stream, i;
512 if( p_sys->b_write_header )
514 msg_Dbg( p_mux, "writing header" );
516 block_t *p_hdr = avi_HeaderCreateRIFF( p_mux );
517 if ( !p_hdr )
518 return VLC_EGENERIC;
519 sout_AccessOutWrite( p_mux->p_access, p_hdr );
521 p_sys->b_write_header = false;
524 for( i = 0; i < p_mux->i_nb_inputs; i++ )
526 int i_count;
527 block_fifo_t *p_fifo;
529 if (!p_mux->pp_inputs[i]->p_sys)
530 continue;
532 i_stream = *((int*)p_mux->pp_inputs[i]->p_sys );
533 p_stream = &p_sys->stream[i_stream];
535 p_fifo = p_mux->pp_inputs[i]->p_fifo;
536 i_count = block_FifoCount( p_fifo );
537 while( i_count > 1 )
539 avi_idx1_entry_t *p_idx;
540 block_t *p_data;
542 p_data = block_FifoGet( p_fifo );
543 if( block_FifoCount( p_fifo ) > 0 )
545 block_t *p_next = block_FifoShow( p_fifo );
546 p_data->i_length = p_next->i_dts - p_data->i_dts;
549 if( PrepareSamples( p_stream, &p_mux->pp_inputs[i]->fmt,
550 &p_data ) != VLC_SUCCESS )
552 i_count--;
553 continue;
556 p_stream->i_frames++;
557 if( p_data->i_length < 0 )
559 msg_Warn( p_mux, "argg length < 0 l" );
560 block_Release( p_data );
561 i_count--;
562 continue;
564 p_stream->i_duration += p_data->i_length;
565 p_stream->i_totalsize += p_data->i_buffer;
567 /* add idx1 entry for this frame */
568 p_idx = &p_sys->idx1.entry[p_sys->idx1.i_entry_count];
569 memcpy( p_idx->fcc, p_stream->fcc, 4 );
570 p_idx->i_flags = 0;
571 if( ( p_data->i_flags & BLOCK_FLAG_TYPE_MASK ) == 0 || ( p_data->i_flags & BLOCK_FLAG_TYPE_I ) )
572 p_idx->i_flags = AVIIF_KEYFRAME;
573 p_idx->i_pos = p_sys->i_movi_size + 4;
574 p_idx->i_length= p_data->i_buffer;
575 p_sys->idx1.i_entry_count++;
576 if( p_sys->idx1.i_entry_count >= p_sys->idx1.i_entry_max )
578 p_sys->idx1.i_entry_max += 10000;
579 p_sys->idx1.entry = xrealloc( p_sys->idx1.entry,
580 p_sys->idx1.i_entry_max * sizeof( avi_idx1_entry_t ) );
583 p_data = block_Realloc( p_data, 8, p_data->i_buffer );
584 if( p_data )
586 SetFCC( p_data->p_buffer, p_stream->fcc );
587 SetDWLE( p_data->p_buffer + 4, p_data->i_buffer - 8 );
589 if( p_data->i_buffer & 0x01 )
591 p_data = block_Realloc( p_data, 0, p_data->i_buffer + 1 );
592 if ( p_data )
593 p_data->p_buffer[ p_data->i_buffer - 1 ] = '\0';
596 if ( p_data )
598 p_sys->i_movi_size += p_data->i_buffer;
599 sout_AccessOutWrite( p_mux->p_access, p_data );
603 i_count--;
607 return( 0 );
610 /****************************************************************************
611 ****************************************************************************
613 ** avi header generation
615 ****************************************************************************
616 ****************************************************************************/
617 #define AVI_BOX_ENTER( fcc ) \
618 int i_datasize_offset; \
619 bo_add_fourcc( p_bo, fcc ); \
620 i_datasize_offset = p_bo->b->i_buffer; \
621 bo_add_32le( p_bo, 0 )
623 #define AVI_BOX_ENTER_LIST( fcc ) \
624 AVI_BOX_ENTER( "LIST" ); \
625 bo_add_fourcc( p_bo, fcc )
627 #define AVI_BOX_EXIT( i_err ) \
628 if( p_bo->b->i_buffer&0x01 ) bo_add_8( p_bo, 0 ); \
629 bo_set_32le( p_bo, i_datasize_offset, p_bo->b->i_buffer - i_datasize_offset - 4 ); \
630 return( i_err );
632 static int avi_HeaderAdd_avih( sout_mux_t *p_mux,
633 bo_t *p_bo )
635 sout_mux_sys_t *p_sys = p_mux->p_sys;
636 avi_stream_t *p_video = NULL;
637 int i_stream;
638 uint32_t i_microsecperframe;
639 int i_maxbytespersec;
640 int i_totalframes;
641 AVI_BOX_ENTER( "avih" );
643 if( p_sys->i_stream_video >= 0 )
645 p_video = &p_sys->stream[p_sys->i_stream_video];
646 if( p_video->i_frames <= 0 )
648 // p_video = NULL;
652 if( p_video )
654 i_microsecperframe =
655 (uint32_t)( (float)1000000 /
656 (float)p_sys->stream[p_sys->i_stream_video].f_fps );
657 i_totalframes = p_sys->stream[p_sys->i_stream_video].i_frames;
659 else
661 msg_Warn( p_mux, "avi file without video track isn't a good idea..." );
662 i_microsecperframe = 0;
663 i_totalframes = 0;
666 for( i_stream = 0,i_maxbytespersec = 0; i_stream < p_sys->i_streams; i_stream++ )
668 if( p_sys->stream[i_stream].i_duration > 0 )
670 i_maxbytespersec +=
671 p_sys->stream[i_stream].i_totalsize /
672 p_sys->stream[i_stream].i_duration;
676 bo_add_32le( p_bo, i_microsecperframe );
677 bo_add_32le( p_bo, i_maxbytespersec );
678 bo_add_32le( p_bo, 0 ); /* padding */
679 bo_add_32le( p_bo, AVIF_TRUSTCKTYPE |
680 AVIF_HASINDEX |
681 AVIF_ISINTERLEAVED ); /* flags */
682 bo_add_32le( p_bo, i_totalframes );
683 bo_add_32le( p_bo, 0 ); /* initial frame */
684 bo_add_32le( p_bo, p_sys->i_streams ); /* streams count */
685 bo_add_32le( p_bo, 1024 * 1024 ); /* suggested buffer size */
686 if( p_video )
688 bo_add_32le( p_bo, p_video->p_bih->biWidth );
689 bo_add_32le( p_bo, p_video->p_bih->biHeight );
691 else
693 bo_add_32le( p_bo, 0 );
694 bo_add_32le( p_bo, 0 );
696 bo_add_32le( p_bo, 0 ); /* ???? */
697 bo_add_32le( p_bo, 0 ); /* ???? */
698 bo_add_32le( p_bo, 0 ); /* ???? */
699 bo_add_32le( p_bo, 0 ); /* ???? */
701 AVI_BOX_EXIT( 0 );
703 static int avi_HeaderAdd_strh( bo_t *p_bo, avi_stream_t *p_stream )
705 AVI_BOX_ENTER( "strh" );
707 switch( p_stream->i_cat )
709 case VIDEO_ES:
711 bo_add_fourcc( p_bo, "vids" );
712 if( p_stream->p_bih->biBitCount )
713 bo_add_fourcc( p_bo, "DIB " );
714 else
715 #ifdef WORDS_BIGENDIAN
716 bo_add_32be( p_bo, p_stream->p_bih->biCompression );
717 #else
718 bo_add_32le( p_bo, p_stream->p_bih->biCompression );
719 #endif
720 bo_add_32le( p_bo, 0 ); /* flags */
721 bo_add_16le( p_bo, 0 ); /* priority */
722 bo_add_16le( p_bo, 0 ); /* langage */
723 bo_add_32le( p_bo, 0 ); /* initial frame */
724 bo_add_32le( p_bo, 1000 );/* scale */
725 bo_add_32le( p_bo, (uint32_t)( 1000 * p_stream->f_fps ));
726 bo_add_32le( p_bo, 0 ); /* start */
727 bo_add_32le( p_bo, p_stream->i_frames );
728 bo_add_32le( p_bo, 1024 * 1024 );
729 bo_add_32le( p_bo, -1 ); /* quality */
730 bo_add_32le( p_bo, 0 ); /* samplesize */
731 bo_add_16le( p_bo, 0 ); /* ??? */
732 bo_add_16le( p_bo, 0 ); /* ??? */
733 bo_add_16le( p_bo, p_stream->p_bih->biWidth );
734 bo_add_16le( p_bo, p_stream->p_bih->biHeight );
736 break;
737 case AUDIO_ES:
739 int i_rate, i_scale, i_samplesize;
741 i_samplesize = p_stream->p_wf->nBlockAlign;
742 if( i_samplesize > 1 )
744 i_scale = i_samplesize;
745 i_rate = /*i_scale **/ p_stream->i_bitrate / 8;
747 else
749 i_samplesize = 1;
750 i_scale = 1000;
751 i_rate = 1000 * p_stream->i_bitrate / 8;
753 bo_add_fourcc( p_bo, "auds" );
754 bo_add_32le( p_bo, 0 ); /* tag */
755 bo_add_32le( p_bo, 0 ); /* flags */
756 bo_add_16le( p_bo, 0 ); /* priority */
757 bo_add_16le( p_bo, 0 ); /* langage */
758 bo_add_32le( p_bo, 0 ); /* initial frame */
759 bo_add_32le( p_bo, i_scale );/* scale */
760 bo_add_32le( p_bo, i_rate );
761 bo_add_32le( p_bo, 0 ); /* start */
762 bo_add_32le( p_bo, p_stream->i_frames );
763 bo_add_32le( p_bo, 10 * 1024 );
764 bo_add_32le( p_bo, -1 ); /* quality */
765 bo_add_32le( p_bo, i_samplesize );
766 bo_add_16le( p_bo, 0 ); /* ??? */
767 bo_add_16le( p_bo, 0 ); /* ??? */
768 bo_add_16le( p_bo, 0 );
769 bo_add_16le( p_bo, 0 );
771 break;
774 AVI_BOX_EXIT( 0 );
777 static int avi_HeaderAdd_strf( bo_t *p_bo, avi_stream_t *p_stream )
779 AVI_BOX_ENTER( "strf" );
781 switch( p_stream->i_cat )
783 case AUDIO_ES:
784 bo_add_16le( p_bo, p_stream->p_wf->wFormatTag );
785 bo_add_16le( p_bo, p_stream->p_wf->nChannels );
786 bo_add_32le( p_bo, p_stream->p_wf->nSamplesPerSec );
787 bo_add_32le( p_bo, p_stream->p_wf->nAvgBytesPerSec );
788 bo_add_16le( p_bo, p_stream->p_wf->nBlockAlign );
789 bo_add_16le( p_bo, p_stream->p_wf->wBitsPerSample );
790 bo_add_16le( p_bo, p_stream->p_wf->cbSize );
791 bo_add_mem( p_bo, p_stream->p_wf->cbSize, (uint8_t*)&p_stream->p_wf[1] );
792 break;
793 case VIDEO_ES:
794 bo_add_32le( p_bo, p_stream->p_bih->biSize );
795 bo_add_32le( p_bo, p_stream->p_bih->biWidth );
796 bo_add_32le( p_bo, p_stream->p_bih->biHeight );
797 bo_add_16le( p_bo, p_stream->p_bih->biPlanes );
798 bo_add_16le( p_bo, p_stream->p_bih->biBitCount );
799 #ifdef WORDS_BIGENDIAN
800 bo_add_32be( p_bo, p_stream->p_bih->biCompression );
801 #else
802 bo_add_32le( p_bo, p_stream->p_bih->biCompression );
803 #endif
804 bo_add_32le( p_bo, p_stream->p_bih->biSizeImage );
805 bo_add_32le( p_bo, p_stream->p_bih->biXPelsPerMeter );
806 bo_add_32le( p_bo, p_stream->p_bih->biYPelsPerMeter );
807 bo_add_32le( p_bo, p_stream->p_bih->biClrUsed );
808 bo_add_32le( p_bo, p_stream->p_bih->biClrImportant );
809 bo_add_mem( p_bo,
810 p_stream->i_bih - sizeof( VLC_BITMAPINFOHEADER ),
811 (uint8_t*)&p_stream->p_bih[1] );
812 break;
815 AVI_BOX_EXIT( 0 );
818 static int avi_HeaderAdd_strl( bo_t *p_bo, avi_stream_t *p_stream )
820 AVI_BOX_ENTER_LIST( "strl" );
822 avi_HeaderAdd_strh( p_bo, p_stream );
823 avi_HeaderAdd_strf( p_bo, p_stream );
825 AVI_BOX_EXIT( 0 );
828 static int avi_HeaderAdd_meta( bo_t *p_bo, const char psz_meta[4],
829 const char *psz_data )
831 if ( psz_data == NULL ) return 1;
832 const char *psz = psz_data;
833 AVI_BOX_ENTER( psz_meta );
834 while (*psz) bo_add_8( p_bo, *psz++ );
835 bo_add_8( p_bo, 0 );
836 AVI_BOX_EXIT( 0 );
839 static int avi_HeaderAdd_INFO( sout_mux_t *p_mux, bo_t *p_bo )
841 char *psz;
843 #define APPLY_META(var, fourcc) \
844 psz = var_InheritString( p_mux, SOUT_CFG_PREFIX var );\
845 if ( psz )\
847 avi_HeaderAdd_meta( p_bo, fourcc, psz );\
848 free( psz );\
851 AVI_BOX_ENTER_LIST( "INFO" );
853 APPLY_META( "artist", "IART")
854 APPLY_META( "comment", "ICMT")
855 APPLY_META( "copyright","ICOP")
856 APPLY_META( "date", "ICRD")
857 APPLY_META( "genre", "IGNR")
858 APPLY_META( "name", "INAM")
859 APPLY_META( "keywords", "IKEY")
860 APPLY_META( "subject", "ISBJ")
861 APPLY_META( "encoder", "ISFT")
862 /* Some are missing, but are they really useful ?? */
864 #undef APPLY_META
866 AVI_BOX_EXIT( 0 );
869 static block_t *avi_HeaderCreateRIFF( sout_mux_t *p_mux )
871 sout_mux_sys_t *p_sys = p_mux->p_sys;
872 int i_stream;
873 int i_junk;
874 bo_t bo;
876 struct
878 int i_riffsize;
879 int i_hdrllistsize;
880 int i_hdrldatastart;
881 } offsets;
883 if (! bo_init( &bo, HDR_BASE_SIZE ) )
884 return NULL;
886 bo_add_fourcc( &bo, "RIFF" );
887 offsets.i_riffsize = bo.b->i_buffer;
888 bo_add_32le( &bo, 0xEFBEADDE );
889 bo_add_fourcc( &bo, "AVI " );
891 bo_add_fourcc( &bo, "LIST" );
892 /* HDRL List size should exclude following data in HDR buffer
893 * -12 (RIFF, RIFF size, 'AVI ' tag),
894 * - 8 (hdr1 LIST tag and its size)
895 * - 12 (movi LIST tag, size, 'movi' listType )
897 offsets.i_hdrllistsize = bo.b->i_buffer;
898 bo_add_32le( &bo, 0xEFBEADDE );
899 bo_add_fourcc( &bo, "hdrl" );
900 offsets.i_hdrldatastart = bo.b->i_buffer;
902 avi_HeaderAdd_avih( p_mux, &bo );
903 for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
905 avi_HeaderAdd_strl( &bo, &p_sys->stream[i_stream] );
908 /* align on 16 bytes */
909 int i_align = ( ( bo.b->i_buffer + 12 + 0xE ) & ~ 0xF );
910 i_junk = i_align - bo.b->i_buffer;
911 bo_add_fourcc( &bo, "JUNK" );
912 bo_add_32le( &bo, i_junk );
913 for( int i=0; i< i_junk; i++ )
915 bo_add_8( &bo, 0 );
918 /* Now set hdrl size */
919 bo_set_32le( &bo, offsets.i_hdrllistsize,
920 bo.b->i_buffer - offsets.i_hdrldatastart );
922 avi_HeaderAdd_INFO( p_mux, &bo );
924 bo_add_fourcc( &bo, "LIST" );
925 bo_add_32le( &bo, p_sys->i_movi_size + 4 );
926 bo_add_fourcc( &bo, "movi" );
928 /* Now set RIFF size */
929 bo_set_32le( &bo, offsets.i_riffsize, bo.b->i_buffer - 8
930 + p_sys->i_movi_size + p_sys->i_idx1_size );
932 return( bo.b );
935 static block_t * avi_HeaderCreateidx1( sout_mux_t *p_mux )
937 sout_mux_sys_t *p_sys = p_mux->p_sys;
938 uint32_t i_idx1_size;
939 bo_t bo;
941 i_idx1_size = 16 * p_sys->idx1.i_entry_count + 8;
943 if (!i_idx1_size || !bo_init( &bo, i_idx1_size ) )
944 return NULL;
945 memset( bo.b->p_buffer, 0, i_idx1_size);
947 bo_add_fourcc( &bo, "idx1" );
948 bo_add_32le( &bo, i_idx1_size - 8);
950 for( unsigned i = 0; i < p_sys->idx1.i_entry_count; i++ )
952 bo_add_fourcc( &bo, p_sys->idx1.entry[i].fcc );
953 bo_add_32le( &bo, p_sys->idx1.entry[i].i_flags );
954 bo_add_32le( &bo, p_sys->idx1.entry[i].i_pos );
955 bo_add_32le( &bo, p_sys->idx1.entry[i].i_length );
958 return( bo.b );