~ubuntu-branches/ubuntu/trusty/gst-libav1.0/trusty-proposed

« back to all changes in this revision

Viewing changes to gst-libs/ext/libav/libavformat/id3v2enc.c

  • Committer: Package Import Robot
  • Author(s): Sebastian Dröge
  • Date: 2013-09-24 17:07:00 UTC
  • mfrom: (1.1.17) (7.1.9 experimental)
  • Revision ID: package-import@ubuntu.com-20130924170700-4dg62s3pwl0pdakz
Tags: 1.2.0-1
* New upstream stable release:
  + debian/control:
    - Build depend on GStreamer and gst-plugins-base >= 1.2.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 */
20
20
 
21
21
#include <stdint.h>
 
22
#include <string.h>
22
23
 
23
24
#include "libavutil/dict.h"
24
25
#include "libavutil/intreadwrite.h"
26
27
#include "avio.h"
27
28
#include "id3v2.h"
28
29
 
29
 
static void id3v2_put_size(AVFormatContext *s, int size)
 
30
static void id3v2_put_size(AVIOContext *pb, int size)
30
31
{
31
 
    avio_w8(s->pb, size >> 21 & 0x7f);
32
 
    avio_w8(s->pb, size >> 14 & 0x7f);
33
 
    avio_w8(s->pb, size >> 7  & 0x7f);
34
 
    avio_w8(s->pb, size       & 0x7f);
 
32
    avio_w8(pb, size >> 21 & 0x7f);
 
33
    avio_w8(pb, size >> 14 & 0x7f);
 
34
    avio_w8(pb, size >> 7  & 0x7f);
 
35
    avio_w8(pb, size       & 0x7f);
35
36
}
36
37
 
37
38
static int string_is_ascii(const uint8_t *str)
40
41
    return !*str;
41
42
}
42
43
 
 
44
static void id3v2_encode_string(AVIOContext *pb, const uint8_t *str,
 
45
                               enum ID3v2Encoding enc)
 
46
{
 
47
    int (*put)(AVIOContext*, const char*);
 
48
 
 
49
    if (enc == ID3v2_ENCODING_UTF16BOM) {
 
50
        avio_wl16(pb, 0xFEFF);      /* BOM */
 
51
        put = avio_put_str16le;
 
52
    } else
 
53
        put = avio_put_str;
 
54
 
 
55
    put(pb, str);
 
56
}
 
57
 
43
58
/**
44
59
 * Write a text frame with one (normal frames) or two (TXXX frames) strings
45
60
 * according to encoding (only UTF-8 or UTF-16+BOM supported).
46
61
 * @return number of bytes written or a negative error code.
47
62
 */
48
 
