#include "avc.h"
#include "flacenc.h"
#include "libavutil/intreadwrite.h"
-#include "libavutil/md5.h"
+#include "libavutil/random_seed.h"
+#include "libavutil/lfg.h"
#include "libavcodec/xiph.h"
#include "libavcodec/mpeg4audio.h"
ByteIOContext *dyn_bc;
ebml_master segment;
int64_t segment_offset;
- int64_t segment_uid;
ebml_master cluster;
int64_t cluster_pos; ///< file offset of the current cluster
int64_t cluster_pts;
int64_t duration_offset;
int64_t duration;
mkv_seekhead *main_seekhead;
- mkv_seekhead *cluster_seekhead;
mkv_cues *cues;
mkv_track *tracks;
- struct AVMD5 *md5_ctx;
unsigned int audio_buffer_size;
AVPacket cur_audio_pkt;
} MatroskaMuxContext;
}
static void put_ebml_binary(ByteIOContext *pb, unsigned int elementid,
- const uint8_t *buf, int size)
+ const void *buf, int size)
{
put_ebml_id(pb, elementid);
put_ebml_num(pb, size, 0);
}
/**
- * Writes a void element of a given size. Useful for reserving space in
+ * Write a void element of a given size. Useful for reserving space in
* the file to be written to later.
*
* @param size The number of bytes to reserve, which must be at least 2.
ByteIOContext *pb = s->pb;
ebml_master ebml_header, segment_info;
AVMetadataTag *tag;
- int ret;
+ int ret, i;
if (!strcmp(s->oformat->name, "webm")) mkv->mode = MODE_WEBM;
else mkv->mode = MODE_MATROSKAv2;
- mkv->md5_ctx = av_mallocz(av_md5_size);
- av_md5_init(mkv->md5_ctx);
mkv->tracks = av_mallocz(s->nb_streams * sizeof(*mkv->tracks));
+ if (!mkv->tracks)
+ return AVERROR(ENOMEM);
ebml_header = start_ebml_master(pb, EBML_ID_HEADER, 0);
put_ebml_uint (pb, EBML_ID_EBMLVERSION , 1);
// isn't more than 10 elements if we only write one of each other
// currently defined level 1 element
mkv->main_seekhead = mkv_start_seekhead(pb, mkv->segment_offset, 10);
- mkv->cluster_seekhead = mkv_start_seekhead(pb, mkv->segment_offset, 0);
- if (mkv->main_seekhead == NULL || mkv->cluster_seekhead == NULL)
+ if (!mkv->main_seekhead)
return AVERROR(ENOMEM);
ret = mkv_add_seekhead_entry(mkv->main_seekhead, MATROSKA_ID_INFO, url_ftell(pb));
if ((tag = av_metadata_get(s->metadata, "title", NULL, 0)))
put_ebml_string(pb, MATROSKA_ID_TITLE, tag->value);
if (!(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT)) {
+ uint32_t segment_uid[4];
+ AVLFG lfg;
+
+ av_lfg_init(&lfg, av_get_random_seed());
+
+ for (i = 0; i < 4; i++)
+ segment_uid[i] = av_lfg_get(&lfg);
+
put_ebml_string(pb, MATROSKA_ID_MUXINGAPP , LIBAVFORMAT_IDENT);
put_ebml_string(pb, MATROSKA_ID_WRITINGAPP, LIBAVFORMAT_IDENT);
-
- // reserve space to write the segment UID later
- mkv->segment_uid = url_ftell(pb);
- put_ebml_void(pb, 19);
+ put_ebml_binary(pb, MATROSKA_ID_SEGMENTUID, segment_uid, 16);
}
// reserve space for the duration
}
if (!mkv->cluster_pos) {
- ret = mkv_add_seekhead_entry(mkv->cluster_seekhead, MATROSKA_ID_CLUSTER, url_ftell(pb));
- if (ret < 0) return ret;
-
mkv->cluster_pos = url_ftell(s->pb);
mkv->cluster = start_ebml_master(pb, MATROSKA_ID_CLUSTER, 0);
put_ebml_uint(pb, MATROSKA_ID_CLUSTERTIMECODE, FFMAX(0, ts));
mkv->cluster_pts = FFMAX(0, ts);
- av_md5_update(mkv->md5_ctx, pkt->data, FFMIN(200, pkt->size));
}
if (codec->codec_type != AVMEDIA_TYPE_SUBTITLE) {
{
MatroskaMuxContext *mkv = s->priv_data;
ByteIOContext *pb = s->pb;
- int64_t currentpos, second_seekhead, cuespos;
+ int64_t currentpos, cuespos;
int ret;
// check if we have an audio packet cached
if (!url_is_streamed(pb)) {
cuespos = mkv_write_cues(pb, mkv->cues, s->nb_streams);
- second_seekhead = mkv_write_seekhead(pb, mkv->cluster_seekhead);
ret = mkv_add_seekhead_entry(mkv->main_seekhead, MATROSKA_ID_CUES , cuespos);
if (ret < 0) return ret;
- if (second_seekhead >= 0) {
- ret = mkv_add_seekhead_entry(mkv->main_seekhead, MATROSKA_ID_SEEKHEAD, second_seekhead);
- if (ret < 0) return ret;
- }
mkv_write_seekhead(pb, mkv->main_seekhead);
// update the duration
url_fseek(pb, mkv->duration_offset, SEEK_SET);
put_ebml_float(pb, MATROSKA_ID_DURATION, mkv->duration);
- // write the md5sum of some frames as the segment UID
- if (!(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT)) {
- uint8_t segment_uid[16];
- av_md5_final(mkv->md5_ctx, segment_uid);
- url_fseek(pb, mkv->segment_uid, SEEK_SET);
- put_ebml_binary(pb, MATROSKA_ID_SEGMENTUID, segment_uid, 16);
- }
url_fseek(pb, currentpos, SEEK_SET);
}
end_ebml_master(pb, mkv->segment);
- av_free(mkv->md5_ctx);
av_free(mkv->tracks);
av_destruct_packet(&mkv->cur_audio_pkt);
put_flush_packet(pb);