vf_pixdesctest.c 3.86 KB
Newer Older
1
/*
2 3
 * Copyright (c) 2009 Stefano Sabatini
 *
4
 * This file is part of Libav.
5
 *
6
 * Libav is free software; you can redistribute it and/or
7 8 9 10
 * 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.
 *
11
 * Libav is distributed in the hope that it will be useful,
12 13 14 15 16
 * 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
17
 * License along with Libav; if not, write to the Free Software
18 19 20 21 22 23 24 25
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/**
 * @file
 * pixdesc test filter
 */

26
#include "libavutil/common.h"
27 28
#include "libavutil/pixdesc.h"
#include "avfilter.h"
29
#include "internal.h"
30
#include "video.h"
31

32
typedef struct PixdescTestContext {
33 34 35 36 37 38 39 40 41 42 43 44 45 46
    const AVPixFmtDescriptor *pix_desc;
    uint16_t *line;
} PixdescTestContext;

static av_cold void uninit(AVFilterContext *ctx)
{
    PixdescTestContext *priv = ctx->priv;
    av_freep(&priv->line);
}

static int config_props(AVFilterLink *inlink)
{
    PixdescTestContext *priv = inlink->dst->priv;

47
    priv->pix_desc = av_pix_fmt_desc_get(inlink->format);
48

49
    av_freep(&priv->line);
50 51 52 53 54 55
    if (!(priv->line = av_malloc(sizeof(*priv->line) * inlink->w)))
        return AVERROR(ENOMEM);

    return 0;
}

56
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
57 58 59
{
    PixdescTestContext *priv = inlink->dst->priv;
    AVFilterLink *outlink    = inlink->dst->outputs[0];
60
    AVFrame *out;
61
    int i, c, w = inlink->w, h = inlink->h;
62

63
    out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
64
    if (!out) {
65
        av_frame_free(&in);
66
        return AVERROR(ENOMEM);
67
    }
68

69
    av_frame_copy_props(out, in);
70 71 72 73

    for (i = 0; i < 4; i++) {
        int h = outlink->h;
        h = i == 1 || i == 2 ? h>>priv->pix_desc->log2_chroma_h : h;
74 75 76 77
        if (out->data[i]) {
            uint8_t *data = out->data[i] +
                (out->linesize[i] > 0 ? 0 : out->linesize[i] * (h-1));
            memset(data, 0, FFABS(out->linesize[i]) * h);
78 79 80 81
        }
    }

    /* copy palette */
82 83
    if (priv->pix_desc->flags & AV_PIX_FMT_FLAG_PAL ||
        priv->pix_desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL)
84
        memcpy(out->data[1], in->data[1], 256*4);
85 86 87 88 89

    for (c = 0; c < priv->pix_desc->nb_components; c++) {
        int w1 = c == 1 || c == 2 ? w>>priv->pix_desc->log2_chroma_w : w;
        int h1 = c == 1 || c == 2 ? h>>priv->pix_desc->log2_chroma_h : h;

90
        for (i = 0; i < h1; i++) {
91
            av_read_image_line(priv->line,
92 93
                               in->data,
                               in->linesize,
94 95
                               priv->pix_desc,
                               0, i, c, w1, 0);
96

97
            av_write_image_line(priv->line,
98 99
                                out->data,
                                out->linesize,
100 101
                                priv->pix_desc,
                                0, i, c, w1);
102 103 104
        }
    }

105
    av_frame_free(&in);
106
    return ff_filter_frame(outlink, out);
107 108
}

109 110 111 112
static const AVFilterPad avfilter_vf_pixdesctest_inputs[] = {
    {
        .name         = "default",
        .type         = AVMEDIA_TYPE_VIDEO,
113
        .filter_frame = filter_frame,
114 115 116 117 118 119 120 121 122 123 124 125 126
        .config_props = config_props,
    },
    { NULL }
};

static const AVFilterPad avfilter_vf_pixdesctest_outputs[] = {
    {
        .name = "default",
        .type = AVMEDIA_TYPE_VIDEO,
    },
    { NULL }
};

127
AVFilter ff_vf_pixdesctest = {
128
    .name        = "pixdesctest",
129
    .description = NULL_IF_CONFIG_SMALL("Test pixel format definitions."),
130 131 132 133

    .priv_size = sizeof(PixdescTestContext),
    .uninit    = uninit,

134 135 136
    .inputs    = avfilter_vf_pixdesctest_inputs,

    .outputs   = avfilter_vf_pixdesctest_outputs,
137
};