FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
avisynth.c
Go to the documentation of this file.
1 /*
2  * AviSynth/AvxSynth support
3  * Copyright (c) 2012 AvxSynth Team.
4  *
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.
10  *
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.
15  *
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
19  */
20 
21 #include "libavutil/internal.h"
22 #include "libavcodec/internal.h"
23 #include "avformat.h"
24 #include "internal.h"
25 
26 /* Enable function pointer definitions for runtime loading. */
27 #define AVSC_NO_DECLSPEC
28 
29 /* Platform-specific directives for AviSynth vs AvxSynth. */
30 #ifdef _WIN32
31  #include <windows.h>
32  #undef EXTERN_C
35  #define AVISYNTH_LIB "avisynth"
36  #define USING_AVISYNTH
37 #else
38  #include <dlfcn.h>
40  #if defined (__APPLE__)
41  #define AVISYNTH_LIB "libavxsynth.dylib"
42  #else
43  #define AVISYNTH_LIB "libavxsynth.so"
44  #endif
45 
46  #define LoadLibrary(x) dlopen(x, RTLD_NOW | RTLD_GLOBAL)
47  #define GetProcAddress dlsym
48  #define FreeLibrary dlclose
49 #endif
50 
51 typedef struct AviSynthLibrary {
52  void *library;
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
70 
71 typedef struct AviSynthContext {
74  const AVS_VideoInfo *vi;
75 
76  /* avisynth_read_packet_video() iterates over this. */
77  int n_planes;
78  const int *planes;
79 
82  int64_t curr_sample;
83 
84  int error;
85 
86  /* Linked list pointers. */
89 
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,
93  AVS_PLANAR_V };
94 
95 /* A conflict between C++ global objects, atexit, and dynamic loading requires
96  * us to register our own atexit handler to prevent double freeing. */
98 static int avs_atexit_called = 0;
99 
100 /* Linked list of AviSynthContexts. An atexit handler destroys this list. */
102 
103 static av_cold void avisynth_atexit_handler(void);
104 
106 {
107  avs_library.library = LoadLibrary(AVISYNTH_LIB);
108  if (!avs_library.library)
109  return AVERROR_UNKNOWN;
110 
111 #define LOAD_AVS_FUNC(name, continue_on_fail) \
112  avs_library.name = \
113  (void *)GetProcAddress(avs_library.library, #name); \
114  if (!continue_on_fail && !avs_library.name) \
115  goto fail;
116 
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);
131 #undef LOAD_AVS_FUNC
132 
133  atexit(avisynth_atexit_handler);
134  return 0;
135 
136 fail:
137  FreeLibrary(avs_library.library);
138  return AVERROR_UNKNOWN;
139 }
140 
141 /* Note that avisynth_context_create and avisynth_context_destroy
142  * do not allocate or free the actual context! That is taken care of
143  * by libavformat. */
145 {
146  AviSynthContext *avs = s->priv_data;
147  int ret;
148 
149  if (!avs_library.library)
150  if (ret = avisynth_load_library())
151  return ret;
152 
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);
156  if (error) {
157  av_log(s, AV_LOG_ERROR, "%s\n", error);
158  return AVERROR_UNKNOWN;
159  }
160  }
161 
162  if (!avs_ctx_list) {
163  avs_ctx_list = avs;
164  } else {
165  avs->next = avs_ctx_list;
166  avs_ctx_list = avs;
167  }
168 
169  return 0;
170 }
171 
173 {
174  if (avs_atexit_called)
175  return;
176 
177  if (avs == avs_ctx_list) {
178  avs_ctx_list = avs->next;
179  } else {
181  while (prev->next != avs)
182  prev = prev->next;
183  prev->next = avs->next;
184  }
185 
186  if (avs->clip) {
187  avs_library.avs_release_clip(avs->clip);
188  avs->clip = NULL;
189  }
190  if (avs->env) {
191  avs_library.avs_delete_script_environment(avs->env);
192  avs->env = NULL;
193  }
194 }
195 
197 {
199 
200  while (avs) {
201  AviSynthContext *next = avs->next;
203  avs = next;
204  }
205  FreeLibrary(avs_library.library);
206 
207  avs_atexit_called = 1;
208 }
209 
210 /* Create AVStream from audio and video data. */
212 {
213  AviSynthContext *avs = s->priv_data;
214  int planar = 0; // 0: packed, 1: YUV, 2: Y8
215 
218  st->codec->width = avs->vi->width;
219  st->codec->height = avs->vi->height;
220 
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 };
225  st->start_time = 0;
226  st->duration = avs->vi->num_frames;
227  st->nb_frames = avs->vi->num_frames;
228 
229  switch (avs->vi->pixel_type) {
230 #ifdef USING_AVISYNTH
231  case AVS_CS_YV24:
233  planar = 1;
234  break;
235  case AVS_CS_YV16:
237  planar = 1;
238  break;
239  case AVS_CS_YV411:
241  planar = 1;
242  break;
243  case AVS_CS_Y8:
245  planar = 2;
246  break;
247 #endif
248  case AVS_CS_BGR24:
250  break;
251  case AVS_CS_BGR32:
253  break;
254  case AVS_CS_YUY2:
256  break;
257  case AVS_CS_YV12:
259  planar = 1;
260  break;
261  case AVS_CS_I420: // Is this even used anywhere?
263  planar = 1;
264  break;
265  default:
266  av_log(s, AV_LOG_ERROR,
267  "unknown AviSynth colorspace %d\n", avs->vi->pixel_type);
268  avs->error = 1;
269  return AVERROR_UNKNOWN;
270  }
271 
272  switch (planar) {
273  case 2: // Y8
274  avs->n_planes = 1;
275  avs->planes = avs_planes_grey;
276  break;
277  case 1: // YUV
278  avs->n_planes = 3;
279  avs->planes = avs_planes_yuv;
280  break;
281  default:
282  avs->n_planes = 1;
283  avs->planes = avs_planes_packed;
284  }
285  return 0;
286 }
287 
289 {
290  AviSynthContext *avs = s->priv_data;
291 
294  st->codec->channels = avs->vi->nchannels;
295  st->time_base = (AVRational) { 1,
297 
298  switch (avs->vi->sample_type) {
299  case AVS_SAMPLE_INT8:
301  break;
302  case AVS_SAMPLE_INT16:
304  break;
305  case AVS_SAMPLE_INT24:
307  break;
308  case AVS_SAMPLE_INT32:
310  break;
311  case AVS_SAMPLE_FLOAT:
313  break;
314  default:
315  av_log(s, AV_LOG_ERROR,
316  "unknown AviSynth sample type %d\n", avs->vi->sample_type);
317  avs->error = 1;
318  return AVERROR_UNKNOWN;
319  }
320  return 0;
321 }
322 
324 {
325  AviSynthContext *avs = s->priv_data;
326  AVStream *st;
327  int ret;
328  int id = 0;
329 
330  if (avs_has_video(avs->vi)) {
331  st = avformat_new_stream(s, NULL);
332  if (!st)
333  return AVERROR_UNKNOWN;
334  st->id = id++;
335  if (ret = avisynth_create_stream_video(s, st))
336  return ret;
337  }
338  if (avs_has_audio(avs->vi)) {
339  st = avformat_new_stream(s, NULL);
340  if (!st)
341  return AVERROR_UNKNOWN;
342  st->id = id++;
343  if (ret = avisynth_create_stream_audio(s, st))
344  return ret;
345  }
346  return 0;
347 }
348 
350 {
351  AviSynthContext *avs = s->priv_data;
352  AVS_Value arg, val;
353  int ret;
354 #ifdef USING_AVISYNTH
355  char filename_ansi[MAX_PATH * 4];
356  wchar_t filename_wc[MAX_PATH * 4];
357 #endif
358 
359  if (ret = avisynth_context_create(s))
360  return ret;
361 
362 #ifdef USING_AVISYNTH
363  /* Convert UTF-8 to ANSI code page */
364  MultiByteToWideChar(CP_UTF8, 0, s->filename, -1, filename_wc, MAX_PATH * 4);
365  WideCharToMultiByte(CP_THREAD_ACP, 0, filename_wc, -1, filename_ansi,
366  MAX_PATH * 4, NULL, NULL);
367  arg = avs_new_value_string(filename_ansi);
368 #else
369  arg = avs_new_value_string(s->filename);
370 #endif
371  val = avs_library.avs_invoke(avs->env, "Import", arg, 0);
372  if (avs_is_error(val)) {
373  av_log(s, AV_LOG_ERROR, "%s\n", avs_as_error(val));
374  ret = AVERROR_UNKNOWN;
375  goto fail;
376  }
377  if (!avs_is_clip(val)) {
378  av_log(s, AV_LOG_ERROR, "AviSynth script did not return a clip\n");
379  ret = AVERROR_UNKNOWN;
380  goto fail;
381  }
382 
383  avs->clip = avs_library.avs_take_clip(val, avs->env);
384  avs->vi = avs_library.avs_get_video_info(avs->clip);
385 
386  /* Release the AVS_Value as it will go out of scope. */
387  avs_library.avs_release_value(val);
388 
389  if (ret = avisynth_create_stream(s))
390  goto fail;
391 
392  return 0;
393 
394 fail:
396  return ret;
397 }
398 
400  AVPacket *pkt, int *discard)
401 {
402  AviSynthContext *avs = s->priv_data;
403 
404  avs->curr_stream++;
405  avs->curr_stream %= s->nb_streams;
406 
407  *st = s->streams[avs->curr_stream];
408  if ((*st)->discard == AVDISCARD_ALL)
409  *discard = 1;
410  else
411  *discard = 0;
412 
413  return;
414 }
415 
416 /* Copy AviSynth clip data into an AVPacket. */
418  int discard)
419 {
420  AviSynthContext *avs = s->priv_data;
422  unsigned char *dst_p;
423  const unsigned char *src_p;
424  int n, i, plane, rowsize, planeheight, pitch, bits;
425  const char *error;
426 
427  if (avs->curr_frame >= avs->vi->num_frames)
428  return AVERROR_EOF;
429 
430  /* This must happen even if the stream is discarded to prevent desync. */
431  n = avs->curr_frame++;
432  if (discard)
433  return 0;
434 
435 #ifdef USING_AVISYNTH
436  /* Define the bpp values for the new AviSynth 2.6 colorspaces.
437  * Since AvxSynth doesn't have these functions, special-case
438  * it in order to avoid implicit declaration errors. */
439 
440  if (avs_is_yv24(avs->vi))
441  bits = 24;
442  else if (avs_is_yv16(avs->vi))
443  bits = 16;
444  else if (avs_is_yv411(avs->vi))
445  bits = 12;
446  else if (avs_is_y8(avs->vi))
447  bits = 8;
448  else
449 #endif
450  bits = avs_bits_per_pixel(avs->vi);
451 
452  /* Without the cast to int64_t, calculation overflows at about 9k x 9k
453  * resolution. */
454  pkt->size = (((int64_t)avs->vi->width *
455  (int64_t)avs->vi->height) * bits) / 8;
456  if (!pkt->size)
457  return AVERROR_UNKNOWN;
458 
459  if (av_new_packet(pkt, pkt->size) < 0)
460  return AVERROR(ENOMEM);
461 
462  pkt->pts = n;
463  pkt->dts = n;
464  pkt->duration = 1;
465  pkt->stream_index = avs->curr_stream;
466 
467  frame = avs_library.avs_get_frame(avs->clip, n);
468  error = avs_library.avs_clip_get_error(avs->clip);
469  if (error) {
470  av_log(s, AV_LOG_ERROR, "%s\n", error);
471  avs->error = 1;
472  av_packet_unref(pkt);
473  return AVERROR_UNKNOWN;
474  }
475 
476  dst_p = pkt->data;
477  for (i = 0; i < avs->n_planes; i++) {
478  plane = avs->planes[i];
479  src_p = avs_get_read_ptr_p(frame, plane);
480  pitch = avs_get_pitch_p(frame, plane);
481 
482 #ifdef USING_AVISYNTH
483  if (avs_library.avs_get_version(avs->clip) == 3) {
484  rowsize = avs_get_row_size_p_25(frame, plane);
485  planeheight = avs_get_height_p_25(frame, plane);
486  } else {
487  rowsize = avs_get_row_size_p(frame, plane);
488  planeheight = avs_get_height_p(frame, plane);
489  }
490 #else
491  rowsize = avs_get_row_size_p(frame, plane);
492  planeheight = avs_get_height_p(frame, plane);
493 #endif
494 
495  /* Flip RGB video. */
496  if (avs_is_rgb24(avs->vi) || avs_is_rgb(avs->vi)) {
497  src_p = src_p + (planeheight - 1) * pitch;
498  pitch = -pitch;
499  }
500 
501  avs_library.avs_bit_blt(avs->env, dst_p, rowsize, src_p, pitch,
502  rowsize, planeheight);
503  dst_p += rowsize * planeheight;
504  }
505 
506  avs_library.avs_release_video_frame(frame);
507  return 0;
508 }
509 
511  int discard)
512 {
513  AviSynthContext *avs = s->priv_data;
514  AVRational fps, samplerate;
515  int samples;
516  int64_t n;
517  const char *error;
518 
519  if (avs->curr_sample >= avs->vi->num_audio_samples)
520  return AVERROR_EOF;
521 
522  fps.num = avs->vi->fps_numerator;
523  fps.den = avs->vi->fps_denominator;
524  samplerate.num = avs->vi->audio_samples_per_second;
525  samplerate.den = 1;
526 
527  if (avs_has_video(avs->vi)) {
528  if (avs->curr_frame < avs->vi->num_frames)
529  samples = av_rescale_q(avs->curr_frame, samplerate, fps) -
530  avs->curr_sample;
531  else
532  samples = av_rescale_q(1, samplerate, fps);
533  } else {
534  samples = 1000;
535  }
536 
537  /* After seeking, audio may catch up with video. */
538  if (samples <= 0) {
539  pkt->size = 0;
540  pkt->data = NULL;
541  return 0;
542  }
543 
544  if (avs->curr_sample + samples > avs->vi->num_audio_samples)
545  samples = avs->vi->num_audio_samples - avs->curr_sample;
546 
547  /* This must happen even if the stream is discarded to prevent desync. */
548  n = avs->curr_sample;
549  avs->curr_sample += samples;
550  if (discard)
551  return 0;
552 
553  pkt->size = avs_bytes_per_channel_sample(avs->vi) *
554  samples * avs->vi->nchannels;
555  if (!pkt->size)
556  return AVERROR_UNKNOWN;
557 
558  if (av_new_packet(pkt, pkt->size) < 0)
559  return AVERROR(ENOMEM);
560 
561  pkt->pts = n;
562  pkt->dts = n;
563  pkt->duration = samples;
564  pkt->stream_index = avs->curr_stream;
565 
566  avs_library.avs_get_audio(avs->clip, pkt->data, n, samples);
567  error = avs_library.avs_clip_get_error(avs->clip);
568  if (error) {
569  av_log(s, AV_LOG_ERROR, "%s\n", error);
570  avs->error = 1;
571  av_packet_unref(pkt);
572  return AVERROR_UNKNOWN;
573  }
574  return 0;
575 }
576 
578 {
579  int ret;
580 
581  // Calling library must implement a lock for thread-safe opens.
582  if (ret = avpriv_lock_avformat())
583  return ret;
584 
585  if (ret = avisynth_open_file(s)) {
587  return ret;
588  }
589 
591  return 0;
592 }
593 
595 {
596  AviSynthContext *avs = s->priv_data;
597  AVStream *st;
598  int discard = 0;
599  int ret;
600 
601  if (avs->error)
602  return AVERROR_UNKNOWN;
603 
604  /* If either stream reaches EOF, try to read the other one before
605  * giving up. */
606  avisynth_next_stream(s, &st, pkt, &discard);
607  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
608  ret = avisynth_read_packet_video(s, pkt, discard);
609  if (ret == AVERROR_EOF && avs_has_audio(avs->vi)) {
610  avisynth_next_stream(s, &st, pkt, &discard);
611  return avisynth_read_packet_audio(s, pkt, discard);
612  }
613  } else {
614  ret = avisynth_read_packet_audio(s, pkt, discard);
615  if (ret == AVERROR_EOF && avs_has_video(avs->vi)) {
616  avisynth_next_stream(s, &st, pkt, &discard);
617  return avisynth_read_packet_video(s, pkt, discard);
618  }
619  }
620 
621  return ret;
622 }
623 
625 {
626  if (avpriv_lock_avformat())
627  return AVERROR_UNKNOWN;
628 
631  return 0;
632 }
633 
634 static int avisynth_read_seek(AVFormatContext *s, int stream_index,
635  int64_t timestamp, int flags)
636 {
637  AviSynthContext *avs = s->priv_data;
638  AVStream *st;
639  AVRational fps, samplerate;
640 
641  if (avs->error)
642  return AVERROR_UNKNOWN;
643 
644  fps = (AVRational) { avs->vi->fps_numerator,
645  avs->vi->fps_denominator };
646  samplerate = (AVRational) { avs->vi->audio_samples_per_second, 1 };
647 
648  st = s->streams[stream_index];
649  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
650  /* AviSynth frame counts are signed int. */
651  if ((timestamp >= avs->vi->num_frames) ||
652  (timestamp > INT_MAX) ||
653  (timestamp < 0))
654  return AVERROR_EOF;
655  avs->curr_frame = timestamp;
656  if (avs_has_audio(avs->vi))
657  avs->curr_sample = av_rescale_q(timestamp, samplerate, fps);
658  } else {
659  if ((timestamp >= avs->vi->num_audio_samples) || (timestamp < 0))
660  return AVERROR_EOF;
661  /* Force frame granularity for seeking. */
662  if (avs_has_video(avs->vi)) {
663  avs->curr_frame = av_rescale_q(timestamp, fps, samplerate);
664  avs->curr_sample = av_rescale_q(avs->curr_frame, samplerate, fps);
665  } else {
666  avs->curr_sample = timestamp;
667  }
668  }
669 
670  return 0;
671 }
672 
674  .name = "avisynth",
675  .long_name = NULL_IF_CONFIG_SMALL("AviSynth script"),
676  .priv_data_size = sizeof(AviSynthContext),
681  .extensions = "avs",
682 };