Mercurial > libavformat.hg
annotate flacenc.c @ 5857:121d6994c20e libavformat
Add VorbisComment writing to FLAC files.
Patch by James Darnley <james darnley at gmail>.
author | jbr |
---|---|
date | Sat, 20 Mar 2010 13:36:43 +0000 |
parents | c52d40f0a955 |
children | 92daebc423f4 |
rev | line source |
---|---|
885 | 1 /* |
4577
1cc2041c2e03
Separate the raw FLAC muxer from raw.c to its own file, flacenc.c.
jbr
parents:
4563
diff
changeset
|
2 * raw FLAC muxer |
1cc2041c2e03
Separate the raw FLAC muxer from raw.c to its own file, flacenc.c.
jbr
parents:
4563
diff
changeset
|
3 * Copyright (c) 2006-2009 Justin Ruggles |
0 | 4 * |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1245
diff
changeset
|
5 * This file is part of FFmpeg. |
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1245
diff
changeset
|
6 * |
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1245
diff
changeset
|
7 * FFmpeg is free software; you can redistribute it and/or |
0 | 8 * modify it under the terms of the GNU Lesser General Public |
9 * License as published by the Free Software Foundation; either | |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1245
diff
changeset
|
10 * version 2.1 of the License, or (at your option) any later version. |
0 | 11 * |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1245
diff
changeset
|
12 * FFmpeg is distributed in the hope that it will be useful, |
0 | 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 | |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1245
diff
changeset
|
18 * License along with FFmpeg; if not, write to the Free Software |
896
edbe5c3717f9
Update licensing information: The FSF changed postal address.
diego
parents:
887
diff
changeset
|
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
0 | 20 */ |
3286 | 21 |
4578 | 22 #include "libavcodec/flac.h" |
0 | 23 #include "avformat.h" |
4581
c52d40f0a955
Share the function to write a raw FLAC header and use it in the Matroska
jbr
parents:
4580
diff
changeset
|
24 #include "flacenc.h" |
5857 | 25 #include "metadata.h" |
26 #include "vorbiscomment.h" | |
27 #include "libavcodec/bytestream.h" | |
0 | 28 |
5857 | 29 int ff_flac_write_header(ByteIOContext *pb, AVCodecContext *codec, |
30 int last_block) | |
1078
0bc9422cc0ad
Raw flac muxer, patch by Justin Ruggles (jruggle earthlink net). Can be
banan
parents:
1070
diff
changeset
|
31 { |
5857 | 32 uint8_t header[8] = { |
33 0x66, 0x4C, 0x61, 0x43, 0x00, 0x00, 0x00, 0x22 | |
1078
0bc9422cc0ad
Raw flac muxer, patch by Justin Ruggles (jruggle earthlink net). Can be
banan
parents:
1070
diff
changeset
|
34 }; |
4578 | 35 uint8_t *streaminfo; |
36 enum FLACExtradataFormat format; | |
37 | |
5857 | 38 header[4] = last_block ? 0x80 : 0x00; |
4578 | 39 if (!ff_flac_is_extradata_valid(codec, &format, &streaminfo)) |
40 return -1; | |
41 | |
4580 | 42 /* write "fLaC" stream marker and first metadata block header if needed */ |
4578 | 43 if (format == FLAC_EXTRADATA_FORMAT_STREAMINFO) { |
4581
c52d40f0a955
Share the function to write a raw FLAC header and use it in the Matroska
jbr
parents:
4580
diff
changeset
|
44 put_buffer(pb, header, 8); |
1078
0bc9422cc0ad
Raw flac muxer, patch by Justin Ruggles (jruggle earthlink net). Can be
banan
parents:
1070
diff
changeset
|
45 } |
4579
e5fca5c56cb2
Add support for full header extradata to raw FLAC muxer.
jbr
parents:
4578
diff
changeset
|
46 |
e5fca5c56cb2
Add support for full header extradata to raw FLAC muxer.
jbr
parents:
4578
diff
changeset
|
47 /* write STREAMINFO or full header */ |
4581
c52d40f0a955
Share the function to write a raw FLAC header and use it in the Matroska
jbr
parents:
4580
diff
changeset
|
48 put_buffer(pb, codec->extradata, codec->extradata_size); |
4579
e5fca5c56cb2
Add support for full header extradata to raw FLAC muxer.
jbr
parents:
4578
diff
changeset
|
49 |
1078
0bc9422cc0ad
Raw flac muxer, patch by Justin Ruggles (jruggle earthlink net). Can be
banan
parents:
1070
diff
changeset
|
50 return 0; |
0bc9422cc0ad
Raw flac muxer, patch by Justin Ruggles (jruggle earthlink net). Can be
banan
parents:
1070
diff
changeset
|
51 } |
3926 | 52 |
5857 | 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 | |
4581
c52d40f0a955
Share the function to write a raw FLAC header and use it in the Matroska
jbr
parents:
4580
diff
changeset
|
89 static int flac_write_header(struct AVFormatContext *s) |
c52d40f0a955
Share the function to write a raw FLAC header and use it in the Matroska
jbr
parents:
4580
diff
changeset
|
90 { |
5857 | 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; | |
4581
c52d40f0a955
Share the function to write a raw FLAC header and use it in the Matroska
jbr
parents:
4580
diff
changeset
|
110 } |
c52d40f0a955
Share the function to write a raw FLAC header and use it in the Matroska
jbr
parents:
4580
diff
changeset
|
111 |
3926 | 112 static int flac_write_trailer(struct AVFormatContext *s) |
113 { | |
114 ByteIOContext *pb = s->pb; | |
4578 | 115 uint8_t *streaminfo; |
116 enum FLACExtradataFormat format; | |
3973
549a09cf23fe
Remove offset_t typedef and use int64_t directly instead.
diego
parents:
3926
diff
changeset
|
117 int64_t file_size; |
3926 | 118 |
4578 | 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 */ | |
3926 | 124 file_size = url_ftell(pb); |
125 url_fseek(pb, 8, SEEK_SET); | |
4578 | 126 put_buffer(pb, streaminfo, FLAC_STREAMINFO_SIZE); |
3926 | 127 url_fseek(pb, file_size, SEEK_SET); |
128 put_flush_packet(pb); | |
4578 | 129 } else { |
130 av_log(s, AV_LOG_WARNING, "unable to rewrite FLAC header.\n"); | |
3926 | 131 } |
132 return 0; | |
133 } | |
1078
0bc9422cc0ad
Raw flac muxer, patch by Justin Ruggles (jruggle earthlink net). Can be
banan
parents:
1070
diff
changeset
|
134 |
4577
1cc2041c2e03
Separate the raw FLAC muxer from raw.c to its own file, flacenc.c.
jbr
parents:
4563
diff
changeset
|
135 static int flac_write_packet(struct AVFormatContext *s, AVPacket *pkt) |
0 | 136 { |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2545
diff
changeset
|
137 put_buffer(s->pb, pkt->data, pkt->size); |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2545
diff
changeset
|
138 put_flush_packet(s->pb); |
0 | 139 return 0; |
140 } | |
141 | |
3546
45c3d2b2b2fb
Alphabetically order AVInputFormat/AVOutputFormat declarations.
diego
parents:
3545
diff
changeset
|
142 AVOutputFormat flac_muxer = { |
45c3d2b2b2fb
Alphabetically order AVInputFormat/AVOutputFormat declarations.
diego
parents:
3545
diff
changeset
|
143 "flac", |
45c3d2b2b2fb
Alphabetically order AVInputFormat/AVOutputFormat declarations.
diego
parents:
3545
diff
changeset
|
144 NULL_IF_CONFIG_SMALL("raw FLAC"), |
45c3d2b2b2fb
Alphabetically order AVInputFormat/AVOutputFormat declarations.
diego
parents:
3545
diff
changeset
|
145 "audio/x-flac", |
45c3d2b2b2fb
Alphabetically order AVInputFormat/AVOutputFormat declarations.
diego
parents:
3545
diff
changeset
|
146 "flac", |
45c3d2b2b2fb
Alphabetically order AVInputFormat/AVOutputFormat declarations.
diego
parents:
3545
diff
changeset
|
147 0, |
45c3d2b2b2fb
Alphabetically order AVInputFormat/AVOutputFormat declarations.
diego
parents:
3545
diff
changeset
|
148 CODEC_ID_FLAC, |
45c3d2b2b2fb
Alphabetically order AVInputFormat/AVOutputFormat declarations.
diego
parents:
3545
diff
changeset
|
149 CODEC_ID_NONE, |
45c3d2b2b2fb
Alphabetically order AVInputFormat/AVOutputFormat declarations.
diego
parents:
3545
diff
changeset
|
150 flac_write_header, |
4577
1cc2041c2e03
Separate the raw FLAC muxer from raw.c to its own file, flacenc.c.
jbr
parents:
4563
diff
changeset
|
151 flac_write_packet, |
3926 | 152 flac_write_trailer, |
3546
45c3d2b2b2fb
Alphabetically order AVInputFormat/AVOutputFormat declarations.
diego
parents:
3545
diff
changeset
|
153 .flags= AVFMT_NOTIMESTAMPS, |
5857 | 154 .metadata_conv = ff_vorbiscomment_metadata_conv, |
3546
45c3d2b2b2fb
Alphabetically order AVInputFormat/AVOutputFormat declarations.
diego
parents:
3545
diff
changeset
|
155 }; |