FFmpeg
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/mem.h"
29 #include "libavutil/opt.h"
30 
31 #include "filters.h"
32 #include "qp_table.h"
33 #include "video.h"
34 
36 
37 typedef struct PPFilterContext {
38  const AVClass *class;
39  char *subfilters;
40  int mode_id;
42  void *pp_ctx;
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 
53 
55 {
56  int i;
57  PPFilterContext *pp = ctx->priv;
58 
59  for (i = 0; i <= PP_QUALITY_MAX; i++) {
61  if (!pp->modes[i])
62  return AVERROR_EXTERNAL;
63  }
64  pp->mode_id = PP_QUALITY_MAX;
65  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[] = {
89 };
90 
92 {
93  int flags = PP_CPU_CAPS_AUTO;
94  PPFilterContext *pp = inlink->dst->priv;
95 
96  switch (inlink->format) {
97  case AV_PIX_FMT_GRAY8:
99  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  pp->pp_ctx = pp_get_context(inlink->w, inlink->h, flags);
112  if (!pp->pp_ctx)
113  return AVERROR(ENOMEM);
114  return 0;
115 }
116 
118 {
119  AVFilterContext *ctx = inlink->dst;
120  PPFilterContext *pp = ctx->priv;
121  AVFilterLink *outlink = ctx->outputs[0];
122  const int aligned_w = FFALIGN(outlink->w, 8);
123  const int aligned_h = FFALIGN(outlink->h, 8);
124  AVFrame *outbuf;
125  int qstride = 0;
126  int8_t *qp_table = NULL;
127  int ret;
128 
129  outbuf = ff_get_video_buffer(outlink, aligned_w, aligned_h);
130  if (!outbuf) {
131  av_frame_free(&inbuf);
132  return AVERROR(ENOMEM);
133  }
134  av_frame_copy_props(outbuf, inbuf);
135  outbuf->width = inbuf->width;
136  outbuf->height = inbuf->height;
137 
138  ret = ff_qp_table_extract(inbuf, &qp_table, &qstride, NULL, NULL);
139  if (ret < 0) {
140  av_frame_free(&inbuf);
141  av_frame_free(&outbuf);
142  return ret;
143  }
144 
145  pp_postprocess((const uint8_t **)inbuf->data, inbuf->linesize,
146  outbuf->data, outbuf->linesize,
147  aligned_w, outlink->h,
148  qp_table,
149  qstride,
150  pp->modes[pp->mode_id],
151  pp->pp_ctx,
152  outbuf->pict_type | (qp_table ? PP_PICT_TYPE_QP2 : 0));
153 
154  av_frame_free(&inbuf);
155  av_freep(&qp_table);
156  return ff_filter_frame(outlink, outbuf);
157 }
158 
160 {
161  int i;
162  PPFilterContext *pp = ctx->priv;
163 
164  for (i = 0; i <= PP_QUALITY_MAX; i++)
165  pp_free_mode(pp->modes[i]);
166  if (pp->pp_ctx)
167  pp_free_context(pp->pp_ctx);
168 }
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 
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,
188  .process_command = pp_process_command,
189  .priv_class = &pp_class,
191 };
ff_get_video_buffer
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:116
pp_get_mode_by_name_and_quality
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:597
pp_config_props
static int pp_config_props(AVFilterLink *inlink)
Definition: vf_pp.c:91
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:71
av_clip
#define av_clip
Definition: common.h:100
qp_table.h
PP_FORMAT_420
#define PP_FORMAT_420
Definition: postprocess.h:95
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
FILTER_PIXFMTS_ARRAY
#define FILTER_PIXFMTS_ARRAY(array)
Definition: filters.h:242
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1061
inlink
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
Definition: filter_design.txt:212
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:162
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: filters.h:262
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:389
AVFrame::width
int width
Definition: frame.h:461
AVOption
AVOption.
Definition: opt.h:429
AV_PIX_FMT_YUV440P
@ AV_PIX_FMT_YUV440P
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition: pixfmt.h:106
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:205
video.h
PP_PICT_TYPE_QP2
#define PP_PICT_TYPE_QP2
MPEG2 style QScale.
Definition: postprocess.h:101
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:410
pp_options
static const AVOption pp_options[]
Definition: vf_pp.c:47
PPFilterContext::mode_id
int mode_id
Definition: vf_pp.c:40
AVFilterPad
A filter pad used for either input or output.
Definition: filters.h:38
pp_free_context
av_cold void pp_free_context(void *vc)
Definition: postprocess.c:865
avassert.h
av_cold
#define av_cold
Definition: attributes.h:90
pp_free_mode
void pp_free_mode(pp_mode *mode)
Definition: postprocess.c:793
ff_video_default_filterpad
const AVFilterPad ff_video_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_VIDEO.
Definition: video.c:37
postprocess.h
AV_PIX_FMT_YUVJ422P
@ 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:86
pp_postprocess
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:888
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(pp)
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:40
filters.h
pp_inputs
static const AVFilterPad pp_inputs[]
Definition: vf_pp.c:170
ctx
AVFormatContext * ctx
Definition: movenc.c:49
AV_PIX_FMT_YUV420P
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:73
pp_init
static av_cold int pp_init(AVFilterContext *ctx)
Definition: vf_pp.c:54
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: filters.h:263
AV_PIX_FMT_YUVJ444P
@ 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:87
PP_CPU_CAPS_AUTO
#define PP_CPU_CAPS_AUTO
Definition: postprocess.h:92
pp_get_context
av_cold pp_context * pp_get_context(int width, int height, int cpuCaps)
Definition: postprocess.c:835
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:75
NULL
#define NULL
Definition: coverity.c:32
av_frame_copy_props
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:725
PPFilterContext::subfilters
char * subfilters
Definition: vf_pp.c:39
AV_PIX_FMT_YUVJ420P
@ 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:85
AV_PIX_FMT_GRAY8
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:81
PP_FORMAT_422
#define PP_FORMAT_422
Definition: postprocess.h:96
AVFrame::pict_type
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:491
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:368
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:94
pp_mode
void pp_mode
Definition: postprocess.h:65
PP_QUALITY_MAX
#define PP_QUALITY_MAX
Definition: postprocess.h:60
AVERROR_EXTERNAL
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition: error.h:59
FLAGS
#define FLAGS
Definition: vf_pp.c:46
AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
#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:182
PP_FORMAT_440
#define PP_FORMAT_440
Definition: postprocess.h:99
uninit
static void uninit(AVBSFContext *ctx)
Definition: pcm_rechunk.c:68
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
OFFSET
#define OFFSET(x)
Definition: vf_pp.c:45
AV_PIX_FMT_YUVJ440P
@ 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:107
AVFilterPad::name
const char * name
Pad name.
Definition: filters.h:44
AVFilter
Filter definition.
Definition: avfilter.h:201
ret
ret
Definition: filter_design.txt:187
ff_qp_table_extract
int ff_qp_table_extract(AVFrame *frame, int8_t **table, int *table_w, int *table_h, enum AVVideoEncParamsType *qscale_type)
Extract a libpostproc-compatible QP table - an 8-bit QP value per 16x16 macroblock,...
Definition: qp_table.c:27
PPFilterContext
Definition: vf_pp.c:37
PP_FORMAT_444
#define PP_FORMAT_444
Definition: postprocess.h:98
PPFilterContext::modes
pp_mode * modes[PP_QUALITY_MAX+1]
Definition: vf_pp.c:41
PP_FORMAT_411
#define PP_FORMAT_411
Definition: postprocess.h:97
pp_process_command
static int pp_process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
Definition: vf_pp.c:68
AVFrame::height
int height
Definition: frame.h:461
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:72
AV_PIX_FMT_YUV444P
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:78
AVFilterContext
An instance of a filter.
Definition: avfilter.h:457
AV_PIX_FMT_GBRP
@ AV_PIX_FMT_GBRP
planar GBR 4:4:4 24bpp
Definition: pixfmt.h:165
ff_vf_pp
const AVFilter ff_vf_pp
Definition: vf_pp.c:179
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AV_PIX_FMT_YUV422P
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:77
mem.h
pp_uninit
static av_cold void pp_uninit(AVFilterContext *ctx)
Definition: vf_pp.c:159
FFALIGN
#define FFALIGN(x, a)
Definition: macros.h:78
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
AV_PIX_FMT_YUV411P
@ AV_PIX_FMT_YUV411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:80
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:482
pp_filter_frame
static int pp_filter_frame(AVFilterLink *inlink, AVFrame *inbuf)
Definition: vf_pp.c:117
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition: frame.h:434
pix_fmts
static enum AVPixelFormat pix_fmts[]
Definition: vf_pp.c:80
AV_OPT_TYPE_STRING
@ AV_OPT_TYPE_STRING
Underlying C type is a uint8_t* that is either NULL or points to a C string allocated with the av_mal...
Definition: opt.h:276
PPFilterContext::pp_ctx
void * pp_ctx
Definition: vf_pp.c:42