[FFmpeg-devel] [PATCH 06/10 rev2] spdifenc: fix byte order on big-endian systems

Anssi Hannula anssi.hannula
Sat Jan 1 06:38:57 CET 2011

The preamble and final odd byte were outputted in the wrong byte order
on big-endian systems.


I'm wondering if this patch would be a better choice. All our audio
decoders output native-endian samples. As this muxer is usually used in
place of an encoder, it would be logical for it to have its output in
native-endian as well.

However, this would cause (AFAIK) the muxer to be the only one that has
different output depending on system endianness.. Don't know if that is
very big an issue, though.
Also, it looks like big-endian systems may have audio devices that take
little-endian format, thus requiring the byteswap anyway; though that'd
need to be done for regular audio decoding as well and we don't provide
any switches there either.

Please comment.

(BTW, technically this is a big-endian format by nature, and it is only
byteswapped in order to output it to audio devices/applications that
expect S16LE input; but I think it is too late to change our muxer to be
fixed big-endian (avoiding byte-swaps) at this point, and it would
indeed complicate its use on little-endian systems)

 libavformat/spdifenc.c |   19 ++++++++++++++-----
 1 files changed, 14 insertions(+), 5 deletions(-)

diff --git a/libavformat/spdifenc.c b/libavformat/spdifenc.c
index 2f6c443..b01ce70 100644
--- a/libavformat/spdifenc.c
+++ b/libavformat/spdifenc.c
@@ -476,6 +476,14 @@ static int spdif_write_trailer(AVFormatContext *s)
     return 0;
+/* always provide output in native-endian format as this muxer is usually
+ * used in place of a decoder and our decoders output native-endian as well */
+#define spdif_put_16 put_be16
+#define spdif_put_16 put_le16
 static int spdif_write_packet(struct AVFormatContext *s, AVPacket *pkt)
     IEC61937Context *ctx = s->priv_data;
@@ -500,10 +508,10 @@ static int spdif_write_packet(struct AVFormatContext *s, AVPacket *pkt)
     if (ctx->use_preamble) {
-        put_le16(s->pb, SYNCWORD1);       //Pa
-        put_le16(s->pb, SYNCWORD2);       //Pb
-        put_le16(s->pb, ctx->data_type);  //Pc
-        put_le16(s->pb, ctx->length_code);//Pd
+        spdif_put_16(s->pb, SYNCWORD1);       //Pa
+        spdif_put_16(s->pb, SYNCWORD2);       //Pb
+        spdif_put_16(s->pb, ctx->data_type);  //Pc
+        spdif_put_16(s->pb, ctx->length_code);//Pd
     if (HAVE_BIGENDIAN ^ ctx->extra_bswap) {
@@ -516,8 +524,9 @@ static int spdif_write_packet(struct AVFormatContext *s, AVPacket *pkt)
     put_buffer(s->pb, ctx->buffer, ctx->out_bytes & ~1);
+    /* a final lone byte has to be MSB aligned */
     if (ctx->out_bytes & 1)
-        put_be16(s->pb, ctx->out_buf[ctx->out_bytes - 1]);
+        spdif_put_16(s->pb, ctx->out_buf[ctx->out_bytes - 1] << 8);
     put_nbyte(s->pb, 0, padding);

More information about the ffmpeg-devel mailing list