From db4f3801c2185b8da1d9fb3f4b3da3d2ab92ea13 Mon Sep 17 00:00:00 2001 From: Sylvain BERTRAND Date: Sun, 24 May 2020 21:42:08 +0000 Subject: [PATCH] npv: fix obvious multithread bug --- npv/TODO | 1 + npv/video/local/code.frag.c | 28 ++++++++++++++++------------ npv/video/public/code.frag.c | 2 +- npv/video/public/state.frag.h | 2 +- 4 files changed, 19 insertions(+), 14 deletions(-) diff --git a/npv/TODO b/npv/TODO index d6dcb23..7fec801 100644 --- a/npv/TODO +++ b/npv/TODO @@ -1,4 +1,5 @@ not ordered: +- make seek/end/etc work again - better/smoother/less drop/(correct?) video frame display strategy - xcb(resize)/vulkan swapchain loss or "not optimal" ? (state re-factoring) - x11 map/unmap handling to: diff --git a/npv/video/local/code.frag.c b/npv/video/local/code.frag.c index a4b757b..f43e609 100644 --- a/npv/video/local/code.frag.c +++ b/npv/video/local/code.frag.c @@ -229,10 +229,15 @@ static void dec_a_grow(void) dec_frs_p.priv_a = realloc(dec_frs_p.priv_a, sizeof(*dec_frs_p.priv_a) * (dec_frs_p.n_max + 1)); if (dec_frs_p.priv_a == 0) - FATALV("unable to allocate memory for an additional private data for decoded frames\n"); + FATALV("unable to allocate memory for an additional pointer on private data for decoded frames\n"); + dec_frs_p.a[new_idx] = avutil_video_fr_ref_alloc(); if (dec_frs_p.a[new_idx] == 0) FATALV("ffmpeg:unable to allocate a decoded frame reference\n"); + dec_frs_p.priv_a[new_idx] = calloc(1, sizeof(**dec_frs_p.priv_a)); + if (dec_frs_p.priv_a[new_idx] == 0) + FATALV("unable to allocate decoded frame private data\n"); + ++dec_frs_p.n_max; } /* extract a fr ref, shift the a, push it back at the e, and unref its bufs */ @@ -241,7 +246,7 @@ static void fr_drop(u16 fr) struct dec_fr_priv_t *fr_priv; avutil_video_fr_ref_t *save; - fr_priv = dec_frs_p.priv_a + fr; + fr_priv = dec_frs_p.priv_a[fr]; if (!fr_priv->was_qed_to_pe) WARNINGV("dropping undisplayed frame\n"); save = dec_frs_p.a[fr]; @@ -255,11 +260,10 @@ static void fr_drop(u16 fr) dec_frs_p.a[e - 1] = save; memmove(&dec_frs_p.priv_a[fr], &dec_frs_p.priv_a[fr + 1], - sizeof(dec_frs_p.priv_a[fr]) * (e - (fr + 1))); - memset(&dec_frs_p.priv_a[e - 1], 0, - sizeof(dec_frs_p.priv_a[e - 1])); + sizeof(*dec_frs_p.priv_a) * (e - (fr + 1))); + memset(dec_frs_p.priv_a[e - 1], 0, sizeof(**dec_frs_p.priv_a)); } else - memset(&dec_frs_p.priv_a[0], 0, sizeof(dec_frs_p.priv_a[0])); + memset(dec_frs_p.priv_a[0], 0, sizeof(**dec_frs_p.priv_a)); dec_frs_p.n--; } static void frs_drop(s64 now) @@ -280,7 +284,7 @@ static void frs_drop(s64 now) break; pts = dec_frs_p.a[fr]->pts; - fr_priv = dec_frs_p.priv_a + fr; + fr_priv = dec_frs_p.priv_a[fr]; if ((dec_frs_p.a[fr] != dec_frs_p.being_scaled.fr) && (pts < low)) @@ -308,7 +312,7 @@ static void select_fr(s64 now, avutil_video_fr_ref_t **selected_fr, delta = s64_abs(now - (s64)dec_frs_p.a[fr_idx]->pts); if (delta < selected_fr_delta) { *selected_fr = dec_frs_p.a[fr_idx]; - *selected_fr_priv = dec_frs_p.priv_a + fr_idx; + *selected_fr_priv = dec_frs_p.priv_a[fr_idx]; selected_fr_idx = fr_idx; selected_fr_delta = delta; } @@ -319,7 +323,7 @@ static void select_fr(s64 now, avutil_video_fr_ref_t **selected_fr, struct dec_fr_priv_t *prev_fr_priv; if (selected_fr_idx >= 2) { - prev_fr_priv = dec_frs_p.priv_a + selected_fr_idx - 2; + prev_fr_priv = dec_frs_p.priv_a[selected_fr_idx - 2]; if (!prev_fr_priv->was_qed_to_pe) { /* hijack the time slot */ *selected_fr = dec_frs_p.a[selected_fr_idx - 2]; @@ -327,7 +331,7 @@ static void select_fr(s64 now, avutil_video_fr_ref_t **selected_fr, return; } } - prev_fr_priv = dec_frs_p.priv_a + selected_fr_idx - 1; + prev_fr_priv = dec_frs_p.priv_a[selected_fr_idx - 1]; if (!prev_fr_priv->was_qed_to_pe) { /* hijack the time slot */ *selected_fr = dec_frs_p.a[selected_fr_idx - 1]; @@ -346,7 +350,7 @@ static void frs_clear_last_qed_to_pe(void) if (fr == dec_frs_p.n) break; - fr_priv = dec_frs_p.priv_a + fr; + fr_priv = dec_frs_p.priv_a[fr]; if (fr_priv->is_last_qed_to_pe) { fr_priv->is_last_qed_to_pe = false; break; @@ -363,9 +367,9 @@ static void frs_reset(void) if (fr == dec_frs_p.n) break; avutil_video_fr_unref(dec_frs_p.a[fr]); + memset(dec_frs_p.priv_a[fr], 0, sizeof(**dec_frs_p.priv_a)); ++fr; } - memset(dec_frs_p.priv_a, 0, sizeof(*dec_frs_p.priv_a) * dec_frs_p.n); dec_frs_p.n = 0; } static void scaler_img_destroy(void) diff --git a/npv/video/public/code.frag.c b/npv/video/public/code.frag.c index 8cd0380..e7b22d9 100644 --- a/npv/video/public/code.frag.c +++ b/npv/video/public/code.frag.c @@ -93,7 +93,7 @@ static u8 dec_fr_try_receive(void) if (r == AVUTIL_AVERROR(EAGAIN)) return AGAIN; else if (r == 0) { - memset(&dec_frs_p.priv_a[fr], 0, sizeof(dec_frs_p.priv_a[fr])); + memset(dec_frs_p.priv_a[fr], 0, sizeof(**dec_frs_p.priv_a)); ++dec_frs_p.n; return HAVE_FR; } else if (r == AVUTIL_AVERROR_EOF) { diff --git a/npv/video/public/state.frag.h b/npv/video/public/state.frag.h index 8fde273..7918f89 100644 --- a/npv/video/public/state.frag.h +++ b/npv/video/public/state.frag.h @@ -22,7 +22,7 @@ static struct { u16 n; /* we did not factor the 2 following fields on purpose */ avutil_video_fr_ref_t **a; - struct dec_fr_priv_t *priv_a; + struct dec_fr_priv_t **priv_a; struct { avutil_video_fr_ref_t *fr; struct dec_fr_priv_t *fr_priv; -- 2.11.4.GIT