[PATCH 0/2] [RFC] avfilter/yadif experiment #2
Hi all Patch 1/2 fixes a lame bug compared to the last iteration Patch 2/2 is new for both patches testing and comments with sample videos are VERY welcome. This patchset is also largely based on feedback from the previous iteration So tell me where it looks bad and maybe ill find a way to make it look better ... PS: this is not designed with speed or ease of implementation in mind ATM, first iam trying to improve the vissual output, next would be to look at how to make it fast ...
This increases the in->out delay by 1 frame Signed-off-by: Michael Niedermayer <michaelni@gmx.at> --- libavfilter/vf_yadif.c | 77 +++++++++++++++++++++++++++++++++++------------- libavfilter/yadif.h | 8 +++-- 2 files changed, 63 insertions(+), 22 deletions(-) diff --git a/libavfilter/vf_yadif.c b/libavfilter/vf_yadif.c index 111948b..fca1579 100644 --- a/libavfilter/vf_yadif.c +++ b/libavfilter/vf_yadif.c @@ -74,6 +74,9 @@ typedef struct ThreadData { \ diff = FFMAX3(diff, min, -max); \ } \ + if (!(mode&4)) { \ + diff = FFMAX3(diff, FFABS(prev4[0] - prev2[0]), FFABS(next2[0] - next4[0])); \ + } \ \ if (spatial_pred > d + diff) \ spatial_pred = d + diff; \ @@ -88,10 +91,13 @@ typedef struct ThreadData { next++; \ prev2++; \ next2++; \ + prev4++; \ + next4++; \ } static void filter_line_c(void *dst1, - void *prev1, void *cur1, void *next1, + void *prev3, void *prev1, + void *cur1, void *next1, void *next3, int w, int prefs, int mrefs, int parity, int mode) { uint8_t *dst = dst1; @@ -101,6 +107,8 @@ static void filter_line_c(void *dst1, int x; uint8_t *prev2 = parity ? prev : cur ; uint8_t *next2 = parity ? cur : next; + uint8_t *prev4 = parity ? prev3: prev ; + uint8_t *next4 = parity ? next : next3; /* The function is called with the pointers already pointing to data[3] and * with 6 subtracted from the width. This allows the FILTER macro to be @@ -110,7 +118,8 @@ static void filter_line_c(void *dst1, } #define MAX_ALIGN 8 -static void filter_edges(void *dst1, void *prev1, void *cur1, void *next1, +static void filter_edges(void *dst1, void *prev3, void *prev1, + void *cur1, void *next1, void *next3, int w, int prefs, int mrefs, int parity, int mode) { uint8_t *dst = dst1; @@ -120,6 +129,8 @@ static void filter_edges(void *dst1, void *prev1, void *cur1, void *next1, int x; uint8_t *prev2 = parity ? prev : cur ; uint8_t *next2 = parity ? cur : next; + uint8_t *prev4 = parity ? prev3: prev ; + uint8_t *next4 = parity ? next : next3; /* Only edge pixels need to be processed here. A constant value of false * for is_not_edge should let the compiler ignore the whole branch. */ @@ -131,6 +142,8 @@ static void filter_edges(void *dst1, void *prev1, void *cur1, void *next1, next = (uint8_t*)next1 + w - (MAX_ALIGN-1); prev2 = (uint8_t*)(parity ? prev : cur); next2 = (uint8_t*)(parity ? cur : next); + prev4 = (uint8_t*)(parity ? prev3: next); + next4 = (uint8_t*)(parity ? prev : next3); FILTER(w - (MAX_ALIGN-1), w - 3, 1) FILTER(w - 3, w, 0) @@ -138,7 +151,8 @@ static void filter_edges(void *dst1, void *prev1, void *cur1, void *next1, static void filter_line_c_16bit(void *dst1, - void *prev1, void *cur1, void *next1, + void *prev3, void *prev1, + void *cur1, void *next1, void *next3, int w, int prefs, int mrefs, int parity, int mode) { @@ -149,13 +163,16 @@ static void filter_line_c_16bit(void *dst1, int x; uint16_t *prev2 = parity ? prev : cur ; uint16_t *next2 = parity ? cur : next; + uint16_t *prev4 = parity ? prev3: prev ; + uint16_t *next4 = parity ? next : next3; mrefs /= 2; prefs /= 2; FILTER(0, w, 1) } -static void filter_edges_16bit(void *dst1, void *prev1, void *cur1, void *next1, +static void filter_edges_16bit(void *dst1, void *prev3, void *prev1, + void *cur1, void *next1, void *next3, int w, int prefs, int mrefs, int parity, int mode) { uint16_t *dst = dst1; @@ -165,6 +182,8 @@ static void filter_edges_16bit(void *dst1, void *prev1, void *cur1, void *next1, int x; uint16_t *prev2 = parity ? prev : cur ; uint16_t *next2 = parity ? cur : next; + uint16_t *prev4 = parity ? prev3: prev ; + uint16_t *next4 = parity ? next : next3; mrefs /= 2; prefs /= 2; @@ -176,6 +195,8 @@ static void filter_edges_16bit(void *dst1, void *prev1, void *cur1, void *next1, next = (uint16_t*)next1 + w - (MAX_ALIGN/2-1); prev2 = (uint16_t*)(parity ? prev : cur); next2 = (uint16_t*)(parity ? cur : next); + prev4 = (uint16_t*)(parity ? prev3: next); + next4 = (uint16_t*)(parity ? prev : next3); FILTER(w - (MAX_ALIGN/2-1), w - 3, 1) FILTER(w - 3, w, 0) @@ -197,17 +218,19 @@ static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) */ for (y = slice_start; y < slice_end; y++) { if ((y ^ td->parity) & 1) { + uint8_t *prev2= &s->prev2->data[td->plane][y * refs]; uint8_t *prev = &s->prev->data[td->plane][y * refs]; uint8_t *cur = &s->cur ->data[td->plane][y * refs]; uint8_t *next = &s->next->data[td->plane][y * refs]; + uint8_t *next2= &s->next2->data[td->plane][y * refs]; uint8_t *dst = &td->frame->data[td->plane][y * td->frame->linesize[td->plane]]; int mode = y == 1 || y + 2 == td->h ? 2 : s->mode; - s->filter_line(dst + pix_3, prev + pix_3, cur + pix_3, - next + pix_3, td->w - (3 + MAX_ALIGN/df-1), + s->filter_line(dst + pix_3, prev2 + pix_3, prev + pix_3, cur + pix_3, + next + pix_3, next2 + pix_3, td->w - (3 + MAX_ALIGN/df-1), y + 1 < td->h ? refs : -refs, y ? -refs : refs, td->parity ^ td->tff, mode); - s->filter_edges(dst, prev, cur, next, td->w, + s->filter_edges(dst, prev2, prev, cur, next, next2, td->w, y + 1 < td->h ? refs : -refs, y ? -refs : refs, td->parity ^ td->tff, mode); @@ -319,24 +342,33 @@ static int filter_frame(AVFilterLink *link, AVFrame *frame) if (yadif->frame_pending) return_frame(ctx, 1); - if (yadif->prev) - av_frame_free(&yadif->prev); + if (yadif->prev2) + av_frame_free(&yadif->prev2); + yadif->prev2= yadif->prev; yadif->prev = yadif->cur; yadif->cur = yadif->next; - yadif->next = frame; + yadif->next = yadif->next2; + yadif->next2= frame; if (!yadif->cur) return 0; - if (checkstride(yadif, yadif->next, yadif->cur)) { + if (checkstride(yadif, yadif->next2, yadif->next)) { av_log(ctx, AV_LOG_VERBOSE, "Reallocating frame due to differing stride\n"); - fixstride(link, yadif->next); + fixstride(link, yadif->next2); } + if (checkstride(yadif, yadif->next2, yadif->next)) + fixstride(link, yadif->next); if (checkstride(yadif, yadif->next, yadif->cur)) fixstride(link, yadif->cur); if (yadif->prev && checkstride(yadif, yadif->next, yadif->prev)) fixstride(link, yadif->prev); - if (checkstride(yadif, yadif->next, yadif->cur) || (yadif->prev && checkstride(yadif, yadif->next, yadif->prev))) { + if (yadif->prev && checkstride(yadif, yadif->prev, yadif->prev2)) + fixstride(link, yadif->prev2); + if ( checkstride(yadif, yadif->next2, yadif->next) + || checkstride(yadif, yadif->next, yadif->cur) + || (yadif->prev && checkstride(yadif, yadif->next, yadif->prev)) + || (yadif->prev2 && checkstride(yadif, yadif->prev, yadif->prev2))) { av_log(ctx, AV_LOG_ERROR, "Failed to reallocate frame\n"); return -1; } @@ -355,6 +387,9 @@ static int filter_frame(AVFilterLink *link, AVFrame *frame) if (!yadif->prev && !(yadif->prev = av_frame_clone(yadif->cur))) return AVERROR(ENOMEM); + if (!yadif->prev2 && + !(yadif->prev2 = av_frame_clone(yadif->prev))) + return AVERROR(ENOMEM); yadif->out = ff_get_video_buffer(ctx->outputs[0], link->w, link->h); if (!yadif->out) @@ -382,21 +417,21 @@ static int request_frame(AVFilterLink *link) do { int ret; - if (yadif->eof) + if (yadif->eof>1) return AVERROR_EOF; ret = ff_request_frame(link->src->inputs[0]); if (ret == AVERROR_EOF && yadif->cur) { - AVFrame *next = av_frame_clone(yadif->next); + AVFrame *next = av_frame_clone(yadif->next2); if (!next) return AVERROR(ENOMEM); - next->pts = yadif->next->pts * 2 - yadif->cur->pts; + next->pts = yadif->next2->pts * 2 - yadif->next->pts; filter_frame(link->src->inputs[0], next); - yadif->eof = 1; + yadif->eof++; } else if (ret < 0) { return ret; } @@ -409,9 +444,11 @@ static av_cold void uninit(AVFilterContext *ctx) { YADIFContext *yadif = ctx->priv; + av_frame_free(&yadif->prev2); av_frame_free(&yadif->prev); av_frame_free(&yadif->cur ); av_frame_free(&yadif->next); + av_frame_free(&yadif->next2); } static int query_formats(AVFilterContext *ctx) @@ -484,8 +521,8 @@ static int config_props(AVFilterLink *link) s->filter_edges = filter_edges; } - if (ARCH_X86) - ff_yadif_init_x86(s); +// if (ARCH_X86) +// ff_yadif_init_x86(s); return 0; } @@ -497,7 +534,7 @@ static int config_props(AVFilterLink *link) #define CONST(name, help, val, unit) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, INT_MIN, INT_MAX, FLAGS, unit } static const AVOption yadif_options[] = { - { "mode", "specify the interlacing mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=YADIF_MODE_SEND_FRAME}, 0, 3, FLAGS, "mode"}, + { "mode", "specify the interlacing mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=YADIF_MODE_SEND_FRAME}, 0, 7, FLAGS, "mode"}, CONST("send_frame", "send one frame for each frame", YADIF_MODE_SEND_FRAME, "mode"), CONST("send_field", "send one frame for each field", YADIF_MODE_SEND_FIELD, "mode"), CONST("send_frame_nospatial", "send one frame for each frame, but skip spatial interlacing check", YADIF_MODE_SEND_FRAME_NOSPATIAL, "mode"), diff --git a/libavfilter/yadif.h b/libavfilter/yadif.h index 3ddf005..ee5c9af 100644 --- a/libavfilter/yadif.h +++ b/libavfilter/yadif.h @@ -50,17 +50,21 @@ typedef struct YADIFContext { int frame_pending; AVFrame *cur; + AVFrame *next2; AVFrame *next; AVFrame *prev; + AVFrame *prev2; AVFrame *out; /** * Required alignment for filter_line */ void (*filter_line)(void *dst, - void *prev, void *cur, void *next, + void *prev2, void *prev, void + *cur, void *next, void *next2, int w, int prefs, int mrefs, int parity, int mode); - void (*filter_edges)(void *dst, void *prev, void *cur, void *next, + void (*filter_edges)(void *dst, void *prev2, void *prev, + void *cur, void *next, void *next2, int w, int prefs, int mrefs, int parity, int mode); const AVPixFmtDescriptor *csp; -- 1.7.9.5
On 21 September 2013 01:37, Michael Niedermayer <michaelni@gmx.at> wrote:
This increases the in->out delay by 1 frame
Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
Does not appear to apply cleanly to git head. I think I made the patch apply (and 2/2) but my first test (running a zoneplate through) the quite high-frequency detail (close to the corners of the zoneplate image) were severely corrupted. -- Mark Himsley
On Mon, Sep 23, 2013 at 04:59:28PM +0100, Mark Himsley wrote:
On 21 September 2013 01:37, Michael Niedermayer <michaelni@gmx.at> wrote:
This increases the in->out delay by 1 frame
Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
Does not appear to apply cleanly to git head.
https://github.com/michaelni/FFmpeg/tree/yadif_experiment
I think I made the patch apply (and 2/2) but my first test (running a zoneplate through) the quite high-frequency detail (close to the corners of the zoneplate image) were severely corrupted.
where do i find the video and command line used to reproduce it? also you didnt say if its better or worse than before the patches [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB If you think the mosad wants you dead since a long time then you are either wrong or dead since a long time.
On 23/09/2013 18:45, Michael Niedermayer wrote:
On Mon, Sep 23, 2013 at 04:59:28PM +0100, Mark Himsley wrote:
On 21 September 2013 01:37, Michael Niedermayer <michaelni@gmx.at> wrote:
This increases the in->out delay by 1 frame
Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
Does not appear to apply cleanly to git head.
I'll try that.
I think I made the patch apply (and 2/2) but my first test (running a zoneplate through) the quite high-frequency detail (close to the corners of the zoneplate image) were severely corrupted.
where do i find the video and command line used to reproduce it? also you didnt say if its better or worse than before the patches
I thought "severely corrupted" was enough. Yes, corrupted compared to previous. My command was: ffmpeg -i "Zone Plate 85L.mov" -vf yadif=mode=1 -t 00:00:00.12 -f image2 zoneplate.yadifi.%06d.png I do not believe I am allowed to let that test file out, but I'll see if I can make something similar that demonstrates the corruption (assuming I see the corruption from your tree). -- Mark
On 23/09/2013 20:01, Mark Himsley wrote:
I do not believe I am allowed to let that test file out, but I'll see if I can make something similar that demonstrates the corruption (assuming I see the corruption from your tree).
For your reference, this image will do for a static zone plate. http://www.spearsandmunsil.com/cms/wp-content/uploads/2013/02/1920x1080p24_L... Michael, I am not getting the corruption from your repo - so I can't have applied your patch correctly. I have not had time to do proper quality tests yet, sorry, but work tasks have to take presidence. -- Mark
Signed-off-by: Michael Niedermayer <michaelni@gmx.at> --- libavfilter/vf_yadif.c | 108 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 103 insertions(+), 5 deletions(-) diff --git a/libavfilter/vf_yadif.c b/libavfilter/vf_yadif.c index fca1579..b815d7a 100644 --- a/libavfilter/vf_yadif.c +++ b/libavfilter/vf_yadif.c @@ -45,7 +45,73 @@ typedef struct ThreadData { spatial_score= score;\ spatial_pred= (cur[mrefs +(j)] + cur[prefs -(j)])>>1;\ -/* The is_not_edge argument here controls when the code will enter a branch +#define CHECK2(j)\ + { int score = FFABS(cur[mrefs - 1 + (j)] - cur[prefs - 1 - (j)])\ + + FFABS(cur[mrefs +(j)] - cur[prefs -(j)])\ + + FFABS(cur[mrefs + 1 + (j)] - cur[prefs + 1 - (j)]);\ + if (FFABS(j) > 0)\ + score += FFABS(cur[mrefs - 2 + (j)] - cur[prefs - 2 - (j)])\ + + FFABS(cur[mrefs + 2 + (j)] - cur[prefs + 2 - (j)]);\ + if (FFABS(j) > 1)\ + score += FFABS(cur[mrefs - 3 + (j)] - cur[prefs - 3 - (j)])\ + + FFABS(cur[mrefs + 3 + (j)] - cur[prefs + 3 - (j)]);\ + if (FFABS(j) > 2)\ + score += FFABS(cur[mrefs - 4 + (j)] - cur[prefs - 4 - (j)])\ + + FFABS(cur[mrefs + 4 + (j)] - cur[prefs + 4 - (j)]);\ + if (FFABS(j) > 3)\ + score += FFABS(cur[mrefs - 5 + (j)] - cur[prefs - 5 - (j)])\ + + FFABS(cur[mrefs + 5 + (j)] - cur[prefs + 5 - (j)]);\ + \ + if (score < spatial_score) {\ + spatial_score= score;\ + spatial_pred= (cur[mrefs +(j)] + cur[prefs -(j)])>>1;\ + +#define SQR(a) ((a)*(a)) + +#define CHECK3(j)\ + { int score = SQR(cur[mrefs - 1 + (j)] - cur[prefs - 1 - (j)])\ + + SQR(cur[mrefs + (j)] - cur[prefs -(j)])\ + + SQR(cur[mrefs + 1 + (j)] - cur[prefs + 1 - (j)])\ + + SQR(cur[3*mrefs - 1 + (j)] - cur[mrefs - 1 - (j)])\ + + SQR(cur[3*mrefs + (j)] - cur[mrefs - (j)])\ + + SQR(cur[3*mrefs + 1 + (j)] - cur[mrefs + 1 - (j)])\ + + SQR(cur[prefs - 1 + (j)] - cur[3*prefs - 1 - (j)])\ + + SQR(cur[prefs + (j)] - cur[3*prefs - (j)])\ + + SQR(cur[prefs + 1 + (j)] - cur[3*prefs + 1 - (j)]);\ + if (FFABS(j) > 0)\ + score += SQR(cur[mrefs - 2 + (j)] - cur[prefs - 2 - (j)])\ + + SQR(cur[mrefs + 2 + (j)] - cur[prefs + 2 - (j)])\ + + SQR(cur[3*mrefs - 2 + (j)] - cur[mrefs - 2 - (j)])\ + + SQR(cur[3*mrefs + 2 + (j)] - cur[mrefs + 2 - (j)])\ + + SQR(cur[prefs - 2 + (j)] - cur[3*prefs - 2 - (j)])\ + + SQR(cur[prefs + 2 + (j)] - cur[3*prefs + 2 - (j)]);\ + if (FFABS(j) > 1)\ + score += SQR(cur[mrefs - 3 + (j)] - cur[prefs - 3 - (j)])\ + + SQR(cur[mrefs + 3 + (j)] - cur[prefs + 3 - (j)])\ + + SQR(cur[3*mrefs - 3 + (j)] - cur[mrefs - 3 - (j)])\ + + SQR(cur[3*mrefs + 3 + (j)] - cur[mrefs + 3 - (j)])\ + + SQR(cur[prefs - 3 + (j)] - cur[3*prefs - 3 - (j)])\ + + SQR(cur[prefs + 3 + (j)] - cur[3*prefs + 3 - (j)]);\ + if (FFABS(j) > 2)\ + score += SQR(cur[mrefs - 4 + (j)] - cur[prefs - 4 - (j)])\ + + SQR(cur[mrefs + 4 + (j)] - cur[prefs + 4 - (j)])\ + + SQR(cur[3*mrefs - 4 + (j)] - cur[mrefs - 4 - (j)])\ + + SQR(cur[3*mrefs + 4 + (j)] - cur[mrefs + 4 - (j)])\ + + SQR(cur[prefs - 4 + (j)] - cur[3*prefs - 4 - (j)])\ + + SQR(cur[prefs + 4 + (j)] - cur[3*prefs + 4 - (j)]);\ + if (FFABS(j) > 3)\ + score += SQR(cur[mrefs - 5 + (j)] - cur[prefs - 5 - (j)])\ + + SQR(cur[mrefs + 5 + (j)] - cur[prefs + 5 - (j)])\ + + SQR(cur[3*mrefs - 5 + (j)] - cur[mrefs - 5 - (j)])\ + + SQR(cur[3*mrefs + 5 + (j)] - cur[mrefs + 5 - (j)])\ + + SQR(cur[prefs - 5 + (j)] - cur[3*prefs - 5 - (j)])\ + + SQR(cur[prefs + 5 + (j)] - cur[3*prefs + 5 - (j)]);\ + score = score * 256 / (9 + 6*FFABS(j));\ + if (score < spatial_score) {\ + spatial_score= score;\ + spatial_pred= (cur[mrefs +(j)] + cur[prefs -(j)])>>1;\ + + /* The is_not_edge argument here controls when the code will enter a branch * which reads up to and including x-3 and x+3. */ #define FILTER(start, end, is_not_edge) \ @@ -60,10 +126,42 @@ typedef struct ThreadData { int spatial_pred = (c+e) >> 1; \ \ if (is_not_edge) {\ - int spatial_score = FFABS(cur[mrefs - 1] - cur[prefs - 1]) + FFABS(c-e) \ - + FFABS(cur[mrefs + 1] - cur[prefs + 1]) - 1; \ - CHECK(-1) CHECK(-2) }} }} \ - CHECK( 1) CHECK( 2) }} }} \ + int spatial_score = INT_MAX;\ + int z_score = SQR(cur[3*mrefs - 2] - cur[3*mrefs + 1])\ + + SQR(cur[ mrefs - 2] - cur[ mrefs + 1]) \ + + SQR(cur[ prefs - 2] - cur[ prefs + 1]) \ + + SQR(cur[3*prefs - 2] - cur[3*prefs + 1]) \ + + SQR(cur[3*mrefs - 1] - cur[3*mrefs + 2])\ + + SQR(cur[ mrefs - 1] - cur[ mrefs + 2]) \ + + SQR(cur[ prefs - 1] - cur[ prefs + 2]) \ + + SQR(cur[3*prefs - 1] - cur[3*prefs + 2]) \ + + SQR(cur[3*mrefs - 3] - cur[3*mrefs])\ + + SQR(cur[ mrefs - 3] - cur[ mrefs]) \ + + SQR(cur[ prefs - 3] - cur[ prefs]) \ + + SQR(cur[3*prefs - 3] - cur[3*prefs]) \ + + SQR(cur[3*mrefs] - cur[3*mrefs + 3])\ + + SQR(cur[ mrefs] - cur[ mrefs + 3]) \ + + SQR(cur[ prefs] - cur[ prefs + 3]) \ + + SQR(cur[3*prefs] - cur[3*prefs + 3]) \ + + SQR(cur[3*mrefs - 4] - cur[3*mrefs - 1])\ + + SQR(cur[ mrefs - 4] - cur[ mrefs - 1]) \ + + SQR(cur[ prefs - 4] - cur[ prefs - 1]) \ + + SQR(cur[3*prefs - 4] - cur[3*prefs - 1]) \ + + SQR(cur[3*mrefs + 1] - cur[3*mrefs + 4])\ + + SQR(cur[ mrefs + 1] - cur[ mrefs + 4]) \ + + SQR(cur[ prefs + 1] - cur[ prefs + 4]) \ + + SQR(cur[3*prefs + 1] - cur[3*prefs + 4]); \ + CHECK3(0) }}\ + CHECK3(-1) CHECK3(-2) }} }} \ + CHECK3( 1) CHECK3( 2) }} }} \ + if (z_score * 256 / 8 < spatial_score) { \ + CHECK3(2) }} \ + CHECK3(-2) }} \ + CHECK3(3) }} \ + CHECK3(-3) }} \ + CHECK3(4) }} \ + CHECK3(-4) }} \ + }\ }\ \ if (!(mode&2)) { \ -- 1.7.9.5
participants (3)
-
Mark Himsley -
Mark Himsley -
Michael Niedermayer