]> git.sesse.net Git - ffmpeg/blobdiff - libavfilter/vf_fieldorder.c
vaapi_h265: cu_qp_delta should not be used in constant-QP mode
[ffmpeg] / libavfilter / vf_fieldorder.c
index 754cccd88578fdcc889239c8fb42c9f3b8a88588..dd4f8cc69416fed9a98ca6ea4ca79538f6caee12 100644 (file)
  * video field order filter, heavily influenced by vf_pad.c
  */
 
-/* #define DEBUG */
+#include <stdio.h>
+#include <string.h>
 
 #include "libavutil/imgutils.h"
+#include "libavutil/internal.h"
+#include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "avfilter.h"
 #include "formats.h"
 #include "internal.h"
 #include "video.h"
 
-typedef struct
-{
-    unsigned int dst_tff;      ///< output bff/tff
+typedef struct FieldOrderContext {
+    const AVClass *class;
+    int dst_tff;               ///< output bff/tff
     int          line_size[4]; ///< bytes of pixel data per line for each plane
 } FieldOrderContext;
 
-static av_cold int init(AVFilterContext *ctx, const char *args)
-{
-    FieldOrderContext *fieldorder = ctx->priv;
-
-    const char *tff = "tff";
-    const char *bff = "bff";
-
-    if (!args) {
-        fieldorder->dst_tff = 1;
-    } else if (sscanf(args, "%u", &fieldorder->dst_tff) == 1) {
-        fieldorder->dst_tff = !!fieldorder->dst_tff;
-    } else if (!strcmp(tff, args)) {
-        fieldorder->dst_tff = 1;
-    } else if (!strcmp(bff, args)) {
-        fieldorder->dst_tff = 0;
-    } else {
-        av_log(ctx, AV_LOG_ERROR, "Invalid argument '%s'.\n", args);
-        return AVERROR(EINVAL);
-    }
-
-    av_log(ctx, AV_LOG_VERBOSE, "output field order: %s\n",
-            fieldorder->dst_tff ? tff : bff);
-
-    return 0;
-}
-
 static int query_formats(AVFilterContext *ctx)
 {
     AVFilterFormats  *formats;
-    enum PixelFormat pix_fmt;
+    enum AVPixelFormat pix_fmt;
     int              ret;
 
     /** accept any input pixel format that is not hardware accelerated, not
      *  a bitstream format, and does not have vertically sub-sampled chroma */
     if (ctx->inputs[0]) {
+        const AVPixFmtDescriptor *desc = NULL;
         formats = NULL;
-        for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++)
-            if (!(  av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_HWACCEL
-                 || av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_BITSTREAM)
-                && av_pix_fmt_descriptors[pix_fmt].nb_components
-                && !av_pix_fmt_descriptors[pix_fmt].log2_chroma_h
-                && (ret = ff_add_format(&formats, pix_fmt)) < 0) {
+        while ((desc = av_pix_fmt_desc_next(desc))) {
+            pix_fmt = av_pix_fmt_desc_get_id(desc);
+            if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL ||
+                  desc->flags & AV_PIX_FMT_FLAG_BITSTREAM) &&
+                desc->nb_components && !desc->log2_chroma_h &&
+                (ret = ff_add_format(&formats, pix_fmt)) < 0) {
                 ff_formats_unref(&formats);
                 return ret;
             }
+        }
         ff_formats_ref(formats, &ctx->inputs[0]->out_formats);
         ff_formats_ref(formats, &ctx->outputs[0]->in_formats);
     }
@@ -92,146 +71,122 @@ static int query_formats(AVFilterContext *ctx)
 
 static int config_input(AVFilterLink *inlink)
 {
-    AVFilterContext   *ctx        = inlink->dst;
-    FieldOrderContext *fieldorder = ctx->priv;
+    AVFilterContext   *ctx = inlink->dst;
+    FieldOrderContext *s   = ctx->priv;
     int               plane;
 
     /** full an array with the number of bytes that the video
      *  data occupies per line for each plane of the input video */
     for (plane = 0; plane < 4; plane++) {
-        fieldorder->line_size[plane] = av_image_get_linesize(
-                inlink->format,
-                inlink->w,
-                plane);
+        s->line_size[plane] = av_image_get_linesize(inlink->format, inlink->w,
+                                                    plane);
     }
 
     return 0;
 }
 
