Revision 66061a12

View differences:

libavformat/Makefile
69 69
OBJS-$(CONFIG_FILMSTRIP_DEMUXER)         += filmstripdec.o
70 70
OBJS-$(CONFIG_FILMSTRIP_MUXER)           += filmstripenc.o
71 71
OBJS-$(CONFIG_FLAC_DEMUXER)              += flacdec.o raw.o id3v1.o \
72
                                            id3v2.o oggparsevorbis.o
73
OBJS-$(CONFIG_FLAC_MUXER)                += flacenc.o
72
                                            id3v2.o \
73
                                            vorbiscomment.o
74
OBJS-$(CONFIG_FLAC_MUXER)                += flacenc.o vorbiscomment.o
74 75
OBJS-$(CONFIG_FLIC_DEMUXER)              += flic.o
75 76
OBJS-$(CONFIG_FLV_DEMUXER)               += flvdec.o
76 77
OBJS-$(CONFIG_FLV_MUXER)                 += flvenc.o avc.o
......
148 149
                                            oggparsespeex.o  \
149 150
                                            oggparsetheora.o \
150 151
                                            oggparsevorbis.o \
151
                                            riff.o
152
                                            riff.o \
153
                                            vorbiscomment.o
152 154
OBJS-$(CONFIG_OGG_MUXER)                 += oggenc.o
153 155
OBJS-$(CONFIG_OMA_DEMUXER)               += oma.o raw.o
154 156
OBJS-$(CONFIG_PCM_ALAW_DEMUXER)          += raw.o
libavformat/flacdec.c
24 24
#include "raw.h"
25 25
#include "id3v2.h"
26 26
#include "oggdec.h"
27
#include "vorbiscomment.h"
27 28

  
28 29
static int flac_read_header(AVFormatContext *s,
29 30
                             AVFormatParameters *ap)
libavformat/flacenc.c
22 22
#include "libavcodec/flac.h"
23 23
#include "avformat.h"
24 24
#include "flacenc.h"
25
#include "metadata.h"
26
#include "vorbiscomment.h"
27
#include "libavcodec/bytestream.h"
25 28

  
26
int ff_flac_write_header(ByteIOContext *pb, AVCodecContext *codec)
29
int ff_flac_write_header(ByteIOContext *pb, AVCodecContext *codec,
30
                         int last_block)
27 31
{
28
    static const uint8_t header[8] = {
29
        0x66, 0x4C, 0x61, 0x43, 0x80, 0x00, 0x00, 0x22
32
    uint8_t header[8] = {
33
        0x66, 0x4C, 0x61, 0x43, 0x00, 0x00, 0x00, 0x22
30 34
    };
31 35
    uint8_t *streaminfo;
32 36
    enum FLACExtradataFormat format;
33 37

  
38
    header[4] = last_block ? 0x80 : 0x00;
34 39
    if (!ff_flac_is_extradata_valid(codec, &format, &streaminfo))
35 40
        return -1;
36 41

  
......
45 50
    return 0;
46 51
}
47 52

  
53
static int flac_write_block_padding(ByteIOContext *pb, unsigned int n_padding_bytes,
54
                                    int last_block)
55
{
56
    put_byte(pb, last_block ? 0x81 : 0x01);
57
    put_be24(pb, n_padding_bytes);
58
    while (n_padding_bytes > 0) {
59
        put_byte(pb, 0);
60
        n_padding_bytes--;
61
    }
62
    return 0;
63
}
64

  
65
static int flac_write_block_comment(ByteIOContext *pb, AVMetadata *m,
66
                                    int last_block, int bitexact)
67
{
68
    const char *vendor = bitexact ? "ffmpeg" : LIBAVFORMAT_IDENT;
69
    unsigned int len, count;
70
    uint8_t *p, *p0;
71

  
72
    len = ff_vorbiscomment_length(m, vendor, &count);
73
    p0 = av_malloc(len+4);
74
    if (!p0)
75
        return AVERROR(ENOMEM);
76
    p = p0;
77

  
78
    bytestream_put_byte(&p, last_block ? 0x84 : 0x04);
79
    bytestream_put_be24(&p, len);
80
    ff_vorbiscomment_write(&p, m, vendor, count);
81

  
82
    put_buffer(pb, p0, len+4);
83
    av_freep(&p0);
84
    p = NULL;
85

  
86
    return 0;
87
}
88

  
48 89
static int flac_write_header(struct AVFormatContext *s)
49 90
{
50
    return ff_flac_write_header(s->pb, s->streams[0]->codec);
91
    int ret;
92
    AVCodecContext *codec = s->streams[0]->codec;
93

  
94
    ret = ff_flac_write_header(s->pb, codec, 0);
95
    if (ret)
96
        return ret;
97

  
98
    ret = flac_write_block_comment(s->pb, s->metadata, 0,
99
                                   codec->flags & CODEC_FLAG_BITEXACT);
100
    if (ret)
101
        return ret;
102

  
103
    /* The command line flac encoder defaults to placing a seekpoint
104
     * every 10s.  So one might add padding to allow that later
105
     * but there seems to be no simple way to get the duration here.
106
     * So let's try the flac default of 8192 bytes */
107
    flac_write_block_padding(s->pb, 8192, 1);
108

  
109
    return ret;
51 110
}
52 111

  
53 112
static int flac_write_trailer(struct AVFormatContext *s)
......
92 151
    flac_write_packet,
93 152
    flac_write_trailer,
94 153
    .flags= AVFMT_NOTIMESTAMPS,
154
    .metadata_conv = ff_vorbiscomment_metadata_conv,
95 155
};
libavformat/flacenc.h
24 24

  
25 25
#include "avformat.h"
26 26

  
27
int ff_flac_write_header(ByteIOContext *pb, AVCodecContext *codec);
27
int ff_flac_write_header(ByteIOContext *pb, AVCodecContext *codec,
28
                         int last_block);
