Main Page | Modules | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages

avi.c

00001 /*****************************************************************************
00002  * avi.c
00003  *****************************************************************************
00004  * Copyright (C) 2001, 2002 the VideoLAN team
00005  * $Id: avi.c 11664 2005-07-09 06:17:09Z courmisch $
00006  *
00007  * Authors: Laurent Aimar <[email protected]>
00008  *
00009  * This program is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software
00021  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
00022  *****************************************************************************/
00023 
00024 /*****************************************************************************
00025  * Preamble
00026  *****************************************************************************/
00027 /* TODO: add OpenDML write support */
00028 
00029 #include <stdlib.h>
00030 
00031 #include <vlc/vlc.h>
00032 #include <vlc/input.h>
00033 #include <vlc/sout.h>
00034 
00035 #include "codecs.h"
00036 
00037 /*****************************************************************************
00038  * Module descriptor
00039  *****************************************************************************/
00040 static int  Open   ( vlc_object_t * );
00041 static void Close  ( vlc_object_t * );
00042 
00043 vlc_module_begin();
00044     set_description( _("AVI muxer") );
00045     set_category( CAT_SOUT );
00046     set_subcategory( SUBCAT_SOUT_MUX );
00047     set_capability( "sout mux", 5 );
00048     add_shortcut( "avi" );
00049     set_callbacks( Open, Close );
00050 vlc_module_end();
00051 
00052 
00053 /*****************************************************************************
00054  * Local prototypes
00055  *****************************************************************************/
00056 static int Control( sout_mux_t *, int, va_list );
00057 static int AddStream( sout_mux_t *, sout_input_t * );
00058 static int DelStream( sout_mux_t *, sout_input_t * );
00059 static int Mux      ( sout_mux_t * );
00060 
00061 typedef struct avi_stream_s
00062 {
00063     int i_cat;
00064 
00065     char fcc[4];
00066 
00067     mtime_t i_duration;       // in µs
00068 
00069     int     i_frames;        // total frame count
00070     int64_t i_totalsize;    // total stream size
00071 
00072     float   f_fps;
00073     int     i_bitrate;
00074 
00075     BITMAPINFOHEADER    *p_bih;
00076     WAVEFORMATEX        *p_wf;
00077 
00078 } avi_stream_t;
00079 
00080 typedef struct avi_idx1_entry_s
00081 {
00082     char     fcc[4];
00083     uint32_t i_flags;
00084     uint32_t i_pos;
00085     uint32_t i_length;
00086 
00087 } avi_idx1_entry_t;
00088 
00089 typedef struct avi_idx1_s
00090 {
00091     unsigned int i_entry_count;
00092     unsigned int i_entry_max;
00093 
00094     avi_idx1_entry_t *entry;
00095 } avi_idx1_t;
00096 
00097 struct sout_mux_sys_t
00098 {
00099     vlc_bool_t b_write_header;
00100 
00101     int i_streams;
00102     int i_stream_video;
00103 
00104     off_t i_movi_size;
00105     avi_stream_t stream[100];
00106 
00107     avi_idx1_t idx1;
00108     off_t i_idx1_size;
00109 
00110 };
00111 
00112 // FIXME FIXME
00113 #define HDR_SIZE 10240
00114 
00115 /* Flags in avih */
00116 #define AVIF_HASINDEX       0x00000010  // Index at end of file?
00117 #define AVIF_ISINTERLEAVED  0x00000100
00118 #define AVIF_TRUSTCKTYPE    0x00000800  // Use CKType to find key frames?
00119 
00120 /* Flags for index */
00121 #define AVIIF_KEYFRAME      0x00000010L /* this frame is a key frame.*/
00122 
00123 
00124 static block_t *avi_HeaderCreateRIFF( sout_mux_t * );
00125 static block_t *avi_HeaderCreateidx1( sout_mux_t * );
00126 
00127 static void SetFCC( uint8_t *p, char *fcc )
00128 {
00129     memcpy( p, fcc, 4 );
00130 }
00131 
00132 /*****************************************************************************
00133  * Open:
00134  *****************************************************************************/
00135 static int Open( vlc_object_t *p_this )
00136 {
00137     sout_mux_t      *p_mux = (sout_mux_t*)p_this;
00138     sout_mux_sys_t  *p_sys = p_mux->p_sys;
00139 
00140     msg_Dbg( p_mux, "AVI muxer opened" );
00141 
00142     p_sys = malloc( sizeof( sout_mux_sys_t ) );
00143     p_sys->i_streams = 0;
00144     p_sys->i_stream_video = -1;
00145     p_sys->i_movi_size = 0;
00146 
00147     p_sys->idx1.i_entry_count = 0;
00148     p_sys->idx1.i_entry_max = 10000;
00149     p_sys->idx1.entry = calloc( p_sys->idx1.i_entry_max,
00150                                 sizeof( avi_idx1_entry_t ) );
00151     p_sys->b_write_header = VLC_TRUE;
00152 
00153 
00154     p_mux->pf_control   = Control;
00155     p_mux->pf_addstream = AddStream;
00156     p_mux->pf_delstream = DelStream;
00157     p_mux->pf_mux       = Mux;
00158     p_mux->p_sys        = p_sys;
00159 
00160     return VLC_SUCCESS;
00161 }
00162 
00163 /*****************************************************************************
00164  * Close:
00165  *****************************************************************************/
00166 static void Close( vlc_object_t * p_this )
00167 {
00168     sout_mux_t      *p_mux = (sout_mux_t*)p_this;
00169     sout_mux_sys_t  *p_sys = p_mux->p_sys;
00170 
00171     block_t       *p_hdr, *p_idx1;
00172     int                 i_stream;
00173 
00174     msg_Dbg( p_mux, "AVI muxer closed" );
00175 
00176     /* first create idx1 chunk (write at the end of the stream */
00177     p_idx1 = avi_HeaderCreateidx1( p_mux );
00178     p_sys->i_idx1_size = p_idx1->i_buffer;
00179     sout_AccessOutWrite( p_mux->p_access, p_idx1 );
00180 
00181     /* calculate some value for headers creations */
00182     for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
00183     {
00184         avi_stream_t *p_stream;
00185 
00186         p_stream = &p_sys->stream[i_stream];
00187 
00188         p_stream->f_fps = 25;
00189         if( p_stream->i_duration > 0 )
00190         {
00191             p_stream->f_fps = (float)p_stream->i_frames /
00192                               ( (float)p_stream->i_duration /
00193                                 (float)1000000 );
00194         }
00195         p_stream->i_bitrate = 128 * 1024;
00196         if( p_stream->i_duration > 0 )
00197         {
00198             p_stream->i_bitrate =
00199                 8 * (uint64_t)1000000 *
00200                     (uint64_t)p_stream->i_totalsize /
00201                     (uint64_t)p_stream->i_duration;
00202         }
00203         msg_Info( p_mux, "stream[%d] duration:"I64Fd" totalsize:"I64Fd
00204                   " frames:%d fps:%f kb/s:%d",
00205                   i_stream,
00206                   (int64_t)p_stream->i_duration / (int64_t)1000000,
00207                   p_stream->i_totalsize,
00208                   p_stream->i_frames,
00209                   p_stream->f_fps, p_stream->i_bitrate/1024 );
00210     }
00211 
00212     p_hdr = avi_HeaderCreateRIFF( p_mux );
00213     sout_AccessOutSeek( p_mux->p_access, 0 );
00214     sout_AccessOutWrite( p_mux->p_access, p_hdr );
00215 }
00216 
00217 static int Control( sout_mux_t *p_mux, int i_query, va_list args )
00218 {
00219     vlc_bool_t *pb_bool;
00220     char **ppsz;
00221 
00222    switch( i_query )
00223    {
00224        case MUX_CAN_ADD_STREAM_WHILE_MUXING:
00225            pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
00226            *pb_bool = VLC_FALSE;
00227            return VLC_SUCCESS;
00228 
00229        case MUX_GET_ADD_STREAM_WAIT:
00230            pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
00231            *pb_bool = VLC_TRUE;
00232            return VLC_SUCCESS;
00233 
00234        case MUX_GET_MIME:
00235            ppsz = (char**)va_arg( args, char ** );
00236            *ppsz = strdup( "video/avi" );
00237            return VLC_SUCCESS;
00238 
00239         default:
00240             return VLC_EGENERIC;
00241    }
00242 }
00243 
00244 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
00245 {
00246     sout_mux_sys_t  *p_sys = p_mux->p_sys;
00247     avi_stream_t    *p_stream;
00248 
00249     if( p_sys->i_streams >= 100 )
00250     {
00251         msg_Err( p_mux, "too many streams" );
00252         return( -1 );
00253     }
00254 
00255     msg_Dbg( p_mux, "adding input" );
00256     p_input->p_sys = malloc( sizeof( int ) );
00257 
00258     *((int*)p_input->p_sys) = p_sys->i_streams;
00259     p_stream = &p_sys->stream[p_sys->i_streams];
00260 
00261     switch( p_input->p_fmt->i_cat )
00262     {
00263         case AUDIO_ES:
00264             p_stream->i_cat = AUDIO_ES;
00265             p_stream->fcc[0] = '0' + p_sys->i_streams / 10;
00266             p_stream->fcc[1] = '0' + p_sys->i_streams % 10;
00267             p_stream->fcc[2] = 'w';
00268             p_stream->fcc[3] = 'b';
00269 
00270             p_stream->p_bih = NULL;
00271 
00272             p_stream->p_wf  = malloc( sizeof( WAVEFORMATEX ) +
00273                                       p_input->p_fmt->i_extra );
00274 #define p_wf p_stream->p_wf
00275             p_wf->cbSize = p_input->p_fmt->i_extra;
00276             if( p_wf->cbSize > 0 )
00277             {
00278                 memcpy( &p_wf[1],
00279                         p_input->p_fmt->p_extra,
00280                         p_input->p_fmt->i_extra );
00281             }
00282             p_wf->nChannels      = p_input->p_fmt->audio.i_channels;
00283             p_wf->nSamplesPerSec = p_input->p_fmt->audio.i_rate;
00284             p_wf->nBlockAlign    = p_input->p_fmt->audio.i_blockalign;
00285             p_wf->nAvgBytesPerSec= p_input->p_fmt->i_bitrate / 8;
00286             p_wf->wBitsPerSample = 0;
00287 
00288             switch( p_input->p_fmt->i_codec )
00289             {
00290                 case VLC_FOURCC( 'a', '5', '2', ' ' ):
00291                     p_wf->wFormatTag = WAVE_FORMAT_A52;
00292                     break;
00293                 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
00294                     p_wf->wFormatTag = WAVE_FORMAT_MPEGLAYER3;
00295                     break;
00296                 case VLC_FOURCC( 'w', 'm', 'a', '1' ):
00297                     p_wf->wFormatTag = WAVE_FORMAT_WMA1;
00298                     break;
00299                 case VLC_FOURCC( 'w', 'm', 'a', ' ' ):
00300                 case VLC_FOURCC( 'w', 'm', 'a', '2' ):
00301                     p_wf->wFormatTag = WAVE_FORMAT_WMA2;
00302                     break;
00303                 case VLC_FOURCC( 'w', 'm', 'a', 'p' ):
00304                     p_wf->wFormatTag = WAVE_FORMAT_WMAP;
00305                     break;
00306                 case VLC_FOURCC( 'w', 'm', 'a', 'l' ):
00307                     p_wf->wFormatTag = WAVE_FORMAT_WMAL;
00308                     break;
00309                     /* raw codec */
00310                 case VLC_FOURCC( 'u', '8', ' ', ' ' ):
00311                     p_wf->wFormatTag = WAVE_FORMAT_PCM;
00312                     p_wf->nBlockAlign= p_wf->nChannels;
00313                     p_wf->wBitsPerSample = 8;
00314                     break;
00315                 case VLC_FOURCC( 's', '1', '6', 'l' ):
00316                     p_wf->wFormatTag = WAVE_FORMAT_PCM;
00317                     p_wf->nBlockAlign= 2 * p_wf->nChannels;
00318                     p_wf->wBitsPerSample = 16;
00319                     break;
00320                 case VLC_FOURCC( 's', '2', '4', 'l' ):
00321                     p_wf->wFormatTag = WAVE_FORMAT_PCM;
00322                     p_wf->nBlockAlign= 3 * p_wf->nChannels;
00323                     p_wf->wBitsPerSample = 24;
00324                     break;
00325                 case VLC_FOURCC( 's', '3', '2', 'l' ):
00326                     p_wf->wFormatTag = WAVE_FORMAT_PCM;
00327                     p_wf->nBlockAlign= 4 * p_wf->nChannels;
00328                     p_wf->wBitsPerSample = 32;
00329                     break;
00330                 default:
00331                     return VLC_EGENERIC;
00332             }
00333 #undef p_wf
00334             break;
00335         case VIDEO_ES:
00336             p_stream->i_cat = VIDEO_ES;
00337             p_stream->fcc[0] = '0' + p_sys->i_streams / 10;
00338             p_stream->fcc[1] = '0' + p_sys->i_streams % 10;
00339             p_stream->fcc[2] = 'd';
00340             p_stream->fcc[3] = 'c';
00341             if( p_sys->i_stream_video < 0 )
00342             {
00343                 p_sys->i_stream_video = p_sys->i_streams;
00344             }
00345             p_stream->p_wf  = NULL;
00346             p_stream->p_bih = malloc( sizeof( BITMAPINFOHEADER ) +
00347                                       p_input->p_fmt->i_extra );
00348 #define p_bih p_stream->p_bih
00349             p_bih->biSize  = sizeof( BITMAPINFOHEADER ) +
00350                              p_input->p_fmt->i_extra;
00351             if( p_input->p_fmt->i_extra > 0 )
00352             {
00353                 memcpy( &p_bih[1],
00354                         p_input->p_fmt->p_extra,
00355                         p_input->p_fmt->i_extra );
00356             }
00357             p_bih->biWidth = p_input->p_fmt->video.i_width;
00358             p_bih->biHeight= p_input->p_fmt->video.i_height;
00359             p_bih->biPlanes= 1;
00360             p_bih->biBitCount       = 24;
00361             p_bih->biSizeImage      = 0;
00362             p_bih->biXPelsPerMeter  = 0;
00363             p_bih->biYPelsPerMeter  = 0;
00364             p_bih->biClrUsed        = 0;
00365             p_bih->biClrImportant   = 0;
00366             switch( p_input->p_fmt->i_codec )
00367             {
00368                 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
00369                     p_bih->biCompression = VLC_FOURCC( 'X', 'V', 'I', 'D' );
00370                     break;
00371                 default:
00372                     p_bih->biCompression = p_input->p_fmt->i_codec;
00373                     break;
00374             }
00375 #undef p_bih
00376             break;
00377         default:
00378             return( VLC_EGENERIC );
00379     }
00380     p_stream->i_totalsize = 0;
00381     p_stream->i_frames    = 0;
00382     p_stream->i_duration  = 0;
00383 
00384     /* fixed later */
00385     p_stream->f_fps = 25;
00386     p_stream->i_bitrate = 128 * 1024;
00387 
00388     p_sys->i_streams++;
00389     return( VLC_SUCCESS );
00390 }
00391 
00392 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
00393 {
00394 
00395     msg_Dbg( p_mux, "removing input" );
00396 
00397     free( p_input->p_sys );
00398     return( 0 );
00399 }
00400 
00401 static int Mux      ( sout_mux_t *p_mux )
00402 {
00403     sout_mux_sys_t  *p_sys = p_mux->p_sys;
00404     avi_stream_t    *p_stream;
00405     int i_stream;
00406     int i;
00407 
00408     if( p_sys->b_write_header )
00409     {
00410         block_t *p_hdr;
00411 
00412         msg_Dbg( p_mux, "writing header" );
00413 
00414         p_hdr = avi_HeaderCreateRIFF( p_mux );
00415         sout_AccessOutWrite( p_mux->p_access, p_hdr );
00416 
00417         p_sys->b_write_header = VLC_FALSE;
00418     }
00419 
00420     for( i = 0; i < p_mux->i_nb_inputs; i++ )
00421     {
00422         int i_count;
00423         block_fifo_t *p_fifo;
00424 
00425         i_stream = *((int*)p_mux->pp_inputs[i]->p_sys );
00426         p_stream = &p_sys->stream[i_stream];
00427 
00428         p_fifo = p_mux->pp_inputs[i]->p_fifo;
00429         i_count = p_fifo->i_depth;
00430         while( i_count > 1 )
00431         {
00432             avi_idx1_entry_t *p_idx;
00433             block_t *p_data;
00434 
00435             p_data = block_FifoGet( p_fifo );
00436             if( p_fifo->i_depth > 0 )
00437             {
00438                 block_t *p_next = block_FifoShow( p_fifo );
00439                 p_data->i_length = p_next->i_dts - p_data->i_dts;
00440             }
00441 
00442             p_stream->i_frames++;
00443             if( p_data->i_length < 0 )
00444             {
00445                 msg_Warn( p_mux, "argg length < 0 l" );
00446                 block_Release( p_data );
00447                 i_count--;
00448                 continue;
00449             }
00450             p_stream->i_duration  += p_data->i_length;
00451             p_stream->i_totalsize += p_data->i_buffer;
00452 
00453             /* add idx1 entry for this frame */
00454             p_idx = &p_sys->idx1.entry[p_sys->idx1.i_entry_count];
00455             memcpy( p_idx->fcc, p_stream->fcc, 4 );
00456             p_idx->i_flags = AVIIF_KEYFRAME;
00457             p_idx->i_pos   = p_sys->i_movi_size + 4;
00458             p_idx->i_length= p_data->i_buffer;
00459             p_sys->idx1.i_entry_count++;
00460             if( p_sys->idx1.i_entry_count >= p_sys->idx1.i_entry_max )
00461             {
00462                 p_sys->idx1.i_entry_max += 10000;
00463                 p_sys->idx1.entry = realloc( p_sys->idx1.entry,
00464                                              p_sys->idx1.i_entry_max * sizeof( avi_idx1_entry_t ) );
00465             }
00466 
00467             p_data = block_Realloc( p_data, 8, p_data->i_buffer );
00468             if( p_data )
00469             {
00470                 SetFCC( p_data->p_buffer, p_stream->fcc );
00471                 SetDWLE( p_data->p_buffer + 4, p_data->i_buffer - 8 );
00472 
00473                 if( p_data->i_buffer & 0x01 )
00474                 {
00475                     p_data = block_Realloc( p_data, 0, p_data->i_buffer + 1 );
00476                 }
00477 
00478                 p_sys->i_movi_size += p_data->i_buffer;
00479                 sout_AccessOutWrite( p_mux->p_access, p_data );
00480             }
00481 
00482             i_count--;
00483         }
00484 
00485     }
00486     return( 0 );
00487 }
00488 
00489 /****************************************************************************/
00490 /****************************************************************************/
00491 /****************************************************************************/
00492 /****************************************************************************/
00493 
00494 typedef struct buffer_out_s
00495 {
00496     int      i_buffer_size;
00497     int      i_buffer;
00498     uint8_t  *p_buffer;
00499 
00500 } buffer_out_t;
00501 
00502 static void bo_Init( buffer_out_t *p_bo, int i_size, uint8_t *p_buffer )
00503 {
00504     p_bo->i_buffer_size = i_size;
00505     p_bo->i_buffer = 0;
00506     p_bo->p_buffer = p_buffer;
00507 }
00508 static void bo_AddByte( buffer_out_t *p_bo, uint8_t i )
00509 {
00510     if( p_bo->i_buffer < p_bo->i_buffer_size )
00511     {
00512         p_bo->p_buffer[p_bo->i_buffer] = i;
00513     }
00514     p_bo->i_buffer++;
00515 }
00516 static void bo_AddWordLE( buffer_out_t *p_bo, uint16_t i )
00517 {
00518     bo_AddByte( p_bo, i &0xff );
00519     bo_AddByte( p_bo, ( ( i >> 8) &0xff ) );
00520 }
00521 static void bo_AddWordBE( buffer_out_t *p_bo, uint16_t i )
00522 {
00523     bo_AddByte( p_bo, ( ( i >> 8) &0xff ) );
00524     bo_AddByte( p_bo, i &0xff );
00525 }
00526 static void bo_AddDWordLE( buffer_out_t *p_bo, uint32_t i )
00527 {
00528     bo_AddWordLE( p_bo, i &0xffff );
00529     bo_AddWordLE( p_bo, ( ( i >> 16) &0xffff ) );
00530 }
00531 static void bo_AddDWordBE( buffer_out_t *p_bo, uint32_t i )
00532 {
00533     bo_AddWordBE( p_bo, ( ( i >> 16) &0xffff ) );
00534     bo_AddWordBE( p_bo, i &0xffff );
00535 }
00536 #if 0
00537 static void bo_AddLWordLE( buffer_out_t *p_bo, uint64_t i )
00538 {
00539     bo_AddDWordLE( p_bo, i &0xffffffff );
00540     bo_AddDWordLE( p_bo, ( ( i >> 32) &0xffffffff ) );
00541 }
00542 static void bo_AddLWordBE( buffer_out_t *p_bo, uint64_t i )
00543 {
00544     bo_AddDWordBE( p_bo, ( ( i >> 32) &0xffffffff ) );
00545     bo_AddDWordBE( p_bo, i &0xffffffff );
00546 }
00547 #endif
00548 
00549 static void bo_AddFCC( buffer_out_t *p_bo, char *fcc )
00550 {
00551     bo_AddByte( p_bo, fcc[0] );
00552     bo_AddByte( p_bo, fcc[1] );
00553     bo_AddByte( p_bo, fcc[2] );
00554     bo_AddByte( p_bo, fcc[3] );
00555 }
00556 
00557 static void bo_AddMem( buffer_out_t *p_bo, int i_size, uint8_t *p_mem )
00558 {
00559     int i;
00560 
00561     for( i = 0; i < i_size; i++ )
00562     {
00563         bo_AddByte( p_bo, p_mem[i] );
00564     }
00565 }
00566 
00567 /****************************************************************************
00568  ****************************************************************************
00569  **
00570  ** avi header generation
00571  **
00572  ****************************************************************************
00573  ****************************************************************************/
00574 #define AVI_BOX_ENTER( fcc ) \
00575     buffer_out_t _bo_sav_; \
00576     bo_AddFCC( p_bo, fcc ); \
00577     _bo_sav_ = *p_bo; \
00578     bo_AddDWordLE( p_bo, 0 )
00579 
00580 #define AVI_BOX_ENTER_LIST( fcc ) \
00581     AVI_BOX_ENTER( "LIST" ); \
00582     bo_AddFCC( p_bo, fcc )
00583 
00584 #define AVI_BOX_EXIT( i_err ) \
00585     if( p_bo->i_buffer&0x01 ) bo_AddByte( p_bo, 0 ); \
00586     bo_AddDWordLE( &_bo_sav_, p_bo->i_buffer - _bo_sav_.i_buffer - 4 ); \
00587     return( i_err );
00588 
00589 static int avi_HeaderAdd_avih( sout_mux_t *p_mux,
00590                                buffer_out_t *p_bo )
00591 {
00592     sout_mux_sys_t  *p_sys = p_mux->p_sys;
00593     avi_stream_t    *p_video = NULL;
00594     int         i_stream;
00595     uint32_t    i_microsecperframe;
00596     int         i_maxbytespersec;
00597     int         i_totalframes;
00598     AVI_BOX_ENTER( "avih" );
00599 
00600     if( p_sys->i_stream_video >= 0 )
00601     {
00602         p_video = &p_sys->stream[p_sys->i_stream_video];
00603         if( p_video->i_frames <= 0 )
00604         {
00605         //    p_video = NULL;
00606         }
00607     }
00608 
00609     if( p_video )
00610     {
00611         i_microsecperframe =
00612             (uint32_t)( (float)1000000 /
00613                         (float)p_sys->stream[p_sys->i_stream_video].f_fps );
00614         i_totalframes = p_sys->stream[p_sys->i_stream_video].i_frames;
00615     }
00616     else
00617     {
00618         msg_Warn( p_mux, "avi file without video track isn't a good idea..." );
00619         i_microsecperframe = 0;
00620         i_totalframes = 0;
00621     }
00622 
00623     for( i_stream = 0,i_maxbytespersec = 0; i_stream < p_sys->i_streams; i_stream++ )
00624     {
00625         if( p_sys->stream[i_stream].i_duration > 0 )
00626         {
00627             i_maxbytespersec +=
00628                 p_sys->stream[p_sys->i_stream_video].i_totalsize /
00629                 p_sys->stream[p_sys->i_stream_video].i_duration;
00630         }
00631     }
00632 
00633     bo_AddDWordLE( p_bo, i_microsecperframe );
00634     bo_AddDWordLE( p_bo, i_maxbytespersec );
00635     bo_AddDWordLE( p_bo, 0 );                   /* padding */
00636     bo_AddDWordLE( p_bo, AVIF_TRUSTCKTYPE |
00637                          AVIF_HASINDEX |
00638                          AVIF_ISINTERLEAVED );  /* flags */
00639     bo_AddDWordLE( p_bo, i_totalframes );
00640     bo_AddDWordLE( p_bo, 0 );                   /* initial frame */
00641     bo_AddDWordLE( p_bo, p_sys->i_streams );    /* streams count */
00642     bo_AddDWordLE( p_bo, 1024 * 1024 );         /* suggested buffer size */
00643     if( p_video )
00644     {
00645         bo_AddDWordLE( p_bo, p_video->p_bih->biWidth );
00646         bo_AddDWordLE( p_bo, p_video->p_bih->biHeight );
00647     }
00648     else
00649     {
00650         bo_AddDWordLE( p_bo, 0 );
00651         bo_AddDWordLE( p_bo, 0 );
00652     }
00653     bo_AddDWordLE( p_bo, 0 );                   /* ???? */
00654     bo_AddDWordLE( p_bo, 0 );                   /* ???? */
00655     bo_AddDWordLE( p_bo, 0 );                   /* ???? */
00656     bo_AddDWordLE( p_bo, 0 );                   /* ???? */
00657 
00658     AVI_BOX_EXIT( 0 );
00659 }
00660 static int avi_HeaderAdd_strh( sout_mux_t   *p_mux,
00661                                buffer_out_t *p_bo,
00662                                avi_stream_t *p_stream )
00663 {
00664     AVI_BOX_ENTER( "strh" );
00665 
00666     switch( p_stream->i_cat )
00667     {
00668         case VIDEO_ES:
00669             {
00670                 bo_AddFCC( p_bo, "vids" );
00671                 bo_AddDWordBE( p_bo, p_stream->p_bih->biCompression );
00672                 bo_AddDWordLE( p_bo, 0 );   /* flags */
00673                 bo_AddWordLE(  p_bo, 0 );   /* priority */
00674                 bo_AddWordLE(  p_bo, 0 );   /* langage */
00675                 bo_AddDWordLE( p_bo, 0 );   /* initial frame */
00676                 bo_AddDWordLE( p_bo, 1000 );/* scale */
00677                 bo_AddDWordLE( p_bo, (uint32_t)( 1000 * p_stream->f_fps ));
00678                 bo_AddDWordLE( p_bo, 0 );   /* start */
00679                 bo_AddDWordLE( p_bo, p_stream->i_frames );
00680                 bo_AddDWordLE( p_bo, 1024 * 1024 );
00681                 bo_AddDWordLE( p_bo, -1 );  /* quality */
00682                 bo_AddDWordLE( p_bo, 0 );   /* samplesize */
00683                 bo_AddWordLE(  p_bo, 0 );   /* ??? */
00684                 bo_AddWordLE(  p_bo, 0 );   /* ??? */
00685                 bo_AddWordLE(  p_bo, p_stream->p_bih->biWidth );
00686                 bo_AddWordLE(  p_bo, p_stream->p_bih->biHeight );
00687             }
00688             break;
00689         case AUDIO_ES:
00690             {
00691                 int i_rate, i_scale, i_samplesize;
00692 
00693                 i_samplesize = p_stream->p_wf->nBlockAlign;
00694                 if( i_samplesize > 1 )
00695                 {
00696                     i_scale = i_samplesize;
00697                     i_rate = /*i_scale **/ p_stream->i_bitrate / 8;
00698                 }
00699                 else
00700                 {
00701                     i_samplesize = 1;
00702                     i_scale = 1000;
00703                     i_rate = 1000 * p_stream->i_bitrate / 8;
00704                 }
00705                 bo_AddFCC( p_bo, "auds" );
00706                 bo_AddDWordLE( p_bo, 0 );   /* tag */
00707                 bo_AddDWordLE( p_bo, 0 );   /* flags */
00708                 bo_AddWordLE(  p_bo, 0 );   /* priority */
00709                 bo_AddWordLE(  p_bo, 0 );   /* langage */
00710                 bo_AddDWordLE( p_bo, 0 );   /* initial frame */
00711                 bo_AddDWordLE( p_bo, i_scale );/* scale */
00712                 bo_AddDWordLE( p_bo, i_rate );
00713                 bo_AddDWordLE( p_bo, 0 );   /* start */
00714                 bo_AddDWordLE( p_bo, p_stream->i_frames );
00715                 bo_AddDWordLE( p_bo, 10 * 1024 );
00716                 bo_AddDWordLE( p_bo, -1 );  /* quality */
00717                 bo_AddDWordLE( p_bo, i_samplesize );
00718                 bo_AddWordLE(  p_bo, 0 );   /* ??? */
00719                 bo_AddWordLE(  p_bo, 0 );   /* ??? */
00720                 bo_AddWordLE(  p_bo, 0 );
00721                 bo_AddWordLE(  p_bo, 0 );
00722             }
00723             break;
00724     }
00725 
00726     AVI_BOX_EXIT( 0 );
00727 }
00728 
00729 static int avi_HeaderAdd_strf( sout_mux_t *p_mux,
00730                                buffer_out_t *p_bo,
00731                                avi_stream_t *p_stream )
00732 {
00733     AVI_BOX_ENTER( "strf" );
00734 
00735     switch( p_stream->i_cat )
00736     {
00737         case AUDIO_ES:
00738             bo_AddWordLE( p_bo, p_stream->p_wf->wFormatTag );
00739             bo_AddWordLE( p_bo, p_stream->p_wf->nChannels );
00740             bo_AddDWordLE( p_bo, p_stream->p_wf->nSamplesPerSec );
00741             bo_AddDWordLE( p_bo, p_stream->p_wf->nAvgBytesPerSec );
00742             bo_AddWordLE( p_bo, p_stream->p_wf->nBlockAlign );
00743             bo_AddWordLE( p_bo, p_stream->p_wf->wBitsPerSample );
00744             bo_AddWordLE( p_bo, p_stream->p_wf->cbSize );
00745             bo_AddMem( p_bo, p_stream->p_wf->cbSize, (uint8_t*)&p_stream->p_wf[1] );
00746             break;
00747         case VIDEO_ES:
00748             bo_AddDWordLE( p_bo, p_stream->p_bih->biSize );
00749             bo_AddDWordLE( p_bo, p_stream->p_bih->biWidth );
00750             bo_AddDWordLE( p_bo, p_stream->p_bih->biHeight );
00751             bo_AddWordLE( p_bo, p_stream->p_bih->biPlanes );
00752             bo_AddWordLE( p_bo, p_stream->p_bih->biBitCount );
00753             if( VLC_FOURCC( 0, 0, 0, 1 ) == 0x00000001 )
00754             {
00755                 bo_AddDWordBE( p_bo, p_stream->p_bih->biCompression );
00756             }
00757             else
00758             {
00759                 bo_AddDWordLE( p_bo, p_stream->p_bih->biCompression );
00760             }
00761             bo_AddDWordLE( p_bo, p_stream->p_bih->biSizeImage );
00762             bo_AddDWordLE( p_bo, p_stream->p_bih->biXPelsPerMeter );
00763             bo_AddDWordLE( p_bo, p_stream->p_bih->biYPelsPerMeter );
00764             bo_AddDWordLE( p_bo, p_stream->p_bih->biClrUsed );
00765             bo_AddDWordLE( p_bo, p_stream->p_bih->biClrImportant );
00766             bo_AddMem( p_bo,
00767                        p_stream->p_bih->biSize - sizeof( BITMAPINFOHEADER ),
00768                        (uint8_t*)&p_stream->p_bih[1] );
00769             break;
00770     }
00771 
00772     AVI_BOX_EXIT( 0 );
00773 }
00774 
00775 static int avi_HeaderAdd_strl( sout_mux_t *p_mux,
00776                                buffer_out_t *p_bo,
00777                                avi_stream_t *p_stream )
00778 {
00779     AVI_BOX_ENTER_LIST( "strl" );
00780 
00781     avi_HeaderAdd_strh( p_mux, p_bo, p_stream );
00782     avi_HeaderAdd_strf( p_mux, p_bo, p_stream );
00783 
00784     AVI_BOX_EXIT( 0 );
00785 }
00786 
00787 static block_t *avi_HeaderCreateRIFF( sout_mux_t *p_mux )
00788 {
00789     sout_mux_sys_t      *p_sys = p_mux->p_sys;
00790     block_t       *p_hdr;
00791     int                 i_stream;
00792     int                 i_maxbytespersec;
00793     int                 i_junk;
00794     buffer_out_t        bo;
00795 
00796     p_hdr = block_New( p_mux, HDR_SIZE );
00797     memset( p_hdr->p_buffer, 0, HDR_SIZE );
00798 
00799     bo_Init( &bo, HDR_SIZE, p_hdr->p_buffer );
00800 
00801     bo_AddFCC( &bo, "RIFF" );
00802     bo_AddDWordLE( &bo, p_sys->i_movi_size + HDR_SIZE - 8 + p_sys->i_idx1_size );
00803     bo_AddFCC( &bo, "AVI " );
00804 
00805     bo_AddFCC( &bo, "LIST" );
00806     bo_AddDWordLE( &bo, HDR_SIZE - 8);
00807     bo_AddFCC( &bo, "hdrl" );
00808 
00809     avi_HeaderAdd_avih( p_mux, &bo );
00810     for( i_stream = 0,i_maxbytespersec = 0; i_stream < p_sys->i_streams; i_stream++ )
00811     {
00812         avi_HeaderAdd_strl( p_mux, &bo, &p_sys->stream[i_stream] );
00813     }
00814 
00815     i_junk = HDR_SIZE - bo.i_buffer - 8 - 12;
00816     bo_AddFCC( &bo, "JUNK" );
00817     bo_AddDWordLE( &bo, i_junk );
00818 
00819     bo.i_buffer += i_junk;
00820     bo_AddFCC( &bo, "LIST" );
00821     bo_AddDWordLE( &bo, p_sys->i_movi_size + 4 );
00822     bo_AddFCC( &bo, "movi" );
00823 
00824     return( p_hdr );
00825 }
00826 
00827 static block_t * avi_HeaderCreateidx1( sout_mux_t *p_mux )
00828 {
00829     sout_mux_sys_t      *p_sys = p_mux->p_sys;
00830     block_t       *p_idx1;
00831     uint32_t            i_idx1_size;
00832     unsigned int        i;
00833     buffer_out_t        bo;
00834 
00835     i_idx1_size = 16 * p_sys->idx1.i_entry_count;
00836 
00837     p_idx1 = block_New( p_mux, i_idx1_size + 8 );
00838     memset( p_idx1->p_buffer, 0, i_idx1_size );
00839 
00840     bo_Init( &bo, i_idx1_size, p_idx1->p_buffer );
00841     bo_AddFCC( &bo, "idx1" );
00842     bo_AddDWordLE( &bo, i_idx1_size );
00843 
00844     for( i = 0; i < p_sys->idx1.i_entry_count; i++ )
00845     {
00846         bo_AddFCC( &bo, p_sys->idx1.entry[i].fcc );
00847         bo_AddDWordLE( &bo, p_sys->idx1.entry[i].i_flags );
00848         bo_AddDWordLE( &bo, p_sys->idx1.entry[i].i_pos );
00849         bo_AddDWordLE( &bo, p_sys->idx1.entry[i].i_length );
00850     }
00851 
00852     return( p_idx1 );
00853 }
00854 

Generated on Tue Dec 20 10:14:31 2005 for vlc-0.8.4a by  doxygen 1.4.2