FFmpeg
Loading...
Searching...
No Matches
resman.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2025 - softworkz
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/**
22 * @file
23 * output writers for filtergraph details
24 */
25
26#include "config.h"
27
28#include <string.h>
29
30#if CONFIG_RESOURCE_COMPRESSION
31#include <zlib.h>
32#endif
33
34#include "resman.h"
35#include "libavutil/avassert.h"
36#include "libavutil/pixdesc.h"
37#include "libavutil/dict.h"
38#include "libavutil/common.h"
39
40extern const unsigned char ff_graph_html_data[];
41extern const unsigned int ff_graph_html_len;
42
43extern const unsigned char ff_graph_css_data[];
44extern const unsigned ff_graph_css_len;
45
50
51
52static const AVClass resman_class = {
53 .class_name = "ResourceManager",
54};
55
60
62
64
65
66#if CONFIG_RESOURCE_COMPRESSION
67
68static int decompress_gzip(ResourceManagerContext *ctx, uint8_t *in, unsigned in_len, char **out, size_t *out_len)
69{
70 z_stream strm;
71 unsigned chunk = 65534;
72 int ret;
73 uint8_t *buf;
74
75 *out = NULL;
76 memset(&strm, 0, sizeof(strm));
77
78 // Allocate output buffer with extra byte for null termination
79 buf = (uint8_t *)av_mallocz(chunk + 1);
80 if (!buf) {
81 av_log(ctx, AV_LOG_ERROR, "Failed to allocate decompression buffer\n");
82 return AVERROR(ENOMEM);
83 }
84
85 // 15 + 16 tells zlib to detect GZIP or zlib automatically
86 ret = inflateInit2(&strm, 15 + 16);
87 if (ret != Z_OK) {
88 av_log(ctx, AV_LOG_ERROR, "Error during zlib initialization: %s\n", strm.msg);
89 av_free(buf);
90 return AVERROR(ENOSYS);
91 }
92
93 strm.avail_in = in_len;
94 strm.next_in = in;
95 strm.avail_out = chunk;
96 strm.next_out = buf;
97
98 ret = inflate(&strm, Z_FINISH);
99 if (ret != Z_OK && ret != Z_STREAM_END) {
100 av_log(ctx, AV_LOG_ERROR, "Inflate failed: %d, %s\n", ret, strm.msg);
101 inflateEnd(&strm);
102 av_free(buf);
103 return (ret == Z_STREAM_END) ? Z_OK : ((ret == Z_OK) ? Z_BUF_ERROR : ret);
104 }
105
106 if (strm.avail_out == 0) {
107 // TODO: Error or loop decoding?
108 av_log(ctx, AV_LOG_WARNING, "Decompression buffer may be too small\n");
109 }
110
111 *out_len = chunk - strm.avail_out;
112 buf[*out_len] = 0; // Ensure null termination
113
114 inflateEnd(&strm);
115 *out = (char *)buf;
116 return Z_OK;
117}
118#endif
119
121{
123
124 av_dict_free(&resman_ctx.resource_dic);
125
127}
128
129
131{
133 FFResourceDefinition resource_definition = { 0 };
134 AVDictionaryEntry *dic_entry;
135 char *res = NULL;
136
137 for (unsigned i = 0; i < FF_ARRAY_ELEMS(resource_definitions); ++i) {
139 if (def.resource_id == resource_id) {
140 resource_definition = def;
141 break;
142 }
143 }
144
145 av_assert1(resource_definition.name);
146
148
149 dic_entry = av_dict_get(ctx->resource_dic, resource_definition.name, NULL, 0);
150
151 if (!dic_entry) {
152 int dict_ret;
153
154#if CONFIG_RESOURCE_COMPRESSION
155
156 char *out = NULL;
157 size_t out_len;
158
159 int ret = decompress_gzip(ctx, (uint8_t *)resource_definition.data, *resource_definition.data_len, &out, &out_len);
160
161 if (ret) {
162 av_log(ctx, AV_LOG_ERROR, "Unable to decompress the resource with ID %d\n", resource_id);
163 goto end;
164 }
165
166 dict_ret = av_dict_set(&ctx->resource_dic, resource_definition.name, out, 0);
167 if (dict_ret < 0) {
168 av_log(ctx, AV_LOG_ERROR, "Failed to store decompressed resource in dictionary: %d\n", dict_ret);
169 av_freep(&out);
170 goto end;
171 }
172
173 av_freep(&out);
174#else
175
176 dict_ret = av_dict_set(&ctx->resource_dic, resource_definition.name, (const char *)resource_definition.data, 0);
177 if (dict_ret < 0) {
178 av_log(ctx, AV_LOG_ERROR, "Failed to store resource in dictionary: %d\n", dict_ret);
179 goto end;
180 }
181
182#endif
183 dic_entry = av_dict_get(ctx->resource_dic, resource_definition.name, NULL, 0);
184
185 if (!dic_entry) {
186 av_log(ctx, AV_LOG_ERROR, "Failed to retrieve resource from dictionary after storing it\n");
187 goto end;
188 }
189 }
190
191 res = dic_entry->value;
192
193end:
195 return res;
196}
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition avassert.h:58
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
common internal and external API header
#define NULL
Definition coverity.c:32
Public dictionary API.
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values.
Definition dict.c:233
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition dict.c:60
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition dict.c:86
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
#define AV_MUTEX_INITIALIZER
Definition thread.h:185
static int ff_mutex_unlock(AVMutex *mutex)
Definition thread.h:189
static int ff_mutex_lock(AVMutex *mutex)
Definition thread.h:188
#define AVMutex
Definition thread.h:184
const unsigned int ff_graph_html_len
static ResourceManagerContext resman_ctx
Definition resman.c:63
const unsigned char ff_graph_css_data[]
const unsigned ff_graph_css_len
const unsigned char ff_graph_html_data[]
static const FFResourceDefinition resource_definitions[]
Definition resman.c:46
char * ff_resman_get_string(FFResourceId resource_id)
Definition resman.c:130
static AVMutex mutex
Definition resman.c:61
static const AVClass resman_class
Definition resman.c:52
void ff_resman_uninit(void)
Definition resman.c:120
FFResourceId
Definition resman.h:32
@ FF_RESOURCE_GRAPH_CSS
Definition resman.h:33
@ FF_RESOURCE_GRAPH_HTML
Definition resman.h:34
#define FF_ARRAY_ELEMS(a)
Describe the class of an AVClass context structure.
Definition log.h:76
char * value
Definition dict.h:92
const char * name
Definition resman.h:39
FFResourceId resource_id
Definition resman.h:38
const unsigned * data_len
Definition resman.h:42
const unsigned char * data
Definition resman.h:41
AVDictionary * resource_dic
Definition resman.c:58
#define av_free(p)
#define av_mallocz(s)
#define av_freep(p)
#define av_log(a,...)
static FILE * out
Definition movenc.c:55
static AVFormatContext * ctx
Definition movenc.c:49
static void inflate(uint8_t *dst, const uint8_t *p1, int width, int threshold, const uint8_t *coordinates[], int coord, int maxc)