[FFmpeg-devel] [PATCH 2/4] Use new av_dict_set_int helper function.

Reimar Döffinger Reimar.Doeffinger at gmx.de
Mon Aug 11 21:17:17 CEST 2014


Get rid of the many, slightly differing, implementations
of basically the same thing.

Signed-off-by: Reimar Döffinger <Reimar.Doeffinger at gmx.de>
---
 ffmpeg_opt.c                     | 12 +++---------
 ffplay.c                         |  2 +-
 libavfilter/vf_bbox.c            |  4 +---
 libavfilter/vf_cropdetect.c      |  4 +---
 libavformat/cinedec.c            |  4 +---
 libavformat/ftp.c                | 10 ++++------
 libavformat/hls.c                | 17 ++++-------------
 libavformat/id3v1.c              |  4 +---
 libavformat/matroskadec.c        | 31 +++++++------------------------
 libavformat/mlvdec.c             | 16 ++++------------
 libavformat/mov.c                | 14 +++-----------
 libavformat/mxfdec.c             |  8 ++------
 libavformat/smoothstreamingenc.c |  4 +---
 libavformat/vqf.c                |  6 +-----
 14 files changed, 34 insertions(+), 102 deletions(-)

diff --git a/ffmpeg_opt.c b/ffmpeg_opt.c
index 1962cf4..f738e27 100644
--- a/ffmpeg_opt.c
+++ b/ffmpeg_opt.c
@@ -785,7 +785,6 @@ static int open_input_file(OptionsContext *o, const char *filename)
     AVInputFormat *file_iformat = NULL;
     int err, i, ret;
     int64_t timestamp;
-    uint8_t buf[128];
     AVDictionary **opts;
     AVDictionary *unused_opts = NULL;
     AVDictionaryEntry *e = NULL;
