Showing the changes to libavfilter (1 of 1 file). Show all
Unified · Split
| @@ -39,6 +39,7 @@ typedef struct LenscorrectionCtx { · 38 unmodified lines | |||
| 39 | int width; | 39 | int width; |
| 40 | int height; | 40 | int height; |
| 41 | int hsub, vsub; | 41 | int hsub, vsub; |
| 42 | int depth; | ||
| 42 | int nb_planes; | 43 | int nb_planes; |
| 43 | double cx, cy, k1, k2; | 44 | double cx, cy, k1, k2; |
| 44 | int interpolation; | 45 | int interpolation; |
| @@ -66,113 +67,137 @@ AVFILTER_DEFINE_CLASS(lenscorrection); · 21 unmodified lines | |||
| 66 | typedef struct ThreadData { | 67 | typedef struct ThreadData { |
| 67 | AVFrame *in, *out; | 68 | AVFrame *in, *out; |
| 68 | int w, h; | 69 | int w, h; |
| 70 | int depth; | ||
| 69 | int plane; | 71 | int plane; |
| 70 | int xcenter, ycenter; | 72 | int xcenter, ycenter; |
| 71 | int32_t *correction; | 73 | int32_t *correction; |
| 72 | } ThreadData; | 74 | } ThreadData; |
| 73 | 75 | ||
| 74 | static int filter_slice(AVFilterContext *ctx, void *arg, int job, int nb_jobs) | 76 | #define NEAREST(type, name) \ |
| 75 | { | 77 | static int filter##name##_slice(AVFilterContext *ctx, void *arg, int job, \ |
| 76 | ThreadData *td = arg; | 78 | int nb_jobs) \ |
| 77 | AVFrame *in = td->in; | 79 | { \ |
| 78 | AVFrame *out = td->out; | 80 | ThreadData *td = arg; \ |
| 79 | 81 | AVFrame *in = td->in; \ | |
| 80 | const int w = td->w, h = td->h; | 82 | AVFrame *out = td->out; \ |
| 81 | const int xcenter = td->xcenter; | 83 | \ |
| 82 | const int ycenter = td->ycenter; | 84 | const int w = td->w, h = td->h; \ |
| 83 | const int start = (h * job ) / nb_jobs; | 85 | const int xcenter = td->xcenter; \ |
| 84 | const int end = (h * (job+1)) / nb_jobs; | 86 | const int ycenter = td->ycenter; \ |
| 85 | const int plane = td->plane; | 87 | const int start = (h * job ) / nb_jobs; \ |
| 86 | const int inlinesize = in->linesize[plane]; | 88 | const int end = (h * (job+1)) / nb_jobs; \ |
| 87 | const int outlinesize = out->linesize[plane]; | 89 | const int plane = td->plane; \ |
| 88 | const uint8_t *indata = in->data[plane]; | 90 | const int inlinesize = in->linesize[plane] / sizeof(type); \ |
| 89 | uint8_t *outrow = out->data[plane] + start * outlinesize; | 91 | const int outlinesize = out->linesize[plane] / sizeof(type); \ |
| 90 | int i; | 92 | const type *indata = (const type *)in->data[plane]; \ |
| 91 | for (i = start; i < end; i++, outrow += outlinesize) { | 93 | type *outrow = (type *)out->data[plane] + start * outlinesize; \ |
| 92 | const int off_y = i - ycenter; | 94 | for (int i = start; i < end; i++, outrow += outlinesize) { \ |
| 93 | uint8_t *out = outrow; | 95 | const int off_y = i - ycenter; \ |
| 94 | int j; | 96 | type *out = outrow; \ |
| 95 | for (j = 0; j < w; j++) { | 97 | for (int j = 0; j < w; j++) { \ |
| 96 | const int off_x = j - xcenter; | 98 | const int off_x = j - xcenter; \ |
| 97 | const int64_t radius_mult = td->correction[j + i*w]; | 99 | const int64_t radius_mult = td->correction[j + i*w]; \ |
| 98 | const int x = xcenter + ((radius_mult * off_x + (1<<23))>>24); | 100 | const int x = xcenter + ((radius_mult * off_x + (1<<23))>>24); \ |
| 99 | const int y = ycenter + ((radius_mult * off_y + (1<<23))>>24); | 101 | const int y = ycenter + ((radius_mult * off_y + (1<<23))>>24); \ |
| 100 | const char isvalid = x >= 0 && x < w && y >= 0 && y < h; | 102 | const char isvalid = x >= 0 && x < w && y >= 0 && y < h; \ |
| 101 | *out++ = isvalid ? indata[y * inlinesize + x] : 0; | 103 | *out++ = isvalid ? indata[y * inlinesize + x] : 0; \ |
| 102 | } | 104 | } \ |
| 103 | } | 105 | } \ |
| 104 | return 0; | 106 | return 0; \ |
| 105 | } | 107 | } |
| 106 | 108 | ||
| 107 | static int filter_slice_bilinear(AVFilterContext *ctx, void *arg, int job, int nb_jobs) | ||
| 108 | { | ||
| 109 | ThreadData *td = arg; | ||
| 110 | AVFrame *in = td->in; | ||
| 111 | AVFrame *out = td->out; | ||
| 112 | |||
| 113 | const int64_t max = (1 << 24) - 1; | ||
| 114 | const int64_t add = (1 << 23); | ||
| 115 | const int w = td->w, h = td->h; | ||
| 116 | const int xcenter = td->xcenter; | ||
| 117 | const int ycenter = td->ycenter; | ||
| 118 | const int start = (h * job ) / nb_jobs; | ||
| 119 | const int end = (h * (job+1)) / nb_jobs; | ||
| 120 | const int plane = td->plane; | ||
| 121 | const int inlinesize = in->linesize[plane]; | ||
| 122 | const int outlinesize = out->linesize[plane]; | ||
| 123 | const uint8_t *indata = in->data[plane]; | ||
| 124 | uint8_t *outrow = out->data[plane] + start * outlinesize; | ||
| 125 | |||
| 126 | for (int i = start; i < end; i++, outrow += outlinesize) { | ||
| 127 | const int off_y = i - ycenter; | ||
| 128 | uint8_t *out = outrow; | ||
| 129 | |||
| 130 | for (int j = 0; j < w; j++) { | ||
| 131 | const int off_x = j - xcenter; | ||
| 132 | const int64_t radius_mult = td->correction[j + i*w]; | ||
| 133 | const int x = xcenter + ((radius_mult * off_x + (1<<23)) >> 24); | ||
| 134 | const int y = ycenter + ((radius_mult * off_y + (1<<23)) >> 24); | ||
| 135 | const char isvalid = x >= 0 && x <= w - 1 && y >= 0 && y <= h - 1; | ||
| 136 | 109 | ||
| 137 | if (isvalid) { | 110 | NEAREST(uint8_t, 8) |
| 138 | const int nx = FFMIN(x + 1, w - 1); | 111 | NEAREST(uint16_t, 16) |
| 139 | const int ny = FFMIN(y + 1, h - 1); | 112 | |
| 140 | const int64_t du = off_x >= 0 ? (radius_mult * off_x + add) & max : max - ((radius_mult * -off_x + add) & max); | 113 | #define BILINEAR(type, name) \ |
| 141 | const int64_t dv = off_y >= 0 ? (radius_mult * off_y + add) & max : max - ((radius_mult * -off_y + add) & max); | 114 | static int filter##name##_slice_bilinear(AVFilterContext *ctx, void *arg, \ |
| 142 | const int64_t p0 = indata[ y * inlinesize + x]; | 115 | int job, int nb_jobs) \ |
| 143 | const int64_t p1 = indata[ y * inlinesize + nx]; | 116 | { \ |
| 144 | const int64_t p2 = indata[ny * inlinesize + x]; | 117 | ThreadData *td = arg; \ |
| 145 | const int64_t p3 = indata[ny * inlinesize + nx]; | 118 | AVFrame *in = td->in; \ |
| 146 | int64_t sum = 0; | 119 | AVFrame *out = td->out; \ |
| 147 | 120 | \ | |
| 148 | sum += (max - du) * (max - dv) * p0; | 121 | const int depth = td->depth; \ |
| 149 | sum += ( du) * (max - dv) * p1; | 122 | const uint64_t max = (1 << 24) - 1; \ |
| 150 | sum += (max - du) * ( dv) * p2; | 123 | const uint64_t add = (1 << 23); \ |
| 151 | sum += ( du) * ( dv) * p3; | 124 | const int w = td->w, h = td->h; \ |
| 152 | 125 | const int xcenter = td->xcenter; \ | |
| 153 | out[j] = av_clip_uint8((sum + (1LL << 47)) >> 48); | 126 | const int ycenter = td->ycenter; \ |
| 154 | } else { | 127 | const int start = (h * job ) / nb_jobs; \ |
| 155 | out[j] = 0; | 128 | const int end = (h * (job+1)) / nb_jobs; \ |
| 156 | } | 129 | const int plane = td->plane; \ |
| 157 | } | 130 | const int inlinesize = in->linesize[plane] / sizeof(type); \ |
| 158 | } | 131 | const int outlinesize = out->linesize[plane] / sizeof(type); \ |
| 159 | 132 | const type *indata = (const type *)in->data[plane]; \ | |
| 160 | return 0; | 133 | type *outrow = (type *)out->data[plane] + start * outlinesize; \ |
| 134 | \ | ||
| 135 | for (int i = start; i < end; i++, outrow += outlinesize) { \ | ||
| 136 | const int off_y = i - ycenter; \ | ||
| 137 | type *out = outrow; \ | ||
| 138 | \ | ||
| 139 | for (int j = 0; j < w; j++) { \ | ||
| 140 | const int off_x = j - xcenter; \ | ||
| 141 | const int64_t radius_mult = td->correction[j + i*w]; \ | ||
| 142 | const int x = xcenter + ((radius_mult * off_x + (1<<23)) >> 24); \ | ||
| 143 | const int y = ycenter + ((radius_mult * off_y + (1<<23)) >> 24); \ | ||
| 144 | const char isvalid = x >= 0 && x <= w - 1 && y >= 0 && y <= h - 1; \ | ||
| 145 | \ | ||
| 146 | if (isvalid) { \ | ||
| 147 | const int nx = FFMIN(x + 1, w - 1); \ | ||
| 148 | const int ny = FFMIN(y + 1, h - 1); \ | ||
| 149 | const uint64_t du = off_x >= 0 ? (radius_mult * off_x + add) & max : max - ((radius_mult * -off_x + add) & max); \ | ||
| 150 | const uint64_t dv = off_y >= 0 ? (radius_mult * off_y + add) & max : max - ((radius_mult * -off_y + add) & max); \ | ||
| 151 | const uint64_t p0 = indata[ y * inlinesize + x]; \ | ||
| 152 | const uint64_t p1 = indata[ y * inlinesize + nx]; \ | ||
| 153 | const uint64_t p2 = indata[ny * inlinesize + x]; \ | ||
| 154 | const uint64_t p3 = indata[ny * inlinesize + nx]; \ | ||
| 155 | uint64_t sum = 0; \ | ||
| 156 | \ | ||
| 157 | sum += (max - du) * (max - dv) * p0; \ | ||
| 158 | sum += ( du) * (max - dv) * p1; \ | ||
| 159 | sum += (max - du) * ( dv) * p2; \ | ||
| 160 | sum += ( du) * ( dv) * p3; \ | ||
| 161 | \ | ||
| 162 | out[j] = av_clip_uintp2_c((sum + (1ULL << 47)) >> 48, depth); \ | ||
| 163 | } else { \ | ||
| 164 | out[j] = 0; \ | ||
| 165 | } \ | ||
| 166 | } \ | ||
| 167 | } \ | ||
| 168 | \ | ||
| 169 | return 0; \ | ||
| 161 | } | 170 | } |
| 162 | 171 | ||
| 172 | BILINEAR(uint8_t, 8) | ||
| 173 | BILINEAR(uint16_t, 16) | ||
| 174 | |||
| 163 | static int query_formats(AVFilterContext *ctx) | 175 | static int query_formats(AVFilterContext *ctx) |
| 164 | { | 176 | { |
| 165 | static const enum AVPixelFormat pix_fmts[] = { | 177 | static const enum AVPixelFormat pix_fmts[] = { |
| 166 | AV_PIX_FMT_GRAY8, | 178 | AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, |
| 179 | AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, | ||
| 180 | AV_PIX_FMT_GRAY16, | ||
| 167 | AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P, | 181 | AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P, |
| 168 | AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, | 182 | AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, |
| 169 | AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P, | 183 | AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P, |
| 170 | AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, | 184 | AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, |
| 171 | AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P, | 185 | AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P, |
| 172 | AV_PIX_FMT_YUVJ411P, | 186 | AV_PIX_FMT_YUVJ411P, |
| 173 | AV_PIX_FMT_GBRP, | 187 | AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9, |
| 188 | AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10, | ||
| 189 | AV_PIX_FMT_YUV440P10, | ||
| 190 | AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12, | ||
| 191 | AV_PIX_FMT_YUV440P12, | ||
| 192 | AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV420P14, | ||
| 193 | AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16, | ||
| 194 | AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10, | ||
| 195 | AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16, | ||
| 174 | AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P, | 196 | AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P, |
| 175 | AV_PIX_FMT_GBRAP, | 197 | AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12, AV_PIX_FMT_YUVA444P16, |
| 198 | AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA422P16, | ||
| 199 | AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16, | ||
| 200 | AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16, | ||
| 176 | AV_PIX_FMT_NONE | 201 | AV_PIX_FMT_NONE |
| 177 | }; | 202 | }; |
| 178 | AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts); | 203 | AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts); |
| @@ -197,14 +222,16 @@ static int config_props(AVFilterLink *outlink) · 18 unmodified lines | |||
| 197 | LenscorrectionCtx *rect = ctx->priv; | 222 | LenscorrectionCtx *rect = ctx->priv; |
| 198 | AVFilterLink *inlink = ctx->inputs[0]; | 223 | AVFilterLink *inlink = ctx->inputs[0]; |
| 199 | const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(inlink->format); | 224 | const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(inlink->format); |
| 225 | |||
| 226 | rect->depth = pixdesc->comp[0].depth; | ||
| 200 | rect->hsub = pixdesc->log2_chroma_w; | 227 | rect->hsub = pixdesc->log2_chroma_w; |
| 201 | rect->vsub = pixdesc->log2_chroma_h; | 228 | rect->vsub = pixdesc->log2_chroma_h; |
| 202 | outlink->w = rect->width = inlink->w; | 229 | outlink->w = rect->width = inlink->w; |
| 203 | outlink->h = rect->height = inlink->h; | 230 | outlink->h = rect->height = inlink->h; |
| 204 | rect->nb_planes = av_pix_fmt_count_planes(inlink->format); | 231 | rect->nb_planes = av_pix_fmt_count_planes(inlink->format); |
| 205 | rect->filter_slice = filter_slice; | 232 | rect->filter_slice = rect->depth <= 8 ? filter8_slice : filter16_slice; |
| 206 | if (rect->interpolation) | 233 | if (rect->interpolation) |
| 207 | rect->filter_slice = filter_slice_bilinear; | 234 | rect->filter_slice = rect->depth <= 8 ? filter8_slice_bilinear : filter16_slice_bilinear; |
| 208 | return 0; | 235 | return 0; |
| 209 | } | 236 | } |
| 210 | 237 | ||
| @@ -239,7 +266,9 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) · 28 unmodified lines | |||
| 239 | .h = h, | 266 | .h = h, |
| 240 | .xcenter = xcenter, | 267 | .xcenter = xcenter, |
| 241 | .ycenter = ycenter, | 268 | .ycenter = ycenter, |
| 242 | .plane = plane}; | 269 | .plane = plane, |
| 270 | .depth = rect->depth, | ||
| 271 | }; | ||
| 243 | 272 | ||
| 244 | if (!rect->correction[plane]) { | 273 | if (!rect->correction[plane]) { |
| 245 | int i,j; | 274 | int i,j; |