[FFmpeg-devel] [PATCH] ff_put_wav_header: add flag to force WAVEFORMATEX

Daniel Verkamp daniel at drv.nu
Mon Apr 28 08:49:15 CEST 2014


Partially undoes commit 2c4e08d89327595f7f4be57dda4b3775e1198d5e:

    riff: always generate a proper WAVEFORMATEX structure in
    ff_put_wav_header

A new flag, FF_PUT_WAV_HEADER_FORCE_WAVEFORMATEX, is added to force the
use of WAVEFORMATEX rather than PCMWAVEFORMAT even for PCM codecs.

This flag is used in the Matroska muxer (the cause of the original
change) and in the ASF muxer, because the specifications for
these formats indicate explicitly that WAVEFORMATEX should be used.

Muxers for other formats will return to the original behavior of writing
PCMWAVEFORMAT when writing a header for raw PCM.

In particular, this causes raw PCM in WAV to generate the canonical
44-byte header expected by some tools.
---
 libavformat/asfenc.c      |  2 +-
 libavformat/avienc.c      |  2 +-
 libavformat/matroskaenc.c |  2 +-
 libavformat/movenc.c      |  4 ++--
 libavformat/riff.h        | 16 +++++++++++++++-
 libavformat/riffenc.c     |  6 ++++--
 libavformat/wavenc.c      |  4 ++--
 libavformat/wtvenc.c      |  2 +-
 8 files changed, 27 insertions(+), 11 deletions(-)

diff --git a/libavformat/asfenc.c b/libavformat/asfenc.c
index 8add6e0..23d83b7 100644
--- a/libavformat/asfenc.c
+++ b/libavformat/asfenc.c
@@ -527,7 +527,7 @@ static int asf_write_header1(AVFormatContext *s, int64_t file_size,
 
         if (enc->codec_type == AVMEDIA_TYPE_AUDIO) {
             /* WAVEFORMATEX header */
-            int wavsize = ff_put_wav_header(pb, enc);
+            int wavsize = ff_put_wav_header(pb, enc, FF_PUT_WAV_HEADER_FORCE_WAVEFORMATEX);
 
             if (wavsize < 0)
                 return -1;
diff --git a/libavformat/avienc.c b/libavformat/avienc.c
index 7d5aee0..89e2a53 100644
--- a/libavformat/avienc.c
+++ b/libavformat/avienc.c
@@ -325,7 +325,7 @@ static int avi_write_header(AVFormatContext *s)
                           av_get_pix_fmt_name(stream->pix_fmt));
                 break;
             case AVMEDIA_TYPE_AUDIO:
-                if ((ret = ff_put_wav_header(pb, stream)) < 0)
+                if ((ret = ff_put_wav_header(pb, stream, 0)) < 0)
                     return ret;
                 break;
             default:
diff --git a/libavformat/matroskaenc.c b/libavformat/matroskaenc.c
index a628c7a..62e2d57 100644
--- a/libavformat/matroskaenc.c
+++ b/libavformat/matroskaenc.c
@@ -593,7 +593,7 @@ static int mkv_write_codecprivate(AVFormatContext *s, AVIOContext *pb, AVCodecCo
         if (!codec->codec_tag)
             codec->codec_tag = tag;
 
-        ff_put_wav_header(dyn_cp, codec);
+        ff_put_wav_header(dyn_cp, codec, FF_PUT_WAV_HEADER_FORCE_WAVEFORMATEX);
     }
 
     codecpriv_size = avio_close_dyn_buf(dyn_cp, &codecpriv);
diff --git a/libavformat/movenc.c b/libavformat/movenc.c
index 22b4ca6..bb0cb55 100644
--- a/libavformat/movenc.c
+++ b/libavformat/movenc.c
@@ -401,7 +401,7 @@ static int mov_write_ms_tag(AVIOContext *pb, MOVTrack *track)
     avio_wb32(pb, 0);
     avio_wl32(pb, track->tag); // store it byteswapped
     track->enc->codec_tag = av_bswap16(track->tag >> 16);
-    ff_put_wav_header(pb, track->enc);
+    ff_put_wav_header(pb, track->enc, 0);
     return update_size(pb, pos);
 }
 
@@ -410,7 +410,7 @@ static int mov_write_wfex_tag(AVIOContext *pb, MOVTrack *track)
     int64_t pos = avio_tell(pb);
     avio_wb32(pb, 0);
     ffio_wfourcc(pb, "wfex");
-    ff_put_wav_header(pb, track->enc);
+    ff_put_wav_header(pb, track->enc, FF_PUT_WAV_HEADER_FORCE_WAVEFORMATEX);
     return update_size(pb, pos);
 }
 
