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