metadata_compat.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 FF_API_OLD_METADATA
  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. { "composer", SIZE_OFFSET(author) },
  44. { "performer", SIZE_OFFSET(author) },
  45. { "description", SIZE_OFFSET(comment) },
  46. { "albumtitle", SIZE_OFFSET(album) },
  47. { "date", SIZE_OFFSET(year) },
  48. { "date_written", SIZE_OFFSET(year) },
  49. { "date_released", SIZE_OFFSET(year) },
  50. { "tracknumber", SIZE_OFFSET(track) },
  51. { "part_number", SIZE_OFFSET(track) },
  52. };
  53. void ff_metadata_demux_compat(AVFormatContext *ctx)
  54. {
  55. AVMetadata *m;
  56. int i, j;
  57. if ((m = ctx->metadata))
  58. for (j=0; j<m->count; j++)
  59. for (i=0; i<FF_ARRAY_ELEMS(compat_tab); i++)
  60. if (!strcasecmp(m->elems[j].key, compat_tab[i].name)) {
  61. int *ptr = (int *)((char *)ctx+compat_tab[i].offset);
  62. if (*ptr) continue;
  63. if (compat_tab[i].size > sizeof(int))
  64. av_strlcpy((char *)ptr, m->elems[j].value, compat_tab[i].size);
  65. else
  66. *ptr = atoi(m->elems[j].value);
  67. }
  68. for (i=0; i<ctx->nb_chapters; i++)
  69. if ((m = ctx->chapters[i]->metadata))
  70. for (j=0; j<m->count; j++)
  71. if (!strcasecmp(m->elems[j].key, "title")) {
  72. av_free(ctx->chapters[i]->title);
  73. ctx->chapters[i]->title = av_strdup(m->elems[j].value);
  74. }
  75. for (i=0; i<ctx->nb_programs; i++)
  76. if ((m = ctx->programs[i]->metadata))
  77. for (j=0; j<m->count; j++) {
  78. if (!strcasecmp(m->elems[j].key, "name")) {
  79. av_free(ctx->programs[i]->name);
  80. ctx->programs[i]->name = av_strdup(m->elems[j].value);
  81. }
  82. if (!strcasecmp(m->elems[j].key, "provider_name")) {
  83. av_free(ctx->programs[i]->provider_name);
  84. ctx->programs[i]->provider_name = av_strdup(m->elems[j].value);
  85. }
  86. }
  87. for (i=0; i<ctx->nb_streams; i++)
  88. if ((m = ctx->streams[i]->metadata))
  89. for (j=0; j<m->count; j++) {
  90. if (!strcasecmp(m->elems[j].key, "language"))
  91. av_strlcpy(ctx->streams[i]->language, m->elems[j].value, 4);
  92. if (!strcasecmp(m->elems[j].key, "filename")) {
  93. av_free(ctx->streams[i]->filename);
  94. ctx->streams[i]->filename= av_strdup(m->elems[j].value);
  95. }
  96. }
  97. }
  98. #define FILL_METADATA(s, key, value) { \
  99. if (!av_metadata_get(s->metadata, #key, NULL, 0)) \
  100. av_metadata_set2(&s->metadata, #key, value, 0); \
  101. }
  102. #define FILL_METADATA_STR(s, key) { \
  103. if (s->key && *s->key) FILL_METADATA(s, key, s->key); }
  104. #define FILL_METADATA_INT(s, key) { \
  105. char number[10]; \
  106. snprintf(number, sizeof(number), "%d", s->key); \
  107. if(s->key) FILL_METADATA(s, key, number) }
  108. void ff_metadata_mux_compat(AVFormatContext *ctx)
  109. {
  110. int i;
  111. if (ctx->metadata && ctx->metadata->count > 0)
  112. return;
  113. FILL_METADATA_STR(ctx, title);
  114. FILL_METADATA_STR(ctx, author);
  115. FILL_METADATA_STR(ctx, copyright);
  116. FILL_METADATA_STR(ctx, comment);
  117. FILL_METADATA_STR(ctx, album);
  118. FILL_METADATA_INT(ctx, year);
  119. FILL_METADATA_INT(ctx, track);
  120. FILL_METADATA_STR(ctx, genre);
  121. for (i=0; i<ctx->nb_chapters; i++)
  122. FILL_METADATA_STR(ctx->chapters[i], title);
  123. for (i=0; i<ctx->nb_programs; i++) {
  124. FILL_METADATA_STR(ctx->programs[i], name);
  125. FILL_METADATA_STR(ctx->programs[i], provider_name);
  126. }
  127. for (i=0; i<ctx->nb_streams; i++) {
  128. FILL_METADATA_STR(ctx->streams[i], language);
  129. FILL_METADATA_STR(ctx->streams[i], filename);
  130. }
  131. }
  132. #endif /* FF_API_OLD_METADATA */