]> git.sesse.net Git - ffmpeg/commitdiff
avformat/yuv4mpegenc: Simplify writing global and packet headers
authorAndreas Rheinhardt <andreas.rheinhardt@gmail.com>
Fri, 4 Sep 2020 10:36:41 +0000 (12:36 +0200)
committerAndreas Rheinhardt <andreas.rheinhardt@gmail.com>
Fri, 4 Sep 2020 15:34:26 +0000 (17:34 +0200)
YUV4MPEG writes a string as header for both the file itself as well as
for every frame; these strings contain magic strings and these were up
until now included in the string to write via %s. Yet they are compile
time constants, so one can use the compile-time string concatentation
instead of inserting these strings at runtime.
Furthermore, the global header has been written via snprintf() to
a local buffer first before writing it. This can be simplified by using
avio_printf().

Reviewed-by: Paul B Mahol <onemda@gmail.com>
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
libavformat/yuv4mpegenc.c

index c4781042bd2f88c110b2b1d55643876f8ddd0ab4..fdd020e13b0e2b5f11885d998ad2abf26185a0c5 100644 (file)
 #include "internal.h"
 #include "yuv4mpeg.h"
 
-#define Y4M_LINE_MAX 256
-
 static int yuv4_write_header(AVFormatContext *s)
 {
     AVStream *st;
     AVIOContext *pb = s->pb;
     int width, height;
-    int raten, rated, aspectn, aspectd, n;
+    int raten, rated, aspectn, aspectd, ret;
     char inter;
     const char *colorspace = "";
     const char *colorrange = "";
-    char buf[Y4M_LINE_MAX + 1];
     int field_order;
 
     st     = s->streams[0];
@@ -170,19 +167,15 @@ static int yuv4_write_header(AVFormatContext *s)
         break;
     }
 
-    /* construct stream header, if this is the first frame */
-    n = snprintf(buf, Y4M_LINE_MAX, "%s W%d H%d F%d:%d I%c A%d:%d%s%s\n",
-                 Y4M_MAGIC, width, height, raten, rated, inter,
-                 aspectn, aspectd, colorspace, colorrange);
-
-    if (n < 0) {
+    ret = avio_printf(pb, Y4M_MAGIC " W%d H%d F%d:%d I%c A%d:%d%s%s\n",
+                      width, height, raten, rated, inter,
+                      aspectn, aspectd, colorspace, colorrange);
+    if (ret < 0) {
         av_log(s, AV_LOG_ERROR,
                "Error. YUV4MPEG stream header write failed.\n");
-        return AVERROR(EIO);
+        return ret;
     }
 
-    avio_write(pb, buf, strlen(buf));
-
     return 0;
 }
 
@@ -200,7 +193,7 @@ static int yuv4_write_packet(AVFormatContext *s, AVPacket *pkt)
 
     /* construct frame header */
 
-    avio_printf(s->pb, "%s\n", Y4M_FRAME_MAGIC);
+    avio_printf(s->pb, Y4M_FRAME_MAGIC "\n");
 
     width  = st->codecpar->width;
     height = st->codecpar->height;