Line | Branch | Exec | Source |
---|---|---|---|
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 PCM_DEMUX_TARGET_FPS 10 | ||
28 | |||
29 | 4566 | int ff_pcm_default_packet_size(AVCodecParameters *par) | |
30 | { | ||
31 | int nb_samples, max_samples, bits_per_sample; | ||
32 | int64_t bitrate; | ||
33 | |||
34 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4566 times.
|
4566 | if (par->block_align <= 0) |
35 | ✗ | return AVERROR(EINVAL); | |
36 | |||
37 | 4566 | max_samples = INT_MAX / par->block_align; | |
38 | 4566 | bits_per_sample = av_get_bits_per_sample(par->codec_id); | |
39 | 4566 | bitrate = par->bit_rate; | |
40 | |||
41 | /* Don't trust the codecpar bitrate if we can calculate it ourselves */ | ||
42 |
4/6✓ Branch 0 taken 4553 times.
✓ Branch 1 taken 13 times.
✓ Branch 2 taken 4553 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4553 times.
✗ Branch 5 not taken.
|
4566 | if (bits_per_sample > 0 && par->sample_rate > 0 && par->ch_layout.nb_channels > 0) |
43 |
1/2✓ Branch 0 taken 4553 times.
✗ Branch 1 not taken.
|
4553 | if ((int64_t)par->sample_rate * par->ch_layout.nb_channels < INT64_MAX / bits_per_sample) |
44 | 4553 | bitrate = bits_per_sample * (int64_t)par->sample_rate * par->ch_layout.nb_channels; | |
45 | |||
46 |
1/2✓ Branch 0 taken 4566 times.
✗ Branch 1 not taken.
|
4566 | if (bitrate > 0) { |
47 | 4566 | nb_samples = av_clip64(bitrate / 8 / PCM_DEMUX_TARGET_FPS / par->block_align, 1, max_samples); | |
48 | 4566 | nb_samples = 1 << av_log2(nb_samples); | |
49 | } else { | ||
50 | /* Fallback to a size based method for a non-pcm codec with unknown bitrate */ | ||
51 | ✗ | nb_samples = av_clip(4096 / par->block_align, 1, max_samples); | |
52 | } | ||
53 | |||
54 | 4566 | return par->block_align * nb_samples; | |
55 | } | ||
56 | |||
57 | 4024 | int ff_pcm_read_packet(AVFormatContext *s, AVPacket *pkt) | |
58 | { | ||
59 | int ret, size; | ||
60 | |||
61 | 4024 | size = ff_pcm_default_packet_size(s->streams[0]->codecpar); | |
62 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4024 times.
|
4024 | if (size < 0) |
63 | ✗ | return size; | |
64 | |||
65 | 4024 | ret = av_get_packet(s->pb, pkt, size); | |
66 | |||
67 | 4024 | pkt->flags &= ~AV_PKT_FLAG_CORRUPT; | |
68 | 4024 | pkt->stream_index = 0; | |
69 | |||
70 | 4024 | return ret; | |
71 | } | ||
72 | |||
73 | 616 | int ff_pcm_read_seek(AVFormatContext *s, | |
74 | int stream_index, int64_t timestamp, int flags) | ||
75 | { | ||
76 | AVStream *st; | ||
77 | int block_align, byte_rate; | ||
78 | int64_t pos, ret; | ||
79 | |||
80 | 616 | st = s->streams[0]; | |
81 | |||
82 |
1/2✓ Branch 0 taken 616 times.
✗ Branch 1 not taken.
|
616 | block_align = st->codecpar->block_align ? st->codecpar->block_align : |
83 | ✗ | (av_get_bits_per_sample(st->codecpar->codec_id) * st->codecpar->ch_layout.nb_channels) >> 3; | |
84 |
1/2✓ Branch 0 taken 616 times.
✗ Branch 1 not taken.
|
616 | byte_rate = st->codecpar->bit_rate ? st->codecpar->bit_rate >> 3 : |
85 | ✗ | block_align * st->codecpar->sample_rate; | |
86 | |||
87 |
2/4✓ Branch 0 taken 616 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 616 times.
|
616 | if (block_align <= 0 || byte_rate <= 0) |
88 | ✗ | return -1; | |
89 |
2/2✓ Branch 0 taken 184 times.
✓ Branch 1 taken 432 times.
|
616 | if (timestamp < 0) timestamp = 0; |
90 | |||
91 | /* compute the position by aligning it to block_align */ | ||
92 | 616 | pos = av_rescale_rnd(timestamp * byte_rate, | |
93 | 616 | st->time_base.num, | |
94 | 616 | st->time_base.den * (int64_t)block_align, | |
95 |
2/2✓ Branch 0 taken 317 times.
✓ Branch 1 taken 299 times.
|
616 | (flags & AVSEEK_FLAG_BACKWARD) ? AV_ROUND_DOWN : AV_ROUND_UP); |
96 | 616 | pos *= block_align; | |
97 | |||
98 | /* recompute exact position */ | ||
99 | 616 | ffstream(st)->cur_dts = av_rescale(pos, st->time_base.den, byte_rate * (int64_t)st->time_base.num); | |
100 |
2/2✓ Branch 2 taken 14 times.
✓ Branch 3 taken 602 times.
|
616 | if ((ret = avio_seek(s->pb, pos + ffformatcontext(s)->data_offset, SEEK_SET)) < 0) |
101 | 14 | return ret; | |
102 | 602 | return 0; | |
103 | } | ||
104 |