00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "movieVideoCursor.h"
00016 #include "config_movies.h"
00017 #include "pStatCollector.h"
00018 #include "pStatTimer.h"
00019 #include "bamReader.h"
00020 #include "bamWriter.h"
00021
00022 PStatCollector MovieVideoCursor::_copy_pcollector("*:Copy Video into Texture");
00023 PStatCollector MovieVideoCursor::_copy_pcollector_ram("*:Copy Video into Texture:modify_ram_image");
00024 PStatCollector MovieVideoCursor::_copy_pcollector_copy("*:Copy Video into Texture:copy");
00025
00026 TypeHandle MovieVideoCursor::_type_handle;
00027 TypeHandle MovieVideoCursor::Buffer::_type_handle;
00028
00029
00030
00031
00032
00033
00034
00035
00036 MovieVideoCursor::
00037 MovieVideoCursor(MovieVideo *src) :
00038 _source(src),
00039 _size_x(1),
00040 _size_y(1),
00041 _num_components(3),
00042 _length(1.0E10),
00043 _can_seek(true),
00044 _can_seek_fast(true),
00045 _aborted(false),
00046 _streaming(false),
00047 _ready(false)
00048 {
00049 }
00050
00051
00052
00053
00054
00055
00056 MovieVideoCursor::
00057 ~MovieVideoCursor() {
00058 }
00059
00060
00061
00062
00063
00064
00065
00066
00067 void MovieVideoCursor::
00068 setup_texture(Texture *tex) const {
00069 int fullx = size_x();
00070 int fully = size_y();
00071 tex->adjust_this_size(fullx, fully, tex->get_name(), true);
00072 Texture::Format fmt = (get_num_components() == 4) ? Texture::F_rgba : Texture::F_rgb;
00073 tex->setup_texture(Texture::TT_2d_texture, fullx, fully, 1, Texture::T_unsigned_byte, fmt);
00074 tex->set_pad_size(fullx - size_x(), fully - size_y());
00075 }
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102 bool MovieVideoCursor::
00103 set_time(double timestamp, int loop_count) {
00104 return true;
00105 }
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119 PT(MovieVideoCursor::Buffer) MovieVideoCursor::
00120 fetch_buffer() {
00121 return NULL;
00122 }
00123
00124
00125
00126
00127
00128
00129 void MovieVideoCursor::
00130 apply_to_texture(const Buffer *buffer, Texture *t, int page) {
00131 if (buffer == NULL) {
00132 return;
00133 }
00134
00135 PStatTimer timer(_copy_pcollector);
00136
00137 nassertv(t->get_x_size() >= size_x());
00138 nassertv(t->get_y_size() >= size_y());
00139 nassertv((t->get_num_components() == 3) || (t->get_num_components() == 4));
00140 nassertv(t->get_component_width() == 1);
00141 nassertv(page < t->get_num_pages());
00142
00143 PTA_uchar img;
00144 {
00145 PStatTimer timer2(_copy_pcollector_ram);
00146 t->set_keep_ram_image(true);
00147 img = t->modify_ram_image();
00148 }
00149
00150 unsigned char *data = img.p() + page * t->get_expected_ram_page_size();
00151
00152 PStatTimer timer2(_copy_pcollector_copy);
00153 if (t->get_x_size() == size_x() && t->get_num_components() == get_num_components()) {
00154 memcpy(data, buffer->_block, size_x() * size_y() * get_num_components());
00155
00156 } else {
00157 unsigned char *p = buffer->_block;
00158 if (t->get_num_components() == get_num_components()) {
00159 int src_stride = size_x() * get_num_components();
00160 int dst_stride = t->get_x_size() * t->get_num_components();
00161 for (int y=0; y<size_y(); y++) {
00162 memcpy(data, p, src_stride);
00163 data += dst_stride;
00164 p += src_stride;
00165 }
00166 } else {
00167 int src_width = get_num_components();
00168 int dst_width = t->get_num_components();
00169 for (int y = 0; y < size_y(); ++y) {
00170 for (int x = 0; x < size_x(); ++x) {
00171 data[0] = p[0];
00172 data[1] = p[1];
00173 data[2] = p[2];
00174 data += dst_width;
00175 p += src_width;
00176 }
00177 }
00178 }
00179 }
00180 }
00181
00182
00183
00184
00185
00186
00187
00188
00189 void MovieVideoCursor::
00190 apply_to_texture_alpha(const Buffer *buffer, Texture *t, int page, int alpha_src) {
00191 if (buffer == NULL) {
00192 return;
00193 }
00194
00195 PStatTimer timer(_copy_pcollector);
00196
00197 nassertv(t->get_x_size() >= size_x());
00198 nassertv(t->get_y_size() >= size_y());
00199 nassertv(t->get_num_components() == 4);
00200 nassertv(t->get_component_width() == 1);
00201 nassertv(page < t->get_z_size());
00202 nassertv((alpha_src >= 0) && (alpha_src <= get_num_components()));
00203
00204 PTA_uchar img;
00205 {
00206 PStatTimer timer2(_copy_pcollector_ram);
00207 t->set_keep_ram_image(true);
00208 img = t->modify_ram_image();
00209 }
00210
00211 unsigned char *data = img.p() + page * t->get_expected_ram_page_size();
00212
00213 PStatTimer timer2(_copy_pcollector_copy);
00214 int src_stride = size_x() * get_num_components();
00215 int dst_stride = t->get_x_size() * 4;
00216 if (alpha_src == 0) {
00217 unsigned char *p = buffer->_block;
00218 for (int y=0; y<size_y(); y++) {
00219 for (int x=0; x<size_x(); x++) {
00220 data[x*4+3] = (p[x*4+0] + p[x*4+1] + p[x*4+2]) / 3;
00221 }
00222 data += dst_stride;
00223 p += src_stride;
00224 }
00225 } else {
00226 alpha_src -= 1;
00227 unsigned char *p = buffer->_block;
00228 int src_width = get_num_components();
00229 for (int y=0; y<size_y(); y++) {
00230 for (int x=0; x<size_x(); x++) {
00231 data[x*4+3] = p[x *src_width + alpha_src];
00232 }
00233 data += dst_stride;
00234 p += src_stride;
00235 }
00236 }
00237 }
00238
00239
00240
00241
00242
00243
00244
00245
00246 void MovieVideoCursor::
00247 apply_to_texture_rgb(const Buffer *buffer, Texture *t, int page) {
00248 if (buffer == NULL) {
00249 return;
00250 }
00251
00252 PStatTimer timer(_copy_pcollector);
00253
00254 nassertv(t->get_x_size() >= size_x());
00255 nassertv(t->get_y_size() >= size_y());
00256 nassertv(t->get_num_components() == 4);
00257 nassertv(t->get_component_width() == 1);
00258 nassertv(page < t->get_z_size());
00259
00260 PTA_uchar img;
00261 {
00262 PStatTimer timer2(_copy_pcollector_ram);
00263 t->set_keep_ram_image(true);
00264 img = t->modify_ram_image();
00265 }
00266
00267 unsigned char *data = img.p() + page * t->get_expected_ram_page_size();
00268
00269 PStatTimer timer2(_copy_pcollector_copy);
00270 int src_stride = size_x() * get_num_components();
00271 int src_width = get_num_components();
00272 int dst_stride = t->get_x_size() * 4;
00273 unsigned char *p = buffer->_block;
00274 for (int y=0; y<size_y(); y++) {
00275 for (int x=0; x<size_x(); x++) {
00276 data[x * 4 + 0] = p[x * src_width + 0];
00277 data[x * 4 + 1] = p[x * src_width + 1];
00278 data[x * 4 + 2] = p[x * src_width + 2];
00279 }
00280 data += dst_stride;
00281 p += src_stride;
00282 }
00283 }
00284
00285
00286
00287
00288
00289
00290
00291
00292 MovieVideoCursor::Buffer *MovieVideoCursor::
00293 get_standard_buffer() {
00294 if (_standard_buffer == NULL) {
00295 _standard_buffer = make_new_buffer();
00296 }
00297 return _standard_buffer;
00298 }
00299
00300
00301
00302
00303
00304
00305
00306 PT(MovieVideoCursor::Buffer) MovieVideoCursor::
00307 make_new_buffer() {
00308 return new Buffer(size_x() * size_y() * get_num_components());
00309 }
00310
00311
00312
00313
00314
00315
00316
00317 void MovieVideoCursor::
00318 write_datagram(BamWriter *manager, Datagram &dg) {
00319 TypedWritableReferenceCount::write_datagram(manager, dg);
00320
00321 manager->write_pointer(dg, _source);
00322 }
00323
00324
00325
00326
00327
00328
00329
00330
00331 int MovieVideoCursor::
00332 complete_pointers(TypedWritable **p_list, BamReader *manager) {
00333 int pi = TypedWritableReferenceCount::complete_pointers(p_list, manager);
00334
00335 _source = DCAST(MovieVideo, p_list[pi++]);
00336
00337 return pi;
00338 }
00339
00340
00341
00342
00343
00344
00345
00346
00347 void MovieVideoCursor::
00348 fillin(DatagramIterator &scan, BamReader *manager) {
00349 TypedWritableReferenceCount::fillin(scan, manager);
00350
00351 manager->read_pointer(scan);
00352 }
00353
00354
00355
00356
00357
00358
00359 MovieVideoCursor::Buffer::
00360 Buffer(size_t block_size) :
00361 _block_size(block_size)
00362 {
00363 _deleted_chain = memory_hook->get_deleted_chain(_block_size);
00364 _block = (unsigned char *)_deleted_chain->allocate(_block_size, get_class_type());
00365 }
00366
00367
00368
00369
00370
00371
00372 MovieVideoCursor::Buffer::
00373 ~Buffer() {
00374 _deleted_chain->deallocate(_block, get_class_type());
00375 }
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389 int MovieVideoCursor::Buffer::
00390 compare_timestamp(const Buffer *other) const {
00391 return 0;
00392 }
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403 double MovieVideoCursor::Buffer::
00404 get_timestamp() const {
00405 return 0.0;
00406 }