Mercurial > libavformat.hg
annotate oggenc.c @ 3020:38777f77320e libavformat
it seems ogg requires granule to be -1 on unfinished packets
author | bcoudurier |
---|---|
date | Tue, 05 Feb 2008 01:01:56 +0000 |
parents | d52c718e83f9 |
children | 6f393e79e896 |
rev | line source |
---|---|
2731 | 1 /* |
2 * Ogg muxer | |
3 * Copyright (c) 2007 Baptiste Coudurier <baptiste dot coudurier at free dot fr> | |
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 "crc.h" | |
24 #include "xiph.h" | |
25 #include "bytestream.h" | |
26 | |
27 typedef struct { | |
28 int64_t duration; | |
29 unsigned page_counter; | |
30 uint8_t *header[3]; | |
31 int header_len[3]; | |
32 /** for theora granule */ | |
33 int kfgshift; | |
34 int64_t last_kf_pts; | |
35 int vrev; | |
36 } OGGStreamContext; | |
37 | |
38 static void ogg_update_checksum(AVFormatContext *s, offset_t crc_offset) | |
39 { | |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2731
diff
changeset
|
40 offset_t pos = url_ftell(s->pb); |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2731
diff
changeset
|
41 uint32_t checksum = get_checksum(s->pb); |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2731
diff
changeset
|
42 url_fseek(s->pb, crc_offset, SEEK_SET); |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2731
diff
changeset
|
43 put_be32(s->pb, checksum); |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2731
diff
changeset
|
44 url_fseek(s->pb, pos, SEEK_SET); |
2731 | 45 } |
46 | |
47 static int ogg_write_page(AVFormatContext *s, const uint8_t *data, int size, | |
48 int64_t granule, int stream_index, int flags) | |
49 { | |
50 OGGStreamContext *oggstream = s->streams[stream_index]->priv_data; | |
51 offset_t crc_offset; | |
52 int page_segments, i; | |
53 | |
3020
38777f77320e
it seems ogg requires granule to be -1 on unfinished packets
bcoudurier
parents:
2771
diff
changeset
|
54 if (size >= 255*255) { |
38777f77320e
it seems ogg requires granule to be -1 on unfinished packets
bcoudurier
parents:
2771
diff
changeset
|
55 granule = -1; |
38777f77320e
it seems ogg requires granule to be -1 on unfinished packets
bcoudurier
parents:
2771
diff
changeset
|
56 size = 255*255; |
38777f77320e
it seems ogg requires granule to be -1 on unfinished packets
bcoudurier
parents:
2771
diff
changeset
|
57 } |
38777f77320e
it seems ogg requires granule to be -1 on unfinished packets
bcoudurier
parents:
2771
diff
changeset
|
58 |
2731 | 59 page_segments = FFMIN((size/255)+!!size, 255); |
60 | |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2731
diff
changeset
|
61 init_checksum(s->pb, ff_crc04C11DB7_update, 0); |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2731
diff
changeset
|
62 put_tag(s->pb, "OggS"); |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2731
diff
changeset
|
63 put_byte(s->pb, 0); |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2731
diff
changeset
|
64 put_byte(s->pb, flags); |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2731
diff
changeset
|
65 put_le64(s->pb, granule); |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2731
diff
changeset
|
66 put_le32(s->pb, stream_index); |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2731
diff
changeset
|
67 put_le32(s->pb, oggstream->page_counter++); |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2731
diff
changeset
|
68 crc_offset = url_ftell(s->pb); |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2731
diff
changeset
|
69 put_le32(s->pb, 0); // crc |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2731
diff
changeset
|
70 put_byte(s->pb, page_segments); |
2731 | 71 for (i = 0; i < page_segments-1; i++) |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2731
diff
changeset
|
72 put_byte(s->pb, 255); |
2731 | 73 if (size) { |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2731
diff
changeset
|
74 put_byte(s->pb, size - (page_segments-1)*255); |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2731
diff
changeset
|
75 put_buffer(s->pb, data, size); |
2731 | 76 } |
77 ogg_update_checksum(s, crc_offset); | |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2731
diff
changeset
|
78 put_flush_packet(s->pb); |
2731 | 79 return size; |
80 } | |
81 | |
82 static int ogg_build_flac_headers(const uint8_t *extradata, int extradata_size, | |
83 OGGStreamContext *oggstream, int bitexact) | |
84 { | |
85 const char *vendor = bitexact ? "ffmpeg" : LIBAVFORMAT_IDENT; | |
86 uint8_t *p; | |
87 if (extradata_size != 34) | |
88 return -1; | |
89 oggstream->header_len[0] = 79; | |
90 oggstream->header[0] = av_mallocz(79); // per ogg flac specs | |
91 p = oggstream->header[0]; | |
92 bytestream_put_byte(&p, 0x7F); | |
93 bytestream_put_buffer(&p, "FLAC", 4); | |
94 bytestream_put_byte(&p, 1); // major version | |
95 bytestream_put_byte(&p, 0); // minor version | |
96 bytestream_put_be16(&p, 1); // headers packets without this one | |
97 bytestream_put_buffer(&p, "fLaC", 4); | |
98 bytestream_put_byte(&p, 0x00); // streaminfo | |
99 bytestream_put_be24(&p, 34); | |
100 bytestream_put_buffer(&p, extradata, 34); | |
101 oggstream->header_len[1] = 1+3+4+strlen(vendor)+4; | |
102 oggstream->header[1] = av_mallocz(oggstream->header_len[1]); | |
103 p = oggstream->header[1]; | |
104 bytestream_put_byte(&p, 0x84); // last metadata block and vorbis comment | |
105 bytestream_put_be24(&p, oggstream->header_len[1] - 4); | |
106 bytestream_put_le32(&p, strlen(vendor)); | |
107 bytestream_put_buffer(&p, vendor, strlen(vendor)); | |
108 bytestream_put_le32(&p, 0); // user comment list length | |
109 return 0; | |
110 } | |
111 | |
112 static int ogg_write_header(AVFormatContext *s) | |
113 { | |
114 OGGStreamContext *oggstream; | |
115 int i, j; | |
116 for (i = 0; i < s->nb_streams; i++) { | |
117 AVStream *st = s->streams[i]; | |
118 if (st->codec->codec_type == CODEC_TYPE_AUDIO) | |
119 av_set_pts_info(st, 64, 1, st->codec->sample_rate); | |
120 else if (st->codec->codec_type == CODEC_TYPE_VIDEO) | |
121 av_set_pts_info(st, 64, st->codec->time_base.num, st->codec->time_base.den); | |
122 if (st->codec->codec_id != CODEC_ID_VORBIS && | |
123 st->codec->codec_id != CODEC_ID_THEORA && | |
124 st->codec->codec_id != CODEC_ID_FLAC) { | |
125 av_log(s, AV_LOG_ERROR, "Unsupported codec id in stream %d\n", i); | |
126 return -1; | |
127 } | |
128 | |
129 if (!st->codec->extradata || !st->codec->extradata_size) { | |
130 av_log(s, AV_LOG_ERROR, "No extradata present\n"); | |
131 return -1; | |
132 } | |
133 oggstream = av_mallocz(sizeof(*oggstream)); | |
134 st->priv_data = oggstream; | |
135 if (st->codec->codec_id == CODEC_ID_FLAC) { | |
136 if (ogg_build_flac_headers(st->codec->extradata, st->codec->extradata_size, | |
137 oggstream, st->codec->flags & CODEC_FLAG_BITEXACT) < 0) { | |
138 av_log(s, AV_LOG_ERROR, "Extradata corrupted\n"); | |
139 av_freep(&st->priv_data); | |
140 } | |
141 } else { | |
142 if (ff_split_xiph_headers(st->codec->extradata, st->codec->extradata_size, | |
143 st->codec->codec_id == CODEC_ID_VORBIS ? 30 : 42, | |
144 oggstream->header, oggstream->header_len) < 0) { | |
145 av_log(s, AV_LOG_ERROR, "Extradata corrupted\n"); | |
146 av_freep(&st->priv_data); | |
147 return -1; | |
148 } | |
149 if (st->codec->codec_id == CODEC_ID_THEORA) { | |
150 /** KFGSHIFT is the width of the less significant section of the granule position | |
151 The less significant section is the frame count since the last keyframe */ | |
152 oggstream->kfgshift = ((oggstream->header[0][40]&3)<<3)|(oggstream->header[0][41]>>5); | |
153 oggstream->vrev = oggstream->header[0][9]; | |
154 av_log(s, AV_LOG_DEBUG, "theora kfgshift %d, vrev %d\n", | |
155 oggstream->kfgshift, oggstream->vrev); | |
156 } | |
157 } | |
158 } | |
159 for (i = 0; i < 3; i++) { | |
160 for (j = 0; j < s->nb_streams; j++) { | |
161 AVStream *st = s->streams[j]; | |
162 OGGStreamContext *oggstream = st->priv_data; | |
163 if (oggstream && oggstream->header_len[i]) { | |
164 ogg_write_page(s, oggstream->header[i], oggstream->header_len[i], | |
165 0, st->index, i ? 0 : 2); // bos | |
166 } | |
167 } | |
168 } | |
169 return 0; | |
170 } | |
171 | |
172 static int ogg_write_packet(AVFormatContext *s, AVPacket *pkt) | |
173 { | |
174 AVStream *st = s->streams[pkt->stream_index]; | |
175 OGGStreamContext *oggstream = st->priv_data; | |
176 uint8_t *ptr = pkt->data; | |
177 int ret, size = pkt->size; | |
178 int64_t granule; | |
179 | |
180 if (st->codec->codec_id == CODEC_ID_THEORA) { | |
181 int64_t pts = oggstream->vrev < 1 ? pkt->pts : pkt->pts + pkt->duration; | |
182 int pframe_count; | |
183 if (pkt->flags & PKT_FLAG_KEY) | |
184 oggstream->last_kf_pts = pts; | |
185 pframe_count = pts - oggstream->last_kf_pts; | |
186 // prevent frame count from overflow if key frame flag is not set | |
187 if (pframe_count >= (1<<oggstream->kfgshift)) { | |
188 oggstream->last_kf_pts += pframe_count; | |
189 pframe_count = 0; | |
190 } | |
191 granule = (oggstream->last_kf_pts<<oggstream->kfgshift) | pframe_count; | |
192 } else | |
193 granule = pkt->pts + pkt->duration; | |
194 oggstream->duration = granule; | |
195 do { | |
196 ret = ogg_write_page(s, ptr, size, granule, pkt->stream_index, ptr != pkt->data); | |
197 ptr += ret; size -= ret; | |
198 } while (size > 0 || ret == 255*255); // need to output a last nil page | |
199 | |
200 return 0; | |
201 } | |
202 | |
203 | |
204 static int ogg_write_trailer(AVFormatContext *s) | |
205 { | |
206 int i; | |
207 for (i = 0; i < s->nb_streams; i++) { | |
208 AVStream *st = s->streams[i]; | |
209 OGGStreamContext *oggstream = st->priv_data; | |
210 ogg_write_page(s, NULL, 0, oggstream->duration, i, 4); // eos | |
211 if (st->codec->codec_id == CODEC_ID_FLAC) { | |
212 av_free(oggstream->header[0]); | |
213 av_free(oggstream->header[1]); | |
214 } | |
215 av_freep(&st->priv_data); | |
216 } | |
217 return 0; | |
218 } | |
219 | |
220 AVOutputFormat ogg_muxer = { | |
221 "ogg", | |
222 "Ogg format", | |
223 "application/ogg", | |
224 "ogg", | |
225 0, | |
226 CODEC_ID_FLAC, | |
227 CODEC_ID_THEORA, | |
228 ogg_write_header, | |
229 ogg_write_packet, | |
230 ogg_write_trailer, | |
231 }; |