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