2 * AviSynth/AvxSynth support
3 * Copyright (c) 2012 AvxSynth Team
5 * This file is part of FFmpeg
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.
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.
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
21 #include "libavutil/internal.h"
22 #include "libavcodec/internal.h"
27 /* Enable function pointer definitions for runtime loading. */
28 #define AVSC_NO_DECLSPEC
30 /* Platform-specific directives for AviSynth vs AvxSynth. */
32 #include "compat/w32dlfcn.h"
34 #include "compat/avisynth/avisynth_c.h"
35 #define AVISYNTH_LIB "avisynth"
36 #define USING_AVISYNTH
39 #include "compat/avisynth/avxsynth_c.h"
40 #define AVISYNTH_NAME "libavxsynth"
41 #define AVISYNTH_LIB AVISYNTH_NAME SLIBSUF
44 typedef struct AviSynthLibrary {
46 #define AVSC_DECLARE_FUNC(name) name ## _func name
47 AVSC_DECLARE_FUNC(avs_bit_blt);
48 AVSC_DECLARE_FUNC(avs_clip_get_error);
49 AVSC_DECLARE_FUNC(avs_create_script_environment);
50 AVSC_DECLARE_FUNC(avs_delete_script_environment);
51 AVSC_DECLARE_FUNC(avs_get_audio);
52 AVSC_DECLARE_FUNC(avs_get_error);
53 AVSC_DECLARE_FUNC(avs_get_frame);
54 AVSC_DECLARE_FUNC(avs_get_version);
55 AVSC_DECLARE_FUNC(avs_get_video_info);
56 AVSC_DECLARE_FUNC(avs_invoke);
57 AVSC_DECLARE_FUNC(avs_release_clip);
58 AVSC_DECLARE_FUNC(avs_release_value);
59 AVSC_DECLARE_FUNC(avs_release_video_frame);
60 AVSC_DECLARE_FUNC(avs_take_clip);
62 AVSC_DECLARE_FUNC(avs_bits_per_pixel);
63 AVSC_DECLARE_FUNC(avs_get_height_p);
64 AVSC_DECLARE_FUNC(avs_get_pitch_p);
65 AVSC_DECLARE_FUNC(avs_get_read_ptr_p);
66 AVSC_DECLARE_FUNC(avs_get_row_size_p);
67 AVSC_DECLARE_FUNC(avs_is_planar_rgb);
68 AVSC_DECLARE_FUNC(avs_is_planar_rgba);
70 #undef AVSC_DECLARE_FUNC
73 typedef struct AviSynthContext {
74 AVS_ScriptEnvironment *env;
76 const AVS_VideoInfo *vi;
78 /* avisynth_read_packet_video() iterates over this. */
88 /* Linked list pointers. */
89 struct AviSynthContext *next;
92 static const int avs_planes_packed[1] = { 0 };
93 static const int avs_planes_grey[1] = { AVS_PLANAR_Y };
94 static const int avs_planes_yuv[3] = { AVS_PLANAR_Y, AVS_PLANAR_U,
97 static const int avs_planes_rgb[3] = { AVS_PLANAR_G, AVS_PLANAR_B,
99 static const int avs_planes_yuva[4] = { AVS_PLANAR_Y, AVS_PLANAR_U,
100 AVS_PLANAR_V, AVS_PLANAR_A };
101 static const int avs_planes_rgba[4] = { AVS_PLANAR_G, AVS_PLANAR_B,
102 AVS_PLANAR_R, AVS_PLANAR_A };
105 /* A conflict between C++ global objects, atexit, and dynamic loading requires
106 * us to register our own atexit handler to prevent double freeing. */
107 static AviSynthLibrary avs_library;
108 static int avs_atexit_called = 0;
110 /* Linked list of AviSynthContexts. An atexit handler destroys this list. */
111 static AviSynthContext *avs_ctx_list = NULL;
113 static av_cold void avisynth_atexit_handler(void);
115 static av_cold int avisynth_load_library(void)
117 avs_library.library = dlopen(AVISYNTH_LIB, RTLD_NOW | RTLD_LOCAL);
118 if (!avs_library.library)
119 return AVERROR_UNKNOWN;
121 #define LOAD_AVS_FUNC(name, continue_on_fail) \
123 (void *)dlsym(avs_library.library, #name); \
124 if (!continue_on_fail && !avs_library.name) \
127 LOAD_AVS_FUNC(avs_bit_blt, 0);
128 LOAD_AVS_FUNC(avs_clip_get_error, 0);
129 LOAD_AVS_FUNC(avs_create_script_environment, 0);
130 LOAD_AVS_FUNC(avs_delete_script_environment, 0);
131 LOAD_AVS_FUNC(avs_get_audio, 0);
132 LOAD_AVS_FUNC(avs_get_error, 1); // New to AviSynth 2.6
133 LOAD_AVS_FUNC(avs_get_frame, 0);
134 LOAD_AVS_FUNC(avs_get_version, 0);
135 LOAD_AVS_FUNC(avs_get_video_info, 0);
136 LOAD_AVS_FUNC(avs_invoke, 0);
137 LOAD_AVS_FUNC(avs_release_clip, 0);
138 LOAD_AVS_FUNC(avs_release_value, 0);
139 LOAD_AVS_FUNC(avs_release_video_frame, 0);
140 LOAD_AVS_FUNC(avs_take_clip, 0);
141 #ifdef USING_AVISYNTH
142 LOAD_AVS_FUNC(avs_bits_per_pixel, 1);
143 LOAD_AVS_FUNC(avs_get_height_p, 1);
144 LOAD_AVS_FUNC(avs_get_pitch_p, 1);
145 LOAD_AVS_FUNC(avs_get_read_ptr_p, 1);
146 LOAD_AVS_FUNC(avs_get_row_size_p, 1);
147 LOAD_AVS_FUNC(avs_is_planar_rgb, 1);
148 LOAD_AVS_FUNC(avs_is_planar_rgba, 1);
152 atexit(avisynth_atexit_handler);
156 dlclose(avs_library.library);
157 return AVERROR_UNKNOWN;
160 /* Note that avisynth_context_create and avisynth_context_destroy
161 * do not allocate or free the actual context! That is taken care of
163 static av_cold int avisynth_context_create(AVFormatContext *s)
165 AviSynthContext *avs = s->priv_data;
168 if (!avs_library.library)
169 if (ret = avisynth_load_library())
172 avs->env = avs_library.avs_create_script_environment(3);
173 if (avs_library.avs_get_error) {
174 const char *error = avs_library.avs_get_error(avs->env);
176 av_log(s, AV_LOG_ERROR, "%s\n", error);
177 return AVERROR_UNKNOWN;
184 avs->next = avs_ctx_list;
191 static av_cold void avisynth_context_destroy(AviSynthContext *avs)
193 if (avs_atexit_called)
196 if (avs == avs_ctx_list) {
197 avs_ctx_list = avs->next;
199 AviSynthContext *prev = avs_ctx_list;
200 while (prev->next != avs)
202 prev->next = avs->next;
206 avs_library.avs_release_clip(avs->clip);
210 avs_library.avs_delete_script_environment(avs->env);
215 static av_cold void avisynth_atexit_handler(void)
217 AviSynthContext *avs = avs_ctx_list;
220 AviSynthContext *next = avs->next;
221 avisynth_context_destroy(avs);
224 dlclose(avs_library.library);
226 avs_atexit_called = 1;
229 /* Create AVStream from audio and video data. */
230 static int avisynth_create_stream_video(AVFormatContext *s, AVStream *st)
232 AviSynthContext *avs = s->priv_data;
233 int planar = 0; // 0: packed, 1: YUV, 2: Y8, 3: Planar RGB, 4: YUVA, 5: Planar RGBA
235 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
236 st->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
237 st->codecpar->width = avs->vi->width;
238 st->codecpar->height = avs->vi->height;
240 st->avg_frame_rate = (AVRational) { avs->vi->fps_numerator,
241 avs->vi->fps_denominator };
243 st->duration = avs->vi->num_frames;
244 st->nb_frames = avs->vi->num_frames;
245 avpriv_set_pts_info(st, 32, avs->vi->fps_denominator, avs->vi->fps_numerator);
247 switch (avs->vi->pixel_type) {
248 #ifdef USING_AVISYNTH
249 /* 10~16-bit YUV pix_fmts (AviSynth+) */
250 case AVS_CS_YUV444P10:
251 st->codecpar->format = AV_PIX_FMT_YUV444P10;
254 case AVS_CS_YUV422P10:
255 st->codecpar->format = AV_PIX_FMT_YUV422P10;
258 case AVS_CS_YUV420P10:
259 st->codecpar->format = AV_PIX_FMT_YUV420P10;
262 case AVS_CS_YUV444P12:
263 st->codecpar->format = AV_PIX_FMT_YUV444P12;
266 case AVS_CS_YUV422P12:
267 st->codecpar->format = AV_PIX_FMT_YUV422P12;
270 case AVS_CS_YUV420P12:
271 st->codecpar->format = AV_PIX_FMT_YUV420P12;
274 case AVS_CS_YUV444P14:
275 st->codecpar->format = AV_PIX_FMT_YUV444P14;
278 case AVS_CS_YUV422P14:
279 st->codecpar->format = AV_PIX_FMT_YUV422P14;
282 case AVS_CS_YUV420P14:
283 st->codecpar->format = AV_PIX_FMT_YUV420P14;
286 case AVS_CS_YUV444P16:
287 st->codecpar->format = AV_PIX_FMT_YUV444P16;
290 case AVS_CS_YUV422P16:
291 st->codecpar->format = AV_PIX_FMT_YUV422P16;
294 case AVS_CS_YUV420P16:
295 st->codecpar->format = AV_PIX_FMT_YUV420P16;
298 /* 8~16-bit YUV pix_fmts with Alpha (AviSynth+) */
300 st->codecpar->format = AV_PIX_FMT_YUVA444P;
304 st->codecpar->format = AV_PIX_FMT_YUVA422P;
308 st->codecpar->format = AV_PIX_FMT_YUVA420P;
311 case AVS_CS_YUVA444P10:
312 st->codecpar->format = AV_PIX_FMT_YUVA444P10;
315 case AVS_CS_YUVA422P10:
316 st->codecpar->format = AV_PIX_FMT_YUVA422P10;
319 case AVS_CS_YUVA420P10:
320 st->codecpar->format = AV_PIX_FMT_YUVA420P10;
323 case AVS_CS_YUVA444P16:
324 st->codecpar->format = AV_PIX_FMT_YUVA444P16;
327 case AVS_CS_YUVA422P16:
328 st->codecpar->format = AV_PIX_FMT_YUVA422P16;
331 case AVS_CS_YUVA420P16:
332 st->codecpar->format = AV_PIX_FMT_YUVA420P16;
335 /* Planar RGB pix_fmts (AviSynth+) */
337 st->codecpar->format = AV_PIX_FMT_GBRP;
341 st->codecpar->format = AV_PIX_FMT_GBRP10;
345 st->codecpar->format = AV_PIX_FMT_GBRP12;
349 st->codecpar->format = AV_PIX_FMT_GBRP14;
353 st->codecpar->format = AV_PIX_FMT_GBRP16;
356 /* Planar RGB pix_fmts with Alpha (AviSynth+) */
358 st->codecpar->format = AV_PIX_FMT_GBRAP;
362 st->codecpar->format = AV_PIX_FMT_GBRAP10;
366 st->codecpar->format = AV_PIX_FMT_GBRAP12;
370 st->codecpar->format = AV_PIX_FMT_GBRAP16;
373 /* GRAY16 (AviSynth+) */
375 st->codecpar->format = AV_PIX_FMT_GRAY16;
378 /* pix_fmts added in AviSynth 2.6 */
380 st->codecpar->format = AV_PIX_FMT_YUV444P;
384 st->codecpar->format = AV_PIX_FMT_YUV422P;
388 st->codecpar->format = AV_PIX_FMT_YUV411P;
392 st->codecpar->format = AV_PIX_FMT_GRAY8;
395 /* 16-bit packed RGB pix_fmts (AviSynth+) */
397 st->codecpar->format = AV_PIX_FMT_BGR48;
400 st->codecpar->format = AV_PIX_FMT_BGRA64;
403 /* AviSynth 2.5 and AvxSynth pix_fmts */
405 st->codecpar->format = AV_PIX_FMT_BGR24;
408 st->codecpar->format = AV_PIX_FMT_RGB32;
411 st->codecpar->format = AV_PIX_FMT_YUYV422;
414 st->codecpar->format = AV_PIX_FMT_YUV420P;
417 case AVS_CS_I420: // Is this even used anywhere?
418 st->codecpar->format = AV_PIX_FMT_YUV420P;
422 av_log(s, AV_LOG_ERROR,
423 "unknown AviSynth colorspace %d\n", avs->vi->pixel_type);
425 return AVERROR_UNKNOWN;
429 #ifdef USING_AVISYNTH
430 case 5: // Planar RGB + Alpha
432 avs->planes = avs_planes_rgba;
434 case 4: // YUV + Alpha
436 avs->planes = avs_planes_yuva;
438 case 3: // Planar RGB
440 avs->planes = avs_planes_rgb;
445 avs->planes = avs_planes_grey;
449 avs->planes = avs_planes_yuv;
453 avs->planes = avs_planes_packed;
458 static int avisynth_create_stream_audio(AVFormatContext *s, AVStream *st)
460 AviSynthContext *avs = s->priv_data;
462 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
463 st->codecpar->sample_rate = avs->vi->audio_samples_per_second;
464 st->codecpar->channels = avs->vi->nchannels;
465 st->duration = avs->vi->num_audio_samples;
466 avpriv_set_pts_info(st, 64, 1, avs->vi->audio_samples_per_second);
468 switch (avs->vi->sample_type) {
469 case AVS_SAMPLE_INT8:
470 st->codecpar->codec_id = AV_CODEC_ID_PCM_U8;
472 case AVS_SAMPLE_INT16:
473 st->codecpar->codec_id = AV_CODEC_ID_PCM_S16LE;
475 case AVS_SAMPLE_INT24:
476 st->codecpar->codec_id = AV_CODEC_ID_PCM_S24LE;
478 case AVS_SAMPLE_INT32:
479 st->codecpar->codec_id = AV_CODEC_ID_PCM_S32LE;
481 case AVS_SAMPLE_FLOAT:
482 st->codecpar->codec_id = AV_CODEC_ID_PCM_F32LE;
485 av_log(s, AV_LOG_ERROR,
486 "unknown AviSynth sample type %d\n", avs->vi->sample_type);
488 return AVERROR_UNKNOWN;
493 static int avisynth_create_stream(AVFormatContext *s)
495 AviSynthContext *avs = s->priv_data;
500 if (avs_has_video(avs->vi)) {
501 st = avformat_new_stream(s, NULL);
503 return AVERROR_UNKNOWN;
505 if (ret = avisynth_create_stream_video(s, st))
508 if (avs_has_audio(avs->vi)) {
509 st = avformat_new_stream(s, NULL);
511 return AVERROR_UNKNOWN;
513 if (ret = avisynth_create_stream_audio(s, st))
519 static int avisynth_open_file(AVFormatContext *s)
521 AviSynthContext *avs = s->priv_data;
524 #ifdef USING_AVISYNTH
525 char filename_ansi[MAX_PATH * 4];
526 wchar_t filename_wc[MAX_PATH * 4];
529 if (ret = avisynth_context_create(s))
532 #ifdef USING_AVISYNTH
533 /* Convert UTF-8 to ANSI code page */
534 MultiByteToWideChar(CP_UTF8, 0, s->filename, -1, filename_wc, MAX_PATH * 4);
535 WideCharToMultiByte(CP_THREAD_ACP, 0, filename_wc, -1, filename_ansi,
536 MAX_PATH * 4, NULL, NULL);
537 arg = avs_new_value_string(filename_ansi);
539 arg = avs_new_value_string(s->filename);
541 val = avs_library.avs_invoke(avs->env, "Import", arg, 0);
542 if (avs_is_error(val)) {
543 av_log(s, AV_LOG_ERROR, "%s\n", avs_as_error(val));
544 ret = AVERROR_UNKNOWN;
547 if (!avs_is_clip(val)) {
548 av_log(s, AV_LOG_ERROR, "AviSynth script did not return a clip\n");
549 ret = AVERROR_UNKNOWN;
553 avs->clip = avs_library.avs_take_clip(val, avs->env);
554 avs->vi = avs_library.avs_get_video_info(avs->clip);
556 #ifdef USING_AVISYNTH
557 /* On Windows, FFmpeg supports AviSynth interface version 6 or higher.
558 * This includes AviSynth 2.6 RC1 or higher, and AviSynth+ r1718 or higher,
559 * and excludes 2.5 and the 2.6 alphas. Since AvxSynth identifies itself
560 * as interface version 3 like 2.5.8, this needs to be special-cased. */
562 if (avs_library.avs_get_version(avs->clip) < 6) {
563 av_log(s, AV_LOG_ERROR,
564 "AviSynth version is too old. Please upgrade to either AviSynth 2.6 >= RC1 or AviSynth+ >= r1718.\n");
565 ret = AVERROR_UNKNOWN;
570 /* Release the AVS_Value as it will go out of scope. */
571 avs_library.avs_release_value(val);
573 if (ret = avisynth_create_stream(s))
579 avisynth_context_destroy(avs);
583 static void avisynth_next_stream(AVFormatContext *s, AVStream **st,
584 AVPacket *pkt, int *discard)
586 AviSynthContext *avs = s->priv_data;
589 avs->curr_stream %= s->nb_streams;
591 *st = s->streams[avs->curr_stream];
592 if ((*st)->discard == AVDISCARD_ALL)
600 /* Copy AviSynth clip data into an AVPacket. */
601 static int avisynth_read_packet_video(AVFormatContext *s, AVPacket *pkt,
604 AviSynthContext *avs = s->priv_data;
605 AVS_VideoFrame *frame;
606 unsigned char *dst_p;
607 const unsigned char *src_p;
608 int n, i, plane, rowsize, planeheight, pitch, bits;
611 if (avs->curr_frame >= avs->vi->num_frames)
614 /* This must happen even if the stream is discarded to prevent desync. */
615 n = avs->curr_frame++;
619 #ifdef USING_AVISYNTH
620 /* Detect whether we're using AviSynth 2.6 or AviSynth+ by
621 * looking for whether avs_is_planar_rgb exists. */
625 if (GetProcAddress(avs_library.library, "avs_is_planar_rgb") == NULL)
630 /* avs_bits_per_pixel changed to AVSC_API with AviSynth 2.6, which
631 * requires going through avs_library, while AvxSynth has it under
632 * the older AVSC_INLINE type, so special-case this. */
634 bits = avs_library.avs_bits_per_pixel(avs->vi);
636 bits = avs_bits_per_pixel(avs->vi);
639 /* Without the cast to int64_t, calculation overflows at about 9k x 9k
641 pkt->size = (((int64_t)avs->vi->width *
642 (int64_t)avs->vi->height) * bits) / 8;
644 return AVERROR_UNKNOWN;
646 if (av_new_packet(pkt, pkt->size) < 0)
647 return AVERROR(ENOMEM);
652 pkt->stream_index = avs->curr_stream;
654 frame = avs_library.avs_get_frame(avs->clip, n);
655 error = avs_library.avs_clip_get_error(avs->clip);
657 av_log(s, AV_LOG_ERROR, "%s\n", error);
659 av_packet_unref(pkt);
660 return AVERROR_UNKNOWN;
664 for (i = 0; i < avs->n_planes; i++) {
665 plane = avs->planes[i];
666 #ifdef USING_AVISYNTH
667 src_p = avs_library.avs_get_read_ptr_p(frame, plane);
668 pitch = avs_library.avs_get_pitch_p(frame, plane);
670 rowsize = avs_library.avs_get_row_size_p(frame, plane);
671 planeheight = avs_library.avs_get_height_p(frame, plane);
673 src_p = avs_get_read_ptr_p(frame, plane);
674 pitch = avs_get_pitch_p(frame, plane);
676 rowsize = avs_get_row_size_p(frame, plane);
677 planeheight = avs_get_height_p(frame, plane);
680 /* Flip RGB video. */
681 if (avs_is_rgb24(avs->vi) || avs_is_rgb(avs->vi)) {
682 src_p = src_p + (planeheight - 1) * pitch;
686 #ifdef USING_AVISYNTH
687 /* Flip Planar RGB video. */
688 if (avsplus && (avs_library.avs_is_planar_rgb(avs->vi) ||
689 avs_library.avs_is_planar_rgba(avs->vi))) {
690 src_p = src_p + (planeheight - 1) * pitch;
695 avs_library.avs_bit_blt(avs->env, dst_p, rowsize, src_p, pitch,
696 rowsize, planeheight);
697 dst_p += rowsize * planeheight;
700 avs_library.avs_release_video_frame(frame);
704 static int avisynth_read_packet_audio(AVFormatContext *s, AVPacket *pkt,
707 AviSynthContext *avs = s->priv_data;
708 AVRational fps, samplerate;
713 if (avs->curr_sample >= avs->vi->num_audio_samples)
716 fps.num = avs->vi->fps_numerator;
717 fps.den = avs->vi->fps_denominator;
718 samplerate.num = avs->vi->audio_samples_per_second;
721 if (avs_has_video(avs->vi)) {
722 if (avs->curr_frame < avs->vi->num_frames)
723 samples = av_rescale_q(avs->curr_frame, samplerate, fps) -
726 samples = av_rescale_q(1, samplerate, fps);
731 /* After seeking, audio may catch up with video. */
738 if (avs->curr_sample + samples > avs->vi->num_audio_samples)
739 samples = avs->vi->num_audio_samples - avs->curr_sample;
741 /* This must happen even if the stream is discarded to prevent desync. */
742 n = avs->curr_sample;
743 avs->curr_sample += samples;
747 pkt->size = avs_bytes_per_channel_sample(avs->vi) *
748 samples * avs->vi->nchannels;
750 return AVERROR_UNKNOWN;
752 if (av_new_packet(pkt, pkt->size) < 0)
753 return AVERROR(ENOMEM);
757 pkt->duration = samples;
758 pkt->stream_index = avs->curr_stream;
760 avs_library.avs_get_audio(avs->clip, pkt->data, n, samples);
761 error = avs_library.avs_clip_get_error(avs->clip);
763 av_log(s, AV_LOG_ERROR, "%s\n", error);
765 av_packet_unref(pkt);
766 return AVERROR_UNKNOWN;
771 static av_cold int avisynth_read_header(AVFormatContext *s)
775 // Calling library must implement a lock for thread-safe opens.
776 if (ret = avpriv_lock_avformat())
779 if (ret = avisynth_open_file(s)) {
780 avpriv_unlock_avformat();
784 avpriv_unlock_avformat();
788 static int avisynth_read_packet(AVFormatContext *s, AVPacket *pkt)
790 AviSynthContext *avs = s->priv_data;
796 return AVERROR_UNKNOWN;
798 /* If either stream reaches EOF, try to read the other one before
800 avisynth_next_stream(s, &st, pkt, &discard);
801 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
802 ret = avisynth_read_packet_video(s, pkt, discard);
803 if (ret == AVERROR_EOF && avs_has_audio(avs->vi)) {
804 avisynth_next_stream(s, &st, pkt, &discard);
805 return avisynth_read_packet_audio(s, pkt, discard);
808 ret = avisynth_read_packet_audio(s, pkt, discard);
809 if (ret == AVERROR_EOF && avs_has_video(avs->vi)) {
810 avisynth_next_stream(s, &st, pkt, &discard);
811 return avisynth_read_packet_video(s, pkt, discard);
818 static av_cold int avisynth_read_close(AVFormatContext *s)
820 if (avpriv_lock_avformat())
821 return AVERROR_UNKNOWN;
823 avisynth_context_destroy(s->priv_data);
824 avpriv_unlock_avformat();
828 static int avisynth_read_seek(AVFormatContext *s, int stream_index,
829 int64_t timestamp, int flags)
831 AviSynthContext *avs = s->priv_data;
833 AVRational fps, samplerate;
836 return AVERROR_UNKNOWN;
838 fps = (AVRational) { avs->vi->fps_numerator,
839 avs->vi->fps_denominator };
840 samplerate = (AVRational) { avs->vi->audio_samples_per_second, 1 };
842 st = s->streams[stream_index];
843 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
844 /* AviSynth frame counts are signed int. */
845 if ((timestamp >= avs->vi->num_frames) ||
846 (timestamp > INT_MAX) ||
849 avs->curr_frame = timestamp;
850 if (avs_has_audio(avs->vi))
851 avs->curr_sample = av_rescale_q(timestamp, samplerate, fps);
853 if ((timestamp >= avs->vi->num_audio_samples) || (timestamp < 0))
855 /* Force frame granularity for seeking. */
856 if (avs_has_video(avs->vi)) {
857 avs->curr_frame = av_rescale_q(timestamp, fps, samplerate);
858 avs->curr_sample = av_rescale_q(avs->curr_frame, samplerate, fps);
860 avs->curr_sample = timestamp;
867 AVInputFormat ff_avisynth_demuxer = {
869 .long_name = NULL_IF_CONFIG_SMALL("AviSynth script"),
870 .priv_data_size = sizeof(AviSynthContext),
871 .read_header = avisynth_read_header,
872 .read_packet = avisynth_read_packet,
873 .read_close = avisynth_read_close,
874 .read_seek = avisynth_read_seek,