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

📄 png2yuv.c

📁 Motion JPEG编解码器源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/*png2yuv========  Converts a collection of PNG images to a YUV4MPEG stream.  (see png2yuv -h for help (or have a look at the function "usage"))    PNG (Portable Network Graphics) is a lossless 2D image format.   See www.libpng.org for more information.   Based on rpf2yuv.c.  Copyright (C) 1999, 2002 Gernot Ziegler (gz@lysator.liu.se)  Copyright (C) 2001, 2004 Matthew Marjanovic (maddog@mir.com)  This program is free software; you can redistribute it and/or modify  it under the terms of the GNU General Public License as published by  the Free Software Foundation; either version 2 of the License, or  (at your option) any later version.    This program is distributed in the hope that it will be useful,  but WITHOUT ANY WARRANTY; without even the implied warranty of  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the  GNU General Public License for more details.    You should have received a copy of the GNU General Public License  along with this program; if not, write to the Free Software  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.*/#include <config.h>#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <string.h>#include <errno.h>#include <sys/types.h>#include <png.h>#include "mjpeg_logging.h"#include "mjpeg_types.h"#include "yuv4mpeg.h"#include "mpegconsts.h"#include "subsample.h"#include "colorspace.h"//#include "mplexconsts.hh"#define DEFAULT_CHROMA_MODE Y4M_CHROMA_420JPEG#define MAXPIXELS (2800*1152)  /**< Maximum size of final image */typedef struct _parameters {  char *pngformatstr;  uint32_t begin;       /**< the video frame start */  int32_t numframes;   /**< -1 means: take all frames */  y4m_ratio_t framerate;  int interlace;   /**< will the YUV4MPEG stream be interlaced? */  int interleave;  /**< are the PNG frames field-interleaved? */  int verbose; /**< the verbosity of the program (see mjpeg_logging.h) */  png_uint_32 width;  png_uint_32 height;  int ss_mode; /**< subsampling mode (based on ssm_id from subsample.h) */  int new_width; /// new MPEG2 width, in case the original one is uneven} parameters_t;struct _parameters *sh_param; png_structp png_ptr;png_infop info_ptr, end_info;uint8_t *raw0, *raw1, *raw2;  /* buffer for RGB first, and then Y/Cb/Cr planes of decoded PNG *//* * The User Interface parts  *//* usage * Prints a short description of the program, including default values  * in: prog: The name of the program  */static void usage(char *prog){  char *h;    if (NULL != (h = (char *)strrchr(prog,'/')))    prog = h+1;    fprintf(stderr, 	  "usage: %s [ options ]\n"	  "\n"	  "where options are ([] shows the defaults):\n"	  "  -v num        verbosity (0,1,2)                  [1]\n"	  "  -b framenum   starting frame number              [0]\n"	  "  -f framerate  framerate for output stream (fps)     \n"	  "  -n numframes  number of frames to process        [-1 = all]\n"	  "  -j {1}%%{2}d{3} Read PNG frames with the name components as follows:\n"	  "               {1} PNG filename prefix (e g rendered_ )\n"	  "               {2} Counting placeholder (like in C, printf, eg 06 ))\n"	  "  -I x  interlacing mode:  p = none/progressive\n"	  "                           t = top-field-first\n"	  "                           b = bottom-field-first\n"	  "  -L x  interleaving mode:  0 = non-interleaved (two successive\n"	  "                                 fields per PNG file)\n"	  "                            1 = interleaved fields\n"          "  -S mode  chroma subsampling mode [%s]\n"	  "\n"	  "%s pipes a sequence of PNG files to stdout,\n"	  "making the direct encoding of MPEG files possible under mpeg2enc.\n"	  "Any 8bit PNG format supported by libpng can be read.\n"	  "stdout will be filled with the YUV4MPEG movie data stream,\n"	  "so be prepared to pipe it on to mpeg2enc or to write it into a file.\n"	  "\n"	  "\n"	  "examples:\n"	  "  %s -j in_%%06d.png -f 25 -I p -b 100000 > result.yuv\n"	  "  | combines all the available PNGs that match \n"	  "    in_??????.png, starting with 100000 (in_100000.png, \n"	  "    in_100001.png, etc...) into the uncompressed YUV4MPEG videofile result.yuv\n"	  "    The videofile has 25 frames per second and does not use any interlacing.\n"	  "  %s -Ip -L0 -j abc_%%04d.png | mpeg2enc -f3 -o out.m2v\n"	  "  | combines all the available PNGs that match \n"	  "    abc_??????.png, starting with 0000 (abc_0000.png, \n"	  "    abc_0001.png, etc...) and pipes it to mpeg2enc which encodes\n"	  "    an MPEG2-file called out.m2v out of it\n"	  "\n",	  prog, y4m_chroma_keyword(DEFAULT_CHROMA_MODE), prog, prog, prog);}/** parse_commandline * Parses the commandline for the supplied parameters. * in: argc, argv: the classic commandline parameters */static void parse_commandline(int argc, char ** argv, parameters_t *param){  int c;    param->pngformatstr = NULL;  param->begin = 0;  param->numframes = -1;  param->framerate = y4m_fps_UNKNOWN;  param->interlace = Y4M_UNKNOWN;  param->interleave = -1;  param->verbose = 1;  param->ss_mode = DEFAULT_CHROMA_MODE;  //param->mza_filename = NULL;  //param->make_z_alpha = 0;  /* parse options */  for (;;) {    if (-1 == (c = getopt(argc, argv, "I:hv:L:b:j:n:f:z:S:")))      break;    switch (c) {    case 'j':      param->pngformatstr = strdup(optarg);      break;#if 0     case 'z':      param->mza_filename = strdup(optarg);      param->make_z_alpha = 1;      break;#else    case 'z':      mjpeg_error("Z encoding currently unsupported !\n");      exit(-1);      break;#endif    case 'S':      param->ss_mode = y4m_chroma_parse_keyword(optarg);      if (param->ss_mode == Y4M_UNKNOWN) {	mjpeg_error_exit1("Unknown subsampling mode option:  %s", optarg);      } else if (!chroma_sub_implemented(param->ss_mode)) {	mjpeg_error_exit1("Unsupported subsampling mode option:  %s", optarg);      }      break;    case 'b':      param->begin = atol(optarg);      break;    case 'n':      param->numframes = atol(optarg);      break;    case 'f':      param->framerate = mpeg_conform_framerate(atof(optarg));      break;    case 'I':      switch (optarg[0]) {      case 'p':	param->interlace = Y4M_ILACE_NONE;	break;      case 't':	param->interlace = Y4M_ILACE_TOP_FIRST;	break;      case 'b':	param->interlace = Y4M_ILACE_BOTTOM_FIRST;	break;      default:	mjpeg_error_exit1 ("-I option requires arg p, t, or b");      }      break;    case 'L':      param->interleave = atoi(optarg);      if ((param->interleave != 0) &&	  (param->interleave != 1)) 	mjpeg_error_exit1 ("-L option requires arg 0 or 1");      break;    case 'v':      param->verbose = atoi(optarg);      if (param->verbose < 0 || param->verbose > 2) 	mjpeg_error_exit1( "-v option requires arg 0, 1, or 2");          break;         case 'h':    default:      usage(argv[0]);      exit(1);    }  }  if (param->pngformatstr == NULL)     {       mjpeg_error("%s:  input format string not specified. (Use -j option.)",		  argv[0]);       usage(argv[0]);       exit(1);    }  if (Y4M_RATIO_EQL(param->framerate, y4m_fps_UNKNOWN))     {      mjpeg_error("%s:  framerate not specified.  (Use -f option)",		  argv[0]);       usage(argv[0]);       exit(1);    }}void png_separation(png_structp png_ptr, png_row_infop row_info, png_bytep data){  int row_nr = png_ptr->row_number; // internal variable ?   int i, width = row_info->width;   int new_width = sh_param->new_width;  /* contents of row_info:   *  png_uint_32 width      width of row   *  png_uint_32 rowbytes   number of bytes in row   *  png_byte color_type    color type of pixels   *  png_byte bit_depth     bit depth of samples   *  png_byte channels      number of channels (1-4)   *  png_byte pixel_depth   bits per pixel (depth*channels)   */  //mjpeg_debug("PNG YUV transformation callback; color_type is %d row_number %d\n",   //	 row_info->color_type, row_nr);  if(row_info->color_type == PNG_COLOR_TYPE_GRAY) // only Z available    {      //mjpeg_debug("Grayscale to YUV, row %d", row_nr);      for (i = 0; i < width; i++)	{	  raw0[i + row_nr * new_width] = data[i];	  raw1[i + row_nr * new_width] = data[i];	  raw2[i + row_nr * new_width] = data[i];	}      return;    }  if(row_info->color_type == PNG_COLOR_TYPE_RGB) // Z and Alpha available    {      //mjpeg_info("RGB to YUV, row %d", row_nr);      for (i = 0; i < width; i++)	{	  raw0[i + row_nr * new_width] = data[i*3];	  raw1[i + row_nr * new_width] = data[i*3 + 1];	  raw2[i + row_nr * new_width] = data[i*3 + 2];	}      return;    }  mjpeg_error_exit1("mpegz: UNKNOWN COLOR FORMAT %d in PNG transformation !\n", row_info->color_type);}/* * The file handling parts  *//** Reads one PNG file. @param process Process the image data (0 for initial parameter determination)@returns -1 on failure, 1 on sucess*/int decode_png(const char *pngname, int process, parameters_t *param){  int num_pass = 1;  int bit_depth, color_type;  FILE *pngfile;  //png_byte hdptr[8];  /* Now open this PNG file, and examine its header to retrieve the      YUV4MPEG info that shall be written */  pngfile = fopen(pngname, "rb");  if (!pngfile)    {      perror("PNG file open failed:");      return -1;    }  //fread(hdptr, 1, 8, pngfile);#if 0   bool is_png = !png_sig_cmp(hdptr, 0, 8);  if (!is_png)    {      mjpeg_error("%s is _no_ PNG file !\n");      return -1;    }

⌨️ 快捷键说明

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