xref: /aosp_15_r20/external/flac/src/metaflac/operations_shorthand_seektable.c (revision 600f14f40d737144c998e2ec7a483122d3776fbc)
1 /* metaflac - Command-line FLAC metadata editor
2  * Copyright (C) 2001-2009  Josh Coalson
3  * Copyright (C) 2011-2023  Xiph.Org Foundation
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program 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
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 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 "utils.h"
25 #include "FLAC/assert.h"
26 #include "FLAC/stream_decoder.h"
27 #include "FLAC/metadata.h"
28 #include "share/grabbag.h"
29 #include "operations_shorthand.h"
30 
31 static FLAC__bool populate_seekpoint_values(const char *filename, FLAC__StreamMetadata *block, FLAC__bool *needs_write);
32 
do_shorthand_operation__add_seekpoints(const char * filename,FLAC__Metadata_Chain * chain,const char * specification,FLAC__bool * needs_write)33 FLAC__bool do_shorthand_operation__add_seekpoints(const char *filename, FLAC__Metadata_Chain *chain, const char *specification, FLAC__bool *needs_write)
34 {
35 	FLAC__bool ok = true, found_seektable_block = false;
36 	FLAC__StreamMetadata *block = 0;
37 	FLAC__Metadata_Iterator *iterator = FLAC__metadata_iterator_new();
38 	FLAC__uint64 total_samples = 0;
39 	unsigned sample_rate = 0;
40 
41 	if(0 == iterator)
42 		die("out of memory allocating iterator");
43 
44 	FLAC__metadata_iterator_init(iterator, chain);
45 
46 	do {
47 		block = FLAC__metadata_iterator_get_block(iterator);
48 		if(block->type == FLAC__METADATA_TYPE_STREAMINFO) {
49 			sample_rate = block->data.stream_info.sample_rate;
50 			total_samples = block->data.stream_info.total_samples;
51 		}
52 		else if(block->type == FLAC__METADATA_TYPE_SEEKTABLE)
53 			found_seektable_block = true;
54 	} while(!found_seektable_block && FLAC__metadata_iterator_next(iterator));
55 
56 	if(total_samples == 0) {
57 		flac_fprintf(stderr, "%s: ERROR: cannot add seekpoints because STREAMINFO block does not specify total_samples\n", filename);
58 		FLAC__metadata_iterator_delete(iterator);
59 		return false;
60 	}
61 
62 	if(!found_seektable_block) {
63 		/* create a new block */
64 		block = FLAC__metadata_object_new(FLAC__METADATA_TYPE_SEEKTABLE);
65 		if(0 == block)
66 			die("out of memory allocating SEEKTABLE block");
67 		while(FLAC__metadata_iterator_prev(iterator))
68 			;
69 		if(!FLAC__metadata_iterator_insert_block_after(iterator, block)) {
70 			print_error_with_chain_status(chain, "%s: ERROR: adding new SEEKTABLE block to metadata", filename);
71 			FLAC__metadata_object_delete(block);
72 			FLAC__metadata_iterator_delete(iterator);
73 			return false;
74 		}
75 		/* iterator is left pointing to new block */
76 		FLAC__ASSERT(FLAC__metadata_iterator_get_block(iterator) == block);
77 	}
78 
79 	FLAC__metadata_iterator_delete(iterator);
80 
81 	FLAC__ASSERT(0 != block);
82 	FLAC__ASSERT(block->type == FLAC__METADATA_TYPE_SEEKTABLE);
83 
84 	if(!grabbag__seektable_convert_specification_to_template(specification, /*only_explicit_placeholders=*/false, total_samples, sample_rate, block, /*spec_has_real_points=*/0)) {
85 		flac_fprintf(stderr, "%s: ERROR (internal) preparing seektable with seekpoints\n", filename);
86 		return false;
87 	}
88 
89 	ok = populate_seekpoint_values(filename, block, needs_write);
90 
91 	if(ok)
92 		(void) FLAC__format_seektable_sort(&block->data.seek_table);
93 
94 	return ok;
95 }
96 
97 /*
98  * local routines
99  */
100 
101 typedef struct {
102 	FLAC__StreamMetadata_SeekTable *seektable_template;
103 	FLAC__uint64 samples_written;
104 	FLAC__uint64 audio_offset, last_offset;
105 	unsigned first_seekpoint_to_check;
106 	FLAC__bool error_occurred;
107 	FLAC__StreamDecoderErrorStatus error_status;
108 } ClientData;
109 
write_callback_(const FLAC__StreamDecoder * decoder,const FLAC__Frame * frame,const FLAC__int32 * const buffer[],void * client_data)110 static FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
111 {
112 	ClientData *cd = (ClientData*)client_data;
113 
114 	(void)buffer;
115 	FLAC__ASSERT(0 != cd);
116 
117 	if(!cd->error_occurred) {
118 		const unsigned blocksize = frame->header.blocksize;
119 		const FLAC__uint64 frame_first_sample = cd->samples_written;
120 		const FLAC__uint64 frame_last_sample = frame_first_sample + (FLAC__uint64)blocksize - 1;
121 		FLAC__uint64 test_sample;
122 		unsigned i;
123 		for(i = cd->first_seekpoint_to_check; i < cd->seektable_template->num_points; i++) {
124 			test_sample = cd->seektable_template->points[i].sample_number;
125 			if(test_sample > frame_last_sample) {
126 				break;
127 			}
128 			else if(test_sample >= frame_first_sample) {
129 				cd->seektable_template->points[i].sample_number = frame_first_sample;
130 				cd->seektable_template->points[i].stream_offset = cd->last_offset - cd->audio_offset;
131 				cd->seektable_template->points[i].frame_samples = blocksize;
132 				cd->first_seekpoint_to_check++;
133 				/* DO NOT: "break;" and here's why:
134 				 * The seektable template may contain more than one target
135 				 * sample for any given frame; we will keep looping, generating
136 				 * duplicate seekpoints for them, and we'll clean it up later,
137 				 * just before writing the seektable back to the metadata.
138 				 */
139 			}
140 			else {
141 				cd->first_seekpoint_to_check++;
142 			}
143 		}
144 		cd->samples_written += blocksize;
145 		if(!FLAC__stream_decoder_get_decode_position(decoder, &cd->last_offset))
146 			return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
147 		return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
148 	}
149 	else
150 		return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
151 }
152 
error_callback_(const FLAC__StreamDecoder * decoder,FLAC__StreamDecoderErrorStatus status,void * client_data)153 static void error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
154 {
155 	ClientData *cd = (ClientData*)client_data;
156 
157 	(void)decoder;
158 	FLAC__ASSERT(0 != cd);
159 
160 	if(!cd->error_occurred) { /* don't let multiple errors overwrite the first one */
161 		cd->error_occurred = true;
162 		cd->error_status = status;
163 	}
164 }
165 
populate_seekpoint_values(const char * filename,FLAC__StreamMetadata * block,FLAC__bool * needs_write)166 FLAC__bool populate_seekpoint_values(const char *filename, FLAC__StreamMetadata *block, FLAC__bool *needs_write)
167 {
168 	FLAC__StreamDecoder *decoder;
169 	ClientData client_data;
170 	FLAC__bool ok = true;
171 
172 	FLAC__ASSERT(0 != block);
173 	FLAC__ASSERT(block->type == FLAC__METADATA_TYPE_SEEKTABLE);
174 
175 	client_data.seektable_template = &block->data.seek_table;
176 	client_data.samples_written = 0;
177 	/* client_data.audio_offset must be determined later */
178 	client_data.first_seekpoint_to_check = 0;
179 	client_data.error_occurred = false;
180 
181 	decoder = FLAC__stream_decoder_new();
182 
183 	if(0 == decoder) {
184 		flac_fprintf(stderr, "%s: ERROR (--add-seekpoint) creating the decoder instance\n", filename);
185 		return false;
186 	}
187 
188 	FLAC__stream_decoder_set_md5_checking(decoder, false);
189 	FLAC__stream_decoder_set_metadata_ignore_all(decoder);
190 
191 	if(FLAC__stream_decoder_init_file(decoder, filename, write_callback_, /*metadata_callback=*/0, error_callback_, &client_data) != FLAC__STREAM_DECODER_INIT_STATUS_OK) {
192 		flac_fprintf(stderr, "%s: ERROR (--add-seekpoint) initializing the decoder instance (%s)\n", filename, FLAC__stream_decoder_get_resolved_state_string(decoder));
193 		ok = false;
194 	}
195 
196 	if(ok && !FLAC__stream_decoder_process_until_end_of_metadata(decoder)) {
197 		flac_fprintf(stderr, "%s: ERROR (--add-seekpoint) decoding file (%s)\n", filename, FLAC__stream_decoder_get_resolved_state_string(decoder));
198 		ok = false;
199 	}
200 
201 	if(ok && !FLAC__stream_decoder_get_decode_position(decoder, &client_data.audio_offset)) {
202 		flac_fprintf(stderr, "%s: ERROR (--add-seekpoint) decoding file\n", filename);
203 		ok = false;
204 	}
205 	client_data.last_offset = client_data.audio_offset;
206 
207 	if(ok && !FLAC__stream_decoder_process_until_end_of_stream(decoder)) {
208 		flac_fprintf(stderr, "%s: ERROR (--add-seekpoint) decoding file (%s)\n", filename, FLAC__stream_decoder_get_resolved_state_string(decoder));
209 		ok = false;
210 	}
211 
212 	if(ok && client_data.error_occurred) {
213 		flac_fprintf(stderr, "%s: ERROR (--add-seekpoint) decoding file (%u:%s)\n", filename, (unsigned)client_data.error_status, FLAC__StreamDecoderErrorStatusString[client_data.error_status]);
214 		ok = false;
215 	}
216 
217 	*needs_write = true;
218 	FLAC__stream_decoder_delete(decoder);
219 	return ok;
220 }
221