FFmpeg
vf_untile.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020 Nicolas George
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include "libavutil/imgutils.h"
22 #include "libavutil/opt.h"
23 #include "libavutil/pixdesc.h"
24 #include "avfilter.h"
25 #include "formats.h"
26 #include "filters.h"
27 #include "video.h"
28 
29 typedef struct UntileContext {
30  const AVClass *class;
31  unsigned w, h;
32  unsigned current;
33  unsigned nb_frames;
36  int64_t dpts, pts;
37  int max_step[4];
39 
40 #define OFFSET(x) offsetof(UntileContext, x)
41 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
42 
43 static const AVOption untile_options[] = {
44  { "layout", "set grid size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE,
45  {.str = "6x5"}, 0, 0, FLAGS },
46  { NULL }
47 };
48 
49 AVFILTER_DEFINE_CLASS(untile);
50 
52 {
53  UntileContext *s = ctx->priv;
54 
55  if (s->w > UINT_MAX / s->h) {
56  av_log(ctx, AV_LOG_ERROR, "Tile size %ux%u is insane.\n",
57  s->w, s->h);
58  return AVERROR(EINVAL);
59  }
60  s->nb_frames = s->w * s->h;
61  return 0;
62 }
63 
65 {
66  int reject_flags = AV_PIX_FMT_FLAG_HWACCEL |
69 
70  return ff_set_common_formats(ctx, ff_formats_pixdesc_filter(0, reject_flags));
71 }
72 
73 static int config_output(AVFilterLink *outlink)
74 {
75  AVFilterContext *ctx = outlink->src;
76  UntileContext *s = ctx->priv;
77  AVFilterLink *inlink = ctx->inputs[0];
78  AVRational dt;
79 
80  s->desc = av_pix_fmt_desc_get(outlink->format);
81  if (inlink->w % (s->w << s->desc->log2_chroma_w) ||
82  inlink->h % (s->h << s->desc->log2_chroma_h)) {
84  "Input resolution %ux%u not multiple of layout %ux%u.\n",
85  inlink->w, inlink->h, s->w, s->h);
86  return AVERROR(EINVAL);
87  }
88  outlink->w = inlink->w / s->w;
89  outlink->h = inlink->h / s->h;
90  outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
91  outlink->frame_rate = av_mul_q(inlink->frame_rate, av_make_q(s->nb_frames, 1));
92  if (outlink->frame_rate.num)
93  dt = av_inv_q(outlink->frame_rate);
94  else
95  dt = av_mul_q(inlink->time_base, av_make_q(1, s->nb_frames));
96  outlink->time_base = av_gcd_q(inlink->time_base, dt, AV_TIME_BASE / 2, AV_TIME_BASE_Q);
97  s->dpts = av_rescale_q(1, dt, outlink->time_base);
98  av_log(ctx, AV_LOG_VERBOSE, "frame interval: %"PRId64"*%d/%d\n",
99  s->dpts, dt.num, dt.den);
100  av_image_fill_max_pixsteps(s->max_step, NULL, s->desc);
101  return 0;
102 }
103 
105 {
106  UntileContext *s = ctx->priv;
107  AVFilterLink *inlink = ctx->inputs[0];
108  AVFilterLink *outlink = ctx->outputs[0];
109  AVFrame *out;
110  int i, x, y, ret;
111 
113  if (!s->frame) {
114  ret = ff_inlink_consume_frame(inlink, &s->frame);
115  if (ret < 0)
116  return ret;
117  if (ret)
118  s->pts = av_rescale_q(s->frame->pts, inlink->time_base, outlink->time_base);
119  }
120  if (s->frame) {
121  if (s->current == s->nb_frames - 1) {
122  out = s->frame;
123  s->frame = NULL;
124  } else {
125  out = av_frame_clone(s->frame);
126  if (!out)
127  return AVERROR(ENOMEM);
128  }
129  x = outlink->w * (s->current % s->w);
130  y = outlink->h * (s->current / s->w);
131  out->width = outlink->w;
132  out->height = outlink->h;
133  out->data[0] += y * out->linesize[0];
134  out->data[0] += x * s->max_step[0];
135  if (!(s->desc->flags & AV_PIX_FMT_FLAG_PAL)) {
136  for (i = 1; i < 3; i ++) {
137  if (out->data[i]) {
138  out->data[i] += (y >> s->desc->log2_chroma_h) * out->linesize[i];
139  out->data[i] += (x >> s->desc->log2_chroma_w) * s->max_step[i];
140  }
141  }
142  }
143  if (out->data[3]) {
144  out->data[3] += y * out->linesize[3];
145  out->data[3] += x * s->max_step[3];
146  }
147  out->pts = s->pts;
148  s->pts += s->dpts;
149  if (++s->current == s->nb_frames)
150  s->current = 0;
151  return ff_filter_frame(outlink, out);
152  }
155  return FFERROR_NOT_READY;
156 
157 }
158 
160 {
161  UntileContext *s = ctx->priv;
162 
163  av_frame_free(&s->frame);
164 }
165 
166 static const AVFilterPad untile_outputs[] = {
167  {
168  .name = "default",
169  .type = AVMEDIA_TYPE_VIDEO,
170  .config_props = config_output,
171  },
172 };
173 
175  .name = "untile",
176  .description = NULL_IF_CONFIG_SMALL("Untile a frame into a sequence of frames."),
177  .init = init,
178  .uninit = uninit,
179  .activate = activate,
180  .priv_size = sizeof(UntileContext),
184  .priv_class = &untile_class,
185 };
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
out
FILE * out
Definition: movenc.c:54
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:978
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2964
FFERROR_NOT_READY
return FFERROR_NOT_READY
Definition: filter_design.txt:204
AV_TIME_BASE_Q
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:264
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
UntileContext::current
unsigned current
Definition: vf_untile.c:32
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:100
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:340
pixdesc.h
w
uint8_t w
Definition: llviddspenc.c:38
AVOption
AVOption.
Definition: opt.h:251
FILTER_QUERY_FUNC
#define FILTER_QUERY_FUNC(func)
Definition: internal.h:169
FLAGS
#define FLAGS
Definition: vf_untile.c:41
ff_vf_untile
const AVFilter ff_vf_untile
Definition: vf_untile.c:174
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:196
UntileContext::dpts
int64_t dpts
Definition: vf_untile.c:36
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:170
video.h
FF_FILTER_FORWARD_STATUS_BACK
#define FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink)
Forward the status on an output link to an input link.
Definition: filters.h:199
formats.h
ff_inlink_consume_frame
int ff_inlink_consume_frame(AVFilterLink *link, AVFrame **rframe)
Take a frame from the link's FIFO and update the link's stats.
Definition: avfilter.c:1383
untile_outputs
static const AVFilterPad untile_outputs[]
Definition: vf_untile.c:166
AV_PIX_FMT_FLAG_HWACCEL
#define AV_PIX_FMT_FLAG_HWACCEL
Pixel format is an HW accelerated format.
Definition: pixdesc.h:128
init
static av_cold int init(AVFilterContext *ctx)
Definition: vf_untile.c:51
AVRational::num
int num
Numerator.
Definition: rational.h:59
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:47
UntileContext::max_step
int max_step[4]
Definition: vf_untile.c:37
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
ff_set_common_formats
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:770
UntileContext
Definition: vf_untile.c:29
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:36
s
#define s(width, name)
Definition: cbs_vp9.c:198
untile_options
static const AVOption untile_options[]
Definition: vf_untile.c:43
filters.h
ctx
AVFormatContext * ctx
Definition: movenc.c:48
av_frame_clone
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:609
av_rescale_q
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:192
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
UntileContext::desc
const AVPixFmtDescriptor * desc
Definition: vf_untile.c:35
AV_OPT_TYPE_IMAGE_SIZE
@ AV_OPT_TYPE_IMAGE_SIZE
offset must point to two consecutive integers
Definition: opt.h:235
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(untile)
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_untile.c:159
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:106
FF_PIX_FMT_FLAG_SW_FLAT_SUB
#define FF_PIX_FMT_FLAG_SW_FLAT_SUB
Definition: formats.h:245
activate
static int activate(AVFilterContext *ctx)
Definition: vf_untile.c:104
av_make_q
static AVRational av_make_q(int num, int den)
Create an AVRational.
Definition: rational.h:71
AV_PIX_FMT_FLAG_BITSTREAM
#define AV_PIX_FMT_FLAG_BITSTREAM
All values of a component are bit-wise packed end to end.
Definition: pixdesc.h:124
FF_FILTER_FORWARD_WANTED
FF_FILTER_FORWARD_WANTED(outlink, inlink)
config_output
static int config_output(AVFilterLink *outlink)
Definition: vf_untile.c:73
ff_formats_pixdesc_filter
AVFilterFormats * ff_formats_pixdesc_filter(unsigned want, unsigned rej)
Construct a formats list containing all pixel formats with certain properties.
Definition: formats.c:519
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:245
av_gcd_q
AVRational av_gcd_q(AVRational a, AVRational b, int max_den, AVRational def)
Return the best rational so that a and b are multiple of it.
Definition: rational.c:186
AV_TIME_BASE
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:254
UntileContext::h
unsigned h
Definition: vf_untile.c:31
av_inv_q
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition: rational.h:159
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:53
query_formats
static int query_formats(AVFilterContext *ctx)
Definition: vf_untile.c:64
AVFilter
Filter definition.
Definition: avfilter.h:166
ret
ret
Definition: filter_design.txt:187
OFFSET
#define OFFSET(x)
Definition: vf_untile.c:40
UntileContext::pts
int64_t pts
Definition: vf_untile.c:36
UntileContext::nb_frames
unsigned nb_frames
Definition: vf_untile.c:33
AVRational::den
int den
Denominator.
Definition: rational.h:60
avfilter.h
UntileContext::w
unsigned w
Definition: vf_untile.c:31
UntileContext::frame
AVFrame * frame
Definition: vf_untile.c:34
av_mul_q
AVRational av_mul_q(AVRational b, AVRational c)
Multiply two rationals.
Definition: rational.c:80
av_image_fill_max_pixsteps
void av_image_fill_max_pixsteps(int max_pixsteps[4], int max_pixstep_comps[4], const AVPixFmtDescriptor *pixdesc)
Compute the max pixel step for each plane of an image with a format described by pixdesc.
Definition: imgutils.c:35
AVFilterContext
An instance of a filter.
Definition: avfilter.h:397
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
FF_FILTER_FORWARD_STATUS
FF_FILTER_FORWARD_STATUS(inlink, outlink)
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:193
imgutils.h
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AV_PIX_FMT_FLAG_PAL
#define AV_PIX_FMT_FLAG_PAL
Pixel format has a palette in data[1], values are indexes in this palette.
Definition: pixdesc.h:120