2 * AviSynth/AvxSynth support
3 * Copyright (c) 2012 AvxSynth Team.
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.
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.
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
21 #include "libavutil/internal.h"
22 #include "libavcodec/internal.h"
26 /* Enable function pointer definitions for runtime loading. */
27 #define AVSC_NO_DECLSPEC
29 /* Platform-specific directives for AviSynth vs AvxSynth. */
33 #include "compat/avisynth/avisynth_c.h"
34 #include "compat/avisynth/avisynth_c_25.h"
35 #define AVISYNTH_LIB "avisynth"
36 #define USING_AVISYNTH
39 #include "compat/avisynth/avxsynth_c.h"
40 #if defined (__APPLE__)
41 #define AVISYNTH_LIB "libavxsynth.dylib"
43 #define AVISYNTH_LIB "libavxsynth.so"
46 #define LoadLibrary(x) dlopen(x, RTLD_NOW | RTLD_GLOBAL)
47 #define GetProcAddress dlsym
48 #define FreeLibrary dlclose
51 typedef struct AviSynthLibrary
{
53 #define AVSC_DECLARE_FUNC(name) name ## _func name
54 AVSC_DECLARE_FUNC(avs_bit_blt
);
55 AVSC_DECLARE_FUNC(avs_clip_get_error
);
56 AVSC_DECLARE_FUNC(avs_create_script_environment
);
57 AVSC_DECLARE_FUNC(avs_delete_script_environment
);
58 AVSC_DECLARE_FUNC(avs_get_audio
);
59 AVSC_DECLARE_FUNC(avs_get_error
);
60 AVSC_DECLARE_FUNC(avs_get_frame
);
61 AVSC_DECLARE_FUNC(avs_get_version
);
62 AVSC_DECLARE_FUNC(avs_get_video_info
);
63 AVSC_DECLARE_FUNC(avs_invoke
);
64 AVSC_DECLARE_FUNC(avs_release_clip
);
65 AVSC_DECLARE_FUNC(avs_release_value
);
66 AVSC_DECLARE_FUNC(avs_release_video_frame
);
67 AVSC_DECLARE_FUNC(avs_take_clip
);
68 #undef AVSC_DECLARE_FUNC
71 typedef struct AviSynthContext
{
72 AVS_ScriptEnvironment
*env
;
74 const AVS_VideoInfo
*vi
;
76 /* avisynth_read_packet_video() iterates over this. */
86 /* Linked list pointers. */
87 struct AviSynthContext
*next
;
90 static const int avs_planes_packed
[1] = { 0 };
91 static const int avs_planes_grey
[1] = { AVS_PLANAR_Y
};
92 static const int avs_planes_yuv
[3] = { AVS_PLANAR_Y
, AVS_PLANAR_U
,
95 /* A conflict between C++ global objects, atexit, and dynamic loading requires
96 * us to register our own atexit handler to prevent double freeing. */
97 static AviSynthLibrary avs_library
;
98 static int avs_atexit_called
= 0;
100 /* Linked list of AviSynthContexts. An atexit handler destroys this list. */
101 static AviSynthContext
*avs_ctx_list
= NULL
;
103 static av_cold
void avisynth_atexit_handler(void);
105 static av_cold
int avisynth_load_library(void)
107 avs_library
.library
= LoadLibrary(AVISYNTH_LIB
);
108 if (!avs_library
.library
)
109 return AVERROR_UNKNOWN
;
111 #define LOAD_AVS_FUNC(name, continue_on_fail) \
113 (void *)GetProcAddress(avs_library.library, #name); \
114 if (!continue_on_fail && !avs_library.name) \
117 LOAD_AVS_FUNC(avs_bit_blt
, 0);
118 LOAD_AVS_FUNC(avs_clip_get_error
, 0);
119 LOAD_AVS_FUNC(avs_create_script_environment
, 0);
120 LOAD_AVS_FUNC(avs_delete_script_environment
, 0);
121 LOAD_AVS_FUNC(avs_get_audio
, 0);
122 LOAD_AVS_FUNC(avs_get_error
, 1); // New to AviSynth 2.6
123 LOAD_AVS_FUNC(avs_get_frame
, 0);
124 LOAD_AVS_FUNC(avs_get_version
, 0);
125 LOAD_AVS_FUNC(avs_get_video_info
, 0);
126 LOAD_AVS_FUNC(avs_invoke
, 0);
127 LOAD_AVS_FUNC(avs_release_clip
, 0);
128 LOAD_AVS_FUNC(avs_release_value
, 0);
129 LOAD_AVS_FUNC(avs_release_video_frame
, 0);
130 LOAD_AVS_FUNC(avs_take_clip
, 0);
133 atexit(avisynth_atexit_handler
);
137 FreeLibrary(avs_library
.library
);
138 return AVERROR_UNKNOWN
;
141 /* Note that avisynth_context_create and avisynth_context_destroy
142 * do not allocate or free the actual context! That is taken care of
144 static av_cold
int avisynth_context_create(AVFormatContext
*s
)
146 AviSynthContext
*avs
= s
->priv_data
;
149 if (!avs_library
.library
)
150 if (ret
= avisynth_load_library())
153 avs
->env
= avs_library
.avs_create_script_environment(3);
154 if (avs_library
.avs_get_error
) {
155 const char *error
= avs_library
.avs_get_error(avs
->env
);
157 av_log(s
, AV_LOG_ERROR
, "%s\n", error
);
158 return AVERROR_UNKNOWN
;
165 avs
->next
= avs_ctx_list
;
172 static av_cold
void avisynth_context_destroy(AviSynthContext
*avs
)
174 if (avs_atexit_called
)
177 if (avs
== avs_ctx_list
) {
178 avs_ctx_list
= avs
->next
;
180 AviSynthContext
*prev
= avs_ctx_list
;
181 while (prev
->next
!= avs
)
183 prev
->next
= avs
->next
;
187 avs_library
.avs_release_clip(avs
->clip
);
191 avs_library
.avs_delete_script_environment(avs
->env
);
196 static av_cold
void avisynth_atexit_handler(void)
198 AviSynthContext
*avs
= avs_ctx_list
;
201 AviSynthContext
*next
= avs
->next
;
202 avisynth_context_destroy(avs
);
205 FreeLibrary(avs_library
.library
);
207 avs_atexit_called
= 1;
210 /* Create AVStream from audio and video data. */
211 static int avisynth_create_stream_video(AVFormatContext
*s
, AVStream
*st
)
213 AviSynthContext
*avs
= s
->priv_data
;
214 int planar
= 0; // 0: packed, 1: YUV, 2: Y8
216 st
->codec
->codec_type
= AVMEDIA_TYPE_VIDEO
;
217 st
->codec
->codec_id
= AV_CODEC_ID_RAWVIDEO
;
218 st
->codec
->width
= avs
->vi
->width
;
219 st
->codec
->height
= avs
->vi
->height
;
221 st
->time_base
= (AVRational
) { avs
->vi
->fps_denominator
,
222 avs
->vi
->fps_numerator
};
223 st
->avg_frame_rate
= (AVRational
) { avs
->vi
->fps_numerator
,
224 avs
->vi
->fps_denominator
};
226 st
->duration
= avs
->vi
->num_frames
;
227 st
->nb_frames
= avs
->vi
->num_frames
;
229 switch (avs
->vi
->pixel_type
) {
230 #ifdef USING_AVISYNTH
232 st
->codec
->pix_fmt
= AV_PIX_FMT_YUV444P
;
236 st
->codec
->pix_fmt
= AV_PIX_FMT_YUV422P
;
240 st
->codec
->pix_fmt
= AV_PIX_FMT_YUV411P
;
244 st
->codec
->pix_fmt
= AV_PIX_FMT_GRAY8
;
249 st
->codec
->pix_fmt
= AV_PIX_FMT_BGR24
;
252 st
->codec
->pix_fmt
= AV_PIX_FMT_RGB32
;
255 st
->codec
->pix_fmt
= AV_PIX_FMT_YUYV422
;
258 st
->codec
->pix_fmt
= AV_PIX_FMT_YUV420P
;
261 case AVS_CS_I420
: // Is this even used anywhere?
262 st
->codec
->pix_fmt
= AV_PIX_FMT_YUV420P
;
266 av_log(s
, AV_LOG_ERROR
,
267 "unknown AviSynth colorspace %d\n", avs
->vi
->pixel_type
);
269 return AVERROR_UNKNOWN
;
275 avs
->planes
= avs_planes_grey
;
279 avs
->planes
= avs_planes_yuv
;
283 avs
->planes
= avs_planes_packed
;
288 static int avisynth_create_stream_audio(AVFormatContext
*s
, AVStream
*st
)
290 AviSynthContext
*avs
= s
->priv_data
;
292 st
->codec
->codec_type
= AVMEDIA_TYPE_AUDIO
;
293 st
->codec
->sample_rate
= avs
->vi
->audio_samples_per_second
;
294 st
->codec
->channels
= avs
->vi
->nchannels
;
295 st
->time_base
= (AVRational
) { 1,
296 avs
->vi
->audio_samples_per_second
};
297 st
->duration
= avs
->vi
->num_audio_samples
;
299 switch (avs
->vi
->sample_type
) {
300 case AVS_SAMPLE_INT8
:
301 st
->codec
->codec_id
= AV_CODEC_ID_PCM_U8
;
303 case AVS_SAMPLE_INT16
:
304 st
->codec
->codec_id
= AV_CODEC_ID_PCM_S16LE
;
306 case AVS_SAMPLE_INT24
:
307 st
->codec
->codec_id
= AV_CODEC_ID_PCM_S24LE
;
309 case AVS_SAMPLE_INT32
:
310 st
->codec
->codec_id
= AV_CODEC_ID_PCM_S32LE
;
312 case AVS_SAMPLE_FLOAT
:
313 st
->codec
->codec_id
= AV_CODEC_ID_PCM_F32LE
;
316 av_log(s
, AV_LOG_ERROR
,
317 "unknown AviSynth sample type %d\n", avs
->vi
->sample_type
);
319 return AVERROR_UNKNOWN
;
324 static int avisynth_create_stream(AVFormatContext
*s
)
326 AviSynthContext
*avs
= s
->priv_data
;
331 if (avs_has_video(avs
->vi
)) {
332 st
= avformat_new_stream(s
, NULL
);
334 return AVERROR_UNKNOWN
;
336 if (ret
= avisynth_create_stream_video(s
, st
))
339 if (avs_has_audio(avs
->vi
)) {
340 st
= avformat_new_stream(s
, NULL
);
342 return AVERROR_UNKNOWN
;
344 if (ret
= avisynth_create_stream_audio(s
, st
))
350 static int avisynth_open_file(AVFormatContext
*s
)
352 AviSynthContext
*avs
= s
->priv_data
;
355 #ifdef USING_AVISYNTH
356 char filename_ansi
[MAX_PATH
* 4];
357 wchar_t filename_wc
[MAX_PATH
* 4];
360 if (ret
= avisynth_context_create(s
))
363 #ifdef USING_AVISYNTH
364 /* Convert UTF-8 to ANSI code page */
365 MultiByteToWideChar(CP_UTF8
, 0, s
->filename
, -1, filename_wc
, MAX_PATH
* 4);
366 WideCharToMultiByte(CP_THREAD_ACP
, 0, filename_wc
, -1, filename_ansi
,
367 MAX_PATH
* 4, NULL
, NULL
);
368 arg
= avs_new_value_string(filename_ansi
);
370 arg
= avs_new_value_string(s
->filename
);
372 val
= avs_library
.avs_invoke(avs
->env
, "Import", arg
, 0);
373 if (avs_is_error(val
)) {
374 av_log(s
, AV_LOG_ERROR
, "%s\n", avs_as_error(val
));
375 ret
= AVERROR_UNKNOWN
;
378 if (!avs_is_clip(val
)) {
379 av_log(s
, AV_LOG_ERROR
, "AviSynth script did not return a clip\n");
380 ret
= AVERROR_UNKNOWN
;
384 avs
->clip
= avs_library
.avs_take_clip(val
, avs
->env
);
385 avs
->vi
= avs_library
.avs_get_video_info(avs
->clip
);
387 /* Release the AVS_Value as it will go out of scope. */
388 avs_library
.avs_release_value(val
);
390 if (ret
= avisynth_create_stream(s
))
396 avisynth_context_destroy(avs
);
400 static void avisynth_next_stream(AVFormatContext
*s
, AVStream
**st
,
401 AVPacket
*pkt
, int *discard
)
403 AviSynthContext
*avs
= s
->priv_data
;
406 avs
->curr_stream
%= s
->nb_streams
;
408 *st
= s
->streams
[avs
->curr_stream
];
409 if ((*st
)->discard
== AVDISCARD_ALL
)
417 /* Copy AviSynth clip data into an AVPacket. */
418 static int avisynth_read_packet_video(AVFormatContext
*s
, AVPacket
*pkt
,
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
;
428 if (avs
->curr_frame
>= avs
->vi
->num_frames
)
431 /* This must happen even if the stream is discarded to prevent desync. */
432 n
= avs
->curr_frame
++;
436 #ifdef USING_AVISYNTH
437 /* Define the bpp values for the new AviSynth 2.6 colorspaces.
438 * Since AvxSynth doesn't have these functions, special-case
439 * it in order to avoid implicit declaration errors. */
441 if (avs_is_yv24(avs
->vi
))
443 else if (avs_is_yv16(avs
->vi
))
445 else if (avs_is_yv411(avs
->vi
))
447 else if (avs_is_y8(avs
->vi
))
451 bits
= avs_bits_per_pixel(avs
->vi
);
453 /* Without the cast to int64_t, calculation overflows at about 9k x 9k
455 pkt
->size
= (((int64_t)avs
->vi
->width
*
456 (int64_t)avs
->vi
->height
) * bits
) / 8;
458 return AVERROR_UNKNOWN
;
460 if (av_new_packet(pkt
, pkt
->size
) < 0)
461 return AVERROR(ENOMEM
);
466 pkt
->stream_index
= avs
->curr_stream
;
468 frame
= avs_library
.avs_get_frame(avs
->clip
, n
);
469 error
= avs_library
.avs_clip_get_error(avs
->clip
);
471 av_log(s
, AV_LOG_ERROR
, "%s\n", error
);
473 av_packet_unref(pkt
);
474 return AVERROR_UNKNOWN
;
478 for (i
= 0; i
< avs
->n_planes
; i
++) {
479 plane
= avs
->planes
[i
];
480 src_p
= avs_get_read_ptr_p(frame
, plane
);
481 pitch
= avs_get_pitch_p(frame
, plane
);
483 #ifdef USING_AVISYNTH
484 if (avs_library
.avs_get_version(avs
->clip
) == 3) {
485 rowsize
= avs_get_row_size_p_25(frame
, plane
);
486 planeheight
= avs_get_height_p_25(frame
, plane
);
488 rowsize
= avs_get_row_size_p(frame
, plane
);
489 planeheight
= avs_get_height_p(frame
, plane
);
492 rowsize
= avs_get_row_size_p(frame
, plane
);
493 planeheight
= avs_get_height_p(frame
, plane
);
496 /* Flip RGB video. */
497 if (avs_is_rgb24(avs
->vi
) || avs_is_rgb(avs
->vi
)) {
498 src_p
= src_p
+ (planeheight
- 1) * pitch
;
502 avs_library
.avs_bit_blt(avs
->env
, dst_p
, rowsize
, src_p
, pitch
,
503 rowsize
, planeheight
);
504 dst_p
+= rowsize
* planeheight
;
507 avs_library
.avs_release_video_frame(frame
);
511 static int avisynth_read_packet_audio(AVFormatContext
*s
, AVPacket
*pkt
,
514 AviSynthContext
*avs
= s
->priv_data
;
515 AVRational fps
, samplerate
;
520 if (avs
->curr_sample
>= avs
->vi
->num_audio_samples
)
523 fps
.num
= avs
->vi
->fps_numerator
;
524 fps
.den
= avs
->vi
->fps_denominator
;
525 samplerate
.num
= avs
->vi
->audio_samples_per_second
;
528 if (avs_has_video(avs
->vi
)) {
529 if (avs
->curr_frame
< avs
->vi
->num_frames
)
530 samples
= av_rescale_q(avs
->curr_frame
, samplerate
, fps
) -
533 samples
= av_rescale_q(1, samplerate
, fps
);
538 /* After seeking, audio may catch up with video. */
545 if (avs
->curr_sample
+ samples
> avs
->vi
->num_audio_samples
)
546 samples
= avs
->vi
->num_audio_samples
- avs
->curr_sample
;
548 /* This must happen even if the stream is discarded to prevent desync. */
549 n
= avs
->curr_sample
;
550 avs
->curr_sample
+= samples
;
554 pkt
->size
= avs_bytes_per_channel_sample(avs
->vi
) *
555 samples
* avs
->vi
->nchannels
;
557 return AVERROR_UNKNOWN
;
559 if (av_new_packet(pkt
, pkt
->size
) < 0)
560 return AVERROR(ENOMEM
);
564 pkt
->duration
= samples
;
565 pkt
->stream_index
= avs
->curr_stream
;
567 avs_library
.avs_get_audio(avs
->clip
, pkt
->data
, n
, samples
);
568 error
= avs_library
.avs_clip_get_error(avs
->clip
);
570 av_log(s
, AV_LOG_ERROR
, "%s\n", error
);
572 av_packet_unref(pkt
);
573 return AVERROR_UNKNOWN
;
578 static av_cold
int avisynth_read_header(AVFormatContext
*s
)
582 // Calling library must implement a lock for thread-safe opens.
583 if (ret
= avpriv_lock_avformat())
586 if (ret
= avisynth_open_file(s
)) {
587 avpriv_unlock_avformat();
591 avpriv_unlock_avformat();
595 static int avisynth_read_packet(AVFormatContext
*s
, AVPacket
*pkt
)
597 AviSynthContext
*avs
= s
->priv_data
;
603 return AVERROR_UNKNOWN
;
605 /* If either stream reaches EOF, try to read the other one before
607 avisynth_next_stream(s
, &st
, pkt
, &discard
);
608 if (st
->codec
->codec_type
== AVMEDIA_TYPE_VIDEO
) {
609 ret
= avisynth_read_packet_video(s
, pkt
, discard
);
610 if (ret
== AVERROR_EOF
&& avs_has_audio(avs
->vi
)) {
611 avisynth_next_stream(s
, &st
, pkt
, &discard
);
612 return avisynth_read_packet_audio(s
, pkt
, discard
);
615 ret
= avisynth_read_packet_audio(s
, pkt
, discard
);
616 if (ret
== AVERROR_EOF
&& avs_has_video(avs
->vi
)) {
617 avisynth_next_stream(s
, &st
, pkt
, &discard
);
618 return avisynth_read_packet_video(s
, pkt
, discard
);
625 static av_cold
int avisynth_read_close(AVFormatContext
*s
)
627 if (avpriv_lock_avformat())
628 return AVERROR_UNKNOWN
;
630 avisynth_context_destroy(s
->priv_data
);
631 avpriv_unlock_avformat();
635 static int avisynth_read_seek(AVFormatContext
*s
, int stream_index
,
636 int64_t timestamp
, int flags
)
638 AviSynthContext
*avs
= s
->priv_data
;
640 AVRational fps
, samplerate
;
643 return AVERROR_UNKNOWN
;
645 fps
= (AVRational
) { avs
->vi
->fps_numerator
,
646 avs
->vi
->fps_denominator
};
647 samplerate
= (AVRational
) { avs
->vi
->audio_samples_per_second
, 1 };
649 st
= s
->streams
[stream_index
];
650 if (st
->codec
->codec_type
== AVMEDIA_TYPE_VIDEO
) {
651 /* AviSynth frame counts are signed int. */
652 if ((timestamp
>= avs
->vi
->num_frames
) ||
653 (timestamp
> INT_MAX
) ||
656 avs
->curr_frame
= timestamp
;
657 if (avs_has_audio(avs
->vi
))
658 avs
->curr_sample
= av_rescale_q(timestamp
, samplerate
, fps
);
660 if ((timestamp
>= avs
->vi
->num_audio_samples
) || (timestamp
< 0))
662 /* Force frame granularity for seeking. */
663 if (avs_has_video(avs
->vi
)) {
664 avs
->curr_frame
= av_rescale_q(timestamp
, fps
, samplerate
);
665 avs
->curr_sample
= av_rescale_q(avs
->curr_frame
, samplerate
, fps
);
667 avs
->curr_sample
= timestamp
;
674 AVInputFormat ff_avisynth_demuxer
= {
676 .long_name
= NULL_IF_CONFIG_SMALL("AviSynth script"),
677 .priv_data_size
= sizeof(AviSynthContext
),
678 .read_header
= avisynth_read_header
,
679 .read_packet
= avisynth_read_packet
,
680 .read_close
= avisynth_read_close
,
681 .read_seek
= avisynth_read_seek
,