UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
change_pixfmt.c
Go to the documentation of this file.
1
5/*
6 * Copyright (c) 2020-2024 CESNET
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, is permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * 3. Neither the name of CESNET nor the names of its contributors may be
21 * used to endorse or promote products derived from this software without
22 * specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
26 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
27 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
28 * EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
29 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
34 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
35 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <stdbool.h> // for false, bool, true
39#include <stdint.h> // for uint32_t
40#include <stdio.h> // for printf
41#include <stdlib.h> // for free, NULL, calloc
42#include <string.h> // for memcpy, strcmp
43
44#include "capture_filter.h" // for CAPTURE_FILTER_AB...
45#include "debug.h" // for log_msg, LOG_LEVE...
46#include "lib_common.h" // for REGISTER_MODULE
47#include "pixfmt_conv.h" // for get_decoder_from_to
48#include "types.h" // for tile, video_frame
49#include "utils/color_out.h" // for color_printf, TER...
50#include "utils/macros.h" // for to_fourcc
51#include "video_codec.h" // for vc_get_linesize
52#include "video_frame.h" // for vf_alloc_desc
53#include "vo_postprocess.h" // for VO_PP_PROPERTY_CO...
54#include "vo_postprocess/capture_filter_wrapper.h" // for ADD_VO_PP_CAPTURE...
55struct module;
56
57#define MAGIC to_fourcc('C', 'F', 'C', 'P')
58#define MOD_NAME "[change pixfmt cap. f.] "
59
65
66static int init(struct module *parent, const char *cfg, void **state)
67{
69
70 if (strlen(cfg) == 0 || strcmp(cfg, "help") == 0) {
71 printf("Performs pixel format change transformation.\n\n"
72 "usage:\n");
73 color_printf(TERM_FG_RED "\t--capture-filter change_pixfmt:<name>\n" TERM_FG_RESET);
74 return 1;
75 }
76
78 s->magic = MAGIC;
80 if (!s->to_codec) {
81 log_msg(LOG_LEVEL_ERROR, MOD_NAME "Wrong codec: %s\n", cfg);
82 free(s);
83 return -1;
84 }
85
86 *state = s;
87 return 0;
88}
89
90static void done(void *state)
91{
92 free(state);
93}
94
95static struct video_frame *filter(void *state, struct video_frame *in)
96{
101
102 if (!decoder) {
103 log_msg(LOG_LEVEL_ERROR, MOD_NAME "Unable to find decoder!\n");
104 return NULL;
105 }
106 struct video_frame *out = vf_alloc_desc(desc);
107 if (s->vo_pp_out_buffer) {
108 out->tiles[0].data = s->vo_pp_out_buffer;
109 } else {
110 out->tiles[0].data = malloc(out->tiles[0].data_len + MAX_PADDING);
112 }
114
115 unsigned char *in_data = (unsigned char *) in->tiles[0].data;
116 unsigned char *out_data = (unsigned char *) out->tiles[0].data;
117 int src_linesize = vc_get_linesize(in->tiles[0].width, in->color_spec);
118 int dst_linesize = vc_get_linesize(in->tiles[0].width, s->to_codec);
119
120 for (unsigned int i = 0; i < in->tiles[0].height; i += 1) {
121 decoder(out_data, in_data, dst_linesize, DEFAULT_R_SHIFT, DEFAULT_G_SHIFT, DEFAULT_B_SHIFT);
122 in_data += src_linesize;
123 out_data += dst_linesize;
124 }
125
127
128 return out;
129}
130
131
132static void vo_pp_set_out_buffer(void *state, char *buffer)
133{
135 s->vo_pp_out_buffer = buffer;
136}
137
139 .init = init,
140 .done = done,
141 .filter = filter,
142};
143
144static bool
146{
148 return false;
149 }
150
152 const size_t alloc_len = *len;
153 *len = 0;
154 for (codec_t c = VC_FIRST; c < VC_END; ++c) {
155 decoder_t decoder = get_decoder_from_to(c, s->to_codec);
156 if (decoder == NULL) {
157 continue;
158 }
159 if (*len + sizeof c > alloc_len) {
160 MSG(ERROR, "Insufficient size %zd for get_property!\n",
161 alloc_len);
162 return false;
163 }
164 memcpy((char*) val + *len, &c, sizeof c);
165 *len += sizeof c;
166 }
167 return true;
168}
169
170// coverity[leaked_storage:SUPPRESS]
174
176
177
178/* vim: set expandtab sw=8: */
#define UNUSED(x)
void ** state
int void * val
struct video_desc desc
int void size_t * len
int property
void vf_free(struct video_frame *buf)
Frees video_frame structure.
struct video_desc video_desc_from_frame(const struct video_frame *frame)
Returns struct video_desc from video frame.
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.
codec_t get_codec_from_name(const char *name)
#define CAPTURE_FILTER_ABI_VERSION
#define ADD_VO_PP_CAPTURE_FILTER_WRAPPER(name, init, filter, done, set_out_buf, get_prop_callb)
static struct video_frame * filter(void *state, struct video_frame *in)
static void vo_pp_set_out_buffer(void *state, char *buffer)
static bool change_pixfmt_vo_pp_get_property(void *state, int property, void *val, size_t *len)
#define MAGIC
static const struct capture_filter_info capture_filter_change_pixfmt
#define MOD_NAME
static void done(void *state)
static int init(struct module *parent, const char *cfg, void **state)
int color_printf(const char *format,...)
#define TERM_FG_RED
Definition color_out.h:50
#define TERM_FG_RESET
Definition color_out.h:59
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
@ LIBRARY_CLASS_CAPTURE_FILTER
Definition lib_common.h:75
#define REGISTER_MODULE(name, info, lclass, abi)
Registers module to global modules' registry.
Definition lib_common.h:144
decoder_t get_decoder_from_to(codec_t in, codec_t out)
#define DEFAULT_B_SHIFT
Definition pixfmt_conv.h:72
#define DEFAULT_R_SHIFT
Definition pixfmt_conv.h:70
decoder_func_t * decoder_t
Definition pixfmt_conv.h:96
#define DEFAULT_G_SHIFT
Definition pixfmt_conv.h:71
const char * parent
Definition settings.cpp:198
void * vo_pp_out_buffer
buffer to write to if we use vo_pp wrapper (otherwise unused)
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
codec_t color_spec
Definition types.h:182
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
struct video_frame_callbacks callbacks
Definition types.h:324
codec_t color_spec
Definition types.h:306
codec_t
Definition types.h:65
@ VC_FIRST
Definition types.h:69
@ VC_END
Definition types.h:110
#define MAX_PADDING
Definition video_codec.h:63
static void change_pixfmt(AVFrame *frame, unsigned char *dst, av_to_uv_convert_t *convert, codec_t out_codec, int width, int height, int pitch, int rgb_shift[static restrict 3], struct state_libavcodec_decompress_sws *sws)
Definition libavcodec.c:823
#define VIDEO_FRAME_DISPOSE(frame)
Definition video_frame.h:76
#define VO_PP_PROPERTY_CODECS