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