FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 RAW_SAMPLES 1024
28 
30 {
31  int ret, size;
32 
34  if (size <= 0)
35  return AVERROR(EINVAL);
36 
37  ret= av_get_packet(s->pb, pkt, size);
38 
39  pkt->flags &= ~AV_PKT_FLAG_CORRUPT;
40  pkt->stream_index = 0;
41 
42  return ret;
43 }
44 
46  int stream_index, int64_t timestamp, int flags)
47 {
48  AVStream *st;
49  int block_align, byte_rate;
50  int64_t pos, ret;
51 
52  st = s->streams[0];
53 
54  block_align = st->codecpar->block_align ? st->codecpar->block_align :
56  byte_rate = st->codecpar->bit_rate ? st->codecpar->bit_rate >> 3 :
57  block_align * st->codecpar->sample_rate;
58 
59  if (block_align <= 0 || byte_rate <= 0)
60  return -1;
61  if (timestamp < 0) timestamp = 0;
62 
63  /* compute the position by aligning it to block_align */
64  pos = av_rescale_rnd(timestamp * byte_rate,
65  st->time_base.num,
66  st->time_base.den * (int64_t)block_align,
68  pos *= block_align;
69 
70  /* recompute exact position */
71  st->cur_dts = av_rescale(pos, st->time_base.den, byte_rate * (int64_t)st->time_base.num);
72  if ((ret = avio_seek(s->pb, pos + s->internal->data_offset, SEEK_SET)) < 0)
73  return ret;
74  return 0;
75 }
#define AVSEEK_FLAG_BACKWARD
Definition: avformat.h:2423
const char * s
Definition: avisynth_c.h:768
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:4152
int num
Numerator.
Definition: rational.h:59
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:244
AVFormatInternal * internal
An opaque field for libavformat internal usage.
Definition: avformat.h:1780
static AVPacket pkt
Format I/O context.
Definition: avformat.h:1349
int64_t cur_dts
Definition: avformat.h:1066
Round toward +infinity.
Definition: mathematics.h:83
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1417
static int flags
Definition: log.c:57
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:289
ptrdiff_t size
Definition: opengl_enc.c:101
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: avcodec.h:4181
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition: utils.c:1662
#define AVERROR(e)
Definition: error.h:43
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1685
int block_align
Audio only.
Definition: avcodec.h:4269
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
int ff_pcm_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Definition: pcm.c:45
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:889
int ff_pcm_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: pcm.c:29
AVIOContext * pb
I/O context.
Definition: avformat.h:1391
Round toward -infinity.
Definition: mathematics.h:82
int sample_rate
Audio only.
Definition: avcodec.h:4262
Main libavformat public API header.
int den
Denominator.
Definition: rational.h:60
#define AV_PKT_FLAG_CORRUPT
The packet content is corrupted.
Definition: avcodec.h:1712
int channels
Audio only.
Definition: avcodec.h:4258
AVCodecParameters * codecpar
Definition: avformat.h:1252
int stream_index
Definition: avcodec.h:1681
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:926
This structure stores compressed data.
Definition: avcodec.h:1656
#define RAW_SAMPLES
Definition: pcm.c:27