Statistics
| Branch: | Revision:

ffmpeg / libavformat / metadata_compat.c @ 383b25fd

History | View | Annotate | Download (5.68 KB)

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

    
21
#include <strings.h>
22
#include "avformat.h"
23
#include "metadata.h"
24
#include "libavutil/avstring.h"
25

    
26
#if LIBAVFORMAT_VERSION_MAJOR < 53
27

    
28
#define SIZE_OFFSET(x) sizeof(((AVFormatContext*)0)->x),offsetof(AVFormatContext,x)
29

    
30
static const struct {
31
    const char name[16];
32
    int   size;
33
    int   offset;
34
} compat_tab[] = {
35
    { "title",           SIZE_OFFSET(title)     },
36
    { "author",          SIZE_OFFSET(author)    },
37
    { "copyright",       SIZE_OFFSET(copyright) },
38
    { "comment",         SIZE_OFFSET(comment)   },
39
    { "album",           SIZE_OFFSET(album)     },
40
    { "year",            SIZE_OFFSET(year)      },
41
    { "track",           SIZE_OFFSET(track)     },
42
    { "genre",           SIZE_OFFSET(genre)     },
43

    
44
    { "artist",          SIZE_OFFSET(author)    },
45
    { "creator",         SIZE_OFFSET(author)    },
46
    { "written_by",      SIZE_OFFSET(author)    },
47
    { "lead_performer",  SIZE_OFFSET(author)    },
48
    { "composer",        SIZE_OFFSET(author)    },
49
    { "performer",       SIZE_OFFSET(author)    },
50
    { "description",     SIZE_OFFSET(comment)   },
51
    { "albumtitle",      SIZE_OFFSET(album)     },
52
    { "date",            SIZE_OFFSET(year)      },
53
    { "date_written",    SIZE_OFFSET(year)      },
54
    { "date_released",   SIZE_OFFSET(year)      },
55
    { "tracknumber",     SIZE_OFFSET(track)     },
56
    { "part_number",     SIZE_OFFSET(track)     },
57
};
58

    
59
void ff_metadata_demux_compat(AVFormatContext *ctx)
60
{
61
    AVMetadata *m;
62
    int i, j;
63

    
64
    if ((m = ctx->metadata))
65
        for (j=0; j<m->count; j++)
66
            for (i=0; i<FF_ARRAY_ELEMS(compat_tab); i++)
67
                if (!strcasecmp(m->elems[j].key, compat_tab[i].name)) {
68
                    int *ptr = (int *)((char *)ctx+compat_tab[i].offset);
69
                    if (*ptr)  continue;
70
                    if (compat_tab[i].size > sizeof(int))
71
                        av_strlcpy((char *)ptr, m->elems[j].value, compat_tab[i].size);
72
                    else
73
                        *ptr = atoi(m->elems[j].value);
74
                }
75

    
76
    for (i=0; i<ctx->nb_chapters; i++)
77
        if ((m = ctx->chapters[i]->metadata))
78
            for (j=0; j<m->count; j++)
79
                if (!strcasecmp(m->elems[j].key, "title")) {
80
                    av_free(ctx->chapters[i]->title);
81
                    ctx->chapters[i]->title = av_strdup(m->elems[j].value);
82
                }
83

    
84
    for (i=0; i<ctx->nb_programs; i++)
85
        if ((m = ctx->programs[i]->metadata))
86
            for (j=0; j<m->count; j++) {
87
                if (!strcasecmp(m->elems[j].key, "name")) {
88
                    av_free(ctx->programs[i]->name);
89
                    ctx->programs[i]->name = av_strdup(m->elems[j].value);
90
                }
91
                if (!strcasecmp(m->elems[j].key, "provider_name")) {
92
                    av_free(ctx->programs[i]->provider_name);
93
                    ctx->programs[i]->provider_name = av_strdup(m->elems[j].value);
94
                }
95
            }
96

    
97
    for (i=0; i<ctx->nb_streams; i++)
98
        if ((m = ctx->streams[i]->metadata))
99
            for (j=0; j<m->count; j++) {
100
                if (!strcasecmp(m->elems[j].key, "language"))
101
                    av_strlcpy(ctx->streams[i]->language, m->elems[j].value, 4);
102
                if (!strcasecmp(m->elems[j].key, "filename")) {
103
                    av_free(ctx->streams[i]->filename);
104
                    ctx->streams[i]->filename= av_strdup(m->elems[j].value);
105
                }
106
            }
107
}
108

    
109

    
110
#define FILL_METADATA(s, key, value) {                                        \
111
    if (!av_metadata_get(s->metadata, #key, NULL, 0))                         \
112
        av_metadata_set2(&s->metadata, #key, value, 0);                       \
113
    }
114
#define FILL_METADATA_STR(s, key) {                                           \
115
    if (s->key && *s->key)  FILL_METADATA(s, key, s->key); }
116
#define FILL_METADATA_INT(s, key) {                                           \
117
    char number[10];                                                          \
118
    snprintf(number, sizeof(number), "%d", s->key);                           \
119
    if(s->key)  FILL_METADATA(s, key, number) }
120

    
121
void ff_metadata_mux_compat(AVFormatContext *ctx)
122
{
123
    int i;
124

    
125
    if (ctx->metadata && ctx->metadata->count > 0)
126
        return;
127

    
128
    FILL_METADATA_STR(ctx, title);
129
    FILL_METADATA_STR(ctx, author);
130
    FILL_METADATA_STR(ctx, copyright);
131
    FILL_METADATA_STR(ctx, comment);
132
    FILL_METADATA_STR(ctx, album);
133
    FILL_METADATA_INT(ctx, year);
134
    FILL_METADATA_INT(ctx, track);
135
    FILL_METADATA_STR(ctx, genre);
136
    for (i=0; i<ctx->nb_chapters; i++)
137
        FILL_METADATA_STR(ctx->chapters[i], title);
138
    for (i=0; i<ctx->nb_programs; i++) {
139
        FILL_METADATA_STR(ctx->programs[i], name);
140
        FILL_METADATA_STR(ctx->programs[i], provider_name);
141
    }
142
    for (i=0; i<ctx->nb_streams; i++) {
143
        FILL_METADATA_STR(ctx->streams[i], language);
144
        FILL_METADATA_STR(ctx->streams[i], filename);
145
    }
146
}
147

    
148
#endif /* LIBAVFORMAT_VERSION_MAJOR < 53 */