1 /*****************************************************************************
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 /*****************************************************************************
25 *****************************************************************************/
26 /* TODO: add OpenDML write support */
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
36 #include <vlc_block.h>
37 #include <vlc_codecs.h>
38 #include <vlc_boxes.h>
39 #include "../demux/avi/bitmapinfoheader.h"
41 /*****************************************************************************
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")
60 set_description( N_("AVI muxer") )
61 set_category( CAT_SOUT
)
62 set_subcategory( SUBCAT_SOUT_MUX
)
63 set_capability( "sout mux", 5 )
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
)
82 /*****************************************************************************
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
96 vlc_tick_t i_duration
; // in µs
98 int i_frames
; // total frame count
99 int64_t i_totalsize
; // total stream size
104 VLC_BITMAPINFOHEADER
*p_bih
;
110 typedef struct avi_idx1_entry_s
119 typedef struct avi_idx1_s
121 unsigned int i_entry_count
;
122 unsigned int i_entry_max
;
124 avi_idx1_entry_t
*entry
;
135 avi_stream_t stream
[100];
142 #define HDR_BASE_SIZE 512 /* single video&audio ~ 400 bytes header */
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
)
161 /*****************************************************************************
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
) );
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
)
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
;
194 p_mux
->p_sys
= p_sys
;
199 /*****************************************************************************
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
;
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
);
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
/
233 p_stream
->i_bitrate
=
234 8 * (uint64_t)1000000 *
235 (uint64_t)p_stream
->i_totalsize
/
236 (uint64_t)p_stream
->i_duration
;
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",
246 SEC_FROM_VLC_TICK(p_stream
->i_duration
),
247 p_stream
->i_totalsize
,
249 p_stream
->f_fps
, p_stream
->i_bitrate
/1024 );
252 p_hdr
= avi_HeaderCreateRIFF( p_mux
);
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
);
270 static int Control( sout_mux_t
*p_mux
, int i_query
, va_list args
)
278 case MUX_CAN_ADD_STREAM_WHILE_MUXING
:
279 pb_bool
= va_arg( args
, bool * );
284 ppsz
= va_arg( args
, char ** );
285 *ppsz
= strdup( "video/avi" );
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" );
304 msg_Dbg( p_mux
, "adding input" );
305 p_input
->p_sys
= malloc( sizeof( int ) );
306 if( !p_input
->p_sys
)
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
)
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
;
324 WAVEFORMATEX
*p_wf
= malloc( sizeof( WAVEFORMATEX
) +
325 p_input
->p_fmt
->i_extra
);
328 free( p_input
->p_sys
);
329 p_input
->p_sys
= NULL
;
333 p_wf
->cbSize
= p_input
->p_fmt
->i_extra
;
334 if( p_wf
->cbSize
> 0 )
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
)
349 p_wf
->wFormatTag
= WAVE_FORMAT_A52
;
350 p_wf
->nBlockAlign
= 1;
353 p_wf
->wFormatTag
= WAVE_FORMAT_MPEGLAYER3
;
354 p_wf
->nBlockAlign
= 1;
357 p_wf
->wFormatTag
= WAVE_FORMAT_WMA1
;
360 p_wf
->wFormatTag
= WAVE_FORMAT_WMA2
;
363 p_wf
->wFormatTag
= WAVE_FORMAT_WMAP
;
366 p_wf
->wFormatTag
= WAVE_FORMAT_WMAL
;
369 p_wf
->wFormatTag
= WAVE_FORMAT_ALAW
;
371 case VLC_CODEC_MULAW
:
372 p_wf
->wFormatTag
= WAVE_FORMAT_MULAW
;
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
;
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
;
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
;
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
;
405 free( p_input
->p_sys
);
406 p_input
->p_sys
= NULL
;
409 p_stream
->p_wf
= p_wf
;
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
;
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;
440 p_stream
->f_fps
= 25;
441 p_stream
->i_bitrate
= 128 * 1024;
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
,
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
);
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
;
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
;
506 if( p_sys
->b_write_header
)
508 msg_Dbg( p_mux
, "writing header" );
510 block_t
*p_hdr
= avi_HeaderCreateRIFF( p_mux
);
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
++ )
521 block_fifo_t
*p_fifo
;
523 if (!p_mux
->pp_inputs
[i
]->p_sys
)
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
);
533 avi_idx1_entry_t
*p_idx
;
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
)
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
);
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 );
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
);
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 );
587 p_data
->p_buffer
[ p_data
->i_buffer
- 1 ] = '\0';
592 p_sys
->i_movi_size
+= p_data
->i_buffer
;
593 sout_AccessOutWrite( p_mux
->p_access
, p_data
);
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 ); \
626 static int avi_HeaderAdd_avih( sout_mux_t
*p_mux
,
629 sout_mux_sys_t
*p_sys
= p_mux
->p_sys
;
630 avi_stream_t
*p_video
= NULL
;
632 uint32_t i_microsecperframe
;
633 int i_maxbytespersec
;
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 )
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
;
655 msg_Warn( p_mux
, "avi file without video track isn't a good idea..." );
656 i_microsecperframe
= 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 )
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
|
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 */
682 bo_add_32le( p_bo
, p_video
->p_bih
->biWidth
);
683 bo_add_32le( p_bo
, p_video
->p_bih
->biHeight
);
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 ); /* ???? */
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
)
705 bo_add_fourcc( p_bo
, "vids" );
706 if( p_stream
->p_bih
->biBitCount
)
707 bo_add_fourcc( p_bo
, "DIB " );
709 #ifdef WORDS_BIGENDIAN
710 bo_add_32be( p_bo
, p_stream
->p_bih
->biCompression
);
712 bo_add_32le( p_bo
, p_stream
->p_bih
->biCompression
);
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
);
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;
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 );
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
)
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] );
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
);
796 bo_add_32le( p_bo
, p_stream
->p_bih
->biCompression
);
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
);
804 p_stream
->i_bih
- sizeof( VLC_BITMAPINFOHEADER
),
805 (uint8_t*)&p_stream
->p_bih
[1] );
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
);
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
++ );
833 static int avi_HeaderAdd_INFO( sout_mux_t
*p_mux
, bo_t
*p_bo
)
837 #define APPLY_META(var, fourcc) \
838 psz = var_InheritString( p_mux, SOUT_CFG_PREFIX var );\
841 avi_HeaderAdd_meta( p_bo, fourcc, 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 ?? */
863 static block_t
*avi_HeaderCreateRIFF( sout_mux_t
*p_mux
)
865 sout_mux_sys_t
*p_sys
= p_mux
->p_sys
;
877 if (! bo_init( &bo
, HDR_BASE_SIZE
) )
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
++ )
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
);
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
;
935 i_idx1_size
= 16 * p_sys
->idx1
.i_entry_count
+ 8;
937 if (!i_idx1_size
|| !bo_init( &bo
, i_idx1_size
) )
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
);