7d20f24af2a8e66e528006a771b6d690d6eeedbc
[flac.git] / src / metaflac / operations_shorthand_seektable.c
1 /* metaflac - Command-line FLAC metadata editor
2  * Copyright (C) 2001,2002  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 #include "utils.h"
20 #include "FLAC/assert.h"
21 #include "FLAC/file_decoder.h"
22 #include "FLAC/metadata.h"
23 #include "share/grabbag.h"
24
25 static FLAC__bool populate_seekpoint_values(const char *filename, FLAC__StreamMetadata *block, FLAC__bool *needs_write);
26
27 FLAC__bool do_shorthand_operation__add_seekpoints(const char *filename, FLAC__Metadata_Chain *chain, const char *specification, FLAC__bool *needs_write)
28 {
29         FLAC__bool ok = true, found_seektable_block = false;
30         FLAC__StreamMetadata *block = 0;
31         FLAC__Metadata_Iterator *iterator = FLAC__metadata_iterator_new();
32         FLAC__uint64 total_samples = 0;
33         unsigned sample_rate = 0;
34
35         if(0 == iterator)
36                 die("out of memory allocating iterator");
37
38         FLAC__metadata_iterator_init(iterator, chain);
39
40         do {
41                 block = FLAC__metadata_iterator_get_block(iterator);
42                 if(block->type == FLAC__METADATA_TYPE_STREAMINFO) {
43                         sample_rate = block->data.stream_info.sample_rate;
44                         total_samples = block->data.stream_info.total_samples;
45                 }
46                 else if(block->type == FLAC__METADATA_TYPE_SEEKTABLE)
47                         found_seektable_block = true;
48         } while(!found_seektable_block && FLAC__metadata_iterator_next(iterator));
49
50         if(total_samples == 0) {
51                 fprintf(stderr, "%s: ERROR: cannot add seekpoints because STREAMINFO block does not specify total_samples\n", filename);
52                 return false;
53         }
54
55         if(!found_seektable_block) {
56                 /* create a new block */
57                 block = FLAC__metadata_object_new(FLAC__METADATA_TYPE_SEEKTABLE);
58                 if(0 == block)
59                         die("out of memory allocating SEEKTABLE block");
60                 while(FLAC__metadata_iterator_prev(iterator))
61                         ;
62                 if(!FLAC__metadata_iterator_insert_block_after(iterator, block)) {
63                         fprintf(stderr, "%s: ERROR: adding new SEEKTABLE block to metadata, status =\"%s\"\n", filename, FLAC__Metadata_ChainStatusString[FLAC__metadata_chain_status(chain)]);
64                         return false;
65                 }
66                 /* iterator is left pointing to new block */
67                 FLAC__ASSERT(FLAC__metadata_iterator_get_block(iterator) == block);
68         }
69
70         FLAC__metadata_iterator_delete(iterator);
71
72         FLAC__ASSERT(0 != block);
73         FLAC__ASSERT(block->type == FLAC__METADATA_TYPE_SEEKTABLE);
74
75         if(!grabbag__seektable_convert_specification_to_template(specification, /*only_explicit_placeholders=*/false, total_samples, sample_rate, block, /*spec_has_real_points=*/0)) {
76                 fprintf(stderr, "%s: ERROR (internal) preparing seektable with seekpoints\n", filename);
77                 return false;
78         }
79
80         ok = populate_seekpoint_values(filename, block, needs_write);
81
82         if(ok) {
83                 //@@@@ compact it
84         }
85
86         return ok;
87 }
88
89 /*
90  * local routines
91  */
92
93 typedef struct {
94         FLAC__StreamMetadata_SeekTable *seektable_template;
95         FLAC__uint64 samples_written;
96         unsigned first_seekpoint_to_check;
97         FLAC__bool error_occurred;
98         FLAC__StreamDecoderErrorStatus error_status;
99 } ClientData;
100
101 static FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__FileDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
102 {
103         ClientData *cd = (ClientData*)client_data;
104
105         FLAC__ASSERT(0 != cd);
106
107         if(!cd->error_occurred && cd->seektable_template->num_points > 0) {
108                 const unsigned blocksize = frame->header.blocksize;
109                 //@@@@
110                 cd->samples_written += blocksize;
111                 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
112         }
113         else
114                 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
115 }
116 #if 0
117 @@@@
118 {
119         const unsigned blocksize = FLAC__stream_encoder_get_blocksize(encoder);
120         const FLAC__uint64 frame_first_sample = seekable_stream_encoder->private_->samples_written;
121         const FLAC__uint64 frame_last_sample = frame_first_sample + (FLAC__uint64)blocksize - 1;
122         FLAC__uint64 test_sample;
123         unsigned i;
124         for(i = seekable_stream_encoder->private_->first_seekpoint_to_check; i < seekable_stream_encoder->private_->seek_table->num_points; i++) {
125                 test_sample = seekable_stream_encoder->private_->seek_table->points[i].sample_number;
126                 if(test_sample > frame_last_sample) {
127                         break;
128                 }
129                 else if(test_sample >= frame_first_sample) {
130                         seekable_stream_encoder->private_->seek_table->points[i].sample_number = frame_first_sample;
131                         seekable_stream_encoder->private_->seek_table->points[i].stream_offset = seekable_stream_encoder->private_->bytes_written - seekable_stream_encoder->private_->stream_offset;
132                         seekable_stream_encoder->private_->seek_table->points[i].frame_samples = blocksize;
133                         seekable_stream_encoder->private_->first_seekpoint_to_check++;
134                         /* DO NOT: "break;" and here's why:
135                          * The seektable template may contain more than one target
136                          * sample for any given frame; we will keep looping, generating
137                          * duplicate seekpoints for them, and we'll clean it up later,
138                          * just before writing the seektable back to the metadata.
139                          */
140                 }
141                 else {
142                         seekable_stream_encoder->private_->first_seekpoint_to_check++;
143                 }
144         }
145 }
146 #endif
147
148 static void metadata_callback_(const FLAC__FileDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
149 {
150         (void)decoder, (void)metadata, (void)client_data;
151         FLAC__ASSERT(0); /* we asked to skip all metadata */
152 }
153
154 static void error_callback_(const FLAC__FileDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
155 {
156         ClientData *cd = (ClientData*)client_data;
157
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
166 FLAC__bool populate_seekpoint_values(const char *filename, FLAC__StreamMetadata *block, FLAC__bool *needs_write)
167 {
168         FLAC__FileDecoder *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.first_seekpoint_to_check = 0;
178         client_data.error_occurred = false;
179
180         decoder = FLAC__file_decoder_new();
181
182         if(0 == decoder) {
183                 fprintf(stderr, "%s: ERROR (--add-seekpoint) creating the decoder instance\n", filename);
184                 return false;
185         }
186
187         FLAC__file_decoder_set_md5_checking(decoder, false);
188         FLAC__file_decoder_set_filename(decoder, filename);
189         FLAC__file_decoder_set_metadata_ignore_all(decoder);
190         FLAC__file_decoder_set_write_callback(decoder, write_callback_);
191         FLAC__file_decoder_set_metadata_callback(decoder, metadata_callback_);
192         FLAC__file_decoder_set_error_callback(decoder, error_callback_);
193         FLAC__file_decoder_set_client_data(decoder, &client_data);
194
195         if(FLAC__file_decoder_init(decoder) != FLAC__FILE_DECODER_OK) {
196                 fprintf(stderr, "%s: ERROR (--add-seekpoint) initializing the decoder instance (%s)\n", filename, FLAC__file_decoder_get_resolved_state_string(decoder));
197                 ok = false;
198         }
199
200         if(ok && !FLAC__file_decoder_process_until_end_of_file(decoder)) {
201                 fprintf(stderr, "%s: ERROR (--add-seekpoint) decoding file (%s)\n", filename, FLAC__file_decoder_get_resolved_state_string(decoder));
202                 ok = false;
203         }
204
205         if(ok && client_data.error_occurred) {
206                 fprintf(stderr, "%s: ERROR (--add-seekpoint) decoding file (%u:%s)\n", filename, (unsigned)client_data.error_status, FLAC__StreamDecoderErrorStatusString[client_data.error_status]);
207                 ok = false;
208         }
209
210         FLAC__file_decoder_delete(decoder);
211         return ok;
212 }