Unified · Split
| @@ -32,6 +32,8 @@ typedef struct ScrollContext { · 31 unmodified lines | ||
| 32 | 32 | float h_pos, v_pos; |
| 33 | 33 | float h_ipos, v_ipos; |
| 34 | 34 | |
| 35 | + int pos_h[4], pos_v[4]; | |
| 36 | + | |
| 35 | 37 | const AVPixFmtDescriptor *desc; |
| 36 | 38 | int nb_planes; |
| 37 | 39 | int bytes; |
| @@ -70,10 +72,47 @@ static int query_formats(AVFilterContext *ctx) · 32 unmodified lines | ||
| 70 | 72 | return ff_set_common_formats(ctx, fmts_list); |
| 71 | 73 | } |
| 72 | 74 | |
| 73 | −static void scroll(ScrollContext *s, AVFrame *in, AVFrame *out) | |
| 75 | +typedef struct ThreadData { | |
| 76 | + AVFrame *in, *out; | |
| 77 | +} ThreadData; | |
| 78 | + | |
| 79 | +static int scroll_slice(AVFilterContext *ctx, void *arg, int jobnr, | |
| 80 | + int nb_jobs) | |
| 74 | 81 | { |
| 82 | + ScrollContext *s = ctx->priv; | |
| 83 | + ThreadData *td = arg; | |
| 84 | + AVFrame *in = td->in; | |
| 85 | + AVFrame *out = td->out; | |
| 86 | + | |
| 87 | + for (int p = 0; p < s->nb_planes; p++) { | |
| 88 | + const uint8_t *src = in->data[p]; | |
| 89 | + const int h = s->planeheight[p]; | |
| 90 | + const int w = s->planewidth[p] * s->bytes; | |
| 91 | + const int slice_start = (h * jobnr) / nb_jobs; | |
| 92 | + const int slice_end = (h * (jobnr+1)) / nb_jobs; | |
| 93 | + uint8_t *dst = out->data[p] + slice_start * out->linesize[p]; | |
| 94 | + | |
| 95 | + for (int y = slice_start; y < slice_end; y++) { | |
| 96 | + int yy = (y + s->pos_v[p]) % h; | |
| 97 | + const uint8_t *ssrc = src + yy * in->linesize[p]; | |
| 98 | + | |
| 99 | + if (s->pos_h[p] < w) | |
| 100 | + memcpy(dst, ssrc + s->pos_h[p], w - s->pos_h[p]); | |
| 101 | + if (s->pos_h[p] > 0) | |
| 102 | + memcpy(dst + w - s->pos_h[p], ssrc, s->pos_h[p]); | |
| 103 | + | |
| 104 | + dst += out->linesize[p]; | |
| 105 | + } | |
| 106 | + } | |
| 107 | + | |
| 108 | + return 0; | |
| 109 | +} | |
| 110 | + | |
| 111 | +static void scroll(AVFilterContext *ctx, AVFrame *in, AVFrame *out) | |
| 112 | +{ | |
| 113 | + ScrollContext *s = ctx->priv; | |
| 114 | + ThreadData td; | |
| 75 | 115 | int h_pos, v_pos; |
| 76 | − int pos_h[4], pos_v[4]; | |
| 77 | 116 | |
| 78 | 117 | s->h_pos = fmodf(s->h_pos, in->width); |
| 79 | 118 | s->v_pos = fmodf(s->v_pos, in->height); |
| @@ -86,29 +125,14 @@ static void scroll(ScrollContext *s, AVFrame *in, AVFrame *out) · 6 unmodified lines | ||
| 86 | 125 | if (v_pos < 0) |
| 87 | 126 | v_pos += in->height; |
| 88 | 127 | |
| 89 | − pos_v[1] = pos_v[2] = AV_CEIL_RSHIFT(v_pos, s->desc->log2_chroma_h); | |
| 90 | − pos_v[0] = pos_v[3] = v_pos; | |
| 91 | − pos_h[1] = pos_h[2] = AV_CEIL_RSHIFT(h_pos, s->desc->log2_chroma_w) * s->bytes; | |
| 92 | − pos_h[0] = pos_h[3] = h_pos * s->bytes; | |
| 128 | + s->pos_v[1] = s->pos_v[2] = AV_CEIL_RSHIFT(v_pos, s->desc->log2_chroma_h); | |
| 129 | + s->pos_v[0] = s->pos_v[3] = v_pos; | |
| 130 | + s->pos_h[1] = s->pos_h[2] = AV_CEIL_RSHIFT(h_pos, s->desc->log2_chroma_w) * s->bytes; | |
| 131 | + s->pos_h[0] = s->pos_h[3] = h_pos * s->bytes; | |
| 93 | 132 | |
| 94 | − for (int p = 0; p < s->nb_planes; p++) { | |
| 95 | − const uint8_t *src = in->data[p]; | |
| 96 | − uint8_t *dst = out->data[p]; | |
| 97 | − const int h = s->planeheight[p]; | |
| 98 | − const int w = s->planewidth[p] * s->bytes; | |
| 99 | − | |
| 100 | − for (int y = 0; y < h; y++) { | |
| 101 | − int yy = (y + pos_v[p]) % h; | |
| 102 | − const uint8_t *ssrc = src + yy * in->linesize[p]; | |
| 103 | − | |
| 104 | − if (pos_h[p] < w) | |
| 105 | − memcpy(dst, ssrc + pos_h[p], w - pos_h[p]); | |
| 106 | − if (pos_h[p] > 0) | |
| 107 | − memcpy(dst + w - pos_h[p], ssrc, pos_h[p]); | |
| 108 | − | |
| 109 | − dst += out->linesize[p]; | |
| 110 | − } | |
| 111 | − } | |
| 133 | + td.in = in; td.out = out; | |
| 134 | + ctx->internal->execute(ctx, scroll_slice, &td, NULL, | |
| 135 | + FFMIN(out->height, ff_filter_get_nb_threads(ctx))); | |
| 112 | 136 | |
| 113 | 137 | s->h_pos += s->h_speed * in->width; |
| 114 | 138 | s->v_pos += s->v_speed * in->height; |
| @@ -117,7 +141,6 @@ static void scroll(ScrollContext *s, AVFrame *in, AVFrame *out) · 2 unmodified lines | ||
| 117 | 141 | static int filter_frame(AVFilterLink *inlink, AVFrame *in) |
| 118 | 142 | { |
| 119 | 143 | AVFilterContext *ctx = inlink->dst; |
| 120 | − ScrollContext *s = ctx->priv; | |
| 121 | 144 | AVFilterLink *outlink = ctx->outputs[0]; |
| 122 | 145 | AVFrame *out; |
| 123 | 146 | |
| @@ -128,7 +151,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) · 4 unmodified lines | ||
| 128 | 151 | } |
| 129 | 152 | av_frame_copy_props(out, in); |
| 130 | 153 | |
| 131 | − scroll(s, in, out); | |
| 154 | + scroll(ctx, in, out); | |
| 132 | 155 | |
| 133 | 156 | av_frame_free(&in); |
| 134 | 157 | return ff_filter_frame(outlink, out); |
| @@ -196,6 +219,6 @@ AVFilter ff_vf_scroll = { · 61 unmodified lines | ||
| 196 | 219 | .query_formats = query_formats, |
| 197 | 220 | .inputs = scroll_inputs, |
| 198 | 221 | .outputs = scroll_outputs, |
| 199 | − .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC, | |
| 222 | + .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS, | |
| 200 | 223 | .process_command = ff_filter_process_command, |
| 201 | 224 | }; |