diff mbox

[FFmpeg-devel,11/12] lavf/sccdec: fix timestamps and demux one eai608 frame at a time

Message ID 20180704183514.71654-11-baptiste.coudurier@gmail.com
State New
Headers show

Commit Message

Baptiste Coudurier July 4, 2018, 6:35 p.m. UTC
---
 libavformat/sccdec.c | 100 ++++++++++++++++++++++++-------------------
 1 file changed, 56 insertions(+), 44 deletions(-)

Comments

Paul B Mahol Nov. 25, 2018, 8:21 p.m. UTC | #1
On 7/4/18, Baptiste Coudurier <baptiste.coudurier@gmail.com> wrote:
> ---
>  libavformat/sccdec.c | 100 ++++++++++++++++++++++++-------------------
>  1 file changed, 56 insertions(+), 44 deletions(-)
>

Will those scc patches be applied?
Paul B Mahol June 27, 2019, 6:23 p.m. UTC | #2
On 11/25/18, Paul B Mahol <onemda@gmail.com> wrote:
> On 7/4/18, Baptiste Coudurier <baptiste.coudurier@gmail.com> wrote:
>> ---
>>  libavformat/sccdec.c | 100 ++++++++++++++++++++++++-------------------
>>  1 file changed, 56 insertions(+), 44 deletions(-)
>>
>
> Will those scc patches be applied?
>

This one breaks FATE.
Forcing positive first timestamp drops initial captions.
diff mbox

Patch

diff --git a/libavformat/sccdec.c b/libavformat/sccdec.c
index 89d21b9c1f..0472b4e031 100644
--- a/libavformat/sccdec.c
+++ b/libavformat/sccdec.c
@@ -24,9 +24,14 @@ 
 #include "subtitles.h"
 #include "libavutil/bprint.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/timecode.h"
+#include "libavutil/opt.h"
 
 typedef struct SCCContext {
+    AVClass *av_class;
     FFDemuxSubtitlesQueue q;
+    AVTimecode initial_tc;
+    char *timecode_start;
 } SCCContext;
 
 static int scc_probe(AVProbeData *p)
