UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
logo.c File Reference
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "capture_filter.h"
#include "compat/strings.h"
#include "debug.h"
#include "lib_common.h"
#include "pixfmt_conv.h"
#include "types.h"
#include "utils/macros.h"
#include "utils/pam.h"
#include "video_codec.h"
#include "video_frame.h"
Include dependency graph for logo.c:

Go to the source code of this file.

Classes

struct  state_capture_filter_logo
 

Macros

#define MOD_NAME   "[logo] "
 

Functions

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 bool load_logo_data_from_file (struct state_capture_filter_logo *s, const char *filename)
 
 REGISTER_MODULE (logo, &capture_filter_logo, LIBRARY_CLASS_CAPTURE_FILTER, CAPTURE_FILTER_ABI_VERSION)
 

Variables

static const struct capture_filter_info capture_filter_logo
 

Detailed Description

Author
Martin Pulec pulec.nosp@m.@ces.nosp@m.net.c.nosp@m.z

Definition in file logo.c.

Macro Definition Documentation

◆ MOD_NAME

#define MOD_NAME   "[logo] "

Definition at line 58 of file logo.c.

Referenced by load_logo_data_from_file().

Function Documentation

◆ done()

static void done ( void * state)
static

Definition at line 151 of file logo.c.

152{
154 state;
155 free(s->logo);
156 free(s);
157}
void ** state
unsigned char * logo
Definition logo.c:61

References state_capture_filter_logo::logo, and state.

◆ filter()

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

Definition at line 159 of file logo.c.

