metadata_compat.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright (c) 2009 Aurelien Jacobs <aurel@gnuage.org>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <strings.h>
  21. #include "avformat.h"
  22. #include "metadata.h"
  23. #include "libavutil/avstring.h"
  24. #if LIBAVFORMAT_VERSION_MAJOR < 53
  25. #define SIZE_OFFSET(x) sizeof(((AVFormatContext*)0)->x),offsetof(AVFormatContext,x)
  26. static const struct {
  27. const char name[16];
  28. int size;
  29. int offset;
  30. } compat_tab[] = {
  31. { "title", SIZE_OFFSET(title) },
  32. { "author", SIZE_OFFSET(author) },
  33. { "copyright", SIZE_OFFSET(copyright) },
  34. { "comment", SIZE_OFFSET(comment) },
  35. { "album", SIZE_OFFSET(album) },
  36. { "year", SIZE_OFFSET(year) },
  37. { "track", SIZE_OFFSET(track) },
  38. { "genre", SIZE_OFFSET(genre) },
  39. { "artist", SIZE_OFFSET(author) },
  40. { "creator", SIZE_OFFSET(author) },
  41. { "written_by", SIZE_OFFSET(author) },
  42. { "lead_performer", SIZE_OFFSET(author) },
  43. { "description", SIZE_OFFSET(comment) },
  44. { "albumtitle", SIZE_OFFSET(album) },
  45. { "date_written", SIZE_OFFSET(year) },
  46. { "date_released", SIZE_OFFSET(year) },
  47. { "tracknumber", SIZE_OFFSET(track) },
  48. { "part_number", SIZE_OFFSET(track) },
  49. };
  50. void ff_metadata_demux_compat(AVFormatContext *ctx)
  51. {
  52. AVMetadata *m;
  53. int i, j;
  54. if ((m = ctx->metadata))
  55. for (j=0; j<m->count; j++)
  56. for (i=0; i<FF_ARRAY_ELEMS(compat_tab); i++)
  57. if (!strcasecmp(m->elems[j].key, compat_tab[i].name)) {
  58. int *ptr = (int *)((char *)ctx+compat_tab[i].offset);
  59. if (*ptr) continue;
  60. if (compat_tab[i].size > sizeof(int))
  61. av_strlcpy((char *)ptr, m->elems[j].value, compat_tab[i].size);
  62. else
  63. *ptr = atoi(m->elems[j].value);
  64. }
  65. for (i=0; i<ctx->nb_chapters; i++)
  66. if ((m = ctx->chapters[i]->metadata))
  67. for (j=0; j<m->count; j++)
  68. if (!strcasecmp(m->elems[j].key, "title")) {
  69. av_free(ctx->chapters[i]->title);
  70. ctx->chapters[i]->title = av_strdup(m->elems[j].value);
  71. }
  72. for (i=0; i<ctx->nb_programs; i++)
  73. if ((m = ctx->programs[i]->metadata))
  74. for (j=0; j<m->count; j++) {
  75. if (!strcasecmp(m->elems[j].key, "name")) {
  76. av_free(ctx->programs[i]->name);
  77. ctx->programs[i]->name = av_strdup(m->elems[j].value);
  78. }
  79. if (!strcasecmp(m->elems[j].key, "provider_name")) {
  80. av_free(ctx->programs[i]->provider_name);
  81. ctx->programs[i]->provider_name = av_strdup(m->elems[j].value);
  82. }
  83. }
  84. for (i=0; i<ctx->nb_streams; i++)
  85. if ((m = ctx->streams[i]->metadata))
  86. for (j=0; j<m->count; j++) {
  87. if (!strcasecmp(m->elems[j].key, "language"))
  88. av_strlcpy(ctx->streams[i]->language, m->elems[j].value, 4);
  89. if (!strcasecmp(m->elems[j].key, "filename")) {
  90. av_free(ctx->streams[i]->filename);
  91. ctx->streams[i]->filename= av_strdup(m->elems[j].value);
  92. }
  93. }
  94. }
  95. #define FILL_METADATA(s, key, value) { \
  96. if (value && *value && !av_metadata_get(s->metadata, #key, NULL, 0)) \
  97. av_metadata_set(&s->metadata, #key, value); \
  98. }
  99. #define FILL_METADATA_STR(s, key) FILL_METADATA(s, key, s->key)
  100. #define FILL_METADATA_INT(s, key) { \
  101. char number[10]; \
  102. snprintf(number, sizeof(number), "%d", s->key); \
  103. if(s->key) FILL_METADATA(s, key, number) }
  104. void ff_metadata_mux_compat(AVFormatContext *ctx)
  105. {
  106. int i;
  107. if (ctx->metadata && ctx->metadata->count > 0)
  108. return;
  109. FILL_METADATA_STR(ctx, title);
  110. FILL_METADATA_STR(ctx, author);
  111. FILL_METADATA_STR(ctx, copyright);
  112. FILL_METADATA_STR(ctx, comment);
  113. FILL_METADATA_STR(ctx, album);
  114. FILL_METADATA_INT(ctx, year);
  115. FILL_METADATA_INT(ctx, track);
  116. FILL_METADATA_STR(ctx, genre);
  117. for (i=0; i<ctx->nb_chapters; i++)
  118. FILL_METADATA_STR(ctx->chapters[i], title);
  119. for (i=0; i<ctx->nb_programs; i++) {
  120. FILL_METADATA_STR(ctx->programs[i], name);
  121. FILL_METADATA_STR(ctx->programs[i], provider_name);
  122. }
  123. for (i=0; i<ctx->nb_streams; i++) {
  124. FILL_METADATA_STR(ctx->streams[i], language);
  125. FILL_METADATA_STR(ctx->streams[i], filename);
  126. }
  127. }
  128. #endif /* LIBAVFORMAT_VERSION_MAJOR < 53 */