Skip to content

Instantly share code, notes, and snippets.

@msg7086
Created August 19, 2019 01:17
Show Gist options
  • Save msg7086/3f8866b0a8d1e7cd623b27d8b1fd7eb2 to your computer and use it in GitHub Desktop.
Save msg7086/3f8866b0a8d1e7cd623b27d8b1fd7eb2 to your computer and use it in GitHub Desktop.
Patches for L-SMASH-Works
From 816c62dbcecd99a832090288c25190e5d398feb4 Mon Sep 17 00:00:00 2001
From: Holy Wu <HolyWu@users.noreply.github.com>
Date: Sun, 11 Aug 2019 23:13:30 +0800
Subject: [PATCH] Add parameter cachefile
---
AviSynth/lsmashsource.cpp | 4 ++--
AviSynth/lwlibav_source.cpp | 30 +++++++++++++++++-------------
VapourSynth/lsmashsource.c | 2 +-
VapourSynth/lwlibav_source.c | 3 +++
common/lwindex.c | 33 ++++++++++++++++++---------------
common/lwindex.h | 1 +
6 files changed, 42 insertions(+), 31 deletions(-)
diff --git a/AviSynth/lsmashsource.cpp b/AviSynth/lsmashsource.cpp
index f23e0b1..a8ec65d 100644
--- a/AviSynth/lsmashsource.cpp
+++ b/AviSynth/lsmashsource.cpp
@@ -67,7 +67,7 @@ extern "C" __declspec(dllexport) const char* __stdcall AvisynthPluginInit3( IScr
env->AddFunction
(
"LWLibavVideoSource",
- "[source]s[stream_index]i[threads]i[cache]b[seek_mode]i[seek_threshold]i[dr]b[fpsnum]i[fpsden]i[repeat]b[dominance]i[format]s[decoder]s",
+ "[source]s[stream_index]i[threads]i[cache]b[cachefile]s[seek_mode]i[seek_threshold]i[dr]b[fpsnum]i[fpsden]i[repeat]b[dominance]i[format]s[decoder]s",
CreateLWLibavVideoSource,
0
);
@@ -75,7 +75,7 @@ extern "C" __declspec(dllexport) const char* __stdcall AvisynthPluginInit3( IScr
env->AddFunction
(
"LWLibavAudioSource",
- "[source]s[stream_index]i[cache]b[av_sync]b[layout]s[rate]i[decoder]s",
+ "[source]s[stream_index]i[cache]b[cachefile]s[av_sync]b[layout]s[rate]i[decoder]s",
CreateLWLibavAudioSource,
0
);
diff --git a/AviSynth/lwlibav_source.cpp b/AviSynth/lwlibav_source.cpp
index cfec3e6..614d621 100644
--- a/AviSynth/lwlibav_source.cpp
+++ b/AviSynth/lwlibav_source.cpp
@@ -269,21 +269,23 @@ AVSValue __cdecl CreateLWLibavVideoSource( AVSValue args, void *user_data, IScri
int stream_index = args[1].AsInt( -1 );
int threads = args[2].AsInt( 0 );
int no_create_index = args[3].AsBool( true ) ? 0 : 1;
- int seek_mode = args[4].AsInt( 0 );
- uint32_t forward_seek_threshold = args[5].AsInt( 10 );
- int direct_rendering = args[6].AsBool( false ) ? 1 : 0;
- int fps_num = args[7].AsInt( 0 );
- int fps_den = args[8].AsInt( 1 );
- int apply_repeat_flag = args[9].AsBool( false ) ? 1 : 0;
- int field_dominance = args[10].AsInt( 0 );
- enum AVPixelFormat pixel_format = get_av_output_pixel_format( args[11].AsString( nullptr ) );
- const char *preferred_decoder_names = args[12].AsString( nullptr );
+ const char *index_file_path = args[4].AsString( nullptr );
+ int seek_mode = args[5].AsInt( 0 );
+ uint32_t forward_seek_threshold = args[6].AsInt( 10 );
+ int direct_rendering = args[7].AsBool( false ) ? 1 : 0;
+ int fps_num = args[8].AsInt( 0 );
+ int fps_den = args[9].AsInt( 1 );
+ int apply_repeat_flag = args[10].AsBool( false ) ? 1 : 0;
+ int field_dominance = args[11].AsInt( 0 );
+ enum AVPixelFormat pixel_format = get_av_output_pixel_format( args[12].AsString( nullptr ) );
+ const char *preferred_decoder_names = args[13].AsString( nullptr );
/* Set LW-Libav options. */
lwlibav_option_t opt;
opt.file_path = source;
opt.threads = threads >= 0 ? threads : 0;
opt.av_sync = 0;
opt.no_create_index = no_create_index;
+ opt.index_file_path = index_file_path;
opt.force_video = (stream_index >= 0);
opt.force_video_index = stream_index >= 0 ? stream_index : -1;
opt.force_audio = 0;
@@ -308,16 +310,18 @@ AVSValue __cdecl CreateLWLibavAudioSource( AVSValue args, void *user_data, IScri
const char *source = args[0].AsString();
int stream_index = args[1].AsInt( -1 );
int no_create_index = args[2].AsBool( true ) ? 0 : 1;
- int av_sync = args[3].AsBool( false ) ? 1 : 0;
- const char *layout_string = args[4].AsString( nullptr );
- uint32_t sample_rate = args[5].AsInt( 0 );
- const char *preferred_decoder_names = args[6].AsString( nullptr );
+ const char *index_file_path = args[3].AsString( nullptr );
+ int av_sync = args[4].AsBool( false ) ? 1 : 0;
+ const char *layout_string = args[5].AsString( nullptr );
+ uint32_t sample_rate = args[6].AsInt( 0 );
+ const char *preferred_decoder_names = args[7].AsString( nullptr );
/* Set LW-Libav options. */
lwlibav_option_t opt;
opt.file_path = source;
opt.threads = 0;
opt.av_sync = av_sync;
opt.no_create_index = no_create_index;
+ opt.index_file_path = index_file_path;
opt.force_video = 0;
opt.force_video_index = -1;
opt.force_audio = (stream_index >= 0);
diff --git a/VapourSynth/lsmashsource.c b/VapourSynth/lsmashsource.c
index c9b833e..5ce547f 100644
--- a/VapourSynth/lsmashsource.c
+++ b/VapourSynth/lsmashsource.c
@@ -85,7 +85,7 @@ VS_EXTERNAL_API(void) VapourSynthPluginInit( VSConfigPlugin config_func, VSRegis
register_func
(
"LWLibavSource",
- "source:data;stream_index:int:opt;cache:int:opt;" COMMON_OPTS "repeat:int:opt;dominance:int:opt;",
+ "source:data;stream_index:int:opt;cache:int:opt;cachefile:data:opt;" COMMON_OPTS "repeat:int:opt;dominance:int:opt;",
vs_lwlibavsource_create,
NULL,
plugin
diff --git a/VapourSynth/lwlibav_source.c b/VapourSynth/lwlibav_source.c
index 74cad91..576c24c 100644
--- a/VapourSynth/lwlibav_source.c
+++ b/VapourSynth/lwlibav_source.c
@@ -283,6 +283,7 @@ void VS_CC vs_lwlibavsource_create( const VSMap *in, VSMap *out, void *user_data
int64_t fps_den;
int64_t apply_repeat_flag;
int64_t field_dominance;
+ const char *index_file_path;
const char *format;
const char *preferred_decoder_names;
set_option_int64 ( &stream_index, -1, "stream_index", in, vsapi );
@@ -296,6 +297,7 @@ void VS_CC vs_lwlibavsource_create( const VSMap *in, VSMap *out, void *user_data
set_option_int64 ( &fps_den, 1, "fpsden", in, vsapi );
set_option_int64 ( &apply_repeat_flag, 0, "repeat", in, vsapi );
set_option_int64 ( &field_dominance, 0, "dominance", in, vsapi );
+ set_option_string( &index_file_path, NULL, "cachefile", in, vsapi );
set_option_string( &format, NULL, "format", in, vsapi );
set_option_string( &preferred_decoder_names, NULL, "decoder", in, vsapi );
set_preferred_decoder_names_on_buf( hp->preferred_decoder_names_buf, preferred_decoder_names );
@@ -305,6 +307,7 @@ void VS_CC vs_lwlibavsource_create( const VSMap *in, VSMap *out, void *user_data
opt.threads = threads >= 0 ? threads : 0;
opt.av_sync = 0;
opt.no_create_index = !cache_index;
+ opt.index_file_path = index_file_path;
opt.force_video = (stream_index >= 0);
opt.force_video_index = stream_index >= 0 ? stream_index : -1;
opt.force_audio = 0;
diff --git a/common/lwindex.c b/common/lwindex.c
index 9491770..cc50c49 100644
--- a/common/lwindex.c
+++ b/common/lwindex.c
@@ -2013,9 +2013,15 @@ static void create_index
</ExtraDataList>
</LibavReaderIndexFile>
*/
- char index_path[512] = { 0 };
- sprintf( index_path, "%s.lwi", lwhp->file_path );
- FILE *index = !opt->no_create_index ? lw_fopen( index_path, "wb" ) : NULL;
+ FILE *index;
+ if( opt->index_file_path && *opt->index_file_path )
+ index = !opt->no_create_index ? lw_fopen( opt->index_file_path, "wb" ) : NULL;
+ else
+ {
+ char index_path[512] = { 0 };
+ sprintf( index_path, "%s.lwi", lwhp->file_path );
+ index = !opt->no_create_index ? lw_fopen( index_path, "wb" ) : NULL;
+ }
if( !index && !opt->no_create_index )
{
free( video_info );
@@ -3201,21 +3207,20 @@ int lwlibav_construct_index
{
/* Try to open the index file. */
size_t file_path_length = strlen( opt->file_path );
- char *index_file_path = (char *)lw_malloc_zero(file_path_length + 5);
- if( !index_file_path )
- return -1;
- memcpy( index_file_path, opt->file_path, file_path_length );
- const char *ext = file_path_length >= 5 ? &opt->file_path[file_path_length - 4] : NULL;
- int has_lwi_ext = ext && !strncmp( ext, ".lwi", strlen( ".lwi" ) );
- if( has_lwi_ext )
- index_file_path[file_path_length] = '\0';
+ FILE *index;
+ if( opt->index_file_path && *opt->index_file_path )
+ index = lw_fopen( opt->index_file_path, (opt->force_video || opt->force_audio) ? "r+b" : "rb" );
else
{
+ char *index_file_path = (char *)lw_malloc_zero(file_path_length + 5);
+ if( !index_file_path )
+ return -1;
+ memcpy( index_file_path, opt->file_path, file_path_length );
memcpy( index_file_path + file_path_length, ".lwi", strlen( ".lwi" ) );
index_file_path[file_path_length + 4] = '\0';
+ index = lw_fopen( index_file_path, (opt->force_video || opt->force_audio) ? "r+b" : "rb" );
+ free( index_file_path );
}
- FILE *index = lw_fopen( index_file_path, (opt->force_video || opt->force_audio) ? "r+b" : "rb" );
- free( index_file_path );
if( index )
{
uint8_t lwindex_version[4] = { 0 };
@@ -3243,8 +3248,6 @@ int lwlibav_construct_index
if( !lwhp->file_path )
goto fail;
memcpy( lwhp->file_path, opt->file_path, file_path_length );
- if( has_lwi_ext )
- lwhp->file_path[file_path_length - 4] = '\0';
}
av_register_all();
avcodec_register_all();
diff --git a/common/lwindex.h b/common/lwindex.h
index 2cfce3c..886daed 100644
--- a/common/lwindex.h
+++ b/common/lwindex.h
@@ -35,6 +35,7 @@ typedef struct
int threads;
int av_sync;
int no_create_index;
+ const char *index_file_path;
int force_video;
int force_video_index;
int force_audio;
--
2.23.0.windows.1
From 8e72cd804db978457a718175c9845633a37cfc18 Mon Sep 17 00:00:00 2001
From: Holy Wu <HolyWu@users.noreply.github.com>
Date: Fri, 16 Aug 2019 11:48:23 +0800
Subject: [PATCH] Don't index audio streams in VideoSource filter
---
AviSynth/lwlibav_source.cpp | 2 +-
VapourSynth/lwlibav_source.c | 2 +-
common/lwindex.c | 8 +++++---
3 files changed, 7 insertions(+), 5 deletions(-)
diff --git a/AviSynth/lwlibav_source.cpp b/AviSynth/lwlibav_source.cpp
index 614d621..96e5c4e 100644
--- a/AviSynth/lwlibav_source.cpp
+++ b/AviSynth/lwlibav_source.cpp
@@ -289,7 +289,7 @@ AVSValue __cdecl CreateLWLibavVideoSource( AVSValue args, void *user_data, IScri
opt.force_video = (stream_index >= 0);
opt.force_video_index = stream_index >= 0 ? stream_index : -1;
opt.force_audio = 0;
- opt.force_audio_index = -1;
+ opt.force_audio_index = -2;
opt.apply_repeat_flag = apply_repeat_flag;
opt.field_dominance = CLIP_VALUE( field_dominance, 0, 2 ); /* 0: Obey source flags, 1: TFF, 2: BFF */
opt.vfr2cfr.active = fps_num > 0 && fps_den > 0 ? 1 : 0;
diff --git a/VapourSynth/lwlibav_source.c b/VapourSynth/lwlibav_source.c
index 576c24c..80952a1 100644
--- a/VapourSynth/lwlibav_source.c
+++ b/VapourSynth/lwlibav_source.c
@@ -311,7 +311,7 @@ void VS_CC vs_lwlibavsource_create( const VSMap *in, VSMap *out, void *user_data
opt.force_video = (stream_index >= 0);
opt.force_video_index = stream_index >= 0 ? stream_index : -1;
opt.force_audio = 0;
- opt.force_audio_index = -1;
+ opt.force_audio_index = -2;
opt.apply_repeat_flag = apply_repeat_flag;
opt.field_dominance = CLIP_VALUE( field_dominance, 0, 2 ); /* 0: Obey source flags, 1: TFF, 2: BFF */
opt.vfr2cfr.active = fps_num > 0 && fps_den > 0 ? 1 : 0;
diff --git a/common/lwindex.c b/common/lwindex.c
index cc50c49..d0377b0 100644
--- a/common/lwindex.c
+++ b/common/lwindex.c
@@ -2054,7 +2054,7 @@ static void create_index
video_index_pos = ftell( index );
fprintf( index, "<ActiveVideoStreamIndex>%+011d</ActiveVideoStreamIndex>\n", -1 );
audio_index_pos = ftell( index );
- fprintf( index, "<ActiveAudioStreamIndex>%+011d</ActiveAudioStreamIndex>\n", -1 );
+ fprintf( index, "<ActiveAudioStreamIndex>%+011d</ActiveAudioStreamIndex>\n", adhp->stream_index == -2 ? -2 : -1 );
}
AVPacket pkt = { 0 };
av_init_packet( &pkt );
@@ -2285,7 +2285,7 @@ static void create_index
av_get_pix_fmt_name( pkt_ctx->pix_fmt ) ? av_get_pix_fmt_name( pkt_ctx->pix_fmt ) : "none",
pkt_ctx->colorspace );
}
- else
+ else if( adhp->stream_index != -2 )
{
if( adhp->stream_index == -1 && (!opt->force_audio || (opt->force_audio && pkt.stream_index == opt->force_audio_index)) )
{
@@ -2751,6 +2751,8 @@ static int parse_index
if( !audio_info )
goto fail_parsing;
}
+ if( active_audio_index == -2 && opt->force_audio_index >= -1 )
+ goto fail_parsing;
vdhp->codec_id = AV_CODEC_ID_NONE;
adhp->codec_id = AV_CODEC_ID_NONE;
vdhp->initial_pix_fmt = AV_PIX_FMT_NONE;
@@ -3260,7 +3262,7 @@ int lwlibav_construct_index
}
lwhp->threads = opt->threads;
vdhp->stream_index = -1;
- adhp->stream_index = -1;
+ adhp->stream_index = ( opt->force_audio_index == -2 ) ? -2 : -1;
/* Create the index file. */
create_index( lwhp, vdhp, vohp, adhp, aohp, format_ctx, opt, indicator, php );
/* Close file.
--
2.23.0.windows.1
From 44792398fd2480fbbfaae66512765c9fa5e75f6b Mon Sep 17 00:00:00 2001
From: Holy Wu <HolyWu@users.noreply.github.com>
Date: Fri, 16 Aug 2019 19:33:08 +0800
Subject: [PATCH] Print indexing progress to stderr
---
AviSynth/lwlibav_source.cpp | 25 +++++++++++++++++++++----
VapourSynth/lwlibav_source.c | 21 +++++++++++++++++++--
2 files changed, 40 insertions(+), 6 deletions(-)
diff --git a/AviSynth/lwlibav_source.cpp b/AviSynth/lwlibav_source.cpp
index 96e5c4e..d284b69 100644
--- a/AviSynth/lwlibav_source.cpp
+++ b/AviSynth/lwlibav_source.cpp
@@ -21,6 +21,8 @@
/* This file is available under an ISC license.
* However, when distributing its binary file, it will be under LGPL or GPL. */
+#include <stdio.h>
+#include <string.h>
#include "lsmashsource.h"
extern "C"
@@ -42,6 +44,21 @@ extern "C"
#pragma warning( disable:4996 )
+static int update_indicator( progress_handler_t *, const char *message, int percent )
+{
+ if ( !strcmp( message, "Creating Index file" ) )
+ {
+ fprintf( stderr, "Creating lwi index file %d%%\r", percent );
+ fflush( stderr );
+ }
+ return 0;
+}
+
+static void close_indicator( progress_handler_t * )
+{
+ fprintf( stderr, "\n" );
+}
+
static void prepare_video_decoding
(
lwlibav_video_decode_handler_t *vdhp,
@@ -104,8 +121,8 @@ LWLibavVideoSource::LWLibavVideoSource
/* Set up progress indicator. */
progress_indicator_t indicator;
indicator.open = NULL;
- indicator.update = NULL;
- indicator.close = NULL;
+ indicator.update = update_indicator;
+ indicator.close = close_indicator;
/* Construct index. */
int ret = lwlibav_construct_index( &lwh, vdhp, vohp, adhp.get(), aohp.get(), lhp, opt, &indicator, NULL );
free_audio_decode_handler();
@@ -214,8 +231,8 @@ LWLibavAudioSource::LWLibavAudioSource
/* Set up progress indicator. */
progress_indicator_t indicator;
indicator.open = NULL;
- indicator.update = NULL;
- indicator.close = NULL;
+ indicator.update = update_indicator;
+ indicator.close = close_indicator;
/* Construct index. */
if( lwlibav_construct_index( &lwh, vdhp.get(), vohp.get(), adhp, aohp, lhp, opt, &indicator, NULL ) < 0 )
env->ThrowError( "LWLibavAudioSource: failed to get construct index." );
diff --git a/VapourSynth/lwlibav_source.c b/VapourSynth/lwlibav_source.c
index 80952a1..cbdbf69 100644
--- a/VapourSynth/lwlibav_source.c
+++ b/VapourSynth/lwlibav_source.c
@@ -74,6 +74,8 @@ uint64_t output_pcm_samples_from_packet
void lw_cleanup_audio_output_handler( lw_audio_output_handler_t *aohp ){ } */
+#include <stdio.h>
+#include <string.h>
#include "lsmashsource.h"
#include "video_output.h"
@@ -132,6 +134,21 @@ static lwlibav_handler_t *alloc_handler
return hp;
}
+static int update_indicator( progress_handler_t *, const char *message, int percent )
+{
+ if ( !strcmp( message, "Creating Index file" ) )
+ {
+ fprintf( stderr, "Creating lwi index file %d%%\r", percent );
+ fflush( stderr );
+ }
+ return 0;
+}
+
+static void close_indicator( progress_handler_t * )
+{
+ fprintf( stderr, "\n" );
+}
+
static void VS_CC vs_filter_init( VSMap *in, VSMap *out, void **instance_data, VSNode *node, VSCore *core, const VSAPI *vsapi )
{
lwlibav_handler_t *hp = (lwlibav_handler_t *)*instance_data;
@@ -326,8 +343,8 @@ void VS_CC vs_lwlibavsource_create( const VSMap *in, VSMap *out, void *user_data
/* Set up progress indicator. */
progress_indicator_t indicator;
indicator.open = NULL;
- indicator.update = NULL;
- indicator.close = NULL;
+ indicator.update = update_indicator;
+ indicator.close = close_indicator;
/* Construct index. */
int ret = lwlibav_construct_index( lwhp, vdhp, vohp, hp->adhp, hp->aohp, &lh, &opt, &indicator, NULL );
lwlibav_audio_free_decode_handler_ptr( &hp->adhp );
--
2.23.0.windows.1
From 6bc0a585f1dd4404cdce35f8d29d76e87df9ca5a Mon Sep 17 00:00:00 2001
From: Holy Wu <HolyWu@users.noreply.github.com>
Date: Fri, 16 Aug 2019 19:55:10 +0800
Subject: [PATCH] Tell lavf to discard unwanted packets while indexing
---
common/lwindex.c | 11 +++++++++++
1 file changed, 11 insertions(+)
diff --git a/common/lwindex.c b/common/lwindex.c
index d0377b0..4fe21f9 100644
--- a/common/lwindex.c
+++ b/common/lwindex.c
@@ -2088,9 +2088,15 @@ static void create_index
AVCodecParameters *codecpar = stream->codecpar;
if( codecpar->codec_type != AVMEDIA_TYPE_VIDEO
&& codecpar->codec_type != AVMEDIA_TYPE_AUDIO )
+ {
+ stream->discard = AVDISCARD_ALL;
continue;
+ }
if( codecpar->codec_id == AV_CODEC_ID_NONE )
+ {
+ stream->discard = AVDISCARD_ALL;
continue;
+ }
lwindex_helper_t *helper = get_index_helper( &indexer, stream );
if( !helper )
{
@@ -2099,7 +2105,10 @@ static void create_index
}
AVCodecContext *pkt_ctx = helper->codec_ctx;
if( !pkt_ctx )
+ {
+ stream->discard = AVDISCARD_ALL;
continue;
+ }
int extradata_index = append_extradata_if_new( helper, pkt_ctx, &pkt );
if( extradata_index < 0 )
{
@@ -2387,6 +2396,8 @@ static void create_index
av_get_sample_fmt_name( pkt_ctx->sample_fmt ) ? av_get_sample_fmt_name( pkt_ctx->sample_fmt ) : "none",
bits_per_sample, frame_length );
}
+ else
+ stream->discard = AVDISCARD_ALL;
if( indicator->update )
{
/* Update progress dialog. */
--
2.23.0.windows.1
From 6a509317bac0317507a63792858a4f62bdca55b0 Mon Sep 17 00:00:00 2001
From: Holy Wu <HolyWu@users.noreply.github.com>
Date: Sat, 17 Aug 2019 08:48:04 +0800
Subject: [PATCH] Remove InputFilePath field from the index file
---
common/lwindex.c | 28 +++++-----------------------
common/lwindex.h | 2 +-
2 files changed, 6 insertions(+), 24 deletions(-)
diff --git a/common/lwindex.c b/common/lwindex.c
index 4fe21f9..cd1fb76 100644
--- a/common/lwindex.c
+++ b/common/lwindex.c
@@ -1995,8 +1995,7 @@ static void create_index
}
/*
# Structure of Libav reader index file
- <LibavReaderIndexFile=13>
- <InputFilePath>foobar.omo</InputFilePath>
+ <LibavReaderIndexFile=14>
<LibavReaderIndex=0x00000208,0,marumoska>
<ActiveVideoStreamIndex>+0000000000</ActiveVideoStreamIndex>
<ActiveAudioStreamIndex>-0000000001</ActiveAudioStreamIndex>
@@ -2049,7 +2048,6 @@ static void create_index
fprintf( index, "<LSMASHWorksIndexVersion=%" PRIu8 ".%" PRIu8 ".%" PRIu8 ".%" PRIu8 ">\n",
lwindex_version[0], lwindex_version[1], lwindex_version[2], lwindex_version[3] );
fprintf( index, "<LibavReaderIndexFile=%d>\n", LWINDEX_INDEX_FILE_VERSION );
- fprintf( index, "<InputFilePath>%s</InputFilePath>\n", lwhp->file_path );
fprintf( index, "<LibavReaderIndex=0x%08x,%d,%s>\n", lwhp->format_flags, lwhp->raw_demuxer, lwhp->format_name );
video_index_pos = ftell( index );
fprintf( index, "<ActiveVideoStreamIndex>%+011d</ActiveVideoStreamIndex>\n", -1 );
@@ -2716,19 +2714,6 @@ static int parse_index
FILE *index
)
{
- /* Test to open the target file. */
- char file_path[512] = { 0 };
- if( fscanf( index, "<InputFilePath>%[^\n<]</InputFilePath>\n", file_path ) != 1 )
- return -1;
- FILE *target = lw_fopen( file_path, "rb" );
- if( !target )
- return -1;
- fclose( target );
- size_t file_path_length = strlen( file_path );
- lwhp->file_path = (char *)lw_malloc_zero( file_path_length + 1 );
- if( !lwhp->file_path )
- return -1;
- memcpy( lwhp->file_path, file_path, file_path_length );
/* Parse the index file. */
char format_name[256];
int active_video_index;
@@ -3220,6 +3205,10 @@ int lwlibav_construct_index
{
/* Try to open the index file. */
size_t file_path_length = strlen( opt->file_path );
+ lwhp->file_path = (char *)lw_malloc_zero( file_path_length + 1 );
+ if( !lwhp->file_path )
+ goto fail;
+ memcpy( lwhp->file_path, opt->file_path, file_path_length );
FILE *index;
if( opt->index_file_path && *opt->index_file_path )
index = lw_fopen( opt->index_file_path, (opt->force_video || opt->force_audio) ? "r+b" : "rb" );
@@ -3255,13 +3244,6 @@ int lwlibav_construct_index
fclose( index );
}
/* Open file. */
- if( !lwhp->file_path )
- {
- lwhp->file_path = (char *)lw_malloc_zero( file_path_length + 1 );
- if( !lwhp->file_path )
- goto fail;
- memcpy( lwhp->file_path, opt->file_path, file_path_length );
- }
av_register_all();
avcodec_register_all();
AVFormatContext *format_ctx = NULL;
diff --git a/common/lwindex.h b/common/lwindex.h
index 886daed..da83d44 100644
--- a/common/lwindex.h
+++ b/common/lwindex.h
@@ -27,7 +27,7 @@
/* index file version
* This version is bumped when its structure changed so that the lwindex invokes
* reindexing opened file immediately. */
-#define LWINDEX_INDEX_FILE_VERSION 13
+#define LWINDEX_INDEX_FILE_VERSION 14
typedef struct
{
--
2.23.0.windows.1
From bf5e796126e039374c04e1d9b194492a65381f31 Mon Sep 17 00:00:00 2001
From: Holy Wu <HolyWu@users.noreply.github.com>
Date: Sat, 17 Aug 2019 15:18:23 +0800
Subject: [PATCH] Re-create index file when file size or timestamp doesn't
match
---
common/lwindex.c | 36 ++++++++++++++++++++++++++++++++++++
1 file changed, 36 insertions(+)
diff --git a/common/lwindex.c b/common/lwindex.c
index cd1fb76..165bd2b 100644
--- a/common/lwindex.c
+++ b/common/lwindex.c
@@ -48,6 +48,11 @@ extern "C"
#include "lwindex.h"
#include "decode.h"
+#include <sys/stat.h>
+#ifdef _WIN32
+#include <windows.h>
+#endif
+
typedef struct
{
lwlibav_extradata_handler_t exh;
@@ -1996,6 +2001,8 @@ static void create_index
/*
# Structure of Libav reader index file
<LibavReaderIndexFile=14>
+ <FileSize=000>
+ <FileLastModificationTime=000>
<LibavReaderIndex=0x00000208,0,marumoska>
<ActiveVideoStreamIndex>+0000000000</ActiveVideoStreamIndex>
<ActiveAudioStreamIndex>-0000000001</ActiveAudioStreamIndex>
@@ -2048,6 +2055,17 @@ static void create_index
fprintf( index, "<LSMASHWorksIndexVersion=%" PRIu8 ".%" PRIu8 ".%" PRIu8 ".%" PRIu8 ">\n",
lwindex_version[0], lwindex_version[1], lwindex_version[2], lwindex_version[3] );
fprintf( index, "<LibavReaderIndexFile=%d>\n", LWINDEX_INDEX_FILE_VERSION );
+#ifdef _WIN32
+ wchar_t *wname;
+ lw_string_to_wchar( CP_UTF8, lwhp->file_path, &wname );
+ struct _stat64 file_stat;
+ _wstat64( wname, &file_stat );
+#else
+ struct stat64 file_stat;
+ stat64( lwhp->file_path, &file_stat );
+#endif
+ fprintf( index, "<FileSize=%" PRId64 ">\n", file_stat.st_size );
+ fprintf( index, "<FileLastModificationTime=%" PRId64 ">\n", file_stat.st_mtime );
fprintf( index, "<LibavReaderIndex=0x%08x,%d,%s>\n", lwhp->format_flags, lwhp->raw_demuxer, lwhp->format_name );
video_index_pos = ftell( index );
fprintf( index, "<ActiveVideoStreamIndex>%+011d</ActiveVideoStreamIndex>\n", -1 );
@@ -2715,9 +2733,27 @@ static int parse_index
)
{
/* Parse the index file. */
+ int64_t file_size;
+ int64_t file_last_modification_time;
char format_name[256];
int active_video_index;
int active_audio_index;
+#ifdef _WIN32
+ wchar_t *wname;
+ lw_string_to_wchar( CP_UTF8, lwhp->file_path, &wname );
+ struct _stat64 file_stat;
+ if( _wstat64( wname, &file_stat ) )
+ return -1;
+#else
+ struct stat64 file_stat;
+ if( stat64( lwhp->file_path, &file_stat ) )
+ return -1;
+#endif
+ if( fscanf( index, "<FileSize=%" SCNd64 ">\n", &file_size ) != 1
+ || fscanf( index, "<FileLastModificationTime=%" SCNd64 ">\n", &file_last_modification_time ) != 1 )
+ return -1;
+ if( file_size != file_stat.st_size || file_last_modification_time != file_stat.st_mtime )
+ return -1;
if( fscanf( index, "<LibavReaderIndex=0x%x,%d,%[^>]>\n",
(unsigned int *)&lwhp->format_flags, &lwhp->raw_demuxer, format_name ) != 3 )
return -1;
--
2.23.0.windows.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment