00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "avformat.h"
00026 #include "pcm.h"
00027 #include "riff.h"
00028
00029 typedef struct {
00030 int64_t data;
00031 int64_t data_end;
00032 int64_t minpts;
00033 int64_t maxpts;
00034 int last_duration;
00035 int w64;
00036 } WAVContext;
00037
00038 #if CONFIG_WAV_MUXER
00039 static int wav_write_header(AVFormatContext *s)
00040 {
00041 WAVContext *wav = s->priv_data;
00042 ByteIOContext *pb = s->pb;
00043 int64_t fmt, fact;
00044
00045 put_tag(pb, "RIFF");
00046 put_le32(pb, 0);
00047 put_tag(pb, "WAVE");
00048
00049
00050 fmt = ff_start_tag(pb, "fmt ");
00051 if (ff_put_wav_header(pb, s->streams[0]->codec) < 0) {
00052 av_log(s, AV_LOG_ERROR, "%s codec not supported in WAVE format\n",
00053 s->streams[0]->codec->codec ? s->streams[0]->codec->codec->name : "NONE");
00054 av_free(wav);
00055 return -1;
00056 }
00057 ff_end_tag(pb, fmt);
00058
00059 if (s->streams[0]->codec->codec_tag != 0x01
00060 && !url_is_streamed(s->pb)) {
00061 fact = ff_start_tag(pb, "fact");
00062 put_le32(pb, 0);
00063 ff_end_tag(pb, fact);
00064 }
00065
00066 av_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
00067 wav->maxpts = wav->last_duration = 0;
00068 wav->minpts = INT64_MAX;
00069
00070
00071 wav->data = ff_start_tag(pb, "data");
00072
00073 put_flush_packet(pb);
00074
00075 return 0;
00076 }
00077
00078 static int wav_write_packet(AVFormatContext *s, AVPacket *pkt)
00079 {
00080 ByteIOContext *pb = s->pb;
00081 WAVContext *wav = s->priv_data;
00082 put_buffer(pb, pkt->data, pkt->size);
00083 if(pkt->pts != AV_NOPTS_VALUE) {
00084 wav->minpts = FFMIN(wav->minpts, pkt->pts);
00085 wav->maxpts = FFMAX(wav->maxpts, pkt->pts);
00086 wav->last_duration = pkt->duration;
00087 } else
00088 av_log(s, AV_LOG_ERROR, "wav_write_packet: NOPTS\n");
00089 return 0;
00090 }
00091
00092 static int wav_write_trailer(AVFormatContext *s)
00093 {
00094 ByteIOContext *pb = s->pb;
00095 WAVContext *wav = s->priv_data;
00096 int64_t file_size;
00097
00098 put_flush_packet(pb);
00099
00100 if (!url_is_streamed(s->pb)) {
00101 ff_end_tag(pb, wav->data);
00102
00103
00104 file_size = url_ftell(pb);
00105 url_fseek(pb, 4, SEEK_SET);
00106 put_le32(pb, (uint32_t)(file_size - 8));
00107 url_fseek(pb, file_size, SEEK_SET);
00108
00109 put_flush_packet(pb);
00110
00111 if(s->streams[0]->codec->codec_tag != 0x01) {
00112
00113 int number_of_samples;
00114 number_of_samples = av_rescale(wav->maxpts - wav->minpts + wav->last_duration,
00115 s->streams[0]->codec->sample_rate * (int64_t)s->streams[0]->time_base.num,
00116 s->streams[0]->time_base.den);
00117 url_fseek(pb, wav->data-12, SEEK_SET);
00118 put_le32(pb, number_of_samples);
00119 url_fseek(pb, file_size, SEEK_SET);
00120 put_flush_packet(pb);
00121 }
00122 }
00123 return 0;
00124 }
00125
00126 AVOutputFormat wav_muxer = {
00127 "wav",
00128 NULL_IF_CONFIG_SMALL("WAV format"),
00129 "audio/x-wav",
00130 "wav",
00131 sizeof(WAVContext),
00132 CODEC_ID_PCM_S16LE,
00133 CODEC_ID_NONE,
00134 wav_write_header,
00135 wav_write_packet,
00136 wav_write_trailer,
00137 .codec_tag= (const AVCodecTag* const []){ff_codec_wav_tags, 0},
00138 };
00139 #endif
00140
00141
00142 #if CONFIG_WAV_DEMUXER
00143
00144 static int64_t next_tag(ByteIOContext *pb, unsigned int *tag)
00145 {
00146 *tag = get_le32(pb);
00147 return get_le32(pb);
00148 }
00149
00150
00151 static int64_t find_tag(ByteIOContext *pb, uint32_t tag1)
00152 {
00153 unsigned int tag;
00154 int64_t size;
00155
00156 for (;;) {
00157 if (url_feof(pb))
00158 return -1;
00159 size = next_tag(pb, &tag);
00160 if (tag == tag1)
00161 break;
00162 url_fseek(pb, size, SEEK_CUR);
00163 }
00164 return size;
00165 }
00166
00167 static int wav_probe(AVProbeData *p)
00168 {
00169
00170 if (p->buf_size <= 32)
00171 return 0;
00172 if (!memcmp(p->buf + 8, "WAVE", 4)) {
00173 if (!memcmp(p->buf, "RIFF", 4))
00174
00175
00176
00177
00178
00179 return AVPROBE_SCORE_MAX - 1;
00180 else if (!memcmp(p->buf, "RF64", 4) &&
00181 !memcmp(p->buf + 12, "ds64", 4))
00182 return AVPROBE_SCORE_MAX;
00183 }
00184 return 0;
00185 }
00186
00187
00188 static int wav_read_header(AVFormatContext *s,
00189 AVFormatParameters *ap)
00190 {
00191 int64_t size, av_uninit(data_size);
00192 int64_t sample_count=0;
00193 int rf64;
00194 unsigned int tag;
00195 ByteIOContext *pb = s->pb;
00196 AVStream *st;
00197 WAVContext *wav = s->priv_data;
00198
00199
00200 tag = get_le32(pb);
00201
00202 rf64 = tag == MKTAG('R', 'F', '6', '4');
00203 if (!rf64 && tag != MKTAG('R', 'I', 'F', 'F'))
00204 return -1;
00205 get_le32(pb);
00206 tag = get_le32(pb);
00207 if (tag != MKTAG('W', 'A', 'V', 'E'))
00208 return -1;
00209
00210 if (rf64) {
00211 if (get_le32(pb) != MKTAG('d', 's', '6', '4'))
00212 return -1;
00213 size = get_le32(pb);
00214 if (size < 16)
00215 return -1;
00216 get_le64(pb);
00217 data_size = get_le64(pb);
00218 sample_count = get_le64(pb);
00219 url_fskip(pb, size - 16);
00220 }
00221
00222
00223 size = find_tag(pb, MKTAG('f', 'm', 't', ' '));
00224 if (size < 0)
00225 return -1;
00226 st = av_new_stream(s, 0);
00227 if (!st)
00228 return AVERROR(ENOMEM);
00229
00230 ff_get_wav_header(pb, st->codec, size);
00231 st->need_parsing = AVSTREAM_PARSE_FULL;
00232
00233 av_set_pts_info(st, 64, 1, st->codec->sample_rate);
00234
00235 for (;;) {
00236 if (url_feof(pb))
00237 return -1;
00238 size = next_tag(pb, &tag);
00239 if (tag == MKTAG('d', 'a', 't', 'a')){
00240 break;
00241 }else if (tag == MKTAG('f','a','c','t') && !sample_count){
00242 sample_count = get_le32(pb);
00243 size -= 4;
00244 }
00245 url_fseek(pb, size, SEEK_CUR);
00246 }
00247 if (rf64)
00248 size = data_size;
00249 if (size < 0)
00250 return -1;
00251 if (!size) {
00252 wav->data_end = INT64_MAX;
00253 } else
00254 wav->data_end= url_ftell(pb) + size;
00255
00256 if (!sample_count && st->codec->channels && av_get_bits_per_sample(st->codec->codec_id))
00257 sample_count = (size<<3) / (st->codec->channels * (uint64_t)av_get_bits_per_sample(st->codec->codec_id));
00258 if (sample_count)
00259 st->duration = sample_count;
00260 return 0;
00261 }
00262
00266 static int64_t find_guid(ByteIOContext *pb, const uint8_t guid1[16])
00267 {
00268 uint8_t guid[16];
00269 int64_t size;
00270
00271 while (!url_feof(pb)) {
00272 get_buffer(pb, guid, 16);
00273 size = get_le64(pb);
00274 if (size <= 24)
00275 return -1;
00276 if (!memcmp(guid, guid1, 16))
00277 return size;
00278 url_fskip(pb, FFALIGN(size, INT64_C(8)) - 24);
00279 }
00280 return -1;
00281 }
00282
00283 static const uint8_t guid_data[16] = { 'd', 'a', 't', 'a',
00284 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
00285
00286 #define MAX_SIZE 4096
00287
00288 static int wav_read_packet(AVFormatContext *s,
00289 AVPacket *pkt)
00290 {
00291 int ret, size;
00292 int64_t left;
00293 AVStream *st;
00294 WAVContext *wav = s->priv_data;
00295
00296 st = s->streams[0];
00297
00298 left = wav->data_end - url_ftell(s->pb);
00299 if (left <= 0){
00300 if (CONFIG_W64_DEMUXER && wav->w64)
00301 left = find_guid(s->pb, guid_data) - 24;
00302 else
00303 left = find_tag(s->pb, MKTAG('d', 'a', 't', 'a'));
00304 if (left < 0)
00305 return AVERROR_EOF;
00306 wav->data_end= url_ftell(s->pb) + left;
00307 }
00308
00309 size = MAX_SIZE;
00310 if (st->codec->block_align > 1) {
00311 if (size < st->codec->block_align)
00312 size = st->codec->block_align;
00313 size = (size / st->codec->block_align) * st->codec->block_align;
00314 }
00315 size = FFMIN(size, left);
00316 ret = av_get_packet(s->pb, pkt, size);
00317 if (ret < 0)
00318 return ret;
00319 pkt->stream_index = 0;
00320
00321 return ret;
00322 }
00323
00324 static int wav_read_seek(AVFormatContext *s,
00325 int stream_index, int64_t timestamp, int flags)
00326 {
00327 AVStream *st;
00328
00329 st = s->streams[0];
00330 switch (st->codec->codec_id) {
00331 case CODEC_ID_MP2:
00332 case CODEC_ID_MP3:
00333 case CODEC_ID_AC3:
00334 case CODEC_ID_DTS:
00335
00336 return -1;
00337 default:
00338 break;
00339 }
00340 return pcm_read_seek(s, stream_index, timestamp, flags);
00341 }
00342
00343 AVInputFormat wav_demuxer = {
00344 "wav",
00345 NULL_IF_CONFIG_SMALL("WAV format"),
00346 sizeof(WAVContext),
00347 wav_probe,
00348 wav_read_header,
00349 wav_read_packet,
00350 NULL,
00351 wav_read_seek,
00352 .flags= AVFMT_GENERIC_INDEX,
00353 .codec_tag= (const AVCodecTag* const []){ff_codec_wav_tags, 0},
00354 };
00355 #endif
00356
00357
00358 #if CONFIG_W64_DEMUXER
00359 static const uint8_t guid_riff[16] = { 'r', 'i', 'f', 'f',
00360 0x2E, 0x91, 0xCF, 0x11, 0xA5, 0xD6, 0x28, 0xDB, 0x04, 0xC1, 0x00, 0x00 };
00361
00362 static const uint8_t guid_wave[16] = { 'w', 'a', 'v', 'e',
00363 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
00364
00365 static const uint8_t guid_fmt [16] = { 'f', 'm', 't', ' ',
00366 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
00367
00368 static int w64_probe(AVProbeData *p)
00369 {
00370 if (p->buf_size <= 40)
00371 return 0;
00372 if (!memcmp(p->buf, guid_riff, 16) &&
00373 !memcmp(p->buf + 24, guid_wave, 16))
00374 return AVPROBE_SCORE_MAX;
00375 else
00376 return 0;
00377 }
00378
00379 static int w64_read_header(AVFormatContext *s, AVFormatParameters *ap)
00380 {
00381 int64_t size;
00382 ByteIOContext *pb = s->pb;
00383 WAVContext *wav = s->priv_data;
00384 AVStream *st;
00385 uint8_t guid[16];
00386
00387 get_buffer(pb, guid, 16);
00388 if (memcmp(guid, guid_riff, 16))
00389 return -1;
00390
00391 if (get_le64(pb) < 16 + 8 + 16 + 8 + 16 + 8)
00392 return -1;
00393
00394 get_buffer(pb, guid, 16);
00395 if (memcmp(guid, guid_wave, 16)) {
00396 av_log(s, AV_LOG_ERROR, "could not find wave guid\n");
00397 return -1;
00398 }
00399
00400 size = find_guid(pb, guid_fmt);
00401 if (size < 0) {
00402 av_log(s, AV_LOG_ERROR, "could not find fmt guid\n");
00403 return -1;
00404 }
00405
00406 st = av_new_stream(s, 0);
00407 if (!st)
00408 return AVERROR(ENOMEM);
00409
00410
00411 ff_get_wav_header(pb, st->codec, size - 24);
00412 url_fskip(pb, FFALIGN(size, INT64_C(8)) - size);
00413
00414 st->need_parsing = AVSTREAM_PARSE_FULL;
00415
00416 av_set_pts_info(st, 64, 1, st->codec->sample_rate);
00417
00418 size = find_guid(pb, guid_data);
00419 if (size < 0) {
00420 av_log(s, AV_LOG_ERROR, "could not find data guid\n");
00421 return -1;
00422 }
00423 wav->data_end = url_ftell(pb) + size - 24;
00424 wav->w64 = 1;
00425
00426 return 0;
00427 }
00428
00429 AVInputFormat w64_demuxer = {
00430 "w64",
00431 NULL_IF_CONFIG_SMALL("Sony Wave64 format"),
00432 sizeof(WAVContext),
00433 w64_probe,
00434 w64_read_header,
00435 wav_read_packet,
00436 NULL,
00437 wav_read_seek,
00438 .flags = AVFMT_GENERIC_INDEX,
00439 .codec_tag = (const AVCodecTag* const []){ff_codec_wav_tags, 0},
00440 };
00441 #endif