@@ -62,57 +67,53 @@  static int scc_read_header(AVFormatContext *s)
 {
     SCCContext *scc = s->priv_data;
     AVStream *st = avformat_new_stream(s, NULL);
-    char line[4096], line2[4096];
-    int count = 0, ret = 0;
-    ptrdiff_t len2, len;
-    uint8_t out[4096];
+    char line[4096];
+    int ret = 0;
+    ptrdiff_t len;
     FFTextReader tr;
+    const AVRational frame_rate = {30000,1001};
 
     ff_text_init_avio(s, &tr, s->pb);
 
     if (!st)
         return AVERROR(ENOMEM);
-    avpriv_set_pts_info(st, 64, 1, 1000);
+    avpriv_set_pts_info(st, 64, 1001, 30000);
     st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
     st->codecpar->codec_id   = AV_CODEC_ID_EIA_608;
 
+    if (av_timecode_init_from_string(&scc->initial_tc,
+                                     (AVRational){30000, 1001},
+                                     scc->timecode_start, s) < 0)
+        return -1;
+
+    while (!ff_text_eof(&tr)) {
+        len = ff_subtitles_read_line(&tr, line, sizeof(line));
+        if (!strncmp(line, "Scenarist_SCC V1.0", 18))
+            break;
+    }
+
     while (!ff_text_eof(&tr)) {
         const int64_t pos = ff_text_pos(&tr);
         char *saveptr = NULL, *lline;
-        int hh1, mm1, ss1, fs1, i;
-        int hh2, mm2, ss2, fs2;
-        int64_t ts_start, ts_end;
+        AVTimecode tc_start;
+        int i;
         AVPacket *sub;
-
-        if (count == 0) {
-            while (!ff_text_eof(&tr)) {
-                len = ff_subtitles_read_line(&tr, line, sizeof(line));
-                if (len > 13)
-                    break;
-            }
-        }
-
-        if (!strncmp(line, "Scenarist_SCC V1.0", 18))
-            continue;
-        if (sscanf(line, "%d:%d:%d%*[:;]%d", &hh1, &mm1, &ss1, &fs1) != 4)
-            continue;
-
-        ts_start = (hh1 * 3600LL + mm1 * 60LL + ss1) * 1000LL + fs1 * 33;
+        char out[4];
 
         while (!ff_text_eof(&tr)) {
-            len2 = ff_subtitles_read_line(&tr, line2, sizeof(line2));
-            if (len2 > 13)
+            len = ff_subtitles_read_line(&tr, line, sizeof(line));
+            if (len > 13)
                 break;
         }
-        if (sscanf(line2, "%d:%d:%d%*[:;]%d", &hh2, &mm2, &ss2, &fs2) != 4)
-            continue;
 
-        ts_end = (hh2 * 3600LL + mm2 * 60LL + ss2) * 1000LL + fs2 * 33;
-        count++;
+        if (av_timecode_init_from_string(&tc_start, frame_rate, line, s) < 0)
+            continue;
 
         lline = (char *)&line;
         lline += 12;
 
+        out[3] = 0;
+
         for (i = 0; i < 4095; i += 3) {
             char *ptr = av_strtok(lline, " ", &saveptr);
             char c1, c2, c3, c4;
@@ -124,21 +125,17 @@  static int scc_read_header(AVFormatContext *s)
                 break;
 
             lline = NULL;
-            out[i+0] = 0xfc;
-            out[i+1] = convert(c2) | (convert(c1) << 4);
-            out[i+2] = convert(c4) | (convert(c3) << 4);
+            out[0] = 0xfc;
+            out[1] = convert(c2) | (convert(c1) << 4);
+            out[2] = convert(c4) | (convert(c3) << 4);
+
+            sub = ff_subtitles_queue_insert(&scc->q, out, 3, 0);
+            if (!sub)
+                return AVERROR(ENOMEM);
+            sub->pos = pos + i * 3;
+            sub->pts = tc_start.start + i / 3;
+            sub->duration = 1;
         }
-        out[i] = 0;
-
-        sub = ff_subtitles_queue_insert(&scc->q, out, i, 0);
-        if (!sub)
-            return AVERROR(ENOMEM);
-
-        sub->pos = pos;
-        sub->pts = ts_start;
-        sub->duration = FFMAX(1200, ts_end - ts_start);
-        memmove(line, line2, sizeof(line));
-        FFSWAP(ptrdiff_t, len, len2);
     }
 
     ff_subtitles_queue_finalize(s, &scc->q);
@@ -149,7 +146,8 @@  static int scc_read_header(AVFormatContext *s)
 static int scc_read_packet(AVFormatContext *s, AVPacket *pkt)
 {
     SCCContext *scc = s->priv_data;
-    return ff_subtitles_queue_read_packet(&scc->q, pkt);
+    int ret = ff_subtitles_queue_read_packet(&scc->q, pkt);
+    return ret;
 }
 
 static int scc_read_seek(AVFormatContext *s, int stream_index,
@@ -167,6 +165,19 @@  static int scc_read_close(AVFormatContext *s)
     return 0;
 }
 
+static const AVOption scc_options[] = {
+    { "timecode_start", "Set the SCC file initial timecode, to adjust timestamps",
+      offsetof(SCCContext, timecode_start), AV_OPT_TYPE_STRING, {.str = "00:00:00;00"}, CHAR_MIN, CHAR_MAX, AV_OPT_FLAG_ENCODING_PARAM},
+    { NULL },
+};
+
+static const AVClass scc_demuxer_class = {
+    .class_name     = "scc demuxer",
+    .item_name      = av_default_item_name,
+    .option         = scc_options,
+    .version        = LIBAVUTIL_VERSION_INT,
+};
+
 AVInputFormat ff_scc_demuxer = {
     .name           = "scc",
     .long_name      = NULL_IF_CONFIG_SMALL("Scenarist Closed Captions"),
@@ -177,4 +188,5 @@  AVInputFormat ff_scc_demuxer = {
     .read_seek2     = scc_read_seek,
     .read_close     = scc_read_close,
     .extensions     = "scc",
+    .priv_class     = &scc_demuxer_class,
 };