00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "avformat.h"
00023 #include "riff.h"
00024 #include "isom.h"
00025 #include "matroska.h"
00026 #include "avc.h"
00027 #include "flacenc.h"
00028 #include "avlanguage.h"
00029 #include "libavutil/samplefmt.h"
00030 #include "libavutil/intreadwrite.h"
00031 #include "libavutil/random_seed.h"
00032 #include "libavutil/lfg.h"
00033 #include "libavutil/dict.h"
00034 #include "libavcodec/xiph.h"
00035 #include "libavcodec/mpeg4audio.h"
00036 #include <strings.h>
00037
00038 typedef struct ebml_master {
00039 int64_t pos;
00040 int sizebytes;
00041 } ebml_master;
00042
00043 typedef struct mkv_seekhead_entry {
00044 unsigned int elementid;
00045 uint64_t segmentpos;
00046 } mkv_seekhead_entry;
00047
00048 typedef struct mkv_seekhead {
00049 int64_t filepos;
00050 int64_t segment_offset;
00051 int reserved_size;
00052 int max_entries;
00053 mkv_seekhead_entry *entries;
00054 int num_entries;
00055 } mkv_seekhead;
00056
00057 typedef struct {
00058 uint64_t pts;
00059 int tracknum;
00060 int64_t cluster_pos;
00061 } mkv_cuepoint;
00062
00063 typedef struct {
00064 int64_t segment_offset;
00065 mkv_cuepoint *entries;
00066 int num_entries;
00067 } mkv_cues;
00068
00069 typedef struct {
00070 int write_dts;
00071 } mkv_track;
00072
00073 #define MODE_MATROSKAv2 0x01
00074 #define MODE_WEBM 0x02
00075
00076 typedef struct MatroskaMuxContext {
00077 int mode;
00078 AVIOContext *dyn_bc;
00079 ebml_master segment;
00080 int64_t segment_offset;
00081 ebml_master cluster;
00082 int64_t cluster_pos;
00083 int64_t cluster_pts;
00084 int64_t duration_offset;
00085 int64_t duration;
00086 mkv_seekhead *main_seekhead;
00087 mkv_cues *cues;
00088 mkv_track *tracks;
00089
00090 unsigned int audio_buffer_size;
00091 AVPacket cur_audio_pkt;
00092 } MatroskaMuxContext;
00093
00094
00097 #define MAX_SEEKENTRY_SIZE 21
00098
00101 #define MAX_CUETRACKPOS_SIZE 22
00102
00104 #define MAX_CUEPOINT_SIZE(num_tracks) 12 + MAX_CUETRACKPOS_SIZE*num_tracks
00105
00106
00107 static int ebml_id_size(unsigned int id)
00108 {
00109 return (av_log2(id+1)-1)/7+1;
00110 }
00111
00112 static void put_ebml_id(AVIOContext *pb, unsigned int id)
00113 {
00114 int i = ebml_id_size(id);
00115 while (i--)
00116 avio_w8(pb, id >> (i*8));
00117 }
00118
00124 static void put_ebml_size_unknown(AVIOContext *pb, int bytes)
00125 {
00126 assert(bytes <= 8);
00127 avio_w8(pb, 0x1ff >> bytes);
00128 while (--bytes)
00129 avio_w8(pb, 0xff);
00130 }
00131
00135 static int ebml_num_size(uint64_t num)
00136 {
00137 int bytes = 1;
00138 while ((num+1) >> bytes*7) bytes++;
00139 return bytes;
00140 }
00141
00148 static void put_ebml_num(AVIOContext *pb, uint64_t num, int bytes)
00149 {
00150 int i, needed_bytes = ebml_num_size(num);
00151
00152
00153 assert(num < (1ULL<<56)-1);
00154
00155 if (bytes == 0)
00156
00157 bytes = needed_bytes;
00158
00159
00160 assert(bytes >= needed_bytes);
00161
00162 num |= 1ULL << bytes*7;
00163 for (i = bytes - 1; i >= 0; i--)
00164 avio_w8(pb, num >> i*8);
00165 }
00166
00167 static void put_ebml_uint(AVIOContext *pb, unsigned int elementid, uint64_t val)
00168 {
00169 int i, bytes = 1;
00170 uint64_t tmp = val;
00171 while (tmp>>=8) bytes++;
00172
00173 put_ebml_id(pb, elementid);
00174 put_ebml_num(pb, bytes, 0);
00175 for (i = bytes - 1; i >= 0; i--)
00176 avio_w8(pb, val >> i*8);
00177 }
00178
00179 static void put_ebml_float(AVIOContext *pb, unsigned int elementid, double val)
00180 {
00181 put_ebml_id(pb, elementid);
00182 put_ebml_num(pb, 8, 0);
00183 avio_wb64(pb, av_dbl2int(val));
00184 }
00185
00186 static void put_ebml_binary(AVIOContext *pb, unsigned int elementid,
00187 const void *buf, int size)
00188 {
00189 put_ebml_id(pb, elementid);
00190 put_ebml_num(pb, size, 0);
00191 avio_write(pb, buf, size);
00192 }
00193
00194 static void put_ebml_string(AVIOContext *pb, unsigned int elementid, const char *str)
00195 {
00196 put_ebml_binary(pb, elementid, str, strlen(str));
00197 }
00198
00205 static void put_ebml_void(AVIOContext *pb, uint64_t size)
00206 {
00207 int64_t currentpos = avio_tell(pb);
00208
00209 assert(size >= 2);
00210
00211 put_ebml_id(pb, EBML_ID_VOID);
00212
00213
00214
00215 if (size < 10)
00216 put_ebml_num(pb, size-1, 0);
00217 else
00218 put_ebml_num(pb, size-9, 8);
00219 while(avio_tell(pb) < currentpos + size)
00220 avio_w8(pb, 0);
00221 }
00222
00223 static ebml_master start_ebml_master(AVIOContext *pb, unsigned int elementid, uint64_t expectedsize)
00224 {
00225 int bytes = expectedsize ? ebml_num_size(expectedsize) : 8;
00226 put_ebml_id(pb, elementid);
00227 put_ebml_size_unknown(pb, bytes);
00228 return (ebml_master){ avio_tell(pb), bytes };
00229 }
00230
00231 static void end_ebml_master(AVIOContext *pb, ebml_master master)
00232 {
00233 int64_t pos = avio_tell(pb);
00234
00235 if (avio_seek(pb, master.pos - master.sizebytes, SEEK_SET) < 0)
00236 return;
00237 put_ebml_num(pb, pos - master.pos, master.sizebytes);
00238 avio_seek(pb, pos, SEEK_SET);
00239 }
00240
00241 static void put_xiph_size(AVIOContext *pb, int size)
00242 {
00243 int i;
00244 for (i = 0; i < size / 255; i++)
00245 avio_w8(pb, 255);
00246 avio_w8(pb, size % 255);
00247 }
00248
00260 static mkv_seekhead * mkv_start_seekhead(AVIOContext *pb, int64_t segment_offset, int numelements)
00261 {
00262 mkv_seekhead *new_seekhead = av_mallocz(sizeof(mkv_seekhead));
00263 if (new_seekhead == NULL)
00264 return NULL;
00265
00266 new_seekhead->segment_offset = segment_offset;
00267
00268 if (numelements > 0) {
00269 new_seekhead->filepos = avio_tell(pb);
00270
00271
00272
00273 new_seekhead->reserved_size = numelements * MAX_SEEKENTRY_SIZE + 13;
00274 new_seekhead->max_entries = numelements;
00275 put_ebml_void(pb, new_seekhead->reserved_size);
00276 }
00277 return new_seekhead;
00278 }
00279
00280 static int mkv_add_seekhead_entry(mkv_seekhead *seekhead, unsigned int elementid, uint64_t filepos)
00281 {
00282 mkv_seekhead_entry *entries = seekhead->entries;
00283
00284
00285 if (seekhead->max_entries > 0 && seekhead->max_entries <= seekhead->num_entries)
00286 return -1;
00287
00288 entries = av_realloc(entries, (seekhead->num_entries + 1) * sizeof(mkv_seekhead_entry));
00289 if (entries == NULL)
00290 return AVERROR(ENOMEM);
00291
00292 entries[seekhead->num_entries ].elementid = elementid;
00293 entries[seekhead->num_entries++].segmentpos = filepos - seekhead->segment_offset;
00294
00295 seekhead->entries = entries;
00296 return 0;
00297 }
00298
00308 static int64_t mkv_write_seekhead(AVIOContext *pb, mkv_seekhead *seekhead)
00309 {
00310 ebml_master metaseek, seekentry;
00311 int64_t currentpos;
00312 int i;
00313
00314 currentpos = avio_tell(pb);
00315
00316 if (seekhead->reserved_size > 0)
00317 if (avio_seek(pb, seekhead->filepos, SEEK_SET) < 0)
00318 return -1;
00319
00320 metaseek = start_ebml_master(pb, MATROSKA_ID_SEEKHEAD, seekhead->reserved_size);
00321 for (i = 0; i < seekhead->num_entries; i++) {
00322 mkv_seekhead_entry *entry = &seekhead->entries[i];
00323
00324 seekentry = start_ebml_master(pb, MATROSKA_ID_SEEKENTRY, MAX_SEEKENTRY_SIZE);
00325
00326 put_ebml_id(pb, MATROSKA_ID_SEEKID);
00327 put_ebml_num(pb, ebml_id_size(entry->elementid), 0);
00328 put_ebml_id(pb, entry->elementid);
00329
00330 put_ebml_uint(pb, MATROSKA_ID_SEEKPOSITION, entry->segmentpos);
00331 end_ebml_master(pb, seekentry);
00332 }
00333 end_ebml_master(pb, metaseek);
00334
00335 if (seekhead->reserved_size > 0) {
00336 uint64_t remaining = seekhead->filepos + seekhead->reserved_size - avio_tell(pb);
00337 put_ebml_void(pb, remaining);
00338 avio_seek(pb, currentpos, SEEK_SET);
00339
00340 currentpos = seekhead->filepos;
00341 }
00342 av_free(seekhead->entries);
00343 av_free(seekhead);
00344
00345 return currentpos;
00346 }
00347
00348 static mkv_cues * mkv_start_cues(int64_t segment_offset)
00349 {
00350 mkv_cues *cues = av_mallocz(sizeof(mkv_cues));
00351 if (cues == NULL)
00352 return NULL;
00353
00354 cues->segment_offset = segment_offset;
00355 return cues;
00356 }
00357
00358 static int mkv_add_cuepoint(mkv_cues *cues, int stream, int64_t ts, int64_t cluster_pos)
00359 {
00360 mkv_cuepoint *entries = cues->entries;
00361
00362 entries = av_realloc(entries, (cues->num_entries + 1) * sizeof(mkv_cuepoint));
00363 if (entries == NULL)
00364 return AVERROR(ENOMEM);
00365
00366 if (ts < 0)
00367 return 0;
00368
00369 entries[cues->num_entries ].pts = ts;
00370 entries[cues->num_entries ].tracknum = stream + 1;
00371 entries[cues->num_entries++].cluster_pos = cluster_pos - cues->segment_offset;
00372
00373 cues->entries = entries;
00374 return 0;
00375 }
00376
00377 static int64_t mkv_write_cues(AVIOContext *pb, mkv_cues *cues, int num_tracks)
00378 {
00379 ebml_master cues_element;
00380 int64_t currentpos;
00381 int i, j;
00382
00383 currentpos = avio_tell(pb);
00384 cues_element = start_ebml_master(pb, MATROSKA_ID_CUES, 0);
00385
00386 for (i = 0; i < cues->num_entries; i++) {
00387 ebml_master cuepoint, track_positions;
00388 mkv_cuepoint *entry = &cues->entries[i];
00389 uint64_t pts = entry->pts;
00390
00391 cuepoint = start_ebml_master(pb, MATROSKA_ID_POINTENTRY, MAX_CUEPOINT_SIZE(num_tracks));
00392 put_ebml_uint(pb, MATROSKA_ID_CUETIME, pts);
00393
00394
00395
00396 for (j = 0; j < cues->num_entries - i && entry[j].pts == pts; j++) {
00397 track_positions = start_ebml_master(pb, MATROSKA_ID_CUETRACKPOSITION, MAX_CUETRACKPOS_SIZE);
00398 put_ebml_uint(pb, MATROSKA_ID_CUETRACK , entry[j].tracknum );
00399 put_ebml_uint(pb, MATROSKA_ID_CUECLUSTERPOSITION, entry[j].cluster_pos);
00400 end_ebml_master(pb, track_positions);
00401 }
00402 i += j - 1;
00403 end_ebml_master(pb, cuepoint);
00404 }
00405 end_ebml_master(pb, cues_element);
00406
00407 return currentpos;
00408 }
00409
00410 static int put_xiph_codecpriv(AVFormatContext *s, AVIOContext *pb, AVCodecContext *codec)
00411 {
00412 uint8_t *header_start[3];
00413 int header_len[3];
00414 int first_header_size;
00415 int j;
00416
00417 if (codec->codec_id == CODEC_ID_VORBIS)
00418 first_header_size = 30;
00419 else
00420 first_header_size = 42;
00421
00422 if (ff_split_xiph_headers(codec->extradata, codec->extradata_size,
00423 first_header_size, header_start, header_len) < 0) {
00424 av_log(s, AV_LOG_ERROR, "Extradata corrupt.\n");
00425 return -1;
00426 }
00427
00428 avio_w8(pb, 2);
00429 for (j = 0; j < 2; j++) {
00430 put_xiph_size(pb, header_len[j]);
00431 }
00432 for (j = 0; j < 3; j++)
00433 avio_write(pb, header_start[j], header_len[j]);
00434
00435 return 0;
00436 }
00437
00438 static void get_aac_sample_rates(AVFormatContext *s, AVCodecContext *codec, int *sample_rate, int *output_sample_rate)
00439 {
00440 MPEG4AudioConfig mp4ac;
00441
00442 if (ff_mpeg4audio_get_config(&mp4ac, codec->extradata, codec->extradata_size) < 0) {
00443 av_log(s, AV_LOG_WARNING, "Error parsing AAC extradata, unable to determine samplerate.\n");
00444 return;
00445 }
00446
00447 *sample_rate = mp4ac.sample_rate;
00448 *output_sample_rate = mp4ac.ext_sample_rate;
00449 }
00450
00451 static int mkv_write_codecprivate(AVFormatContext *s, AVIOContext *pb, AVCodecContext *codec, int native_id, int qt_id)
00452 {
00453 AVIOContext *dyn_cp;
00454 uint8_t *codecpriv;
00455 int ret, codecpriv_size;
00456
00457 ret = avio_open_dyn_buf(&dyn_cp);
00458 if(ret < 0)
00459 return ret;
00460
00461 if (native_id) {
00462 if (codec->codec_id == CODEC_ID_VORBIS || codec->codec_id == CODEC_ID_THEORA)
00463 ret = put_xiph_codecpriv(s, dyn_cp, codec);
00464 else if (codec->codec_id == CODEC_ID_FLAC)
00465 ret = ff_flac_write_header(dyn_cp, codec, 1);
00466 else if (codec->codec_id == CODEC_ID_H264)
00467 ret = ff_isom_write_avcc(dyn_cp, codec->extradata, codec->extradata_size);
00468 else if (codec->extradata_size)
00469 avio_write(dyn_cp, codec->extradata, codec->extradata_size);
00470 } else if (codec->codec_type == AVMEDIA_TYPE_VIDEO) {
00471 if (qt_id) {
00472 if (!codec->codec_tag)
00473 codec->codec_tag = ff_codec_get_tag(codec_movvideo_tags, codec->codec_id);
00474 if (codec->extradata_size)
00475 avio_write(dyn_cp, codec->extradata, codec->extradata_size);
00476 } else {
00477 if (!codec->codec_tag)
00478 codec->codec_tag = ff_codec_get_tag(ff_codec_bmp_tags, codec->codec_id);
00479 if (!codec->codec_tag) {
00480 av_log(s, AV_LOG_ERROR, "No bmp codec ID found.\n");
00481 ret = -1;
00482 }
00483
00484 ff_put_bmp_header(dyn_cp, codec, ff_codec_bmp_tags, 0);
00485 }
00486
00487 } else if (codec->codec_type == AVMEDIA_TYPE_AUDIO) {
00488 unsigned int tag;
00489 tag = ff_codec_get_tag(ff_codec_wav_tags, codec->codec_id);
00490 if (!tag) {
00491 av_log(s, AV_LOG_ERROR, "No wav codec ID found.\n");
00492 ret = -1;
00493 }
00494 if (!codec->codec_tag)
00495 codec->codec_tag = tag;
00496
00497 ff_put_wav_header(dyn_cp, codec);
00498 }
00499
00500 codecpriv_size = avio_close_dyn_buf(dyn_cp, &codecpriv);
00501 if (codecpriv_size)
00502 put_ebml_binary(pb, MATROSKA_ID_CODECPRIVATE, codecpriv, codecpriv_size);
00503 av_free(codecpriv);
00504 return ret;
00505 }
00506
00507 static int mkv_write_tracks(AVFormatContext *s)
00508 {
00509 MatroskaMuxContext *mkv = s->priv_data;
00510 AVIOContext *pb = s->pb;
00511 ebml_master tracks;
00512 int i, j, ret;
00513
00514 ret = mkv_add_seekhead_entry(mkv->main_seekhead, MATROSKA_ID_TRACKS, avio_tell(pb));
00515 if (ret < 0) return ret;
00516
00517 tracks = start_ebml_master(pb, MATROSKA_ID_TRACKS, 0);
00518 for (i = 0; i < s->nb_streams; i++) {
00519 AVStream *st = s->streams[i];
00520 AVCodecContext *codec = st->codec;
00521 ebml_master subinfo, track;
00522 int native_id = 0;
00523 int qt_id = 0;
00524 int bit_depth = av_get_bits_per_sample(codec->codec_id);
00525 int sample_rate = codec->sample_rate;
00526 int output_sample_rate = 0;
00527 AVDictionaryEntry *tag;
00528
00529 if (!bit_depth)
00530 bit_depth = av_get_bytes_per_sample(codec->sample_fmt) << 3;
00531
00532 if (codec->codec_id == CODEC_ID_AAC)
00533 get_aac_sample_rates(s, codec, &sample_rate, &output_sample_rate);
00534
00535 track = start_ebml_master(pb, MATROSKA_ID_TRACKENTRY, 0);
00536 put_ebml_uint (pb, MATROSKA_ID_TRACKNUMBER , i + 1);
00537 put_ebml_uint (pb, MATROSKA_ID_TRACKUID , i + 1);
00538 put_ebml_uint (pb, MATROSKA_ID_TRACKFLAGLACING , 0);
00539
00540 if ((tag = av_dict_get(st->metadata, "title", NULL, 0)))
00541 put_ebml_string(pb, MATROSKA_ID_TRACKNAME, tag->value);
00542 tag = av_dict_get(st->metadata, "language", NULL, 0);
00543 put_ebml_string(pb, MATROSKA_ID_TRACKLANGUAGE, tag ? tag->value:"und");
00544
00545 if (st->disposition)
00546 put_ebml_uint(pb, MATROSKA_ID_TRACKFLAGDEFAULT, !!(st->disposition & AV_DISPOSITION_DEFAULT));
00547
00548
00549
00550 for (j = 0; ff_mkv_codec_tags[j].id != CODEC_ID_NONE; j++) {
00551 if (ff_mkv_codec_tags[j].id == codec->codec_id) {
00552 put_ebml_string(pb, MATROSKA_ID_CODECID, ff_mkv_codec_tags[j].str);
00553 native_id = 1;
00554 break;
00555 }
00556 }
00557
00558 if (mkv->mode == MODE_WEBM && !(codec->codec_id == CODEC_ID_VP8 ||
00559 codec->codec_id == CODEC_ID_VORBIS)) {
00560 av_log(s, AV_LOG_ERROR,
00561 "Only VP8 video and Vorbis audio are supported for WebM.\n");
00562 return AVERROR(EINVAL);
00563 }
00564
00565 switch (codec->codec_type) {
00566 case AVMEDIA_TYPE_VIDEO:
00567 put_ebml_uint(pb, MATROSKA_ID_TRACKTYPE, MATROSKA_TRACK_TYPE_VIDEO);
00568 put_ebml_uint(pb, MATROSKA_ID_TRACKDEFAULTDURATION, av_q2d(codec->time_base)*1E9);
00569
00570 if (!native_id &&
00571 ff_codec_get_tag(codec_movvideo_tags, codec->codec_id) &&
00572 (!ff_codec_get_tag(ff_codec_bmp_tags, codec->codec_id)
00573 || codec->codec_id == CODEC_ID_SVQ1
00574 || codec->codec_id == CODEC_ID_SVQ3
00575 || codec->codec_id == CODEC_ID_CINEPAK))
00576 qt_id = 1;
00577
00578 if (qt_id)
00579 put_ebml_string(pb, MATROSKA_ID_CODECID, "V_QUICKTIME");
00580 else if (!native_id) {
00581
00582 put_ebml_string(pb, MATROSKA_ID_CODECID, "V_MS/VFW/FOURCC");
00583 mkv->tracks[i].write_dts = 1;
00584 }
00585
00586 subinfo = start_ebml_master(pb, MATROSKA_ID_TRACKVIDEO, 0);
00587
00588 put_ebml_uint (pb, MATROSKA_ID_VIDEOPIXELWIDTH , codec->width);
00589 put_ebml_uint (pb, MATROSKA_ID_VIDEOPIXELHEIGHT, codec->height);
00590
00591 if ((tag = av_dict_get(st->metadata, "stereo_mode", NULL, 0)) ||
00592 (tag = av_dict_get( s->metadata, "stereo_mode", NULL, 0))) {
00593
00594 uint64_t st_mode = MATROSKA_VIDEO_STEREO_MODE_COUNT;
00595
00596 for (j=0; j<MATROSKA_VIDEO_STEREO_MODE_COUNT; j++)
00597 if (!strcmp(tag->value, matroska_video_stereo_mode[j])){
00598 st_mode = j;
00599 break;
00600 }
00601
00602 if ((mkv->mode == MODE_WEBM && st_mode > 3 && st_mode != 11)
00603 || st_mode >= MATROSKA_VIDEO_STEREO_MODE_COUNT) {
00604 av_log(s, AV_LOG_ERROR,
00605 "The specified stereo mode is not valid.\n");
00606 return AVERROR(EINVAL);
00607 } else
00608 put_ebml_uint(pb, MATROSKA_ID_VIDEOSTEREOMODE, st_mode);
00609 }
00610
00611 if (st->sample_aspect_ratio.num) {
00612 int d_width = codec->width*av_q2d(st->sample_aspect_ratio);
00613 put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYWIDTH , d_width);
00614 put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYHEIGHT, codec->height);
00615 put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYUNIT, 3);
00616 }
00617
00618 if (codec->codec_id == CODEC_ID_RAWVIDEO) {
00619 uint32_t color_space = av_le2ne32(codec->codec_tag);
00620 put_ebml_binary(pb, MATROSKA_ID_VIDEOCOLORSPACE, &color_space, sizeof(color_space));
00621 }
00622 end_ebml_master(pb, subinfo);
00623 break;
00624
00625 case AVMEDIA_TYPE_AUDIO:
00626 put_ebml_uint(pb, MATROSKA_ID_TRACKTYPE, MATROSKA_TRACK_TYPE_AUDIO);
00627
00628 if (!native_id)
00629
00630 put_ebml_string(pb, MATROSKA_ID_CODECID, "A_MS/ACM");
00631
00632 subinfo = start_ebml_master(pb, MATROSKA_ID_TRACKAUDIO, 0);
00633 put_ebml_uint (pb, MATROSKA_ID_AUDIOCHANNELS , codec->channels);
00634 put_ebml_float (pb, MATROSKA_ID_AUDIOSAMPLINGFREQ, sample_rate);
00635 if (output_sample_rate)
00636 put_ebml_float(pb, MATROSKA_ID_AUDIOOUTSAMPLINGFREQ, output_sample_rate);
00637 if (bit_depth)
00638 put_ebml_uint(pb, MATROSKA_ID_AUDIOBITDEPTH, bit_depth);
00639 end_ebml_master(pb, subinfo);
00640 break;
00641
00642 case AVMEDIA_TYPE_SUBTITLE:
00643 put_ebml_uint(pb, MATROSKA_ID_TRACKTYPE, MATROSKA_TRACK_TYPE_SUBTITLE);
00644 if (!native_id) {
00645 av_log(s, AV_LOG_ERROR, "Subtitle codec %d is not supported.\n", codec->codec_id);
00646 return AVERROR(ENOSYS);
00647 }
00648 break;
00649 default:
00650 av_log(s, AV_LOG_ERROR, "Only audio, video, and subtitles are supported for Matroska.\n");
00651 break;
00652 }
00653 ret = mkv_write_codecprivate(s, pb, codec, native_id, qt_id);
00654 if (ret < 0) return ret;
00655
00656 end_ebml_master(pb, track);
00657
00658
00659 av_set_pts_info(st, 64, 1, 1000);
00660 }
00661 end_ebml_master(pb, tracks);
00662 return 0;
00663 }
00664
00665 static int mkv_write_chapters(AVFormatContext *s)
00666 {
00667 MatroskaMuxContext *mkv = s->priv_data;
00668 AVIOContext *pb = s->pb;
00669 ebml_master chapters, editionentry;
00670 AVRational scale = {1, 1E9};
00671 int i, ret;
00672
00673 if (!s->nb_chapters)
00674 return 0;
00675
00676 ret = mkv_add_seekhead_entry(mkv->main_seekhead, MATROSKA_ID_CHAPTERS, avio_tell(pb));
00677 if (ret < 0) return ret;
00678
00679 chapters = start_ebml_master(pb, MATROSKA_ID_CHAPTERS , 0);
00680 editionentry = start_ebml_master(pb, MATROSKA_ID_EDITIONENTRY, 0);
00681 put_ebml_uint(pb, MATROSKA_ID_EDITIONFLAGDEFAULT, 1);
00682 put_ebml_uint(pb, MATROSKA_ID_EDITIONFLAGHIDDEN , 0);
00683 for (i = 0; i < s->nb_chapters; i++) {
00684 ebml_master chapteratom, chapterdisplay;
00685 AVChapter *c = s->chapters[i];
00686 AVDictionaryEntry *t = NULL;
00687
00688 chapteratom = start_ebml_master(pb, MATROSKA_ID_CHAPTERATOM, 0);
00689 put_ebml_uint(pb, MATROSKA_ID_CHAPTERUID, c->id);
00690 put_ebml_uint(pb, MATROSKA_ID_CHAPTERTIMESTART,
00691 av_rescale_q(c->start, c->time_base, scale));
00692 put_ebml_uint(pb, MATROSKA_ID_CHAPTERTIMEEND,
00693 av_rescale_q(c->end, c->time_base, scale));
00694 put_ebml_uint(pb, MATROSKA_ID_CHAPTERFLAGHIDDEN , 0);
00695 put_ebml_uint(pb, MATROSKA_ID_CHAPTERFLAGENABLED, 1);
00696 if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
00697 chapterdisplay = start_ebml_master(pb, MATROSKA_ID_CHAPTERDISPLAY, 0);
00698 put_ebml_string(pb, MATROSKA_ID_CHAPSTRING, t->value);
00699 put_ebml_string(pb, MATROSKA_ID_CHAPLANG , "und");
00700 end_ebml_master(pb, chapterdisplay);
00701 }
00702 end_ebml_master(pb, chapteratom);
00703 }
00704 end_ebml_master(pb, editionentry);
00705 end_ebml_master(pb, chapters);
00706 return 0;
00707 }
00708
00709 static void mkv_write_simpletag(AVIOContext *pb, AVDictionaryEntry *t)
00710 {
00711 uint8_t *key = av_strdup(t->key);
00712 uint8_t *p = key;
00713 const uint8_t *lang = NULL;
00714 ebml_master tag;
00715
00716 if ((p = strrchr(p, '-')) &&
00717 (lang = av_convert_lang_to(p + 1, AV_LANG_ISO639_2_BIBL)))
00718 *p = 0;
00719
00720 p = key;
00721 while (*p) {
00722 if (*p == ' ')
00723 *p = '_';
00724 else if (*p >= 'a' && *p <= 'z')
00725 *p -= 'a' - 'A';
00726 p++;
00727 }
00728
00729 tag = start_ebml_master(pb, MATROSKA_ID_SIMPLETAG, 0);
00730 put_ebml_string(pb, MATROSKA_ID_TAGNAME, key);
00731 if (lang)
00732 put_ebml_string(pb, MATROSKA_ID_TAGLANG, lang);
00733 put_ebml_string(pb, MATROSKA_ID_TAGSTRING, t->value);
00734 end_ebml_master(pb, tag);
00735
00736 av_freep(&key);
00737 }
00738
00739 static int mkv_write_tag(AVFormatContext *s, AVDictionary *m, unsigned int elementid,
00740 unsigned int uid, ebml_master *tags)
00741 {
00742 MatroskaMuxContext *mkv = s->priv_data;
00743 ebml_master tag, targets;
00744 AVDictionaryEntry *t = NULL;
00745 int ret;
00746
00747 if (!tags->pos) {
00748 ret = mkv_add_seekhead_entry(mkv->main_seekhead, MATROSKA_ID_TAGS, avio_tell(s->pb));
00749 if (ret < 0) return ret;
00750
00751 *tags = start_ebml_master(s->pb, MATROSKA_ID_TAGS, 0);
00752 }
00753
00754 tag = start_ebml_master(s->pb, MATROSKA_ID_TAG, 0);
00755 targets = start_ebml_master(s->pb, MATROSKA_ID_TAGTARGETS, 0);
00756 if (elementid)
00757 put_ebml_uint(s->pb, elementid, uid);
00758 end_ebml_master(s->pb, targets);
00759
00760 while ((t = av_dict_get(m, "", t, AV_DICT_IGNORE_SUFFIX)))
00761 if (strcasecmp(t->key, "title") && strcasecmp(t->key, "stereo_mode"))
00762 mkv_write_simpletag(s->pb, t);
00763
00764 end_ebml_master(s->pb, tag);
00765 return 0;
00766 }
00767
00768 static int mkv_write_tags(AVFormatContext *s)
00769 {
00770 ebml_master tags = {0};
00771 int i, ret;
00772
00773 ff_metadata_conv_ctx(s, ff_mkv_metadata_conv, NULL);
00774
00775 if (av_dict_get(s->metadata, "", NULL, AV_DICT_IGNORE_SUFFIX)) {
00776 ret = mkv_write_tag(s, s->metadata, 0, 0, &tags);
00777 if (ret < 0) return ret;
00778 }
00779
00780 for (i = 0; i < s->nb_streams; i++) {
00781 AVStream *st = s->streams[i];
00782
00783 if (!av_dict_get(st->metadata, "", 0, AV_DICT_IGNORE_SUFFIX))
00784 continue;
00785
00786 ret = mkv_write_tag(s, st->metadata, MATROSKA_ID_TAGTARGETS_TRACKUID, i + 1, &tags);
00787 if (ret < 0) return ret;
00788 }
00789
00790 for (i = 0; i < s->nb_chapters; i++) {
00791 AVChapter *ch = s->chapters[i];
00792
00793 if (!av_dict_get(ch->metadata, "", NULL, AV_DICT_IGNORE_SUFFIX))
00794 continue;
00795
00796 ret = mkv_write_tag(s, ch->metadata, MATROSKA_ID_TAGTARGETS_CHAPTERUID, ch->id, &tags);
00797 if (ret < 0) return ret;
00798 }
00799
00800 if (tags.pos)
00801 end_ebml_master(s->pb, tags);
00802 return 0;
00803 }
00804
00805 static int mkv_write_header(AVFormatContext *s)
00806 {
00807 MatroskaMuxContext *mkv = s->priv_data;
00808 AVIOContext *pb = s->pb;
00809 ebml_master ebml_header, segment_info;
00810 AVDictionaryEntry *tag;
00811 int ret, i;
00812
00813 if (!strcmp(s->oformat->name, "webm")) mkv->mode = MODE_WEBM;
00814 else mkv->mode = MODE_MATROSKAv2;
00815
00816 mkv->tracks = av_mallocz(s->nb_streams * sizeof(*mkv->tracks));
00817 if (!mkv->tracks)
00818 return AVERROR(ENOMEM);
00819
00820 ebml_header = start_ebml_master(pb, EBML_ID_HEADER, 0);
00821 put_ebml_uint (pb, EBML_ID_EBMLVERSION , 1);
00822 put_ebml_uint (pb, EBML_ID_EBMLREADVERSION , 1);
00823 put_ebml_uint (pb, EBML_ID_EBMLMAXIDLENGTH , 4);
00824 put_ebml_uint (pb, EBML_ID_EBMLMAXSIZELENGTH , 8);
00825 put_ebml_string (pb, EBML_ID_DOCTYPE , s->oformat->name);
00826 put_ebml_uint (pb, EBML_ID_DOCTYPEVERSION , 2);
00827 put_ebml_uint (pb, EBML_ID_DOCTYPEREADVERSION , 2);
00828 end_ebml_master(pb, ebml_header);
00829
00830 mkv->segment = start_ebml_master(pb, MATROSKA_ID_SEGMENT, 0);
00831 mkv->segment_offset = avio_tell(pb);
00832
00833
00834
00835
00836
00837
00838 mkv->main_seekhead = mkv_start_seekhead(pb, mkv->segment_offset, 10);
00839 if (!mkv->main_seekhead)
00840 return AVERROR(ENOMEM);
00841
00842 ret = mkv_add_seekhead_entry(mkv->main_seekhead, MATROSKA_ID_INFO, avio_tell(pb));
00843 if (ret < 0) return ret;
00844
00845 segment_info = start_ebml_master(pb, MATROSKA_ID_INFO, 0);
00846 put_ebml_uint(pb, MATROSKA_ID_TIMECODESCALE, 1000000);
00847 if ((tag = av_dict_get(s->metadata, "title", NULL, 0)))
00848 put_ebml_string(pb, MATROSKA_ID_TITLE, tag->value);
00849 if (!(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT)) {
00850 uint32_t segment_uid[4];
00851 AVLFG lfg;
00852
00853 av_lfg_init(&lfg, av_get_random_seed());
00854
00855 for (i = 0; i < 4; i++)
00856 segment_uid[i] = av_lfg_get(&lfg);
00857
00858 put_ebml_string(pb, MATROSKA_ID_MUXINGAPP , LIBAVFORMAT_IDENT);
00859 put_ebml_string(pb, MATROSKA_ID_WRITINGAPP, LIBAVFORMAT_IDENT);
00860 put_ebml_binary(pb, MATROSKA_ID_SEGMENTUID, segment_uid, 16);
00861 }
00862
00863
00864 mkv->duration = 0;
00865 mkv->duration_offset = avio_tell(pb);
00866 put_ebml_void(pb, 11);
00867 end_ebml_master(pb, segment_info);
00868
00869 ret = mkv_write_tracks(s);
00870 if (ret < 0) return ret;
00871
00872 if (mkv->mode != MODE_WEBM) {
00873 ret = mkv_write_chapters(s);
00874 if (ret < 0) return ret;
00875
00876 ret = mkv_write_tags(s);
00877 if (ret < 0) return ret;
00878 }
00879
00880 if (!s->pb->seekable)
00881 mkv_write_seekhead(pb, mkv->main_seekhead);
00882
00883 mkv->cues = mkv_start_cues(mkv->segment_offset);
00884 if (mkv->cues == NULL)
00885 return AVERROR(ENOMEM);
00886
00887 av_init_packet(&mkv->cur_audio_pkt);
00888 mkv->cur_audio_pkt.size = 0;
00889 mkv->audio_buffer_size = 0;
00890
00891 avio_flush(pb);
00892 return 0;
00893 }
00894
00895 static int mkv_blockgroup_size(int pkt_size)
00896 {
00897 int size = pkt_size + 4;
00898 size += ebml_num_size(size);
00899 size += 2;
00900 size += 8;
00901 size += ebml_num_size(size);
00902 size += 1;
00903 return size;
00904 }
00905
00906 static int ass_get_duration(const uint8_t *p)
00907 {
00908 int sh, sm, ss, sc, eh, em, es, ec;
00909 uint64_t start, end;
00910
00911 if (sscanf(p, "%*[^,],%d:%d:%d%*c%d,%d:%d:%d%*c%d",
00912 &sh, &sm, &ss, &sc, &eh, &em, &es, &ec) != 8)
00913 return 0;
00914 start = 3600000*sh + 60000*sm + 1000*ss + 10*sc;
00915 end = 3600000*eh + 60000*em + 1000*es + 10*ec;
00916 return end - start;
00917 }
00918
00919 static int mkv_write_ass_blocks(AVFormatContext *s, AVIOContext *pb, AVPacket *pkt)
00920 {
00921 MatroskaMuxContext *mkv = s->priv_data;
00922 int i, layer = 0, max_duration = 0, size, line_size, data_size = pkt->size;
00923 uint8_t *start, *end, *data = pkt->data;
00924 ebml_master blockgroup;
00925 char buffer[2048];
00926
00927 while (data_size) {
00928 int duration = ass_get_duration(data);
00929 max_duration = FFMAX(duration, max_duration);
00930 end = memchr(data, '\n', data_size);
00931 size = line_size = end ? end-data+1 : data_size;
00932 size -= end ? (end[-1]=='\r')+1 : 0;
00933 start = data;
00934 for (i=0; i<3; i++, start++)
00935 if (!(start = memchr(start, ',', size-(start-data))))
00936 return max_duration;
00937 size -= start - data;
00938 sscanf(data, "Dialogue: %d,", &layer);
00939 i = snprintf(buffer, sizeof(buffer), "%"PRId64",%d,",
00940 s->streams[pkt->stream_index]->nb_frames++, layer);
00941 size = FFMIN(i+size, sizeof(buffer));
00942 memcpy(buffer+i, start, size-i);
00943
00944 av_log(s, AV_LOG_DEBUG, "Writing block at offset %" PRIu64 ", size %d, "
00945 "pts %" PRId64 ", duration %d\n",
00946 avio_tell(pb), size, pkt->pts, duration);
00947 blockgroup = start_ebml_master(pb, MATROSKA_ID_BLOCKGROUP, mkv_blockgroup_size(size));
00948 put_ebml_id(pb, MATROSKA_ID_BLOCK);
00949 put_ebml_num(pb, size+4, 0);
00950 avio_w8(pb, 0x80 | (pkt->stream_index + 1));
00951 avio_wb16(pb, pkt->pts - mkv->cluster_pts);
00952 avio_w8(pb, 0);
00953 avio_write(pb, buffer, size);
00954 put_ebml_uint(pb, MATROSKA_ID_BLOCKDURATION, duration);
00955 end_ebml_master(pb, blockgroup);
00956
00957 data += line_size;
00958 data_size -= line_size;
00959 }
00960
00961 return max_duration;
00962 }
00963
00964 static void mkv_write_block(AVFormatContext *s, AVIOContext *pb,
00965 unsigned int blockid, AVPacket *pkt, int flags)
00966 {
00967 MatroskaMuxContext *mkv = s->priv_data;
00968 AVCodecContext *codec = s->streams[pkt->stream_index]->codec;
00969 uint8_t *data = NULL;
00970 int size = pkt->size;
00971 int64_t ts = mkv->tracks[pkt->stream_index].write_dts ? pkt->dts : pkt->pts;
00972
00973 av_log(s, AV_LOG_DEBUG, "Writing block at offset %" PRIu64 ", size %d, "
00974 "pts %" PRId64 ", dts %" PRId64 ", duration %d, flags %d\n",
00975 avio_tell(pb), pkt->size, pkt->pts, pkt->dts, pkt->duration, flags);
00976 if (codec->codec_id == CODEC_ID_H264 && codec->extradata_size > 0 &&
00977 (AV_RB24(codec->extradata) == 1 || AV_RB32(codec->extradata) == 1))
00978 ff_avc_parse_nal_units_buf(pkt->data, &data, &size);
00979 else
00980 data = pkt->data;
00981 put_ebml_id(pb, blockid);
00982 put_ebml_num(pb, size+4, 0);
00983 avio_w8(pb, 0x80 | (pkt->stream_index + 1));
00984 avio_wb16(pb, ts - mkv->cluster_pts);
00985 avio_w8(pb, flags);
00986 avio_write(pb, data, size);
00987 if (data != pkt->data)
00988 av_free(data);
00989 }
00990
00991 static int srt_get_duration(uint8_t **buf)
00992 {
00993 int i, duration = 0;
00994
00995 for (i=0; i<2 && !duration; i++) {
00996 int s_hour, s_min, s_sec, s_hsec, e_hour, e_min, e_sec, e_hsec;
00997 if (sscanf(*buf, "%d:%2d:%2d%*1[,.]%3d --> %d:%2d:%2d%*1[,.]%3d",
00998 &s_hour, &s_min, &s_sec, &s_hsec,
00999 &e_hour, &e_min, &e_sec, &e_hsec) == 8) {
01000 s_min += 60*s_hour; e_min += 60*e_hour;
01001 s_sec += 60*s_min; e_sec += 60*e_min;
01002 s_hsec += 1000*s_sec; e_hsec += 1000*e_sec;
01003 duration = e_hsec - s_hsec;
01004 }
01005 *buf += strcspn(*buf, "\n") + 1;
01006 }
01007 return duration;
01008 }
01009
01010 static int mkv_write_srt_blocks(AVFormatContext *s, AVIOContext *pb, AVPacket *pkt)
01011 {
01012 ebml_master blockgroup;
01013 AVPacket pkt2 = *pkt;
01014 int64_t duration = srt_get_duration(&pkt2.data);
01015 pkt2.size -= pkt2.data - pkt->data;
01016
01017 blockgroup = start_ebml_master(pb, MATROSKA_ID_BLOCKGROUP,
01018 mkv_blockgroup_size(pkt2.size));
01019 mkv_write_block(s, pb, MATROSKA_ID_BLOCK, &pkt2, 0);
01020 put_ebml_uint(pb, MATROSKA_ID_BLOCKDURATION, duration);
01021 end_ebml_master(pb, blockgroup);
01022
01023 return duration;
01024 }
01025
01026 static void mkv_flush_dynbuf(AVFormatContext *s)
01027 {
01028 MatroskaMuxContext *mkv = s->priv_data;
01029 int bufsize;
01030 uint8_t *dyn_buf;
01031
01032 if (!mkv->dyn_bc)
01033 return;
01034
01035 bufsize = avio_close_dyn_buf(mkv->dyn_bc, &dyn_buf);
01036 avio_write(s->pb, dyn_buf, bufsize);
01037 av_free(dyn_buf);
01038 mkv->dyn_bc = NULL;
01039 }
01040
01041 static int mkv_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
01042 {
01043 MatroskaMuxContext *mkv = s->priv_data;
01044 AVIOContext *pb = s->pb;
01045 AVCodecContext *codec = s->streams[pkt->stream_index]->codec;
01046 int keyframe = !!(pkt->flags & AV_PKT_FLAG_KEY);
01047 int duration = pkt->duration;
01048 int ret;
01049 int64_t ts = mkv->tracks[pkt->stream_index].write_dts ? pkt->dts : pkt->pts;
01050
01051 if (ts == AV_NOPTS_VALUE) {
01052 av_log(s, AV_LOG_ERROR, "Can't write packet with unknown timestamp\n");
01053 return AVERROR(EINVAL);
01054 }
01055
01056 if (!s->pb->seekable) {
01057 if (!mkv->dyn_bc)
01058 avio_open_dyn_buf(&mkv->dyn_bc);
01059 pb = mkv->dyn_bc;
01060 }
01061
01062 if (!mkv->cluster_pos) {
01063 mkv->cluster_pos = avio_tell(s->pb);
01064 mkv->cluster = start_ebml_master(pb, MATROSKA_ID_CLUSTER, 0);
01065 put_ebml_uint(pb, MATROSKA_ID_CLUSTERTIMECODE, FFMAX(0, ts));
01066 mkv->cluster_pts = FFMAX(0, ts);
01067 }
01068
01069 if (codec->codec_type != AVMEDIA_TYPE_SUBTITLE) {
01070 mkv_write_block(s, pb, MATROSKA_ID_SIMPLEBLOCK, pkt, keyframe << 7);
01071 } else if (codec->codec_id == CODEC_ID_SSA) {
01072 duration = mkv_write_ass_blocks(s, pb, pkt);
01073 } else if (codec->codec_id == CODEC_ID_SRT) {
01074 duration = mkv_write_srt_blocks(s, pb, pkt);
01075 } else {
01076 ebml_master blockgroup = start_ebml_master(pb, MATROSKA_ID_BLOCKGROUP, mkv_blockgroup_size(pkt->size));
01077 duration = pkt->convergence_duration;
01078 mkv_write_block(s, pb, MATROSKA_ID_BLOCK, pkt, 0);
01079 put_ebml_uint(pb, MATROSKA_ID_BLOCKDURATION, duration);
01080 end_ebml_master(pb, blockgroup);
01081 }
01082
01083 if (codec->codec_type == AVMEDIA_TYPE_VIDEO && keyframe) {
01084 ret = mkv_add_cuepoint(mkv->cues, pkt->stream_index, ts, mkv->cluster_pos);
01085 if (ret < 0) return ret;
01086 }
01087
01088 mkv->duration = FFMAX(mkv->duration, ts + duration);
01089 return 0;
01090 }
01091
01092 static int mkv_copy_packet(MatroskaMuxContext *mkv, const AVPacket *pkt)
01093 {
01094 uint8_t *data = mkv->cur_audio_pkt.data;
01095 mkv->cur_audio_pkt = *pkt;
01096 mkv->cur_audio_pkt.data = av_fast_realloc(data, &mkv->audio_buffer_size, pkt->size);
01097 if (!mkv->cur_audio_pkt.data)
01098 return AVERROR(ENOMEM);
01099
01100 memcpy(mkv->cur_audio_pkt.data, pkt->data, pkt->size);
01101 mkv->cur_audio_pkt.size = pkt->size;
01102 return 0;
01103 }
01104
01105 static int mkv_write_packet(AVFormatContext *s, AVPacket *pkt)
01106 {
01107 MatroskaMuxContext *mkv = s->priv_data;
01108 AVIOContext *pb = s->pb->seekable ? s->pb : mkv->dyn_bc;
01109 AVCodecContext *codec = s->streams[pkt->stream_index]->codec;
01110 int ret, keyframe = !!(pkt->flags & AV_PKT_FLAG_KEY);
01111 int64_t ts = mkv->tracks[pkt->stream_index].write_dts ? pkt->dts : pkt->pts;
01112 int cluster_size = avio_tell(pb) - (s->pb->seekable ? mkv->cluster_pos : 0);
01113
01114
01115
01116 if (mkv->cluster_pos &&
01117 ((!s->pb->seekable && (cluster_size > 32*1024 || ts > mkv->cluster_pts + 1000))
01118 || cluster_size > 5*1024*1024 || ts > mkv->cluster_pts + 5000
01119 || (codec->codec_type == AVMEDIA_TYPE_VIDEO && keyframe && cluster_size > 4*1024))) {
01120 av_log(s, AV_LOG_DEBUG, "Starting new cluster at offset %" PRIu64
01121 " bytes, pts %" PRIu64 "\n", avio_tell(pb), ts);
01122 end_ebml_master(pb, mkv->cluster);
01123 mkv->cluster_pos = 0;
01124 if (mkv->dyn_bc)
01125 mkv_flush_dynbuf(s);
01126 }
01127
01128
01129 if (mkv->cur_audio_pkt.size > 0) {
01130 ret = mkv_write_packet_internal(s, &mkv->cur_audio_pkt);
01131 mkv->cur_audio_pkt.size = 0;
01132 if (ret < 0) {
01133 av_log(s, AV_LOG_ERROR, "Could not write cached audio packet ret:%d\n", ret);
01134 return ret;
01135 }
01136 }
01137
01138
01139
01140 if (codec->codec_type == AVMEDIA_TYPE_AUDIO)
01141 ret = mkv_copy_packet(mkv, pkt);
01142 else
01143 ret = mkv_write_packet_internal(s, pkt);
01144 return ret;
01145 }
01146
01147 static int mkv_write_trailer(AVFormatContext *s)
01148 {
01149 MatroskaMuxContext *mkv = s->priv_data;
01150 AVIOContext *pb = s->pb;
01151 int64_t currentpos, cuespos;
01152 int ret;
01153
01154
01155 if (mkv->cur_audio_pkt.size > 0) {
01156 ret = mkv_write_packet_internal(s, &mkv->cur_audio_pkt);
01157 mkv->cur_audio_pkt.size = 0;
01158 if (ret < 0) {
01159 av_log(s, AV_LOG_ERROR, "Could not write cached audio packet ret:%d\n", ret);
01160 return ret;
01161 }
01162 }
01163
01164 if (mkv->dyn_bc) {
01165 end_ebml_master(mkv->dyn_bc, mkv->cluster);
01166 mkv_flush_dynbuf(s);
01167 } else if (mkv->cluster_pos) {
01168 end_ebml_master(pb, mkv->cluster);
01169 }
01170
01171 if (pb->seekable) {
01172 if (mkv->cues->num_entries) {
01173 cuespos = mkv_write_cues(pb, mkv->cues, s->nb_streams);
01174
01175 ret = mkv_add_seekhead_entry(mkv->main_seekhead, MATROSKA_ID_CUES, cuespos);
01176 if (ret < 0) return ret;
01177 }
01178
01179 mkv_write_seekhead(pb, mkv->main_seekhead);
01180
01181
01182 av_log(s, AV_LOG_DEBUG, "end duration = %" PRIu64 "\n", mkv->duration);
01183 currentpos = avio_tell(pb);
01184 avio_seek(pb, mkv->duration_offset, SEEK_SET);
01185 put_ebml_float(pb, MATROSKA_ID_DURATION, mkv->duration);
01186
01187 avio_seek(pb, currentpos, SEEK_SET);
01188 }
01189
01190 end_ebml_master(pb, mkv->segment);
01191 av_free(mkv->tracks);
01192 av_freep(&mkv->cues->entries);
01193 av_freep(&mkv->cues);
01194 av_destruct_packet(&mkv->cur_audio_pkt);
01195 avio_flush(pb);
01196 return 0;
01197 }
01198
01199 #if CONFIG_MATROSKA_MUXER
01200 AVOutputFormat ff_matroska_muxer = {
01201 "matroska",
01202 NULL_IF_CONFIG_SMALL("Matroska file format"),
01203 "video/x-matroska",
01204 "mkv",
01205 sizeof(MatroskaMuxContext),
01206 CODEC_ID_MP2,
01207 CODEC_ID_MPEG4,
01208 mkv_write_header,
01209 mkv_write_packet,
01210 mkv_write_trailer,
01211 .flags = AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS,
01212 .subtitle_codec = CODEC_ID_TEXT,
01213 };
01214 #endif
01215
01216 #if CONFIG_WEBM_MUXER
01217 AVOutputFormat ff_webm_muxer = {
01218 "webm",
01219 NULL_IF_CONFIG_SMALL("WebM file format"),
01220 "video/webm",
01221 "webm",
01222 sizeof(MatroskaMuxContext),
01223 CODEC_ID_VORBIS,
01224 CODEC_ID_VP8,
01225 mkv_write_header,
01226 mkv_write_packet,
01227 mkv_write_trailer,
01228 .flags = AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT,
01229 };
01230 #endif
01231
01232 #if CONFIG_MATROSKA_AUDIO_MUXER
01233 AVOutputFormat ff_matroska_audio_muxer = {
01234 "matroska",
01235 NULL_IF_CONFIG_SMALL("Matroska file format"),
01236 "audio/x-matroska",
01237 "mka",
01238 sizeof(MatroskaMuxContext),
01239 CODEC_ID_MP2,
01240 CODEC_ID_NONE,
01241 mkv_write_header,
01242 mkv_write_packet,
01243 mkv_write_trailer,
01244 .flags = AVFMT_GLOBALHEADER,
01245 };
01246 #endif