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