[FFmpeg-devel] [PATCH 0/4] User controllable padding

James Darnley james.darnley at gmail.com
Mon Dec 30 19:38:52 CET 2013


Patch 1: Adds a new field to AVFormatContext and a new option to control the
         amount of padding added to headers.  The current default value is 0
         and it is limited to between 0 and 1MiB (2^20).

Patch 2: Uses the new option in flacenc.  Previously the muxer wrote a fixed
         8192 bytes of padding.

Patch 4: Uses the new option in id3v2 tags.  There is a comment there saying
         that 10 bytes are written solve compatability problems with other
         software.  Does this mean I should change the default padding value
         from 0?  Should I clip the value in id3 tags to 10?
         Comment quoted below.
> adding an arbitrary amount of padding bytes at the end of the ID3 metadata
> fixes cover art display for some software (iTunes, Traktor, Serato, Torq)

Changes to FATE results need doing.

James Darnley (4):
  AVFormatContext: add metadata_header_padding field
  lavf/flacenc: use metadata_header_padding
  lavf/flacenc: fix comment after previous change
  lavf/id3v2enc: use metadata_header_padding

 libavformat/aiffenc.c       |    2 +-
 libavformat/avformat.h      |    7 +++++++
 libavformat/flacenc.c       |   11 +++++++----
 libavformat/id3v2.h         |    2 +-
 libavformat/id3v2enc.c      |   11 +++++------
 libavformat/mp3enc.c        |    4 ++--
 libavformat/options_table.h |    1 +
 7 files changed, 24 insertions(+), 14 deletions(-)

P.S. I would send these with git send-email but I can't get its
dependencies installed at the moment.
-------------- next part --------------
From cfad09648bde3fd058280323a13f5623b816001f Mon Sep 17 00:00:00 2001
From: James Darnley <james.darnley at gmail.com>
Date: Sun, 29 Dec 2013 22:42:33 +0100
Subject: [PATCH 1/4] AVFormatContext: add metadata_header_padding field

This field is used to store the number of bytes that should be written
as padding to a metadata header of a file.  For example:
 - The FLAC format's METADATA_BLOCK_PADDING [1]
 - The ID3v2 tag format's padding           [2]

[1] http://xiph.org/flac/format.html#metadata_block_padding
[2] http://id3.org/id3v2.3.0#ID3v2_overview
---
 libavformat/avformat.h      |    7 +++++++
 libavformat/options_table.h |    1 +
 2 files changed, 8 insertions(+), 0 deletions(-)

diff --git a/libavformat/avformat.h b/libavformat/avformat.h
index 52eef0d..3632701 100644
--- a/libavformat/avformat.h
+++ b/libavformat/avformat.h
@@ -1344,6 +1344,13 @@ typedef struct AVFormatContext {
      * Demuxing: Set by user via av_format_set_subtitle_codec (NO direct access).
      */
     AVCodec *subtitle_codec;
+
+    /**
+     * Number of bytes to be written as padding in a metadata header.
+     * Demuxing: Unused.
+     * Muxing: set by user.
+     */
+    int metadata_header_padding;
 } AVFormatContext;
 
 int av_format_get_probe_score(const AVFormatContext *s);
diff --git a/libavformat/options_table.h b/libavformat/options_table.h
index 8145325..d9d826a 100644
--- a/libavformat/options_table.h
+++ b/libavformat/options_table.h
@@ -77,6 +77,7 @@ static const AVOption avformat_options[] = {
 {"skip_initial_bytes", "set number of bytes to skip before reading header and frames", OFFSET(skip_initial_bytes), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX-1, D},
 {"correct_ts_overflow", "correct single timestamp overflows", OFFSET(correct_ts_overflow), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, D},
 {"flush_packets", "enable flushing of the I/O context after each packet", OFFSET(flush_packets), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, E},
+{"metadata_header_padding", "set number of bytes to be written as padding in a metadata header", OFFSET(metadata_header_padding), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1048576 /* How large should this go?  Not all formats are equal. */, E},
 {NULL},
 };
 
-- 
1.7.9