static int id3v2_put_ttag(AVFormatContext *s, const char *str1, const char *str2,
 
63
static int id3v2_put_ttag(ID3v2EncContext *id3, AVIOContext *avioc, const char *str1, const char *str2,
49
64
                          uint32_t tag, enum ID3v2Encoding enc)
50
65
{
51
66
    int len;
52
67
    uint8_t *pb;
53
 
    int (*put)(AVIOContext*, const char*);
54
68
    AVIOContext *dyn_buf;
55
69
    if (avio_open_dyn_buf(&dyn_buf) < 0)
56
70
        return AVERROR(ENOMEM);
62
76
        enc = ID3v2_ENCODING_ISO8859;
63
77
 
64
78
    avio_w8(dyn_buf, enc);
65
 
    if (enc == ID3v2_ENCODING_UTF16BOM) {
66
 
        avio_wl16(dyn_buf, 0xFEFF);      /* BOM */
67
 
        put = avio_put_str16le;
68
 
    } else
69
 
        put = avio_put_str;
70
 
 
71
 
    put(dyn_buf, str1);
 
79
    id3v2_encode_string(dyn_buf, str1, enc);
72
80
    if (str2)
73
 
        put(dyn_buf, str2);
 
81
        id3v2_encode_string(dyn_buf, str2, enc);
74
82
    len = avio_close_dyn_buf(dyn_buf, &pb);
75
83
 
76
 
    avio_wb32(s->pb, tag);
77
 
    id3v2_put_size(s, len);
78
 
    avio_wb16(s->pb, 0);
79
 
    avio_write(s->pb, pb, len);
 
84
    avio_wb32(avioc, tag);
 
85
    /* ID3v2.3 frame size is not synchsafe */
 
86
    if (id3->version == 3)
 
87
        avio_wb32(avioc, len);
 
88
    else
 
89
        id3v2_put_size(avioc, len);
 
90
    avio_wb16(avioc, 0);
 
91
    avio_write(avioc, pb, len);
80
92
 
81
93
    av_freep(&pb);
82
94
    return len + ID3v2_HEADER_SIZE;
83
95
}
84
96
 
85
 
static int id3v2_check_write_tag(AVFormatContext *s, AVDictionaryEntry *t, const char table[][4],
86
 
                                 enum ID3v2Encoding enc)
 
97
static int id3v2_check_write_tag(ID3v2EncContext *id3, AVIOContext *pb, AVDictionaryEntry *t,
 
98
                                 const char table[][4], enum ID3v2Encoding enc)
87
99
{
88
100
    uint32_t tag;
89
101
    int i;
93
105
    tag = AV_RB32(t->key);
94
106
    for (i = 0; *table[i]; i++)
95
107
        if (tag == AV_RB32(table[i]))
96
 
            return id3v2_put_ttag(s, t->value, NULL, tag, enc);
 
108
            return id3v2_put_ttag(id3, pb, t->value, NULL, tag, enc);
97
109
    return -1;
98
110
}
99
111
 
100
 
int ff_id3v2_write(struct AVFormatContext *s, int id3v2_version,
101
 
                   const char *magic)
102
 
{
103
 
    int64_t size_pos, cur_pos;
 
112
void ff_id3v2_start(ID3v2EncContext *id3, AVIOContext *pb, int id3v2_version,
 
113
                    const char *magic)
 
114
{
 
115
    id3->version = id3v2_version;
 
116
 
 
117
    avio_wb32(pb, MKBETAG(magic[0], magic[1], magic[2], id3v2_version));
 
118
    avio_w8(pb, 0);
 
119
    avio_w8(pb, 0); /* flags */
 
120
 
 
121
    /* reserve space for size */
 
122
    id3->size_pos = avio_tell(pb);
 
123
    avio_wb32(pb, 0);
 
124
}
 
125
 
 
126
int ff_id3v2_write_metadata(AVFormatContext *s, ID3v2EncContext *id3)
 
127
{
104
128
    AVDictionaryEntry *t = NULL;
105
 
 
106
 
    int totlen = 0, enc = id3v2_version == 3 ? ID3v2_ENCODING_UTF16BOM :
107
 
                                               ID3v2_ENCODING_UTF8;
108
 
 
109
 
 
110
 
    avio_wb32(s->pb, MKBETAG(magic[0], magic[1], magic[2], id3v2_version));
111
 
    avio_w8(s->pb, 0);
112
 
    avio_w8(s->pb, 0); /* flags */
113
 
 
114
 
    /* reserve space for size */
115
 
    size_pos = avio_tell(s->pb);
116
 
    avio_wb32(s->pb, 0);
 
129
    int enc = id3->version == 3 ? ID3v2_ENCODING_UTF16BOM :
 
130
                                  ID3v2_ENCODING_UTF8;
117
131
 
118
132
    ff_metadata_conv(&s->metadata, ff_id3v2_34_metadata_conv, NULL);
119
 
    if (id3v2_version == 4)
 
133
    if (id3->version == 4)
120
134
        ff_metadata_conv(&s->metadata, ff_id3v2_4_metadata_conv, NULL);
121
135
 
122
136
    while ((t = av_dict_get(s->metadata, "", t, AV_DICT_IGNORE_SUFFIX))) {
123
137
        int ret;
124
138
 
125
 
        if ((ret = id3v2_check_write_tag(s, t, ff_id3v2_tags, enc)) > 0) {
126
 
            totlen += ret;
 
139
        if ((ret = id3v2_check_write_tag(id3, s->pb, t, ff_id3v2_tags, enc)) > 0) {
 
140
            id3->len += ret;
127
141
            continue;
128
142
        }
129
 
        if ((ret = id3v2_check_write_tag(s, t, id3v2_version == 3 ?
 
143
        if ((ret = id3v2_check_write_tag(id3, s->pb, t, id3->version == 3 ?
130
144
                                               ff_id3v2_3_tags : ff_id3v2_4_tags, enc)) > 0) {
131
 
            totlen += ret;
 
145
            id3->len += ret;
132
146
            continue;
133
147
        }
134
148
 
135
149
        /* unknown tag, write as TXXX frame */
136
 
        if ((ret = id3v2_put_ttag(s, t->key, t->value, MKBETAG('T', 'X', 'X', 'X'), enc)) < 0)
 
150
        if ((ret = id3v2_put_ttag(id3, s->pb, t->key, t->value, MKBETAG('T', 'X', 'X', 'X'), enc)) < 0)
137
151
            return ret;
138
 
        totlen += ret;
139
 
    }
140
 
 
141
 
    cur_pos = avio_tell(s->pb);
142
 
    avio_seek(s->pb, size_pos, SEEK_SET);
143
 
    id3v2_put_size(s, totlen);
144
 
    avio_seek(s->pb, cur_pos, SEEK_SET);
 
152
        id3->len += ret;
 
153
    }
 
154
 
 
155
    return 0;
 
156
}
 
157
 
 
158
int ff_id3v2_write_apic(AVFormatContext *s, ID3v2EncContext *id3, AVPacket *pkt)
 
159
{
 
160
    AVStream *st = s->streams[pkt->stream_index];
 
161
    AVDictionaryEntry *e;
 
162
 
 
163
    AVIOContext *dyn_buf;
 
164
    uint8_t     *buf;
 
165
    const CodecMime *mime = ff_id3v2_mime_tags;
 
166
    const char  *mimetype = NULL, *desc = "";
 
167
    int enc = id3->version == 3 ? ID3v2_ENCODING_UTF16BOM :
 
168
                                  ID3v2_ENCODING_UTF8;
 
169
    int i, len, type = 0;
 
170
 
 
171
    /* get the mimetype*/
 
172
    while (mime->id != AV_CODEC_ID_NONE) {
 
173
        if (mime->id == st->codec->codec_id) {
 
174
            mimetype = mime->str;
 
175
            break;
 
176
        }
 
177
        mime++;
 
178
    }
 
179
    if (!mimetype) {
 
180
        av_log(s, AV_LOG_ERROR, "No mimetype is known for stream %d, cannot "
 
181
               "write an attached picture.\n", st->index);
 
182
        return AVERROR(EINVAL);
 
183
    }
 
184
 
 
185
    /* get the picture type */
 
186
    e = av_dict_get(st->metadata, "comment", NULL, 0);
 
187
    for (i = 0; e && i < FF_ARRAY_ELEMS(ff_id3v2_picture_types); i++) {
 
188
        if (strstr(ff_id3v2_picture_types[i], e->value) == ff_id3v2_picture_types[i]) {
 
189
            type = i;
 
190
            break;
 
191
        }
 
192
    }
 
193
 
 
194
    /* get the description */
 
195
    if ((e = av_dict_get(st->metadata, "title", NULL, 0)))
 
196
        desc = e->value;
 
197
 
 
198
    /* start writing */
 
199
    if (avio_open_dyn_buf(&dyn_buf) < 0)
 
200
        return AVERROR(ENOMEM);
 
201
 
 
202
    avio_w8(dyn_buf, enc);
 
203
    avio_put_str(dyn_buf, mimetype);
 
204
    avio_w8(dyn_buf, type);
 
205
    id3v2_encode_string(dyn_buf, desc, enc);
 
206
    avio_write(dyn_buf, pkt->data, pkt->size);
 
207
    len = avio_close_dyn_buf(dyn_buf, &buf);
 
208
 
 
209
    avio_wb32(s->pb, MKBETAG('A', 'P', 'I', 'C'));
 
210
    if (id3->version == 3)
 
211
        avio_wb32(s->pb, len);
 
212
    else
 
213
        id3v2_put_size(s->pb, len);
 
214
    avio_wb16(s->pb, 0);
 
215
    avio_write(s->pb, buf, len);
 
216
    av_freep(&buf);
 
217
 
 
218
    id3->len += len + ID3v2_HEADER_SIZE;
 
219
 
 
220
    return 0;
 
221
}
 
222
 
 
223
void ff_id3v2_finish(ID3v2EncContext *id3, AVIOContext *pb)
 
224
{
 
225
    int64_t cur_pos = avio_tell(pb);
 
226
    avio_seek(pb, id3->size_pos, SEEK_SET);
 
227
    id3v2_put_size(pb, id3->len);
 
228
    avio_seek(pb, cur_pos, SEEK_SET);
 
229
}
 
230
 
 
231
int ff_id3v2_write_simple(struct AVFormatContext *s, int id3v2_version,
 
232
                          const char *magic)
 
233
{
 
234
    ID3v2EncContext id3 = { 0 };
 
235
    int ret;
 
236
 
 
237
    ff_id3v2_start(&id3, s->pb, id3v2_version, magic);
 
238
    if ((ret = ff_id3v2_write_metadata(s, &id3)) < 0)
 
239
        return ret;
 
240
    ff_id3v2_finish(&id3, s->pb);
 
241
 
145
242
    return 0;
146
243
}