History

avfilter/vf_zscale: fix several issues in previous commit

Paul B Mahol committed 4 years ago

Commit 837c55d1 parent d0aefc31 file · +25 −35Browse files

Unified · Split

libavfilter/vf_zscale.c+25−35
@@ -44,8 +44,7 @@ · 43 unmodified lines
4444 #include "libavutil/pixdesc.h"
4545 #include "libavutil/imgutils.h"
4646
47−#define ZIMG_ALIGNMENT 32
48−#define MIN_TILESIZE 64
47+#define ZIMG_ALIGNMENT 64
4948 #define MAX_THREADS 64
5049
5150 static const char *const var_names[] = {
@@ -118,7 +117,6 @@ typedef struct ZScaleContext { · 66 unmodified lines
118117
119118 void *tmp[MAX_THREADS]; //separate for each thread;
120119 int nb_threads;
121− int slice_h;
122120
123121 zimg_image_format src_format, dst_format;
124122 zimg_image_format alpha_src_format, alpha_dst_format;
@@ -566,48 +564,47 @@ static void format_init(zimg_image_format *format, AVFrame *frame, const AVPixFm · 441 unmodified lines
566564 }
567565
568566 static int graphs_build(AVFrame *in, AVFrame *out, const AVPixFmtDescriptor *desc, const AVPixFmtDescriptor *out_desc,
569− ZScaleContext *s, int job_nr)
567+ AVFilterContext *ctx, int job_nr, int n_jobs)
570568 {
569+ ZScaleContext *s = ctx->priv;
571570 int ret;
572571 size_t size;
573572 zimg_image_format src_format;
574573 zimg_image_format dst_format;
575574 zimg_image_format alpha_src_format;
576575 zimg_image_format alpha_dst_format;
576+ const int in_slice_start = 4 * ((((in->height + 2) / 4) * job_nr) / n_jobs);
577+ const int in_slice_end = (job_nr == n_jobs-1) ? in->height : 4 * (((in->height + 2) / 4) * (job_nr+1) / n_jobs);
578+ const int out_slice_start = 4 * ((((out->height + 2) / 4) * job_nr) / n_jobs);
579+ const int out_slice_end = (job_nr == n_jobs-1) ? out->height : 4 * (((out->height + 2) / 4) * (job_nr+1) / n_jobs);
577580
578581 src_format = s->src_format;
579582 dst_format = s->dst_format;
580583 /* The input slice is specified through the active_region field,
581584 unlike the output slice.
582− according to zimg requirements input and output slices should have even dimentions */
585+ according to zimg requirements input and output slices should have even dimensions */
583586 src_format.active_region.width = in->width;
584− src_format.active_region.height = s->slice_h;
587+ src_format.active_region.height = in_slice_end - in_slice_start;
585588 src_format.active_region.left = 0;
586− src_format.active_region.top = job_nr * src_format.active_region.height;
589+ src_format.active_region.top = in_slice_start;
587590 //dst now is the single tile only!!
588591 dst_format.width = out->width;
589− dst_format.height = ((unsigned int)(out->height / s->nb_threads)) & 0xfffffffe;
590−
591− //the last slice could differ from the previous ones due to the slices division "tail"
592− if (job_nr == (s->nb_threads - 1)) {
593− src_format.active_region.height = src_format.height - src_format.active_region.top;
594− dst_format.height = out->height - job_nr * dst_format.height;
595− }
592+ dst_format.height = out_slice_end - out_slice_start;
596593
597594 if (s->graph[job_nr]) {
598595 zimg_filter_graph_free(s->graph[job_nr]);
599596 }
600597 s->graph[job_nr] = zimg_filter_graph_build(&src_format, &dst_format, &s->params);
601598 if (!s->graph[job_nr])
602− return print_zimg_error(NULL);
599+ return print_zimg_error(ctx);
603600
604601 ret = zimg_filter_graph_get_tmp_size(s->graph[job_nr], &size);
605602 if (ret)
606− return print_zimg_error(NULL);
603+ return print_zimg_error(ctx);
607604
608605 if (s->tmp[job_nr])
609606 av_freep(&s->tmp[job_nr]);
610− s->tmp[job_nr] = av_malloc(size);
607+ s->tmp[job_nr] = av_calloc(size, 1);
611608 if (!s->tmp[job_nr])
612609 return AVERROR(ENOMEM);
613610
@@ -617,25 +614,19 @@ static int graphs_build(AVFrame *in, AVFrame *out, const AVPixFmtDescriptor *des · 3 unmodified lines
617614 /* The input slice is specified through the active_region field, unlike the output slice.
618615 according to zimg requirements input and output slices should have even dimentions */
619616 alpha_src_format.active_region.width = in->width;
620− alpha_src_format.active_region.height = s->slice_h;
617+ alpha_src_format.active_region.height = in_slice_end - in_slice_start;
621618 alpha_src_format.active_region.left = 0;
622− alpha_src_format.active_region.top = job_nr * alpha_src_format.active_region.height;
619+ alpha_src_format.active_region.top = in_slice_start;
623620 //dst now is the single tile only!!
624621 alpha_dst_format.width = out->width;
625− alpha_dst_format.height = ((unsigned int)(out->height / s->nb_threads)) & 0xfffffffe;
626−
627− //the last slice could differ from the previous ones due to the slices division "tail"
628− if (job_nr == (s->nb_threads - 1)) {
629− alpha_src_format.active_region.height = alpha_src_format.height - alpha_src_format.active_region.top;
630− alpha_dst_format.height = out->height - job_nr * alpha_dst_format.height;
631− }
622+ alpha_dst_format.height = out_slice_end - out_slice_start;
632623
633624 if (s->alpha_graph[job_nr]) {
634625 zimg_filter_graph_free(s->alpha_graph[job_nr]);
635626 }
636627 s->alpha_graph[job_nr] = zimg_filter_graph_build(&alpha_src_format, &alpha_dst_format, &s->alpha_params);
637628 if (!s->alpha_graph[job_nr])
638− return print_zimg_error(NULL);
629+ return print_zimg_error(ctx);
639630 }
640631 return 0;
641632 }
@@ -702,12 +693,11 @@ static int filter_slice(AVFilterContext *ctx, void *data, int job_nr, int n_jobs · 60 unmodified lines
702693 ThreadData *td = data;
703694 int ret = 0;
704695 int p;
705− int out_sampl;
706696 int need_gb;
707697 ZScaleContext *s = ctx->priv;
708698 zimg_image_buffer_const src_buf = { ZIMG_API_VERSION };
709699 zimg_image_buffer dst_buf = { ZIMG_API_VERSION };
710− int dst_tile_height = ((unsigned int)(td->out->height / n_jobs)) & 0xfffffffe;
700+ const int out_slice_start = 4 * ((((td->out->height + 2) / 4) * job_nr) / n_jobs);
711701
712702 /* create zimg filter graphs for each thread
713703 only if not created earlier or there is some change in frame parameters */
@@ -720,12 +710,13 @@ static int filter_slice(AVFilterContext *ctx, void *data, int job_nr, int n_jobs · 6 unmodified lines
720710 compare_zimg_graph_builder_params(&s->alpha_params, &s->alpha_params_tmp);
721711
722712 if (need_gb){
723− ret = graphs_build(td->in, td->out, td->desc, td->odesc, s, job_nr);
713+ ret = graphs_build(td->in, td->out, td->desc, td->odesc, ctx, job_nr, n_jobs);
724714 if (ret < 0)
725715 return print_zimg_error(ctx);
726716 }
727− out_sampl = FFMAX3(td->out->linesize[0], td->out->linesize[1], td->out->linesize[2]);
728717 for (int i = 0; i < 3; i++) {
718+ const int vsamp = i >= 1 ? td->odesc->log2_chroma_h : 0;
719+
729720 p = td->desc->comp[i].plane;
730721
731722 src_buf.plane[i].data = td->in->data[p];
@@ -733,7 +724,7 @@ static int filter_slice(AVFilterContext *ctx, void *data, int job_nr, int n_jobs · 1 unmodified line
733724 src_buf.plane[i].mask = -1;
734725
735726 p = td->odesc->comp[i].plane;
736− dst_buf.plane[i].data = td->out->data[p] + td->out->linesize[p] * dst_tile_height * td->out->linesize[p] / out_sampl * job_nr;
727+ dst_buf.plane[i].data = td->out->data[p] + td->out->linesize[p] * (out_slice_start >> vsamp);
737728 dst_buf.plane[i].stride = td->out->linesize[p];
738729 dst_buf.plane[i].mask = -1;
739730 }
@@ -746,7 +737,7 @@ static int filter_slice(AVFilterContext *ctx, void *data, int job_nr, int n_jobs · 6 unmodified lines
746737 src_buf.plane[0].stride = td->in->linesize[3];
747738 src_buf.plane[0].mask = -1;
748739
749− dst_buf.plane[0].data = td->out->data[3] + td->out->linesize[3] * dst_tile_height * job_nr;
740+ dst_buf.plane[0].data = td->out->data[3] + td->out->linesize[3] * out_slice_start;
750741 dst_buf.plane[0].stride = td->out->linesize[3];
751742 dst_buf.plane[0].mask = -1;
752743
@@ -804,8 +795,7 @@ static int filter_frame(AVFilterLink *link, AVFrame *in) · 51 unmodified lines
804795 link->dst->inputs[0]->w = in->width;
805796 link->dst->inputs[0]->h = in->height;
806797
807− s->nb_threads = FFMIN(ff_filter_get_nb_threads(ctx), link->h / MIN_TILESIZE);
808− s->slice_h = ((unsigned int)(link->h / s->nb_threads)) & 0xfffffffe; // slice_h should be even for zimg
798+ s->nb_threads = FFMIN(ff_filter_get_nb_threads(ctx), link->h / 8);
809799 s->in_colorspace = in->colorspace;
810800 s->in_trc = in->color_trc;
811801 s->in_primaries = in->color_primaries;

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t