* [FFmpeg-devel] [PATCH] avformat/subtitles: check for double BOM in UTF-16 files
@ 2023-09-23 23:20 llyyr
0 siblings, 0 replies; only message in thread
From: llyyr @ 2023-09-23 23:20 UTC (permalink / raw)
To: ffmpeg-devel; +Cc: llyyr
While these files certainly aren't the norm, and might not even be
considered valid by many programs, there are plenty of older ASS tracks
in UTF-16 LE/BE encoding that contain double BOMs.
This patch teaches ff_text_init_avio about double BOMs and makes it
check for them in UTF-16 LE/BE files. This works by reading two more
bytes after the first BOM check, and seeking back if a second BOM
doesn't exist. If it does exist, we simply procede with buf_pos two
bytes ahead.
While this hack could certainly live in assdec.c, and would be much
simpler that way, there certainly isn't any harm in allowing other
subtitle format readers to be aware of double BOMs too.
---
libavformat/subtitles.c | 14 ++++++++++++++
1 file changed, 14 insertions(+)
diff --git a/libavformat/subtitles.c b/libavformat/subtitles.c
index 3413763c7b..a7b83cbb69 100644
--- a/libavformat/subtitles.c
+++ b/libavformat/subtitles.c
@@ -44,6 +44,20 @@ void ff_text_init_avio(void *s, FFTextReader *r, AVIOContext *pb)
r->buf_pos += 3;
}
}
+ if (r->type != FF_UTF_8) {
+ // Check for double BOM in UTF-16 LE/BE files
+ for (i = 0; i < 2; i++)
+ r->buf[r->buf_len++] = avio_r8(r->pb);
+ if (strncmp("\xFF\xFE\xFF\xFE", r->buf, 4) == 0 ||
+ strncmp("\xFE\xFF\xFE\xFF", r->buf, 4) == 0) {
+ // We did find a second BOM, so move buf_pos two bytes ahead
+ r->buf_pos += 2;
+ } else {
+ // We did not find a second BOM, undo the seek
+ r->buf_len -= 2;
+ avio_seek(r->pb, -2, SEEK_CUR); // Seek back two bytes
+ }
+ }
if (s && (r->type == FF_UTF16LE || r->type == FF_UTF16BE))
av_log(s, AV_LOG_INFO,
"UTF16 is automatically converted to UTF8, do not specify a character encoding\n");
--
2.42.0
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2023-09-23 23:21 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-09-23 23:20 [FFmpeg-devel] [PATCH] avformat/subtitles: check for double BOM in UTF-16 files llyyr
Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
This inbox may be cloned and mirrored by anyone:
git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git
# If you have public-inbox 1.1+ installed, you may
# initialize and index your mirror using the following commands:
public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
ffmpegdev@gitmailbox.com
public-inbox-index ffmpegdev
Example config snippet for mirrors.
AGPL code for this site: git clone https://public-inbox.org/public-inbox.git