jpegutils.c

来自「Motion JPEG编解码器源代码」· C语言 代码 · 共 1,145 行 · 第 1/3 页

C
1,145
字号
/* *  jpegutils.c: Some Utility programs for dealing with *               JPEG encoded images * *  Copyright (C) 1999 Rainer Johanni <Rainer@Johanni.de> *  Copyright (C) 2001 pHilipp Zabel  <pzabel@gmx.de> * *  based on jdatasrc.c and jdatadst.c from the Independent *  JPEG Group's software by Thomas G. Lane * *  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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. */#ifdef HAVE_CONFIG_H#include "config.h"#endif#include <stdio.h>#include <string.h>#include <setjmp.h>#include <jpeglib.h>#include <jerror.h>#include <assert.h>#include "mjpeg_logging.h"#include "jpegutils.h"#include "lav_io.h" /* * jpeg_data:       buffer with input / output jpeg * len:             Length of jpeg buffer * itype:           0: Not interlaced *                  1: Interlaced, Top field first *                  2: Interlaced, Bottom field first * ctype            Chroma format for decompression. *                  Currently only CHROMA420 and CHROMA422 are available * raw0             buffer with input / output raw Y channel * raw1             buffer with input / output raw U/Cb channel * raw2             buffer with input / output raw V/Cr channel * width            width of Y channel (width of U/V is width/2) * height           height of Y channel (height of U/V is height/2) */static void jpeg_buffer_src  (j_decompress_ptr cinfo, unsigned char *buffer,                       long num);static void jpeg_buffer_dest (j_compress_ptr cinfo,   unsigned char *buffer,                       long len);static void jpeg_skip_ff (j_decompress_ptr cinfo);/******************************************************************* *                                                                 * *    The following routines define a JPEG Source manager which    * *    just reads from a given buffer (instead of a file as in      * *    the jpeg library)                                            * *                                                                 * *******************************************************************//* * Initialize source --- called by jpeg_read_header * before any data is actually read. */static void init_source (j_decompress_ptr cinfo){   /* no work necessary here */}/* * Fill the input buffer --- called whenever buffer is emptied. * * Should never be called since all data should be allready provided. * Is nevertheless sometimes called - sets the input buffer to data * which is the JPEG EOI marker; * */static uint8_t EOI_data[2] = { 0xFF, 0xD9 };static boolean fill_input_buffer (j_decompress_ptr cinfo){   cinfo->src->next_input_byte = EOI_data;   cinfo->src->bytes_in_buffer = 2;   return TRUE;}/* * Skip data --- used to skip over a potentially large amount of * uninteresting data (such as an APPn marker). * */static void skip_input_data (j_decompress_ptr cinfo, long num_bytes){   if (num_bytes > 0) {      if (num_bytes > (long) cinfo->src->bytes_in_buffer)         num_bytes = (long) cinfo->src->bytes_in_buffer;      cinfo->src->next_input_byte += (size_t) num_bytes;      cinfo->src->bytes_in_buffer -= (size_t) num_bytes;   }}/* * Terminate source --- called by jpeg_finish_decompress * after all data has been read.  Often a no-op. */static void term_source (j_decompress_ptr cinfo){   /* no work necessary here */}/* * Prepare for input from a data buffer. */static voidjpeg_buffer_src (j_decompress_ptr cinfo, unsigned char *buffer, long num){   /* The source object and input buffer are made permanent so that a series    * of JPEG images can be read from the same buffer by calling jpeg_buffer_src    * only before the first one.  (If we discarded the buffer at the end of    * one image, we'd likely lose the start of the next one.)    * This makes it unsafe to use this manager and a different source    * manager serially with the same JPEG object.  Caveat programmer.    */   if (cinfo->src == NULL) {    /* first time for this JPEG object? */      cinfo->src = (struct jpeg_source_mgr *)          (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,                                      sizeof (struct jpeg_source_mgr));   }   cinfo->src->init_source = init_source;   cinfo->src->fill_input_buffer = fill_input_buffer;   cinfo->src->skip_input_data = skip_input_data;   cinfo->src->resync_to_restart = jpeg_resync_to_restart;	/* use default method */   cinfo->src->term_source = term_source;   cinfo->src->bytes_in_buffer = num;   cinfo->src->next_input_byte = (JOCTET *) buffer;}/* * jpeg_skip_ff is not a part of the source manager but it is * particularly useful when reading several images from the same buffer: * It should be called to skip padding 0xff bytes beetween images. */static void jpeg_skip_ff (j_decompress_ptr cinfo){   while (cinfo->src->bytes_in_buffer > 1          && cinfo->src->next_input_byte[0] == 0xff          && cinfo->src->next_input_byte[1] == 0xff) {      cinfo->src->bytes_in_buffer--;      cinfo->src->next_input_byte++;   }}/******************************************************************* *                                                                 * *    The following routines define a JPEG Destination manager     * *    which just reads from a given buffer (instead of a file      * *    as in the jpeg library)                                      * *                                                                 * *******************************************************************//* * Initialize destination --- called by jpeg_start_compress * before any data is actually written. */static void init_destination (j_compress_ptr cinfo){   /* No work necessary here */}/* * Empty the output buffer --- called whenever buffer fills up. * * Should never be called since all data should be written to the buffer. * If it gets called, the given jpeg buffer was too small. * */static boolean empty_output_buffer (j_compress_ptr cinfo){   /*FIXME: */   mjpeg_error( "Given jpeg buffer was too small!");   ERREXIT (cinfo, JERR_BUFFER_SIZE);	/* shouldn't be FILE_WRITE but BUFFER_OVERRUN! */   return TRUE;}/* * Terminate destination --- called by jpeg_finish_compress * after all data has been written.  Usually needs to flush buffer. * * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding * application must deal with any cleanup that should happen even * for error exit. */static void term_destination (j_compress_ptr cinfo){   /* no work necessary here */}/* * Prepare for output to a stdio stream. * The caller must have already opened the stream, and is responsible * for closing it after finishing compression. */static voidjpeg_buffer_dest (j_compress_ptr cinfo, unsigned char *buf, long len){   /* The destination object is made permanent so that multiple JPEG images    * can be written to the same file without re-executing jpeg_stdio_dest.    * This makes it dangerous to use this manager and a different destination    * manager serially with the same JPEG object, because their private object    * sizes may be different.  Caveat programmer.    */   if (cinfo->dest == NULL) {   /* first time for this JPEG object? */      cinfo->dest = (struct jpeg_destination_mgr *)          (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,                                      sizeof (struct                                              jpeg_destination_mgr));   }   cinfo->dest->init_destination = init_destination;   cinfo->dest->empty_output_buffer = empty_output_buffer;   cinfo->dest->term_destination = term_destination;   cinfo->dest->free_in_buffer = len;   cinfo->dest->next_output_byte = (JOCTET *) buf;}/******************************************************************* *                                                                 * *    decode_jpeg_data: Decode a (possibly interlaced) JPEG frame  * *                                                                 * *******************************************************************//* * ERROR HANDLING: * *    We want in all cases to return to the user. *    The following kind of error handling is from the *    example.c file in the Independent JPEG Group's JPEG software */struct my_error_mgr {   struct jpeg_error_mgr pub;   /* "public" fields */   jmp_buf setjmp_buffer;       /* for return to caller */};static void my_error_exit (j_common_ptr cinfo){   /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */   struct my_error_mgr *myerr = (struct my_error_mgr *) cinfo->err;   /* Always display the message. */   /* We could postpone this until after returning, if we chose. */   (*cinfo->err->output_message) (cinfo);   /* Return control to the setjmp point */   longjmp (myerr->setjmp_buffer, 1);}#define MAX_LUMA_WIDTH   4096#define MAX_CHROMA_WIDTH 2048static unsigned char buf0[16][MAX_LUMA_WIDTH];static unsigned char buf1[8][MAX_CHROMA_WIDTH];static unsigned char buf2[8][MAX_CHROMA_WIDTH];static unsigned char chr1[8][MAX_CHROMA_WIDTH];static unsigned char chr2[8][MAX_CHROMA_WIDTH];#if 1  /* generation of 'std' Huffman tables... */static void add_huff_table (j_decompress_ptr dinfo,			    JHUFF_TBL **htblptr, 			    const UINT8 *bits, const UINT8 *val)/* Define a Huffman table */{  int nsymbols, len;  if (*htblptr == NULL)    *htblptr = jpeg_alloc_huff_table((j_common_ptr) dinfo);  /* Copy the number-of-symbols-of-each-code-length counts */  memcpy((*htblptr)->bits, bits, sizeof((*htblptr)->bits));  /* Validate the counts.  We do this here mainly so we can copy the right   * number of symbols from the val[] array, without risking marching off   * the end of memory.  jchuff.c will do a more thorough test later.   */  nsymbols = 0;  for (len = 1; len <= 16; len++)    nsymbols += bits[len];  if (nsymbols < 1 || nsymbols > 256)    mjpeg_error_exit1("jpegutils.c:  add_huff_table failed badly. ");  memcpy((*htblptr)->huffval, val, nsymbols * sizeof(UINT8));}static void std_huff_tables (j_decompress_ptr dinfo)/* Set up the standard Huffman tables (cf. JPEG standard section K.3) *//* IMPORTANT: these are only valid for 8-bit data precision! */{  static const UINT8 bits_dc_luminance[17] =    { /* 0-base */ 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 };  static const UINT8 val_dc_luminance[] =    { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };    static const UINT8 bits_dc_chrominance[17] =    { /* 0-base */ 0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 };  static const UINT8 val_dc_chrominance[] =    { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };    static const UINT8 bits_ac_luminance[17] =    { /* 0-base */ 0, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d };  static const UINT8 val_ac_luminance[] =    { 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,      0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,      0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,      0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,      0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,      0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,      0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,      0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,      0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,      0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,      0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,      0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,      0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,      0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,      0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,      0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,      0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,      0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,      0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,      0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,      0xf9, 0xfa };    static const UINT8 bits_ac_chrominance[17] =    { /* 0-base */ 0, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77 };  static const UINT8 val_ac_chrominance[] =    { 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,      0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,      0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,      0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,      0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,      0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,

⌨️ 快捷键说明

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