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 = (name ## _func) \
127 dlsym(avs_library.library, #name); \
128 if (!continue_on_fail && !avs_library.name) \
131 LOAD_AVS_FUNC(avs_bit_blt, 0);
132 LOAD_AVS_FUNC(avs_clip_get_error, 0);
133 LOAD_AVS_FUNC(avs_create_script_environment, 0);
134 LOAD_AVS_FUNC(avs_delete_script_environment, 0);
135 LOAD_AVS_FUNC(avs_get_audio, 0);
136 LOAD_AVS_FUNC(avs_get_error, 1); // New to AviSynth 2.6
137 LOAD_AVS_FUNC(avs_get_frame, 0);
138 LOAD_AVS_FUNC(avs_get_version, 0);
139 LOAD_AVS_FUNC(avs_get_video_info, 0);
140 LOAD_AVS_FUNC(avs_invoke, 0);
141 LOAD_AVS_FUNC(avs_release_clip, 0);
142 LOAD_AVS_FUNC(avs_release_value, 0);
143 LOAD_AVS_FUNC(avs_release_video_frame, 0);
144 LOAD_AVS_FUNC(avs_take_clip, 0);
145 #ifdef USING_AVISYNTH
146 LOAD_AVS_FUNC(avs_bits_per_pixel, 1);
147 LOAD_AVS_FUNC(avs_get_height_p, 1);
148 LOAD_AVS_FUNC(avs_get_pitch_p, 1);
149 LOAD_AVS_FUNC(avs_get_read_ptr_p, 1);
150 LOAD_AVS_FUNC(avs_get_row_size_p, 1);
151 LOAD_AVS_FUNC(avs_is_planar_rgb, 1);
152 LOAD_AVS_FUNC(avs_is_planar_rgba, 1);
156 atexit(avisynth_atexit_handler);
160 dlclose(avs_library.library);
161 return AVERROR_UNKNOWN;
164 /* Note that avisynth_context_create and avisynth_context_destroy
165 * do not allocate or free the actual context! That is taken care of
167 static av_cold int avisynth_context_create(AVFormatContext *s)
169 AviSynthContext *avs = s->priv_data;
172 if (!avs_library.library)
173 if (ret = avisynth_load_library())
176 avs->env = avs_library.avs_create_script_environment(3);
177 if (avs_library.avs_get_error) {
178 const char *error = avs_library.avs_get_error(avs->env);
180 av_log(s, AV_LOG_ERROR, "%s\n", error);
181 return AVERROR_UNKNOWN;
188 avs->next = avs_ctx_list;
195 static av_cold void avisynth_context_destroy(AviSynthContext *avs)
197 if (avs_atexit_called)
200 if (avs == avs_ctx_list) {
201 avs_ctx_list = avs->next;
203 AviSynthContext *prev = avs_ctx_list;
204 while (prev->next != avs)
206 prev->next = avs->next;
210 avs_library.avs_release_clip(avs->clip);
214 avs_library.avs_delete_script_environment(avs->env);
219 static av_cold void avisynth_atexit_handler(void)
221 AviSynthContext *avs = avs_ctx_list;
224 AviSynthContext *next = avs->next;
225 avisynth_context_destroy(avs);
228 dlclose(avs_library.library);
230 avs_atexit_called = 1;
233 /* Create AVStream from audio and video data. */
234 static int avisynth_create_stream_video(AVFormatContext *s, AVStream *st)
236 AviSynthContext *avs = s->priv_data;
237 int planar = 0; // 0: packed, 1: YUV, 2: Y8, 3: Planar RGB, 4: YUVA, 5: Planar RGBA
239 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
240 st->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
241 st->codecpar->width = avs->vi->width;
242 st->codecpar->height = avs->vi->height;
244 st->avg_frame_rate = (AVRational) { avs->vi->fps_numerator,
245 avs->vi->fps_denominator };
247 st->duration = avs->vi->num_frames;
248 st->nb_frames = avs->vi->num_frames;
249 avpriv_set_pts_info(st, 32, avs->vi->fps_denominator, avs->vi->fps_numerator);
251 switch (avs->vi->pixel_type) {
252 #ifdef USING_AVISYNTH
253 /* 10~16-bit YUV pix_fmts (AviSynth+) */
254 case AVS_CS_YUV444P10:
255 st->codecpar->format = AV_PIX_FMT_YUV444P10;
258 case AVS_CS_YUV422P10:
259 st->codecpar->format = AV_PIX_FMT_YUV422P10;
262 case AVS_CS_YUV420P10:
263 st->codecpar->format = AV_PIX_FMT_YUV420P10;
266 case AVS_CS_YUV444P12:
267 st->codecpar->format = AV_PIX_FMT_YUV444P12;
270 case AVS_CS_YUV422P12:
271 st->codecpar->format = AV_PIX_FMT_YUV422P12;
274 case AVS_CS_YUV420P12:
275 st->codecpar->format = AV_PIX_FMT_YUV420P12;
278 case AVS_CS_YUV444P14:
279 st->codecpar->format = AV_PIX_FMT_YUV444P14;
282 case AVS_CS_YUV422P14:
283 st->codecpar->format = AV_PIX_FMT_YUV422P14;
286 case AVS_CS_YUV420P14:
287 st->codecpar->format = AV_PIX_FMT_YUV420P14;
290 case AVS_CS_YUV444P16:
291 st->codecpar->format = AV_PIX_FMT_YUV444P16;
294 case AVS_CS_YUV422P16:
295 st->codecpar->format = AV_PIX_FMT_YUV422P16;
298 case AVS_CS_YUV420P16:
299 st->codecpar->format = AV_PIX_FMT_YUV420P16;
302 /* 8~16-bit YUV pix_fmts with Alpha (AviSynth+) */
304 st->codecpar->format = AV_PIX_FMT_YUVA444P;
308 st->codecpar->format = AV_PIX_FMT_YUVA422P;
312 st->codecpar->format = AV_PIX_FMT_YUVA420P;
315 case AVS_CS_YUVA444P10:
316 st->codecpar->format = AV_PIX_FMT_YUVA444P10;
319 case AVS_CS_YUVA422P10:
320 st->codecpar->format = AV_PIX_FMT_YUVA422P10;
323 case AVS_CS_YUVA420P10:
324 st->codecpar->format = AV_PIX_FMT_YUVA420P10;
327 case AVS_CS_YUVA422P12:
328 st->codecpar->format = AV_PIX_FMT_YUVA422P12;
331 case AVS_CS_YUVA444P16:
332 st->codecpar->format = AV_PIX_FMT_YUVA444P16;
335 case AVS_CS_YUVA422P16:
336 st->codecpar->format = AV_PIX_FMT_YUVA422P16;
339 case AVS_CS_YUVA420P16:
340 st->codecpar->format = AV_PIX_FMT_YUVA420P16;
343 /* Planar RGB pix_fmts (AviSynth+) */
345 st->codecpar->format = AV_PIX_FMT_GBRP;
349 st->codecpar->format = AV_PIX_FMT_GBRP10;
353 st->codecpar->format = AV_PIX_FMT_GBRP12;
357 st->codecpar->format = AV_PIX_FMT_GBRP14;
361 st->codecpar->format = AV_PIX_FMT_GBRP16;
364 /* Single precision floating point Planar RGB (AviSynth+) */
366 st->codecpar->format = AV_PIX_FMT_GBRPF32;
369 /* Planar RGB pix_fmts with Alpha (AviSynth+) */
371 st->codecpar->format = AV_PIX_FMT_GBRAP;
375 st->codecpar->format = AV_PIX_FMT_GBRAP10;
379 st->codecpar->format = AV_PIX_FMT_GBRAP12;
383 st->codecpar->format = AV_PIX_FMT_GBRAP16;
386 /* Single precision floating point Planar RGB with Alpha (AviSynth+) */
388 st->codecpar->format = AV_PIX_FMT_GBRAPF32;
391 /* 10~16-bit gray pix_fmts (AviSynth+) */
393 st->codecpar->format = AV_PIX_FMT_GRAY10;
397 st->codecpar->format = AV_PIX_FMT_GRAY12;
401 st->codecpar->format = AV_PIX_FMT_GRAY14;
405 st->codecpar->format = AV_PIX_FMT_GRAY16;
408 /* Single precision floating point gray (AviSynth+) */
410 st->codecpar->format = AV_PIX_FMT_GRAYF32;
413 /* pix_fmts added in AviSynth 2.6 */
415 st->codecpar->format = AV_PIX_FMT_YUV444P;
419 st->codecpar->format = AV_PIX_FMT_YUV422P;
423 st->codecpar->format = AV_PIX_FMT_YUV411P;
427 st->codecpar->format = AV_PIX_FMT_GRAY8;
430 /* 16-bit packed RGB pix_fmts (AviSynth+) */
432 st->codecpar->format = AV_PIX_FMT_BGR48;
435 st->codecpar->format = AV_PIX_FMT_BGRA64;
438 /* AviSynth 2.5 and AvxSynth pix_fmts */
440 st->codecpar->format = AV_PIX_FMT_BGR24;
443 st->codecpar->format = AV_PIX_FMT_RGB32;
446 st->codecpar->format = AV_PIX_FMT_YUYV422;
449 st->codecpar->format = AV_PIX_FMT_YUV420P;
452 case AVS_CS_I420: // Is this even used anywhere?
453 st->codecpar->format = AV_PIX_FMT_YUV420P;
457 av_log(s, AV_LOG_ERROR,
458 "unknown AviSynth colorspace %d\n", avs->vi->pixel_type);
460 return AVERROR_UNKNOWN;
464 #ifdef USING_AVISYNTH
465 case 5: // Planar RGB + Alpha
467 avs->planes = avs_planes_rgba;
469 case 4: // YUV + Alpha
471 avs->planes = avs_planes_yuva;
473 case 3: // Planar RGB
475 avs->planes = avs_planes_rgb;
480 avs->planes = avs_planes_grey;
484 avs->planes = avs_planes_yuv;
488 avs->planes = avs_planes_packed;
493 static int avisynth_create_stream_audio(AVFormatContext *s, AVStream *st)
495 AviSynthContext *avs = s->priv_data;
497 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
498 st->codecpar->sample_rate = avs->vi->audio_samples_per_second;
499 st->codecpar->channels = avs->vi->nchannels;
500 st->duration = avs->vi->num_audio_samples;
501 avpriv_set_pts_info(st, 64, 1, avs->vi->audio_samples_per_second);
503 switch (avs->vi->sample_type) {
504 case AVS_SAMPLE_INT8:
505 st->codecpar->codec_id = AV_CODEC_ID_PCM_U8;
507 case AVS_SAMPLE_INT16:
508 st->codecpar->codec_id = AV_CODEC_ID_PCM_S16LE;
510 case AVS_SAMPLE_INT24:
511 st->codecpar->codec_id = AV_CODEC_ID_PCM_S24LE;
513 case AVS_SAMPLE_INT32:
514 st->codecpar->codec_id = AV_CODEC_ID_PCM_S32LE;
516 case AVS_SAMPLE_FLOAT:
517 st->codecpar->codec_id = AV_CODEC_ID_PCM_F32LE;
520 av_log(s, AV_LOG_ERROR,
521 "unknown AviSynth sample type %d\n", avs->vi->sample_type);
523 return AVERROR_UNKNOWN;
528 static int avisynth_create_stream(AVFormatContext *s)
530 AviSynthContext *avs = s->priv_data;
535 if (avs_has_video(avs->vi)) {
536 st = avformat_new_stream(s, NULL);
538 return AVERROR_UNKNOWN;
540 if (ret = avisynth_create_stream_video(s, st))
543 if (avs_has_audio(avs->vi)) {
544 st = avformat_new_stream(s, NULL);
546 return AVERROR_UNKNOWN;
548 if (ret = avisynth_create_stream_audio(s, st))
554 static int avisynth_open_file(AVFormatContext *s)
556 AviSynthContext *avs = s->priv_data;
559 #ifdef USING_AVISYNTH
560 char filename_ansi[MAX_PATH * 4];
561 wchar_t filename_wc[MAX_PATH * 4];
564 if (ret = avisynth_context_create(s))
567 #ifdef USING_AVISYNTH
568 /* Convert UTF-8 to ANSI code page */
569 MultiByteToWideChar(CP_UTF8, 0, s->filename, -1, filename_wc, MAX_PATH * 4);
570 WideCharToMultiByte(CP_THREAD_ACP, 0, filename_wc, -1, filename_ansi,
571 MAX_PATH * 4, NULL, NULL);
572 arg = avs_new_value_string(filename_ansi);
574 arg = avs_new_value_string(s->filename);
576 val = avs_library.avs_invoke(avs->env, "Import", arg, 0);
577 if (avs_is_error(val)) {
578 av_log(s, AV_LOG_ERROR, "%s\n", avs_as_error(val));
579 ret = AVERROR_UNKNOWN;
582 if (!avs_is_clip(val)) {
583 av_log(s, AV_LOG_ERROR, "AviSynth script did not return a clip\n");
584 ret = AVERROR_UNKNOWN;
588 avs->clip = avs_library.avs_take_clip(val, avs->env);
589 avs->vi = avs_library.avs_get_video_info(avs->clip);
591 #ifdef USING_AVISYNTH
592 /* On Windows, FFmpeg supports AviSynth interface version 6 or higher.
593 * This includes AviSynth 2.6 RC1 or higher, and AviSynth+ r1718 or higher,
594 * and excludes 2.5 and the 2.6 alphas. Since AvxSynth identifies itself
595 * as interface version 3 like 2.5.8, this needs to be special-cased. */
597 if (avs_library.avs_get_version(avs->clip) < 6) {
598 av_log(s, AV_LOG_ERROR,
599 "AviSynth version is too old. Please upgrade to either AviSynth 2.6 >= RC1 or AviSynth+ >= r1718.\n");
600 ret = AVERROR_UNKNOWN;
605 /* Release the AVS_Value as it will go out of scope. */
606 avs_library.avs_release_value(val);
608 if (ret = avisynth_create_stream(s))
614 avisynth_context_destroy(avs);
618 static void avisynth_next_stream(AVFormatContext *s, AVStream **st,
619 AVPacket *pkt, int *discard)
621 AviSynthContext *avs = s->priv_data;
624 avs->curr_stream %= s->nb_streams;
626 *st = s->streams[avs->curr_stream];
627 if ((*st)->discard == AVDISCARD_ALL)
635 /* Copy AviSynth clip data into an AVPacket. */
636 static int avisynth_read_packet_video(AVFormatContext *s, AVPacket *pkt,
639 AviSynthContext *avs = s->priv_data;
640 AVS_VideoFrame *frame;
641 unsigned char *dst_p;
642 const unsigned char *src_p;
643 int n, i, plane, rowsize, planeheight, pitch, bits, ret;
645 int avsplus av_unused;
647 if (avs->curr_frame >= avs->vi->num_frames)
650 /* This must happen even if the stream is discarded to prevent desync. */
651 n = avs->curr_frame++;
655 #ifdef USING_AVISYNTH
656 /* Detect whether we're using AviSynth 2.6 or AviSynth+ by
657 * looking for whether avs_is_planar_rgb exists. */
658 if (GetProcAddress(avs_library.library, "avs_is_planar_rgb") == NULL)
663 /* avs_bits_per_pixel changed to AVSC_API with AviSynth 2.6, which
664 * requires going through avs_library, while AvxSynth has it under
665 * the older AVSC_INLINE type, so special-case this. */
667 bits = avs_library.avs_bits_per_pixel(avs->vi);
669 bits = avs_bits_per_pixel(avs->vi);
672 /* Without the cast to int64_t, calculation overflows at about 9k x 9k
674 pkt->size = (((int64_t)avs->vi->width *
675 (int64_t)avs->vi->height) * bits) / 8;
677 return AVERROR_UNKNOWN;
679 if ((ret = av_new_packet(pkt, pkt->size)) < 0)
685 pkt->stream_index = avs->curr_stream;
687 frame = avs_library.avs_get_frame(avs->clip, n);
688 error = avs_library.avs_clip_get_error(avs->clip);
690 av_log(s, AV_LOG_ERROR, "%s\n", error);
692 av_packet_unref(pkt);
693 return AVERROR_UNKNOWN;
697 for (i = 0; i < avs->n_planes; i++) {
698 plane = avs->planes[i];
699 #ifdef USING_AVISYNTH
700 src_p = avs_library.avs_get_read_ptr_p(frame, plane);
701 pitch = avs_library.avs_get_pitch_p(frame, plane);
703 rowsize = avs_library.avs_get_row_size_p(frame, plane);
704 planeheight = avs_library.avs_get_height_p(frame, plane);
706 src_p = avs_get_read_ptr_p(frame, plane);
707 pitch = avs_get_pitch_p(frame, plane);
709 rowsize = avs_get_row_size_p(frame, plane);
710 planeheight = avs_get_height_p(frame, plane);
713 /* Flip RGB video. */
714 if (avs_is_rgb24(avs->vi) || avs_is_rgb(avs->vi)) {
715 src_p = src_p + (planeheight - 1) * pitch;
719 #ifdef USING_AVISYNTH
720 /* Flip Planar RGB video */
721 if (avsplus && (avs_library.avs_is_planar_rgb(avs->vi) ||
722 avs_library.avs_is_planar_rgba(avs->vi))) {
723 src_p = src_p + (planeheight - 1) * pitch;
728 avs_library.avs_bit_blt(avs->env, dst_p, rowsize, src_p, pitch,
729 rowsize, planeheight);
730 dst_p += rowsize * planeheight;
733 avs_library.avs_release_video_frame(frame);
737 static int avisynth_read_packet_audio(AVFormatContext *s, AVPacket *pkt,
740 AviSynthContext *avs = s->priv_data;
741 AVRational fps, samplerate;
746 if (avs->curr_sample >= avs->vi->num_audio_samples)
749 fps.num = avs->vi->fps_numerator;
750 fps.den = avs->vi->fps_denominator;
751 samplerate.num = avs->vi->audio_samples_per_second;
754 if (avs_has_video(avs->vi)) {
755 if (avs->curr_frame < avs->vi->num_frames)
756 samples = av_rescale_q(avs->curr_frame, samplerate, fps) -
759 samples = av_rescale_q(1, samplerate, fps);
764 /* After seeking, audio may catch up with video. */
771 if (avs->curr_sample + samples > avs->vi->num_audio_samples)
772 samples = avs->vi->num_audio_samples - avs->curr_sample;
774 /* This must happen even if the stream is discarded to prevent desync. */
775 n = avs->curr_sample;
776 avs->curr_sample += samples;
780 pkt->size = avs_bytes_per_channel_sample(avs->vi) *
781 samples * avs->vi->nchannels;
783 return AVERROR_UNKNOWN;
785 if ((ret = av_new_packet(pkt, pkt->size)) < 0)
790 pkt->duration = samples;
791 pkt->stream_index = avs->curr_stream;
793 avs_library.avs_get_audio(avs->clip, pkt->data, n, samples);
794 error = avs_library.avs_clip_get_error(avs->clip);
796 av_log(s, AV_LOG_ERROR, "%s\n", error);
798 av_packet_unref(pkt);
799 return AVERROR_UNKNOWN;
804 static av_cold int avisynth_read_header(AVFormatContext *s)
808 // Calling library must implement a lock for thread-safe opens.
809 if (ret = ff_lock_avformat())
812 if (ret = avisynth_open_file(s)) {
813 ff_unlock_avformat();
817 ff_unlock_avformat();
821 static int avisynth_read_packet(AVFormatContext *s, AVPacket *pkt)
823 AviSynthContext *avs = s->priv_data;
829 return AVERROR_UNKNOWN;
831 /* If either stream reaches EOF, try to read the other one before
833 avisynth_next_stream(s, &st, pkt, &discard);
834 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
835 ret = avisynth_read_packet_video(s, pkt, discard);
836 if (ret == AVERROR_EOF && avs_has_audio(avs->vi)) {
837 avisynth_next_stream(s, &st, pkt, &discard);
838 return avisynth_read_packet_audio(s, pkt, discard);
841 ret = avisynth_read_packet_audio(s, pkt, discard);
842 if (ret == AVERROR_EOF && avs_has_video(avs->vi)) {
843 avisynth_next_stream(s, &st, pkt, &discard);
844 return avisynth_read_packet_video(s, pkt, discard);
851 static av_cold int avisynth_read_close(AVFormatContext *s)
853 if (ff_lock_avformat())
854 return AVERROR_UNKNOWN;
856 avisynth_context_destroy(s->priv_data);
857 ff_unlock_avformat();
861 static int avisynth_read_seek(AVFormatContext *s, int stream_index,
862 int64_t timestamp, int flags)
864 AviSynthContext *avs = s->priv_data;
866 AVRational fps, samplerate;
869 return AVERROR_UNKNOWN;
871 fps = (AVRational) { avs->vi->fps_numerator,
872 avs->vi->fps_denominator };
873 samplerate = (AVRational) { avs->vi->audio_samples_per_second, 1 };
875 st = s->streams[stream_index];
876 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
877 /* AviSynth frame counts are signed int. */
878 if ((timestamp >= avs->vi->num_frames) ||
879 (timestamp > INT_MAX) ||
882 avs->curr_frame = timestamp;
883 if (avs_has_audio(avs->vi))
884 avs->curr_sample = av_rescale_q(timestamp, samplerate, fps);
886 if ((timestamp >= avs->vi->num_audio_samples) || (timestamp < 0))
888 /* Force frame granularity for seeking. */
889 if (avs_has_video(avs->vi)) {
890 avs->curr_frame = av_rescale_q(timestamp, fps, samplerate);
891 avs->curr_sample = av_rescale_q(avs->curr_frame, samplerate, fps);
893 avs->curr_sample = timestamp;
900 AVInputFormat ff_avisynth_demuxer = {
902 .long_name = NULL_IF_CONFIG_SMALL("AviSynth script"),
903 .priv_data_size = sizeof(AviSynthContext),
904 .read_header = avisynth_read_header,
905 .read_packet = avisynth_read_packet,
906 .read_close = avisynth_read_close,
907 .read_seek = avisynth_read_seek,