28 29

  
29 30
#endif /* AVFORMAT_FLACENC_H */
libavformat/matroskaenc.c
470 470
        if (codec->codec_id == CODEC_ID_VORBIS || codec->codec_id == CODEC_ID_THEORA)
471 471
            ret = put_xiph_codecpriv(s, dyn_cp, codec);
472 472
        else if (codec->codec_id == CODEC_ID_FLAC)
473
            ret = ff_flac_write_header(dyn_cp, codec);
473
            ret = ff_flac_write_header(dyn_cp, codec, 1);
474 474
        else if (codec->codec_id == CODEC_ID_H264)
475 475
            ret = ff_isom_write_avcc(dyn_cp, codec->extradata, codec->extradata_size);
476 476
        else if (codec->extradata_size)
libavformat/oggdec.c
33 33
#include <stdio.h>
34 34
#include "oggdec.h"
35 35
#include "avformat.h"
36
#include "vorbiscomment.h"
36 37

  
37 38
#define MAX_PAGE_SIZE 65307
38 39
#define DECODER_BUFFER_SIZE MAX_PAGE_SIZE
libavformat/oggdec.h
112 112
extern const struct ogg_codec ff_theora_codec;
113 113
extern const struct ogg_codec ff_vorbis_codec;
114 114

  
115
extern const AVMetadataConv ff_vorbiscomment_metadata_conv[];
116

  
117 115
int ff_vorbis_comment(AVFormatContext *ms, AVMetadata **m, const uint8_t *buf, int size);
118 116

  
119 117
static inline int
libavformat/oggparsevorbis.c
30 30
#include "avformat.h"
31 31
#include "oggdec.h"
32 32

  
33
/**
34
 * VorbisComment metadata conversion mapping.
35
 * from Ogg Vorbis I format specification: comment field and header specification
36
 * http://xiph.org/vorbis/doc/v-comment.html
37
 */
