UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
resize.c File Reference
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include "capture_filter.h"
#include "capture_filter/resize_utils.h"
#include "debug.h"
#include "lib_common.h"
#include "utils/color_out.h"
#include "utils/macros.h"
#include "utils/parallel_conv.h"
#include "video.h"
#include "video_codec.h"
#include "vo_postprocess/capture_filter_wrapper.h"
Include dependency graph for resize.c:

Go to the source code of this file.

Classes

struct  state_resize
 

Macros

#define MOD_NAME   "[resize filter] "
 

Functions

static void cleanup_common (struct state_resize *s)
 
static void done (void *state)
 
static struct video_frame * filter (void *state, struct video_frame *in)
 
static int init (struct module *parent, const char *cfg, void **state)
 
static int parse_fmt (char *cfg, struct resize_param *param)
 
static bool reconfigure_if_needed (struct state_resize *s, const struct video_frame *in)
 
 REGISTER_MODULE (resize, &capture_filter_resize, LIBRARY_CLASS_CAPTURE_FILTER, CAPTURE_FILTER_ABI_VERSION)
 
static void usage ()
 
static void vo_pp_set_out_buffer (void *state, char *buffer)
 

Variables

static const struct capture_filter_info capture_filter_resize
 

Detailed Description

Macro Definition Documentation

◆ MOD_NAME

#define MOD_NAME   "[resize filter] "

Definition at line 63 of file resize.c.

Function Documentation

◆ cleanup_common()

static void cleanup_common ( struct state_resize * s)
static

Definition at line 172 of file resize.c.

173{
174 vf_free(s->dec_frame);
175 s->dec_frame = NULL;
176}
void vf_free(struct video_frame *buf)
Frees video_frame structure.
struct video_frame * dec_frame
Definition resize.c:77

References state_resize::dec_frame, and vf_free().

Referenced by done(), and reconfigure_if_needed().

Here is the call graph for this function:

◆ done()

static void done ( void * state)
static

Definition at line 179 of file resize.c.

180{
182 free(state);
183}
void ** state
static void cleanup_common(struct state_resize *s)
Definition resize.c:172

References cleanup_common(), and state.

Here is the call graph for this function:

◆ filter()

static struct video_frame * filter ( void * state,
struct video_frame * in )
static

Definition at line 231 of file resize.c.

232{
233 struct state_resize *s = state;
234
235 if (!reconfigure_if_needed(s, in)) {
237 return NULL;
238 }
239
240 struct video_frame *out_frame = vf_alloc_desc(s->out_desc);
241 if (s->vo_pp_out_buffer) {
242 out_frame->tiles[0].data = s->vo_pp_out_buffer;
243 } else {
244 out_frame->tiles[0].data = (char *) malloc(out_frame->tiles[0].data_len);
246 }
247
248 for (unsigned int i = 0; i < out_frame->tile_count; i++) {
249 if (s->decoder != vc_memcpy) {
251 (int) in->tiles[i].height, s->dec_frame->tiles[i].data,
253 in->tiles[i].data,
255 0);
256 }
257 struct video_frame *const in_frame =
258 s->decoder == vc_memcpy ? in : s->dec_frame;
259
260 resize_frame(in_frame->tiles[i].data, in_frame->color_spec,
261 out_frame->tiles[i].data, (int) in_frame->tiles[i].width,
262 (int) in_frame->tiles[i].height, &s->param);
263 }
264
266
267 out_frame->callbacks.dispose = vf_free;
268
269 return out_frame;
270}
void vf_data_deleter(struct video_frame *buf)
int vc_get_linesize(unsigned int width, codec_t codec)
struct video_frame * vf_alloc_desc(struct video_desc desc)
Allocates video frame accordig given desc.
void parallel_pix_conv(int height, char *out, int out_linesize, const char *in, int in_linesize, decoder_t decode, int threads)
decoder_func_t vc_memcpy
dummy conversion - ptr to it returned if no conversion needed
static bool reconfigure_if_needed(struct state_resize *s, const struct video_frame *in)
Definition resize.c:186
void resize_frame(char *indata, codec_t in_color, char *outdata, int width, int height, struct resize_param *resize_spec)
decoder_t decoder
Definition resize.c:76
struct resize_param param
Definition resize.c:72
char * vo_pp_out_buffer
buffer to write to if we use vo_pp wrapper (otherwise unused)
Definition resize.c:75
struct video_desc out_desc
Definition resize.c:74
unsigned int width
Definition types.h:280
char * data
Raw tile data. Pointer must be at least 4B aligned.
Definition types.h:287
unsigned int data_len
length of the data
Definition types.h:288
unsigned int height
Definition types.h:281
void(* data_deleter)(struct video_frame *)
Definition types.h:256
void(* dispose)(struct video_frame *)
Definition types.h:245
Struct video_frame represents a video frame and contains video description.
Definition types.h:305
struct tile tiles[FLEXIBLE_ARRAY_MEMBER]
Definition types.h:345
unsigned int tile_count
Definition types.h:344
struct video_frame_callbacks callbacks
Definition types.h:324
codec_t color_spec
Definition types.h:306
#define VIDEO_FRAME_DISPOSE(frame)
Definition video_frame.h:76

