1 /*
2  * Demuxer for avisynth
3  * Copyright (c) 2005 Gianluigi Tiesi <sherpya@netfarm.it>
4  *
5  * This file is part of MPlayer.
6  *
7  * MPlayer 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  * MPlayer 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 License
18  * along with MPlayer; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21 
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <limits.h>
26 
27 #include "config.h"
28 #include "mp_msg.h"
29 #include "help_mp.h"
30 
31 #include "stream/stream.h"
32 #include "demuxer.h"
33 #include "stheader.h"
34 #include "libvo/fastmemcpy.h"
35 
36 #include "loader/wine/winbase.h"
37 #include "loader/wine/windef.h"
38 
39 #ifdef WIN32_LOADER
40 #include "loader/ldt_keeper.h"
41 #endif
42 
43 #include "demux_avs.h"
44 
45 #define MAX_AVS_SIZE    16 * 1024 /* 16k should be enough */
46 
47 typedef WINAPI AVS_ScriptEnvironment* (*imp_avs_create_script_environment)(int version);
48 typedef WINAPI AVS_Value (*imp_avs_invoke)(AVS_ScriptEnvironment *, const char * name, AVS_Value args, const char** arg_names);
49 typedef WINAPI const AVS_VideoInfo *(*imp_avs_get_video_info)(AVS_Clip *);
50 typedef WINAPI AVS_Clip* (*imp_avs_take_clip)(AVS_Value, AVS_ScriptEnvironment *);
51 typedef WINAPI void (*imp_avs_release_clip)(AVS_Clip *);
52 typedef WINAPI AVS_VideoFrame* (*imp_avs_get_frame)(AVS_Clip *, int n);
53 typedef WINAPI void (*imp_avs_release_video_frame)(AVS_VideoFrame *);
54 typedef WINAPI int (*imp_avs_get_audio)(AVS_Clip *, void * buf, uint64_t start, uint64_t count);
55 
56 #define Q(string) # string
57 #define IMPORT_FUNC(x) \
58     AVS->x = ( imp_##x ) GetProcAddress(AVS->dll, Q(x)); \
59     if (!AVS->x) { mp_msg(MSGT_DEMUX,MSGL_V,"AVS: failed to load "Q(x)"()\n"); return 0; }
60 
61 typedef struct tagAVS
62 {
63     AVS_ScriptEnvironment *avs_env;
64     AVS_Value handler;
65     AVS_Clip *clip;
66     const AVS_VideoInfo *video_info;
67 #ifdef WIN32_LOADER
68     ldt_fs_t* ldt_fs;
69 #endif
70     HMODULE dll;
71     int frameno;
72     uint64_t sampleno;
73     int init;
74 
75     imp_avs_create_script_environment avs_create_script_environment;
76     imp_avs_invoke avs_invoke;
77     imp_avs_get_video_info avs_get_video_info;
78     imp_avs_take_clip avs_take_clip;
79     imp_avs_release_clip avs_release_clip;
80     imp_avs_get_frame avs_get_frame;
81     imp_avs_release_video_frame avs_release_video_frame;
82     imp_avs_get_audio avs_get_audio;
83 } AVS_T;
84 
initAVS(const char * filename)85 static AVS_T *initAVS(const char *filename)
86 {
87     AVS_T *AVS = calloc(1, sizeof(AVS_T));
88     AVS_Value arg0 = avs_new_value_string(filename);
89     AVS_Value args = avs_new_value_array(&arg0, 1);
90 
91 #ifdef WIN32_LOADER
92     AVS->ldt_fs = Setup_LDT_Keeper();
93 #endif
94 
95     AVS->dll = LoadLibraryA("avisynth.dll");
96     if(!AVS->dll)
97     {
98         mp_msg(MSGT_DEMUX ,MSGL_V, "AVS: failed to load avisynth.dll\n");
99         goto avs_err;
100     }
101 
102     /* Dynamic import of needed stuff from avisynth.dll */
103     IMPORT_FUNC(avs_create_script_environment);
104     IMPORT_FUNC(avs_invoke);
105     IMPORT_FUNC(avs_get_video_info);
106     IMPORT_FUNC(avs_take_clip);
107     IMPORT_FUNC(avs_release_clip);
108     IMPORT_FUNC(avs_get_frame);
109     IMPORT_FUNC(avs_release_video_frame);
110     IMPORT_FUNC(avs_get_audio);
111 
112     AVS->avs_env = AVS->avs_create_script_environment(AVISYNTH_INTERFACE_VERSION);
113     if (!AVS->avs_env)
114     {
115         mp_msg(MSGT_DEMUX, MSGL_V, "AVS: avs_create_script_environment failed\n");
116         goto avs_err;
117     }
118 
119 
120     AVS->handler = AVS->avs_invoke(AVS->avs_env, "Import", args, 0);
121 
122     if (avs_is_error(AVS->handler))
123     {
124         mp_msg(MSGT_DEMUX, MSGL_V, "AVS: Avisynth error: %s\n", avs_as_string(AVS->handler));
125         goto avs_err;
126     }
127 
128     if (!avs_is_clip(AVS->handler))
129     {
130         mp_msg(MSGT_DEMUX, MSGL_V, "AVS: Avisynth doesn't return a clip\n");
131         goto avs_err;
132     }
133 
134     return AVS;
135 
136 avs_err:
137     if (AVS->dll) FreeLibrary(AVS->dll);
138 #ifdef WIN32_LOADER
139     Restore_LDT_Keeper(AVS->ldt_fs);
140 #endif
141     free(AVS);
142     return NULL;
143 }
144 
145 /* Implement RGB MODES ?? */
146 #if 0
147 static inline int get_mmioFOURCC(const AVS_VideoInfo *v)
148 {
149     if (avs_is_rgb(v)) return mmioFOURCC(8, 'R', 'G', 'B');
150     if (avs_is_rgb24(v)) return mmioFOURCC(24, 'R', 'G', 'B');
151     if (avs_is_rgb32(v)) return mmioFOURCC(32, 'R', 'G', 'B');
152     if (avs_is_yv12(v)) return mmioFOURCC('Y', 'V', '1', '2');
153     if (avs_is_yuy(v)) return mmioFOURCC('Y', 'U', 'Y', ' ');
154     if (avs_is_yuy2(v)) return mmioFOURCC('Y', 'U', 'Y', '2');
155     return 0;
156 }
157 #endif
158 
demux_avs_fill_buffer(demuxer_t * demuxer,demux_stream_t * ds)159 static int demux_avs_fill_buffer(demuxer_t *demuxer, demux_stream_t *ds)
160 {
161     AVS_VideoFrame *curr_frame;
162     demux_packet_t *dp = NULL;
163     AVS_T *AVS = demuxer->priv;
164 
165     if (ds == demuxer->video)
166     {
167         sh_video_t *sh_video = demuxer->video->sh;
168         unsigned char *dst;
169         int w, h;
170         if (AVS->video_info->num_frames <= AVS->frameno) return 0; // EOF
171 
172         curr_frame = AVS->avs_get_frame(AVS->clip, AVS->frameno);
173         if (!curr_frame)
174         {
175             mp_msg(MSGT_DEMUX, MSGL_V, "AVS: error getting frame -- EOF??\n");
176             return 0;
177         }
178         w = curr_frame->row_size;
179         h = curr_frame->height;
180 
181         dp = new_demux_packet(w * h + 2 * (w / 2) * (h / 2));
182 
183         dp->pts=AVS->frameno / sh_video->fps;
184 
185         dst = dp->buffer;
186         memcpy_pic(dst, curr_frame->vfb->data + curr_frame->offset,
187                    w, h, w, curr_frame->pitch);
188         dst += w * h;
189         w /= 2; h /= 2;
190         memcpy_pic(dst, curr_frame->vfb->data + curr_frame->offsetV,
191                    w, h, w, curr_frame->pitchUV);
192         dst += w * h;
193         memcpy_pic(dst, curr_frame->vfb->data + curr_frame->offsetU,
194                    w, h, w, curr_frame->pitchUV);
195         ds_add_packet(demuxer->video, dp);
196 
197         AVS->frameno++;
198         AVS->avs_release_video_frame(curr_frame);
199     }
200 
201     /* Audio */
202     if (ds == demuxer->audio)
203     {
204         sh_audio_t *sh_audio = ds->sh;
205         int samples = sh_audio->samplerate;
206         uint64_t l;
207         samples = FFMIN(samples, AVS->video_info->num_audio_samples - AVS->sampleno);
208         if (!samples) return 0;
209         l = samples * sh_audio->channels * sh_audio->samplesize;
210         if (l > INT_MAX) {
211             mp_msg(MSGT_DEMUX, MSGL_FATAL, "AVS: audio packet too big\n");
212             return 0;
213         }
214         dp = new_demux_packet(l);
215         dp->pts = AVS->sampleno / sh_audio->samplerate;
216 
217         if (AVS->avs_get_audio(AVS->clip, dp->buffer, AVS->sampleno, samples))
218         {
219             mp_msg(MSGT_DEMUX, MSGL_V, "AVS: avs_get_audio() failed\n");
220             return 0;
221         }
222         ds_add_packet(demuxer->audio, dp);
223 
224         AVS->sampleno += samples;
225     }
226 
227     return 1;
228 }
229 
demux_open_avs(demuxer_t * demuxer)230 static demuxer_t* demux_open_avs(demuxer_t* demuxer)
231 {
232     int found = 0;
233     AVS_T *AVS = demuxer->priv;
234     int audio_samplesize = 0;
235     AVS->frameno = 0;
236     AVS->sampleno = 0;
237 
238     mp_msg(MSGT_DEMUX, MSGL_V, "AVS: demux_open_avs()\n");
239     demuxer->seekable = 1;
240 
241     AVS->clip = AVS->avs_take_clip(AVS->handler, AVS->avs_env);
242     if(!AVS->clip)
243     {
244         mp_msg(MSGT_DEMUX, MSGL_V, "AVS: avs_take_clip() failed\n");
245         return NULL;
246     }
247 
248     AVS->video_info = AVS->avs_get_video_info(AVS->clip);
249     if (!AVS->video_info)
250     {
251         mp_msg(MSGT_DEMUX, MSGL_V, "AVS: avs_get_video_info() call failed\n");
252         return NULL;
253     }
254 
255     if (!avs_is_yv12(AVS->video_info))
256     {
257         AVS->handler = AVS->avs_invoke(AVS->avs_env, "ConvertToYV12", avs_new_value_array(&AVS->handler, 1), 0);
258         if (avs_is_error(AVS->handler))
259         {
260             mp_msg(MSGT_DEMUX, MSGL_V, "AVS: Cannot convert input video to YV12: %s\n", avs_as_string(AVS->handler));
261             return NULL;
262         }
263 
264         AVS->clip = AVS->avs_take_clip(AVS->handler, AVS->avs_env);
265 
266         if(!AVS->clip)
267         {
268             mp_msg(MSGT_DEMUX, MSGL_V, "AVS: avs_take_clip() failed\n");
269             return NULL;
270         }
271 
272         AVS->video_info = AVS->avs_get_video_info(AVS->clip);
273         if (!AVS->video_info)
274         {
275             mp_msg(MSGT_DEMUX, MSGL_V, "AVS: avs_get_video_info() call failed\n");
276             return NULL;
277         }
278     }
279 
280     // TODO check field-based ??
281 
282     /* Video */
283     if (avs_has_video(AVS->video_info))
284     {
285         sh_video_t *sh_video = new_sh_video(demuxer, 0);
286         found = 1;
287 
288         if (demuxer->video->id == -1) demuxer->video->id = 0;
289         if (demuxer->video->id == 0)
290         demuxer->video->sh = sh_video;
291 
292         sh_video->disp_w = AVS->video_info->width;
293         sh_video->disp_h = AVS->video_info->height;
294 
295         //sh_video->format = get_mmioFOURCC(AVS->video_info);
296         sh_video->format = mmioFOURCC('Y', 'V', '1', '2');
297         sh_video->fps = (double) AVS->video_info->fps_numerator / (double) AVS->video_info->fps_denominator;
298         sh_video->frametime = 1.0 / sh_video->fps;
299 
300         sh_video->bih = malloc(sizeof(*sh_video->bih) + (256 * 4));
301         sh_video->bih->biCompression = sh_video->format;
302         sh_video->bih->biBitCount = avs_bits_per_pixel(AVS->video_info);
303         //sh_video->bih->biPlanes = 2;
304 
305         sh_video->bih->biWidth = AVS->video_info->width;
306         sh_video->bih->biHeight = AVS->video_info->height;
307         sh_video->num_frames = 0;
308         sh_video->num_frames_decoded = 0;
309     }
310 
311     /* Audio */
312     if (avs_has_audio(AVS->video_info))
313       switch (AVS->video_info->sample_type) {
314         case AVS_SAMPLE_INT8:  audio_samplesize = 1; break;
315         case AVS_SAMPLE_INT16: audio_samplesize = 2; break;
316         case AVS_SAMPLE_INT24: audio_samplesize = 3; break;
317         case AVS_SAMPLE_INT32:
318         case AVS_SAMPLE_FLOAT: audio_samplesize = 4; break;
319         default:
320           mp_msg(MSGT_DEMUX, MSGL_ERR, "AVS: unknown audio type, disabling\n");
321       }
322     if (audio_samplesize)
323     {
324         sh_audio_t *sh_audio = new_sh_audio(demuxer, 0, NULL);
325         found = 1;
326         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);
327 
328         if (demuxer->audio->id == -1) demuxer->audio->id = 0;
329         if (demuxer->audio->id == 0)
330         demuxer->audio->sh = sh_audio;
331 
332         sh_audio->wf = malloc(sizeof(*sh_audio->wf));
333         sh_audio->wf->wFormatTag = sh_audio->format =
334             (AVS->video_info->sample_type == AVS_SAMPLE_FLOAT) ? 0x3 : 0x1;
335         sh_audio->wf->nChannels = sh_audio->channels = AVS->video_info->nchannels;
336         sh_audio->wf->nSamplesPerSec = sh_audio->samplerate = AVS->video_info->audio_samples_per_second;
337         sh_audio->samplesize = audio_samplesize;
338         sh_audio->wf->nAvgBytesPerSec = sh_audio->channels * sh_audio->samplesize * sh_audio->samplerate;
339         sh_audio->wf->nBlockAlign = sh_audio->channels * sh_audio->samplesize;
340         sh_audio->wf->wBitsPerSample = sh_audio->samplesize * 8;
341         sh_audio->wf->cbSize = 0;
342         sh_audio->i_bps = sh_audio->wf->nAvgBytesPerSec;
343     }
344 
345     AVS->init = 1;
346     if (found)
347         return demuxer;
348     else
349         return NULL;
350 }
351 
demux_avs_control(demuxer_t * demuxer,int cmd,void * arg)352 static int demux_avs_control(demuxer_t *demuxer, int cmd, void *arg)
353 {
354     sh_video_t *sh_video=demuxer->video->sh;
355     sh_audio_t *sh_audio=demuxer->audio->sh;
356     AVS_T *AVS = demuxer->priv;
357 
358     switch(cmd)
359     {
360         case DEMUXER_CTRL_GET_TIME_LENGTH:
361         {
362             double res = sh_video ? (double)AVS->video_info->num_frames / sh_video->fps : 0;
363             if (sh_audio)
364               res = FFMAX(res, (double)AVS->video_info->num_audio_samples / sh_audio->samplerate);
365             *((double *)arg) = res;
366             return DEMUXER_CTRL_OK;
367         }
368         case DEMUXER_CTRL_GET_PERCENT_POS:
369         {
370             if (sh_video)
371             *((int *)arg) = AVS->frameno * 100 / AVS->video_info->num_frames;
372             else
373               *((int *)arg) = AVS->sampleno * 100 / AVS->video_info->num_audio_samples;
374             return DEMUXER_CTRL_OK;
375         }
376     default:
377         return DEMUXER_CTRL_NOTIMPL;
378     }
379 }
380 
demux_close_avs(demuxer_t * demuxer)381 static void demux_close_avs(demuxer_t* demuxer)
382 {
383     AVS_T *AVS = demuxer->priv;
384 
385     if (AVS)
386     {
387         if (AVS->dll)
388         {
389             if (AVS->clip)
390                 AVS->avs_release_clip(AVS->clip);
391             mp_msg(MSGT_DEMUX, MSGL_V, "AVS: Unloading avisynth.dll\n");
392             FreeLibrary(AVS->dll);
393         }
394 #ifdef WIN32_LOADER
395         Restore_LDT_Keeper(AVS->ldt_fs);
396 #endif
397         free(AVS);
398     }
399 }
400 
demux_seek_avs(demuxer_t * demuxer,float rel_seek_secs,float audio_delay,int flags)401 static void demux_seek_avs(demuxer_t *demuxer, float rel_seek_secs, float audio_delay, int flags)
402 {
403     sh_video_t *sh_video=demuxer->video->sh;
404     sh_audio_t *sh_audio=demuxer->audio->sh;
405     AVS_T *AVS = demuxer->priv;
406     double video_pos = sh_video ?
407                        (double)AVS->frameno / sh_video->fps :
408                        (double)AVS->sampleno / sh_audio->samplerate;
409     double duration = sh_video ?
410                       (double)AVS->video_info->num_frames / sh_video->fps :
411                       (double)AVS->video_info->num_audio_samples / sh_audio->samplerate;
412 
413     //mp_msg(MSGT_DEMUX, MSGL_V, "AVS: seek rel_seek_secs = %f - flags = %x\n", rel_seek_secs, flags);
414 
415     if (flags&SEEK_ABSOLUTE) video_pos=0;
416     if (flags&SEEK_FACTOR) rel_seek_secs *= duration;
417 
418     video_pos += rel_seek_secs;
419     if (video_pos < 0) video_pos = 0;
420 
421     if (sh_video) {
422       AVS->frameno = FFMIN(video_pos * sh_video->fps,
423                            AVS->video_info->num_frames);
424       sh_video->num_frames_decoded = AVS->frameno;
425       sh_video->num_frames = AVS->frameno;
426     }
427     video_pos += audio_delay;
428     if (video_pos < 0) video_pos = 0;
429     if (sh_audio)
430       AVS->sampleno = FFMIN(video_pos * sh_audio->samplerate,
431                             AVS->video_info->num_audio_samples);
432 }
433 
avs_check_file(demuxer_t * demuxer)434 static int avs_check_file(demuxer_t *demuxer)
435 {
436     mp_msg(MSGT_DEMUX, MSGL_V, "AVS: avs_check_file - attempting to open file %s\n", demuxer->filename);
437 
438     if (!demuxer->filename) return 0;
439 
440     /* Avoid crazy memory eating when passing an mpg stream */
441     if (demuxer->movi_end > MAX_AVS_SIZE)
442     {
443         mp_msg(MSGT_DEMUX,MSGL_V, "AVS: File is too big, aborting...\n");
444         return 0;
445     }
446 
447     demuxer->priv = initAVS(demuxer->filename);
448 
449     if (demuxer->priv)
450     {
451         mp_msg(MSGT_DEMUX,MSGL_V, "AVS: Init Ok\n");
452         return DEMUXER_TYPE_AVS;
453     }
454     mp_msg(MSGT_DEMUX,MSGL_V, "AVS: Init failed\n");
455     return 0;
456 }
457 
458 
459 const demuxer_desc_t demuxer_desc_avs = {
460   "Avisynth demuxer",
461   "avs",
462   "AVS",
463   "Gianluigi Tiesi",
464   "Requires binary dll",
465   DEMUXER_TYPE_AVS,
466   0, // unsafe autodetect
467   avs_check_file,
468   demux_avs_fill_buffer,
469   demux_open_avs,
470   demux_close_avs,
471   demux_seek_avs,
472   demux_avs_control
473 };
474