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