3 * Copyright 2006 Free Software Foundation, Inc.
5 * This file is part of GNU Radio
7 * GNU Radio is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3, or (at your option)
12 * GNU Radio is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Radio; see the file COPYING. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street,
20 * Boston, MA 02110-1301, USA.
29 #include <video_sdl_sink_uc.h>
30 #include <gr_io_signature.h>
31 #include <sys/types.h>
42 video_sdl_sink_uc::video_sdl_sink_uc (double framerate,int width, int height,unsigned int format,int dst_width,int dst_height)
43 : gr_sync_block ("video_sdl_sink_uc",
44 gr_make_io_signature (1, 3, sizeof (unsigned char)),
45 gr_make_io_signature (0, 0, 0)),
46 d_chunk_size (width*height),
47 d_framerate(framerate),
48 d_wanted_frametime_ms(0),
51 d_dst_width(dst_width),
52 d_dst_height(dst_height),
61 d_wanted_frametime_ms=0;//Go as fast as possible
63 d_wanted_frametime_ms=(int)(1000.0/framerate);
64 if(dst_width<0) d_dst_width=d_width;
65 if(dst_height<0) d_dst_height=d_height;
66 if(0==format) d_format=IMGFMT_YV12;
68 atexit(SDL_Quit);//check if this is the way to do this
69 if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
70 std::cerr << "video_sdl_sink_uc: Couldn't initialize SDL:" << SDL_GetError() << " \n SDL_Init(SDL_INIT_VIDEO) failed\n";
71 throw std::runtime_error ("video_sdl_sink_uc");
74 /* accept any depth */
75 d_screen = SDL_SetVideoMode(dst_width, dst_height, 0, SDL_SWSURFACE|SDL_RESIZABLE|SDL_ANYFORMAT);//SDL_DOUBLEBUF |SDL_SWSURFACE| SDL_HWSURFACE||SDL_FULLSCREEN
76 if ( d_screen == NULL ) {
77 std::cerr << "Unable to set SDL video mode: " << SDL_GetError() <<"\n SDL_SetVideoMode() Failed \n";
81 SDL_FreeYUVOverlay(d_image);
83 /* Initialize and create the YUV Overlay used for video out */
84 if (!(d_image = SDL_CreateYUVOverlay (d_width, d_height, SDL_YV12_OVERLAY, d_screen))) {
85 std::cerr << "SDL: Couldn't create a YUV overlay: \n"<< SDL_GetError() <<"\n";
86 throw std::runtime_error ("video_sdl_sink_uc");
89 printf("SDL screen_mode %d bits-per-pixel\n",
90 d_screen->format->BitsPerPixel);
91 printf("SDL overlay_mode %i \n",
93 d_chunk_size = std::min(1,16384/width); //width*16;
94 d_chunk_size = d_chunk_size*width;
95 //d_chunk_size = (int) (width);
96 set_output_multiple (d_chunk_size);
97 /* Set the default playback area */
100 d_dst_rect.w = d_dst_width;
101 d_dst_rect.h = d_dst_height;
102 //clear the surface to grey
103 if ( SDL_LockYUVOverlay( d_image ) ) {
104 std::cerr << "SDL: Couldn't lock YUV overlay: \n"<< SDL_GetError() <<"\n";
105 throw std::runtime_error ("video_sdl_sink_uc");
107 memset(d_image->pixels[0], 128, d_image->pitches[0]*d_height);
108 memset(d_image->pixels[1], 128, d_image->pitches[1]*d_height/2);
109 memset(d_image->pixels[2], 128, d_image->pitches[2]*d_height/2);
110 SDL_UnlockYUVOverlay( d_image );
113 video_sdl_sink_uc::~video_sdl_sink_uc ()
118 video_sdl_sink_uc_sptr
119 video_sdl_make_sink_uc (double framerate,int width, int height,unsigned int format,int dst_width,int dst_height)
121 return video_sdl_sink_uc_sptr (new video_sdl_sink_uc (framerate, width, height,format,dst_width,dst_height));
125 video_sdl_sink_uc::copy_line_pixel_interleaved(unsigned char *dst_pixels_u,unsigned char *dst_pixels_v,const unsigned char * src_pixels,int src_width)
127 for(int i=0;i<src_width;i++)
129 dst_pixels_u[i]=src_pixels[i*2];
130 dst_pixels_v[i]=src_pixels[i*2+1];
136 video_sdl_sink_uc::copy_line_line_interleaved(unsigned char *dst_pixels_u,unsigned char *dst_pixels_v,const unsigned char * src_pixels,int src_width)
138 memcpy(dst_pixels_u, src_pixels, src_width);
139 memcpy(dst_pixels_v, src_pixels+src_width, src_width);
144 video_sdl_sink_uc::copy_line_single_plane(unsigned char *dst_pixels,const unsigned char * src_pixels,int src_width)
146 memcpy(dst_pixels, src_pixels, src_width);
151 video_sdl_sink_uc::copy_line_single_plane_dec2(unsigned char *dst_pixels,const unsigned char * src_pixels,int src_width)
153 for(int i=0,j=0;i<src_width;i+=2,j++)
155 dst_pixels[j]=(unsigned char)src_pixels[i];
161 video_sdl_sink_uc::copy_plane_to_surface (int plane,int noutput_items,
162 const unsigned char * src_pixels)
164 const int first_dst_plane=(12==plane ||1122==plane)?1:plane;
165 const int second_dst_plane=(12==plane ||1122==plane)?2:plane;
166 int current_line=(0==plane)?d_current_line:d_current_line/2;
167 unsigned char * dst_pixels = (unsigned char *)d_image->pixels[first_dst_plane];
168 dst_pixels=&dst_pixels[current_line*d_image->pitches[first_dst_plane]];
169 unsigned char * dst_pixels_2 = (unsigned char *)d_image->pixels[second_dst_plane];
170 dst_pixels_2=&dst_pixels_2[current_line*d_image->pitches[second_dst_plane]];
171 int src_width=(0==plane || 12==plane || 1122==plane)?d_width:d_width/2;
172 int noutput_items_produced=0;
173 int max_height=(0==plane)?d_height-1:d_height/2-1;
174 for (int i = 0; i < noutput_items; i += src_width){
175 //output one line at a time
178 copy_line_pixel_interleaved(dst_pixels,dst_pixels_2,src_pixels,src_width);
179 dst_pixels_2 += d_image->pitches[second_dst_plane];
181 else if (1122==plane)
183 copy_line_line_interleaved(dst_pixels,dst_pixels_2,src_pixels,src_width);
184 dst_pixels_2 += d_image->pitches[second_dst_plane];
185 src_pixels += src_width;
188 copy_line_single_plane(dst_pixels,src_pixels,src_width);
189 else /* 1==plane || 2==plane*/
190 copy_line_single_plane_dec2(dst_pixels,src_pixels,src_width);//decimate by two horizontally
191 src_pixels += src_width;
192 dst_pixels += d_image->pitches[first_dst_plane];
193 noutput_items_produced+=src_width;
195 if (current_line>max_height)
198 //TODO, do this all in a seperate thread
200 dst_pixels=d_image->pixels[first_dst_plane];
201 dst_pixels_2=d_image->pixels[second_dst_plane];
204 SDL_DisplayYUVOverlay(d_image, &d_dst_rect);
205 //SDL_Flip(d_screen);
206 unsigned int ticks=SDL_GetTicks();//milliseconds
207 d_wanted_ticks+=d_wanted_frametime_ms;
209 int time_diff=d_wanted_ticks-ticks;
210 d_avg_delay=time_diff*avg_alpha +d_avg_delay*(1.0-avg_alpha);
214 if(0==plane) d_current_line=current_line;
215 return noutput_items_produced;
219 video_sdl_sink_uc::work (int noutput_items,
220 gr_vector_const_void_star &input_items,
221 gr_vector_void_star &output_items)
223 unsigned char *src_pixels_0,*src_pixels_1,*src_pixels_2;
224 int noutput_items_produced=0;
226 int delay=(int)d_avg_delay;
227 if(0==d_wanted_ticks)
228 d_wanted_ticks=SDL_GetTicks();
230 SDL_Delay((unsigned int)delay);//compensate if running too fast
232 if ( SDL_LockYUVOverlay( d_image ) ) {
235 switch (input_items.size ()){
236 case 3: // first channel=Y, second channel is U , third channel is V
237 src_pixels_0 = (unsigned char *) input_items[0];
238 src_pixels_1 = (unsigned char *) input_items[1];
239 src_pixels_2 = (unsigned char *) input_items[2];
240 for (int i = 0; i < noutput_items; i += d_chunk_size){
241 copy_plane_to_surface (1,d_chunk_size, src_pixels_1);
242 copy_plane_to_surface (2,d_chunk_size, src_pixels_2);
243 noutput_items_produced+=copy_plane_to_surface (0,d_chunk_size, src_pixels_0);
244 src_pixels_0 += d_chunk_size;
245 src_pixels_1 += d_chunk_size;
246 src_pixels_2 += d_chunk_size;
250 if(1) //if(pixel_interleaved_uv)
252 // first channel=Y, second channel is alternating pixels U and V
253 src_pixels_0 = (unsigned char *) input_items[0];
254 src_pixels_1 = (unsigned char *) input_items[1];
255 for (int i = 0; i < noutput_items; i += d_chunk_size){
256 copy_plane_to_surface (12,d_chunk_size/2, src_pixels_1);
257 noutput_items_produced+=copy_plane_to_surface (0,d_chunk_size, src_pixels_0);
258 src_pixels_0 += d_chunk_size;
259 src_pixels_1 += d_chunk_size;
263 // first channel=Y, second channel is alternating lines U and V
264 src_pixels_0 = (unsigned char *) input_items[0];
265 src_pixels_1 = (unsigned char *) input_items[1];
266 for (int i = 0; i < noutput_items; i += d_chunk_size){
267 copy_plane_to_surface (1222,d_chunk_size/2, src_pixels_1);
268 noutput_items_produced+=copy_plane_to_surface (0,d_chunk_size, src_pixels_0);
269 src_pixels_0 += d_chunk_size;
270 src_pixels_1 += d_chunk_size;
274 case 1: // grey (Y) input
277 src_pixels_0 = (unsigned char *) input_items[plane];
278 for (int i = 0; i < noutput_items; i += d_chunk_size){
279 noutput_items_produced+=copy_plane_to_surface (plane,d_chunk_size, src_pixels_0);
280 src_pixels_0 += d_chunk_size;
283 default: //0 or more then 3 channels
284 std::cerr << "video_sdl_sink_uc: Wrong number of channels: ";
285 std::cerr <<"1, 2 or 3 channels are supported.\n Requested number of channels is "<< input_items.size () <<"\n";
286 throw std::runtime_error ("video_sdl_sink_uc");
289 SDL_UnlockYUVOverlay( d_image );
290 return noutput_items_produced;