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
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  */
22 #include "libavutil/mathematics.h"
23 #include "avformat.h"
24 #include "internal.h"
25 #include "pcm.h"
27 #define RAW_SAMPLES 1024
30 {
31  AVCodecParameters *par = s->streams[0]->codecpar;
32  int ret, size;
34  if (par->block_align <= 0)
35  return AVERROR(EINVAL);
37  /*
38  * Compute read size to complete a read every 62ms.
39  * Clamp to RAW_SAMPLES if larger.
40  */
41  size = FFMAX(par->sample_rate/25, 1);
42  size = FFMIN(size, RAW_SAMPLES) * par->block_align;
44  ret = av_get_packet(s->pb, pkt, size);
46  pkt->flags &= ~AV_PKT_FLAG_CORRUPT;
47  pkt->stream_index = 0;
49  return ret;
50 }
53  int stream_index, int64_t timestamp, int flags)
54 {
55  AVStream *st;
56  int block_align, byte_rate;
57  int64_t pos, ret;
59  st = s->streams[0];
61  block_align = st->codecpar->block_align ? st->codecpar->block_align :
63  byte_rate = st->codecpar->bit_rate ? st->codecpar->bit_rate >> 3 :
64  block_align * st->codecpar->sample_rate;
66  if (block_align <= 0 || byte_rate <= 0)
67  return -1;
68  if (timestamp < 0) timestamp = 0;
70  /* compute the position by aligning it to block_align */
71  pos = av_rescale_rnd(timestamp * byte_rate,
72  st->time_base.num,
73  st->time_base.den * (int64_t)block_align,
75  pos *= block_align;
77  /* recompute exact position */
78  st->cur_dts = av_rescale(pos, st->time_base.den, byte_rate * (int64_t)st->time_base.num);
79  if ((ret = avio_seek(s->pb, pos + s->internal->data_offset, SEEK_SET)) < 0)
80  return ret;
81  return 0;
82 }
Definition: avformat.h:2511
int64_t data_offset
offset of the first packet
Definition: internal.h:82
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3953
int num
Definition: rational.h:59
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:246
AVFormatInternal * internal
An opaque field for libavformat internal usage.
Definition: avformat.h:1811
static AVPacket pkt
This struct describes the properties of an encoded stream.
Definition: avcodec.h:3945
Format I/O context.
Definition: avformat.h:1358
int64_t cur_dts
Definition: avformat.h:1084
Round toward +infinity.
Definition: mathematics.h:83
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1426
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:310
ptrdiff_t size
Definition: opengl_enc.c:100
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: avcodec.h:3982
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition: utils.c:1506
#define FFMAX(a, b)
Definition: common.h:94
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1483
int block_align
Audio only.
Definition: avcodec.h:4070
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
#define FFMIN(a, b)
Definition: common.h:96
#define s(width, name)
Definition: cbs_vp9.c:257
int ff_pcm_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Definition: pcm.c:52
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
Stream structure.
Definition: avformat.h:881
int ff_pcm_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: pcm.c:29
AVIOContext * pb
I/O context.
Definition: avformat.h:1400
Round toward -infinity.
Definition: mathematics.h:82
#define flags(name, subs,...)
Definition: cbs_av1.c:561
int sample_rate
Audio only.
Definition: avcodec.h:4063
Main libavformat public API header.
int den
Definition: rational.h:60
The packet content is corrupted.
Definition: avcodec.h:1510
int channels
Audio only.
Definition: avcodec.h:4059
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1028
int stream_index
Definition: avcodec.h:1479
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:910
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
This structure stores compressed data.
Definition: avcodec.h:1454
Definition: pcm.c:27