1 /*
2 * Copyright (c) 2002 A'rpi
3 * Copyright (C) 2012 Clément Bœsch
4 *
5 * This file is part of FFmpeg.
6 *
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.
11 *
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.
16 *
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.
20 */
21
22 /**
23 * @file
24 * libpostproc filter, ported from MPlayer.
25 */
26
27 #include "libavutil/avassert.h"
28 #include "libavutil/opt.h"
29
30 #include "internal.h"
31 #include "qp_table.h"
32
33 #include "libpostproc/postprocess.h"
34
35 typedef struct PPFilterContext {
36 const AVClass *class;
37 char *subfilters;
38 int mode_id;
39 pp_mode *modes[PP_QUALITY_MAX + 1];
40 void *pp_ctx;
41 } PPFilterContext;
42
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 },
47 { NULL }
48 };
49
50 AVFILTER_DEFINE_CLASS(pp);
51
pp_init(AVFilterContext * ctx)52 static av_cold int pp_init(AVFilterContext *ctx)
53 {
54 int i;
55 PPFilterContext *pp = ctx->priv;
56
57 for (i = 0; i <= PP_QUALITY_MAX; i++) {
58 pp->modes[i] = pp_get_mode_by_name_and_quality(pp->subfilters, i);
59 if (!pp->modes[i])
60 return AVERROR_EXTERNAL;
61 }
62 pp->mode_id = PP_QUALITY_MAX;
63 return 0;
64 }
65
pp_process_command(AVFilterContext * ctx,const char * cmd,const char * args,char * res,int res_len,int flags)66 static int pp_process_command(AVFilterContext *ctx, const char *cmd, const char *args,
67 char *res, int res_len, int flags)
68 {
69 PPFilterContext *pp = ctx->priv;
70
71 if (!strcmp(cmd, "quality")) {
72 pp->mode_id = av_clip(strtol(args, NULL, 10), 0, PP_QUALITY_MAX);
73 return 0;
74 }
75 return AVERROR(ENOSYS);
76 }
77
78 static const enum AVPixelFormat pix_fmts[] = {
79 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVJ420P,
80 AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVJ422P,
81 AV_PIX_FMT_YUV411P,
82 AV_PIX_FMT_GBRP,
83 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P,
84 AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUVJ440P,
85 AV_PIX_FMT_GRAY8,
86 AV_PIX_FMT_NONE
87 };
88
pp_config_props(AVFilterLink * inlink)89 static int pp_config_props(AVFilterLink *inlink)
90 {
91 int flags = PP_CPU_CAPS_AUTO;
92 PPFilterContext *pp = inlink->dst->priv;
93
94 switch (inlink->format) {
95 case AV_PIX_FMT_GRAY8:
96 case AV_PIX_FMT_YUVJ420P:
97 case AV_PIX_FMT_YUV420P: flags |= PP_FORMAT_420; break;
98 case AV_PIX_FMT_YUVJ422P:
99 case AV_PIX_FMT_YUV422P: flags |= PP_FORMAT_422; break;
100 case AV_PIX_FMT_YUV411P: flags |= PP_FORMAT_411; break;
101 case AV_PIX_FMT_GBRP:
102 case AV_PIX_FMT_YUVJ444P:
103 case AV_PIX_FMT_YUV444P: flags |= PP_FORMAT_444; break;
104 case AV_PIX_FMT_YUVJ440P:
105 case AV_PIX_FMT_YUV440P: flags |= PP_FORMAT_440; break;
106 default: av_assert0(0);
107 }
108
109 pp->pp_ctx = pp_get_context(inlink->w, inlink->h, flags);
110 if (!pp->pp_ctx)
111 return AVERROR(ENOMEM);
112 return 0;
113 }
114
pp_filter_frame(AVFilterLink * inlink,AVFrame * inbuf)115 static int pp_filter_frame(AVFilterLink *inlink, AVFrame *inbuf)
116 {
117 AVFilterContext *ctx = inlink->dst;
118 PPFilterContext *pp = ctx->priv;
119 AVFilterLink *outlink = ctx->outputs[0];
120 const int aligned_w = FFALIGN(outlink->w, 8);
121 const int aligned_h = FFALIGN(outlink->h, 8);
122 AVFrame *outbuf;
123 int qstride = 0;
124 int8_t *qp_table = NULL;
125 int ret;
126
127 outbuf = ff_get_video_buffer(outlink, aligned_w, aligned_h);
128 if (!outbuf) {
129 av_frame_free(&inbuf);
130 return AVERROR(ENOMEM);
131 }
132 av_frame_copy_props(outbuf, inbuf);
133 outbuf->width = inbuf->width;
134 outbuf->height = inbuf->height;
135
136 ret = ff_qp_table_extract(inbuf, &qp_table, &qstride, NULL, NULL);
137 if (ret < 0) {
138 av_frame_free(&inbuf);
139 av_frame_free(&outbuf);
140 return ret;
141 }
142
143 pp_postprocess((const uint8_t **)inbuf->data, inbuf->linesize,
144 outbuf->data, outbuf->linesize,
145 aligned_w, outlink->h,
146 qp_table,
147 qstride,
148 pp->modes[pp->mode_id],
149 pp->pp_ctx,
150 outbuf->pict_type | (qp_table ? PP_PICT_TYPE_QP2 : 0));
151
152 av_frame_free(&inbuf);
153 av_freep(&qp_table);
154 return ff_filter_frame(outlink, outbuf);
155 }
156
pp_uninit(AVFilterContext * ctx)157 static av_cold void pp_uninit(AVFilterContext *ctx)
158 {
159 int i;
160 PPFilterContext *pp = ctx->priv;
161
162 for (i = 0; i <= PP_QUALITY_MAX; i++)
163 pp_free_mode(pp->modes[i]);
164 if (pp->pp_ctx)
165 pp_free_context(pp->pp_ctx);
166 }
167
168 static const AVFilterPad pp_inputs[] = {
169 {
170 .name = "default",
171 .type = AVMEDIA_TYPE_VIDEO,
172 .config_props = pp_config_props,
173 .filter_frame = pp_filter_frame,
174 },
175 };
176
177 static const AVFilterPad pp_outputs[] = {
178 {
179 .name = "default",
180 .type = AVMEDIA_TYPE_VIDEO,
181 },
182 };
183
184 const AVFilter ff_vf_pp = {
185 .name = "pp",
186 .description = NULL_IF_CONFIG_SMALL("Filter video using libpostproc."),
187 .priv_size = sizeof(PPFilterContext),
188 .init = pp_init,
189 .uninit = pp_uninit,
190 FILTER_INPUTS(pp_inputs),
191 FILTER_OUTPUTS(pp_outputs),
192 FILTER_PIXFMTS_ARRAY(pix_fmts),
193 .process_command = pp_process_command,
194 .priv_class = &pp_class,
195 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
196 };
197