3 * Copyright (c) 2003 Fabrice Bellard
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavutil/mathematics.h"
27 #define RAW_SAMPLES 1024
29 int ff_pcm_read_packet(AVFormatContext *s, AVPacket *pkt)
31 AVCodecParameters *par = s->streams[0]->codecpar;
34 if (par->block_align <= 0)
35 return AVERROR(EINVAL);
38 * Compute read size to complete a read every 62ms.
39 * Clamp to RAW_SAMPLES if larger.
41 size = FFMAX(par->sample_rate/25, 1);
42 if (par->block_align <= INT_MAX / RAW_SAMPLES) {
43 size = FFMIN(size, RAW_SAMPLES) * par->block_align;
45 size = par->block_align;
48 ret = av_get_packet(s->pb, pkt, size);
50 pkt->flags &= ~AV_PKT_FLAG_CORRUPT;
51 pkt->stream_index = 0;
56 int ff_pcm_read_seek(AVFormatContext *s,
57 int stream_index, int64_t timestamp, int flags)
60 int block_align, byte_rate;
65 block_align = st->codecpar->block_align ? st->codecpar->block_align :
66 (av_get_bits_per_sample(st->codecpar->codec_id) * st->codecpar->channels) >> 3;
67 byte_rate = st->codecpar->bit_rate ? st->codecpar->bit_rate >> 3 :
68 block_align * st->codecpar->sample_rate;
70 if (block_align <= 0 || byte_rate <= 0)
72 if (timestamp < 0) timestamp = 0;
74 /* compute the position by aligning it to block_align */
75 pos = av_rescale_rnd(timestamp * byte_rate,
77 st->time_base.den * (int64_t)block_align,
78 (flags & AVSEEK_FLAG_BACKWARD) ? AV_ROUND_DOWN : AV_ROUND_UP);
81 /* recompute exact position */
82 st->cur_dts = av_rescale(pos, st->time_base.den, byte_rate * (int64_t)st->time_base.num);
83 if ((ret = avio_seek(s->pb, pos + s->internal->data_offset, SEEK_SET)) < 0)