Skip to content

Commit

Permalink
common/ffmpeg.c, ffmpeg.h, lwindex.c: first batch of changes for FFmp…
Browse files Browse the repository at this point in the history
…eg AVStream API change

Signed-off-by: akarin <i@akarin.info>
  • Loading branch information
AkarinVS committed Jan 12, 2022
1 parent dac11cc commit e8f8a6c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 19 deletions.
24 changes: 24 additions & 0 deletions common/ffmpeg.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,28 @@

/* This file is available under an ISC license. */

#include "libavformat/avformat.h"

#include "ffmpeg.h"

// https://github.com/FFmpeg/FFmpeg/commit/557953a397dfdd9c7a3d8c2f60d1204599e3d3ac
#if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(58, 78, 100)
#define FFMPEG_45
#endif

int avstream_get_index_entries_count(const AVStream *st) {
#ifdef FFMPEG_45
return avformat_index_get_entries_count(st);
#else
return st->nb_index_entries;
#endif
}

const AVIndexEntry *avstream_index_get_entry(const AVStream *st, int idx) {
#ifdef FFMPEG_45
return avformat_index_get_entry(st, idx);
#else
if (idx < 0 || idx >= avstream_get_index_entries_count(st)) return NULL;
return &st->index_entries[idx];
#endif
}
2 changes: 2 additions & 0 deletions common/ffmpeg.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@

/* This file is available under an ISC license. */

int avstream_get_index_entries_count(const AVStream *st);
const AVIndexEntry *avstream_index_get_entry(const AVStream *st, int idx);
41 changes: 22 additions & 19 deletions common/lwindex.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ extern "C"
#include "progress.h"
#include "lwindex.h"
#include "decode.h"
#include "ffmpeg.h"

#include <sys/stat.h>
#include "xxhash.h"
Expand Down Expand Up @@ -1924,8 +1925,8 @@ static inline void print_index

static inline void write_av_index_entry
(
FILE *index,
AVIndexEntry *ie
FILE *index,
const AVIndexEntry *ie
)
{
print_index( index, "POS=%" PRId64 ",TS=%" PRId64 ",Flags=%x,Size=%d,Distance=%d\n",
Expand Down Expand Up @@ -2612,7 +2613,7 @@ static int create_index
else
{
/* Check the active stream is DV in AVI Type-1 or not. */
if( adhp->dv_in_avi == 1 && format_ctx->streams[ adhp->stream_index ]->nb_index_entries == 0 )
if( adhp->dv_in_avi == 1 && avstream_get_index_entries_count( format_ctx->streams[ adhp->stream_index ] ) == 0 )
{
/* DV in AVI Type-1 */
audio_sample_count = video_info ? MIN( video_sample_count, audio_sample_count ) : 0;
Expand Down Expand Up @@ -2720,45 +2721,47 @@ static int create_index
AVStream *stream = format_ctx->streams[stream_index];
if( stream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO )
{
print_index( index, "<StreamIndexEntries=%d,%d,%d>\n", stream_index, AVMEDIA_TYPE_VIDEO, stream->nb_index_entries );
const int nr = avstream_get_index_entries_count( stream );
print_index( index, "<StreamIndexEntries=%d,%d,%d>\n", stream_index, AVMEDIA_TYPE_VIDEO, nr );
if( vdhp->stream_index != stream_index )
for( int i = 0; i < stream->nb_index_entries; i++ )
write_av_index_entry( index, &stream->index_entries[i] );
else if( stream->nb_index_entries > 0 )
for( int i = 0; i < nr; i++ )
write_av_index_entry( index, avstream_index_get_entry( stream, i ) );
else if( nr > 0 )
{
vdhp->index_entries = (AVIndexEntry *)av_malloc( stream->index_entries_allocated_size );
vdhp->index_entries = (AVIndexEntry *)av_malloc( nr * sizeof( AVIndexEntry ) );
if( !vdhp->index_entries )
goto fail_index;
for( int i = 0; i < stream->nb_index_entries; i++ )
for( int i = 0; i < nr; i++ )
{
AVIndexEntry *ie = &stream->index_entries[i];
const AVIndexEntry *ie = avstream_index_get_entry( stream, i );
vdhp->index_entries[i] = *ie;
write_av_index_entry( index, ie );
}
vdhp->index_entries_count = stream->nb_index_entries;
vdhp->index_entries_count = nr;
}
print_index( index, "</StreamIndexEntries>\n" );
}
else if( stream->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && adhp->stream_index != -2 )
{
print_index( index, "<StreamIndexEntries=%d,%d,%d>\n", stream_index, AVMEDIA_TYPE_AUDIO, stream->nb_index_entries );
const int nr = avstream_get_index_entries_count( stream );
print_index( index, "<StreamIndexEntries=%d,%d,%d>\n", stream_index, AVMEDIA_TYPE_AUDIO, nr );
if( adhp->stream_index != stream_index )
for( int i = 0; i < stream->nb_index_entries; i++ )
write_av_index_entry( index, &stream->index_entries[i] );
else if( stream->nb_index_entries > 0 )
for( int i = 0; i < nr; i++ )
write_av_index_entry( index, avstream_index_get_entry( stream, i ) );
else if( nr > 0 )
{
/* Audio stream in matroska container requires index_entries for seeking.
* This avoids for re-reading the file to create index_entries since the file will be closed once. */
adhp->index_entries = (AVIndexEntry *)av_malloc( stream->index_entries_allocated_size );
adhp->index_entries = (AVIndexEntry *)av_malloc( nr * sizeof( AVIndexEntry ) );
if( !adhp->index_entries )
goto fail_index;
for( int i = 0; i < stream->nb_index_entries; i++ )
for( int i = 0; i < nr; i++ )
{
AVIndexEntry *ie = &stream->index_entries[i];
const AVIndexEntry *ie = avstream_index_get_entry( stream, i );
adhp->index_entries[i] = *ie;
write_av_index_entry( index, ie );
}
adhp->index_entries_count = stream->nb_index_entries;
adhp->index_entries_count = nr;
}
print_index( index, "</StreamIndexEntries>\n" );
}
Expand Down

0 comments on commit e8f8a6c

Please sign in to comment.