FFmpeg  4.4.6
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  AVCodecParameters *par = s->streams[0]->codecpar;
32  int ret, size;
33 
34  if (par->block_align <= 0)
35  return AVERROR(EINVAL);
36 
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  if (par->block_align <= INT_MAX / RAW_SAMPLES) {
44  } else {
45  size = par->block_align;
46  }
47 
48  ret = av_get_packet(s->pb, pkt, size);
49 
51  pkt->stream_index = 0;
52 
53  return ret;
54 }
55 
57  int stream_index, int64_t timestamp, int flags)
58 {
59  AVStream *st;
60  int block_align, byte_rate;
61  int64_t pos, ret;
62 
63  st = s->streams[0];
64 
65  block_align = st->codecpar->block_align ? st->codecpar->block_align :
67  byte_rate = st->codecpar->bit_rate ? st->codecpar->bit_rate >> 3 :
68  block_align * st->codecpar->sample_rate;
69 
70  if (block_align <= 0 || byte_rate <= 0)
71  return -1;
72  if (timestamp < 0) timestamp = 0;
73 
74  /* compute the position by aligning it to block_align */
75  pos = av_rescale_rnd(timestamp * byte_rate,
76  st->time_base.num,
77  st->time_base.den * (int64_t)block_align,
79  pos *= block_align;
80 
81  /* recompute exact position */
82  st->cur_dts = av_rescale(pos, st->time_base.den, byte_rate * (int64_t)st->time_base.num);
83  if ((ret = avio_seek(s->pb, pos + s->internal->data_offset, SEEK_SET)) < 0)
84  return ret;
85  return 0;
86 }
Main libavformat public API header.
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
#define AVSEEK_FLAG_BACKWARD
Definition: avformat.h:2415
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:253
#define flags(name, subs,...)
Definition: cbs_av1.c:572
#define s(width, name)
Definition: cbs_vp9.c:257
#define FFMIN(a, b)
Definition: common.h:105
#define FFMAX(a, b)
Definition: common.h:103
long long int64_t
Definition: coverity.c:34
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition: utils.c:639
#define AV_PKT_FLAG_CORRUPT
The packet content is corrupted.
Definition: packet.h:411
#define AVERROR(e)
Definition: error.h:43
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
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
@ AV_ROUND_DOWN
Round toward -infinity.
Definition: mathematics.h:82
@ AV_ROUND_UP
Round toward +infinity.
Definition: mathematics.h:83
int ff_pcm_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: pcm.c:29
#define RAW_SAMPLES
Definition: pcm.c:27
int ff_pcm_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Definition: pcm.c:56
common internal API header
unsigned int pos
Definition: spdifenc.c:412
This struct describes the properties of an encoded stream.
Definition: codec_par.h:52
int channels
Audio only.
Definition: codec_par.h:166
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: codec_par.h:89
int block_align
Audio only.
Definition: codec_par.h:177
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
int sample_rate
Audio only.
Definition: codec_par.h:170
Format I/O context.
Definition: avformat.h:1232
This structure stores compressed data.
Definition: packet.h:346
int stream_index
Definition: packet.h:371
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:375
int num
Numerator.
Definition: rational.h:59
int den
Denominator.
Definition: rational.h:60
Stream structure.
Definition: avformat.h:873
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1038
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition: avformat.h:902
int64_t cur_dts
Definition: avformat.h:1066
AVPacket * pkt
Definition: movenc.c:59
int size