Revision 66061a12 libavformat/flacenc.c

View differences:

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
};

Also available in: Unified diff