📄 jpeg2yuv.c
字号:
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <jpeglib.h>
#include "jpegutils.h"
#include "lav_io.h"
#include <sys/types.h>
#include <dirent.h>
#include "mjpeg_logging.h"
#include "mjpeg_types.h"
#include "yuv4mpeg.h"
#include "mpegconsts.h"
#define MAXPIXELS (2048*2048) /* Maximum size of final image */
typedef struct _parameters {
char *jpegformatstr;
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 JPEG frames field-interleaved? */
int verbose; /* the verbosity of the program (see mjpeg_logging.h) */
int width;
int height;
int colorspace;
int loop;
int rescale_YUV;
} parameters_t;
static struct jpeg_decompress_struct dinfo;
static struct jpeg_error_mgr jerr;
/*
* 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"
" -l num loop -1=forever, n >= 1 n-times \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 JPEG frames with the name components as follows:\n"
" {1} JPEG 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 JPEG file)\n"
" 1 = interleaved fields\n"
" -R 1/0 ... 1: rescale YUV color values from 0-255 to 16-235 (default: 1)\n"
"\n"
"%s pipes a sequence of JPEG files to stdout,\n"
"making the direct encoding of MPEG files possible under mpeg2enc.\n"
"Any JPEG format supported by libjpeg 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.jpeg -b 100000 > result.yuv\n"
" | combines all the available JPEGs that match \n"
" in_??????.jpeg, starting with 100000 (in_100000.jpeg, \n"
" in_100001.jpeg, etc...) into the uncompressed YUV4MPEG videofile result.yuv\n"
" %s -It -L0 -j abc_%%04d.jpeg | mpeg2enc -f3 -o out.m2v\n"
" | combines all the available JPEGs that match \n"
" abc_??????.jpeg, starting with 0000 (abc_0000.jpeg, \n"
" abc_0001.jpeg, etc...) and pipes it to mpeg2enc which encodes\n"
" an MPEG2-file called out.m2v out of it\n"
"\n",
prog, 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->jpegformatstr = NULL;
param->begin = 0;
param->numframes = -1;
param->framerate = y4m_fps_UNKNOWN;
param->interlace = Y4M_UNKNOWN;
param->interleave = -1;
param->verbose = 1;
param->loop = 1;
param->rescale_YUV = 1;
/* parse options */
for (;;) {
if (-1 == (c = getopt(argc, argv, "I:hv:L:b:j:n:f:l:R:")))
break;
switch (c) {
case 'j':
param->jpegformatstr = strdup(optarg);
break;
case 'b':
param->begin = atol(optarg);
break;
case 'n':
param->numframes = atol(optarg);
break;
case 'R':
param->rescale_YUV = atoi(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 'l':
param->loop = atoi(optarg);
if (param->loop == 0 || param->loop < -1 )
mjpeg_error_exit1( "-l option requires a number greater than 0 or -1 to loop forever ");
break;
case 'h':
default:
mjpeg_info("Wp x, char %c\n", c);
usage(argv[0]);
exit(1);
}
}
if (param->jpegformatstr == 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);
}
}
/*
* The file handling parts
*/
/** init_parse_files
* Verifies the JPEG input files and prepares YUV4MPEG header information.
* @returns 0 on success
*/
static int init_parse_files(parameters_t *param, char *filename)
{
char jpegname[255];
FILE *jpegfile;
strcpy(jpegname, filename);
mjpeg_info("Parsing file %s", jpegname);
mjpeg_debug("Analyzing %s to get the right pic params", jpegname);
jpegfile = fopen(jpegname, "rb");
if (jpegfile == NULL) {
mjpeg_error_exit1("System error while opening: \"%s\": %s",
jpegname, strerror(errno));
return 1;
}
dinfo.err = jpeg_std_error(&jerr); /* ?????????? */
jpeg_create_decompress(&dinfo);
jpeg_stdio_src(&dinfo, jpegfile);
jpeg_read_header(&dinfo, 1);
switch (dinfo.jpeg_color_space)
{
case JCS_YCbCr:
mjpeg_info("YUV colorspace detected.\n");
dinfo.out_color_space = JCS_YCbCr;
break;
case JCS_GRAYSCALE:
mjpeg_info("Grayscale colorspace detected.\n");
dinfo.out_color_space = JCS_GRAYSCALE;
break;
default:
mjpeg_error("Unsupported colorspace detected.\n"); break;
}
mjpeg_info("Starting decompression");
jpeg_start_decompress(&dinfo);
if (dinfo.output_components != 3 && dinfo.out_color_space == JCS_YCbCr)
mjpeg_error_exit1("Output components of color JPEG image = %d, must be 3.",
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -