History

avformat/avisynth: remove library allocation from global state

As part of this, the mutexes are no longer necessary, and avisynth_read_close needs to check that avs->avs_library.library still exists before it attempts to call avisynth_context_destroy and dlclose.

Stephen Hutchinson committed 2 years ago

Commit 48d6cd31 parent 8f01ce21 file · +64 −69Browse files

Unified · Split

libavformat/avisynth.c+64−69
@@ -115,6 +115,7 @@ typedef struct AviSynthContext { · 114 unmodified lines
115 int error;115 int error;
116 116
117 uint32_t flags;117 uint32_t flags;
118 struct AviSynthLibrary avs_library;
118} AviSynthContext;119} AviSynthContext;
119 120
120static const int avs_planes_packed[1] = { 0 };121static const int avs_planes_packed[1] = { 0 };
@@ -128,20 +129,16 @@ static const int avs_planes_yuva[4] = { AVS_PLANAR_Y, AVS_PLANAR_U, · 7 unmodified lines
128static const int avs_planes_rgba[4] = { AVS_PLANAR_G, AVS_PLANAR_B,129static const int avs_planes_rgba[4] = { AVS_PLANAR_G, AVS_PLANAR_B,
129 AVS_PLANAR_R, AVS_PLANAR_A };130 AVS_PLANAR_R, AVS_PLANAR_A };
130 131
131static AVMutex avisynth_mutex = AV_MUTEX_INITIALIZER;132static av_cold int avisynth_load_library(AviSynthContext *avs)
132
133static AviSynthLibrary avs_library;
134
135static av_cold int avisynth_load_library(void)
136{133{
137 avs_library.library = dlopen(AVISYNTH_LIB, RTLD_NOW | RTLD_LOCAL);134 avs->avs_library.library = dlopen(AVISYNTH_LIB, RTLD_NOW | RTLD_LOCAL);
138 if (!avs_library.library)135 if (!avs->avs_library.library)
139 return AVERROR_UNKNOWN;136 return AVERROR_UNKNOWN;
140 137
141#define LOAD_AVS_FUNC(name, continue_on_fail) \138#define LOAD_AVS_FUNC(name, continue_on_fail) \
142 avs_library.name = (name ## _func) \139 avs->avs_library.name = (name ## _func) \
143 dlsym(avs_library.library, #name); \140 dlsym(avs->avs_library.library, #name); \
144 if (!continue_on_fail && !avs_library.name) \141 if (!continue_on_fail && !avs->avs_library.name) \
145 goto fail;142 goto fail;
146 143
147 LOAD_AVS_FUNC(avs_bit_blt, 0);144 LOAD_AVS_FUNC(avs_bit_blt, 0);
@@ -177,7 +174,7 @@ static av_cold int avisynth_load_library(void) · 29 unmodified lines
177 return 0;174 return 0;
178 175
179fail:176fail:
180 dlclose(avs_library.library);177 dlclose(avs->avs_library.library);
181 return AVERROR_UNKNOWN;178 return AVERROR_UNKNOWN;
182}179}
183 180
@@ -189,13 +186,13 @@ static av_cold int avisynth_context_create(AVFormatContext *s) · 5 unmodified lines
189 AviSynthContext *avs = s->priv_data;186 AviSynthContext *avs = s->priv_data;
190 int ret;187 int ret;
191 188
192 if (!avs_library.library)189 if (!avs->avs_library.library)
193 if (ret = avisynth_load_library())190 if (ret = avisynth_load_library(avs))
194 return ret;191 return ret;
195 192
196 avs->env = avs_library.avs_create_script_environment(3);193 avs->env = avs->avs_library.avs_create_script_environment(3);
197 if (avs_library.avs_get_error) {194 if (avs->avs_library.avs_get_error) {
198 const char *error = avs_library.avs_get_error(avs->env);195 const char *error = avs->avs_library.avs_get_error(avs->env);
199 if (error) {196 if (error) {
200 av_log(s, AV_LOG_ERROR, "%s\n", error);197 av_log(s, AV_LOG_ERROR, "%s\n", error);
201 return AVERROR_UNKNOWN;198 return AVERROR_UNKNOWN;
@@ -208,11 +205,11 @@ static av_cold int avisynth_context_create(AVFormatContext *s) · 6 unmodified lines
208static av_cold void avisynth_context_destroy(AviSynthContext *avs)205static av_cold void avisynth_context_destroy(AviSynthContext *avs)
209{206{
210 if (avs->clip) {207 if (avs->clip) {
211 avs_library.avs_release_clip(avs->clip);208 avs->avs_library.avs_release_clip(avs->clip);
212 avs->clip = NULL;209 avs->clip = NULL;
213 }210 }
214 if (avs->env) {211 if (avs->env) {
215 avs_library.avs_delete_script_environment(avs->env);212 avs->avs_library.avs_delete_script_environment(avs->env);
216 avs->env = NULL;213 avs->env = NULL;
217 }214 }
218}215}
@@ -486,17 +483,17 @@ static int avisynth_create_stream_video(AVFormatContext *s, AVStream *st) · 267 unmodified lines
486 * version 9 at the minimum. Technically, 8.1 works, but the time483 * version 9 at the minimum. Technically, 8.1 works, but the time
487 * distance between 8.1 and 9 is very small, so just restrict it to 9. */484 * distance between 8.1 and 9 is very small, so just restrict it to 9. */
488 485
489 if (avs_library.avs_get_version(avs->clip) >= 9) {486 if (avs->avs_library.avs_get_version(avs->clip) >= 9) {
490 487
491 frame = avs_library.avs_get_frame(avs->clip, 0);488 frame = avs->avs_library.avs_get_frame(avs->clip, 0);
492 avsmap = avs_library.avs_get_frame_props_ro(avs->env, frame);489 avsmap = avs->avs_library.avs_get_frame_props_ro(avs->env, frame);
493 490
494 /* Field order */491 /* Field order */
495 if(avs->flags & AVISYNTH_FRAMEPROP_FIELD_ORDER) {492 if(avs->flags & AVISYNTH_FRAMEPROP_FIELD_ORDER) {
496 if(avs_library.avs_prop_get_type(avs->env, avsmap, "_FieldBased") == AVS_PROPTYPE_UNSET) {493 if(avs->avs_library.avs_prop_get_type(avs->env, avsmap, "_FieldBased") == AVS_PROPTYPE_UNSET) {
497 st->codecpar->field_order = AV_FIELD_UNKNOWN;494 st->codecpar->field_order = AV_FIELD_UNKNOWN;
498 } else {495 } else {
499 switch (avs_library.avs_prop_get_int(avs->env, avsmap, "_FieldBased", 0, &error)) {496 switch (avs->avs_library.avs_prop_get_int(avs->env, avsmap, "_FieldBased", 0, &error)) {
500 case 0:497 case 0:
501 st->codecpar->field_order = AV_FIELD_PROGRESSIVE;498 st->codecpar->field_order = AV_FIELD_PROGRESSIVE;
502 break;499 break;
@@ -514,10 +511,10 @@ static int avisynth_create_stream_video(AVFormatContext *s, AVStream *st) · 11 unmodified lines
514 511
515 /* Color Range */512 /* Color Range */
516 if(avs->flags & AVISYNTH_FRAMEPROP_RANGE) {513 if(avs->flags & AVISYNTH_FRAMEPROP_RANGE) {
517 if(avs_library.avs_prop_get_type(avs->env, avsmap, "_ColorRange") == AVS_PROPTYPE_UNSET) {514 if(avs->avs_library.avs_prop_get_type(avs->env, avsmap, "_ColorRange") == AVS_PROPTYPE_UNSET) {
518 st->codecpar->color_range = AVCOL_RANGE_UNSPECIFIED;515 st->codecpar->color_range = AVCOL_RANGE_UNSPECIFIED;
519 } else {516 } else {
520 switch (avs_library.avs_prop_get_int(avs->env, avsmap, "_ColorRange", 0, &error)) {517 switch (avs->avs_library.avs_prop_get_int(avs->env, avsmap, "_ColorRange", 0, &error)) {
521 case 0:518 case 0:
522 st->codecpar->color_range = AVCOL_RANGE_JPEG;519 st->codecpar->color_range = AVCOL_RANGE_JPEG;
523 break;520 break;
@@ -532,7 +529,7 @@ static int avisynth_create_stream_video(AVFormatContext *s, AVStream *st) · 8 unmodified lines
532 529
533 /* Color Primaries */530 /* Color Primaries */
534 if(avs->flags & AVISYNTH_FRAMEPROP_PRIMARIES) {531 if(avs->flags & AVISYNTH_FRAMEPROP_PRIMARIES) {
535 switch (avs_library.avs_prop_get_int(avs->env, avsmap, "_Primaries", 0, &error)) {532 switch (avs->avs_library.avs_prop_get_int(avs->env, avsmap, "_Primaries", 0, &error)) {
536 case 1:533 case 1:
537 st->codecpar->color_primaries = AVCOL_PRI_BT709;534 st->codecpar->color_primaries = AVCOL_PRI_BT709;
538 break;535 break;
@@ -576,7 +573,7 @@ static int avisynth_create_stream_video(AVFormatContext *s, AVStream *st) · 37 unmodified lines
576 573
577 /* Color Transfer Characteristics */574 /* Color Transfer Characteristics */
578 if(avs->flags & AVISYNTH_FRAMEPROP_TRANSFER) {575 if(avs->flags & AVISYNTH_FRAMEPROP_TRANSFER) {
579 switch (avs_library.avs_prop_get_int(avs->env, avsmap, "_Transfer", 0, &error)) {576 switch (avs->avs_library.avs_prop_get_int(avs->env, avsmap, "_Transfer", 0, &error)) {
580 case 1:577 case 1:
581 st->codecpar->color_trc = AVCOL_TRC_BT709;578 st->codecpar->color_trc = AVCOL_TRC_BT709;
582 break;579 break;
@@ -635,10 +632,10 @@ static int avisynth_create_stream_video(AVFormatContext *s, AVStream *st) · 52 unmodified lines
635 632
636 /* Matrix coefficients */633 /* Matrix coefficients */
637 if(avs->flags & AVISYNTH_FRAMEPROP_MATRIX) {634 if(avs->flags & AVISYNTH_FRAMEPROP_MATRIX) {
638 if(avs_library.avs_prop_get_type(avs->env, avsmap, "_Matrix") == AVS_PROPTYPE_UNSET) {635 if(avs->avs_library.avs_prop_get_type(avs->env, avsmap, "_Matrix") == AVS_PROPTYPE_UNSET) {
639 st->codecpar->color_space = AVCOL_SPC_UNSPECIFIED;636 st->codecpar->color_space = AVCOL_SPC_UNSPECIFIED;
640 } else {637 } else {
641 switch (avs_library.avs_prop_get_int(avs->env, avsmap, "_Matrix", 0, &error)) {638 switch (avs->avs_library.avs_prop_get_int(avs->env, avsmap, "_Matrix", 0, &error)) {
642 case 0:639 case 0:
643 st->codecpar->color_space = AVCOL_SPC_RGB;640 st->codecpar->color_space = AVCOL_SPC_RGB;
644 break;641 break;
@@ -689,10 +686,10 @@ static int avisynth_create_stream_video(AVFormatContext *s, AVStream *st) · 44 unmodified lines
689 686
690 /* Chroma Location */687 /* Chroma Location */
691 if(avs->flags & AVISYNTH_FRAMEPROP_CHROMA_LOCATION) {688 if(avs->flags & AVISYNTH_FRAMEPROP_CHROMA_LOCATION) {
692 if(avs_library.avs_prop_get_type(avs->env, avsmap, "_ChromaLocation") == AVS_PROPTYPE_UNSET) {689 if(avs->avs_library.avs_prop_get_type(avs->env, avsmap, "_ChromaLocation") == AVS_PROPTYPE_UNSET) {
693 st->codecpar->chroma_location = AVCHROMA_LOC_UNSPECIFIED;690 st->codecpar->chroma_location = AVCHROMA_LOC_UNSPECIFIED;
694 } else {691 } else {
695 switch (avs_library.avs_prop_get_int(avs->env, avsmap, "_ChromaLocation", 0, &error)) {692 switch (avs->avs_library.avs_prop_get_int(avs->env, avsmap, "_ChromaLocation", 0, &error)) {
696 case 0:693 case 0:
697 st->codecpar->chroma_location = AVCHROMA_LOC_LEFT;694 st->codecpar->chroma_location = AVCHROMA_LOC_LEFT;
698 break;695 break;
@@ -719,12 +716,12 @@ static int avisynth_create_stream_video(AVFormatContext *s, AVStream *st) · 20 unmodified lines
719 716
720 /* Sample aspect ratio */717 /* Sample aspect ratio */
721 if(avs->flags & AVISYNTH_FRAMEPROP_SAR) {718 if(avs->flags & AVISYNTH_FRAMEPROP_SAR) {
722 sar_num = avs_library.avs_prop_get_int(avs->env, avsmap, "_SARNum", 0, &error);719 sar_num = avs->avs_library.avs_prop_get_int(avs->env, avsmap, "_SARNum", 0, &error);
723 sar_den = avs_library.avs_prop_get_int(avs->env, avsmap, "_SARDen", 0, &error);720 sar_den = avs->avs_library.avs_prop_get_int(avs->env, avsmap, "_SARDen", 0, &error);
724 st->sample_aspect_ratio = (AVRational){ sar_num, sar_den };721 st->sample_aspect_ratio = (AVRational){ sar_num, sar_den };
725 }722 }
726 723
727 avs_library.avs_release_video_frame(frame);724 avs->avs_library.avs_release_video_frame(frame);
728 } else {725 } else {
729 st->codecpar->field_order = AV_FIELD_UNKNOWN;726 st->codecpar->field_order = AV_FIELD_UNKNOWN;
730 /* AviSynth works with frame-based video, detecting field order can727 /* AviSynth works with frame-based video, detecting field order can
@@ -753,9 +750,9 @@ static int avisynth_create_stream_audio(AVFormatContext *s, AVStream *st) · 22 unmodified lines
753 st->duration = avs->vi->num_audio_samples;750 st->duration = avs->vi->num_audio_samples;
754 avpriv_set_pts_info(st, 64, 1, avs->vi->audio_samples_per_second);751 avpriv_set_pts_info(st, 64, 1, avs->vi->audio_samples_per_second);
755 752
756 if (avs_library.avs_get_version(avs->clip) >= 10)753 if (avs->avs_library.avs_get_version(avs->clip) >= 10)
757 av_channel_layout_from_mask(&st->codecpar->ch_layout,754 av_channel_layout_from_mask(&st->codecpar->ch_layout,
758 avs_library.avs_get_channel_mask(avs->vi));755 avs->avs_library.avs_get_channel_mask(avs->vi));
759 756
760 switch (avs->vi->sample_type) {757 switch (avs->vi->sample_type) {
761 case AVS_SAMPLE_INT8:758 case AVS_SAMPLE_INT8:
@@ -817,13 +814,13 @@ static int avisynth_open_file(AVFormatContext *s) · 55 unmodified lines
817 if (ret = avisynth_context_create(s))814 if (ret = avisynth_context_create(s))
818 return ret;815 return ret;
819 816
820 if (!avs_library.avs_check_version(avs->env, 7)) {817 if (!avs->avs_library.avs_check_version(avs->env, 7)) {
821 AVS_Value args[] = {818 AVS_Value args[] = {
822 avs_new_value_string(s->url),819 avs_new_value_string(s->url),
823 avs_new_value_bool(1) // filename is in UTF-8820 avs_new_value_bool(1) // filename is in UTF-8
824 };821 };
825 val = avs_library.avs_invoke(avs->env, "Import",822 val = avs->avs_library.avs_invoke(avs->env, "Import",
826 avs_new_value_array(args, 2), 0);823 avs_new_value_array(args, 2), 0);
827 } else {824 } else {
828 AVS_Value arg;825 AVS_Value arg;
829#ifdef _WIN32826#ifdef _WIN32
@@ -837,7 +834,7 @@ static int avisynth_open_file(AVFormatContext *s) · 7 unmodified lines
837#else834#else
838 arg = avs_new_value_string(s->url);835 arg = avs_new_value_string(s->url);
839#endif836#endif
840 val = avs_library.avs_invoke(avs->env, "Import", arg, 0);837 val = avs->avs_library.avs_invoke(avs->env, "Import", arg, 0);
841#ifdef _WIN32838#ifdef _WIN32
842 av_free(filename_ansi);839 av_free(filename_ansi);
843#endif840#endif
@@ -854,14 +851,14 @@ static int avisynth_open_file(AVFormatContext *s) · 10 unmodified lines
854 goto fail;851 goto fail;
855 }852 }
856 853
857 avs->clip = avs_library.avs_take_clip(val, avs->env);854 avs->clip = avs->avs_library.avs_take_clip(val, avs->env);
858 avs->vi = avs_library.avs_get_video_info(avs->clip);855 avs->vi = avs->avs_library.avs_get_video_info(avs->clip);
859 856
860 /* On Windows, FFmpeg supports AviSynth interface version 6 or higher.857 /* On Windows, FFmpeg supports AviSynth interface version 6 or higher.
861 * This includes AviSynth 2.6 RC1 or higher, and AviSynth+ r1718 or higher,858 * This includes AviSynth 2.6 RC1 or higher, and AviSynth+ r1718 or higher,
862 * and excludes 2.5 and the 2.6 alphas. */859 * and excludes 2.5 and the 2.6 alphas. */
863 860
864 if (avs_library.avs_get_version(avs->clip) < 6) {861 if (avs->avs_library.avs_get_version(avs->clip) < 6) {
865 av_log(s, AV_LOG_ERROR,862 av_log(s, AV_LOG_ERROR,
866 "AviSynth version is too old. Please upgrade to either AviSynth 2.6 >= RC1 or AviSynth+ >= r1718.\n");863 "AviSynth version is too old. Please upgrade to either AviSynth 2.6 >= RC1 or AviSynth+ >= r1718.\n");
867 ret = AVERROR_UNKNOWN;864 ret = AVERROR_UNKNOWN;
@@ -869,7 +866,7 @@ static int avisynth_open_file(AVFormatContext *s) · 1 unmodified line
869 }866 }
870 867
871 /* Release the AVS_Value as it will go out of scope. */868 /* Release the AVS_Value as it will go out of scope. */
872 avs_library.avs_release_value(val);869 avs->avs_library.avs_release_value(val);
873 870
874 if (ret = avisynth_create_stream(s))871 if (ret = avisynth_create_stream(s))
875 goto fail;872 goto fail;
@@ -917,7 +914,7 @@ static int avisynth_read_packet_video(AVFormatContext *s, AVPacket *pkt, · 41 unmodified lines
917 if (discard)914 if (discard)
918 return 0;915 return 0;
919 916
920 bits = avs_library.avs_bits_per_pixel(avs->vi);917 bits = avs->avs_library.avs_bits_per_pixel(avs->vi);
921 918
922 /* Without the cast to int64_t, calculation overflows at about 9k x 9k919 /* Without the cast to int64_t, calculation overflows at about 9k x 9k
923 * resolution. */920 * resolution. */
@@ -934,8 +931,8 @@ static int avisynth_read_packet_video(AVFormatContext *s, AVPacket *pkt, · 10 unmodified lines
934 pkt->duration = 1;931 pkt->duration = 1;
935 pkt->stream_index = avs->curr_stream;932 pkt->stream_index = avs->curr_stream;
936 933
937 frame = avs_library.avs_get_frame(avs->clip, n);934 frame = avs->avs_library.avs_get_frame(avs->clip, n);
938 error = avs_library.avs_clip_get_error(avs->clip);935 error = avs->avs_library.avs_clip_get_error(avs->clip);
939 if (error) {936 if (error) {
940 av_log(s, AV_LOG_ERROR, "%s\n", error);937 av_log(s, AV_LOG_ERROR, "%s\n", error);
941 avs->error = 1;938 avs->error = 1;
@@ -946,26 +943,26 @@ static int avisynth_read_packet_video(AVFormatContext *s, AVPacket *pkt, · 4 unmodified lines
946 dst_p = pkt->data;943 dst_p = pkt->data;
947 for (i = 0; i < avs->n_planes; i++) {944 for (i = 0; i < avs->n_planes; i++) {
948 plane = avs->planes[i];945 plane = avs->planes[i];
949 src_p = avs_library.avs_get_read_ptr_p(frame, plane);946 src_p = avs->avs_library.avs_get_read_ptr_p(frame, plane);
950 pitch = avs_library.avs_get_pitch_p(frame, plane);947 pitch = avs->avs_library.avs_get_pitch_p(frame, plane);
951 948
952 rowsize = avs_library.avs_get_row_size_p(frame, plane);949 rowsize = avs->avs_library.avs_get_row_size_p(frame, plane);
953 planeheight = avs_library.avs_get_height_p(frame, plane);950 planeheight = avs->avs_library.avs_get_height_p(frame, plane);
954 951
955 /* Flip RGB video. */952 /* Flip RGB video. */
956 if (avs_library.avs_is_color_space(avs->vi, AVS_CS_BGR) ||953 if (avs->avs_library.avs_is_color_space(avs->vi, AVS_CS_BGR) ||
957 avs_library.avs_is_color_space(avs->vi, AVS_CS_BGR48) ||954 avs->avs_library.avs_is_color_space(avs->vi, AVS_CS_BGR48) ||
958 avs_library.avs_is_color_space(avs->vi, AVS_CS_BGR64)) {955 avs->avs_library.avs_is_color_space(avs->vi, AVS_CS_BGR64)) {
959 src_p = src_p + (planeheight - 1) * pitch;956 src_p = src_p + (planeheight - 1) * pitch;
960 pitch = -pitch;957 pitch = -pitch;
961 }958 }
962 959
963 avs_library.avs_bit_blt(avs->env, dst_p, rowsize, src_p, pitch,960 avs->avs_library.avs_bit_blt(avs->env, dst_p, rowsize, src_p, pitch,
964 rowsize, planeheight);961 rowsize, planeheight);
965 dst_p += rowsize * planeheight;962 dst_p += rowsize * planeheight;
966 }963 }
967 964
968 avs_library.avs_release_video_frame(frame);965 avs->avs_library.avs_release_video_frame(frame);
969 return 0;966 return 0;
970}967}
971 968
@@ -1025,8 +1022,8 @@ static int avisynth_read_packet_audio(AVFormatContext *s, AVPacket *pkt, · 53 unmodified lines
1025 pkt->duration = samples;1022 pkt->duration = samples;
1026 pkt->stream_index = avs->curr_stream;1023 pkt->stream_index = avs->curr_stream;
1027 1024
1028 avs_library.avs_get_audio(avs->clip, pkt->data, n, samples);1025 avs->avs_library.avs_get_audio(avs->clip, pkt->data, n, samples);
1029 error = avs_library.avs_clip_get_error(avs->clip);1026 error = avs->avs_library.avs_clip_get_error(avs->clip);
1030 if (error) {1027 if (error) {
1031 av_log(s, AV_LOG_ERROR, "%s\n", error);1028 av_log(s, AV_LOG_ERROR, "%s\n", error);
1032 avs->error = 1;1029 avs->error = 1;
@@ -1040,16 +1037,9 @@ static av_cold int avisynth_read_header(AVFormatContext *s) · 7 unmodified lines
1040{1037{
1041 int ret;1038 int ret;
1042 1039
1043 // Calling library must implement a lock for thread-safe opens.1040 if (ret = avisynth_open_file(s))
1044 if (ff_mutex_lock(&avisynth_mutex))
1045 return AVERROR_UNKNOWN;
1046
1047 if (ret = avisynth_open_file(s)) {
1048 ff_mutex_unlock(&avisynth_mutex);
1049 return ret;1041 return ret;
1050 }
1051 1042
1052 ff_mutex_unlock(&avisynth_mutex);
1053 return 0;1043 return 0;
1054}1044}
1055 1045
@@ -1085,8 +1075,13 @@ static int avisynth_read_packet(AVFormatContext *s, AVPacket *pkt) · 29 unmodified lines
1085 1075
1086static av_cold int avisynth_read_close(AVFormatContext *s)1076static av_cold int avisynth_read_close(AVFormatContext *s)
1087{1077{
1088 avisynth_context_destroy(s->priv_data);1078 AviSynthContext *avs = s->priv_data;
1089 dlclose(avs_library.library);1079
1080 if (avs->avs_library.library) {
1081 avisynth_context_destroy(s->priv_data);
1082 dlclose(avs->avs_library.library);
1083 }
1084
1090 return 0;1085 return 0;
1091}1086}
1092 1087

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t