[libav-devel] [PATCH 039/264] ff_put_wav_header: switch to codecpar

Anton Khirnov anton at khirnov.net
Sat Dec 12 21:49:12 CET 2015


---
 libavformat/asfenc.c      |  4 ++-
 libavformat/avienc.c      |  3 +-
 libavformat/matroskaenc.c |  6 ++--
 libavformat/movenc.c      | 38 ++++++++++----------
 libavformat/riff.h        |  2 +-
 libavformat/riffenc.c     | 91 ++++++++++++++++++++++++-----------------------
 libavformat/wavenc.c      |  2 +-
 7 files changed, 76 insertions(+), 70 deletions(-)

diff --git a/libavformat/asfenc.c b/libavformat/asfenc.c
index f6608d5..8b65768 100644
--- a/libavformat/asfenc.c
+++ b/libavformat/asfenc.c
@@ -377,6 +377,7 @@ static int asf_write_header1(AVFormatContext *s, int64_t file_size,
     int header_size, n, extra_size, extra_size2, wav_extra_size, file_time;
     int has_title;
     int metadata_count;
+    AVCodecParameters *par;
     AVCodecContext *enc;
     int64_t header_offset, cur_pos, hpos;
     int bit_rate;
@@ -480,6 +481,7 @@ static int asf_write_header1(AVFormatContext *s, int64_t file_size,
         //        ASFStream *stream = &asf->streams[n];
 
         enc                 = s->streams[n]->codec;
+        par                 = s->streams[n]->codecpar;
         asf->streams[n].num = n + 1;
         asf->streams[n].seq = 0;
 
@@ -514,7 +516,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(s, pb, par);
 
             if (wavsize < 0)
                 return -1;
diff --git a/libavformat/avienc.c b/libavformat/avienc.c
index 9c2936a..3980bf6 100644
--- a/libavformat/avienc.c
+++ b/libavformat/avienc.c
@@ -214,6 +214,7 @@ static int avi_write_header(AVFormatContext *s)
     for (i = 0; i < n; i++) {
         AVStream *st = s->streams[i];
         AVCodecContext *enc = st->codec;
+        AVCodecParameters *par = st->codecpar;
         AVIStream *avist = st->priv_data;
         list2 = ff_start_tag(pb, "LIST");
         ffio_wfourcc(pb, "strl");
@@ -293,7 +294,7 @@ static int avi_write_header(AVFormatContext *s)
                 ff_put_bmp_header(pb, enc, ff_codec_bmp_tags, 0);
                 break;
             case AVMEDIA_TYPE_AUDIO:
-                if (ff_put_wav_header(pb, enc) < 0)
+                if (ff_put_wav_header(s, pb, par) < 0)
                     return -1;
                 break;
             default:
diff --git a/libavformat/matroskaenc.c b/libavformat/matroskaenc.c
index baffd54..1606bfa 100644
--- a/libavformat/matroskaenc.c
+++ b/libavformat/matroskaenc.c
@@ -574,6 +574,7 @@ static int mkv_write_native_codecprivate(AVFormatContext *s,
 }
 
 static int mkv_write_codecprivate(AVFormatContext *s, AVIOContext *pb,
+                                  AVCodecParameters *par,
                                   AVCodecContext *codec, int native_id,
                                   int qt_id)
 {
@@ -615,7 +616,7 @@ static int mkv_write_codecprivate(AVFormatContext *s, AVIOContext *pb,
         if (!codec->codec_tag)
             codec->codec_tag = tag;
 
-        ff_put_wav_header(dyn_cp, codec);
+        ff_put_wav_header(s, dyn_cp, par);
     }
 
     codecpriv_size = avio_close_dyn_buf(dyn_cp, &codecpriv);
@@ -729,6 +730,7 @@ static int mkv_write_track(AVFormatContext *s, MatroskaMuxContext *mkv,
 {
     AVStream *st = s->streams[i];
     AVCodecContext *codec = st->codec;
+    AVCodecParameters *par = st->codecpar;
     ebml_master subinfo, track;
     int native_id = 0;
     int qt_id = 0;
@@ -866,7 +868,7 @@ static int mkv_write_track(AVFormatContext *s, MatroskaMuxContext *mkv,
         av_log(s, AV_LOG_ERROR, "Only audio, video, and subtitles are supported for Matroska.\n");
         break;
     }
-    ret = mkv_write_codecprivate(s, pb, codec, native_id, qt_id);
+    ret = mkv_write_codecprivate(s, pb, par, codec, native_id, qt_id);
     if (ret < 0)
         return ret;
 
diff --git a/libavformat/movenc.c b/libavformat/movenc.c
index 1026bae..deb37f2 100644
--- a/libavformat/movenc.c
+++ b/libavformat/movenc.c
@@ -361,22 +361,22 @@ static int mov_write_esds_tag(AVIOContext *pb, MOVTrack *track) // Basic
     return update_size(pb, pos);
 }
 
-static int mov_write_ms_tag(AVIOContext *pb, MOVTrack *track)
+static int mov_write_ms_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track)
 {
     int64_t pos = avio_tell(pb);
     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(s, pb, track->st->codecpar);
     return update_size(pb, pos);
 }
 
-static int mov_write_wfex_tag(AVIOContext *pb, MOVTrack *track)
+static int mov_write_wfex_tag(AVFormatContext *s, 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(s, pb, track->st->codecpar);
     return update_size(pb, pos);
 }
 
@@ -405,7 +405,7 @@ static int mov_write_chan_tag(AVIOContext *pb, MOVTrack *track)
     return update_size(pb, pos);
 }
 
-static int mov_write_wave_tag(AVIOContext *pb, MOVTrack *track)
+static int mov_write_wave_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track)
 {
     int64_t pos = avio_tell(pb);
 
@@ -430,7 +430,7 @@ static int mov_write_wave_tag(AVIOContext *pb, MOVTrack *track)
         mov_write_extradata_tag(pb, track);
     } else if (track->enc->codec_id == AV_CODEC_ID_ADPCM_MS ||
                track->enc->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV) {
-        mov_write_ms_tag(pb, track);
+        mov_write_ms_tag(s, pb, track);
     }
 
     avio_wb32(pb, 8);     /* size */
@@ -609,7 +609,7 @@ static int get_samples_per_packet(MOVTrack *track)
     return first_duration;
 }
 
-static int mov_write_audio_tag(AVIOContext *pb, MOVTrack *track)
+static int mov_write_audio_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track)
 {
     int64_t pos = avio_tell(pb);
     int version = 0;
@@ -665,7 +665,7 @@ static int mov_write_audio_tag(AVIOContext *pb, MOVTrack *track)
          track->enc->codec_id == AV_CODEC_ID_ALAC          ||
          track->enc->codec_id == AV_CODEC_ID_ADPCM_MS      ||
          track->enc->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV))
