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
24
26
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
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 /* Endianness guards for audio */
46 #if HAVE_BIGENDIAN
47 #define PCM(format) (AV_CODEC_ID_PCM_ ## format ## BE)
48 #else
49 #define PCM(format) (AV_CODEC_ID_PCM_ ## format ## LE)
50 #endif
51
52 #include <avisynth/avisynth_c.h>
53
56 #define AVSC_DECLARE_FUNC(name) name ## _func name
79 #undef AVSC_DECLARE_FUNC
81
83 AVS_ScriptEnvironment *
env;
85 const AVS_VideoInfo *
vi;
86
87 /* avisynth_read_packet_video() iterates over this. */
90
94
96
97 /* Linked list pointers. */
100
104 AVS_PLANAR_V };
106 AVS_PLANAR_R };
108 AVS_PLANAR_V, AVS_PLANAR_A };
110 AVS_PLANAR_R, AVS_PLANAR_A };
111
112 /* A conflict between C++ global objects, atexit, and dynamic loading requires
113 * us to register our own atexit handler to prevent double freeing. */
116
117 /* Linked list of AviSynthContexts. An atexit handler destroys this list. */
119
121
123 {
127
128 #define LOAD_AVS_FUNC(name, continue_on_fail) \
129 avs_library.name = (name ## _func) \
130 dlsym(avs_library.library, #name); \
131 if (!continue_on_fail && !avs_library.name) \
132 goto fail;
133
156 #undef LOAD_AVS_FUNC
157
159 return 0;
160
164 }
165
166 /* Note that avisynth_context_create and avisynth_context_destroy
167 * do not allocate or free the actual context! That is taken care of
168 * by libavformat. */
170 {
173
177
184 }
185 }
186
189 } else {
192 }
193
194 return 0;
195 }
196
198 {
200 return;
201
204 } else {
206 while (prev->
next != avs)
209 }
210
214 }
218 }
219 }
220
222 {
224
225 while (avs) {
229 }
231
233 }
234
235 /* Create AVStream from audio and video data. */
237 {
239 int planar = 0;
// 0: packed, 1: YUV, 2: Y8, 3: Planar RGB, 4: YUVA, 5: Planar RGBA
240
245
247 avs->
vi->fps_denominator };
252
255
256 /* The following typically only works when assumetff (-bff) and
257 * assumefieldbased is used in-script. Additional
258 * logic using GetParity() could deliver more accurate results
259 * but also decodes a frame which we want to avoid. */
261 if (avs_is_field_based(avs->
vi)) {
262 if (avs_is_tff(avs->
vi)) {
264 }
265 else if (avs_is_bff(avs->
vi)) {
267 }
268 }
269
270 switch (avs->
vi->pixel_type) {
271 /* 10~16-bit YUV pix_fmts (AviSynth+) */
272 case AVS_CS_YUV444P10:
275 break;
276 case AVS_CS_YUV422P10:
279 break;
280 case AVS_CS_YUV420P10:
283 break;
284 case AVS_CS_YUV444P12:
287 break;
288 case AVS_CS_YUV422P12:
291 break;
292 case AVS_CS_YUV420P12:
295 break;
296 case AVS_CS_YUV444P14:
299 break;
300 case AVS_CS_YUV422P14:
303 break;
304 case AVS_CS_YUV420P14:
307 break;
308 case AVS_CS_YUV444P16:
311 break;
312 case AVS_CS_YUV422P16:
315 break;
316 case AVS_CS_YUV420P16:
319 break;
320 /* 8~16-bit YUV pix_fmts with Alpha (AviSynth+) */
321 case AVS_CS_YUVA444:
324 break;
325 case AVS_CS_YUVA422:
328 break;
329 case AVS_CS_YUVA420:
332 break;
333 case AVS_CS_YUVA444P10:
336 break;
337 case AVS_CS_YUVA422P10:
340 break;
341 case AVS_CS_YUVA420P10:
344 break;
345 case AVS_CS_YUVA422P12:
348 break;
349 case AVS_CS_YUVA444P16:
352 break;
353 case AVS_CS_YUVA422P16:
356 break;
357 case AVS_CS_YUVA420P16:
360 break;
361 /* Planar RGB pix_fmts (AviSynth+) */
362 case AVS_CS_RGBP:
365 break;
366 case AVS_CS_RGBP10:
369 break;
370 case AVS_CS_RGBP12:
373 break;
374 case AVS_CS_RGBP14:
377 break;
378 case AVS_CS_RGBP16:
381 break;
382 /* Single precision floating point Planar RGB (AviSynth+) */
383 case AVS_CS_RGBPS:
386 break;
387 /* Planar RGB pix_fmts with Alpha (AviSynth+) */
388 case AVS_CS_RGBAP:
391 break;
392 case AVS_CS_RGBAP10:
395 break;
396 case AVS_CS_RGBAP12:
399 break;
400 case AVS_CS_RGBAP16:
403 break;
404 /* Single precision floating point Planar RGB with Alpha (AviSynth+) */
405 case AVS_CS_RGBAPS:
408 break;
409 /* 10~16-bit gray pix_fmts (AviSynth+) */
410 case AVS_CS_Y10:
413 break;
414 case AVS_CS_Y12:
417 break;
418 case AVS_CS_Y14:
421 break;
422 case AVS_CS_Y16:
425 break;
426 /* Single precision floating point gray (AviSynth+) */
427 case AVS_CS_Y32:
430 break;
431 /* pix_fmts added in AviSynth 2.6 */
432 case AVS_CS_YV24:
435 break;
436 case AVS_CS_YV16:
439 break;
440 case AVS_CS_YV411:
443 break;
444 case AVS_CS_Y8:
447 break;
448 /* 16-bit packed RGB pix_fmts (AviSynth+) */
449 case AVS_CS_BGR48:
451 break;
452 case AVS_CS_BGR64:
454 break;
455 /* AviSynth 2.5 pix_fmts */
456 case AVS_CS_BGR24:
458 break;
459 case AVS_CS_BGR32:
461 break;
462 case AVS_CS_YUY2:
464 break;
465 case AVS_CS_YV12:
468 break;
469 case AVS_CS_I420: // Is this even used anywhere?
472 break;
473 default:
475 "unknown AviSynth colorspace %d\n", avs->
vi->pixel_type);
478 }
479
481 case 5: // Planar RGB + Alpha
484 break;
485 case 4: // YUV + Alpha
488 break;
489 case 3: // Planar RGB
492 break;
493 case 2: // Y8
496 break;
497 case 1: // YUV
500 break;
501 default:
504 }
505 return 0;
506 }
507
509 {
511
517
518 switch (avs->
vi->sample_type) {
519 case AVS_SAMPLE_INT8:
521 break;
522 case AVS_SAMPLE_INT16:
524 break;
525 case AVS_SAMPLE_INT24:
527 break;
528 case AVS_SAMPLE_INT32:
530 break;
531 case AVS_SAMPLE_FLOAT:
533 break;
534 default:
536 "unknown AviSynth sample type %d\n", avs->
vi->sample_type);
539 }
540 return 0;
541 }
542
544 {
548 int id = 0;
549
550 if (avs_has_video(avs->
vi)) {
552 if (!st)
557 }
558 if (avs_has_audio(avs->
vi)) {
560 if (!st)
565 }
566 return 0;
567 }
568
570 {
574 #ifdef _WIN32
575 char filename_ansi[MAX_PATH * 4];
576 wchar_t filename_wc[MAX_PATH * 4];
577 #endif
578
581
582 #ifdef _WIN32
583 /* Convert UTF-8 to ANSI code page */
584 MultiByteToWideChar(CP_UTF8, 0,
s->url, -1, filename_wc, MAX_PATH * 4);
585 WideCharToMultiByte(CP_THREAD_ACP, 0, filename_wc, -1, filename_ansi,
587 arg = avs_new_value_string(filename_ansi);
588 #else
589 arg = avs_new_value_string(
s->url);
590 #endif
592 if (avs_is_error(
val)) {
596 }
597 if (!avs_is_clip(
val)) {
601 }
602
605
606 /* On Windows, FFmpeg supports AviSynth interface version 6 or higher.
607 * This includes AviSynth 2.6 RC1 or higher, and AviSynth+ r1718 or higher,
608 * and excludes 2.5 and the 2.6 alphas. */
609
612 "AviSynth version is too old. Please upgrade to either AviSynth 2.6 >= RC1 or AviSynth+ >= r1718.\n");
615 }
616
617 /* Release the AVS_Value as it will go out of scope. */
619
622
623 return 0;
624
628 }
629
632 {
634
637
640 *discard = 1;
641 else
642 *discard = 0;
643
644 return;
645 }
646
647 /* Copy AviSynth clip data into an AVPacket. */
649 int discard)
650 {
652 AVS_VideoFrame *
frame;
653 unsigned char *dst_p;
654 const unsigned char *src_p;
655 int n,
i, plane, rowsize, planeheight, pitch,
bits,
ret;
657
660
661 /* This must happen even if the stream is discarded to prevent desync. */
663 if (discard)
664 return 0;
665
667
668 /* Without the cast to int64_t, calculation overflows at about 9k x 9k
669 * resolution. */
671 (int64_t)avs->
vi->height) *
bits) / 8;
674
677
682
690 }
691
697
700
701 /* Flip RGB video. */
705 src_p = src_p + (planeheight - 1) * pitch;
706 pitch = -pitch;
707 }
708
710 rowsize, planeheight);
711 dst_p += rowsize * planeheight;
712 }
713
715 return 0;
716 }
717
719 int discard)
720 {
724 int64_t n;
726
729
730 fps.
num = avs->
vi->fps_numerator;
731 fps.
den = avs->
vi->fps_denominator;
732 samplerate.
num = avs->
vi->audio_samples_per_second;
734
735 if (avs_has_video(avs->
vi)) {
739 else
741 } else {
743 }
744
745 /* After seeking, audio may catch up with video. */
749 return 0;
750 }
751
754
755 /* This must happen even if the stream is discarded to prevent desync. */
758 if (discard)
759 return 0;
760
761 pkt->
size = avs_bytes_per_channel_sample(avs->
vi) *
765
768
773
781 }
782 return 0;
783 }
784
786 {
788
789 // Calling library must implement a lock for thread-safe opens.
792
796 }
797
799 return 0;
800 }
801
803 {
806 int discard = 0;
808
811
812 /* If either stream reaches EOF, try to read the other one before
813 * giving up. */
820 }
821 } else {
826 }
827 }
828
830 }
831
833 {
836
839 return 0;
840 }
841
843 int64_t timestamp,
int flags)
844 {
848
851
853 avs->
vi->fps_denominator };
854 samplerate = (
AVRational) { avs->
vi->audio_samples_per_second, 1 };
855
856 st =
s->streams[stream_index];
858 /* AviSynth frame counts are signed int. */
859 if ((timestamp >= avs->
vi->num_frames) ||
860 (timestamp > INT_MAX) ||
861 (timestamp < 0))
864 if (avs_has_audio(avs->
vi))
866 } else {
867 if ((timestamp >= avs->
vi->num_audio_samples) || (timestamp < 0))
869 /* Force frame granularity for seeking. */
870 if (avs_has_video(avs->
vi)) {
873 } else {
875 }
876 }
877
878 return 0;
879 }
880
889 .extensions = "avs",
890 };