FFmpeg
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"
30 #include "libavutil/intreadwrite.h"
31 #include "libavutil/mathematics.h"
32 #include "libavutil/replaygain.h"
33 
34 #include "avformat.h"
35 #include "internal.h"
36 #include "replaygain.h"
37 
38 static 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 (abs(db) > (INT32_MAX - mb) / 100000)
64  return min;
65 
66  return db * 100000 + sign * mb;
67 }
68 
70  int32_t ag, uint32_t ap)
71 {
72  AVReplayGain *replaygain;
73 
74  if (tg == INT32_MIN && ag == INT32_MIN)
75  return 0;
76 
78  sizeof(*replaygain));
79  if (!replaygain)
80  return AVERROR(ENOMEM);
81 
82  replaygain->track_gain = tg;
83  replaygain->track_peak = tp;
84  replaygain->album_gain = ag;
85  replaygain->album_peak = ap;
86 
87  return 0;
88 }
89 
91 {
92  const AVDictionaryEntry *tg, *tp, *ag, *ap;
93 
94  tg = av_dict_get(metadata, "REPLAYGAIN_TRACK_GAIN", NULL, 0);
95  tp = av_dict_get(metadata, "REPLAYGAIN_TRACK_PEAK", NULL, 0);
96  ag = av_dict_get(metadata, "REPLAYGAIN_ALBUM_GAIN", NULL, 0);
97  ap = av_dict_get(metadata, "REPLAYGAIN_ALBUM_PEAK", NULL, 0);
98 
99  return ff_replaygain_export_raw(st,
100  parse_value(tg ? tg->value : NULL, INT32_MIN),
101  parse_value(tp ? tp->value : NULL, 0),
102  parse_value(ag ? ag->value : NULL, INT32_MIN),
103  parse_value(ap ? ap->value : NULL, 0));
104 }
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
ff_replaygain_export
int ff_replaygain_export(AVStream *st, AVDictionary *metadata)
Parse replaygain tags and export them as per-stream side data.
Definition: replaygain.c:90
ff_replaygain_export_raw
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
AVReplayGain::album_gain
int32_t album_gain
Same as track_gain, but for the whole album.
Definition: replaygain.h:43
mathematics.h
AVDictionary
Definition: dict.c:30
replaygain.h
scale
static av_always_inline float scale(float x, float s)
Definition: vf_v360.c:1388
AV_PKT_DATA_REPLAYGAIN
@ AV_PKT_DATA_REPLAYGAIN
This side data should be associated with an audio stream and contains ReplayGain information in form ...
Definition: packet.h:99
av_dict_get
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:40
intreadwrite.h
AVReplayGain::track_peak
uint32_t track_peak
Peak track amplitude, with 100000 representing full scale (but values may overflow).
Definition: replaygain.h:39
internal.h
NULL
#define NULL
Definition: coverity.c:32
abs
#define abs(x)
Definition: cuda_runtime.h:35
AVReplayGain::track_gain
int32_t track_gain
Track replay gain in microbels (divide by 100000 to get the value in dB).
Definition: replaygain.h:34
av_isdigit
static av_const int av_isdigit(int c)
Locale-independent conversion of ASCII isdigit.
Definition: avstring.h:211
tg
#define tg
Definition: regdef.h:74
mb
#define mb
Definition: vf_colormatrix.c:101
parse_value
static int32_t parse_value(const char *value, int32_t min)
Definition: replaygain.c:38
value
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default value
Definition: writing_filters.txt:86
AVStream
Stream structure.
Definition: avformat.h:935
avformat.h
dict.h
AVReplayGain::album_peak
uint32_t album_peak
Same as track_peak, but for the whole album,.
Definition: replaygain.h:47
AVReplayGain
ReplayGain information (see http://wiki.hydrogenaudio.org/index.php?title=ReplayGain_1....
Definition: replaygain.h:29
AVDictionaryEntry
Definition: dict.h:79
av_stream_new_side_data
uint8_t * av_stream_new_side_data(AVStream *stream, enum AVPacketSideDataType type, size_t size)
Allocate new information from stream.
Definition: utils.c:1772
int32_t
int32_t
Definition: audioconvert.c:56
replaygain.h
AVDictionaryEntry::value
char * value
Definition: dict.h:81
avstring.h
min
float min
Definition: vorbis_enc_data.h:429