2010-08-30 21:17:34 +00:00
|
|
|
/*
|
|
|
|
|
* PCM common functions
|
|
|
|
|
* Copyright (c) 2003 Fabrice Bellard
|
|
|
|
|
*
|
|
|
|
|
* 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.1 of the License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* 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 FFmpeg; if not, write to the Free Software
|
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
|
*/
|
|
|
|
|
|
2011-06-04 12:58:23 +01:00
|
|
|
#include "libavutil/mathematics.h"
|
2010-08-30 21:17:34 +00:00
|
|
|
#include "avformat.h"
|
2015-02-06 14:53:40 +01:00
|
|
|
#include "internal.h"
|
2010-08-30 21:17:34 +00:00
|
|
|
#include "pcm.h"
|
|
|
|
|
|
2024-03-04 00:27:11 +01:00
|
|
|
#define PCM_DEMUX_TARGET_FPS 10
|
2012-12-03 17:22:21 +00:00
|
|
|
|
2024-03-02 22:56:35 +01:00
|
|
|
int ff_pcm_default_packet_size(AVCodecParameters *par)
|
2012-12-03 17:22:21 +00:00
|
|
|
{
|
2024-03-02 22:56:35 +01:00
|
|
|
int nb_samples, max_samples, bits_per_sample;
|
|
|
|
|
int64_t bitrate;
|
2012-12-03 17:22:21 +00:00
|
|
|
|
2018-03-10 15:50:06 +01:00
|
|
|
if (par->block_align <= 0)
|
2012-12-07 13:05:43 +00:00
|
|
|
return AVERROR(EINVAL);
|
2012-12-03 17:22:21 +00:00
|
|
|
|
2024-03-02 22:56:35 +01:00
|
|
|
max_samples = INT_MAX / par->block_align;
|
|
|
|
|
bits_per_sample = av_get_bits_per_sample(par->codec_id);
|
|
|
|
|
bitrate = par->bit_rate;
|
|
|
|
|
|
|
|
|
|
/* Don't trust the codecpar bitrate if we can calculate it ourselves */
|
|
|
|
|
if (bits_per_sample > 0 && par->sample_rate > 0 && par->ch_layout.nb_channels > 0)
|
|
|
|
|
if ((int64_t)par->sample_rate * par->ch_layout.nb_channels < INT64_MAX / bits_per_sample)
|
2024-04-04 00:42:20 +02:00
|
|
|
bitrate = bits_per_sample * (int64_t)par->sample_rate * par->ch_layout.nb_channels;
|
2024-03-02 22:56:35 +01:00
|
|
|
|
|
|
|
|
if (bitrate > 0) {
|
|
|
|
|
nb_samples = av_clip64(bitrate / 8 / PCM_DEMUX_TARGET_FPS / par->block_align, 1, max_samples);
|
|
|
|
|
nb_samples = 1 << av_log2(nb_samples);
|
2020-10-20 21:44:32 +02:00
|
|
|
} else {
|
2024-03-02 22:56:35 +01:00
|
|
|
/* Fallback to a size based method for a non-pcm codec with unknown bitrate */
|
|
|
|
|
nb_samples = av_clip(4096 / par->block_align, 1, max_samples);
|
2020-10-20 21:44:32 +02:00
|
|
|
}
|
2018-03-10 15:50:06 +01:00
|
|
|
|
2024-03-02 22:56:35 +01:00
|
|
|
return par->block_align * nb_samples;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int ff_pcm_read_packet(AVFormatContext *s, AVPacket *pkt)
|
|
|
|
|
{
|
|
|
|
|
int ret, size;
|
|
|
|
|
|
|
|
|
|
size = ff_pcm_default_packet_size(s->streams[0]->codecpar);
|
|
|
|
|
if (size < 0)
|
|
|
|
|
return size;
|
|
|
|
|
|
2018-03-10 15:50:06 +01:00
|
|
|
ret = av_get_packet(s->pb, pkt, size);
|
2012-12-03 17:22:21 +00:00
|
|
|
|
|
|
|
|
pkt->flags &= ~AV_PKT_FLAG_CORRUPT;
|
|
|
|
|
pkt->stream_index = 0;
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-15 11:28:39 +02:00
|
|
|
int ff_pcm_read_seek(AVFormatContext *s,
|
|
|
|
|
int stream_index, int64_t timestamp, int flags)
|
2010-08-30 21:17:34 +00:00
|
|
|
{
|
|
|
|
|
AVStream *st;
|
2026-05-02 13:47:22 +02:00
|
|
|
int block_align;
|
|
|
|
|
int64_t byte_rate;
|
2010-08-30 21:17:34 +00:00
|
|
|
int64_t pos, ret;
|
|
|
|
|
|
|
|
|
|
st = s->streams[0];
|
|
|
|
|
|
2014-06-18 20:42:52 +02:00
|
|
|
block_align = st->codecpar->block_align ? st->codecpar->block_align :
|
2017-03-31 18:06:05 +02:00
|
|
|
(av_get_bits_per_sample(st->codecpar->codec_id) * st->codecpar->ch_layout.nb_channels) >> 3;
|
2014-06-18 20:42:52 +02:00
|
|
|
byte_rate = st->codecpar->bit_rate ? st->codecpar->bit_rate >> 3 :
|
2026-05-02 13:47:22 +02:00
|
|
|
block_align * (int64_t)st->codecpar->sample_rate;
|
2010-08-30 21:17:34 +00:00
|
|
|
|
2026-05-02 13:47:22 +02:00
|
|
|
if (block_align <= 0 || byte_rate <= 0 || FFMAX(timestamp, st->time_base.num) > INT64_MAX / byte_rate)
|
2010-08-30 21:17:34 +00:00
|
|
|
return -1;
|
|
|
|
|
if (timestamp < 0) timestamp = 0;
|
|
|
|
|
|
|
|
|
|
/* compute the position by aligning it to block_align */
|
|
|
|
|
pos = av_rescale_rnd(timestamp * byte_rate,
|
|
|
|
|
st->time_base.num,
|
|
|
|
|
st->time_base.den * (int64_t)block_align,
|
|
|
|
|
(flags & AVSEEK_FLAG_BACKWARD) ? AV_ROUND_DOWN : AV_ROUND_UP);
|
2026-05-02 13:47:22 +02:00
|
|
|
|
|
|
|
|
if (pos > (INT64_MAX - FFMAX(ffformatcontext(s)->data_offset, 0)) / block_align)
|
|
|
|
|
return -1;
|
2010-08-30 21:17:34 +00:00
|
|
|
pos *= block_align;
|
|
|
|
|
|
|
|
|
|
/* recompute exact position */
|
2021-08-24 19:41:16 +02:00
|
|
|
ffstream(st)->cur_dts = av_rescale(pos, st->time_base.den, byte_rate * (int64_t)st->time_base.num);
|
2021-08-24 14:58:07 +02:00
|
|
|
if ((ret = avio_seek(s->pb, pos + ffformatcontext(s)->data_offset, SEEK_SET)) < 0)
|
2010-08-30 21:17:34 +00:00
|
|
|
return ret;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|