FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_pp.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 46 67 68.7%
Functions: 4 5 80.0%
Branches: 13 24 54.2%

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