Revision 645439c3

View differences:

libavformat/Makefile
71 71
OBJS-$(CONFIG_EAC3_MUXER)                += rawenc.o
72 72
OBJS-$(CONFIG_FFM_DEMUXER)               += ffmdec.o
73 73
OBJS-$(CONFIG_FFM_MUXER)                 += ffmenc.o
74
OBJS-$(CONFIG_FFMETADATA_DEMUXER)        += metadec.o
75
OBJS-$(CONFIG_FFMETADATA_MUXER)          += metaenc.o
74
OBJS-$(CONFIG_FFMETADATA_DEMUXER)        += ffmetadec.o
75
OBJS-$(CONFIG_FFMETADATA_MUXER)          += ffmetaenc.o
76 76
OBJS-$(CONFIG_FILMSTRIP_DEMUXER)         += filmstripdec.o
77 77
OBJS-$(CONFIG_FILMSTRIP_MUXER)           += filmstripenc.o
78 78
OBJS-$(CONFIG_FLAC_DEMUXER)              += flacdec.o rawdec.o \
libavformat/ffmetadec.c
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
};
libavformat/ffmetaenc.c
1
/*
2
 * Metadata muxer
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

  
26
static void write_escape_str(ByteIOContext *s, const uint8_t *str)
27
{
28
    const uint8_t *p = str;
29

  
30
    while (*p) {
31
        if (*p == '#' || *p == ';' || *p == '=' || *p == '\\' || *p == '\n')
32
            put_byte(s, '\\');
33
        put_byte(s, *p);
34
        p++;
35
    }
36
}
37

  
38
static void write_tags(ByteIOContext *s, AVMetadata *m)
39
{
40
    AVMetadataTag *t = NULL;
41
    while ((t = av_metadata_get(m, "", t, AV_METADATA_IGNORE_SUFFIX))) {
42
        write_escape_str(s, t->key);
43
        put_byte(s, '=');
44
        write_escape_str(s, t->value);
45
        put_byte(s, '\n');
46
    }
47
}
48

  
49
static int write_header(AVFormatContext *s)
50
{
51
    put_tag(s->pb, ID_STRING);
52
    put_byte(s->pb, '1');          // version
53
    put_byte(s->pb, '\n');
54
    put_flush_packet(s->pb);
55
    return 0;
56
}
57

  
58
static int write_trailer(AVFormatContext *s)
59
{
60
    int i;
61

  
62
    write_tags(s->pb, s->metadata);
63

  
64
    for (i = 0; i < s->nb_streams; i++) {
65
        put_tag(s->pb, ID_STREAM);
66
        put_byte(s->pb, '\n');
67
        write_tags(s->pb, s->streams[i]->metadata);
68
    }
69

  
70
    for (i = 0; i < s->nb_chapters; i++) {
71
        AVChapter *ch = s->chapters[i];
72
        put_tag(s->pb, ID_CHAPTER);
73
        put_byte(s->pb, '\n');
74
        url_fprintf(s->pb, "TIMEBASE=%d/%d\n", ch->time_base.num, ch->time_base.den);
75
        url_fprintf(s->pb, "START=%lld\n", ch->start);
76
        url_fprintf(s->pb, "END=%lld\n",   ch->end);
77
        write_tags(s->pb, ch->metadata);
78
    }
79

  
80
    put_flush_packet(s->pb);
81

  
82
    return 0;
83
}
84

  
85
static int write_packet(AVFormatContext *s, AVPacket *pkt)
86
{
87
    return 0;
88
}
89

  
90
AVOutputFormat ffmetadata_muxer = {
91
    .name          = "ffmetadata",
92
    .long_name     = NULL_IF_CONFIG_SMALL("FFmpeg metadata in text format"),
93
    .extensions    = "ffmeta",
94
    .video_codec   = CODEC_ID_NONE,
95
    .audio_codec   = CODEC_ID_NONE,
96
    .write_header  = write_header,
97
    .write_packet  = write_packet,
98
    .write_trailer = write_trailer,
99
    .flags         = AVFMT_NOTIMESTAMPS | AVFMT_NOSTREAMS,
100
};
libavformat/metadec.c
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
};
libavformat/metaenc.c
1
/*
2
 * Metadata muxer
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

  
26
static void write_escape_str(ByteIOContext *s, const uint8_t *str)
27
{
28
    const uint8_t *p = str;
29

  
30
    while (*p) {
31
        if (*p == '#' || *p == ';' || *p == '=' || *p == '\\' || *p == '\n')
32
            put_byte(s, '\\');
33
        put_byte(s, *p);
34
        p++;
35
    }
36
}
37

  
38
static void write_tags(ByteIOContext *s, AVMetadata *m)
39
{
40
    AVMetadataTag *t = NULL;
41
    while ((t = av_metadata_get(m, "", t, AV_METADATA_IGNORE_SUFFIX))) {
42
        write_escape_str(s, t->key);
43
        put_byte(s, '=');
44
        write_escape_str(s, t->value);
45
        put_byte(s, '\n');
46
    }
47
}
48

  
49
static int write_header(AVFormatContext *s)
50
{
51
    put_tag(s->pb, ID_STRING);
52
    put_byte(s->pb, '1');          // version
53
    put_byte(s->pb, '\n');
54
    put_flush_packet(s->pb);
55
    return 0;
56
}
57

  
58
static int write_trailer(AVFormatContext *s)
59
{
60
    int i;
61

  
62
    write_tags(s->pb, s->metadata);
63

  
64
    for (i = 0; i < s->nb_streams; i++) {
65
        put_tag(s->pb, ID_STREAM);
66
        put_byte(s->pb, '\n');
67
        write_tags(s->pb, s->streams[i]->metadata);
68
    }
69

  
70
    for (i = 0; i < s->nb_chapters; i++) {
71
        AVChapter *ch = s->chapters[i];
72
        put_tag(s->pb, ID_CHAPTER);
73
        put_byte(s->pb, '\n');
74
        url_fprintf(s->pb, "TIMEBASE=%d/%d\n", ch->time_base.num, ch->time_base.den);
75
        url_fprintf(s->pb, "START=%lld\n", ch->start);
76
        url_fprintf(s->pb, "END=%lld\n",   ch->end);
77
        write_tags(s->pb, ch->metadata);
78
    }
79

  
80
    put_flush_packet(s->pb);
81

  
82
    return 0;
83
}
84

  
85
static int write_packet(AVFormatContext *s, AVPacket *pkt)
86
{
87
    return 0;
88
}
89

  
90
AVOutputFormat ffmetadata_muxer = {
91
    .name          = "ffmetadata",
92
    .long_name     = NULL_IF_CONFIG_SMALL("FFmpeg metadata in text format"),
93
    .extensions    = "ffmeta",
94
    .video_codec   = CODEC_ID_NONE,
95
    .audio_codec   = CODEC_ID_NONE,
96
    .write_header  = write_header,
97
    .write_packet  = write_packet,
98
    .write_trailer = write_trailer,
99
    .flags         = AVFMT_NOTIMESTAMPS | AVFMT_NOSTREAMS,
100
};

Also available in: Unified diff