cf4dbf8f01f1fbca83296b5f71aa1946c46b41e6
[libav.git] / libavformat / replaygain.c
1 /*
2 * This file is part of Libav.
3 *
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.
8 *
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.
13 *
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
17 */
18
19 /**
20 * @file
21 * replaygain tags parsing
22 */
23
24 #include <stdint.h>
25 #include <stdlib.h>
26 #include <string.h>
27
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"
34
35 #include "avformat.h"
36 #include "replaygain.h"
37
38 static int32_t parse_gain(const char *gain)
39 {
40 char *fraction;
41 int scale = 10000;
42 int32_t mb = 0;
43 int db;
44
45 if (!gain)
46 return INT32_MIN;
47
48 gain += strspn(gain, " \t");
49
50 db = strtol(gain, &fraction, 0);
51 if (*fraction++ == '.') {
52 while (av_isdigit(*fraction) && scale) {
53 mb += scale * (*fraction - '0');
54 scale /= 10;
55 fraction++;
56 }
57 }
58
59 if (abs(db) > (INT32_MAX - mb) / 100000)
60 return INT32_MIN;
61
62 return db * 100000 + FFSIGN(db) * mb;
63 }
64
65 static uint32_t parse_peak(const uint8_t *peak)
66 {
67 int64_t val = 0;
68 int64_t scale = 1;
69
70 if (!peak)
71 return 0;
72
73 peak += strspn(peak, " \t");
74
75 if (peak[0] == '1' && peak[1] == '.')
76 return UINT32_MAX;
77 else if (!(peak[0] == '0' && peak[1] == '.'))
78 return 0;
79
80 peak += 2;
81
82 while (av_isdigit(*peak)) {
83 int digit = *peak - '0';
84
85 if (scale > INT64_MAX / 10)
86 break;
87
88 val = 10 * val + digit;
89 scale *= 10;
90
91 peak++;
92 }
93
94 return av_rescale(val, UINT32_MAX, scale);
95 }
96
97 static int replaygain_export(AVStream *st,
98 const uint8_t *track_gain, const uint8_t *track_peak,
99 const uint8_t *album_gain, const uint8_t *album_peak)
100 {
101 AVPacketSideData *sd, *tmp;
102 AVReplayGain *replaygain;
103 uint8_t *data;
104 int32_t tg, ag;
105 uint32_t tp, ap;
106
107 tg = parse_gain(track_gain);
108 ag = parse_gain(album_gain);
109 tp = parse_peak(track_peak);
110 ap = parse_peak(album_peak);
111
112 if (tg == INT32_MIN && ag == INT32_MIN)
113 return 0;
114
115 replaygain = av_mallocz(sizeof(*replaygain));
116 if (!replaygain)
117 return AVERROR(ENOMEM);
118
119 tmp = av_realloc_array(st->side_data, st->nb_side_data + 1, sizeof(*tmp));
120 if (!tmp) {
121 av_freep(&replaygain);
122 return AVERROR(ENOMEM);
123 }
124 st->side_data = tmp;
125 st->nb_side_data++;
126
127 sd = &st->side_data[st->nb_side_data - 1];
128 sd->type = AV_PKT_DATA_REPLAYGAIN;
129 sd->data = (uint8_t*)replaygain;
130 sd->size = sizeof(*replaygain);
131
132 replaygain->track_gain = tg;
133 replaygain->track_peak = tp;
134 replaygain->album_gain = ag;
135 replaygain->album_peak = ap;
136
137 return 0;
138 }
139
140 int ff_replaygain_export(AVStream *st, AVDictionary *metadata)
141 {
142 const AVDictionaryEntry *tg, *tp, *ag, *ap;
143
144 tg = av_dict_get(metadata, "REPLAYGAIN_TRACK_GAIN", NULL, 0);
145 tp = av_dict_get(metadata, "REPLAYGAIN_TRACK_PEAK", NULL, 0);
146 ag = av_dict_get(metadata, "REPLAYGAIN_ALBUM_GAIN", NULL, 0);
147 ap = av_dict_get(metadata, "REPLAYGAIN_ALBUM_PEAK", NULL, 0);
148
149 return replaygain_export(st,
150 tg ? tg->value : NULL,
151 tp ? tp->value : NULL,
152 ag ? ag->value : NULL,
153 ap ? ap->value : NULL);
154 }