summaryrefslogtreecommitdiffstats
path: root/libmpdemux/demux_avs.c
blob: 7c8de70225af10e9e0d50bdddd1e0deaf6eba535 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/*
 * Demuxer for avisynth
 * Copyright (c) 2005 Gianluigi Tiesi <sherpya@netfarm.it>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include "config.h"
#include "mp_msg.h"
#include "help_mp.h"

#include "stream.h"
#include "demuxer.h"
#include "stheader.h"

#include "wine/windef.h"

#ifdef WIN32_LOADER
#include "ldt_keeper.h"
#endif

#include "demux_avs.h"

#define MAX_AVS_SIZE    16 * 1024 /* 16k should be enough */
#undef ENABLE_AUDIO

HMODULE WINAPI LoadLibraryA(LPCSTR);
FARPROC WINAPI GetProcAddress(HMODULE,LPCSTR);
int     WINAPI FreeLibrary(HMODULE);

typedef WINAPI AVS_ScriptEnvironment* (*imp_avs_create_script_environment)(int version);
typedef WINAPI AVS_Value (*imp_avs_invoke)(AVS_ScriptEnvironment *, const char * name, AVS_Value args, const char** arg_names);
typedef WINAPI const AVS_VideoInfo *(*imp_avs_get_video_info)(AVS_Clip *);
typedef WINAPI AVS_Clip* (*imp_avs_take_clip)(AVS_Value, AVS_ScriptEnvironment *);
typedef WINAPI AVS_VideoFrame* (*imp_avs_get_frame)(AVS_Clip *, int n);
typedef WINAPI void (*imp_avs_release_video_frame)(AVS_VideoFrame *);
#ifdef ENABLE_AUDIO
//typedef WINAPI int (*imp_avs_get_audio)(AVS_Clip *, void * buf, uint64_t start, uint64_t count); 
typedef WINAPI int (*imp_avs_get_audio)(AVS_ScriptEnvironment *, void * buf, uint64_t start, uint64_t count); 
#endif

