FFmpeg
avisynth.c
Go to the documentation of this file.
1 /*
2  * AviSynth(+) support
3  * Copyright (c) 2012 AvxSynth Team
4  *
5  * This file is part of FFmpeg
6  *
7  * FFmpeg 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  * FFmpeg 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
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include "libavutil/attributes.h"
23 #include "libavutil/internal.h"
24 
25 #include "libavcodec/internal.h"
26 
27 #include "avformat.h"
28 #include "internal.h"
29 #include "config.h"
30 
31 /* Enable function pointer definitions for runtime loading. */
32 #define AVSC_NO_DECLSPEC
33 
34 /* Platform-specific directives. */
35 #ifdef _WIN32
36  #include "compat/w32dlfcn.h"
37  #undef EXTERN_C
38  #define AVISYNTH_LIB "avisynth"
39 #else
40  #include <dlfcn.h>
41  #define AVISYNTH_NAME "libavisynth"
42  #define AVISYNTH_LIB AVISYNTH_NAME SLIBSUF
43 #endif
44 
45 /* Endianness guards for audio */
46 #if HAVE_BIGENDIAN
47  #define PCM(format) (AV_CODEC_ID_PCM_ ## format ## BE)
48 #else
49  #define PCM(format) (AV_CODEC_ID_PCM_ ## format ## LE)
50 #endif
51 
52 #include <avisynth/avisynth_c.h>
53 
54 typedef struct AviSynthLibrary {
55  void *library;
56 #define AVSC_DECLARE_FUNC(name) name ## _func name
57  AVSC_DECLARE_FUNC(avs_bit_blt);
58  AVSC_DECLARE_FUNC(avs_clip_get_error);
59  AVSC_DECLARE_FUNC(avs_create_script_environment);
60  AVSC_DECLARE_FUNC(avs_delete_script_environment);
61  AVSC_DECLARE_FUNC(avs_get_audio);
62  AVSC_DECLARE_FUNC(avs_get_error);
63  AVSC_DECLARE_FUNC(avs_get_frame);
64  AVSC_DECLARE_FUNC(avs_get_version);
65  AVSC_DECLARE_FUNC(avs_get_video_info);
66  AVSC_DECLARE_FUNC(avs_invoke);
67  AVSC_DECLARE_FUNC(avs_is_color_space);
68  AVSC_DECLARE_FUNC(avs_release_clip);
69  AVSC_DECLARE_FUNC(avs_release_value);
70  AVSC_DECLARE_FUNC(avs_release_video_frame);
71  AVSC_DECLARE_FUNC(avs_take_clip);
72  AVSC_DECLARE_FUNC(avs_bits_per_pixel);
73  AVSC_DECLARE_FUNC(avs_get_height_p);
74  AVSC_DECLARE_FUNC(avs_get_pitch_p);
75  AVSC_DECLARE_FUNC(avs_get_read_ptr_p);
76  AVSC_DECLARE_FUNC(avs_get_row_size_p);
77  AVSC_DECLARE_FUNC(avs_is_planar_rgb);
78  AVSC_DECLARE_FUNC(avs_is_planar_rgba);
79  AVSC_DECLARE_FUNC(avs_get_frame_props_ro);
80  AVSC_DECLARE_FUNC(avs_prop_get_int);
81  AVSC_DECLARE_FUNC(avs_prop_get_type);
82  AVSC_DECLARE_FUNC(avs_get_env_property);
83 #undef AVSC_DECLARE_FUNC
85 
86 typedef struct AviSynthContext {
87  AVS_ScriptEnvironment *env;
88  AVS_Clip *clip;
89  const AVS_VideoInfo *vi;
90 
91  /* avisynth_read_packet_video() iterates over this. */
92  int n_planes;
93  const int *planes;
94 
97  int64_t curr_sample;
98 
99  int error;
100 
101  /* Linked list pointers. */
104 
105 static const int avs_planes_packed[1] = { 0 };
106 static const int avs_planes_grey[1] = { AVS_PLANAR_Y };
107 static const int avs_planes_yuv[3] = { AVS_PLANAR_Y, AVS_PLANAR_U,
108  AVS_PLANAR_V };
109 static const int avs_planes_rgb[3] = { AVS_PLANAR_G, AVS_PLANAR_B,
110  AVS_PLANAR_R };
111 static const int avs_planes_yuva[4] = { AVS_PLANAR_Y, AVS_PLANAR_U,
112  AVS_PLANAR_V, AVS_PLANAR_A };
113 static const int avs_planes_rgba[4] = { AVS_PLANAR_G, AVS_PLANAR_B,
114  AVS_PLANAR_R, AVS_PLANAR_A };
115 
116 /* A conflict between C++ global objects, atexit, and dynamic loading requires
117  * us to register our own atexit handler to prevent double freeing. */
119 static int avs_atexit_called = 0;
120 
121 /* Linked list of AviSynthContexts. An atexit handler destroys this list. */
123 
124 static av_cold void avisynth_atexit_handler(void);
125 
127 {
128  avs_library.library = dlopen(AVISYNTH_LIB, RTLD_NOW | RTLD_LOCAL);
129  if (!avs_library.library)
130  return AVERROR_UNKNOWN;
131 
132 #define LOAD_AVS_FUNC(name, continue_on_fail) \
133  avs_library.name = (name ## _func) \
134  dlsym(avs_library.library, #name); \
135  if (!continue_on_fail && !avs_library.name) \
136  goto fail;
137 
138  LOAD_AVS_FUNC(avs_bit_blt, 0);
139  LOAD_AVS_FUNC(avs_clip_get_error, 0);
140  LOAD_AVS_FUNC(avs_create_script_environment, 0);
141  LOAD_AVS_FUNC(avs_delete_script_environment, 0);
142  LOAD_AVS_FUNC(avs_get_audio, 0);
143  LOAD_AVS_FUNC(avs_get_error, 1); // New to AviSynth 2.6
144  LOAD_AVS_FUNC(avs_get_frame, 0);
145  LOAD_AVS_FUNC(avs_get_version, 0);
146  LOAD_AVS_FUNC(avs_get_video_info, 0);
147  LOAD_AVS_FUNC(avs_invoke, 0);
148  LOAD_AVS_FUNC(avs_is_color_space, 1);
149  LOAD_AVS_FUNC(avs_release_clip, 0);
150  LOAD_AVS_FUNC(avs_release_value, 0);
151  LOAD_AVS_FUNC(avs_release_video_frame, 0);
152  LOAD_AVS_FUNC(avs_take_clip, 0);
153  LOAD_AVS_FUNC(avs_bits_per_pixel, 1);
154  LOAD_AVS_FUNC(avs_get_height_p, 1);
155  LOAD_AVS_FUNC(avs_get_pitch_p, 1);
156  LOAD_AVS_FUNC(avs_get_read_ptr_p, 1);
157  LOAD_AVS_FUNC(avs_get_row_size_p, 1);
158  LOAD_AVS_FUNC(avs_is_planar_rgb, 1);
159  LOAD_AVS_FUNC(avs_is_planar_rgba, 1);
160  LOAD_AVS_FUNC(avs_get_frame_props_ro, 1);
161  LOAD_AVS_FUNC(avs_prop_get_int, 1);
162  LOAD_AVS_FUNC(avs_prop_get_type, 1);
163  LOAD_AVS_FUNC(avs_get_env_property, 1);
164 #undef LOAD_AVS_FUNC
165 
166  atexit(avisynth_atexit_handler);
167  return 0;
168 
169 fail:
170  dlclose(avs_library.library);
171  return AVERROR_UNKNOWN;
172 }
173 
174 /* Note that avisynth_context_create and avisynth_context_destroy
175  * do not allocate or free the actual context! That is taken care of
176  * by libavformat. */
178 {
179  AviSynthContext *avs = s->priv_data;
180  int ret;
181 
182  if (!avs_library.library)
183  if (ret = avisynth_load_library())
184  return ret;
185 
186  avs->env = avs_library.avs_create_script_environment(3);
187  if (avs_library.avs_get_error) {
188  const char *error = avs_library.avs_get_error(avs->env);
189  if (error) {
190  av_log(s, AV_LOG_ERROR, "%s\n", error);
191  return AVERROR_UNKNOWN;
192  }
193  }
194 
195  if (!avs_ctx_list) {
196  avs_ctx_list = avs;
197  } else {
198  avs->next = avs_ctx_list;
199  avs_ctx_list = avs;
200  }
201 
202  return 0;
203 }
204 
206 {
207  if (avs_atexit_called)
208  return;
209 
210  if (avs == avs_ctx_list) {
211  avs_ctx_list = avs->next;
212  } else {
214  while (prev->next != avs)
215  prev = prev->next;
216  prev->next = avs->next;
217  }
218 
219  if (avs->clip) {
220  avs_library.avs_release_clip(avs->clip);
221  avs->clip = NULL;
222  }
223  if (avs->env) {
224  avs_library.avs_delete_script_environment(avs->env);
225  avs->env = NULL;
226  }
227 }
228 
230 {
232 
233  while (avs) {
234  AviSynthContext *next = avs->next;
236  avs = next;
237  }
238  dlclose(avs_library.library);
239 
240  avs_atexit_called = 1;
241 }
242 
243 /* Create AVStream from audio and video data. */
245 {
246  AviSynthContext *avs = s->priv_data;
247  const AVS_Map *avsmap;
248  AVS_VideoFrame *frame;
249  int error;
250  int planar = 0; // 0: packed, 1: YUV, 2: Y8, 3: Planar RGB, 4: YUVA, 5: Planar RGBA
251 
254  st->codecpar->width = avs->vi->width;
255  st->codecpar->height = avs->vi->height;
256 
257  st->avg_frame_rate = (AVRational) { avs->vi->fps_numerator,
258  avs->vi->fps_denominator };
259  st->start_time = 0;
260  st->duration = avs->vi->num_frames;
261  st->nb_frames = avs->vi->num_frames;
262  avpriv_set_pts_info(st, 32, avs->vi->fps_denominator, avs->vi->fps_numerator);
263 
264 
265  switch (avs->vi->pixel_type) {
266  /* 10~16-bit YUV pix_fmts (AviSynth+) */
267  case AVS_CS_YUV444P10:
269  planar = 1;
270  break;
271  case AVS_CS_YUV422P10:
273  planar = 1;
274  break;
275  case AVS_CS_YUV420P10:
277  planar = 1;
278  break;
279  case AVS_CS_YUV444P12:
281  planar = 1;
282  break;
283  case AVS_CS_YUV422P12:
285  planar = 1;
286  break;
287  case AVS_CS_YUV420P12:
289  planar = 1;
290  break;
291  case AVS_CS_YUV444P14:
293  planar = 1;
294  break;
295  case AVS_CS_YUV422P14:
297  planar = 1;
298  break;
299  case AVS_CS_YUV420P14:
301  planar = 1;
302  break;
303  case AVS_CS_YUV444P16:
305  planar = 1;
306  break;
307  case AVS_CS_YUV422P16:
309  planar = 1;
310  break;
311  case AVS_CS_YUV420P16:
313  planar = 1;
314  break;
315  /* 8~16-bit YUV pix_fmts with Alpha (AviSynth+) */
316  case AVS_CS_YUVA444:
318  planar = 4;
319  break;
320  case AVS_CS_YUVA422:
322  planar = 4;
323  break;
324  case AVS_CS_YUVA420:
326  planar = 4;
327  break;
328  case AVS_CS_YUVA444P10:
330  planar = 4;
331  break;
332  case AVS_CS_YUVA422P10:
334  planar = 4;
335  break;
336  case AVS_CS_YUVA420P10:
338  planar = 4;
339  break;
340  case AVS_CS_YUVA422P12:
342  planar = 4;
343  break;
344  case AVS_CS_YUVA444P16:
346  planar = 4;
347  break;
348  case AVS_CS_YUVA422P16:
350  planar = 4;
351  break;
352  case AVS_CS_YUVA420P16:
354  planar = 4;
355  break;
356  /* Planar RGB pix_fmts (AviSynth+) */
357  case AVS_CS_RGBP:
359  planar = 3;
360  break;
361  case AVS_CS_RGBP10:
363  planar = 3;
364  break;
365  case AVS_CS_RGBP12:
367  planar = 3;
368  break;
369  case AVS_CS_RGBP14:
371  planar = 3;
372  break;
373  case AVS_CS_RGBP16:
375  planar = 3;
376  break;
377  /* Single precision floating point Planar RGB (AviSynth+) */
378  case AVS_CS_RGBPS:
380  planar = 3;
381  break;
382  /* Planar RGB pix_fmts with Alpha (AviSynth+) */
383  case AVS_CS_RGBAP:
385  planar = 5;
386  break;
387  case AVS_CS_RGBAP10:
389  planar = 5;
390  break;
391  case AVS_CS_RGBAP12:
393  planar = 5;
394  break;
395  case AVS_CS_RGBAP16:
397  planar = 5;
398  break;
399  /* Single precision floating point Planar RGB with Alpha (AviSynth+) */
400  case AVS_CS_RGBAPS:
402  planar = 5;
403  break;
404  /* 10~16-bit gray pix_fmts (AviSynth+) */
405  case AVS_CS_Y10:
407  planar = 2;
408  break;
409  case AVS_CS_Y12:
411  planar = 2;
412  break;
413  case AVS_CS_Y14:
415  planar = 2;
416  break;
417  case AVS_CS_Y16:
419  planar = 2;
420  break;
421  /* Single precision floating point gray (AviSynth+) */
422  case AVS_CS_Y32:
424  planar = 2;
425  break;
426  /* pix_fmts added in AviSynth 2.6 */
427  case AVS_CS_YV24:
429  planar = 1;
430  break;
431  case AVS_CS_YV16:
433  planar = 1;
434  break;
435  case AVS_CS_YV411:
437  planar = 1;
438  break;
439  case AVS_CS_Y8:
441  planar = 2;
442  break;
443  /* 16-bit packed RGB pix_fmts (AviSynth+) */
444  case AVS_CS_BGR48:
446  break;
447  case AVS_CS_BGR64:
449  break;
450  /* AviSynth 2.5 pix_fmts */
451  case AVS_CS_BGR24:
453  break;
454  case AVS_CS_BGR32:
456  break;
457  case AVS_CS_YUY2:
459  break;
460  case AVS_CS_YV12:
462  planar = 1;
463  break;
464  case AVS_CS_I420: // Is this even used anywhere?
466  planar = 1;
467  break;
468  default:
470  "unknown AviSynth colorspace %d\n", avs->vi->pixel_type);
471  avs->error = 1;
472  return AVERROR_UNKNOWN;
473  }
474 
475  switch (planar) {
476  case 5: // Planar RGB + Alpha
477  avs->n_planes = 4;
478  avs->planes = avs_planes_rgba;
479  break;
480  case 4: // YUV + Alpha
481  avs->n_planes = 4;
482  avs->planes = avs_planes_yuva;
483  break;
484  case 3: // Planar RGB
485  avs->n_planes = 3;
486  avs->planes = avs_planes_rgb;
487  break;
488  case 2: // Y8
489  avs->n_planes = 1;
490  avs->planes = avs_planes_grey;
491  break;
492  case 1: // YUV
493  avs->n_planes = 3;
494  avs->planes = avs_planes_yuv;
495  break;
496  default:
497  avs->n_planes = 1;
498  avs->planes = avs_planes_packed;
499  }
500 
501  /* Read AviSynth+'s frame properties to set additional info.
502  *
503  * Due to a bug preventing the C interface from accessing frame
504  * properties in earlier versions of interface version 8, and
505  * previous attempts at being clever resulting in pre-8 versions
506  * of AviSynth+ segfaulting, only enable this if we detect
507  * version 9 at the minimum. Technically, 8.1 works, but the time
508  * distance between 8.1 and 9 is very small, so just restrict it to 9. */
509 
510  if (avs_library.avs_get_version(avs->clip) >= 9) {
511 
512  frame = avs_library.avs_get_frame(avs->clip, 0);
513  avsmap = avs_library.avs_get_frame_props_ro(avs->env, frame);
514 
515  /* Field order */
516  if(avs_library.avs_prop_get_type(avs->env, avsmap, "_FieldBased") == AVS_PROPTYPE_UNSET) {
518  } else {
519  switch (avs_library.avs_prop_get_int(avs->env, avsmap, "_FieldBased", 0, &error)) {
520  case 0:
522  break;
523  case 1:
525  break;
526  case 2:
528  break;
529  default:
531  }
532  }
533 
534  /* Color Range */
535  if(avs_library.avs_prop_get_type(avs->env, avsmap, "_ColorRange") == AVS_PROPTYPE_UNSET) {
537  } else {
538  switch (avs_library.avs_prop_get_int(avs->env, avsmap, "_ColorRange", 0, &error)) {
539  case 0:
541  break;
542  case 1:
544  break;
545  default:
547  }
548  }
549 
550  /* Color Primaries */
551  switch (avs_library.avs_prop_get_int(avs->env, avsmap, "_Primaries", 0, &error)) {
552  case 1:
554  break;
555  case 2:
557  break;
558  case 4:
560  break;
561  case 5:
563  break;
564  case 6:
566  break;
567  case 7:
569  break;
570  case 8:
572  break;
573  case 9:
575  break;
576  case 10:
578  break;
579  case 11:
581  break;
582  case 12:
584  break;
585  case 22:
587  break;
588  default:
590  }
591 
592  /* Color Transfer Characteristics */
593  switch (avs_library.avs_prop_get_int(avs->env, avsmap, "_Transfer", 0, &error)) {
594  case 1:
596  break;
597  case 2:
599  break;
600  case 4:
602  break;
603  case 5:
605  break;
606  case 6:
608  break;
609  case 7:
611  break;
612  case 8:
614  break;
615  case 9:
617  break;
618  case 10:
620  break;
621  case 11:
623  break;
624  case 12:
626  break;
627  case 13:
629  break;
630  case 14:
632  break;
633  case 15:
635  break;
636  case 16:
638  break;
639  case 17:
641  break;
642  case 18:
644  break;
645  default:
647  }
648 
649  /* Matrix coefficients */
650  if(avs_library.avs_prop_get_type(avs->env, avsmap, "_Matrix") == AVS_PROPTYPE_UNSET) {
652  } else {
653  switch (avs_library.avs_prop_get_int(avs->env, avsmap, "_Matrix", 0, &error)) {
654  case 0:
656  break;
657  case 1:
659  break;
660  case 2:
662  break;
663  case 4:
665  break;
666  case 5:
668  break;
669  case 6:
671  break;
672  case 7:
674  break;
675  case 8:
677  break;
678  case 9:
680  break;
681  case 10:
683  break;
684  case 11:
686  break;
687  case 12:
689  break;
690  case 13:
692  break;
693  case 14:
695  break;
696  default:
698  }
699  }
700 
701  /* Chroma Location */
702  if(avs_library.avs_prop_get_type(avs->env, avsmap, "_ChromaLocation") == AVS_PROPTYPE_UNSET) {
704  } else {
705  switch (avs_library.avs_prop_get_int(avs->env, avsmap, "_ChromaLocation", 0, &error)) {
706  case 0:
708  break;
709  case 1:
711  break;
712  case 2:
714  break;
715  case 3:
717  break;
718  case 4:
720  break;
721  case 5:
723  break;
724  default:
726  }
727  }
728  } else {
730  /* AviSynth works with frame-based video, detecting field order can
731  * only work when avs_is_field_based returns 'false'. */
732  av_log(s, AV_LOG_TRACE, "avs_is_field_based: %d\n", avs_is_field_based(avs->vi));
733  if (avs_is_field_based(avs->vi) == 0) {
734  if (avs_is_tff(avs->vi)) {
736  }
737  else if (avs_is_bff(avs->vi)) {
739  }
740  }
741  }
742 
743  return 0;
744 }
745 
747 {
748  AviSynthContext *avs = s->priv_data;
749 
751  st->codecpar->sample_rate = avs->vi->audio_samples_per_second;
752  st->codecpar->channels = avs->vi->nchannels;
753  st->duration = avs->vi->num_audio_samples;
754  avpriv_set_pts_info(st, 64, 1, avs->vi->audio_samples_per_second);
755 
756  switch (avs->vi->sample_type) {
757  case AVS_SAMPLE_INT8:
759  break;
760  case AVS_SAMPLE_INT16:
761  st->codecpar->codec_id = PCM(S16);
762  break;
763  case AVS_SAMPLE_INT24:
764  st->codecpar->codec_id = PCM(S24);
765  break;
766  case AVS_SAMPLE_INT32:
767  st->codecpar->codec_id = PCM(S32);
768  break;
769  case AVS_SAMPLE_FLOAT:
770  st->codecpar->codec_id = PCM(F32);
771  break;
772  default:
774  "unknown AviSynth sample type %d\n", avs->vi->sample_type);
775  avs->error = 1;
776  return AVERROR_UNKNOWN;
777  }
778  return 0;
779 }
780 
782 {
783  AviSynthContext *avs = s->priv_data;
784  AVStream *st;
785  int ret;
786  int id = 0;
787 
788  if (avs_has_video(avs->vi)) {
789  st = avformat_new_stream(s, NULL);
790  if (!st)
791  return AVERROR_UNKNOWN;
792  st->id = id++;
794  return ret;
795  }
796  if (avs_has_audio(avs->vi)) {
797  st = avformat_new_stream(s, NULL);
798  if (!st)
799  return AVERROR_UNKNOWN;
800  st->id = id++;
802  return ret;
803  }
804  return 0;
805 }
806 
808 {
809  AviSynthContext *avs = s->priv_data;
810  AVS_Value arg, val;
811  int ret;
812 #ifdef _WIN32
813  char filename_ansi[MAX_PATH * 4];
814  wchar_t filename_wc[MAX_PATH * 4];
815 #endif
816 
818  return ret;
819 
820 #ifdef _WIN32
821  /* Convert UTF-8 to ANSI code page */
822  MultiByteToWideChar(CP_UTF8, 0, s->url, -1, filename_wc, MAX_PATH * 4);
823  WideCharToMultiByte(CP_THREAD_ACP, 0, filename_wc, -1, filename_ansi,
824  MAX_PATH * 4, NULL, NULL);
825  arg = avs_new_value_string(filename_ansi);
826 #else
827  arg = avs_new_value_string(s->url);
828 #endif
829  val = avs_library.avs_invoke(avs->env, "Import", arg, 0);
830  if (avs_is_error(val)) {
831  av_log(s, AV_LOG_ERROR, "%s\n", avs_as_error(val));
833  goto fail;
834  }
835  if (!avs_is_clip(val)) {
836  av_log(s, AV_LOG_ERROR, "AviSynth script did not return a clip\n");
838  goto fail;
839  }
840 
841  avs->clip = avs_library.avs_take_clip(val, avs->env);
842  avs->vi = avs_library.avs_get_video_info(avs->clip);
843 
844  /* On Windows, FFmpeg supports AviSynth interface version 6 or higher.
845  * This includes AviSynth 2.6 RC1 or higher, and AviSynth+ r1718 or higher,
846  * and excludes 2.5 and the 2.6 alphas. */
847 
848  if (avs_library.avs_get_version(avs->clip) < 6) {
850  "AviSynth version is too old. Please upgrade to either AviSynth 2.6 >= RC1 or AviSynth+ >= r1718.\n");
852  goto fail;
853  }
854 
855  /* Release the AVS_Value as it will go out of scope. */
856  avs_library.avs_release_value(val);
857 
859  goto fail;
860 
861  return 0;
862 
863 fail:
865  return ret;
866 }
867 
869  AVPacket *pkt, int *discard)
870 {
871  AviSynthContext *avs = s->priv_data;
872 
873  avs->curr_stream++;
874  avs->curr_stream %= s->nb_streams;
875 
876  *st = s->streams[avs->curr_stream];
877  if ((*st)->discard == AVDISCARD_ALL)
878  *discard = 1;
879  else
880  *discard = 0;
881 
882  return;
883 }
884 
885 /* Copy AviSynth clip data into an AVPacket. */
887  int discard)
888 {
889  AviSynthContext *avs = s->priv_data;
890  AVS_VideoFrame *frame;
891  unsigned char *dst_p;
892  const unsigned char *src_p;
893  int n, i, plane, rowsize, planeheight, pitch, bits, ret;
894  const char *error;
895 
896  if (avs->curr_frame >= avs->vi->num_frames)
897  return AVERROR_EOF;
898 
899  /* This must happen even if the stream is discarded to prevent desync. */
900  n = avs->curr_frame++;
901  if (discard)
902  return 0;
903 
904  bits = avs_library.avs_bits_per_pixel(avs->vi);
905 
906  /* Without the cast to int64_t, calculation overflows at about 9k x 9k
907  * resolution. */
908  pkt->size = (((int64_t)avs->vi->width *
909  (int64_t)avs->vi->height) * bits) / 8;
910  if (!pkt->size)
911  return AVERROR_UNKNOWN;
912 
913  if ((ret = av_new_packet(pkt, pkt->size)) < 0)
914  return ret;
915 
916  pkt->pts = n;
917  pkt->dts = n;
918  pkt->duration = 1;
919  pkt->stream_index = avs->curr_stream;
920 
921  frame = avs_library.avs_get_frame(avs->clip, n);
922  error = avs_library.avs_clip_get_error(avs->clip);
923  if (error) {
924  av_log(s, AV_LOG_ERROR, "%s\n", error);
925  avs->error = 1;
927  return AVERROR_UNKNOWN;
928  }
929 
930  dst_p = pkt->data;
931  for (i = 0; i < avs->n_planes; i++) {
932  plane = avs->planes[i];
933  src_p = avs_library.avs_get_read_ptr_p(frame, plane);
934  pitch = avs_library.avs_get_pitch_p(frame, plane);
935 
936  rowsize = avs_library.avs_get_row_size_p(frame, plane);
937  planeheight = avs_library.avs_get_height_p(frame, plane);
938 
939  /* Flip RGB video. */
940  if (avs_library.avs_is_color_space(avs->vi, AVS_CS_BGR) ||
941  avs_library.avs_is_color_space(avs->vi, AVS_CS_BGR48) ||
942  avs_library.avs_is_color_space(avs->vi, AVS_CS_BGR64)) {
943  src_p = src_p + (planeheight - 1) * pitch;
944  pitch = -pitch;
945  }
946 
947  avs_library.avs_bit_blt(avs->env, dst_p, rowsize, src_p, pitch,
948  rowsize, planeheight);
949  dst_p += rowsize * planeheight;
950  }
951 
952  avs_library.avs_release_video_frame(frame);
953  return 0;
954 }
955 
957  int discard)
958 {
959  AviSynthContext *avs = s->priv_data;
960  AVRational fps, samplerate;
961  int samples, ret;
962  int64_t n;
963  const char *error;
964 
965  if (avs->curr_sample >= avs->vi->num_audio_samples)
966  return AVERROR_EOF;
967 
968  fps.num = avs->vi->fps_numerator;
969  fps.den = avs->vi->fps_denominator;
970  samplerate.num = avs->vi->audio_samples_per_second;
971  samplerate.den = 1;
972 
973  if (avs_has_video(avs->vi)) {
974  if (avs->curr_frame < avs->vi->num_frames)
975  samples = av_rescale_q(avs->curr_frame, samplerate, fps) -
976  avs->curr_sample;
977  else
978  samples = av_rescale_q(1, samplerate, fps);
979  } else {
980  samples = 1000;
981  }
982 
983  /* After seeking, audio may catch up with video. */
984  if (samples <= 0) {
985  pkt->size = 0;
986  pkt->data = NULL;
987  return 0;
988  }
989 
990  if (avs->curr_sample + samples > avs->vi->num_audio_samples)
991  samples = avs->vi->num_audio_samples - avs->curr_sample;
992 
993  /* This must happen even if the stream is discarded to prevent desync. */
994  n = avs->curr_sample;
995  avs->curr_sample += samples;
996  if (discard)
997  return 0;
998 
999  pkt->size = avs_bytes_per_channel_sample(avs->vi) *
1000  samples * avs->vi->nchannels;
1001  if (!pkt->size)
1002  return AVERROR_UNKNOWN;
1003 
1004  if ((ret = av_new_packet(pkt, pkt->size)) < 0)
1005  return ret;
1006 
1007  pkt->pts = n;
1008  pkt->dts = n;
1009  pkt->duration = samples;
1010  pkt->stream_index = avs->curr_stream;
1011 
1012  avs_library.avs_get_audio(avs->clip, pkt->data, n, samples);
1013  error = avs_library.avs_clip_get_error(avs->clip);
1014  if (error) {
1015  av_log(s, AV_LOG_ERROR, "%s\n", error);
1016  avs->error = 1;
1018  return AVERROR_UNKNOWN;
1019  }
1020  return 0;
1021 }
1022 
1024 {
1025  int ret;
1026 
1027  // Calling library must implement a lock for thread-safe opens.
1028  if (ret = ff_lock_avformat())
1029  return ret;
1030 
1031  if (ret = avisynth_open_file(s)) {
1033  return ret;
1034  }
1035 
1037  return 0;
1038 }
1039 
1041 {
1042  AviSynthContext *avs = s->priv_data;
1043  AVStream *st;
1044  int discard = 0;
1045  int ret;
1046 
1047  if (avs->error)
1048  return AVERROR_UNKNOWN;
1049 
1050  /* If either stream reaches EOF, try to read the other one before
1051  * giving up. */
1052  avisynth_next_stream(s, &st, pkt, &discard);
1053  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
1054  ret = avisynth_read_packet_video(s, pkt, discard);
1055  if (ret == AVERROR_EOF && avs_has_audio(avs->vi)) {
1056  avisynth_next_stream(s, &st, pkt, &discard);
1057  return avisynth_read_packet_audio(s, pkt, discard);
1058  }
1059  } else {
1060  ret = avisynth_read_packet_audio(s, pkt, discard);
1061  if (ret == AVERROR_EOF && avs_has_video(avs->vi)) {
1062  avisynth_next_stream(s, &st, pkt, &discard);
1063  return avisynth_read_packet_video(s, pkt, discard);
1064  }
1065  }
1066 
1067  return ret;
1068 }
1069 
1071 {
1072  if (ff_lock_avformat())
1073  return AVERROR_UNKNOWN;
1074 
1075  avisynth_context_destroy(s->priv_data);
1077  return 0;
1078 }
1079 
1080 static int avisynth_read_seek(AVFormatContext *s, int stream_index,
1081  int64_t timestamp, int flags)
1082 {
1083  AviSynthContext *avs = s->priv_data;
1084  AVStream *st;
1085  AVRational fps, samplerate;
1086 
1087  if (avs->error)
1088  return AVERROR_UNKNOWN;
1089 
1090  fps = (AVRational) { avs->vi->fps_numerator,
1091  avs->vi->fps_denominator };
1092  samplerate = (AVRational) { avs->vi->audio_samples_per_second, 1 };
1093 
1094  st = s->streams[stream_index];
1095  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
1096  /* AviSynth frame counts are signed int. */
1097  if ((timestamp >= avs->vi->num_frames) ||
1098  (timestamp > INT_MAX) ||
1099  (timestamp < 0))
1100  return AVERROR_EOF;
1101  avs->curr_frame = timestamp;
1102  if (avs_has_audio(avs->vi))
1103  avs->curr_sample = av_rescale_q(timestamp, samplerate, fps);
1104  } else {
1105  if ((timestamp >= avs->vi->num_audio_samples) || (timestamp < 0))
1106  return AVERROR_EOF;
1107  /* Force frame granularity for seeking. */
1108  if (avs_has_video(avs->vi)) {
1109  avs->curr_frame = av_rescale_q(timestamp, fps, samplerate);
1110  avs->curr_sample = av_rescale_q(avs->curr_frame, samplerate, fps);
1111  } else {
1112  avs->curr_sample = timestamp;
1113  }
1114  }
1115 
1116  return 0;
1117 }
1118 
1120  .name = "avisynth",
1121  .long_name = NULL_IF_CONFIG_SMALL("AviSynth script"),
1122  .priv_data_size = sizeof(AviSynthContext),
1127  .extensions = "avs",
1128 };
error
static void error(const char *err)
Definition: target_bsf_fuzzer.c:31
AV_PIX_FMT_YUVA422P16
#define AV_PIX_FMT_YUVA422P16
Definition: pixfmt.h:449
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:422
AV_PIX_FMT_GBRAP16
#define AV_PIX_FMT_GBRAP16
Definition: pixfmt.h:428
AVCOL_PRI_EBU3213
@ AVCOL_PRI_EBU3213
EBU Tech. 3213-E (nothing there) / one of JEDEC P22 group phosphors.
Definition: pixfmt.h:487
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: options.c:237
avisynth_atexit_handler
static av_cold void avisynth_atexit_handler(void)
Definition: avisynth.c:229
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:57
avisynth_load_library
static av_cold int avisynth_load_library(void)
Definition: avisynth.c:126
AVCodecParameters::color_space
enum AVColorSpace color_space
Definition: codec_par.h:150
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
AVCOL_TRC_LINEAR
@ AVCOL_TRC_LINEAR
"Linear transfer characteristics"
Definition: pixfmt.h:505
AVCHROMA_LOC_BOTTOM
@ AVCHROMA_LOC_BOTTOM
Definition: pixfmt.h:625
avisynth_read_seek
static int avisynth_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Definition: avisynth.c:1080
avisynth_read_packet
static int avisynth_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: avisynth.c:1040
AV_CODEC_ID_RAWVIDEO
@ AV_CODEC_ID_RAWVIDEO
Definition: codec_id.h:63
AviSynthContext::curr_sample
int64_t curr_sample
Definition: avisynth.c:97
AV_PIX_FMT_YUVA420P16
#define AV_PIX_FMT_YUVA420P16
Definition: pixfmt.h:448
AVCOL_RANGE_JPEG
@ AVCOL_RANGE_JPEG
Full range content.
Definition: pixfmt.h:599
avs_atexit_called
static int avs_atexit_called
Definition: avisynth.c:119
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:374
AV_PIX_FMT_YUVA420P10
#define AV_PIX_FMT_YUVA420P10
Definition: pixfmt.h:443
AviSynthLibrary::library
void * library
Definition: avisynth.c:55
avs_planes_yuv
static const int avs_planes_yuv[3]
Definition: avisynth.c:107
AVCOL_TRC_UNSPECIFIED
@ AVCOL_TRC_UNSPECIFIED
Definition: pixfmt.h:499
AVStream::avg_frame_rate
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:1028
AV_PIX_FMT_YUV420P10
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:406
AVCOL_SPC_RGB
@ AVCOL_SPC_RGB
order of coefficients is actually GBR, also IEC 61966-2-1 (sRGB), YZX and ST 428-1
Definition: pixfmt.h:526
AVCOL_TRC_BT2020_12
@ AVCOL_TRC_BT2020_12
ITU-R BT2020 for 12-bit system.
Definition: pixfmt.h:512
AV_PIX_FMT_BGR24
@ AV_PIX_FMT_BGR24
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:69
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:392
AVERROR_UNKNOWN
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:73
AV_PIX_FMT_YUVA422P10
#define AV_PIX_FMT_YUVA422P10
Definition: pixfmt.h:444
AVCOL_SPC_BT2020_CL
@ AVCOL_SPC_BT2020_CL
ITU-R BT2020 constant luminance system.
Definition: pixfmt.h:537
AviSynthContext::curr_stream
int curr_stream
Definition: avisynth.c:95
ff_unlock_avformat
int ff_unlock_avformat(void)
Definition: utils.c:53
AVCodecParameters::color_primaries
enum AVColorPrimaries color_primaries
Definition: codec_par.h:148
AviSynthLibrary
Definition: avisynth.c:54
AVCOL_SPC_BT470BG
@ AVCOL_SPC_BT470BG
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM / IEC 61966-2-4 xvYCC601
Definition: pixfmt.h:531
avpriv_set_pts_info
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: avformat.c:697
AV_FIELD_TT
@ AV_FIELD_TT
Definition: codec_par.h:40
AV_PIX_FMT_GBRP14
#define AV_PIX_FMT_GBRP14
Definition: pixfmt.h:424
AVCOL_TRC_IEC61966_2_1
@ AVCOL_TRC_IEC61966_2_1
IEC 61966-2-1 (sRGB or sYCC)
Definition: pixfmt.h:510
AV_PIX_FMT_GBRAP
@ AV_PIX_FMT_GBRAP
planar GBRA 4:4:4:4 32bpp
Definition: pixfmt.h:205
fail
#define fail()
Definition: checkasm.h:130
AV_PIX_FMT_GBRP10
#define AV_PIX_FMT_GBRP10
Definition: pixfmt.h:422
read_seek
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition: libcdio.c:151
AV_PIX_FMT_YUVA444P16
#define AV_PIX_FMT_YUVA444P16
Definition: pixfmt.h:450
AVCOL_TRC_GAMMA28
@ AVCOL_TRC_GAMMA28
also ITU-R BT470BG
Definition: pixfmt.h:502
read_close
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:143
val
static double val(void *priv, double ch)
Definition: aeval.c:77
AV_PIX_FMT_GRAY16
#define AV_PIX_FMT_GRAY16
Definition: pixfmt.h:390
AVCOL_TRC_LOG_SQRT
@ AVCOL_TRC_LOG_SQRT
"Logarithmic transfer characteristic (100 * Sqrt(10) : 1 range)"
Definition: pixfmt.h:507
AVStream::duration
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:998
AVISYNTH_LIB
#define AVISYNTH_LIB
Definition: avisynth.c:42
AVRational::num
int num
Numerator.
Definition: rational.h:59
avisynth_create_stream_video
static int avisynth_create_stream_video(AVFormatContext *s, AVStream *st)
Definition: avisynth.c:244
AVCOL_TRC_GAMMA22
@ AVCOL_TRC_GAMMA22
also ITU-R BT470M / ITU-R BT1700 625 PAL & SECAM
Definition: pixfmt.h:501
AV_PIX_FMT_YUV444P10
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:409
AVCodecParameters::color_trc
enum AVColorTransferCharacteristic color_trc
Definition: codec_par.h:149
AV_LOG_TRACE
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition: log.h:206
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
AVInputFormat
Definition: avformat.h:656
av_cold
#define av_cold
Definition: attributes.h:90
AV_PIX_FMT_YUV422P16
#define AV_PIX_FMT_YUV422P16
Definition: pixfmt.h:418
AV_PIX_FMT_GBRAP10
#define AV_PIX_FMT_GBRAP10
Definition: pixfmt.h:426
s
#define s(width, name)
Definition: cbs_vp9.c:256
AVCHROMA_LOC_TOP
@ AVCHROMA_LOC_TOP
Definition: pixfmt.h:623
av_new_packet
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:97
AV_PIX_FMT_GBRAP12
#define AV_PIX_FMT_GBRAP12
Definition: pixfmt.h:427
LOAD_AVS_FUNC
#define LOAD_AVS_FUNC(name, continue_on_fail)
AV_PIX_FMT_YUVA420P
@ AV_PIX_FMT_YUVA420P
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition: pixfmt.h:101
AVCOL_TRC_BT1361_ECG
@ AVCOL_TRC_BT1361_ECG
ITU-R BT1361 Extended Colour Gamut.
Definition: pixfmt.h:509
AV_PIX_FMT_YUV444P16
#define AV_PIX_FMT_YUV444P16
Definition: pixfmt.h:419
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:661
AVCOL_SPC_SMPTE170M
@ AVCOL_SPC_SMPTE170M
also ITU-R BT601-6 525 / ITU-R BT1358 525 / ITU-R BT1700 NTSC / functionally identical to above
Definition: pixfmt.h:532
avisynth_read_close
static av_cold int avisynth_read_close(AVFormatContext *s)
Definition: avisynth.c:1070
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AVCodecParameters::width
int width
Video only.
Definition: codec_par.h:127
PCM
#define PCM(format)
Definition: avisynth.c:49
bits
uint8_t bits
Definition: vp3data.h:141
avs_ctx_list
static AviSynthContext * avs_ctx_list
Definition: avisynth.c:122
AV_FIELD_UNKNOWN
@ AV_FIELD_UNKNOWN
Definition: codec_par.h:38
AV_PIX_FMT_YUV420P16
#define AV_PIX_FMT_YUV420P16
Definition: pixfmt.h:417
avs_planes_rgba
static const int avs_planes_rgba[4]
Definition: avisynth.c:113
AVCOL_PRI_SMPTE428
@ AVCOL_PRI_SMPTE428
SMPTE ST 428-1 (CIE 1931 XYZ)
Definition: pixfmt.h:483
AV_PIX_FMT_GRAY14
#define AV_PIX_FMT_GRAY14
Definition: pixfmt.h:389
AviSynthContext::error
int error
Definition: avisynth.c:99
av_rescale_q
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
avisynth_next_stream
static void avisynth_next_stream(AVFormatContext *s, AVStream **st, AVPacket *pkt, int *discard)
Definition: avisynth.c:868
AV_PIX_FMT_YUV420P
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
AVCOL_PRI_SMPTE240M
@ AVCOL_PRI_SMPTE240M
identical to above, also called "SMPTE C" even though it uses D65
Definition: pixfmt.h:480
AV_PIX_FMT_GRAYF32
#define AV_PIX_FMT_GRAYF32
Definition: pixfmt.h:438
AVCOL_PRI_UNSPECIFIED
@ AVCOL_PRI_UNSPECIFIED
Definition: pixfmt.h:474
AVCOL_SPC_CHROMA_DERIVED_CL
@ AVCOL_SPC_CHROMA_DERIVED_CL
Chromaticity-derived constant luminance system.
Definition: pixfmt.h:540
AVCOL_PRI_BT470BG
@ AVCOL_PRI_BT470BG
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM
Definition: pixfmt.h:478
arg
const char * arg
Definition: jacosubdec.c:67
AVCOL_PRI_SMPTE170M
@ AVCOL_PRI_SMPTE170M
also ITU-R BT601-6 525 / ITU-R BT1358 525 / ITU-R BT1700 NTSC
Definition: pixfmt.h:479
AV_PIX_FMT_GRAY10
#define AV_PIX_FMT_GRAY10
Definition: pixfmt.h:387
AVDISCARD_ALL
@ AVDISCARD_ALL
discard all
Definition: defs.h:54
AVFormatContext
Format I/O context.
Definition: avformat.h:1213
AviSynthContext::env
AVS_ScriptEnvironment * env
Definition: avisynth.c:87
AV_PIX_FMT_GBRP16
#define AV_PIX_FMT_GBRP16
Definition: pixfmt.h:425
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1108
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:529
AviSynthContext::planes
const int * planes
Definition: avisynth.c:93
AV_PIX_FMT_BGR48
#define AV_PIX_FMT_BGR48
Definition: pixfmt.h:397
NULL
#define NULL
Definition: coverity.c:32
avisynth_context_destroy
static av_cold void avisynth_context_destroy(AviSynthContext *avs)
Definition: avisynth.c:205
AVCHROMA_LOC_LEFT
@ AVCHROMA_LOC_LEFT
MPEG-2/4 4:2:0, H.264 default for 4:2:0.
Definition: pixfmt.h:620
AV_PIX_FMT_YUYV422
@ AV_PIX_FMT_YUYV422
packed YUV 4:2:2, 16bpp, Y0 Cb Y1 Cr
Definition: pixfmt.h:67
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
AVCHROMA_LOC_TOPLEFT
@ AVCHROMA_LOC_TOPLEFT
ITU-R 601, SMPTE 274M 296M S314M(DV 4:1:1), mpeg2 4:2:2.
Definition: pixfmt.h:622
AVCOL_TRC_IEC61966_2_4
@ AVCOL_TRC_IEC61966_2_4
IEC 61966-2-4.
Definition: pixfmt.h:508
avisynth_read_packet_audio
static int avisynth_read_packet_audio(AVFormatContext *s, AVPacket *pkt, int discard)
Definition: avisynth.c:956
AVCOL_PRI_BT709
@ AVCOL_PRI_BT709
also ITU-R BT1361 / IEC 61966-2-4 / SMPTE RP 177 Annex B
Definition: pixfmt.h:473
avisynth_read_header
static av_cold int avisynth_read_header(AVFormatContext *s)
Definition: avisynth.c:1023
avs_library
static AviSynthLibrary avs_library
Definition: avisynth.c:118
AVCOL_TRC_BT2020_10
@ AVCOL_TRC_BT2020_10
ITU-R BT2020 for 10-bit system.
Definition: pixfmt.h:511
AVCOL_SPC_YCGCO
@ AVCOL_SPC_YCGCO
used by Dirac / VC-2 and H.264 FRext, see ITU-T SG16
Definition: pixfmt.h:534
AV_PIX_FMT_YUV422P10
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:407
AV_PIX_FMT_GRAY8
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:74
AVCOL_RANGE_UNSPECIFIED
@ AVCOL_RANGE_UNSPECIFIED
Definition: pixfmt.h:565
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:177
AVStream::nb_frames
int64_t nb_frames
number of frames in this stream if known or 0
Definition: avformat.h:1000
AVCOL_PRI_BT2020
@ AVCOL_PRI_BT2020
ITU-R BT2020.
Definition: pixfmt.h:482
AviSynthContext::clip
AVS_Clip * clip
Definition: avisynth.c:88
AVCOL_TRC_SMPTE2084
@ AVCOL_TRC_SMPTE2084
SMPTE ST 2084 for 10-, 12-, 14- and 16-bit systems.
Definition: pixfmt.h:513
AVCOL_PRI_SMPTE431
@ AVCOL_PRI_SMPTE431
SMPTE ST 431-2 (2011) / DCI P3.
Definition: pixfmt.h:485
AVPacket::size
int size
Definition: packet.h:375
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:116
AVCOL_TRC_SMPTE240M
@ AVCOL_TRC_SMPTE240M
Definition: pixfmt.h:504
avisynth_read_packet_video
static int avisynth_read_packet_video(AVFormatContext *s, AVPacket *pkt, int discard)
Definition: avisynth.c:886
avisynth_create_stream
static int avisynth_create_stream(AVFormatContext *s)
Definition: avisynth.c:781
AVCOL_PRI_FILM
@ AVCOL_PRI_FILM
colour filters using Illuminant C
Definition: pixfmt.h:481
AVCOL_TRC_LOG
@ AVCOL_TRC_LOG
"Logarithmic transfer characteristic (100:1 range)"
Definition: pixfmt.h:506
AviSynthContext
Definition: avisynth.c:86
AV_PIX_FMT_GBRPF32
#define AV_PIX_FMT_GBRPF32
Definition: pixfmt.h:435
AV_PIX_FMT_YUV422P12
#define AV_PIX_FMT_YUV422P12
Definition: pixfmt.h:411
ff_lock_avformat
int ff_lock_avformat(void)
Definition: utils.c:48
AV_PIX_FMT_YUV444P12
#define AV_PIX_FMT_YUV444P12
Definition: pixfmt.h:413
AVCHROMA_LOC_UNSPECIFIED
@ AVCHROMA_LOC_UNSPECIFIED
Definition: pixfmt.h:619
AVPacket::dts
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition: packet.h:373
AV_PIX_FMT_RGB32
#define AV_PIX_FMT_RGB32
Definition: pixfmt.h:379
AV_PIX_FMT_YUVA444P
@ AV_PIX_FMT_YUVA444P
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
Definition: pixfmt.h:167
AV_PIX_FMT_YUVA444P10
#define AV_PIX_FMT_YUVA444P10
Definition: pixfmt.h:445
attributes.h
AVCOL_SPC_CHROMA_DERIVED_NCL
@ AVCOL_SPC_CHROMA_DERIVED_NCL
Chromaticity-derived non-constant luminance system.
Definition: pixfmt.h:539
AviSynthContext::curr_frame
int curr_frame
Definition: avisynth.c:96
AVCOL_TRC_BT709
@ AVCOL_TRC_BT709
also ITU-R BT1361
Definition: pixfmt.h:498
AVCOL_SPC_SMPTE240M
@ AVCOL_SPC_SMPTE240M
derived from 170M primaries and D65 white point, 170M is derived from BT470 System M's primaries
Definition: pixfmt.h:533
AV_FIELD_BB
@ AV_FIELD_BB
Definition: codec_par.h:41
avs_planes_grey
static const int avs_planes_grey[1]
Definition: avisynth.c:106
AV_PIX_FMT_BGRA64
#define AV_PIX_FMT_BGRA64
Definition: pixfmt.h:401
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:367
AVCOL_SPC_BT2020_NCL
@ AVCOL_SPC_BT2020_NCL
ITU-R BT2020 non-constant luminance system.
Definition: pixfmt.h:536
internal.h
AVCodecParameters::height
int height
Definition: codec_par.h:128
AV_PIX_FMT_GBRP12
#define AV_PIX_FMT_GBRP12
Definition: pixfmt.h:423
AVCodecParameters::color_range
enum AVColorRange color_range
Video only.
Definition: codec_par.h:147
AVCOL_SPC_UNSPECIFIED
@ AVCOL_SPC_UNSPECIFIED
Definition: pixfmt.h:528
AVCOL_RANGE_MPEG
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition: pixfmt.h:582
AVCodecParameters::field_order
enum AVFieldOrder field_order
Video only.
Definition: codec_par.h:142
avisynth_create_stream_audio
static int avisynth_create_stream_audio(AVFormatContext *s, AVStream *st)
Definition: avisynth.c:746
AVCOL_PRI_BT470M
@ AVCOL_PRI_BT470M
also FCC Title 47 Code of Federal Regulations 73.682 (a)(20)
Definition: pixfmt.h:476
AVStream::id
int id
Format-specific stream ID.
Definition: avformat.h:962
ret
ret
Definition: filter_design.txt:187
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
AVStream
Stream structure.
Definition: avformat.h:948
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
avformat.h
AV_PIX_FMT_YUV420P12
#define AV_PIX_FMT_YUV420P12
Definition: pixfmt.h:410
AVCodecParameters::chroma_location
enum AVChromaLocation chroma_location
Definition: codec_par.h:151
AV_PIX_FMT_YUV422P14
#define AV_PIX_FMT_YUV422P14
Definition: pixfmt.h:415
AVCOL_TRC_ARIB_STD_B67
@ AVCOL_TRC_ARIB_STD_B67
ARIB STD-B67, known as "Hybrid log-gamma".
Definition: pixfmt.h:517
AVCHROMA_LOC_CENTER
@ AVCHROMA_LOC_CENTER
MPEG-1 4:2:0, JPEG 4:2:0, H.263 4:2:0.
Definition: pixfmt.h:621
ff_avisynth_demuxer
const AVInputFormat ff_avisynth_demuxer
Definition: avisynth.c:1119
AVRational::den
int den
Denominator.
Definition: rational.h:60
AVCOL_SPC_FCC
@ AVCOL_SPC_FCC
FCC Title 47 Code of Federal Regulations 73.682 (a)(20)
Definition: pixfmt.h:530
AV_PIX_FMT_YUVA422P12
#define AV_PIX_FMT_YUVA422P12
Definition: pixfmt.h:446
AV_PIX_FMT_GBRAPF32
#define AV_PIX_FMT_GBRAPF32
Definition: pixfmt.h:436
avs_planes_rgb
static const int avs_planes_rgb[3]
Definition: avisynth.c:109
samples
Filter the word “frame” indicates either a video frame or a group of audio samples
Definition: filter_design.txt:8
AVCOL_TRC_SMPTE170M
@ AVCOL_TRC_SMPTE170M
also ITU-R BT601-6 525 or 625 / ITU-R BT1358 525 or 625 / ITU-R BT1700 NTSC
Definition: pixfmt.h:503
AVPacket::stream_index
int stream_index
Definition: packet.h:376
AV_PIX_FMT_YUV444P
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:71
AV_PIX_FMT_GBRP
@ AV_PIX_FMT_GBRP
planar GBR 4:4:4 24bpp
Definition: pixfmt.h:158
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AV_PIX_FMT_YUV422P
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:70
avs_planes_yuva
static const int avs_planes_yuva[4]
Definition: avisynth.c:111
AV_CODEC_ID_PCM_U8
@ AV_CODEC_ID_PCM_U8
Definition: codec_id.h:321
AV_FIELD_PROGRESSIVE
@ AV_FIELD_PROGRESSIVE
Definition: codec_par.h:39
AVCodecParameters::format
int format
Definition: codec_par.h:85
AVCOL_PRI_SMPTE432
@ AVCOL_PRI_SMPTE432
SMPTE ST 432-1 (2010) / P3 D65 / Display P3.
Definition: pixfmt.h:486
AviSynthContext::n_planes
int n_planes
Definition: avisynth.c:92
AviSynthContext::vi
const AVS_VideoInfo * vi
Definition: avisynth.c:89
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:61
AVPacket
This structure stores compressed data.
Definition: packet.h:351
AVCOL_SPC_SMPTE2085
@ AVCOL_SPC_SMPTE2085
SMPTE 2085, Y'D'zD'x.
Definition: pixfmt.h:538
AV_PIX_FMT_YUV411P
@ AV_PIX_FMT_YUV411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:73
AviSynthContext::next
struct AviSynthContext * next
Definition: avisynth.c:102
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:561
AviSynthLibrary::AVSC_DECLARE_FUNC
AVSC_DECLARE_FUNC(avs_bit_blt)
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVCOL_TRC_SMPTE428
@ AVCOL_TRC_SMPTE428
SMPTE ST 428-1.
Definition: pixfmt.h:515
avisynth_context_create
static av_cold int avisynth_context_create(AVFormatContext *s)
Definition: avisynth.c:177
AV_PIX_FMT_YUV444P14
#define AV_PIX_FMT_YUV444P14
Definition: pixfmt.h:416
avisynth_open_file
static int avisynth_open_file(AVFormatContext *s)
Definition: avisynth.c:807
AVStream::start_time
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base.
Definition: avformat.h:988
AV_PIX_FMT_GRAY12
#define AV_PIX_FMT_GRAY12
Definition: pixfmt.h:388
planar
uint8_t pi<< 24) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_U8,(uint64_t)((*(const uint8_t *) pi - 0x80U))<< 56) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16,(*(const int16_t *) pi >>8)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_S16, *(const int16_t *) pi *(1<< 16)) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S16,(uint64_t)(*(const int16_t *) pi)<< 48) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32,(*(const int32_t *) pi >>24)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S32,(uint64_t)(*(const int32_t *) pi)<< 32) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S64,(*(const int64_t *) pi >>56)+0x80) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S64, *(const int64_t *) pi *(1.0f/(UINT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S64, *(const int64_t *) pi *(1.0/(UINT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_FLT, llrintf(*(const float *) pi *(UINT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_DBL, llrint(*(const double *) pi *(UINT64_C(1)<< 63))) #define FMT_PAIR_FUNC(out, in) static conv_func_type *const fmt_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), };static void cpy1(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, len);} static void cpy2(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, 2 *len);} static void cpy4(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, 4 *len);} static void cpy8(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, 8 *len);} AudioConvert *swri_audio_convert_alloc(enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, const int *ch_map, int flags) { 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) return NULL;ctx=av_mallocz(sizeof(*ctx));if(!ctx) return NULL;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)){ case 1:ctx->simd_f=cpy1;break;case 2:ctx->simd_f=cpy2;break;case 4:ctx->simd_f=cpy4;break;case 8:ctx->simd_f=cpy8;break;} } if(HAVE_X86ASM &&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);return ctx;} void swri_audio_convert_free(AudioConvert **ctx) { av_freep(ctx);} int swri_audio_convert(AudioConvert *ctx, AudioData *out, AudioData *in, int len) { int ch;int off=0;const int os=(out->planar ? 1 :out->ch_count) *out->bps;unsigned misaligned=0;av_assert0(ctx->channels==out->ch_count);if(ctx->in_simd_align_mask) { int planes=in->planar ? in->ch_count :1;unsigned m=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) { int planes=out->planar ? out->ch_count :1;unsigned m=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){ int planes=out->planar ? out->ch_count :1;for(ch=0;ch< planes;ch++){ ctx->simd_f(out->ch+ch,(const uint8_t **) in->ch+ch, off *(out-> planar
Definition: audioconvert.c:56
AVCOL_SPC_BT709
@ AVCOL_SPC_BT709
also ITU-R BT1361 / IEC 61966-2-4 xvYCC709 / derived in SMPTE RP 177 Annex B
Definition: pixfmt.h:527
AVCOL_SPC_ICTCP
@ AVCOL_SPC_ICTCP
ITU-R BT.2100-0, ICtCp.
Definition: pixfmt.h:541
avs_planes_packed
static const int avs_planes_packed[1]
Definition: avisynth.c:105
AVCHROMA_LOC_BOTTOMLEFT
@ AVCHROMA_LOC_BOTTOMLEFT
Definition: pixfmt.h:624
AV_PIX_FMT_YUVA422P
@ AV_PIX_FMT_YUVA422P
planar YUV 4:2:2 24bpp, (1 Cr & Cb sample per 2x1 Y & A samples)
Definition: pixfmt.h:166
AV_PIX_FMT_YUV420P14
#define AV_PIX_FMT_YUV420P14
Definition: pixfmt.h:414
w32dlfcn.h