]> git.sesse.net Git - ffmpeg/blob - libavformat/avisynth.c
hwcontext_vulkan: remove plane size alignment checks when host importing
[ffmpeg] / libavformat / avisynth.c
1 /*
2  * AviSynth(+) support
3  * Copyright (c) 2012 AvxSynth Team
4  *
5  * This file is part of FFmpeg
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include "libavutil/attributes.h"
23 #include "libavutil/internal.h"
24
25 #include "libavcodec/internal.h"
26
27 #include "avformat.h"
28 #include "internal.h"
29 #include "config.h"
30
31 /* Enable function pointer definitions for runtime loading. */
32 #define AVSC_NO_DECLSPEC
33
34 /* Platform-specific directives. */
35 #ifdef _WIN32
36   #include "compat/w32dlfcn.h"
37   #undef EXTERN_C
38   #define AVISYNTH_LIB "avisynth"
39 #else
40   #include <dlfcn.h>
41   #define AVISYNTH_NAME "libavisynth"
42   #define AVISYNTH_LIB AVISYNTH_NAME SLIBSUF
43 #endif
44
45 #include <avisynth/avisynth_c.h>
46
47 typedef struct AviSynthLibrary {
48     void *library;
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_release_clip);
61     AVSC_DECLARE_FUNC(avs_release_value);
62     AVSC_DECLARE_FUNC(avs_release_video_frame);
63     AVSC_DECLARE_FUNC(avs_take_clip);
64     AVSC_DECLARE_FUNC(avs_bits_per_pixel);
65     AVSC_DECLARE_FUNC(avs_get_height_p);
66     AVSC_DECLARE_FUNC(avs_get_pitch_p);
67     AVSC_DECLARE_FUNC(avs_get_read_ptr_p);
68     AVSC_DECLARE_FUNC(avs_get_row_size_p);
69     AVSC_DECLARE_FUNC(avs_is_planar_rgb);
70     AVSC_DECLARE_FUNC(avs_is_planar_rgba);
71 #undef AVSC_DECLARE_FUNC
72 } AviSynthLibrary;
73
74 typedef struct AviSynthContext {
75     AVS_ScriptEnvironment *env;
76     AVS_Clip *clip;
77     const AVS_VideoInfo *vi;
78
79     /* avisynth_read_packet_video() iterates over this. */
80     int n_planes;
81     const int *planes;
82
83     int curr_stream;
84     int curr_frame;
85     int64_t curr_sample;
86
87     int error;
88
89     /* Linked list pointers. */
90     struct AviSynthContext *next;
91 } AviSynthContext;
92
93 static const int avs_planes_packed[1] = { 0 };
94 static const int avs_planes_grey[1]   = { AVS_PLANAR_Y };
95 static const int avs_planes_yuv[3]    = { AVS_PLANAR_Y, AVS_PLANAR_U,
96                                           AVS_PLANAR_V };
97 static const int avs_planes_rgb[3]    = { AVS_PLANAR_G, AVS_PLANAR_B,
98                                           AVS_PLANAR_R };
99 static const int avs_planes_yuva[4]   = { AVS_PLANAR_Y, AVS_PLANAR_U,
100                                           AVS_PLANAR_V, AVS_PLANAR_A };
101 static const int avs_planes_rgba[4]   = { AVS_PLANAR_G, AVS_PLANAR_B,
102                                           AVS_PLANAR_R, AVS_PLANAR_A };
103
104 /* A conflict between C++ global objects, atexit, and dynamic loading requires
105  * us to register our own atexit handler to prevent double freeing. */
106 static AviSynthLibrary avs_library;
107 static int avs_atexit_called        = 0;
108
109 /* Linked list of AviSynthContexts. An atexit handler destroys this list. */
110 static AviSynthContext *avs_ctx_list = NULL;
111
112 static av_cold void avisynth_atexit_handler(void);
113
114 static av_cold int avisynth_load_library(void)
115 {
116     avs_library.library = dlopen(AVISYNTH_LIB, RTLD_NOW | RTLD_LOCAL);
117     if (!avs_library.library)
118         return AVERROR_UNKNOWN;
119
120 #define LOAD_AVS_FUNC(name, continue_on_fail)                          \
121         avs_library.name = (name ## _func)                             \
122                            dlsym(avs_library.library, #name);          \
123         if (!continue_on_fail && !avs_library.name)                    \
124             goto fail;
125
126     LOAD_AVS_FUNC(avs_bit_blt, 0);
127     LOAD_AVS_FUNC(avs_clip_get_error, 0);
128     LOAD_AVS_FUNC(avs_create_script_environment, 0);
129     LOAD_AVS_FUNC(avs_delete_script_environment, 0);
130     LOAD_AVS_FUNC(avs_get_audio, 0);
131     LOAD_AVS_FUNC(avs_get_error, 1); // New to AviSynth 2.6
132     LOAD_AVS_FUNC(avs_get_frame, 0);
133     LOAD_AVS_FUNC(avs_get_version, 0);
134     LOAD_AVS_FUNC(avs_get_video_info, 0);
135     LOAD_AVS_FUNC(avs_invoke, 0);
136     LOAD_AVS_FUNC(avs_release_clip, 0);
137     LOAD_AVS_FUNC(avs_release_value, 0);
138     LOAD_AVS_FUNC(avs_release_video_frame, 0);
139     LOAD_AVS_FUNC(avs_take_clip, 0);
140     LOAD_AVS_FUNC(avs_bits_per_pixel, 1);
141     LOAD_AVS_FUNC(avs_get_height_p, 1);
142     LOAD_AVS_FUNC(avs_get_pitch_p, 1);
143     LOAD_AVS_FUNC(avs_get_read_ptr_p, 1);
144     LOAD_AVS_FUNC(avs_get_row_size_p, 1);
145     LOAD_AVS_FUNC(avs_is_planar_rgb, 1);
146     LOAD_AVS_FUNC(avs_is_planar_rgba, 1);
147 #undef LOAD_AVS_FUNC
148
149     atexit(avisynth_atexit_handler);
150     return 0;
151
152 fail:
153     dlclose(avs_library.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 {
162     AviSynthContext *avs = s->priv_data;
163     int ret;
164
165     if (!avs_library.library)
166         if (ret = avisynth_load_library())
167             return ret;
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 {
190     if (avs_atexit_called)
191         return;
192
193     if (avs == avs_ctx_list) {
194         avs_ctx_list = avs->next;
195     } else {
196         AviSynthContext *prev = avs_ctx_list;
197         while (prev->next != avs)
198             prev = prev->next;
199         prev->next = avs->next;
200     }
201
202     if (avs->clip) {
203         avs_library.avs_release_clip(avs->clip);
204         avs->clip = NULL;
205     }
206     if (avs->env) {
207         avs_library.avs_delete_script_environment(avs->env);
208         avs->env = NULL;
209     }
210 }
211
212 static av_cold void avisynth_atexit_handler(void)
213 {
214     AviSynthContext *avs = avs_ctx_list;
215
216     while (avs) {
217         AviSynthContext *next = avs->next;
218         avisynth_context_destroy(avs);
219         avs = next;
220     }
221     dlclose(avs_library.library);
222
223     avs_atexit_called = 1;
224 }
225
226 /* Create AVStream from audio and video data. */
227 static int avisynth_create_stream_video(AVFormatContext *s, AVStream *st)
228 {
229     AviSynthContext *avs = s->priv_data;
230     int planar = 0; // 0: packed, 1: YUV, 2: Y8, 3: Planar RGB, 4: YUVA, 5: Planar RGBA
231
232     st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
233     st->codecpar->codec_id   = AV_CODEC_ID_RAWVIDEO;
234     st->codecpar->width      = avs->vi->width;
235     st->codecpar->height     = avs->vi->height;
236
237     st->avg_frame_rate    = (AVRational) { avs->vi->fps_numerator,
238                                            avs->vi->fps_denominator };
239     st->start_time        = 0;
240     st->duration          = avs->vi->num_frames;
241     st->nb_frames         = avs->vi->num_frames;
242     avpriv_set_pts_info(st, 32, avs->vi->fps_denominator, avs->vi->fps_numerator);
243
244     switch (avs->vi->pixel_type) {
245     /* 10~16-bit YUV pix_fmts (AviSynth+) */
246     case AVS_CS_YUV444P10:
247         st->codecpar->format = AV_PIX_FMT_YUV444P10;
248         planar               = 1;
249         break;
250     case AVS_CS_YUV422P10:
251         st->codecpar->format = AV_PIX_FMT_YUV422P10;
252         planar               = 1;
253         break;
254     case AVS_CS_YUV420P10:
255         st->codecpar->format = AV_PIX_FMT_YUV420P10;
256         planar               = 1;
257         break;
258     case AVS_CS_YUV444P12:
259         st->codecpar->format = AV_PIX_FMT_YUV444P12;
260         planar               = 1;
261         break;
262     case AVS_CS_YUV422P12:
263         st->codecpar->format = AV_PIX_FMT_YUV422P12;
264         planar               = 1;
265         break;
266     case AVS_CS_YUV420P12:
267         st->codecpar->format = AV_PIX_FMT_YUV420P12;
268         planar               = 1;
269         break;
270     case AVS_CS_YUV444P14:
271         st->codecpar->format = AV_PIX_FMT_YUV444P14;
272         planar               = 1;
273         break;
274     case AVS_CS_YUV422P14:
275         st->codecpar->format = AV_PIX_FMT_YUV422P14;
276         planar               = 1;
277         break;
278     case AVS_CS_YUV420P14:
279         st->codecpar->format = AV_PIX_FMT_YUV420P14;
280         planar               = 1;
281         break;
282     case AVS_CS_YUV444P16:
283         st->codecpar->format = AV_PIX_FMT_YUV444P16;
284         planar               = 1;
285         break;
286     case AVS_CS_YUV422P16:
287         st->codecpar->format = AV_PIX_FMT_YUV422P16;
288         planar               = 1;
289         break;
290     case AVS_CS_YUV420P16:
291         st->codecpar->format = AV_PIX_FMT_YUV420P16;
292         planar               = 1;
293         break;
294     /* 8~16-bit YUV pix_fmts with Alpha (AviSynth+) */
295     case AVS_CS_YUVA444:
296         st->codecpar->format = AV_PIX_FMT_YUVA444P;
297         planar               = 4;
298         break;
299     case AVS_CS_YUVA422:
300         st->codecpar->format = AV_PIX_FMT_YUVA422P;
301         planar               = 4;
302         break;
303     case AVS_CS_YUVA420:
304         st->codecpar->format = AV_PIX_FMT_YUVA420P;
305         planar               = 4;
306         break;
307     case AVS_CS_YUVA444P10:
308         st->codecpar->format = AV_PIX_FMT_YUVA444P10;
309         planar               = 4;
310         break;
311     case AVS_CS_YUVA422P10:
312         st->codecpar->format = AV_PIX_FMT_YUVA422P10;
313         planar               = 4;
314         break;
315     case AVS_CS_YUVA420P10:
316         st->codecpar->format = AV_PIX_FMT_YUVA420P10;
317         planar               = 4;
318         break;
319     case AVS_CS_YUVA422P12:
320         st->codecpar->format = AV_PIX_FMT_YUVA422P12;
321         planar               = 4;
322         break;
323     case AVS_CS_YUVA444P16:
324         st->codecpar->format = AV_PIX_FMT_YUVA444P16;
325         planar               = 4;
326         break;
327     case AVS_CS_YUVA422P16:
328         st->codecpar->format = AV_PIX_FMT_YUVA422P16;
329         planar               = 4;
330         break;
331     case AVS_CS_YUVA420P16:
332         st->codecpar->format = AV_PIX_FMT_YUVA420P16;
333         planar               = 4;
334         break;
335     /* Planar RGB pix_fmts (AviSynth+) */
336     case AVS_CS_RGBP:
337         st->codecpar->format = AV_PIX_FMT_GBRP;
338         planar               = 3;
339         break;
340     case AVS_CS_RGBP10:
341         st->codecpar->format = AV_PIX_FMT_GBRP10;
342         planar               = 3;
343         break;
344     case AVS_CS_RGBP12:
345         st->codecpar->format = AV_PIX_FMT_GBRP12;
346         planar               = 3;
347         break;
348     case AVS_CS_RGBP14:
349         st->codecpar->format = AV_PIX_FMT_GBRP14;
350         planar               = 3;
351         break;
352     case AVS_CS_RGBP16:
353         st->codecpar->format = AV_PIX_FMT_GBRP16;
354         planar               = 3;
355         break;
356     /* Single precision floating point Planar RGB (AviSynth+) */
357     case AVS_CS_RGBPS:
358         st->codecpar->format = AV_PIX_FMT_GBRPF32;
359         planar               = 3;
360         break;
361     /* Planar RGB pix_fmts with Alpha (AviSynth+) */
362     case AVS_CS_RGBAP:
363         st->codecpar->format = AV_PIX_FMT_GBRAP;
364         planar               = 5;
365         break;
366     case AVS_CS_RGBAP10:
367         st->codecpar->format = AV_PIX_FMT_GBRAP10;
368         planar               = 5;
369         break;
370     case AVS_CS_RGBAP12:
371         st->codecpar->format = AV_PIX_FMT_GBRAP12;
372         planar               = 5;
373         break;
374     case AVS_CS_RGBAP16:
375         st->codecpar->format = AV_PIX_FMT_GBRAP16;
376         planar               = 5;
377         break;
378     /* Single precision floating point Planar RGB with Alpha (AviSynth+) */
379     case AVS_CS_RGBAPS:
380         st->codecpar->format = AV_PIX_FMT_GBRAPF32;
381         planar               = 5;
382         break;
383     /* 10~16-bit gray pix_fmts (AviSynth+) */
384     case AVS_CS_Y10:
385         st->codecpar->format = AV_PIX_FMT_GRAY10;
386         planar               = 2;
387         break;
388     case AVS_CS_Y12:
389         st->codecpar->format = AV_PIX_FMT_GRAY12;
390         planar               = 2;
391         break;
392     case AVS_CS_Y14:
393         st->codecpar->format = AV_PIX_FMT_GRAY14;
394         planar               = 2;
395         break;
396     case AVS_CS_Y16:
397         st->codecpar->format = AV_PIX_FMT_GRAY16;
398         planar               = 2;
399         break;
400     /* Single precision floating point gray (AviSynth+) */
401     case AVS_CS_Y32:
402         st->codecpar->format = AV_PIX_FMT_GRAYF32;
403         planar               = 2;
404         break;
405     /* pix_fmts added in AviSynth 2.6 */
406     case AVS_CS_YV24:
407         st->codecpar->format = AV_PIX_FMT_YUV444P;
408         planar               = 1;
409         break;
410     case AVS_CS_YV16:
411         st->codecpar->format = AV_PIX_FMT_YUV422P;
412         planar               = 1;
413         break;
414     case AVS_CS_YV411:
415         st->codecpar->format = AV_PIX_FMT_YUV411P;
416         planar               = 1;
417         break;
418     case AVS_CS_Y8:
419         st->codecpar->format = AV_PIX_FMT_GRAY8;
420         planar               = 2;
421         break;
422     /* 16-bit packed RGB pix_fmts (AviSynth+) */
423     case AVS_CS_BGR48:
424         st->codecpar->format = AV_PIX_FMT_BGR48;
425         break;
426     case AVS_CS_BGR64:
427         st->codecpar->format = AV_PIX_FMT_BGRA64;
428         break;
429     /* AviSynth 2.5 pix_fmts */
430     case AVS_CS_BGR24:
431         st->codecpar->format = AV_PIX_FMT_BGR24;
432         break;
433     case AVS_CS_BGR32:
434         st->codecpar->format = AV_PIX_FMT_RGB32;
435         break;
436     case AVS_CS_YUY2:
437         st->codecpar->format = AV_PIX_FMT_YUYV422;
438         break;
439     case AVS_CS_YV12:
440         st->codecpar->format = AV_PIX_FMT_YUV420P;
441         planar               = 1;
442         break;
443     case AVS_CS_I420: // Is this even used anywhere?
444         st->codecpar->format = AV_PIX_FMT_YUV420P;
445         planar               = 1;
446         break;
447     default:
448         av_log(s, AV_LOG_ERROR,
449                "unknown AviSynth colorspace %d\n", avs->vi->pixel_type);
450         avs->error = 1;
451         return AVERROR_UNKNOWN;
452     }
453
454     switch (planar) {
455     case 5: // Planar RGB + Alpha
456         avs->n_planes = 4;
457         avs->planes   = avs_planes_rgba;
458         break;
459     case 4: // YUV + Alpha
460         avs->n_planes = 4;
461         avs->planes   = avs_planes_yuva;
462         break;
463     case 3: // Planar RGB
464         avs->n_planes = 3;
465         avs->planes   = avs_planes_rgb;
466         break;
467     case 2: // Y8
468         avs->n_planes = 1;
469         avs->planes   = avs_planes_grey;
470         break;
471     case 1: // YUV
472         avs->n_planes = 3;
473         avs->planes   = avs_planes_yuv;
474         break;
475     default:
476         avs->n_planes = 1;
477         avs->planes   = avs_planes_packed;
478     }
479     return 0;
480 }
481
482 static int avisynth_create_stream_audio(AVFormatContext *s, AVStream *st)
483 {
484     AviSynthContext *avs = s->priv_data;
485
486     st->codecpar->codec_type  = AVMEDIA_TYPE_AUDIO;
487     st->codecpar->sample_rate = avs->vi->audio_samples_per_second;
488     st->codecpar->channels    = avs->vi->nchannels;
489     st->duration              = avs->vi->num_audio_samples;
490     avpriv_set_pts_info(st, 64, 1, avs->vi->audio_samples_per_second);
491
492     switch (avs->vi->sample_type) {
493     case AVS_SAMPLE_INT8:
494         st->codecpar->codec_id = AV_CODEC_ID_PCM_U8;
495         break;
496     case AVS_SAMPLE_INT16:
497         st->codecpar->codec_id = AV_CODEC_ID_PCM_S16LE;
498         break;
499     case AVS_SAMPLE_INT24:
500         st->codecpar->codec_id = AV_CODEC_ID_PCM_S24LE;
501         break;
502     case AVS_SAMPLE_INT32:
503         st->codecpar->codec_id = AV_CODEC_ID_PCM_S32LE;
504         break;
505     case AVS_SAMPLE_FLOAT:
506         st->codecpar->codec_id = AV_CODEC_ID_PCM_F32LE;
507         break;
508     default:
509         av_log(s, AV_LOG_ERROR,
510                "unknown AviSynth sample type %d\n", avs->vi->sample_type);
511         avs->error = 1;
512         return AVERROR_UNKNOWN;
513     }
514     return 0;
515 }
516
517 static int avisynth_create_stream(AVFormatContext *s)
518 {
519     AviSynthContext *avs = s->priv_data;
520     AVStream *st;
521     int ret;
522     int id = 0;
523
524     if (avs_has_video(avs->vi)) {
525         st = avformat_new_stream(s, NULL);
526         if (!st)
527             return AVERROR_UNKNOWN;
528         st->id = id++;
529         if (ret = avisynth_create_stream_video(s, st))
530             return ret;
531     }
532     if (avs_has_audio(avs->vi)) {
533         st = avformat_new_stream(s, NULL);
534         if (!st)
535             return AVERROR_UNKNOWN;
536         st->id = id++;
537         if (ret = avisynth_create_stream_audio(s, st))
538             return ret;
539     }
540     return 0;
541 }
542
543 static int avisynth_open_file(AVFormatContext *s)
544 {
545     AviSynthContext *avs = s->priv_data;
546     AVS_Value arg, val;
547     int ret;
548 #ifdef _WIN32
549     char filename_ansi[MAX_PATH * 4];
550     wchar_t filename_wc[MAX_PATH * 4];
551 #endif
552
553     if (ret = avisynth_context_create(s))
554         return ret;
555
556 #ifdef _WIN32
557     /* Convert UTF-8 to ANSI code page */
558     MultiByteToWideChar(CP_UTF8, 0, s->url, -1, filename_wc, MAX_PATH * 4);
559     WideCharToMultiByte(CP_THREAD_ACP, 0, filename_wc, -1, filename_ansi,
560                         MAX_PATH * 4, NULL, NULL);
561     arg = avs_new_value_string(filename_ansi);
562 #else
563     arg = avs_new_value_string(s->url);
564 #endif
565     val = avs_library.avs_invoke(avs->env, "Import", arg, 0);
566     if (avs_is_error(val)) {
567         av_log(s, AV_LOG_ERROR, "%s\n", avs_as_error(val));
568         ret = AVERROR_UNKNOWN;
569         goto fail;
570     }
571     if (!avs_is_clip(val)) {
572         av_log(s, AV_LOG_ERROR, "AviSynth script did not return a clip\n");
573         ret = AVERROR_UNKNOWN;
574         goto fail;
575     }
576
577     avs->clip = avs_library.avs_take_clip(val, avs->env);
578     avs->vi   = avs_library.avs_get_video_info(avs->clip);
579
580     /* On Windows, FFmpeg supports AviSynth interface version 6 or higher.
581      * This includes AviSynth 2.6 RC1 or higher, and AviSynth+ r1718 or higher,
582      * and excludes 2.5 and the 2.6 alphas. */
583
584     if (avs_library.avs_get_version(avs->clip) < 6) {
585         av_log(s, AV_LOG_ERROR,
586                "AviSynth version is too old. Please upgrade to either AviSynth 2.6 >= RC1 or AviSynth+ >= r1718.\n");
587         ret = AVERROR_UNKNOWN;
588         goto fail;
589     }
590
591     /* Release the AVS_Value as it will go out of scope. */
592     avs_library.avs_release_value(val);
593
594     if (ret = avisynth_create_stream(s))
595         goto fail;
596
597     return 0;
598
599 fail:
600     avisynth_context_destroy(avs);
601     return ret;
602 }
603
604 static void avisynth_next_stream(AVFormatContext *s, AVStream **st,
605                                  AVPacket *pkt, int *discard)
606 {
607     AviSynthContext *avs = s->priv_data;
608
609     avs->curr_stream++;
610     avs->curr_stream %= s->nb_streams;
611
612     *st = s->streams[avs->curr_stream];
613     if ((*st)->discard == AVDISCARD_ALL)
614         *discard = 1;
615     else
616         *discard = 0;
617
618     return;
619 }
620
621 /* Copy AviSynth clip data into an AVPacket. */
622 static int avisynth_read_packet_video(AVFormatContext *s, AVPacket *pkt,
623                                       int discard)
624 {
625     AviSynthContext *avs = s->priv_data;
626     AVS_VideoFrame *frame;
627     unsigned char *dst_p;
628     const unsigned char *src_p;
629     int n, i, plane, rowsize, planeheight, pitch, bits, ret;
630     const char *error;
631     int avsplus av_unused;
632
633     if (avs->curr_frame >= avs->vi->num_frames)
634         return AVERROR_EOF;
635
636     /* This must happen even if the stream is discarded to prevent desync. */
637     n = avs->curr_frame++;
638     if (discard)
639         return 0;
640
641 #ifdef _WIN32
642     /* Detect whether we're using AviSynth 2.6 or AviSynth+ by
643      * looking for whether avs_is_planar_rgb exists. */
644     if (GetProcAddress(avs_library.library, "avs_is_planar_rgb") == NULL)
645         avsplus = 0;
646     else
647         avsplus = 1;
648 #else
649     /* AviSynth+ is now the only variant of AviSynth we support
650      * on Linux and macOS. */
651     avsplus = 1;
652 #endif
653
654     bits = avs_library.avs_bits_per_pixel(avs->vi);
655
656     /* Without the cast to int64_t, calculation overflows at about 9k x 9k
657      * resolution. */
658     pkt->size = (((int64_t)avs->vi->width *
659                   (int64_t)avs->vi->height) * bits) / 8;
660     if (!pkt->size)
661         return AVERROR_UNKNOWN;
662
663     if ((ret = av_new_packet(pkt, pkt->size)) < 0)
664         return ret;
665
666     pkt->pts      = n;
667     pkt->dts      = n;
668     pkt->duration = 1;
669     pkt->stream_index = avs->curr_stream;
670
671     frame = avs_library.avs_get_frame(avs->clip, n);
672     error = avs_library.avs_clip_get_error(avs->clip);
673     if (error) {
674         av_log(s, AV_LOG_ERROR, "%s\n", error);
675         avs->error = 1;
676         av_packet_unref(pkt);
677         return AVERROR_UNKNOWN;
678     }
679
680     dst_p = pkt->data;
681     for (i = 0; i < avs->n_planes; i++) {
682         plane = avs->planes[i];
683         src_p = avs_library.avs_get_read_ptr_p(frame, plane);
684         pitch = avs_library.avs_get_pitch_p(frame, plane);
685
686         rowsize     = avs_library.avs_get_row_size_p(frame, plane);
687         planeheight = avs_library.avs_get_height_p(frame, plane);
688
689         /* Flip RGB video. */
690         if (avs_is_rgb24(avs->vi) || avs_is_rgb(avs->vi)) {
691             src_p = src_p + (planeheight - 1) * pitch;
692             pitch = -pitch;
693         }
694
695         /* Flip Planar RGB video */
696         if (avsplus && (avs_library.avs_is_planar_rgb(avs->vi) ||
697                         avs_library.avs_is_planar_rgba(avs->vi))) {
698             src_p = src_p + (planeheight - 1) * pitch;
699             pitch = -pitch;
700         }
701
702         avs_library.avs_bit_blt(avs->env, dst_p, rowsize, src_p, pitch,
703                                  rowsize, planeheight);
704         dst_p += rowsize * planeheight;
705     }
706
707     avs_library.avs_release_video_frame(frame);
708     return 0;
709 }
710
711 static int avisynth_read_packet_audio(AVFormatContext *s, AVPacket *pkt,
712                                       int discard)
713 {
714     AviSynthContext *avs = s->priv_data;
715     AVRational fps, samplerate;
716     int samples, ret;
717     int64_t n;
718     const char *error;
719
720     if (avs->curr_sample >= avs->vi->num_audio_samples)
721         return AVERROR_EOF;
722
723     fps.num        = avs->vi->fps_numerator;
724     fps.den        = avs->vi->fps_denominator;
725     samplerate.num = avs->vi->audio_samples_per_second;
726     samplerate.den = 1;
727
728     if (avs_has_video(avs->vi)) {
729         if (avs->curr_frame < avs->vi->num_frames)
730             samples = av_rescale_q(avs->curr_frame, samplerate, fps) -
731                       avs->curr_sample;
732         else
733             samples = av_rescale_q(1, samplerate, fps);
734     } else {
735         samples = 1000;
736     }
737
738     /* After seeking, audio may catch up with video. */
739     if (samples <= 0) {
740         pkt->size = 0;
741         pkt->data = NULL;
742         return 0;
743     }
744
745     if (avs->curr_sample + samples > avs->vi->num_audio_samples)
746         samples = avs->vi->num_audio_samples - avs->curr_sample;
747
748     /* This must happen even if the stream is discarded to prevent desync. */
749     n                 = avs->curr_sample;
750     avs->curr_sample += samples;
751     if (discard)
752         return 0;
753
754     pkt->size = avs_bytes_per_channel_sample(avs->vi) *
755                 samples * avs->vi->nchannels;
756     if (!pkt->size)
757         return AVERROR_UNKNOWN;
758
759     if ((ret = av_new_packet(pkt, pkt->size)) < 0)
760         return ret;
761
762     pkt->pts      = n;
763     pkt->dts      = n;
764     pkt->duration = samples;
765     pkt->stream_index = avs->curr_stream;
766
767     avs_library.avs_get_audio(avs->clip, pkt->data, n, samples);
768     error = avs_library.avs_clip_get_error(avs->clip);
769     if (error) {
770         av_log(s, AV_LOG_ERROR, "%s\n", error);
771         avs->error = 1;
772         av_packet_unref(pkt);
773         return AVERROR_UNKNOWN;
774     }
775     return 0;
776 }
777
778 static av_cold int avisynth_read_header(AVFormatContext *s)
779 {
780     int ret;
781
782     // Calling library must implement a lock for thread-safe opens.
783     if (ret = ff_lock_avformat())
784         return ret;
785
786     if (ret = avisynth_open_file(s)) {
787         ff_unlock_avformat();
788         return ret;
789     }
790
791     ff_unlock_avformat();
792     return 0;
793 }
794
795 static int avisynth_read_packet(AVFormatContext *s, AVPacket *pkt)
796 {
797     AviSynthContext *avs = s->priv_data;
798     AVStream *st;
799     int discard = 0;
800     int ret;
801
802     if (avs->error)
803         return AVERROR_UNKNOWN;
804
805     /* If either stream reaches EOF, try to read the other one before
806      * giving up. */
807     avisynth_next_stream(s, &st, pkt, &discard);
808     if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
809         ret = avisynth_read_packet_video(s, pkt, discard);
810         if (ret == AVERROR_EOF && avs_has_audio(avs->vi)) {
811             avisynth_next_stream(s, &st, pkt, &discard);
812             return avisynth_read_packet_audio(s, pkt, discard);
813         }
814     } else {
815         ret = avisynth_read_packet_audio(s, pkt, discard);
816         if (ret == AVERROR_EOF && avs_has_video(avs->vi)) {
817             avisynth_next_stream(s, &st, pkt, &discard);
818             return avisynth_read_packet_video(s, pkt, discard);
819         }
820     }
821
822     return ret;
823 }
824
825 static av_cold int avisynth_read_close(AVFormatContext *s)
826 {
827     if (ff_lock_avformat())
828         return AVERROR_UNKNOWN;
829
830     avisynth_context_destroy(s->priv_data);
831     ff_unlock_avformat();
832     return 0;
833 }
834
835 static int avisynth_read_seek(AVFormatContext *s, int stream_index,
836                               int64_t timestamp, int flags)
837 {
838     AviSynthContext *avs = s->priv_data;
839     AVStream *st;
840     AVRational fps, samplerate;
841
842     if (avs->error)
843         return AVERROR_UNKNOWN;
844
845     fps        = (AVRational) { avs->vi->fps_numerator,
846                                 avs->vi->fps_denominator };
847     samplerate = (AVRational) { avs->vi->audio_samples_per_second, 1 };
848
849     st = s->streams[stream_index];
850     if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
851         /* AviSynth frame counts are signed int. */
852         if ((timestamp >= avs->vi->num_frames) ||
853             (timestamp > INT_MAX)              ||
854             (timestamp < 0))
855             return AVERROR_EOF;
856         avs->curr_frame = timestamp;
857         if (avs_has_audio(avs->vi))
858             avs->curr_sample = av_rescale_q(timestamp, samplerate, fps);
859     } else {
860         if ((timestamp >= avs->vi->num_audio_samples) || (timestamp < 0))
861             return AVERROR_EOF;
862         /* Force frame granularity for seeking. */
863         if (avs_has_video(avs->vi)) {
864             avs->curr_frame  = av_rescale_q(timestamp, fps, samplerate);
865             avs->curr_sample = av_rescale_q(avs->curr_frame, samplerate, fps);
866         } else {
867             avs->curr_sample = timestamp;
868         }
869     }
870
871     return 0;
872 }
873
874 AVInputFormat ff_avisynth_demuxer = {
875     .name           = "avisynth",
876     .long_name      = NULL_IF_CONFIG_SMALL("AviSynth script"),
877     .priv_data_size = sizeof(AviSynthContext),
878     .read_header    = avisynth_read_header,
879     .read_packet    = avisynth_read_packet,
880     .read_close     = avisynth_read_close,
881     .read_seek      = avisynth_read_seek,
882     .extensions     = "avs",
883 };