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