Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
To: ffmpeg-devel@ffmpeg.org
Cc: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Subject: [FFmpeg-devel] [PATCH 06/25] avformat/matroskaenc: Avoid seeks when writing EBML header
Date: Mon, 17 Jan 2022 00:03:46 +0100
Message-ID: <AM7PR03MB6660E51B694E17057D0DF6658F569@AM7PR03MB6660.eurprd03.prod.outlook.com> (raw)
In-Reply-To: <AM7PR03MB66609FAEE5128E3BA3F57C0F8F569@AM7PR03MB6660.eurprd03.prod.outlook.com>

Using start/end_ebml_master() to write an EBML Master element
uses seeks under the hood. This does not work if the output is
unseekable with the AVIOContext's buffer being very small
(the size of the currently written Matroska EBML header is 40)
or with the AVIOContext being in direct mode, because then
this seek can't be performed in the AVIOContext's buffer.
So using an approach that does not rely on seeking at all
is preferable; this is achieved by switching to EbmlWriter.

Also factor writing the EBML header out into a function of its own.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavformat/matroskaenc.c | 39 ++++++++++++++++++++++++---------------
 1 file changed, 24 insertions(+), 15 deletions(-)

diff --git a/libavformat/matroskaenc.c b/libavformat/matroskaenc.c
index 0927e0b873..4d4d270db1 100644
--- a/libavformat/matroskaenc.c
+++ b/libavformat/matroskaenc.c
@@ -214,10 +214,6 @@ typedef struct MatroskaMuxContext {
     uint32_t            segment_uid[4];
 } MatroskaMuxContext;
 
-/** 2 bytes * 7 for EBML IDs, 7 1-byte EBML lengths, 6 1-byte uint,
- * 8 byte for "matroska" doctype string */
-#define MAX_EBML_HEADER_SIZE 35
-
 /** 2 bytes * 3 for EBML IDs, 3 1-byte EBML lengths, 8 bytes for 64 bit
  * offset, 4 bytes for target EBML ID */
 #define MAX_SEEKENTRY_SIZE 21
@@ -463,6 +459,13 @@ static void ebml_writer_add_uid(EbmlWriter *writer, uint32_t id,
     elem->priv.uint = val;
 }
 
+static void ebml_writer_add_uint(EbmlWriter *writer, uint32_t id,
+                                 uint64_t val)
+{
+    EbmlElement *elem = ebml_writer_add(writer, id, EBML_UINT);
+    elem->priv.uint = val;
+}
+
 static int ebml_writer_str_len(EbmlElement *elem)
 {
     size_t len = strlen(elem->priv.str);
@@ -2113,11 +2116,26 @@ static int64_t get_metadata_duration(AVFormatContext *s)
     return max;
 }
 
+static void ebml_write_header(AVIOContext *pb,
+                              const char *doctype, int version)
+{
+    EBML_WRITER(8);
+    ebml_writer_open_master(&writer, EBML_ID_HEADER);
+    ebml_writer_add_uint  (&writer, EBML_ID_EBMLVERSION,              1);
+    ebml_writer_add_uint  (&writer, EBML_ID_EBMLREADVERSION,          1);
+    ebml_writer_add_uint  (&writer, EBML_ID_EBMLMAXIDLENGTH,          4);
+    ebml_writer_add_uint  (&writer, EBML_ID_EBMLMAXSIZELENGTH,        8);
+    ebml_writer_add_string(&writer, EBML_ID_DOCTYPE,            doctype);
+    ebml_writer_add_uint  (&writer, EBML_ID_DOCTYPEVERSION,     version);
+    ebml_writer_add_uint  (&writer, EBML_ID_DOCTYPEREADVERSION,       2);
+    /* The size is bounded, so no need to check this. */
+    ebml_writer_write(&writer, pb);
+}
+
 static int mkv_write_header(AVFormatContext *s)
 {
     MatroskaMuxContext *mkv = s->priv_data;
     AVIOContext *pb = s->pb;
-    ebml_master ebml_header;
     const AVDictionaryEntry *tag;
     int ret, i, version = 2;
     int64_t creation_time;
@@ -2134,16 +2152,7 @@ static int mkv_write_header(AVFormatContext *s)
             version = 4;
     }
 
-    ebml_header = start_ebml_master(pb, EBML_ID_HEADER, MAX_EBML_HEADER_SIZE);
-    put_ebml_uint  (pb, EBML_ID_EBMLVERSION       ,           1);
-    put_ebml_uint  (pb, EBML_ID_EBMLREADVERSION   ,           1);
-    put_ebml_uint  (pb, EBML_ID_EBMLMAXIDLENGTH   ,           4);
-    put_ebml_uint  (pb, EBML_ID_EBMLMAXSIZELENGTH ,           8);
-    put_ebml_string(pb, EBML_ID_DOCTYPE           , s->oformat->name);
-    put_ebml_uint  (pb, EBML_ID_DOCTYPEVERSION    ,     version);
-    put_ebml_uint  (pb, EBML_ID_DOCTYPEREADVERSION,           2);
-    end_ebml_master(pb, ebml_header);
-
+    ebml_write_header(pb, s->oformat->name, version);
     put_ebml_id(pb, MATROSKA_ID_SEGMENT);
     put_ebml_size_unknown(pb, 8);
     mkv->segment_offset = avio_tell(pb);