160{
162 state;
163 decoder_t decoder, coder;
164 decoder = get_decoder_from_to(in->color_spec, RGB);
165 if (decoder == NULL) {
166 MSG(ERROR, "Cannot find decoder from %s to RGB!\n",
168 }
169 coder = get_decoder_from_to(RGB, in->color_spec);
170 if (coder == NULL) {
171 MSG(ERROR, "Cannot find encoder from %s to RGB!\n",
173 }
174 if (decoder == NULL || coder == NULL)
175 return in;
176
177 int rect_x = s->x;
178 int rect_y = s->y;
179
180 if (rect_x < 0 || rect_x + s->width > in->tiles[0].width) {
181 rect_x = in->tiles[0].width - s->width;
182 }
183 assert(get_pf_block_bytes(in->color_spec) > 0);
184 rect_x = (rect_x / get_pf_block_bytes(in->color_spec)) * get_pf_block_bytes(in->color_spec);
185
186 if (rect_y < 0 || rect_y + s->height > in->tiles[0].height) {
187 rect_y = in->tiles[0].height - s->height;
188 }
189
190 if (rect_x < 0 || rect_y < 0)
191 return in;
192
193 int dec_width = s->width;
194 dec_width = (dec_width + 1) / get_pf_block_bytes(in->color_spec) * get_pf_block_bytes(in->color_spec);
195 int linesize = dec_width * 3;
196
197 unsigned char *segment = (unsigned char *) malloc(linesize * s->height);
198
199 for (unsigned int y = 0; y < s->height; ++y) {
200 decoder(segment + y * linesize, (unsigned char *) in->tiles[0].data + (y + rect_y) *
202 vc_get_linesize(rect_x, in->color_spec), linesize,
203 0, 8, 16);
204 }
205
206 const unsigned char *overlay_data = s->logo;
207 for (unsigned int y = 0; y < s->height; ++y) {
208 unsigned char *image_data = segment + y * linesize;
209 for (unsigned int x = 0; x < s->width; ++x) {
210 int alpha = overlay_data[3];
211 for (int i = 0; i < 3; ++i) {
212 *image_data = (*image_data * (255 - alpha) + *overlay_data++ * alpha) / 255;
213 image_data++;
214 }
215 overlay_data++; // skip alpha
216 }
217 }
218
219 for (unsigned int y = 0; y < s->height; ++y) {
220 coder((unsigned char *) in->tiles[0].data + (rect_y + y) *
222 vc_get_linesize(rect_x, in->color_spec),
223 segment + y * linesize,
224 vc_get_linesize(s->width, in->color_spec), 0, 8, 16);
225 }
226
227 free(segment);
228
229 return in;
230}
int vc_get_linesize(unsigned int width, codec_t codec)
const char * get_codec_name(codec_t codec)
#define MSG(l, fmt,...)
Definition debug.h:96
decoder_t get_decoder_from_to(codec_t in, codec_t out)
decoder_func_t * decoder_t
Definition pixfmt_conv.h:96
unsigned int width
Definition logo.c:62
unsigned int height
Definition logo.c:63
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 height
Definition types.h:281
struct tile tiles[FLEXIBLE_ARRAY_MEMBER]
Definition types.h:345
codec_t color_spec
Definition types.h:306
@ RGB
RGB 8-bit (packed into 24-bit word)
Definition types.h:80
int get_pf_block_bytes(codec_t codec)
returns codec_info_t::block_size_bytes

References video_frame::color_spec, tile::data, get_codec_name(), get_decoder_from_to(), get_pf_block_bytes(), state_capture_filter_logo::height, tile::height, state_capture_filter_logo::logo, MSG, RGB, state, video_frame::tiles, vc_get_linesize(), state_capture_filter_logo::width, tile::width, state_capture_filter_logo::x, and state_capture_filter_logo::y.

Here is the call graph for this function:

◆ init()

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

Definition at line 106 of file logo.c.

107{
108 UNUSED(parent);
109 struct state_capture_filter_logo *s = calloc(1, sizeof *s);
110
111 s->x = s->y = -1;
112
113 if (strlen(cfg) == 0 || strcasecmp(cfg, "help") == 0) {
114 printf("Draws overlay logo over video:\n\n");
115 printf("'logo' usage:\n");
116 printf("\tlogo:<file>[:<x>[:<y>]]\n");
117 printf("\t\t<file> - is path to logo to be added in PAM format with alpha\n");
118 free(s);
119 return 1;
120 }
121 char *tmp = strdup(cfg);
122 char *save_ptr = NULL;
123 char *item;
124 if ((item = strtok_r(tmp, ":", &save_ptr)) == NULL) {
125 fprintf(stderr, "File name with logo required!\n");
126 goto error;
127 }
128
130 goto error;
131 }
132
133 if ((item = strtok_r(NULL, ":", &save_ptr))) {
134 s->x = atoi(item);
135 if ((item = strtok_r(NULL, ":", &save_ptr))) {
136 s->y = atoi(item);
137 }
138 }
139 free(tmp);
140 tmp = NULL;
141
142 *state = s;
143 return 0;
144error:
145 free(tmp);
146 free(s->logo);
147 free(s);
148 return -1;
149}
#define UNUSED(x)
static bool load_logo_data_from_file(struct state_capture_filter_logo *s, const char *filename)
Definition logo.c:72
const char * parent
Definition settings.cpp:198
Definition hd-rum.c:27

References load_logo_data_from_file(), state_capture_filter_logo::logo, parent, state, UNUSED, state_capture_filter_logo::x, and state_capture_filter_logo::y.

Here is the call graph for this function:

◆ load_logo_data_from_file()

static bool load_logo_data_from_file ( struct state_capture_filter_logo * s,
const char * filename )
static

Definition at line 72 of file logo.c.

72 {
73 if (strcasecmp(filename + (MAX(strlen(filename), 4) - 4), ".pam") == 0) {
74 bool rgb;
75 unsigned char *data;
76 struct pam_metadata info;
77 if (!pam_read(filename, &info, &data, malloc)) {
78 return false;
79 }
80 s->width = info.width;
81 s->height = info.height;
82 if (info.depth != 3 && info.depth != 4) {
83 log_msg(LOG_LEVEL_ERROR, MOD_NAME "Unsupported depth %d in PAM file.\n", info.depth);
84 free(data);
85 return false;
86 }
87 rgb = info.depth == 3;
88 int datalen = info.depth * s->width * s->height;
89 if (rgb) {
90 datalen = 4 * s->width * s->height;
91 unsigned char * tmp = malloc(datalen);
92 vc_copylineRGBtoRGBA(tmp, data, datalen, 0, 8, 16);
93 s->logo = tmp;
94 free(data);
95 } else {
96 s->logo = data;
97 }
98 } else {
99 log_msg(LOG_LEVEL_ERROR, MOD_NAME "Only logo in PAM format is currently supported.\n");
100 return false;
101 }
102
103 return true;
104}
void log_msg(int level, const char *format,...)
Definition debug.cpp:130
#define LOG_LEVEL_ERROR
general errors
Definition debug.h:59
#define MOD_NAME
Definition logo.c:58
#define MAX(a, b)
Definition macros.h:65
bool pam_read(const char *filename, struct pam_metadata *info, unsigned char **data, void *(*allocator)(size_t))
Definition pam.c:141
decoder_func_t vc_copylineRGBtoRGBA

References state_capture_filter_logo::height, LOG_LEVEL_ERROR, log_msg(), state_capture_filter_logo::logo, MAX, MOD_NAME, pam_read(), vc_copylineRGBtoRGBA, and state_capture_filter_logo::width.

Referenced by init().

Here is the call graph for this function:

◆ REGISTER_MODULE()

REGISTER_MODULE ( logo ,
& capture_filter_logo,
LIBRARY_CLASS_CAPTURE_FILTER ,
CAPTURE_FILTER_ABI_VERSION  )

Variable Documentation

◆ capture_filter_logo

const struct capture_filter_info capture_filter_logo
static
Initial value:
= {
}
static struct video_frame * filter(void *state, struct video_frame *in)
Definition logo.c:159
static void done(void *state)
Definition logo.c:151
static int init(struct module *parent, const char *cfg, void **state)
Definition logo.c:106

Definition at line 232 of file logo.c.

232 {
233 init,
234 done,
235 filter,
236};