FFmpeg
pcm.c
Go to the documentation of this file.
1 /*
2  * PCM common functions
3  * Copyright (c) 2003 Fabrice Bellard
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21 
22 #include "libavutil/mathematics.h"
23 #include "avformat.h"
24 #include "internal.h"
25 #include "pcm.h"
26 
27 #define PCM_DEMUX_TARGET_FPS 10
28 
30 {
31  int nb_samples, max_samples, bits_per_sample;
33 
34  if (par->block_align <= 0)
35  return AVERROR(EINVAL);
36 
37  max_samples = INT_MAX / par->block_align;
38  bits_per_sample = av_get_bits_per_sample(par->codec_id);
39  bitrate = par->bit_rate;
40 
41  /* Don't trust the codecpar bitrate if we can calculate it ourselves */
42  if (bits_per_sample > 0 && par->sample_rate > 0 && par->ch_layout.nb_channels > 0)
43  if ((int64_t)par->sample_rate * par->ch_layout.nb_channels < INT64_MAX / bits_per_sample)
44  bitrate = bits_per_sample * (int64_t)par->sample_rate * par->ch_layout.nb_channels;
45 
46  if (bitrate > 0) {
47  nb_samples = av_clip64(bitrate / 8 / PCM_DEMUX_TARGET_FPS / par->block_align, 1, max_samples);
48  nb_samples = 1 << av_log2(nb_samples);
49  } else {
50  /* Fallback to a size based method for a non-pcm codec with unknown bitrate */
51  nb_samples = av_clip(4096 / par->block_align, 1, max_samples);
52  }
53 
54  return par->block_align * nb_samples;
55 }
56 
58 {
59  int ret, size;
60 
61  size = ff_pcm_default_packet_size(s->streams[0]->codecpar);
62  if (size < 0)
63  return size;
64 
65  ret = av_get_packet(s->pb, pkt, size);
66 
68  pkt->stream_index = 0;
69 
70  return ret;
71 }
72 
74  int stream_index, int64_t timestamp, int flags)
75 {
76  AVStream *st;
77  int block_align, byte_rate;
78  int64_t pos, ret;
79 
80  st = s->streams[0];
81 
82  block_align = st->codecpar->block_align ? st->codecpar->block_align :
84  byte_rate = st->codecpar->bit_rate ? st->codecpar->bit_rate >> 3 :
85  block_align * st->codecpar->sample_rate;
86 
87  if (block_align <= 0 || byte_rate <= 0)
88  return -1;
89  if (timestamp < 0) timestamp = 0;
90 
91  /* compute the position by aligning it to block_align */
92  pos = av_rescale_rnd(timestamp * byte_rate,
93  st->time_base.num,
94  st->time_base.den * (int64_t)block_align,
96  pos *= block_align;
97 
98  /* recompute exact position */
99  ffstream(st)->cur_dts = av_rescale(pos, st->time_base.den, byte_rate * (int64_t)st->time_base.num);
100  if ((ret = avio_seek(s->pb, pos + ffformatcontext(s)->data_offset, SEEK_SET)) < 0)
101  return ret;
102  return 0;
103 }
AV_ROUND_UP
@ AV_ROUND_UP
Round toward +infinity.
Definition: mathematics.h:134
av_clip
#define av_clip
Definition: common.h:100
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
pcm.h
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: codec_par.h:47
ffformatcontext
static av_always_inline FFFormatContext * ffformatcontext(AVFormatContext *s)
Definition: internal.h:127
int64_t
long long int64_t
Definition: coverity.c:34
mathematics.h
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:327
ffstream
static av_always_inline FFStream * ffstream(AVStream *st)
Definition: internal.h:358
AVRational::num
int num
Numerator.
Definition: rational.h:59
av_clip64
#define av_clip64
Definition: common.h:103
av_get_bits_per_sample
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition: utils.c:550
pkt
AVPacket * pkt
Definition: movenc.c:60
AV_PKT_FLAG_CORRUPT
#define AV_PKT_FLAG_CORRUPT
The packet content is corrupted.
Definition: packet.h:595
s
#define s(width, name)
Definition: cbs_vp9.c:198
bitrate
int64_t bitrate
Definition: av1_levels.c:47
if
if(ret)
Definition: filter_design.txt:179
AVFormatContext
Format I/O context.
Definition: avformat.h:1300
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:771
AVSEEK_FLAG_BACKWARD
#define AVSEEK_FLAG_BACKWARD
Definition: avformat.h:2498
AVStream::time_base
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition: avformat.h:787
ff_pcm_default_packet_size
int ff_pcm_default_packet_size(AVCodecParameters *par)
Definition: pcm.c:29
AVCodecParameters::ch_layout
AVChannelLayout ch_layout
Audio only.
Definition: codec_par.h:180
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:184
AV_ROUND_DOWN
@ AV_ROUND_DOWN
Round toward -infinity.
Definition: mathematics.h:133
av_rescale_rnd
int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding rnd)
Rescale a 64-bit integer with specified rounding.
Definition: mathematics.c:58
size
int size
Definition: twinvq_data.h:10344
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:545
AVCodecParameters::block_align
int block_align
Audio only.
Definition: codec_par.h:191
ff_pcm_read_packet
int ff_pcm_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: pcm.c:57
av_rescale
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
Definition: mathematics.c:129
av_get_packet
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition: utils.c:91
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:748
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:231
ff_pcm_read_seek
int ff_pcm_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Definition: pcm.c:73
PCM_DEMUX_TARGET_FPS
#define PCM_DEMUX_TARGET_FPS
Definition: pcm.c:27
pos
unsigned int pos
Definition: spdifenc.c:414
avformat.h
AVRational::den
int den
Denominator.
Definition: rational.h:60
AVPacket::stream_index
int stream_index
Definition: packet.h:541
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:55
AVPacket
This structure stores compressed data.
Definition: packet.h:516
FFStream::cur_dts
int64_t cur_dts
Definition: internal.h:349
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:482
AVCodecParameters::bit_rate
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: codec_par.h:97
av_log2
int av_log2(unsigned v)
Definition: intmath.c:26