FFmpeg  2.1.1
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 #include "internal.h"
30 
31 #include "libpostproc/postprocess.h"
32 
33 typedef struct {
34  const AVClass *class;
35  char *subfilters;
36  int mode_id;
38  void *pp_ctx;
40 
41 #define OFFSET(x) offsetof(PPFilterContext, x)
42 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
43 static const AVOption pp_options[] = {
44  { "subfilters", "set postprocess subfilters", OFFSET(subfilters), AV_OPT_TYPE_STRING, {.str="de"}, .flags = FLAGS },
45  { NULL }
46 };
47 
49 
51 {
52  int i;
53  PPFilterContext *pp = ctx->priv;
54 
55  for (i = 0; i <= PP_QUALITY_MAX; i++) {
57  if (!pp->modes[i])
58  return AVERROR_EXTERNAL;
59  }
60  pp->mode_id = PP_QUALITY_MAX;
61  return 0;
62 }
63 
64 static int pp_process_command(AVFilterContext *ctx, const char *cmd, const char *args,
65  char *res, int res_len, int flags)
66 {
67  PPFilterContext *pp = ctx->priv;
68 
69  if (!strcmp(cmd, "quality")) {
70  pp->mode_id = av_clip(strtol(args, NULL, 10), 0, PP_QUALITY_MAX);
71  return 0;
72  }
73  return AVERROR(ENOSYS);
74 }
75 
77 {
78  static const enum PixelFormat pix_fmts[] = {
84  };
86  return 0;
87 }
88 
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) {
96  case AV_PIX_FMT_YUV420P: flags |= PP_FORMAT_420; break;
98  case AV_PIX_FMT_YUV422P: flags |= PP_FORMAT_422; break;
99  case AV_PIX_FMT_YUV411P: flags |= PP_FORMAT_411; break;
100  case AV_PIX_FMT_YUVJ444P:
101  case AV_PIX_FMT_YUV444P: flags |= PP_FORMAT_444; break;
102  default: av_assert0(0);
103  }
104 
105  pp->pp_ctx = pp_get_context(inlink->w, inlink->h, flags);
106  if (!pp->pp_ctx)
107  return AVERROR(ENOMEM);
108  return 0;
109 }
110 
111 static int pp_filter_frame(AVFilterLink *inlink, AVFrame *inbuf)
112 {
113  AVFilterContext *ctx = inlink->dst;
114  PPFilterContext *pp = ctx->priv;
115  AVFilterLink *outlink = ctx->outputs[0];
116  const int aligned_w = FFALIGN(outlink->w, 8);
117  const int aligned_h = FFALIGN(outlink->h, 8);
118  AVFrame *outbuf;
119  int qstride, qp_type;
120  int8_t *qp_table ;
121 
122  outbuf = ff_get_video_buffer(outlink, aligned_w, aligned_h);
123  if (!outbuf) {
124  av_frame_free(&inbuf);
125  return AVERROR(ENOMEM);
126  }
127  av_frame_copy_props(outbuf, inbuf);
128  outbuf->width = inbuf->width;
129  outbuf->height = inbuf->height;
130  qp_table = av_frame_get_qp_table(inbuf, &qstride, &qp_type);
131 
132  pp_postprocess((const uint8_t **)inbuf->data, inbuf->linesize,
133  outbuf->data, outbuf->linesize,
134  aligned_w, outlink->h,
135  qp_table,
136  qstride,
137  pp->modes[pp->mode_id],
138  pp->pp_ctx,
139  outbuf->pict_type | (qp_type ? PP_PICT_TYPE_QP2 : 0));
140 
141  av_frame_free(&inbuf);
142  return ff_filter_frame(outlink, outbuf);
143 }
144 
146 {
147  int i;
148  PPFilterContext *pp = ctx->priv;
149 
150  for (i = 0; i <= PP_QUALITY_MAX; i++)
151  pp_free_mode(pp->modes[i]);
152  if (pp->pp_ctx)
153  pp_free_context(pp->pp_ctx);
154 }
155 
156 static const AVFilterPad pp_inputs[] = {
157  {
158  .name = "default",
159  .type = AVMEDIA_TYPE_VIDEO,
160  .config_props = pp_config_props,
161  .filter_frame = pp_filter_frame,
162  },
163  { NULL }
164 };
165 
166 static const AVFilterPad pp_outputs[] = {
167  {
168  .name = "default",
169  .type = AVMEDIA_TYPE_VIDEO,
170  },
171  { NULL }
172 };
173 
175  .name = "pp",
176  .description = NULL_IF_CONFIG_SMALL("Filter video using libpostproc."),
177  .priv_size = sizeof(PPFilterContext),
178  .init = pp_init,
179  .uninit = pp_uninit,
181  .inputs = pp_inputs,
182  .outputs = pp_outputs,
184  .priv_class = &pp_class,
186 };
static av_cold void pp_uninit(AVFilterContext *ctx)
Definition: vf_pp.c:145
#define AVERROR_EXTERNAL
This structure describes decoded (raw) audio or video data.
Definition: frame.h:96
AVOption.
Definition: opt.h:253
const char * name
Filter name.
Definition: avfilter.h:468
void * priv
private data for use by the filter
Definition: avfilter.h:648
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:111
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
void pp_free_context(pp_context *ppContext)
Definition: postprocess.c:926
planar YUV 4:2:2, 16bpp, (1 Cr &amp; Cb sample per 2x1 Y samples)
Definition: avcodec.h:4538
static int query_formats(AVFilterContext *ctx)
Definition: af_aconvert.c:79
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:109
pp_context * pp_get_context(int width, int height, int flags)
Definition: postprocess.c:895
Pixel format.
Definition: avcodec.h:4533
#define av_cold
Definition: avcodec.h:653
#define PP_FORMAT_422
Definition: postprocess.h:96
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:294
#define AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
Some filters support a generic &quot;enable&quot; expression option that can be used to enable or disable a fil...
Definition: avfilter.h:445
const char * name
Pad name.
Definition: internal.h:66
static const AVFilterPad pp_outputs[]
Definition: vf_pp.c:166
static uint8_t * res
Definition: ffhash.c:43
static const AVOption pp_options[]
Definition: vf_pp.c:43
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1118
uint8_t
static av_cold int uninit(AVCodecContext *avctx)
Definition: crystalhd.c:334
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only &quot;metadata&quot; fields from src to dst.
Definition: frame.c:446
static av_cold int pp_init(AVFilterContext *ctx)
Definition: vf_pp.c:50
void 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:531
A filter pad used for either input or output.
Definition: internal.h:60
#define PP_QUALITY_MAX
Definition: postprocess.h:52
int width
width and height of the video frame
Definition: frame.h:145
void pp_mode
Definition: postprocess.h:59
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV444P and setting color_...
Definition: avcodec.h:4548
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:151
planar YUV 4:1:1, 12bpp, (1 Cr &amp; Cb sample per 4x1 Y samples)
Definition: avcodec.h:4541
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:123
#define PP_FORMAT_411
Definition: postprocess.h:97
int mode_id
Definition: vf_pp.c:36
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:664
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:167
#define PP_PICT_TYPE_QP2
MPEG2 style QScale.
Definition: postprocess.h:100
AVFilter avfilter_vf_pp
Definition: vf_pp.c:174
void pp_free_mode(pp_mode *mode)
Definition: postprocess.c:853
char * subfilters
Definition: vf_pp.c:35
static int process_command(AVFilterContext *ctx, const char *cmd, const char *arg, char *res, int res_len, int flags)
Definition: af_atempo.c:1156
static const AVFilterPad pp_inputs[]
Definition: vf_pp.c:156
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV420P and setting color_...
Definition: avcodec.h:4546
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:642
planar YUV 4:2:0, 12bpp, (1 Cr &amp; Cb sample per 2x2 Y samples)
Definition: avcodec.h:4534
Describe the class of an AVClass context structure.
Definition: log.h:50
Filter definition.
Definition: avfilter.h:464
static int pp_filter_frame(AVFilterLink *inlink, AVFrame *inbuf)
Definition: vf_pp.c:111
static const AVFilterPad inputs[]
Definition: af_ashowinfo.c:102
int8_t * av_frame_get_qp_table(AVFrame *f, int *stride, int *type)
Definition: frame.c:62
planar YUV 4:4:4, 24bpp, (1 Cr &amp; Cb sample per 1x1 Y samples)
Definition: avcodec.h:4539
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:124
#define PP_FORMAT_444
Definition: postprocess.h:98
static int flags
Definition: cpu.c:45
#define PixelFormat
Definition: avcodec.h:4990
#define PP_CPU_CAPS_AUTO
Definition: postprocess.h:92
void * pp_ctx
Definition: vf_pp.c:38
#define FLAGS
Definition: vf_pp.c:42
#define PP_FORMAT_420
Definition: postprocess.h:95
#define FFALIGN(x, a)
Definition: avcodec.h:930
static const SiprModeParam modes[MODE_COUNT]
Definition: sipr.c:69
static int pp_query_formats(AVFilterContext *ctx)
Definition: vf_pp.c:76
#define AVFILTER_DEFINE_CLASS(fname)
Definition: internal.h:301
#define AVERROR(e)
An instance of a filter.
Definition: avfilter.h:627
void pp_postprocess(const uint8_t *src[3], const int srcStride[3], uint8_t *dst[3], const int dstStride[3], int horizontalSize, int verticalSize, const QP_STORE_T *QP_store, int QP_stride, pp_mode *mode, pp_context *ppContext, int pict_type)
Definition: postprocess.c:947
static int pp_process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
Definition: vf_pp.c:64
int height
Definition: frame.h:145
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
internal API functions
pp_mode * modes[PP_QUALITY_MAX+1]
Definition: vf_pp.c:37
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV422P and setting color_...
Definition: avcodec.h:4547
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:107
#define OFFSET(x)
Definition: vf_pp.c:41
static int pp_config_props(AVFilterLink *inlink)
Definition: vf_pp.c:89