X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavformat%2Fwav.c;h=a6db698aa53dcbe12ed225efb8fc3c394b8a025e;hb=15025553627082053c3a756937185cef13ba37df;hp=cc28ec5e3192d7b05c7118e61c9507bc6d22043d;hpb=9d9f4119bd17cdfb3409dfa2d68252c16444431d;p=ffmpeg diff --git a/libavformat/wav.c b/libavformat/wav.c index cc28ec5e319..a6db698aa53 100644 --- a/libavformat/wav.c +++ b/libavformat/wav.c @@ -1,19 +1,21 @@ /* - * WAV encoder and decoder + * WAV muxer and demuxer * Copyright (c) 2001, 2002 Fabrice Bellard. * - * This library is free software; you can redistribute it and/or + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. + * version 2.1 of the License, or (at your option) any later version. * - * This library is distributed in the hope that it will be useful, + * FFmpeg is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software + * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #include "avformat.h" @@ -23,6 +25,9 @@ typedef struct { offset_t data; offset_t data_end; + int64_t minpts; + int64_t maxpts; + int last_duration; } WAVContext; #ifdef CONFIG_MUXERS @@ -30,7 +35,7 @@ static int wav_write_header(AVFormatContext *s) { WAVContext *wav = s->priv_data; ByteIOContext *pb = &s->pb; - offset_t fmt; + offset_t fmt, fact; put_tag(pb, "RIFF"); put_le32(pb, 0); /* file length */ @@ -44,7 +49,16 @@ static int wav_write_header(AVFormatContext *s) } end_tag(pb, fmt); + if(s->streams[0]->codec->codec_tag != 0x01 /* hence for all other than PCM */ + && !url_is_streamed(&s->pb)) { + fact = start_tag(pb, "fact"); + put_le32(pb, 0); + end_tag(pb, fact); + } + av_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate); + wav->maxpts = wav->last_duration = 0; + wav->minpts = INT64_MAX; /* data header */ wav->data = start_tag(pb, "data"); @@ -57,7 +71,14 @@ static int wav_write_header(AVFormatContext *s) static int wav_write_packet(AVFormatContext *s, AVPacket *pkt) { ByteIOContext *pb = &s->pb; + WAVContext *wav = s->priv_data; put_buffer(pb, pkt->data, pkt->size); + if(pkt->pts != AV_NOPTS_VALUE) { + wav->minpts = FFMIN(wav->minpts, pkt->pts); + wav->maxpts = FFMAX(wav->maxpts, pkt->pts); + wav->last_duration = pkt->duration; + } else + av_log(s, AV_LOG_ERROR, "wav_write_packet: NOPTS\n"); return 0; } @@ -77,6 +98,18 @@ static int wav_write_trailer(AVFormatContext *s) url_fseek(pb, file_size, SEEK_SET); put_flush_packet(pb); + + if(s->streams[0]->codec->codec_tag != 0x01) { + /* Update num_samps in fact chunk */ + int number_of_samples; + number_of_samples = av_rescale(wav->maxpts - wav->minpts + wav->last_duration, + s->streams[0]->codec->sample_rate * (int64_t)s->streams[0]->time_base.num, + s->streams[0]->time_base.den); + url_fseek(pb, wav->data-12, SEEK_SET); + put_le32(pb, number_of_samples); + url_fseek(pb, file_size, SEEK_SET); + put_flush_packet(pb); + } } return 0; } @@ -146,7 +179,7 @@ static int wav_read_header(AVFormatContext *s, return AVERROR_NOMEM; get_wav_header(pb, st->codec, size); - st->need_parsing = 1; + st->need_parsing = AVSTREAM_PARSE_FULL; av_set_pts_info(st, 64, 1, st->codec->sample_rate); @@ -186,13 +219,11 @@ static int wav_read_packet(AVFormatContext *s, size = (size / st->codec->block_align) * st->codec->block_align; } size= FFMIN(size, left); - if (av_new_packet(pkt, size)) + ret= av_get_packet(&s->pb, pkt, size); + if (ret <= 0) return AVERROR_IO; pkt->stream_index = 0; - ret = get_buffer(&s->pb, pkt->data, pkt->size); - if (ret < 0) - av_free_packet(pkt); /* note: we need to modify the packet size here to handle the last packet */ pkt->size = ret; @@ -233,6 +264,8 @@ AVInputFormat wav_demuxer = { wav_read_packet, wav_read_close, wav_read_seek, + .flags= AVFMT_GENERIC_INDEX, + .codec_tag= (const AVCodecTag*[]){codec_wav_tags, 0}, }; #endif #ifdef CONFIG_WAV_MUXER @@ -247,5 +280,6 @@ AVOutputFormat wav_muxer = { wav_write_header, wav_write_packet, wav_write_trailer, + .codec_tag= (const AVCodecTag*[]){codec_wav_tags, 0}, }; #endif