2 * This file is part of Libav.
4 * Libav is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * Libav is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with Libav; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 * replaygain tags parsing
28 #include "libavutil/avstring.h"
29 #include "libavutil/dict.h"
30 #include "libavutil/intreadwrite.h"
31 #include "libavutil/mathematics.h"
32 #include "libavutil/mem.h"
33 #include "libavutil/replaygain.h"
36 #include "replaygain.h"
38 static int32_t parse_value(const char *value, int32_t min)
49 value += strspn(value, " \t");
54 db = strtol(value, &fraction, 0);
55 if (*fraction++ == '.') {
56 while (av_isdigit(*fraction) && scale) {
57 mb += scale * (*fraction - '0');
63 if (abs(db) > (INT32_MAX - mb) / 100000)
66 return db * 100000 + sign * mb;
69 int ff_replaygain_export_raw(AVStream *st, int32_t tg, uint32_t tp,
70 int32_t ag, uint32_t ap)
72 AVPacketSideData *sd, *tmp;
73 AVReplayGain *replaygain;
75 if (tg == INT32_MIN && ag == INT32_MIN)
78 for (int i = 0; i < st->nb_side_data; i++) {
79 AVPacketSideData *src_sd = &st->side_data[i];
81 if (src_sd->type == AV_PKT_DATA_REPLAYGAIN)
85 replaygain = av_mallocz(sizeof(*replaygain));
87 return AVERROR(ENOMEM);
89 tmp = av_realloc_array(st->side_data, st->nb_side_data + 1, sizeof(*tmp));
91 av_freep(&replaygain);
92 return AVERROR(ENOMEM);
97 sd = &st->side_data[st->nb_side_data - 1];
98 sd->type = AV_PKT_DATA_REPLAYGAIN;
99 sd->data = (uint8_t*)replaygain;
100 sd->size = sizeof(*replaygain);
102 replaygain->track_gain = tg;
103 replaygain->track_peak = tp;
104 replaygain->album_gain = ag;
105 replaygain->album_peak = ap;
110 int ff_replaygain_export(AVStream *st, AVDictionary *metadata)
112 const AVDictionaryEntry *tg, *tp, *ag, *ap;
114 tg = av_dict_get(metadata, "REPLAYGAIN_TRACK_GAIN", NULL, 0);
115 tp = av_dict_get(metadata, "REPLAYGAIN_TRACK_PEAK", NULL, 0);
116 ag = av_dict_get(metadata, "REPLAYGAIN_ALBUM_GAIN", NULL, 0);
117 ap = av_dict_get(metadata, "REPLAYGAIN_ALBUM_PEAK", NULL, 0);
119 return ff_replaygain_export_raw(st,
120 parse_value(tg ? tg->value : NULL, INT32_MIN),
121 parse_value(tp ? tp->value : NULL, 0),
122 parse_value(ag ? ag->value : NULL, INT32_MIN),
123 parse_value(ap ? ap->value : NULL, 0));