@@ -814,8 +813,7 @@ static int open_input_file(OptionsContext *o, const char *filename)
         exit_program(1);
     }
     if (o->nb_audio_sample_rate) {
-        snprintf(buf, sizeof(buf), "%d", o->audio_sample_rate[o->nb_audio_sample_rate - 1].u.i);
-        av_dict_set(&o->g->format_opts, "sample_rate", buf, 0);
+        av_dict_set_int(&o->g->format_opts, "sample_rate", o->audio_sample_rate[o->nb_audio_sample_rate - 1].u.i, 0);
     }
     if (o->nb_audio_channels) {
         /* because we set audio_channels based on both the "ac" and
@@ -824,9 +822,7 @@ static int open_input_file(OptionsContext *o, const char *filename)
         if (file_iformat && file_iformat->priv_class &&
             av_opt_find(&file_iformat->priv_class, "channels", NULL, 0,
                         AV_OPT_SEARCH_FAKE_OBJ)) {
-            snprintf(buf, sizeof(buf), "%d",
-                     o->audio_channels[o->nb_audio_channels - 1].u.i);
-            av_dict_set(&o->g->format_opts, "channels", buf, 0);
+            av_dict_set_int(&o->g->format_opts, "channels", o->audio_channels[o->nb_audio_channels - 1].u.i, 0);
         }
     }
     if (o->nb_frame_rates) {
@@ -2058,9 +2054,7 @@ loop_end:
         assert_file_overwrite(filename);
 
     if (o->mux_preload) {
-        uint8_t buf[64];
-        snprintf(buf, sizeof(buf), "%d", (int)(o->mux_preload*AV_TIME_BASE));
-        av_dict_set(&of->opts, "preload", buf, 0);
+        av_dict_set_int(&of->opts, "preload", o->mux_preload*AV_TIME_BASE, 0);
     }
     oc->max_delay = (int)(o->mux_max_delay * AV_TIME_BASE);
 
diff --git a/ffplay.c b/ffplay.c
index e1e4ccc..662b7f9 100644
--- a/ffplay.c
+++ b/ffplay.c
@@ -2582,7 +2582,7 @@ static int stream_component_open(VideoState *is, int stream_index)
     if (!av_dict_get(opts, "threads", NULL, 0))
         av_dict_set(&opts, "threads", "auto", 0);
     if (stream_lowres)
-        av_dict_set(&opts, "lowres", av_asprintf("%d", stream_lowres), AV_DICT_DONT_STRDUP_VAL);
+        av_dict_set_int(&opts, "lowres", stream_lowres, 0);
     if (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
         av_dict_set(&opts, "refcounted_frames", "1", 0);
     if (avcodec_open2(avctx, codec, &opts) < 0)
diff --git a/libavfilter/vf_bbox.c b/libavfilter/vf_bbox.c
index 6c6aab1..1e6feff 100644
--- a/libavfilter/vf_bbox.c
+++ b/libavfilter/vf_bbox.c
@@ -61,8 +61,7 @@ static int query_formats(AVFilterContext *ctx)
 }
 
 #define SET_META(key, value) \
-    snprintf(buf, sizeof(buf), "%d", value);  \
-    av_dict_set(metadata, key, buf, 0);
+    av_dict_set_int(metadata, key, value, 0);
 
 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
 {
@@ -70,7 +69,6 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
     BBoxContext *bbox = ctx->priv;
     FFBoundingBox box;
     int has_bbox, w, h;
-    char buf[32];
 
     has_bbox =
         ff_calculate_bounding_box(&box,
diff --git a/libavfilter/vf_cropdetect.c b/libavfilter/vf_cropdetect.c
index ed8e7a8..f85a0bb 100644
--- a/libavfilter/vf_cropdetect.c
+++ b/libavfilter/vf_cropdetect.c
@@ -113,8 +113,7 @@ static int config_input(AVFilterLink *inlink)
 }
 
 #define SET_META(key, value) \
-    snprintf(buf, sizeof(buf), "%d", value);  \
-    av_dict_set(metadata, key, buf, 0)
+    av_dict_set_int(metadata, key, value, 0)
 
 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
 {
@@ -123,7 +122,6 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
     int bpp = s->max_pixsteps[0];
     int w, h, x, y, shrink_by;
     AVDictionary **metadata;
-    char buf[32];
 
     // ignore first 2 frames - they may be empty
     if (++s->frame_nb > 0) {
diff --git a/libavformat/cinedec.c b/libavformat/cinedec.c
index 3720884..7daf0e0 100644
--- a/libavformat/cinedec.c
+++ b/libavformat/cinedec.c
@@ -73,9 +73,7 @@ static int cine_read_probe(AVProbeData *p)
 static int set_metadata_int(AVDictionary **dict, const char *key, int value)
 {
     if (value) {
-        char buf[64];
-        snprintf(buf, sizeof(buf), "%i", value);
-        return av_dict_set(dict, key, buf, 0);
+        return av_dict_set_int(dict, key, value, 0);
     }
     return 0;
 }
diff --git a/libavformat/ftp.c b/libavformat/ftp.c
index 66c0665..9ee9b16 100644
--- a/libavformat/ftp.c
+++ b/libavformat/ftp.c
@@ -455,7 +455,7 @@ static int ftp_features(FTPContext *s)
 
 static int ftp_connect_control_connection(URLContext *h)
 {
-    char buf[CONTROL_BUFFER_SIZE], opts_format[20], *response = NULL;
+    char buf[CONTROL_BUFFER_SIZE], *response = NULL;
     int err;
     AVDictionary *opts = NULL;
     FTPContext *s = h->priv_data;
@@ -465,8 +465,7 @@ static int ftp_connect_control_connection(URLContext *h)
         ff_url_join(buf, sizeof(buf), "tcp", NULL,
                     s->hostname, s->server_control_port, NULL);
         if (s->rw_timeout != -1) {
-            snprintf(opts_format, sizeof(opts_format), "%d", s->rw_timeout);
-            av_dict_set(&opts, "timeout", opts_format, 0);
+            av_dict_set_int(&opts, "timeout", s->rw_timeout, 0);
         } /* if option is not given, don't pass it and let tcp use its own default */
         err = ffurl_open(&s->conn_control, buf, AVIO_FLAG_READ_WRITE,
                          &h->interrupt_callback, &opts);
@@ -505,7 +504,7 @@ static int ftp_connect_control_connection(URLContext *h)
 static int ftp_connect_data_connection(URLContext *h)
 {
     int err;
-    char buf[CONTROL_BUFFER_SIZE], opts_format[20];
+    char buf[CONTROL_BUFFER_SIZE];
     AVDictionary *opts = NULL;
     FTPContext *s = h->priv_data;
 
@@ -519,8 +518,7 @@ static int ftp_connect_data_connection(URLContext *h)
         /* Open data connection */
         ff_url_join(buf, sizeof(buf), "tcp", NULL, s->hostname, s->server_data_port, NULL);
         if (s->rw_timeout != -1) {
-            snprintf(opts_format, sizeof(opts_format), "%d", s->rw_timeout);
-            av_dict_set(&opts, "timeout", opts_format, 0);
+            av_dict_set_int(&opts, "timeout", s->rw_timeout, 0);
         } /* if option is not given, don't pass it and let tcp use its own default */
         err = ffurl_open(&s->conn_data, buf, h->flags,
                          &h->interrupt_callback, &opts);
diff --git a/libavformat/hls.c b/libavformat/hls.c
index 2ea62c6..0c86461 100644
--- a/libavformat/hls.c
+++ b/libavformat/hls.c
@@ -919,14 +919,8 @@ static int open_input(HLSContext *c, struct playlist *pls)
     if (seg->size >= 0) {
         /* try to restrict the HTTP request to the part we want
          * (if this is in fact a HTTP request) */
-        char offset[24] = { 0 };
-        char end_offset[24] = { 0 };
-        snprintf(offset, sizeof(offset) - 1, "%"PRId64,
-                 seg->url_offset);
-        snprintf(end_offset, sizeof(end_offset) - 1, "%"PRId64,
-                 seg->url_offset + seg->size);
-        av_dict_set(&opts, "offset", offset, 0);
-        av_dict_set(&opts, "end_offset", end_offset, 0);
+        av_dict_set_int(&opts, "offset", seg->url_offset, 0);
+        av_dict_set_int(&opts, "end_offset", seg->url_offset + seg->size, 0);
     }
 
     av_log(pls->parent, AV_LOG_VERBOSE, "HLS request for url '%s', offset %"PRId64", playlist %d\n",
@@ -1397,15 +1391,12 @@ static int hls_read_header(AVFormatContext *s)
     /* Create a program for each variant */
     for (i = 0; i < c->n_variants; i++) {
         struct variant *v = c->variants[i];
-        char bitrate_str[20];
         AVProgram *program;
 
-        snprintf(bitrate_str, sizeof(bitrate_str), "%d", v->bandwidth);
-
         program = av_new_program(s, i);
         if (!program)
             goto fail;
-        av_dict_set(&program->metadata, "variant_bitrate", bitrate_str, 0);
+        av_dict_set_int(&program->metadata, "variant_bitrate", v->bandwidth, 0);
 
         for (j = 0; j < v->n_playlists; j++) {
             struct playlist *pls = v->playlists[j];
@@ -1419,7 +1410,7 @@ static int hls_read_header(AVFormatContext *s)
 
                 /* Set variant_bitrate for streams unique to this variant */
                 if (!is_shared && v->bandwidth)
-                    av_dict_set(&st->metadata, "variant_bitrate", bitrate_str, 0);
+                    av_dict_set_int(&st->metadata, "variant_bitrate", v->bandwidth, 0);
             }
         }
     }
diff --git a/libavformat/id3v1.c b/libavformat/id3v1.c
index d73adc7..0617a9c 100644
--- a/libavformat/id3v1.c
+++ b/libavformat/id3v1.c
@@ -203,7 +203,6 @@ static void get_string(AVFormatContext *s, const char *key,
  */
 static int parse_tag(AVFormatContext *s, const uint8_t *buf)
 {
-    char str[5];
     int genre;
 
     if (!(buf[0] == 'T' &&
@@ -216,8 +215,7 @@ static int parse_tag(AVFormatContext *s, const uint8_t *buf)
     get_string(s, "date",    buf + 93,  4);
     get_string(s, "comment", buf + 97, 30);
     if (buf[125] == 0 && buf[126] != 0) {
-        snprintf(str, sizeof(str), "%d", buf[126]);
-        av_dict_set(&s->metadata, "track", str, 0);
+        av_dict_set_int(&s->metadata, "track", buf[126], 0);
     }
     genre = buf[127];
     if (genre <= ID3v1_GENRE_MAX)
diff --git a/libavformat/matroskadec.c b/libavformat/matroskadec.c
index 31ee456..ae48884 100644
--- a/libavformat/matroskadec.c
+++ b/libavformat/matroskadec.c
@@ -3345,30 +3345,18 @@ static int webm_dash_manifest_cues(AVFormatContext *s)
     matroska_parse_cues(matroska);
 
     // cues start
-    buf = av_asprintf("%" PRId64, cues_start);
-    if (!buf) return AVERROR(ENOMEM);
-    av_dict_set(&s->streams[0]->metadata, CUES_START, buf, 0);
-    av_free(buf);
+    av_dict_set_int(&s->streams[0]->metadata, CUES_START, cues_start, 0);
 
     // cues end
-    buf = av_asprintf("%" PRId64, cues_end);
-    if (!buf) return AVERROR(ENOMEM);
-    av_dict_set(&s->streams[0]->metadata, CUES_END, buf, 0);
-    av_free(buf);
+    av_dict_set_int(&s->streams[0]->metadata, CUES_END, cues_end, 0);
 
     // bandwidth
     bandwidth = webm_dash_manifest_compute_bandwidth(s, cues_start);
     if (bandwidth < 0) return -1;
-    buf = av_asprintf("%" PRId64, bandwidth);
-    if (!buf) return AVERROR(ENOMEM);
-    av_dict_set(&s->streams[0]->metadata, BANDWIDTH, buf, 0);
-    av_free(buf);
+    av_dict_set_int(&s->streams[0]->metadata, BANDWIDTH, bandwidth, 0);
 
     // check if all clusters start with key frames
-    buf = av_asprintf("%d", webm_clusters_start_with_keyframe(s));
-    if (!buf) return AVERROR(ENOMEM);
-    av_dict_set(&s->streams[0]->metadata, CLUSTER_KEYFRAME, buf, 0);
-    av_free(buf);
+    av_dict_set_int(&s->streams[0]->metadata, CLUSTER_KEYFRAME, webm_clusters_start_with_keyframe(s), 0);
 
     // store cue point timestamps as a comma separated list for checking subsegment alignment in
     // the muxer. assumes that each timestamp cannot be more than 20 characters long.
@@ -3399,10 +3387,8 @@ static int webm_dash_manifest_read_header(AVFormatContext *s)
     }
 
     // initialization range
-    buf = av_asprintf("%" PRId64, avio_tell(s->pb) - 5); // 5 is the offset of Cluster ID.
-    if (!buf) return AVERROR(ENOMEM);
-    av_dict_set(&s->streams[0]->metadata, INITIALIZATION_RANGE, buf, 0);
-    av_free(buf);
+    // 5 is the offset of Cluster ID.
+    av_dict_set_int(&s->streams[0]->metadata, INITIALIZATION_RANGE, avio_tell(s->pb) - 5, 0);
 
     // basename of the file
     buf = strrchr(s->filename, '/');
@@ -3417,10 +3403,7 @@ static int webm_dash_manifest_read_header(AVFormatContext *s)
 
     // track number
     tracks = matroska->tracks.elem;
-    buf = av_asprintf("%" PRId64, tracks[0].num);
-    if (!buf) return AVERROR(ENOMEM);
-    av_dict_set(&s->streams[0]->metadata, TRACK_NUMBER, buf, 0);
-    av_free(buf);
+    av_dict_set_int(&s->streams[0]->metadata, TRACK_NUMBER, tracks[0].num, 0);
 
     // parse the cues and populate Cue related fields
     return webm_dash_manifest_cues(s);
diff --git a/libavformat/mlvdec.c b/libavformat/mlvdec.c
index 0ed1bfa..1855ea4 100644
--- a/libavformat/mlvdec.c
+++ b/libavformat/mlvdec.c
@@ -95,30 +95,22 @@ static void read_string(AVFormatContext *avctx, AVIOContext *pb, const char *tag
 
 static void read_uint8(AVFormatContext *avctx, AVIOContext *pb, const char *tag, const char *fmt)
 {
-    char value[4];
-    snprintf(value, sizeof(value), "%i", avio_r8(pb));
-    av_dict_set(&avctx->metadata, tag, value, 0);
+    av_dict_set_int(&avctx->metadata, tag, avio_r8(pb), 0);
 }
 
 static void read_uint16(AVFormatContext *avctx, AVIOContext *pb, const char *tag, const char *fmt)
 {
-    char value[8];
-    snprintf(value, sizeof(value), "%i", avio_rl16(pb));
-    av_dict_set(&avctx->metadata, tag, value, 0);
+    av_dict_set_int(&avctx->metadata, tag, avio_rl16(pb), 0);
 }
 
 static void read_uint32(AVFormatContext *avctx, AVIOContext *pb, const char *tag, const char *fmt)
 {
-    char value[16];
-    snprintf(value, sizeof(value), fmt, avio_rl32(pb));
-    av_dict_set(&avctx->metadata, tag, value, 0);
+    av_dict_set_int(&avctx->metadata, tag, avio_rl32(pb), 0);
 }
 
 static void read_uint64(AVFormatContext *avctx, AVIOContext *pb, const char *tag, const char *fmt)
 {
-    char value[32];
-    snprintf(value, sizeof(value), fmt, avio_rl64(pb));
-    av_dict_set(&avctx->metadata, tag, value, 0);
+    av_dict_set_int(&avctx->metadata, tag, avio_rl64(pb), 0);
 }
 
 static int scan_file(AVFormatContext *avctx, AVStream *vst, AVStream *ast, int file)
diff --git a/libavformat/mov.c b/libavformat/mov.c
index b2ef482..4074365 100644
--- a/libavformat/mov.c
+++ b/libavformat/mov.c
@@ -91,15 +91,12 @@ static int mov_metadata_track_or_disc_number(MOVContext *c, AVIOContext *pb,
 static int mov_metadata_int8_bypass_padding(MOVContext *c, AVIOContext *pb,
                                             unsigned len, const char *key)
 {
-    char buf[16];
-
     /* bypass padding bytes */
     avio_r8(pb);
     avio_r8(pb);
     avio_r8(pb);
 
-    snprintf(buf, sizeof(buf), "%d", avio_r8(pb));
-    av_dict_set(&c->fc->metadata, key, buf, 0);
+    av_dict_set_int(&c->fc->metadata, key, avio_r8(pb), 0);
 
     return 0;
 }
@@ -107,10 +104,7 @@ static int mov_metadata_int8_bypass_padding(MOVContext *c, AVIOContext *pb,
 static int mov_metadata_int8_no_padding(MOVContext *c, AVIOContext *pb,
                                         unsigned len, const char *key)
 {
-    char buf[16];
-
-    snprintf(buf, sizeof(buf), "%d", avio_r8(pb));
-    av_dict_set(&c->fc->metadata, key, buf, 0);
+    av_dict_set_int(&c->fc->metadata, key, avio_r8(pb), 0);
 
     return 0;
 }
@@ -730,7 +724,6 @@ static int mov_read_ftyp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
 {
     uint32_t minor_ver;
     int comp_brand_size;
-    char minor_ver_str[11]; /* 32 bit integer -> 10 digits + null */
     char* comp_brands_str;
     uint8_t type[5] = {0};
 
@@ -740,8 +733,7 @@ static int mov_read_ftyp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
     av_log(c->fc, AV_LOG_DEBUG, "ISO: File Type Major Brand: %.4s\n",(char *)&type);
     av_dict_set(&c->fc->metadata, "major_brand", type, 0);
     minor_ver = avio_rb32(pb); /* minor version */
-    snprintf(minor_ver_str, sizeof(minor_ver_str), "%"PRIu32"", minor_ver);
-    av_dict_set(&c->fc->metadata, "minor_version", minor_ver_str, 0);
+    av_dict_set_int(&c->fc->metadata, "minor_version", minor_ver, 0);
 
     comp_brand_size = atom.size - 8;
     if (comp_brand_size < 0)
diff --git a/libavformat/mxfdec.c b/libavformat/mxfdec.c
index 2fc1ce0..ed1cecb 100644
--- a/libavformat/mxfdec.c
+++ b/libavformat/mxfdec.c
@@ -1632,14 +1632,10 @@ static int mxf_parse_structural_metadata(MXFContext *mxf)
             }
             st->need_parsing = AVSTREAM_PARSE_HEADERS;
             if (material_track->sequence->origin) {
-                char material_origin[3];
-                snprintf(material_origin, sizeof(material_origin), "%d", material_track->sequence->origin);
-                av_dict_set(&st->metadata, "material_track_origin", material_origin, 0);
+                av_dict_set_int(&st->metadata, "material_track_origin", material_track->sequence->origin, 0);
             }
             if (source_track->sequence->origin) {
-                char source_origin[3];
-                snprintf(source_origin, sizeof(source_origin), "%d", source_track->sequence->origin);
-                av_dict_set(&st->metadata, "source_track_origin", source_origin, 0);
+                av_dict_set_int(&st->metadata, "source_track_origin", source_track->sequence->origin, 0);
             }
         } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
             container_ul = mxf_get_codec_ul(mxf_sound_essence_container_uls, essence_container_ul);
diff --git a/libavformat/smoothstreamingenc.c b/libavformat/smoothstreamingenc.c
index 9491005..0781a0a 100644
--- a/libavformat/smoothstreamingenc.c
+++ b/libavformat/smoothstreamingenc.c
@@ -315,7 +315,6 @@ static int ism_write_header(AVFormatContext *s)
         AVFormatContext *ctx;
         AVStream *st;
         AVDictionary *opts = NULL;
-        char buf[10];
 
         if (!s->streams[i]->codec->bit_rate) {
             av_log(s, AV_LOG_ERROR, "No bit rate set for stream %d\n", i);
@@ -351,8 +350,7 @@ static int ism_write_header(AVFormatContext *s)
             goto fail;
         }
 
-        snprintf(buf, sizeof(buf), "%d", c->lookahead_count);
-        av_dict_set(&opts, "ism_lookahead", buf, 0);
+        av_dict_set_int(&opts, "ism_lookahead", c->lookahead_count, 0);
         av_dict_set(&opts, "movflags", "frag_custom", 0);
         if ((ret = avformat_write_header(ctx, &opts)) < 0) {
              goto fail;
diff --git a/libavformat/vqf.c b/libavformat/vqf.c
index d1ea0b4..15e8246 100644
--- a/libavformat/vqf.c
+++ b/libavformat/vqf.c
@@ -144,11 +144,7 @@ static int vqf_read_header(AVFormatContext *s)
             break;
         case MKTAG('D','S','I','Z'): // size of compressed data
         {
-            char buf[8] = {0};
-            int size = avio_rb32(s->pb);
-
-            snprintf(buf, sizeof(buf), "%d", size);
-            av_dict_set(&s->metadata, "size", buf, 0);
+            av_dict_set_int(&s->metadata, "size", avio_rb32(s->pb), 0);
         }
             break;
         case MKTAG('Y','E','A','R'): // recording date
-- 
2.1.0.rc1



More information about the ffmpeg-devel mailing list