⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 yuvinactive.c

📁 Motion JPEG编解码器源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
temp_pix_cb = NULL;free(temp_pix_cr);temp_pix_cr = NULL;}/** Here we set a n x n area to a average color */void average_block(int go_h, int go_w, int horz, int plane, int *orig_offset, uint8_t *frame[]){unsigned char *temp_pix;int i, j, summe, offset_pix;temp_pix = (unsigned char*)malloc(1);*temp_pix=0;offset_pix = *orig_offset;summe = 0;for ( i=0; i < go_h; i++)  {    for (j=0; j < go_w; j++)      {         memcpy(temp_pix,(frame[plane]+offset_pix),1);         summe += (int)*temp_pix;         offset_pix++;      }     offset_pix += horz - j;  }*temp_pix = (unsigned char)(summe / (go_h * go_w));offset_pix = *orig_offset; for (i = 0; i < go_h; i++)  {     for (j = 0; j < go_w; j++)       memcpy((frame[plane]+offset_pix+j), temp_pix, 1);             offset_pix += horz;  }free(temp_pix);temp_pix=NULL;}/** Here we average the area */void average_area(struct area_s inarea, int horz, int vert, uint8_t *frame[],                  int average_pixel){int orig_offset, sub_offset, plane;int go_w, go_h, togo_w, togo_h;orig_offset = (horz* inarea.voffset) + inarea.hoffset; sub_offset = ((horz/2) * (inarea.voffset/2)) + (inarea.hoffset/2);go_w = average_pixel; go_h = average_pixel; togo_w = inarea.width - go_w ; /* we decrease here one block, else we  */togo_h = inarea.height - go_h; /* that solves a problem in the while */ plane = 0; /* here we set that we wnat to use the first plane of the frame */while ( go_h != 0 )  {     while ( go_w != 0)    {     average_block(go_h, go_w, horz, plane, &orig_offset, frame);     average_block(go_h/2, go_w/2, horz/2, plane+1, &sub_offset, frame);     average_block(go_h/2, go_w/2, horz/2, plane+2, &sub_offset, frame);     orig_offset += go_w;     sub_offset += go_w/2;     if ( (togo_w - go_w) >= 0 )       togo_w -= go_w;   /* normal decrease of the square horicontal*/     else if (togo_w != 0)       {       go_w = togo_w;    /* the last few pixels */       togo_w = 0;       }     else       go_w = 0;         /* this row finished averaging the pixels */    }   /* Here we go to the next row we have to average,first line+ (width-1line) */   orig_offset = orig_offset+ (horz -inarea.width) + (horz *(average_pixel -1));   sub_offset = sub_offset+ ((horz/2) - (inarea.width/2) +                            ((horz/2) *(average_pixel/2) -1));    /* we also have to reset the go_w variable, that cost me hours .... */    go_w = average_pixel;   togo_w = inarea.width - average_pixel ;    if ( (togo_h - go_h) >= 0 )     togo_h -= go_h;   /* normal decrease of the square vertical */   else if (togo_h != 0)     {     go_h = togo_h;    /* the last few pixels */     togo_h = 0;     }   else     go_h = 0;         /* this field finished averaging the pixels */  }}/** Here we set the area darker, only touching luma */void set_darker(struct area_s inarea, int horz, int vert, uint8_t *frame[],                int darker){int i, n, hoffset_pix; uint8_t *plane_l;unsigned char *temp_pix;unsigned char *pix;float dark;dark  = 1 - (darker* 0.01);temp_pix = (unsigned char *)malloc(1);pix = (unsigned char *)malloc(1);*temp_pix=0;*pix=0;/* First we do the luma */plane_l  = frame[0];hoffset_pix = (horz * inarea.voffset) + inarea.hoffset;for (i = 0; i < inarea.height; i++)  {    for (n = 0; n < inarea.width; n++)      {         memcpy( temp_pix, (plane_l+hoffset_pix), 1);         *pix = 16 + (int)((*temp_pix - 16) * dark);         if (*pix < 16 ) /* We take care that we don't produce values */           *pix = 16;    /* which should not be used */         memset( (plane_l + hoffset_pix), *pix, 1);         hoffset_pix++;      }    hoffset_pix += (horz - inarea.width) ;  }/* And then the Cr and Cb */plane_l  = frame[1];hoffset_pix = ((horz/2) * (inarea.voffset/2)) + (inarea.hoffset/2);for (i = 0; i < (inarea.height/2); i++)  {    for (n = 0; n < (inarea.width/2); n++)      {         memcpy( temp_pix, (plane_l+hoffset_pix), 1);         *pix = 128 + (int)((*temp_pix - 128) * dark);         memset( (plane_l + hoffset_pix), *pix, 1);         hoffset_pix++;      }    hoffset_pix += ((horz - inarea.width) /2) ;  }plane_l  = frame[2];hoffset_pix = ((horz/2) * (inarea.voffset/2)) + (inarea.hoffset/2);for (i = 0; i < (inarea.height/2); i++)  {    for (n = 0; n < (inarea.width/2); n++)      {         memcpy( temp_pix, (plane_l+hoffset_pix), 1);         *pix = 128 + (int)((*temp_pix - 128) * dark);         memset( (plane_l + hoffset_pix), *pix, 1);         hoffset_pix++;      }    hoffset_pix += ((horz - inarea.width) /2) ;  }free(temp_pix);temp_pix=NULL;free(pix);pix=NULL;}/** Here is the first stage of setting the stream to black */void set_inactive(struct area_s inarea, int horz, int vert, uint8_t *frame[],                  struct color_yuv *coloryuv){int i, hoffset_pix;uint8_t *plane_l, *plane_cb, *plane_cr;plane_l = frame[0];plane_cb= frame[1];plane_cr= frame[2];/* Number of pixels for the luma */hoffset_pix = (horz * inarea.voffset) + inarea.hoffset;for (i = 0; i < inarea.height; i++) /* Setting the Luma */  {    memset( (plane_l + hoffset_pix), (*coloryuv).luma , (inarea.width) );    hoffset_pix += horz;  } /* Number of pixels chroma */hoffset_pix = ((horz / 2)  * (inarea.voffset/2) ) + (inarea.hoffset / 2 );for (i = 0; i < (inarea.height/2); i++) /*Setting the chroma */  {    memset( (plane_cb + hoffset_pix), (*coloryuv).chroma_b, (inarea.width/2) );    memset( (plane_cr + hoffset_pix), (*coloryuv).chroma_r, (inarea.width/2) );    hoffset_pix += (horz/2);  }}/** MAIN */int main( int argc, char **argv){int i, frame_count;int horz, vert;      /* width and height of the frame */uint8_t *frame[3];  /*pointer to the 3 color planes of the input frame */struct area_s inarea;struct color_yuv coloryuv;int input_fd = 0;    /* std in */int output_fd = 1;   /* std out */int darker = 0;  /* how much darker should the image be */int copy_pixel = 0; /* how much pixels we should use for filling up the area */int average_pixel = 0; /* how much pixel to use for average */y4m_stream_info_t istream, ostream;y4m_frame_info_t iframe;inarea.width=0; inarea.height=0; inarea.voffset=0; inarea.hoffset=0;coloryuv.luma    = LUMA;  /*Setting the luma to black */coloryuv.chroma_b = CHROMA; /*Setting the chroma to center, means white */coloryuv.chroma_r = CHROMA; /*Setting the chroma to center, means white */(void)mjpeg_default_handler_verbosity(verbose);  /* processing commandline */  process_commandline(argc, argv, &inarea, &darker, &copy_pixel, &coloryuv,                      &average_pixel);  y4m_init_stream_info(&istream);  y4m_init_stream_info(&ostream);  y4m_init_frame_info(&iframe);  /* First read the header of the y4m stream */  i = y4m_read_stream_header(input_fd, &istream);    if ( i != Y4M_OK)   /* a basic check if we really have y4m stream */    mjpeg_error_exit1("Input stream error: %s", y4m_strerr(i));  else     {      /* Here we copy the input stream info to the output stream info header */      y4m_copy_stream_info(&ostream, &istream);      /* Here we write the new output header to the output fd */      y4m_write_stream_header(output_fd, &ostream);      horz = y4m_si_get_width(&istream);   /* get the width of the frame */      vert = y4m_si_get_height(&istream);  /* get the height of the frame */      if ( (inarea.width + inarea.hoffset) > horz)      mjpeg_error_exit1("Input width and offset larger than framewidth,exit");       if ( (inarea.height + inarea.voffset) > vert)      mjpeg_error_exit1("Input height and offset larger than frameheight,exit");      /* Here we allocate the memory for on frame */      frame[0] = malloc( horz * vert );      frame[1] = malloc( (horz/2) * (vert/2) );      frame[2] = malloc( (horz/2) * (vert/2) );      /* Here we set the initial number of of frames */      /* We do not need it. Just for showing that is does something */      frame_count = 0 ;       /* This is the main loop here can filters effects, scaling and so       on be done with the video frames. Just up to your mind */      /* We read now a single frame with the header and check if it does not      have any problems or we have alreaddy processed the last without data */      while(y4m_read_frame(input_fd, &istream, &iframe, frame) == Y4M_OK)        {           frame_count++;            /* You can do something usefull here */           if (darker != 0)             set_darker(inarea, horz, vert, frame, darker);           else if (copy_pixel != 0)             copy_area(inarea, horz, vert, frame, copy_pixel);           else if (average_pixel != 0)             average_area(inarea, horz, vert, frame, average_pixel);           else             set_inactive(inarea, horz, vert, frame, &coloryuv);           /* Now we put out the read frame */           y4m_write_frame(output_fd, &ostream, &iframe, frame);        }      /* Cleaning up the data structures */      y4m_fini_stream_info(&istream);      y4m_fini_stream_info(&ostream);      y4m_fini_frame_info(&iframe);    }    /* giving back the memory to the system */    free(frame[0]);    frame[0] = 0;    free(frame[1]);    frame[1] = 0;    free(frame[2]);    frame[2] = 0;  exit(0); /* exiting */ }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -