4 * Copyright (C) 2011 Adam Williams <broadcast at earthling dot net>
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.
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.
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
31 #include "bcpbuffer.h"
32 #include "bcresources.h"
33 #include "bcsignals.h"
34 #include "bcsynchronous.h"
35 #include "bctexture.h"
36 #include "bcwindowbase.h"
38 #include "bccmodels.h"
44 static void png_read_function(png_structp png_ptr,
45 png_bytep data, png_size_t length)
47 VFrame *frame = (VFrame*)png_get_io_ptr(png_ptr);
48 if(frame->image_size - frame->image_offset < (long)length)
50 printf("PngReadFunction::png_read_function %d: overrun\n", __LINE__);
51 length = frame->image_size - frame->image_offset;
54 memcpy(data, &frame->image[frame->image_offset], length);
55 frame->image_offset += length;
65 VFrameScene::VFrameScene()
69 VFrameScene::~VFrameScene()
79 //static BCCounter counter;
82 VFrame::VFrame(unsigned char *png_data)
89 VFrame::VFrame(unsigned char *png_data, long image_size)
93 read_png(png_data, image_size);
96 VFrame::VFrame(VFrame &frame)
100 allocate_data(0, -1, 0, 0, 0, frame.w, frame.h,
101 frame.color_model, frame.bytes_per_line);
102 memcpy(data, frame.data, bytes_per_line * h);
106 VFrame::VFrame(int w, int h, int color_model, long bytes_per_line)
109 params = new BC_Hash;
110 allocate_data(data, -1, 0, 0, 0, w, h,
111 color_model, bytes_per_line);
114 VFrame::VFrame(unsigned char *data, int shmid, int w, int h,
115 int color_model, long bytes_per_line)
118 params = new BC_Hash;
119 allocate_data(data, shmid, 0, 0, 0, w, h,
120 color_model, bytes_per_line);
123 VFrame::VFrame(unsigned char *data, int shmid,
124 long y_offset, long u_offset, long v_offset,
125 int w, int h, int color_model, long bytes_per_line)
128 params = new BC_Hash;
129 allocate_data(data, shmid, y_offset, u_offset, v_offset, w, h,
130 color_model, bytes_per_line);
133 VFrame::VFrame(BC_Bitmap *bitmap, int w, int h,
134 int color_model, long bytes_per_line)
137 params = new BC_Hash;
139 unsigned char *data = 0;
140 if( bitmap->is_shared() )
141 shmid = bitmap->get_shmid();
143 data = bitmap->get_data();
144 allocate_data(data, shmid,
145 bitmap->get_y_offset(),
146 bitmap->get_u_offset(),
147 bitmap->get_v_offset(),
148 w, h, color_model, bytes_per_line);
154 params = new BC_Hash;
155 this->color_model = BC_COMPRESSED;
163 // Delete effect stack
164 prev_effects.remove_all_objects();
165 next_effects.remove_all_objects();
170 int VFrame::equivalent(VFrame *src, int test_stacks)
172 return (src->get_color_model() == get_color_model() &&
173 src->get_w() == get_w() &&
174 src->get_h() == get_h() &&
175 src->bytes_per_line == bytes_per_line &&
176 (!test_stacks || equal_stacks(src)));
179 int VFrame::data_matches(VFrame *frame)
181 if(data && frame->get_data() &&
182 frame->params_match(get_w(), get_h(), get_color_model()) &&
183 get_data_size() == frame->get_data_size())
185 int data_size = get_data_size();
186 unsigned char *ptr1 = get_data();
187 unsigned char *ptr2 = frame->get_data();
188 for(int i = 0; i < data_size; i++)
190 if(*ptr1++ != *ptr2++) return 0;
197 // long VFrame::set_shm_offset(long offset)
199 // shm_offset = offset;
203 // long VFrame::get_shm_offset()
205 // return shm_offset;
208 int VFrame::get_memory_type()
213 int VFrame::params_match(int w, int h, int color_model)
215 return (this->w == w &&
217 this->color_model == color_model);
221 int VFrame::reset_parameters(int do_opengl)
225 memory_type = VFrame::PRIVATE;
233 compressed_allocated = 0;
234 compressed_size = 0; // Size of current image
241 sequence_number = -1;
247 // By default, anything is going to be done in RAM
248 opengl_state = VFrame::RAM;
253 prev_effects.set_array_delete();
254 next_effects.set_array_delete();
258 int VFrame::clear_objects(int do_opengl)
273 case VFrame::PRIVATE:
275 // if(this->w * this->h > 1500 * 1100)
276 // printf("VFrame::clear_objects 2 this=%p data=%p\n", this, data);
279 //printf("VFrame::clear_objects %d this=%p shmid=%p data=%p\n", __LINE__, this, shmid, data);
299 // Delete row pointers
322 int VFrame::get_field2_offset()
324 return field2_offset;
327 int VFrame::set_field2_offset(int value)
329 this->field2_offset = value;
333 void VFrame::set_keyframe(int value)
335 this->is_keyframe = value;
338 int VFrame::get_keyframe()
344 VFrameScene* VFrame::get_scene()
349 int VFrame::calculate_bytes_per_pixel(int color_model)
351 return BC_CModels::calculate_pixelsize(color_model);
354 long VFrame::get_bytes_per_line()
356 return bytes_per_line;
359 long VFrame::get_data_size()
361 return calculate_data_size(w, h, bytes_per_line, color_model) - 4;
364 long VFrame::calculate_data_size(int w, int h, int bytes_per_line, int color_model)
366 return BC_CModels::calculate_datasize(w, h, bytes_per_line, color_model);
369 void VFrame::create_row_pointers()
372 switch(color_model) {
374 if( this->v_offset ) break;
377 this->v_offset = sz + w / 4 * h / 4;
382 if( this->v_offset ) break;
385 this->v_offset = sz + sz / 4;
389 if( this->v_offset ) break;
392 this->v_offset = sz + sz / 2;
395 if( this->v_offset ) break;
398 this->v_offset = sz + sz;
401 if( this->v_offset || a ) break;
402 a = this->data + 3 * sz * sizeof(float);
404 if( this->v_offset ) break;
406 this->u_offset = sz * sizeof(float);
407 this->v_offset = 2 * sz * sizeof(float);
411 rows = new unsigned char*[h];
412 for(int i = 0; i < h; i++)
413 rows[i] = &this->data[i * this->bytes_per_line];
416 y = this->data + this->y_offset;
417 u = this->data + this->u_offset;
418 v = this->data + this->v_offset;
421 int VFrame::allocate_data(unsigned char *data, int shmid,
422 long y_offset, long u_offset, long v_offset, int w, int h,
423 int color_model, long bytes_per_line)
427 this->color_model = color_model;
428 this->bytes_per_pixel = calculate_bytes_per_pixel(color_model);
429 this->y_offset = this->u_offset = this->v_offset = 0;
431 // printf("VFrame::allocate_data %d shmid == 0\n", __LINE__, shmid);
434 this->bytes_per_line = bytes_per_line >= 0 ?
435 bytes_per_line : this->bytes_per_pixel * w;
437 // Allocate data + padding for MMX
439 //printf("VFrame::allocate_data %d %p\n", __LINE__, this->data);
440 memory_type = VFrame::SHARED;
443 this->y_offset = y_offset;
444 this->u_offset = u_offset;
445 this->v_offset = v_offset;
447 else if(shmid >= 0) {
448 memory_type = VFrame::SHMGET;
449 this->data = (unsigned char*)shmat(shmid, NULL, 0);
450 //printf("VFrame::allocate_data %d shmid=%d data=%p\n", __LINE__, shmid, this->data);
452 this->y_offset = y_offset;
453 this->u_offset = u_offset;
454 this->v_offset = v_offset;
457 memory_type = VFrame::PRIVATE;
458 int size = calculate_data_size(this->w, this->h,
459 this->bytes_per_line, this->color_model);
460 if(BC_WindowBase::get_resources()->use_vframe_shm() && use_shm) {
461 this->shmid = shmget(IPC_PRIVATE, size, IPC_CREAT | 0777);
462 if(this->shmid < 0) {
463 printf("VFrame::allocate_data %d could not allocate shared memory\n", __LINE__);
466 this->data = (unsigned char*)shmat(this->shmid, NULL, 0);
467 //printf("VFrame::allocate_data %d %d %d\n", __LINE__, size, this->shmid);
469 //printf("VFrame::allocate_data %d %p\n", __LINE__, this->data);
470 // This causes it to automatically delete when the program exits.
471 shmctl(this->shmid, IPC_RMID, 0);
474 // Have to use malloc for libpng
475 this->data = (unsigned char *)malloc(size);
479 // if(this->w * this->h > 1500 * 1100)
480 // printf("VFrame::allocate_data 2 this=%p w=%d h=%d this->data=%p\n",
481 // this, this->w, this->h, this->data);
484 printf("VFrame::allocate_data %dx%d: memory exhausted.\n", this->w, this->h);
486 //printf("VFrame::allocate_data %d %p data=%p %d %d\n", __LINE__, this, this->data, this->w, this->h);
487 //if(size > 1000000) printf("VFrame::allocate_data %d\n", size);
490 // Create row pointers
491 create_row_pointers();
495 void VFrame::set_memory(unsigned char *data,
505 memory_type = VFrame::SHARED;
508 this->y_offset = y_offset;
509 this->u_offset = u_offset;
510 this->v_offset = v_offset;
515 memory_type = VFrame::SHMGET;
516 this->data = (unsigned char*)shmat(shmid, NULL, 0);
520 y = this->data + this->y_offset;
521 u = this->data + this->u_offset;
522 v = this->data + this->v_offset;
524 create_row_pointers();
527 void VFrame::set_memory(BC_Bitmap *bitmap)
530 unsigned char *data = 0;
531 if( bitmap->is_shared() )
532 shmid = bitmap->get_shmid();
534 data = bitmap->get_data();
535 set_memory(data, shmid,
536 bitmap->get_y_offset(),
537 bitmap->get_u_offset(),
538 bitmap->get_v_offset());
541 void VFrame::set_compressed_memory(unsigned char *data,
550 memory_type = VFrame::SHARED;
557 memory_type = VFrame::SHMGET;
558 this->data = (unsigned char*)shmat(shmid, NULL, 0);
562 this->compressed_allocated = data_allocated;
563 this->compressed_size = data_size;
567 // Reallocate uncompressed buffer with or without alpha
568 int VFrame::reallocate(
579 // if(shmid == 0) printf("VFrame::reallocate %d shmid=%d\n", __LINE__, shmid);
581 // reset_parameters(0);
594 int VFrame::allocate_compressed_data(long bytes)
596 if(bytes < 1) return 1;
598 // Want to preserve original contents
599 if(data && compressed_allocated < bytes)
602 unsigned char *new_data = 0;
603 if(BC_WindowBase::get_resources()->use_vframe_shm() && use_shm)
605 new_shmid = shmget(IPC_PRIVATE,
608 new_data = (unsigned char*)shmat(new_shmid, NULL, 0);
609 shmctl(new_shmid, IPC_RMID, 0);
613 // Have to use malloc for libpng
614 new_data = (unsigned char *)malloc(bytes);
617 bcopy(data, new_data, compressed_allocated);
620 if(memory_type == VFrame::PRIVATE)
630 if(memory_type == VFrame::SHMGET)
638 compressed_allocated = bytes;
643 if(BC_WindowBase::get_resources()->use_vframe_shm() && use_shm)
645 shmid = shmget(IPC_PRIVATE,
648 data = (unsigned char*)shmat(shmid, NULL, 0);
649 shmctl(shmid, IPC_RMID, 0);
653 // Have to use malloc for libpng
654 data = (unsigned char *)malloc(bytes);
657 compressed_allocated = bytes;
664 int VFrame::read_png(const unsigned char *data, long img_sz)
666 // Test for RAW format
667 if(data[0] == 'R' && data[1] == 'A' && data[2] == 'W' && data[3] == ' ') {
668 int new_color_model = BC_RGBA8888;
669 w = data[4] | (data[5] << 8) | (data[6] << 16) | (data[7] << 24);
670 h = data[8] | (data[9] << 8) | (data[10] << 16) | (data[11] << 24);
671 int components = data[12];
672 new_color_model = components == 3 ? BC_RGB888 : BC_RGBA8888;
673 // This shares the data directly
674 // reallocate(data + 20, 0, 0, 0, w, h, new_color_model, -1);
676 // Can't use shared data for theme since button constructions overlay the
678 reallocate(NULL, -1, 0, 0, 0, w, h, new_color_model, -1);
679 memcpy(get_data(), data + 16, w * h * components);
682 else if(data[0] == 0x89 && data[1] == 'P' && data[2] == 'N' && data[3] == 'G') {
684 png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
685 png_infop info_ptr = png_create_info_struct(png_ptr);
689 image = data; image_size = img_sz;
690 png_set_read_fn(png_ptr, this, PngReadFunction::png_read_function);
691 png_read_info(png_ptr, info_ptr);
693 w = png_get_image_width(png_ptr, info_ptr);
694 h = png_get_image_height(png_ptr, info_ptr);
696 int src_color_model = png_get_color_type(png_ptr, info_ptr);
698 /* tell libpng to strip 16 bit/color files down to 8 bits/color */
699 png_set_strip_16(png_ptr);
701 /* extract multiple pixels with bit depths of 1, 2, and 4 from a single
702 * byte into separate bytes (useful for paletted and grayscale images).
704 png_set_packing(png_ptr);
706 /* expand paletted colors into true RGB triplets */
707 if (src_color_model == PNG_COLOR_TYPE_PALETTE)
708 png_set_expand(png_ptr);
710 /* expand grayscale images to the full 8 bits from 1, 2, or 4 bits/pixel */
711 if (src_color_model == PNG_COLOR_TYPE_GRAY && png_get_bit_depth(png_ptr, info_ptr) < 8)
712 png_set_expand(png_ptr);
714 if (src_color_model == PNG_COLOR_TYPE_GRAY ||
715 src_color_model == PNG_COLOR_TYPE_GRAY_ALPHA)
716 png_set_gray_to_rgb(png_ptr);
718 /* expand paletted or RGB images with transparency to full alpha channels
719 * so the data will be available as RGBA quartets */
720 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)){
722 png_set_expand(png_ptr);
725 switch(src_color_model)
727 case PNG_COLOR_TYPE_GRAY:
728 case PNG_COLOR_TYPE_RGB:
729 new_color_model = BC_RGB888;
732 case PNG_COLOR_TYPE_GRAY_ALPHA:
733 case PNG_COLOR_TYPE_RGB_ALPHA:
735 new_color_model = BC_RGBA8888;
738 case PNG_COLOR_TYPE_PALETTE:
740 new_color_model = BC_RGBA8888;
742 new_color_model = BC_RGB888;
745 reallocate(NULL, -1, 0, 0, 0, w, h, new_color_model, -1);
747 //printf("VFrame::read_png %d %d %d %p\n", __LINE__, w, h, get_rows());
748 png_read_image(png_ptr, get_rows());
749 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
752 printf("VFrame::read_png %d: unknown file format"
753 " 0x%02x 0x%02x 0x%02x 0x%02x\n",
754 __LINE__, data[4], data[5], data[6], data[7]);
759 int VFrame::read_png(const unsigned char *data)
762 ((long)data[0] << 24) | ((long)data[1] << 16) |
763 ((long)data[2] << 8) | (long)data[3];
764 return read_png(data+4, img_sz);
767 int VFrame::write_png(const char *path)
769 png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
770 png_infop info_ptr = png_create_info_struct(png_ptr);
771 FILE *out_fd = fopen(path, "w");
774 printf("VFrame::write_png %d %s %s\n", __LINE__, path, strerror(errno));
778 int png_cmodel = PNG_COLOR_TYPE_RGB;
779 switch(get_color_model())
783 png_cmodel = PNG_COLOR_TYPE_RGB;
787 png_cmodel = PNG_COLOR_TYPE_GRAY;
791 png_init_io(png_ptr, out_fd);
792 png_set_compression_level(png_ptr, 9);
793 png_set_IHDR(png_ptr,
800 PNG_COMPRESSION_TYPE_DEFAULT,
801 PNG_FILTER_TYPE_DEFAULT);
802 png_write_info(png_ptr, info_ptr);
803 png_write_image(png_ptr, get_rows());
804 png_write_end(png_ptr, info_ptr);
805 png_destroy_write_struct(&png_ptr, &info_ptr);
811 #define ZERO_YUV(components, type, max) \
813 for(int i = 0; i < h; i++) \
815 type *row = (type*)get_rows()[i]; \
816 for(int j = 0; j < w; j++) \
818 row[j * components] = 0; \
819 row[j * components + 1] = (max + 1) / 2; \
820 row[j * components + 2] = (max + 1) / 2; \
821 if(components == 4) row[j * components + 3] = 0; \
826 int VFrame::clear_frame()
829 //printf("VFrame::clear_frame %d\n", __LINE__);
830 switch(color_model) {
836 bzero(get_u(), w / 4 * h / 4);
837 bzero(get_v(), w / 4 * h / 4);
843 bzero(get_u(), sz / 4);
844 bzero(get_v(), sz / 4);
849 bzero(get_u(), sz / 2);
850 bzero(get_v(), sz / 2);
853 case BC_RGBA_FLOATP: if( a ) {
854 float *ap = (float *)a;
855 for( int i=sz; --i>=0; ++ap ) *ap = 1.f; }
856 case BC_RGB_FLOATP: {
857 float *rp = (float *)y;
858 for( int i=sz; --i>=0; ++rp ) *rp = 0.f;
859 float *gp = (float *)u;
860 for( int i=sz; --i>=0; ++gp ) *gp = 0.f;
861 float *bp = (float *)v;
862 for( int i=sz; --i>=0; ++bp ) *bp = 0.f;
871 ZERO_YUV(3, unsigned char, 0xff);
875 ZERO_YUV(4, unsigned char, 0xff);
879 ZERO_YUV(3, uint16_t, 0xffff);
882 case BC_YUVA16161616:
883 ZERO_YUV(4, uint16_t, 0xffff);
887 bzero(data, calculate_data_size(w, h, bytes_per_line, color_model));
893 void VFrame::rotate90()
895 // Allocate new frame
896 int new_w = h, new_h = w, new_bytes_per_line = bytes_per_pixel * new_w;
897 unsigned char *new_data = new unsigned char[calculate_data_size(new_w, new_h, new_bytes_per_line, color_model)];
898 unsigned char **new_rows = new unsigned char*[new_h];
899 for(int i = 0; i < new_h; i++)
900 new_rows[i] = &new_data[new_bytes_per_line * i];
903 for(int in_y = 0, out_x = new_w - 1; in_y < h; in_y++, out_x--)
905 for(int in_x = 0, out_y = 0; in_x < w; in_x++, out_y++)
907 for(int k = 0; k < bytes_per_pixel; k++)
909 new_rows[out_y][out_x * bytes_per_pixel + k] =
910 rows[in_y][in_x * bytes_per_pixel + k];
919 bytes_per_line = new_bytes_per_line;
924 void VFrame::rotate270()
926 // Allocate new frame
927 int new_w = h, new_h = w, new_bytes_per_line = bytes_per_pixel * new_w;
928 unsigned char *new_data = new unsigned char[calculate_data_size(new_w, new_h, new_bytes_per_line, color_model)];
929 unsigned char **new_rows = new unsigned char*[new_h];
930 for(int i = 0; i < new_h; i++)
931 new_rows[i] = &new_data[new_bytes_per_line * i];
934 for(int in_y = 0, out_x = 0; in_y < h; in_y++, out_x++)
936 for(int in_x = 0, out_y = new_h - 1; in_x < w; in_x++, out_y--)
938 for(int k = 0; k < bytes_per_pixel; k++)
940 new_rows[out_y][out_x * bytes_per_pixel + k] =
941 rows[in_y][in_x * bytes_per_pixel + k];
950 bytes_per_line = new_bytes_per_line;
955 void VFrame::flip_vert()
957 unsigned char *temp = new unsigned char[bytes_per_line];
958 for(int i = 0, j = h - 1; i < j; i++, j--)
960 memcpy(temp, rows[j], bytes_per_line);
961 memcpy(rows[j], rows[i], bytes_per_line);
962 memcpy(rows[i], temp, bytes_per_line);
967 void VFrame::flip_horiz()
969 unsigned char temp[32];
970 for(int i = 0; i < h; i++)
972 unsigned char *row = rows[i];
973 for(int j = 0; j < bytes_per_line / 2; j += bytes_per_pixel)
975 memcpy(temp, row + j, bytes_per_pixel);
976 memcpy(row + j, row + bytes_per_line - j - bytes_per_pixel, bytes_per_pixel);
977 memcpy(row + bytes_per_line - j - bytes_per_pixel, temp, bytes_per_pixel);
984 int VFrame::copy_from(VFrame *frame)
986 int w = MIN(this->w, frame->get_w());
987 int h = MIN(this->h, frame->get_h());
988 timestamp = frame->timestamp;
990 switch(frame->color_model)
993 allocate_compressed_data(frame->compressed_size);
994 memcpy(data, frame->data, frame->compressed_size);
995 this->compressed_size = frame->compressed_size;
999 memcpy(get_y(), frame->get_y(), w * h);
1000 memcpy(get_u(), frame->get_u(), w / 4 * h / 4);
1001 memcpy(get_v(), frame->get_v(), w / 4 * h / 4);
1006 //printf("%d %d %p %p %p %p %p %p\n", w, h, get_y(), get_u(), get_v(), frame->get_y(), frame->get_u(), frame->get_v());
1007 memcpy(get_y(), frame->get_y(), w * h);
1008 memcpy(get_u(), frame->get_u(), w * h / 4);
1009 memcpy(get_v(), frame->get_v(), w * h / 4);
1013 //printf("%d %d %p %p %p %p %p %p\n", w, h, get_y(), get_u(), get_v(), frame->get_y(), frame->get_u(), frame->get_v());
1014 memcpy(get_y(), frame->get_y(), w * h);
1015 memcpy(get_u(), frame->get_u(), w * h / 2);
1016 memcpy(get_v(), frame->get_v(), w * h / 2);
1020 //printf("%d %d %p %p %p %p %p %p\n", w, h, get_y(), get_u(), get_v(), frame->get_y(), frame->get_u(), frame->get_v());
1021 memcpy(get_y(), frame->get_y(), w * h);
1022 memcpy(get_u(), frame->get_u(), w * h);
1023 memcpy(get_v(), frame->get_v(), w * h);
1026 // printf("VFrame::copy_from %d\n", calculate_data_size(w,
1029 // frame->color_model));
1030 // Copy without extra 4 bytes in case the source is a hardware device
1031 memmove(data, frame->data, get_data_size());
1038 int VFrame::transfer_from(VFrame *that, int bg_color)
1040 if( this->get_color_model() == that->get_color_model() &&
1041 this->get_w() == that->get_w() && this->get_h() == that->get_h() )
1042 return this->copy_from(that);
1044 timestamp = that->timestamp;
1045 BC_CModels::transfer(
1046 this->get_rows(), that->get_rows(), // Packed data out/in
1047 this->get_y(), this->get_u(), this->get_v(), // Planar data out/in
1048 that->get_y(), that->get_u(), that->get_v(),
1049 0, 0, that->get_w(), that->get_h(), // Dimensions in/out
1050 0, 0, this->get_w(), this->get_h(),
1051 that->get_color_model(), this->get_color_model(), // Color models in/out
1052 bg_color, // alpha blend bg_color
1053 that->get_w(), this->get_w()); // rowspans (of luma for YUV)
1062 #define OVERLAY(type, max, components) \
1064 type **in_rows = (type**)src->get_rows(); \
1065 type **out_rows = (type**)get_rows(); \
1066 int in_w = src->get_w(); \
1067 int in_h = src->get_h(); \
1069 for(int i = 0; i < in_h; i++) \
1071 if(i + out_y1 >= 0 && i + out_y1 < h) \
1073 type *src_row = in_rows[i]; \
1074 type *dst_row = out_rows[i + out_y1] + out_x1 * components; \
1076 for(int j = 0; j < in_w; j++) \
1078 if(j + out_x1 >= 0 && j + out_x1 < w) \
1080 int opacity = src_row[3]; \
1081 int transparency = dst_row[3] * (max - src_row[3]) / max; \
1082 dst_row[0] = (transparency * dst_row[0] + opacity * src_row[0]) / max; \
1083 dst_row[1] = (transparency * dst_row[1] + opacity * src_row[1]) / max; \
1084 dst_row[2] = (transparency * dst_row[2] + opacity * src_row[2]) / max; \
1085 dst_row[3] = MAX(dst_row[3], src_row[3]); \
1088 dst_row += components; \
1089 src_row += components; \
1096 void VFrame::overlay(VFrame *src,
1100 switch(get_color_model())
1103 OVERLAY(unsigned char, 0xff, 4);
1110 int VFrame::get_scale_tables(int *column_table, int *row_table,
1111 int in_x1, int in_y1, int in_x2, int in_y2,
1112 int out_x1, int out_y1, int out_x2, int out_y2)
1115 float w_in = in_x2 - in_x1;
1116 float h_in = in_y2 - in_y1;
1117 int w_out = out_x2 - out_x1;
1118 int h_out = out_y2 - out_y1;
1120 float hscale = w_in / w_out;
1121 float vscale = h_in / h_out;
1123 for(i = 0; i < w_out; i++)
1125 column_table[i] = (int)(hscale * i);
1128 for(i = 0; i < h_out; i++)
1130 row_table[i] = (int)(vscale * i) + in_y1;
1135 void VFrame::push_prev_effect(const char *name)
1138 prev_effects.append(ptr = new char[strlen(name) + 1]);
1140 if(prev_effects.total > MAX_STACK_ELEMENTS) prev_effects.remove_object(0);
1143 void VFrame::pop_prev_effect()
1145 if(prev_effects.total)
1146 prev_effects.remove_object(prev_effects.last());
1149 void VFrame::push_next_effect(const char *name)
1152 next_effects.append(ptr = new char[strlen(name) + 1]);
1154 if(next_effects.total > MAX_STACK_ELEMENTS) next_effects.remove_object(0);
1157 void VFrame::pop_next_effect()
1159 if(next_effects.total)
1160 next_effects.remove_object(next_effects.last());
1163 const char* VFrame::get_next_effect(int number)
1165 if(!next_effects.total) return "";
1167 if(number > next_effects.total - 1) number = next_effects.total - 1;
1169 return next_effects.values[next_effects.total - number - 1];
1172 const char* VFrame::get_prev_effect(int number)
1174 if(!prev_effects.total) return "";
1176 if(number > prev_effects.total - 1) number = prev_effects.total - 1;
1178 return prev_effects.values[prev_effects.total - number - 1];
1181 BC_Hash* VFrame::get_params()
1186 void VFrame::clear_stacks()
1188 next_effects.remove_all_objects();
1189 prev_effects.remove_all_objects();
1191 params = new BC_Hash;
1194 void VFrame::copy_params(VFrame *src)
1196 params->copy_from(src->params);
1199 void VFrame::copy_stacks(VFrame *src)
1203 for(int i = 0; i < src->next_effects.total; i++)
1206 next_effects.append(ptr = new char[strlen(src->next_effects.values[i]) + 1]);
1207 strcpy(ptr, src->next_effects.values[i]);
1209 for(int i = 0; i < src->prev_effects.total; i++)
1212 prev_effects.append(ptr = new char[strlen(src->prev_effects.values[i]) + 1]);
1213 strcpy(ptr, src->prev_effects.values[i]);
1216 params->copy_from(src->params);
1219 int VFrame::equal_stacks(VFrame *src)
1221 for(int i = 0; i < src->next_effects.total && i < next_effects.total; i++)
1223 if(strcmp(src->next_effects.values[i], next_effects.values[i])) return 0;
1226 for(int i = 0; i < src->prev_effects.total && i < prev_effects.total; i++)
1228 if(strcmp(src->prev_effects.values[i], prev_effects.values[i])) return 0;
1231 if(!params->equivalent(src->params)) return 0;
1235 void VFrame::dump_stacks()
1237 printf("VFrame::dump_stacks\n");
1238 printf(" next_effects:\n");
1239 for(int i = next_effects.total - 1; i >= 0; i--)
1240 printf(" %s\n", next_effects.values[i]);
1241 printf(" prev_effects:\n");
1242 for(int i = prev_effects.total - 1; i >= 0; i--)
1243 printf(" %s\n", prev_effects.values[i]);
1246 void VFrame::dump_params()
1253 printf("VFrame::dump %d this=%p\n", __LINE__, this);
1254 printf(" w=%d h=%d colormodel=%d rows=%p use_shm=%d shmid=%d\n",
1255 w, h, color_model, rows, use_shm, shmid);
1258 int VFrame::filefork_size()
1260 return sizeof(int) * 12 + sizeof(long);
1264 void VFrame::to_filefork(unsigned char *buffer)
1266 *(int*)(buffer + 0) = shmid;
1267 *(int*)(buffer + 4) = y_offset;
1268 *(int*)(buffer + 8) = u_offset;
1269 *(int*)(buffer + 12) = v_offset;
1270 *(int*)(buffer + 16) = w;
1271 *(int*)(buffer + 20) = h;
1272 *(int*)(buffer + 24) = color_model;
1273 *(int*)(buffer + 28) = bytes_per_line;
1274 *(int*)(buffer + 32) = compressed_allocated;
1275 *(int*)(buffer + 36) = compressed_size;
1276 *(int*)(buffer + 40) = is_keyframe;
1277 *(long*)(buffer + 44) = sequence_number;
1280 //printf("VFrame::to_filefork %d %lld\n", __LINE__, sequence_number);
1281 // printf("VFrame::to_filefork %d", __LINE__);
1282 // for(int i = 0; i < 40; i++)
1284 // printf(" %02x", buffer[i]);
1291 void VFrame::from_filefork(unsigned char *buffer)
1293 // This frame will always be preallocated shared memory
1294 //printf("VFrame::from_filefork %d %d\n", __LINE__, *(int*)(buffer + 24));
1295 if(*(int*)(buffer + 24) == BC_COMPRESSED)
1297 set_compressed_memory(0,
1298 *(int*)(buffer + 0), // shmid
1299 *(int*)(buffer + 36), // compressed_size
1300 *(int*)(buffer + 32)); // compressed_allocated
1301 color_model = BC_COMPRESSED;
1302 //printf("VFrame::from_filefork %d %d\n", __LINE__, get_compressed_size());
1307 // printf("VFrame::from_filefork %d", __LINE__);
1308 // for(int i = 0; i < 40; i++)
1310 // printf(" %02x", buffer[i]);
1314 *(int*)(buffer + 0), // shmid
1315 *(int*)(buffer + 4), // y_offset
1316 *(int*)(buffer + 8), // u_offset
1317 *(int*)(buffer + 12), // v_offset
1318 *(int*)(buffer + 16), // w
1319 *(int*)(buffer + 20), // h
1320 *(int*)(buffer + 24), // colormodel
1321 *(int*)(buffer + 28)); // bytes per line
1325 is_keyframe = *(int*)(buffer + 40);
1326 sequence_number = *(long*)(buffer + 44);
1327 //printf("VFrame::from_filefork %d %lld\n", __LINE__, sequence_number);
1330 int VFrame::get_memory_usage()
1332 if(get_compressed_allocated()) return get_compressed_allocated();
1333 return get_h() * get_bytes_per_line();