FFmpeg
Loading...
Searching...
No Matches
replaygain.c
Go to the documentation of this file.
1/*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg 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 * FFmpeg 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 FFmpeg; 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"
33
34#include "avformat.h"
35#include "internal.h"
36#include "replaygain.h"
37
38static int32_t parse_value(const char *value, int32_t min)
39{
40 char *fraction;
41 int scale = 10000;
42 int32_t mb = 0;
43 int sign = 1;
44 int db;
45
46 if (!value)
47 return min;
48
49 value += strspn(value, " \t");
50
51 if (*value == '-')
52 sign = -1;
53
54 db = strtol(value, &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 (llabs(db) > (INT32_MAX - mb) / 100000)
64 return min;
65
66 return db * 100000 + sign * mb;
67}
68
69int ff_replaygain_export_raw(AVStream *st, int32_t tg, uint32_t tp,
70 int32_t ag, uint32_t ap)
71{
73 AVReplayGain *replaygain;
74
75 if (tg == INT32_MIN && ag == INT32_MIN)
76 return 0;
77
81 sizeof(*replaygain), 0);
82 if (!sd)
83 return AVERROR(ENOMEM);
84
85 replaygain = (AVReplayGain*)sd->data;
86 replaygain->track_gain = tg;
87 replaygain->track_peak = tp;
88 replaygain->album_gain = ag;
89 replaygain->album_peak = ap;
90
91 return 0;
92}
93
95{
96 const AVDictionaryEntry *tg, *tp, *ag, *ap;
97
98 tg = av_dict_get(metadata, "REPLAYGAIN_TRACK_GAIN", NULL, 0);
99 tp = av_dict_get(metadata, "REPLAYGAIN_TRACK_PEAK", NULL, 0);
100 ag = av_dict_get(metadata, "REPLAYGAIN_ALBUM_GAIN", NULL, 0);
101 ap = av_dict_get(metadata, "REPLAYGAIN_ALBUM_PEAK", NULL, 0);
102
103 return ff_replaygain_export_raw(st,
104 parse_value(tg ? tg->value : NULL, INT32_MIN),
105 parse_value(tp ? tp->value : NULL, 0),
106 parse_value(ag ? ag->value : NULL, INT32_MIN),
107 parse_value(ap ? ap->value : NULL, 0));
108}
int32_t
Main libavformat public API header.
static int FUNC metadata(CodedBitstreamContext *ctx, RWContext *rw, APVRawMetadata *current)
#define mb(name)
Definition cbs_lcevc.c:95
#define NULL
Definition coverity.c:32
#define min(a, b)
Public dictionary API.
double value
Definition eval.c:102
AVPacketSideData * av_packet_side_data_new(AVPacketSideData **psd, int *pnb_sd, enum AVPacketSideDataType type, size_t size, int flags)
Allocate a new packet side data.
Definition packet.c:620
@ AV_PKT_DATA_REPLAYGAIN
This side data should be associated with an audio stream and contains ReplayGain information in form ...
Definition packet.h:96
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition dict.c:60
#define AVERROR(e)
Definition error.h:45
static av_const int av_isdigit(int c)
Locale-independent conversion of ASCII isdigit.
Definition avstring.h:202
static void scale(int *out, const int *in, const int w, const int h, const int shift)
Definition intra.c:278
int ff_replaygain_export(AVStream *st, AVDictionary *metadata)
Parse replaygain tags and export them as per-stream side data.
Definition replaygain.c:94
int ff_replaygain_export_raw(AVStream *st, int32_t tg, uint32_t tp, int32_t ag, uint32_t ap)
Export already decoded replaygain values as per-stream side data.
Definition replaygain.c:69
static int32_t parse_value(const char *value, int32_t min)
Definition replaygain.c:38
int nb_coded_side_data
Amount of entries in coded_side_data.
Definition codec_par.h:88
AVPacketSideData * coded_side_data
Additional data associated with the entire stream.
Definition codec_par.h:83
char * value
Definition dict.h:92
This structure stores auxiliary information for decoding, presenting, or otherwise processing the cod...
Definition packet.h:424
uint8_t * data
Definition packet.h:425
ReplayGain information (see http://wiki.hydrogenaudio.org/index.php?title=ReplayGain_1....
Definition replaygain.h:29
uint32_t track_peak
Peak track amplitude, with 100000 representing full scale (but values may overflow).
Definition replaygain.h:39
uint32_t album_peak
Same as track_peak, but for the whole album,.
Definition replaygain.h:47
int32_t track_gain
Track replay gain in microbels (divide by 100000 to get the value in dB).
Definition replaygain.h:34
int32_t album_gain
Same as track_gain, but for the whole album.
Definition replaygain.h:43
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789