[FFmpeg-devel] User controllable padding v2
James Darnley
james.darnley at gmail.com
Fri Jan 17 20:03:40 CET 2014
I followed some of the suggestions from last time I posted this. I made
the maximum value INT_MAX and the default 1024. I added the accessors.
I've updated the fate tests. The changes are file sizes and position
in files, both of which agree with the changes I made. I also caught a
typo in a comment.
James Darnley (6):
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
lavf/id3v2enc: update comment about minimum padding
fate: fix tests after header padding changes
libavformat/aiffenc.c | 2 +-
libavformat/avformat.h | 9 ++++++++
libavformat/flacenc.c | 13 ++++++++---
libavformat/id3v2.h | 2 +-
libavformat/id3v2enc.c | 21 +++++++++++--------
libavformat/mp3enc.c | 4 +-
libavformat/options_table.h | 1 +
libavformat/utils.c | 1 +
libavformat/version.h | 4 +-
tests/ref/acodec/flac | 4 +-
tests/ref/lavf-fate/mp3 | 4 +-
tests/ref/seek/acodec-flac | 46
+++++++++++++++++++++---------------------
12 files changed, 65 insertions(+), 46 deletions(-)
-------------- next part --------------
From bdff41ac4a70b4695e91a874f1d8a74f5b7daed7 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/6] 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 | 9 +++++++++
libavformat/options_table.h | 1 +
libavformat/utils.c | 1 +
3 files changed, 11 insertions(+), 0 deletions(-)
diff --git a/libavformat/avformat.h b/libavformat/avformat.h
index 6d719d7..609ba43 100644
--- a/libavformat/avformat.h
+++ b/libavformat/avformat.h
@@ -1348,6 +1348,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 via av_format_set_metadata_header_padding.
+ */
+ int metadata_header_padding;
} AVFormatContext;
int av_format_get_probe_score(const AVFormatContext *s);
@@ -1357,6 +1364,8 @@ AVCodec * av_format_get_audio_codec(const AVFormatContext *s);
void av_format_set_audio_codec(AVFormatContext *s, AVCodec *c);
AVCodec * av_format_get_subtitle_codec(const AVFormatContext *s);
void av_format_set_subtitle_codec(AVFormatContext *s, AVCodec *c);
+int av_format_get_metadata_header_padding(const AVFormatContext *s);
+void av_format_set_metadata_header_padding(AVFormatContext *s, int c);
/**
* Returns the method used to set ctx->duration.
diff --git a/libavformat/options_table.h b/libavformat/options_table.h
index 8145325..0b1cebb 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 = 1024}, 0, INT_MAX, E},
{NULL},
};
diff --git a/libavformat/utils.c b/libavformat/utils.c
index c530511..421dfef 100644
--- a/libavformat/utils.c
+++ b/libavformat/utils.c
@@ -103,6 +103,7 @@ MAKE_ACCESSORS(AVStream, stream, AVRational, r_frame_rate)
MAKE_ACCESSORS(AVFormatContext, format, AVCodec *, video_codec)
MAKE_ACCESSORS(AVFormatContext, format, AVCodec *, audio_codec)
MAKE_ACCESSORS(AVFormatContext, format, AVCodec *, subtitle_codec)
+MAKE_ACCESSORS(AVFormatContext, format, int, metadata_header_padding)
static AVCodec *find_decoder(AVFormatContext *s, AVStream *st, enum AVCodecID codec_id)
{
--
1.7.9
-------------- next part --------------
From 5d116da8fde6b0c35af776a91084ec323d956361 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/6] 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 | 11 ++++++++---
1 files changed, 8 insertions(+), 3 deletions(-)
diff --git a/libavformat/flacenc.c b/libavformat/flacenc.c
index d2e274c..9430f24 100644
--- a/libavformat/flacenc.c
+++ b/libavformat/flacenc.c
@@ -70,7 +70,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;
FlacMuxerContext *c = s->priv_data;
@@ -86,11 +86,15 @@ static int flac_write_header(struct AVFormatContext *s)
return AVERROR(EINVAL);
}
+ /* The FLAC specification states that 24 bits are used to represent the
+ * size of a metadata block so we must clip this value to 2^24-1. */
+ padding = av_clip_c(s->metadata_header_padding, 0, 16777215);
+
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;
@@ -99,7 +103,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 98d39f16852f4500f17b7a0491ee95b6a6292409 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/6] 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 9430f24..67c6767 100644
--- a/libavformat/flacenc.c
+++ b/libavformat/flacenc.c
@@ -102,7 +102,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 430d9954258feec3562edcc4cf9d71c7375a2962 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/6] 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 | 16 ++++++++++------
libavformat/mp3enc.c | 4 ++--
4 files changed, 14 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 935e934..eb4dc79 100644
--- a/libavformat/id3v2.h
+++ b/libavformat/id3v2.h
@@ -134,7 +134,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..7d44e85 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,21 @@ 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;
+ /* The ID3v2.3 specification states that 28 bits are used to represent the
+ * size of the whole tag. Therefore the current size of the tag needs to be
+ * subtracted from the upper limit of 2^28-1 to clip the value correctly. */
+ padding_bytes = av_clip(padding_bytes, 10, 268435455 - id3->len);
+
/* 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 +353,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 9523cf9..adf6a32 100644
--- a/libavformat/mp3enc.c
+++ b/libavformat/mp3enc.c
@@ -304,7 +304,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)) {
@@ -513,7 +513,7 @@ static int mp3_write_header(struct AVFormatContext *s)
if (!mp3->pics_to_write) {
if (mp3->id3v2_version)
- 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 --------------
From 81e900ec27d8b55d5e9da3f52637f36ff2f18857 Mon Sep 17 00:00:00 2001
From: James Darnley <james.darnley at gmail.com>
Date: Thu, 2 Jan 2014 20:34:35 +0100
Subject: [PATCH 5/6] lavf/id3v2enc: update comment about minimum padding
---
libavformat/id3v2enc.c | 7 +++----
1 files changed, 3 insertions(+), 4 deletions(-)
diff --git a/libavformat/id3v2enc.c b/libavformat/id3v2enc.c
index 7d44e85..5582b6e 100644
--- a/libavformat/id3v2enc.c
+++ b/libavformat/id3v2enc.c
@@ -330,11 +330,10 @@ void ff_id3v2_finish(ID3v2EncContext *id3, AVIOContext *pb,
/* The ID3v2.3 specification states that 28 bits are used to represent the
* size of the whole tag. Therefore the current size of the tag needs to be
* subtracted from the upper limit of 2^28-1 to clip the value correctly. */
+ /* The minimum of 10 is an arbitrary amount of padding at the end of the tag
+ * to fix cover art display with some software such as iTunes, Traktor,
+ * Serato, Torq. */
padding_bytes = av_clip(padding_bytes, 10, 268435455 - id3->len);
-
- /* 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;
--
1.7.9
-------------- next part --------------
From 6937734e7456c6745ac7c0b6da684c1a6e0e0590 Mon Sep 17 00:00:00 2001
From: James Darnley <james.darnley at gmail.com>
Date: Fri, 17 Jan 2014 15:15:00 +0100
Subject: [PATCH 6/6] fate: fix tests after header padding changes
---
tests/ref/acodec/flac | 4 +-
tests/ref/lavf-fate/mp3 | 4 +-
tests/ref/seek/acodec-flac | 46 ++++++++++++++++++++++----------------------
3 files changed, 27 insertions(+), 27 deletions(-)
diff --git a/tests/ref/acodec/flac b/tests/ref/acodec/flac
index 1cc3770..0fb6221 100644
--- a/tests/ref/acodec/flac
+++ b/tests/ref/acodec/flac
@@ -1,4 +1,4 @@
-151eef9097f944726968bec48649f00a *tests/data/fate/acodec-flac.flac
-361582 tests/data/fate/acodec-flac.flac
+304faf78eed382edb2efe457cb70ad3a *tests/data/fate/acodec-flac.flac
+354414 tests/data/fate/acodec-flac.flac
64151e4bcc2b717aa5a8454d424d6a1f *tests/data/fate/acodec-flac.out.wav
stddev: 0.00 PSNR:999.99 MAXDIFF: 0 bytes: 1058400/ 1058400
diff --git a/tests/ref/lavf-fate/mp3 b/tests/ref/lavf-fate/mp3
index 6f201e0..c29d87a 100644
--- a/tests/ref/lavf-fate/mp3
+++ b/tests/ref/lavf-fate/mp3
@@ -1,3 +1,3 @@
-6bdea919dc6856d76ef2553698e2b0d3 *./tests/data/lavf-fate/lavf.mp3
-96376 ./tests/data/lavf-fate/lavf.mp3
+7e6695027aa12704f146b7c5458a5ce0 *./tests/data/lavf-fate/lavf.mp3
+97390 ./tests/data/lavf-fate/lavf.mp3
./tests/data/lavf-fate/lavf.mp3 CRC=0x6c9850fe
diff --git a/tests/ref/seek/acodec-flac b/tests/ref/seek/acodec-flac
index ab31891..e83a914 100644
--- a/tests/ref/seek/acodec-flac
+++ b/tests/ref/seek/acodec-flac
@@ -1,49 +1,49 @@
-ret: 0 st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos: 8256 size: 614
+ret: 0 st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos: 1088 size: 614
ret: 0 st:-1 flags:0 ts:-1.000000
-ret: 0 st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos: 8256 size: 614
+ret: 0 st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos: 1088 size: 614
ret: 0 st:-1 flags:1 ts: 1.894167
-ret: 0 st: 0 flags:1 dts: 1.880816 pts: 1.880816 pos: 86742 size: 2191
+ret: 0 st: 0 flags:1 dts: 1.880816 pts: 1.880816 pos: 79574 size: 2191
ret: 0 st: 0 flags:0 ts: 0.788345
-ret: 0 st: 0 flags:1 dts: 0.809796 pts: 0.809796 pos: 27366 size: 615
+ret: 0 st: 0 flags:1 dts: 0.809796 pts: 0.809796 pos: 20198 size: 615
ret:-1 st: 0 flags:1 ts:-0.317506
ret: 0 st:-1 flags:0 ts: 2.576668
-ret: 0 st: 0 flags:1 dts: 2.586122 pts: 2.586122 pos: 145606 size: 2384
+ret: 0 st: 0 flags:1 dts: 2.586122 pts: 2.586122 pos: 138438 size: 2384
ret: 0 st:-1 flags:1 ts: 1.470835
-ret: 0 st: 0 flags:1 dts: 1.462857 pts: 1.462857 pos: 53388 size: 1851
+ret: 0 st: 0 flags:1 dts: 1.462857 pts: 1.462857 pos: 46220 size: 1851
ret: 0 st: 0 flags:0 ts: 0.365011
-ret: 0 st: 0 flags:1 dts: 0.365714 pts: 0.365714 pos: 16890 size: 614
+ret: 0 st: 0 flags:1 dts: 0.365714 pts: 0.365714 pos: 9722 size: 614
ret:-1 st: 0 flags:1 ts:-0.740839
ret: 0 st:-1 flags:0 ts: 2.153336
-ret: 0 st: 0 flags:1 dts: 2.168163 pts: 2.168163 pos: 110531 size: 2143
+ret: 0 st: 0 flags:1 dts: 2.168163 pts: 2.168163 pos: 103363 size: 2143
ret: 0 st:-1 flags:1 ts: 1.047503
-ret: 0 st: 0 flags:1 dts: 1.044898 pts: 1.044898 pos: 32880 size: 579
+ret: 0 st: 0 flags:1 dts: 1.044898 pts: 1.044898 pos: 25712 size: 579
ret: 0 st: 0 flags:0 ts:-0.058322
-ret: 0 st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos: 8256 size: 614
+ret: 0 st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos: 1088 size: 614
ret: 0 st: 0 flags:1 ts: 2.835828
-ret: 0 st: 0 flags:1 dts: 2.821224 pts: 2.821224 pos: 167112 size: 2391
+ret: 0 st: 0 flags:1 dts: 2.821224 pts: 2.821224 pos: 159944 size: 2391
ret: 0 st:-1 flags:0 ts: 1.730004
-ret: 0 st: 0 flags:1 dts: 1.750204 pts: 1.750204 pos: 75788 size: 2191
+ret: 0 st: 0 flags:1 dts: 1.750204 pts: 1.750204 pos: 68620 size: 2191
ret: 0 st:-1 flags:1 ts: 0.624171
-ret: 0 st: 0 flags:1 dts: 0.600816 pts: 0.600816 pos: 22446 size: 616
+ret: 0 st: 0 flags:1 dts: 0.600816 pts: 0.600816 pos: 15278 size: 616
ret: 0 st: 0 flags:0 ts:-0.481655
-ret: 0 st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos: 8256 size: 614
+ret: 0 st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos: 1088 size: 614
ret: 0 st: 0 flags:1 ts: 2.412494
-ret: 0 st: 0 flags:1 dts: 2.403265 pts: 2.403265 pos: 129793 size: 2138
+ret: 0 st: 0 flags:1 dts: 2.403265 pts: 2.403265 pos: 122625 size: 2138
ret: 0 st:-1 flags:0 ts: 1.306672
-ret: 0 st: 0 flags:1 dts: 1.332245 pts: 1.332245 pos: 44812 size: 1609
+ret: 0 st: 0 flags:1 dts: 1.332245 pts: 1.332245 pos: 37644 size: 1609
ret: 0 st:-1 flags:1 ts: 0.200839
-ret: 0 st: 0 flags:1 dts: 0.182857 pts: 0.182857 pos: 12572 size: 628
+ret: 0 st: 0 flags:1 dts: 0.182857 pts: 0.182857 pos: 5404 size: 628
ret: 0 st: 0 flags:0 ts:-0.904989
-ret: 0 st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos: 8256 size: 614
+ret: 0 st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos: 1088 size: 614
ret: 0 st: 0 flags:1 ts: 1.989184
-ret: 0 st: 0 flags:1 dts: 1.985306 pts: 1.985306 pos: 95508 size: 2169
+ret: 0 st: 0 flags:1 dts: 1.985306 pts: 1.985306 pos: 88340 size: 2169
ret: 0 st:-1 flags:0 ts: 0.883340
-ret: 0 st: 0 flags:1 dts: 0.888163 pts: 0.888163 pos: 29211 size: 620
+ret: 0 st: 0 flags:1 dts: 0.888163 pts: 0.888163 pos: 22043 size: 620
ret:-1 st:-1 flags:1 ts:-0.222493
ret: 0 st: 0 flags:0 ts: 2.671678
-ret: 0 st: 0 flags:1 dts: 2.690612 pts: 2.690612 pos: 155154 size: 2394
+ret: 0 st: 0 flags:1 dts: 2.690612 pts: 2.690612 pos: 147986 size: 2394
ret: 0 st: 0 flags:1 ts: 1.565850
-ret: 0 st: 0 flags:1 dts: 1.541224 pts: 1.541224 pos: 59082 size: 1974
+ret: 0 st: 0 flags:1 dts: 1.541224 pts: 1.541224 pos: 51914 size: 1974
ret: 0 st:-1 flags:0 ts: 0.460008
-ret: 0 st: 0 flags:1 dts: 0.470204 pts: 0.470204 pos: 19353 size: 608
+ret: 0 st: 0 flags:1 dts: 0.470204 pts: 0.470204 pos: 12185 size: 608
ret:-1 st:-1 flags:1 ts:-0.645825
--
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/20140117/7ab957c1/attachment.asc>
More information about the ffmpeg-devel
mailing list