Skip to content

Commit

Permalink
fftools: Stop using av_fopen_utf8
Browse files Browse the repository at this point in the history
Provide a header based inline reimplementation of it.

Using av_fopen_utf8 doesn't work outside of the libraries when built
with MSVC as shared libraries (in the default configuration, where
each DLL gets a separate statically linked CRT).

Signed-off-by: Martin Storsjö <martin@martin.st>
  • Loading branch information
mstorsjo committed May 23, 2022
1 parent 1f9b5fa commit 3fb9244
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
3 changes: 2 additions & 1 deletion fftools/ffmpeg_opt.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#endif

#include "ffmpeg.h"
#include "fopen_utf8.h"
#include "cmdutils.h"
#include "opt_common.h"

Expand Down Expand Up @@ -1882,7 +1883,7 @@ static OutputStream *new_video_stream(OptionsContext *o, AVFormatContext *oc, in
video_enc->stats_in = logbuffer;
}
if (video_enc->flags & AV_CODEC_FLAG_PASS1) {
f = av_fopen_utf8(logfilename, "wb");
f = fopen_utf8(logfilename, "wb");
if (!f) {
av_log(NULL, AV_LOG_FATAL,
"Cannot write log file '%s' for pass-1 encoding: %s\n",
Expand Down
71 changes: 71 additions & 0 deletions fftools/fopen_utf8.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#ifndef FFTOOLS_FOPEN_UTF8_H
#define FFTOOLS_FOPEN_UTF8_H

#include <stdio.h>

/* The fopen_utf8 function here is essentially equivalent to av_fopen_utf8,
* except that it doesn't set O_CLOEXEC, and that it isn't exported
* from a different library. (On Windows, each DLL might use a different
* CRT, and FILE* handles can't be shared across them.) */

#ifdef _WIN32
#include "libavutil/wchar_filename.h"

static inline FILE *fopen_utf8(const char *path_utf8, const char *mode)
{
wchar_t *path_w, *mode_w;
FILE *f;

/* convert UTF-8 to wide chars */
if (utf8towchar(path_utf8, &path_w)) /* This sets errno on error. */
return NULL;
if (!path_w)
goto fallback;

if (utf8towchar(mode, &mode_w))
return NULL;
if (!mode_w) {
/* If failing to interpret the mode string as utf8, it is an invalid
* parameter. */
av_freep(&path_w);
errno = EINVAL;
return NULL;
}

f = _wfopen(path_w, mode_w);
av_freep(&path_w);
av_freep(&mode_w);

return f;
fallback:
/* path may be in CP_ACP */
return fopen(path_utf8, mode);
}

#else

static inline FILE *fopen_utf8(const char *path, const char *mode)
{
return fopen(path, mode);
}
#endif

#endif /* FFTOOLS_FOPEN_UTF8_H */

0 comments on commit 3fb9244

Please sign in to comment.