From: llyyr <llyyr.public@gmail.com> To: ffmpeg-devel@ffmpeg.org Cc: llyyr <llyyr.public@gmail.com> Subject: [FFmpeg-devel] [PATCH] avformat/subtitles: check for double BOM in UTF-16 files Date: Sun, 24 Sep 2023 04:50:49 +0530 Message-ID: <20230923232049.14119-1-llyyr.public@gmail.com> (raw) 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".
reply other threads:[~2023-09-23 23:21 UTC|newest] Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20230923232049.14119-1-llyyr.public@gmail.com \ --to=llyyr.public@gmail.com \ --cc=ffmpeg-devel@ffmpeg.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
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