FFmpeg
Loading...
Searching...
No Matches
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
29typedef struct UntileContext {
30 const AVClass *class;
31 unsigned w, h;
32 unsigned current;
33 unsigned nb_frames;
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
43static 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
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 AVFilterFormatsConfig **cfg_in,
66 AVFilterFormatsConfig **cfg_out)
67{
68 int reject_flags = AV_PIX_FMT_FLAG_HWACCEL |
71
72 return ff_set_common_formats2(ctx, cfg_in, cfg_out,
73 ff_formats_pixdesc_filter(0, reject_flags));
74}
75
76static int config_output(AVFilterLink *outlink)
77{
78 AVFilterContext *ctx = outlink->src;
79 UntileContext *s = ctx->priv;
80 AVFilterLink *inlink = ctx->inputs[0];
81 FilterLink *il = ff_filter_link(inlink);
82 FilterLink *ol = ff_filter_link(outlink);
83 AVRational dt;
84
85 s->desc = av_pix_fmt_desc_get(outlink->format);
86 if (inlink->w % (s->w << s->desc->log2_chroma_w) ||
87 inlink->h % (s->h << s->desc->log2_chroma_h)) {
89 "Input resolution %ux%u not multiple of layout %ux%u.\n",
90 inlink->w, inlink->h, s->w, s->h);
91 return AVERROR(EINVAL);
92 }
93 outlink->w = inlink->w / s->w;
94 outlink->h = inlink->h / s->h;
96 ol->frame_rate = av_mul_q(il->frame_rate, av_make_q(s->nb_frames, 1));
97 if (ol->frame_rate.num)
98 dt = av_inv_q(ol->frame_rate);
99 else
100 dt = av_mul_q(inlink->time_base, av_make_q(1, s->nb_frames));
101 outlink->time_base = av_gcd_q(inlink->time_base, dt, AV_TIME_BASE / 2, AV_TIME_BASE_Q);
102 s->dpts = av_rescale_q(1, dt, outlink->time_base);
103 av_log(ctx, AV_LOG_VERBOSE, "frame interval: %"PRId64"*%d/%d\n",
104 s->dpts, dt.num, dt.den);
105 av_image_fill_max_pixsteps(s->max_step, NULL, s->desc);
106 return 0;
107}
108
110{
111 UntileContext *s = ctx->priv;
112 AVFilterLink *inlink = ctx->inputs[0];
113 AVFilterLink *outlink = ctx->outputs[0];
114 AVFrame *out;
115 int i, x, y, ret;
116
117 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
118 if (!s->frame) {
119 ret = ff_inlink_consume_frame(inlink, &s->frame);
120 if (ret < 0)
121 return ret;
122 if (ret)
123 s->pts = av_rescale_q(s->frame->pts, inlink->time_base, outlink->time_base);
124 }
125 if (s->frame) {
126 if (s->current == s->nb_frames - 1) {
127 out = s->frame;
128 s->frame = NULL;
129 } else {
130 out = av_frame_clone(s->frame);
131 if (!out)
132 return AVERROR(ENOMEM);
133 }
134 x = outlink->w * (s->current % s->w);
135 y = outlink->h * (s->current / s->w);
136 out->width = outlink->w;
137 out->height = outlink->h;
138 out->data[0] += y * out->linesize[0];
139 out->data[0] += x * s->max_step[0];
140 if (!(s->desc->flags & AV_PIX_FMT_FLAG_PAL)) {
141 for (i = 1; i < 3; i ++) {
142 if (out->data[i]) {
143 out->data[i] += (y >> s->desc->log2_chroma_h) * out->linesize[i];
144 out->data[i] += (x >> s->desc->log2_chroma_w) * s->max_step[i];
145 }
146 }
147 }
148 if (out->data[3]) {
149 out->data[3] += y * out->linesize[3];
150 out->data[3] += x * s->max_step[3];
151 }
152 out->pts = s->pts;
153 s->pts += s->dpts;
154 if (++s->current == s->nb_frames)
155 s->current = 0;
156 return ff_filter_frame(outlink, out);
157 }
158 FF_FILTER_FORWARD_STATUS(inlink, outlink);
159 FF_FILTER_FORWARD_WANTED(outlink, inlink);
160 return FFERROR_NOT_READY;
161
162}
163
165{
166 UntileContext *s = ctx->priv;
167
168 av_frame_free(&s->frame);
169}
170
171static const AVFilterPad untile_outputs[] = {
172 {
173 .name = "default",
174 .type = AVMEDIA_TYPE_VIDEO,
175 .config_props = config_output,
176 },
177};
178
180 .p.name = "untile",
181 .p.description = NULL_IF_CONFIG_SMALL("Untile a frame into a sequence of frames."),
182 .p.priv_class = &untile_class,
183 .init = init,
184 .uninit = uninit,
185 .activate = activate,
186 .priv_size = sizeof(UntileContext),
190};
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
Definition aeval.c:246
const FFFilter ff_vf_untile
Definition vf_untile.c:179
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
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:1520
Main libavfilter public API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define FLAGS
Definition cmdutils.c:598
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
int ff_set_common_formats2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, AVFilterFormats *formats)
Definition formats.c:1137
AVFilterFormats * ff_formats_pixdesc_filter(unsigned want, unsigned rej)
Construct a formats list containing all pixel formats with certain properties.
Definition formats.c:620
#define FF_PIX_FMT_FLAG_SW_FLAT_SUB
Definition formats.h:463
@ AV_OPT_TYPE_IMAGE_SIZE
Underlying C type is two consecutive integers.
Definition opt.h:302
#define AVERROR(e)
Definition error.h:45
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition frame.c:483
#define AV_LOG_VERBOSE
Detailed information.
Definition log.h:226
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
AVRational av_mul_q(AVRational b, AVRational c)
Multiply two rationals.
Definition rational.c:80
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:188
static AVRational av_make_q(int num, int den)
Create an AVRational.
Definition rational.h:71
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition rational.h:159
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
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
#define AV_TIME_BASE
Internal time base represented as integer.
Definition avutil.h:253
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition avutil.h:263
misc image utilities
static av_cold void uninit(AVBitStreamFilterContext *ctx)
static int activate(AVBitStreamFilterContext *ctx)
static int config_output(AVBitStreamFilterLink *outlink)
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
#define FF_FILTER_FORWARD_WANTED(outlink, inlink)
Forward the frame_wanted_out flag from an output link to an input link.
Definition filters.h:694
#define FF_FILTER_FORWARD_STATUS(inlink, outlink)
Acknowledge the status on an input link and forward it to an output link.
Definition filters.h:666
#define FFERROR_NOT_READY
Filters implementation helper functions and internal structures.
Definition filters.h:34
#define FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink)
Forward the status on an output link to an input link.
Definition filters.h:639
static FilterLink * ff_filter_link(AVFilterLink *link)
Definition filters.h:199
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:478
#define FILTER_QUERY_FUNC2(func)
Definition filters.h:241
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
uint8_t w
Definition llvidencdsp.c:39
AVOptions.
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3460
#define AV_PIX_FMT_FLAG_BITSTREAM
All values of a component are bit-wise packed end to end.
Definition pixdesc.h:124
#define AV_PIX_FMT_FLAG_HWACCEL
Pixel format is an HW accelerated format.
Definition pixdesc.h:128
#define AV_PIX_FMT_FLAG_PAL
Pixel format has a palette in data[1], values are indexes in this palette.
Definition pixdesc.h:120
Describe the class of an AVClass context structure.
Definition log.h:76
An instance of a filter.
Definition avfilter.h:273
Lists of formats / etc.
Definition avfilter.h:120
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
AVOption.
Definition opt.h:428
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition pixdesc.h:69
Rational number (pair of numerator and denominator).
Definition rational.h:58
int num
Numerator.
Definition rational.h:59
int den
Denominator.
Definition rational.h:60
unsigned h
Definition vf_untile.c:31
const AVPixFmtDescriptor * desc
Definition vf_untile.c:35
int64_t dpts
Definition vf_untile.c:36
unsigned nb_frames
Definition vf_untile.c:33
AVFrame * frame
Definition vf_untile.c:34
int max_step[4]
Definition vf_untile.c:37
unsigned current
Definition vf_untile.c:32
int64_t pts
Definition vf_untile.c:36
unsigned w
Definition vf_untile.c:31
#define av_log(a,...)
static FILE * out
Definition movenc.c:55
static AVFormatContext * ctx
Definition movenc.c:49
static const AVOption untile_options[]
Definition vf_untile.c:43
static const AVFilterPad untile_outputs[]
Definition vf_untile.c:171
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
Definition vf_untile.c:64
static int activate(AVFilterContext *ctx)
Definition vf_untile.c:109
static av_cold void uninit(AVFilterContext *ctx)
Definition vf_untile.c:164
#define OFFSET(x)
Definition vf_untile.c:40
static int config_output(AVFilterLink *outlink)
Definition vf_untile.c:76
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