r870: Merge 2.1:
[cinelerra_cv.git] / cinelerra / filedv.C
blobb599f224effc4ca8c17df463b28e0bf3cd22965a
1 #include "asset.h"
2 #include "bcsignals.h"
3 #include "byteorder.h"
4 #include "dv1394.h"
5 #include "edit.h"
6 #include "file.h"
7 #include "filedv.h"
8 #include "guicast.h"
9 #include "interlacemodes.h"
10 #include "language.h"
11 #include "mutex.h"
12 #include "mwindow.inc"
13 #include "quicktime.h"
14 #include "vframe.h"
15 #include "videodevice.inc"
16 #include "cmodel_permutation.h"
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <fcntl.h>
21 #include <unistd.h>
22 #include <string.h>
23 #include <errno.h>
25 #include <iostream>
27 FileDV::FileDV(Asset *asset, File *file)
28  : FileBase(asset, file)
30         stream = 0;
31         decoder = 0;
32         encoder = 0;
33         audio_encoder = 0;
34         audio_buffer = 0;
35         video_buffer = 0;
36         output_size = 0;
37         video_position = 0;
38         audio_position = 0;
40         audio_sample_buffer = 0;
41         audio_sample_buffer_len = 0;
42         audio_sample_buffer_start = 0;
43         audio_sample_buffer_end = 0;
44         audio_sample_buffer_maxsize = 0;
46         audio_frames_written = 0;
48         if(asset->format == FILE_UNKNOWN)
49                 asset->format = FILE_RAWDV;
50         asset->byte_order = 0;
52         stream_lock = new Mutex("FileDV::stream_lock");
53         decoder_lock = new Mutex("FileDV::decoder_lock");
54         video_position_lock = new Mutex("FileDV::video_position_lock");
57 FileDV::~FileDV()
59         if(stream) close_file();
61         if(decoder) dv_decoder_free(decoder);
62         if(encoder) dv_encoder_free(encoder);
63         if(audio_encoder) dv_encoder_free(audio_encoder);
64         
65         delete stream_lock;
66         delete decoder_lock;
67         delete video_position_lock;
68         
69         delete[] video_buffer;
70         delete[] audio_buffer;
71         
72         if(audio_sample_buffer)
73         {
74                 for(int i = 0; i < asset->channels; i++)
75                         delete[] audio_sample_buffer[i];
76                 delete[] audio_sample_buffer;
77         }
80 void FileDV::get_parameters(BC_WindowBase *parent_window,
81         Asset *asset,
82         BC_WindowBase* &format_window,
83         int audio_options,
84         int video_options)
86         if(audio_options)
87         {
88                 DVConfigAudio *window = new DVConfigAudio(parent_window, asset);
89                 format_window = window;
90                 window->create_objects();
91                 window->run_window();
92                 delete window;
93         }
94         else
95         if(video_options)
96         {
97                 DVConfigVideo *window = new DVConfigVideo(parent_window, asset);
98                 format_window = window;
99                 window->create_objects();
100                 window->run_window();
101                 delete window;
102         }
106 int FileDV::reset_parameters_derived()
108         if(decoder) dv_decoder_free(decoder);
109         if(encoder) dv_encoder_free(encoder);
110         if(audio_encoder) dv_encoder_free(audio_encoder);
111         decoder = 0;
112         encoder = 0;
113         audio_encoder = 0;
115 TRACE("FileDV::reset_parameters_derived 10")
117 TRACE("FileDV::reset_parameters_derived: 20")
118         delete[] audio_buffer;
119         delete[] video_buffer;
120 TRACE("FileDV::reset_parameters_derived: 30")
121         
122         audio_buffer = 0;
123         video_buffer = 0;
124         
125         if(stream) fclose(stream);
126         
127         stream = 0;
128         
129         audio_position = 0;
130         video_position = 0;
131         
132         if(audio_sample_buffer)
133         {
134                 for(int i = 0; i < asset->channels; i++)
135                         delete[] audio_sample_buffer[i];
136                 delete[] audio_sample_buffer;
137         }
138         audio_sample_buffer = 0;
139         audio_sample_buffer_start = 0;
140         audio_sample_buffer_len = 0;
141         audio_sample_buffer_end = 0;
142         audio_sample_buffer_maxsize = 0;
143         
144         audio_frames_written = 0;
145 // output_size gets set in open_file, once we know if the frames are PAL or NTSC
146 // output and input are allocated at the same point.
147         output_size = 0;
148         
149 UNTRACE
150         return 0;
153 int FileDV::open_file(int rd, int wr)
156 TRACE("FileDV::open_file 10")
158         if(wr)
159         {
161 TRACE("FileDV::open_file 20")
163                 
164                 if (!(asset->height == 576 && asset->width == 720 && asset->frame_rate == 25) &&
165                     !(asset->height == 480 && asset->width == 720 && (asset->frame_rate >= 29.96 && asset->frame_rate <= 29.98)))
166                 {
167                         printf("Resolution not supported for dv: %ix%i framerate: %f\n", asset->width, asset->height, asset->frame_rate); 
168                         if (asset->height == 480 && asset->width == 720 && asset->frame_rate == 30)
169                         {
170                                 printf("Suggestion: Proper frame rate for NTSC DV is 29.97\n");
171                         }
172                         return 1;       
173                 }   
174                 if (!(asset->channels == 2 && (asset->sample_rate == 48000 || asset->sample_rate == 44100)) &&
175                     !((asset->channels == 4 || asset->channels == 2) && asset->sample_rate == 32000))
176                 {
177                         printf("Audio configuration not supported for dv: channels: %i, sample rate: %i\n", asset->channels, asset->sample_rate); 
178                         return 1;       
179                 }   
180                   
181                 
182                     
183                     
185                 if((stream = fopen(asset->path, "w+b")) == 0)
186                 {
187                         perror(_("FileDV::open_file rdwr"));
188                         return 1;
189                 }
190                 
191                 // Create a new encoder
192                 if(encoder) dv_encoder_free(encoder);
193                 encoder = dv_encoder_new(0,0,0);
194                 encoder->vlc_encode_passes = 3;
195                 encoder->static_qno = 0;
196                 encoder->force_dct = DV_DCT_AUTO;
197         
198                 if(audio_encoder) dv_encoder_free(audio_encoder);
199                 audio_encoder = dv_encoder_new(0, 0, 0);
200                 audio_encoder->vlc_encode_passes = 3;
201                 audio_encoder->static_qno = 0;
202                 audio_encoder->force_dct = DV_DCT_AUTO;
203         
204                 if(decoder) dv_decoder_free(decoder);
205                 decoder = dv_decoder_new(0,0,0);
206                 decoder->quality = DV_QUALITY_BEST;
207         
209                 isPAL = (asset->height == 576 ? 1 : 0);
210                 encoder->isPAL = isPAL;
211                 output_size = (isPAL ? DV1394_PAL_FRAME_SIZE : DV1394_NTSC_FRAME_SIZE);
213                 // Compare to 16 / 8 rather than == 16 / 9 in case of floating point
214                 // rounding errors
215                 encoder->is16x9 = asset->aspect_ratio > 16 / 8;
216         }
217         else
218         {
219                 unsigned char *temp;
221 TRACE("FileDV::open_file 30")
223                 struct stat info;
225 TRACE("FileDV::open_file 40")
227                 if((stream = fopen(asset->path, "rb")) == 0)
228                 {
229                         perror(_("FileDV::open_file rd"));
230                         return 1;
231                 }
233                 // temp storage to find out the correct info from the stream.
234                 temp = new unsigned char[DV1394_PAL_FRAME_SIZE];
235                 memset(temp, 0, DV1394_PAL_FRAME_SIZE);
237                 // need file size info to get length.
238                 stat(asset->path, &info);
239                 
240 TRACE("FileDV::open_file 50")
242                 // read the first frame so we can get the stream info from it
243                 // by reading the greatest possible frame size, we ensure we get all the
244                 // data. libdv will determine if it's PAL or NTSC, and input and output
245                 // buffers get allocated accordingly.
246                 fread(temp, DV1394_PAL_FRAME_SIZE, 1, stream);
248 TRACE("FileDV::open_file 60")
250                 if(decoder) dv_decoder_free(decoder);
251                 decoder = dv_decoder_new(0,0,0);
252                 decoder->quality = DV_QUALITY_BEST;
253         
255                 if(dv_parse_header(decoder, temp) > -1 )
256                 {
257                         // define video params first -- we need to find out output_size
258                         // always have video
259                         asset->video_data = 1;
260                         asset->layers = 1;
262                         //TODO: according to the information I found, letterbox and widescreen
263                         //are the same thing; however, libdv provides a function to check
264                         //if the video feed is one of the other. Need to find out if there
265                         //is a difference.
266                         if(dv_format_normal(decoder) != 0) asset->aspect_ratio = (double) 4 / 3;
267                         else asset->aspect_ratio = (double) 16 / 9;
269                         asset->width = decoder->width;
270                         asset->height = decoder->height;
271                         
272                         if(dv_is_progressive(decoder) > 0)
273                                 asset->interlace_mode = BC_ILACE_MODE_NOTINTERLACED;
274                         else
275                                 asset->interlace_mode = BC_ILACE_MODE_BOTTOM_FIRST;
276                         
277                         isPAL = dv_is_PAL(decoder);
278                         
279                         output_size = (isPAL ? DV1394_PAL_FRAME_SIZE : DV1394_NTSC_FRAME_SIZE);
280                         asset->video_length = info.st_size / output_size;
281                         
282                         if(!asset->frame_rate)
283                                 asset->frame_rate = (isPAL ? 25 : 29.97);
284                         strncpy(asset->vcodec, "dvc ", 4);
286                         // see if there are any audio tracks
287                         asset->channels = dv_get_num_channels(decoder);
288                         if(asset->channels > 0)
289                         {
290                                 asset->audio_data = 1;
291                                 asset->sample_rate = dv_get_frequency(decoder);
292                                 // libdv always scales the quantization up to 16 bits for dv_decode_full_audio
293                                 asset->bits = 16;
294                                 asset->audio_length = (int64_t) (info.st_size / output_size / asset->frame_rate * asset->sample_rate);
295                                 strncpy(asset->acodec, "dvc ", 4);
296                         }
297                         else
298                                 asset->audio_data = 0;
299                 }
300                 else
301                 {
302                         asset->audio_data = 0;
303                         asset->video_data = 0;
304                 }
306                 fseeko(stream, 0, SEEK_SET);
307 TRACE("FileDV::open_file 80")
309                 delete[] temp;
310         }
312         // allocate space for audio and video
313         video_buffer = new unsigned char[output_size + 4];
314         audio_buffer = new unsigned char[output_size + 4];
315                         
316 UNTRACE
317         return 0;
320 int FileDV::check_sig(Asset *asset)
322         unsigned char temp[3];
323         FILE *t_stream = fopen(asset->path, "rb");
325         fread(&temp, 3, 1, t_stream);
327         fclose(t_stream);
328         
329         if(temp[0] == 0x1f &&
330                         temp[1] == 0x07 &&
331                         temp[2] == 0x00)
332                 return 1;
334         return 0;
337 int FileDV::close_file_derived()
339         if(stream) fclose(stream);
340         stream = 0;
342         return 0;
345 int64_t FileDV::get_video_position()
347         return video_position;
350 int64_t FileDV::get_audio_position()
352         return audio_position;
355 int FileDV::set_video_position(int64_t x)
357         video_position = x;
358         return 0;
361 int FileDV::set_audio_position(int64_t x)
363         audio_position = x;
364         return 0;
367 int FileDV::audio_samples_copy(double **buffer, int64_t len)
369         // take the buffer and copy it into a queue
370         if(!audio_sample_buffer)
371         {
372                 audio_sample_buffer = new int16_t*[asset->channels];
373                 if(!audio_sample_buffer)
374                 {
375                         fprintf(stderr, "ERROR: Unable to allocate memory for audio_sample_buffer.\n");
376                         return 1;
377                 }
378                 
379                 for(int i = 0; i < asset->channels; i++)
380                 {
381                         audio_sample_buffer[i] = new int16_t[len * 2];
383                         if(!audio_sample_buffer[i])
384                         {
385                                 fprintf(stderr, "ERROR: Unable to allocate memory for "
386                                         "audio_sample_buffer channel %d\n", i);
387                                 return 1;
388                         }
389                 }
390                 audio_sample_buffer_maxsize = len * 2;
391                 audio_sample_buffer_len = 0;
392                 audio_sample_buffer_start = 0;
393                 audio_sample_buffer_end = 0;
394         }
396         if(audio_sample_buffer_maxsize <= audio_sample_buffer_len + len)
397         {
398                 // Allocate double the needed size
399                 for(int i = 0; i < asset->channels; i++)
400                 {
401                         int16_t *tmp = new int16_t[(audio_sample_buffer_len + len) * 2];
402                         if(!tmp)
403                         {
404                                 fprintf(stderr, "ERROR: Unable to reallocate memory for "
405                                         "audio_sample_buffer channel %d\n", i);
406                                 return 1;
407                         }
408                         // Copy everything from audio_sample_buffer into tmp
409                         for(int a = 0, b = audio_sample_buffer_start;
410                                         a < audio_sample_buffer_len;
411                                         a++, b = (b < (audio_sample_buffer_maxsize - 1) ? (b + 1) : 0))
412                         {
413                                 tmp[a] = audio_sample_buffer[i][b];
414                         }
415                         // Free the current buffer, and reassign tmp to audio_sample_buffer[i]
416                         delete[] audio_sample_buffer[i];
417                         audio_sample_buffer[i] = tmp;
418                 }
419                 audio_sample_buffer_start = 0;
420                 audio_sample_buffer_end = audio_sample_buffer_len - 1;
421                 audio_sample_buffer_maxsize = (audio_sample_buffer_len + len) * 2;
422         }
425         for(int i = 0; i < asset->channels; i++)
426         {
427                 if(len + audio_sample_buffer_end < audio_sample_buffer_maxsize)
428                 {
429                         // copy buffer into audio_sample_buffer, straight out (no loop around)
430                         for(int a = 0; a < len; a++)
431                         {
432                                 audio_sample_buffer[i][audio_sample_buffer_end + a] = 
433                                         (buffer[i][a] * 32767);
434                         }
435                         if(i == (asset->channels - 1))
436                                 audio_sample_buffer_end += len;
437                 }
438                 else
439                 {
440                         // Need to loop back to the start of audio_sample_buffer
441                         int copy_size = audio_sample_buffer_maxsize - audio_sample_buffer_end;
443                         for(int a = 0; a < copy_size; a++)
444                                 audio_sample_buffer[i][a + audio_sample_buffer_end] =
445                                         (buffer[i][a] * 32767);
447                         for(int a = 0; a < len - copy_size; a++)
448                                 audio_sample_buffer[i][a] = (buffer[i][a + copy_size] * 32767);
449                         
450                         if(i == (asset->channels - 1))
451                                 audio_sample_buffer_end = len - copy_size;
452                 }
453         }
454         
455         audio_sample_buffer_len += len;
456         
457         return 0;
460 int FileDV::write_samples(double **buffer, int64_t len)
462         if(audio_samples_copy(buffer, len) != 0)
463         {
464                 fprintf(stderr, "ERROR: Unable to store sample for FileDV::write_samples\n");
465                 return 1;       
466         }
467         video_position_lock->lock("FileDV::write_samples");
469 TRACE("FileDV::write_samples 200")
470         // Get number of frames to be written. Order of operations is important here;
471         // the buffer length must be multiplied by the frame rate first in case the
472         // number of samples in the buffer is less than the sample rate.
473         int nFrames = MIN(video_position - audio_frames_written,
474                                                         audio_sample_buffer_len * asset->frame_rate / asset->sample_rate);
476         video_position_lock->unlock();
478 TRACE("FileDV::write_samples 210")
479         
480         int16_t **tmp_buf = new int16_t*[asset->channels];
481         for(int a = 0; a < asset->channels; a++)
482                 tmp_buf[a] = new int16_t[asset->sample_rate];
484 TRACE("FileDV::write_samples 220")
485         
486         for(int i = 0; i < nFrames; i++)
487         {
488                 stream_lock->lock("FileDV::write_samples 10");
489                 if(fseeko(stream, (off_t) audio_frames_written * output_size, SEEK_SET) != 0)
490                 {
491                         fprintf(stderr, "ERROR: Unable to set audio write position to %lli\n", (off_t) audio_frames_written * output_size);
492                         stream_lock->unlock();
493                         return 1;
494                 }
495                 
496                 if(fread(audio_buffer, output_size, 1, stream) != 1)
497                 {
498                         fprintf(stderr, "ERROR: Unable to read from file\n");
499                         stream_lock->unlock();
500                         return 1;
501                 }
502                 
503                 stream_lock->unlock();
507 TRACE("FileDV::write_samples 230")
509                 int samples = dv_calculate_samples(audio_encoder, asset->sample_rate,
510                                                                 audio_frames_written);
512                 if(samples > audio_sample_buffer_maxsize - 1 - audio_sample_buffer_start)
513                 {
514 TRACE("FileDV::write_samples 240")
515                         int copy_size = audio_sample_buffer_maxsize - audio_sample_buffer_start - 1;
516                         
517                         for(int a = 0; a < asset->channels; a++)
518                         {
519                                 memcpy(tmp_buf[a], audio_sample_buffer[a] + audio_sample_buffer_start,
520                                         copy_size);
521                                 memcpy(tmp_buf[a] + copy_size, audio_sample_buffer[a],
522                                         samples - copy_size);
523                         }
524 TRACE("FileDV::write_samples 250")
525                         // Encode the audio into the frame
526                         if(dv_encode_full_audio(audio_encoder, tmp_buf, asset->channels,
527                                 asset->sample_rate, audio_buffer) < 0)
528                         {
529                                 fprintf(stderr, "ERROR: unable to encode audio frame %d\n", audio_frames_written);
530                         }
531                 }
532                 else
533                 {
534 TRACE("FileDV::write_samples 260")
535                         int16_t **tmp_buf2 = new int16_t*[asset->channels];
536                         for(int a = 0; a < asset->channels; a++)
537                                 tmp_buf2[a] = audio_sample_buffer[a] + audio_sample_buffer_start;
538                         if(dv_encode_full_audio(audio_encoder, tmp_buf2,
539                                 asset->channels, asset->sample_rate, audio_buffer) < 0)
540                         {
541                                 fprintf(stderr, "ERROR: unable to encode audio frame %d\n", audio_frames_written);
542                         }
543                         delete[] tmp_buf2;
544                 }               
545                 
546 TRACE("FileDV::write_samples 270")
548                 stream_lock->lock("FileDV::write_samples 20");
549                 if(fseeko(stream, (off_t) audio_frames_written * output_size, SEEK_SET) != 0)
550                 {
551                         fprintf(stderr, "ERROR: Unable to relocate for audio write to %lli\n", (off_t) audio_frames_written * output_size);
552                         stream_lock->unlock();
553                         return 1;
554                 }
555                 
556                 if(fwrite(audio_buffer, output_size, 1, stream) != 1)
557                 {
558                         fprintf(stderr, "ERROR: Unable to write audio\n");
559                         stream_lock->unlock();
560                         return 1;
561                 }
563                 stream_lock->unlock();
564                 
565                 audio_frames_written++;
566                 audio_sample_buffer_len -= samples;
567                 audio_sample_buffer_start += samples;
568                 if(audio_sample_buffer_start >= audio_sample_buffer_maxsize)
569                         audio_sample_buffer_start -= audio_sample_buffer_maxsize;
570         }
572 TRACE("FileDV::write_samples 280")
574         for(int a = 0; a < asset->channels; a++)
575                 delete[] tmp_buf[a];
576         delete[] tmp_buf;
578 TRACE("FileDV::write_samples 290")
581 UNTRACE
583         return 0;
586 int FileDV::write_frames(VFrame ***frames, int len)
588         int result = 0;
590         if(stream == 0) return 1;
592         for(int j = 0; j < len && !result; j++)
593         {
594                 VFrame *temp_frame = frames[0][j];
596 //printf("FileDV::write_frames: color_model %i\n", temp_frame->get_color_model());
597                         switch(temp_frame->get_color_model())
598                         {
599                                 case BC_COMPRESSED:
600                                         memcpy(video_buffer, temp_frame->get_data(), output_size);
601                                         break;
602                                 case BC_YUV422:
603 //printf("FileDV::write_frames: 4\n");
604                                         dv_encode_full_frame(encoder, temp_frame->get_rows(),
605                                                 e_dv_color_yuv, video_buffer);
606                                         break;
607                                 case BC_RGB888:
608 //printf("FileDV::write_frames: 5\n");
609                                         dv_encode_full_frame(encoder, temp_frame->get_rows(),
610                                                 e_dv_color_rgb, video_buffer);
611                                         break;
612                                 default:
613                                         unsigned char *data = new unsigned char[asset->height * asset->width * 2];
614                                         unsigned char **cmodel_buf = new unsigned char *[asset->height];
615 //printf("FileDV::write_frames: 6\n");
616                                         unsigned char **row_pointers = temp_frame->get_rows();
617                                         for(int i = 0; i < asset->height; i++)
618                                                 cmodel_buf[i] = data + asset->width * 2 * i;
619                                         
620                                         cmodel_transfer(cmodel_buf,
621                                                 row_pointers,
622                                                 cmodel_buf[0],
623                                                 cmodel_buf[1],
624                                                 cmodel_buf[2],
625                                                 row_pointers[0],
626                                                 row_pointers[1],
627                                                 row_pointers[2],
628                                                 0,
629                                                 0,
630                                                 asset->width,
631                                                 asset->height,
632                                                 0,
633                                                 0,
634                                                 asset->width,
635                                                 asset->height,
636                                                 temp_frame->get_color_model(),
637                                                 BC_YUV422,
638                                                 0,
639                                                 asset->width,
640                                                 asset->width);
642                                         dv_encode_full_frame(encoder, cmodel_buf,
643                                                 e_dv_color_yuv, video_buffer);
645                                         delete[] cmodel_buf;
646                                         delete[] data;
647                                         break;
648                         }
649 //printf("FileDV::write_frames: 7\n");
651                 // This is the only thread that modifies video_position,
652                 // so video_position_lock can remain unlocked for reads.
653                 stream_lock->lock("FileDV::write_frames");
654                 if(fseeko(stream, (off_t) video_position * output_size, SEEK_SET) != 0)
655                 {
656                         fprintf(stderr, "FileDV::write_frames: Unable to seek file to %lli\n", (off_t)(video_position * output_size));
657                 }
658                 if(fwrite(video_buffer, output_size, 1, stream) < 1)
659                 {
660                         fprintf(stderr, "FileDV::write_frames: Unable to write video data.\n");         
661                 }
662                 stream_lock->unlock();
663                 
664                 video_position_lock->lock();
665                 video_position++;
666                 video_position_lock->unlock();
667         }
668         
669         return 0;
672 int FileDV::read_compressed_frame(VFrame *buffer)
674         int64_t result;
675         if(stream == 0) return 0;
677         if (fseeko(stream, (off_t) video_position * output_size, SEEK_SET))
678         {
679                 fprintf(stderr, "FileDV::read_compressed_frame: Unable to seek file to %lli\n", (off_t)(video_position * output_size));
680         }
681         result = fread(buffer->get_data(), output_size, 1, stream);
682         video_position++;
684         buffer->set_compressed_size(result);
685         
686         return result != 0;
689 int FileDV::write_compressed_frame(VFrame *buffer)
691         int result = 0;
692         if(stream == 0) return 0;
694         if (fseeko(stream, (off_t) video_position * output_size, SEEK_SET))
695         {
696                 fprintf(stderr, "FileDV::read_compressed_frame: Unable to seek file to %lli", (off_t)(video_position * output_size));
697         }
698         result = fwrite(buffer->get_data(), buffer->get_compressed_size(), 1, stream);
699         video_position++;
700         return result != 0;
703 int64_t FileDV::compressed_frame_size()
705         return output_size;
708 int FileDV::read_samples(double *buffer, int64_t len)
710         int count = 0;
711         int result = 0;
712         int frame_count = get_audio_frame(audio_position);
713         int offset = get_audio_offset(audio_position);
714         
715         stream_lock->lock("FileDV::read_samples");
716         if(stream == 0)
717         {
718                 stream_lock->unlock();
719                 return 1;
720         }
721         stream_lock->unlock();
723         // If the sample rate is 32 kHz, and the bitsize is 12, libdv
724         // requires we have space allocated for 4 channels even if
725         // the data only contains two channels.
727         // decoder will exist since it is not free'd after open_file
728         int channels = (asset->sample_rate == 32000 && decoder->audio->quantization == 12) ? 4 : 2;
730         int16_t **out_buffer = new int16_t*[channels];
731         for(int i = 0; i < channels; i++)
732                 out_buffer[i] = new int16_t[DV_AUDIO_MAX_SAMPLES];
734         while(count < len)
735         {
736                 stream_lock->lock();
737                 
738                 if(fseeko(stream, (off_t) frame_count * output_size, SEEK_SET) != 0)
739                 {
740                         stream_lock->unlock();
741                         result = 1;
742                         break;
743                 }
744                 
745                 if(fread(audio_buffer, output_size, 1, stream) < 1)
746                 {
747                         stream_lock->unlock();
748                         result = 1;
749                         break;
750                 }
752                 stream_lock->unlock();
753                 
754                 frame_count++;
755                 
756                 decoder_lock->lock("FileDV::read_samples");
757                 
758                 if(dv_decode_full_audio(decoder, audio_buffer, out_buffer) < 0)
759                 {
760                         fprintf(stderr, "Error decoding audio frame %d\n", frame_count - 1);
761                 }
763                 int end = dv_get_num_samples(decoder);
764                 decoder_lock->unlock();
766                 if(len - count + offset < end)
767                         end = len - count + offset;
769                 for(int i = offset; i < end; i++)
770                         buffer[count++] = out_buffer[file->current_channel][i] / 32767.0;
772                 offset = 0;
773         }
774         
775         for(int i = 0; i < channels; i++)
776                 delete[] out_buffer[i];
777         delete[] out_buffer;
779         audio_position += len;
780         
781         return result;
784 int FileDV::read_frame(VFrame *frame)
786         if(stream == 0) return 1;
787         int pitches[3] = {720 * 2, 0, 0};
789 TRACE("FileDV::read_frame 1")
790         unsigned char **row_pointers = frame->get_rows();
793 TRACE("FileDV::read_frame 10")
795         // Seek to video position
796         stream_lock->lock("FileDV::read_frame");
797         if(fseeko(stream, (off_t) video_position * output_size, SEEK_SET) < 0)
798         {
799                 fprintf(stderr, "FileDV::read_frame: Unable to seek file to %lli", (off_t)(video_position * output_size));
800                 stream_lock->unlock();
801                 return 1;
802         }
803         fread(video_buffer, output_size, 1, stream);
804         stream_lock->unlock();
805         video_position++;
806         
808 TRACE("FileDV::read_frame 20")
810         switch(frame->get_color_model())
811         {
812                 case BC_COMPRESSED:
814 TRACE("FileDV::read_frame 30")
816                         frame->allocate_compressed_data(output_size);
817                         frame->set_compressed_size(output_size);
818                         memcpy(frame->get_data(), video_buffer, output_size);
819                         break;
820                 case BC_RGB888:
822 TRACE("FileDV::read_frame 40")
824                         pitches[0] = 720 * 3;
825                         decoder_lock->lock("FileDV::read_frame 10");
826                         dv_decode_full_frame(decoder, video_buffer, e_dv_color_rgb,
827                                 row_pointers, pitches);
828                         decoder_lock->unlock();
829                         break;
830                 case BC_YUV422:
831 TRACE("FileDV::read_frame 50")
832                         decoder_lock->lock("FileDV::read_frame 20");
833                         dv_decode_full_frame(decoder, video_buffer, e_dv_color_yuv,
834                                 row_pointers, pitches);
835                         decoder_lock->unlock();
836                         break;
838                 default:
839                         unsigned char *data = new unsigned char[asset->height * asset->width * 2];
840                         unsigned char **temp_pointers = new unsigned char*[asset->height];
842                         for(int i = 0; i < asset->height; i++)
843                                 temp_pointers[i] = data + asset->width * 2 * i;
844                                 
845                         
846 TRACE("FileDV::read_frame 69")
848                         decoder_lock->lock("FileDV::read_frame 30");
849                         dv_decode_full_frame(decoder, video_buffer, e_dv_color_yuv,
850                                 temp_pointers, pitches);
851                         decoder_lock->unlock();
853 TRACE("FileDV::read_frame 70")
855                         cmodel_transfer(row_pointers,
856                                 temp_pointers,
857                                 row_pointers[0],
858                                 row_pointers[1],
859                                 row_pointers[2],
860                                 temp_pointers[0],
861                                 temp_pointers[1],
862                                 temp_pointers[2],
863                                 0,
864                                 0,
865                                 asset->width,
866                                 asset->height,
867                                 0,
868                                 0,
869                                 asset->width,
870                                 asset->height,
871                                 BC_YUV422,
872                                 frame->get_color_model(),
873                                 0,
874                                 asset->width,
875                                 asset->width);
876                         
877                         //for(int i = 0; i < asset->height; i++)
878                         //      delete[] temp_pointers[i];
879                         delete[] temp_pointers;
880                         delete[] data;
882                         
883                         break;
884         }
886 TRACE("FileDV::read_frame 80")
888 UNTRACE
890         return 0;       
893 int FileDV::colormodel_supported(int colormodel)
895         return colormodel;
898 int FileDV::can_copy_from(Edit *edit, int64_t position)
900         if(edit->asset->format == FILE_RAWDV ||
901                         (edit->asset->format == FILE_MOV &&
902                                 (match4(edit->asset->vcodec, QUICKTIME_DV) ||
903                                 match4(edit->asset->vcodec, QUICKTIME_DVSD) ||
904                                 match4(edit->asset->vcodec, QUICKTIME_DVCP))))
905                 return 1;
907         return 0;
910 int FileDV::get_best_colormodel(Asset *asset, int driver)
912         switch(driver)
913         {
914                 case PLAYBACK_X11:
915                         return BC_RGB888;
916                         break;
917                 case PLAYBACK_X11_XV:
918                         return BC_YUV422;
919                         break;
920                 case PLAYBACK_DV1394:
921                 case PLAYBACK_FIREWIRE:
922                         return BC_COMPRESSED;
923                         break;
924                 case PLAYBACK_LML:
925                 case PLAYBACK_BUZ:
926                         return BC_YUV422P;
927                         break;
928                 case VIDEO4LINUX:
929                 case VIDEO4LINUX2:
930                 case CAPTURE_BUZ:
931                 case CAPTURE_LML:
932                 case VIDEO4LINUX2JPEG:
933                         return BC_YUV422;
934                         break;
935                 case CAPTURE_FIREWIRE:
936                         return BC_COMPRESSED;
937                         break;
938         }
939         return BC_RGB888;
942 int FileDV::get_audio_frame(int64_t pos)
944         return (double) pos * asset->frame_rate / asset->sample_rate;
947 // Get the sample offset from the frame start reported by get_audio_frame
948 int FileDV::get_audio_offset(int64_t pos)
950         int frame = get_audio_frame(pos);
951                 
952         // Samples needed from last frame
953         return  pos - frame * asset->sample_rate / asset->frame_rate;
976 DVConfigAudio::DVConfigAudio(BC_WindowBase *parent_window, Asset *asset)
977  : BC_Window(PROGRAM_NAME ": Audio Compression",
978         parent_window->get_abs_cursor_x(1),
979         parent_window->get_abs_cursor_y(1),
980         350,
981         250)
983         this->parent_window = parent_window;
984         this->asset = asset;
987 DVConfigAudio::~DVConfigAudio()
992 int DVConfigAudio::create_objects()
994         add_tool(new BC_Title(10, 10, _("There are no audio options for this format")));
995         add_subwindow(new BC_OKButton(this));
996         return 0;
999 int DVConfigAudio::close_event()
1001         set_done(0);
1002         return 1;
1010 DVConfigVideo::DVConfigVideo(BC_WindowBase *parent_window, Asset *asset)
1011  : BC_Window(PROGRAM_NAME ": Video Compression",
1012         parent_window->get_abs_cursor_x(1),
1013         parent_window->get_abs_cursor_y(1),
1014         350,
1015         250)
1017         this->parent_window = parent_window;
1018         this->asset = asset;
1021 DVConfigVideo::~DVConfigVideo()
1026 int DVConfigVideo::create_objects()
1028         add_tool(new BC_Title(10, 10, _("There are no video options for this format")));
1029         add_subwindow(new BC_OKButton(this));
1030         return 0;
1033 int DVConfigVideo::close_event()
1035         set_done(0);
1036         return 1;