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
115115 int error;
116116
117117 uint32_t flags;
118+ struct AviSynthLibrary avs_library;
118119 } AviSynthContext;
119120
120121 static 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
128129 static const int avs_planes_rgba[4] = { AVS_PLANAR_G, AVS_PLANAR_B,
129130 AVS_PLANAR_R, AVS_PLANAR_A };
130131
131−static AVMutex avisynth_mutex = AV_MUTEX_INITIALIZER;
132−
133−static AviSynthLibrary avs_library;
134−
135−static av_cold int avisynth_load_library(void)
132+static av_cold int avisynth_load_library(AviSynthContext *avs)
136133 {
137− avs_library.library = dlopen(AVISYNTH_LIB, RTLD_NOW | RTLD_LOCAL);
138− if (!avs_library.library)
134+ avs->avs_library.library = dlopen(AVISYNTH_LIB, RTLD_NOW | RTLD_LOCAL);
135+ if (!avs->avs_library.library)
139136 return AVERROR_UNKNOWN;
140137
141138 #define LOAD_AVS_FUNC(name, continue_on_fail) \
142− avs_library.name = (name ## _func) \
143− dlsym(avs_library.library, #name); \
144− if (!continue_on_fail && !avs_library.name) \
139+ avs->avs_library.name = (name ## _func) \
140+ dlsym(avs->avs_library.library, #name); \
141+ if (!continue_on_fail && !avs->avs_library.name) \
145142 goto fail;
146143
147144 LOAD_AVS_FUNC(avs_bit_blt, 0);
@@ -177,7 +174,7 @@ static av_cold int avisynth_load_library(void) · 29 unmodified lines
177174 return 0;
178175
179176 fail:
180− dlclose(avs_library.library);
177+ dlclose(avs->avs_library.library);
181178 return AVERROR_UNKNOWN;
182179 }
183180
@@ -189,13 +186,13 @@ static av_cold int avisynth_context_create(AVFormatContext *s) · 5 unmodified lines
189186 AviSynthContext *avs = s->priv_data;
190187 int ret;
191188
192− if (!avs_library.library)
193− if (ret = avisynth_load_library())
189+ if (!avs->avs_library.library)
190+ if (ret = avisynth_load_library(avs))
194191 return ret;
195192
196− avs->env = avs_library.avs_create_script_environment(3);
197− if (avs_library.avs_get_error) {
198− const char *error = avs_library.avs_get_error(avs->env);
193+ avs->env = avs->avs_library.avs_create_script_environment(3);
194+ if (avs->avs_library.avs_get_error) {
195+ const char *error = avs->avs_library.avs_get_error(avs->env);
199196 if (error) {
200197 av_log(s, AV_LOG_ERROR, "%s\n", error);
201198 return AVERROR_UNKNOWN;
@@ -208,11 +205,11 @@ static av_cold int avisynth_context_create(AVFormatContext *s) · 6 unmodified lines
208205 static av_cold void avisynth_context_destroy(AviSynthContext *avs)
209206 {
210207 if (avs->clip) {
211− avs_library.avs_release_clip(avs->clip);
208+ avs->avs_library.avs_release_clip(avs->clip);
212209 avs->clip = NULL;
213210 }
214211 if (avs->env) {
215− avs_library.avs_delete_script_environment(avs->env);
212+ avs->avs_library.avs_delete_script_environment(avs->env);
216213 avs->env = NULL;
217214 }
218215 }
@@ -486,17 +483,17 @@ static int avisynth_create_stream_video(AVFormatContext *s, AVStream *st) · 267 unmodified lines
486483 * version 9 at the minimum. Technically, 8.1 works, but the time
487484 * distance between 8.1 and 9 is very small, so just restrict it to 9. */
488485
489− if (avs_library.avs_get_version(avs->clip) >= 9) {
486+ if (avs->avs_library.avs_get_version(avs->clip) >= 9) {
490487
491− frame = avs_library.avs_get_frame(avs->clip, 0);
492− avsmap = avs_library.avs_get_frame_props_ro(avs->env, frame);
488+ frame = avs->avs_library.avs_get_frame(avs->clip, 0);
489+ avsmap = avs->avs_library.avs_get_frame_props_ro(avs->env, frame);
493490
494491 /* Field order */
495492 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) {
497494 st->codecpar->field_order = AV_FIELD_UNKNOWN;
498495 } 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)) {
500497 case 0:
501498 st->codecpar->field_order = AV_FIELD_PROGRESSIVE;
502499 break;
@@ -514,10 +511,10 @@ static int avisynth_create_stream_video(AVFormatContext *s, AVStream *st) · 11 unmodified lines
514511
515512 /* Color Range */
516513 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) {
518515 st->codecpar->color_range = AVCOL_RANGE_UNSPECIFIED;
519516 } 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)) {
521518 case 0:
522519 st->codecpar->color_range = AVCOL_RANGE_JPEG;
523520 break;
@@ -532,7 +529,7 @@ static int avisynth_create_stream_video(AVFormatContext *s, AVStream *st) · 8 unmodified lines
532529
533530 /* Color Primaries */
534531 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)) {
536533 case 1:
537534 st->codecpar->color_primaries = AVCOL_PRI_BT709;
538535 break;
@@ -576,7 +573,7 @@ static int avisynth_create_stream_video(AVFormatContext *s, AVStream *st) · 37 unmodified lines
576573
577574 /* Color Transfer Characteristics */
578575 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)) {
580577 case 1:
581578 st->codecpar->color_trc = AVCOL_TRC_BT709;
582579 break;
@@ -635,10 +632,10 @@ static int avisynth_create_stream_video(AVFormatContext *s, AVStream *st) · 52 unmodified lines
635632
636633 /* Matrix coefficients */
637634 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) {
639636 st->codecpar->color_space = AVCOL_SPC_UNSPECIFIED;
640637 } 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)) {
642639 case 0:
643640 st->codecpar->color_space = AVCOL_SPC_RGB;
644641 break;
@@ -689,10 +686,10 @@ static int avisynth_create_stream_video(AVFormatContext *s, AVStream *st) · 44 unmodified lines
689686
690687 /* Chroma Location */
691688 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) {
693690 st->codecpar->chroma_location = AVCHROMA_LOC_UNSPECIFIED;
694691 } 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)) {
696693 case 0:
697694 st->codecpar->chroma_location = AVCHROMA_LOC_LEFT;
698695 break;
@@ -719,12 +716,12 @@ static int avisynth_create_stream_video(AVFormatContext *s, AVStream *st) · 20 unmodified lines
719716
720717 /* Sample aspect ratio */
721718 if(avs->flags & AVISYNTH_FRAMEPROP_SAR) {
722− sar_num = 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);
719+ sar_num = avs->avs_library.avs_prop_get_int(avs->env, avsmap, "_SARNum", 0, &error);
720+ sar_den = avs->avs_library.avs_prop_get_int(avs->env, avsmap, "_SARDen", 0, &error);
724721 st->sample_aspect_ratio = (AVRational){ sar_num, sar_den };
725722 }
726723
727− avs_library.avs_release_video_frame(frame);
724+ avs->avs_library.avs_release_video_frame(frame);
728725 } else {
729726 st->codecpar->field_order = AV_FIELD_UNKNOWN;
730727 /* 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
753750 st->duration = avs->vi->num_audio_samples;
754751 avpriv_set_pts_info(st, 64, 1, avs->vi->audio_samples_per_second);
755752
756− if (avs_library.avs_get_version(avs->clip) >= 10)
753+ if (avs->avs_library.avs_get_version(avs->clip) >= 10)
757754 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));
759756
760757 switch (avs->vi->sample_type) {
761758 case AVS_SAMPLE_INT8:
@@ -817,13 +814,13 @@ static int avisynth_open_file(AVFormatContext *s) · 55 unmodified lines
817814 if (ret = avisynth_context_create(s))
818815 return ret;
819816
820− if (!avs_library.avs_check_version(avs->env, 7)) {
817+ if (!avs->avs_library.avs_check_version(avs->env, 7)) {
821818 AVS_Value args[] = {
822819 avs_new_value_string(s->url),
823820 avs_new_value_bool(1) // filename is in UTF-8
824821 };
825− val = avs_library.avs_invoke(avs->env, "Import",
826− avs_new_value_array(args, 2), 0);
822+ val = avs->avs_library.avs_invoke(avs->env, "Import",
823+ avs_new_value_array(args, 2), 0);
827824 } else {
828825 AVS_Value arg;
829826 #ifdef _WIN32
@@ -837,7 +834,7 @@ static int avisynth_open_file(AVFormatContext *s) · 7 unmodified lines
837834 #else
838835 arg = avs_new_value_string(s->url);
839836 #endif
840− val = avs_library.avs_invoke(avs->env, "Import", arg, 0);
837+ val = avs->avs_library.avs_invoke(avs->env, "Import", arg, 0);
841838 #ifdef _WIN32
842839 av_free(filename_ansi);
843840 #endif
@@ -854,14 +851,14 @@ static int avisynth_open_file(AVFormatContext *s) · 10 unmodified lines
854851 goto fail;
855852 }
856853
857− avs->clip = avs_library.avs_take_clip(val, avs->env);
858− avs->vi = avs_library.avs_get_video_info(avs->clip);
854+ avs->clip = avs->avs_library.avs_take_clip(val, avs->env);
855+ avs->vi = avs->avs_library.avs_get_video_info(avs->clip);
859856
860857 /* On Windows, FFmpeg supports AviSynth interface version 6 or higher.
861858 * This includes AviSynth 2.6 RC1 or higher, and AviSynth+ r1718 or higher,
862859 * and excludes 2.5 and the 2.6 alphas. */
863860
864− if (avs_library.avs_get_version(avs->clip) < 6) {
861+ if (avs->avs_library.avs_get_version(avs->clip) < 6) {
865862 av_log(s, AV_LOG_ERROR,
866863 "AviSynth version is too old. Please upgrade to either AviSynth 2.6 >= RC1 or AviSynth+ >= r1718.\n");
867864 ret = AVERROR_UNKNOWN;
@@ -869,7 +866,7 @@ static int avisynth_open_file(AVFormatContext *s) · 1 unmodified line
869866 }
870867
871868 /* 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);
873870
874871 if (ret = avisynth_create_stream(s))
875872 goto fail;
@@ -917,7 +914,7 @@ static int avisynth_read_packet_video(AVFormatContext *s, AVPacket *pkt, · 41 unmodified lines
917914 if (discard)
918915 return 0;
919916
920− bits = avs_library.avs_bits_per_pixel(avs->vi);
917+ bits = avs->avs_library.avs_bits_per_pixel(avs->vi);
921918
922919 /* Without the cast to int64_t, calculation overflows at about 9k x 9k
923920 * resolution. */
@@ -934,8 +931,8 @@ static int avisynth_read_packet_video(AVFormatContext *s, AVPacket *pkt, · 10 unmodified lines
934931 pkt->duration = 1;
935932 pkt->stream_index = avs->curr_stream;
936933
937− frame = avs_library.avs_get_frame(avs->clip, n);
938− error = avs_library.avs_clip_get_error(avs->clip);
934+ frame = avs->avs_library.avs_get_frame(avs->clip, n);
935+ error = avs->avs_library.avs_clip_get_error(avs->clip);
939936 if (error) {
940937 av_log(s, AV_LOG_ERROR, "%s\n", error);
941938 avs->error = 1;
@@ -946,26 +943,26 @@ static int avisynth_read_packet_video(AVFormatContext *s, AVPacket *pkt, · 4 unmodified lines
946943 dst_p = pkt->data;
947944 for (i = 0; i < avs->n_planes; i++) {
948945 plane = avs->planes[i];
949− src_p = avs_library.avs_get_read_ptr_p(frame, plane);
950− pitch = avs_library.avs_get_pitch_p(frame, plane);
946+ src_p = avs->avs_library.avs_get_read_ptr_p(frame, plane);
947+ pitch = avs->avs_library.avs_get_pitch_p(frame, plane);
951948
952− rowsize = avs_library.avs_get_row_size_p(frame, plane);
953− planeheight = avs_library.avs_get_height_p(frame, plane);
949+ rowsize = avs->avs_library.avs_get_row_size_p(frame, plane);
950+ planeheight = avs->avs_library.avs_get_height_p(frame, plane);
954951
955952 /* Flip RGB video. */
956− if (avs_library.avs_is_color_space(avs->vi, AVS_CS_BGR) ||
957− avs_library.avs_is_color_space(avs->vi, AVS_CS_BGR48) ||
958− avs_library.avs_is_color_space(avs->vi, AVS_CS_BGR64)) {
953+ if (avs->avs_library.avs_is_color_space(avs->vi, AVS_CS_BGR) ||
954+ avs->avs_library.avs_is_color_space(avs->vi, AVS_CS_BGR48) ||
955+ avs->avs_library.avs_is_color_space(avs->vi, AVS_CS_BGR64)) {
959956 src_p = src_p + (planeheight - 1) * pitch;
960957 pitch = -pitch;
961958 }
962959
963− avs_library.avs_bit_blt(avs->env, dst_p, rowsize, src_p, pitch,
964− rowsize, planeheight);
960+ avs->avs_library.avs_bit_blt(avs->env, dst_p, rowsize, src_p, pitch,
961+ rowsize, planeheight);
965962 dst_p += rowsize * planeheight;
966963 }
967964
968− avs_library.avs_release_video_frame(frame);
965+ avs->avs_library.avs_release_video_frame(frame);
969966 return 0;
970967 }
971968
@@ -1025,8 +1022,8 @@ static int avisynth_read_packet_audio(AVFormatContext *s, AVPacket *pkt, · 53 unmodified lines
10251022 pkt->duration = samples;
10261023 pkt->stream_index = avs->curr_stream;
10271024
1028− avs_library.avs_get_audio(avs->clip, pkt->data, n, samples);
1029− error = avs_library.avs_clip_get_error(avs->clip);
1025+ avs->avs_library.avs_get_audio(avs->clip, pkt->data, n, samples);
1026+ error = avs->avs_library.avs_clip_get_error(avs->clip);
10301027 if (error) {
10311028 av_log(s, AV_LOG_ERROR, "%s\n", error);
10321029 avs->error = 1;
@@ -1040,16 +1037,9 @@ static av_cold int avisynth_read_header(AVFormatContext *s) · 7 unmodified lines
10401037 {
10411038 int ret;
10421039
1043− // Calling library must implement a lock for thread-safe opens.
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);
1040+ if (ret = avisynth_open_file(s))
10491041 return ret;
1050− }
10511042
1052− ff_mutex_unlock(&avisynth_mutex);
10531043 return 0;
10541044 }
10551045
@@ -1085,8 +1075,13 @@ static int avisynth_read_packet(AVFormatContext *s, AVPacket *pkt) · 29 unmodified lines
10851075
10861076 static av_cold int avisynth_read_close(AVFormatContext *s)
10871077 {
1088− avisynth_context_destroy(s->priv_data);
1089− dlclose(avs_library.library);
1078+ AviSynthContext *avs = s->priv_data;
1079+
1080+ if (avs->avs_library.library) {
1081+ avisynth_context_destroy(s->priv_data);
1082+ dlclose(avs->avs_library.library);
1083+ }
1084+
10901085 return 0;
10911086 }
10921087

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