~siretart/ubuntu/utopic/libav/libav10

« back to all changes in this revision

Viewing changes to libavfilter/vf_interlace.c

  • Committer: Package Import Robot
  • Author(s): Reinhard Tartler
  • Date: 2014-05-11 12:28:45 UTC
  • mfrom: (1.1.22) (2.1.38 experimental)
  • Revision ID: package-import@ubuntu.com-20140511122845-gxvpts83i958y0i5
Tags: 6:10.1-1
* New upstream release 10:
   - pcm-dvd: Fix 20bit decoding (bug/592)
   - avi: Improve non-interleaved detection (bug/666)
   - arm: hpeldsp: fix put_pixels8_y2_{,no_rnd_}armv6
   - arm: hpeldsp: prevent overreads in armv6 asm (bug/646)
   - avfilter: Add missing emms_c when needed
   - rtmpproto: Check the buffer sizes when copying app/playpath strings
   - swscale: Fix an undefined behaviour
   - vp9: Read the frame size as unsigned
   - dcadec: Use correct channel count in stereo downmix check
   - dcadec: Do not decode the XCh extension when downmixing to stereo
   - matroska: add the Opus mapping
   - matroskadec: read the CodecDelay element
   - rtmpproto: Make sure to pass on the error code if read_connect failed
   - lavr: allocate the resampling buffer with a positive size
   - mp3enc: Properly write bitrate value in XING header (Closes: #736088)
   - golomb: Fix the implementation of get_se_golomb_long
* Drop debian/libav-tools.maintscript. ffserver is no longer found in
  stable, and this seems to cause other problems today (Closes: #742676)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * This file is part of Libav.
 
3
 *
 
4
 * Libav is free software; you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License as published by
 
6
 * the Free Software Foundation; either version 2 of the License, or
 
7
 * (at your option) any later version.
 
8
 *
 
9
 * Libav is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 * GNU General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU General Public License along
 
15
 * with Libav; if not, write to the Free Software Foundation, Inc.,
 
16
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
17
 */
 
18
 
 
19
/**
 
20
 * @file
 
21
 * progressive to interlaced content filter, inspired by heavy debugging of tinterlace filter
 
22
 */
 
23
 
 
24
#include "libavutil/common.h"
 
25
#include "libavutil/opt.h"
 
26
#include "libavutil/imgutils.h"
 
27
#include "libavutil/avassert.h"
 
28
 
 
29
#include "formats.h"
 
30
#include "avfilter.h"
 
31
#include "internal.h"
 
32
#include "video.h"
 
33
 
 
34
enum ScanMode {
 
35
    MODE_TFF = 0,
 
36
    MODE_BFF = 1,
 
37
};
 
38
 
 
39
enum FieldType {
 
40
    FIELD_UPPER = 0,
 
41
    FIELD_LOWER = 1,
 
42
};
 
43
 
 
44
typedef struct {
 
45
    const AVClass *class;
 
46
    enum ScanMode scan;    // top or bottom field first scanning
 
47
    int lowpass;           // enable or disable low pass filterning
 
48
    AVFrame *cur, *next;   // the two frames from which the new one is obtained
 
49
    int got_output;        // signal an output frame is reday to request_frame()
 
50
} InterlaceContext;
 
51
 
 
52
#define OFFSET(x) offsetof(InterlaceContext, x)
 
53
#define V AV_OPT_FLAG_VIDEO_PARAM
 
54
static const AVOption options[] = {
 
55
    { "scan", "scanning mode", OFFSET(scan),
 
56
        AV_OPT_TYPE_INT,   {.i64 = MODE_TFF }, 0, 1, .flags = V, .unit = "scan" },
 
57
    { "tff", "top field first", 0,
 
58
        AV_OPT_TYPE_CONST, {.i64 = MODE_TFF }, INT_MIN, INT_MAX, .flags = V, .unit = "scan" },
 
59
    { "bff", "bottom field first", 0,
 
60
        AV_OPT_TYPE_CONST, {.i64 = MODE_BFF }, INT_MIN, INT_MAX, .flags = V, .unit = "scan" },
 
61
    { "lowpass", "enable vertical low-pass filter", OFFSET(lowpass),
 
62
        AV_OPT_TYPE_INT,   {.i64 = 1 },        0, 1, .flags = V },
 
63
    { NULL }
 
64
};
 
65
 
 
66
static const AVClass class = {
 
67
    .class_name = "interlace filter",
 
68
    .item_name  = av_default_item_name,
 
69
    .option     = options,
 
70
    .version    = LIBAVUTIL_VERSION_INT,
 
71
};
 
72
 
 
73
 
 
74
static const enum AVPixelFormat formats_supported[] = {
 
75
    AV_PIX_FMT_YUV420P,  AV_PIX_FMT_YUV422P,  AV_PIX_FMT_YUV444P,
 
76
    AV_PIX_FMT_YUV444P,  AV_PIX_FMT_YUV410P,  AV_PIX_FMT_YUVA420P,
 
77
    AV_PIX_FMT_GRAY8,    AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
 
78
    AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_NONE
 
79
};
 
80
 
 
81
static int query_formats(AVFilterContext *ctx)
 
82
{
 
83
    ff_set_common_formats(ctx, ff_make_format_list(formats_supported));
 
84
    return 0;
 
85
}
 
86
 
 
87
static av_cold void uninit(AVFilterContext *ctx)
 
88
{
 
89
    InterlaceContext *s = ctx->priv;
 
90
 
 
91
    av_frame_free(&s->cur);
 
92
    av_frame_free(&s->next);
 
93
 
 
94
    av_opt_free(s);
 
95
}
 
96
 
 
97
static int config_out_props(AVFilterLink *outlink)
 
98
{
 
99
    AVFilterContext *ctx = outlink->src;
 
100
    AVFilterLink *inlink = outlink->src->inputs[0];
 
101
    InterlaceContext *s = ctx->priv;
 
102
 
 
103
    if (inlink->h < 2) {
 
104
        av_log(ctx, AV_LOG_ERROR, "input video height is too small\n");
 
105
        return AVERROR_INVALIDDATA;
 
106
    }
 
107
    // same input size
 
108
    outlink->w = inlink->w;
 
109
    outlink->h = inlink->h;
 
110
    outlink->time_base = inlink->time_base;
 
111
    // half framerate
 
112
    outlink->time_base.num *= 2;
 
113
 
 
114
    av_log(ctx, AV_LOG_VERBOSE, "%s interlacing %s lowpass filter\n",
 
115
           s->scan == MODE_TFF ? "tff" : "bff", (s->lowpass) ? "with" : "without");
 
116
 
 
117
    return 0;
 
118
}
 
119
 
 
120
static void copy_picture_field(AVFrame *src_frame, AVFrame *dst_frame,
 
121
                               AVFilterLink *inlink, enum FieldType field_type,
 
122
                               int lowpass)
 
123
{
 
124
    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
 
125
    int vsub = desc->log2_chroma_h;
 
126
    int plane, i, j;
 
127
 
 
128
    for (plane = 0; plane < desc->nb_components; plane++) {
 
129
        int lines = (plane == 1 || plane == 2) ? -(-inlink->h) >> vsub : inlink->h;
 
130
        int linesize = av_image_get_linesize(inlink->format, inlink->w, plane);
 
131
        uint8_t *dstp = dst_frame->data[plane];
 
132
        const uint8_t *srcp = src_frame->data[plane];
 
133
 
 
134
        av_assert0(linesize >= 0);
 
135
 
 
136
        lines = (lines + (field_type == FIELD_UPPER)) / 2;
 
137
        if (field_type == FIELD_LOWER)
 
138
            srcp += src_frame->linesize[plane];
 
139
        if (field_type == FIELD_LOWER)
 
140
            dstp += dst_frame->linesize[plane];
 
141
        if (lowpass) {
 
142
            int srcp_linesize = src_frame->linesize[plane] * 2;
 
143
            int dstp_linesize = dst_frame->linesize[plane] * 2;
 
144
            for (j = lines; j > 0; j--) {
 
145
                const uint8_t *srcp_above = srcp - src_frame->linesize[plane];
 
146
                const uint8_t *srcp_below = srcp + src_frame->linesize[plane];
 
147
                if (j == lines)
 
148
                    srcp_above = srcp; // there is no line above
 
149
                if (j == 1)
 
150
                    srcp_below = srcp; // there is no line below
 
151
                for (i = 0; i < linesize; i++) {
 
152
                    // this calculation is an integer representation of
 
153
                    // '0.5 * current + 0.25 * above + 0.25 * below'
 
154
                    // '1 +' is for rounding.
 
155
                    dstp[i] = (1 + srcp[i] + srcp[i] + srcp_above[i] + srcp_below[i]) >> 2;
 
156
                }
 
157
                dstp += dstp_linesize;
 
158
                srcp += srcp_linesize;
 
159
            }
 
160
        } else {
 
161
            av_image_copy_plane(dstp, dst_frame->linesize[plane] * 2,
 
162
                                srcp, src_frame->linesize[plane] * 2,
 
163
                                linesize, lines);
 
164
        }
 
165
    }
 
166
}
 
167
 
 
168
static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
 
169
{
 
170
    AVFilterContext *ctx = inlink->dst;
 
171
    AVFilterLink *outlink = ctx->outputs[0];
 
172
    InterlaceContext *s = ctx->priv;
 
173
    AVFrame *out;
 
174
    int tff, ret;
 
175
 
 
176
    av_frame_free(&s->cur);
 
177
    s->cur  = s->next;
 
178
    s->next = buf;
 
179
 
 
180
    /* we need at least two frames */
 
181
    if (!s->cur || !s->next)
 
182
        return 0;
 
183
 
 
184
    if (s->cur->interlaced_frame) {
 
185
        av_log(ctx, AV_LOG_WARNING,
 
186
               "video is already interlaced, adjusting framerate only\n");
 
187
        out = av_frame_clone(s->cur);
 
188
        if (!out)
 
189
            return AVERROR(ENOMEM);
 
190
        out->pts /= 2;  // adjust pts to new framerate
 
191
        ret = ff_filter_frame(outlink, out);
 
192
        s->got_output = 1;
 
193
        return ret;
 
194
    }
 
195
 
 
196
    tff = (s->scan == MODE_TFF);
 
197
    out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
 
198
    if (!out)
 
199
        return AVERROR(ENOMEM);
 
200
 
 
201
    av_frame_copy_props(out, s->cur);
 
202
    out->interlaced_frame = 1;
 
203
    out->top_field_first  = tff;
 
204
    out->pts             /= 2;  // adjust pts to new framerate
 
205
 
 
206
    /* copy upper/lower field from cur */
 
207
    copy_picture_field(s->cur, out, inlink, tff ? FIELD_UPPER : FIELD_LOWER, s->lowpass);
 
208
    av_frame_free(&s->cur);
 
209
 
 
210
    /* copy lower/upper field from next */
 
211
    copy_picture_field(s->next, out, inlink, tff ? FIELD_LOWER : FIELD_UPPER, s->lowpass);
 
212
    av_frame_free(&s->next);
 
213
 
 
214
    ret = ff_filter_frame(outlink, out);
 
215
    s->got_output = 1;
 
216
 
 
217
    return ret;
 
218
}
 
219
 
 
220
static int request_frame(AVFilterLink *outlink)
 
221
{
 
222
    AVFilterContext *ctx = outlink->src;
 
223
    InterlaceContext *s  = ctx->priv;
 
224
    int ret = 0;
 
225
 
 
226
    s->got_output = 0;
 
227
    while (ret >= 0 && !s->got_output)
 
228
        ret = ff_request_frame(ctx->inputs[0]);
 
229
 
 
230
    return ret;
 
231
}
 
232
 
 
233
static const AVFilterPad inputs[] = {
 
234
    {
 
235
        .name         = "default",
 
236
        .type         = AVMEDIA_TYPE_VIDEO,
 
237
        .filter_frame = filter_frame,
 
238
    },
 
239
    { NULL }
 
240
};
 
241
 
 
242
static const AVFilterPad outputs[] = {
 
243
    {
 
244
        .name          = "default",
 
245
        .type          = AVMEDIA_TYPE_VIDEO,
 
246
        .config_props  = config_out_props,
 
247
        .request_frame = request_frame,
 
248
    },
 
249
    { NULL }
 
250
};
 
251
 
 
252
AVFilter ff_vf_interlace = {
 
253
    .name          = "interlace",
 
254
    .description   = NULL_IF_CONFIG_SMALL("Convert progressive video into interlaced."),
 
255
    .uninit        = uninit,
 
256
 
 
257
    .priv_class    = &class,
 
258
    .priv_size     = sizeof(InterlaceContext),
 
259
    .query_formats = query_formats,
 
260
 
 
261
    .inputs        = inputs,
 
262
    .outputs       = outputs,
 
263
};
 
264