-------------- next part --------------
From 504d554e1605e29c70e820e23b08587b59268ac6 Mon Sep 17 00:00:00 2001
From: James Darnley <james.darnley at gmail.com>
Date: Sun, 29 Dec 2013 22:58:27 +0100
Subject: [PATCH 2/4] lavf/flacenc: use metadata_header_padding

Allows a user to control the amount, if any, of padding they want added
to the file.  If set to zero the block will not be written at all.  If
set to some positive number four more bytes will be added to the file
due to the small header required for the block.
---
 libavformat/flacenc.c |    9 ++++++---
 1 files changed, 6 insertions(+), 3 deletions(-)

diff --git a/libavformat/flacenc.c b/libavformat/flacenc.c
index 87e9362..31b60d1 100644
--- a/libavformat/flacenc.c
+++ b/libavformat/flacenc.c
@@ -64,7 +64,7 @@ static int flac_write_block_comment(AVIOContext *pb, AVDictionary **m,
 
 static int flac_write_header(struct AVFormatContext *s)
 {
-    int ret;
+    int ret, padding;
     AVCodecContext *codec = s->streams[0]->codec;
 
     if (s->nb_streams > 1) {
@@ -76,11 +76,13 @@ static int flac_write_header(struct AVFormatContext *s)
         return AVERROR(EINVAL);
     }
 
+    padding = s->metadata_header_padding;
+
     ret = ff_flac_write_header(s->pb, codec, 0);
     if (ret)
         return ret;
 
-    ret = flac_write_block_comment(s->pb, &s->metadata, 0,
+    ret = flac_write_block_comment(s->pb, &s->metadata, !padding,
                                    codec->flags & CODEC_FLAG_BITEXACT);
     if (ret)
         return ret;
@@ -89,7 +91,8 @@ static int flac_write_header(struct AVFormatContext *s)
      * every 10s.  So one might add padding to allow that later
      * but there seems to be no simple way to get the duration here.
      * So let's try the flac default of 8192 bytes */
-    flac_write_block_padding(s->pb, 8192, 1);
+    if (padding)
+        flac_write_block_padding(s->pb, padding, 1);
 
     return ret;
 }
-- 
1.7.9

-------------- next part --------------
From d27319032881c3594d526f0b4899f877f037e486 Mon Sep 17 00:00:00 2001
From: James Darnley <james.darnley at gmail.com>
Date: Mon, 30 Dec 2013 00:41:50 +0100
Subject: [PATCH 3/4] lavf/flacenc: fix comment after previous change

---
 libavformat/flacenc.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/libavformat/flacenc.c b/libavformat/flacenc.c
index 31b60d1..4eb581c 100644
--- a/libavformat/flacenc.c
+++ b/libavformat/flacenc.c
@@ -90,7 +90,7 @@ static int flac_write_header(struct AVFormatContext *s)
     /* The command line flac encoder defaults to placing a seekpoint
      * every 10s.  So one might add padding to allow that later
      * but there seems to be no simple way to get the duration here.
-     * So let's try the flac default of 8192 bytes */
+     * So just add the amount requested by the user. */
     if (padding)
         flac_write_block_padding(s->pb, padding, 1);
 
-- 
1.7.9

-------------- next part --------------
From 7a8abec9b921571cd96bf4d44ad05c183b294592 Mon Sep 17 00:00:00 2001
From: James Darnley <james.darnley at gmail.com>
Date: Mon, 30 Dec 2013 18:13:08 +0100
Subject: [PATCH 4/4] lavf/id3v2enc: use metadata_header_padding

As with the change to flacenc this allows the user to control the amount
of padding they want added to the file.
---
 libavformat/aiffenc.c  |    2 +-
 libavformat/id3v2.h    |    2 +-
 libavformat/id3v2enc.c |   11 +++++------
 libavformat/mp3enc.c   |    4 ++--
 4 files changed, 9 insertions(+), 10 deletions(-)

diff --git a/libavformat/aiffenc.c b/libavformat/aiffenc.c
index 6e3d8bc..90fc433 100644
--- a/libavformat/aiffenc.c
+++ b/libavformat/aiffenc.c
@@ -66,7 +66,7 @@ static int put_id3v2_tags(AVFormatContext *s, AIFFOutputContext *aiff)
             return ret;
         pict_list = pict_list->next;
     }
-    ff_id3v2_finish(&id3v2, pb);
+    ff_id3v2_finish(&id3v2, pb, s->metadata_header_padding);
 
     end = avio_tell(pb);
     size = end - pos;
diff --git a/libavformat/id3v2.h b/libavformat/id3v2.h
index e893922..05df7c2 100644
--- a/libavformat/id3v2.h
+++ b/libavformat/id3v2.h
@@ -114,7 +114,7 @@ int ff_id3v2_write_apic(AVFormatContext *s, ID3v2EncContext *id3, AVPacket *pkt)
 /**
  * Finalize an opened ID3v2 tag.
  */
-void ff_id3v2_finish(ID3v2EncContext *id3, AVIOContext *pb);
+void ff_id3v2_finish(ID3v2EncContext *id3, AVIOContext *pb, int padding_bytes);
 
 /**
  * Write an ID3v2 tag containing all global metadata from s.
diff --git a/libavformat/id3v2enc.c b/libavformat/id3v2enc.c
index 6052244..6a85237 100644
--- a/libavformat/id3v2enc.c
+++ b/libavformat/id3v2enc.c
@@ -29,8 +29,6 @@
 #include "avio_internal.h"
 #include "id3v2.h"
 
-#define PADDING_BYTES 10
-
 static void id3v2_put_size(AVIOContext *pb, int size)
 {
     avio_w8(pb, size >> 21 & 0x7f);
@@ -324,15 +322,16 @@ int ff_id3v2_write_apic(AVFormatContext *s, ID3v2EncContext *id3, AVPacket *pkt)
     return 0;
 }
 
-void ff_id3v2_finish(ID3v2EncContext *id3, AVIOContext *pb)
+void ff_id3v2_finish(ID3v2EncContext *id3, AVIOContext *pb,
+                     int padding_bytes)
 {
     int64_t cur_pos;
 
     /* adding an arbitrary amount of padding bytes at the end of the
      * ID3 metadata fixes cover art display for some software (iTunes,
      * Traktor, Serato, Torq) */
-    ffio_fill(pb, 0, PADDING_BYTES);
-    id3->len += PADDING_BYTES;
+    ffio_fill(pb, 0, padding_bytes);
+    id3->len += padding_bytes;
 
     cur_pos = avio_tell(pb);
     avio_seek(pb, id3->size_pos, SEEK_SET);
@@ -349,7 +348,7 @@ int ff_id3v2_write_simple(struct AVFormatContext *s, int id3v2_version,
     ff_id3v2_start(&id3, s->pb, id3v2_version, magic);
     if ((ret = ff_id3v2_write_metadata(s, &id3)) < 0)
         return ret;
-    ff_id3v2_finish(&id3, s->pb);
+    ff_id3v2_finish(&id3, s->pb, s->metadata_header_padding);
 
     return 0;
 }
diff --git a/libavformat/mp3enc.c b/libavformat/mp3enc.c
index a5f672b..c0d3f8e 100644
--- a/libavformat/mp3enc.c
+++ b/libavformat/mp3enc.c
@@ -303,7 +303,7 @@ static int mp3_queue_flush(AVFormatContext *s)
     AVPacketList *pktl;
     int ret = 0, write = 1;
 
-    ff_id3v2_finish(&mp3->id3, s->pb);
+    ff_id3v2_finish(&mp3->id3, s->pb, s->metadata_header_padding);
     mp3_write_xing(s);
 
     while ((pktl = mp3->queue)) {
@@ -493,7 +493,7 @@ static int mp3_write_header(struct AVFormatContext *s)
         return ret;
 
     if (!mp3->pics_to_write) {
-        ff_id3v2_finish(&mp3->id3, s->pb);
+        ff_id3v2_finish(&mp3->id3, s->pb, s->metadata_header_padding);
         mp3_write_xing(s);
     }
 
-- 
1.7.9

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 683 bytes
Desc: OpenPGP digital signature
URL: <http://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20131230/04bd775c/attachment.asc>


More information about the ffmpeg-devel mailing list