-        mov_write_wave_tag(pb, track);
+        mov_write_wave_tag(s, pb, track);
     else if (track->tag == MKTAG('m','p','4','a'))
         mov_write_esds_tag(pb, track);
     else if (track->enc->codec_id == AV_CODEC_ID_AMR_NB)
@@ -675,7 +675,7 @@ static int mov_write_audio_tag(AVIOContext *pb, MOVTrack *track)
     else if (track->enc->codec_id == AV_CODEC_ID_ALAC)
         mov_write_extradata_tag(pb, track);
     else if (track->enc->codec_id == AV_CODEC_ID_WMAPRO)
-        mov_write_wfex_tag(pb, track);
+        mov_write_wfex_tag(s, pb, track);
     else if (track->vos_len > 0)
         mov_write_glbl_tag(pb, track);
 
@@ -1144,7 +1144,7 @@ static int mov_write_tmcd_tag(AVIOContext *pb, MOVTrack *track)
     return update_size(pb, pos);
 }
 
-static int mov_write_stsd_tag(AVIOContext *pb, MOVTrack *track)
+static int mov_write_stsd_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track)
 {
     int64_t pos = avio_tell(pb);
     avio_wb32(pb, 0); /* size */
@@ -1154,7 +1154,7 @@ static int mov_write_stsd_tag(AVIOContext *pb, MOVTrack *track)
     if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO)
         mov_write_video_tag(pb, track);
     else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
-        mov_write_audio_tag(pb, track);
+        mov_write_audio_tag(s, pb, track);
     else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)
         mov_write_subtitle_tag(pb, track);
     else if (track->enc->codec_tag == MKTAG('r','t','p',' '))
@@ -1259,12 +1259,12 @@ static int mov_write_dref_tag(AVIOContext *pb)
     return 28;
 }
 
