Libav
Main Page
Related Pages
Modules
Data Structures
Files
Examples
File List
Globals
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 Libav.
6
*
7
* Libav 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
* Libav 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 Libav; 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
int
ff_pcm_read_seek
(
AVFormatContext
*s,
27
int
stream_index, int64_t timestamp,
int
flags
)
28
{
29
AVStream
*st;
30
int
block_align, byte_rate;
31
int64_t pos, ret;
32
33
st = s->
streams
[0];
34
35
block_align = st->
codec
->
block_align
? st->
codec
->
block_align
:
36
(
av_get_bits_per_sample
(st->
codec
->
codec_id
) * st->
codec
->
channels
) >> 3;
37
byte_rate = st->
codec
->
bit_rate
? st->
codec
->
bit_rate
>> 3 :
38
block_align * st->
codec
->
sample_rate
;
39
40
if
(block_align <= 0 || byte_rate <= 0)
41
return
-1;
42
if
(timestamp < 0) timestamp = 0;
43
44
/* compute the position by aligning it to block_align */
45
pos =
av_rescale_rnd
(timestamp * byte_rate,
46
st->
time_base
.
num
,
47
st->
time_base
.
den
* (int64_t)block_align,
48
(flags &
AVSEEK_FLAG_BACKWARD
) ?
AV_ROUND_DOWN
:
AV_ROUND_UP
);
49
pos *= block_align;
50
51
/* recompute exact position */
52
st->
cur_dts
=
av_rescale
(pos, st->
time_base
.
den
, byte_rate * (int64_t)st->
time_base
.
num
);
53
if
((ret =
avio_seek
(s->
pb
, pos + s->
data_offset
, SEEK_SET)) < 0)
54
return
ret;
55
return
0;
56
}
AVSEEK_FLAG_BACKWARD
#define AVSEEK_FLAG_BACKWARD
Definition:
avformat.h:1609
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:61
AVRational::num
int num
numerator
Definition:
rational.h:44
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition:
aviobuf.c:186
AVFormatContext::data_offset
int64_t data_offset
offset of the first packet
Definition:
avformat.h:1220
AVCodecContext::block_align
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs...
Definition:
avcodec.h:1844
AVFormatContext
Format I/O context.
Definition:
avformat.h:922
mathematics.h
AVStream::cur_dts
int64_t cur_dts
Definition:
avformat.h:851
AV_ROUND_UP
Round toward +infinity.
Definition:
mathematics.h:53
pcm.h
AVFormatContext::streams
AVStream ** streams
A list of all streams in the file.
Definition:
avformat.h:990
flags
static int flags
Definition:
log.c:44
av_get_bits_per_sample
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition:
utils.c:2043
AVStream::codec
AVCodecContext * codec
Codec context associated with this stream.
Definition:
avformat.h:718
AVCodecContext::bit_rate
int bit_rate
the average bitrate
Definition:
avcodec.h:1114
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:116
ff_pcm_read_seek
int ff_pcm_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Definition:
pcm.c:26
AVStream
Stream structure.
Definition:
avformat.h:699
AVCodecContext::codec_id
enum AVCodecID codec_id
Definition:
avcodec.h:1067
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition:
avcodec.h:1807
AVFormatContext::pb
AVIOContext * pb
I/O context.
Definition:
avformat.h:964
AV_ROUND_DOWN
Round toward -infinity.
Definition:
mathematics.h:52
avformat.h
Main libavformat public API header.
AVRational::den
int den
denominator
Definition:
rational.h:45
AVCodecContext::channels
int channels
number of audio channels
Definition:
avcodec.h:1808
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:741
Generated on Thu Nov 2 2017 14:03:25 for Libav by
1.8.8