avisynth.c 25.8 KB
Newer Older
1
/*
d s's avatar
d s committed
2
 * AviSynth/AvxSynth support
3
 * Copyright (c) 2012 AvxSynth Team
4
 *
5
 * This file is part of Libav.
6
 *
7
 * Libav is free software; you can redistribute it and/or
8 9
 * modify it under the terms of the GNU Lesser General Public
 * 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
 * Libav is distributed in the hope that it will be useful,
13 14 15 16 17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with Libav; if not, write to the Free Software
19 20 21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

22
#include "libavutil/attributes.h"
23
#include "libavutil/internal.h"
24

d s's avatar
d s committed
25
#include "libavcodec/internal.h"
26

27
#include "avformat.h"
28
#include "internal.h"
29
#include "config.h"
d s's avatar
d s committed
30 31 32 33

/* Enable function pointer definitions for runtime loading. */
#define AVSC_NO_DECLSPEC

34
/* Platform-specific directives for AviSynth vs AvxSynth. */
35
#if CONFIG_AVISYNTH
d s's avatar
d s committed
36 37 38 39 40 41 42
  #include <windows.h>
  #undef EXTERN_C
  #include <avisynth/avisynth_c.h>
  #define AVISYNTH_LIB "avisynth"
#else
  #include <dlfcn.h>
  #include <avxsynth/avxsynth_c.h>
43 44
  #define AVISYNTH_NAME "libavxsynth"
  #define AVISYNTH_LIB AVISYNTH_NAME SLIBSUF
d s's avatar
d s committed
45

46
  #define LoadLibrary(x) dlopen(x, RTLD_NOW | RTLD_LOCAL)
d s's avatar
d s committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
  #define GetProcAddress dlsym
  #define FreeLibrary dlclose
#endif

typedef struct AviSynthLibrary {
    void *library;
#define AVSC_DECLARE_FUNC(name) name ## _func name
    AVSC_DECLARE_FUNC(avs_bit_blt);
    AVSC_DECLARE_FUNC(avs_clip_get_error);
    AVSC_DECLARE_FUNC(avs_create_script_environment);
    AVSC_DECLARE_FUNC(avs_delete_script_environment);
    AVSC_DECLARE_FUNC(avs_get_audio);
    AVSC_DECLARE_FUNC(avs_get_error);
    AVSC_DECLARE_FUNC(avs_get_frame);
    AVSC_DECLARE_FUNC(avs_get_version);
    AVSC_DECLARE_FUNC(avs_get_video_info);
    AVSC_DECLARE_FUNC(avs_invoke);
    AVSC_DECLARE_FUNC(avs_release_clip);
    AVSC_DECLARE_FUNC(avs_release_value);
    AVSC_DECLARE_FUNC(avs_release_video_frame);
    AVSC_DECLARE_FUNC(avs_take_clip);
68
#if CONFIG_AVISYNTH
69 70 71 72 73
    AVSC_DECLARE_FUNC(avs_bits_per_pixel);
    AVSC_DECLARE_FUNC(avs_get_height_p);
    AVSC_DECLARE_FUNC(avs_get_pitch_p);
    AVSC_DECLARE_FUNC(avs_get_read_ptr_p);
    AVSC_DECLARE_FUNC(avs_get_row_size_p);
74 75
    AVSC_DECLARE_FUNC(avs_is_planar_rgb);
    AVSC_DECLARE_FUNC(avs_is_planar_rgba);
76
#endif
d s's avatar
d s committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
#undef AVSC_DECLARE_FUNC
} AviSynthLibrary;

typedef struct AviSynthContext {
    AVS_ScriptEnvironment *env;
    AVS_Clip *clip;
    const AVS_VideoInfo *vi;

    /* avisynth_read_packet_video() iterates over this. */
    int n_planes;
    const int *planes;

    int curr_stream;
    int curr_frame;
    int64_t curr_sample;

    int error;

    /* Linked list pointers. */
    struct AviSynthContext *next;
Diego Biurrun's avatar
Diego Biurrun committed
97
} AviSynthContext;
98

d s's avatar
d s committed
99 100 101 102
static const int avs_planes_packed[1] = { 0 };
static const int avs_planes_grey[1]   = { AVS_PLANAR_Y };
static const int avs_planes_yuv[3]    = { AVS_PLANAR_Y, AVS_PLANAR_U,
                                          AVS_PLANAR_V };
