3 * Copyright (c) 2012 AvxSynth Team
5 * This file is part of FFmpeg
7 * FFmpeg 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 * FFmpeg 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 FFmpeg; 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. */
36 #include "compat/w32dlfcn.h"
38 #define AVISYNTH_LIB "avisynth"
41 #define AVISYNTH_NAME "libavisynth"
42 #define AVISYNTH_LIB AVISYNTH_NAME SLIBSUF
45 #include <avisynth/avisynth_c.h>
47 typedef struct AviSynthLibrary {
49 #define AVSC_DECLARE_FUNC(name) name ## _func name
50 AVSC_DECLARE_FUNC(avs_bit_blt);
51 AVSC_DECLARE_FUNC(avs_clip_get_error);
52 AVSC_DECLARE_FUNC(avs_create_script_environment);
53 AVSC_DECLARE_FUNC(avs_delete_script_environment);
54 AVSC_DECLARE_FUNC(avs_get_audio);
55 AVSC_DECLARE_FUNC(avs_get_error);
56 AVSC_DECLARE_FUNC(avs_get_frame);
57 AVSC_DECLARE_FUNC(avs_get_version);
58 AVSC_DECLARE_FUNC(avs_get_video_info);
59 AVSC_DECLARE_FUNC(avs_invoke);
60 AVSC_DECLARE_FUNC(avs_is_color_space);
61 AVSC_DECLARE_FUNC(avs_release_clip);
62 AVSC_DECLARE_FUNC(avs_release_value);
63 AVSC_DECLARE_FUNC(avs_release_video_frame);
64 AVSC_DECLARE_FUNC(avs_take_clip);
65 AVSC_DECLARE_FUNC(avs_bits_per_pixel);
66 AVSC_DECLARE_FUNC(avs_get_height_p);
67 AVSC_DECLARE_FUNC(avs_get_pitch_p);
68 AVSC_DECLARE_FUNC(avs_get_read_ptr_p);
69 AVSC_DECLARE_FUNC(avs_get_row_size_p);
70 AVSC_DECLARE_FUNC(avs_is_planar_rgb);
71 AVSC_DECLARE_FUNC(avs_is_planar_rgba);
72 #undef AVSC_DECLARE_FUNC
75 typedef struct AviSynthContext {
76 AVS_ScriptEnvironment *env;
78 const AVS_VideoInfo *vi;
80 /* avisynth_read_packet_video() iterates over this. */
90 /* Linked list pointers. */
91 struct AviSynthContext *next;
94 static const int avs_planes_packed[1] = { 0 };
95 static const int avs_planes_grey[1] = { AVS_PLANAR_Y };
96 static const int avs_planes_yuv[3] = { AVS_PLANAR_Y, AVS_PLANAR_U,
98 static const int avs_planes_rgb[3] = { AVS_PLANAR_G, AVS_PLANAR_B,
100 static const int avs_planes_yuva[4] = { AVS_PLANAR_Y, AVS_PLANAR_U,
101 AVS_PLANAR_V, AVS_PLANAR_A };
102 static const int avs_planes_rgba[4] = { AVS_PLANAR_G, AVS_PLANAR_B,
103 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) \
122 avs_library.name = (name ## _func) \
123 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_is_color_space, 1);
138 LOAD_AVS_FUNC(avs_release_clip, 0);
139 LOAD_AVS_FUNC(avs_release_value, 0);
140 LOAD_AVS_FUNC(avs_release_video_frame, 0);
141 LOAD_AVS_FUNC(avs_take_clip, 0);
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);
151 atexit(avisynth_atexit_handler);
155 dlclose(avs_library.library);
156 return AVERROR_UNKNOWN;
159 /* Note that avisynth_context_create and avisynth_context_destroy
160 * do not allocate or free the actual context! That is taken care of
162 static av_cold int avisynth_context_create(AVFormatContext *s)
164 AviSynthContext *avs = s->priv_data;
167 if (!avs_library.library)
168 if (ret = avisynth_load_library())
171 avs->env = avs_library.avs_create_script_environment(3);
172 if (avs_library.avs_get_error) {
173 const char *error = avs_library.avs_get_error(avs->env);
175 av_log(s, AV_LOG_ERROR, "%s\n", error);
176 return AVERROR_UNKNOWN;
183 avs->next = avs_ctx_list;
190 static av_cold void avisynth_context_destroy(AviSynthContext *avs)
192 if (avs_atexit_called)
195 if (avs == avs_ctx_list) {
196 avs_ctx_list = avs->next;
198 AviSynthContext *prev = avs_ctx_list;
199 while (prev->next != avs)
201 prev->next = avs->next;
205 avs_library.avs_release_clip(avs->clip);
209 avs_library.avs_delete_script_environment(avs->env);
214 static av_cold void avisynth_atexit_handler(void)
216 AviSynthContext *avs = avs_ctx_list;
219 AviSynthContext *next = avs->next;
220 avisynth_context_destroy(avs);
223 dlclose(avs_library.library);
225 avs_atexit_called = 1;
228 /* Create AVStream from audio and video data. */
229 static int avisynth_create_stream_video(AVFormatContext *s, AVStream *st)
231 AviSynthContext *avs = s->priv_data;
232 int planar = 0; // 0: packed, 1: YUV, 2: Y8, 3: Planar RGB, 4: YUVA, 5: Planar RGBA
234 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
235 st->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
236 st->codecpar->width = avs->vi->width;
237 st->codecpar->height = avs->vi->height;
239 st->avg_frame_rate = (AVRational) { avs->vi->fps_numerator,
240 avs->vi->fps_denominator };
242 st->duration = avs->vi->num_frames;
243 st->nb_frames = avs->vi->num_frames;
244 avpriv_set_pts_info(st, 32, avs->vi->fps_denominator, avs->vi->fps_numerator);
246 switch (avs->vi->pixel_type) {
247 /* 10~16-bit YUV pix_fmts (AviSynth+) */
248 case AVS_CS_YUV444P10:
249 st->codecpar->format = AV_PIX_FMT_YUV444P10;
252 case AVS_CS_YUV422P10:
253 st->codecpar->format = AV_PIX_FMT_YUV422P10;
256 case AVS_CS_YUV420P10:
257 st->codecpar->format = AV_PIX_FMT_YUV420P10;
260 case AVS_CS_YUV444P12:
261 st->codecpar->format = AV_PIX_FMT_YUV444P12;
264 case AVS_CS_YUV422P12:
265 st->codecpar->format = AV_PIX_FMT_YUV422P12;
268 case AVS_CS_YUV420P12:
269 st->codecpar->format = AV_PIX_FMT_YUV420P12;
272 case AVS_CS_YUV444P14:
273 st->codecpar->format = AV_PIX_FMT_YUV444P14;
276 case AVS_CS_YUV422P14:
277 st->codecpar->format = AV_PIX_FMT_YUV422P14;
280 case AVS_CS_YUV420P14:
281 st->codecpar->format = AV_PIX_FMT_YUV420P14;
284 case AVS_CS_YUV444P16:
285 st->codecpar->format = AV_PIX_FMT_YUV444P16;
288 case AVS_CS_YUV422P16:
289 st->codecpar->format = AV_PIX_FMT_YUV422P16;
292 case AVS_CS_YUV420P16:
293 st->codecpar->format = AV_PIX_FMT_YUV420P16;
296 /* 8~16-bit YUV pix_fmts with Alpha (AviSynth+) */
298 st->codecpar->format = AV_PIX_FMT_YUVA444P;
302 st->codecpar->format = AV_PIX_FMT_YUVA422P;
306 st->codecpar->format = AV_PIX_FMT_YUVA420P;
309 case AVS_CS_YUVA444P10:
310 st->codecpar->format = AV_PIX_FMT_YUVA444P10;
313 case AVS_CS_YUVA422P10:
314 st->codecpar->format = AV_PIX_FMT_YUVA422P10;
317 case AVS_CS_YUVA420P10:
318 st->codecpar->format = AV_PIX_FMT_YUVA420P10;
321 case AVS_CS_YUVA422P12:
322 st->codecpar->format = AV_PIX_FMT_YUVA422P12;
325 case AVS_CS_YUVA444P16:
326 st->codecpar->format = AV_PIX_FMT_YUVA444P16;
329 case AVS_CS_YUVA422P16:
330 st->codecpar->format = AV_PIX_FMT_YUVA422P16;
333 case AVS_CS_YUVA420P16:
334 st->codecpar->format = AV_PIX_FMT_YUVA420P16;
337 /* Planar RGB pix_fmts (AviSynth+) */
339 st->codecpar->format = AV_PIX_FMT_GBRP;
343 st->codecpar->format = AV_PIX_FMT_GBRP10;
347 st->codecpar->format = AV_PIX_FMT_GBRP12;
351 st->codecpar->format = AV_PIX_FMT_GBRP14;
355 st->codecpar->format = AV_PIX_FMT_GBRP16;
358 /* Single precision floating point Planar RGB (AviSynth+) */
360 st->codecpar->format = AV_PIX_FMT_GBRPF32;
363 /* Planar RGB pix_fmts with Alpha (AviSynth+) */
365 st->codecpar->format = AV_PIX_FMT_GBRAP;
369 st->codecpar->format = AV_PIX_FMT_GBRAP10;
373 st->codecpar->format = AV_PIX_FMT_GBRAP12;
377 st->codecpar->format = AV_PIX_FMT_GBRAP16;
380 /* Single precision floating point Planar RGB with Alpha (AviSynth+) */
382 st->codecpar->format = AV_PIX_FMT_GBRAPF32;
385 /* 10~16-bit gray pix_fmts (AviSynth+) */
387 st->codecpar->format = AV_PIX_FMT_GRAY10;
391 st->codecpar->format = AV_PIX_FMT_GRAY12;
395 st->codecpar->format = AV_PIX_FMT_GRAY14;
399 st->codecpar->format = AV_PIX_FMT_GRAY16;
402 /* Single precision floating point gray (AviSynth+) */
404 st->codecpar->format = AV_PIX_FMT_GRAYF32;
407 /* pix_fmts added in AviSynth 2.6 */
409 st->codecpar->format = AV_PIX_FMT_YUV444P;
413 st->codecpar->format = AV_PIX_FMT_YUV422P;
417 st->codecpar->format = AV_PIX_FMT_YUV411P;
421 st->codecpar->format = AV_PIX_FMT_GRAY8;
424 /* 16-bit packed RGB pix_fmts (AviSynth+) */
426 st->codecpar->format = AV_PIX_FMT_BGR48;
429 st->codecpar->format = AV_PIX_FMT_BGRA64;
431 /* AviSynth 2.5 pix_fmts */
433 st->codecpar->format = AV_PIX_FMT_BGR24;
436 st->codecpar->format = AV_PIX_FMT_RGB32;
439 st->codecpar->format = AV_PIX_FMT_YUYV422;
442 st->codecpar->format = AV_PIX_FMT_YUV420P;
445 case AVS_CS_I420: // Is this even used anywhere?
446 st->codecpar->format = AV_PIX_FMT_YUV420P;
450 av_log(s, AV_LOG_ERROR,
451 "unknown AviSynth colorspace %d\n", avs->vi->pixel_type);
453 return AVERROR_UNKNOWN;
457 case 5: // Planar RGB + Alpha
459 avs->planes = avs_planes_rgba;
461 case 4: // YUV + Alpha
463 avs->planes = avs_planes_yuva;
465 case 3: // Planar RGB
467 avs->planes = avs_planes_rgb;
471 avs->planes = avs_planes_grey;
475 avs->planes = avs_planes_yuv;
479 avs->planes = avs_planes_packed;
484 static int avisynth_create_stream_audio(AVFormatContext *s, AVStream *st)
486 AviSynthContext *avs = s->priv_data;
488 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
489 st->codecpar->sample_rate = avs->vi->audio_samples_per_second;
490 st->codecpar->channels = avs->vi->nchannels;
491 st->duration = avs->vi->num_audio_samples;
492 avpriv_set_pts_info(st, 64, 1, avs->vi->audio_samples_per_second);
494 switch (avs->vi->sample_type) {
495 case AVS_SAMPLE_INT8:
496 st->codecpar->codec_id = AV_CODEC_ID_PCM_U8;
498 case AVS_SAMPLE_INT16:
499 st->codecpar->codec_id = AV_CODEC_ID_PCM_S16LE;
501 case AVS_SAMPLE_INT24:
502 st->codecpar->codec_id = AV_CODEC_ID_PCM_S24LE;
504 case AVS_SAMPLE_INT32:
505 st->codecpar->codec_id = AV_CODEC_ID_PCM_S32LE;
507 case AVS_SAMPLE_FLOAT:
508 st->codecpar->codec_id = AV_CODEC_ID_PCM_F32LE;
511 av_log(s, AV_LOG_ERROR,
512 "unknown AviSynth sample type %d\n", avs->vi->sample_type);
514 return AVERROR_UNKNOWN;
519 static int avisynth_create_stream(AVFormatContext *s)
521 AviSynthContext *avs = s->priv_data;
526 if (avs_has_video(avs->vi)) {
527 st = avformat_new_stream(s, NULL);
529 return AVERROR_UNKNOWN;
531 if (ret = avisynth_create_stream_video(s, st))
534 if (avs_has_audio(avs->vi)) {
535 st = avformat_new_stream(s, NULL);
537 return AVERROR_UNKNOWN;
539 if (ret = avisynth_create_stream_audio(s, st))
545 static int avisynth_open_file(AVFormatContext *s)
547 AviSynthContext *avs = s->priv_data;
551 char filename_ansi[MAX_PATH * 4];
552 wchar_t filename_wc[MAX_PATH * 4];
555 if (ret = avisynth_context_create(s))
559 /* Convert UTF-8 to ANSI code page */
560 MultiByteToWideChar(CP_UTF8, 0, s->url, -1, filename_wc, MAX_PATH * 4);
561 WideCharToMultiByte(CP_THREAD_ACP, 0, filename_wc, -1, filename_ansi,
562 MAX_PATH * 4, NULL, NULL);
563 arg = avs_new_value_string(filename_ansi);
565 arg = avs_new_value_string(s->url);
567 val = avs_library.avs_invoke(avs->env, "Import", arg, 0);
568 if (avs_is_error(val)) {
569 av_log(s, AV_LOG_ERROR, "%s\n", avs_as_error(val));
570 ret = AVERROR_UNKNOWN;
573 if (!avs_is_clip(val)) {
574 av_log(s, AV_LOG_ERROR, "AviSynth script did not return a clip\n");
575 ret = AVERROR_UNKNOWN;
579 avs->clip = avs_library.avs_take_clip(val, avs->env);
580 avs->vi = avs_library.avs_get_video_info(avs->clip);
582 /* On Windows, FFmpeg supports AviSynth interface version 6 or higher.
583 * This includes AviSynth 2.6 RC1 or higher, and AviSynth+ r1718 or higher,
584 * and excludes 2.5 and the 2.6 alphas. */
586 if (avs_library.avs_get_version(avs->clip) < 6) {
587 av_log(s, AV_LOG_ERROR,
588 "AviSynth version is too old. Please upgrade to either AviSynth 2.6 >= RC1 or AviSynth+ >= r1718.\n");
589 ret = AVERROR_UNKNOWN;
593 /* Release the AVS_Value as it will go out of scope. */
594 avs_library.avs_release_value(val);
596 if (ret = avisynth_create_stream(s))
602 avisynth_context_destroy(avs);
606 static void avisynth_next_stream(AVFormatContext *s, AVStream **st,
607 AVPacket *pkt, int *discard)
609 AviSynthContext *avs = s->priv_data;
612 avs->curr_stream %= s->nb_streams;
614 *st = s->streams[avs->curr_stream];
615 if ((*st)->discard == AVDISCARD_ALL)
623 /* Copy AviSynth clip data into an AVPacket. */
624 static int avisynth_read_packet_video(AVFormatContext *s, AVPacket *pkt,
627 AviSynthContext *avs = s->priv_data;
628 AVS_VideoFrame *frame;
629 unsigned char *dst_p;
630 const unsigned char *src_p;
631 int n, i, plane, rowsize, planeheight, pitch, bits, ret;
634 if (avs->curr_frame >= avs->vi->num_frames)
637 /* This must happen even if the stream is discarded to prevent desync. */
638 n = avs->curr_frame++;
642 bits = avs_library.avs_bits_per_pixel(avs->vi);
644 /* Without the cast to int64_t, calculation overflows at about 9k x 9k
646 pkt->size = (((int64_t)avs->vi->width *
647 (int64_t)avs->vi->height) * bits) / 8;
649 return AVERROR_UNKNOWN;
651 if ((ret = av_new_packet(pkt, pkt->size)) < 0)
657 pkt->stream_index = avs->curr_stream;
659 frame = avs_library.avs_get_frame(avs->clip, n);
660 error = avs_library.avs_clip_get_error(avs->clip);
662 av_log(s, AV_LOG_ERROR, "%s\n", error);
664 av_packet_unref(pkt);
665 return AVERROR_UNKNOWN;
669 for (i = 0; i < avs->n_planes; i++) {
670 plane = avs->planes[i];
671 src_p = avs_library.avs_get_read_ptr_p(frame, plane);
672 pitch = avs_library.avs_get_pitch_p(frame, plane);
674 rowsize = avs_library.avs_get_row_size_p(frame, plane);
675 planeheight = avs_library.avs_get_height_p(frame, plane);
677 /* Flip RGB video. */
678 if (avs_library.avs_is_color_space(avs->vi, AVS_CS_BGR) ||
679 avs_library.avs_is_color_space(avs->vi, AVS_CS_BGR48) ||
680 avs_library.avs_is_color_space(avs->vi, AVS_CS_BGR64)) {
681 src_p = src_p + (planeheight - 1) * pitch;
685 avs_library.avs_bit_blt(avs->env, dst_p, rowsize, src_p, pitch,
686 rowsize, planeheight);
687 dst_p += rowsize * planeheight;
690 avs_library.avs_release_video_frame(frame);
694 static int avisynth_read_packet_audio(AVFormatContext *s, AVPacket *pkt,
697 AviSynthContext *avs = s->priv_data;
698 AVRational fps, samplerate;
703 if (avs->curr_sample >= avs->vi->num_audio_samples)
706 fps.num = avs->vi->fps_numerator;
707 fps.den = avs->vi->fps_denominator;
708 samplerate.num = avs->vi->audio_samples_per_second;
711 if (avs_has_video(avs->vi)) {
712 if (avs->curr_frame < avs->vi->num_frames)
713 samples = av_rescale_q(avs->curr_frame, samplerate, fps) -
716 samples = av_rescale_q(1, samplerate, fps);
721 /* After seeking, audio may catch up with video. */
728 if (avs->curr_sample + samples > avs->vi->num_audio_samples)
729 samples = avs->vi->num_audio_samples - avs->curr_sample;
731 /* This must happen even if the stream is discarded to prevent desync. */
732 n = avs->curr_sample;
733 avs->curr_sample += samples;
737 pkt->size = avs_bytes_per_channel_sample(avs->vi) *
738 samples * avs->vi->nchannels;
740 return AVERROR_UNKNOWN;
742 if ((ret = av_new_packet(pkt, pkt->size)) < 0)
747 pkt->duration = samples;
748 pkt->stream_index = avs->curr_stream;
750 avs_library.avs_get_audio(avs->clip, pkt->data, n, samples);
751 error = avs_library.avs_clip_get_error(avs->clip);
753 av_log(s, AV_LOG_ERROR, "%s\n", error);
755 av_packet_unref(pkt);
756 return AVERROR_UNKNOWN;
761 static av_cold int avisynth_read_header(AVFormatContext *s)
765 // Calling library must implement a lock for thread-safe opens.
766 if (ret = ff_lock_avformat())
769 if (ret = avisynth_open_file(s)) {
770 ff_unlock_avformat();
774 ff_unlock_avformat();
778 static int avisynth_read_packet(AVFormatContext *s, AVPacket *pkt)
780 AviSynthContext *avs = s->priv_data;
786 return AVERROR_UNKNOWN;
788 /* If either stream reaches EOF, try to read the other one before
790 avisynth_next_stream(s, &st, pkt, &discard);
791 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
792 ret = avisynth_read_packet_video(s, pkt, discard);
793 if (ret == AVERROR_EOF && avs_has_audio(avs->vi)) {
794 avisynth_next_stream(s, &st, pkt, &discard);
795 return avisynth_read_packet_audio(s, pkt, discard);
798 ret = avisynth_read_packet_audio(s, pkt, discard);
799 if (ret == AVERROR_EOF && avs_has_video(avs->vi)) {
800 avisynth_next_stream(s, &st, pkt, &discard);
801 return avisynth_read_packet_video(s, pkt, discard);
808 static av_cold int avisynth_read_close(AVFormatContext *s)
810 if (ff_lock_avformat())
811 return AVERROR_UNKNOWN;
813 avisynth_context_destroy(s->priv_data);
814 ff_unlock_avformat();
818 static int avisynth_read_seek(AVFormatContext *s, int stream_index,
819 int64_t timestamp, int flags)
821 AviSynthContext *avs = s->priv_data;
823 AVRational fps, samplerate;
826 return AVERROR_UNKNOWN;
828 fps = (AVRational) { avs->vi->fps_numerator,
829 avs->vi->fps_denominator };
830 samplerate = (AVRational) { avs->vi->audio_samples_per_second, 1 };
832 st = s->streams[stream_index];
833 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
834 /* AviSynth frame counts are signed int. */
835 if ((timestamp >= avs->vi->num_frames) ||
836 (timestamp > INT_MAX) ||
839 avs->curr_frame = timestamp;
840 if (avs_has_audio(avs->vi))
841 avs->curr_sample = av_rescale_q(timestamp, samplerate, fps);
843 if ((timestamp >= avs->vi->num_audio_samples) || (timestamp < 0))
845 /* Force frame granularity for seeking. */
846 if (avs_has_video(avs->vi)) {
847 avs->curr_frame = av_rescale_q(timestamp, fps, samplerate);
848 avs->curr_sample = av_rescale_q(avs->curr_frame, samplerate, fps);
850 avs->curr_sample = timestamp;
857 AVInputFormat ff_avisynth_demuxer = {
859 .long_name = NULL_IF_CONFIG_SMALL("AviSynth script"),
860 .priv_data_size = sizeof(AviSynthContext),
861 .read_header = avisynth_read_header,
862 .read_packet = avisynth_read_packet,
863 .read_close = avisynth_read_close,
864 .read_seek = avisynth_read_seek,