FFmpeg: libavformat/vapoursynth.c Source File
Go to the documentation of this file. 1 /*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 /**
20 * @file
21 * VapourSynth demuxer
22 *
23 * Synthesizes vapour (?)
24 */
25
27
28 #include <vapoursynth/VSScript4.h>
29
41
42 /* Platform-specific directives. */
43 #ifdef _WIN32
44 #include <windows.h>
47 #undef EXTERN_C
48 #define VSSCRIPT_LIB "VSScript.dll"
49 #else
50 #include <dlfcn.h>
51 #define VSSCRIPT_NAME "libvapoursynth-script"
52 #define VSSCRIPT_LIB VSSCRIPT_NAME SLIBSUF
53 #endif
54
58 };
59
60 typedef const VSSCRIPTAPI *(*VSScriptGetAPIFunc)(
int version);
61
64
66
70
74
76
77 /* options */
80
81 #define OFFSET(x) offsetof(VSContext, x)
82 #define A AV_OPT_FLAG_AUDIO_PARAM
83 #define D AV_OPT_FLAG_DECODING_PARAM
85 {
"max_script_size",
"set max file size supported (in bytes)",
OFFSET(max_script_size),
AV_OPT_TYPE_INT64, {.i64 = 1 * 1024 * 1024}, 0, SIZE_MAX - 1,
A|
D},
87 };
88
90 {
91 void *vslibrary =
NULL;
92 #ifdef _WIN32
93 const HKEY hkeys[] = {HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE};
95 WCHAR vss_path[512];
96 DWORD buf_size = sizeof(vss_path) - 2;
97 char *vss_path_utf8;
99
101 if ((
r = RegGetValueW(hkeys[
i],
L "SOFTWARE\\VapourSynth",
102 L "VSScriptDLL", RRF_RT_REG_SZ,
NULL,
103 &vss_path, &buf_size)) == ERROR_SUCCESS)
104 break;
105 }
106 if (
r == ERROR_SUCCESS && wchartoutf8(vss_path, &vss_path_utf8) == 0) {
107 vslibrary = dlopen(vss_path_utf8, RTLD_NOW | RTLD_GLOBAL);
109 }
110 else
111 #endif
112 vslibrary = dlopen(
VSSCRIPT_LIB, RTLD_NOW | RTLD_GLOBAL);
113
114 if (vslibrary !=
NULL) {
116 dlclose(vslibrary);
118 }
119 }
120 return vslibrary;
121 }
122
124 {
126
128 vss->vssapi->freeScript(
vss->vss);
129 }
130 }
131
133 {
135
138
140
143
146
147 return 0;
148 }
149
151 {
155 return 1; // not affected by byte order
158 }
159
161 {
162 static const int yuv_order[4] = {0, 1, 2, 0};
163 static const int rgb_order[4] = {1, 2, 0, 0};
165
167 int is_rgb, is_yuv,
i;
168 const int *order;
170
172
176 continue;
177
180 continue;
181
182 is_rgb = vsf->colorFamily == cfRGB;
184 continue;
185
186 is_yuv = vsf->colorFamily == cfYUV ||
187 vsf->colorFamily == cfGray;
188 if (!is_rgb && !is_yuv)
189 continue;
190
192 continue;
193
195 continue;
196
198 continue;
199
200 order = is_yuv ? yuv_order : rgb_order;
201
204 if (order[
c->plane] !=
i ||
205 c->offset != 0 ||
c->shift != 0 ||
206 c->step != vsf->bytesPerSample ||
207 c->depth != vsf->bitsPerSample)
208 goto cont;
209 }
210
211 // Use it.
212 memcpy(c_order, order, sizeof(int[4]));
214
215 cont: ;
216 }
217
219 }
220
222 {
230 char vsfmt[32];
231 const VSVideoInfo *
info;
233 int err = 0;
234
237 "Check VapourSynth installation.\n");
239 goto done;
240 }
241
242 if (!(vs->
vssapi = get_vssapi(VSSCRIPT_API_VERSION))) {
245 goto done;
246 }
247
248 if (!(vs->
vsapi = vs->
vssapi->getVSAPI(VAPOURSYNTH_API_VERSION))) {
250 "Check VapourSynth installation.\n");
252 goto done;
253 }
254
256 if (!vss_state) {
258 goto done;
259 }
261
266 goto done;
267 }
268
272 goto done;
273 }
274
276 if (sz < 0)
279 }
280
282 if (!buf) {
284 goto done;
285 }
287
288 if (sz < 0) {
290 err = sz;
291 goto done;
292 }
293
294 // Data left means our buffer (the max_script_size option) is too small
297 "value %"PRIi64", consider increasing the max_script_size option\n",
300 goto done;
301 }
302
303 buf[sz] = '0円';
304 if (vs->
vssapi->evaluateBuffer(vss_state->
vss, buf,
s->url)) {
305 const char *msg = vs->
vssapi->getError(vss_state->
vss);
308 goto done;
309 }
310
315 goto done;
316 }
317
319 if (!st) {
321 goto done;
322 }
323
325
326 if (!
info->format.colorFamily || !
info->width || !
info->height) {
329 goto done;
330 }
331
336 } else {
337 // VFR. Just set "something".
340 }
341
347
350 vs->
vsapi->getVideoFormatName(&
info->format, vsfmt) ? vsfmt :
"(unknown)");
352 goto done;
353 }
355 vs->
vsapi->getVideoFormatName(&
info->format, vsfmt) ? vsfmt :
"(unknown)",
357
358 done:
360 return err;
361 }
362
364 {
366
368 }
369
371 {
374 int err = 1;
375
377 return err || res < INT_MIN || res > INT_MAX ? def : res;
378 }
379
384 };
385
387 {
389
392
394
396 }
397
399 {
403 char vserr[80];
404 const VSFrame *vsframe;
406 const VSMap *props;
410 int err = 0;
412
415
417 if (!ref_data) {
419 goto end;
420 }
421
422 // (the READONLY flag is important because the ref is reused for plane data)
424 if (!vsframe_ref) {
427 goto end;
428 }
429
431 if (!vsframe) {
434 goto end;
435 }
436
438 ref_data->
frame = vsframe;
439
443 goto end;
444 }
445
446 props = vs->
vsapi->getFramePropertiesRO(vsframe);
447
451 goto end;
452 }
453
458
459 // Values according to ISO/IEC 14496-10.
463
466
469
472
474
475 for (
i = 0;
i <
info->format.numPlanes;
i++) {
477 ptrdiff_t plane_h =
frame->height;
478
479 frame->data[
i] = (
void *)vs->
vsapi->getReadPtr(vsframe, p);
480 frame->linesize[
i] = vs->
vsapi->getStride(vsframe, p);
481
485 goto end;
486 }
487
488 // Each plane needs an AVBufferRef that indicates the correct plane
489 // memory range. VapourSynth doesn't even give us the memory range,
490 // so make up a bad guess to make FFmpeg happy (even if almost nothing
491 // checks the memory range).
492 if (
i == 1 ||
i == 2)
496 }
497
502 goto end;
503 }
504
506
510
513
515
516 end:
519 return err;
520 }
521
523 {
525
528
530 return 0;
531 }
532
534 {
535 // Explicitly do not support this. VS scripts are written in Python, and
536 // can run arbitrary code on the user's system.
537 return 0;
538 }
539
545 };
546
548 .
p.
name =
"vapoursynth",
558 };
enum AVPixelFormat av_pix_fmt_swap_endianness(enum AVPixelFormat pix_fmt)
Utility function to swap the endianness of a pixel format.
#define AV_LOG_WARNING
Something somehow does not look correct.
AVPixelFormat
Pixel format.
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default minimum maximum flags name is the option name
Filter the word "frame" indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
enum AVMediaType codec_type
General type of the encoded data.
enum AVColorSpace color_space
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
#define AVERROR_EOF
End of file.
uint8_t * data
The data buffer.
const FFInputFormat ff_vapoursynth_demuxer
#define AV_PIX_FMT_FLAG_FLOAT
The pixel format contains IEEE-754 floating point values.
const VSSCRIPTAPI * vssapi
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
This structure describes decoded (raw) audio or video data.
@ AVCOL_RANGE_JPEG
Full range content.
const AVPixFmtDescriptor * av_pix_fmt_desc_next(const AVPixFmtDescriptor *prev)
Iterate over all pixel format descriptors known to libavutil.
#define AV_LOG_VERBOSE
Detailed information.
static av_cold enum AVPixelFormat match_pixfmt(const VSVideoFormat *vsf, int c_order[4])
AVBufferRef * av_buffer_ref(const AVBufferRef *buf)
Create a new reference to an AVBuffer.
static int get_vs_prop_int(AVFormatContext *s, const VSMap *map, const char *name, int def)
int64_t avio_size(AVIOContext *s)
Get the filesize.
static void free_vss_state(void *opaque, uint8_t *data)
static av_cold void * vs_load_library(VSScriptGetAPIFunc *get_vssapi)
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
static const AVClass class_vs
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
#define AV_PIX_FMT_FLAG_HWACCEL
Pixel format is an HW accelerated format.
static av_cold int read_close(AVFormatContext *ctx)
#define AVERROR_BUFFER_TOO_SMALL
Buffer too small.
int64_t duration
Decoding: duration of the stream, in stream time base.
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
#define FF_ARRAY_ELEMS(a)
const VSSCRIPTAPI * vssapi
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
static const AVOption options[]
#define AV_CEIL_RSHIFT(a, b)
#define AV_BUFFER_FLAG_READONLY
Always treat the buffer as read-only, even when it has only one reference.
@ AV_OPT_TYPE_INT64
Underlying C type is int64_t.
#define av_assert0(cond)
assert() equivalent, that is always enabled.
static av_cold int read_header_vs(AVFormatContext *s)
#define AV_PIX_FMT_FLAG_ALPHA
The pixel format has an alpha channel.
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
@ AV_CODEC_ID_WRAPPED_AVFRAME
Passthrough codec, AVFrames wrapped in AVPacket.
#define FF_INFMT_FLAG_INIT_CLEANUP
For an FFInputFormat with this flag set read_close() needs to be called by the caller upon read_heade...
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
AVCodecParameters * codecpar
Codec parameters associated with this stream.
#define LIBAVUTIL_VERSION_INT
static int read_header(FFV1Context *f)
Describe the class of an AVClass context structure.
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it.
uint8_t nb_components
The number of components each pixel has, (1-4)
const char * av_default_item_name(void *ptr)
Return the context name.
This structure contains the data a format has to probe a file.
static int read_seek_vs(AVFormatContext *s, int stream_idx, int64_t ts, int flags)
uint64_t flags
Combination of AV_PIX_FMT_FLAG_...
static av_cold int is_native_endian(enum AVPixelFormat pixfmt)
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
AVBufferRef * av_buffer_create(uint8_t *data, size_t size, void(*free)(void *opaque, uint8_t *data), void *opaque, int flags)
Create an AVBuffer from an existing array.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
#define AV_PIX_FMT_FLAG_RGB
The pixel format contains RGB-like data (as opposed to YUV/grayscale).
#define AV_PIX_FMT_FLAG_BITSTREAM
All values of a component are bit-wise packed end to end.
static av_cold int read_close_vs(AVFormatContext *s)
#define AVERROR_EXTERNAL
Generic error in an external library.
enum AVPixelFormat av_pix_fmt_desc_get_id(const AVPixFmtDescriptor *desc)
int flags
A combination of AV_PKT_FLAG values.
size_t size
Size of data in bytes.
#define AV_PIX_FMT_FLAG_BAYER
The pixel format is following a Bayer pattern.
static void free_frame(void *opaque, uint8_t *data)
#define i(width, name, range_min, range_max)
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
static int read_packet_vs(AVFormatContext *s, AVPacket *pkt)
#define AV_TIME_BASE
Internal time base represented as integer.
#define AV_PIX_FMT_FLAG_BE
Pixel format is big-endian.
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
enum AVPixelFormat pixfmt
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
static av_cold int probe_vs(const AVProbeData *p)
#define AV_PIX_FMT_FLAG_XYZ
The pixel format contains XYZ-like data (as opposed to YUV/RGB/grayscale).
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
static int read_probe(const AVProbeData *p)
A reference to a data buffer.
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
const VDPAUPixFmtMap * map
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
This structure stores compressed data.
#define flags(name, subs,...)
#define AV_PKT_FLAG_TRUSTED
The packet comes from a trusted source.
const typedef VSSCRIPTAPI *(* VSScriptGetAPIFunc)(int version)
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
static void free_vsframe_ref(void *opaque, uint8_t *data)
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
Generated on Fri Aug 22 2025 13:59:37 for FFmpeg by
doxygen
1.8.17