From patchwork Fri Apr 19 23:41:10 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 12839 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id A60C64494CF for ; Sat, 20 Apr 2019 02:50:55 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 900E168AB6B; Sat, 20 Apr 2019 02:50:55 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f66.google.com (mail-wm1-f66.google.com [209.85.128.66]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 6F4AD68AB60 for ; Sat, 20 Apr 2019 02:50:52 +0300 (EEST) Received: by mail-wm1-f66.google.com with SMTP id z6so9823496wmi.0 for ; Fri, 19 Apr 2019 16:50:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=MhTt89T4a2ZUQfschaZJ9v6gLUZCzl5TExxJYKvpV1E=; b=LG4lgVfQa0CBiZ9W47/I0zCr0zPjXUfhnw/cMSK1y4iGQ2BB2BC6Ji1uobzxIhCSTG DL7gPP40/5VeggF8qXFI9RB+nT1r7Pq5l820m0q00G3XoSa9l++F4o9qnoXsSNvDSKG4 JxnyjhGmF7dRgB4SkTPVIIHhIMYkr06u6uPLr4pa4ByAVH/jbpOFhUbKsLlrs5sEFvjC cXmJatBcNk2thXqEQqR5HYw1mRNxVYCly7+TsX0MkzQm/gEUrXxrBpaI6j7zbQifPg2l igOB5kWk4sPCe7YNYwXg0k31DuuZM2GBb9ufFuBUSyTsoJQ+4xa714f4lw8HM83qN5md 3cvA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=MhTt89T4a2ZUQfschaZJ9v6gLUZCzl5TExxJYKvpV1E=; b=JwYhlOY1T4un72RXPhf5Cak43zq/KidPiHMI5A0BUD/+I7sSnLJg3wFoJAeCQhq9Qt GRMJssh5Dx1sf4JezkqEjY9O3bbFgayT/l6i8ioGliaY6LzQOPUdo2zJcgtsZIvTAEom YJoTGG06LBlu47Yrn9UV+9xe7zllnzw+D5JZA093rHTwPyYWQJAVgNSnkgIj2TURB97f puKTZKqDrf/rO/ToRls4n00TGtGfYBJarXcgaznPmbM+oMn1M6ZQalm1zhFsbKYnAI4A ndfySbCVZBMdwilPRE9f8dy54s20I5yk6qWyVdK3snBrCWEF6R7qbaQq7Bqf6L8PbH0T t2+A== X-Gm-Message-State: APjAAAVJ977UjSt1leHYSqOurH8kiqKsjfFIIolsULZsvzQyQwPMbOJv +3lSalA+Dz+oyCKeM/SgwZwflyhT X-Google-Smtp-Source: APXvYqyTIFsVpqR/C1UReydqNuz4SxZDT8I55hCbaazfz2l0hWoRLqwvPHI/0WJrtlszZVAZnUyo+A== X-Received: by 2002:a7b:cf2c:: with SMTP id m12mr4019988wmg.21.1555717851511; Fri, 19 Apr 2019 16:50:51 -0700 (PDT) Received: from localhost.localdomain (ipbcc08c44.dynamic.kabel-deutschland.de. [188.192.140.68]) by smtp.gmail.com with ESMTPSA id t14sm6002395wmi.16.2019.04.19.16.50.50 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 19 Apr 2019 16:50:50 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Sat, 20 Apr 2019 01:41:10 +0200 Message-Id: <20190419234110.32980-15-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190419234110.32980-1-andreas.rheinhardt@gmail.com> References: <20190402133305.3328-1-andreas.rheinhardt@googlemail.com> <20190419234110.32980-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 15/15] avformat/matroskaenc: Reduce usage of ebml_master X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Andreas Rheinhardt Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" After the last few commits, the functions for writing master elements with CRC-32 elements didn't really make use of the ebml_master structure any more, so remove these parameters from the functions. The only things that still need to be kept are the positions of the level 1 elements that are written preliminarily and updated later. These positions are stored in the MatroskaMuxContext and replace the corresponding ebml_master structures. Signed-off-by: Andreas Rheinhardt --- libavformat/matroskaenc.c | 103 ++++++++++++++++++-------------------- 1 file changed, 49 insertions(+), 54 deletions(-) diff --git a/libavformat/matroskaenc.c b/libavformat/matroskaenc.c index fc0e95440e..537fcd8eac 100644 --- a/libavformat/matroskaenc.c +++ b/libavformat/matroskaenc.c @@ -58,8 +58,7 @@ #include "libavcodec/internal.h" typedef struct ebml_master { - int64_t pos; ///< absolute offset in the containing AVIOContext where the size field starts - ///< for level 1 elements or else where the master's elements start + int64_t pos; ///< absolute offset in the containing AVIOContext where the master's elements start int sizebytes; ///< how many bytes were reserved for the size } ebml_master; @@ -122,15 +121,14 @@ typedef struct MatroskaMuxContext { const AVClass *class; int mode; AVIOContext *tags_bc; - ebml_master tags; + int64_t tags_pos; AVIOContext *info_bc; - ebml_master info; + int64_t info_pos; AVIOContext *tracks_bc; - ebml_master tracks_master; + int64_t tracks_pos; ebml_master segment; int64_t segment_offset; AVIOContext *cluster_bc; - ebml_master cluster; int64_t cluster_pos; ///< file offset of the current cluster int64_t cluster_pts; int64_t duration_offset; @@ -334,7 +332,7 @@ static void end_ebml_master(AVIOContext *pb, ebml_master master) } static int start_ebml_master_crc32(AVIOContext *pb, AVIOContext **dyn_cp, MatroskaMuxContext *mkv, - ebml_master *master, uint32_t elementid) + uint32_t elementid) { int ret; @@ -342,15 +340,13 @@ static int start_ebml_master_crc32(AVIOContext *pb, AVIOContext **dyn_cp, Matros return ret; put_ebml_id(pb, elementid); - *master = (ebml_master) { avio_tell(pb), 0 }; if (mkv->write_crc) put_ebml_void(*dyn_cp, 6); /* Reserve space for CRC32 so position/size calculations using avio_tell() take it into account */ return 0; } -static void end_ebml_master_crc32(AVIOContext *pb, AVIOContext **dyn_cp, MatroskaMuxContext *mkv, - ebml_master master) +static void end_ebml_master_crc32(AVIOContext *pb, AVIOContext **dyn_cp, MatroskaMuxContext *mkv) { uint8_t *buf, crc[4]; int size, skip = 0; @@ -372,11 +368,13 @@ static void end_ebml_master_crc32(AVIOContext *pb, AVIOContext **dyn_cp, Matrosk * Complete ebml master without destroying the buffer, allowing for later updates */ static void end_ebml_master_crc32_preliminary(AVIOContext *pb, AVIOContext **dyn_cp, MatroskaMuxContext *mkv, - ebml_master master) + int64_t *pos) { uint8_t *buf; int size = avio_get_dyn_buf(*dyn_cp, &buf); + *pos = avio_tell(pb); + put_ebml_num(pb, size, 0); avio_write(pb, buf, size); } @@ -489,7 +487,7 @@ static int64_t mkv_write_seekhead(AVIOContext *pb, MatroskaMuxContext *mkv) { AVIOContext *dyn_cp; mkv_seekhead *seekhead = mkv->seekhead; - ebml_master metaseek, seekentry; + ebml_master seekentry; int64_t currentpos; int i; @@ -502,7 +500,7 @@ static int64_t mkv_write_seekhead(AVIOContext *pb, MatroskaMuxContext *mkv) } } - if (start_ebml_master_crc32(pb, &dyn_cp, mkv, &metaseek, MATROSKA_ID_SEEKHEAD) < 0) { + if (start_ebml_master_crc32(pb, &dyn_cp, mkv, MATROSKA_ID_SEEKHEAD) < 0) { currentpos = -1; goto fail; } @@ -519,7 +517,7 @@ static int64_t mkv_write_seekhead(AVIOContext *pb, MatroskaMuxContext *mkv) put_ebml_uint(dyn_cp, MATROSKA_ID_SEEKPOSITION, entry->segmentpos); end_ebml_master(dyn_cp, seekentry); } - end_ebml_master_crc32(pb, &dyn_cp, mkv, metaseek); + end_ebml_master_crc32(pb, &dyn_cp, mkv); if (seekhead->reserved_size > 0) { uint64_t remaining = seekhead->filepos + seekhead->reserved_size - avio_tell(pb); @@ -572,12 +570,11 @@ static int64_t mkv_write_cues(AVFormatContext *s, mkv_cues *cues, mkv_track *tra { MatroskaMuxContext *mkv = s->priv_data; AVIOContext *dyn_cp, *pb = s->pb; - ebml_master cues_element; int64_t currentpos; int i, j, ret; currentpos = avio_tell(pb); - ret = start_ebml_master_crc32(pb, &dyn_cp, mkv, &cues_element, MATROSKA_ID_CUES); + ret = start_ebml_master_crc32(pb, &dyn_cp, mkv, MATROSKA_ID_CUES); if (ret < 0) return ret; @@ -623,7 +620,7 @@ static int64_t mkv_write_cues(AVFormatContext *s, mkv_cues *cues, mkv_track *tra i += j - 1; end_ebml_master(dyn_cp, cuepoint); } - end_ebml_master_crc32(pb, &dyn_cp, mkv, cues_element); + end_ebml_master_crc32(pb, &dyn_cp, mkv); return currentpos; } @@ -1453,7 +1450,7 @@ static int mkv_write_tracks(AVFormatContext *s) if (ret < 0) return ret; - ret = start_ebml_master_crc32(pb, &mkv->tracks_bc, mkv, &mkv->tracks_master, MATROSKA_ID_TRACKS); + ret = start_ebml_master_crc32(pb, &mkv->tracks_bc, mkv, MATROSKA_ID_TRACKS); if (ret < 0) return ret; @@ -1468,9 +1465,9 @@ static int mkv_write_tracks(AVFormatContext *s) } if ((pb->seekable & AVIO_SEEKABLE_NORMAL) && !mkv->is_live) - end_ebml_master_crc32_preliminary(pb, &mkv->tracks_bc, mkv, mkv->tracks_master); + end_ebml_master_crc32_preliminary(pb, &mkv->tracks_bc, mkv, &mkv->tracks_pos); else - end_ebml_master_crc32(pb, &mkv->tracks_bc, mkv, mkv->tracks_master); + end_ebml_master_crc32(pb, &mkv->tracks_bc, mkv); return 0; } @@ -1479,7 +1476,7 @@ static int mkv_write_chapters(AVFormatContext *s) { MatroskaMuxContext *mkv = s->priv_data; AVIOContext *dyn_cp, *pb = s->pb; - ebml_master chapters, editionentry; + ebml_master editionentry; AVRational scale = {1, 1E9}; int i, ret; @@ -1489,7 +1486,7 @@ static int mkv_write_chapters(AVFormatContext *s) ret = mkv_add_seekhead_entry(mkv->seekhead, MATROSKA_ID_CHAPTERS, avio_tell(pb)); if (ret < 0) return ret; - ret = start_ebml_master_crc32(pb, &dyn_cp, mkv, &chapters, MATROSKA_ID_CHAPTERS); + ret = start_ebml_master_crc32(pb, &dyn_cp, mkv, MATROSKA_ID_CHAPTERS); if (ret < 0) return ret; editionentry = start_ebml_master(dyn_cp, MATROSKA_ID_EDITIONENTRY, 0); @@ -1527,7 +1524,7 @@ static int mkv_write_chapters(AVFormatContext *s) end_ebml_master(dyn_cp, chapteratom); } end_ebml_master(dyn_cp, editionentry); - end_ebml_master_crc32(pb, &dyn_cp, mkv, chapters); + end_ebml_master_crc32(pb, &dyn_cp, mkv); mkv->wrote_chapters = 1; return 0; @@ -1568,19 +1565,18 @@ static int mkv_write_simpletag(AVIOContext *pb, AVDictionaryEntry *t) } static int mkv_write_tag_targets(AVFormatContext *s, uint32_t elementid, - unsigned int uid, ebml_master *tags, - ebml_master* tag) + unsigned int uid, ebml_master *tag) { AVIOContext *pb; MatroskaMuxContext *mkv = s->priv_data; ebml_master targets; int ret; - if (!tags->pos) { + if (!mkv->tags_bc) { ret = mkv_add_seekhead_entry(mkv->seekhead, MATROSKA_ID_TAGS, avio_tell(s->pb)); if (ret < 0) return ret; - start_ebml_master_crc32(s->pb, &mkv->tags_bc, mkv, tags, MATROSKA_ID_TAGS); + start_ebml_master_crc32(s->pb, &mkv->tags_bc, mkv, MATROSKA_ID_TAGS); } pb = mkv->tags_bc; @@ -1607,14 +1603,14 @@ static int mkv_check_tag_name(const char *name, uint32_t elementid) } static int mkv_write_tag(AVFormatContext *s, AVDictionary *m, uint32_t elementid, - unsigned int uid, ebml_master *tags) + unsigned int uid) { MatroskaMuxContext *mkv = s->priv_data; ebml_master tag; int ret; AVDictionaryEntry *t = NULL; - ret = mkv_write_tag_targets(s, elementid, uid, tags, &tag); + ret = mkv_write_tag_targets(s, elementid, uid, &tag); if (ret < 0) return ret; @@ -1649,7 +1645,7 @@ static int mkv_write_tags(AVFormatContext *s) ff_metadata_conv_ctx(s, ff_mkv_metadata_conv, NULL); if (mkv_check_tag(s->metadata, 0)) { - ret = mkv_write_tag(s, s->metadata, 0, 0, &mkv->tags); + ret = mkv_write_tag(s, s->metadata, 0, 0); if (ret < 0) return ret; } @@ -1662,7 +1658,7 @@ static int mkv_write_tags(AVFormatContext *s) if (!mkv_check_tag(st->metadata, MATROSKA_ID_TAGTARGETS_TRACKUID)) continue; - ret = mkv_write_tag(s, st->metadata, MATROSKA_ID_TAGTARGETS_TRACKUID, i + 1, &mkv->tags); + ret = mkv_write_tag(s, st->metadata, MATROSKA_ID_TAGTARGETS_TRACKUID, i + 1); if (ret < 0) return ret; } @@ -1676,7 +1672,7 @@ static int mkv_write_tags(AVFormatContext *s) if (st->codecpar->codec_type == AVMEDIA_TYPE_ATTACHMENT) continue; - mkv_write_tag_targets(s, MATROSKA_ID_TAGTARGETS_TRACKUID, i + 1, &mkv->tags, &tag_target); + mkv_write_tag_targets(s, MATROSKA_ID_TAGTARGETS_TRACKUID, i + 1, &tag_target); pb = mkv->tags_bc; tag = start_ebml_master(pb, MATROSKA_ID_SIMPLETAG, 0); @@ -1698,7 +1694,7 @@ static int mkv_write_tags(AVFormatContext *s) if (!mkv_check_tag(ch->metadata, MATROSKA_ID_TAGTARGETS_CHAPTERUID)) continue; - ret = mkv_write_tag(s, ch->metadata, MATROSKA_ID_TAGTARGETS_CHAPTERUID, ch->id + mkv->chapter_id_offset, &mkv->tags); + ret = mkv_write_tag(s, ch->metadata, MATROSKA_ID_TAGTARGETS_CHAPTERUID, ch->id + mkv->chapter_id_offset); if (ret < 0) return ret; } @@ -1712,17 +1708,17 @@ static int mkv_write_tags(AVFormatContext *s) if (!mkv_check_tag(st->metadata, MATROSKA_ID_TAGTARGETS_ATTACHUID)) continue; - ret = mkv_write_tag(s, st->metadata, MATROSKA_ID_TAGTARGETS_ATTACHUID, attachment->fileuid, &mkv->tags); + ret = mkv_write_tag(s, st->metadata, MATROSKA_ID_TAGTARGETS_ATTACHUID, attachment->fileuid); if (ret < 0) return ret; } } - if (mkv->tags.pos) { + if (mkv->tags_bc) { if ((s->pb->seekable & AVIO_SEEKABLE_NORMAL) && !mkv->is_live) - end_ebml_master_crc32_preliminary(s->pb, &mkv->tags_bc, mkv, mkv->tags); + end_ebml_master_crc32_preliminary(s->pb, &mkv->tags_bc, mkv, &mkv->tags_pos); else - end_ebml_master_crc32(s->pb, &mkv->tags_bc, mkv, mkv->tags); + end_ebml_master_crc32(s->pb, &mkv->tags_bc, mkv); } return 0; } @@ -1731,7 +1727,6 @@ static int mkv_write_attachments(AVFormatContext *s) { MatroskaMuxContext *mkv = s->priv_data; AVIOContext *dyn_cp, *pb = s->pb; - ebml_master attachments; AVLFG c; int i, ret; @@ -1747,7 +1742,7 @@ static int mkv_write_attachments(AVFormatContext *s) ret = mkv_add_seekhead_entry(mkv->seekhead, MATROSKA_ID_ATTACHMENTS, avio_tell(pb)); if (ret < 0) return ret; - ret = start_ebml_master_crc32(pb, &dyn_cp, mkv, &attachments, MATROSKA_ID_ATTACHMENTS); + ret = start_ebml_master_crc32(pb, &dyn_cp, mkv, MATROSKA_ID_ATTACHMENTS); if (ret < 0) return ret; for (i = 0; i < s->nb_streams; i++) { @@ -1820,7 +1815,7 @@ static int mkv_write_attachments(AVFormatContext *s) mkv->attachments->entries[mkv->attachments->num_entries].stream_idx = i; mkv->attachments->entries[mkv->attachments->num_entries++].fileuid = fileuid; } - end_ebml_master_crc32(pb, &dyn_cp, mkv, attachments); + end_ebml_master_crc32(pb, &dyn_cp, mkv); return 0; } @@ -1906,7 +1901,7 @@ static int mkv_write_header(AVFormatContext *s) ret = mkv_add_seekhead_entry(mkv->seekhead, MATROSKA_ID_INFO, avio_tell(pb)); if (ret < 0) goto fail; - ret = start_ebml_master_crc32(pb, &mkv->info_bc, mkv, &mkv->info, MATROSKA_ID_INFO); + ret = start_ebml_master_crc32(pb, &mkv->info_bc, mkv, MATROSKA_ID_INFO); if (ret < 0) return ret; pb = mkv->info_bc; @@ -1965,9 +1960,9 @@ static int mkv_write_header(AVFormatContext *s) } } if ((s->pb->seekable & AVIO_SEEKABLE_NORMAL) && !mkv->is_live) - end_ebml_master_crc32_preliminary(s->pb, &mkv->info_bc, mkv, mkv->info); + end_ebml_master_crc32_preliminary(s->pb, &mkv->info_bc, mkv, &mkv->info_pos); else - end_ebml_master_crc32(s->pb, &mkv->info_bc, mkv, mkv->info); + end_ebml_master_crc32(s->pb, &mkv->info_bc, mkv); pb = s->pb; // initialize stream_duration fields @@ -2255,7 +2250,7 @@ static void mkv_start_new_cluster(AVFormatContext *s, AVPacket *pkt) { MatroskaMuxContext *mkv = s->priv_data; - end_ebml_master_crc32(s->pb, &mkv->cluster_bc, mkv, mkv->cluster); + end_ebml_master_crc32(s->pb, &mkv->cluster_bc, mkv); mkv->cluster_pos = -1; av_log(s, AV_LOG_DEBUG, "Starting new cluster at offset %" PRIu64 " bytes, " @@ -2400,7 +2395,7 @@ static int mkv_write_packet_internal(AVFormatContext *s, AVPacket *pkt, int add_ if (mkv->cluster_pos == -1) { mkv->cluster_pos = avio_tell(s->pb); - ret = start_ebml_master_crc32(s->pb, &mkv->cluster_bc, mkv, &mkv->cluster, MATROSKA_ID_CLUSTER); + ret = start_ebml_master_crc32(s->pb, &mkv->cluster_bc, mkv, MATROSKA_ID_CLUSTER); if (ret < 0) return ret; put_ebml_uint(mkv->cluster_bc, MATROSKA_ID_CLUSTERTIMECODE, FFMAX(0, ts)); @@ -2536,7 +2531,7 @@ static int mkv_write_flush_packet(AVFormatContext *s, AVPacket *pkt) if (!pkt) { if (mkv->cluster_pos != -1) { - end_ebml_master_crc32(s->pb, &mkv->cluster_bc, mkv, mkv->cluster); + end_ebml_master_crc32(s->pb, &mkv->cluster_bc, mkv); mkv->cluster_pos = -1; av_log(s, AV_LOG_DEBUG, "Flushing cluster at offset %" PRIu64 " bytes\n", @@ -2567,7 +2562,7 @@ static int mkv_write_trailer(AVFormatContext *s) } if (mkv->cluster_bc) { - end_ebml_master_crc32(pb, &mkv->cluster_bc, mkv, mkv->cluster); + end_ebml_master_crc32(pb, &mkv->cluster_bc, mkv); } ret = mkv_write_chapters(s); @@ -2615,12 +2610,12 @@ static int mkv_write_trailer(AVFormatContext *s) currentpos = avio_tell(pb); avio_seek(mkv->info_bc, mkv->duration_offset, SEEK_SET); put_ebml_float(mkv->info_bc, MATROSKA_ID_DURATION, mkv->duration); - avio_seek(pb, mkv->info.pos, SEEK_SET); - end_ebml_master_crc32(pb, &mkv->info_bc, mkv, mkv->info); + avio_seek(pb, mkv->info_pos, SEEK_SET); + end_ebml_master_crc32(pb, &mkv->info_bc, mkv); // write tracks master - avio_seek(pb, mkv->tracks_master.pos, SEEK_SET); - end_ebml_master_crc32(pb, &mkv->tracks_bc, mkv, mkv->tracks_master); + avio_seek(pb, mkv->tracks_pos, SEEK_SET); + end_ebml_master_crc32(pb, &mkv->tracks_bc, mkv); // update stream durations if (!mkv->is_live && mkv->stream_durations) { @@ -2647,9 +2642,9 @@ static int mkv_write_trailer(AVFormatContext *s) } avio_seek(mkv->tags_bc, curr, SEEK_SET); } - if (mkv->tags.pos && !mkv->is_live) { - avio_seek(pb, mkv->tags.pos, SEEK_SET); - end_ebml_master_crc32(pb, &mkv->tags_bc, mkv, mkv->tags); + if (mkv->tags_bc && !mkv->is_live) { + avio_seek(pb, mkv->tags_pos, SEEK_SET); + end_ebml_master_crc32(pb, &mkv->tags_bc, mkv); } avio_seek(pb, currentpos, SEEK_SET);