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 = NULL;
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 = av_mallocz(sizeof(AviSynthLibrary));
109 return AVERROR_UNKNOWN;
111 avs_library->library = LoadLibrary(AVISYNTH_LIB);
112 if (!avs_library->library)
115 #define LOAD_AVS_FUNC(name, continue_on_fail) \
116 avs_library->name = \
117 (void *)GetProcAddress(avs_library->library, #name); \
118 if (!continue_on_fail && !avs_library->name) \
121 LOAD_AVS_FUNC(avs_bit_blt, 0);
122 LOAD_AVS_FUNC(avs_clip_get_error, 0);
123 LOAD_AVS_FUNC(avs_create_script_environment, 0);
124 LOAD_AVS_FUNC(avs_delete_script_environment, 0);
125 LOAD_AVS_FUNC(avs_get_audio, 0);
126 LOAD_AVS_FUNC(avs_get_error, 1); // New to AviSynth 2.6
127 LOAD_AVS_FUNC(avs_get_frame, 0);
128 LOAD_AVS_FUNC(avs_get_version, 0);
129 LOAD_AVS_FUNC(avs_get_video_info, 0);
130 LOAD_AVS_FUNC(avs_invoke, 0);
131 LOAD_AVS_FUNC(avs_release_clip, 0);
132 LOAD_AVS_FUNC(avs_release_value, 0);
133 LOAD_AVS_FUNC(avs_release_video_frame, 0);
134 LOAD_AVS_FUNC(avs_take_clip, 0);
137 atexit(avisynth_atexit_handler);
141 FreeLibrary(avs_library->library);
143 av_freep(&avs_library);
144 return AVERROR_UNKNOWN;
147 /* Note that avisynth_context_create and avisynth_context_destroy
148 * do not allocate or free the actual context! That is taken care of
150 static av_cold int avisynth_context_create(AVFormatContext *s)
152 AviSynthContext *avs = s->priv_data;
156 if (ret = avisynth_load_library())
159 avs->env = avs_library->avs_create_script_environment(3);
160 if (avs_library->avs_get_error) {
161 const char *error = avs_library->avs_get_error(avs->env);
163 av_log(s, AV_LOG_ERROR, "%s\n", error);
164 return AVERROR_UNKNOWN;
171 avs->next = avs_ctx_list;
178 static av_cold void avisynth_context_destroy(AviSynthContext *avs)
180 if (avs_atexit_called)
183 if (avs == avs_ctx_list) {
184 avs_ctx_list = avs->next;
186 AviSynthContext *prev = avs_ctx_list;
187 while (prev->next != avs)
189 prev->next = avs->next;
193 avs_library->avs_release_clip(avs->clip);
197 avs_library->avs_delete_script_environment(avs->env);
202 static av_cold void avisynth_atexit_handler(void)
204 AviSynthContext *avs = avs_ctx_list;
207 AviSynthContext *next = avs->next;
208 avisynth_context_destroy(avs);
211 FreeLibrary(avs_library->library);
212 av_freep(&avs_library);
214 avs_atexit_called = 1;
217 /* Create AVStream from audio and video data. */
218 static int avisynth_create_stream_video(AVFormatContext *s, AVStream *st)
220 AviSynthContext *avs = s->priv_data;
221 int planar = 0; // 0: packed, 1: YUV, 2: Y8
223 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
224 st->codec->codec_id = AV_CODEC_ID_RAWVIDEO;
225 st->codec->width = avs->vi->width;
226 st->codec->height = avs->vi->height;
228 st->time_base = (AVRational) { avs->vi->fps_denominator,
229 avs->vi->fps_numerator };
230 st->avg_frame_rate = (AVRational) { avs->vi->fps_numerator,
231 avs->vi->fps_denominator };
233 st->duration = avs->vi->num_frames;
234 st->nb_frames = avs->vi->num_frames;
236 switch (avs->vi->pixel_type) {
237 #ifdef USING_AVISYNTH
239 st->codec->pix_fmt = AV_PIX_FMT_YUV444P;
243 st->codec->pix_fmt = AV_PIX_FMT_YUV422P;
247 st->codec->pix_fmt = AV_PIX_FMT_YUV411P;
251 st->codec->pix_fmt = AV_PIX_FMT_GRAY8;
256 st->codec->pix_fmt = AV_PIX_FMT_BGR24;
259 st->codec->pix_fmt = AV_PIX_FMT_RGB32;
262 st->codec->pix_fmt = AV_PIX_FMT_YUYV422;
265 st->codec->pix_fmt = AV_PIX_FMT_YUV420P;
268 case AVS_CS_I420: // Is this even used anywhere?
269 st->codec->pix_fmt = AV_PIX_FMT_YUV420P;
273 av_log(s, AV_LOG_ERROR,
274 "unknown AviSynth colorspace %d\n", avs->vi->pixel_type);
276 return AVERROR_UNKNOWN;
282 avs->planes = avs_planes_grey;
286 avs->planes = avs_planes_yuv;
290 avs->planes = avs_planes_packed;
295 static int avisynth_create_stream_audio(AVFormatContext *s, AVStream *st)
297 AviSynthContext *avs = s->priv_data;
299 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
300 st->codec->sample_rate = avs->vi->audio_samples_per_second;
301 st->codec->channels = avs->vi->nchannels;
302 st->time_base = (AVRational) { 1,
303 avs->vi->audio_samples_per_second };
305 switch (avs->vi->sample_type) {
306 case AVS_SAMPLE_INT8:
307 st->codec->codec_id = AV_CODEC_ID_PCM_U8;
309 case AVS_SAMPLE_INT16:
310 st->codec->codec_id = AV_CODEC_ID_PCM_S16LE;
312 case AVS_SAMPLE_INT24:
313 st->codec->codec_id = AV_CODEC_ID_PCM_S24LE;
315 case AVS_SAMPLE_INT32:
316 st->codec->codec_id = AV_CODEC_ID_PCM_S32LE;
318 case AVS_SAMPLE_FLOAT:
319 st->codec->codec_id = AV_CODEC_ID_PCM_F32LE;
322 av_log(s, AV_LOG_ERROR,
323 "unknown AviSynth sample type %d\n", avs->vi->sample_type);
325 return AVERROR_UNKNOWN;
330 static int avisynth_create_stream(AVFormatContext *s)
332 AviSynthContext *avs = s->priv_data;
337 if (avs_has_video(avs->vi)) {
338 st = avformat_new_stream(s, NULL);
340 return AVERROR_UNKNOWN;
342 if (ret = avisynth_create_stream_video(s, st))
345 if (avs_has_audio(avs->vi)) {
346 st = avformat_new_stream(s, NULL);
348 return AVERROR_UNKNOWN;
350 if (ret = avisynth_create_stream_audio(s, st))
356 static int avisynth_open_file(AVFormatContext *s)
358 AviSynthContext *avs = s->priv_data;
361 #ifdef USING_AVISYNTH
362 char filename_ansi[MAX_PATH * 4];
363 wchar_t filename_wc[MAX_PATH * 4];
366 if (ret = avisynth_context_create(s))
369 #ifdef USING_AVISYNTH
370 /* Convert UTF-8 to ANSI code page */
371 MultiByteToWideChar(CP_UTF8, 0, s->filename, -1, filename_wc, MAX_PATH * 4);
372 WideCharToMultiByte(CP_THREAD_ACP, 0, filename_wc, -1, filename_ansi,
373 MAX_PATH * 4, NULL, NULL);
374 arg = avs_new_value_string(filename_ansi);
376 arg = avs_new_value_string(s->filename);
378 val = avs_library->avs_invoke(avs->env, "Import", arg, 0);
379 if (avs_is_error(val)) {
380 av_log(s, AV_LOG_ERROR, "%s\n", avs_as_error(val));
381 ret = AVERROR_UNKNOWN;
384 if (!avs_is_clip(val)) {
385 av_log(s, AV_LOG_ERROR, "AviSynth script did not return a clip\n");
386 ret = AVERROR_UNKNOWN;
390 avs->clip = avs_library->avs_take_clip(val, avs->env);
391 avs->vi = avs_library->avs_get_video_info(avs->clip);
393 /* Release the AVS_Value as it will go out of scope. */
394 avs_library->avs_release_value(val);
396 if (ret = avisynth_create_stream(s))
402 avisynth_context_destroy(avs);
406 static void avisynth_next_stream(AVFormatContext *s, AVStream **st,
407 AVPacket *pkt, int *discard)
409 AviSynthContext *avs = s->priv_data;
411 pkt->stream_index = avs->curr_stream++;
412 avs->curr_stream %= s->nb_streams;
414 *st = s->streams[pkt->stream_index];
415 if ((*st)->discard == AVDISCARD_ALL)
423 /* Copy AviSynth clip data into an AVPacket. */
424 static int avisynth_read_packet_video(AVFormatContext *s, AVPacket *pkt,
427 AviSynthContext *avs = s->priv_data;
428 AVS_VideoFrame *frame;
429 unsigned char *dst_p;
430 const unsigned char *src_p;
431 int n, i, plane, rowsize, planeheight, pitch, bits;
434 if (avs->curr_frame >= avs->vi->num_frames)
437 /* This must happen even if the stream is discarded to prevent desync. */
438 n = avs->curr_frame++;
446 #ifdef USING_AVISYNTH
447 /* Define the bpp values for the new AviSynth 2.6 colorspaces.
448 * Since AvxSynth doesn't have these functions, special-case
449 * it in order to avoid implicit declaration errors. */
451 if (avs_is_yv24(avs->vi))
453 else if (avs_is_yv16(avs->vi))
455 else if (avs_is_yv411(avs->vi))
457 else if (avs_is_y8(avs->vi))
461 bits = avs_bits_per_pixel(avs->vi);
463 /* Without the cast to int64_t, calculation overflows at about 9k x 9k
465 pkt->size = (((int64_t)avs->vi->width *
466 (int64_t)avs->vi->height) * bits) / 8;
468 return AVERROR_UNKNOWN;
469 pkt->data = av_malloc(pkt->size);
471 return AVERROR(ENOMEM);
473 frame = avs_library->avs_get_frame(avs->clip, n);
474 error = avs_library->avs_clip_get_error(avs->clip);
476 av_log(s, AV_LOG_ERROR, "%s\n", error);
478 av_freep(&pkt->data);
479 return AVERROR_UNKNOWN;
483 for (i = 0; i < avs->n_planes; i++) {
484 plane = avs->planes[i];
485 src_p = avs_get_read_ptr_p(frame, plane);
486 pitch = avs_get_pitch_p(frame, plane);
488 #ifdef USING_AVISYNTH
489 if (avs_library->avs_get_version(avs->clip) == 3) {
490 rowsize = avs_get_row_size_p_25(frame, plane);
491 planeheight = avs_get_height_p_25(frame, plane);
493 rowsize = avs_get_row_size_p(frame, plane);
494 planeheight = avs_get_height_p(frame, plane);
497 rowsize = avs_get_row_size_p(frame, plane);
498 planeheight = avs_get_height_p(frame, plane);
501 /* Flip RGB video. */
502 if (avs_is_rgb24(avs->vi) || avs_is_rgb(avs->vi)) {
503 src_p = src_p + (planeheight - 1) * pitch;
507 avs_library->avs_bit_blt(avs->env, dst_p, rowsize, src_p, pitch,
508 rowsize, planeheight);
509 dst_p += rowsize * planeheight;
512 avs_library->avs_release_video_frame(frame);
516 static int avisynth_read_packet_audio(AVFormatContext *s, AVPacket *pkt,
519 AviSynthContext *avs = s->priv_data;
520 AVRational fps, samplerate;
525 if (avs->curr_sample >= avs->vi->num_audio_samples)
528 fps.num = avs->vi->fps_numerator;
529 fps.den = avs->vi->fps_denominator;
530 samplerate.num = avs->vi->audio_samples_per_second;
533 if (avs_has_video(avs->vi)) {
534 if (avs->curr_frame < avs->vi->num_frames)
535 samples = av_rescale_q(avs->curr_frame, samplerate, fps) -
538 samples = av_rescale_q(1, samplerate, fps);
543 /* After seeking, audio may catch up with video. */
550 if (avs->curr_sample + samples > avs->vi->num_audio_samples)
551 samples = avs->vi->num_audio_samples - avs->curr_sample;
553 /* This must happen even if the stream is discarded to prevent desync. */
554 n = avs->curr_sample;
555 avs->curr_sample += samples;
561 pkt->duration = samples;
563 pkt->size = avs_bytes_per_channel_sample(avs->vi) *
564 samples * avs->vi->nchannels;
566 return AVERROR_UNKNOWN;
567 pkt->data = av_malloc(pkt->size);
569 return AVERROR(ENOMEM);
571 avs_library->avs_get_audio(avs->clip, pkt->data, n, samples);
572 error = avs_library->avs_clip_get_error(avs->clip);
574 av_log(s, AV_LOG_ERROR, "%s\n", error);
576 av_freep(&pkt->data);
577 return AVERROR_UNKNOWN;
582 static av_cold int avisynth_read_header(AVFormatContext *s)
586 // Calling library must implement a lock for thread-safe opens.
587 if (ret = avpriv_lock_avformat())
590 if (ret = avisynth_open_file(s)) {
591 avpriv_unlock_avformat();
595 avpriv_unlock_avformat();
599 static int avisynth_read_packet(AVFormatContext *s, AVPacket *pkt)
601 AviSynthContext *avs = s->priv_data;
607 return AVERROR_UNKNOWN;
609 pkt->destruct = av_destruct_packet;
611 /* If either stream reaches EOF, try to read the other one before
613 avisynth_next_stream(s, &st, pkt, &discard);
614 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
615 ret = avisynth_read_packet_video(s, pkt, discard);
616 if (ret == AVERROR_EOF && avs_has_audio(avs->vi)) {
617 avisynth_next_stream(s, &st, pkt, &discard);
618 return avisynth_read_packet_audio(s, pkt, discard);
621 ret = avisynth_read_packet_audio(s, pkt, discard);
622 if (ret == AVERROR_EOF && avs_has_video(avs->vi)) {
623 avisynth_next_stream(s, &st, pkt, &discard);
624 return avisynth_read_packet_video(s, pkt, discard);
631 static av_cold int avisynth_read_close(AVFormatContext *s)
633 if (avpriv_lock_avformat())
634 return AVERROR_UNKNOWN;
636 avisynth_context_destroy(s->priv_data);
637 avpriv_unlock_avformat();
641 static int avisynth_read_seek(AVFormatContext *s, int stream_index,
642 int64_t timestamp, int flags)
644 AviSynthContext *avs = s->priv_data;
646 AVRational fps, samplerate;
649 return AVERROR_UNKNOWN;
651 fps = (AVRational) { avs->vi->fps_numerator,
652 avs->vi->fps_denominator };
653 samplerate = (AVRational) { avs->vi->audio_samples_per_second, 1 };
655 st = s->streams[stream_index];
656 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
657 /* AviSynth frame counts are signed int. */
658 if ((timestamp >= avs->vi->num_frames) ||
659 (timestamp > INT_MAX) ||
662 avs->curr_frame = timestamp;
663 if (avs_has_audio(avs->vi))
664 avs->curr_sample = av_rescale_q(timestamp, samplerate, fps);
666 if ((timestamp >= avs->vi->num_audio_samples) || (timestamp < 0))
668 /* Force frame granularity for seeking. */
669 if (avs_has_video(avs->vi)) {
670 avs->curr_frame = av_rescale_q(timestamp, fps, samplerate);
671 avs->curr_sample = av_rescale_q(avs->curr_frame, samplerate, fps);
673 avs->curr_sample = timestamp;
680 AVInputFormat ff_avisynth_demuxer = {
682 .long_name = NULL_IF_CONFIG_SMALL("AviSynth script"),
683 .priv_data_size = sizeof(AviSynthContext),
684 .read_header = avisynth_read_header,
685 .read_packet = avisynth_read_packet,
686 .read_close = avisynth_read_close,
687 .read_seek = avisynth_read_seek,