]> git.sesse.net Git - ffmpeg/blob - libavformat/avisynth.c
Merge commit '6b58e11a8331690ec32e9869db89ae10c54614e9'
[ffmpeg] / libavformat / avisynth.c
1 /*
2  * Avi/AvxSynth support
3  * Copyright (c) 2012 AvxSynth Team.
4  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "avformat.h"
22 #include "internal.h"
23 #include "libavcodec/internal.h"
24
25 // Enable function pointer definitions for runtime loading.
26 #define AVSC_NO_DECLSPEC
27
28 // Shut up ffmpeg error messages.
29 // avisynth_c.h contains inline functions that call these functions.
30 #undef malloc
31 #undef free
32 #undef printf
33
34 // Platform-specific directives for AviSynth vs AvxSynth.
35 #ifdef _WIN32
36   #include <windows.h>
37   #undef EXTERN_C
38   #include "compat/avisynth/avisynth_c.h"
39   #define AVISYNTH_LIB "avisynth"
40 #else
41   #include <dlfcn.h>
42   #include "compat/avisynth/avxsynth_c.h"
43    #if defined (__APPLE__)
44      #define AVISYNTH_LIB "libavxsynth.dylib"
45    #else
46      #define AVISYNTH_LIB "libavxsynth.so"
47    #endif
48
49   #define LoadLibrary(x) dlopen(x, RTLD_NOW | RTLD_GLOBAL)
50   #define GetProcAddress dlsym
51   #define FreeLibrary dlclose
52 #endif
53
54 // AvxSynth doesn't have these colorspaces, so disable them
55 #ifndef _WIN32
56 #define avs_is_yv24(vi) 0
57 #define avs_is_yv16(vi) 0
58 #define avs_is_yv411(vi) 0
59 #define avs_is_y8(vi) 0
60 #endif
61
62 typedef struct {
63     void *library;
64 #define AVSC_DECLARE_FUNC(name) name##_func name
65     AVSC_DECLARE_FUNC(avs_bit_blt);
66     AVSC_DECLARE_FUNC(avs_clip_get_error);
67     AVSC_DECLARE_FUNC(avs_create_script_environment);
68     AVSC_DECLARE_FUNC(avs_delete_script_environment);
69     AVSC_DECLARE_FUNC(avs_get_audio);
70     AVSC_DECLARE_FUNC(avs_get_error);
71     AVSC_DECLARE_FUNC(avs_get_frame);
72     AVSC_DECLARE_FUNC(avs_get_version);
73     AVSC_DECLARE_FUNC(avs_get_video_info);
74     AVSC_DECLARE_FUNC(avs_invoke);
75     AVSC_DECLARE_FUNC(avs_release_clip);
76     AVSC_DECLARE_FUNC(avs_release_value);
77     AVSC_DECLARE_FUNC(avs_release_video_frame);
78     AVSC_DECLARE_FUNC(avs_take_clip);
79 #undef AVSC_DECLARE_FUNC
80 } AviSynthLibrary;
81
82 struct AviSynthContext {
83     AVS_ScriptEnvironment *env;
84     AVS_Clip *clip;
85     const AVS_VideoInfo *vi;
86
87     // avisynth_read_packet_video() iterates over this.
88     int n_planes;
89     const int *planes;
90
91     int curr_stream;
92     int curr_frame;
93     int64_t curr_sample;
94
95     int error;
96
97     // Linked list pointers.
98     struct AviSynthContext *next;
99 };
100 typedef struct AviSynthContext AviSynthContext;
101
102 static const int avs_planes_packed[1] = {0};
103 static const int avs_planes_grey[1] = {AVS_PLANAR_Y};
104 static const int avs_planes_yuv[3] = {AVS_PLANAR_Y, AVS_PLANAR_U, AVS_PLANAR_V};
105
106 // A conflict between C++ global objects, atexit, and dynamic loading requires
107 // us to register our own atexit handler to prevent double freeing.
108 static AviSynthLibrary *avs_library = NULL;
109 static int avs_atexit_called = 0;
110
111 // Linked list of AviSynthContexts. An atexit handler destroys this list.
112 static AviSynthContext *avs_ctx_list = NULL;
113
114 static av_cold void avisynth_atexit_handler(void);
115
116 static av_cold int avisynth_load_library(void) {
117     avs_library = av_mallocz(sizeof(AviSynthLibrary));
118     if (!avs_library)
119         return AVERROR_UNKNOWN;
120
121     avs_library->library = LoadLibrary(AVISYNTH_LIB);
122     if (!avs_library->library)
123         goto init_fail;
124
125 #define LOAD_AVS_FUNC(name, continue_on_fail) \
126 { \
127     avs_library->name = (void*)GetProcAddress(avs_library->library, #name); \
128     if(!continue_on_fail && !avs_library->name) \
129         goto fail; \
130 }
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 #undef LOAD_AVS_FUNC
146
147     atexit(avisynth_atexit_handler);
148     return 0;
149
150 fail:
151     FreeLibrary(avs_library->library);
152 init_fail:
153     av_freep(&avs_library);
154     return AVERROR_UNKNOWN;
155 }
156
157 // Note that avisynth_context_create and avisynth_context_destroy
158 // do not allocate or free the actual context! That is taken care of
159 // by libavformat.
160 static av_cold int avisynth_context_create(AVFormatContext *s) {
161     AviSynthContext *avs = (AviSynthContext *)s->priv_data;
162     int ret;
163
164     if (!avs_library) {
165         if (ret = avisynth_load_library())
166             return ret;
167     }
168
169     avs->env = avs_library->avs_create_script_environment(3);
170     if (avs_library->avs_get_error) {
171         const char *error = avs_library->avs_get_error(avs->env);
172         if (error) {
173             av_log(s, AV_LOG_ERROR, "%s\n", error);
174             return AVERROR_UNKNOWN;
175         }
176     }
177
178     if (!avs_ctx_list) {
179         avs_ctx_list = avs;
180     } else {
181         avs->next = avs_ctx_list;
182         avs_ctx_list = avs;
183     }
184
185     return 0;
186 }
187
188 static av_cold void avisynth_context_destroy(AviSynthContext *avs) {
189     if (avs_atexit_called)
190        return;
191
192     if (avs == avs_ctx_list) {
193         avs_ctx_list = avs->next;
194     } else {
195         AviSynthContext *prev = avs_ctx_list;
196         while (prev->next != avs)
197             prev = prev->next;
198         prev->next = avs->next;
199     }
200
201     if (avs->clip) {
202         avs_library->avs_release_clip(avs->clip);
203         avs->clip = NULL;
204     }
205     if (avs->env) {
206         avs_library->avs_delete_script_environment(avs->env);
207         avs->env = NULL;
208     }
209 }
210
211 static av_cold void avisynth_atexit_handler(void) {
212     AviSynthContext *avs = avs_ctx_list;
213
214     while (avs) {
215         AviSynthContext *next = avs->next;
216         avisynth_context_destroy(avs);
217         avs = next;
218     }
219     FreeLibrary(avs_library->library);
220     av_freep(&avs_library);
221
222     avs_atexit_called = 1;
223 }
224
225 // Create AVStream from audio and video data.
226 static int avisynth_create_stream_video(AVFormatContext *s, AVStream *st) {
227     AviSynthContext *avs = s->priv_data;
228     int planar = 0; // 0: packed, 1: YUV, 2: Y8
229
230     st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
231     st->codec->codec_id = CODEC_ID_RAWVIDEO;
232     st->codec->width = avs->vi->width;
233     st->codec->height = avs->vi->height;
234
235     st->time_base = (AVRational) {avs->vi->fps_denominator, avs->vi->fps_numerator};
236     st->avg_frame_rate = (AVRational) {avs->vi->fps_numerator, avs->vi->fps_denominator};
237     st->start_time = 0;
238     st->duration = avs->vi->num_frames;
239     st->nb_frames = avs->vi->num_frames;
240
241     switch (avs->vi->pixel_type) {
242 #ifdef _WIN32
243     case AVS_CS_YV24:
244         st->codec->pix_fmt = AV_PIX_FMT_YUV444P;
245         planar = 1;
246         break;
247     case AVS_CS_YV16:
248         st->codec->pix_fmt = AV_PIX_FMT_YUV422P;
249         planar = 1;
250         break;
251     case AVS_CS_YV411:
252         st->codec->pix_fmt = AV_PIX_FMT_YUV411P;
253         planar = 1;
254         break;
255     case AVS_CS_Y8:
256         st->codec->pix_fmt = AV_PIX_FMT_GRAY8;
257         planar = 2;
258         break;
259 #endif
260     case AVS_CS_BGR24:
261         st->codec->pix_fmt = AV_PIX_FMT_BGR24;
262         break;
263     case AVS_CS_BGR32:
264         st->codec->pix_fmt = AV_PIX_FMT_RGB32;
265         break;
266     case AVS_CS_YUY2:
267         st->codec->pix_fmt = AV_PIX_FMT_YUYV422;
268         break;
269     case AVS_CS_YV12:
270         st->codec->pix_fmt = AV_PIX_FMT_YUV420P;
271         planar = 1;
272         break;
273     case AVS_CS_I420: // Is this even used anywhere?
274         st->codec->pix_fmt = AV_PIX_FMT_YUV420P;
275         planar = 1;
276         break;
277     default:
278         av_log(s, AV_LOG_ERROR, "unknown AviSynth colorspace %d\n", avs->vi->pixel_type);
279         avs->error = 1;
280         return AVERROR_UNKNOWN;
281     }
282
283     switch (planar) {
284     case 2: // Y8
285         avs->n_planes = 1;
286         avs->planes = avs_planes_grey;
287         break;
288     case 1: // YUV
289         avs->n_planes = 3;
290         avs->planes = avs_planes_yuv;
291         break;
292     default:
293         avs->n_planes = 1;
294         avs->planes = avs_planes_packed;
295     }
296     return 0;
297 }
298
299 static int avisynth_create_stream_audio(AVFormatContext *s, AVStream *st) {
300     AviSynthContext *avs = s->priv_data;
301
302     st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
303     st->codec->sample_rate = avs->vi->audio_samples_per_second;
304     st->codec->channels = avs->vi->nchannels;
305     st->time_base = (AVRational) {1, avs->vi->audio_samples_per_second};
306
307     switch (avs->vi->sample_type) {
308     case AVS_SAMPLE_INT8:
309         st->codec->codec_id = CODEC_ID_PCM_U8;
310         break;
311     case AVS_SAMPLE_INT16:
312         st->codec->codec_id = CODEC_ID_PCM_S16LE;
313         break;
314     case AVS_SAMPLE_INT24:
315         st->codec->codec_id = CODEC_ID_PCM_S24LE;
316         break;
317     case AVS_SAMPLE_INT32:
318         st->codec->codec_id = CODEC_ID_PCM_S32LE;
319         break;
320     case AVS_SAMPLE_FLOAT:
321         st->codec->codec_id = CODEC_ID_PCM_F32LE;
322         break;
323     default:
324         av_log(s, AV_LOG_ERROR, "unknown AviSynth sample type %d\n", avs->vi->sample_type);
325         avs->error = 1;
326         return AVERROR_UNKNOWN;
327     }
328     return 0;
329 }
330
331 static int avisynth_create_stream(AVFormatContext *s) {
332     AviSynthContext *avs = s->priv_data;
333     AVStream *st;
334     int ret;
335     int id = 0;
336
337     if (avs_has_video(avs->vi)) {
338         st = avformat_new_stream(s, NULL);
339         if (!st)
340             return AVERROR_UNKNOWN;
341         st->id = id++;
342         if (ret = avisynth_create_stream_video(s, st))
343             return ret;
344     }
345     if (avs_has_audio(avs->vi)) {
346         st = avformat_new_stream(s, NULL);
347         if (!st)
348             return AVERROR_UNKNOWN;
349         st->id = id++;
350         if (ret = avisynth_create_stream_audio(s, st))
351             return ret;
352     }
353     return 0;
354 }
355
356 static int avisynth_open_file(AVFormatContext *s) {
357     AviSynthContext *avs = (AviSynthContext *)s->priv_data;
358     AVS_Value arg, val;
359     int ret;
360 #ifdef _WIN32
361     char filename_ansi[MAX_PATH * 4];
362     wchar_t filename_wc[MAX_PATH * 4];
363 #endif
364
365     if (ret = avisynth_context_create(s))
366         return ret;
367
368 #ifdef _WIN32
369     // Convert UTF-8 to ANSI code page
370     MultiByteToWideChar(CP_UTF8, 0, s->filename, -1, filename_wc, MAX_PATH * 4);
371     WideCharToMultiByte(CP_THREAD_ACP, 0, filename_wc, -1, filename_ansi, MAX_PATH * 4, NULL, NULL);
372     arg = avs_new_value_string(filename_ansi);
373 #else
374     arg = avs_new_value_string(s->filename);
375 #endif
376     val = avs_library->avs_invoke(avs->env, "Import", arg, 0);
377     if (avs_is_error(val)) {
378         av_log(s, AV_LOG_ERROR, "%s\n", avs_as_error(val));
379         ret = AVERROR_UNKNOWN;
380         goto fail;
381     }
382     if (!avs_is_clip(val)) {
383         av_log(s, AV_LOG_ERROR, "%s\n", "AviSynth script did not return a clip");
384         ret = AVERROR_UNKNOWN;
385         goto fail;
386     }
387
388     avs->clip = avs_library->avs_take_clip(val, avs->env);
389     avs->vi = avs_library->avs_get_video_info(avs->clip);
390
391     // Release the AVS_Value as it will go out of scope.
392     avs_library->avs_release_value(val);
393
394     if (ret = avisynth_create_stream(s))
395         goto fail;
396
397     return 0;
398
399 fail:
400     avisynth_context_destroy(avs);
401     return ret;
402 }
403
404 static void avisynth_next_stream(AVFormatContext *s, AVStream **st, AVPacket *pkt, int *discard) {
405     AviSynthContext *avs = s->priv_data;
406
407     pkt->stream_index = avs->curr_stream++;
408     avs->curr_stream %= s->nb_streams;
409
410     *st = s->streams[pkt->stream_index];
411     if ((*st)->discard == AVDISCARD_ALL)
412         *discard = 1;
413     else
414         *discard = 0;
415
416     return;
417 }
418
419 // Copy AviSynth clip data into an AVPacket.
420 static int avisynth_read_packet_video(AVFormatContext *s, AVPacket *pkt, int discard) {
421     AviSynthContext *avs = s->priv_data;
422     AVS_VideoFrame *frame;
423     unsigned char *dst_p;
424     const unsigned char *src_p;
425     int n, i, plane, rowsize, planeheight, pitch, bits;
426     const char *error;
427
428     if (avs->curr_frame >= avs->vi->num_frames)
429         return AVERROR_EOF;
430
431     // This must happen even if the stream is discarded to prevent desync.
432     n = avs->curr_frame++;
433     if (discard)
434         return 0;
435
436     pkt->pts = n;
437     pkt->dts = n;
438     pkt->duration = 1;
439
440     // Define the bpp values for the new AviSynth 2.6 colorspaces
441     if (avs_is_yv24(avs->vi)) {
442         bits = 24;
443     } else if (avs_is_yv16(avs->vi)) {
444         bits = 16;
445     } else if (avs_is_yv411(avs->vi)) {
446         bits = 12;
447     } else if (avs_is_y8(avs->vi)) {
448         bits = 8;
449     } else {
450         bits = avs_bits_per_pixel(avs->vi);
451     }
452
453     // Without cast to int64_t, calculation overflows at about 9k x 9k resolution.
454     pkt->size = (((int64_t)avs->vi->width * (int64_t)avs->vi->height) * bits) / 8;
455     if (!pkt->size)
456         return AVERROR_UNKNOWN;
457     pkt->data = av_malloc(pkt->size);
458     if (!pkt->data)
459         return AVERROR_UNKNOWN;
460
461     frame = avs_library->avs_get_frame(avs->clip, n);
462     error = avs_library->avs_clip_get_error(avs->clip);
463     if (error) {
464         av_log(s, AV_LOG_ERROR, "%s\n", error);
465         avs->error = 1;
466         av_freep(&pkt->data);
467         return AVERROR_UNKNOWN;
468     }
469
470     dst_p = pkt->data;
471     for (i = 0; i < avs->n_planes; i++) {
472         plane = avs->planes[i];
473         src_p = avs_get_read_ptr_p(frame, plane);
474         rowsize = avs_get_row_size_p(frame, plane);
475         planeheight = avs_get_height_p(frame, plane);
476         pitch = avs_get_pitch_p(frame, plane);
477
478         // Flip RGB video.
479         if (avs_is_rgb24(avs->vi) || avs_is_rgb(avs->vi)) {
480             src_p = src_p + (planeheight - 1) * pitch;
481             pitch = -pitch;
482         }
483
484     // An issue with avs_bit_blt on 2.5.8 prevents video from working correctly.
485     // This problem doesn't exist for 2.6 and AvxSynth, so enable the workaround
486     // for 2.5.8 only. This only displays the warning and exits if the script has
487     // video. 2.5.8's internal interface version is 3, so avs_get_version allows
488     // it to work only in the circumstance that the interface is 5 or higher (4 is
489     // unused).  There's a strong chance that AvxSynth, having been based on 2.5.8,
490     // would also be identified as interface version 3, but since AvxSynth doesn't
491     // suffer from this problem, special-case it.
492 #ifdef _WIN32
493     if (avs_library->avs_get_version(avs->clip) > 3) {
494         avs_library->avs_bit_blt(avs->env, dst_p, rowsize, src_p, pitch, rowsize, planeheight);
495     } else {
496         av_log(s, AV_LOG_ERROR, "Video input from AviSynth 2.5.8 is not supported. Please upgrade to 2.6.\n");
497         avs->error = 1;
498         av_freep(&pkt->data);
499         return AVERROR_UNKNOWN;
500     }
501 #else
502         avs_library->avs_bit_blt(avs->env, dst_p, rowsize, src_p, pitch, rowsize, planeheight);
503 #endif
504         dst_p += rowsize * planeheight;
505     }
506
507     avs_library->avs_release_video_frame(frame);
508     return 0;
509 }
510
511 static int avisynth_read_packet_audio(AVFormatContext *s, AVPacket *pkt, int discard) {
512     AviSynthContext *avs = s->priv_data;
513     AVRational fps, samplerate;
514     int samples;
515     int64_t n;
516     const char *error;
517
518     if (avs->curr_sample >= avs->vi->num_audio_samples)
519         return AVERROR_EOF;
520
521     fps.num = avs->vi->fps_numerator;
522     fps.den = avs->vi->fps_denominator;
523     samplerate.num = avs->vi->audio_samples_per_second;
524     samplerate.den = 1;
525
526     if (avs_has_video(avs->vi)) {
527         if (avs->curr_frame < avs->vi->num_frames)
528             samples = av_rescale_q(avs->curr_frame, samplerate, fps) - avs->curr_sample;
529         else
530             samples = av_rescale_q(1, samplerate, fps);
531     } else {
532         samples = 1000;
533     }
534
535     // After seeking, audio may catch up with video.
536     if (samples <= 0) {
537         pkt->size = 0;
538         pkt->data = NULL;
539         return 0;
540     }
541
542     if (avs->curr_sample + samples > avs->vi->num_audio_samples)
543         samples = avs->vi->num_audio_samples - avs->curr_sample;
544
545     // This must happen even if the stream is discarded to prevent desync.
546     n = avs->curr_sample;
547     avs->curr_sample += samples;
548     if (discard)
549         return 0;
550
551     pkt->pts = n;
552     pkt->dts = n;
553     pkt->duration = samples;
554
555     pkt->size = avs_bytes_per_channel_sample(avs->vi) * samples * avs->vi->nchannels;
556     if (!pkt->size)
557         return AVERROR_UNKNOWN;
558     pkt->data = av_malloc(pkt->size);
559     if (!pkt->data)
560         return AVERROR_UNKNOWN;
561
562     avs_library->avs_get_audio(avs->clip, pkt->data, n, samples);
563     error = avs_library->avs_clip_get_error(avs->clip);
564     if (error) {
565         av_log(s, AV_LOG_ERROR, "%s\n", error);
566         avs->error = 1;
567         av_freep(&pkt->data);
568         return AVERROR_UNKNOWN;
569     }
570     return 0;
571 }
572
573 static av_cold int avisynth_read_header(AVFormatContext *s) {
574     int ret;
575
576     // Calling library must implement a lock for thread-safe opens.
577     if (ret = avpriv_lock_avformat())
578         return ret;
579
580     if (ret = avisynth_open_file(s)) {
581         avpriv_unlock_avformat();
582         return ret;
583     }
584
585     avpriv_unlock_avformat();
586     return 0;
587 }
588
589 static int avisynth_read_packet(AVFormatContext *s, AVPacket *pkt) {
590     AviSynthContext *avs = s->priv_data;
591     AVStream *st;
592     int discard = 0;
593     int ret;
594
595     if (avs->error)
596         return AVERROR_UNKNOWN;
597
598     pkt->destruct = av_destruct_packet;
599
600     // If either stream reaches EOF, try to read the other one before giving up.
601     avisynth_next_stream(s, &st, pkt, &discard);
602     if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
603         ret = avisynth_read_packet_video(s, pkt, discard);
604         if (ret == AVERROR_EOF && avs_has_audio(avs->vi)) {
605             avisynth_next_stream(s, &st, pkt, &discard);
606             return avisynth_read_packet_audio(s, pkt, discard);
607         }
608         return ret;
609     } else {
610         ret = avisynth_read_packet_audio(s, pkt, discard);
611         if (ret == AVERROR_EOF && avs_has_video(avs->vi)) {
612             avisynth_next_stream(s, &st, pkt, &discard);
613             return avisynth_read_packet_video(s, pkt, discard);
614         }
615         return ret;
616     }
617 }
618
619 static av_cold int avisynth_read_close(AVFormatContext *s) {
620     if (avpriv_lock_avformat())
621         return AVERROR_UNKNOWN;
622
623     avisynth_context_destroy(s->priv_data);
624     avpriv_unlock_avformat();
625     return 0;
626 }
627
628 static int avisynth_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags) {
629     AviSynthContext *avs = s->priv_data;
630     AVStream *st;
631     AVRational fps, samplerate;
632
633     if (avs->error)
634         return AVERROR_UNKNOWN;
635
636     fps = (AVRational) {avs->vi->fps_numerator, avs->vi->fps_denominator};
637     samplerate = (AVRational) {avs->vi->audio_samples_per_second, 1};
638
639     st = s->streams[stream_index];
640     if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
641         // AviSynth frame counts are signed int.
642         if ((timestamp >= avs->vi->num_frames) || (timestamp > INT_MAX) || (timestamp < 0))
643             return AVERROR_EOF;
644         avs->curr_frame = timestamp;
645         if (avs_has_audio(avs->vi))
646             avs->curr_sample = av_rescale_q(timestamp, samplerate, fps);
647     } else {
648         if ((timestamp >= avs->vi->num_audio_samples) || (timestamp < 0))
649             return AVERROR_EOF;
650         // Force frame granularity for seeking.
651         if (avs_has_video(avs->vi)) {
652             avs->curr_frame = av_rescale_q(timestamp, fps, samplerate);
653             avs->curr_sample = av_rescale_q(avs->curr_frame, samplerate, fps);
654         } else {
655             avs->curr_sample = timestamp;
656         }
657     }
658
659     return 0;
660 }
661
662 AVInputFormat ff_avisynth_demuxer = {
663     .name           = "avisynth",
664     .long_name      = NULL_IF_CONFIG_SMALL("AviSynth script"),
665     .priv_data_size = sizeof(AviSynthContext),
666     .read_header    = avisynth_read_header,
667     .read_packet    = avisynth_read_packet,
668     .read_close     = avisynth_read_close,
669     .read_seek      = avisynth_read_seek,
670     .extensions     = "avs",
671 };