[FFmpeg-devel,2/3] avformat/movenc: add mov_write_int_metadata()

Submitted by Moritz Barsnick on Sept. 19, 2018, 1:58 p.m.

Details

Message ID 7c20364f6ad0f6fb75f3a362ea7da3026c37c53b.1537349317.git.barsnick@gmx.net
State New
Headers show

Commit Message

Moritz Barsnick Sept. 19, 2018, 1:58 p.m.
Converted from mov_write_int8_metadata(), allowing to also write
16-bit and 32-bit integer atoms, at up to 32-bit total size.
Converted mov_write_int8_metadata() into a macro specifying a
truncation of one byte length.

Signed-off-by: Moritz Barsnick <barsnick@gmx.net>
---
 libavformat/movenc.c | 22 ++++++++++++++--------
 1 file changed, 14 insertions(+), 8 deletions(-)

Patch hide | download patch | download mbox

diff --git a/libavformat/movenc.c b/libavformat/movenc.c
index 7c326faa49..0bb66e5fd1 100644
--- a/libavformat/movenc.c
+++ b/libavformat/movenc.c
@@ -3453,20 +3453,25 @@  static int mov_write_trkn_tag(AVIOContext *pb, MOVMuxContext *mov,
     return size;
 }
 
-static int mov_write_int8_metadata(AVFormatContext *s, AVIOContext *pb,
-                                   const char *name, const char *tag,
-                                   int len)
+#define mov_write_int8_metadata(a, b, c, d, e) mov_write_int_metadata(a, b, c, d, e, 1)
+static int mov_write_int_metadata(AVFormatContext *s, AVIOContext *pb,
+                                  const char *name, const char *tag,
+                                  int len, int truncate)
 {
     AVDictionaryEntry *t = NULL;
-    uint8_t num;
+    uint32_t num;
     int size = 24 + len;
 
     if (len != 1 && len != 2 && len != 4)
         return -1;
+    if (truncate > len)
+        return -1;
+    if (truncate != 1 && truncate != 2 && truncate != 4)
+        return -1;
 
     if (!(t = av_dict_get(s->metadata, tag, NULL, 0)))
         return 0;
-    num = atoi(t->value);
+    num = (uint32_t)strtoull(t->value, NULL, 10);
 
     avio_wb32(pb, size);
     ffio_wfourcc(pb, name);
@@ -3475,9 +3480,10 @@  static int mov_write_int8_metadata(AVFormatContext *s, AVIOContext *pb,
     avio_wb32(pb, 0x15);      // type specifier
     avio_wb32(pb, 0);
     // data:
-    if      (len == 4) avio_wb32(pb, num);
-    else if (len == 2) avio_wb16(pb, num);
-    else               avio_w8  (pb, num);
+    num &= (1U << (truncate*8)) - 1; // mask out the lower "truncate" number of bytes
+    if      (len == 4) avio_wb32(pb,           num);
+    else if (len == 2) avio_wb16(pb, (uint16_t)num);
+    else               avio_w8  (pb, (uint8_t) num);
 
     return size;
 }