38
const AVMetadataConv ff_vorbiscomment_metadata_conv[] = {
39
    { "ALBUMARTIST", "album_artist"},
40
    { "TRACKNUMBER", "track"  },
41
    { 0 }
42
};
43

  
44 33
static int ogm_chapter(AVFormatContext *as, uint8_t *key, uint8_t *val)
45 34
{
46 35
    int i, cnum, h, m, s, ms, keylen = strlen(key);
libavformat/vorbiscomment.c
1
/*
2
 * VorbisComment writer
3
 * Copyright (c) 2009 James Darnley
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 "metadata.h"
24
#include "vorbiscomment.h"
25
#include "libavcodec/bytestream.h"
26

  
27
/**
28
 * VorbisComment metadata conversion mapping.
29
 * from Ogg Vorbis I format specification: comment field and header specification
30
 * http://xiph.org/vorbis/doc/v-comment.html
31
 */
32
const AVMetadataConv ff_vorbiscomment_metadata_conv[] = {
33
    { "ALBUMARTIST", "album_artist"},
34
    { "TRACKNUMBER", "track"  },
35
    { 0 }
36
};
37

  
38
int ff_vorbiscomment_length(AVMetadata *m, const char *vendor_string,
39
                            unsigned *count)
40
{
41
    int len = 8;
42
    len += strlen(vendor_string);
43
    *count = 0;
44
    if (m) {
45
        AVMetadataTag *tag = NULL;
46
        while ( (tag = av_metadata_get(m, "", tag, AV_METADATA_IGNORE_SUFFIX) ) ) {
47
            len += 4 +strlen(tag->key) + 1 + strlen(tag->value);
48
            (*count)++;
49
        }
50
    }
51
    return len;
52
}
53

  
54
int ff_vorbiscomment_write(uint8_t **p, AVMetadata *m,
55
                           const char *vendor_string, const unsigned count)
56
{
57
    bytestream_put_le32(p, strlen(vendor_string));
58
    bytestream_put_buffer(p, vendor_string, strlen(vendor_string));
59
    if (m) {
60
        AVMetadataTag *tag = NULL;
61
        bytestream_put_le32(p, count);
62
        while ( (tag = av_metadata_get(m, "", tag, AV_METADATA_IGNORE_SUFFIX) ) ) {
63
            unsigned int len1 = strlen(tag->key);
64
            unsigned int len2 = strlen(tag->value);
65
            bytestream_put_le32(p, len1+1+len2);
66
            bytestream_put_buffer(p, tag->key, len1);
67
            bytestream_put_byte(p, '=');
68
            bytestream_put_buffer(p, tag->value, len2);
69
        }
70
    } else
71
        bytestream_put_le32(p, 0);
72
    return 0;
73
}
libavformat/vorbiscomment.h
1
/*
2
 * VorbisComment writer
3
 * Copyright (c) 2009 James Darnley
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
#ifndef AVFORMAT_VORBISCOMMENT_H
23
#define AVFORMAT_VORBISCOMMENT_H
24

  
25
#include "avformat.h"
26
#include "metadata.h"
27

  
28
/**
29
 * Calculates the length in bytes of a VorbisComment. This is the minimum
30
 * size required by ff_vorbiscomment_write().
31
 *
32
 * @param m The metadata structure to be parsed. For no metadata, set to NULL.
33
 * @param vendor_string The vendor string to be added into the VorbisComment.
34
 * For no string, set to an empty string.
35
 * @param count Pointer to store the number of tags in m because m->count is "not allowed"
36
 * @return The length in bytes.
37
 */
38
int ff_vorbiscomment_length(AVMetadata *m, const char *vendor_string,
39
                            unsigned *count);
40

  
41
/**
42
 * Writes a VorbisComment into a buffer. The buffer, p, must have enough
43
 * data to hold the whole VorbisComment. The minimum size required can be
44
 * obtained by passing the same AVMetadata and vendor_string to
45
 * ff_vorbiscomment_length()
46
 *
47
 * @param p The buffer in which to write.
48
 * @param m The metadata struct to write.
49
 * @param vendor_string The vendor string to write.
50
 * @param count The number of tags in m because m->count is "not allowed"
51
 */
52
int ff_vorbiscomment_write(uint8_t **p, AVMetadata *m,
53
                           const char *vendor_string, const unsigned count);
54

  
55
extern const AVMetadataConv ff_vorbiscomment_metadata_conv[];
56

  
57
#endif /* AVFORMAT_VORBISCOMMENT_H */
tests/ref/acodec/flac
1
7781a016edfc242a39e4d65af02d861a *./tests/data/acodec/flac.flac
2
353368 ./tests/data/acodec/flac.flac
1
151eef9097f944726968bec48649f00a *./tests/data/acodec/flac.flac
2
361582 ./tests/data/acodec/flac.flac
3 3
95e54b261530a1bcf6de6fe3b21dc5f6 *./tests/data/flac.acodec.out.wav
4 4
stddev:    0.00 PSNR:999.99 bytes:  1058444/  1058444
tests/ref/seek/flac.flac.ref
1
ret: 0         st: 0 flags:1 dts: NOPTS    pts: NOPTS    pos:     42 size:  1024
1
ret: 0         st: 0 flags:1 dts: NOPTS    pts: NOPTS    pos:   8256 size:  1024
2 2
ret:-1         st:-1 flags:0  ts:-1.000000
3 3
ret:-1         st:-1 flags:1  ts: 1.894167
4 4
ret:-1         st: 0 flags:0  ts: 0.788345

Also available in: Unified diff