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