-static int mov_write_stbl_tag(AVIOContext *pb, MOVTrack *track)
+static int mov_write_stbl_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track)
 {
     int64_t pos = avio_tell(pb);
     avio_wb32(pb, 0); /* size */
     ffio_wfourcc(pb, "stbl");
-    mov_write_stsd_tag(pb, track);
+    mov_write_stsd_tag(s, pb, track);
     mov_write_stts_tag(pb, track);
     if ((track->enc->codec_type == AVMEDIA_TYPE_VIDEO ||
          track->enc->codec_tag == MKTAG('r','t','p',' ')) &&
@@ -1424,7 +1424,7 @@ static int mov_write_hmhd_tag(AVIOContext *pb)
     return 28;
 }
 
-static int mov_write_minf_tag(AVIOContext *pb, MOVTrack *track)
+static int mov_write_minf_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track)
 {
     int64_t pos = avio_tell(pb);
     avio_wb32(pb, 0); /* size */
@@ -1447,7 +1447,7 @@ static int mov_write_minf_tag(AVIOContext *pb, MOVTrack *track)
     if (track->mode == MODE_MOV) /* FIXME: Why do it for MODE_MOV only ? */
         mov_write_hdlr_tag(pb, NULL);
     mov_write_dinf_tag(pb);
-    mov_write_stbl_tag(pb, track);
+    mov_write_stbl_tag(s, pb, track);
     return update_size(pb, pos);
 }
 
@@ -1490,15 +1490,15 @@ static int mov_write_mdhd_tag(AVIOContext *pb, MOVMuxContext *mov,
     return 32;
 }
 
-static int mov_write_mdia_tag(AVIOContext *pb, MOVMuxContext *mov,
-                              MOVTrack *track)
+static int mov_write_mdia_tag(AVFormatContext *s, AVIOContext *pb,
+                              MOVMuxContext *mov, MOVTrack *track)
 {
     int64_t pos = avio_tell(pb);
     avio_wb32(pb, 0); /* size */
     ffio_wfourcc(pb, "mdia");
     mov_write_mdhd_tag(pb, mov, track);
     mov_write_hdlr_tag(pb, track);
-    mov_write_minf_tag(pb, track);
+    mov_write_minf_tag(s, pb, track);
     return update_size(pb, pos);
 }
 
@@ -1816,7 +1816,7 @@ static int mov_write_trak_tag(AVIOContext *pb, MOVMuxContext *mov,
     }
     if (track->tref_tag)
         mov_write_tref_tag(pb, track);
-    mov_write_mdia_tag(pb, mov, track);
+    mov_write_mdia_tag(mov->fc, pb, mov, track);
     if (track->mode == MODE_PSP)
         mov_write_uuid_tag_psp(pb, track); // PSP Movies require this uuid box
     if (track->tag == MKTAG('r','t','p',' '))
diff --git a/libavformat/riff.h b/libavformat/riff.h
index 1332109..8ccf4db 100644
--- a/libavformat/riff.h
+++ b/libavformat/riff.h
@@ -46,7 +46,7 @@ void ff_end_tag(AVIOContext *pb, int64_t start);
 int ff_get_bmp_header(AVIOContext *pb, AVStream *st);
 
 void ff_put_bmp_header(AVIOContext *pb, AVCodecContext *enc, const AVCodecTag *tags, int for_asf);
-int ff_put_wav_header(AVIOContext *pb, AVCodecContext *enc);
+int ff_put_wav_header(AVFormatContext *s, AVIOContext *pb, AVCodecParameters *par);
 enum AVCodecID ff_wav_codec_get_id(unsigned int tag, int bps);
 int ff_get_wav_header(AVFormatContext *s, AVIOContext *pb, AVCodecParameters *par, int size);
 
