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 #include "config.h"
26 
27 /* Enable function pointer definitions for runtime loading. */
28 #define AVSC_NO_DECLSPEC
29 
30 /* Platform-specific directives for AviSynth vs AvxSynth. */
31 #ifdef _WIN32
32  #include <windows.h>
33  #undef EXTERN_C
35  #define AVISYNTH_LIB "avisynth"
36  #define USING_AVISYNTH
37 #else
38  #include <dlfcn.h>
40  #define AVISYNTH_NAME "libavxsynth"
41  #define AVISYNTH_LIB AVISYNTH_NAME SLIBSUF
42 
43  #define LoadLibrary(x) dlopen(x, RTLD_NOW | RTLD_LOCAL)
44  #define GetProcAddress dlsym
45  #define FreeLibrary dlclose
46 #endif
47 
48 typedef struct AviSynthLibrary {
49  void *library;
50 #define AVSC_DECLARE_FUNC(name) name ## _func name
51  AVSC_DECLARE_FUNC(avs_bit_blt);
52  AVSC_DECLARE_FUNC(avs_clip_get_error);
53  AVSC_DECLARE_FUNC(avs_create_script_environment);
54  AVSC_DECLARE_FUNC(avs_delete_script_environment);
55  AVSC_DECLARE_FUNC(avs_get_audio);
56  AVSC_DECLARE_FUNC(avs_get_error);
57  AVSC_DECLARE_FUNC(avs_get_frame);
58  AVSC_DECLARE_FUNC(avs_get_version);
59  AVSC_DECLARE_FUNC(avs_get_video_info);
60  AVSC_DECLARE_FUNC(avs_invoke);
61  AVSC_DECLARE_FUNC(avs_release_clip);
62  AVSC_DECLARE_FUNC(avs_release_value);
63  AVSC_DECLARE_FUNC(avs_release_video_frame);
64  AVSC_DECLARE_FUNC(avs_take_clip);
65 #ifdef USING_AVISYNTH
71  AVSC_DECLARE_FUNC(avs_is_yv24);
72  AVSC_DECLARE_FUNC(avs_is_yv16);
73  AVSC_DECLARE_FUNC(avs_is_yv411);
74  AVSC_DECLARE_FUNC(avs_is_y8);
75 #endif
76 #undef AVSC_DECLARE_FUNC
78 
79 typedef struct AviSynthContext {
82  const AVS_VideoInfo *vi;
83 
84  /* avisynth_read_packet_video() iterates over this. */
85  int n_planes;
86  const int *planes;
87 
90  int64_t curr_sample;
91 
92  int error;
93 
94  /* Linked list pointers. */
97 
98 static const int avs_planes_packed[1] = { 0 };
99 static const int avs_planes_grey[1] = { AVS_PLANAR_Y };
100 static const int avs_planes_yuv[3] = { AVS_PLANAR_Y, AVS_PLANAR_U,
101  AVS_PLANAR_V };
102 
103 /* A conflict between C++ global objects, atexit, and dynamic loading requires
104  * us to register our own atexit handler to prevent double freeing. */
106 static int avs_atexit_called = 0;
107 
108 /* Linked list of AviSynthContexts. An atexit handler destroys this list. */
110 
111 static av_cold void avisynth_atexit_handler(void);
112 
114 {
115  avs_library.library = LoadLibrary(AVISYNTH_LIB);
116  if (!avs_library.library)
117  return AVERROR_UNKNOWN;
118 
119 #define LOAD_AVS_FUNC(name, continue_on_fail) \
120  avs_library.name = \
121  (void *)GetProcAddress(avs_library.library, #name); \
122  if (!continue_on_fail && !avs_library.name) \
123  goto fail;
124 
125  LOAD_AVS_FUNC(avs_bit_blt, 0);
126  LOAD_AVS_FUNC(avs_clip_get_error, 0);
127  LOAD_AVS_FUNC(avs_create_script_environment, 0);
128  LOAD_AVS_FUNC(avs_delete_script_environment, 0);
129  LOAD_AVS_FUNC(avs_get_audio, 0);
130  LOAD_AVS_FUNC(avs_get_error, 1); // New to AviSynth 2.6
131  LOAD_AVS_FUNC(avs_get_frame, 0);
132  LOAD_AVS_FUNC(avs_get_version, 0);
133  LOAD_AVS_FUNC(avs_get_video_info, 0);
134  LOAD_AVS_FUNC(avs_invoke, 0);
135  LOAD_AVS_FUNC(avs_release_clip, 0);
136  LOAD_AVS_FUNC(avs_release_value, 0);
137  LOAD_AVS_FUNC(avs_release_video_frame, 0);
138  LOAD_AVS_FUNC(avs_take_clip, 0);
139 #ifdef USING_AVISYNTH
145  LOAD_AVS_FUNC(avs_is_yv24, 1);
146  LOAD_AVS_FUNC(avs_is_yv16, 1);
147  LOAD_AVS_FUNC(avs_is_yv411, 1);
148  LOAD_AVS_FUNC(avs_is_y8, 1);
149 #endif
150 #undef LOAD_AVS_FUNC
151 
152  atexit(avisynth_atexit_handler);
153  return 0;
154 
155 fail:
156  FreeLibrary(avs_library.library);
157  return AVERROR_UNKNOWN;
158 }
159 
160 /* Note that avisynth_context_create and avisynth_context_destroy
161  * do not allocate or free the actual context! That is taken care of
162  * by libavformat. */
164 {
165  AviSynthContext *avs = s->priv_data;
166  int ret;
167 
168  if (!avs_library.library)
169  if (ret = avisynth_load_library())
170  return ret;
171 
172  avs->env = avs_library.avs_create_script_environment(3);
173  if (avs_library.avs_get_error) {
174  const char *error = avs_library.avs_get_error(avs->env);
175  if (error) {
176  av_log(s, AV_LOG_ERROR, "%s\n", error);
177  return AVERROR_UNKNOWN;
178  }
179  }
180 
181  if (!avs_ctx_list) {
182  avs_ctx_list = avs;
183  } else {
184  avs->next = avs_ctx_list;
185  avs_ctx_list = avs;
186  }
187 
188  return 0;
189 }
190 
192 {
193  if (avs_atexit_called)
194  return;
195 
196  if (avs == avs_ctx_list) {
197  avs_ctx_list = avs->next;
198  } else {
200  while (prev->next != avs)
201  prev = prev->next;
202  prev->next = avs->next;
203  }
204 
205  if (avs->clip) {
206  avs_library.avs_release_clip(avs->clip);
207  avs->clip = NULL;
208  }
209  if (avs->env) {
210  avs_library.avs_delete_script_environment(avs->env);
211  avs->env = NULL;
212  }
213 }
214 
216 {
218 
219  while (avs) {
220  AviSynthContext *next = avs->next;
222  avs = next;
223  }
224  FreeLibrary(avs_library.library);
225 
226  avs_atexit_called = 1;
227 }
228 
229 /* Create AVStream from audio and video data. */
231 {
232  AviSynthContext *avs = s->priv_data;
233  int planar = 0; // 0: packed, 1: YUV, 2: Y8
234 
237  st->codec->width = avs->vi->width;
238  st->codec->height = avs->vi->height;
239 
240  st->time_base = (AVRational) { avs->vi->fps_denominator,
241  avs->vi->fps_numerator };
242  st->avg_frame_rate = (AVRational) { avs->vi->fps_numerator,
243  avs->vi->fps_denominator };
244  st->start_time = 0;
245  st->duration = avs->vi->num_frames;
246  st->nb_frames = avs->vi->num_frames;
247 
248  switch (avs->vi->pixel_type) {
249 #ifdef USING_AVISYNTH
250  case AVS_CS_YV24:
252  planar = 1;
253  break;
254  case AVS_CS_YV16:
256  planar = 1;
257  break;
258  case AVS_CS_YV411:
260  planar = 1;
261  break;
262  case AVS_CS_Y8:
264  planar = 2;
265  break;
266 #endif
267  case AVS_CS_BGR24:
269  break;
270  case AVS_CS_BGR32:
272  break;
273  case AVS_CS_YUY2:
275  break;
276  case AVS_CS_YV12:
278  planar = 1;
279  break;
280  case AVS_CS_I420: // Is this even used anywhere?
282  planar = 1;
283  break;
284  default:
285  av_log(s, AV_LOG_ERROR,
286  "unknown AviSynth colorspace %d\n", avs->vi->pixel_type);
287  avs->error = 1;
288  return AVERROR_UNKNOWN;
289  }
290 
291  switch (planar) {
292  case 2: // Y8
293  avs->n_planes = 1;
294  avs->planes = avs_planes_grey;
295  break;
296  case 1: // YUV
297  avs->n_planes = 3;
298  avs->planes = avs_planes_yuv;
299  break;
300  default:
301  avs->n_planes = 1;
302  avs->planes = avs_planes_packed;
303  }
304  return 0;
305 }
306 
308 {
309  AviSynthContext *avs = s->priv_data;
310 
313  st->codec->channels = avs->vi->nchannels;
314  st->time_base = (AVRational) { 1,
316  st->duration = avs->vi->num_audio_samples;
317 
318  switch (avs->vi->sample_type) {
319  case AVS_SAMPLE_INT8:
321  break;
322  case AVS_SAMPLE_INT16:
324  break;
325  case AVS_SAMPLE_INT24:
327  break;
328  case AVS_SAMPLE_INT32:
330  break;
331  case AVS_SAMPLE_FLOAT:
333  break;
334  default:
335  av_log(s, AV_LOG_ERROR,
336  "unknown AviSynth sample type %d\n", avs->vi->sample_type);
337  avs->error = 1;
338  return AVERROR_UNKNOWN;
339  }
340  return 0;
341 }
342 
344 {
345  AviSynthContext *avs = s->priv_data;
346  AVStream *st;
347  int ret;
348  int id = 0;
349 
350  if (avs_has_video(avs->vi)) {
351  st = avformat_new_stream(s, NULL);
352  if (!st)
353  return AVERROR_UNKNOWN;
354  st->id = id++;
355  if (ret = avisynth_create_stream_video(s, st))
356  return ret;
357  }
358  if (avs_has_audio(avs->vi)) {
359  st = avformat_new_stream(s, NULL);
360  if (!st)
361  return AVERROR_UNKNOWN;
362  st->id = id++;
363  if (ret = avisynth_create_stream_audio(s, st))
364  return ret;
365  }
366  return 0;
367 }
368 
370 {
371  AviSynthContext *avs = s->priv_data;
372  AVS_Value arg, val;
373  int ret;
374 #ifdef USING_AVISYNTH
375  char filename_ansi[MAX_PATH * 4];
376  wchar_t filename_wc[MAX_PATH * 4];
377 #endif
378 
379  if (ret = avisynth_context_create(s))
380  return ret;
381 
382 #ifdef USING_AVISYNTH
383  /* Convert UTF-8 to ANSI code page */
384  MultiByteToWideChar(CP_UTF8, 0, s->filename, -1, filename_wc, MAX_PATH * 4);
385  WideCharToMultiByte(CP_THREAD_ACP, 0, filename_wc, -1, filename_ansi,
386  MAX_PATH * 4, NULL, NULL);
387  arg = avs_new_value_string(filename_ansi);
388 #else
389  arg = avs_new_value_string(s->filename);
390 #endif
391  val = avs_library.avs_invoke(avs->env, "Import", arg, 0);
392  if (avs_is_error(val)) {
393  av_log(s, AV_LOG_ERROR, "%s\n", avs_as_error(val));
394  ret = AVERROR_UNKNOWN;
395  goto fail;
396  }
397  if (!avs_is_clip(val)) {
398  av_log(s, AV_LOG_ERROR, "AviSynth script did not return a clip\n");
399  ret = AVERROR_UNKNOWN;
400  goto fail;
401  }
402 
403  avs->clip = avs_library.avs_take_clip(val, avs->env);
404  avs->vi = avs_library.avs_get_video_info(avs->clip);
405 
406 #ifdef USING_AVISYNTH
407  /* On Windows, FFmpeg supports AviSynth interface version 6 or higher.
408  * This includes AviSynth 2.6 RC1 or higher, and AviSynth+ r1718 or higher,
409  * and excludes 2.5 and the 2.6 alphas. Since AvxSynth identifies itself
410  * as interface version 3 like 2.5.8, this needs to be special-cased. */
411 
412  if (avs_library.avs_get_version(avs->clip) < 6) {
413  av_log(s, AV_LOG_ERROR,
414  "AviSynth version is too old. Please upgrade to either AviSynth 2.6 >= RC1 or AviSynth+ >= r1718.\n");
415  ret = AVERROR_UNKNOWN;
416  goto fail;
417  }
418 #endif
419 
420  /* Release the AVS_Value as it will go out of scope. */
421  avs_library.avs_release_value(val);
422 
423  if (ret = avisynth_create_stream(s))
424  goto fail;
425 
426  return 0;
427 
428 fail:
430  return ret;
431 }
432 
434  AVPacket *pkt, int *discard)
435 {
436  AviSynthContext *avs = s->priv_data;
437 
438  avs->curr_stream++;
439  avs->curr_stream %= s->nb_streams;
440 
441  *st = s->streams[avs->curr_stream];
442  if ((*st)->discard == AVDISCARD_ALL)
443  *discard = 1;
444  else
445  *discard = 0;
446 
447  return;
448 }
449 
450 /* Copy AviSynth clip data into an AVPacket. */
452  int discard)
453 {
454  AviSynthContext *avs = s->priv_data;
456  unsigned char *dst_p;
457  const unsigned char *src_p;
458  int n, i, plane, rowsize, planeheight, pitch, bits;
459  const char *error;
460 
461  if (avs->curr_frame >= avs->vi->num_frames)
462  return AVERROR_EOF;
463 
464  /* This must happen even if the stream is discarded to prevent desync. */
465  n = avs->curr_frame++;
466  if (discard)
467  return 0;
468 
469 #ifdef USING_AVISYNTH
470  /* Define the bpp values for the new AviSynth 2.6 colorspaces.
471  * Since AvxSynth doesn't have these functions, special-case
472  * it in order to avoid implicit declaration errors. */
473 
474  if (avs_library.avs_is_yv24(avs->vi))
475  bits = 24;
476  else if (avs_library.avs_is_yv16(avs->vi))
477  bits = 16;
478  else if (avs_library.avs_is_yv411(avs->vi))
479  bits = 12;
480  else if (avs_library.avs_is_y8(avs->vi))
481  bits = 8;
482  else
483  bits = avs_library.avs_bits_per_pixel(avs->vi);
484 #else
485  bits = avs_bits_per_pixel(avs->vi);
486 #endif
487 
488  /* Without the cast to int64_t, calculation overflows at about 9k x 9k
489  * resolution. */
490  pkt->size = (((int64_t)avs->vi->width *
491  (int64_t)avs->vi->height) * bits) / 8;
492  if (!pkt->size)
493  return AVERROR_UNKNOWN;
494 
495  if (av_new_packet(pkt, pkt->size) < 0)
496  return AVERROR(ENOMEM);
497 
498  pkt->pts = n;
499  pkt->dts = n;
500  pkt->duration = 1;
501  pkt->stream_index = avs->curr_stream;
502 
503  frame = avs_library.avs_get_frame(avs->clip, n);
504  error = avs_library.avs_clip_get_error(avs->clip);
505  if (error) {
506  av_log(s, AV_LOG_ERROR, "%s\n", error);
507  avs->error = 1;
508  av_packet_unref(pkt);
509  return AVERROR_UNKNOWN;
510  }
511 
512  dst_p = pkt->data;
513  for (i = 0; i < avs->n_planes; i++) {
514  plane = avs->planes[i];
515 #ifdef USING_AVISYNTH
516  src_p = avs_library.avs_get_read_ptr_p(frame, plane);
517  pitch = avs_library.avs_get_pitch_p(frame, plane);
518 
519  rowsize = avs_library.avs_get_row_size_p(frame, plane);
520  planeheight = avs_library.avs_get_height_p(frame, plane);
521 #else
522  src_p = avs_get_read_ptr_p(frame, plane);
523  pitch = avs_get_pitch_p(frame, plane);
524 
525  rowsize = avs_get_row_size_p(frame, plane);
526  planeheight = avs_get_height_p(frame, plane);
527 #endif
528 
529  /* Flip RGB video. */
530  if (avs_is_rgb24(avs->vi) || avs_is_rgb(avs->vi)) {
531  src_p = src_p + (planeheight - 1) * pitch;
532  pitch = -pitch;
533  }
534 
535  avs_library.avs_bit_blt(avs->env, dst_p, rowsize, src_p, pitch,
536  rowsize, planeheight);
537  dst_p += rowsize * planeheight;
538  }
539 
540  avs_library.avs_release_video_frame(frame);
541  return 0;
542 }
543 
545  int discard)
546 {
547  AviSynthContext *avs = s->priv_data;
548  AVRational fps, samplerate;
549  int samples;
550  int64_t n;
551  const char *error;
552 
553  if (avs->curr_sample >= avs->vi->num_audio_samples)
554  return AVERROR_EOF;
555 
556  fps.num = avs->vi->fps_numerator;
557  fps.den = avs->vi->fps_denominator;
558  samplerate.num = avs->vi->audio_samples_per_second;
559  samplerate.den = 1;
560 
561  if (avs_has_video(avs->vi)) {
562  if (avs->curr_frame < avs->vi->num_frames)
563  samples = av_rescale_q(avs->curr_frame, samplerate, fps) -
564  avs->curr_sample;
565  else
566  samples = av_rescale_q(1, samplerate, fps);
567  } else {
568  samples = 1000;
569  }
570 
571  /* After seeking, audio may catch up with video. */
572  if (samples <= 0) {
573  pkt->size = 0;
574  pkt->data = NULL;
575  return 0;
576  }
577 
578  if (avs->curr_sample + samples > avs->vi->num_audio_samples)
579  samples = avs->vi->num_audio_samples - avs->curr_sample;
580 
581  /* This must happen even if the stream is discarded to prevent desync. */
582  n = avs->curr_sample;
583  avs->curr_sample += samples;
584  if (discard)
585  return 0;
586 
587  pkt->size = avs_bytes_per_channel_sample(avs->vi) *
588  samples * avs->vi->nchannels;
589  if (!pkt->size)
590  return AVERROR_UNKNOWN;
591 
592  if (av_new_packet(pkt, pkt->size) < 0)
593  return AVERROR(ENOMEM);
594 
595  pkt->pts = n;
596  pkt->dts = n;
597  pkt->duration = samples;
598  pkt->stream_index = avs->curr_stream;
599 
600  avs_library.avs_get_audio(avs->clip, pkt->data, n, samples);
601  error = avs_library.avs_clip_get_error(avs->clip);
602  if (error) {
603  av_log(s, AV_LOG_ERROR, "%s\n", error);
604  avs->error = 1;
605  av_packet_unref(pkt);
606  return AVERROR_UNKNOWN;
607  }
608  return 0;
609 }
610 
612 {
613  int ret;
614 
615  // Calling library must implement a lock for thread-safe opens.
616  if (ret = avpriv_lock_avformat())
617  return ret;
618 
619  if (ret = avisynth_open_file(s)) {
621  return ret;
622  }
623 
625  return 0;
626 }
627 
629 {
630  AviSynthContext *avs = s->priv_data;
631  AVStream *st;
632  int discard = 0;
633  int ret;
634 
635  if (avs->error)
636  return AVERROR_UNKNOWN;
637 
638  /* If either stream reaches EOF, try to read the other one before
639  * giving up. */
640  avisynth_next_stream(s, &st, pkt, &discard);
641  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
642  ret = avisynth_read_packet_video(s, pkt, discard);
643  if (ret == AVERROR_EOF && avs_has_audio(avs->vi)) {
644  avisynth_next_stream(s, &st, pkt, &discard);
645  return avisynth_read_packet_audio(s, pkt, discard);
646  }
647  } else {
648  ret = avisynth_read_packet_audio(s, pkt, discard);
649  if (ret == AVERROR_EOF && avs_has_video(avs->vi)) {
650  avisynth_next_stream(s, &st, pkt, &discard);
651  return avisynth_read_packet_video(s, pkt, discard);
652  }
653  }
654 
655  return ret;
656 }
657 
659 {
660  if (avpriv_lock_avformat())
661  return AVERROR_UNKNOWN;
662 
665  return 0;
666 }
667 
668 static int avisynth_read_seek(AVFormatContext *s, int stream_index,
669  int64_t timestamp, int flags)
670 {
671  AviSynthContext *avs = s->priv_data;
672  AVStream *st;
673  AVRational fps, samplerate;
674 
675  if (avs->error)
676  return AVERROR_UNKNOWN;
677 
678  fps = (AVRational) { avs->vi->fps_numerator,
679  avs->vi->fps_denominator };
680  samplerate = (AVRational) { avs->vi->audio_samples_per_second, 1 };
681 
682  st = s->streams[stream_index];
683  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
684  /* AviSynth frame counts are signed int. */
685  if ((timestamp >= avs->vi->num_frames) ||
686  (timestamp > INT_MAX) ||
687  (timestamp < 0))
688  return AVERROR_EOF;
689  avs->curr_frame = timestamp;
690  if (avs_has_audio(avs->vi))
691  avs->curr_sample = av_rescale_q(timestamp, samplerate, fps);
692  } else {
693  if ((timestamp >= avs->vi->num_audio_samples) || (timestamp < 0))
694  return AVERROR_EOF;
695  /* Force frame granularity for seeking. */
696  if (avs_has_video(avs->vi)) {
697  avs->curr_frame = av_rescale_q(timestamp, fps, samplerate);
698  avs->curr_sample = av_rescale_q(avs->curr_frame, samplerate, fps);
699  } else {
700  avs->curr_sample = timestamp;
701  }
702  }
703 
704  return 0;
705 }
706 
708  .name = "avisynth",
709  .long_name = NULL_IF_CONFIG_SMALL("AviSynth script"),
710  .priv_data_size = sizeof(AviSynthContext),
715  .extensions = "avs",
716 };