]> git.sesse.net Git - ffmpeg/blob - libavformat/pcm.c
omx: Add support for broadcom OMX on raspberry pi
[ffmpeg] / libavformat / pcm.c
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 "internal.h"
25 #include "pcm.h"
26
27 int ff_pcm_read_seek(AVFormatContext *s,
28                      int stream_index, int64_t timestamp, int flags)
29 {
30     AVStream *st;
31     int block_align, byte_rate;
32     int64_t pos, ret;
33
34     st = s->streams[0];
35
36     block_align = st->codecpar->block_align ? st->codecpar->block_align :
37         (av_get_bits_per_sample(st->codecpar->codec_id) * st->codecpar->channels) >> 3;
38     byte_rate = st->codecpar->bit_rate ? st->codecpar->bit_rate >> 3 :
39         block_align * st->codecpar->sample_rate;
40
41     if (block_align <= 0 || byte_rate <= 0)
42         return -1;
43     if (timestamp < 0) timestamp = 0;
44
45     /* compute the position by aligning it to block_align */
46     pos = av_rescale_rnd(timestamp * byte_rate,
47                          st->time_base.num,
48                          st->time_base.den * (int64_t)block_align,
49                          (flags & AVSEEK_FLAG_BACKWARD) ? AV_ROUND_DOWN : AV_ROUND_UP);
50     pos *= block_align;
51
52     /* recompute exact position */
53     st->cur_dts = av_rescale(pos, st->time_base.den, byte_rate * (int64_t)st->time_base.num);
54     if ((ret = avio_seek(s->pb, pos + s->internal->data_offset, SEEK_SET)) < 0)
55         return ret;
56     return 0;
57 }