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.

155 lines
3.9KB

  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. /**
  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_gain(const char *gain)
  34. {
  35. char *fraction;
  36. int scale = 10000;
  37. int32_t mb = 0;
  38. int db;
  39. if (!gain)
  40. return INT32_MIN;
  41. gain += strspn(gain, " \t");
  42. db = strtol(gain, &fraction, 0);
  43. if (*fraction++ == '.') {
  44. while (av_isdigit(*fraction) && scale) {
  45. mb += scale * (*fraction - '0');
  46. scale /= 10;
  47. fraction++;
  48. }
  49. }
  50. if (abs(db) > (INT32_MAX - mb) / 100000)
  51. return INT32_MIN;
  52. return db * 100000 + FFSIGN(db) * mb;
  53. }
  54. static uint32_t parse_peak(const uint8_t *peak)
  55. {
  56. int64_t val = 0;
  57. int64_t scale = 1;
  58. if (!peak)
  59. return 0;
  60. peak += strspn(peak, " \t");
  61. if (peak[0] == '1' && peak[1] == '.')
  62. return UINT32_MAX;
  63. else if (!(peak[0] == '0' && peak[1] == '.'))
  64. return 0;
  65. peak += 2;
  66. while (av_isdigit(*peak)) {
  67. int digit = *peak - '0';
  68. if (scale > INT64_MAX / 10)
  69. break;
  70. val = 10 * val + digit;
  71. scale *= 10;
  72. peak++;
  73. }
  74. return av_rescale(val, UINT32_MAX, scale);
  75. }
  76. static int replaygain_export(AVStream *st,
  77. const uint8_t *track_gain, const uint8_t *track_peak,
  78. const uint8_t *album_gain, const uint8_t *album_peak)
  79. {
  80. AVPacketSideData *sd, *tmp;
  81. AVReplayGain *replaygain;
  82. uint8_t *data;
  83. int32_t tg, ag;
  84. uint32_t tp, ap;
  85. tg = parse_gain(track_gain);
  86. ag = parse_gain(album_gain);
  87. tp = parse_peak(track_peak);
  88. ap = parse_peak(album_peak);
  89. if (tg == INT32_MIN && ag == INT32_MIN)
  90. return 0;
  91. replaygain = av_mallocz(sizeof(*replaygain));
  92. if (!replaygain)
  93. return AVERROR(ENOMEM);
  94. tmp = av_realloc_array(st->side_data, st->nb_side_data + 1, sizeof(*tmp));
  95. if (!tmp) {
  96. av_freep(&replaygain);
  97. return AVERROR(ENOMEM);
  98. }
  99. st->side_data = tmp;
  100. st->nb_side_data++;
  101. sd = &st->side_data[st->nb_side_data - 1];
  102. sd->type = AV_PKT_DATA_REPLAYGAIN;
  103. sd->data = (uint8_t*)replaygain;
  104. sd->size = sizeof(*replaygain);
  105. replaygain->track_gain = tg;
  106. replaygain->track_peak = tp;
  107. replaygain->album_gain = ag;
  108. replaygain->album_peak = ap;
  109. return 0;
  110. }
  111. int ff_replaygain_export(AVStream *st, AVDictionary *metadata)
  112. {
  113. const AVDictionaryEntry *tg, *tp, *ag, *ap;
  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. return replaygain_export(st,
  119. tg ? tg->value : NULL,
  120. tp ? tp->value : NULL,
  121. ag ? ag->value : NULL,
  122. ap ? ap->value : NULL);
  123. }