103 104 105 106 107 108 109 110
#ifdef USING_AVISYNTH
static const int avs_planes_rgb[3]    = { AVS_PLANAR_G, AVS_PLANAR_B,
                                          AVS_PLANAR_R };
static const int avs_planes_yuva[4]   = { AVS_PLANAR_Y, AVS_PLANAR_U,
                                          AVS_PLANAR_V, AVS_PLANAR_A };
static const int avs_planes_rgba[4]   = { AVS_PLANAR_G, AVS_PLANAR_B,
                                          AVS_PLANAR_R, AVS_PLANAR_A };
#endif
d s's avatar
d s committed
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128

/* A conflict between C++ global objects, atexit, and dynamic loading requires
 * us to register our own atexit handler to prevent double freeing. */
static AviSynthLibrary avs_library;
static int avs_atexit_called        = 0;

/* Linked list of AviSynthContexts. An atexit handler destroys this list. */
static AviSynthContext *avs_ctx_list = NULL;

static av_cold void avisynth_atexit_handler(void);

static av_cold int avisynth_load_library(void)
{
    avs_library.library = LoadLibrary(AVISYNTH_LIB);
    if (!avs_library.library)
        return AVERROR_UNKNOWN;

#define LOAD_AVS_FUNC(name, continue_on_fail)                          \
129 130
        avs_library.name = (name ## _func)                             \
                           GetProcAddress(avs_library.library, #name); \
d s's avatar
d s committed
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
        if (!continue_on_fail && !avs_library.name)                    \
            goto fail;

    LOAD_AVS_FUNC(avs_bit_blt, 0);
    LOAD_AVS_FUNC(avs_clip_get_error, 0);
    LOAD_AVS_FUNC(avs_create_script_environment, 0);
    LOAD_AVS_FUNC(avs_delete_script_environment, 0);
    LOAD_AVS_FUNC(avs_get_audio, 0);
    LOAD_AVS_FUNC(avs_get_error, 1); // New to AviSynth 2.6
    LOAD_AVS_FUNC(avs_get_frame, 0);
    LOAD_AVS_FUNC(avs_get_version, 0);
    LOAD_AVS_FUNC(avs_get_video_info, 0);
    LOAD_AVS_FUNC(avs_invoke, 0);
    LOAD_AVS_FUNC(avs_release_clip, 0);
    LOAD_AVS_FUNC(avs_release_value, 0);
    LOAD_AVS_FUNC(avs_release_video_frame, 0);
    LOAD_AVS_FUNC(avs_take_clip, 0);
148
#if CONFIG_AVISYNTH
149 150 151 152 153
    LOAD_AVS_FUNC(avs_bits_per_pixel, 1);
    LOAD_AVS_FUNC(avs_get_height_p, 1);
    LOAD_AVS_FUNC(avs_get_pitch_p, 1);
    LOAD_AVS_FUNC(avs_get_read_ptr_p, 1);
    LOAD_AVS_FUNC(avs_get_row_size_p, 1);
154 155
    LOAD_AVS_FUNC(avs_is_planar_rgb, 1);
    LOAD_AVS_FUNC(avs_is_planar_rgba, 1);
156
#endif
d s's avatar
d s committed
157 158 159 160 161 162 163 164 165 166 167 168 169 170
#undef LOAD_AVS_FUNC

    atexit(avisynth_atexit_handler);
    return 0;

fail:
    FreeLibrary(avs_library.library);
    return AVERROR_UNKNOWN;
}

/* Note that avisynth_context_create and avisynth_context_destroy
 * do not allocate or free the actual context! That is taken care of
 * by libavformat. */
static av_cold int avisynth_context_create(AVFormatContext *s)
171
{
172
    AviSynthContext *avs = s->priv_data;
d s's avatar
d s committed
173 174 175 176 177 178 179 180 181 182 183 184
    int ret;

    if (!avs_library.library)
        if (ret = avisynth_load_library())
            return ret;

    avs->env = avs_library.avs_create_script_environment(3);
    if (avs_library.avs_get_error) {
        const char *error = avs_library.avs_get_error(avs->env);
        if (error) {
            av_log(s, AV_LOG_ERROR, "%s\n", error);
            return AVERROR_UNKNOWN;
185 186 187
        }
    }

d s's avatar
d s committed
188 189 190 191 192 193 194
    if (!avs_ctx_list) {
        avs_ctx_list = avs;
    } else {
        avs->next    = avs_ctx_list;
        avs_ctx_list = avs;
    }

195
    return 0;
196 197
}

d s's avatar
d s committed
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
static av_cold void avisynth_context_destroy(AviSynthContext *avs)
{
    if (avs_atexit_called)
        return;

    if (avs == avs_ctx_list) {
        avs_ctx_list = avs->next;
    } else {
        AviSynthContext *prev = avs_ctx_list;
        while (prev->next != avs)
            prev = prev->next;
        prev->next = avs->next;
    }

    if (avs->clip) {
        avs_library.avs_release_clip(avs->clip);
        avs->clip = NULL;
    }
    if (avs->env) {
        avs_library.avs_delete_script_environment(avs->env);
        avs->env = NULL;
    }
}

static av_cold void avisynth_atexit_handler(void)
{
    AviSynthContext *avs = avs_ctx_list;

    while (avs) {
        AviSynthContext *next = avs->next;
        avisynth_context_destroy(avs);
        avs = next;
    }
    FreeLibrary(avs_library.library);

    avs_atexit_called = 1;
}

/* Create AVStream from audio and video data. */
static int avisynth_create_stream_video(AVFormatContext *s, AVStream *st)
{
    AviSynthContext *avs = s->priv_data;
240
    int planar = 0; // 0: packed, 1: YUV, 2: Y8, 3: Planar RGB, 4: YUVA, 5: Planar RGBA
d s's avatar
d s committed
241

242 243 244 245
    st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
    st->codecpar->codec_id   = AV_CODEC_ID_RAWVIDEO;
    st->codecpar->width      = avs->vi->width;
    st->codecpar->height     = avs->vi->height;
d s's avatar
d s committed
246 247 248 249 250 251

    st->avg_frame_rate    = (AVRational) { avs->vi->fps_numerator,
                                           avs->vi->fps_denominator };
    st->start_time        = 0;
    st->duration          = avs->vi->num_frames;
    st->nb_frames         = avs->vi->num_frames;
252
    avpriv_set_pts_info(st, 32, avs->vi->fps_denominator, avs->vi->fps_numerator);
d s's avatar
d s committed
253 254

    switch (avs->vi->pixel_type) {
255
#if CONFIG_AVISYNTH
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
    /* 10~16-bit YUV pix_fmts (AviSynth+) */
    case AVS_CS_YUV444P10:
        st->codecpar->format = AV_PIX_FMT_YUV444P10;
        planar               = 1;
        break;
    case AVS_CS_YUV422P10:
        st->codecpar->format = AV_PIX_FMT_YUV422P10;
        planar               = 1;
        break;
    case AVS_CS_YUV420P10:
        st->codecpar->format = AV_PIX_FMT_YUV420P10;
        planar               = 1;
        break;
    case AVS_CS_YUV444P12:
        st->codecpar->format = AV_PIX_FMT_YUV444P12;
        planar               = 1;
        break;
    case AVS_CS_YUV422P12:
        st->codecpar->format = AV_PIX_FMT_YUV422P12;
        planar               = 1;
        break;
    case AVS_CS_YUV420P12:
        st->codecpar->format = AV_PIX_FMT_YUV420P12;
        planar               = 1;
        break;
    case AVS_CS_YUV444P16:
        st->codecpar->format = AV_PIX_FMT_YUV444P16;
        planar               = 1;
        break;
    case AVS_CS_YUV422P16:
        st->codecpar->format = AV_PIX_FMT_YUV422P16;
        planar               = 1;
        break;
    case AVS_CS_YUV420P16:
        st->codecpar->format = AV_PIX_FMT_YUV420P16;
        planar               = 1;
        break;
    /* 8~16-bit YUV pix_fmts with Alpha (AviSynth+) */
    case AVS_CS_YUVA444:
        st->codecpar->format = AV_PIX_FMT_YUVA444P;
        planar               = 4;
        break;
    case AVS_CS_YUVA422:
        st->codecpar->format = AV_PIX_FMT_YUVA422P;
        planar               = 4;
        break;
    case AVS_CS_YUVA420:
        st->codecpar->format = AV_PIX_FMT_YUVA420P;
        planar               = 4;
        break;
    case AVS_CS_YUVA444P10:
        st->codecpar->format = AV_PIX_FMT_YUVA444P10;
        planar               = 4;
        break;
    case AVS_CS_YUVA422P10:
        st->codecpar->format = AV_PIX_FMT_YUVA422P10;
        planar               = 4;
        break;
    case AVS_CS_YUVA420P10:
        st->codecpar->format = AV_PIX_FMT_YUVA420P10;
        planar               = 4;
        break;
    case AVS_CS_YUVA444P16:
        st->codecpar->format = AV_PIX_FMT_YUVA444P16;
        planar               = 4;
        break;
    case AVS_CS_YUVA422P16:
        st->codecpar->format = AV_PIX_FMT_YUVA422P16;
        planar               = 4;
        break;
    case AVS_CS_YUVA420P16:
        st->codecpar->format = AV_PIX_FMT_YUVA420P16;
        planar               = 4;
        break;
    /* Planar RGB pix_fmts (AviSynth+) */
    case AVS_CS_RGBP:
        st->codecpar->format = AV_PIX_FMT_GBRP;
        planar               = 3;
        break;
    case AVS_CS_RGBP10:
        st->codecpar->format = AV_PIX_FMT_GBRP10;
        planar               = 3;
        break;
    case AVS_CS_RGBP12:
        st->codecpar->format = AV_PIX_FMT_GBRP12;
        planar               = 3;
        break;
    case AVS_CS_RGBP16:
        st->codecpar->format = AV_PIX_FMT_GBRP16;
        planar               = 3;
        break;
    /* Planar RGB pix_fmts with Alpha (AviSynth+) */
    case AVS_CS_RGBAP:
        st->codecpar->format = AV_PIX_FMT_GBRAP;
        planar               = 5;
        break;
    case AVS_CS_RGBAP12:
        st->codecpar->format = AV_PIX_FMT_GBRAP12;
        planar               = 5;
        break;
    case AVS_CS_RGBAP16:
        st->codecpar->format = AV_PIX_FMT_GBRAP16;
        planar               = 5;
        break;
    /* GRAY16 (AviSynth+) */
    case AVS_CS_Y16:
        st->codecpar->format = AV_PIX_FMT_GRAY16;
        planar               = 2;
        break;
    /* pix_fmts added in AviSynth 2.6 */
d s's avatar
d s committed
366
    case AVS_CS_YV24:
367 368
        st->codecpar->format = AV_PIX_FMT_YUV444P;
        planar               = 1;
d s's avatar
d s committed
369 370
        break;
    case AVS_CS_YV16:
371 372
        st->codecpar->format = AV_PIX_FMT_YUV422P;
        planar               = 1;
d s's avatar
d s committed
373 374
        break;
    case AVS_CS_YV411:
375 376
        st->codecpar->format = AV_PIX_FMT_YUV411P;
        planar               = 1;
d s's avatar
d s committed
377 378
        break;
    case AVS_CS_Y8:
379 380
        st->codecpar->format = AV_PIX_FMT_GRAY8;
        planar               = 2;
d s's avatar
d s committed
381
        break;
382 383 384 385 386 387 388
    /* 16-bit packed RGB pix_fmts (AviSynth+) */
    case AVS_CS_BGR48:
        st->codecpar->format = AV_PIX_FMT_BGR48;
        break;
    case AVS_CS_BGR64:
        st->codecpar->format = AV_PIX_FMT_BGRA64;
        break;
d s's avatar
d s committed
389
#endif
390
    /* AviSynth 2.5 and AvxSynth pix_fmts */
d s's avatar
d s committed
391
    case AVS_CS_BGR24:
392
        st->codecpar->format = AV_PIX_FMT_BGR24;
d s's avatar
d s committed
393 394
        break;
    case AVS_CS_BGR32:
395
        st->codecpar->format = AV_PIX_FMT_RGB32;
d s's avatar
d s committed
396 397
        break;
    case AVS_CS_YUY2:
398
        st->codecpar->format = AV_PIX_FMT_YUYV422;
d s's avatar
d s committed
399 400
        break;
    case AVS_CS_YV12:
401 402
        st->codecpar->format = AV_PIX_FMT_YUV420P;
        planar               = 1;
d s's avatar
d s committed
403 404
        break;
    case AVS_CS_I420: // Is this even used anywhere?
405 406
        st->codecpar->format = AV_PIX_FMT_YUV420P;
        planar               = 1;
d s's avatar
d s committed
407 408 409 410 411 412 413 414 415
        break;
    default:
        av_log(s, AV_LOG_ERROR,
               "unknown AviSynth colorspace %d\n", avs->vi->pixel_type);
        avs->error = 1;
        return AVERROR_UNKNOWN;
    }

    switch (planar) {
416 417 418 419 420 421 422 423 424 425 426 427 428 429
#ifdef USING_AVISYNTH
    case 5: // Planar RGB + Alpha
        avs->n_planes = 4;
        avs->planes   = avs_planes_rgba;
        break;
    case 4: // YUV + Alpha
        avs->n_planes = 4;
        avs->planes   = avs_planes_yuva;
        break;
    case 3: // Planar RGB
        avs->n_planes = 3;
        avs->planes   = avs_planes_rgb;
        break;
#endif
d s's avatar
d s committed
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
    case 2: // Y8
        avs->n_planes = 1;
        avs->planes   = avs_planes_grey;
        break;
    case 1: // YUV
        avs->n_planes = 3;
        avs->planes   = avs_planes_yuv;
        break;
    default:
        avs->n_planes = 1;
        avs->planes   = avs_planes_packed;
    }
    return 0;
}

static int avisynth_create_stream_audio(AVFormatContext *s, AVStream *st)
{
    AviSynthContext *avs = s->priv_data;

449 450 451
    st->codecpar->codec_type  = AVMEDIA_TYPE_AUDIO;
    st->codecpar->sample_rate = avs->vi->audio_samples_per_second;
    st->codecpar->channels    = avs->vi->nchannels;
452 453
    st->duration              = avs->vi->num_audio_samples;
    avpriv_set_pts_info(st, 64, 1, avs->vi->audio_samples_per_second);
d s's avatar
d s committed
454 455 456

    switch (avs->vi->sample_type) {
    case AVS_SAMPLE_INT8:
457
        st->codecpar->codec_id = AV_CODEC_ID_PCM_U8;
d s's avatar
d s committed
458 459
        break;
    case AVS_SAMPLE_INT16:
460
        st->codecpar->codec_id = AV_CODEC_ID_PCM_S16LE;
d s's avatar
d s committed
461 462
        break;
    case AVS_SAMPLE_INT24:
463
        st->codecpar->codec_id = AV_CODEC_ID_PCM_S24LE;
d s's avatar
d s committed
464 465
        break;
    case AVS_SAMPLE_INT32:
466
        st->codecpar->codec_id = AV_CODEC_ID_PCM_S32LE;
d s's avatar
d s committed
467 468
        break;
    case AVS_SAMPLE_FLOAT:
469
        st->codecpar->codec_id = AV_CODEC_ID_PCM_F32LE;
d s's avatar
d s committed
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
        break;
    default:
        av_log(s, AV_LOG_ERROR,
               "unknown AviSynth sample type %d\n", avs->vi->sample_type);
        avs->error = 1;
        return AVERROR_UNKNOWN;
    }
    return 0;
}

static int avisynth_create_stream(AVFormatContext *s)
{
    AviSynthContext *avs = s->priv_data;
    AVStream *st;
    int ret;
    int id = 0;

    if (avs_has_video(avs->vi)) {
        st = avformat_new_stream(s, NULL);
        if (!st)
            return AVERROR_UNKNOWN;
        st->id = id++;
        if (ret = avisynth_create_stream_video(s, st))
            return ret;
    }
    if (avs_has_audio(avs->vi)) {
        st = avformat_new_stream(s, NULL);
        if (!st)
            return AVERROR_UNKNOWN;
        st->id = id++;
        if (ret = avisynth_create_stream_audio(s, st))
            return ret;
    }
    return 0;
}

static int avisynth_open_file(AVFormatContext *s)
507
{
508
    AviSynthContext *avs = s->priv_data;
d s's avatar
d s committed
509 510
    AVS_Value arg, val;
    int ret;
511
#if CONFIG_AVISYNTH
d s's avatar
d s committed
512 513 514 515 516 517 518
    char filename_ansi[MAX_PATH * 4];
    wchar_t filename_wc[MAX_PATH * 4];
#endif

    if (ret = avisynth_context_create(s))
        return ret;

519
#if CONFIG_AVISYNTH
d s's avatar
d s committed
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
    /* Convert UTF-8 to ANSI code page */
    MultiByteToWideChar(CP_UTF8, 0, s->filename, -1, filename_wc, MAX_PATH * 4);
    WideCharToMultiByte(CP_THREAD_ACP, 0, filename_wc, -1, filename_ansi,
                        MAX_PATH * 4, NULL, NULL);
    arg = avs_new_value_string(filename_ansi);
#else
    arg = avs_new_value_string(s->filename);
#endif
    val = avs_library.avs_invoke(avs->env, "Import", arg, 0);
    if (avs_is_error(val)) {
        av_log(s, AV_LOG_ERROR, "%s\n", avs_as_error(val));
        ret = AVERROR_UNKNOWN;
        goto fail;
    }
    if (!avs_is_clip(val)) {
        av_log(s, AV_LOG_ERROR, "AviSynth script did not return a clip\n");
        ret = AVERROR_UNKNOWN;
        goto fail;
    }
539

d s's avatar
d s committed
540 541
    avs->clip = avs_library.avs_take_clip(val, avs->env);
    avs->vi   = avs_library.avs_get_video_info(avs->clip);
542

543
#if CONFIG_AVISYNTH
544 545 546 547
    /* On Windows, libav supports AviSynth interface version 6 or higher.
     * This includes AviSynth 2.6 RC1 or higher, and AviSynth+ r1718 or higher,
     * and excludes 2.5 and the 2.6 alphas. Since AvxSynth identifies itself
     * as interface version 3 like 2.5.8, this needs to be special-cased. */
d s's avatar
d s committed
548

549
    if (avs_library.avs_get_version(avs->clip) < 6) {
d s's avatar
d s committed
550
        av_log(s, AV_LOG_ERROR,
551
               "AviSynth version is too old. Please upgrade to either AviSynth 2.6 >= RC1 or AviSynth+ >= r1718.\n");
d s's avatar
d s committed
552 553 554 555
        ret = AVERROR_UNKNOWN;
        goto fail;
    }
#endif
556

d s's avatar
d s committed
557 558 559 560 561 562 563
    /* Release the AVS_Value as it will go out of scope. */
    avs_library.avs_release_value(val);

    if (ret = avisynth_create_stream(s))
        goto fail;

    return 0;
564

d s's avatar
d s committed
565 566 567 568
fail:
    avisynth_context_destroy(avs);
    return ret;
}
569

d s's avatar
d s committed
570 571 572 573
static void avisynth_next_stream(AVFormatContext *s, AVStream **st,
                                 AVPacket *pkt, int *discard)
{
    AviSynthContext *avs = s->priv_data;
574

575
    avs->curr_stream++;
d s's avatar
d s committed
576
    avs->curr_stream %= s->nb_streams;
577

578
    *st = s->streams[avs->curr_stream];
d s's avatar
d s committed
579 580 581 582
    if ((*st)->discard == AVDISCARD_ALL)
        *discard = 1;
    else
        *discard = 0;
583

d s's avatar
d s committed
584
    return;
585 586
}

d s's avatar
d s committed
587 588 589
/* Copy AviSynth clip data into an AVPacket. */
static int avisynth_read_packet_video(AVFormatContext *s, AVPacket *pkt,
                                      int discard)
590
{
591
    AviSynthContext *avs = s->priv_data;
d s's avatar
d s committed
592 593 594 595 596
    AVS_VideoFrame *frame;
    unsigned char *dst_p;
    const unsigned char *src_p;
    int n, i, plane, rowsize, planeheight, pitch, bits;
    const char *error;
597
    int avsplus av_unused;
d s's avatar
d s committed
598 599 600 601 602 603 604 605 606

    if (avs->curr_frame >= avs->vi->num_frames)
        return AVERROR_EOF;

    /* This must happen even if the stream is discarded to prevent desync. */
    n = avs->curr_frame++;
    if (discard)
        return 0;

607
#if CONFIG_AVISYNTH
608 609 610 611 612 613 614
    /* Detect whether we're using AviSynth 2.6 or AviSynth+ by
     * looking for whether avs_is_planar_rgb exists. */
    if (GetProcAddress(avs_library.library, "avs_is_planar_rgb") == NULL)
        avsplus = 0;
    else
        avsplus = 1;

615 616 617 618 619
    /* avs_bits_per_pixel changed to AVSC_API with AviSynth 2.6, which
     * requires going through avs_library, while AvxSynth has it under
     * the older AVSC_INLINE type, so special-case this. */

    bits = avs_library.avs_bits_per_pixel(avs->vi);
620 621
#else
    bits = avs_bits_per_pixel(avs->vi);
d s's avatar
d s committed
622 623 624 625 626 627 628 629 630 631 632 633
#endif

    /* Without the cast to int64_t, calculation overflows at about 9k x 9k
     * resolution. */
    pkt->size = (((int64_t)avs->vi->width *
                  (int64_t)avs->vi->height) * bits) / 8;
    if (!pkt->size)
        return AVERROR_UNKNOWN;

    if (av_new_packet(pkt, pkt->size) < 0)
        return AVERROR(ENOMEM);

634 635 636 637 638
    pkt->pts      = n;
    pkt->dts      = n;
    pkt->duration = 1;
    pkt->stream_index = avs->curr_stream;

d s's avatar
d s committed
639 640 641 642 643 644 645 646
    frame = avs_library.avs_get_frame(avs->clip, n);
    error = avs_library.avs_clip_get_error(avs->clip);
    if (error) {
        av_log(s, AV_LOG_ERROR, "%s\n", error);
        avs->error = 1;
        av_packet_unref(pkt);
        return AVERROR_UNKNOWN;
    }
647

d s's avatar
d s committed
648 649 650
    dst_p = pkt->data;
    for (i = 0; i < avs->n_planes; i++) {
        plane = avs->planes[i];
651
#if CONFIG_AVISYNTH
652 653 654 655 656 657
        src_p = avs_library.avs_get_read_ptr_p(frame, plane);
        pitch = avs_library.avs_get_pitch_p(frame, plane);

        rowsize     = avs_library.avs_get_row_size_p(frame, plane);
        planeheight = avs_library.avs_get_height_p(frame, plane);
#else
d s's avatar
d s committed
658 659 660 661 662
        src_p = avs_get_read_ptr_p(frame, plane);
        pitch = avs_get_pitch_p(frame, plane);

        rowsize     = avs_get_row_size_p(frame, plane);
        planeheight = avs_get_height_p(frame, plane);
663
#endif
d s's avatar
d s committed
664 665 666 667 668 669

        /* Flip RGB video. */
        if (avs_is_rgb24(avs->vi) || avs_is_rgb(avs->vi)) {
            src_p = src_p + (planeheight - 1) * pitch;
            pitch = -pitch;
        }
670

671 672 673 674 675 676 677 678 679
#ifdef USING_AVISYNTH
        /* Flip Planar RGB video */
        if (avsplus && (avs_library.avs_is_planar_rgb(avs->vi) ||
                        avs_library.avs_is_planar_rgba(avs->vi))) {
            src_p = src_p + (planeheight - 1) * pitch;
            pitch = -pitch;
        }
#endif

d s's avatar
d s committed
680 681 682 683 684 685
        avs_library.avs_bit_blt(avs->env, dst_p, rowsize, src_p, pitch,
                                 rowsize, planeheight);
        dst_p += rowsize * planeheight;
    }

    avs_library.avs_release_video_frame(frame);
686
    return 0;
687 688
}

d s's avatar
d s committed
689 690
static int avisynth_read_packet_audio(AVFormatContext *s, AVPacket *pkt,
                                      int discard)
691
{
692
    AviSynthContext *avs = s->priv_data;
d s's avatar
d s committed
693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739
    AVRational fps, samplerate;
    int samples;
    int64_t n;
    const char *error;

    if (avs->curr_sample >= avs->vi->num_audio_samples)
        return AVERROR_EOF;

    fps.num        = avs->vi->fps_numerator;
    fps.den        = avs->vi->fps_denominator;
    samplerate.num = avs->vi->audio_samples_per_second;
    samplerate.den = 1;

    if (avs_has_video(avs->vi)) {
        if (avs->curr_frame < avs->vi->num_frames)
            samples = av_rescale_q(avs->curr_frame, samplerate, fps) -
                      avs->curr_sample;
        else
            samples = av_rescale_q(1, samplerate, fps);
    } else {
        samples = 1000;
    }

    /* After seeking, audio may catch up with video. */
    if (samples <= 0) {
        pkt->size = 0;
        pkt->data = NULL;
        return 0;
    }

    if (avs->curr_sample + samples > avs->vi->num_audio_samples)
        samples = avs->vi->num_audio_samples - avs->curr_sample;

    /* This must happen even if the stream is discarded to prevent desync. */
    n                 = avs->curr_sample;
    avs->curr_sample += samples;
    if (discard)
        return 0;

    pkt->size = avs_bytes_per_channel_sample(avs->vi) *
                samples * avs->vi->nchannels;
    if (!pkt->size)
        return AVERROR_UNKNOWN;

    if (av_new_packet(pkt, pkt->size) < 0)
        return AVERROR(ENOMEM);

740 741 742 743 744
    pkt->pts      = n;
    pkt->dts      = n;
    pkt->duration = samples;
    pkt->stream_index = avs->curr_stream;

d s's avatar
d s committed
745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762
    avs_library.avs_get_audio(avs->clip, pkt->data, n, samples);
    error = avs_library.avs_clip_get_error(avs->clip);
    if (error) {
        av_log(s, AV_LOG_ERROR, "%s\n", error);
        avs->error = 1;
        av_packet_unref(pkt);
        return AVERROR_UNKNOWN;
    }
    return 0;
}

static av_cold int avisynth_read_header(AVFormatContext *s)
{
    int ret;

    // Calling library must implement a lock for thread-safe opens.
    if (ret = avpriv_lock_avformat())
        return ret;
763

d s's avatar
d s committed
764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785
    if (ret = avisynth_open_file(s)) {
        avpriv_unlock_avformat();
        return ret;
    }

    avpriv_unlock_avformat();
    return 0;
}

static int avisynth_read_packet(AVFormatContext *s, AVPacket *pkt)
{
    AviSynthContext *avs = s->priv_data;
    AVStream *st;
    int discard = 0;
    int ret;

    if (avs->error)
        return AVERROR_UNKNOWN;

    /* If either stream reaches EOF, try to read the other one before
     * giving up. */
    avisynth_next_stream(s, &st, pkt, &discard);
786
    if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
d s's avatar
d s committed
787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827
        ret = avisynth_read_packet_video(s, pkt, discard);
        if (ret == AVERROR_EOF && avs_has_audio(avs->vi)) {
            avisynth_next_stream(s, &st, pkt, &discard);
            return avisynth_read_packet_audio(s, pkt, discard);
        }
    } else {
        ret = avisynth_read_packet_audio(s, pkt, discard);
        if (ret == AVERROR_EOF && avs_has_video(avs->vi)) {
            avisynth_next_stream(s, &st, pkt, &discard);
            return avisynth_read_packet_video(s, pkt, discard);
        }
    }

    return ret;
}

static av_cold int avisynth_read_close(AVFormatContext *s)
{
    if (avpriv_lock_avformat())
        return AVERROR_UNKNOWN;

    avisynth_context_destroy(s->priv_data);
    avpriv_unlock_avformat();
    return 0;
}

static int avisynth_read_seek(AVFormatContext *s, int stream_index,
                              int64_t timestamp, int flags)
{
    AviSynthContext *avs = s->priv_data;
    AVStream *st;
    AVRational fps, samplerate;

    if (avs->error)
        return AVERROR_UNKNOWN;

    fps        = (AVRational) { avs->vi->fps_numerator,
                                avs->vi->fps_denominator };
    samplerate = (AVRational) { avs->vi->audio_samples_per_second, 1 };

    st = s->streams[stream_index];
828
    if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
d s's avatar
d s committed
829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847
        /* AviSynth frame counts are signed int. */
        if ((timestamp >= avs->vi->num_frames) ||
            (timestamp > INT_MAX)              ||
            (timestamp < 0))
            return AVERROR_EOF;
        avs->curr_frame = timestamp;
        if (avs_has_audio(avs->vi))
            avs->curr_sample = av_rescale_q(timestamp, samplerate, fps);
    } else {
        if ((timestamp >= avs->vi->num_audio_samples) || (timestamp < 0))
            return AVERROR_EOF;
        /* Force frame granularity for seeking. */
        if (avs_has_video(avs->vi)) {
            avs->curr_frame  = av_rescale_q(timestamp, fps, samplerate);
            avs->curr_sample = av_rescale_q(avs->curr_frame, samplerate, fps);
        } else {
            avs->curr_sample = timestamp;
        }
    }
848

849
    return 0;
850 851
}

852
AVInputFormat ff_avisynth_demuxer = {
853
    .name           = "avisynth",
d s's avatar
d s committed
854
    .long_name      = NULL_IF_CONFIG_SMALL("AviSynth script"),
Diego Biurrun's avatar
Diego Biurrun committed
855
    .priv_data_size = sizeof(AviSynthContext),
856 857 858 859 860
    .read_header    = avisynth_read_header,
    .read_packet    = avisynth_read_packet,
    .read_close     = avisynth_read_close,
    .read_seek      = avisynth_read_seek,
    .extensions     = "avs",
861
};