References video_frame::callbacks, video_frame::color_spec, tile::data, video_frame_callbacks::data_deleter, tile::data_len, state_resize::dec_frame, state_resize::decoder, video_frame_callbacks::dispose, tile::height, state_resize::out_desc, parallel_pix_conv(), state_resize::param, reconfigure_if_needed(), resize_frame(), state, video_frame::tile_count, video_frame::tiles, vc_get_linesize(), vc_memcpy, vf_alloc_desc(), vf_data_deleter(), vf_free(), VIDEO_FRAME_DISPOSE, state_resize::vo_pp_out_buffer, and tile::width.

Here is the call graph for this function:

◆ init()

static int init ( struct module * parent,
const char * cfg,
void ** state )
static

Definition at line 147 of file resize.c.

148{
149 UNUSED(parent);
150 struct resize_param param = { .algo = RESIZE_ALGO_DFL };
151
152 if(strcasecmp(cfg, "help") == 0) {
153 usage();
154 return 1;
155 }
156
157 char *fmt = strdup(cfg);
158 const int rc = parse_fmt(fmt, &param);
159 free(fmt);
160 if (rc != 0) {
161 return rc;
162 }
163
164 struct state_resize *s = calloc(1, sizeof(struct state_resize));
165 s->param = param;
166
167 *state = s;
168 return 0;
169}
#define UNUSED(x)
const char * fmt
static int parse_fmt(char *cfg, struct resize_param *param)
Definition resize.c:101
static void usage()
Definition resize.c:80
#define RESIZE_ALGO_DFL
const char * parent
Definition settings.cpp:198
const char * param
Definition settings.cpp:195

References fmt, param, state_resize::param, parent, parse_fmt(), RESIZE_ALGO_DFL, state, UNUSED, and usage().

Here is the call graph for this function:

◆ parse_fmt()

static int parse_fmt ( char * cfg,
struct resize_param * param )
static

Definition at line 101 of file resize.c.

102{
103 char *save_ptr = NULL;
104 char *item = NULL;
105 while ((item = strtok_r(cfg, ":", &save_ptr))) {
106 cfg = NULL;
107 if (IS_KEY_PREFIX(item, "algorithm")) {
108 param->algo = resize_algo_from_string(strchr(item, '=') + 1);
109 if (param->algo < 0) {
110 return param->algo == RESIZE_ALGO_HELP_SHOWN ? 1 : -1;
111 }
112 continue;
113 }
114 if (!isdigit(item[0]) && item[0] != '.') {
116 "[RESIZE ERROR] Unrecognized part of config "
117 "string: %s\n",
118 item);
119 return -1;
120 }
121 if (strchr(item, 'x')) {
122 param->mode = USE_DIMENSIONS;
123 param->target_width = strtol(item, NULL, 10);
124 param->target_height = strtol(strchr(item, 'x') + 1, NULL, 10);
125 } else {
126 param->mode = USE_FRACTION;
127 param->factor = strtod(item, NULL);
128 if (strchr(item, '/')) {
129 const long den = strtol(strchr(item, '/') + 1, NULL, 10);
130 param->factor = den <= 0 ? .0 : param->factor / den;
131 }
132 }
133 }
134
135 if (param->mode == USE_DIMENSIONS && param->target_width > 0 &&
136 param->target_height > 0) {
137 return 0;
138 }
139 if (param->mode == USE_FRACTION && param->factor > 0) {
140 return 0;
141 }
142
143 MSG(ERROR, "No or incorrect resize size!\n");
144 return -1;
145}
void log_msg(int level, const char *format,...)
Definition debug.cpp:130
#define LOG_LEVEL_ERROR
general errors
Definition debug.h:59
#define MSG(l, fmt,...)
Definition debug.h:96
#define IS_KEY_PREFIX(tok, key)
expands to true value if <k> from tok in format <k>=<v> is prefix of key
Definition macros.h:138
int resize_algo_from_string(const char *str)
#define RESIZE_ALGO_HELP_SHOWN
Definition hd-rum.c:27

References IS_KEY_PREFIX, LOG_LEVEL_ERROR, log_msg(), MSG, param, resize_algo_from_string(), and RESIZE_ALGO_HELP_SHOWN.

Referenced by init().

Here is the call graph for this function:

◆ reconfigure_if_needed()

static bool reconfigure_if_needed ( struct state_resize * s,
const struct video_frame * in )
static

Definition at line 186 of file resize.c.