diff --git a/libavformat/riff.h b/libavformat/riff.h
index dba3803..6f07179 100644
--- a/libavformat/riff.h
+++ b/libavformat/riff.h
@@ -46,7 +46,21 @@ void ff_end_tag(AVIOContext *pb, int64_t start);
 int ff_get_bmp_header(AVIOContext *pb, AVStream *st, unsigned *esize);
 
 void ff_put_bmp_header(AVIOContext *pb, AVCodecContext *enc, const AVCodecTag *tags, int for_asf, int ignore_extradata);
-int ff_put_wav_header(AVIOContext *pb, AVCodecContext *enc);
+
+/**
+ * Tell ff_put_wav_header() to use WAVEFORMATEX even for PCM codecs.
+ */
+#define FF_PUT_WAV_HEADER_FORCE_WAVEFORMATEX    0x00000001
+
+/**
+ * Write WAVEFORMAT header structure.
+ *
+ * @param flags a combination of FF_PUT_WAV_HEADER_* constants
+ *
+ * @return the size or -1 on error
+ */
+int ff_put_wav_header(AVIOContext *pb, AVCodecContext *enc, int flags);
+
 enum AVCodecID ff_wav_codec_get_id(unsigned int tag, int bps);
 int ff_get_wav_header(AVIOContext *pb, AVCodecContext *codec, int size);
 
diff --git a/libavformat/riffenc.c b/libavformat/riffenc.c
index 6c91cb6..54faa82 100644
--- a/libavformat/riffenc.c
+++ b/libavformat/riffenc.c
@@ -51,7 +51,7 @@ void ff_end_tag(AVIOContext *pb, int64_t start)
 
 /* WAVEFORMATEX header */
 /* returns the size or -1 on error */
-int ff_put_wav_header(AVIOContext *pb, AVCodecContext *enc)
+int ff_put_wav_header(AVIOContext *pb, AVCodecContext *enc, int flags)
 {
     int bps, blkalign, bytespersec, frame_size;
     int hdrsize;
@@ -189,7 +189,9 @@ int ff_put_wav_header(AVIOContext *pb, AVCodecContext *enc)
         avio_wl32(pb, 0xAA000080);
         avio_wl32(pb, 0x719B3800);
         }
-    } else {
+    } else if ((flags & FF_PUT_WAV_HEADER_FORCE_WAVEFORMATEX) ||
+               enc->codec_tag != 0x0001 /* PCM */ ||
+               riff_extradata - riff_extradata_start) {
         avio_wl16(pb, riff_extradata - riff_extradata_start); /* cbSize */
     }
     avio_write(pb, riff_extradata_start, riff_extradata - riff_extradata_start);
diff --git a/libavformat/wavenc.c b/libavformat/wavenc.c
index 0067dfe..0ddd218 100644
--- a/libavformat/wavenc.c
+++ b/libavformat/wavenc.c
@@ -141,7 +141,7 @@ static int wav_write_header(AVFormatContext *s)
 
     /* format header */
     fmt = ff_start_tag(pb, "fmt ");
-    if (ff_put_wav_header(pb, s->streams[0]->codec) < 0) {
+    if (ff_put_wav_header(pb, s->streams[0]->codec, 0) < 0) {
         av_log(s, AV_LOG_ERROR, "%s codec not supported in WAVE format\n",
                s->streams[0]->codec->codec ? s->streams[0]->codec->codec->name : "NONE");
         return -1;
@@ -323,7 +323,7 @@ static int w64_write_header(AVFormatContext *s)
     avio_wl64(pb, -1);
     avio_write(pb, ff_w64_guid_wave, sizeof(ff_w64_guid_wave));
     start_guid(pb, ff_w64_guid_fmt, &start);
-    if ((ret = ff_put_wav_header(pb, s->streams[0]->codec)) < 0) {
+    if ((ret = ff_put_wav_header(pb, s->streams[0]->codec, 0)) < 0) {
         av_log(s, AV_LOG_ERROR, "%s codec not supported\n",
                s->streams[0]->codec->codec ? s->streams[0]->codec->codec->name : "NONE");
         return ret;
diff --git a/libavformat/wtvenc.c b/libavformat/wtvenc.c
index f051c66..634545d 100644
--- a/libavformat/wtvenc.c
+++ b/libavformat/wtvenc.c
@@ -289,7 +289,7 @@ static int write_stream_codec_info(AVFormatContext *s, AVStream *st)
     if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
         put_videoinfoheader2(pb, st);
     } else {
-        if (ff_put_wav_header(pb, st->codec) < 0)
+        if (ff_put_wav_header(pb, st->codec, 0) < 0)
             format_type = &ff_format_none;
     }
     hdr_size = avio_tell(pb) - hdr_pos_start;
-- 
1.9.2



More information about the ffmpeg-devel mailing list