libavformat/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 "pcm.h"
25 
26 #define RAW_SAMPLES 1024
27 
29 {
30  int ret, size;
31 
32  size= RAW_SAMPLES*s->streams[0]->codec->block_align;
33  if (size <= 0)
34  return AVERROR(EINVAL);
35 
36  ret= av_get_packet(s->pb, pkt, size);
37 
38  pkt->flags &= ~AV_PKT_FLAG_CORRUPT;
39  pkt->stream_index = 0;
40  if (ret < 0)
41  return ret;
42 
43  return ret;
44 }
45 
47  int stream_index, int64_t timestamp, int flags)
48 {
49  AVStream *st;
50  int block_align, byte_rate;
51  int64_t pos, ret;
52 
53  st = s->streams[0];
54 
55  block_align = st->codec->block_align ? st->codec->block_align :
57  byte_rate = st->codec->bit_rate ? st->codec->bit_rate >> 3 :
58  block_align * st->codec->sample_rate;
59 
60  if (block_align <= 0 || byte_rate <= 0)
61  return -1;
62  if (timestamp < 0) timestamp = 0;
63 
64  /* compute the position by aligning it to block_align */
65  pos = av_rescale_rnd(timestamp * byte_rate,
66  st->time_base.num,
67  st->time_base.den * (int64_t)block_align,
69  pos *= block_align;
70 
71  /* recompute exact position */
72  st->cur_dts = av_rescale(pos, st->time_base.den, byte_rate * (int64_t)st->time_base.num);
73  if ((ret = avio_seek(s->pb, pos + s->data_offset, SEEK_SET)) < 0)
74  return ret;
75  return 0;
76 }
#define AVSEEK_FLAG_BACKWARD
Definition: avformat.h:1743
const char * s
Definition: avisynth_c.h:668
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:60
int num
numerator
Definition: rational.h:44
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:199
int64_t data_offset
offset of the first packet
Definition: avformat.h:1242
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs...
Format I/O context.
Definition: avformat.h:944
int64_t cur_dts
Definition: avformat.h:785
Round toward +infinity.
Definition: mathematics.h:71
static AVPacket pkt
Definition: demuxing.c:56
AVStream ** streams
Definition: avformat.h:992
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.
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
int size
int flags
A combination of AV_PKT_FLAG values.
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:662
int bit_rate
the average bitrate
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:118
ret
Definition: avfilter.c:821
int ff_pcm_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Stream structure.
Definition: avformat.h:643
int ff_pcm_read_packet(AVFormatContext *s, AVPacket *pkt)
enum AVCodecID codec_id
int sample_rate
samples per second
AVIOContext * pb
I/O context.
Definition: avformat.h:977
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer 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.Buffer references ownership and permissions
Round toward -infinity.
Definition: mathematics.h:70
static int flags
Definition: cpu.c:23
Main libavformat public API header.
int den
denominator
Definition: rational.h:45
#define AV_PKT_FLAG_CORRUPT
The packet content is corrupted.
int channels
number of audio channels
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:679
This structure stores compressed data.
#define RAW_SAMPLES