You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

126 lines
3.4KB

  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. * @file
  20. * replaygain tags parsing
  21. */
  22. #include <stdint.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include "libavutil/avstring.h"
  26. #include "libavutil/dict.h"
  27. #include "libavutil/intreadwrite.h"
  28. #include "libavutil/mathematics.h"
  29. #include "libavutil/mem.h"
  30. #include "libavutil/replaygain.h"
  31. #include "avformat.h"
  32. #include "replaygain.h"
  33. static int32_t parse_value(const char *value, int32_t min)
  34. {
  35. char *fraction;
  36. int scale = 10000;
  37. int32_t mb = 0;
  38. int sign = 1;
  39. int db;
  40. if (!value)
  41. return min;
  42. value += strspn(value, " \t");
  43. if (*value == '-')
  44. sign = -1;
  45. db = strtol(value, &fraction, 0);
  46. if (*fraction++ == '.') {
  47. while (av_isdigit(*fraction) && scale) {
  48. mb += scale * (*fraction - '0');
  49. scale /= 10;
  50. fraction++;
  51. }
  52. }
  53. if (abs(db) > (INT32_MAX - mb) / 100000)
  54. return min;
  55. return db * 100000 + sign * mb;
  56. }
  57. int ff_replaygain_export_raw(AVStream *st, int32_t tg, uint32_t tp,
  58. int32_t ag, uint32_t ap)
  59. {
  60. AVPacketSideData *sd, *tmp;
  61. AVReplayGain *replaygain;
  62. int i;
  63. if (tg == INT32_MIN && ag == INT32_MIN)
  64. return 0;
  65. for (i = 0; i < st->nb_side_data; i++) {
  66. AVPacketSideData *src_sd = &st->side_data[i];
  67. if (src_sd->type == AV_PKT_DATA_REPLAYGAIN)
  68. return 0;
  69. }
  70. replaygain = av_mallocz(sizeof(*replaygain));
  71. if (!replaygain)
  72. return AVERROR(ENOMEM);
  73. tmp = av_realloc_array(st->side_data, st->nb_side_data + 1, sizeof(*tmp));
  74. if (!tmp) {
  75. av_freep(&replaygain);
  76. return AVERROR(ENOMEM);
  77. }
  78. st->side_data = tmp;
  79. st->nb_side_data++;
  80. sd = &st->side_data[st->nb_side_data - 1];
  81. sd->type = AV_PKT_DATA_REPLAYGAIN;
  82. sd->data = (uint8_t*)replaygain;
  83. sd->size = sizeof(*replaygain);
  84. replaygain->track_gain = tg;
  85. replaygain->track_peak = tp;
  86. replaygain->album_gain = ag;
  87. replaygain->album_peak = ap;
  88. return 0;
  89. }
  90. int ff_replaygain_export(AVStream *st, AVDictionary *metadata)
  91. {
  92. const AVDictionaryEntry *tg, *tp, *ag, *ap;
  93. tg = av_dict_get(metadata, "REPLAYGAIN_TRACK_GAIN", NULL, 0);
  94. tp = av_dict_get(metadata, "REPLAYGAIN_TRACK_PEAK", NULL, 0);
  95. ag = av_dict_get(metadata, "REPLAYGAIN_ALBUM_GAIN", NULL, 0);
  96. ap = av_dict_get(metadata, "REPLAYGAIN_ALBUM_PEAK", NULL, 0);
  97. return ff_replaygain_export_raw(st,
  98. parse_value(tg ? tg->value : NULL, INT32_MIN),
  99. parse_value(tp ? tp->value : NULL, 0),
  100. parse_value(ag ? ag->value : NULL, INT32_MIN),
  101. parse_value(ap ? ap->value : NULL, 0));
  102. }