187{
189 return true;
190 }
191 struct video_desc dec_desc = video_desc_from_frame(in);
193 const codec_t supp_in_codecs[] = { RESIZE_SUPPORTED_PIXFMT_INIT,
195 if (codec_is_in_set(in->color_spec, supp_in_codecs)) {
196 dec_desc.color_spec = in->color_spec;
197 s->decoder = vc_memcpy;
198 } else {
199 s->decoder = get_best_decoder_from(in->color_spec, supp_in_codecs,
200 &dec_desc.color_spec);
201 if (s->decoder == NULL) {
202 MSG(ERROR,
203 "Cannot decode %s to neither of supported input formats!\n",
205 return false;
206 }
207 }
210 MSG(INFO, "Decoding through %s to output pixfmt %s.\n",
211 get_codec_name(dec_desc.color_spec),
213
214 if (s->param.mode == USE_DIMENSIONS) {
217 } else {
218 s->out_desc.width = in->tiles[0].width * s->param.factor;
219 s->out_desc.height = in->tiles[0].height * s->param.factor;
220 }
223 if (s->decoder != vc_memcpy) {
224 s->dec_frame = vf_alloc_desc_data(dec_desc);
225 }
226 MSG(NOTICE, "resizing from %dx%d to %dx%d\n", s->saved_desc.width,
228 return true;
229}
struct video_desc video_desc_from_frame(const struct video_frame *frame)
Returns struct video_desc from video frame.
struct video_frame * vf_alloc_desc_data(struct video_desc desc)
const char * get_codec_name(codec_t codec)
decoder_t get_best_decoder_from(codec_t in, const codec_t *out_candidates, codec_t *out)
#define RESIZE_SUPPORTED_PIXFMT_INIT
enum resize_param::resize_mode mode
struct video_desc saved_desc
Definition resize.c:73
codec_t color_spec
Definition types.h:182
unsigned int width
width of every tile (!)
Definition types.h:179
unsigned int height
height of every tile (!)
Definition types.h:180
@ DEPTH8
Definition types.h:126
codec_t
Definition types.h:65
@ RGB
RGB 8-bit (packed into 24-bit word)
Definition types.h:80
@ VIDEO_CODEC_NONE
dummy color spec
Definition types.h:66
@ RG48
16-bit RGB little-endian (RlRhGl...)
Definition types.h:95
bool codec_is_in_set(codec_t codec, const codec_t *set)
int get_bits_per_component(codec_t codec)
bool video_desc_eq(struct video_desc a, struct video_desc b)
Compares two video descriptions.

References cleanup_common(), codec_is_in_set(), video_desc::color_spec, video_frame::color_spec, state_resize::dec_frame, state_resize::decoder, DEPTH8, resize_param::factor, get_best_decoder_from(), get_bits_per_component(), get_codec_name(), tile::height, video_desc::height, resize_param::mode, MSG, state_resize::out_desc, state_resize::param, RESIZE_SUPPORTED_PIXFMT_INIT, RG48, RGB, state_resize::saved_desc, resize_param::target_height, resize_param::target_width, video_frame::tiles, vc_memcpy, vf_alloc_desc_data(), VIDEO_CODEC_NONE, video_desc_eq(), video_desc_from_frame(), tile::width, and video_desc::width.

Referenced by filter().

Here is the call graph for this function:

◆ REGISTER_MODULE()

REGISTER_MODULE ( resize ,
& capture_filter_resize,
LIBRARY_CLASS_CAPTURE_FILTER ,
CAPTURE_FILTER_ABI_VERSION  )

◆ usage()

static void usage ( void )
static

Definition at line 80 of file resize.c.

80 {
81 printf("Scaling by scale factor:\n\n");
82 printf("resize usage:\n");
83 color_printf("\t" TBOLD(TRED("resize") ":numerator[/denominator][algo=<a>]") "\n");
84 printf("or\n");
85 printf("\t" TBOLD(TRED("resize") ":<width>x<height>[algo=<a>]") "\n\n");
86 color_printf("Scaling examples:\n"
87 "\t" TBOLD("resize:1/2") " | " TBOLD("resize:0.5")
88 " - downscale input frame size by scale factor of 2\n"
89 "\t" TBOLD("resize:1280x720")
90 " - scales input to 1280x720\n"
91 "\t" TBOLD("resize:720x576")
92 " - scales input to PAL\n");
94 "\nOptions:\n"
95 "\t" TBOLD(
96 "algo") " - scaling algorithm to use (list with `algo:help`)\n");
97 color_printf("\n");
98}
int color_printf(const char *format,...)
#define TRED(x)
Definition color_out.h:75
#define TBOLD(x)
Definition color_out.h:70

References color_printf(), TBOLD, and TRED.

Referenced by init().

Here is the call graph for this function:

◆ vo_pp_set_out_buffer()

static void vo_pp_set_out_buffer ( void * state,
char * buffer )
static

Definition at line 272 of file resize.c.

273{
274 struct state_resize *s = state;
275 s->vo_pp_out_buffer = buffer;
276}

References state, and state_resize::vo_pp_out_buffer.

Variable Documentation

◆ capture_filter_resize

const struct capture_filter_info capture_filter_resize
static
Initial value:
= {
}
static struct video_frame * filter(void *state, struct video_frame *in)
Definition resize.c:231
static void done(void *state)
Definition resize.c:179
static int init(struct module *parent, const char *cfg, void **state)
Definition resize.c:147

Definition at line 278 of file resize.c.

278 {
279 init,
280 done,
281 filter,
282};