📄 conv-mjpeg.c
字号:
line = in->data + in->fmt.width*in->fmt.height; for (i = 0; i < h->mjpg_cinfo.image_height; i+=2, line += in->fmt.width) h->mjpg_ptrs[1][i/2] = line; line = in->data + in->fmt.width*in->fmt.height*3/2; for (i = 0; i < h->mjpg_cinfo.image_height; i+=2, line += in->fmt.width) h->mjpg_ptrs[2][i/2] = line; mjpg_420_compress(h); out->size = h->mjpg_bufused;}static voidmjpg_420_420_compress(void *handle, struct ng_video_buf *out, struct ng_video_buf *in){ struct mjpeg_compress *h = handle; unsigned char *line; unsigned int i; if (ng_debug > 1) fprintf(stderr,"mjpg_420_420_compress\n"); h->mjpg_buffer = out->data; h->mjpg_bufsize = out->size; line = in->data; for (i = 0; i < h->mjpg_cinfo.image_height; i++, line += in->fmt.width) h->mjpg_ptrs[0][i] = line; line = in->data + in->fmt.width*in->fmt.height; for (i = 0; i < h->mjpg_cinfo.image_height; i+=2, line += in->fmt.width/2) h->mjpg_ptrs[1][i/2] = line; line = in->data + in->fmt.width*in->fmt.height*5/4; for (i = 0; i < h->mjpg_cinfo.image_height; i+=2, line += in->fmt.width/2) h->mjpg_ptrs[2][i/2] = line; mjpg_420_compress(h); out->size = h->mjpg_bufused;}/* ---------------------------------------------------------------------- */static voidmjpg_422_422_compress(void *handle, struct ng_video_buf *out, struct ng_video_buf *in){ struct mjpeg_compress *h = handle; unsigned char *line; unsigned int i; if (ng_debug > 1) fprintf(stderr,"mjpg_422_422_compress\n"); h->mjpg_buffer = out->data; h->mjpg_bufsize = out->size; line = in->data; for (i = 0; i < h->mjpg_cinfo.image_height; i++, line += in->fmt.width) h->mjpg_ptrs[0][i] = line; line = in->data + in->fmt.width*in->fmt.height; for (i = 0; i < h->mjpg_cinfo.image_height; i++, line += in->fmt.width/2) h->mjpg_ptrs[1][i] = line; line = in->data + in->fmt.width*in->fmt.height*3/2; for (i = 0; i < h->mjpg_cinfo.image_height; i++, line += in->fmt.width/2) h->mjpg_ptrs[2][i] = line; mjpg_422_compress(h); out->size = h->mjpg_bufused;}/* ---------------------------------------------------------------------- *//* decompress */static void*mjpg_de_init(struct ng_video_fmt *fmt, void *priv){ struct mjpeg_decompress *h; h = malloc(sizeof(*h)); if (NULL == h) return NULL; memset(h,0,sizeof(*h)); h->fmt = *fmt; h->mjpg_cinfo.err = jpeg_std_error(&h->mjpg_jerr); jpeg_create_decompress(&h->mjpg_cinfo); h->mjpg_src.init_source = mjpg_src_init; h->mjpg_src.fill_input_buffer = mjpg_src_fill; h->mjpg_src.skip_input_data = mjpg_src_skip; h->mjpg_src.resync_to_restart = jpeg_resync_to_restart; h->mjpg_src.term_source = mjpg_src_term; h->mjpg_cinfo.src = &h->mjpg_src; switch (h->fmt.fmtid) { case VIDEO_YUV420P: h->mjpg_ptrs[0] = malloc(h->fmt.height*sizeof(char*)); h->mjpg_ptrs[1] = malloc(h->fmt.height*sizeof(char*)); h->mjpg_ptrs[2] = malloc(h->fmt.height*sizeof(char*)); break; } return h;}static voidmjpg_rgb_decompress(void *handle, struct ng_video_buf *out, struct ng_video_buf *in){ struct mjpeg_decompress *h = handle; unsigned char *line; unsigned int i; if (ng_debug > 1) fprintf(stderr,"mjpg_rgb_decompress\n"); h->buf = in; jpeg_read_header(&h->mjpg_cinfo,1); h->mjpg_cinfo.out_color_space = JCS_RGB; jpeg_start_decompress(&h->mjpg_cinfo); for (i = 0, line = out->data; i < out->fmt.height; i++, line += out->fmt.bytesperline) { jpeg_read_scanlines(&h->mjpg_cinfo, &line, 1); } jpeg_finish_decompress(&h->mjpg_cinfo);}static voidmjpg_yuv420_decompress(void *handle, struct ng_video_buf *out, struct ng_video_buf *in){ struct mjpeg_decompress *h = handle; unsigned char **mjpg_run[3]; unsigned char *line; unsigned int i,y; if (ng_debug > 1) fprintf(stderr,"mjpg_yuv_decompress\n"); h->buf = in; jpeg_read_header(&h->mjpg_cinfo,1); h->mjpg_cinfo.raw_data_out = 1; if (ng_debug > 1) fprintf(stderr,"yuv: %dx%d - %d %d / %d %d / %d %d\n", h->mjpg_cinfo.image_width, h->mjpg_cinfo.image_height, h->mjpg_cinfo.comp_info[0].h_samp_factor, h->mjpg_cinfo.comp_info[0].v_samp_factor, h->mjpg_cinfo.comp_info[1].h_samp_factor, h->mjpg_cinfo.comp_info[1].v_samp_factor, h->mjpg_cinfo.comp_info[2].h_samp_factor, h->mjpg_cinfo.comp_info[2].v_samp_factor); jpeg_start_decompress(&h->mjpg_cinfo); mjpg_run[0] = h->mjpg_ptrs[0]; mjpg_run[1] = h->mjpg_ptrs[1]; mjpg_run[2] = h->mjpg_ptrs[2]; line = out->data; for (i = 0; i < h->mjpg_cinfo.image_height; i++, line += out->fmt.width) h->mjpg_ptrs[0][i] = line; if (2 == h->mjpg_cinfo.comp_info[0].v_samp_factor) { /* file has 420 -- all fine */ line = out->data + out->fmt.width*out->fmt.height; for (i = 0; i < out->fmt.height; i+=2, line += out->fmt.width/2) h->mjpg_ptrs[1][i/2] = line; line = out->data + out->fmt.width*out->fmt.height*5/4; for (i = 0; i < out->fmt.height; i+=2, line += out->fmt.width/2) h->mjpg_ptrs[2][i/2] = line; for (y = 0; y < out->fmt.height; y += 2*DCTSIZE) { jpeg_read_raw_data(&h->mjpg_cinfo, mjpg_run,2*DCTSIZE); mjpg_run[0] += 2*DCTSIZE; mjpg_run[1] += DCTSIZE; mjpg_run[2] += DCTSIZE; } } else { /* file has 422 -- drop lines */ line = out->data + out->fmt.width*out->fmt.height; for (i = 0; i < out->fmt.height; i+=2, line += out->fmt.width/2) { h->mjpg_ptrs[1][i+0] = line; h->mjpg_ptrs[1][i+1] = line; } line = out->data + out->fmt.width*out->fmt.height*5/4; for (i = 0; i < out->fmt.height; i+=2, line += out->fmt.width/2) { h->mjpg_ptrs[2][i+0] = line; h->mjpg_ptrs[2][i+1] = line; } for (y = 0; y < h->mjpg_cinfo.image_height; y += DCTSIZE) { jpeg_read_raw_data(&h->mjpg_cinfo, mjpg_run,DCTSIZE); mjpg_run[0] += DCTSIZE; mjpg_run[1] += DCTSIZE; mjpg_run[2] += DCTSIZE; } } jpeg_finish_decompress(&h->mjpg_cinfo);}static voidmjpg_de_cleanup(void *handle){ struct mjpeg_decompress *h = handle; if (ng_debug > 1) fprintf(stderr,"mjpg_de_cleanup\n"); jpeg_destroy_decompress(&h->mjpg_cinfo); if (h->mjpg_ptrs[0]) free(h->mjpg_ptrs[0]); if (h->mjpg_ptrs[1]) free(h->mjpg_ptrs[1]); if (h->mjpg_ptrs[2]) free(h->mjpg_ptrs[2]); free(h);}/* ---------------------------------------------------------------------- *//* static data + register */static struct mjpeg_yuv_priv priv_420 = { luma_h: 2, luma_v: 2,};static struct mjpeg_yuv_priv priv_422 = { luma_h: 2, luma_v: 1,};static struct ng_video_conv mjpg_list[] = { { /* --- compress --- */ init: mjpg_yuv_init, frame: mjpg_420_420_compress, fini: mjpg_cleanup, fmtid_in: VIDEO_YUV420P, fmtid_out: VIDEO_JPEG, priv: &priv_420, },{ init: mjpg_yuv_init, frame: mjpg_422_420_compress, fini: mjpg_cleanup, fmtid_in: VIDEO_YUV422P, fmtid_out: VIDEO_JPEG, priv: &priv_420, },{ init: mjpg_rgb_init, frame: mjpg_rgb_compress, fini: mjpg_cleanup, fmtid_in: VIDEO_RGB24, fmtid_out: VIDEO_JPEG, },{ init: mjpg_rgb_init, frame: mjpg_bgr_compress, fini: mjpg_cleanup, fmtid_in: VIDEO_BGR24, fmtid_out: VIDEO_JPEG, },{ init: mjpg_yuv_init, frame: mjpg_422_422_compress, fini: mjpg_cleanup, fmtid_in: VIDEO_YUV422P, fmtid_out: VIDEO_MJPEG, priv: &priv_422, },{ /* --- uncompress --- */ init: mjpg_de_init, frame: mjpg_rgb_decompress, fini: mjpg_de_cleanup, fmtid_in: VIDEO_MJPEG, fmtid_out: VIDEO_RGB24, },{ init: mjpg_de_init, frame: mjpg_rgb_decompress, fini: mjpg_de_cleanup, fmtid_in: VIDEO_JPEG, fmtid_out: VIDEO_RGB24, },{ init: mjpg_de_init, frame: mjpg_yuv420_decompress, fini: mjpg_de_cleanup, fmtid_in: VIDEO_MJPEG, fmtid_out: VIDEO_YUV420P, },{ init: mjpg_de_init, frame: mjpg_yuv420_decompress, fini: mjpg_de_cleanup, fmtid_in: VIDEO_JPEG, fmtid_out: VIDEO_YUV420P, }};static const int nconv = sizeof(mjpg_list)/sizeof(struct ng_video_conv);extern void ng_plugin_init(void);void ng_plugin_init(void){ ng_conv_register(NG_PLUGIN_MAGIC,__FILE__,mjpg_list,nconv);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -