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