Statistics
| Branch: | Revision:

ffmpeg / libavformat / metadec.c @ fd5b124d

History | View | Annotate | Download (4.51 KB)

1
/*
2
 * Metadata demuxer
3
 * Copyright (c) 2010 Anton Khirnov
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21

    
22
#include "avformat.h"
23
#include "meta.h"
24

    
25
static int probe(AVProbeData *p)
26
{
27
    if(!memcmp(p->buf, ID_STRING, strlen(ID_STRING)))
28
        return AVPROBE_SCORE_MAX;
29
    return 0;
30
}
31

    
32
static void get_line(ByteIOContext *s, uint8_t *buf, int size)
33
{
34
    do {
35
        uint8_t c;
36
        int i = 0;
37

    
38
        while ((c = get_byte(s))) {
39
            if (c == '\\') {
40
                if (i < size - 1)
41
                    buf[i++] = c;
42
                c = get_byte(s);
43
            } else if (c == '\n')
44
                break;
45

    
46
            if (i < size - 1)
47
                buf[i++] = c;
48
        }
49
        buf[i] = 0;
50
    } while (!url_feof(s) && (buf[0] == ';' || buf[0] == '#' || buf[0] == 0));
51
}
52

    
53
static AVChapter *read_chapter(AVFormatContext *s)
54
{
55
    uint8_t line[256];
56
    int64_t start, end;
57
    AVRational tb = {1, 1e9};
58

    
59
    get_line(s->pb, line, sizeof(line));
60

    
61
    if (sscanf(line, "TIMEBASE=%d/%d", &tb.num, &tb.den))
62
        get_line(s->pb, line, sizeof(line));
63
    if (!sscanf(line, "START=%lld", &start)) {
64
        av_log(s, AV_LOG_ERROR, "Expected chapter start timestamp, found %s.\n", line);
65
        start = (s->nb_chapters && s->chapters[s->nb_chapters - 1]->end != AV_NOPTS_VALUE) ?
66
                 s->chapters[s->nb_chapters - 1]->end : 0;
67
    } else
68
        get_line(s->pb, line, sizeof(line));
69

    
70
    if (!sscanf(line, "END=%lld", &end)) {
71
        av_log(s, AV_LOG_ERROR, "Expected chapter end timestamp, found %s.\n", line);
72
        end = AV_NOPTS_VALUE;
73
    }
74

    
75
    return ff_new_chapter(s, s->nb_chapters, tb, start, end, NULL);
76
}
77

    
78
static uint8_t *unescape(uint8_t *buf, int size)
79
{
80
    uint8_t *ret = av_malloc(size + 1);
81
    uint8_t *p1  = ret, *p2 = buf;
82

    
83
    if (!ret)
84
        return NULL;
85

    
86
    while (p2 < buf + size) {
87
        if (*p2 == '\\')
88
            p2++;
89
        *p1++ = *p2++;
90
    }
91
    *p1 = 0;
92
    return ret;
93
}
94

    
95
static int read_tag(uint8_t *line, AVMetadata **m)
96
{
97
    uint8_t *key, *value, *p = line;
98

    
99
    /* find first not escaped '=' */
100
    while (1) {
101
        if (*p == '=')
102
            break;
103
        else if (*p == '\\')
104
            p++;
105

    
106
        if (*p++)
107
            continue;
108

    
109
        return 0;
110
    }
111

    
112
    if (!(key = unescape(line, p - line)))
113
        return AVERROR(ENOMEM);
114
    if (!(value = unescape(p + 1, strlen(p + 1)))) {
115
        av_free(key);
116
        return AVERROR(ENOMEM);
117
    }
118

    
119
    av_metadata_set2(m, key, value, AV_METADATA_DONT_STRDUP_KEY | AV_METADATA_DONT_STRDUP_VAL);
120
    return 0;
121
}
122

    
123
static int read_header(AVFormatContext *s, AVFormatParameters *ap)
124
{
125
    AVMetadata **m = &s->metadata;
126
    uint8_t line[1024];
127

    
128
    while(!url_feof(s->pb)) {
129
        get_line(s->pb, line, sizeof(line));
130

    
131
        if (!memcmp(line, ID_STREAM, strlen(ID_STREAM))) {
132
            AVStream *st = av_new_stream(s, 0);
133

    
134
            if (!st)
135
                return -1;
136

    
137
            st->codec->codec_type = AVMEDIA_TYPE_DATA;
138
            st->codec->codec_id   = CODEC_ID_FFMETADATA;
139

    
140
            m = &st->metadata;
141
        } else if (!memcmp(line, ID_CHAPTER, strlen(ID_CHAPTER))) {
142
            AVChapter *ch = read_chapter(s);
143

    
144
            if (!ch)
145
                return -1;
146

    
147
            m = &ch->metadata;
148
        } else
149
            read_tag(line, m);
150
    }
151

    
152
    s->start_time = 0;
153
    if (s->nb_chapters)
154
        s->duration = av_rescale_q(s->chapters[s->nb_chapters - 1]->end,
155
                                   s->chapters[s->nb_chapters - 1]->time_base,
156
                                   AV_TIME_BASE_Q);
157

    
158
    return 0;
159
}
160

    
161
static int read_packet(AVFormatContext *s, AVPacket *pkt)
162
{
163
    return AVERROR_EOF;
164
}
165

    
166
AVInputFormat ffmetadata_demuxer = {
167
    .name        = "ffmetadata",
168
    .long_name   = NULL_IF_CONFIG_SMALL("FFmpeg metadata in text format"),
169
    .read_probe  = probe,
170
    .read_header = read_header,
171
    .read_packet = read_packet,
172
};