00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <stdint.h>
00023 #include <string.h>
00024
00025 #include "libavutil/dict.h"
00026 #include "libavutil/common.h"
00027 #include "libavutil/mathematics.h"
00028 #include "libavutil/opt.h"
00029
00030 #include "avformat.h"
00031 #include "avio.h"
00032 #include "avio_internal.h"
00033 #include "internal.h"
00034 #include "riff.h"
00035
00036 typedef struct WAVMuxContext {
00037 const AVClass *class;
00038 int64_t data;
00039 int64_t fact_pos;
00040 int64_t minpts;
00041 int64_t maxpts;
00042 int last_duration;
00043 int write_bext;
00044 } WAVMuxContext;
00045
00046 static inline void bwf_write_bext_string(AVFormatContext *s, const char *key, int maxlen)
00047 {
00048 AVDictionaryEntry *tag;
00049 int len = 0;
00050
00051 if (tag = av_dict_get(s->metadata, key, NULL, 0)) {
00052 len = strlen(tag->value);
00053 len = FFMIN(len, maxlen);
00054 avio_write(s->pb, tag->value, len);
00055 }
00056
00057 ffio_fill(s->pb, 0, maxlen - len);
00058 }
00059
00060 static void bwf_write_bext_chunk(AVFormatContext *s)
00061 {
00062 AVDictionaryEntry *tmp_tag;
00063 uint64_t time_reference = 0;
00064 int64_t bext = ff_start_tag(s->pb, "bext");
00065
00066 bwf_write_bext_string(s, "description", 256);
00067 bwf_write_bext_string(s, "originator", 32);
00068 bwf_write_bext_string(s, "originator_reference", 32);
00069 bwf_write_bext_string(s, "origination_date", 10);
00070 bwf_write_bext_string(s, "origination_time", 8);
00071
00072 if (tmp_tag = av_dict_get(s->metadata, "time_reference", NULL, 0))
00073 time_reference = strtoll(tmp_tag->value, NULL, 10);
00074 avio_wl64(s->pb, time_reference);
00075 avio_wl16(s->pb, 1);
00076
00077 if (tmp_tag = av_dict_get(s->metadata, "umid", NULL, 0)) {
00078 unsigned char umidpart_str[17] = {0};
00079 int i;
00080 uint64_t umidpart;
00081 int len = strlen(tmp_tag->value+2);
00082
00083 for (i = 0; i < len/16; i++) {
00084 memcpy(umidpart_str, tmp_tag->value + 2 + (i*16), 16);
00085 umidpart = strtoll(umidpart_str, NULL, 16);
00086 avio_wb64(s->pb, umidpart);
00087 }
00088 ffio_fill(s->pb, 0, 64 - i*8);
00089 } else
00090 ffio_fill(s->pb, 0, 64);
00091
00092 ffio_fill(s->pb, 0, 190);
00093
00094 if (tmp_tag = av_dict_get(s->metadata, "coding_history", NULL, 0))
00095 avio_put_str(s->pb, tmp_tag->value);
00096
00097 ff_end_tag(s->pb, bext);
00098 }
00099
00100 static int wav_write_header(AVFormatContext *s)
00101 {
00102 WAVMuxContext *wav = s->priv_data;
00103 AVIOContext *pb = s->pb;
00104 int64_t fmt;
00105
00106 ffio_wfourcc(pb, "RIFF");
00107 avio_wl32(pb, 0);
00108 ffio_wfourcc(pb, "WAVE");
00109
00110
00111 fmt = ff_start_tag(pb, "fmt ");
00112 if (ff_put_wav_header(pb, s->streams[0]->codec) < 0) {
00113 av_log(s, AV_LOG_ERROR, "%s codec not supported in WAVE format\n",
00114 s->streams[0]->codec->codec ? s->streams[0]->codec->codec->name : "NONE");
00115 return -1;
00116 }
00117 ff_end_tag(pb, fmt);
00118
00119 if (s->streams[0]->codec->codec_tag != 0x01
00120 && s->pb->seekable) {
00121 wav->fact_pos = ff_start_tag(pb, "fact");
00122 avio_wl32(pb, 0);
00123 ff_end_tag(pb, wav->fact_pos);
00124 }
00125
00126 if (wav->write_bext)
00127 bwf_write_bext_chunk(s);
00128
00129 avpriv_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
00130 wav->maxpts = wav->last_duration = 0;
00131 wav->minpts = INT64_MAX;
00132
00133
00134 ff_riff_write_info(s);
00135
00136
00137 wav->data = ff_start_tag(pb, "data");
00138
00139 avio_flush(pb);
00140
00141 return 0;
00142 }
00143
00144 static int wav_write_packet(AVFormatContext *s, AVPacket *pkt)
00145 {
00146 AVIOContext *pb = s->pb;
00147 WAVMuxContext *wav = s->priv_data;
00148 avio_write(pb, pkt->data, pkt->size);
00149 if(pkt->pts != AV_NOPTS_VALUE) {
00150 wav->minpts = FFMIN(wav->minpts, pkt->pts);
00151 wav->maxpts = FFMAX(wav->maxpts, pkt->pts);
00152 wav->last_duration = pkt->duration;
00153 } else
00154 av_log(s, AV_LOG_ERROR, "wav_write_packet: NOPTS\n");
00155 return 0;
00156 }
00157
00158 static int wav_write_trailer(AVFormatContext *s)
00159 {
00160 AVIOContext *pb = s->pb;
00161 WAVMuxContext *wav = s->priv_data;
00162 int64_t file_size;
00163
00164 avio_flush(pb);
00165
00166 if (s->pb->seekable) {
00167 ff_end_tag(pb, wav->data);
00168
00169
00170 file_size = avio_tell(pb);
00171 avio_seek(pb, 4, SEEK_SET);
00172 avio_wl32(pb, (uint32_t)(file_size - 8));
00173 avio_seek(pb, file_size, SEEK_SET);
00174
00175 avio_flush(pb);
00176
00177 if(s->streams[0]->codec->codec_tag != 0x01) {
00178
00179 int number_of_samples;
00180 number_of_samples = av_rescale(wav->maxpts - wav->minpts + wav->last_duration,
00181 s->streams[0]->codec->sample_rate * (int64_t)s->streams[0]->time_base.num,
00182 s->streams[0]->time_base.den);
00183 avio_seek(pb, wav->fact_pos, SEEK_SET);
00184 avio_wl32(pb, number_of_samples);
00185 avio_seek(pb, file_size, SEEK_SET);
00186 avio_flush(pb);
00187 }
00188 }
00189 return 0;
00190 }
00191
00192 #define OFFSET(x) offsetof(WAVMuxContext, x)
00193 #define ENC AV_OPT_FLAG_ENCODING_PARAM
00194 static const AVOption options[] = {
00195 { "write_bext", "Write BEXT chunk.", OFFSET(write_bext), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, ENC },
00196 { NULL },
00197 };
00198
00199 static const AVClass wav_muxer_class = {
00200 .class_name = "WAV muxer",
00201 .item_name = av_default_item_name,
00202 .option = options,
00203 .version = LIBAVUTIL_VERSION_INT,
00204 };
00205
00206 AVOutputFormat ff_wav_muxer = {
00207 .name = "wav",
00208 .long_name = NULL_IF_CONFIG_SMALL("WAV / WAVE (Waveform Audio)"),
00209 .mime_type = "audio/x-wav",
00210 .extensions = "wav",
00211 .priv_data_size = sizeof(WAVMuxContext),
00212 .audio_codec = AV_CODEC_ID_PCM_S16LE,
00213 .video_codec = AV_CODEC_ID_NONE,
00214 .write_header = wav_write_header,
00215 .write_packet = wav_write_packet,
00216 .write_trailer = wav_write_trailer,
00217 .flags = AVFMT_TS_NONSTRICT,
00218 .codec_tag = (const AVCodecTag* const []){ ff_codec_wav_tags, 0 },
00219 .priv_class = &wav_muxer_class,
00220 };