fftools/ffmpeg: use a mutex for enc_stats_write()

It may be called concurrently from different threads to write into the
same file.
This commit is contained in:
Anton Khirnov 2023-12-14 20:15:02 +01:00
parent 244d2fcc49
commit 2ad0b8e0ea
4 changed files with 16 additions and 0 deletions

View File

@ -480,6 +480,9 @@ typedef struct EncStats {
int nb_components; int nb_components;
AVIOContext *io; AVIOContext *io;
pthread_mutex_t lock;
int lock_initialized;
} EncStats; } EncStats;
extern const char *const forced_keyframes_const_names[]; extern const char *const forced_keyframes_const_names[];

View File

@ -491,6 +491,8 @@ void enc_stats_write(OutputStream *ost, EncStats *es,
ptsi = fd->dec.pts; ptsi = fd->dec.pts;
} }
pthread_mutex_lock(&es->lock);
for (size_t i = 0; i < es->nb_components; i++) { for (size_t i = 0; i < es->nb_components; i++) {
const EncStatsComponent *c = &es->components[i]; const EncStatsComponent *c = &es->components[i];
@ -538,6 +540,8 @@ void enc_stats_write(OutputStream *ost, EncStats *es,
} }
avio_w8(io, '\n'); avio_w8(io, '\n');
avio_flush(io); avio_flush(io);
pthread_mutex_unlock(&es->lock);
} }
static inline double psnr(double d) static inline double psnr(double d)

View File

@ -778,6 +778,10 @@ static void enc_stats_uninit(EncStats *es)
for (int i = 0; i < es->nb_components; i++) for (int i = 0; i < es->nb_components; i++)
av_freep(&es->components[i].str); av_freep(&es->components[i].str);
av_freep(&es->components); av_freep(&es->components);
if (es->lock_initialized)
pthread_mutex_destroy(&es->lock);
es->lock_initialized = 0;
} }
static void ost_free(OutputStream **post) static void ost_free(OutputStream **post)

View File

@ -402,6 +402,11 @@ fail:
return ret; return ret;
} }
ret = pthread_mutex_init(&es->lock, NULL);
if (ret)
return AVERROR(ret);
es->lock_initialized = 1;
ret = enc_stats_get_file(&es->io, path); ret = enc_stats_get_file(&es->io, path);
if (ret < 0) if (ret < 0)
return ret; return ret;