batchrender asset path fix, ru xlat, fixup hevc/h265 opts, expand new bg pngs
[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 = 0;
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 = cp;
336                         }
337                         if( *tpath != '/' ) {
338                                 char *cp = cstrcat(2, out_path, tpath);
339                                 delete [] tpath;  tpath = cp;
340                         }
341                 }
342                 if( strcmp(apath, tpath) ) result = 1;
343                 delete [] apath;
344                 delete [] tpath;
345                 delete [] out_path;
346         }
347
348         if(result && format == FILE_FFMPEG)
349                 result = !strcmp(fformat, asset.fformat);
350
351         if(test_audio && result)
352         {
353                 result = (channels == asset.channels &&
354                         sample_rate == asset.sample_rate &&
355                         bits == asset.bits &&
356                         byte_order == asset.byte_order &&
357                         signed_ == asset.signed_ &&
358                         header == asset.header &&
359                         dither == asset.dither &&
360                         !strcmp(acodec, asset.acodec));
361                 if(result && format == FILE_FFMPEG)
362                         result = !strcmp(ff_audio_options, asset.ff_audio_options) &&
363                                 ff_audio_bitrate == asset.ff_audio_bitrate;
364
365         }
366
367
368         if(test_video && result)
369         {
370                 result = (layers == asset.layers &&
371                         program == asset.program &&
372                         frame_rate == asset.frame_rate &&
373                         asset.interlace_autofixoption == interlace_autofixoption &&
374                         asset.interlace_mode    == interlace_mode &&
375                         interlace_fixmethod     == asset.interlace_fixmethod &&
376                         width == asset.width &&
377                         height == asset.height &&
378                         !strcmp(vcodec, asset.vcodec) &&
379                         strcmp(reel_name, asset.reel_name) == 0 &&
380                         reel_number == asset.reel_number &&
381                         tcstart == asset.tcstart &&
382                         tcend == asset.tcend &&
383                         tcformat == asset.tcformat);
384                 if(result && format == FILE_FFMPEG)
385                         result = !strcmp(ff_video_options, asset.ff_video_options) &&
386                                 ff_video_bitrate == asset.ff_video_bitrate &&
387                                 ff_video_quality == asset.ff_video_quality;
388         }
389
390         return result;
391 }
392
393 int Asset::test_path(const char *path)
394 {
395         if(!strcasecmp(this->path, path))
396                 return 1;
397         else
398                 return 0;
399 }
400
401 int Asset::read(FileXML *file,
402         int expand_relative)
403 {
404         int result = 0;
405
406 // Check for relative path.
407         if(expand_relative)
408         {
409                 char new_path[BCTEXTLEN];
410                 char asset_directory[BCTEXTLEN];
411                 char input_directory[BCTEXTLEN];
412                 FileSystem fs;
413
414                 strcpy(new_path, path);
415                 fs.set_current_dir("");
416
417                 fs.extract_dir(asset_directory, path);
418
419 // No path in asset.
420 // Take path of XML file.
421                 if(!asset_directory[0])
422                 {
423                         fs.extract_dir(input_directory, file->filename);
424
425 // Input file has a path
426                         if(input_directory[0])
427                         {
428                                 fs.join_names(path, input_directory, new_path);
429                         }
430                 }
431         }
432
433
434         while(!result)
435         {
436                 result = file->read_tag();
437                 if(!result)
438                 {
439                         if(file->tag.title_is("/ASSET"))
440                         {
441                                 result = 1;
442                         }
443                         else
444                         if(file->tag.title_is("AUDIO"))
445                         {
446                                 read_audio(file);
447                         }
448                         else
449                         if(file->tag.title_is("AUDIO_OMIT"))
450                         {
451                                 read_audio(file);
452                         }
453                         else
454                         if(file->tag.title_is("FORMAT"))
455                         {
456                                 const char *string = file->tag.get_property("TYPE");
457                                 format = File::strtoformat(string);
458                                 use_header =
459                                         file->tag.get_property("USE_HEADER", use_header);
460                                 file->tag.get_property("FFORMAT", fformat);
461                         }
462                         else
463                         if(file->tag.title_is("FOLDER"))
464                         {
465                                 const char *string = file->tag.get_property("NUMBER");
466                                 awindow_folder = string ? atoi(string) :
467                                         AWindowGUI::folder_number(file->read_text());
468                         }
469                         else
470                         if(file->tag.title_is("VIDEO"))
471                         {
472                                 read_video(file);
473                         }
474                         else
475                         if(file->tag.title_is("VIDEO_OMIT"))
476                         {
477                                 read_video(file);
478                         }
479                         else
480                         if(file->tag.title_is("INDEX"))
481                         {
482                                 read_index(file);
483                         }
484                 }
485         }
486
487         boundaries();
488 //printf("Asset::read 2\n");
489         return 0;
490 }
491
492 int Asset::read_audio(FileXML *file)
493 {
494         if(file->tag.title_is("AUDIO")) audio_data = 1;
495         channels = file->tag.get_property("CHANNELS", 2);
496 // This is loaded from the index file after the EDL but this
497 // should be overridable in the EDL.
498         if(!sample_rate) sample_rate = file->tag.get_property("RATE", 48000);
499         bits = file->tag.get_property("BITS", 16);
500         byte_order = file->tag.get_property("BYTE_ORDER", 1);
501         signed_ = file->tag.get_property("SIGNED", 1);
502         header = file->tag.get_property("HEADER", 0);
503         dither = file->tag.get_property("DITHER", 0);
504
505         audio_length = file->tag.get_property("AUDIO_LENGTH", (int64_t)0);
506         acodec[0] = 0;
507         file->tag.get_property("ACODEC", acodec);
508
509         if(!video_data)
510         {
511                 tcstart = 0;
512                 tcend = audio_length;
513                 tcformat = 0;
514         }
515         return 0;
516 }
517
518 int Asset::read_video(FileXML *file)
519 {
520         char string[BCTEXTLEN];
521
522         if(file->tag.title_is("VIDEO")) video_data = 1;
523         actual_height = file->tag.get_property("ACTUAL_HEIGHT", actual_height);
524         actual_width = file->tag.get_property("ACTUAL_WIDTH", actual_width);
525         height = file->tag.get_property("HEIGHT", height);
526         width = file->tag.get_property("WIDTH", width);
527         layers = file->tag.get_property("LAYERS", layers);
528         program = file->tag.get_property("PROGRAM", program);
529 // This is loaded from the index file after the EDL but this
530 // should be overridable in the EDL.
531         if(EQUIV(frame_rate, 0)) frame_rate = file->tag.get_property("FRAMERATE", frame_rate);
532         vcodec[0] = 0;
533         file->tag.get_property("VCODEC", vcodec);
534
535         video_length = file->tag.get_property("VIDEO_LENGTH", (int64_t)0);
536         single_frame = file->tag.get_property("SINGLE_FRAME", (int64_t)0);
537
538         interlace_autofixoption = file->tag.get_property("INTERLACE_AUTOFIX",0);
539
540         ilacemode_to_xmltext(string, ILACE_MODE_NOTINTERLACED);
541         interlace_mode = ilacemode_from_xmltext(file->tag.get_property("INTERLACE_MODE",string), ILACE_MODE_NOTINTERLACED);
542
543         ilacefixmethod_to_xmltext(string, ILACE_FIXMETHOD_NONE);
544         interlace_fixmethod = ilacefixmethod_from_xmltext(file->tag.get_property("INTERLACE_FIXMETHOD",string), ILACE_FIXMETHOD_NONE);
545
546         file->tag.get_property("REEL_NAME", reel_name);
547         reel_number = file->tag.get_property("REEL_NUMBER", reel_number);
548         tcstart = file->tag.get_property("TCSTART", tcstart);
549         tcend = file->tag.get_property("TCEND", tcend);
550         tcformat = file->tag.get_property("TCFORMAT", tcformat);
551
552         return 0;
553 }
554
555 int Asset::read_index(FileXML *file)
556 {
557         index_state->read_xml(file, channels);
558         return 0;
559 }
560
561 // Output path is the path of the output file if name truncation is desired.
562 // It is a "" if complete names should be used.
563
564 int Asset::write(FileXML *file,
565         int include_index,
566         const char *output_path)
567 {
568         char new_path[BCTEXTLEN];
569         char asset_directory[BCTEXTLEN];
570         char output_directory[BCTEXTLEN];
571         FileSystem fs;
572
573 // Make path relative
574         fs.extract_dir(asset_directory, path);
575         if(output_path && output_path[0])
576                 fs.extract_dir(output_directory, output_path);
577         else
578                 output_directory[0] = 0;
579
580 // Asset and EDL are in same directory.  Extract just the name.
581         if(!strcmp(asset_directory, output_directory))
582         {
583                 fs.extract_name(new_path, path);
584         }
585         else
586         {
587                 strcpy(new_path, path);
588         }
589
590         file->tag.set_title("ASSET");
591         file->tag.set_property("SRC", new_path);
592         file->append_tag();
593         file->append_newline();
594
595         file->tag.set_title("FOLDER");
596         file->tag.set_property("NUMBER", awindow_folder);
597         file->append_tag();
598         file->tag.set_title("/FOLDER");
599         file->append_tag();
600         file->append_newline();
601
602 // Write the format information
603         file->tag.set_title("FORMAT");
604
605         file->tag.set_property("TYPE",
606                 File::formattostr(format));
607         file->tag.set_property("USE_HEADER", use_header);
608         file->tag.set_property("FFORMAT", fformat);
609
610         file->append_tag();
611         file->tag.set_title("/FORMAT");
612         file->append_tag();
613         file->append_newline();
614
615 // Requiring data to exist caused batch render to lose settings.
616 // But the only way to know if an asset doesn't have audio or video data
617 // is to not write the block.
618 // So change the block name if the asset doesn't have the data.
619         write_audio(file);
620         write_video(file);
621 // index goes after source
622         if(index_state->index_status == INDEX_READY && include_index)
623                 write_index(file);
624
625         file->tag.set_title("/ASSET");
626         file->append_tag();
627         file->append_newline();
628         return 0;
629 }
630
631 int Asset::write_audio(FileXML *file)
632 {
633 // Let the reader know if the asset has the data by naming the block.
634         if(audio_data)
635                 file->tag.set_title("AUDIO");
636         else
637                 file->tag.set_title("AUDIO_OMIT");
638 // Necessary for PCM audio
639         file->tag.set_property("CHANNELS", channels);
640         file->tag.set_property("RATE", sample_rate);
641         file->tag.set_property("BITS", bits);
642         file->tag.set_property("BYTE_ORDER", byte_order);
643         file->tag.set_property("SIGNED", signed_);
644         file->tag.set_property("HEADER", header);
645         file->tag.set_property("DITHER", dither);
646         if(acodec[0])
647                 file->tag.set_property("ACODEC", acodec);
648
649         file->tag.set_property("AUDIO_LENGTH", audio_length);
650
651
652
653 // Rely on defaults operations for these.
654
655 //      file->tag.set_property("AMPEG_BITRATE", ampeg_bitrate);
656 //      file->tag.set_property("AMPEG_DERIVATIVE", ampeg_derivative);
657 //
658 //      file->tag.set_property("VORBIS_VBR", vorbis_vbr);
659 //      file->tag.set_property("VORBIS_MIN_BITRATE", vorbis_min_bitrate);
660 //      file->tag.set_property("VORBIS_BITRATE", vorbis_bitrate);
661 //      file->tag.set_property("VORBIS_MAX_BITRATE", vorbis_max_bitrate);
662 //
663 //      file->tag.set_property("MP3_BITRATE", mp3_bitrate);
664 //
665
666
667
668         file->append_tag();
669         if(audio_data)
670           file->tag.set_title("/AUDIO");
671         else
672           file->tag.set_title("/AUDIO_OMIT");
673         file->append_tag();
674         file->append_newline();
675         return 0;
676 }
677
678 int Asset::write_video(FileXML *file)
679 {
680         char string[BCTEXTLEN];
681
682         if(video_data)
683                 file->tag.set_title("VIDEO");
684         else
685                 file->tag.set_title("VIDEO_OMIT");
686         file->tag.set_property("ACTUAL_HEIGHT", actual_height);
687         file->tag.set_property("ACTUAL_WIDTH", actual_width);
688         file->tag.set_property("HEIGHT", height);
689         file->tag.set_property("WIDTH", width);
690         file->tag.set_property("LAYERS", layers);
691         file->tag.set_property("PROGRAM", program);
692         file->tag.set_property("FRAMERATE", frame_rate);
693         if(vcodec[0])
694                 file->tag.set_property("VCODEC", vcodec);
695
696         file->tag.set_property("VIDEO_LENGTH", video_length);
697         file->tag.set_property("SINGLE_FRAME", single_frame);
698
699         file->tag.set_property("INTERLACE_AUTOFIX", interlace_autofixoption);
700
701         ilacemode_to_xmltext(string, interlace_mode);
702         file->tag.set_property("INTERLACE_MODE", string);
703
704         ilacefixmethod_to_xmltext(string, interlace_fixmethod);
705         file->tag.set_property("INTERLACE_FIXMETHOD", string);
706
707
708         file->tag.set_property("REEL_NAME", reel_name);
709         file->tag.set_property("REEL_NUMBER", reel_number);
710         file->tag.set_property("TCSTART", tcstart);
711         file->tag.set_property("TCEND", tcend);
712         file->tag.set_property("TCFORMAT", tcformat);
713
714         file->append_tag();
715         if(video_data)
716                 file->tag.set_title("/VIDEO");
717         else
718                 file->tag.set_title("/VIDEO_OMIT");
719
720         file->append_tag();
721         file->append_newline();
722         return 0;
723 }
724
725 int Asset::write_index(FileXML *file)
726 {
727         index_state->write_xml(file);
728         return 0;
729 }
730
731
732 char* Asset::construct_param(const char *param, const char *prefix, char *return_value)
733 {
734         if(prefix)
735                 sprintf(return_value, "%s%s", prefix, param);
736         else
737                 strcpy(return_value, param);
738         return return_value;
739 }
740
741 #define UPDATE_DEFAULT(x, y) defaults->update(construct_param(x, prefix, string), y);
742 #define GET_DEFAULT(x, y) defaults->get(construct_param(x, prefix, string), y);
743
744 void Asset::load_defaults(BC_Hash *defaults,
745         const char *prefix,
746         int do_format,
747         int do_compression,
748         int do_path,
749         int do_data_types,
750         int do_bits)
751 {
752         char string[BCTEXTLEN];
753
754 // Can't save codec here because it's specific to render, record, and effect.
755 // The codec has to be UNKNOWN for file probing to work.
756
757         if(do_path)
758         {
759                 GET_DEFAULT("PATH", path);
760         }
761
762         if(do_compression)
763         {
764                 GET_DEFAULT("AUDIO_CODEC", acodec);
765                 GET_DEFAULT("VIDEO_CODEC", vcodec);
766         }
767
768         if(do_format)
769         {
770                 format = GET_DEFAULT("FORMAT", format);
771                 use_header = GET_DEFAULT("USE_HEADER", use_header);
772                 GET_DEFAULT("FFORMAT", fformat);
773         }
774
775         if(do_data_types)
776         {
777                 audio_data = GET_DEFAULT("AUDIO", 1);
778                 video_data = GET_DEFAULT("VIDEO", 1);
779         }
780
781         if(do_bits)
782         {
783                 bits = GET_DEFAULT("BITS", 16);
784                 dither = GET_DEFAULT("DITHER", 0);
785                 signed_ = GET_DEFAULT("SIGNED", 1);
786                 byte_order = GET_DEFAULT("BYTE_ORDER", 1);
787
788
789
790                 channels = GET_DEFAULT("CHANNELS", 2);
791                 if(!sample_rate) sample_rate = GET_DEFAULT("RATE", 48000);
792                 header = GET_DEFAULT("HEADER", 0);
793                 audio_length = GET_DEFAULT("AUDIO_LENGTH", (int64_t)0);
794
795
796
797                 height = GET_DEFAULT("HEIGHT", height);
798                 width = GET_DEFAULT("WIDTH", width);
799                 actual_height = GET_DEFAULT("ACTUAL_HEIGHT", actual_height);
800                 actual_width = GET_DEFAULT("ACTUAL_WIDTH", actual_width);
801                 program = GET_DEFAULT("PROGRAM", program);
802                 layers = GET_DEFAULT("LAYERS", layers);
803                 if(EQUIV(frame_rate, 0)) frame_rate = GET_DEFAULT("FRAMERATE", frame_rate);
804                 video_length = GET_DEFAULT("VIDEO_LENGTH", (int64_t)0);
805                 single_frame = GET_DEFAULT("SINGLE_FRAME", (int64_t)0);
806         }
807
808         ampeg_bitrate = GET_DEFAULT("AMPEG_BITRATE", ampeg_bitrate);
809         ampeg_derivative = GET_DEFAULT("AMPEG_DERIVATIVE", ampeg_derivative);
810
811         vorbis_vbr = GET_DEFAULT("VORBIS_VBR", vorbis_vbr);
812         vorbis_min_bitrate = GET_DEFAULT("VORBIS_MIN_BITRATE", vorbis_min_bitrate);
813         vorbis_bitrate = GET_DEFAULT("VORBIS_BITRATE", vorbis_bitrate);
814         vorbis_max_bitrate = GET_DEFAULT("VORBIS_MAX_BITRATE", vorbis_max_bitrate);
815
816         theora_fix_bitrate = GET_DEFAULT("THEORA_FIX_BITRATE", theora_fix_bitrate);
817         theora_bitrate = GET_DEFAULT("THEORA_BITRATE", theora_bitrate);
818         theora_quality = GET_DEFAULT("THEORA_QUALITY", theora_quality);
819         theora_sharpness = GET_DEFAULT("THEORA_SHARPNESS", theora_sharpness);
820         theora_keyframe_frequency = GET_DEFAULT("THEORA_KEYFRAME_FREQUENCY", theora_keyframe_frequency);
821         theora_keyframe_force_frequency = GET_DEFAULT("THEORA_FORCE_KEYFRAME_FREQUENCY", theora_keyframe_force_frequency);
822
823         GET_DEFAULT("FF_AUDIO_OPTIONS", ff_audio_options);
824         ff_audio_bitrate = GET_DEFAULT("FF_AUDIO_BITRATE", ff_audio_bitrate);
825         GET_DEFAULT("FF_VIDEO_OPTIONS", ff_video_options);
826         ff_video_bitrate = GET_DEFAULT("FF_VIDEO_BITRATE", ff_video_bitrate);
827         ff_video_quality = GET_DEFAULT("FF_VIDEO_QUALITY", ff_video_quality);
828
829         mp3_bitrate = GET_DEFAULT("MP3_BITRATE", mp3_bitrate);
830
831         jpeg_quality = GET_DEFAULT("JPEG_QUALITY", jpeg_quality);
832         aspect_ratio = GET_DEFAULT("ASPECT_RATIO", aspect_ratio);
833
834         interlace_autofixoption = ILACE_AUTOFIXOPTION_AUTO;
835         interlace_mode          = ILACE_MODE_UNDETECTED;
836         interlace_fixmethod     = ILACE_FIXMETHOD_UPONE;
837
838 // MPEG format information
839         vmpeg_iframe_distance = GET_DEFAULT("VMPEG_IFRAME_DISTANCE", vmpeg_iframe_distance);
840         vmpeg_pframe_distance = GET_DEFAULT("VMPEG_PFRAME_DISTANCE", vmpeg_pframe_distance);
841         vmpeg_progressive = GET_DEFAULT("VMPEG_PROGRESSIVE", vmpeg_progressive);
842         vmpeg_denoise = GET_DEFAULT("VMPEG_DENOISE", vmpeg_denoise);
843         vmpeg_bitrate = GET_DEFAULT("VMPEG_BITRATE", vmpeg_bitrate);
844         vmpeg_derivative = GET_DEFAULT("VMPEG_DERIVATIVE", vmpeg_derivative);
845         vmpeg_quantization = GET_DEFAULT("VMPEG_QUANTIZATION", vmpeg_quantization);
846         vmpeg_cmodel = GET_DEFAULT("VMPEG_CMODEL", vmpeg_cmodel);
847         vmpeg_fix_bitrate = GET_DEFAULT("VMPEG_FIX_BITRATE", vmpeg_fix_bitrate);
848         vmpeg_seq_codes = GET_DEFAULT("VMPEG_SEQ_CODES", vmpeg_seq_codes);
849         vmpeg_preset = GET_DEFAULT("VMPEG_PRESET", vmpeg_preset);
850         vmpeg_field_order = GET_DEFAULT("VMPEG_FIELD_ORDER", vmpeg_field_order);
851
852         theora_fix_bitrate = GET_DEFAULT("THEORA_FIX_BITRATE", theora_fix_bitrate);
853         theora_bitrate = GET_DEFAULT("THEORA_BITRATE", theora_bitrate);
854         theora_quality = GET_DEFAULT("THEORA_QUALITY", theora_quality);
855         theora_sharpness = GET_DEFAULT("THEORA_SHARPNESS", theora_sharpness);
856         theora_keyframe_frequency = GET_DEFAULT("THEORA_KEYFRAME_FREQUENCY", theora_keyframe_frequency);
857         theora_keyframe_force_frequency = GET_DEFAULT("THEORA_FORCE_KEYFRAME_FEQUENCY", theora_keyframe_force_frequency);
858
859
860         ac3_bitrate = GET_DEFAULT("AC3_BITRATE", ac3_bitrate);
861
862         png_use_alpha = GET_DEFAULT("PNG_USE_ALPHA", png_use_alpha);
863         exr_use_alpha = GET_DEFAULT("EXR_USE_ALPHA", exr_use_alpha);
864         exr_compression = GET_DEFAULT("EXR_COMPRESSION", exr_compression);
865         tiff_cmodel = GET_DEFAULT("TIFF_CMODEL", tiff_cmodel);
866         tiff_compression = GET_DEFAULT("TIFF_COMPRESSION", tiff_compression);
867
868         GET_DEFAULT("REEL_NAME", reel_name);
869         reel_number = GET_DEFAULT("REEL_NUMBER", reel_number);
870         tcstart = GET_DEFAULT("TCSTART", tcstart);
871         tcend = GET_DEFAULT("TCEND", tcend);
872         tcformat = GET_DEFAULT("TCFORMAT", tcformat);
873
874         boundaries();
875 }
876
877 void Asset::save_defaults(BC_Hash *defaults,
878         const char *prefix,
879         int do_format,
880         int do_compression,
881         int do_path,
882         int do_data_types,
883         int do_bits)
884 {
885         char string[BCTEXTLEN];
886
887         UPDATE_DEFAULT("PATH", path);
888
889
890
891
892         if(do_format)
893         {
894                 UPDATE_DEFAULT("FORMAT", format);
895                 UPDATE_DEFAULT("USE_HEADER", use_header);
896                 UPDATE_DEFAULT("FFORMAT", fformat);
897         }
898
899         if(do_data_types)
900         {
901                 UPDATE_DEFAULT("AUDIO", audio_data);
902                 UPDATE_DEFAULT("VIDEO", video_data);
903         }
904
905         if(do_compression)
906         {
907                 UPDATE_DEFAULT("AUDIO_CODEC", acodec);
908                 UPDATE_DEFAULT("VIDEO_CODEC", vcodec);
909
910                 UPDATE_DEFAULT("AMPEG_BITRATE", ampeg_bitrate);
911                 UPDATE_DEFAULT("AMPEG_DERIVATIVE", ampeg_derivative);
912
913                 UPDATE_DEFAULT("VORBIS_VBR", vorbis_vbr);
914                 UPDATE_DEFAULT("VORBIS_MIN_BITRATE", vorbis_min_bitrate);
915                 UPDATE_DEFAULT("VORBIS_BITRATE", vorbis_bitrate);
916                 UPDATE_DEFAULT("VORBIS_MAX_BITRATE", vorbis_max_bitrate);
917
918                 UPDATE_DEFAULT("FF_AUDIO_OPTIONS", ff_audio_options);
919                 UPDATE_DEFAULT("FF_AUDIO_BITRATE", ff_audio_bitrate);
920                 UPDATE_DEFAULT("FF_VIDEO_OPTIONS", ff_video_options);
921                 UPDATE_DEFAULT("FF_VIDEO_BITRATE", ff_video_bitrate);
922                 UPDATE_DEFAULT("FF_VIDEO_QUALITY", ff_video_quality);
923
924                 UPDATE_DEFAULT("THEORA_FIX_BITRATE", theora_fix_bitrate);
925                 UPDATE_DEFAULT("THEORA_BITRATE", theora_bitrate);
926                 UPDATE_DEFAULT("THEORA_QUALITY", theora_quality);
927                 UPDATE_DEFAULT("THEORA_SHARPNESS", theora_sharpness);
928                 UPDATE_DEFAULT("THEORA_KEYFRAME_FREQUENCY", theora_keyframe_frequency);
929                 UPDATE_DEFAULT("THEORA_FORCE_KEYFRAME_FREQUENCY", theora_keyframe_force_frequency);
930
931
932
933                 UPDATE_DEFAULT("MP3_BITRATE", mp3_bitrate);
934
935                 UPDATE_DEFAULT("JPEG_QUALITY", jpeg_quality);
936                 UPDATE_DEFAULT("ASPECT_RATIO", aspect_ratio);
937
938 // MPEG format information
939                 UPDATE_DEFAULT("VMPEG_IFRAME_DISTANCE", vmpeg_iframe_distance);
940                 UPDATE_DEFAULT("VMPEG_PFRAME_DISTANCE", vmpeg_pframe_distance);
941                 UPDATE_DEFAULT("VMPEG_PROGRESSIVE", vmpeg_progressive);
942                 UPDATE_DEFAULT("VMPEG_DENOISE", vmpeg_denoise);
943                 UPDATE_DEFAULT("VMPEG_BITRATE", vmpeg_bitrate);
944                 UPDATE_DEFAULT("VMPEG_DERIVATIVE", vmpeg_derivative);
945                 UPDATE_DEFAULT("VMPEG_QUANTIZATION", vmpeg_quantization);
946                 UPDATE_DEFAULT("VMPEG_CMODEL", vmpeg_cmodel);
947                 UPDATE_DEFAULT("VMPEG_FIX_BITRATE", vmpeg_fix_bitrate);
948                 UPDATE_DEFAULT("VMPEG_SEQ_CODES", vmpeg_seq_codes);
949                 UPDATE_DEFAULT("VMPEG_PRESET", vmpeg_preset);
950                 UPDATE_DEFAULT("VMPEG_FIELD_ORDER", vmpeg_field_order);
951
952
953                 UPDATE_DEFAULT("AC3_BITRATE", ac3_bitrate);
954
955
956                 UPDATE_DEFAULT("PNG_USE_ALPHA", png_use_alpha);
957                 UPDATE_DEFAULT("EXR_USE_ALPHA", exr_use_alpha);
958                 UPDATE_DEFAULT("EXR_COMPRESSION", exr_compression);
959                 UPDATE_DEFAULT("TIFF_CMODEL", tiff_cmodel);
960                 UPDATE_DEFAULT("TIFF_COMPRESSION", tiff_compression);
961         }
962
963         if(do_bits)
964         {
965                 UPDATE_DEFAULT("BITS", bits);
966                 UPDATE_DEFAULT("DITHER", dither);
967                 UPDATE_DEFAULT("SIGNED", signed_);
968                 UPDATE_DEFAULT("BYTE_ORDER", byte_order);
969
970
971
972
973
974
975                 UPDATE_DEFAULT("CHANNELS", channels);
976                 UPDATE_DEFAULT("RATE", sample_rate);
977                 UPDATE_DEFAULT("HEADER", header);
978                 UPDATE_DEFAULT("AUDIO_LENGTH", audio_length);
979
980
981
982                 UPDATE_DEFAULT("HEIGHT", height);
983                 UPDATE_DEFAULT("WIDTH", width);
984                 UPDATE_DEFAULT("ACTUAL_HEIGHT", actual_height);
985                 UPDATE_DEFAULT("ACTUAL_WIDTH", actual_width);
986                 UPDATE_DEFAULT("PROGRAM", program);
987                 UPDATE_DEFAULT("LAYERS", layers);
988                 UPDATE_DEFAULT("FRAMERATE", frame_rate);
989                 UPDATE_DEFAULT("VIDEO_LENGTH", video_length);
990                 UPDATE_DEFAULT("SINGLE_FRAME", single_frame);
991
992         }
993
994         UPDATE_DEFAULT("REEL_NAME", reel_name);
995         UPDATE_DEFAULT("REEL_NUMBER", reel_number);
996         UPDATE_DEFAULT("TCSTART", tcstart);
997         UPDATE_DEFAULT("TCEND", tcend);
998         UPDATE_DEFAULT("TCFORMAT", tcformat);
999 }
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009 int Asset::dump(FILE *fp)
1010 {
1011         fprintf(fp,"  asset::dump\n");
1012         fprintf(fp,"   this=%p path=%s\n", this, path);
1013         fprintf(fp,"   index_status %d\n", index_state->index_status);
1014         fprintf(fp,"   format %d\n", format);
1015         fprintf(fp,"   fformat=\"%s\"\n", fformat);
1016         fprintf(fp,"   ff_audio_options=\"%s\"\n", ff_audio_options);
1017         fprintf(fp,"   ff_audio_bitrate=%d\n", ff_audio_bitrate);
1018         fprintf(fp,"   ff_video_options=\"%s\"\n", ff_video_options);
1019         fprintf(fp,"   ff_video_bitrate=%d\n", ff_video_bitrate);
1020         fprintf(fp,"   ff_video_quality=%d\n", ff_video_quality);
1021         fprintf(fp,"   audio_data %d channels %d samplerate %d bits %d"
1022                 " byte_order %d signed %d header %d dither %d acodec %4.4s\n",
1023                 audio_data, channels, sample_rate, bits, byte_order, signed_,
1024                 header, dither, acodec);
1025         fprintf(fp,"   audio_length %jd\n", audio_length);
1026         char string[BCTEXTLEN];
1027         ilacemode_to_xmltext(string, interlace_mode);
1028         fprintf(fp,"   video_data %d program %d layers %d framerate %f width %d"
1029                 " height %d vcodec %4.4s aspect_ratio %f ilace_mode %s\n",
1030                 video_data, layers, program, frame_rate, width, height,
1031                 vcodec, aspect_ratio,string);
1032         fprintf(fp,"   reel_name %s reel_number %i tcstart %jd tcend %jd tcf %d\n",
1033                 reel_name, reel_number, tcstart, tcend, tcformat);
1034         fprintf(fp,"   video_length %jd repeat %d\n", video_length, single_frame);
1035
1036
1037         return 0;
1038 }
1039
1040
1041 // For Indexable
1042 int Asset::get_audio_channels()
1043 {
1044         return channels;
1045 }
1046
1047 int Asset::get_sample_rate()
1048 {
1049         return sample_rate;
1050 }
1051
1052 int64_t Asset::get_audio_samples()
1053 {
1054         return audio_length;
1055 }
1056
1057 int Asset::have_audio()
1058 {
1059         return audio_data;
1060 }
1061
1062 int Asset::have_video()
1063 {
1064         return video_data;
1065 }
1066
1067 int Asset::get_w()
1068 {
1069         return width;
1070 }
1071
1072 int Asset::get_h()
1073 {
1074         return height;
1075 }
1076
1077 double Asset::get_frame_rate()
1078 {
1079         return frame_rate;
1080 }
1081
1082
1083 int Asset::get_video_layers()
1084 {
1085         return layers;
1086 }
1087
1088 int Asset::get_program()
1089 {
1090         return program;
1091 }
1092
1093 int64_t Asset::get_video_frames()
1094 {
1095         return video_length;
1096 }
1097
1098 double Asset::total_length_framealigned(double fps)
1099 {
1100         if (video_data && audio_data) {
1101                 double aud = floor(( (double)audio_length / sample_rate) * fps) / fps;
1102                 double vid = floor(( (double)video_length / frame_rate) * fps) / fps;
1103                 return MIN(aud,vid);
1104         }
1105
1106         if (audio_data)
1107                 return (double)audio_length / sample_rate;
1108
1109         if (video_data)
1110                 return (double)video_length / frame_rate;
1111
1112         return 0;
1113 }
1114
1115 int Asset::set_timecode(char *tc, int format, int end)
1116 {
1117         int hr, min, sec;
1118
1119         hr = ((int) tc[0] - 48) * 10 + (int) tc[1] - 48;
1120         min = ((int) tc[3] - 48) * 10 + (int) tc[4] - 48;
1121         sec = ((int) tc[6] - 48) * 10 + (int) tc[7] - 48;
1122
1123         // This needs to be modified to handle drop-frame
1124
1125         if(end)
1126                 tcend = (int64_t) (((hr * 3600) + (min * 60) + sec) * frame_rate);
1127         else
1128                 tcstart = (int64_t) (((hr * 3600) + (min * 60) + sec) * frame_rate);
1129
1130         tcformat = format;
1131         return 0;
1132 }
1133
1134