diff --git a/libavformat/riffenc.c b/libavformat/riffenc.c
index 2e4b7e0..d31b1a2 100644
--- a/libavformat/riffenc.c
+++ b/libavformat/riffenc.c
@@ -47,7 +47,8 @@ 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(AVFormatContext *s, AVIOContext *pb,
+                      AVCodecParameters *par)
 {
     int bps, blkalign, bytespersec, frame_size;
     int hdrsize = 18;
@@ -56,82 +57,82 @@ int ff_put_wav_header(AVIOContext *pb, AVCodecContext *enc)
     uint8_t *riff_extradata       = temp;
     uint8_t *riff_extradata_start = temp;
 
-    if (!enc->codec_tag || enc->codec_tag > 0xffff)
+    if (!par->codec_tag || par->codec_tag > 0xffff)
         return -1;
 
     /* We use the known constant frame size for the codec if known, otherwise
      * fall back on using AVCodecContext.frame_size, which is not as reliable
      * for indicating packet duration. */
-    frame_size = av_get_audio_frame_duration(enc, enc->block_align);
+    frame_size = av_get_audio_frame_duration2(par, par->block_align);
 
-    waveformatextensible = (enc->channels > 2 && enc->channel_layout) ||
-                           enc->sample_rate > 48000 ||
-                           av_get_bits_per_sample(enc->codec_id) > 16;
+    waveformatextensible = (par->channels > 2 && par->channel_layout) ||
+                           par->sample_rate > 48000 ||
+                           av_get_bits_per_sample(par->codec_id) > 16;
 
     if (waveformatextensible)
         avio_wl16(pb, 0xfffe);
     else
-        avio_wl16(pb, enc->codec_tag);
+        avio_wl16(pb, par->codec_tag);
 
-    avio_wl16(pb, enc->channels);
-    avio_wl32(pb, enc->sample_rate);
-    if (enc->codec_id == AV_CODEC_ID_MP2 ||
-        enc->codec_id == AV_CODEC_ID_MP3 ||
-        enc->codec_id == AV_CODEC_ID_GSM_MS) {
+    avio_wl16(pb, par->channels);
+    avio_wl32(pb, par->sample_rate);
+    if (par->codec_id == AV_CODEC_ID_MP2 ||
+        par->codec_id == AV_CODEC_ID_MP3 ||
+        par->codec_id == AV_CODEC_ID_GSM_MS) {
         bps = 0;
     } else {
-        if (!(bps = av_get_bits_per_sample(enc->codec_id))) {
-            if (enc->bits_per_coded_sample)
-                bps = enc->bits_per_coded_sample;
+        if (!(bps = av_get_bits_per_sample(par->codec_id))) {
+            if (par->bits_per_coded_sample)
+                bps = par->bits_per_coded_sample;
             else
                 bps = 16;  // default to 16
         }
     }
-    if (bps != enc->bits_per_coded_sample && enc->bits_per_coded_sample) {
-        av_log(enc, AV_LOG_WARNING,
+    if (bps != par->bits_per_coded_sample && par->bits_per_coded_sample) {
+        av_log(s, AV_LOG_WARNING,
                "requested bits_per_coded_sample (%d) "
                "and actually stored (%d) differ\n",
-               enc->bits_per_coded_sample, bps);
+               par->bits_per_coded_sample, bps);
     }
 
-    if (enc->codec_id == AV_CODEC_ID_MP2) {
+    if (par->codec_id == AV_CODEC_ID_MP2) {
         blkalign = frame_size;
-    } else if (enc->codec_id == AV_CODEC_ID_MP3) {
-        blkalign = 576 * (enc->sample_rate <= 24000 ? 1 : 2);
-    } else if (enc->codec_id == AV_CODEC_ID_AC3) {
+    } else if (par->codec_id == AV_CODEC_ID_MP3) {
+        blkalign = 576 * (par->sample_rate <= 24000 ? 1 : 2);
+    } else if (par->codec_id == AV_CODEC_ID_AC3) {
         blkalign = 3840;                /* maximum bytes per frame */
-    } else if (enc->block_align != 0) { /* specified by the codec */
-        blkalign = enc->block_align;
+    } else if (par->block_align != 0) { /* specified by the codec */
+        blkalign = par->block_align;
     } else
-        blkalign = bps * enc->channels / av_gcd(8, bps);
-    if (enc->codec_id == AV_CODEC_ID_PCM_U8 ||
-        enc->codec_id == AV_CODEC_ID_PCM_S24LE ||
-        enc->codec_id == AV_CODEC_ID_PCM_S32LE ||
-        enc->codec_id == AV_CODEC_ID_PCM_F32LE ||
-        enc->codec_id == AV_CODEC_ID_PCM_F64LE ||
-        enc->codec_id == AV_CODEC_ID_PCM_S16LE) {
-        bytespersec = enc->sample_rate * blkalign;
+        blkalign = bps * par->channels / av_gcd(8, bps);
+    if (par->codec_id == AV_CODEC_ID_PCM_U8 ||
+        par->codec_id == AV_CODEC_ID_PCM_S24LE ||
+        par->codec_id == AV_CODEC_ID_PCM_S32LE ||
+        par->codec_id == AV_CODEC_ID_PCM_F32LE ||
+        par->codec_id == AV_CODEC_ID_PCM_F64LE ||
+        par->codec_id == AV_CODEC_ID_PCM_S16LE) {
+        bytespersec = par->sample_rate * blkalign;
     } else {
-        bytespersec = enc->bit_rate / 8;
+        bytespersec = par->bit_rate / 8;
     }
     avio_wl32(pb, bytespersec); /* bytes per second */
     avio_wl16(pb, blkalign);    /* block align */
     avio_wl16(pb, bps);         /* bits per sample */
-    if (enc->codec_id == AV_CODEC_ID_MP3) {
+    if (par->codec_id == AV_CODEC_ID_MP3) {
         hdrsize += 12;
         bytestream_put_le16(&riff_extradata, 1);    /* wID */
         bytestream_put_le32(&riff_extradata, 2);    /* fdwFlags */
         bytestream_put_le16(&riff_extradata, 1152); /* nBlockSize */
         bytestream_put_le16(&riff_extradata, 1);    /* nFramesPerBlock */
         bytestream_put_le16(&riff_extradata, 1393); /* nCodecDelay */
-    } else if (enc->codec_id == AV_CODEC_ID_MP2) {
+    } else if (par->codec_id == AV_CODEC_ID_MP2) {
         hdrsize += 22;
         /* fwHeadLayer */
         bytestream_put_le16(&riff_extradata, 2);
         /* dwHeadBitrate */
-        bytestream_put_le32(&riff_extradata, enc->bit_rate);
+        bytestream_put_le32(&riff_extradata, par->bit_rate);
         /* fwHeadMode */
-        bytestream_put_le16(&riff_extradata, enc->channels == 2 ? 1 : 8);
+        bytestream_put_le16(&riff_extradata, par->channels == 2 ? 1 : 8);
         /* fwHeadModeExt */
         bytestream_put_le16(&riff_extradata, 0);
         /* wHeadEmphasis */
@@ -142,15 +143,15 @@ int ff_put_wav_header(AVIOContext *pb, AVCodecContext *enc)
         bytestream_put_le32(&riff_extradata, 0);
         /* dwPTSHigh */
         bytestream_put_le32(&riff_extradata, 0);
-    } else if (enc->codec_id == AV_CODEC_ID_GSM_MS ||
-               enc->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV) {
+    } else if (par->codec_id == AV_CODEC_ID_GSM_MS ||
+               par->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV) {
         hdrsize += 2;
         /* wSamplesPerBlock */
         bytestream_put_le16(&riff_extradata, frame_size);
-    } else if (enc->extradata_size) {
-        riff_extradata_start = enc->extradata;
-        riff_extradata       = enc->extradata + enc->extradata_size;
-        hdrsize             += enc->extradata_size;
+    } else if (par->extradata_size) {
+        riff_extradata_start = par->extradata;
+        riff_extradata       = par->extradata + par->extradata_size;
+        hdrsize             += par->extradata_size;
     }
     /* write WAVEFORMATEXTENSIBLE extensions */
     if (waveformatextensible) {
@@ -160,9 +161,9 @@ int ff_put_wav_header(AVIOContext *pb, AVCodecContext *enc)
         /* ValidBitsPerSample || SamplesPerBlock || Reserved */
         avio_wl16(pb, bps);
         /* dwChannelMask */
-        avio_wl32(pb, enc->channel_layout);
+        avio_wl32(pb, par->channel_layout);
         /* GUID + next 3 */
-        avio_wl32(pb, enc->codec_tag);
+        avio_wl32(pb, par->codec_tag);
         avio_wl32(pb, 0x00100000);
         avio_wl32(pb, 0xAA000080);
         avio_wl32(pb, 0x719B3800);
diff --git a/libavformat/wavenc.c b/libavformat/wavenc.c
index 25f6ffc..82cf282 100644
--- a/libavformat/wavenc.c
+++ b/libavformat/wavenc.c
@@ -109,7 +109,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(s, pb, s->streams[0]->codecpar) < 0) {
         const AVCodecDescriptor *desc = avcodec_descriptor_get(s->streams[0]->codec->codec_id);
         av_log(s, AV_LOG_ERROR, "%s codec not supported in WAVE format\n",
                desc ? desc->name : "unknown");
-- 
2.0.0



More information about the libav-devel mailing list