Libav
replaygain.c
Go to the documentation of this file.
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 
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_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 
69 int ff_replaygain_export_raw(AVStream *st, int32_t tg, uint32_t tp,
70  int32_t ag, uint32_t ap)
71 {
72  AVPacketSideData *sd, *tmp;
73  AVReplayGain *replaygain;
74 
75  if (tg == INT32_MIN && ag == INT32_MIN)
76  return 0;
77 
78  for (int i = 0; i < st->nb_side_data; i++) {
79  AVPacketSideData *src_sd = &st->side_data[i];
80 
81  if (src_sd->type == AV_PKT_DATA_REPLAYGAIN)
82  return 0;
83  }
84 
85  replaygain = av_mallocz(sizeof(*replaygain));
86  if (!replaygain)
87  return AVERROR(ENOMEM);
88 
89  tmp = av_realloc_array(st->side_data, st->nb_side_data + 1, sizeof(*tmp));
90  if (!tmp) {
91  av_freep(&replaygain);
92  return AVERROR(ENOMEM);
93  }
94  st->side_data = tmp;
95  st->nb_side_data++;
96 
97  sd = &st->side_data[st->nb_side_data - 1];
99  sd->data = (uint8_t*)replaygain;
100  sd->size = sizeof(*replaygain);
101 
102  replaygain->track_gain = tg;
103  replaygain->track_peak = tp;
104  replaygain->album_gain = ag;
105  replaygain->album_peak = ap;
106 
107  return 0;
108 }
109 
111 {
112  const AVDictionaryEntry *tg, *tp, *ag, *ap;
113 
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);
118 
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));
124 }
memory handling functions
static int32_t parse_value(const char *value, int32_t min)
Definition: replaygain.c:38
int av_isdigit(int c)
Locale-independent conversion of ASCII isdigit.
Definition: avstring.c:215
AVPacketSideData * side_data
An array of side data that applies to the whole stream (i.e.
Definition: avformat.h:807
uint32_t track_peak
Peak track amplitude, with 100000 representing full scale (but values may overflow).
Definition: replaygain.h:40
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
Public dictionary API.
uint8_t
int nb_side_data
The number of elements in the AVStream.side_data array.
Definition: avformat.h:811
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:38
uint8_t * data
Definition: avcodec.h:917
int ff_replaygain_export(AVStream *st, AVDictionary *metadata)
Parse replaygain tags and export them as per-stream side data.
Definition: replaygain.c:110
int32_t album_gain
Same as track_gain, but for the whole album.
Definition: replaygain.h:44
#define AVERROR(e)
Definition: error.h:43
enum AVPacketSideDataType type
Definition: avcodec.h:919
int32_t
Stream structure.
Definition: avformat.h:699
NULL
Definition: eval.c:55
This side data should be associated with an audio stream and contains ReplayGain information in form ...
Definition: avcodec.h:904
Main libavformat public API header.
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
void * av_realloc_array(void *ptr, size_t nmemb, size_t size)
Definition: mem.c:160
uint32_t album_peak
Same as track_peak, but for the whole album,.
Definition: replaygain.h:48
char * value
Definition: dict.h:76
int32_t track_gain
Track replay gain in microbels (divide by 100000 to get the value in dB).
Definition: replaygain.h:35
ReplayGain information (see http://wiki.hydrogenaudio.org/index.php?title=ReplayGain_1.0_specification).
Definition: replaygain.h:30
float min
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:205