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