-static AVFilterBufferRef *get_video_buffer(AVFilterLink *inlink, int perms, int w, int h)
-{
-    AVFilterContext   *ctx        = inlink->dst;
-    AVFilterLink      *outlink    = ctx->outputs[0];
-
-    return ff_get_video_buffer(outlink, perms, w, h);
-}
-
-static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
-{
-    AVFilterContext   *ctx        = inlink->dst;
-    AVFilterLink      *outlink    = ctx->outputs[0];
-
-    AVFilterBufferRef *outpicref, *for_next_filter;
-
-    outpicref = avfilter_ref_buffer(inpicref, ~0);
-    outlink->out_buf = outpicref;
-
-    for_next_filter = avfilter_ref_buffer(outpicref, ~0);
-    ff_start_frame(outlink, for_next_filter);
-}
-
-static void draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
+static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
 {
-    AVFilterContext   *ctx        = inlink->dst;
-    FieldOrderContext *fieldorder = ctx->priv;
-    AVFilterLink      *outlink    = ctx->outputs[0];
-
-    AVFilterBufferRef *inpicref   = inlink->cur_buf;
-
-    /** can only currently do slices if this filter is doing nothing
-     *  because this filter is moving picture content, the output
-     *  slice will contain different video lines than the input slice
-     *  and that complexity will be added later */
-    if (  !inpicref->video->interlaced
-        || inpicref->video->top_field_first == fieldorder->dst_tff) {
-        ff_draw_slice(outlink, y, h, slice_dir);
+    AVFilterContext   *ctx     = inlink->dst;
+    FieldOrderContext *s       = ctx->priv;
+    AVFilterLink      *outlink = ctx->outputs[0];
+    int h, plane, line_step, line_size, line;
+    uint8_t *data;
+
+    if (!frame->interlaced_frame ||
+        frame->top_field_first == s->dst_tff) {
+        av_log(ctx, AV_LOG_VERBOSE,
+               "Skipping %s.\n",
+               frame->interlaced_frame ?
+               "frame with same field order" : "progressive frame");
+        return ff_filter_frame(outlink, frame);
     }
-}
 
-static void end_frame(AVFilterLink *inlink)
-{
-    AVFilterContext   *ctx        = inlink->dst;
-    FieldOrderContext *fieldorder = ctx->priv;
-    AVFilterLink      *outlink    = ctx->outputs[0];
-
-    AVFilterBufferRef *inpicref   = inlink->cur_buf;
-    AVFilterBufferRef *outpicref  = outlink->out_buf;
-
-    int               h, plane, line_step, line_size, line;
-    uint8_t           *cpy_src, *cpy_dst;
-
-    if (    inpicref->video->interlaced
-         && inpicref->video->top_field_first != fieldorder->dst_tff) {
-        av_dlog(ctx,
-                "picture will move %s one line\n",
-                fieldorder->dst_tff ? "up" : "down");
-        h = inpicref->video->h;
-        for (plane = 0; plane < 4 && inpicref->data[plane]; plane++) {
-            line_step = inpicref->linesize[plane];
-            line_size = fieldorder->line_size[plane];
-            cpy_src = inpicref->data[plane];
-            cpy_dst = outpicref->data[plane];
-            if (fieldorder->dst_tff) {
-                /** Move every line up one line, working from
-                 *  the top to the bottom of the frame.
-                 *  The original top line is lost.
-                 *  The new last line is created as a copy of the
-                 *  penultimate line from that field. */
-                for (line = 0; line < h; line++) {
-                    if (1 + line < outpicref->video->h) {
-                        memcpy(cpy_dst, cpy_src + line_step, line_size);
-                    } else {
-                        memcpy(cpy_dst, cpy_src - line_step - line_step, line_size);
-                    }
-                    cpy_src += line_step;
-                    cpy_dst += line_step;
+    av_log(ctx, AV_LOG_TRACE,
+            "picture will move %s one line\n",
+            s->dst_tff ? "up" : "down");
+    h = frame->height;
+    for (plane = 0; plane < 4 && frame->data[plane]; plane++) {
+        line_step = frame->linesize[plane];
+        line_size = s->line_size[plane];
+        data = frame->data[plane];
+        if (s->dst_tff) {
+            /** Move every line up one line, working from
+             *  the top to the bottom of the frame.
+             *  The original top line is lost.
+             *  The new last line is created as a copy of the
+             *  penultimate line from that field. */
+            for (line = 0; line < h; line++) {
+                if (1 + line < frame->height) {
+                    memcpy(data, data + line_step, line_size);
+                } else {
+                    memcpy(data, data - line_step - line_step, line_size);
                 }
-            } else {
-                /** Move every line down one line, working from
-                 *  the bottom to the top of the frame.
-                 *  The original bottom line is lost.
-                 *  The new first line is created as a copy of the
-                 *  second line from that field. */
-                cpy_src += (h - 1) * line_step;
-                cpy_dst += (h - 1) * line_step;
-                for (line = h - 1; line >= 0 ; line--) {
-                    if (line > 0) {
-                        memcpy(cpy_dst, cpy_src - line_step, line_size);
-                    } else {
-                        memcpy(cpy_dst, cpy_src + line_step + line_step, line_size);
-                    }
-                    cpy_src -= line_step;
-                    cpy_dst -= line_step;
+                data += line_step;
+            }
+        } else {
+            /** Move every line down one line, working from
+             *  the bottom to the top of the frame.
+             *  The original bottom line is lost.
+             *  The new first line is created as a copy of the
+             *  second line from that field. */
+            data += (h - 1) * line_step;
+            for (line = h - 1; line >= 0 ; line--) {
+                if (line > 0) {
+                    memcpy(data, data - line_step, line_size);
+                } else {
+                    memcpy(data, data + line_step + line_step, line_size);
                 }
+                data -= line_step;
             }
         }
-        outpicref->video->top_field_first = fieldorder->dst_tff;
-        ff_draw_slice(outlink, 0, h, 1);
-    } else {
-        av_dlog(ctx,
-                "not interlaced or field order already correct\n");
     }
+    frame->top_field_first = s->dst_tff;
 
-    ff_end_frame(outlink);
+    return ff_filter_frame(outlink, frame);
 }
 
-AVFilter avfilter_vf_fieldorder = {
+#define OFFSET(x) offsetof(FieldOrderContext, x)
+#define FLAGS AV_OPT_FLAG_VIDEO_PARAM
+static const AVOption options[] = {
+    { "order", "output field order", OFFSET(dst_tff), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, FLAGS, "order" },
+        { "bff", "bottom field first", 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, .unit = "order" },
+        { "tff", "top field first",    0, AV_OPT_TYPE_CONST, { .i64 = 1 }, .unit = "order" },
+    { NULL },
+};
+
+static const AVClass fieldorder_class = {
+    .class_name = "fieldorder",
+    .item_name  = av_default_item_name,
+    .option     = options,
+    .version    = LIBAVUTIL_VERSION_INT,
+};
+
+static const AVFilterPad avfilter_vf_fieldorder_inputs[] = {
+    {
+        .name             = "default",
+        .type             = AVMEDIA_TYPE_VIDEO,
+        .config_props     = config_input,
+        .filter_frame     = filter_frame,
+        .needs_writable   = 1,
+    },
+    { NULL }
+};
+
+static const AVFilterPad avfilter_vf_fieldorder_outputs[] = {
+    {
+        .name = "default",
+        .type = AVMEDIA_TYPE_VIDEO,
+    },
+    { NULL }
+};
+
+AVFilter ff_vf_fieldorder = {
     .name          = "fieldorder",
     .description   = NULL_IF_CONFIG_SMALL("Set the field order."),
-    .init          = init,
     .priv_size     = sizeof(FieldOrderContext),
+    .priv_class    = &fieldorder_class,
     .query_formats = query_formats,
-    .inputs        = (const AVFilterPad[]) {{ .name             = "default",
-                                              .type             = AVMEDIA_TYPE_VIDEO,
-                                              .config_props     = config_input,
-                                              .start_frame      = start_frame,
-                                              .get_video_buffer = get_video_buffer,
-                                              .draw_slice       = draw_slice,
-                                              .end_frame        = end_frame,
-                                              .min_perms        = AV_PERM_READ,
-                                              .rej_perms        = AV_PERM_REUSE2|AV_PERM_PRESERVE,},
-                                            { .name = NULL}},
-    .outputs       = (const AVFilterPad[]) {{ .name             = "default",
-                                              .type             = AVMEDIA_TYPE_VIDEO, },
-                                            { .name = NULL}},
+    .inputs        = avfilter_vf_fieldorder_inputs,
+    .outputs       = avfilter_vf_fieldorder_outputs,
 };