[FFmpeg-devel] [PATCH] avio: add avio_get_str as a replacement for get_strz

Anton Khirnov anton
Mon Feb 21 08:13:44 CET 2011


It's more flexible and compatible with avio_get_str16.
---
 libavformat/avio.h    |   16 +++++++++++++++-
 libavformat/aviobuf.c |   29 +++++++++++++++++++----------
 libavformat/cafdec.c  |    4 ++--
 libavformat/ffmdec.c  |    3 ++-
 libavformat/mov.c     |    2 +-
 5 files changed, 39 insertions(+), 15 deletions(-)

diff --git a/libavformat/avio.h b/libavformat/avio.h
index 060f06e..7684769 100644
--- a/libavformat/avio.h
+++ b/libavformat/avio.h
@@ -487,6 +487,14 @@ uint64_t get_le64(AVIOContext *s);
 unsigned int get_le16(AVIOContext *s);
 
 /**
+ * Read a UTF-8 string from pb. The reading will terminate when either
+ * a NULL character was encountered or maxlen bytes have been read.
+ *
+ * @return number of bytes read (is always <= maxlen).
+ */
+int avio_get_str(AVIOContext *pb, int maxlen, char *buf, int buflen);
+
+/**
  * Read a UTF-16 string from pb and convert it to UTF-8.
  * The reading will terminate when either a null or invalid character was
  * encountered or maxlen bytes have been read.
@@ -495,7 +503,13 @@ unsigned int get_le16(AVIOContext *s);
 int avio_get_str16le(AVIOContext *pb, int maxlen, char *buf, int buflen);
 int avio_get_str16be(AVIOContext *pb, int maxlen, char *buf, int buflen);
 
-char *get_strz(AVIOContext *s, char *buf, int maxlen);
+#if FF_API_OLD_AVIO
+/**
+ * @deprecated use avio_get_str instead
+ */
+attribute_deprecated char *get_strz(AVIOContext *s, char *buf, int maxlen);
+#endif
+
 unsigned int get_be16(AVIOContext *s);
 unsigned int get_be24(AVIOContext *s);
 unsigned int get_be32(AVIOContext *s);
diff --git a/libavformat/aviobuf.c b/libavformat/aviobuf.c
index 7b1f5ca..e911983 100644
--- a/libavformat/aviobuf.c
+++ b/libavformat/aviobuf.c
@@ -588,20 +588,13 @@ unsigned int get_be32(AVIOContext *s)
     return val;
 }
 
+#if FF_API_OLD_AVIO
 char *get_strz(AVIOContext *s, char *buf, int maxlen)
 {
-    int i = 0;
-    char c;
-
-    while ((c = get_byte(s))) {
-        if (i < maxlen-1)
-            buf[i++] = c;
-    }
-
-    buf[i] = 0; /* Ensure null terminated, but may be truncated */
-
+    avio_get_str(s, INT_MAX, buf, maxlen);
     return buf;
 }
+#endif
 
 int ff_get_line(AVIOContext *s, char *buf, int maxlen)
 {
@@ -618,6 +611,22 @@ int ff_get_line(AVIOContext *s, char *buf, int maxlen)
     return i;
 }
 
+int avio_get_str(AVIOContext *s, int maxlen, char *buf, int buflen)
+{
+    int ret = 0;
+
+    while (ret < maxlen) {
+        char c = get_byte(s);
+        ret++;
+        if (!c)
+            break;
+        if (ret < buflen - 1)
+            *buf++ = c;
+    }
+    *buf = 0;
+    return ret;
+}
+
 #define GET_STR16(type, read) \
     int avio_get_str16 ##type(AVIOContext *pb, int maxlen, char *buf, int buflen)\
 {\
diff --git a/libavformat/cafdec.c b/libavformat/cafdec.c
index 27e29a2..876856f 100644
--- a/libavformat/cafdec.c
+++ b/libavformat/cafdec.c
@@ -185,8 +185,8 @@ static void read_info_chunk(AVFormatContext *s, int64_t size)
     for (i = 0; i < nb_entries; i++) {
         char key[32];
         char value[1024];
-        get_strz(pb, key, sizeof(key));
-        get_strz(pb, value, sizeof(value));
+        size -= avio_get_str(pb, size, key,   sizeof(key));
+        size -= avio_get_str(pb, size, value, sizeof(value));
         av_metadata_set2(&s->metadata, key, value, 0);
     }
 }
diff --git a/libavformat/ffmdec.c b/libavformat/ffmdec.c
index 7712090..281e105 100644
--- a/libavformat/ffmdec.c
+++ b/libavformat/ffmdec.c
@@ -325,7 +325,8 @@ static int ffm_read_header(AVFormatContext *s, AVFormatParameters *ap)
             codec->qcompress = get_be16(pb) / 10000.0;
             codec->qblur = get_be16(pb) / 10000.0;
             codec->bit_rate_tolerance = get_be32(pb);
-            codec->rc_eq = av_strdup(get_strz(pb, rc_eq_buf, sizeof(rc_eq_buf)));
+            avio_get_str(pb, INT_MAX, rc_eq_buf, sizeof(rc_eq_buf));
+            codec->rc_eq = av_strdup(rc_eq_buf);
             codec->rc_max_rate = get_be32(pb);
             codec->rc_min_rate = get_be32(pb);
             codec->rc_buffer_size = get_be32(pb);
diff --git a/libavformat/mov.c b/libavformat/mov.c
index 2c314e3..741d6c4 100644
--- a/libavformat/mov.c
+++ b/libavformat/mov.c
@@ -2331,7 +2331,7 @@ static void mov_read_chapters(AVFormatContext *s)
             avio_get_str16le(sc->pb, len, title, title_len);
         else {
             AV_WB16(title, ch);
-            get_strz(sc->pb, title + 2, len - 1);
+            avio_get_str(sc->pb, len - 2, title + 2, title_len - 2);
         }
 
         ff_new_chapter(s, i, st->time_base, sample->timestamp, end, title);
-- 
1.7.2.3




More information about the ffmpeg-devel mailing list