1*600f14f4SXin Li /* grabbag - Convenience lib for various routines common to several tools
2*600f14f4SXin Li * Copyright (C) 2002-2009 Josh Coalson
3*600f14f4SXin Li * Copyright (C) 2011-2023 Xiph.Org Foundation
4*600f14f4SXin Li *
5*600f14f4SXin Li * This library is free software; you can redistribute it and/or
6*600f14f4SXin Li * modify it under the terms of the GNU Lesser General Public
7*600f14f4SXin Li * License as published by the Free Software Foundation; either
8*600f14f4SXin Li * version 2.1 of the License, or (at your option) any later version.
9*600f14f4SXin Li *
10*600f14f4SXin Li * This library is distributed in the hope that it will be useful,
11*600f14f4SXin Li * but WITHOUT ANY WARRANTY; without even the implied warranty of
12*600f14f4SXin Li * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13*600f14f4SXin Li * Lesser General Public License for more details.
14*600f14f4SXin Li *
15*600f14f4SXin Li * You should have received a copy of the GNU Lesser General Public
16*600f14f4SXin Li * License along with this library; if not, write to the Free Software
17*600f14f4SXin Li * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18*600f14f4SXin Li */
19*600f14f4SXin Li
20*600f14f4SXin Li #ifdef HAVE_CONFIG_H
21*600f14f4SXin Li # include <config.h>
22*600f14f4SXin Li #endif
23*600f14f4SXin Li
24*600f14f4SXin Li #include "share/grabbag.h"
25*600f14f4SXin Li #include "share/compat.h"
26*600f14f4SXin Li #include "FLAC/assert.h"
27*600f14f4SXin Li #include <stdlib.h> /* for atoi() */
28*600f14f4SXin Li #include <string.h>
29*600f14f4SXin Li
grabbag__seektable_convert_specification_to_template(const char * spec,FLAC__bool only_explicit_placeholders,FLAC__uint64 total_samples_to_encode,uint32_t sample_rate,FLAC__StreamMetadata * seektable_template,FLAC__bool * spec_has_real_points)30*600f14f4SXin Li FLAC__bool grabbag__seektable_convert_specification_to_template(const char *spec, FLAC__bool only_explicit_placeholders, FLAC__uint64 total_samples_to_encode, uint32_t sample_rate, FLAC__StreamMetadata *seektable_template, FLAC__bool *spec_has_real_points)
31*600f14f4SXin Li {
32*600f14f4SXin Li uint32_t i;
33*600f14f4SXin Li const char *pt;
34*600f14f4SXin Li
35*600f14f4SXin Li FLAC__ASSERT(0 != spec);
36*600f14f4SXin Li FLAC__ASSERT(0 != seektable_template);
37*600f14f4SXin Li FLAC__ASSERT(seektable_template->type == FLAC__METADATA_TYPE_SEEKTABLE);
38*600f14f4SXin Li
39*600f14f4SXin Li if(0 != spec_has_real_points)
40*600f14f4SXin Li *spec_has_real_points = false;
41*600f14f4SXin Li
42*600f14f4SXin Li for(pt = spec, i = 0; pt && *pt; i++) {
43*600f14f4SXin Li const char *q = strchr(pt, ';');
44*600f14f4SXin Li FLAC__ASSERT(0 != q);
45*600f14f4SXin Li
46*600f14f4SXin Li if(q > pt) {
47*600f14f4SXin Li if(0 == strncmp(pt, "X;", 2)) { /* -S X */
48*600f14f4SXin Li if(!FLAC__metadata_object_seektable_template_append_placeholders(seektable_template, 1))
49*600f14f4SXin Li return false;
50*600f14f4SXin Li }
51*600f14f4SXin Li else if(q[-1] == 'x') { /* -S #x */
52*600f14f4SXin Li if(total_samples_to_encode > 0) { /* we can only do these if we know the number of samples to encode up front */
53*600f14f4SXin Li if(0 != spec_has_real_points)
54*600f14f4SXin Li *spec_has_real_points = true;
55*600f14f4SXin Li if(!only_explicit_placeholders) {
56*600f14f4SXin Li const int n = (uint32_t)atoi(pt);
57*600f14f4SXin Li if(n > 0)
58*600f14f4SXin Li if(!FLAC__metadata_object_seektable_template_append_spaced_points(seektable_template, (uint32_t)n, total_samples_to_encode))
59*600f14f4SXin Li return false;
60*600f14f4SXin Li }
61*600f14f4SXin Li }
62*600f14f4SXin Li }
63*600f14f4SXin Li else if(q[-1] == 's') { /* -S #s */
64*600f14f4SXin Li if(total_samples_to_encode > 0 && sample_rate > 0) { /* we can only do these if we know the number of samples and sample rate to encode up front */
65*600f14f4SXin Li if(0 != spec_has_real_points)
66*600f14f4SXin Li *spec_has_real_points = true;
67*600f14f4SXin Li if(!only_explicit_placeholders) {
68*600f14f4SXin Li const double sec = atof(pt);
69*600f14f4SXin Li if(sec > 0.0) {
70*600f14f4SXin Li uint32_t samples = (uint32_t)(sec * (double)sample_rate);
71*600f14f4SXin Li /* Restrict seekpoints to two per second of audio. */
72*600f14f4SXin Li samples = samples < sample_rate / 2 ? sample_rate / 2 : samples;
73*600f14f4SXin Li if(samples > 0) {
74*600f14f4SXin Li /* +1 for the initial point at sample 0 */
75*600f14f4SXin Li if(!FLAC__metadata_object_seektable_template_append_spaced_points_by_samples(seektable_template, samples, total_samples_to_encode))
76*600f14f4SXin Li return false;
77*600f14f4SXin Li }
78*600f14f4SXin Li }
79*600f14f4SXin Li }
80*600f14f4SXin Li }
81*600f14f4SXin Li }
82*600f14f4SXin Li else { /* -S # */
83*600f14f4SXin Li if(0 != spec_has_real_points)
84*600f14f4SXin Li *spec_has_real_points = true;
85*600f14f4SXin Li if(!only_explicit_placeholders) {
86*600f14f4SXin Li char *endptr;
87*600f14f4SXin Li const FLAC__int64 n = (FLAC__int64)strtoll(pt, &endptr, 10);
88*600f14f4SXin Li if(
89*600f14f4SXin Li (n > 0 || (endptr > pt && *endptr == ';')) && /* is a valid number (extra check needed for "0") */
90*600f14f4SXin Li (total_samples_to_encode == 0 || (FLAC__uint64)n < total_samples_to_encode) /* number is not >= the known total_samples_to_encode */
91*600f14f4SXin Li )
92*600f14f4SXin Li if(!FLAC__metadata_object_seektable_template_append_point(seektable_template, (FLAC__uint64)n))
93*600f14f4SXin Li return false;
94*600f14f4SXin Li }
95*600f14f4SXin Li }
96*600f14f4SXin Li }
97*600f14f4SXin Li
98*600f14f4SXin Li pt = ++q;
99*600f14f4SXin Li }
100*600f14f4SXin Li
101*600f14f4SXin Li if(!FLAC__metadata_object_seektable_template_sort(seektable_template, /*compact=*/true))
102*600f14f4SXin Li return false;
103*600f14f4SXin Li
104*600f14f4SXin Li return true;
105*600f14f4SXin Li }
106