00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "libavutil/crc.h"
00023 #include "libavutil/opt.h"
00024 #include "libavutil/random_seed.h"
00025 #include "libavcodec/xiph.h"
00026 #include "libavcodec/bytestream.h"
00027 #include "libavcodec/flac.h"
00028 #include "avformat.h"
00029 #include "avio_internal.h"
00030 #include "internal.h"
00031 #include "vorbiscomment.h"
00032
00033 #define MAX_PAGE_SIZE 65025
00034
00035 typedef struct {
00036 int64_t granule;
00037 int stream_index;
00038 uint8_t flags;
00039 uint8_t segments_count;
00040 uint8_t segments[255];
00041 uint8_t data[MAX_PAGE_SIZE];
00042 uint16_t size;
00043 } OGGPage;
00044
00045 typedef struct {
00046 unsigned page_counter;
00047 uint8_t *header[3];
00048 int header_len[3];
00050 int kfgshift;
00051 int64_t last_kf_pts;
00052 int vrev;
00053 int eos;
00054 unsigned page_count;
00055 OGGPage page;
00056 unsigned serial_num;
00057 int64_t last_granule;
00058 } OGGStreamContext;
00059
00060 typedef struct OGGPageList {
00061 OGGPage page;
00062 struct OGGPageList *next;
00063 } OGGPageList;
00064
00065 typedef struct {
00066 const AVClass *class;
00067 OGGPageList *page_list;
00068 int pref_size;
00069 } OGGContext;
00070
00071
00072 static const AVOption options[] = {
00073 { "oggpagesize", "Set preferred Ogg page size.",
00074 offsetof(OGGContext, pref_size), FF_OPT_TYPE_INT, {.dbl=0}, 0, MAX_PAGE_SIZE, AV_OPT_FLAG_ENCODING_PARAM},
00075 { NULL },
00076 };
00077
00078 static const AVClass ogg_muxer_class = {
00079 "Ogg muxer",
00080 av_default_item_name,
00081 options,
00082 LIBAVUTIL_VERSION_INT,
00083 };
00084
00085
00086 static void ogg_update_checksum(AVFormatContext *s, AVIOContext *pb, int64_t crc_offset)
00087 {
00088 int64_t pos = avio_tell(pb);
00089 uint32_t checksum = ffio_get_checksum(pb);
00090 avio_seek(pb, crc_offset, SEEK_SET);
00091 avio_wb32(pb, checksum);
00092 avio_seek(pb, pos, SEEK_SET);
00093 }
00094
00095 static int ogg_write_page(AVFormatContext *s, OGGPage *page, int extra_flags)
00096 {
00097 OGGStreamContext *oggstream = s->streams[page->stream_index]->priv_data;
00098 AVIOContext *pb;
00099 int64_t crc_offset;
00100 int ret, size;
00101 uint8_t *buf;
00102
00103 ret = avio_open_dyn_buf(&pb);
00104 if (ret < 0)
00105 return ret;
00106 ffio_init_checksum(pb, ff_crc04C11DB7_update, 0);
00107 ffio_wfourcc(pb, "OggS");
00108 avio_w8(pb, 0);
00109 avio_w8(pb, page->flags | extra_flags);
00110 avio_wl64(pb, page->granule);
00111 avio_wl32(pb, oggstream->serial_num);
00112 avio_wl32(pb, oggstream->page_counter++);
00113 crc_offset = avio_tell(pb);
00114 avio_wl32(pb, 0);
00115 avio_w8(pb, page->segments_count);
00116 avio_write(pb, page->segments, page->segments_count);
00117 avio_write(pb, page->data, page->size);
00118
00119 ogg_update_checksum(s, pb, crc_offset);
00120 avio_flush(pb);
00121
00122 size = avio_close_dyn_buf(pb, &buf);
00123 if (size < 0)
00124 return size;
00125
00126 avio_write(s->pb, buf, size);
00127 avio_flush(s->pb);
00128 av_free(buf);
00129 oggstream->page_count--;
00130 return 0;
00131 }
00132
00133 static int64_t ogg_granule_to_timestamp(OGGStreamContext *oggstream, int64_t granule)
00134 {
00135 if (oggstream->kfgshift)
00136 return (granule>>oggstream->kfgshift) +
00137 (granule & ((1<<oggstream->kfgshift)-1));
00138 else
00139 return granule;
00140 }
00141
00142 static int ogg_compare_granule(AVFormatContext *s, OGGPage *next, OGGPage *page)
00143 {
00144 AVStream *st2 = s->streams[next->stream_index];
00145 AVStream *st = s->streams[page->stream_index];
00146 int64_t next_granule, cur_granule;
00147
00148 if (next->granule == -1 || page->granule == -1)
00149 return 0;
00150
00151 next_granule = av_rescale_q(ogg_granule_to_timestamp(st2->priv_data, next->granule),
00152 st2->time_base, AV_TIME_BASE_Q);
00153 cur_granule = av_rescale_q(ogg_granule_to_timestamp(st->priv_data, page->granule),
00154 st ->time_base, AV_TIME_BASE_Q);
00155 return next_granule > cur_granule;
00156 }
00157
00158 static int ogg_reset_cur_page(OGGStreamContext *oggstream)
00159 {
00160 oggstream->page.granule = -1;
00161 oggstream->page.flags = 0;
00162 oggstream->page.segments_count = 0;
00163 oggstream->page.size = 0;
00164 return 0;
00165 }
00166
00167 static int ogg_buffer_page(AVFormatContext *s, OGGStreamContext *oggstream)
00168 {
00169 OGGContext *ogg = s->priv_data;
00170 OGGPageList **p = &ogg->page_list;
00171 OGGPageList *l = av_mallocz(sizeof(*l));
00172
00173 if (!l)
00174 return AVERROR(ENOMEM);
00175 l->page = oggstream->page;
00176
00177 oggstream->page_count++;
00178 ogg_reset_cur_page(oggstream);
00179
00180 while (*p) {
00181 if (ogg_compare_granule(s, &(*p)->page, &l->page))
00182 break;
00183 p = &(*p)->next;
00184 }
00185 l->next = *p;
00186 *p = l;
00187
00188 return 0;
00189 }
00190
00191 static int ogg_buffer_data(AVFormatContext *s, AVStream *st,
00192 uint8_t *data, unsigned size, int64_t granule)
00193 {
00194 OGGStreamContext *oggstream = st->priv_data;
00195 OGGContext *ogg = s->priv_data;
00196 int total_segments = size / 255 + 1;
00197 uint8_t *p = data;
00198 int i, segments, len, flush = 0;
00199
00200
00201 if (st->codec->codec_id == CODEC_ID_THEORA &&
00202 ogg_granule_to_timestamp(oggstream, granule) >
00203 ogg_granule_to_timestamp(oggstream, oggstream->last_granule) + 1) {
00204 if (oggstream->page.granule != -1)
00205 ogg_buffer_page(s, oggstream);
00206 flush = 1;
00207 }
00208
00209 for (i = 0; i < total_segments; ) {
00210 OGGPage *page = &oggstream->page;
00211
00212 segments = FFMIN(total_segments - i, 255 - page->segments_count);
00213
00214 if (i && !page->segments_count)
00215 page->flags |= 1;
00216
00217 memset(page->segments+page->segments_count, 255, segments - 1);
00218 page->segments_count += segments - 1;
00219
00220 len = FFMIN(size, segments*255);
00221 page->segments[page->segments_count++] = len - (segments-1)*255;
00222 memcpy(page->data+page->size, p, len);
00223 p += len;
00224 size -= len;
00225 i += segments;
00226 page->size += len;
00227
00228 if (i == total_segments)
00229 page->granule = granule;
00230
00231 if(page->segments_count == 255 ||
00232 (ogg->pref_size > 0 && page->size >= ogg->pref_size)) {
00233 ogg_buffer_page(s, oggstream);
00234 }
00235 }
00236
00237 if (flush && oggstream->page.granule != -1)
00238 ogg_buffer_page(s, oggstream);
00239
00240 return 0;
00241 }
00242
00243 static uint8_t *ogg_write_vorbiscomment(int offset, int bitexact,
00244 int *header_len, AVDictionary **m, int framing_bit)
00245 {
00246 const char *vendor = bitexact ? "ffmpeg" : LIBAVFORMAT_IDENT;
00247 int size;
00248 uint8_t *p, *p0;
00249 unsigned int count;
00250
00251 ff_metadata_conv(m, ff_vorbiscomment_metadata_conv, NULL);
00252
00253 size = offset + ff_vorbiscomment_length(*m, vendor, &count) + framing_bit;
00254 p = av_mallocz(size);
00255 if (!p)
00256 return NULL;
00257 p0 = p;
00258
00259 p += offset;
00260 ff_vorbiscomment_write(&p, m, vendor, count);
00261 if (framing_bit)
00262 bytestream_put_byte(&p, 1);
00263
00264 *header_len = size;
00265 return p0;
00266 }
00267
00268 static int ogg_build_flac_headers(AVCodecContext *avctx,
00269 OGGStreamContext *oggstream, int bitexact,
00270 AVDictionary **m)
00271 {
00272 enum FLACExtradataFormat format;
00273 uint8_t *streaminfo;
00274 uint8_t *p;
00275
00276 if (!ff_flac_is_extradata_valid(avctx, &format, &streaminfo))
00277 return -1;
00278
00279
00280 oggstream->header_len[0] = 51;
00281 oggstream->header[0] = av_mallocz(51);
00282 p = oggstream->header[0];
00283 if (!p)
00284 return AVERROR(ENOMEM);
00285 bytestream_put_byte(&p, 0x7F);
00286 bytestream_put_buffer(&p, "FLAC", 4);
00287 bytestream_put_byte(&p, 1);
00288 bytestream_put_byte(&p, 0);
00289 bytestream_put_be16(&p, 1);
00290 bytestream_put_buffer(&p, "fLaC", 4);
00291 bytestream_put_byte(&p, 0x00);
00292 bytestream_put_be24(&p, 34);
00293 bytestream_put_buffer(&p, streaminfo, FLAC_STREAMINFO_SIZE);
00294
00295
00296 p = ogg_write_vorbiscomment(4, bitexact, &oggstream->header_len[1], m, 0);
00297 if (!p)
00298 return AVERROR(ENOMEM);
00299 oggstream->header[1] = p;
00300 bytestream_put_byte(&p, 0x84);
00301 bytestream_put_be24(&p, oggstream->header_len[1] - 4);
00302
00303 return 0;
00304 }
00305
00306 #define SPEEX_HEADER_SIZE 80
00307
00308 static int ogg_build_speex_headers(AVCodecContext *avctx,
00309 OGGStreamContext *oggstream, int bitexact,
00310 AVDictionary **m)
00311 {
00312 uint8_t *p;
00313
00314 if (avctx->extradata_size < SPEEX_HEADER_SIZE)
00315 return -1;
00316
00317
00318 p = av_mallocz(SPEEX_HEADER_SIZE);
00319 if (!p)
00320 return AVERROR(ENOMEM);
00321 oggstream->header[0] = p;
00322 oggstream->header_len[0] = SPEEX_HEADER_SIZE;
00323 bytestream_put_buffer(&p, avctx->extradata, SPEEX_HEADER_SIZE);
00324 AV_WL32(&oggstream->header[0][68], 0);
00325
00326
00327 p = ogg_write_vorbiscomment(0, bitexact, &oggstream->header_len[1], m, 0);
00328 if (!p)
00329 return AVERROR(ENOMEM);
00330 oggstream->header[1] = p;
00331
00332 return 0;
00333 }
00334
00335 static int ogg_write_header(AVFormatContext *s)
00336 {
00337 OGGStreamContext *oggstream;
00338 int i, j;
00339
00340 for (i = 0; i < s->nb_streams; i++) {
00341 AVStream *st = s->streams[i];
00342 unsigned serial_num = i;
00343
00344 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
00345 av_set_pts_info(st, 64, 1, st->codec->sample_rate);
00346 else if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
00347 av_set_pts_info(st, 64, st->codec->time_base.num, st->codec->time_base.den);
00348 if (st->codec->codec_id != CODEC_ID_VORBIS &&
00349 st->codec->codec_id != CODEC_ID_THEORA &&
00350 st->codec->codec_id != CODEC_ID_SPEEX &&
00351 st->codec->codec_id != CODEC_ID_FLAC) {
00352 av_log(s, AV_LOG_ERROR, "Unsupported codec id in stream %d\n", i);
00353 return -1;
00354 }
00355
00356 if (!st->codec->extradata || !st->codec->extradata_size) {
00357 av_log(s, AV_LOG_ERROR, "No extradata present\n");
00358 return -1;
00359 }
00360 oggstream = av_mallocz(sizeof(*oggstream));
00361 oggstream->page.stream_index = i;
00362
00363 if (!(st->codec->flags & CODEC_FLAG_BITEXACT))
00364 do {
00365 serial_num = av_get_random_seed();
00366 for (j = 0; j < i; j++) {
00367 OGGStreamContext *sc = s->streams[j]->priv_data;
00368 if (serial_num == sc->serial_num)
00369 break;
00370 }
00371 } while (j < i);
00372 oggstream->serial_num = serial_num;
00373
00374 st->priv_data = oggstream;
00375 if (st->codec->codec_id == CODEC_ID_FLAC) {
00376 int err = ogg_build_flac_headers(st->codec, oggstream,
00377 st->codec->flags & CODEC_FLAG_BITEXACT,
00378 &s->metadata);
00379 if (err) {
00380 av_log(s, AV_LOG_ERROR, "Error writing FLAC headers\n");
00381 av_freep(&st->priv_data);
00382 return err;
00383 }
00384 } else if (st->codec->codec_id == CODEC_ID_SPEEX) {
00385 int err = ogg_build_speex_headers(st->codec, oggstream,
00386 st->codec->flags & CODEC_FLAG_BITEXACT,
00387 &s->metadata);
00388 if (err) {
00389 av_log(s, AV_LOG_ERROR, "Error writing Speex headers\n");
00390 av_freep(&st->priv_data);
00391 return err;
00392 }
00393 } else {
00394 uint8_t *p;
00395 const char *cstr = st->codec->codec_id == CODEC_ID_VORBIS ? "vorbis" : "theora";
00396 int header_type = st->codec->codec_id == CODEC_ID_VORBIS ? 3 : 0x81;
00397 int framing_bit = st->codec->codec_id == CODEC_ID_VORBIS ? 1 : 0;
00398
00399 if (ff_split_xiph_headers(st->codec->extradata, st->codec->extradata_size,
00400 st->codec->codec_id == CODEC_ID_VORBIS ? 30 : 42,
00401 oggstream->header, oggstream->header_len) < 0) {
00402 av_log(s, AV_LOG_ERROR, "Extradata corrupted\n");
00403 av_freep(&st->priv_data);
00404 return -1;
00405 }
00406
00407 p = ogg_write_vorbiscomment(7, st->codec->flags & CODEC_FLAG_BITEXACT,
00408 &oggstream->header_len[1], &s->metadata,
00409 framing_bit);
00410 if (!p)
00411 return AVERROR(ENOMEM);
00412
00413 oggstream->header[1] = p;
00414 bytestream_put_byte(&p, header_type);
00415 bytestream_put_buffer(&p, cstr, 6);
00416
00417 if (st->codec->codec_id == CODEC_ID_THEORA) {
00420 oggstream->kfgshift = ((oggstream->header[0][40]&3)<<3)|(oggstream->header[0][41]>>5);
00421 oggstream->vrev = oggstream->header[0][9];
00422 av_log(s, AV_LOG_DEBUG, "theora kfgshift %d, vrev %d\n",
00423 oggstream->kfgshift, oggstream->vrev);
00424 }
00425 }
00426 }
00427
00428 for (j = 0; j < s->nb_streams; j++) {
00429 OGGStreamContext *oggstream = s->streams[j]->priv_data;
00430 ogg_buffer_data(s, s->streams[j], oggstream->header[0],
00431 oggstream->header_len[0], 0);
00432 oggstream->page.flags |= 2;
00433 ogg_buffer_page(s, oggstream);
00434 }
00435 for (j = 0; j < s->nb_streams; j++) {
00436 AVStream *st = s->streams[j];
00437 OGGStreamContext *oggstream = st->priv_data;
00438 for (i = 1; i < 3; i++) {
00439 if (oggstream && oggstream->header_len[i])
00440 ogg_buffer_data(s, st, oggstream->header[i],
00441 oggstream->header_len[i], 0);
00442 }
00443 ogg_buffer_page(s, oggstream);
00444 }
00445 return 0;
00446 }
00447
00448 static void ogg_write_pages(AVFormatContext *s, int flush)
00449 {
00450 OGGContext *ogg = s->priv_data;
00451 OGGPageList *next, *p;
00452
00453 if (!ogg->page_list)
00454 return;
00455
00456 for (p = ogg->page_list; p; ) {
00457 OGGStreamContext *oggstream =
00458 s->streams[p->page.stream_index]->priv_data;
00459 if (oggstream->page_count < 2 && !flush)
00460 break;
00461 ogg_write_page(s, &p->page,
00462 flush && oggstream->page_count == 1 ? 4 : 0);
00463 next = p->next;
00464 av_freep(&p);
00465 p = next;
00466 }
00467 ogg->page_list = p;
00468 }
00469
00470 static int ogg_write_packet(AVFormatContext *s, AVPacket *pkt)
00471 {
00472 AVStream *st = s->streams[pkt->stream_index];
00473 OGGStreamContext *oggstream = st->priv_data;
00474 int ret;
00475 int64_t granule;
00476
00477 if (st->codec->codec_id == CODEC_ID_THEORA) {
00478 int64_t pts = oggstream->vrev < 1 ? pkt->pts : pkt->pts + pkt->duration;
00479 int pframe_count;
00480 if (pkt->flags & AV_PKT_FLAG_KEY)
00481 oggstream->last_kf_pts = pts;
00482 pframe_count = pts - oggstream->last_kf_pts;
00483
00484 if (pframe_count >= (1<<oggstream->kfgshift)) {
00485 oggstream->last_kf_pts += pframe_count;
00486 pframe_count = 0;
00487 }
00488 granule = (oggstream->last_kf_pts<<oggstream->kfgshift) | pframe_count;
00489 } else
00490 granule = pkt->pts + pkt->duration;
00491
00492 ret = ogg_buffer_data(s, st, pkt->data, pkt->size, granule);
00493 if (ret < 0)
00494 return ret;
00495
00496 ogg_write_pages(s, 0);
00497
00498 oggstream->last_granule = granule;
00499
00500 return 0;
00501 }
00502
00503 static int ogg_write_trailer(AVFormatContext *s)
00504 {
00505 int i;
00506
00507
00508 for (i = 0; i < s->nb_streams; i++)
00509 ogg_buffer_page(s, s->streams[i]->priv_data);
00510
00511 ogg_write_pages(s, 1);
00512
00513 for (i = 0; i < s->nb_streams; i++) {
00514 AVStream *st = s->streams[i];
00515 OGGStreamContext *oggstream = st->priv_data;
00516 if (st->codec->codec_id == CODEC_ID_FLAC ||
00517 st->codec->codec_id == CODEC_ID_SPEEX) {
00518 av_free(oggstream->header[0]);
00519 av_free(oggstream->header[1]);
00520 }
00521 av_freep(&st->priv_data);
00522 }
00523 return 0;
00524 }
00525
00526 AVOutputFormat ff_ogg_muxer = {
00527 "ogg",
00528 NULL_IF_CONFIG_SMALL("Ogg"),
00529 "application/ogg",
00530 "ogg,ogv,spx",
00531 sizeof(OGGContext),
00532 CODEC_ID_FLAC,
00533 CODEC_ID_THEORA,
00534 ogg_write_header,
00535 ogg_write_packet,
00536 ogg_write_trailer,
00537 .priv_class = &ogg_muxer_class,
00538 };