]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/ffmetadec.c
hwcontext_vulkan: dynamically load functions
[ffmpeg] / libavformat / ffmetadec.c
index 3290b3b7bca0e15b678d9c7519dc3c4548f77220..93ec6f589953ef90cb7f4f43439c77e5cefa7b27 100644 (file)
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/bprint.h"
 #include "libavutil/mathematics.h"
 #include "avformat.h"
 #include "ffmeta.h"
 #include "internal.h"
 #include "libavutil/dict.h"
 
-static int probe(AVProbeData *p)
+static int probe(const AVProbeData *p)
 {
     if(!memcmp(p->buf, ID_STRING, strlen(ID_STRING)))
         return AVPROBE_SCORE_MAX;
     return 0;
 }
 
+static int64_t read_line_to_bprint_escaped(AVIOContext *s, AVBPrint *bp)
+{
+    int len, end;
+    int64_t read = 0;
+    char tmp[1024];
+    char c;
+    char prev = ' ';
+
+    do {
+        len = 0;
+        do {
+            c = avio_r8(s);
+            end = prev != '\\' && (c == '\r' || c == '\n' || c == '\0');
+            if (!end)
+                tmp[len++] = c;
+            prev = c;
+        } while (!end && len < sizeof(tmp));
+        av_bprint_append_data(bp, tmp, len);
+        read += len;
+    } while (!end);
+
+    if (c == '\r' && avio_r8(s) != '\n' && !avio_feof(s))
+        avio_skip(s, -1);
+
+    if (!c && s->error)
+        return s->error;
+
+    if (!c && !read && avio_feof(s))
+        return AVERROR_EOF;
+
+    return read;
+}
+
+static void get_bprint_line(AVIOContext *s, AVBPrint *bp)
+{
+
+    do {
+        av_bprint_clear(bp);
+        read_line_to_bprint_escaped(s, bp);
+    } while (!avio_feof(s) && (bp->str[0] == ';' || bp->str[0] == '#' || bp->str[0] == 0));
+}
+
 static void get_line(AVIOContext *s, uint8_t *buf, int size)
 {
     do {
@@ -58,19 +101,22 @@ static AVChapter *read_chapter(AVFormatContext *s)
     uint8_t line[256];
     int64_t start, end;
     AVRational tb = {1, 1e9};
+    int ret;
 
     get_line(s->pb, line, sizeof(line));
 
     if (sscanf(line, "TIMEBASE=%d/%d", &tb.num, &tb.den))
         get_line(s->pb, line, sizeof(line));
-    if (!sscanf(line, "START=%"SCNd64, &start)) {
+    ret = sscanf(line, "START=%"SCNd64, &start);
+    if (ret <= 0) {
         av_log(s, AV_LOG_ERROR, "Expected chapter start timestamp, found %s.\n", line);
         start = (s->nb_chapters && s->chapters[s->nb_chapters - 1]->end != AV_NOPTS_VALUE) ?
                  s->chapters[s->nb_chapters - 1]->end : 0;
     } else
         get_line(s->pb, line, sizeof(line));
 
-    if (!sscanf(line, "END=%"SCNd64, &end)) {
+    ret = sscanf(line, "END=%"SCNd64, &end);
+    if (ret <= 0) {
         av_log(s, AV_LOG_ERROR, "Expected chapter end timestamp, found %s.\n", line);
         end = AV_NOPTS_VALUE;
     }
@@ -128,32 +174,36 @@ static int read_tag(const uint8_t *line, AVDictionary **m)
 static int read_header(AVFormatContext *s)
 {
     AVDictionary **m = &s->metadata;
-    uint8_t line[1024];
+    AVBPrint bp;
+
+    av_bprint_init(&bp, 0, AV_BPRINT_SIZE_UNLIMITED);
 
     while(!avio_feof(s->pb)) {
-        get_line(s->pb, line, sizeof(line));
+        get_bprint_line(s->pb, &bp);
 
-        if (!memcmp(line, ID_STREAM, strlen(ID_STREAM))) {
+        if (!memcmp(bp.str, ID_STREAM, strlen(ID_STREAM))) {
             AVStream *st = avformat_new_stream(s, NULL);
 
             if (!st)
-                return AVERROR(ENOMEM);
+                goto nomem;
 
             st->codecpar->codec_type = AVMEDIA_TYPE_DATA;
             st->codecpar->codec_id   = AV_CODEC_ID_FFMETADATA;
 
             m = &st->metadata;
-        } else if (!memcmp(line, ID_CHAPTER, strlen(ID_CHAPTER))) {
+        } else if (!memcmp(bp.str, ID_CHAPTER, strlen(ID_CHAPTER))) {
             AVChapter *ch = read_chapter(s);
 
             if (!ch)
-                return AVERROR(ENOMEM);
+                goto nomem;
 
             m = &ch->metadata;
         } else
-            read_tag(line, m);
+            read_tag(bp.str, m);
     }
 
+    av_bprint_finalize(&bp, NULL);
+
     s->start_time = 0;
     if (s->nb_chapters)
         s->duration = av_rescale_q(s->chapters[s->nb_chapters - 1]->end,
@@ -161,6 +211,10 @@ static int read_header(AVFormatContext *s)
                                    AV_TIME_BASE_Q);
 
     return 0;
+nomem:
+    av_bprint_finalize(&bp, NULL);
+
+    return AVERROR(ENOMEM);
 }
 
 static int read_packet(AVFormatContext *s, AVPacket *pkt)
@@ -168,7 +222,7 @@ static int read_packet(AVFormatContext *s, AVPacket *pkt)
     return AVERROR_EOF;
 }
 
-AVInputFormat ff_ffmetadata_demuxer = {
+const AVInputFormat ff_ffmetadata_demuxer = {
     .name        = "ffmetadata",
     .long_name   = NULL_IF_CONFIG_SMALL("FFmpeg metadata in text"),
     .read_probe  = probe,