Statistics
| Branch: | Revision:

ffmpeg / libavformat / flacenc.c @ 66061a12

History | View | Annotate | Download (4.35 KB)

1
/*
2
 * raw FLAC muxer
3
 * Copyright (c) 2006-2009 Justin Ruggles
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 "libavcodec/flac.h"
23
#include "avformat.h"
24
#include "flacenc.h"
25
#include "metadata.h"
26
#include "vorbiscomment.h"
27
#include "libavcodec/bytestream.h"
28

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

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

    
42
    /* write "fLaC" stream marker and first metadata block header if needed */
43
    if (format == FLAC_EXTRADATA_FORMAT_STREAMINFO) {
44
        put_buffer(pb, header, 8);
45
    }
46

    
47
    /* write STREAMINFO or full header */
48
    put_buffer(pb, codec->extradata, codec->extradata_size);
49

    
50
    return 0;
51
}
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

    
89
static int flac_write_header(struct AVFormatContext *s)
90
{
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;
110
}
111

    
112
static int flac_write_trailer(struct AVFormatContext *s)
113
{
114
    ByteIOContext *pb = s->pb;
115
    uint8_t *streaminfo;
116
    enum FLACExtradataFormat format;
117
    int64_t file_size;
118

    
119
    if (!ff_flac_is_extradata_valid(s->streams[0]->codec, &format, &streaminfo))
120
        return -1;
121

    
122
    if (!url_is_streamed(pb)) {
123
        /* rewrite the STREAMINFO header block data */
124
        file_size = url_ftell(pb);
125
        url_fseek(pb, 8, SEEK_SET);
126
        put_buffer(pb, streaminfo, FLAC_STREAMINFO_SIZE);
127
        url_fseek(pb, file_size, SEEK_SET);
128
        put_flush_packet(pb);
129
    } else {
130
        av_log(s, AV_LOG_WARNING, "unable to rewrite FLAC header.\n");
131
    }
132
    return 0;
133
}
134

    
135
static int flac_write_packet(struct AVFormatContext *s, AVPacket *pkt)
136
{
137
    put_buffer(s->pb, pkt->data, pkt->size);
138
    put_flush_packet(s->pb);
139
    return 0;
140
}
141

    
142
AVOutputFormat flac_muxer = {
143
    "flac",
144
    NULL_IF_CONFIG_SMALL("raw FLAC"),
145
    "audio/x-flac",
146
    "flac",
147
    0,
148
    CODEC_ID_FLAC,
149
    CODEC_ID_NONE,
150
    flac_write_header,
151
    flac_write_packet,
152
    flac_write_trailer,
153
    .flags= AVFMT_NOTIMESTAMPS,
154
    .metadata_conv = ff_vorbiscomment_metadata_conv,
155
};