LCOV - code coverage report
Current view: top level - libavformat - replaygain.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 17 37 45.9 %
Date: 2017-12-18 13:19:42 Functions: 3 3 100.0 %

          Line data    Source code
       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/mem.h"
      33             : #include "libavutil/replaygain.h"
      34             : 
      35             : #include "avformat.h"
      36             : #include "internal.h"
      37             : #include "replaygain.h"
      38             : 
      39        1036 : static int32_t parse_value(const char *value, int32_t min)
      40             : {
      41             :     char *fraction;
      42        1036 :     int  scale = 10000;
      43        1036 :     int32_t mb = 0;
      44        1036 :     int sign   = 1;
      45             :     int db;
      46             : 
      47        1036 :     if (!value)
      48        1036 :         return min;
      49             : 
      50           0 :     value += strspn(value, " \t");
      51             : 
      52           0 :     if (*value == '-')
      53           0 :         sign = -1;
      54             : 
      55           0 :     db = strtol(value, &fraction, 0);
      56           0 :     if (*fraction++ == '.') {
      57           0 :         while (av_isdigit(*fraction) && scale) {
      58           0 :             mb += scale * (*fraction - '0');
      59           0 :             scale /= 10;
      60           0 :             fraction++;
      61             :         }
      62             :     }
      63             : 
      64           0 :     if (abs(db) > (INT32_MAX - mb) / 100000)
      65           0 :         return min;
      66             : 
      67           0 :     return db * 100000 + sign * mb;
      68             : }
      69             : 
      70         267 : int ff_replaygain_export_raw(AVStream *st, int32_t tg, uint32_t tp,
      71             :                              int32_t ag, uint32_t ap)
      72             : {
      73             :     AVReplayGain *replaygain;
      74             : 
      75         267 :     if (tg == INT32_MIN && ag == INT32_MIN)
      76         267 :         return 0;
      77             : 
      78           0 :     replaygain = (AVReplayGain*)av_stream_new_side_data(st, AV_PKT_DATA_REPLAYGAIN,
      79             :                                                         sizeof(*replaygain));
      80           0 :     if (!replaygain)
      81           0 :         return AVERROR(ENOMEM);
      82             : 
      83           0 :     replaygain->track_gain = tg;
      84           0 :     replaygain->track_peak = tp;
      85           0 :     replaygain->album_gain = ag;
      86           0 :     replaygain->album_peak = ap;
      87             : 
      88           0 :     return 0;
      89             : }
      90             : 
      91         259 : int ff_replaygain_export(AVStream *st, AVDictionary *metadata)
      92             : {
      93             :     const AVDictionaryEntry *tg, *tp, *ag, *ap;
      94             : 
      95         259 :     tg = av_dict_get(metadata, "REPLAYGAIN_TRACK_GAIN", NULL, 0);
      96         259 :     tp = av_dict_get(metadata, "REPLAYGAIN_TRACK_PEAK", NULL, 0);
      97         259 :     ag = av_dict_get(metadata, "REPLAYGAIN_ALBUM_GAIN", NULL, 0);
      98         259 :     ap = av_dict_get(metadata, "REPLAYGAIN_ALBUM_PEAK", NULL, 0);
      99             : 
     100         518 :     return ff_replaygain_export_raw(st,
     101             :                              parse_value(tg ? tg->value : NULL, INT32_MIN),
     102         259 :                              parse_value(tp ? tp->value : NULL, 0),
     103             :                              parse_value(ag ? ag->value : NULL, INT32_MIN),
     104         259 :                              parse_value(ap ? ap->value : NULL, 0));
     105             : }

Generated by: LCOV version 1.13