apetagenc.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * APE tag writer
  3. * Copyright (c) 2012 Paul B Mahol
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/dict.h"
  22. #include "avio_internal.h"
  23. #include "avformat.h"
  24. #include "apetag.h"
  25. static int string_is_ascii(const uint8_t *str)
  26. {
  27. while (*str && *str >= 0x20 && *str <= 0x7e ) str++;
  28. return !*str;
  29. }
  30. void ff_ape_write(AVFormatContext *s)
  31. {
  32. int64_t tag_bytes;
  33. AVDictionaryEntry *t = NULL;
  34. AVIOContext *pb = s->pb;
  35. int tags = 0, vlen;
  36. tag_bytes = avio_tell(s->pb);
  37. while ((t = av_dict_get(s->metadata, "", t, AV_DICT_IGNORE_SUFFIX))) {
  38. if (!string_is_ascii(t->key)) {
  39. av_log(s, AV_LOG_WARNING, "Non ASCII keys are not allowed\n");
  40. continue;
  41. }
  42. vlen = strlen(t->value);
  43. avio_wl32(pb, vlen + 1);
  44. avio_wl32(pb, 0); // flags
  45. avio_put_str(pb, t->key);
  46. avio_put_str(pb, t->value);
  47. tags++;
  48. }
  49. tag_bytes = avio_tell(s->pb) - tag_bytes;
  50. if (!tags)
  51. return;
  52. avio_write(pb, APE_TAG_PREAMBLE, 8);
  53. avio_wl32(pb, APE_TAG_VERSION);
  54. avio_wl32(pb, tag_bytes + APE_TAG_FOOTER_BYTES);
  55. avio_wl32(pb, tags); // item count
  56. avio_wl32(pb, 0); // global flags
  57. ffio_fill(pb, 0, 8); // reserved
  58. }