ffmpeg load_defaults, create default codec file def
[goodguy/history.git] / cinelerra-5.0 / cinelerra / asset.C
1 /*
2  * CINELERRA
3  * Copyright (C) 2010 Adam Williams <broadcast at earthling dot net>
4  * 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (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
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  * 
19  */
20
21
22 #include "asset.h"
23 #include "assets.h"
24 #include "bchash.h"
25 #include "bcsignals.h"
26 #include "clip.h"
27 #include "edl.h"
28 #include "file.h"
29 #include "filesystem.h"
30 #include "filexml.h"
31 #include "format.inc"
32 #include "indexstate.h"
33 #include "quicktime.h"
34
35 #include <stdio.h>
36 #include <string.h>
37
38
39 Asset::Asset()
40  : Indexable(1), ListItem<Asset>()
41 {
42         init_values();
43 }
44
45 Asset::Asset(Asset &asset)
46  : Indexable(1), ListItem<Asset>()
47 {
48         init_values();
49         this->copy_from(&asset, 1);
50 }
51
52 Asset::Asset(const char *path)
53  : Indexable(1), ListItem<Asset>()
54 {
55         init_values();
56         strcpy(this->path, path);
57 }
58
59 Asset::Asset(const int plugin_type, const char *plugin_title)
60  : Indexable(1), ListItem<Asset>()
61 {
62         init_values();
63 }
64
65 Asset::~Asset()
66 {
67 }
68
69 int Asset::init_values()
70 {
71         path[0] = 0;
72 //      format = FILE_MOV;
73 // Has to be unknown for file probing to succeed
74         format = FILE_UNKNOWN;
75         fformat[0] = 0;
76         bits = 0;
77         byte_order = 0;
78         signed_ = 0;
79         header = 0;
80         dither = 0;
81         reset_audio();
82         reset_video();
83
84         strcpy(vcodec, QUICKTIME_YUV2);
85         strcpy(acodec, QUICKTIME_TWOS);
86
87         ff_audio_options[0] = 0;
88         ff_video_options[0] = 0;
89         ff_audio_bitrate = 0;
90         ff_video_bitrate = 0;
91         ff_video_quality = 0;
92
93         jpeg_quality = 100;
94         aspect_ratio = -1;
95
96         ampeg_bitrate = 256;
97         ampeg_derivative = 3;
98
99         vorbis_vbr = 0;
100         vorbis_min_bitrate = -1;
101         vorbis_bitrate = 128000;
102         vorbis_max_bitrate = -1;
103
104         theora_fix_bitrate = 1;
105         theora_bitrate = 860000;
106         theora_quality = 16;
107         theora_sharpness = 2;
108         theora_keyframe_frequency = 64;
109         theora_keyframe_force_frequency = 64;
110
111         mp3_bitrate = 256000;
112
113
114         mp4a_bitrate = 256000;
115         mp4a_quantqual = 100;
116
117
118
119 // mpeg parameters
120         vmpeg_iframe_distance = 45;
121         vmpeg_pframe_distance = 0;
122         vmpeg_progressive = 0;
123         vmpeg_denoise = 1;
124         vmpeg_bitrate = 1000000;
125         vmpeg_derivative = 1;
126         vmpeg_quantization = 15;
127         vmpeg_cmodel = BC_YUV420P;
128         vmpeg_fix_bitrate = 0;
129         vmpeg_seq_codes = 0;
130         vmpeg_preset = 0;
131         vmpeg_field_order = 0;
132
133 // Divx parameters.  BC_Hash from encore2
134         divx_bitrate = 2000000;
135         divx_rc_period = 50;
136         divx_rc_reaction_ratio = 45;
137         divx_rc_reaction_period = 10;
138         divx_max_key_interval = 250;
139         divx_max_quantizer = 31;
140         divx_min_quantizer = 1;
141         divx_quantizer = 5;
142         divx_quality = 5;
143         divx_fix_bitrate = 1;
144         divx_use_deblocking = 1;
145
146         h264_bitrate = 2000000;
147         h264_quantizer = 5;
148         h264_fix_bitrate = 0;
149
150         ms_bitrate = 1000000;
151         ms_bitrate_tolerance = 500000;
152         ms_quantization = 10;
153         ms_interlaced = 0;
154         ms_gop_size = 45;
155         ms_fix_bitrate = 1;
156
157         ac3_bitrate = 128;
158
159         png_use_alpha = 0;
160         exr_use_alpha = 0;
161         exr_compression = 0;
162
163         tiff_cmodel = 0;
164         tiff_compression = 0;
165
166         use_header = 1;
167
168         id = EDL::next_id();
169
170         return 0;
171 }
172
173 void Asset::reset_audio()
174 {
175         audio_data = 0;
176         channels = 0;
177         sample_rate = 0;
178         audio_length = 0;
179 }
180
181 void Asset::reset_video()
182 {
183         video_data = 0;
184         layers = 0;
185         actual_width = width = 0;
186         actual_height = height = 0;
187         video_length = 0;
188         vmpeg_cmodel = BC_YUV420P;
189         frame_rate = 0;
190         program = -1;
191 }
192
193
194 void Asset::boundaries()
195 {
196 //printf("Asset::boundaries %d %d %f\n", __LINE__, sample_rate, frame_rate);
197 // sample_rate & frame_rate are user defined
198 //      CLAMP(sample_rate, 1, 1000000);
199 //      CLAMP(frame_rate, 0.001, 1000000);
200         CLAMP(channels, 0, 16);
201         CLAMP(width, 0, 10000);
202         CLAMP(height, 0, 10000);
203 //printf("Asset::boundaries %d %d %f\n", __LINE__, sample_rate, frame_rate);
204 }
205
206
207 void Asset::reset_index()
208 {
209         index_state->reset();
210 }
211
212 void Asset::copy_from(Asset *asset, int do_index)
213 {
214         copy_location(asset);
215         copy_format(asset, do_index);
216 }
217
218 void Asset::copy_location(Asset *asset)
219 {
220         strcpy(this->path, asset->path);
221         strcpy(this->folder, asset->folder);
222 }
223
224 void Asset::copy_format(Asset *asset, int do_index)
225 {
226         if(do_index) update_index(asset);
227
228         audio_data = asset->audio_data;
229         format = asset->format;
230         strcpy(fformat, asset->fformat);
231         channels = asset->channels;
232         sample_rate = asset->sample_rate;
233         bits = asset->bits;
234         byte_order = asset->byte_order;
235         signed_ = asset->signed_;
236         header = asset->header;
237         dither = asset->dither;
238         mp3_bitrate = asset->mp3_bitrate;
239         mp4a_bitrate = asset->mp4a_bitrate;
240         mp4a_quantqual = asset->mp4a_quantqual;
241         use_header = asset->use_header;
242         aspect_ratio = asset->aspect_ratio;
243
244         video_data = asset->video_data;
245         layers = asset->layers;
246         program = asset->program;
247         frame_rate = asset->frame_rate;
248         width = asset->width;
249         height = asset->height;
250         actual_width = asset->actual_width;
251         actual_height = asset->actual_height;
252         strcpy(vcodec, asset->vcodec);
253         strcpy(acodec, asset->acodec);
254
255         strcpy(ff_audio_options, asset->ff_audio_options);
256         strcpy(ff_video_options, asset->ff_video_options);
257         ff_audio_bitrate = asset->ff_audio_bitrate;
258         ff_video_bitrate = asset->ff_video_bitrate;
259         ff_video_quality = asset->ff_video_quality;
260
261         this->audio_length = asset->audio_length;
262         this->video_length = asset->video_length;
263
264
265         ampeg_bitrate = asset->ampeg_bitrate;
266         ampeg_derivative = asset->ampeg_derivative;
267
268
269         vorbis_vbr = asset->vorbis_vbr;
270         vorbis_min_bitrate = asset->vorbis_min_bitrate;
271         vorbis_bitrate = asset->vorbis_bitrate;
272         vorbis_max_bitrate = asset->vorbis_max_bitrate;
273
274         
275         theora_fix_bitrate = asset->theora_fix_bitrate;
276         theora_bitrate = asset->theora_bitrate;
277         theora_quality = asset->theora_quality;
278         theora_sharpness = asset->theora_sharpness;
279         theora_keyframe_frequency = asset->theora_keyframe_frequency;
280         theora_keyframe_force_frequency = asset->theora_keyframe_frequency;
281
282
283         jpeg_quality = asset->jpeg_quality;
284
285 // mpeg parameters
286         vmpeg_iframe_distance = asset->vmpeg_iframe_distance;
287         vmpeg_pframe_distance = asset->vmpeg_pframe_distance;
288         vmpeg_progressive = asset->vmpeg_progressive;
289         vmpeg_denoise = asset->vmpeg_denoise;
290         vmpeg_bitrate = asset->vmpeg_bitrate;
291         vmpeg_derivative = asset->vmpeg_derivative;
292         vmpeg_quantization = asset->vmpeg_quantization;
293         vmpeg_cmodel = asset->vmpeg_cmodel;
294         vmpeg_fix_bitrate = asset->vmpeg_fix_bitrate;
295         vmpeg_seq_codes = asset->vmpeg_seq_codes;
296         vmpeg_preset = asset->vmpeg_preset;
297         vmpeg_field_order = asset->vmpeg_field_order;
298
299
300         divx_bitrate = asset->divx_bitrate;
301         divx_rc_period = asset->divx_rc_period;
302         divx_rc_reaction_ratio = asset->divx_rc_reaction_ratio;
303         divx_rc_reaction_period = asset->divx_rc_reaction_period;
304         divx_max_key_interval = asset->divx_max_key_interval;
305         divx_max_quantizer = asset->divx_max_quantizer;
306         divx_min_quantizer = asset->divx_min_quantizer;
307         divx_quantizer = asset->divx_quantizer;
308         divx_quality = asset->divx_quality;
309         divx_fix_bitrate = asset->divx_fix_bitrate;
310         divx_use_deblocking = asset->divx_use_deblocking;
311
312         h264_bitrate = asset->h264_bitrate;
313         h264_quantizer = asset->h264_quantizer;
314         h264_fix_bitrate = asset->h264_fix_bitrate;
315
316
317         ms_bitrate = asset->ms_bitrate;
318         ms_bitrate_tolerance = asset->ms_bitrate_tolerance;
319         ms_interlaced = asset->ms_interlaced;
320         ms_quantization = asset->ms_quantization;
321         ms_gop_size = asset->ms_gop_size;
322         ms_fix_bitrate = asset->ms_fix_bitrate;
323
324         
325         ac3_bitrate = asset->ac3_bitrate;
326         
327         png_use_alpha = asset->png_use_alpha;
328         exr_use_alpha = asset->exr_use_alpha;
329         exr_compression = asset->exr_compression;
330
331         tiff_cmodel = asset->tiff_cmodel;
332         tiff_compression = asset->tiff_compression;
333 }
334
335 int64_t Asset::get_index_offset(int channel)
336 {
337         return index_state->get_index_offset(channel);
338 }
339
340 int64_t Asset::get_index_size(int channel)
341 {
342         return index_state->get_index_size(channel);
343 }
344
345
346 char* Asset::get_compression_text(int audio, int video)
347 {
348         if(audio)
349         {
350                 switch(format)
351                 {
352                         case FILE_MOV:
353                         case FILE_AVI:
354                                 if(acodec[0])
355                                         return quicktime_acodec_title(acodec);
356                                 else
357                                         return 0;
358                                 break;
359                 }
360         }
361         else
362         if(video)
363         {
364                 switch(format)
365                 {
366                         case FILE_MOV:
367                         case FILE_AVI:
368                                 if(vcodec[0])
369                                         return quicktime_vcodec_title(vcodec);
370                                 else
371                                         return 0;
372                                 break;
373                 }
374         }
375         return 0;
376 }
377
378 Asset& Asset::operator=(Asset &asset)
379 {
380 printf("Asset::operator=\n");
381         copy_location(&asset);
382         copy_format(&asset, 1);
383         return *this;
384 }
385
386
387 int Asset::equivalent(Asset &asset, 
388         int test_audio, 
389         int test_video)
390 {
391         int result = (!strcmp(asset.path, path) &&
392                 format == asset.format);
393
394         if(result && format == FILE_FFMPEG)
395                 result = !strcmp(fformat, asset.fformat);
396
397         if(test_audio && result)
398         {
399                 result = (channels == asset.channels && 
400                         sample_rate == asset.sample_rate && 
401                         bits == asset.bits && 
402                         byte_order == asset.byte_order && 
403                         signed_ == asset.signed_ && 
404                         header == asset.header && 
405                         dither == asset.dither &&
406                         !strcmp(acodec, asset.acodec));
407                 if(result && format == FILE_FFMPEG)
408                         result = !strcmp(ff_audio_options, asset.ff_audio_options) &&
409                                 ff_audio_bitrate == asset.ff_audio_bitrate;
410                                 
411         }
412
413
414         if(test_video && result)
415         {
416                 result = (layers == asset.layers && 
417                         program == asset.program && 
418                         frame_rate == asset.frame_rate &&
419                         width == asset.width &&
420                         height == asset.height &&
421                         !strcmp(vcodec, asset.vcodec));
422                 if(result && format == FILE_FFMPEG)
423                         result = !strcmp(ff_video_options, asset.ff_video_options) &&
424                                 ff_video_bitrate == asset.ff_video_bitrate &&
425                                 ff_video_quality == asset.ff_video_quality;
426         }
427
428         return result;
429 }
430
431 int Asset::operator==(Asset &asset)
432 {
433
434         return equivalent(asset, 
435                 1, 
436                 1);
437 }
438
439 int Asset::operator!=(Asset &asset)
440 {
441         return !(*this == asset);
442 }
443
444 int Asset::test_path(const char *path)
445 {
446         if(!strcasecmp(this->path, path)) 
447                 return 1; 
448         else 
449                 return 0;
450 }
451
452 int Asset::test_plugin_title(const char *path)
453 {
454         return 0;
455 }
456
457 int Asset::read(FileXML *file, 
458         int expand_relative)
459 {
460         int result = 0;
461
462 // Check for relative path.
463         if(expand_relative)
464         {
465                 char new_path[BCTEXTLEN];
466                 char asset_directory[BCTEXTLEN];
467                 char input_directory[BCTEXTLEN];
468                 FileSystem fs;
469
470                 strcpy(new_path, path);
471                 fs.set_current_dir("");
472
473                 fs.extract_dir(asset_directory, path);
474
475 // No path in asset.
476 // Take path of XML file.
477                 if(!asset_directory[0])
478                 {
479                         fs.extract_dir(input_directory, file->filename);
480
481 // Input file has a path
482                         if(input_directory[0])
483                         {
484                                 fs.join_names(path, input_directory, new_path);
485                         }
486                 }
487         }
488
489
490         while(!result)
491         {
492                 result = file->read_tag();
493                 if(!result)
494                 {
495                         if(file->tag.title_is("/ASSET"))
496                         {
497                                 result = 1;
498                         }
499                         else
500                         if(file->tag.title_is("AUDIO"))
501                         {
502                                 read_audio(file);
503                         }
504                         else
505                         if(file->tag.title_is("AUDIO_OMIT"))
506                         {
507                                 read_audio(file);
508                         }
509                         else
510                         if(file->tag.title_is("FORMAT"))
511                         {
512                                 const char *string = file->tag.get_property("TYPE");
513                                 format = File::strtoformat(string);
514                                 use_header = 
515                                         file->tag.get_property("USE_HEADER", use_header);
516                                 file->tag.get_property("FFORMAT", fformat);
517                         }
518                         else
519                         if(file->tag.title_is("FOLDER"))
520                         {
521                                 strcpy(folder, file->read_text());
522                         }
523                         else
524                         if(file->tag.title_is("VIDEO"))
525                         {
526                                 read_video(file);
527                         }
528                         else
529                         if(file->tag.title_is("VIDEO_OMIT"))
530                         {
531                                 read_video(file);
532                         }
533                         else
534                         if(file->tag.title_is("INDEX"))
535                         {
536                                 read_index(file);
537                         }
538                 }
539         }
540
541         boundaries();
542 //printf("Asset::read 2\n");
543         return 0;
544 }
545
546 int Asset::read_audio(FileXML *file)
547 {
548         if(file->tag.title_is("AUDIO")) audio_data = 1;
549         channels = file->tag.get_property("CHANNELS", 2);
550 // This is loaded from the index file after the EDL but this 
551 // should be overridable in the EDL.
552         if(!sample_rate) sample_rate = file->tag.get_property("RATE", 44100);
553         bits = file->tag.get_property("BITS", 16);
554         byte_order = file->tag.get_property("BYTE_ORDER", 1);
555         signed_ = file->tag.get_property("SIGNED", 1);
556         header = file->tag.get_property("HEADER", 0);
557         dither = file->tag.get_property("DITHER", 0);
558
559         audio_length = file->tag.get_property("AUDIO_LENGTH", (int64_t)0);
560         acodec[0] = 0;
561         file->tag.get_property("ACODEC", acodec);
562
563         return 0;
564 }
565
566 int Asset::read_video(FileXML *file)
567 {
568         if(file->tag.title_is("VIDEO")) video_data = 1;
569         actual_height = file->tag.get_property("ACTUAL_HEIGHT", actual_height);
570         actual_width = file->tag.get_property("ACTUAL_WIDTH", actual_width);
571         height = file->tag.get_property("HEIGHT", height);
572         width = file->tag.get_property("WIDTH", width);
573         layers = file->tag.get_property("LAYERS", layers);
574         program = file->tag.get_property("PROGRAM", program);
575 // This is loaded from the index file after the EDL but this 
576 // should be overridable in the EDL.
577         if(EQUIV(frame_rate, 0)) frame_rate = file->tag.get_property("FRAMERATE", frame_rate);
578         vcodec[0] = 0;
579         file->tag.get_property("VCODEC", vcodec);
580
581         video_length = file->tag.get_property("VIDEO_LENGTH", (int64_t)0);
582
583         return 0;
584 }
585
586 int Asset::read_index(FileXML *file)
587 {
588         index_state->read_xml(file, channels);
589         return 0;
590 }
591
592 int Asset::write_index(const char *path, int data_bytes)
593 {
594         return index_state->write_index(path, data_bytes, this, audio_length);
595 }
596
597 // Output path is the path of the output file if name truncation is desired.
598 // It is a "" if complete names should be used.
599
600 int Asset::write(FileXML *file, 
601         int include_index, 
602         const char *output_path)
603 {
604         char new_path[BCTEXTLEN];
605         char asset_directory[BCTEXTLEN];
606         char output_directory[BCTEXTLEN];
607         FileSystem fs;
608
609 // Make path relative
610         fs.extract_dir(asset_directory, path);
611         if(output_path && output_path[0]) 
612                 fs.extract_dir(output_directory, output_path);
613         else
614                 output_directory[0] = 0;
615
616 // Asset and EDL are in same directory.  Extract just the name.
617         if(!strcmp(asset_directory, output_directory))
618         {
619                 fs.extract_name(new_path, path);
620         }
621         else
622         {
623                 strcpy(new_path, path);
624         }
625
626         file->tag.set_title("ASSET");
627         file->tag.set_property("SRC", new_path);
628         file->append_tag();
629         file->append_newline();
630
631         file->tag.set_title("FOLDER");
632         file->append_tag();
633         file->append_text(folder);
634         file->tag.set_title("/FOLDER");
635         file->append_tag();
636         file->append_newline();
637
638 // Write the format information
639         file->tag.set_title("FORMAT");
640
641         file->tag.set_property("TYPE", 
642                 File::formattostr(format));
643         file->tag.set_property("USE_HEADER", use_header);
644         file->tag.set_property("FFORMAT", fformat);
645
646         file->append_tag();
647         file->append_newline();
648
649 // Requiring data to exist caused batch render to lose settings.
650 // But the only way to know if an asset doesn't have audio or video data 
651 // is to not write the block.
652 // So change the block name if the asset doesn't have the data.
653         write_audio(file);
654         write_video(file);
655 // index goes after source
656         if(index_state->index_status == INDEX_READY && include_index) 
657                 write_index(file);  
658
659         file->tag.set_title("/ASSET");
660         file->append_tag();
661         file->append_newline();
662         return 0;
663 }
664
665 int Asset::write_audio(FileXML *file)
666 {
667 // Let the reader know if the asset has the data by naming the block.
668         if(audio_data)
669                 file->tag.set_title("AUDIO");
670         else
671                 file->tag.set_title("AUDIO_OMIT");
672 // Necessary for PCM audio
673         file->tag.set_property("CHANNELS", channels);
674         file->tag.set_property("RATE", sample_rate);
675         file->tag.set_property("BITS", bits);
676         file->tag.set_property("BYTE_ORDER", byte_order);
677         file->tag.set_property("SIGNED", signed_);
678         file->tag.set_property("HEADER", header);
679         file->tag.set_property("DITHER", dither);
680         if(acodec[0])
681                 file->tag.set_property("ACODEC", acodec);
682         
683         file->tag.set_property("AUDIO_LENGTH", audio_length);
684
685
686
687 // Rely on defaults operations for these.
688
689 //      file->tag.set_property("AMPEG_BITRATE", ampeg_bitrate);
690 //      file->tag.set_property("AMPEG_DERIVATIVE", ampeg_derivative);
691 // 
692 //      file->tag.set_property("VORBIS_VBR", vorbis_vbr);
693 //      file->tag.set_property("VORBIS_MIN_BITRATE", vorbis_min_bitrate);
694 //      file->tag.set_property("VORBIS_BITRATE", vorbis_bitrate);
695 //      file->tag.set_property("VORBIS_MAX_BITRATE", vorbis_max_bitrate);
696 // 
697 //      file->tag.set_property("MP3_BITRATE", mp3_bitrate);
698 // 
699
700
701
702         file->append_tag();
703         file->append_newline();
704         return 0;
705 }
706
707 int Asset::write_video(FileXML *file)
708 {
709         if(video_data)
710                 file->tag.set_title("VIDEO");
711         else
712                 file->tag.set_title("VIDEO_OMIT");
713         file->tag.set_property("ACTUAL_HEIGHT", actual_height);
714         file->tag.set_property("ACTUAL_WIDTH", actual_width);
715         file->tag.set_property("HEIGHT", height);
716         file->tag.set_property("WIDTH", width);
717         file->tag.set_property("LAYERS", layers);
718         file->tag.set_property("PROGRAM", program);
719         file->tag.set_property("FRAMERATE", frame_rate);
720         if(vcodec[0])
721                 file->tag.set_property("VCODEC", vcodec);
722
723         file->tag.set_property("VIDEO_LENGTH", video_length);
724
725
726
727
728         file->append_tag();
729         file->append_newline();
730         return 0;
731 }
732
733 int Asset::write_index(FileXML *file)
734 {
735         index_state->write_xml(file);
736         return 0;
737 }
738
739
740
741
742 const char* Asset::construct_param(const char *param, 
743         const char *prefix, 
744         char *return_value)
745 {
746         if(prefix)
747                 sprintf(return_value, "%s%s", prefix, param);
748         else
749                 strcpy(return_value, param);
750         return return_value;
751 }
752
753 #define UPDATE_DEFAULT(x, y) defaults->update(construct_param(x, prefix, string), y);
754 #define GET_DEFAULT(x, y) defaults->get(construct_param(x, prefix, string), y);
755
756 void Asset::load_defaults(BC_Hash *defaults, 
757         const char *prefix, 
758         int do_format,
759         int do_compression,
760         int do_path,
761         int do_data_types,
762         int do_bits)
763 {
764         char string[BCTEXTLEN];
765
766 // Can't save codec here because it's specific to render, record, and effect.
767 // The codec has to be UNKNOWN for file probing to work.
768
769         if(do_path)
770         {
771                 GET_DEFAULT("PATH", path);
772         }
773
774         if(do_compression)
775         {
776                 GET_DEFAULT("AUDIO_CODEC", acodec);
777                 GET_DEFAULT("VIDEO_CODEC", vcodec);
778         }
779
780         if(do_format)
781         {
782                 format = GET_DEFAULT("FORMAT", format);
783                 use_header = GET_DEFAULT("USE_HEADER", use_header);
784                 GET_DEFAULT("FFORMAT", fformat);
785         }
786
787         if(do_data_types)
788         {
789                 audio_data = GET_DEFAULT("AUDIO", 1);
790                 video_data = GET_DEFAULT("VIDEO", 1);
791         }
792
793         if(do_bits)
794         {
795                 bits = GET_DEFAULT("BITS", 16);
796                 dither = GET_DEFAULT("DITHER", 0);
797                 signed_ = GET_DEFAULT("SIGNED", 1);
798                 byte_order = GET_DEFAULT("BYTE_ORDER", 1);
799
800
801
802 // Used by filefork
803                 channels = GET_DEFAULT("CHANNELS", 2);
804                 if(!sample_rate) sample_rate = GET_DEFAULT("RATE", 44100);
805                 header = GET_DEFAULT("HEADER", 0);
806                 audio_length = GET_DEFAULT("AUDIO_LENGTH", (int64_t)0);
807
808
809
810                 height = GET_DEFAULT("HEIGHT", height);
811                 width = GET_DEFAULT("WIDTH", width);
812                 actual_height = GET_DEFAULT("ACTUAL_HEIGHT", actual_height);
813                 actual_width = GET_DEFAULT("ACTUAL_WIDTH", actual_width);
814                 program = GET_DEFAULT("PROGRAM", program);
815                 layers = GET_DEFAULT("LAYERS", layers);
816                 if(EQUIV(frame_rate, 0)) frame_rate = GET_DEFAULT("FRAMERATE", frame_rate);
817                 video_length = GET_DEFAULT("VIDEO_LENGTH", (int64_t)0);
818         }
819
820         ampeg_bitrate = GET_DEFAULT("AMPEG_BITRATE", ampeg_bitrate);
821         ampeg_derivative = GET_DEFAULT("AMPEG_DERIVATIVE", ampeg_derivative);
822
823         vorbis_vbr = GET_DEFAULT("VORBIS_VBR", vorbis_vbr);
824         vorbis_min_bitrate = GET_DEFAULT("VORBIS_MIN_BITRATE", vorbis_min_bitrate);
825         vorbis_bitrate = GET_DEFAULT("VORBIS_BITRATE", vorbis_bitrate);
826         vorbis_max_bitrate = GET_DEFAULT("VORBIS_MAX_BITRATE", vorbis_max_bitrate);
827
828         theora_fix_bitrate = GET_DEFAULT("THEORA_FIX_BITRATE", theora_fix_bitrate);
829         theora_bitrate = GET_DEFAULT("THEORA_BITRATE", theora_bitrate);
830         theora_quality = GET_DEFAULT("THEORA_QUALITY", theora_quality);
831         theora_sharpness = GET_DEFAULT("THEORA_SHARPNESS", theora_sharpness);
832         theora_keyframe_frequency = GET_DEFAULT("THEORA_KEYFRAME_FREQUENCY", theora_keyframe_frequency);
833         theora_keyframe_force_frequency = GET_DEFAULT("THEORA_FORCE_KEYFRAME_FREQUENCY", theora_keyframe_force_frequency);
834
835         GET_DEFAULT("FF_AUDIO_OPTIONS", ff_audio_options);
836         ff_audio_bitrate = GET_DEFAULT("FF_AUDIO_BITRATE", ff_audio_bitrate);
837         GET_DEFAULT("FF_VIDEO_OPTIONS", ff_video_options);
838         ff_video_bitrate = GET_DEFAULT("FF_VIDEO_BITRATE", ff_video_bitrate);
839         ff_video_quality = GET_DEFAULT("FF_VIDEO_QUALITY", ff_video_quality);
840
841         mp3_bitrate = GET_DEFAULT("MP3_BITRATE", mp3_bitrate);
842         mp4a_bitrate = GET_DEFAULT("MP4A_BITRATE", mp4a_bitrate);
843         mp4a_quantqual = GET_DEFAULT("MP4A_QUANTQUAL", mp4a_quantqual);
844
845         jpeg_quality = GET_DEFAULT("JPEG_QUALITY", jpeg_quality);
846         aspect_ratio = GET_DEFAULT("ASPECT_RATIO", aspect_ratio);
847
848 // MPEG format information
849         vmpeg_iframe_distance = GET_DEFAULT("VMPEG_IFRAME_DISTANCE", vmpeg_iframe_distance);
850         vmpeg_pframe_distance = GET_DEFAULT("VMPEG_PFRAME_DISTANCE", vmpeg_pframe_distance);
851         vmpeg_progressive = GET_DEFAULT("VMPEG_PROGRESSIVE", vmpeg_progressive);
852         vmpeg_denoise = GET_DEFAULT("VMPEG_DENOISE", vmpeg_denoise);
853         vmpeg_bitrate = GET_DEFAULT("VMPEG_BITRATE", vmpeg_bitrate);
854         vmpeg_derivative = GET_DEFAULT("VMPEG_DERIVATIVE", vmpeg_derivative);
855         vmpeg_quantization = GET_DEFAULT("VMPEG_QUANTIZATION", vmpeg_quantization);
856         vmpeg_cmodel = GET_DEFAULT("VMPEG_CMODEL", vmpeg_cmodel);
857         vmpeg_fix_bitrate = GET_DEFAULT("VMPEG_FIX_BITRATE", vmpeg_fix_bitrate);
858         vmpeg_seq_codes = GET_DEFAULT("VMPEG_SEQ_CODES", vmpeg_seq_codes);
859         vmpeg_preset = GET_DEFAULT("VMPEG_PRESET", vmpeg_preset);
860         vmpeg_field_order = GET_DEFAULT("VMPEG_FIELD_ORDER", vmpeg_field_order);
861
862         h264_bitrate = GET_DEFAULT("H264_BITRATE", h264_bitrate);
863         h264_quantizer = GET_DEFAULT("H264_QUANTIZER", h264_quantizer);
864         h264_fix_bitrate = GET_DEFAULT("H264_FIX_BITRATE", h264_fix_bitrate);
865
866
867         divx_bitrate = GET_DEFAULT("DIVX_BITRATE", divx_bitrate);
868         divx_rc_period = GET_DEFAULT("DIVX_RC_PERIOD", divx_rc_period);
869         divx_rc_reaction_ratio = GET_DEFAULT("DIVX_RC_REACTION_RATIO", divx_rc_reaction_ratio);
870         divx_rc_reaction_period = GET_DEFAULT("DIVX_RC_REACTION_PERIOD", divx_rc_reaction_period);
871         divx_max_key_interval = GET_DEFAULT("DIVX_MAX_KEY_INTERVAL", divx_max_key_interval);
872         divx_max_quantizer = GET_DEFAULT("DIVX_MAX_QUANTIZER", divx_max_quantizer);
873         divx_min_quantizer = GET_DEFAULT("DIVX_MIN_QUANTIZER", divx_min_quantizer);
874         divx_quantizer = GET_DEFAULT("DIVX_QUANTIZER", divx_quantizer);
875         divx_quality = GET_DEFAULT("DIVX_QUALITY", divx_quality);
876         divx_fix_bitrate = GET_DEFAULT("DIVX_FIX_BITRATE", divx_fix_bitrate);
877         divx_use_deblocking = GET_DEFAULT("DIVX_USE_DEBLOCKING", divx_use_deblocking);
878
879         ms_bitrate = GET_DEFAULT("MS_BITRATE", ms_bitrate);
880         ms_bitrate_tolerance = GET_DEFAULT("MS_BITRATE_TOLERANCE", ms_bitrate_tolerance);
881         ms_interlaced = GET_DEFAULT("MS_INTERLACED", ms_interlaced);
882         ms_quantization = GET_DEFAULT("MS_QUANTIZATION", ms_quantization);
883         ms_gop_size = GET_DEFAULT("MS_GOP_SIZE", ms_gop_size);
884         ms_fix_bitrate = GET_DEFAULT("MS_FIX_BITRATE", ms_fix_bitrate);
885
886         ac3_bitrate = GET_DEFAULT("AC3_BITRATE", ac3_bitrate);
887
888         png_use_alpha = GET_DEFAULT("PNG_USE_ALPHA", png_use_alpha);
889         exr_use_alpha = GET_DEFAULT("EXR_USE_ALPHA", exr_use_alpha);
890         exr_compression = GET_DEFAULT("EXR_COMPRESSION", exr_compression);
891         tiff_cmodel = GET_DEFAULT("TIFF_CMODEL", tiff_cmodel);
892         tiff_compression = GET_DEFAULT("TIFF_COMPRESSION", tiff_compression);
893
894         boundaries();
895 }
896
897 void Asset::save_defaults(BC_Hash *defaults, 
898         const char *prefix,
899         int do_format,
900         int do_compression,
901         int do_path,
902         int do_data_types,
903         int do_bits)
904 {
905         char string[BCTEXTLEN];
906
907         UPDATE_DEFAULT("PATH", path);
908
909
910
911
912         if(do_format)
913         {
914                 UPDATE_DEFAULT("FORMAT", format);
915                 UPDATE_DEFAULT("USE_HEADER", use_header);
916                 UPDATE_DEFAULT("FFORMAT", fformat);
917         }
918
919         if(do_data_types)
920         {
921                 UPDATE_DEFAULT("AUDIO", audio_data);
922                 UPDATE_DEFAULT("VIDEO", video_data);
923         }
924
925         if(do_compression)
926         {
927                 UPDATE_DEFAULT("AUDIO_CODEC", acodec);
928                 UPDATE_DEFAULT("VIDEO_CODEC", vcodec);
929
930                 UPDATE_DEFAULT("AMPEG_BITRATE", ampeg_bitrate);
931                 UPDATE_DEFAULT("AMPEG_DERIVATIVE", ampeg_derivative);
932
933                 UPDATE_DEFAULT("VORBIS_VBR", vorbis_vbr);
934                 UPDATE_DEFAULT("VORBIS_MIN_BITRATE", vorbis_min_bitrate);
935                 UPDATE_DEFAULT("VORBIS_BITRATE", vorbis_bitrate);
936                 UPDATE_DEFAULT("VORBIS_MAX_BITRATE", vorbis_max_bitrate);
937
938                 UPDATE_DEFAULT("FF_AUDIO_OPTIONS", ff_audio_options);
939                 UPDATE_DEFAULT("FF_AUDIO_BITRATE", ff_audio_bitrate);
940                 UPDATE_DEFAULT("FF_VIDEO_OPTIONS", ff_video_options);
941                 UPDATE_DEFAULT("FF_VIDEO_BITRATE", ff_video_bitrate);
942                 UPDATE_DEFAULT("FF_VIDEO_QUALITY", ff_video_quality);
943
944                 UPDATE_DEFAULT("THEORA_FIX_BITRATE", theora_fix_bitrate);
945                 UPDATE_DEFAULT("THEORA_BITRATE", theora_bitrate);
946                 UPDATE_DEFAULT("THEORA_QUALITY", theora_quality);
947                 UPDATE_DEFAULT("THEORA_SHARPNESS", theora_sharpness);
948                 UPDATE_DEFAULT("THEORA_KEYFRAME_FREQUENCY", theora_keyframe_frequency);
949                 UPDATE_DEFAULT("THEORA_FORCE_KEYFRAME_FREQUENCY", theora_keyframe_force_frequency);
950
951
952
953                 UPDATE_DEFAULT("MP3_BITRATE", mp3_bitrate);
954                 UPDATE_DEFAULT("MP4A_BITRATE", mp4a_bitrate);
955                 UPDATE_DEFAULT("MP4A_QUANTQUAL", mp4a_quantqual);
956
957
958
959
960
961                 UPDATE_DEFAULT("JPEG_QUALITY", jpeg_quality);
962                 UPDATE_DEFAULT("ASPECT_RATIO", aspect_ratio);
963
964 // MPEG format information
965                 UPDATE_DEFAULT("VMPEG_IFRAME_DISTANCE", vmpeg_iframe_distance);
966                 UPDATE_DEFAULT("VMPEG_PFRAME_DISTANCE", vmpeg_pframe_distance);
967                 UPDATE_DEFAULT("VMPEG_PROGRESSIVE", vmpeg_progressive);
968                 UPDATE_DEFAULT("VMPEG_DENOISE", vmpeg_denoise);
969                 UPDATE_DEFAULT("VMPEG_BITRATE", vmpeg_bitrate);
970                 UPDATE_DEFAULT("VMPEG_DERIVATIVE", vmpeg_derivative);
971                 UPDATE_DEFAULT("VMPEG_QUANTIZATION", vmpeg_quantization);
972                 UPDATE_DEFAULT("VMPEG_CMODEL", vmpeg_cmodel);
973                 UPDATE_DEFAULT("VMPEG_FIX_BITRATE", vmpeg_fix_bitrate);
974                 UPDATE_DEFAULT("VMPEG_SEQ_CODES", vmpeg_seq_codes);
975                 UPDATE_DEFAULT("VMPEG_PRESET", vmpeg_preset);
976                 UPDATE_DEFAULT("VMPEG_FIELD_ORDER", vmpeg_field_order);
977
978                 UPDATE_DEFAULT("H264_BITRATE", h264_bitrate);
979                 UPDATE_DEFAULT("H264_QUANTIZER", h264_quantizer);
980                 UPDATE_DEFAULT("H264_FIX_BITRATE", h264_fix_bitrate);
981
982                 UPDATE_DEFAULT("DIVX_BITRATE", divx_bitrate);
983                 UPDATE_DEFAULT("DIVX_RC_PERIOD", divx_rc_period);
984                 UPDATE_DEFAULT("DIVX_RC_REACTION_RATIO", divx_rc_reaction_ratio);
985                 UPDATE_DEFAULT("DIVX_RC_REACTION_PERIOD", divx_rc_reaction_period);
986                 UPDATE_DEFAULT("DIVX_MAX_KEY_INTERVAL", divx_max_key_interval);
987                 UPDATE_DEFAULT("DIVX_MAX_QUANTIZER", divx_max_quantizer);
988                 UPDATE_DEFAULT("DIVX_MIN_QUANTIZER", divx_min_quantizer);
989                 UPDATE_DEFAULT("DIVX_QUANTIZER", divx_quantizer);
990                 UPDATE_DEFAULT("DIVX_QUALITY", divx_quality);
991                 UPDATE_DEFAULT("DIVX_FIX_BITRATE", divx_fix_bitrate);
992                 UPDATE_DEFAULT("DIVX_USE_DEBLOCKING", divx_use_deblocking);
993
994
995                 UPDATE_DEFAULT("MS_BITRATE", ms_bitrate);
996                 UPDATE_DEFAULT("MS_BITRATE_TOLERANCE", ms_bitrate_tolerance);
997                 UPDATE_DEFAULT("MS_INTERLACED", ms_interlaced);
998                 UPDATE_DEFAULT("MS_QUANTIZATION", ms_quantization);
999                 UPDATE_DEFAULT("MS_GOP_SIZE", ms_gop_size);
1000                 UPDATE_DEFAULT("MS_FIX_BITRATE", ms_fix_bitrate);
1001
1002                 UPDATE_DEFAULT("AC3_BITRATE", ac3_bitrate);
1003
1004
1005                 UPDATE_DEFAULT("PNG_USE_ALPHA", png_use_alpha);
1006                 UPDATE_DEFAULT("EXR_USE_ALPHA", exr_use_alpha);
1007                 UPDATE_DEFAULT("EXR_COMPRESSION", exr_compression);
1008                 UPDATE_DEFAULT("TIFF_CMODEL", tiff_cmodel);
1009                 UPDATE_DEFAULT("TIFF_COMPRESSION", tiff_compression);
1010         }
1011
1012         if(do_bits)
1013         {
1014                 UPDATE_DEFAULT("BITS", bits);
1015                 UPDATE_DEFAULT("DITHER", dither);
1016                 UPDATE_DEFAULT("SIGNED", signed_);
1017                 UPDATE_DEFAULT("BYTE_ORDER", byte_order);
1018
1019
1020
1021
1022
1023
1024 // Used by filefork
1025                 UPDATE_DEFAULT("CHANNELS", channels);
1026                 UPDATE_DEFAULT("RATE", sample_rate);
1027                 UPDATE_DEFAULT("HEADER", header);
1028                 UPDATE_DEFAULT("AUDIO_LENGTH", audio_length);
1029
1030
1031
1032                 UPDATE_DEFAULT("HEIGHT", height);
1033                 UPDATE_DEFAULT("WIDTH", width);
1034                 UPDATE_DEFAULT("ACTUAL_HEIGHT", actual_height);
1035                 UPDATE_DEFAULT("ACTUAL_WIDTH", actual_width);
1036                 UPDATE_DEFAULT("PROGRAM", program);
1037                 UPDATE_DEFAULT("LAYERS", layers);
1038                 UPDATE_DEFAULT("FRAMERATE", frame_rate);
1039                 UPDATE_DEFAULT("VIDEO_LENGTH", video_length);
1040
1041         }
1042 }
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052 int Asset::update_path(char *new_path)
1053 {
1054         strcpy(path, new_path);
1055         return 0;
1056 }
1057
1058 void Asset::update_index(Asset *asset)
1059 {
1060         index_state->copy_from(asset->index_state);
1061 }
1062
1063
1064 int Asset::dump(FILE *fp)
1065 {
1066         fprintf(fp,"  asset::dump\n");
1067         fprintf(fp,"   this=%p path=%s\n", this, path);
1068         fprintf(fp,"   index_status %d\n", index_state->index_status);
1069         fprintf(fp,"   format %d\n", format);
1070         fprintf(fp,"   fformat=\"%s\"\n", fformat);
1071         fprintf(fp,"   ff_audio_options=\"%s\"\n", ff_audio_options);
1072         fprintf(fp,"   ff_audio_bitrate=%d\n", ff_audio_bitrate);
1073         fprintf(fp,"   ff_video_options=\"%s\"\n", ff_video_options);
1074         fprintf(fp,"   ff_video_bitrate=%d\n", ff_video_bitrate);
1075         fprintf(fp,"   ff_video_quality=%d\n", ff_video_quality);
1076         fprintf(fp,"   audio_data %d channels %d samplerate %d bits %d"
1077                 " byte_order %d signed %d header %d dither %d acodec %c%c%c%c\n",
1078                 audio_data, channels, sample_rate, bits, byte_order, signed_,
1079                 header, dither, acodec[0], acodec[1], acodec[2], acodec[3]);
1080         fprintf(fp,"   audio_length " _LD "\n", audio_length);
1081         fprintf(fp,"   video_data %d program %d layers %d framerate %f width %d"
1082                 " height %d vcodec %c%c%c%c aspect_ratio %f\n",
1083                 video_data, layers, program, frame_rate, width, height,
1084                 vcodec[0], vcodec[1], vcodec[2], vcodec[3], aspect_ratio);
1085         fprintf(fp,"   video_length " _LD " \n", video_length);
1086         fprintf(fp,"   ms_bitrate_tolerance=%d\n", ms_bitrate_tolerance);
1087         fprintf(fp,"   ms_quantization=%d\n", ms_quantization);
1088         fprintf(fp,"   ms_fix_bitrate=%d\n", ms_fix_bitrate);
1089         fprintf(fp,"   ms_interlaced=%d\n", ms_interlaced);
1090         fprintf(fp,"   h264_bitrate=%d\n", h264_bitrate);
1091         fprintf(fp,"   h264_quantizer=%d\n", h264_quantizer);
1092         fprintf(fp,"   h264_fix_bitrate=%d\n", h264_fix_bitrate);
1093         return 0;
1094 }
1095
1096
1097 // For Indexable
1098 int Asset::get_audio_channels()
1099 {
1100         return channels;
1101 }
1102
1103 int Asset::get_sample_rate()
1104 {
1105         return sample_rate;
1106 }
1107
1108 int64_t Asset::get_audio_samples()
1109 {
1110         return audio_length;
1111 }
1112
1113 int Asset::have_audio()
1114 {
1115         return audio_data;
1116 }
1117
1118 int Asset::have_video()
1119 {
1120         return video_data;
1121 }
1122
1123 int Asset::get_w()
1124 {
1125         return width;
1126 }
1127
1128 int Asset::get_h()
1129 {
1130         return height;
1131 }
1132
1133 double Asset::get_frame_rate()
1134 {
1135         return frame_rate;
1136 }
1137
1138
1139 int Asset::get_video_layers()
1140 {
1141         return layers;
1142 }
1143
1144 int Asset::get_program()
1145 {
1146         return program;
1147 }
1148
1149 int64_t Asset::get_video_frames()
1150 {
1151         return video_length;
1152 }