Statistics
| Branch: | Revision:

ffmpeg / libavformat / metadata_compat.c @ 470bce2b

History | View | Annotate | Download (5.45 KB)

1 e99f76ab Aurelien Jacobs
/*
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
21
#if LIBAVFORMAT_VERSION_MAJOR < 53
22
23
#include <strings.h>
24
#include "metadata.h"
25
#include "libavutil/avstring.h"
26
27
#define SIZE_OFFSET(x) sizeof(((AVFormatContext*)0)->x),offsetof(AVFormatContext,x)
28
29
static const struct {
30
    const char name[16];
31
    int   size;
32
    int   offset;
33
} compat_tab[] = {
34
    { "title",           SIZE_OFFSET(title)     },
35
    { "author",          SIZE_OFFSET(author)    },
36
    { "copyright",       SIZE_OFFSET(copyright) },
37
    { "comment",         SIZE_OFFSET(comment)   },
38
    { "album",           SIZE_OFFSET(album)     },
39
    { "year",            SIZE_OFFSET(year)      },
40
    { "track",           SIZE_OFFSET(track)     },
41
    { "genre",           SIZE_OFFSET(genre)     },
42
43
    { "artist",          SIZE_OFFSET(author)    },
44
    { "creator",         SIZE_OFFSET(author)    },
45
    { "written_by",      SIZE_OFFSET(author)    },
46
    { "lead_performer",  SIZE_OFFSET(author)    },
47
    { "description",     SIZE_OFFSET(comment)   },
48
    { "albumtitle",      SIZE_OFFSET(album)     },
49
    { "date_written",    SIZE_OFFSET(year)      },
50
    { "date_released",   SIZE_OFFSET(year)      },
51
    { "tracknumber",     SIZE_OFFSET(track)     },
52
    { "part_number",     SIZE_OFFSET(track)     },
53
};
54
55
void ff_metadata_demux_compat(AVFormatContext *ctx)
56
{
57
    AVMetadata *m;
58
    int i, j;
59
60
    if ((m = ctx->metadata))
61
        for (j=0; j<m->count; j++)
62
            for (i=0; i<FF_ARRAY_ELEMS(compat_tab); i++)
63
                if (!strcasecmp(m->elems[j].key, compat_tab[i].name)) {
64
                    int *ptr = (int *)((char *)ctx+compat_tab[i].offset);
65
                    if (*ptr)  continue;
66
                    if (compat_tab[i].size > sizeof(int))
67
                        av_strlcpy((char *)ptr, m->elems[j].value, compat_tab[i].size);
68
                    else
69
                        *ptr = atoi(m->elems[j].value);
70
                }
71
72
    for (i=0; i<ctx->nb_chapters; i++)
73
        if ((m = ctx->chapters[i]->metadata))
74
            for (j=0; j<m->count; j++)
75
                if (!strcasecmp(m->elems[j].key, "title")) {
76
                    av_free(ctx->chapters[i]->title);
77
                    ctx->chapters[i]->title = av_strdup(m->elems[j].value);
78
                }
79
80
    for (i=0; i<ctx->nb_programs; i++)
81
        if ((m = ctx->programs[i]->metadata))
82
            for (j=0; j<m->count; j++) {
83
                if (!strcasecmp(m->elems[j].key, "name")) {
84
                    av_free(ctx->programs[i]->name);
85
                    ctx->programs[i]->name = av_strdup(m->elems[j].value);
86
                }
87
                if (!strcasecmp(m->elems[j].key, "provider_name")) {
88
                    av_free(ctx->programs[i]->provider_name);
89
                    ctx->programs[i]->provider_name = av_strdup(m->elems[j].value);
90
                }
91
            }
92
93
    for (i=0; i<ctx->nb_streams; i++)
94
        if ((m = ctx->streams[i]->metadata))
95
            for (j=0; j<m->count; j++) {
96
                if (!strcasecmp(m->elems[j].key, "language"))
97
                    av_strlcpy(ctx->streams[i]->language, m->elems[j].value, 4);
98
                if (!strcasecmp(m->elems[j].key, "filename")) {
99
                    av_free(ctx->streams[i]->filename);
100
                    ctx->streams[i]->filename= av_strdup(m->elems[j].value);
101
                }
102
            }
103
}
104
105 97ca83a5 Aurelien Jacobs
106
#define FILL_METADATA(s, key, value) {                                        \
107
    if (value && *value &&                                                    \
108
        !av_metadata_get(s->metadata, #key, NULL, AV_METADATA_IGNORE_CASE))   \
109
        av_metadata_set(&s->metadata, (const AVMetadataTag){#key, value});    \
110
    }
111
#define FILL_METADATA_STR(s, key)  FILL_METADATA(s, key, s->key)
112
#define FILL_METADATA_INT(s, key) {                                           \
113
    char number[10];                                                          \
114
    snprintf(number, sizeof(number), "%d", s->key);                           \
115
    if(s->key)  FILL_METADATA(s, key, number) }
116
117 5ce00433 Aurelien Jacobs
void ff_metadata_mux_compat(AVFormatContext *ctx)
118 97ca83a5 Aurelien Jacobs
{
119
    int i;
120
121
    FILL_METADATA_STR(ctx, title);
122
    FILL_METADATA_STR(ctx, author);
123
    FILL_METADATA_STR(ctx, copyright);
124
    FILL_METADATA_STR(ctx, comment);
125
    FILL_METADATA_STR(ctx, album);
126
    FILL_METADATA_INT(ctx, year);
127
    FILL_METADATA_INT(ctx, track);
128
    FILL_METADATA_STR(ctx, genre);
129
    for (i=0; i<ctx->nb_chapters; i++)
130
        FILL_METADATA_STR(ctx->chapters[i], title);
131
    for (i=0; i<ctx->nb_programs; i++) {
132
        FILL_METADATA_STR(ctx->programs[i], name);
133
        FILL_METADATA_STR(ctx->programs[i], provider_name);
134
    }
135
    for (i=0; i<ctx->nb_streams; i++) {
136
        FILL_METADATA_STR(ctx->streams[i], language);
137
        FILL_METADATA_STR(ctx->streams[i], filename);
138
    }
139
}
140
141 e99f76ab Aurelien Jacobs
#endif /* LIBAVFORMAT_VERSION_MAJOR < 53 */