/*****************************************************************************
* libavi.c : LibAVI
*****************************************************************************
- * Copyright (C) 2001 the VideoLAN team
+ * Copyright (C) 2001 VLC authors and VideoLAN
* $Id$
* Authors: Laurent Aimar <fenrir@via.ecp.fr>
*
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
*
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#endif
#include <vlc_common.h>
-#include <vlc_demux.h>
+#include <vlc_demux.h> /* stream_*, *_ES */
#include <vlc_codecs.h> /* VLC_BITMAPINFOHEADER */
#include "libavi.h"
#ifndef NDEBUG
-#define AVI_DEBUG 1
+# define AVI_DEBUG 1
#endif
#define __EVEN( x ) (((x) + 1) & ~1)
#ifdef AVI_DEBUG
msg_Dbg( (vlc_object_t*)s,
- "found Chunk fourcc:%8.8x (%4.4s) size:%"PRId64" pos:%"PRId64,
- p_chk->common.i_chunk_fourcc,
+ "found chunk, fourcc: %4.4s size:%"PRId64" pos:%"PRId64,
(char*)&p_chk->common.i_chunk_fourcc,
p_chk->common.i_chunk_size,
p_chk->common.i_chunk_pos );
avi_chunk_t *p_chk;
const uint8_t *p_peek;
bool b_seekable;
+ int i_ret = VLC_SUCCESS;
- if( p_container->common.i_chunk_size > 0 && p_container->common.i_chunk_size < 8 )
+ if( p_container->common.i_chunk_size > 0 && p_container->common.i_chunk_size < 4 )
{
/* empty box */
msg_Warn( (vlc_object_t*)s, "empty list chunk" );
{
msg_Dbg( (vlc_object_t*)s, "skipping movi chunk" );
if( b_seekable )
- {
return AVI_NextChunk( s, p_container );
- }
- return VLC_SUCCESS; /* point at begining of LIST-movi */
+ else
+ return VLC_EGENERIC; /* point at begining of LIST-movi */
}
if( stream_Read( s, NULL, 12 ) != 12 )
}
p_container->common.p_last = p_chk;
- if( AVI_ChunkRead( s, p_chk, p_container ) )
+ i_ret = AVI_ChunkRead( s, p_chk, p_container );
+ if( i_ret )
{
break;
}
}
msg_Dbg( (vlc_object_t*)s, "</list \'%4.4s\'>", (char*)&p_container->list.i_type );
+ if ( i_ret == AVI_ZERO_FOURCC ) return i_ret;
return VLC_SUCCESS;
}
+/* Allow to append indexes after starting playback */
+int AVI_ChunkFetchIndexes( stream_t *s, avi_chunk_t *p_riff )
+{
+ avi_chunk_t *p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0 );
+ if ( !p_movi )
+ return VLC_EGENERIC;
+
+ avi_chunk_t *p_chk;
+ uint64_t i_indexpos = 8 + p_movi->common.i_chunk_pos + p_movi->common.i_chunk_size;
+ bool b_seekable = false;
+ int i_ret = VLC_SUCCESS;
+
+ stream_Control( s, STREAM_CAN_SEEK, &b_seekable );
+ if ( !b_seekable || stream_Seek( s, i_indexpos ) )
+ return VLC_EGENERIC;
+
+ for( ; ; )
+ {
+ p_chk = xmalloc( sizeof( avi_chunk_t ) );
+ memset( p_chk, 0, sizeof( avi_chunk_t ) );
+ if (unlikely( !p_riff->common.p_first ))
+ p_riff->common.p_first = p_chk;
+ else
+ p_riff->common.p_last->common.p_next = p_chk;
+ p_riff->common.p_last = p_chk;
+
+ i_ret = AVI_ChunkRead( s, p_chk, p_riff );
+ if( i_ret )
+ break;
+
+ if( p_chk->common.p_father->common.i_chunk_size > 0 &&
+ ( stream_Tell( s ) >
+ (off_t)p_chk->common.p_father->common.i_chunk_pos +
+ (off_t)__EVEN( p_chk->common.p_father->common.i_chunk_size ) ) )
+ {
+ break;
+ }
+
+ /* If we can't seek then stop when we 've found any index */
+ if( p_chk->common.i_chunk_fourcc == AVIFOURCC_indx ||
+ p_chk->common.i_chunk_fourcc == AVIFOURCC_idx1 )
+ {
+ break;
+ }
+
+ }
+
+ return i_ret;
+}
+
#define AVI_READCHUNK_ENTER \
int64_t i_read = __EVEN(p_chk->common.i_chunk_size ) + 8; \
if( i_read > 100000000 ) \
case( AVIFOURCC_auds ):
p_chk->strf.auds.i_cat = AUDIO_ES;
p_chk->strf.auds.p_wf = xmalloc( __MAX( p_chk->common.i_chunk_size, sizeof( WAVEFORMATEX ) ) );
+ if ( !p_chk->strf.auds.p_wf )
+ {
+ AVI_READCHUNK_EXIT( VLC_ENOMEM );
+ }
AVI_READ2BYTES( p_chk->strf.auds.p_wf->wFormatTag );
AVI_READ2BYTES( p_chk->strf.auds.p_wf->nChannels );
AVI_READ4BYTES( p_chk->strf.auds.p_wf->nSamplesPerSec );
p_chk->strf.vids.i_cat = VIDEO_ES;
p_chk->strf.vids.p_bih = xmalloc( __MAX( p_chk->common.i_chunk_size,
sizeof( *p_chk->strf.vids.p_bih ) ) );
+ if ( !p_chk->strf.vids.p_bih )
+ {
+ AVI_READCHUNK_EXIT( VLC_ENOMEM );
+ }
AVI_READ4BYTES( p_chk->strf.vids.p_bih->biSize );
AVI_READ4BYTES( p_chk->strf.vids.p_bih->biWidth );
AVI_READ4BYTES( p_chk->strf.vids.p_bih->biHeight );
{
p_chk->strf.vids.p_bih->biSize = p_chk->common.i_chunk_size;
}
- if( p_chk->common.i_chunk_size > sizeof(VLC_BITMAPINFOHEADER) )
+ if ( p_chk->common.i_chunk_size > sizeof(VLC_BITMAPINFOHEADER) )
{
+ uint64_t i_extrasize = p_chk->common.i_chunk_size - sizeof(VLC_BITMAPINFOHEADER);
+
+ /* There's a color palette appended, set up VLC_BITMAPINFO */
memcpy( &p_chk->strf.vids.p_bih[1],
p_buff + 8 + sizeof(VLC_BITMAPINFOHEADER), /* 8=fourrc+size */
- p_chk->common.i_chunk_size -sizeof(VLC_BITMAPINFOHEADER) );
+ i_extrasize );
+
+ if ( !p_chk->strf.vids.p_bih->biClrUsed )
+ p_chk->strf.vids.p_bih->biClrUsed = (1 << p_chk->strf.vids.p_bih->biBitCount);
+
+ if( i_extrasize / sizeof(uint32_t) > UINT32_MAX )
+ p_chk->strf.vids.p_bih->biClrUsed = UINT32_MAX;
+ else
+ {
+ p_chk->strf.vids.p_bih->biClrUsed =
+ __MIN( i_extrasize / sizeof(uint32_t),
+ p_chk->strf.vids.p_bih->biClrUsed );
+ }
+
+ /* stay within VLC's limits */
+ p_chk->strf.vids.p_bih->biClrUsed =
+ __MIN( VIDEO_PALETTE_COLORS_MAX, p_chk->strf.vids.p_bih->biClrUsed );
}
+ else p_chk->strf.vids.p_bih->biClrUsed = 0;
#ifdef AVI_DEBUG
msg_Dbg( (vlc_object_t*)s,
"strf: video:%4.4s %"PRIu32"x%"PRIu32" planes:%d %dbpp",
p_chk->strf.vids.p_bih->biBitCount );
#endif
break;
+ case AVIFOURCC_iavs:
+ case AVIFOURCC_ivas:
+ p_chk->strf.common.i_cat = UNKNOWN_ES;
+ break;
+ case( AVIFOURCC_txts ):
+ p_chk->strf.common.i_cat = SPU_ES;
+ break;
default:
- msg_Warn( (vlc_object_t*)s, "unknown stream type" );
+ msg_Warn( (vlc_object_t*)s, "unknown stream type: %4.4s",
+ (char*)&p_strh->strh.i_type );
p_chk->strf.common.i_cat = UNKNOWN_ES;
break;
}
static int AVI_ChunkRead_strd( stream_t *s, avi_chunk_t *p_chk )
{
+ if ( p_chk->common.i_chunk_size == 0 )
+ {
+ msg_Dbg( (vlc_object_t*)s, "Zero sized pre-JUNK section met" );
+ return AVI_STRD_ZERO_CHUNK;
+ }
+
AVI_READCHUNK_ENTER;
p_chk->strd.p_data = xmalloc( p_chk->common.i_chunk_size );
memcpy( p_chk->strd.p_data, p_buff + 8, p_chk->common.i_chunk_size );
AVI_READCHUNK_EXIT( VLC_SUCCESS );
}
+static int AVI_ChunkRead_dmlh( stream_t *s, avi_chunk_t *p_chk )
+{
+ avi_chunk_dmlh_t *p_dmlh = (avi_chunk_dmlh_t*)p_chk;
+
+ AVI_READCHUNK_ENTER;
+
+ AVI_READ4BYTES( p_dmlh->dwTotalFrames );
+
+#ifdef AVI_DEBUG
+ msg_Dbg( (vlc_object_t*)s, "dmlh: dwTotalFrames %d",
+ p_dmlh->dwTotalFrames );
+#endif
+ AVI_READCHUNK_EXIT( VLC_SUCCESS );
+}
+
static const struct
{
vlc_fourcc_t i_fourcc;
const char *psz_type;
} AVI_strz_type[] =
{
- { AVIFOURCC_IARL, "archive location" },
- { AVIFOURCC_IART, "artist" },
- { AVIFOURCC_ICMS, "commisioned" },
- { AVIFOURCC_ICMT, "comments" },
- { AVIFOURCC_ICOP, "copyright" },
- { AVIFOURCC_ICRD, "creation date" },
- { AVIFOURCC_ICRP, "cropped" },
- { AVIFOURCC_IDIM, "dimensions" },
- { AVIFOURCC_IDPI, "dots per inch" },
- { AVIFOURCC_IENG, "engineer" },
- { AVIFOURCC_IGNR, "genre" },
- { AVIFOURCC_IKEY, "keywords" },
- { AVIFOURCC_ILGT, "lightness" },
- { AVIFOURCC_IMED, "medium" },
- { AVIFOURCC_INAM, "name" },
- { AVIFOURCC_IPLT, "palette setting" },
- { AVIFOURCC_IPRD, "product" },
- { AVIFOURCC_ISBJ, "subject" },
- { AVIFOURCC_ISFT, "software" },
- { AVIFOURCC_ISHP, "sharpness" },
- { AVIFOURCC_ISRC, "source" },
- { AVIFOURCC_ISRF, "source form" },
- { AVIFOURCC_ITCH, "technician" },
- { AVIFOURCC_ISMP, "time code" },
- { AVIFOURCC_IDIT, "digitalization time" },
- { AVIFOURCC_strn, "stream name" },
+ { AVIFOURCC_IARL, "Archive location" },
+ { AVIFOURCC_IART, "Artist" },
+ { AVIFOURCC_ICMS, "Commisioned" },
+ { AVIFOURCC_ICMT, "Comments" },
+ { AVIFOURCC_ICOP, "Copyright" },
+ { AVIFOURCC_ICRD, "Creation date" },
+ { AVIFOURCC_ICRP, "Cropped" },
+ { AVIFOURCC_IDIM, "Dimensions" },
+ { AVIFOURCC_IDPI, "Dots per inch" },
+ { AVIFOURCC_IENG, "Engineer" },
+ { AVIFOURCC_IGNR, "Genre" },
+ { AVIFOURCC_ISGN, "Secondary Genre" },
+ { AVIFOURCC_IKEY, "Keywords" },
+ { AVIFOURCC_ILGT, "Lightness" },
+ { AVIFOURCC_IMED, "Medium" },
+ { AVIFOURCC_INAM, "Name" },
+ { AVIFOURCC_IPLT, "Palette setting" },
+ { AVIFOURCC_IPRD, "Product" },
+ { AVIFOURCC_ISBJ, "Subject" },
+ { AVIFOURCC_ISFT, "Software" },
+ { AVIFOURCC_ISHP, "Sharpness" },
+ { AVIFOURCC_ISRC, "Source" },
+ { AVIFOURCC_ISRF, "Source form" },
+ { AVIFOURCC_ITCH, "Technician" },
+ { AVIFOURCC_ISMP, "Time code" },
+ { AVIFOURCC_IDIT, "Digitalization time" },
+ { AVIFOURCC_IWRI, "Writer" },
+ { AVIFOURCC_IPRO, "Producer" },
+ { AVIFOURCC_ICNM, "Cinematographer" },
+ { AVIFOURCC_IPDS, "Production designer" },
+ { AVIFOURCC_IEDT, "Editor" },
+ { AVIFOURCC_ICDS, "Costume designer" },
+ { AVIFOURCC_IMUS, "Music" },
+ { AVIFOURCC_ISTD, "Production studio" },
+ { AVIFOURCC_IDST, "Distributor" },
+ { AVIFOURCC_ICNT, "Country" },
+ { AVIFOURCC_ISTR, "Starring" },
+ { AVIFOURCC_IFRM, "Total number of parts" },
+ { AVIFOURCC_strn, "Stream name" },
+ { AVIFOURCC_IAS1, "First Language" },
+ { AVIFOURCC_IAS2, "Second Language" },
+ { AVIFOURCC_IAS3, "Third Language" },
+ { AVIFOURCC_IAS4, "Fourth Language" },
+ { AVIFOURCC_IAS5, "Fifth Language" },
+ { AVIFOURCC_IAS6, "Sixth Language" },
+ { AVIFOURCC_IAS7, "Seventh Language" },
+ { AVIFOURCC_IAS8, "Eighth Language" },
+ { AVIFOURCC_IAS9, "Ninth Language" },
+
{ 0, "???" }
};
+
static int AVI_ChunkRead_strz( stream_t *s, avi_chunk_t *p_chk )
{
int i_index;
{ AVIFOURCC_indx, AVI_ChunkRead_indx, AVI_ChunkFree_indx },
{ AVIFOURCC_vprp, AVI_ChunkRead_vprp, AVI_ChunkFree_nothing },
{ AVIFOURCC_JUNK, AVI_ChunkRead_nothing, AVI_ChunkFree_nothing },
+ { AVIFOURCC_dmlh, AVI_ChunkRead_dmlh, AVI_ChunkFree_nothing },
- { AVIFOURCC_IARL, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
{ AVIFOURCC_IARL, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
{ AVIFOURCC_IART, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
{ AVIFOURCC_ICMS, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
{ AVIFOURCC_IDPI, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
{ AVIFOURCC_IENG, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
{ AVIFOURCC_IGNR, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
+ { AVIFOURCC_ISGN, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
{ AVIFOURCC_IKEY, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
{ AVIFOURCC_ILGT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
{ AVIFOURCC_IMED, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
{ AVIFOURCC_ITCH, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
{ AVIFOURCC_ISMP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
{ AVIFOURCC_IDIT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
+ { AVIFOURCC_ILNG, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
+ { AVIFOURCC_IRTD, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
+ { AVIFOURCC_IWEB, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
+ { AVIFOURCC_IPRT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
+ { AVIFOURCC_IWRI, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
+ { AVIFOURCC_IPRO, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
+ { AVIFOURCC_ICNM, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
+ { AVIFOURCC_IPDS, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
+ { AVIFOURCC_IEDT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
+ { AVIFOURCC_ICDS, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
+ { AVIFOURCC_IMUS, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
+ { AVIFOURCC_ISTD, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
+ { AVIFOURCC_IDST, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
+ { AVIFOURCC_ICNT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
+ { AVIFOURCC_ISTR, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
+ { AVIFOURCC_IFRM, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
+ { AVIFOURCC_IAS1, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
+ { AVIFOURCC_IAS2, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
+ { AVIFOURCC_IAS3, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
+ { AVIFOURCC_IAS4, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
+ { AVIFOURCC_IAS5, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
+ { AVIFOURCC_IAS6, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
+ { AVIFOURCC_IAS7, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
+ { AVIFOURCC_IAS8, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
+ { AVIFOURCC_IAS9, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
+
+
{ AVIFOURCC_strn, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
{ 0, NULL, NULL }
};
}
}
-int _AVI_ChunkRead( stream_t *s, avi_chunk_t *p_chk, avi_chunk_t *p_father )
+int AVI_ChunkRead( stream_t *s, avi_chunk_t *p_chk, avi_chunk_t *p_father )
{
int i_index;
if( !p_chk )
{
+ msg_Warn( (vlc_object_t*)s, "cannot read null chunk" );
return VLC_EGENERIC;
}
msg_Warn( (vlc_object_t*)s, "cannot read one chunk" );
return VLC_EGENERIC;
}
+
if( p_chk->common.i_chunk_fourcc == VLC_FOURCC( 0, 0, 0, 0 ) )
{
msg_Warn( (vlc_object_t*)s, "found null fourcc chunk (corrupted file?)" );
- return VLC_EGENERIC;
+ return AVI_ZERO_FOURCC;
}
p_chk->common.p_father = p_father;
i_index = AVI_ChunkFunctionFind( p_chk->common.i_chunk_fourcc );
if( AVI_Chunk_Function[i_index].AVI_ChunkRead_function )
{
- return AVI_Chunk_Function[i_index].AVI_ChunkRead_function( s, p_chk );
+ int i_return = AVI_Chunk_Function[i_index].AVI_ChunkRead_function( s, p_chk );
+ if ( i_return == AVI_STRD_ZERO_CHUNK || i_return == AVI_ZERO_FOURCC )
+ {
+ if ( !p_father ) return VLC_EGENERIC;
+ return AVI_NextChunk( s, p_father );
+ }
+ return i_return;
}
else if( ( ((char*)&p_chk->common.i_chunk_fourcc)[0] == 'i' &&
((char*)&p_chk->common.i_chunk_fourcc)[1] == 'x' ) ||
p_chk->common.i_chunk_fourcc = AVIFOURCC_indx;
return AVI_ChunkRead_indx( s, p_chk );
}
- msg_Warn( (vlc_object_t*)s, "unknown chunk (not loaded)" );
+
+ msg_Warn( (vlc_object_t*)s, "unknown chunk: %4.4s (not loaded)",
+ (char*)&p_chk->common.i_chunk_fourcc );
return AVI_NextChunk( s, p_chk );
}
-void _AVI_ChunkFree( stream_t *s,
+void AVI_ChunkFree( stream_t *s,
avi_chunk_t *p_chk )
{
int i_index;
}
else
{
- msg_Warn( (vlc_object_t*)s, "unknown chunk (not unloaded)" );
+ msg_Warn( (vlc_object_t*)s, "unknown chunk: %4.4s (not unloaded)",
+ (char*)&p_chk->common.i_chunk_fourcc );
}
p_chk->common.p_first = NULL;
p_chk->common.p_last = NULL;
static void AVI_ChunkDumpDebug_level( vlc_object_t *p_obj,
avi_chunk_t *p_chk, unsigned i_level )
{
- unsigned i;
avi_chunk_t *p_child;
char str[512];
- if( i_level >= (sizeof(str) - 1)/5 )
+ if( i_level >= (sizeof(str) - 1)/4 )
return;
memset( str, ' ', sizeof( str ) );
- for( i = 1; i < i_level; i++ )
+ for( unsigned i = 1; i < i_level; i++ )
{
- str[i * 5] = '|';
+ str[i * 4] = '|';
}
if( p_chk->common.i_chunk_fourcc == AVIFOURCC_RIFF ||
p_chk->common.i_chunk_fourcc == AVIFOURCC_ON2 ||
p_chk->common.i_chunk_fourcc == AVIFOURCC_LIST )
{
- snprintf( &str[i_level * 5], sizeof(str) - 5*i_level,
+ snprintf( &str[i_level * 4], sizeof(str) - 4*i_level,
"%c %4.4s-%4.4s size:%"PRIu64" pos:%"PRIu64,
i_level ? '+' : '*',
(char*)&p_chk->common.i_chunk_fourcc,
}
else
{
- snprintf( &str[i_level * 5], sizeof(str) - 5*i_level,
+ snprintf( &str[i_level * 4], sizeof(str) - 4*i_level,
"+ %4.4s size:%"PRIu64" pos:%"PRIu64,
(char*)&p_chk->common.i_chunk_fourcc,
p_chk->common.i_chunk_size,
return NULL;
}
-