Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
From: "Tomas Härdin" <git@haerdin.se>
To: FFmpeg development discussions and patches <ffmpeg-devel@ffmpeg.org>
Subject: [FFmpeg-devel] [PATCH 1/2] lavf/subtitles: Add ff_text_peek_r16(), only accept \r, \n, \r\n and \r\r\n line endings
Date: Sat, 30 Mar 2024 01:08:57 +0100
Message-ID: <b976905efa0ce3cd2837839dc116c9a8a82787ee.camel@haerdin.se> (raw)
In-Reply-To: <fcb0c046a815cef418627a75c0e70373533b9b25.camel@haerdin.se>

[-- Attachment #1: Type: text/plain, Size: 505 bytes --]

Here's an alternative first patch that rolls patch 1+3 into one. I'd
like some feedback on this before I continue hacking on patch 2. While
I don't like that we accept any old broken srt file, especially without
knowing what software made it, I'm not completely opposed to
compromising in this specific case. But I'd rather we didn't, and stuck
to \r, \n and \r\n. What I really don't want is runs of \r being eaten
without being "terminated" by a \n, because this messes up Mac support.

/Tomas

[-- Attachment #2: 0001-lavf-subtitles-Add-ff_text_peek_r16-only-accept-r-n-.patch --]
[-- Type: text/x-patch, Size: 3379 bytes --]

From 2ec68c51e4599b8493a2e103793f571451d872d3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tomas=20H=C3=A4rdin?= <git@haerdin.se>
Date: Thu, 28 Mar 2024 20:30:37 +0100
Subject: [PATCH 1/2] lavf/subtitles: Add ff_text_peek_r16(), only accept \r,
 \n, \r\n and \r\r\n line endings

---
 libavformat/subtitles.c | 53 +++++++++++++++++++++++++++++++++++++----
 libavformat/subtitles.h |  5 ++++
 2 files changed, 53 insertions(+), 5 deletions(-)

diff --git a/libavformat/subtitles.c b/libavformat/subtitles.c
index 3413763c7b..01187df6ab 100644
--- a/libavformat/subtitles.c
+++ b/libavformat/subtitles.c
@@ -22,6 +22,7 @@
 #include "subtitles.h"
 #include "avio_internal.h"
 #include "libavutil/avstring.h"
+#include "libavutil/intreadwrite.h"
 
 void ff_text_init_avio(void *s, FFTextReader *r, AVIOContext *pb)
 {
@@ -106,6 +107,42 @@ int ff_text_peek_r8(FFTextReader *r)
     return c;
 }
 
+int ff_text_peek_r16(FFTextReader *r)
+{
+    int c1, c2;
+    if (r->buf_pos < r->buf_len - 1)
+        return AV_RB16(&r->buf[r->buf_pos]);
+
+    // missing one or two bytes
+    c1 = ff_text_r8(r);
+    if (avio_feof(r->pb))
+        return 0;
+
+    if (r->buf_pos == r->buf_len - 1) {
+        // missing one byte
+        r->buf[0] = r->buf[r->buf_pos];
+        r->buf[1] = c1;
+        r->buf_pos = 0;
+        r->buf_len = 2;
+        return AV_RB16(r->buf);
+    }
+
+    // missing two bytes
+    c2 = ff_text_r8(r);
+    if (avio_feof(r->pb)) {
+        r->buf[0] = c1;
+        r->buf_pos = 0;
+        r->buf_len = 1;
+        return 0;
+    }
+
+    r->buf[0] = c1;
+    r->buf[1] = c2;
+    r->buf_pos = 0;
+    r->buf_len = 2;
+    return AV_RB16(r->buf);
+}
+
 AVPacket *ff_subtitles_queue_insert(FFDemuxSubtitlesQueue *q,
                                     const uint8_t *event, size_t len, int merge)
 {
@@ -446,11 +483,12 @@ int ff_subtitles_read_chunk(AVIOContext *pb, AVBPrint *buf)
 ptrdiff_t ff_subtitles_read_line(FFTextReader *tr, char *buf, size_t size)
 {
     size_t cur = 0;
+    unsigned char c;
     if (!size)
         return 0;
     buf[0] = '\0';
     while (cur + 1 < size) {
-        unsigned char c = ff_text_r8(tr);
+        c = ff_text_r8(tr);
         if (!c)
             return ff_text_eof(tr) ? cur : AVERROR_INVALIDDATA;
         if (c == '\r' || c == '\n')
@@ -458,9 +496,14 @@ ptrdiff_t ff_subtitles_read_line(FFTextReader *tr, char *buf, size_t size)
         buf[cur++] = c;
         buf[cur] = '\0';
     }
-    while (ff_text_peek_r8(tr) == '\r')
-        ff_text_r8(tr);
-    if (ff_text_peek_r8(tr) == '\n')
-        ff_text_r8(tr);
+    if (c == '\r') {
+        if (ff_text_peek_r8(tr) == '\n')
+            ff_text_r8(tr);
+        else if (ff_text_peek_r16(tr) == AV_RB16("\r\n")) {
+            // ticket5032-rrn.srt has \r\r\n
+            ff_text_r8(tr);
+            ff_text_r8(tr);
+        }
+    }
     return cur;
 }
diff --git a/libavformat/subtitles.h b/libavformat/subtitles.h
index 88665663c5..2a92044976 100644
--- a/libavformat/subtitles.h
+++ b/libavformat/subtitles.h
@@ -94,6 +94,11 @@ int ff_text_eof(FFTextReader *r);
  */
 int ff_text_peek_r8(FFTextReader *r);
 
+/**
+ * Like ff_text_peek_r8(), but peek two bytes and return them as a big-endian number.
+ */
+int ff_text_peek_r16(FFTextReader *r);
+
 /**
  * Read the given number of bytes (in UTF-8). On error or EOF, \0 bytes are
  * written.
-- 
2.39.2


[-- Attachment #3: Type: text/plain, Size: 251 bytes --]

_______________________________________________
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".

      parent reply	other threads:[~2024-03-30  0:09 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-28 22:55 [FFmpeg-devel] [PATCH 1/3] lavf/subtitles: Do not eat \n\n Tomas Härdin
2024-03-28 22:56 ` [FFmpeg-devel] [PATCH 2/3] lavf/srtdec: Permit streaming input Tomas Härdin
2024-03-28 22:57   ` Tomas Härdin
2024-03-29 23:35     ` Michael Niedermayer
2024-03-30  0:03       ` Tomas Härdin
2024-03-30  8:31       ` Tomas Härdin
2024-03-30 11:36         ` Paul B Mahol
2024-03-30 11:44         ` Nicolas George
2024-03-30 14:44           ` Tomas Härdin
2024-03-30 14:49             ` Nicolas George
2024-03-30 15:23               ` Tomas Härdin
2024-03-30 15:34                 ` Nicolas George
2024-03-30 16:02                 ` Andreas Rheinhardt
2024-03-30 16:28                   ` Tomas Härdin
2024-04-01 13:15                   ` arch1t3cht
2024-04-01 14:34                   ` Tomas Härdin
2024-03-28 22:57 ` [FFmpeg-devel] [PATCH 1/3] lavf/subtitles: Do not eat \n\n Tomas Härdin
2024-03-28 23:06 ` [FFmpeg-devel] [PATCH 3/3] lavf/subtitles: Unfix ticket #5032 Tomas Härdin
2024-03-29 12:29   ` Tomas Härdin
2024-03-30  0:08 ` Tomas Härdin [this message]

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=b976905efa0ce3cd2837839dc116c9a8a82787ee.camel@haerdin.se \
    --to=git@haerdin.se \
    --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