#define Q(string) # string
#define IMPORT_FUNC(x) \
    AVS->x = ( imp_##x ) GetProcAddress(AVS->dll, Q(x)); \
    if (!AVS->x) { mp_msg(MSGT_DEMUX,MSGL_V,"AVS: failed to load "Q(x)"()\n"); return 0; }

typedef struct tagAVS
{
    AVS_ScriptEnvironment *avs_env;
    AVS_Value handler;
    AVS_Clip *clip;
    const AVS_VideoInfo *video_info;
#ifdef WIN32_LOADER
    ldt_fs_t* ldt_fs;
#endif
    HMODULE dll;
    int frameno;
    int init;
    
    imp_avs_create_script_environment avs_create_script_environment;
    imp_avs_invoke avs_invoke;
    imp_avs_get_video_info avs_get_video_info;
    imp_avs_take_clip avs_take_clip;
    imp_avs_get_frame avs_get_frame;
    imp_avs_release_video_frame avs_release_video_frame;
#ifdef ENABLE_AUDIO
    imp_avs_get_audio avs_get_audio;
#endif
} AVS_T;

AVS_T *initAVS(const char *filename)
{   
    AVS_T *AVS = (AVS_T *) malloc (sizeof(AVS_T));
    AVS_Value arg0 = avs_new_value_string(filename);
    AVS_Value args = avs_new_value_array(&arg0, 1);
    
    memset(AVS, 0, sizeof(AVS_T));

#ifdef WIN32_LOADER
    AVS->ldt_fs = Setup_LDT_Keeper();
#endif
    
    AVS->dll = LoadLibraryA("avisynth.dll");
    if(!AVS->dll)
    {
        mp_msg(MSGT_DEMUX ,MSGL_V, "AVS: failed to load avisynth.dll\n");
        goto avs_err;
    }
    
    /* Dynamic import of needed stuff from avisynth.dll */
    IMPORT_FUNC(avs_create_script_environment);
    IMPORT_FUNC(avs_invoke);
    IMPORT_FUNC(avs_get_video_info);
    IMPORT_FUNC(avs_take_clip);
    IMPORT_FUNC(avs_get_frame);
    IMPORT_FUNC(avs_release_video_frame);
#ifdef ENABLE_AUDIO
    IMPORT_FUNC(avs_get_audio);
#endif
    
    AVS->avs_env = AVS->avs_create_script_environment(AVISYNTH_INTERFACE_VERSION);
    if (!AVS->avs_env)
    {
        mp_msg(MSGT_DEMUX, MSGL_V, "AVS: avs_create_script_environment failed\n");
        goto avs_err;
    }
    

    AVS->handler = AVS->avs_invoke(AVS->avs_env, "Import", args, 0);
    
    if (avs_is_error(AVS->handler))
    {
        mp_msg(MSGT_DEMUX, MSGL_V, "AVS: Avisynth error: %s\n", avs_as_string(AVS->handler));
        goto avs_err;
    }

    if (!avs_is_clip(AVS->handler))
    {
        mp_msg(MSGT_DEMUX, MSGL_V, "AVS: Avisynth doesn't return a clip\n");
        goto avs_err;
    }
    
    return AVS;

avs_err:
    if (AVS->dll) FreeLibrary(AVS->dll);
#ifdef WIN32_LOADER
    Restore_LDT_Keeper(AVS->ldt_fs);
#endif
    free(AVS);
    return NULL;
}

/* Implement RGB MODES ?? */
#if 0
static __inline int get_mmioFOURCC(const AVS_VideoInfo *v)
{
    if (avs_is_rgb(v)) return mmioFOURCC(8, 'R', 'G', 'B');
    if (avs_is_rgb24(v)) return mmioFOURCC(24, 'R', 'G', 'B');
    if (avs_is_rgb32(v)) return mmioFOURCC(32, 'R', 'G', 'B');
    if (avs_is_yv12(v)) return mmioFOURCC('Y', 'V', '1', '2');
    if (avs_is_yuy(v)) return mmioFOURCC('Y', 'U', 'Y', ' ');
    if (avs_is_yuy2(v)) return mmioFOURCC('Y', 'U', 'Y', '2');    
    return 0;
}
#endif

static int demux_avs_fill_buffer(demuxer_t *demuxer, demux_stream_t *ds)
{
    AVS_VideoFrame *curr_frame;
    demux_packet_t *dp = NULL;
    AVS_T *AVS = (AVS_T *) demuxer->priv;

    demux_stream_t *d_video=demuxer->video;
    sh_video_t *sh_video=d_video->sh;

#ifdef ENABLE_AUDIO
    demux_stream_t *d_audio=demuxer->audio;
    sh_audio_t *sh_audio=d_audio->sh;
#endif
    
    if (AVS->video_info->num_frames < AVS->frameno) return 0; // EOF
    
    curr_frame = AVS->avs_get_frame(AVS->clip, AVS->frameno);
    if (!curr_frame)
    {
        mp_msg(MSGT_DEMUX, MSGL_V, "AVS: error getting frame -- EOF??\n");
        return 0;
    }

    if (avs_has_video(AVS->video_info))
    {
        dp = new_demux_packet(curr_frame->vfb->data_size);
        sh_video->num_frames_decoded++;
        sh_video->num_frames++;

        d_video->pts=AVS->frameno / sh_video->fps; // OSD

        memcpy(dp->buffer, curr_frame->vfb->data + curr_frame->offset, curr_frame->vfb->data_size);
        ds_add_packet(demuxer->video, dp);

    }
    
#ifdef ENABLE_AUDIO
    /* Audio */
    if (avs_has_audio(AVS->video_info))
    {
        int l = sh_audio->wf->nAvgBytesPerSec;
        dp = new_demux_packet(l);
        
        if (AVS->avs_get_audio(AVS->avs_env, dp->buffer, AVS->frameno*sh_video->fps*l, l))
        {
            mp_msg(MSGT_DEMUX, MSGL_V, "AVS: avs_get_audio() failed\n");
            return 0;
        }
        ds_add_packet(demuxer->audio, dp);
    }
#endif
    
    AVS->frameno++;
    AVS->avs_release_video_frame(curr_frame);
    return 1;
}

static demuxer_t* demux_open_avs(demuxer_t* demuxer)
{
    sh_video_t *sh_video = NULL;
#ifdef ENABLE_AUDIO
    sh_audio_t *sh_audio = NULL;
#endif
    int found = 0;
    AVS_T *AVS = (AVS_T *) demuxer->priv;
    AVS->frameno = 0;

    mp_msg(MSGT_DEMUX, MSGL_V, "AVS: demux_open_avs()\n");
    demuxer->seekable = 1;

    AVS->clip = AVS->avs_take_clip(AVS->handler, AVS->avs_env);
    if(!AVS->clip)
    {
        mp_msg(MSGT_DEMUX, MSGL_V, "AVS: avs_take_clip() failed\n");
        return NULL;
    }

    AVS->video_info = AVS->avs_get_video_info(AVS->clip);
    if (!AVS->video_info)
    {
        mp_msg(MSGT_DEMUX, MSGL_V, "AVS: avs_get_video_info() call failed\n");
        return NULL;
    }
    
    if (!avs_is_yv12(AVS->video_info))
    {
        AVS->handler = AVS->avs_invoke(AVS->avs_env, "ConvertToYV12", avs_new_value_array(&AVS->handler, 1), 0);
        if (avs_is_error(AVS->handler))
        {
            mp_msg(MSGT_DEMUX, MSGL_V, "AVS: Cannot convert input video to YV12: %s\n", avs_as_string(AVS->handler));
            return NULL;
        }
        
        AVS->clip = AVS->avs_take_clip(AVS->handler, AVS->avs_env);
        
        if(!AVS->clip)
        {
            mp_msg(MSGT_DEMUX, MSGL_V, "AVS: avs_take_clip() failed\n");
            return NULL;
        }

        AVS->video_info = AVS->avs_get_video_info(AVS->clip);
        if (!AVS->video_info)
        {
            mp_msg(MSGT_DEMUX, MSGL_V, "AVS: avs_get_video_info() call failed\n");
            return NULL;
        }
    }
    
    // TODO check field-based ??

    /* Video */  
    if (avs_has_video(AVS->video_info))
    {
        found = 1;
        sh_video = new_sh_video(demuxer, 0);
        
        demuxer->video->sh = sh_video;
        sh_video->ds = demuxer->video;
        
        sh_video->disp_w = AVS->video_info->width;
        sh_video->disp_h = AVS->video_info->height;
        
        //sh_video->format = get_mmioFOURCC(AVS->video_info);
        sh_video->format = mmioFOURCC('Y', 'V', '1', '2');
        sh_video->fps = (float) ((float) AVS->video_info->fps_numerator / (float) AVS->video_info->fps_denominator);
        sh_video->frametime = 1.0 / sh_video->fps;
        
        sh_video->bih = (BITMAPINFOHEADER*) malloc(sizeof(BITMAPINFOHEADER) + (256 * 4));
        sh_video->bih->biCompression = sh_video->format;
        sh_video->bih->biBitCount = avs_bits_per_pixel(AVS->video_info);
        //sh_video->bih->biPlanes = 2;
        
        sh_video->bih->biWidth = AVS->video_info->width;
        sh_video->bih->biHeight = AVS->video_info->height;
        sh_video->num_frames = 0;
        sh_video->num_frames_decoded = 0;
    }
    
#ifdef ENABLE_AUDIO
    /* Audio */
    if (avs_has_audio(AVS->video_info))
    {
        found = 1;
        mp_msg(MSGT_DEMUX, MSGL_V, "AVS: Clip has audio -> Channels = %d - Freq = %d\n", AVS->video_info->nchannels, AVS->video_info->audio_samples_per_second);

        sh_audio = new_sh_audio(demuxer, 0);
        demuxer->audio->sh = sh_audio;
        sh_audio->ds = demuxer->audio;
        
        sh_audio->wf = (WAVEFORMATEX*) malloc(sizeof(WAVEFORMATEX));
        sh_audio->wf->wFormatTag = sh_audio->format = 0x1;
        sh_audio->wf->nChannels = sh_audio->channels = AVS->video_info->nchannels;
        sh_audio->wf->nSamplesPerSec = sh_audio->samplerate = AVS->video_info->audio_samples_per_second;
        sh_audio->wf->nAvgBytesPerSec = AVS->video_info->audio_samples_per_second * 4;
        sh_audio->wf->nBlockAlign = 4;
        sh_audio->wf->wBitsPerSample = sh_audio->samplesize = 16; // AVS->video_info->sample_type ??
        sh_audio->wf->cbSize = 0;
        sh_audio->i_bps = sh_audio->wf->nAvgBytesPerSec;
        sh_audio->o_bps = sh_audio->wf->nAvgBytesPerSec;
    }
#endif

    AVS->init = 1;
    if (found)
        return demuxer;
    else
        return NULL;
}

static int demux_avs_control(demuxer_t *demuxer, int cmd, void *arg)
{   
    demux_stream_t *d_video=demuxer->video;
    sh_video_t *sh_video=d_video->sh;
    AVS_T *AVS = (AVS_T *) demuxer->priv;

    switch(cmd)
    {
        case DEMUXER_CTRL_GET_TIME_LENGTH:
        {
            if (!AVS->video_info->num_frames) return DEMUXER_CTRL_DONTKNOW;
            *((double *)arg) = (double)AVS->video_info->num_frames / sh_video->fps;
            return DEMUXER_CTRL_OK;
        }
        case DEMUXER_CTRL_GET_PERCENT_POS:
        {
            if (!AVS->video_info->num_frames) return DEMUXER_CTRL_DONTKNOW;
            *((int *)arg) = (int) (AVS->frameno * 100 / AVS->video_info->num_frames);
            return DEMUXER_CTRL_OK;
        }
    default:
        return DEMUXER_CTRL_NOTIMPL;
    }
}

static void demux_close_avs(demuxer_t* demuxer)
{
    AVS_T *AVS = (AVS_T *) demuxer->priv;
    // TODO release_clip?
    if (AVS)
    {
        if (AVS->dll)
        {
            mp_msg(MSGT_DEMUX, MSGL_V, "AVS: Unloading avisynth.dll\n");
            FreeLibrary(AVS->dll);
        }
#ifdef WIN32_LOADER
        Restore_LDT_Keeper(AVS->ldt_fs);
#endif
        free(AVS);
    }
}

static void demux_seek_avs(demuxer_t *demuxer, float rel_seek_secs, float audio_delay, int flags)
{
    demux_stream_t *d_video=demuxer->video;
    sh_video_t *sh_video=d_video->sh;
    AVS_T *AVS = (AVS_T *) demuxer->priv;
    int video_pos=AVS->frameno;
    
    //mp_msg(MSGT_DEMUX, MSGL_V, "AVS: seek rel_seek_secs = %f - flags = %x\n", rel_seek_secs, flags);
    
    // seek absolute
    if (flags&1) video_pos=0;

    video_pos += (rel_seek_secs * sh_video->fps);
    if (video_pos < 0) video_pos = 0;
    if (video_pos > AVS->video_info->num_frames) video_pos = AVS->video_info->num_frames;
        
    AVS->frameno = video_pos;
    sh_video->num_frames_decoded = video_pos;
    sh_video->num_frames = video_pos;
    d_video->pts=AVS->frameno / sh_video->fps; // OSD
}

static int avs_check_file(demuxer_t *demuxer)
{
    mp_msg(MSGT_DEMUX, MSGL_V, "AVS: avs_check_file - attempting to open file %s\n", demuxer->filename);

    if (!demuxer->filename) return 0;
    
    /* Avoid crazy memory eating when passing an mpg stream */
    if (demuxer->movi_end > MAX_AVS_SIZE)
    {
        mp_msg(MSGT_DEMUX,MSGL_V, "AVS: File is too big, aborting...\n");
        return 0;
    }
    
    demuxer->priv = initAVS(demuxer->filename);
    
    if (demuxer->priv)
    {
        mp_msg(MSGT_DEMUX,MSGL_V, "AVS: Init Ok\n");
        return DEMUXER_TYPE_AVS;
    }
    mp_msg(MSGT_DEMUX,MSGL_V, "AVS: Init failed\n");
    return 0;
}


demuxer_desc_t demuxer_desc_avs = {
  "Avisynth demuxer",
  "avs",
  "AVS",
  "Gianluigi Tiesi",
  "Requires binary dll",
  DEMUXER_TYPE_AVS,
  0, // unsafe autodetect
  avs_check_file,
  demux_avs_fill_buffer,
  demux_open_avs,
  demux_close_avs,
  demux_seek_avs,
  demux_avs_control
};