2 * Copyright (c) 2002 A'rpi
3 * Copyright (C) 2012 Clément Bœsch
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * libpostproc filter, ported from MPlayer.
27 #include "libavutil/avassert.h"
28 #include "libavutil/opt.h"
33 #include "libpostproc/postprocess.h"
35 typedef struct PPFilterContext {
39 pp_mode *modes[PP_QUALITY_MAX + 1];
43 #define OFFSET(x) offsetof(PPFilterContext, x)
44 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
45 static const AVOption pp_options[] = {
46 { "subfilters", "set postprocess subfilters", OFFSET(subfilters), AV_OPT_TYPE_STRING, {.str="de"}, .flags = FLAGS },
50 AVFILTER_DEFINE_CLASS(pp);
52 static av_cold int pp_init(AVFilterContext *ctx)
55 PPFilterContext *pp = ctx->priv;
57 for (i = 0; i <= PP_QUALITY_MAX; i++) {
58 pp->modes[i] = pp_get_mode_by_name_and_quality(pp->subfilters, i);
60 return AVERROR_EXTERNAL;
62 pp->mode_id = PP_QUALITY_MAX;
66 static int pp_process_command(AVFilterContext *ctx, const char *cmd, const char *args,
67 char *res, int res_len, int flags)
69 PPFilterContext *pp = ctx->priv;
71 if (!strcmp(cmd, "quality")) {
72 pp->mode_id = av_clip(strtol(args, NULL, 10), 0, PP_QUALITY_MAX);
75 return AVERROR(ENOSYS);
78 static int pp_query_formats(AVFilterContext *ctx)
80 static const enum AVPixelFormat pix_fmts[] = {
81 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVJ420P,
82 AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVJ422P,
85 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P,
86 AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUVJ440P,
91 AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
93 return AVERROR(ENOMEM);
94 return ff_set_common_formats(ctx, fmts_list);
97 static int pp_config_props(AVFilterLink *inlink)
99 int flags = PP_CPU_CAPS_AUTO;
100 PPFilterContext *pp = inlink->dst->priv;
102 switch (inlink->format) {
103 case AV_PIX_FMT_GRAY8:
104 case AV_PIX_FMT_YUVJ420P:
105 case AV_PIX_FMT_YUV420P: flags |= PP_FORMAT_420; break;
106 case AV_PIX_FMT_YUVJ422P:
107 case AV_PIX_FMT_YUV422P: flags |= PP_FORMAT_422; break;
108 case AV_PIX_FMT_YUV411P: flags |= PP_FORMAT_411; break;
109 case AV_PIX_FMT_GBRP:
110 case AV_PIX_FMT_YUVJ444P:
111 case AV_PIX_FMT_YUV444P: flags |= PP_FORMAT_444; break;
112 case AV_PIX_FMT_YUVJ440P:
113 case AV_PIX_FMT_YUV440P: flags |= PP_FORMAT_440; break;
114 default: av_assert0(0);
117 pp->pp_ctx = pp_get_context(inlink->w, inlink->h, flags);
119 return AVERROR(ENOMEM);
123 static int pp_filter_frame(AVFilterLink *inlink, AVFrame *inbuf)
125 AVFilterContext *ctx = inlink->dst;
126 PPFilterContext *pp = ctx->priv;
127 AVFilterLink *outlink = ctx->outputs[0];
128 const int aligned_w = FFALIGN(outlink->w, 8);
129 const int aligned_h = FFALIGN(outlink->h, 8);
132 int8_t *qp_table = NULL;
135 outbuf = ff_get_video_buffer(outlink, aligned_w, aligned_h);
137 av_frame_free(&inbuf);
138 return AVERROR(ENOMEM);
140 av_frame_copy_props(outbuf, inbuf);
141 outbuf->width = inbuf->width;
142 outbuf->height = inbuf->height;
144 ret = ff_qp_table_extract(inbuf, &qp_table, &qstride, NULL, NULL);
146 av_frame_free(&inbuf);
147 av_frame_free(&outbuf);
152 pp_postprocess((const uint8_t **)inbuf->data, inbuf->linesize,
153 outbuf->data, outbuf->linesize,
154 aligned_w, outlink->h,
157 pp->modes[pp->mode_id],
159 outbuf->pict_type | (qp_table ? PP_PICT_TYPE_QP2 : 0));
161 av_frame_free(&inbuf);
163 return ff_filter_frame(outlink, outbuf);
166 static av_cold void pp_uninit(AVFilterContext *ctx)
169 PPFilterContext *pp = ctx->priv;
171 for (i = 0; i <= PP_QUALITY_MAX; i++)
172 pp_free_mode(pp->modes[i]);
174 pp_free_context(pp->pp_ctx);
177 static const AVFilterPad pp_inputs[] = {
180 .type = AVMEDIA_TYPE_VIDEO,
181 .config_props = pp_config_props,
182 .filter_frame = pp_filter_frame,
187 static const AVFilterPad pp_outputs[] = {
190 .type = AVMEDIA_TYPE_VIDEO,
195 AVFilter ff_vf_pp = {
197 .description = NULL_IF_CONFIG_SMALL("Filter video using libpostproc."),
198 .priv_size = sizeof(PPFilterContext),
201 .query_formats = pp_query_formats,
203 .outputs = pp_outputs,
204 .process_command = pp_process_command,
205 .priv_class = &pp_class,
206 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,