add bluray dv, misc fixes
[goodguy/history.git] / cinelerra-5.1 / cinelerra / vdevicev4l2jpeg.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #ifdef HAVE_VIDEO4LINUX2
23 #include "channel.h"
24 #include "file.h"
25 #include "libmjpeg.h"
26 #include "preferences.h"
27 #include "recordconfig.h"
28 #include "vdevicev4l2jpeg.h"
29 #include "vframe.h"
30 #include "videodevice.h"
31
32 #include <string.h>
33
34 VDeviceV4L2JPEG::VDeviceV4L2JPEG(VideoDevice *device)
35  : VDeviceBase(device), DeviceV4L2Base()
36 {
37 }
38
39 VDeviceV4L2JPEG::~VDeviceV4L2JPEG()
40 {
41         close_all();
42 }
43
44 int VDeviceV4L2JPEG::close_all()
45 {
46         close_dev();
47         return 0;
48 }
49
50 int VDeviceV4L2JPEG::open_input()
51 {
52         int result = get_sources();
53         return result;
54 }
55
56 int VDeviceV4L2JPEG::get_best_colormodel(Asset *asset)
57 {
58         return BC_COMPRESSED;
59 }
60
61 int VDeviceV4L2JPEG::has_signal()
62 {
63         return !status_dev() ? dev_signal() : 0;
64 }
65
66 int VDeviceV4L2JPEG::read_buffer(VFrame *frame)
67 {
68         int result = 0;
69
70         if(is_opened())
71         {
72                 if(device->channel_changed || device->picture_changed)
73                         close_dev();
74         }
75
76         if(!is_opened())
77         {
78                 result = open_dev(frame->get_color_model());
79                 if( !result )
80                 {
81                         device->channel_changed = 0;
82                         device->picture_changed = 0;
83                 }
84         }
85
86 // Get buffer from thread
87         if( !result )
88         {
89                 int bfr = get_buffer();
90                 if(bfr >= 0)
91                 {
92                         VFrame *buffer = device_buffer(bfr);
93                         frame->set_timestamp(buffer->get_timestamp());
94                         frame->allocate_compressed_data(buffer->get_compressed_size());
95                         frame->set_compressed_size(buffer->get_compressed_size());
96
97 // Transfer fields to frame
98                         if(device->odd_field_first)
99                         {
100                                 int field2_offset = mjpeg_get_field2((unsigned char*)buffer->get_data(),
101                                         buffer->get_compressed_size());
102                                 int field1_len = field2_offset;
103                                 int field2_len = buffer->get_compressed_size() -
104                                         field2_offset;
105
106                                 memcpy(frame->get_data(),
107                                         buffer->get_data() + field2_offset,
108                                         field2_len);
109                                 memcpy(frame->get_data() + field2_len,
110                                         buffer->get_data(),
111                                         field1_len);
112                         }
113                         else
114                         {
115                                 bcopy(buffer->get_data(),
116                                         frame->get_data(),
117                                         buffer->get_compressed_size());
118                         }
119
120                         put_buffer(bfr);
121                 }
122                 else
123                         result = 1;
124         }
125
126         if( result )
127                 close_dev();
128
129         return result;
130 }
131
132 #endif