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 static int ogg_write_header(AVFormatContext *s)
00353 {
00354 OGGStreamContext *oggstream;
00355 int i, j;
00356
00357 for (i = 0; i < s->nb_streams; i++) {
00358 AVStream *st = s->streams[i];
00359 unsigned serial_num = i;
00360
00361 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
00362 avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
00363 else if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
00364 avpriv_set_pts_info(st, 64, st->codec->time_base.num, st->codec->time_base.den);
00365 if (st->codec->codec_id != AV_CODEC_ID_VORBIS &&
00366 st->codec->codec_id != AV_CODEC_ID_THEORA &&
00367 st->codec->codec_id != AV_CODEC_ID_SPEEX &&
00368 st->codec->codec_id != AV_CODEC_ID_FLAC) {
00369 av_log(s, AV_LOG_ERROR, "Unsupported codec id in stream %d\n", i);
00370 return -1;
00371 }
00372
00373 if (!st->codec->extradata || !st->codec->extradata_size) {
00374 av_log(s, AV_LOG_ERROR, "No extradata present\n");
00375 return -1;
00376 }
00377 oggstream = av_mallocz(sizeof(*oggstream));
00378 oggstream->page.stream_index = i;
00379
00380 if (!(st->codec->flags & CODEC_FLAG_BITEXACT))
00381 do {
00382 serial_num = av_get_random_seed();
00383 for (j = 0; j < i; j++) {
00384 OGGStreamContext *sc = s->streams[j]->priv_data;
00385 if (serial_num == sc->serial_num)
00386 break;
00387 }
00388 } while (j < i);
00389 oggstream->serial_num = serial_num;
00390
00391 st->priv_data = oggstream;
00392 if (st->codec->codec_id == AV_CODEC_ID_FLAC) {
00393 int err = ogg_build_flac_headers(st->codec, oggstream,
00394 st->codec->flags & CODEC_FLAG_BITEXACT,
00395 &s->metadata);
00396 if (err) {
00397 av_log(s, AV_LOG_ERROR, "Error writing FLAC headers\n");
00398 av_freep(&st->priv_data);
00399 return err;
00400 }
00401 } else if (st->codec->codec_id == AV_CODEC_ID_SPEEX) {
00402 int err = ogg_build_speex_headers(st->codec, oggstream,
00403 st->codec->flags & CODEC_FLAG_BITEXACT,
00404 &s->metadata);
00405 if (err) {
00406 av_log(s, AV_LOG_ERROR, "Error writing Speex headers\n");
00407 av_freep(&st->priv_data);
00408 return err;
00409 }
00410 } else {
00411 uint8_t *p;
00412 const char *cstr = st->codec->codec_id == AV_CODEC_ID_VORBIS ? "vorbis" : "theora";
00413 int header_type = st->codec->codec_id == AV_CODEC_ID_VORBIS ? 3 : 0x81;
00414 int framing_bit = st->codec->codec_id == AV_CODEC_ID_VORBIS ? 1 : 0;
00415
00416 if (avpriv_split_xiph_headers(st->codec->extradata, st->codec->extradata_size,
00417 st->codec->codec_id == AV_CODEC_ID_VORBIS ? 30 : 42,
00418 oggstream->header, oggstream->header_len) < 0) {
00419 av_log(s, AV_LOG_ERROR, "Extradata corrupted\n");
00420 av_freep(&st->priv_data);
00421 return -1;
00422 }
00423
00424 p = ogg_write_vorbiscomment(7, st->codec->flags & CODEC_FLAG_BITEXACT,
00425 &oggstream->header_len[1], &s->metadata,
00426 framing_bit);
00427 oggstream->header[1] = p;
00428 if (!p)
00429 return AVERROR(ENOMEM);
00430
00431 bytestream_put_byte(&p, header_type);
00432 bytestream_put_buffer(&p, cstr, 6);
00433
00434 if (st->codec->codec_id == AV_CODEC_ID_THEORA) {
00437 oggstream->kfgshift = ((oggstream->header[0][40]&3)<<3)|(oggstream->header[0][41]>>5);
00438 oggstream->vrev = oggstream->header[0][9];
00439 av_log(s, AV_LOG_DEBUG, "theora kfgshift %d, vrev %d\n",
00440 oggstream->kfgshift, oggstream->vrev);
00441 }
00442 }
00443 }
00444
00445 for (j = 0; j < s->nb_streams; j++) {
00446 OGGStreamContext *oggstream = s->streams[j]->priv_data;
00447 ogg_buffer_data(s, s->streams[j], oggstream->header[0],
00448 oggstream->header_len[0], 0, 1);
00449 oggstream->page.flags |= 2;
00450 ogg_buffer_page(s, oggstream);
00451 }
00452 for (j = 0; j < s->nb_streams; j++) {
00453 AVStream *st = s->streams[j];
00454 OGGStreamContext *oggstream = st->priv_data;
00455 for (i = 1; i < 3; i++) {
00456 if (oggstream && oggstream->header_len[i])
00457 ogg_buffer_data(s, st, oggstream->header[i],
00458 oggstream->header_len[i], 0, 1);
00459 }
00460 ogg_buffer_page(s, oggstream);
00461 }
00462 return 0;
00463 }
00464
00465 static void ogg_write_pages(AVFormatContext *s, int flush)
00466 {
00467 OGGContext *ogg = s->priv_data;
00468 OGGPageList *next, *p;
00469
00470 if (!ogg->page_list)
00471 return;
00472
00473 for (p = ogg->page_list; p; ) {
00474 OGGStreamContext *oggstream =
00475 s->streams[p->page.stream_index]->priv_data;
00476 if (oggstream->page_count < 2 && !flush)
00477 break;
00478 ogg_write_page(s, &p->page,
00479 flush && oggstream->page_count == 1 ? 4 : 0);
00480 next = p->next;
00481 av_freep(&p);
00482 p = next;
00483 }
00484 ogg->page_list = p;
00485 }
00486
00487 static int ogg_write_packet(AVFormatContext *s, AVPacket *pkt)
00488 {
00489 AVStream *st = s->streams[pkt->stream_index];
00490 OGGStreamContext *oggstream = st->priv_data;
00491 int ret;
00492 int64_t granule;
00493
00494 if (st->codec->codec_id == AV_CODEC_ID_THEORA) {
00495 int64_t pts = oggstream->vrev < 1 ? pkt->pts : pkt->pts + pkt->duration;
00496 int pframe_count;
00497 if (pkt->flags & AV_PKT_FLAG_KEY)
00498 oggstream->last_kf_pts = pts;
00499 pframe_count = pts - oggstream->last_kf_pts;
00500
00501 if (pframe_count >= (1<<oggstream->kfgshift)) {
00502 oggstream->last_kf_pts += pframe_count;
00503 pframe_count = 0;
00504 }
00505 granule = (oggstream->last_kf_pts<<oggstream->kfgshift) | pframe_count;
00506 } else
00507 granule = pkt->pts + pkt->duration;
00508
00509 ret = ogg_buffer_data(s, st, pkt->data, pkt->size, granule, 0);
00510 if (ret < 0)
00511 return ret;
00512
00513 ogg_write_pages(s, 0);
00514
00515 oggstream->last_granule = granule;
00516
00517 return 0;
00518 }
00519
00520 static int ogg_write_trailer(AVFormatContext *s)
00521 {
00522 int i;
00523
00524
00525 for (i = 0; i < s->nb_streams; i++)
00526 ogg_buffer_page(s, s->streams[i]->priv_data);
00527
00528 ogg_write_pages(s, 1);
00529
00530 for (i = 0; i < s->nb_streams; i++) {
00531 AVStream *st = s->streams[i];
00532 OGGStreamContext *oggstream = st->priv_data;
00533 if (st->codec->codec_id == AV_CODEC_ID_FLAC ||
00534 st->codec->codec_id == AV_CODEC_ID_SPEEX) {
00535 av_freep(&oggstream->header[0]);
00536 }
00537 av_freep(&oggstream->header[1]);
00538 av_freep(&st->priv_data);
00539 }
00540 return 0;
00541 }
00542
00543 AVOutputFormat ff_ogg_muxer = {
00544 .name = "ogg",
00545 .long_name = NULL_IF_CONFIG_SMALL("Ogg"),
00546 .mime_type = "application/ogg",
00547 .extensions = "ogg,ogv,spx",
00548 .priv_data_size = sizeof(OGGContext),
00549 .audio_codec = AV_CODEC_ID_FLAC,
00550 .video_codec = AV_CODEC_ID_THEORA,
00551 .write_header = ogg_write_header,
00552 .write_packet = ogg_write_packet,
00553 .write_trailer = ogg_write_trailer,
00554 .priv_class = &ogg_muxer_class,
00555 };