annotate pcm.c @ 6471:b3aea89a4f63 libavformat

nutenc: fix unstable floating-point calculations
author mru
date Fri, 10 Sep 2010 13:16:08 +0000
parents 4aaed59641ff
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6445
4aaed59641ff move pcm demuxers to their own file
aurel
parents:
diff changeset
1 /*
4aaed59641ff move pcm demuxers to their own file
aurel
parents:
diff changeset
2 * PCM common functions
4aaed59641ff move pcm demuxers to their own file
aurel
parents:
diff changeset
3 * Copyright (c) 2003 Fabrice Bellard
4aaed59641ff move pcm demuxers to their own file
aurel
parents:
diff changeset
4 *
4aaed59641ff move pcm demuxers to their own file
aurel
parents:
diff changeset
5 * This file is part of FFmpeg.
4aaed59641ff move pcm demuxers to their own file
aurel
parents:
diff changeset
6 *
4aaed59641ff move pcm demuxers to their own file
aurel
parents:
diff changeset
7 * FFmpeg is free software; you can redistribute it and/or
4aaed59641ff move pcm demuxers to their own file
aurel
parents:
diff changeset
8 * modify it under the terms of the GNU Lesser General Public
4aaed59641ff move pcm demuxers to their own file
aurel
parents:
diff changeset
9 * License as published by the Free Software Foundation; either
4aaed59641ff move pcm demuxers to their own file
aurel
parents:
diff changeset
10 * version 2.1 of the License, or (at your option) any later version.
4aaed59641ff move pcm demuxers to their own file
aurel
parents:
diff changeset
11 *
4aaed59641ff move pcm demuxers to their own file
aurel
parents:
diff changeset
12 * FFmpeg is distributed in the hope that it will be useful,
4aaed59641ff move pcm demuxers to their own file
aurel
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
4aaed59641ff move pcm demuxers to their own file
aurel
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
4aaed59641ff move pcm demuxers to their own file
aurel
parents:
diff changeset
15 * Lesser General Public License for more details.
4aaed59641ff move pcm demuxers to their own file
aurel
parents:
diff changeset
16 *
4aaed59641ff move pcm demuxers to their own file
aurel
parents:
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
4aaed59641ff move pcm demuxers to their own file
aurel
parents:
diff changeset
18 * License along with FFmpeg; if not, write to the Free Software
4aaed59641ff move pcm demuxers to their own file
aurel
parents:
diff changeset
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
4aaed59641ff move pcm demuxers to their own file
aurel
parents:
diff changeset
20 */
4aaed59641ff move pcm demuxers to their own file
aurel
parents:
diff changeset
21
4aaed59641ff move pcm demuxers to their own file
aurel
parents:
diff changeset
22 #include "avformat.h"
4aaed59641ff move pcm demuxers to their own file
aurel
parents:
diff changeset
23 #include "pcm.h"
4aaed59641ff move pcm demuxers to their own file
aurel
parents:
diff changeset
24
4aaed59641ff move pcm demuxers to their own file
aurel
parents:
diff changeset
25 int pcm_read_seek(AVFormatContext *s,
4aaed59641ff move pcm demuxers to their own file
aurel
parents:
diff changeset
26 int stream_index, int64_t timestamp, int flags)
4aaed59641ff move pcm demuxers to their own file
aurel
parents:
diff changeset
27 {
4aaed59641ff move pcm demuxers to their own file
aurel
parents:
diff changeset
28 AVStream *st;
4aaed59641ff move pcm demuxers to their own file
aurel
parents:
diff changeset
29 int block_align, byte_rate;
4aaed59641ff move pcm demuxers to their own file
aurel
parents:
diff changeset
30 int64_t pos, ret;
4aaed59641ff move pcm demuxers to their own file
aurel
parents:
diff changeset
31
4aaed59641ff move pcm demuxers to their own file
aurel
parents:
diff changeset
32 st = s->streams[0];
4aaed59641ff move pcm demuxers to their own file
aurel
parents:
diff changeset
33
4aaed59641ff move pcm demuxers to their own file
aurel
parents:
diff changeset
34 block_align = st->codec->block_align ? st->codec->block_align :
4aaed59641ff move pcm demuxers to their own file
aurel
parents:
diff changeset
35 (av_get_bits_per_sample(st->codec->codec_id) * st->codec->channels) >> 3;
4aaed59641ff move pcm demuxers to their own file
aurel
parents:
diff changeset
36 byte_rate = st->codec->bit_rate ? st->codec->bit_rate >> 3 :
4aaed59641ff move pcm demuxers to their own file
aurel
parents:
diff changeset
37 block_align * st->codec->sample_rate;
4aaed59641ff move pcm demuxers to their own file
aurel
parents:
diff changeset
38
4aaed59641ff move pcm demuxers to their own file
aurel
parents:
diff changeset
39 if (block_align <= 0 || byte_rate <= 0)
4aaed59641ff move pcm demuxers to their own file
aurel
parents:
diff changeset
40 return -1;
4aaed59641ff move pcm demuxers to their own file
aurel
parents:
diff changeset
41 if (timestamp < 0) timestamp = 0;
4aaed59641ff move pcm demuxers to their own file
aurel
parents:
diff changeset
42
4aaed59641ff move pcm demuxers to their own file
aurel
parents:
diff changeset
43 /* compute the position by aligning it to block_align */
4aaed59641ff move pcm demuxers to their own file
aurel
parents:
diff changeset
44 pos = av_rescale_rnd(timestamp * byte_rate,
4aaed59641ff move pcm demuxers to their own file
aurel
parents:
diff changeset
45 st->time_base.num,
4aaed59641ff move pcm demuxers to their own file
aurel
parents:
diff changeset
46 st->time_base.den * (int64_t)block_align,
4aaed59641ff move pcm demuxers to their own file
aurel
parents:
diff changeset
47 (flags & AVSEEK_FLAG_BACKWARD) ? AV_ROUND_DOWN : AV_ROUND_UP);
4aaed59641ff move pcm demuxers to their own file
aurel
parents:
diff changeset
48 pos *= block_align;
4aaed59641ff move pcm demuxers to their own file
aurel
parents:
diff changeset
49
4aaed59641ff move pcm demuxers to their own file
aurel
parents:
diff changeset
50 /* recompute exact position */
4aaed59641ff move pcm demuxers to their own file
aurel
parents:
diff changeset
51 st->cur_dts = av_rescale(pos, st->time_base.den, byte_rate * (int64_t)st->time_base.num);
4aaed59641ff move pcm demuxers to their own file
aurel
parents:
diff changeset
52 if ((ret = url_fseek(s->pb, pos + s->data_offset, SEEK_SET)) < 0)
4aaed59641ff move pcm demuxers to their own file
aurel
parents:
diff changeset
53 return ret;
4aaed59641ff move pcm demuxers to their own file
aurel
parents:
diff changeset
54 return 0;
4aaed59641ff move pcm demuxers to their own file
aurel
parents:
diff changeset
55 }