2 * AviSynth/AvxSynth support
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 for AviSynth vs AvxSynth. */
36 #include "compat/w32dlfcn.h"
38 #include "compat/avisynth/avisynth_c.h"
39 #define AVISYNTH_LIB "avisynth"
40 #define USING_AVISYNTH
43 #include "compat/avisynth/avxsynth_c.h"
44 #define AVISYNTH_NAME "libavxsynth"
45 #define AVISYNTH_LIB AVISYNTH_NAME SLIBSUF
48 typedef struct AviSynthLibrary {
50 #define AVSC_DECLARE_FUNC(name) name ## _func name
51 AVSC_DECLARE_FUNC(avs_bit_blt);
52 AVSC_DECLARE_FUNC(avs_clip_get_error);
53 AVSC_DECLARE_FUNC(avs_create_script_environment);
54 AVSC_DECLARE_FUNC(avs_delete_script_environment);
55 AVSC_DECLARE_FUNC(avs_get_audio);
56 AVSC_DECLARE_FUNC(avs_get_error);
57 AVSC_DECLARE_FUNC(avs_get_frame);
58 AVSC_DECLARE_FUNC(avs_get_version);
59 AVSC_DECLARE_FUNC(avs_get_video_info);
60 AVSC_DECLARE_FUNC(avs_invoke);
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);
66 AVSC_DECLARE_FUNC(avs_bits_per_pixel);
67 AVSC_DECLARE_FUNC(avs_get_height_p);
68 AVSC_DECLARE_FUNC(avs_get_pitch_p);
69 AVSC_DECLARE_FUNC(avs_get_read_ptr_p);
70 AVSC_DECLARE_FUNC(avs_get_row_size_p);
71 AVSC_DECLARE_FUNC(avs_is_planar_rgb);
72 AVSC_DECLARE_FUNC(avs_is_planar_rgba);
74 #undef AVSC_DECLARE_FUNC
77 typedef struct AviSynthContext {
78 AVS_ScriptEnvironment *env;
80 const AVS_VideoInfo *vi;
82 /* avisynth_read_packet_video() iterates over this. */
92 /* Linked list pointers. */
93 struct AviSynthContext *next;
96 static const int avs_planes_packed[1] = { 0 };
97 static const int avs_planes_grey[1] = { AVS_PLANAR_Y };
98 static const int avs_planes_yuv[3] = { AVS_PLANAR_Y, AVS_PLANAR_U,
100 #ifdef USING_AVISYNTH
101 static const int avs_planes_rgb[3] = { AVS_PLANAR_G, AVS_PLANAR_B,
103 static const int avs_planes_yuva[4] = { AVS_PLANAR_Y, AVS_PLANAR_U,
104 AVS_PLANAR_V, AVS_PLANAR_A };
105 static const int avs_planes_rgba[4] = { AVS_PLANAR_G, AVS_PLANAR_B,
106 AVS_PLANAR_R, AVS_PLANAR_A };
109 /* A conflict between C++ global objects, atexit, and dynamic loading requires
110 * us to register our own atexit handler to prevent double freeing. */
111 static AviSynthLibrary avs_library;
112 static int avs_atexit_called = 0;
114 /* Linked list of AviSynthContexts. An atexit handler destroys this list. */
115 static AviSynthContext *avs_ctx_list = NULL;
117 static av_cold void avisynth_atexit_handler(void);
119 static av_cold int avisynth_load_library(void)
121 avs_library.library = dlopen(AVISYNTH_LIB, RTLD_NOW | RTLD_LOCAL);
122 if (!avs_library.library)
123 return AVERROR_UNKNOWN;
125 #define LOAD_AVS_FUNC(name, continue_on_fail) \
126 avs_library.name = dlsym(avs_library.library, #name); \
127 if (!continue_on_fail && !avs_library.name) \
130 LOAD_AVS_FUNC(avs_bit_blt, 0);
131 LOAD_AVS_FUNC(avs_clip_get_error, 0);
132 LOAD_AVS_FUNC(avs_create_script_environment, 0);
133 LOAD_AVS_FUNC(avs_delete_script_environment, 0);
134 LOAD_AVS_FUNC(avs_get_audio, 0);
135 LOAD_AVS_FUNC(avs_get_error, 1); // New to AviSynth 2.6
136 LOAD_AVS_FUNC(avs_get_frame, 0);
137 LOAD_AVS_FUNC(avs_get_version, 0);
138 LOAD_AVS_FUNC(avs_get_video_info, 0);
139 LOAD_AVS_FUNC(avs_invoke, 0);
140 LOAD_AVS_FUNC(avs_release_clip, 0);
141 LOAD_AVS_FUNC(avs_release_value, 0);
142 LOAD_AVS_FUNC(avs_release_video_frame, 0);
143 LOAD_AVS_FUNC(avs_take_clip, 0);
144 #ifdef USING_AVISYNTH
145 LOAD_AVS_FUNC(avs_bits_per_pixel, 1);
146 LOAD_AVS_FUNC(avs_get_height_p, 1);
147 LOAD_AVS_FUNC(avs_get_pitch_p, 1);
148 LOAD_AVS_FUNC(avs_get_read_ptr_p, 1);
149 LOAD_AVS_FUNC(avs_get_row_size_p, 1);
150 LOAD_AVS_FUNC(avs_is_planar_rgb, 1);
151 LOAD_AVS_FUNC(avs_is_planar_rgba, 1);
155 atexit(avisynth_atexit_handler);
159 dlclose(avs_library.library);
160 return AVERROR_UNKNOWN;
163 /* Note that avisynth_context_create and avisynth_context_destroy
164 * do not allocate or free the actual context! That is taken care of
166 static av_cold int avisynth_context_create(AVFormatContext *s)
168 AviSynthContext *avs = s->priv_data;
171 if (!avs_library.library)
172 if (ret = avisynth_load_library())
175 avs->env = avs_library.avs_create_script_environment(3);
176 if (avs_library.avs_get_error) {
177 const char *error = avs_library.avs_get_error(avs->env);
179 av_log(s, AV_LOG_ERROR, "%s\n", error);
180 return AVERROR_UNKNOWN;
187 avs->next = avs_ctx_list;
194 static av_cold void avisynth_context_destroy(AviSynthContext *avs)
196 if (avs_atexit_called)
199 if (avs == avs_ctx_list) {
200 avs_ctx_list = avs->next;
202 AviSynthContext *prev = avs_ctx_list;
203 while (prev->next != avs)
205 prev->next = avs->next;
209 avs_library.avs_release_clip(avs->clip);
213 avs_library.avs_delete_script_environment(avs->env);
218 static av_cold void avisynth_atexit_handler(void)
220 AviSynthContext *avs = avs_ctx_list;
223 AviSynthContext *next = avs->next;
224 avisynth_context_destroy(avs);
227 dlclose(avs_library.library);
229 avs_atexit_called = 1;
232 /* Create AVStream from audio and video data. */
233 static int avisynth_create_stream_video(AVFormatContext *s, AVStream *st)
235 AviSynthContext *avs = s->priv_data;
236 int planar = 0; // 0: packed, 1: YUV, 2: Y8, 3: Planar RGB, 4: YUVA, 5: Planar RGBA
238 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
239 st->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
240 st->codecpar->width = avs->vi->width;
241 st->codecpar->height = avs->vi->height;
243 st->avg_frame_rate = (AVRational) { avs->vi->fps_numerator,
244 avs->vi->fps_denominator };
246 st->duration = avs->vi->num_frames;
247 st->nb_frames = avs->vi->num_frames;
248 avpriv_set_pts_info(st, 32, avs->vi->fps_denominator, avs->vi->fps_numerator);
250 switch (avs->vi->pixel_type) {
251 #ifdef USING_AVISYNTH
252 /* 10~16-bit YUV pix_fmts (AviSynth+) */
253 case AVS_CS_YUV444P10:
254 st->codecpar->format = AV_PIX_FMT_YUV444P10;
257 case AVS_CS_YUV422P10:
258 st->codecpar->format = AV_PIX_FMT_YUV422P10;
261 case AVS_CS_YUV420P10:
262 st->codecpar->format = AV_PIX_FMT_YUV420P10;
265 case AVS_CS_YUV444P12:
266 st->codecpar->format = AV_PIX_FMT_YUV444P12;
269 case AVS_CS_YUV422P12:
270 st->codecpar->format = AV_PIX_FMT_YUV422P12;
273 case AVS_CS_YUV420P12:
274 st->codecpar->format = AV_PIX_FMT_YUV420P12;
277 case AVS_CS_YUV444P14:
278 st->codecpar->format = AV_PIX_FMT_YUV444P14;
281 case AVS_CS_YUV422P14:
282 st->codecpar->format = AV_PIX_FMT_YUV422P14;
285 case AVS_CS_YUV420P14:
286 st->codecpar->format = AV_PIX_FMT_YUV420P14;
289 case AVS_CS_YUV444P16:
290 st->codecpar->format = AV_PIX_FMT_YUV444P16;
293 case AVS_CS_YUV422P16:
294 st->codecpar->format = AV_PIX_FMT_YUV422P16;
297 case AVS_CS_YUV420P16:
298 st->codecpar->format = AV_PIX_FMT_YUV420P16;
301 /* 8~16-bit YUV pix_fmts with Alpha (AviSynth+) */
303 st->codecpar->format = AV_PIX_FMT_YUVA444P;
307 st->codecpar->format = AV_PIX_FMT_YUVA422P;
311 st->codecpar->format = AV_PIX_FMT_YUVA420P;
314 case AVS_CS_YUVA444P10:
315 st->codecpar->format = AV_PIX_FMT_YUVA444P10;
318 case AVS_CS_YUVA422P10:
319 st->codecpar->format = AV_PIX_FMT_YUVA422P10;
322 case AVS_CS_YUVA420P10:
323 st->codecpar->format = AV_PIX_FMT_YUVA420P10;
326 case AVS_CS_YUVA444P16:
327 st->codecpar->format = AV_PIX_FMT_YUVA444P16;
330 case AVS_CS_YUVA422P16:
331 st->codecpar->format = AV_PIX_FMT_YUVA422P16;
334 case AVS_CS_YUVA420P16:
335 st->codecpar->format = AV_PIX_FMT_YUVA420P16;
338 /* Planar RGB pix_fmts (AviSynth+) */
340 st->codecpar->format = AV_PIX_FMT_GBRP;
344 st->codecpar->format = AV_PIX_FMT_GBRP10;
348 st->codecpar->format = AV_PIX_FMT_GBRP12;
352 st->codecpar->format = AV_PIX_FMT_GBRP14;
356 st->codecpar->format = AV_PIX_FMT_GBRP16;
359 /* Planar RGB pix_fmts with Alpha (AviSynth+) */
361 st->codecpar->format = AV_PIX_FMT_GBRAP;
365 st->codecpar->format = AV_PIX_FMT_GBRAP10;
369 st->codecpar->format = AV_PIX_FMT_GBRAP12;
373 st->codecpar->format = AV_PIX_FMT_GBRAP16;
376 /* GRAY16 (AviSynth+) */
378 st->codecpar->format = AV_PIX_FMT_GRAY16;
381 /* pix_fmts added in AviSynth 2.6 */
383 st->codecpar->format = AV_PIX_FMT_YUV444P;
387 st->codecpar->format = AV_PIX_FMT_YUV422P;
391 st->codecpar->format = AV_PIX_FMT_YUV411P;
395 st->codecpar->format = AV_PIX_FMT_GRAY8;
398 /* 16-bit packed RGB pix_fmts (AviSynth+) */
400 st->codecpar->format = AV_PIX_FMT_BGR48;
403 st->codecpar->format = AV_PIX_FMT_BGRA64;
406 /* AviSynth 2.5 and AvxSynth pix_fmts */
408 st->codecpar->format = AV_PIX_FMT_BGR24;
411 st->codecpar->format = AV_PIX_FMT_RGB32;
414 st->codecpar->format = AV_PIX_FMT_YUYV422;
417 st->codecpar->format = AV_PIX_FMT_YUV420P;
420 case AVS_CS_I420: // Is this even used anywhere?
421 st->codecpar->format = AV_PIX_FMT_YUV420P;
425 av_log(s, AV_LOG_ERROR,
426 "unknown AviSynth colorspace %d\n", avs->vi->pixel_type);
428 return AVERROR_UNKNOWN;
432 #ifdef USING_AVISYNTH
433 case 5: // Planar RGB + Alpha
435 avs->planes = avs_planes_rgba;
437 case 4: // YUV + Alpha
439 avs->planes = avs_planes_yuva;
441 case 3: // Planar RGB
443 avs->planes = avs_planes_rgb;
448 avs->planes = avs_planes_grey;
452 avs->planes = avs_planes_yuv;
456 avs->planes = avs_planes_packed;
461 static int avisynth_create_stream_audio(AVFormatContext *s, AVStream *st)
463 AviSynthContext *avs = s->priv_data;
465 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
466 st->codecpar->sample_rate = avs->vi->audio_samples_per_second;
467 st->codecpar->channels = avs->vi->nchannels;
468 st->duration = avs->vi->num_audio_samples;
469 avpriv_set_pts_info(st, 64, 1, avs->vi->audio_samples_per_second);
471 switch (avs->vi->sample_type) {
472 case AVS_SAMPLE_INT8:
473 st->codecpar->codec_id = AV_CODEC_ID_PCM_U8;
475 case AVS_SAMPLE_INT16:
476 st->codecpar->codec_id = AV_CODEC_ID_PCM_S16LE;
478 case AVS_SAMPLE_INT24:
479 st->codecpar->codec_id = AV_CODEC_ID_PCM_S24LE;
481 case AVS_SAMPLE_INT32:
482 st->codecpar->codec_id = AV_CODEC_ID_PCM_S32LE;
484 case AVS_SAMPLE_FLOAT:
485 st->codecpar->codec_id = AV_CODEC_ID_PCM_F32LE;
488 av_log(s, AV_LOG_ERROR,
489 "unknown AviSynth sample type %d\n", avs->vi->sample_type);
491 return AVERROR_UNKNOWN;
496 static int avisynth_create_stream(AVFormatContext *s)
498 AviSynthContext *avs = s->priv_data;
503 if (avs_has_video(avs->vi)) {
504 st = avformat_new_stream(s, NULL);
506 return AVERROR_UNKNOWN;
508 if (ret = avisynth_create_stream_video(s, st))
511 if (avs_has_audio(avs->vi)) {
512 st = avformat_new_stream(s, NULL);
514 return AVERROR_UNKNOWN;
516 if (ret = avisynth_create_stream_audio(s, st))
522 static int avisynth_open_file(AVFormatContext *s)
524 AviSynthContext *avs = s->priv_data;
527 #ifdef USING_AVISYNTH
528 char filename_ansi[MAX_PATH * 4];
529 wchar_t filename_wc[MAX_PATH * 4];
532 if (ret = avisynth_context_create(s))
535 #ifdef USING_AVISYNTH
536 /* Convert UTF-8 to ANSI code page */
537 MultiByteToWideChar(CP_UTF8, 0, s->filename, -1, filename_wc, MAX_PATH * 4);
538 WideCharToMultiByte(CP_THREAD_ACP, 0, filename_wc, -1, filename_ansi,
539 MAX_PATH * 4, NULL, NULL);
540 arg = avs_new_value_string(filename_ansi);
542 arg = avs_new_value_string(s->filename);
544 val = avs_library.avs_invoke(avs->env, "Import", arg, 0);
545 if (avs_is_error(val)) {
546 av_log(s, AV_LOG_ERROR, "%s\n", avs_as_error(val));
547 ret = AVERROR_UNKNOWN;
550 if (!avs_is_clip(val)) {
551 av_log(s, AV_LOG_ERROR, "AviSynth script did not return a clip\n");
552 ret = AVERROR_UNKNOWN;
556 avs->clip = avs_library.avs_take_clip(val, avs->env);
557 avs->vi = avs_library.avs_get_video_info(avs->clip);
559 #ifdef USING_AVISYNTH
560 /* On Windows, FFmpeg supports AviSynth interface version 6 or higher.
561 * This includes AviSynth 2.6 RC1 or higher, and AviSynth+ r1718 or higher,
562 * and excludes 2.5 and the 2.6 alphas. Since AvxSynth identifies itself
563 * as interface version 3 like 2.5.8, this needs to be special-cased. */
565 if (avs_library.avs_get_version(avs->clip) < 6) {
566 av_log(s, AV_LOG_ERROR,
567 "AviSynth version is too old. Please upgrade to either AviSynth 2.6 >= RC1 or AviSynth+ >= r1718.\n");
568 ret = AVERROR_UNKNOWN;
573 /* Release the AVS_Value as it will go out of scope. */
574 avs_library.avs_release_value(val);
576 if (ret = avisynth_create_stream(s))
582 avisynth_context_destroy(avs);
586 static void avisynth_next_stream(AVFormatContext *s, AVStream **st,
587 AVPacket *pkt, int *discard)
589 AviSynthContext *avs = s->priv_data;
592 avs->curr_stream %= s->nb_streams;
594 *st = s->streams[avs->curr_stream];
595 if ((*st)->discard == AVDISCARD_ALL)
603 /* Copy AviSynth clip data into an AVPacket. */
604 static int avisynth_read_packet_video(AVFormatContext *s, AVPacket *pkt,
607 AviSynthContext *avs = s->priv_data;
608 AVS_VideoFrame *frame;
609 unsigned char *dst_p;
610 const unsigned char *src_p;
611 int n, i, plane, rowsize, planeheight, pitch, bits;
613 int avsplus av_unused;
615 if (avs->curr_frame >= avs->vi->num_frames)
618 /* This must happen even if the stream is discarded to prevent desync. */
619 n = avs->curr_frame++;
623 #ifdef USING_AVISYNTH
624 /* Detect whether we're using AviSynth 2.6 or AviSynth+ by
625 * looking for whether avs_is_planar_rgb exists. */
626 if (GetProcAddress(avs_library.library, "avs_is_planar_rgb") == NULL)
631 /* avs_bits_per_pixel changed to AVSC_API with AviSynth 2.6, which
632 * requires going through avs_library, while AvxSynth has it under
633 * the older AVSC_INLINE type, so special-case this. */
635 bits = avs_library.avs_bits_per_pixel(avs->vi);
637 bits = avs_bits_per_pixel(avs->vi);
640 /* Without the cast to int64_t, calculation overflows at about 9k x 9k
642 pkt->size = (((int64_t)avs->vi->width *
643 (int64_t)avs->vi->height) * bits) / 8;
645 return AVERROR_UNKNOWN;
647 if (av_new_packet(pkt, pkt->size) < 0)
648 return AVERROR(ENOMEM);
653 pkt->stream_index = avs->curr_stream;
655 frame = avs_library.avs_get_frame(avs->clip, n);
656 error = avs_library.avs_clip_get_error(avs->clip);
658 av_log(s, AV_LOG_ERROR, "%s\n", error);
660 av_packet_unref(pkt);
661 return AVERROR_UNKNOWN;
665 for (i = 0; i < avs->n_planes; i++) {
666 plane = avs->planes[i];
667 #ifdef USING_AVISYNTH
668 src_p = avs_library.avs_get_read_ptr_p(frame, plane);
669 pitch = avs_library.avs_get_pitch_p(frame, plane);
671 rowsize = avs_library.avs_get_row_size_p(frame, plane);
672 planeheight = avs_library.avs_get_height_p(frame, plane);
674 src_p = avs_get_read_ptr_p(frame, plane);
675 pitch = avs_get_pitch_p(frame, plane);
677 rowsize = avs_get_row_size_p(frame, plane);
678 planeheight = avs_get_height_p(frame, plane);
681 /* Flip RGB video. */
682 if (avs_is_rgb24(avs->vi) || avs_is_rgb(avs->vi)) {
683 src_p = src_p + (planeheight - 1) * pitch;
687 #ifdef USING_AVISYNTH
688 /* Flip Planar RGB video */
689 if (avsplus && (avs_library.avs_is_planar_rgb(avs->vi) ||
690 avs_library.avs_is_planar_rgba(avs->vi))) {
691 src_p = src_p + (planeheight - 1) * pitch;
696 avs_library.avs_bit_blt(avs->env, dst_p, rowsize, src_p, pitch,
697 rowsize, planeheight);
698 dst_p += rowsize * planeheight;
701 avs_library.avs_release_video_frame(frame);
705 static int avisynth_read_packet_audio(AVFormatContext *s, AVPacket *pkt,
708 AviSynthContext *avs = s->priv_data;
709 AVRational fps, samplerate;
714 if (avs->curr_sample >= avs->vi->num_audio_samples)
717 fps.num = avs->vi->fps_numerator;
718 fps.den = avs->vi->fps_denominator;
719 samplerate.num = avs->vi->audio_samples_per_second;
722 if (avs_has_video(avs->vi)) {
723 if (avs->curr_frame < avs->vi->num_frames)
724 samples = av_rescale_q(avs->curr_frame, samplerate, fps) -
727 samples = av_rescale_q(1, samplerate, fps);
732 /* After seeking, audio may catch up with video. */
739 if (avs->curr_sample + samples > avs->vi->num_audio_samples)
740 samples = avs->vi->num_audio_samples - avs->curr_sample;
742 /* This must happen even if the stream is discarded to prevent desync. */
743 n = avs->curr_sample;
744 avs->curr_sample += samples;
748 pkt->size = avs_bytes_per_channel_sample(avs->vi) *
749 samples * avs->vi->nchannels;
751 return AVERROR_UNKNOWN;
753 if (av_new_packet(pkt, pkt->size) < 0)
754 return AVERROR(ENOMEM);
758 pkt->duration = samples;
759 pkt->stream_index = avs->curr_stream;
761 avs_library.avs_get_audio(avs->clip, pkt->data, n, samples);
762 error = avs_library.avs_clip_get_error(avs->clip);
764 av_log(s, AV_LOG_ERROR, "%s\n", error);
766 av_packet_unref(pkt);
767 return AVERROR_UNKNOWN;
772 static av_cold int avisynth_read_header(AVFormatContext *s)
776 // Calling library must implement a lock for thread-safe opens.
777 if (ret = ff_lock_avformat())
780 if (ret = avisynth_open_file(s)) {
781 ff_unlock_avformat();
785 ff_unlock_avformat();
789 static int avisynth_read_packet(AVFormatContext *s, AVPacket *pkt)
791 AviSynthContext *avs = s->priv_data;
797 return AVERROR_UNKNOWN;
799 /* If either stream reaches EOF, try to read the other one before
801 avisynth_next_stream(s, &st, pkt, &discard);
802 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
803 ret = avisynth_read_packet_video(s, pkt, discard);
804 if (ret == AVERROR_EOF && avs_has_audio(avs->vi)) {
805 avisynth_next_stream(s, &st, pkt, &discard);
806 return avisynth_read_packet_audio(s, pkt, discard);
809 ret = avisynth_read_packet_audio(s, pkt, discard);
810 if (ret == AVERROR_EOF && avs_has_video(avs->vi)) {
811 avisynth_next_stream(s, &st, pkt, &discard);
812 return avisynth_read_packet_video(s, pkt, discard);
819 static av_cold int avisynth_read_close(AVFormatContext *s)
821 if (ff_lock_avformat())
822 return AVERROR_UNKNOWN;
824 avisynth_context_destroy(s->priv_data);
825 ff_unlock_avformat();
829 static int avisynth_read_seek(AVFormatContext *s, int stream_index,
830 int64_t timestamp, int flags)
832 AviSynthContext *avs = s->priv_data;
834 AVRational fps, samplerate;
837 return AVERROR_UNKNOWN;
839 fps = (AVRational) { avs->vi->fps_numerator,
840 avs->vi->fps_denominator };
841 samplerate = (AVRational) { avs->vi->audio_samples_per_second, 1 };
843 st = s->streams[stream_index];
844 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
845 /* AviSynth frame counts are signed int. */
846 if ((timestamp >= avs->vi->num_frames) ||
847 (timestamp > INT_MAX) ||
850 avs->curr_frame = timestamp;
851 if (avs_has_audio(avs->vi))
852 avs->curr_sample = av_rescale_q(timestamp, samplerate, fps);
854 if ((timestamp >= avs->vi->num_audio_samples) || (timestamp < 0))
856 /* Force frame granularity for seeking. */
857 if (avs_has_video(avs->vi)) {
858 avs->curr_frame = av_rescale_q(timestamp, fps, samplerate);
859 avs->curr_sample = av_rescale_q(avs->curr_frame, samplerate, fps);
861 avs->curr_sample = timestamp;
868 AVInputFormat ff_avisynth_demuxer = {
870 .long_name = NULL_IF_CONFIG_SMALL("AviSynth script"),
871 .priv_data_size = sizeof(AviSynthContext),
872 .read_header = avisynth_read_header,
873 .read_packet = avisynth_read_packet,
874 .read_close = avisynth_read_close,
875 .read_seek = avisynth_read_seek,