-- 
2.32.0

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

  parent reply	other threads:[~2022-01-16 23:05 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-16 22:49 [FFmpeg-devel] [PATCH 01/25] avformat/matroskaenc: Fix potential overflow Andreas Rheinhardt
2022-01-16 22:51 ` James Almer
2022-01-16 23:05   ` Andreas Rheinhardt
2022-01-16 23:03 ` [FFmpeg-devel] [PATCH 02/25] avformat/matroskaenc: Don't open BlockGroup twice Andreas Rheinhardt
2022-01-16 23:03 ` [FFmpeg-devel] [PATCH 03/25] avformat/matroskaenc: Add API to write Masters with minimal length field Andreas Rheinhardt
2022-01-16 23:03 ` [FFmpeg-devel] [PATCH 04/25] avformat/matroskaenc: Don't waste bytes on SimpleTags length fields Andreas Rheinhardt
2022-01-16 23:03 ` [FFmpeg-devel] [PATCH 05/25] avformat/matroskaenc: Don't waste bytes when writing attachments Andreas Rheinhardt
2022-01-16 23:03 ` Andreas Rheinhardt [this message]
2022-01-16 23:03 ` [FFmpeg-devel] [PATCH 07/25] avformat/matroskaenc: Factor writing TrackVideo out Andreas Rheinhardt
2022-01-16 23:03 ` [FFmpeg-devel] [PATCH 08/25] avformat/matroskaenc: Don't waste bytes on Video element length fields Andreas Rheinhardt
2022-01-16 23:03 ` [FFmpeg-devel] [PATCH 09/25] avformat/matroskaenc: Don't waste bytes on ChapterAtoms " Andreas Rheinhardt
2022-01-16 23:03 ` [FFmpeg-devel] [PATCH 10/25] avformat/matroskaenc: Factor writing Info out Andreas Rheinhardt
2022-01-16 23:03 ` [FFmpeg-devel] [PATCH 11/25] avformat/matroskaenc: Allow to use custom reformatting functions Andreas Rheinhardt
2022-01-16 23:03 ` [FFmpeg-devel] [PATCH 12/25] avformat/matroskaenc: Speed up reformatting WavPack Andreas Rheinhardt
2022-01-16 23:03 ` [FFmpeg-devel] [PATCH 13/25] avformat/av1: Document actual behaviour of ff_av1_filter_obus() Andreas Rheinhardt
2022-01-16 23:03 ` [FFmpeg-devel] [PATCH 14/25] avformat/matroskaenc: Redo reformatting AV1 Andreas Rheinhardt
2022-01-16 23:03 ` [FFmpeg-devel] [PATCH 15/25] avformat/matroskaenc: Use common function for H.2645 annex B->mp4 Andreas Rheinhardt
2022-01-16 23:03 ` [FFmpeg-devel] [PATCH 16/25] avformat/avc: Add functions to split access unit into list of NALUs Andreas Rheinhardt
2022-01-16 23:03 ` [FFmpeg-devel] [PATCH 17/25] avformat/matroskaenc: Avoid temporary buffers when reformatting H.2645 Andreas Rheinhardt
2022-01-16 23:03 ` [FFmpeg-devel] [PATCH 18/25] avformat/matroskaenc: Remove special code for writing subtitles Andreas Rheinhardt
2022-01-16 23:03 ` [FFmpeg-devel] [PATCH 19/25] avformat/matroskaenc: Pass more parameters explicitly to mkv_write_block Andreas Rheinhardt
2022-01-16 23:04 ` [FFmpeg-devel] [PATCH 20/25] avformat/matroskaenc: Redo applying ProRes offset Andreas Rheinhardt
2022-01-16 23:04 ` [FFmpeg-devel] [PATCH 21/25] avformat/matroskaenc: Don't waste bytes on BlockGroup length fields Andreas Rheinhardt
2022-01-16 23:04 ` [FFmpeg-devel] [PATCH 22/25] avformat/matroskaenc: Remove duplicated code for writing WebVTT subs Andreas Rheinhardt
2022-01-16 23:04 ` [FFmpeg-devel] [PATCH 23/25] avformat/matroskaenc: Reindentation Andreas Rheinhardt
2022-01-16 23:04 ` [FFmpeg-devel] [PATCH 24/25] avformat/matroskaenc: Avoid repeated avio_tell() Andreas Rheinhardt
2022-01-16 23:04 ` [FFmpeg-devel] [PATCH 25/25] avformat/matroskaenc: Write data directly into dynamic buffers Andreas Rheinhardt
2022-01-18 11:17 ` [FFmpeg-devel] [PATCH 01/25] avformat/matroskaenc: Fix potential overflow Andreas Rheinhardt
2022-01-18 23:32 ` [FFmpeg-devel] [PATCH 26/31] avformat/mux: Remove assert based on faulty assumptions Andreas Rheinhardt
2022-01-18 23:32 ` [FFmpeg-devel] [PATCH 27/31] fate/matroska: Add test for avoiding negative timestamps Andreas Rheinhardt
2022-01-19  0:33   ` Andreas Rheinhardt
2022-01-18 23:32 ` [FFmpeg-devel] [PATCH 28/31] avformat/avformat: Add AVFMT_AVOID_NEG_TS_DISABLED Andreas Rheinhardt
2022-01-18 23:32 ` [FFmpeg-devel] [PATCH 29/31] avformat/mux: Preserve sync even if later packet has negative ts Andreas Rheinhardt
2022-01-18 23:32 ` [FFmpeg-devel] [PATCH 30/31] avformat/mux: Peek into the muxing queue for avoid_negative_ts Andreas Rheinhardt
2022-01-18 23:32 ` [FFmpeg-devel] [PATCH 31/31] avformat/hls: Remove redundant cast Andreas Rheinhardt

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=AM7PR03MB6660E51B694E17057D0DF6658F569@AM7PR03MB6660.eurprd03.prod.outlook.com \
    --to=andreas.rheinhardt@outlook.com \
    --cc=ffmpeg-devel@ffmpeg.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git