diff options
Diffstat (limited to 'libavfilter/vf_transpose.c')
-rw-r--r-- | libavfilter/vf_transpose.c | 373 |
1 files changed, 270 insertions, 103 deletions
diff --git a/libavfilter/vf_transpose.c b/libavfilter/vf_transpose.c index 07602b9086..982fb0c8ca 100644 --- a/libavfilter/vf_transpose.c +++ b/libavfilter/vf_transpose.c @@ -2,20 +2,20 @@ * Copyright (c) 2010 Stefano Sabatini * Copyright (c) 2008 Vitor Sessak * - * This file is part of Libav. + * This file is part of FFmpeg. * - * Libav is free software; you can redistribute it and/or + * FFmpeg is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * - * Libav is distributed in the hope that it will be useful, + * FFmpeg is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with Libav; if not, write to the Free Software + * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ @@ -38,6 +38,12 @@ #include "internal.h" #include "video.h" +typedef enum { + TRANSPOSE_PT_TYPE_NONE, + TRANSPOSE_PT_TYPE_LANDSCAPE, + TRANSPOSE_PT_TYPE_PORTRAIT, +} PassthroughType; + enum TransposeDir { TRANSPOSE_CCLOCK_FLIP, TRANSPOSE_CLOCK, @@ -50,51 +56,167 @@ typedef struct TransContext { int hsub, vsub; int pixsteps[4]; - enum TransposeDir dir; + int passthrough; ///< PassthroughType, landscape passthrough mode enabled + int dir; ///< TransposeDir + + void (*transpose_8x8)(uint8_t *src, ptrdiff_t src_linesize, + uint8_t *dst, ptrdiff_t dst_linesize); + void (*transpose_block)(uint8_t *src, ptrdiff_t src_linesize, + uint8_t *dst, ptrdiff_t dst_linesize, + int w, int h); } TransContext; static int query_formats(AVFilterContext *ctx) { - enum AVPixelFormat pix_fmts[] = { - AV_PIX_FMT_ARGB, AV_PIX_FMT_RGBA, - AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA, - AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24, - AV_PIX_FMT_RGB565BE, AV_PIX_FMT_RGB565LE, - AV_PIX_FMT_RGB555BE, AV_PIX_FMT_RGB555LE, - AV_PIX_FMT_BGR565BE, AV_PIX_FMT_BGR565LE, - AV_PIX_FMT_BGR555BE, AV_PIX_FMT_BGR555LE, - AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_GRAY16LE, - AV_PIX_FMT_YUV420P16LE, AV_PIX_FMT_YUV420P16BE, - AV_PIX_FMT_YUV422P16LE, AV_PIX_FMT_YUV422P16BE, - AV_PIX_FMT_YUV444P16LE, AV_PIX_FMT_YUV444P16BE, - AV_PIX_FMT_NV12, AV_PIX_FMT_NV21, - AV_PIX_FMT_RGB8, AV_PIX_FMT_BGR8, - AV_PIX_FMT_RGB4_BYTE, AV_PIX_FMT_BGR4_BYTE, - AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, - AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVJ420P, - AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P, - AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P, - AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUVJ440P, - AV_PIX_FMT_YUVA420P, AV_PIX_FMT_GRAY8, - AV_PIX_FMT_NONE - }; - - ff_set_common_formats(ctx, ff_make_format_list(pix_fmts)); - return 0; + AVFilterFormats *pix_fmts = NULL; + int fmt, ret; + + for (fmt = 0; av_pix_fmt_desc_get(fmt); fmt++) { + const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt); + if (!(desc->flags & AV_PIX_FMT_FLAG_PAL || + desc->flags & AV_PIX_FMT_FLAG_HWACCEL || + desc->flags & AV_PIX_FMT_FLAG_BITSTREAM || + desc->log2_chroma_w != desc->log2_chroma_h) && + (ret = ff_add_format(&pix_fmts, fmt)) < 0) + return ret; + } + + + return ff_set_common_formats(ctx, pix_fmts); +} + +static inline void transpose_block_8_c(uint8_t *src, ptrdiff_t src_linesize, + uint8_t *dst, ptrdiff_t dst_linesize, + int w, int h) +{ + int x, y; + for (y = 0; y < h; y++, dst += dst_linesize, src++) + for (x = 0; x < w; x++) + dst[x] = src[x*src_linesize]; +} + +static void transpose_8x8_8_c(uint8_t *src, ptrdiff_t src_linesize, + uint8_t *dst, ptrdiff_t dst_linesize) +{ + transpose_block_8_c(src, src_linesize, dst, dst_linesize, 8, 8); +} + +static inline void transpose_block_16_c(uint8_t *src, ptrdiff_t src_linesize, + uint8_t *dst, ptrdiff_t dst_linesize, + int w, int h) +{ + int x, y; + for (y = 0; y < h; y++, dst += dst_linesize, src += 2) + for (x = 0; x < w; x++) + *((uint16_t *)(dst + 2*x)) = *((uint16_t *)(src + x*src_linesize)); +} + +static void transpose_8x8_16_c(uint8_t *src, ptrdiff_t src_linesize, + uint8_t *dst, ptrdiff_t dst_linesize) +{ + transpose_block_16_c(src, src_linesize, dst, dst_linesize, 8, 8); +} + +static inline void transpose_block_24_c(uint8_t *src, ptrdiff_t src_linesize, + uint8_t *dst, ptrdiff_t dst_linesize, + int w, int h) +{ + int x, y; + for (y = 0; y < h; y++, dst += dst_linesize) { + for (x = 0; x < w; x++) { + int32_t v = AV_RB24(src + x*src_linesize + y*3); + AV_WB24(dst + 3*x, v); + } + } +} + +static void transpose_8x8_24_c(uint8_t *src, ptrdiff_t src_linesize, + uint8_t *dst, ptrdiff_t dst_linesize) +{ + transpose_block_24_c(src, src_linesize, dst, dst_linesize, 8, 8); +} + +static inline void transpose_block_32_c(uint8_t *src, ptrdiff_t src_linesize, + uint8_t *dst, ptrdiff_t dst_linesize, + int w, int h) +{ + int x, y; + for (y = 0; y < h; y++, dst += dst_linesize, src += 4) { + for (x = 0; x < w; x++) + *((uint32_t *)(dst + 4*x)) = *((uint32_t *)(src + x*src_linesize)); + } +} + +static void transpose_8x8_32_c(uint8_t *src, ptrdiff_t src_linesize, + uint8_t *dst, ptrdiff_t dst_linesize) +{ + transpose_block_32_c(src, src_linesize, dst, dst_linesize, 8, 8); +} + +static inline void transpose_block_48_c(uint8_t *src, ptrdiff_t src_linesize, + uint8_t *dst, ptrdiff_t dst_linesize, + int w, int h) +{ + int x, y; + for (y = 0; y < h; y++, dst += dst_linesize, src += 6) { + for (x = 0; x < w; x++) { + int64_t v = AV_RB48(src + x*src_linesize); + AV_WB48(dst + 6*x, v); + } + } +} + +static void transpose_8x8_48_c(uint8_t *src, ptrdiff_t src_linesize, + uint8_t *dst, ptrdiff_t dst_linesize) +{ + transpose_block_48_c(src, src_linesize, dst, dst_linesize, 8, 8); +} + +static inline void transpose_block_64_c(uint8_t *src, ptrdiff_t src_linesize, + uint8_t *dst, ptrdiff_t dst_linesize, + int w, int h) +{ + int x, y; + for (y = 0; y < h; y++, dst += dst_linesize, src += 8) + for (x = 0; x < w; x++) + *((uint64_t *)(dst + 8*x)) = *((uint64_t *)(src + x*src_linesize)); +} + +static void transpose_8x8_64_c(uint8_t *src, ptrdiff_t src_linesize, + uint8_t *dst, ptrdiff_t dst_linesize) +{ + transpose_block_64_c(src, src_linesize, dst, dst_linesize, 8, 8); } static int config_props_output(AVFilterLink *outlink) { AVFilterContext *ctx = outlink->src; - TransContext *trans = ctx->priv; + TransContext *s = ctx->priv; AVFilterLink *inlink = ctx->inputs[0]; const AVPixFmtDescriptor *desc_out = av_pix_fmt_desc_get(outlink->format); const AVPixFmtDescriptor *desc_in = av_pix_fmt_desc_get(inlink->format); - trans->hsub = desc_in->log2_chroma_w; - trans->vsub = desc_in->log2_chroma_h; + if (s->dir&4) { + av_log(ctx, AV_LOG_WARNING, + "dir values greater than 3 are deprecated, use the passthrough option instead\n"); + s->dir &= 3; + s->passthrough = TRANSPOSE_PT_TYPE_LANDSCAPE; + } - av_image_fill_max_pixsteps(trans->pixsteps, NULL, desc_out); + if ((inlink->w >= inlink->h && s->passthrough == TRANSPOSE_PT_TYPE_LANDSCAPE) || + (inlink->w <= inlink->h && s->passthrough == TRANSPOSE_PT_TYPE_PORTRAIT)) { + av_log(ctx, AV_LOG_VERBOSE, + "w:%d h:%d -> w:%d h:%d (passthrough mode)\n", + inlink->w, inlink->h, inlink->w, inlink->h); + return 0; + } else { + s->passthrough = TRANSPOSE_PT_TYPE_NONE; + } + + s->hsub = desc_in->log2_chroma_w; + s->vsub = desc_in->log2_chroma_h; + + av_image_fill_max_pixsteps(s->pixsteps, NULL, desc_out); outlink->w = inlink->h; outlink->h = inlink->w; @@ -105,116 +227,160 @@ static int config_props_output(AVFilterLink *outlink) else outlink->sample_aspect_ratio = inlink->sample_aspect_ratio; + switch (s->pixsteps[0]) { + case 1: s->transpose_block = transpose_block_8_c; + s->transpose_8x8 = transpose_8x8_8_c; break; + case 2: s->transpose_block = transpose_block_16_c; + s->transpose_8x8 = transpose_8x8_16_c; break; + case 3: s->transpose_block = transpose_block_24_c; + s->transpose_8x8 = transpose_8x8_24_c; break; + case 4: s->transpose_block = transpose_block_32_c; + s->transpose_8x8 = transpose_8x8_32_c; break; + case 6: s->transpose_block = transpose_block_48_c; + s->transpose_8x8 = transpose_8x8_48_c; break; + case 8: s->transpose_block = transpose_block_64_c; + s->transpose_8x8 = transpose_8x8_64_c; break; + } + av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d dir:%d -> w:%d h:%d rotation:%s vflip:%d\n", - inlink->w, inlink->h, trans->dir, outlink->w, outlink->h, - trans->dir == 1 || trans->dir == 3 ? "clockwise" : "counterclockwise", - trans->dir == 0 || trans->dir == 3); + inlink->w, inlink->h, s->dir, outlink->w, outlink->h, + s->dir == 1 || s->dir == 3 ? "clockwise" : "counterclockwise", + s->dir == 0 || s->dir == 3); return 0; } -static int filter_frame(AVFilterLink *inlink, AVFrame *in) +static AVFrame *get_video_buffer(AVFilterLink *inlink, int w, int h) { - AVFilterLink *outlink = inlink->dst->outputs[0]; - TransContext *trans = inlink->dst->priv; - AVFrame *out; - int plane; + TransContext *s = inlink->dst->priv; - out = ff_get_video_buffer(outlink, outlink->w, outlink->h); - if (!out) { - av_frame_free(&in); - return AVERROR(ENOMEM); - } + return s->passthrough ? + ff_null_get_video_buffer (inlink, w, h) : + ff_default_get_video_buffer(inlink, w, h); +} - out->pts = in->pts; +typedef struct ThreadData { + AVFrame *in, *out; +} ThreadData; - if (in->sample_aspect_ratio.num == 0) { - out->sample_aspect_ratio = in->sample_aspect_ratio; - } else { - out->sample_aspect_ratio.num = in->sample_aspect_ratio.den; - out->sample_aspect_ratio.den = in->sample_aspect_ratio.num; - } +static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, + int nb_jobs) +{ + TransContext *s = ctx->priv; + ThreadData *td = arg; + AVFrame *out = td->out; + AVFrame *in = td->in; + int plane; for (plane = 0; out->data[plane]; plane++) { - int hsub = plane == 1 || plane == 2 ? trans->hsub : 0; - int vsub = plane == 1 || plane == 2 ? trans->vsub : 0; - int pixstep = trans->pixsteps[plane]; - int inh = in->height >> vsub; - int outw = out->width >> hsub; - int outh = out->height >> vsub; + int hsub = plane == 1 || plane == 2 ? s->hsub : 0; + int vsub = plane == 1 || plane == 2 ? s->vsub : 0; + int pixstep = s->pixsteps[plane]; + int inh = AV_CEIL_RSHIFT(in->height, vsub); + int outw = AV_CEIL_RSHIFT(out->width, hsub); + int outh = AV_CEIL_RSHIFT(out->height, vsub); + int start = (outh * jobnr ) / nb_jobs; + int end = (outh * (jobnr+1)) / nb_jobs; uint8_t *dst, *src; int dstlinesize, srclinesize; int x, y; - dst = out->data[plane]; dstlinesize = out->linesize[plane]; + dst = out->data[plane] + start * dstlinesize; src = in->data[plane]; srclinesize = in->linesize[plane]; - if (trans->dir & 1) { + if (s->dir & 1) { src += in->linesize[plane] * (inh - 1); srclinesize *= -1; } - if (trans->dir & 2) { - dst += out->linesize[plane] * (outh - 1); + if (s->dir & 2) { + dst = out->data[plane] + dstlinesize * (outh - start - 1); dstlinesize *= -1; } - for (y = 0; y < outh; y++) { - switch (pixstep) { - case 1: - for (x = 0; x < outw; x++) - dst[x] = src[x * srclinesize + y]; - break; - case 2: - for (x = 0; x < outw; x++) - *((uint16_t *)(dst + 2 * x)) = - *((uint16_t *)(src + x * srclinesize + y * 2)); - break; - case 3: - for (x = 0; x < outw; x++) { - int32_t v = AV_RB24(src + x * srclinesize + y * 3); - AV_WB24(dst + 3 * x, v); - } - break; - case 4: - for (x = 0; x < outw; x++) - *((uint32_t *)(dst + 4 * x)) = - *((uint32_t *)(src + x * srclinesize + y * 4)); - break; + for (y = start; y < end - 7; y += 8) { + for (x = 0; x < outw - 7; x += 8) { + s->transpose_8x8(src + x * srclinesize + y * pixstep, + srclinesize, + dst + (y - start) * dstlinesize + x * pixstep, + dstlinesize); } - dst += dstlinesize; + if (outw - x > 0 && end - y > 0) + s->transpose_block(src + x * srclinesize + y * pixstep, + srclinesize, + dst + (y - start) * dstlinesize + x * pixstep, + dstlinesize, outw - x, end - y); } + + if (end - y > 0) + s->transpose_block(src + 0 * srclinesize + y * pixstep, + srclinesize, + dst + (y - start) * dstlinesize + 0 * pixstep, + dstlinesize, outw, end - y); } + return 0; +} + +static int filter_frame(AVFilterLink *inlink, AVFrame *in) +{ + AVFilterContext *ctx = inlink->dst; + TransContext *s = ctx->priv; + AVFilterLink *outlink = ctx->outputs[0]; + ThreadData td; + AVFrame *out; + + if (s->passthrough) + return ff_filter_frame(outlink, in); + + out = ff_get_video_buffer(outlink, outlink->w, outlink->h); + if (!out) { + av_frame_free(&in); + return AVERROR(ENOMEM); + } + av_frame_copy_props(out, in); + + if (in->sample_aspect_ratio.num == 0) { + out->sample_aspect_ratio = in->sample_aspect_ratio; + } else { + out->sample_aspect_ratio.num = in->sample_aspect_ratio.den; + out->sample_aspect_ratio.den = in->sample_aspect_ratio.num; + } + + td.in = in, td.out = out; + ctx->internal->execute(ctx, filter_slice, &td, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx))); av_frame_free(&in); return ff_filter_frame(outlink, out); } #define OFFSET(x) offsetof(TransContext, x) -#define FLAGS AV_OPT_FLAG_VIDEO_PARAM -static const AVOption options[] = { - { "dir", "Transpose direction", OFFSET(dir), AV_OPT_TYPE_INT, { .i64 = TRANSPOSE_CCLOCK_FLIP }, - TRANSPOSE_CCLOCK_FLIP, TRANSPOSE_CLOCK_FLIP, FLAGS, "dir" }, - { "cclock_flip", "counter-clockwise with vertical flip", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CCLOCK_FLIP }, .unit = "dir" }, - { "clock", "clockwise", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CLOCK }, .unit = "dir" }, - { "cclock", "counter-clockwise", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CCLOCK }, .unit = "dir" }, - { "clock_flip", "clockwise with vertical flip", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CLOCK_FLIP }, .unit = "dir" }, - { NULL }, -}; +#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM + +static const AVOption transpose_options[] = { + { "dir", "set transpose direction", OFFSET(dir), AV_OPT_TYPE_INT, { .i64 = TRANSPOSE_CCLOCK_FLIP }, 0, 7, FLAGS, "dir" }, + { "cclock_flip", "rotate counter-clockwise with vertical flip", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CCLOCK_FLIP }, .flags=FLAGS, .unit = "dir" }, + { "clock", "rotate clockwise", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CLOCK }, .flags=FLAGS, .unit = "dir" }, + { "cclock", "rotate counter-clockwise", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CCLOCK }, .flags=FLAGS, .unit = "dir" }, + { "clock_flip", "rotate clockwise with vertical flip", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CLOCK_FLIP }, .flags=FLAGS, .unit = "dir" }, -static const AVClass transpose_class = { - .class_name = "transpose", - .item_name = av_default_item_name, - .option = options, - .version = LIBAVUTIL_VERSION_INT, + { "passthrough", "do not apply transposition if the input matches the specified geometry", + OFFSET(passthrough), AV_OPT_TYPE_INT, {.i64=TRANSPOSE_PT_TYPE_NONE}, 0, INT_MAX, FLAGS, "passthrough" }, + { "none", "always apply transposition", 0, AV_OPT_TYPE_CONST, {.i64=TRANSPOSE_PT_TYPE_NONE}, INT_MIN, INT_MAX, FLAGS, "passthrough" }, + { "portrait", "preserve portrait geometry", 0, AV_OPT_TYPE_CONST, {.i64=TRANSPOSE_PT_TYPE_PORTRAIT}, INT_MIN, INT_MAX, FLAGS, "passthrough" }, + { "landscape", "preserve landscape geometry", 0, AV_OPT_TYPE_CONST, {.i64=TRANSPOSE_PT_TYPE_LANDSCAPE}, INT_MIN, INT_MAX, FLAGS, "passthrough" }, + + { NULL } }; +AVFILTER_DEFINE_CLASS(transpose); + static const AVFilterPad avfilter_vf_transpose_inputs[] = { { .name = "default", .type = AVMEDIA_TYPE_VIDEO, + .get_video_buffer = get_video_buffer, .filter_frame = filter_frame, }, { NULL } @@ -237,4 +403,5 @@ AVFilter ff_vf_transpose = { .query_formats = query_formats, .inputs = avfilter_vf_transpose_inputs, .outputs = avfilter_vf_transpose_outputs, + .flags = AVFILTER_FLAG_SLICE_THREADS, }; |