FFmpeg  4.4
vf_pp.c
Go to the documentation of this file.
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 
34 
35 typedef struct PPFilterContext {
36  const AVClass *class;
37  char *subfilters;
38  int mode_id;
40  void *pp_ctx;
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 
51 
53 {
54  int i;
55  PPFilterContext *pp = ctx->priv;
56 
57  for (i = 0; i <= PP_QUALITY_MAX; i++) {
59  if (!pp->modes[i])
60  return AVERROR_EXTERNAL;
61  }
62  pp->mode_id = PP_QUALITY_MAX;
63  return 0;
64 }
65 
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 
79 {
80  static const enum AVPixelFormat pix_fmts[] = {
89  };
90 
92  if (!fmts_list)
93  return AVERROR(ENOMEM);
94  return ff_set_common_formats(ctx, fmts_list);
95 }
96 
97 static int pp_config_props(AVFilterLink *inlink)
98 {
99  int flags = PP_CPU_CAPS_AUTO;
100  PPFilterContext *pp = inlink->dst->priv;
101 
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);
115  }
116 
117  pp->pp_ctx = pp_get_context(inlink->w, inlink->h, flags);
118  if (!pp->pp_ctx)
119  return AVERROR(ENOMEM);
120  return 0;
121 }
122 
123 static int pp_filter_frame(AVFilterLink *inlink, AVFrame *inbuf)
124 {
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);
130  AVFrame *outbuf;
131  int qstride = 0;
132  int8_t *qp_table = NULL;
133  int ret;
134 
135  outbuf = ff_get_video_buffer(outlink, aligned_w, aligned_h);
136  if (!outbuf) {
137  av_frame_free(&inbuf);
138  return AVERROR(ENOMEM);
139  }
140  av_frame_copy_props(outbuf, inbuf);
141  outbuf->width = inbuf->width;
142  outbuf->height = inbuf->height;
143 
144  ret = ff_qp_table_extract(inbuf, &qp_table, &qstride, NULL, NULL);
145  if (ret < 0) {
146  av_frame_free(&inbuf);
147  av_frame_free(&outbuf);
148  return ret;
149  }
150 
151  pp_postprocess((const uint8_t **)inbuf->data, inbuf->linesize,
152  outbuf->data, outbuf->linesize,
153  aligned_w, outlink->h,
154  qp_table,
155  qstride,
156  pp->modes[pp->mode_id],
157  pp->pp_ctx,
158  outbuf->pict_type | (qp_table ? PP_PICT_TYPE_QP2 : 0));
159 
160  av_frame_free(&inbuf);
161  av_freep(&qp_table);
162  return ff_filter_frame(outlink, outbuf);
163 }
164 
166 {
167  int i;
168  PPFilterContext *pp = ctx->priv;
169 
170  for (i = 0; i <= PP_QUALITY_MAX; i++)
171  pp_free_mode(pp->modes[i]);
172  if (pp->pp_ctx)
173  pp_free_context(pp->pp_ctx);
174 }
175 
176 static const AVFilterPad pp_inputs[] = {
177  {
178  .name = "default",
179  .type = AVMEDIA_TYPE_VIDEO,
180  .config_props = pp_config_props,
181  .filter_frame = pp_filter_frame,
182  },
183  { NULL }
184 };
185 
186 static const AVFilterPad pp_outputs[] = {
187  {
188  .name = "default",
189  .type = AVMEDIA_TYPE_VIDEO,
190  },
191  { NULL }
192 };
193 
195  .name = "pp",
196  .description = NULL_IF_CONFIG_SMALL("Filter video using libpostproc."),
197  .priv_size = sizeof(PPFilterContext),
198  .init = pp_init,
199  .uninit = pp_uninit,
201  .inputs = pp_inputs,
202  .outputs = pp_outputs,
204  .priv_class = &pp_class,
206 };
static int query_formats(AVFilterContext *ctx)
Definition: aeval.c:244
static const AVFilterPad inputs[]
Definition: af_acontrast.c:193
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
Definition: af_acrusher.c:336
#define av_cold
Definition: attributes.h:88
uint8_t
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1094
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:31
#define flags(name, subs,...)
Definition: cbs_av1.c:561
#define av_clip
Definition: common.h:122
#define NULL
Definition: coverity.c:32
static av_cold int uninit(AVCodecContext *avctx)
Definition: crystalhd.c:279
int ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:587
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:286
@ AV_OPT_TYPE_STRING
Definition: opt.h:229
#define AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
Some filters support a generic "enable" expression option that can be used to enable or disable a fil...
Definition: avfilter.h:126
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition: error.h:57
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:203
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:658
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
av_cold pp_context * pp_get_context(int width, int height, int cpuCaps)
Definition: postprocess.c:879
av_cold void pp_free_context(void *vc)
Definition: postprocess.c:912
#define PP_CPU_CAPS_AUTO
Definition: postprocess.h:92
#define PP_PICT_TYPE_QP2
MPEG2 style QScale.
Definition: postprocess.h:101
#define PP_FORMAT_420
Definition: postprocess.h:95
#define PP_FORMAT_422
Definition: postprocess.h:96
#define PP_QUALITY_MAX
Definition: postprocess.h:54
pp_mode * pp_get_mode_by_name_and_quality(const char *name, int quality)
Return a pp_mode or NULL if an error occurred.
Definition: postprocess.c:641
void pp_mode
Definition: postprocess.h:59
#define PP_FORMAT_444
Definition: postprocess.h:98
void pp_postprocess(const uint8_t *src[3], const int srcStride[3], uint8_t *dst[3], const int dstStride[3], int width, int height, const int8_t *QP_store, int QPStride, pp_mode *vm, void *vc, int pict_type)
Definition: postprocess.c:935
#define PP_FORMAT_411
Definition: postprocess.h:97
void pp_free_mode(pp_mode *mode)
Definition: postprocess.c:837
#define PP_FORMAT_440
Definition: postprocess.h:99
int i
Definition: input.c:407
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:303
#define FFALIGN(x, a)
Definition: macros.h:48
AVOptions.
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
@ AV_PIX_FMT_YUV440P
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition: pixfmt.h:99
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:70
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:74
@ AV_PIX_FMT_YUVJ440P
planar YUV 4:4:0 full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV440P and setting color_range
Definition: pixfmt.h:100
@ AV_PIX_FMT_YUV411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:73
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:71
@ AV_PIX_FMT_YUVJ422P
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV422P and setting col...
Definition: pixfmt.h:79
@ AV_PIX_FMT_GBRP
planar GBR 4:4:4 24bpp
Definition: pixfmt.h:168
@ AV_PIX_FMT_YUVJ444P
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
Definition: pixfmt.h:80
@ AV_PIX_FMT_YUVJ420P
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
Definition: pixfmt.h:78
external API header
int ff_qp_table_extract(AVFrame *frame, int8_t **table, int *table_w, int *table_h, int *qscale_type)
Extract a libpostproc-compatible QP table - an 8-bit QP value per 16x16 macroblock,...
Definition: qp_table.c:30
Describe the class of an AVClass context structure.
Definition: log.h:67
An instance of a filter.
Definition: avfilter.h:341
void * priv
private data for use by the filter
Definition: avfilter.h:356
A list of supported formats for one end of a filter link.
Definition: formats.h:65
A filter pad used for either input or output.
Definition: internal.h:54
const char * name
Pad name.
Definition: internal.h:60
Filter definition.
Definition: avfilter.h:145
const char * name
Filter name.
Definition: avfilter.h:149
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:332
int width
Definition: frame.h:376
int height
Definition: frame.h:376
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:349
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:401
AVOption.
Definition: opt.h:248
void * pp_ctx
Definition: vf_pp.c:40
char * subfilters
Definition: vf_pp.c:37
int mode_id
Definition: vf_pp.c:38
pp_mode * modes[PP_QUALITY_MAX+1]
Definition: vf_pp.c:39
#define av_freep(p)
AVFormatContext * ctx
Definition: movenc.c:48
static int pp_query_formats(AVFilterContext *ctx)
Definition: vf_pp.c:78
static int pp_process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
Definition: vf_pp.c:66
static const AVFilterPad pp_outputs[]
Definition: vf_pp.c:186
AVFILTER_DEFINE_CLASS(pp)
static av_cold void pp_uninit(AVFilterContext *ctx)
Definition: vf_pp.c:165
#define FLAGS
Definition: vf_pp.c:44
static int pp_filter_frame(AVFilterLink *inlink, AVFrame *inbuf)
Definition: vf_pp.c:123
AVFilter ff_vf_pp
Definition: vf_pp.c:194
static const AVFilterPad pp_inputs[]
Definition: vf_pp.c:176
static av_cold int pp_init(AVFilterContext *ctx)
Definition: vf_pp.c:52
#define OFFSET(x)
Definition: vf_pp.c:43
static int pp_config_props(AVFilterLink *inlink)
Definition: vf_pp.c:97
static const AVOption pp_options[]
Definition: vf_pp.c:45
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:99