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 "libavutil/intreadwrite.h"
00029 #include "libavutil/md5.h"
00030 #include "libavcodec/xiph.h"
00031 #include "libavcodec/mpeg4audio.h"
00032
00033 typedef struct ebml_master {
00034 int64_t pos;
00035 int sizebytes;
00036 } ebml_master;
00037
00038 typedef struct mkv_seekhead_entry {
00039 unsigned int elementid;
00040 uint64_t segmentpos;
00041 } mkv_seekhead_entry;
00042
00043 typedef struct mkv_seekhead {
00044 int64_t filepos;
00045 int64_t segment_offset;
00046 int reserved_size;
00047 int max_entries;
00048 mkv_seekhead_entry *entries;
00049 int num_entries;
00050 } mkv_seekhead;
00051
00052 typedef struct {
00053 uint64_t pts;
00054 int tracknum;
00055 int64_t cluster_pos;
00056 } mkv_cuepoint;
00057
00058 typedef struct {
00059 int64_t segment_offset;
00060 mkv_cuepoint *entries;
00061 int num_entries;
00062 } mkv_cues;
00063
00064 typedef struct {
00065 int write_dts;
00066 } mkv_track;
00067
00068 #define MODE_MATROSKAv2 0x01
00069 #define MODE_WEBM 0x02
00070
00071 typedef struct MatroskaMuxContext {
00072 int mode;
00073 ByteIOContext *dyn_bc;
00074 ebml_master segment;
00075 int64_t segment_offset;
00076 int64_t segment_uid;
00077 ebml_master cluster;
00078 int64_t cluster_pos;
00079 int64_t cluster_pts;
00080 int64_t duration_offset;
00081 int64_t duration;
00082 mkv_seekhead *main_seekhead;
00083 mkv_seekhead *cluster_seekhead;
00084 mkv_cues *cues;
00085 mkv_track *tracks;
00086
00087 struct AVMD5 *md5_ctx;
00088 } MatroskaMuxContext;
00089
00090
00093 #define MAX_SEEKENTRY_SIZE 21
00094
00097 #define MAX_CUETRACKPOS_SIZE 22
00098
00100 #define MAX_CUEPOINT_SIZE(num_tracks) 12 + MAX_CUETRACKPOS_SIZE*num_tracks
00101
00102
00103 static int ebml_id_size(unsigned int id)
00104 {
00105 return (av_log2(id+1)-1)/7+1;
00106 }
00107
00108 static void put_ebml_id(ByteIOContext *pb, unsigned int id)
00109 {
00110 int i = ebml_id_size(id);
00111 while (i--)
00112 put_byte(pb, id >> (i*8));
00113 }
00114
00120 static void put_ebml_size_unknown(ByteIOContext *pb, int bytes)
00121 {
00122 assert(bytes <= 8);
00123 put_byte(pb, 0x1ff >> bytes);
00124 while (--bytes)
00125 put_byte(pb, 0xff);
00126 }
00127
00131 static int ebml_num_size(uint64_t num)
00132 {
00133 int bytes = 1;
00134 while ((num+1) >> bytes*7) bytes++;
00135 return bytes;
00136 }
00137
00144 static void put_ebml_num(ByteIOContext *pb, uint64_t num, int bytes)
00145 {
00146 int i, needed_bytes = ebml_num_size(num);
00147
00148
00149 assert(num < (1ULL<<56)-1);
00150
00151 if (bytes == 0)
00152
00153 bytes = needed_bytes;
00154
00155
00156 assert(bytes >= needed_bytes);
00157
00158 num |= 1ULL << bytes*7;
00159 for (i = bytes - 1; i >= 0; i--)
00160 put_byte(pb, num >> i*8);
00161 }
00162
00163 static void put_ebml_uint(ByteIOContext *pb, unsigned int elementid, uint64_t val)
00164 {
00165 int i, bytes = 1;
00166 uint64_t tmp = val;
00167 while (tmp>>=8) bytes++;
00168
00169 put_ebml_id(pb, elementid);
00170 put_ebml_num(pb, bytes, 0);
00171 for (i = bytes - 1; i >= 0; i--)
00172 put_byte(pb, val >> i*8);
00173 }
00174
00175 static void put_ebml_float(ByteIOContext *pb, unsigned int elementid, double val)
00176 {
00177 put_ebml_id(pb, elementid);
00178 put_ebml_num(pb, 8, 0);
00179 put_be64(pb, av_dbl2int(val));
00180 }
00181
00182 static void put_ebml_binary(ByteIOContext *pb, unsigned int elementid,
00183 const uint8_t *buf, int size)
00184 {
00185 put_ebml_id(pb, elementid);
00186 put_ebml_num(pb, size, 0);
00187 put_buffer(pb, buf, size);
00188 }
00189
00190 static void put_ebml_string(ByteIOContext *pb, unsigned int elementid, const char *str)
00191 {
00192 put_ebml_binary(pb, elementid, str, strlen(str));
00193 }
00194
00201 static void put_ebml_void(ByteIOContext *pb, uint64_t size)
00202 {
00203 int64_t currentpos = url_ftell(pb);
00204
00205 assert(size >= 2);
00206
00207 put_ebml_id(pb, EBML_ID_VOID);
00208
00209
00210
00211 if (size < 10)
00212 put_ebml_num(pb, size-1, 0);
00213 else
00214 put_ebml_num(pb, size-9, 8);
00215 while(url_ftell(pb) < currentpos + size)
00216 put_byte(pb, 0);
00217 }
00218
00219 static ebml_master start_ebml_master(ByteIOContext *pb, unsigned int elementid, uint64_t expectedsize)
00220 {
00221 int bytes = expectedsize ? ebml_num_size(expectedsize) : 8;
00222 put_ebml_id(pb, elementid);
00223 put_ebml_size_unknown(pb, bytes);
00224 return (ebml_master){ url_ftell(pb), bytes };
00225 }
00226
00227 static void end_ebml_master(ByteIOContext *pb, ebml_master master)
00228 {
00229 int64_t pos = url_ftell(pb);
00230
00231 if (url_fseek(pb, master.pos - master.sizebytes, SEEK_SET) < 0)
00232 return;
00233 put_ebml_num(pb, pos - master.pos, master.sizebytes);
00234 url_fseek(pb, pos, SEEK_SET);
00235 }
00236
00237 static void put_xiph_size(ByteIOContext *pb, int size)
00238 {
00239 int i;
00240 for (i = 0; i < size / 255; i++)
00241 put_byte(pb, 255);
00242 put_byte(pb, size % 255);
00243 }
00244
00256 static mkv_seekhead * mkv_start_seekhead(ByteIOContext *pb, int64_t segment_offset, int numelements)
00257 {
00258 mkv_seekhead *new_seekhead = av_mallocz(sizeof(mkv_seekhead));
00259 if (new_seekhead == NULL)
00260 return NULL;
00261
00262 new_seekhead->segment_offset = segment_offset;
00263
00264 if (numelements > 0) {
00265 new_seekhead->filepos = url_ftell(pb);
00266
00267
00268
00269 new_seekhead->reserved_size = numelements * MAX_SEEKENTRY_SIZE + 13;
00270 new_seekhead->max_entries = numelements;
00271 put_ebml_void(pb, new_seekhead->reserved_size);
00272 }
00273 return new_seekhead;
00274 }
00275
00276 static int mkv_add_seekhead_entry(mkv_seekhead *seekhead, unsigned int elementid, uint64_t filepos)
00277 {
00278 mkv_seekhead_entry *entries = seekhead->entries;
00279
00280
00281 if (seekhead->max_entries > 0 && seekhead->max_entries <= seekhead->num_entries)
00282 return -1;
00283
00284 entries = av_realloc(entries, (seekhead->num_entries + 1) * sizeof(mkv_seekhead_entry));
00285 if (entries == NULL)
00286 return AVERROR(ENOMEM);
00287
00288 entries[seekhead->num_entries ].elementid = elementid;
00289 entries[seekhead->num_entries++].segmentpos = filepos - seekhead->segment_offset;
00290
00291 seekhead->entries = entries;
00292 return 0;
00293 }
00294
00304 static int64_t mkv_write_seekhead(ByteIOContext *pb, mkv_seekhead *seekhead)
00305 {
00306 ebml_master metaseek, seekentry;
00307 int64_t currentpos;
00308 int i;
00309
00310 currentpos = url_ftell(pb);
00311
00312 if (seekhead->reserved_size > 0)
00313 if (url_fseek(pb, seekhead->filepos, SEEK_SET) < 0)
00314 return -1;
00315
00316 metaseek = start_ebml_master(pb, MATROSKA_ID_SEEKHEAD, seekhead->reserved_size);
00317 for (i = 0; i < seekhead->num_entries; i++) {
00318 mkv_seekhead_entry *entry = &seekhead->entries[i];
00319
00320 seekentry = start_ebml_master(pb, MATROSKA_ID_SEEKENTRY, MAX_SEEKENTRY_SIZE);
00321
00322 put_ebml_id(pb, MATROSKA_ID_SEEKID);
00323 put_ebml_num(pb, ebml_id_size(entry->elementid), 0);
00324 put_ebml_id(pb, entry->elementid);
00325
00326 put_ebml_uint(pb, MATROSKA_ID_SEEKPOSITION, entry->segmentpos);
00327 end_ebml_master(pb, seekentry);
00328 }
00329 end_ebml_master(pb, metaseek);
00330
00331 if (seekhead->reserved_size > 0) {
00332 uint64_t remaining = seekhead->filepos + seekhead->reserved_size - url_ftell(pb);
00333 put_ebml_void(pb, remaining);
00334 url_fseek(pb, currentpos, SEEK_SET);
00335
00336 currentpos = seekhead->filepos;
00337 }
00338 av_free(seekhead->entries);
00339 av_free(seekhead);
00340
00341 return currentpos;
00342 }
00343
00344 static mkv_cues * mkv_start_cues(int64_t segment_offset)
00345 {
00346 mkv_cues *cues = av_mallocz(sizeof(mkv_cues));
00347 if (cues == NULL)
00348 return NULL;
00349
00350 cues->segment_offset = segment_offset;
00351 return cues;
00352 }
00353
00354 static int mkv_add_cuepoint(mkv_cues *cues, int stream, int64_t ts, int64_t cluster_pos)
00355 {
00356 mkv_cuepoint *entries = cues->entries;
00357
00358 entries = av_realloc(entries, (cues->num_entries + 1) * sizeof(mkv_cuepoint));
00359 if (entries == NULL)
00360 return AVERROR(ENOMEM);
00361
00362 if (ts < 0)
00363 return 0;
00364
00365 entries[cues->num_entries ].pts = ts;
00366 entries[cues->num_entries ].tracknum = stream + 1;
00367 entries[cues->num_entries++].cluster_pos = cluster_pos - cues->segment_offset;
00368
00369 cues->entries = entries;
00370 return 0;
00371 }
00372
00373 static int64_t mkv_write_cues(ByteIOContext *pb, mkv_cues *cues, int num_tracks)
00374 {
00375 ebml_master cues_element;
00376 int64_t currentpos;
00377 int i, j;
00378
00379 currentpos = url_ftell(pb);
00380 cues_element = start_ebml_master(pb, MATROSKA_ID_CUES, 0);
00381
00382 for (i = 0; i < cues->num_entries; i++) {
00383 ebml_master cuepoint, track_positions;
00384 mkv_cuepoint *entry = &cues->entries[i];
00385 uint64_t pts = entry->pts;
00386
00387 cuepoint = start_ebml_master(pb, MATROSKA_ID_POINTENTRY, MAX_CUEPOINT_SIZE(num_tracks));
00388 put_ebml_uint(pb, MATROSKA_ID_CUETIME, pts);
00389
00390
00391
00392 for (j = 0; j < cues->num_entries - i && entry[j].pts == pts; j++) {
00393 track_positions = start_ebml_master(pb, MATROSKA_ID_CUETRACKPOSITION, MAX_CUETRACKPOS_SIZE);
00394 put_ebml_uint(pb, MATROSKA_ID_CUETRACK , entry[j].tracknum );
00395 put_ebml_uint(pb, MATROSKA_ID_CUECLUSTERPOSITION, entry[j].cluster_pos);
00396 end_ebml_master(pb, track_positions);
00397 }
00398 i += j - 1;
00399 end_ebml_master(pb, cuepoint);
00400 }
00401 end_ebml_master(pb, cues_element);
00402
00403 av_free(cues->entries);
00404 av_free(cues);
00405 return currentpos;
00406 }
00407
00408 static int put_xiph_codecpriv(AVFormatContext *s, ByteIOContext *pb, AVCodecContext *codec)
00409 {
00410 uint8_t *header_start[3];
00411 int header_len[3];
00412 int first_header_size;
00413 int j;
00414
00415 if (codec->codec_id == CODEC_ID_VORBIS)
00416 first_header_size = 30;
00417 else
00418 first_header_size = 42;
00419
00420 if (ff_split_xiph_headers(codec->extradata, codec->extradata_size,
00421 first_header_size, header_start, header_len) < 0) {
00422 av_log(s, AV_LOG_ERROR, "Extradata corrupt.\n");
00423 return -1;
00424 }
00425
00426 put_byte(pb, 2);
00427 for (j = 0; j < 2; j++) {
00428 put_xiph_size(pb, header_len[j]);
00429 }
00430 for (j = 0; j < 3; j++)
00431 put_buffer(pb, header_start[j], header_len[j]);
00432
00433 return 0;
00434 }
00435
00436 static void get_aac_sample_rates(AVFormatContext *s, AVCodecContext *codec, int *sample_rate, int *output_sample_rate)
00437 {
00438 int sri;
00439
00440 if (codec->extradata_size < 2) {
00441 av_log(s, AV_LOG_WARNING, "No AAC extradata, unable to determine samplerate.\n");
00442 return;
00443 }
00444
00445 sri = ((codec->extradata[0] << 1) & 0xE) | (codec->extradata[1] >> 7);
00446 if (sri > 12) {
00447 av_log(s, AV_LOG_WARNING, "AAC samplerate index out of bounds\n");
00448 return;
00449 }
00450 *sample_rate = ff_mpeg4audio_sample_rates[sri];
00451
00452
00453 if (codec->extradata_size == 5) {
00454 sri = (codec->extradata[4] >> 3) & 0xF;
00455 if (sri > 12) {
00456 av_log(s, AV_LOG_WARNING, "AAC output samplerate index out of bounds\n");
00457 return;
00458 }
00459 *output_sample_rate = ff_mpeg4audio_sample_rates[sri];
00460 }
00461 }
00462
00463 static int mkv_write_codecprivate(AVFormatContext *s, ByteIOContext *pb, AVCodecContext *codec, int native_id, int qt_id)
00464 {
00465 ByteIOContext *dyn_cp;
00466 uint8_t *codecpriv;
00467 int ret, codecpriv_size;
00468
00469 ret = url_open_dyn_buf(&dyn_cp);
00470 if(ret < 0)
00471 return ret;
00472
00473 if (native_id) {
00474 if (codec->codec_id == CODEC_ID_VORBIS || codec->codec_id == CODEC_ID_THEORA)
00475 ret = put_xiph_codecpriv(s, dyn_cp, codec);
00476 else if (codec->codec_id == CODEC_ID_FLAC)
00477 ret = ff_flac_write_header(dyn_cp, codec, 1);
00478 else if (codec->codec_id == CODEC_ID_H264)
00479 ret = ff_isom_write_avcc(dyn_cp, codec->extradata, codec->extradata_size);
00480 else if (codec->extradata_size)
00481 put_buffer(dyn_cp, codec->extradata, codec->extradata_size);
00482 } else if (codec->codec_type == AVMEDIA_TYPE_VIDEO) {
00483 if (qt_id) {
00484 if (!codec->codec_tag)
00485 codec->codec_tag = ff_codec_get_tag(codec_movvideo_tags, codec->codec_id);
00486 if (codec->extradata_size)
00487 put_buffer(dyn_cp, codec->extradata, codec->extradata_size);
00488 } else {
00489 if (!codec->codec_tag)
00490 codec->codec_tag = ff_codec_get_tag(ff_codec_bmp_tags, codec->codec_id);
00491 if (!codec->codec_tag) {
00492 av_log(s, AV_LOG_ERROR, "No bmp codec ID found.\n");
00493 ret = -1;
00494 }
00495
00496 ff_put_bmp_header(dyn_cp, codec, ff_codec_bmp_tags, 0);
00497 }
00498
00499 } else if (codec->codec_type == AVMEDIA_TYPE_AUDIO) {
00500 unsigned int tag;
00501 tag = ff_codec_get_tag(ff_codec_wav_tags, codec->codec_id);
00502 if (!tag) {
00503 av_log(s, AV_LOG_ERROR, "No wav codec ID found.\n");
00504 ret = -1;
00505 }
00506 if (!codec->codec_tag)
00507 codec->codec_tag = tag;
00508
00509 ff_put_wav_header(dyn_cp, codec);
00510 }
00511
00512 codecpriv_size = url_close_dyn_buf(dyn_cp, &codecpriv);
00513 if (codecpriv_size)
00514 put_ebml_binary(pb, MATROSKA_ID_CODECPRIVATE, codecpriv, codecpriv_size);
00515 av_free(codecpriv);
00516 return ret;
00517 }
00518
00519 static int mkv_write_tracks(AVFormatContext *s)
00520 {
00521 MatroskaMuxContext *mkv = s->priv_data;
00522 ByteIOContext *pb = s->pb;
00523 ebml_master tracks;
00524 int i, j, ret;
00525
00526 ret = mkv_add_seekhead_entry(mkv->main_seekhead, MATROSKA_ID_TRACKS, url_ftell(pb));
00527 if (ret < 0) return ret;
00528
00529 tracks = start_ebml_master(pb, MATROSKA_ID_TRACKS, 0);
00530 for (i = 0; i < s->nb_streams; i++) {
00531 AVStream *st = s->streams[i];
00532 AVCodecContext *codec = st->codec;
00533 ebml_master subinfo, track;
00534 int native_id = 0;
00535 int qt_id = 0;
00536 int bit_depth = av_get_bits_per_sample(codec->codec_id);
00537 int sample_rate = codec->sample_rate;
00538 int output_sample_rate = 0;
00539 AVMetadataTag *tag;
00540
00541 if (!bit_depth)
00542 bit_depth = av_get_bits_per_sample_format(codec->sample_fmt);
00543
00544 if (codec->codec_id == CODEC_ID_AAC)
00545 get_aac_sample_rates(s, codec, &sample_rate, &output_sample_rate);
00546
00547 track = start_ebml_master(pb, MATROSKA_ID_TRACKENTRY, 0);
00548 put_ebml_uint (pb, MATROSKA_ID_TRACKNUMBER , i + 1);
00549 put_ebml_uint (pb, MATROSKA_ID_TRACKUID , i + 1);
00550 put_ebml_uint (pb, MATROSKA_ID_TRACKFLAGLACING , 0);
00551
00552 if ((tag = av_metadata_get(st->metadata, "title", NULL, 0)))
00553 put_ebml_string(pb, MATROSKA_ID_TRACKNAME, tag->value);
00554 tag = av_metadata_get(st->metadata, "language", NULL, 0);
00555 put_ebml_string(pb, MATROSKA_ID_TRACKLANGUAGE, tag ? tag->value:"und");
00556
00557 if (st->disposition)
00558 put_ebml_uint(pb, MATROSKA_ID_TRACKFLAGDEFAULT, !!(st->disposition & AV_DISPOSITION_DEFAULT));
00559
00560
00561
00562 for (j = 0; ff_mkv_codec_tags[j].id != CODEC_ID_NONE; j++) {
00563 if (ff_mkv_codec_tags[j].id == codec->codec_id) {
00564 put_ebml_string(pb, MATROSKA_ID_CODECID, ff_mkv_codec_tags[j].str);
00565 native_id = 1;
00566 break;
00567 }
00568 }
00569
00570 if (mkv->mode == MODE_WEBM && !(codec->codec_id == CODEC_ID_VP8 ||
00571 codec->codec_id == CODEC_ID_VORBIS)) {
00572 av_log(s, AV_LOG_ERROR,
00573 "Only VP8 video and Vorbis audio are supported for WebM.\n");
00574 return AVERROR(EINVAL);
00575 }
00576
00577 switch (codec->codec_type) {
00578 case AVMEDIA_TYPE_VIDEO:
00579 put_ebml_uint(pb, MATROSKA_ID_TRACKTYPE, MATROSKA_TRACK_TYPE_VIDEO);
00580 put_ebml_uint(pb, MATROSKA_ID_TRACKDEFAULTDURATION, av_q2d(codec->time_base)*1E9);
00581
00582 if (!native_id &&
00583 ff_codec_get_tag(codec_movvideo_tags, codec->codec_id) &&
00584 (!ff_codec_get_tag(ff_codec_bmp_tags, codec->codec_id)
00585 || codec->codec_id == CODEC_ID_SVQ1
00586 || codec->codec_id == CODEC_ID_SVQ3
00587 || codec->codec_id == CODEC_ID_CINEPAK))
00588 qt_id = 1;
00589
00590 if (qt_id)
00591 put_ebml_string(pb, MATROSKA_ID_CODECID, "V_QUICKTIME");
00592 else if (!native_id) {
00593
00594 put_ebml_string(pb, MATROSKA_ID_CODECID, "V_MS/VFW/FOURCC");
00595 mkv->tracks[i].write_dts = 1;
00596 }
00597
00598 subinfo = start_ebml_master(pb, MATROSKA_ID_TRACKVIDEO, 0);
00599
00600 put_ebml_uint (pb, MATROSKA_ID_VIDEOPIXELWIDTH , codec->width);
00601 put_ebml_uint (pb, MATROSKA_ID_VIDEOPIXELHEIGHT, codec->height);
00602 if (st->sample_aspect_ratio.num) {
00603 int d_width = codec->width*av_q2d(st->sample_aspect_ratio);
00604 put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYWIDTH , d_width);
00605 put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYHEIGHT, codec->height);
00606 }
00607 end_ebml_master(pb, subinfo);
00608 break;
00609
00610 case AVMEDIA_TYPE_AUDIO:
00611 put_ebml_uint(pb, MATROSKA_ID_TRACKTYPE, MATROSKA_TRACK_TYPE_AUDIO);
00612
00613 if (!native_id)
00614
00615 put_ebml_string(pb, MATROSKA_ID_CODECID, "A_MS/ACM");
00616
00617 subinfo = start_ebml_master(pb, MATROSKA_ID_TRACKAUDIO, 0);
00618 put_ebml_uint (pb, MATROSKA_ID_AUDIOCHANNELS , codec->channels);
00619 put_ebml_float (pb, MATROSKA_ID_AUDIOSAMPLINGFREQ, sample_rate);
00620 if (output_sample_rate)
00621 put_ebml_float(pb, MATROSKA_ID_AUDIOOUTSAMPLINGFREQ, output_sample_rate);
00622 if (bit_depth)
00623 put_ebml_uint(pb, MATROSKA_ID_AUDIOBITDEPTH, bit_depth);
00624 end_ebml_master(pb, subinfo);
00625 break;
00626
00627 case AVMEDIA_TYPE_SUBTITLE:
00628 put_ebml_uint(pb, MATROSKA_ID_TRACKTYPE, MATROSKA_TRACK_TYPE_SUBTITLE);
00629 break;
00630 default:
00631 av_log(s, AV_LOG_ERROR, "Only audio, video, and subtitles are supported for Matroska.");
00632 break;
00633 }
00634 ret = mkv_write_codecprivate(s, pb, codec, native_id, qt_id);
00635 if (ret < 0) return ret;
00636
00637 end_ebml_master(pb, track);
00638
00639
00640 av_set_pts_info(st, 64, 1, 1000);
00641 }
00642 end_ebml_master(pb, tracks);
00643 return 0;
00644 }
00645
00646 static int mkv_write_chapters(AVFormatContext *s)
00647 {
00648 MatroskaMuxContext *mkv = s->priv_data;
00649 ByteIOContext *pb = s->pb;
00650 ebml_master chapters, editionentry;
00651 AVRational scale = {1, 1E9};
00652 int i, ret;
00653
00654 if (!s->nb_chapters)
00655 return 0;
00656
00657 ret = mkv_add_seekhead_entry(mkv->main_seekhead, MATROSKA_ID_CHAPTERS, url_ftell(pb));
00658 if (ret < 0) return ret;
00659
00660 chapters = start_ebml_master(pb, MATROSKA_ID_CHAPTERS , 0);
00661 editionentry = start_ebml_master(pb, MATROSKA_ID_EDITIONENTRY, 0);
00662 put_ebml_uint(pb, MATROSKA_ID_EDITIONFLAGDEFAULT, 1);
00663 put_ebml_uint(pb, MATROSKA_ID_EDITIONFLAGHIDDEN , 0);
00664 for (i = 0; i < s->nb_chapters; i++) {
00665 ebml_master chapteratom, chapterdisplay;
00666 AVChapter *c = s->chapters[i];
00667 AVMetadataTag *t = NULL;
00668
00669 chapteratom = start_ebml_master(pb, MATROSKA_ID_CHAPTERATOM, 0);
00670 put_ebml_uint(pb, MATROSKA_ID_CHAPTERUID, c->id);
00671 put_ebml_uint(pb, MATROSKA_ID_CHAPTERTIMESTART,
00672 av_rescale_q(c->start, c->time_base, scale));
00673 put_ebml_uint(pb, MATROSKA_ID_CHAPTERTIMEEND,
00674 av_rescale_q(c->end, c->time_base, scale));
00675 put_ebml_uint(pb, MATROSKA_ID_CHAPTERFLAGHIDDEN , 0);
00676 put_ebml_uint(pb, MATROSKA_ID_CHAPTERFLAGENABLED, 1);
00677 if ((t = av_metadata_get(c->metadata, "title", NULL, 0))) {
00678 chapterdisplay = start_ebml_master(pb, MATROSKA_ID_CHAPTERDISPLAY, 0);
00679 put_ebml_string(pb, MATROSKA_ID_CHAPSTRING, t->value);
00680 put_ebml_string(pb, MATROSKA_ID_CHAPLANG , "und");
00681 end_ebml_master(pb, chapterdisplay);
00682 }
00683 end_ebml_master(pb, chapteratom);
00684 }
00685 end_ebml_master(pb, editionentry);
00686 end_ebml_master(pb, chapters);
00687 return 0;
00688 }
00689
00690 static int mkv_write_header(AVFormatContext *s)
00691 {
00692 MatroskaMuxContext *mkv = s->priv_data;
00693 ByteIOContext *pb = s->pb;
00694 ebml_master ebml_header, segment_info;
00695 AVMetadataTag *tag;
00696 int ret;
00697
00698 if (!strcmp(s->oformat->name, "webm")) mkv->mode = MODE_WEBM;
00699 else mkv->mode = MODE_MATROSKAv2;
00700
00701 mkv->md5_ctx = av_mallocz(av_md5_size);
00702 av_md5_init(mkv->md5_ctx);
00703 mkv->tracks = av_mallocz(s->nb_streams * sizeof(*mkv->tracks));
00704
00705 ebml_header = start_ebml_master(pb, EBML_ID_HEADER, 0);
00706 put_ebml_uint (pb, EBML_ID_EBMLVERSION , 1);
00707 put_ebml_uint (pb, EBML_ID_EBMLREADVERSION , 1);
00708 put_ebml_uint (pb, EBML_ID_EBMLMAXIDLENGTH , 4);
00709 put_ebml_uint (pb, EBML_ID_EBMLMAXSIZELENGTH , 8);
00710 put_ebml_string (pb, EBML_ID_DOCTYPE , s->oformat->name);
00711 put_ebml_uint (pb, EBML_ID_DOCTYPEVERSION , 2);
00712 put_ebml_uint (pb, EBML_ID_DOCTYPEREADVERSION , 2);
00713 end_ebml_master(pb, ebml_header);
00714
00715 mkv->segment = start_ebml_master(pb, MATROSKA_ID_SEGMENT, 0);
00716 mkv->segment_offset = url_ftell(pb);
00717
00718
00719
00720
00721
00722
00723 mkv->main_seekhead = mkv_start_seekhead(pb, mkv->segment_offset, 10);
00724 mkv->cluster_seekhead = mkv_start_seekhead(pb, mkv->segment_offset, 0);
00725 if (mkv->main_seekhead == NULL || mkv->cluster_seekhead == NULL)
00726 return AVERROR(ENOMEM);
00727
00728 ret = mkv_add_seekhead_entry(mkv->main_seekhead, MATROSKA_ID_INFO, url_ftell(pb));
00729 if (ret < 0) return ret;
00730
00731 segment_info = start_ebml_master(pb, MATROSKA_ID_INFO, 0);
00732 put_ebml_uint(pb, MATROSKA_ID_TIMECODESCALE, 1000000);
00733 if ((tag = av_metadata_get(s->metadata, "title", NULL, 0)))
00734 put_ebml_string(pb, MATROSKA_ID_TITLE, tag->value);
00735 if (!(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT)) {
00736 put_ebml_string(pb, MATROSKA_ID_MUXINGAPP , LIBAVFORMAT_IDENT);
00737 put_ebml_string(pb, MATROSKA_ID_WRITINGAPP, LIBAVFORMAT_IDENT);
00738
00739
00740 mkv->segment_uid = url_ftell(pb);
00741 put_ebml_void(pb, 19);
00742 }
00743
00744
00745 mkv->duration = 0;
00746 mkv->duration_offset = url_ftell(pb);
00747 put_ebml_void(pb, 11);
00748 end_ebml_master(pb, segment_info);
00749
00750 ret = mkv_write_tracks(s);
00751 if (ret < 0) return ret;
00752
00753 if (mkv->mode != MODE_WEBM) {
00754 ret = mkv_write_chapters(s);
00755 if (ret < 0) return ret;
00756 }
00757
00758 if (url_is_streamed(s->pb))
00759 mkv_write_seekhead(pb, mkv->main_seekhead);
00760
00761 mkv->cues = mkv_start_cues(mkv->segment_offset);
00762 if (mkv->cues == NULL)
00763 return AVERROR(ENOMEM);
00764
00765 put_flush_packet(pb);
00766 return 0;
00767 }
00768
00769 static int mkv_blockgroup_size(int pkt_size)
00770 {
00771 int size = pkt_size + 4;
00772 size += ebml_num_size(size);
00773 size += 2;
00774 size += 8;
00775 size += ebml_num_size(size);
00776 size += 1;
00777 return size;
00778 }
00779
00780 static int ass_get_duration(const uint8_t *p)
00781 {
00782 int sh, sm, ss, sc, eh, em, es, ec;
00783 uint64_t start, end;
00784
00785 if (sscanf(p, "%*[^,],%d:%d:%d%*c%d,%d:%d:%d%*c%d",
00786 &sh, &sm, &ss, &sc, &eh, &em, &es, &ec) != 8)
00787 return 0;
00788 start = 3600000*sh + 60000*sm + 1000*ss + 10*sc;
00789 end = 3600000*eh + 60000*em + 1000*es + 10*ec;
00790 return end - start;
00791 }
00792
00793 static int mkv_write_ass_blocks(AVFormatContext *s, ByteIOContext *pb, AVPacket *pkt)
00794 {
00795 MatroskaMuxContext *mkv = s->priv_data;
00796 int i, layer = 0, max_duration = 0, size, line_size, data_size = pkt->size;
00797 uint8_t *start, *end, *data = pkt->data;
00798 ebml_master blockgroup;
00799 char buffer[2048];
00800
00801 while (data_size) {
00802 int duration = ass_get_duration(data);
00803 max_duration = FFMAX(duration, max_duration);
00804 end = memchr(data, '\n', data_size);
00805 size = line_size = end ? end-data+1 : data_size;
00806 size -= end ? (end[-1]=='\r')+1 : 0;
00807 start = data;
00808 for (i=0; i<3; i++, start++)
00809 if (!(start = memchr(start, ',', size-(start-data))))
00810 return max_duration;
00811 size -= start - data;
00812 sscanf(data, "Dialogue: %d,", &layer);
00813 i = snprintf(buffer, sizeof(buffer), "%"PRId64",%d,",
00814 s->streams[pkt->stream_index]->nb_frames++, layer);
00815 size = FFMIN(i+size, sizeof(buffer));
00816 memcpy(buffer+i, start, size-i);
00817
00818 av_log(s, AV_LOG_DEBUG, "Writing block at offset %" PRIu64 ", size %d, "
00819 "pts %" PRId64 ", duration %d\n",
00820 url_ftell(pb), size, pkt->pts, duration);
00821 blockgroup = start_ebml_master(pb, MATROSKA_ID_BLOCKGROUP, mkv_blockgroup_size(size));
00822 put_ebml_id(pb, MATROSKA_ID_BLOCK);
00823 put_ebml_num(pb, size+4, 0);
00824 put_byte(pb, 0x80 | (pkt->stream_index + 1));
00825 put_be16(pb, pkt->pts - mkv->cluster_pts);
00826 put_byte(pb, 0);
00827 put_buffer(pb, buffer, size);
00828 put_ebml_uint(pb, MATROSKA_ID_BLOCKDURATION, duration);
00829 end_ebml_master(pb, blockgroup);
00830
00831 data += line_size;
00832 data_size -= line_size;
00833 }
00834
00835 return max_duration;
00836 }
00837
00838 static void mkv_write_block(AVFormatContext *s, ByteIOContext *pb,
00839 unsigned int blockid, AVPacket *pkt, int flags)
00840 {
00841 MatroskaMuxContext *mkv = s->priv_data;
00842 AVCodecContext *codec = s->streams[pkt->stream_index]->codec;
00843 uint8_t *data = NULL;
00844 int size = pkt->size;
00845 int64_t ts = mkv->tracks[pkt->stream_index].write_dts ? pkt->dts : pkt->pts;
00846
00847 av_log(s, AV_LOG_DEBUG, "Writing block at offset %" PRIu64 ", size %d, "
00848 "pts %" PRId64 ", dts %" PRId64 ", duration %d, flags %d\n",
00849 url_ftell(pb), pkt->size, pkt->pts, pkt->dts, pkt->duration, flags);
00850 if (codec->codec_id == CODEC_ID_H264 && codec->extradata_size > 0 &&
00851 (AV_RB24(codec->extradata) == 1 || AV_RB32(codec->extradata) == 1))
00852 ff_avc_parse_nal_units_buf(pkt->data, &data, &size);
00853 else
00854 data = pkt->data;
00855 put_ebml_id(pb, blockid);
00856 put_ebml_num(pb, size+4, 0);
00857 put_byte(pb, 0x80 | (pkt->stream_index + 1));
00858 put_be16(pb, ts - mkv->cluster_pts);
00859 put_byte(pb, flags);
00860 put_buffer(pb, data, size);
00861 if (data != pkt->data)
00862 av_free(data);
00863 }
00864
00865 static void mkv_flush_dynbuf(AVFormatContext *s)
00866 {
00867 MatroskaMuxContext *mkv = s->priv_data;
00868 int bufsize;
00869 uint8_t *dyn_buf;
00870
00871 if (!mkv->dyn_bc)
00872 return;
00873
00874 bufsize = url_close_dyn_buf(mkv->dyn_bc, &dyn_buf);
00875 put_buffer(s->pb, dyn_buf, bufsize);
00876 av_free(dyn_buf);
00877 mkv->dyn_bc = NULL;
00878 }
00879
00880 static int mkv_write_packet(AVFormatContext *s, AVPacket *pkt)
00881 {
00882 MatroskaMuxContext *mkv = s->priv_data;
00883 ByteIOContext *pb = s->pb;
00884 AVCodecContext *codec = s->streams[pkt->stream_index]->codec;
00885 int keyframe = !!(pkt->flags & AV_PKT_FLAG_KEY);
00886 int duration = pkt->duration;
00887 int ret;
00888 int64_t ts = mkv->tracks[pkt->stream_index].write_dts ? pkt->dts : pkt->pts;
00889
00890 if (ts == AV_NOPTS_VALUE) {
00891 av_log(s, AV_LOG_ERROR, "Can't write packet with unknown timestamp\n");
00892 return AVERROR(EINVAL);
00893 }
00894
00895 if (url_is_streamed(s->pb)) {
00896 if (!mkv->dyn_bc)
00897 url_open_dyn_buf(&mkv->dyn_bc);
00898 pb = mkv->dyn_bc;
00899 }
00900
00901 if (!mkv->cluster_pos) {
00902 ret = mkv_add_seekhead_entry(mkv->cluster_seekhead, MATROSKA_ID_CLUSTER, url_ftell(pb));
00903 if (ret < 0) return ret;
00904
00905 mkv->cluster_pos = url_ftell(s->pb);
00906 mkv->cluster = start_ebml_master(pb, MATROSKA_ID_CLUSTER, 0);
00907 put_ebml_uint(pb, MATROSKA_ID_CLUSTERTIMECODE, FFMAX(0, ts));
00908 mkv->cluster_pts = FFMAX(0, ts);
00909 av_md5_update(mkv->md5_ctx, pkt->data, FFMIN(200, pkt->size));
00910 }
00911
00912 if (codec->codec_type != AVMEDIA_TYPE_SUBTITLE) {
00913 mkv_write_block(s, pb, MATROSKA_ID_SIMPLEBLOCK, pkt, keyframe << 7);
00914 } else if (codec->codec_id == CODEC_ID_SSA) {
00915 duration = mkv_write_ass_blocks(s, pb, pkt);
00916 } else {
00917 ebml_master blockgroup = start_ebml_master(pb, MATROSKA_ID_BLOCKGROUP, mkv_blockgroup_size(pkt->size));
00918 duration = pkt->convergence_duration;
00919 mkv_write_block(s, pb, MATROSKA_ID_BLOCK, pkt, 0);
00920 put_ebml_uint(pb, MATROSKA_ID_BLOCKDURATION, duration);
00921 end_ebml_master(pb, blockgroup);
00922 }
00923
00924 if (codec->codec_type == AVMEDIA_TYPE_VIDEO && keyframe) {
00925 ret = mkv_add_cuepoint(mkv->cues, pkt->stream_index, ts, mkv->cluster_pos);
00926 if (ret < 0) return ret;
00927 }
00928
00929
00930 if ((url_is_streamed(s->pb) && (url_ftell(pb) > 32*1024 || ts > mkv->cluster_pts + 1000))
00931 || url_ftell(pb) > mkv->cluster_pos + 5*1024*1024 || ts > mkv->cluster_pts + 5000) {
00932 av_log(s, AV_LOG_DEBUG, "Starting new cluster at offset %" PRIu64
00933 " bytes, pts %" PRIu64 "\n", url_ftell(pb), ts);
00934 end_ebml_master(pb, mkv->cluster);
00935 mkv->cluster_pos = 0;
00936 if (mkv->dyn_bc)
00937 mkv_flush_dynbuf(s);
00938 }
00939
00940 mkv->duration = FFMAX(mkv->duration, ts + duration);
00941 return 0;
00942 }
00943
00944 static int mkv_write_trailer(AVFormatContext *s)
00945 {
00946 MatroskaMuxContext *mkv = s->priv_data;
00947 ByteIOContext *pb = s->pb;
00948 int64_t currentpos, second_seekhead, cuespos;
00949 int ret;
00950
00951 if (mkv->dyn_bc) {
00952 end_ebml_master(mkv->dyn_bc, mkv->cluster);
00953 mkv_flush_dynbuf(s);
00954 } else if (mkv->cluster_pos) {
00955 end_ebml_master(pb, mkv->cluster);
00956 }
00957
00958 if (!url_is_streamed(pb)) {
00959 cuespos = mkv_write_cues(pb, mkv->cues, s->nb_streams);
00960 second_seekhead = mkv_write_seekhead(pb, mkv->cluster_seekhead);
00961
00962 ret = mkv_add_seekhead_entry(mkv->main_seekhead, MATROSKA_ID_CUES , cuespos);
00963 if (ret < 0) return ret;
00964 if (second_seekhead >= 0) {
00965 ret = mkv_add_seekhead_entry(mkv->main_seekhead, MATROSKA_ID_SEEKHEAD, second_seekhead);
00966 if (ret < 0) return ret;
00967 }
00968 mkv_write_seekhead(pb, mkv->main_seekhead);
00969
00970
00971 av_log(s, AV_LOG_DEBUG, "end duration = %" PRIu64 "\n", mkv->duration);
00972 currentpos = url_ftell(pb);
00973 url_fseek(pb, mkv->duration_offset, SEEK_SET);
00974 put_ebml_float(pb, MATROSKA_ID_DURATION, mkv->duration);
00975
00976
00977 if (!(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT)) {
00978 uint8_t segment_uid[16];
00979 av_md5_final(mkv->md5_ctx, segment_uid);
00980 url_fseek(pb, mkv->segment_uid, SEEK_SET);
00981 put_ebml_binary(pb, MATROSKA_ID_SEGMENTUID, segment_uid, 16);
00982 }
00983 url_fseek(pb, currentpos, SEEK_SET);
00984 }
00985
00986 end_ebml_master(pb, mkv->segment);
00987 av_free(mkv->md5_ctx);
00988 av_free(mkv->tracks);
00989 put_flush_packet(pb);
00990 return 0;
00991 }
00992
00993 #if CONFIG_MATROSKA_MUXER
00994 AVOutputFormat matroska_muxer = {
00995 "matroska",
00996 NULL_IF_CONFIG_SMALL("Matroska file format"),
00997 "video/x-matroska",
00998 "mkv",
00999 sizeof(MatroskaMuxContext),
01000 CODEC_ID_MP2,
01001 CODEC_ID_MPEG4,
01002 mkv_write_header,
01003 mkv_write_packet,
01004 mkv_write_trailer,
01005 .flags = AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS,
01006 .codec_tag = (const AVCodecTag* const []){ff_codec_bmp_tags, ff_codec_wav_tags, 0},
01007 .subtitle_codec = CODEC_ID_TEXT,
01008 };
01009 #endif
01010
01011 #if CONFIG_WEBM_MUXER
01012 AVOutputFormat webm_muxer = {
01013 "webm",
01014 NULL_IF_CONFIG_SMALL("WebM file format"),
01015 "video/webm",
01016 "webm",
01017 sizeof(MatroskaMuxContext),
01018 CODEC_ID_VORBIS,
01019 CODEC_ID_VP8,
01020 mkv_write_header,
01021 mkv_write_packet,
01022 mkv_write_trailer,
01023 .flags = AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS,
01024 };
01025 #endif
01026
01027 #if CONFIG_MATROSKA_AUDIO_MUXER
01028 AVOutputFormat matroska_audio_muxer = {
01029 "matroska",
01030 NULL_IF_CONFIG_SMALL("Matroska file format"),
01031 "audio/x-matroska",
01032 "mka",
01033 sizeof(MatroskaMuxContext),
01034 CODEC_ID_MP2,
01035 CODEC_ID_NONE,
01036 mkv_write_header,
01037 mkv_write_packet,
01038 mkv_write_trailer,
01039 .flags = AVFMT_GLOBALHEADER,
01040 .codec_tag = (const AVCodecTag* const []){ff_codec_wav_tags, 0},
01041 };
01042 #endif