* qatar/master: vf_fade: support slice threading vf_yadif: support slice threading Conflicts: libavfilter/vf_fade.c libavfilter/vf_yadif.c
Unified · Split
| @@ -158,11 +158,61 @@ static void fade_plane(int y, int h, int w, · 157 unmodified lines | ||
| 158 | 158 | } |
| 159 | 159 | } |
| 160 | 160 | |
| 161 | −static int filter_frame(AVFilterLink *inlink, AVFrame *frame) | |
| 161 | +static int filter_slice_luma(AVFilterContext *ctx, void *arg, int jobnr, | |
| 162 | + int nb_jobs) | |
| 162 | 163 | { |
| 163 | − FadeContext *s = inlink->dst->priv; | |
| 164 | − uint8_t *p; | |
| 164 | + FadeContext *s = ctx->priv; | |
| 165 | + AVFrame *frame = arg; | |
| 166 | + int slice_h = frame->height / nb_jobs; | |
| 167 | + int slice_start = jobnr * slice_h; | |
| 168 | + int slice_end = (jobnr == nb_jobs - 1) ? frame->height : (jobnr + 1) * slice_h; | |
| 169 | + int i, j; | |
| 170 | + | |
| 171 | + for (i = slice_start; i < slice_end; i++) { | |
| 172 | + uint8_t *p = frame->data[0] + i * frame->linesize[0]; | |
| 173 | + for (j = 0; j < frame->width * s->bpp; j++) { | |
| 174 | + /* s->factor is using 16 lower-order bits for decimal | |
| 175 | + * places. 32768 = 1 << 15, it is an integer representation | |
| 176 | + * of 0.5 and is for rounding. */ | |
| 177 | + *p = ((*p - s->black_level) * s->factor + s->black_level_scaled) >> 16; | |
| 178 | + p++; | |
| 179 | + } | |
| 180 | + } | |
| 181 | + | |
| 182 | + return 0; | |
| 183 | +} | |
| 184 | + | |
| 185 | +static int filter_slice_chroma(AVFilterContext *ctx, void *arg, int jobnr, | |
| 186 | + int nb_jobs) | |
| 187 | +{ | |
| 188 | + FadeContext *s = ctx->priv; | |
| 189 | + AVFrame *frame = arg; | |
| 190 | + int slice_h = FFALIGN(frame->height / nb_jobs, 1 << s->vsub); | |
| 191 | + int slice_start = jobnr * slice_h; | |
| 192 | + int slice_end = (jobnr == nb_jobs - 1) ? frame->height : (jobnr + 1) * slice_h; | |
| 165 | 193 | int i, j, plane; |
| 194 | + const int width = FF_CEIL_RSHIFT(frame->width, s->hsub); | |
| 195 | + | |
| 196 | + for (plane = 1; plane < 3; plane++) { | |
| 197 | + for (i = slice_start; i < slice_end; i++) { | |
| 198 | + uint8_t *p = frame->data[plane] + (i >> s->vsub) * frame->linesize[plane]; | |
| 199 | + for (j = 0; j < width; j++) { | |
| 200 | + /* 8421367 = ((128 << 1) + 1) << 15. It is an integer | |
| 201 | + * representation of 128.5. The .5 is for rounding | |
| 202 | + * purposes. */ | |
| 203 | + *p = ((*p - 128) * s->factor + 8421367) >> 16; | |
| 204 | + p++; | |
| 205 | + } | |
| 206 | + } | |
| 207 | + } | |
| 208 | + | |
| 209 | + return 0; | |
| 210 | +} | |
| 211 | + | |
| 212 | +static int filter_frame(AVFilterLink *inlink, AVFrame *frame) | |
| 213 | +{ | |
| 214 | + AVFilterContext *ctx = inlink->dst; | |
| 215 | + FadeContext *s = ctx->priv; | |
| 166 | 216 | double frame_timestamp = frame->pts == AV_NOPTS_VALUE ? -1 : frame->pts * av_q2d(inlink->time_base); |
| 167 | 217 | |
| 168 | 218 | // Calculate Fade assuming this is a Fade In |
| @@ -216,7 +266,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *frame) · 47 unmodified lines | ||
| 216 | 266 | if (s->factor < UINT16_MAX) { |
| 217 | 267 | if (s->alpha) { |
| 218 | 268 | // alpha only |
| 219 | − plane = s->is_packed_rgb ? 0 : A; // alpha is on plane 0 for packed formats | |
| 269 | + int plane = s->is_packed_rgb ? 0 : A; // alpha is on plane 0 for packed formats | |
| 220 | 270 | // or plane 3 for planar formats |
| 221 | 271 | fade_plane(0, frame->height, inlink->w, |
| 222 | 272 | s->factor, s->black_level, s->black_level_scaled, |
| @@ -225,25 +275,13 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *frame) · 2 unmodified lines | ||
| 225 | 275 | 1, frame->data[plane], frame->linesize[plane]); |
| 226 | 276 | } else { |
| 227 | 277 | /* luma or rgb plane */ |
| 228 | − fade_plane(0, frame->height, inlink->w, | |
| 229 | − s->factor, s->black_level, s->black_level_scaled, | |
| 230 | − 0, 1, // offset & pixstep for Y plane or RGB packed format | |
| 231 | − s->bpp, frame->data[0], frame->linesize[0]); | |
| 278 | + ctx->internal->execute(ctx, filter_slice_luma, frame, NULL, | |
| 279 | + FFMIN(frame->height, ctx->graph->nb_threads)); | |
| 280 | + | |
| 232 | 281 | if (frame->data[1] && frame->data[2]) { |
| 233 | 282 | /* chroma planes */ |
| 234 | − for (plane = 1; plane < 3; plane++) { | |
| 235 | − for (i = 0; i < frame->height; i++) { | |
| 236 | − const int width = FF_CEIL_RSHIFT(inlink->w, s->hsub); | |
| 237 | − p = frame->data[plane] + (i >> s->vsub) * frame->linesize[plane]; | |
| 238 | − for (j = 0; j < width; j++) { | |
| 239 | − /* 8421367 = ((128 << 1) + 1) << 15. It is an integer | |
| 240 | − * representation of 128.5. The .5 is for rounding | |
| 241 | − * purposes. */ | |
| 242 | − *p = ((*p - 128) * s->factor + 8421367) >> 16; | |
| 243 | − p++; | |
| 244 | − } | |
| 245 | − } | |
| 246 | − } | |
| 283 | + ctx->internal->execute(ctx, filter_slice_chroma, frame, NULL, | |
| 284 | + FFMIN(frame->height, ctx->graph->nb_threads)); | |
| 247 | 285 | } |
| 248 | 286 | } |
| 249 | 287 | } |
| @@ -314,4 +352,5 @@ AVFilter avfilter_vf_fade = { · 64 unmodified lines | ||
| 314 | 352 | |
| 315 | 353 | .inputs = avfilter_vf_fade_inputs, |
| 316 | 354 | .outputs = avfilter_vf_fade_outputs, |
| 355 | + .flags = AVFILTER_FLAG_SLICE_THREADS, | |
| 317 | 356 | }; |
| @@ -31,6 +31,14 @@ · 30 unmodified lines | ||
| 31 | 31 | #undef NDEBUG |
| 32 | 32 | #include <assert.h> |
| 33 | 33 | |
| 34 | +typedef struct ThreadData { | |
| 35 | + AVFrame *frame; | |
| 36 | + int plane; | |
| 37 | + int w, h; | |
| 38 | + int parity; | |
| 39 | + int tff; | |
| 40 | +} ThreadData; | |
| 41 | + | |
| 34 | 42 | #define CHECK(j)\ |
| 35 | 43 | { int score = FFABS(cur[mrefs - 1 + (j)] - cur[prefs - 1 - (j)])\ |
| 36 | 44 | + FFABS(cur[mrefs +(j)] - cur[prefs -(j)])\ |
| @@ -172,50 +180,67 @@ static void filter_edges_16bit(void *dst1, void *prev1, void *cur1, void *next1, · 135 unmodified lines | ||
| 172 | 180 | FILTER(w - 3, w, 0) |
| 173 | 181 | } |
| 174 | 182 | |
| 183 | +static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) | |
| 184 | +{ | |
| 185 | + YADIFContext *s = ctx->priv; | |
| 186 | + ThreadData *td = arg; | |
| 187 | + int refs = s->cur->linesize[td->plane]; | |
| 188 | + int df = (s->csp->comp[td->plane].depth_minus1 + 8) / 8; | |
| 189 | + int pix_3 = 3 * df; | |
| 190 | + int slice_h = td->h / nb_jobs; | |
| 191 | + int slice_start = jobnr * slice_h; | |
| 192 | + int slice_end = (jobnr == nb_jobs - 1) ? td->h : (jobnr + 1) * slice_h; | |
| 193 | + int y; | |
| 194 | + | |
| 195 | + /* filtering reads 3 pixels to the left/right; to avoid invalid reads, | |
| 196 | + * we need to call the c variant which avoids this for border pixels | |
| 197 | + */ | |
| 198 | + for (y = slice_start; y < slice_end; y++) { | |
| 199 | + if ((y ^ td->parity) & 1) { | |
| 200 | + uint8_t *prev = &s->prev->data[td->plane][y * refs]; | |
| 201 | + uint8_t *cur = &s->cur ->data[td->plane][y * refs]; | |
| 202 | + uint8_t *next = &s->next->data[td->plane][y * refs]; | |
| 203 | + uint8_t *dst = &td->frame->data[td->plane][y * td->frame->linesize[td->plane]]; | |
| 204 | + int mode = y == 1 || y + 2 == td->h ? 2 : s->mode; | |
| 205 | + s->filter_line(dst + pix_3, prev + pix_3, cur + pix_3, | |
| 206 | + next + pix_3, td->w - 6, | |
| 207 | + y + 1 < td->h ? refs : -refs, | |
| 208 | + y ? -refs : refs, | |
| 209 | + td->parity ^ td->tff, mode); | |
| 210 | + s->filter_edges(dst, prev, cur, next, td->w, | |
| 211 | + y + 1 < td->h ? refs : -refs, | |
| 212 | + y ? -refs : refs, | |
| 213 | + td->parity ^ td->tff, mode); | |
| 214 | + } else { | |
| 215 | + memcpy(&td->frame->data[td->plane][y * td->frame->linesize[td->plane]], | |
| 216 | + &s->cur->data[td->plane][y * refs], td->w * df); | |
| 217 | + } | |
| 218 | + } | |
| 219 | + return 0; | |
| 220 | +} | |
| 221 | + | |
| 175 | 222 | static void filter(AVFilterContext *ctx, AVFrame *dstpic, |
| 176 | 223 | int parity, int tff) |
| 177 | 224 | { |
| 178 | 225 | YADIFContext *yadif = ctx->priv; |
| 179 | − int y, i; | |
| 226 | + ThreadData td = { .frame = dstpic, .parity = parity, .tff = tff }; | |
| 227 | + int i; | |
| 180 | 228 | |
| 181 | 229 | for (i = 0; i < yadif->csp->nb_components; i++) { |
| 182 | 230 | int w = dstpic->width; |
| 183 | 231 | int h = dstpic->height; |
| 184 | − int refs = yadif->cur->linesize[i]; | |
| 185 | − int df = (yadif->csp->comp[i].depth_minus1 + 8) / 8; | |
| 186 | − int pix_3 = 3 * df; | |
| 187 | 232 | |
| 188 | 233 | if (i == 1 || i == 2) { |
| 189 | − /* Why is this not part of the per-plane description thing? */ | |
| 190 | 234 | w = FF_CEIL_RSHIFT(w, yadif->csp->log2_chroma_w); |
| 191 | 235 | h = FF_CEIL_RSHIFT(h, yadif->csp->log2_chroma_h); |
| 192 | 236 | } |
| 193 | 237 | |
| 194 | − /* filtering reads 3 pixels to the left/right; to avoid invalid reads, | |
| 195 | − * we need to call the c variant which avoids this for border pixels | |
| 196 | − */ | |
| 197 | − | |
| 198 | − for (y = 0; y < h; y++) { | |
| 199 | − if ((y ^ parity) & 1) { | |
| 200 | − uint8_t *prev = &yadif->prev->data[i][y * refs]; | |
| 201 | − uint8_t *cur = &yadif->cur ->data[i][y * refs]; | |
| 202 | − uint8_t *next = &yadif->next->data[i][y * refs]; | |
| 203 | − uint8_t *dst = &dstpic->data[i][y * dstpic->linesize[i]]; | |
| 204 | − int mode = y == 1 || y + 2 == h ? 2 : yadif->mode; | |
| 205 | − yadif->filter_line(dst + pix_3, prev + pix_3, cur + pix_3, | |
| 206 | − next + pix_3, w - 6, | |
| 207 | − y + 1 < h ? refs : -refs, | |
| 208 | − y ? -refs : refs, | |
| 209 | − parity ^ tff, mode); | |
| 210 | − yadif->filter_edges(dst, prev, cur, next, w, | |
| 211 | − y + 1 < h ? refs : -refs, | |
| 212 | − y ? -refs : refs, | |
| 213 | − parity ^ tff, mode); | |
| 214 | − } else { | |
| 215 | − memcpy(&dstpic->data[i][y * dstpic->linesize[i]], | |
| 216 | − &yadif->cur->data[i][y * refs], w * df); | |
| 217 | − } | |
| 218 | − } | |
| 238 | + | |
| 239 | + td.w = w; | |
| 240 | + td.h = h; | |
| 241 | + td.plane = i; | |
| 242 | + | |
| 243 | + ctx->internal->execute(ctx, filter_slice, &td, NULL, FFMIN(h, ctx->graph->nb_threads)); | |
| 219 | 244 | } |
| 220 | 245 | |
| 221 | 246 | emms_c(); |
| @@ -484,5 +509,5 @@ AVFilter avfilter_vf_yadif = { · 262 unmodified lines | ||
| 484 | 509 | |
| 485 | 510 | .inputs = avfilter_vf_yadif_inputs, |
| 486 | 511 | .outputs = avfilter_vf_yadif_outputs, |
| 487 | − .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL, | |
| 512 | + .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS, | |
| 488 | 513 | }; |