2 * AviSynth/AvxSynth support
3 * Copyright (c) 2012 AvxSynth Team
5 * This file is part of Libav.
7 * Libav is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * Libav is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavutil/attributes.h"
23 #include "libavutil/internal.h"
25 #include "libavcodec/internal.h"
31 /* Enable function pointer definitions for runtime loading. */
32 #define AVSC_NO_DECLSPEC
34 /* Platform-specific directives for AviSynth vs AvxSynth. */
38 #include <avisynth/avisynth_c.h>
39 #define AVISYNTH_LIB "avisynth"
42 #include <avxsynth/avxsynth_c.h>
43 #define AVISYNTH_NAME "libavxsynth"
44 #define AVISYNTH_LIB AVISYNTH_NAME SLIBSUF
46 #define LoadLibrary(x) dlopen(x, RTLD_NOW | RTLD_LOCAL)
47 #define GetProcAddress dlsym
48 #define FreeLibrary dlclose
51 typedef struct AviSynthLibrary {
53 #define AVSC_DECLARE_FUNC(name) name ## _func name
54 AVSC_DECLARE_FUNC(avs_bit_blt);
55 AVSC_DECLARE_FUNC(avs_clip_get_error);
56 AVSC_DECLARE_FUNC(avs_create_script_environment);
57 AVSC_DECLARE_FUNC(avs_delete_script_environment);
58 AVSC_DECLARE_FUNC(avs_get_audio);
59 AVSC_DECLARE_FUNC(avs_get_error);
60 AVSC_DECLARE_FUNC(avs_get_frame);
61 AVSC_DECLARE_FUNC(avs_get_version);
62 AVSC_DECLARE_FUNC(avs_get_video_info);
63 AVSC_DECLARE_FUNC(avs_invoke);
64 AVSC_DECLARE_FUNC(avs_release_clip);
65 AVSC_DECLARE_FUNC(avs_release_value);
66 AVSC_DECLARE_FUNC(avs_release_video_frame);
67 AVSC_DECLARE_FUNC(avs_take_clip);
69 AVSC_DECLARE_FUNC(avs_bits_per_pixel);
70 AVSC_DECLARE_FUNC(avs_get_height_p);
71 AVSC_DECLARE_FUNC(avs_get_pitch_p);
72 AVSC_DECLARE_FUNC(avs_get_read_ptr_p);
73 AVSC_DECLARE_FUNC(avs_get_row_size_p);
74 AVSC_DECLARE_FUNC(avs_is_planar_rgb);
75 AVSC_DECLARE_FUNC(avs_is_planar_rgba);
77 #undef AVSC_DECLARE_FUNC
80 typedef struct AviSynthContext {
81 AVS_ScriptEnvironment *env;
83 const AVS_VideoInfo *vi;
85 /* avisynth_read_packet_video() iterates over this. */
95 /* Linked list pointers. */
96 struct AviSynthContext *next;
99 static const int avs_planes_packed[1] = { 0 };
100 static const int avs_planes_grey[1] = { AVS_PLANAR_Y };
101 static const int avs_planes_yuv[3] = { AVS_PLANAR_Y, AVS_PLANAR_U,
103 #ifdef USING_AVISYNTH
104 static const int avs_planes_rgb[3] = { AVS_PLANAR_G, AVS_PLANAR_B,
106 static const int avs_planes_yuva[4] = { AVS_PLANAR_Y, AVS_PLANAR_U,
107 AVS_PLANAR_V, AVS_PLANAR_A };
108 static const int avs_planes_rgba[4] = { AVS_PLANAR_G, AVS_PLANAR_B,
109 AVS_PLANAR_R, AVS_PLANAR_A };
112 /* A conflict between C++ global objects, atexit, and dynamic loading requires
113 * us to register our own atexit handler to prevent double freeing. */
114 static AviSynthLibrary avs_library;
115 static int avs_atexit_called = 0;
117 /* Linked list of AviSynthContexts. An atexit handler destroys this list. */
118 static AviSynthContext *avs_ctx_list = NULL;
120 static av_cold void avisynth_atexit_handler(void);
122 static av_cold int avisynth_load_library(void)
124 avs_library.library = LoadLibrary(AVISYNTH_LIB);
125 if (!avs_library.library)
126 return AVERROR_UNKNOWN;
128 #define LOAD_AVS_FUNC(name, continue_on_fail) \
129 avs_library.name = (name ## _func) \
130 GetProcAddress(avs_library.library, #name); \
131 if (!continue_on_fail && !avs_library.name) \
134 LOAD_AVS_FUNC(avs_bit_blt, 0);
135 LOAD_AVS_FUNC(avs_clip_get_error, 0);
136 LOAD_AVS_FUNC(avs_create_script_environment, 0);
137 LOAD_AVS_FUNC(avs_delete_script_environment, 0);
138 LOAD_AVS_FUNC(avs_get_audio, 0);
139 LOAD_AVS_FUNC(avs_get_error, 1); // New to AviSynth 2.6
140 LOAD_AVS_FUNC(avs_get_frame, 0);
141 LOAD_AVS_FUNC(avs_get_version, 0);
142 LOAD_AVS_FUNC(avs_get_video_info, 0);
143 LOAD_AVS_FUNC(avs_invoke, 0);
144 LOAD_AVS_FUNC(avs_release_clip, 0);
145 LOAD_AVS_FUNC(avs_release_value, 0);
146 LOAD_AVS_FUNC(avs_release_video_frame, 0);
147 LOAD_AVS_FUNC(avs_take_clip, 0);
149 LOAD_AVS_FUNC(avs_bits_per_pixel, 1);
150 LOAD_AVS_FUNC(avs_get_height_p, 1);
151 LOAD_AVS_FUNC(avs_get_pitch_p, 1);
152 LOAD_AVS_FUNC(avs_get_read_ptr_p, 1);
153 LOAD_AVS_FUNC(avs_get_row_size_p, 1);
154 LOAD_AVS_FUNC(avs_is_planar_rgb, 1);
155 LOAD_AVS_FUNC(avs_is_planar_rgba, 1);
159 atexit(avisynth_atexit_handler);
163 FreeLibrary(avs_library.library);
164 return AVERROR_UNKNOWN;
167 /* Note that avisynth_context_create and avisynth_context_destroy
168 * do not allocate or free the actual context! That is taken care of
170 static av_cold int avisynth_context_create(AVFormatContext *s)
172 AviSynthContext *avs = s->priv_data;
175 if (!avs_library.library)
176 if (ret = avisynth_load_library())
179 avs->env = avs_library.avs_create_script_environment(3);
180 if (avs_library.avs_get_error) {
181 const char *error = avs_library.avs_get_error(avs->env);
183 av_log(s, AV_LOG_ERROR, "%s\n", error);
184 return AVERROR_UNKNOWN;
191 avs->next = avs_ctx_list;
198 static av_cold void avisynth_context_destroy(AviSynthContext *avs)
200 if (avs_atexit_called)
203 if (avs == avs_ctx_list) {
204 avs_ctx_list = avs->next;
206 AviSynthContext *prev = avs_ctx_list;
207 while (prev->next != avs)
209 prev->next = avs->next;
213 avs_library.avs_release_clip(avs->clip);
217 avs_library.avs_delete_script_environment(avs->env);
222 static av_cold void avisynth_atexit_handler(void)
224 AviSynthContext *avs = avs_ctx_list;
227 AviSynthContext *next = avs->next;
228 avisynth_context_destroy(avs);
231 FreeLibrary(avs_library.library);
233 avs_atexit_called = 1;
236 /* Create AVStream from audio and video data. */
237 static int avisynth_create_stream_video(AVFormatContext *s, AVStream *st)
239 AviSynthContext *avs = s->priv_data;
240 int planar = 0; // 0: packed, 1: YUV, 2: Y8, 3: Planar RGB, 4: YUVA, 5: Planar RGBA
242 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
243 st->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
244 st->codecpar->width = avs->vi->width;
245 st->codecpar->height = avs->vi->height;
247 st->avg_frame_rate = (AVRational) { avs->vi->fps_numerator,
248 avs->vi->fps_denominator };
250 st->duration = avs->vi->num_frames;
251 st->nb_frames = avs->vi->num_frames;
252 avpriv_set_pts_info(st, 32, avs->vi->fps_denominator, avs->vi->fps_numerator);
254 switch (avs->vi->pixel_type) {
256 /* 10~16-bit YUV pix_fmts (AviSynth+) */
257 case AVS_CS_YUV444P10:
258 st->codecpar->format = AV_PIX_FMT_YUV444P10;
261 case AVS_CS_YUV422P10:
262 st->codecpar->format = AV_PIX_FMT_YUV422P10;
265 case AVS_CS_YUV420P10:
266 st->codecpar->format = AV_PIX_FMT_YUV420P10;
269 case AVS_CS_YUV444P12:
270 st->codecpar->format = AV_PIX_FMT_YUV444P12;
273 case AVS_CS_YUV422P12:
274 st->codecpar->format = AV_PIX_FMT_YUV422P12;
277 case AVS_CS_YUV420P12:
278 st->codecpar->format = AV_PIX_FMT_YUV420P12;
281 case AVS_CS_YUV444P16:
282 st->codecpar->format = AV_PIX_FMT_YUV444P16;
285 case AVS_CS_YUV422P16:
286 st->codecpar->format = AV_PIX_FMT_YUV422P16;
289 case AVS_CS_YUV420P16:
290 st->codecpar->format = AV_PIX_FMT_YUV420P16;
293 /* 8~16-bit YUV pix_fmts with Alpha (AviSynth+) */
295 st->codecpar->format = AV_PIX_FMT_YUVA444P;
299 st->codecpar->format = AV_PIX_FMT_YUVA422P;
303 st->codecpar->format = AV_PIX_FMT_YUVA420P;
306 case AVS_CS_YUVA444P10:
307 st->codecpar->format = AV_PIX_FMT_YUVA444P10;
310 case AVS_CS_YUVA422P10:
311 st->codecpar->format = AV_PIX_FMT_YUVA422P10;
314 case AVS_CS_YUVA420P10:
315 st->codecpar->format = AV_PIX_FMT_YUVA420P10;
318 case AVS_CS_YUVA444P16:
319 st->codecpar->format = AV_PIX_FMT_YUVA444P16;
322 case AVS_CS_YUVA422P16:
323 st->codecpar->format = AV_PIX_FMT_YUVA422P16;
326 case AVS_CS_YUVA420P16:
327 st->codecpar->format = AV_PIX_FMT_YUVA420P16;
330 /* Planar RGB pix_fmts (AviSynth+) */
332 st->codecpar->format = AV_PIX_FMT_GBRP;
336 st->codecpar->format = AV_PIX_FMT_GBRP10;
340 st->codecpar->format = AV_PIX_FMT_GBRP12;
344 st->codecpar->format = AV_PIX_FMT_GBRP16;
347 /* Planar RGB pix_fmts with Alpha (AviSynth+) */
349 st->codecpar->format = AV_PIX_FMT_GBRAP;
353 st->codecpar->format = AV_PIX_FMT_GBRAP12;
357 st->codecpar->format = AV_PIX_FMT_GBRAP16;
360 /* GRAY16 (AviSynth+) */
362 st->codecpar->format = AV_PIX_FMT_GRAY16;
365 /* pix_fmts added in AviSynth 2.6 */
367 st->codecpar->format = AV_PIX_FMT_YUV444P;
371 st->codecpar->format = AV_PIX_FMT_YUV422P;
375 st->codecpar->format = AV_PIX_FMT_YUV411P;
379 st->codecpar->format = AV_PIX_FMT_GRAY8;
382 /* 16-bit packed RGB pix_fmts (AviSynth+) */
384 st->codecpar->format = AV_PIX_FMT_BGR48;
387 st->codecpar->format = AV_PIX_FMT_BGRA64;
390 /* AviSynth 2.5 and AvxSynth pix_fmts */
392 st->codecpar->format = AV_PIX_FMT_BGR24;
395 st->codecpar->format = AV_PIX_FMT_RGB32;
398 st->codecpar->format = AV_PIX_FMT_YUYV422;
401 st->codecpar->format = AV_PIX_FMT_YUV420P;
404 case AVS_CS_I420: // Is this even used anywhere?
405 st->codecpar->format = AV_PIX_FMT_YUV420P;
409 av_log(s, AV_LOG_ERROR,
410 "unknown AviSynth colorspace %d\n", avs->vi->pixel_type);
412 return AVERROR_UNKNOWN;
416 #ifdef USING_AVISYNTH
417 case 5: // Planar RGB + Alpha
419 avs->planes = avs_planes_rgba;
421 case 4: // YUV + Alpha
423 avs->planes = avs_planes_yuva;
425 case 3: // Planar RGB
427 avs->planes = avs_planes_rgb;
432 avs->planes = avs_planes_grey;
436 avs->planes = avs_planes_yuv;
440 avs->planes = avs_planes_packed;
445 static int avisynth_create_stream_audio(AVFormatContext *s, AVStream *st)
447 AviSynthContext *avs = s->priv_data;
449 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
450 st->codecpar->sample_rate = avs->vi->audio_samples_per_second;
451 st->codecpar->channels = avs->vi->nchannels;
452 st->duration = avs->vi->num_audio_samples;
453 avpriv_set_pts_info(st, 64, 1, avs->vi->audio_samples_per_second);
455 switch (avs->vi->sample_type) {
456 case AVS_SAMPLE_INT8:
457 st->codecpar->codec_id = AV_CODEC_ID_PCM_U8;
459 case AVS_SAMPLE_INT16:
460 st->codecpar->codec_id = AV_CODEC_ID_PCM_S16LE;
462 case AVS_SAMPLE_INT24:
463 st->codecpar->codec_id = AV_CODEC_ID_PCM_S24LE;
465 case AVS_SAMPLE_INT32:
466 st->codecpar->codec_id = AV_CODEC_ID_PCM_S32LE;
468 case AVS_SAMPLE_FLOAT:
469 st->codecpar->codec_id = AV_CODEC_ID_PCM_F32LE;
472 av_log(s, AV_LOG_ERROR,
473 "unknown AviSynth sample type %d\n", avs->vi->sample_type);
475 return AVERROR_UNKNOWN;
480 static int avisynth_create_stream(AVFormatContext *s)
482 AviSynthContext *avs = s->priv_data;
487 if (avs_has_video(avs->vi)) {
488 st = avformat_new_stream(s, NULL);
490 return AVERROR_UNKNOWN;
492 if (ret = avisynth_create_stream_video(s, st))
495 if (avs_has_audio(avs->vi)) {
496 st = avformat_new_stream(s, NULL);
498 return AVERROR_UNKNOWN;
500 if (ret = avisynth_create_stream_audio(s, st))
506 static int avisynth_open_file(AVFormatContext *s)
508 AviSynthContext *avs = s->priv_data;
512 char filename_ansi[MAX_PATH * 4];
513 wchar_t filename_wc[MAX_PATH * 4];
516 if (ret = avisynth_context_create(s))
520 /* Convert UTF-8 to ANSI code page */
521 MultiByteToWideChar(CP_UTF8, 0, s->filename, -1, filename_wc, MAX_PATH * 4);
522 WideCharToMultiByte(CP_THREAD_ACP, 0, filename_wc, -1, filename_ansi,
523 MAX_PATH * 4, NULL, NULL);
524 arg = avs_new_value_string(filename_ansi);
526 arg = avs_new_value_string(s->filename);
528 val = avs_library.avs_invoke(avs->env, "Import", arg, 0);
529 if (avs_is_error(val)) {
530 av_log(s, AV_LOG_ERROR, "%s\n", avs_as_error(val));
531 ret = AVERROR_UNKNOWN;
534 if (!avs_is_clip(val)) {
535 av_log(s, AV_LOG_ERROR, "AviSynth script did not return a clip\n");
536 ret = AVERROR_UNKNOWN;
540 avs->clip = avs_library.avs_take_clip(val, avs->env);
541 avs->vi = avs_library.avs_get_video_info(avs->clip);
544 /* On Windows, libav supports AviSynth interface version 6 or higher.
545 * This includes AviSynth 2.6 RC1 or higher, and AviSynth+ r1718 or higher,
546 * and excludes 2.5 and the 2.6 alphas. Since AvxSynth identifies itself
547 * as interface version 3 like 2.5.8, this needs to be special-cased. */
549 if (avs_library.avs_get_version(avs->clip) < 6) {
550 av_log(s, AV_LOG_ERROR,
551 "AviSynth version is too old. Please upgrade to either AviSynth 2.6 >= RC1 or AviSynth+ >= r1718.\n");
552 ret = AVERROR_UNKNOWN;
557 /* Release the AVS_Value as it will go out of scope. */
558 avs_library.avs_release_value(val);
560 if (ret = avisynth_create_stream(s))
566 avisynth_context_destroy(avs);
570 static void avisynth_next_stream(AVFormatContext *s, AVStream **st,
571 AVPacket *pkt, int *discard)
573 AviSynthContext *avs = s->priv_data;
576 avs->curr_stream %= s->nb_streams;
578 *st = s->streams[avs->curr_stream];
579 if ((*st)->discard == AVDISCARD_ALL)
587 /* Copy AviSynth clip data into an AVPacket. */
588 static int avisynth_read_packet_video(AVFormatContext *s, AVPacket *pkt,
591 AviSynthContext *avs = s->priv_data;
592 AVS_VideoFrame *frame;
593 unsigned char *dst_p;
594 const unsigned char *src_p;
595 int n, i, plane, rowsize, planeheight, pitch, bits;
597 int avsplus av_unused;
599 if (avs->curr_frame >= avs->vi->num_frames)
602 /* This must happen even if the stream is discarded to prevent desync. */
603 n = avs->curr_frame++;
608 /* Detect whether we're using AviSynth 2.6 or AviSynth+ by
609 * looking for whether avs_is_planar_rgb exists. */
610 if (GetProcAddress(avs_library.library, "avs_is_planar_rgb") == NULL)
615 /* avs_bits_per_pixel changed to AVSC_API with AviSynth 2.6, which
616 * requires going through avs_library, while AvxSynth has it under
617 * the older AVSC_INLINE type, so special-case this. */
619 bits = avs_library.avs_bits_per_pixel(avs->vi);
621 bits = avs_bits_per_pixel(avs->vi);
624 /* Without the cast to int64_t, calculation overflows at about 9k x 9k
626 pkt->size = (((int64_t)avs->vi->width *
627 (int64_t)avs->vi->height) * bits) / 8;
629 return AVERROR_UNKNOWN;
631 if (av_new_packet(pkt, pkt->size) < 0)
632 return AVERROR(ENOMEM);
637 pkt->stream_index = avs->curr_stream;
639 frame = avs_library.avs_get_frame(avs->clip, n);
640 error = avs_library.avs_clip_get_error(avs->clip);
642 av_log(s, AV_LOG_ERROR, "%s\n", error);
644 av_packet_unref(pkt);
645 return AVERROR_UNKNOWN;
649 for (i = 0; i < avs->n_planes; i++) {
650 plane = avs->planes[i];
652 src_p = avs_library.avs_get_read_ptr_p(frame, plane);
653 pitch = avs_library.avs_get_pitch_p(frame, plane);
655 rowsize = avs_library.avs_get_row_size_p(frame, plane);
656 planeheight = avs_library.avs_get_height_p(frame, plane);
658 src_p = avs_get_read_ptr_p(frame, plane);
659 pitch = avs_get_pitch_p(frame, plane);
661 rowsize = avs_get_row_size_p(frame, plane);
662 planeheight = avs_get_height_p(frame, plane);
665 /* Flip RGB video. */
666 if (avs_is_rgb24(avs->vi) || avs_is_rgb(avs->vi)) {
667 src_p = src_p + (planeheight - 1) * pitch;
671 #ifdef USING_AVISYNTH
672 /* Flip Planar RGB video */
673 if (avsplus && (avs_library.avs_is_planar_rgb(avs->vi) ||
674 avs_library.avs_is_planar_rgba(avs->vi))) {
675 src_p = src_p + (planeheight - 1) * pitch;
680 avs_library.avs_bit_blt(avs->env, dst_p, rowsize, src_p, pitch,
681 rowsize, planeheight);
682 dst_p += rowsize * planeheight;
685 avs_library.avs_release_video_frame(frame);
689 static int avisynth_read_packet_audio(AVFormatContext *s, AVPacket *pkt,
692 AviSynthContext *avs = s->priv_data;
693 AVRational fps, samplerate;
698 if (avs->curr_sample >= avs->vi->num_audio_samples)
701 fps.num = avs->vi->fps_numerator;
702 fps.den = avs->vi->fps_denominator;
703 samplerate.num = avs->vi->audio_samples_per_second;
706 if (avs_has_video(avs->vi)) {
707 if (avs->curr_frame < avs->vi->num_frames)
708 samples = av_rescale_q(avs->curr_frame, samplerate, fps) -
711 samples = av_rescale_q(1, samplerate, fps);
716 /* After seeking, audio may catch up with video. */
723 if (avs->curr_sample + samples > avs->vi->num_audio_samples)
724 samples = avs->vi->num_audio_samples - avs->curr_sample;
726 /* This must happen even if the stream is discarded to prevent desync. */
727 n = avs->curr_sample;
728 avs->curr_sample += samples;
732 pkt->size = avs_bytes_per_channel_sample(avs->vi) *
733 samples * avs->vi->nchannels;
735 return AVERROR_UNKNOWN;
737 if (av_new_packet(pkt, pkt->size) < 0)
738 return AVERROR(ENOMEM);
742 pkt->duration = samples;
743 pkt->stream_index = avs->curr_stream;
745 avs_library.avs_get_audio(avs->clip, pkt->data, n, samples);
746 error = avs_library.avs_clip_get_error(avs->clip);
748 av_log(s, AV_LOG_ERROR, "%s\n", error);
750 av_packet_unref(pkt);
751 return AVERROR_UNKNOWN;
756 static av_cold int avisynth_read_header(AVFormatContext *s)
760 // Calling library must implement a lock for thread-safe opens.
761 if (ret = avpriv_lock_avformat())
764 if (ret = avisynth_open_file(s)) {
765 avpriv_unlock_avformat();
769 avpriv_unlock_avformat();
773 static int avisynth_read_packet(AVFormatContext *s, AVPacket *pkt)
775 AviSynthContext *avs = s->priv_data;
781 return AVERROR_UNKNOWN;
783 /* If either stream reaches EOF, try to read the other one before
785 avisynth_next_stream(s, &st, pkt, &discard);
786 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
787 ret = avisynth_read_packet_video(s, pkt, discard);
788 if (ret == AVERROR_EOF && avs_has_audio(avs->vi)) {
789 avisynth_next_stream(s, &st, pkt, &discard);
790 return avisynth_read_packet_audio(s, pkt, discard);
793 ret = avisynth_read_packet_audio(s, pkt, discard);
794 if (ret == AVERROR_EOF && avs_has_video(avs->vi)) {
795 avisynth_next_stream(s, &st, pkt, &discard);
796 return avisynth_read_packet_video(s, pkt, discard);
803 static av_cold int avisynth_read_close(AVFormatContext *s)
805 if (avpriv_lock_avformat())
806 return AVERROR_UNKNOWN;
808 avisynth_context_destroy(s->priv_data);
809 avpriv_unlock_avformat();
813 static int avisynth_read_seek(AVFormatContext *s, int stream_index,
814 int64_t timestamp, int flags)
816 AviSynthContext *avs = s->priv_data;
818 AVRational fps, samplerate;
821 return AVERROR_UNKNOWN;
823 fps = (AVRational) { avs->vi->fps_numerator,
824 avs->vi->fps_denominator };
825 samplerate = (AVRational) { avs->vi->audio_samples_per_second, 1 };
827 st = s->streams[stream_index];
828 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
829 /* AviSynth frame counts are signed int. */
830 if ((timestamp >= avs->vi->num_frames) ||
831 (timestamp > INT_MAX) ||
834 avs->curr_frame = timestamp;
835 if (avs_has_audio(avs->vi))
836 avs->curr_sample = av_rescale_q(timestamp, samplerate, fps);
838 if ((timestamp >= avs->vi->num_audio_samples) || (timestamp < 0))
840 /* Force frame granularity for seeking. */
841 if (avs_has_video(avs->vi)) {
842 avs->curr_frame = av_rescale_q(timestamp, fps, samplerate);
843 avs->curr_sample = av_rescale_q(avs->curr_frame, samplerate, fps);
845 avs->curr_sample = timestamp;
852 AVInputFormat ff_avisynth_demuxer = {
854 .long_name = NULL_IF_CONFIG_SMALL("AviSynth script"),
855 .priv_data_size = sizeof(AviSynthContext),
856 .read_header = avisynth_read_header,
857 .read_packet = avisynth_read_packet,
858 .read_close = avisynth_read_close,
859 .read_seek = avisynth_read_seek,