[FFmpeg-cvslog] microdvd: sanitize AVPackets.

Clément Bœsch git at videolan.org
Mon Dec 31 00:44:31 CET 2012


ffmpeg | branch: master | Clément Bœsch <ubitux at gmail.com> | Sat Dec 29 23:26:36 2012 +0100| [1f265f52050e967d78e29d8e115c7dc849b14c4e] | committer: Clément Bœsch

microdvd: sanitize AVPackets.

Current MicroDVD AVPackets contain timing information and trailing line
breaks. The data is now only composed of the markup data. Doing this
consistently between text subtitles decoders allows to use different
codec for various formats. For instance, MicroDVD markup is sometimes
found in some VPlayer files. Also, generally speaking, the subtitles
text decoders have no use of these timings (and they must not use them
since it would break any user timing adjustment).

Technically, this is a major ABI break. In practice, a mismatching
lavf/lavc will now error out for MicroDVD decoding. Supporting both
formats requires unnecessary complex and fragile code.

FATE needs update because line breaks in the ASS file were "\n" (because
that's what is used in the original file). ASS format expect "\r\n" line
breaks; this commit fixes this issue. Also note that this "\r\n"
trailing need to be moved at some point from the decoders to the ASS
muxer.

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=1f265f52050e967d78e29d8e115c7dc849b14c4e
---

 libavcodec/microdvddec.c    |   18 +++++++++++++-----
 libavcodec/version.h        |    2 +-
 libavformat/microdvddec.c   |   23 ++++++++++++++++++-----
 libavformat/version.h       |    2 +-
 tests/ref/fate/sub-microdvd |    2 +-
 5 files changed, 34 insertions(+), 13 deletions(-)

diff --git a/libavcodec/microdvddec.c b/libavcodec/microdvddec.c
index e08cc31..f3c640f 100644
--- a/libavcodec/microdvddec.c
+++ b/libavcodec/microdvddec.c
@@ -260,6 +260,7 @@ static int microdvd_decode_frame(AVCodecContext *avctx,
 {
     AVSubtitle *sub = data;
     AVBPrint new_line;
+    char c;
     char *decoded_sub;
     char *line = avpkt->data;
     char *end = avpkt->data + avpkt->size;
@@ -268,11 +269,16 @@ static int microdvd_decode_frame(AVCodecContext *avctx,
     if (avpkt->size <= 0)
         return avpkt->size;
 
-    av_bprint_init(&new_line, 0, 2048);
+    /* To be removed later */
+    if (sscanf(line, "{%*d}{%*[0123456789]}%c", &c) == 1 &&
+        line[avpkt->size - 1] == '\n') {
+        av_log(avctx, AV_LOG_ERROR, "AVPacket is not clean (contains timing "
+               "information and a trailing line break). You need to upgrade "
+               "your libavformat or sanitize your packet.\n");
+        return AVERROR_INVALIDDATA;
+    }
 
-    // skip {frame_start}{frame_end}
-    line = strchr(line, '}'); if (!line) goto end; line++;
-    line = strchr(line, '}'); if (!line) goto end; line++;
+    av_bprint_init(&new_line, 0, 2048);
 
     // subtitle content
     while (line < end && *line) {
@@ -294,8 +300,9 @@ static int microdvd_decode_frame(AVCodecContext *avctx,
             line++;
         }
     }
+    if (new_line.len) {
+        av_bprintf(&new_line, "\r\n");
 
-end:
     av_bprint_finalize(&new_line, &decoded_sub);
     if (*decoded_sub) {
         int64_t start    = avpkt->pts;
@@ -306,6 +313,7 @@ end:
         ff_ass_add_rect(sub, decoded_sub, ts_start, ts_duration, 0);
     }
     av_free(decoded_sub);
+    }
 
     *got_sub_ptr = sub->num_rects > 0;
     return avpkt->size;
diff --git a/libavcodec/version.h b/libavcodec/version.h
index af58490..b4608a2 100644
--- a/libavcodec/version.h
+++ b/libavcodec/version.h
@@ -29,7 +29,7 @@
 #include "libavutil/avutil.h"
 
 #define LIBAVCODEC_VERSION_MAJOR 54
-#define LIBAVCODEC_VERSION_MINOR 84
+#define LIBAVCODEC_VERSION_MINOR 85
 #define LIBAVCODEC_VERSION_MICRO 100
 
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
diff --git a/libavformat/microdvddec.c b/libavformat/microdvddec.c
index 5d5b83f..96ffa3d 100644
--- a/libavformat/microdvddec.c
+++ b/libavformat/microdvddec.c
@@ -83,18 +83,19 @@ static int microdvd_read_header(AVFormatContext *s)
         return AVERROR(ENOMEM);
 
     while (!url_feof(s->pb)) {
+        char *p = line;
         AVPacket *sub;
         int64_t pos = avio_tell(s->pb);
         int len = ff_get_line(s->pb, line, sizeof(line));
 
         if (!len)
             break;
-        if (i < 3) {
+        line[strcspn(line, "\r\n")] = 0;
+        if (i++ < 3) {
             int frame;
             double fps;
             char c;
 
-            i++;
             if ((sscanf(line, "{%d}{}%6lf",    &frame, &fps) == 2 ||
                  sscanf(line, "{%d}{%*d}%6lf", &frame, &fps) == 2)
                 && frame <= 1 && fps > 3 && fps < 100)
@@ -107,12 +108,24 @@ static int microdvd_read_header(AVFormatContext *s)
                 continue;
             }
         }
-        sub = ff_subtitles_queue_insert(&microdvd->q, line, len, 0);
+#define SKIP_FRAME_ID                                       \
+    p = strchr(p, '}');                                     \
+    if (!p) {                                               \
+        av_log(s, AV_LOG_WARNING, "Invalid event \"%s\""    \
+               " at line %d\n", line, i);                   \
+        continue;                                           \
+    }                                                       \
+    p++
+        SKIP_FRAME_ID;
+        SKIP_FRAME_ID;
+        if (!*p)
+            continue;
+        sub = ff_subtitles_queue_insert(&microdvd->q, p, strlen(p), 0);
         if (!sub)
             return AVERROR(ENOMEM);
         sub->pos = pos;
-        sub->pts = get_pts(sub->data);
-        sub->duration = get_duration(sub->data);
+        sub->pts = get_pts(line);
+        sub->duration = get_duration(line);
     }
     ff_subtitles_queue_finalize(&microdvd->q);
     avpriv_set_pts_info(st, 64, pts_info.den, pts_info.num);
diff --git a/libavformat/version.h b/libavformat/version.h
index 90d47d0..6fb42d8 100644
--- a/libavformat/version.h
+++ b/libavformat/version.h
@@ -30,7 +30,7 @@
 #include "libavutil/avutil.h"
 
 #define LIBAVFORMAT_VERSION_MAJOR 54
-#define LIBAVFORMAT_VERSION_MINOR 56
+#define LIBAVFORMAT_VERSION_MINOR 57
 #define LIBAVFORMAT_VERSION_MICRO 100
 
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
diff --git a/tests/ref/fate/sub-microdvd b/tests/ref/fate/sub-microdvd
index 9fc10fb..2059989 100644
--- a/tests/ref/fate/sub-microdvd
+++ b/tests/ref/fate/sub-microdvd
@@ -1 +1 @@
-6356b8c53169aae6a20bce34d0f7be87
+35e133576aa3881d2de8dbf39a8d6df7



More information about the ffmpeg-cvslog mailing list