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