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 #endif
72 #undef AVSC_DECLARE_FUNC
74 
75 typedef struct AviSynthContext {
78  const AVS_VideoInfo *vi;
79 
80  /* avisynth_read_packet_video() iterates over this. */
81  int n_planes;
82  const int *planes;
83 
86  int64_t curr_sample;
87 
88  int error;
89 
90  /* Linked list pointers. */
93 
94 static const int avs_planes_packed[1] = { 0 };
95 static const int avs_planes_grey[1] = { AVS_PLANAR_Y };
96 static const int avs_planes_yuv[3] = { AVS_PLANAR_Y, AVS_PLANAR_U,
97  AVS_PLANAR_V };
98 
99 /* A conflict between C++ global objects, atexit, and dynamic loading requires
100  * us to register our own atexit handler to prevent double freeing. */
102 static int avs_atexit_called = 0;
103 
104 /* Linked list of AviSynthContexts. An atexit handler destroys this list. */
106 
107 static av_cold void avisynth_atexit_handler(void);
108 
110 {
111  avs_library.library = LoadLibrary(AVISYNTH_LIB);
112  if (!avs_library.library)
113  return AVERROR_UNKNOWN;
114 
115 #define LOAD_AVS_FUNC(name, continue_on_fail) \
116  avs_library.name = \
117  (void *)GetProcAddress(avs_library.library, #name); \
118  if (!continue_on_fail && !avs_library.name) \
119  goto fail;
120 
121  LOAD_AVS_FUNC(avs_bit_blt, 0);
122  LOAD_AVS_FUNC(avs_clip_get_error, 0);
123  LOAD_AVS_FUNC(avs_create_script_environment, 0);
124  LOAD_AVS_FUNC(avs_delete_script_environment, 0);
125  LOAD_AVS_FUNC(avs_get_audio, 0);
126  LOAD_AVS_FUNC(avs_get_error, 1); // New to AviSynth 2.6
127  LOAD_AVS_FUNC(avs_get_frame, 0);
128  LOAD_AVS_FUNC(avs_get_version, 0);
129  LOAD_AVS_FUNC(avs_get_video_info, 0);
130  LOAD_AVS_FUNC(avs_invoke, 0);
131  LOAD_AVS_FUNC(avs_release_clip, 0);
132  LOAD_AVS_FUNC(avs_release_value, 0);
133  LOAD_AVS_FUNC(avs_release_video_frame, 0);
134  LOAD_AVS_FUNC(avs_take_clip, 0);
135 #ifdef USING_AVISYNTH
141 #endif
142 #undef LOAD_AVS_FUNC
143 
144  atexit(avisynth_atexit_handler);
145  return 0;
146 
147 fail:
148  FreeLibrary(avs_library.library);
149  return AVERROR_UNKNOWN;
150 }
151 
152 /* Note that avisynth_context_create and avisynth_context_destroy
153  * do not allocate or free the actual context! That is taken care of
154  * by libavformat. */
156 {
157  AviSynthContext *avs = s->priv_data;
158  int ret;
159 
160  if (!avs_library.library)
161  if (ret = avisynth_load_library())
162  return ret;
163 
164  avs->env = avs_library.avs_create_script_environment(3);
165  if (avs_library.avs_get_error) {
166  const char *error = avs_library.avs_get_error(avs->env);
167  if (error) {
168  av_log(s, AV_LOG_ERROR, "%s\n", error);
169  return AVERROR_UNKNOWN;
170  }
171  }
172 
173  if (!avs_ctx_list) {
174  avs_ctx_list = avs;
175  } else {
176  avs->next = avs_ctx_list;
177  avs_ctx_list = avs;
178  }
179 
180  return 0;
181 }
182 
184 {
185  if (avs_atexit_called)
186  return;
187 
188  if (avs == avs_ctx_list) {
189  avs_ctx_list = avs->next;
190  } else {
192  while (prev->next != avs)
193  prev = prev->next;
194  prev->next = avs->next;
195  }
196 
197  if (avs->clip) {
198  avs_library.avs_release_clip(avs->clip);
199  avs->clip = NULL;
200  }
201  if (avs->env) {
202  avs_library.avs_delete_script_environment(avs->env);
203  avs->env = NULL;
204  }
205 }
206 
208 {
210 
211  while (avs) {
212  AviSynthContext *next = avs->next;
214  avs = next;
215  }
216  FreeLibrary(avs_library.library);
217 
218  avs_atexit_called = 1;
219 }
220 
221 /* Create AVStream from audio and video data. */
223 {
224  AviSynthContext *avs = s->priv_data;
225  int planar = 0; // 0: packed, 1: YUV, 2: Y8
226 
229  st->codecpar->width = avs->vi->width;
230  st->codecpar->height = avs->vi->height;
231 
232  st->avg_frame_rate = (AVRational) { avs->vi->fps_numerator,
233  avs->vi->fps_denominator };
234  st->start_time = 0;
235  st->duration = avs->vi->num_frames;
236  st->nb_frames = avs->vi->num_frames;
237  avpriv_set_pts_info(st, 32, avs->vi->fps_denominator, avs->vi->fps_numerator);
238 
239  switch (avs->vi->pixel_type) {
240 #ifdef USING_AVISYNTH
241  case AVS_CS_YV24:
243  planar = 1;
244  break;
245  case AVS_CS_YV16:
247  planar = 1;
248  break;
249  case AVS_CS_YV411:
251  planar = 1;
252  break;
253  case AVS_CS_Y8:
255  planar = 2;
256  break;
257 #endif
258  case AVS_CS_BGR24:
260  break;
261  case AVS_CS_BGR32:
263  break;
264  case AVS_CS_YUY2:
266  break;
267  case AVS_CS_YV12:
269  planar = 1;
270  break;
271  case AVS_CS_I420: // Is this even used anywhere?
273  planar = 1;
274  break;
275  default:
276  av_log(s, AV_LOG_ERROR,
277  "unknown AviSynth colorspace %d\n", avs->vi->pixel_type);
278  avs->error = 1;
279  return AVERROR_UNKNOWN;
280  }
281 
282  switch (planar) {
283  case 2: // Y8
284  avs->n_planes = 1;
285  avs->planes = avs_planes_grey;
286  break;
287  case 1: // YUV
288  avs->n_planes = 3;
289  avs->planes = avs_planes_yuv;
290  break;
291  default:
292  avs->n_planes = 1;
293  avs->planes = avs_planes_packed;
294  }
295  return 0;
296 }
297 
299 {
300  AviSynthContext *avs = s->priv_data;
301 
304  st->codecpar->channels = avs->vi->nchannels;
305  st->duration = avs->vi->num_audio_samples;
307 
308  switch (avs->vi->sample_type) {
309  case AVS_SAMPLE_INT8:
311  break;
312  case AVS_SAMPLE_INT16:
314  break;
315  case AVS_SAMPLE_INT24:
317  break;
318  case AVS_SAMPLE_INT32:
320  break;
321  case AVS_SAMPLE_FLOAT:
323  break;
324  default:
325  av_log(s, AV_LOG_ERROR,
326  "unknown AviSynth sample type %d\n", avs->vi->sample_type);
327  avs->error = 1;
328  return AVERROR_UNKNOWN;
329  }
330  return 0;
331 }
332 
334 {
335  AviSynthContext *avs = s->priv_data;
336  AVStream *st;
337  int ret;
338  int id = 0;
339 
340  if (avs_has_video(avs->vi)) {
341  st = avformat_new_stream(s, NULL);
342  if (!st)
343  return AVERROR_UNKNOWN;
344  st->id = id++;
345  if (ret = avisynth_create_stream_video(s, st))
346  return ret;
347  }
348  if (avs_has_audio(avs->vi)) {
349  st = avformat_new_stream(s, NULL);
350  if (!st)
351  return AVERROR_UNKNOWN;
352  st->id = id++;
353  if (ret = avisynth_create_stream_audio(s, st))
354  return ret;
355  }
356  return 0;
357 }
358 
360 {
361  AviSynthContext *avs = s->priv_data;
362  AVS_Value arg, val;
363  int ret;
364 #ifdef USING_AVISYNTH
365  char filename_ansi[MAX_PATH * 4];
366  wchar_t filename_wc[MAX_PATH * 4];
367 #endif
368 
369  if (ret = avisynth_context_create(s))
370  return ret;
371 
372 #ifdef USING_AVISYNTH
373  /* Convert UTF-8 to ANSI code page */
374  MultiByteToWideChar(CP_UTF8, 0, s->filename, -1, filename_wc, MAX_PATH * 4);
375  WideCharToMultiByte(CP_THREAD_ACP, 0, filename_wc, -1, filename_ansi,
376  MAX_PATH * 4, NULL, NULL);
377  arg = avs_new_value_string(filename_ansi);
378 #else
379  arg = avs_new_value_string(s->filename);
380 #endif
381  val = avs_library.avs_invoke(avs->env, "Import", arg, 0);
382  if (avs_is_error(val)) {
383  av_log(s, AV_LOG_ERROR, "%s\n", avs_as_error(val));
384  ret = AVERROR_UNKNOWN;
385  goto fail;
386  }
387  if (!avs_is_clip(val)) {
388  av_log(s, AV_LOG_ERROR, "AviSynth script did not return a clip\n");
389  ret = AVERROR_UNKNOWN;
390  goto fail;
391  }
392 
393  avs->clip = avs_library.avs_take_clip(val, avs->env);
394  avs->vi = avs_library.avs_get_video_info(avs->clip);
395 
396 #ifdef USING_AVISYNTH
397  /* On Windows, FFmpeg supports AviSynth interface version 6 or higher.
398  * This includes AviSynth 2.6 RC1 or higher, and AviSynth+ r1718 or higher,
399  * and excludes 2.5 and the 2.6 alphas. Since AvxSynth identifies itself
400  * as interface version 3 like 2.5.8, this needs to be special-cased. */
401 
402  if (avs_library.avs_get_version(avs->clip) < 6) {
403  av_log(s, AV_LOG_ERROR,
404  "AviSynth version is too old. Please upgrade to either AviSynth 2.6 >= RC1 or AviSynth+ >= r1718.\n");
405  ret = AVERROR_UNKNOWN;
406  goto fail;
407  }
408 #endif
409 
410  /* Release the AVS_Value as it will go out of scope. */
411  avs_library.avs_release_value(val);
412 
413  if (ret = avisynth_create_stream(s))
414  goto fail;
415 
416  return 0;
417 
418 fail:
420  return ret;
421 }
422 
424  AVPacket *pkt, int *discard)
425 {
426  AviSynthContext *avs = s->priv_data;
427 
428  avs->curr_stream++;
429  avs->curr_stream %= s->nb_streams;
430 
431  *st = s->streams[avs->curr_stream];
432  if ((*st)->discard == AVDISCARD_ALL)
433  *discard = 1;
434  else
435  *discard = 0;
436 
437  return;
438 }
439 
440 /* Copy AviSynth clip data into an AVPacket. */
442  int discard)
443 {
444  AviSynthContext *avs = s->priv_data;
446  unsigned char *dst_p;
447  const unsigned char *src_p;
448  int n, i, plane, rowsize, planeheight, pitch, bits;
449  const char *error;
450 
451  if (avs->curr_frame >= avs->vi->num_frames)
452  return AVERROR_EOF;
453 
454  /* This must happen even if the stream is discarded to prevent desync. */
455  n = avs->curr_frame++;
456  if (discard)
457  return 0;
458 
459 #ifdef USING_AVISYNTH
460  /* avs_bits_per_pixel changed to AVSC_API with AviSynth 2.6, which
461  * requires going through avs_library, while AvxSynth has it under
462  * the older AVSC_INLINE type, so special-case this. */
463 
464  bits = avs_library.avs_bits_per_pixel(avs->vi);
465 #else
466  bits = avs_bits_per_pixel(avs->vi);
467 #endif
468 
469  /* Without the cast to int64_t, calculation overflows at about 9k x 9k
470  * resolution. */
471  pkt->size = (((int64_t)avs->vi->width *
472  (int64_t)avs->vi->height) * bits) / 8;
473  if (!pkt->size)
474  return AVERROR_UNKNOWN;
475 
476  if (av_new_packet(pkt, pkt->size) < 0)
477  return AVERROR(ENOMEM);
478 
479  pkt->pts = n;
480  pkt->dts = n;
481  pkt->duration = 1;
482  pkt->stream_index = avs->curr_stream;
483 
484  frame = avs_library.avs_get_frame(avs->clip, n);
485  error = avs_library.avs_clip_get_error(avs->clip);
486  if (error) {
487  av_log(s, AV_LOG_ERROR, "%s\n", error);
488  avs->error = 1;
489  av_packet_unref(pkt);
490  return AVERROR_UNKNOWN;
491  }
492 
493  dst_p = pkt->data;
494  for (i = 0; i < avs->n_planes; i++) {
495  plane = avs->planes[i];
496 #ifdef USING_AVISYNTH
497  src_p = avs_library.avs_get_read_ptr_p(frame, plane);
498  pitch = avs_library.avs_get_pitch_p(frame, plane);
499 
500  rowsize = avs_library.avs_get_row_size_p(frame, plane);
501  planeheight = avs_library.avs_get_height_p(frame, plane);
502 #else
503  src_p = avs_get_read_ptr_p(frame, plane);
504  pitch = avs_get_pitch_p(frame, plane);
505 
506  rowsize = avs_get_row_size_p(frame, plane);
507  planeheight = avs_get_height_p(frame, plane);
508 #endif
509 
510  /* Flip RGB video. */
511  if (avs_is_rgb24(avs->vi) || avs_is_rgb(avs->vi)) {
512  src_p = src_p + (planeheight - 1) * pitch;
513  pitch = -pitch;
514  }
515 
516  avs_library.avs_bit_blt(avs->env, dst_p, rowsize, src_p, pitch,
517  rowsize, planeheight);
518  dst_p += rowsize * planeheight;
519  }
520 
521  avs_library.avs_release_video_frame(frame);
522  return 0;
523 }
524 
526  int discard)
527 {
528  AviSynthContext *avs = s->priv_data;
529  AVRational fps, samplerate;
530  int samples;
531  int64_t n;
532  const char *error;
533 
534  if (avs->curr_sample >= avs->vi->num_audio_samples)
535  return AVERROR_EOF;
536 
537  fps.num = avs->vi->fps_numerator;
538  fps.den = avs->vi->fps_denominator;
539  samplerate.num = avs->vi->audio_samples_per_second;
540  samplerate.den = 1;
541 
542  if (avs_has_video(avs->vi)) {
543  if (avs->curr_frame < avs->vi->num_frames)
544  samples = av_rescale_q(avs->curr_frame, samplerate, fps) -
545  avs->curr_sample;
546  else
547  samples = av_rescale_q(1, samplerate, fps);
548  } else {
549  samples = 1000;
550  }
551 
552  /* After seeking, audio may catch up with video. */
553  if (samples <= 0) {
554  pkt->size = 0;
555  pkt->data = NULL;
556  return 0;
557  }
558 
559  if (avs->curr_sample + samples > avs->vi->num_audio_samples)
560  samples = avs->vi->num_audio_samples - avs->curr_sample;
561 
562  /* This must happen even if the stream is discarded to prevent desync. */
563  n = avs->curr_sample;
564  avs->curr_sample += samples;
565  if (discard)
566  return 0;
567 
568  pkt->size = avs_bytes_per_channel_sample(avs->vi) *
569  samples * avs->vi->nchannels;
570  if (!pkt->size)
571  return AVERROR_UNKNOWN;
572 
573  if (av_new_packet(pkt, pkt->size) < 0)
574  return AVERROR(ENOMEM);
575 
576  pkt->pts = n;
577  pkt->dts = n;
578  pkt->duration = samples;
579  pkt->stream_index = avs->curr_stream;
580 
581  avs_library.avs_get_audio(avs->clip, pkt->data, n, samples);
582  error = avs_library.avs_clip_get_error(avs->clip);
583  if (error) {
584  av_log(s, AV_LOG_ERROR, "%s\n", error);
585  avs->error = 1;
586  av_packet_unref(pkt);
587  return AVERROR_UNKNOWN;
588  }
589  return 0;
590 }
591 
593 {
594  int ret;
595 
596  // Calling library must implement a lock for thread-safe opens.
597  if (ret = avpriv_lock_avformat())
598  return ret;
599 
600  if (ret = avisynth_open_file(s)) {
602  return ret;
603  }
604 
606  return 0;
607 }
608 
610 {
611  AviSynthContext *avs = s->priv_data;
612  AVStream *st;
613  int discard = 0;
614  int ret;
615 
616  if (avs->error)
617  return AVERROR_UNKNOWN;
618 
619  /* If either stream reaches EOF, try to read the other one before
620  * giving up. */
621  avisynth_next_stream(s, &st, pkt, &discard);
622  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
623  ret = avisynth_read_packet_video(s, pkt, discard);
624  if (ret == AVERROR_EOF && avs_has_audio(avs->vi)) {
625  avisynth_next_stream(s, &st, pkt, &discard);
626  return avisynth_read_packet_audio(s, pkt, discard);
627  }
628  } else {
629  ret = avisynth_read_packet_audio(s, pkt, discard);
630  if (ret == AVERROR_EOF && avs_has_video(avs->vi)) {
631  avisynth_next_stream(s, &st, pkt, &discard);
632  return avisynth_read_packet_video(s, pkt, discard);
633  }
634  }
635 
636  return ret;
637 }
638 
640 {
641  if (avpriv_lock_avformat())
642  return AVERROR_UNKNOWN;
643 
646  return 0;
647 }
648 
649 static int avisynth_read_seek(AVFormatContext *s, int stream_index,
650  int64_t timestamp, int flags)
651 {
652  AviSynthContext *avs = s->priv_data;
653  AVStream *st;
654  AVRational fps, samplerate;
655 
656  if (avs->error)
657  return AVERROR_UNKNOWN;
658 
659  fps = (AVRational) { avs->vi->fps_numerator,
660  avs->vi->fps_denominator };
661  samplerate = (AVRational) { avs->vi->audio_samples_per_second, 1 };
662 
663  st = s->streams[stream_index];
664  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
665  /* AviSynth frame counts are signed int. */
666  if ((timestamp >= avs->vi->num_frames) ||
667  (timestamp > INT_MAX) ||
668  (timestamp < 0))
669  return AVERROR_EOF;
670  avs->curr_frame = timestamp;
671  if (avs_has_audio(avs->vi))
672  avs->curr_sample = av_rescale_q(timestamp, samplerate, fps);
673  } else {
674  if ((timestamp >= avs->vi->num_audio_samples) || (timestamp < 0))
675  return AVERROR_EOF;
676  /* Force frame granularity for seeking. */
677  if (avs_has_video(avs->vi)) {
678  avs->curr_frame = av_rescale_q(timestamp, fps, samplerate);
679  avs->curr_sample = av_rescale_q(avs->curr_frame, samplerate, fps);
680  } else {
681  avs->curr_sample = timestamp;
682  }
683  }
684 
685  return 0;
686 }
687 
689  .name = "avisynth",
690  .long_name = NULL_IF_CONFIG_SMALL("AviSynth script"),
691  .priv_data_size = sizeof(AviSynthContext),
696  .extensions = "avs",
697 };
int plane
Definition: avisynth_c.h:422
#define NULL
Definition: coverity.c:32
const char const char void * val
Definition: avisynth_c.h:771
const char * s
Definition: avisynth_c.h:768
static int avs_atexit_called
Definition: avisynth.c:102
int avpriv_unlock_avformat(void)
Definition: utils.c:3830
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:67
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:4507
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition: libcdio.c:153
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3953
int num
Numerator.
Definition: rational.h:59
int size
Definition: avcodec.h:1591
discard all
Definition: avcodec.h:786
static AVPacket pkt
static const int avs_planes_yuv[3]
Definition: avisynth.c:96
Format I/O context.
Definition: avformat.h:1327
uint8_t bits
Definition: crc.c:296
static int avisynth_read_packet_audio(AVFormatContext *s, AVPacket *pkt, int discard)
Definition: avisynth.c:525
#define av_cold
Definition: attributes.h:82
int width
Video only.
Definition: avcodec.h:4019
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1608
int id
Format-specific stream ID.
Definition: avformat.h:885
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4140
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1395
AVSC_INLINE int avs_is_rgb(const AVS_VideoInfo *p)
Definition: avisynth_c.h:341
static AVFrame * frame
uint8_t * data
Definition: avcodec.h:1590
#define AVERROR_EOF
End of file.
Definition: error.h:55
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
#define av_log(a,...)
static av_cold int avisynth_read_close(AVFormatContext *s)
Definition: avisynth.c:639
AVSC_INLINE int avs_get_pitch_p(const AVS_VideoFrame *p, int plane)
Definition: avxsynth_c.h:340
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
#define AVISYNTH_LIB
Definition: avisynth.c:41
INT64 num_audio_samples
Definition: avisynth_c.h:326
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:86
uint8_t pi<< 24) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_U8,(uint64_t)((*(constuint8_t *) pi-0x80U))<< 56) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S16,(uint64_t)(*(constint16_t *) pi)<< 48) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S32,(uint64_t)(*(constint32_t *) pi)<< 32) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S64,(*(constint64_t *) pi >>56)+0x80) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S64,*(constint64_t *) pi *(1.0f/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S64,*(constint64_t *) pi *(1.0/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_FLT, llrintf(*(constfloat *) pi *(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_DBL, llrint(*(constdouble *) pi *(INT64_C(1)<< 63)))#defineFMT_PAIR_FUNC(out, in) staticconv_func_type *constfmt_pair_to_conv_functions[AV_SAMPLE_FMT_NB *AV_SAMPLE_FMT_NB]={FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S64),};staticvoidcpy1(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, len);}staticvoidcpy2(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 2 *len);}staticvoidcpy4(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 4 *len);}staticvoidcpy8(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 8 *len);}AudioConvert *swri_audio_convert_alloc(enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, constint *ch_map, intflags){AudioConvert *ctx;conv_func_type *f=fmt_pair_to_conv_functions[av_get_packed_sample_fmt(out_fmt)+AV_SAMPLE_FMT_NB *av_get_packed_sample_fmt(in_fmt)];if(!f) returnNULL;ctx=av_mallocz(sizeof(*ctx));if(!ctx) returnNULL;if(channels==1){in_fmt=av_get_planar_sample_fmt(in_fmt);out_fmt=av_get_planar_sample_fmt(out_fmt);}ctx->channels=channels;ctx->conv_f=f;ctx->ch_map=ch_map;if(in_fmt==AV_SAMPLE_FMT_U8||in_fmt==AV_SAMPLE_FMT_U8P) memset(ctx->silence, 0x80, sizeof(ctx->silence));if(out_fmt==in_fmt &&!ch_map){switch(av_get_bytes_per_sample(in_fmt)){case1:ctx->simd_f=cpy1;break;case2:ctx->simd_f=cpy2;break;case4:ctx->simd_f=cpy4;break;case8:ctx->simd_f=cpy8;break;}}if(HAVE_YASM &&1) swri_audio_convert_init_x86(ctx, out_fmt, in_fmt, channels);if(ARCH_ARM) swri_audio_convert_init_arm(ctx, out_fmt, in_fmt, channels);if(ARCH_AARCH64) swri_audio_convert_init_aarch64(ctx, out_fmt, in_fmt, channels);returnctx;}voidswri_audio_convert_free(AudioConvert **ctx){av_freep(ctx);}intswri_audio_convert(AudioConvert *ctx, AudioData *out, AudioData *in, intlen){intch;intoff=0;constintos=(out->planar?1:out->ch_count)*out->bps;unsignedmisaligned=0;av_assert0(ctx->channels==out->ch_count);if(ctx->in_simd_align_mask){intplanes=in->planar?in->ch_count:1;unsignedm=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) in->ch[ch];misaligned|=m &ctx->in_simd_align_mask;}if(ctx->out_simd_align_mask){intplanes=out->planar?out->ch_count:1;unsignedm=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) out->ch[ch];misaligned|=m &ctx->out_simd_align_mask;}if(ctx->simd_f &&!ctx->ch_map &&!misaligned){off=len &~15;av_assert1(off >=0);av_assert1(off<=len);av_assert2(ctx->channels==SWR_CH_MAX||!in->ch[ctx->channels]);if(off >0){if(out->planar==in->planar){intplanes=out->planar?out->ch_count:1;for(ch=0;ch< planes;ch++){ctx->simd_f(out-> ch const uint8_t **in ch off *out planar
Definition: audioconvert.c:56
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
const int * planes
Definition: avisynth.c:82
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3949
const char * arg
Definition: jacosubdec.c:66
AVSC_INLINE int avs_is_rgb24(const AVS_VideoInfo *p)
Definition: avisynth_c.h:344
AVSC_INLINE int avs_has_video(const AVS_VideoInfo *p)
Definition: avisynth_c.h:335
struct AVS_Clip AVS_Clip
Definition: avisynth_c.h:308
static int avisynth_read_packet_video(AVFormatContext *s, AVPacket *pkt, int discard)
Definition: avisynth.c:441
AVInputFormat ff_avisynth_demuxer
Definition: avisynth.c:688
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:956
int avpriv_lock_avformat(void)
Definition: utils.c:3821
#define fail()
Definition: checkasm.h:82
unsigned fps_numerator
Definition: avisynth_c.h:319
AVS_ScriptEnvironment * env
Definition: avisynth.c:76
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:66
static AviSynthLibrary avs_library
Definition: avisynth.c:101
AVSC_DECLARE_FUNC(avs_bit_blt)
static int avisynth_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: avisynth.c:609
common internal API header
AVSC_INLINE int avs_is_clip(AVS_Value v)
Definition: avisynth_c.h:623
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1383
char filename[1024]
input or output filename
Definition: avformat.h:1403
AVSC_INLINE AVS_Value avs_new_value_string(const char *v0)
Definition: avisynth_c.h:657
AVSC_INLINE const char * avs_as_error(AVS_Value v)
Definition: avisynth_c.h:642
AVS_Clip * clip
Definition: avisynth.c:77
static int avisynth_create_stream_video(AVFormatContext *s, AVStream *st)
Definition: avisynth.c:222
static const int avs_planes_grey[1]
Definition: avisynth.c:95
int n
Definition: avisynth_c.h:684
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:65
#define LoadLibrary(x)
Definition: avisynth.c:43
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:514
AVSC_INLINE int avs_bits_per_pixel(const AVS_VideoInfo *p)
Definition: avxsynth_c.h:221
Stream structure.
Definition: avformat.h:878
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
static int avisynth_create_stream_audio(AVFormatContext *s, AVStream *st)
Definition: avisynth.c:298
static av_cold void avisynth_atexit_handler(void)
Definition: avisynth.c:207
AVSC_INLINE const unsigned char * avs_get_read_ptr_p(const AVS_VideoFrame *p, int plane)
Definition: avxsynth_c.h:384
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:563
AVSC_INLINE int avs_is_error(AVS_Value v)
Definition: avisynth_c.h:629
#define AV_PIX_FMT_RGB32
Definition: pixfmt.h:320
packed YUV 4:2:2, 16bpp, Y0 Cb Y1 Cr
Definition: pixfmt.h:63
void * library
Definition: avisynth.c:49
#define LOAD_AVS_FUNC(name, continue_on_fail)
Rational number (pair of numerator and denominator).
Definition: rational.h:58
AVSC_INLINE int avs_bytes_per_channel_sample(const AVS_VideoInfo *p)
Definition: avisynth_c.h:439
static int avisynth_create_stream(AVFormatContext *s)
Definition: avisynth.c:333
const AVS_VideoInfo * vi
Definition: avisynth.c:78
static int flags
Definition: cpu.c:47
static int avisynth_open_file(AVFormatContext *s)
Definition: avisynth.c:359
static av_cold int avisynth_load_library(void)
Definition: avisynth.c:109
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:932
int sample_rate
Audio only.
Definition: avcodec.h:4063
Main libavformat public API header.
struct AVS_ScriptEnvironment AVS_ScriptEnvironment
Definition: avisynth_c.h:309
static int avisynth_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Definition: avisynth.c:649
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:62
Y , 8bpp.
Definition: pixfmt.h:70
common internal api header.
static AviSynthContext * avs_ctx_list
Definition: avisynth.c:105
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base...
Definition: avformat.h:925
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:69
int64_t nb_frames
number of frames in this stream if known or 0
Definition: avformat.h:934
int den
Denominator.
Definition: rational.h:60
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:71
AVSC_INLINE int avs_get_height_p(const AVS_VideoFrame *p, int plane)
Definition: avxsynth_c.h:373
struct AviSynthContext * next
Definition: avisynth.c:91
void * priv_data
Format private data.
Definition: avformat.h:1355
unsigned fps_denominator
Definition: avisynth_c.h:319
int channels
Audio only.
Definition: avcodec.h:4059
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1589
static av_cold int avisynth_context_create(AVFormatContext *s)
Definition: avisynth.c:155
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:662
AVCodecParameters * codecpar
Definition: avformat.h:1230
static const int avs_planes_packed[1]
Definition: avisynth.c:94
static av_cold void avisynth_context_destroy(AviSynthContext *avs)
Definition: avisynth.c:183
int stream_index
Definition: avcodec.h:1592
AVSC_INLINE int avs_get_row_size_p(const AVS_VideoFrame *p, int plane)
Definition: avxsynth_c.h:348
#define FreeLibrary
Definition: avisynth.c:45
static av_cold int avisynth_read_header(AVFormatContext *s)
Definition: avisynth.c:592
This structure stores compressed data.
Definition: avcodec.h:1567
static void avisynth_next_stream(AVFormatContext *s, AVStream **st, AVPacket *pkt, int *discard)
Definition: avisynth.c:423
int64_t curr_sample
Definition: avisynth.c:86
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1583
AVSC_INLINE int avs_has_audio(const AVS_VideoInfo *p)
Definition: avisynth_c.h:338
int audio_samples_per_second
Definition: avisynth_c.h:324