2002-01-26 00:52:59 +00:00
|
|
|
/* Straightforward (to be) optimized JPEG encoder for the YUV422 format
|
|
|
|
* based on mjpeg code from ffmpeg.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2002, Rik Snel
|
|
|
|
* Parts from ffmpeg Copyright (c) 2000, 2001 Gerard Lantau
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* For an excellent introduction to the JPEG format, see:
|
2002-02-11 01:24:56 +00:00
|
|
|
* http://www.ece.purdue.edu/~bouman/grad-labs/lab8/pdf/lab.pdf
|
2002-01-26 00:52:59 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "config.h"
|
|
|
|
#ifdef USE_FASTMEMCPY
|
|
|
|
#include "fastmemcpy.h"
|
|
|
|
#endif
|
|
|
|
#include "../mp_msg.h"
|
2002-02-12 23:30:07 +00:00
|
|
|
/* We need this #define because we need ../libavcodec/common.h to #define
|
|
|
|
* be2me_32, otherwise the linker will complain that it doesn't exist */
|
|
|
|
#define HAVE_AV_CONFIG_H
|
2002-02-11 01:24:56 +00:00
|
|
|
#include "../libavcodec/avcodec.h"
|
2002-01-26 00:52:59 +00:00
|
|
|
#include "../libavcodec/dsputil.h"
|
2002-02-11 01:24:56 +00:00
|
|
|
#include "../libavcodec/mpegvideo.h"
|
2002-01-26 00:52:59 +00:00
|
|
|
|
2002-02-11 01:24:56 +00:00
|
|
|
#include "jpeg_enc.h"
|
2002-01-26 00:52:59 +00:00
|
|
|
|
2002-03-29 13:46:34 +00:00
|
|
|
extern int avcodec_inited;
|
|
|
|
|
2002-02-11 01:24:56 +00:00
|
|
|
/* zr_mjpeg_encode_mb needs access to these tables for the black & white
|
|
|
|
* option */
|
|
|
|
typedef struct MJpegContext {
|
|
|
|
UINT8 huff_size_dc_luminance[12];
|
|
|
|
UINT16 huff_code_dc_luminance[12];
|
|
|
|
UINT8 huff_size_dc_chrominance[12];
|
|
|
|
UINT16 huff_code_dc_chrominance[12];
|
2002-01-26 00:52:59 +00:00
|
|
|
|
2002-02-11 01:24:56 +00:00
|
|
|
UINT8 huff_size_ac_luminance[256];
|
|
|
|
UINT16 huff_code_ac_luminance[256];
|
|
|
|
UINT8 huff_size_ac_chrominance[256];
|
|
|
|
UINT16 huff_code_ac_chrominance[256];
|
|
|
|
} MJpegContext;
|
2002-01-26 00:52:59 +00:00
|
|
|
|
|
|
|
|
2002-02-11 01:24:56 +00:00
|
|
|
/* A very important function pointer */
|
|
|
|
extern int (*dct_quantize)(MpegEncContext *s,
|
2002-04-27 18:22:52 +00:00
|
|
|
DCTELEM *block, int n, int qscale, int *overflow);
|
2002-01-26 00:52:59 +00:00
|
|
|
|
|
|
|
|
2002-02-11 01:24:56 +00:00
|
|
|
/* Begin excessive code duplication ************************************/
|
|
|
|
/* Code coming from mpegvideo.c and mjpeg.c in ../libavcodec ***********/
|
2002-01-26 00:52:59 +00:00
|
|
|
|
|
|
|
static const unsigned short aanscales[64] = {
|
|
|
|
/* precomputed values scaled up by 14 bits */
|
|
|
|
16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
|
|
|
|
22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
|
|
|
|
21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
|
|
|
|
19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
|
|
|
|
16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
|
|
|
|
12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
|
|
|
|
8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446,
|
|
|
|
4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247
|
|
|
|
};
|
|
|
|
|
2002-04-27 18:22:52 +00:00
|
|
|
static void convert_matrix(int (*qmat)[64], uint16_t (*qmat16)[64], uint16_t (*qmat16_bias)[64],
|
|
|
|
const UINT16 *quant_matrix, int bias)
|
2002-01-26 00:52:59 +00:00
|
|
|
{
|
2002-04-27 18:22:52 +00:00
|
|
|
int qscale;
|
|
|
|
|
|
|
|
for(qscale=1; qscale<32; qscale++){
|
|
|
|
int i;
|
2002-06-07 18:55:47 +00:00
|
|
|
if (av_fdct == fdct_ifast) {
|
2002-04-27 18:22:52 +00:00
|
|
|
for(i=0;i<64;i++) {
|
|
|
|
const int j= block_permute_op(i);
|
|
|
|
/* 16 <= qscale * quant_matrix[i] <= 7905 */
|
|
|
|
/* 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026 */
|
|
|
|
/* (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */
|
|
|
|
/* 3444240 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */
|
|
|
|
|
|
|
|
qmat[qscale][j] = (int)((UINT64_C(1) << (QMAT_SHIFT + 11)) /
|
|
|
|
(aanscales[i] * qscale * quant_matrix[j]));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for(i=0;i<64;i++) {
|
|
|
|
/* We can safely suppose that 16 <= quant_matrix[i] <= 255
|
|
|
|
So 16 <= qscale * quant_matrix[i] <= 7905
|
|
|
|
so (1<<19) / 16 >= (1<<19) / (qscale * quant_matrix[i]) >= (1<<19) / 7905
|
|
|
|
so 32768 >= (1<<19) / (qscale * quant_matrix[i]) >= 67
|
|
|
|
*/
|
|
|
|
qmat [qscale][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[i]);
|
|
|
|
qmat16[qscale][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[block_permute_op(i)]);
|
|
|
|
|
|
|
|
if(qmat16[qscale][i]==0 || qmat16[qscale][i]==128*256) qmat16[qscale][i]=128*256-1;
|
|
|
|
|
|
|
|
qmat16_bias[qscale][i]= ROUNDED_DIV(bias<<(16-QUANT_BIAS_SHIFT), qmat16[qscale][i]);
|
|
|
|
}
|
2002-01-26 00:52:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-02-11 01:24:56 +00:00
|
|
|
static inline void encode_dc(MpegEncContext *s, int val,
|
|
|
|
UINT8 *huff_size, UINT16 *huff_code)
|
2002-01-26 00:52:59 +00:00
|
|
|
{
|
|
|
|
int mant, nbits;
|
|
|
|
|
|
|
|
if (val == 0) {
|
2002-02-11 01:24:56 +00:00
|
|
|
jput_bits(&s->pb, huff_size[0], huff_code[0]);
|
2002-01-26 00:52:59 +00:00
|
|
|
} else {
|
|
|
|
mant = val;
|
|
|
|
if (val < 0) {
|
|
|
|
val = -val;
|
|
|
|
mant--;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* compute the log (XXX: optimize) */
|
|
|
|
nbits = 0;
|
|
|
|
while (val != 0) {
|
|
|
|
val = val >> 1;
|
|
|
|
nbits++;
|
|
|
|
}
|
|
|
|
|
2002-02-11 01:24:56 +00:00
|
|
|
jput_bits(&s->pb, huff_size[nbits], huff_code[nbits]);
|
2002-01-26 00:52:59 +00:00
|
|
|
|
2002-02-11 01:24:56 +00:00
|
|
|
jput_bits(&s->pb, nbits, mant & ((1 << nbits) - 1));
|
2002-01-26 00:52:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-02-11 01:24:56 +00:00
|
|
|
static void encode_block(MpegEncContext *s, DCTELEM *block, int n)
|
2002-01-26 00:52:59 +00:00
|
|
|
{
|
|
|
|
int mant, nbits, code, i, j;
|
|
|
|
int component, dc, run, last_index, val;
|
2002-02-11 01:24:56 +00:00
|
|
|
MJpegContext *m = s->mjpeg_ctx;
|
|
|
|
UINT8 *huff_size_ac;
|
|
|
|
UINT16 *huff_code_ac;
|
2002-01-26 00:52:59 +00:00
|
|
|
|
|
|
|
/* DC coef */
|
2002-02-11 01:24:56 +00:00
|
|
|
component = (n <= 3 ? 0 : n - 4 + 1);
|
|
|
|
dc = block[0]; /* overflow is impossible */
|
|
|
|
val = dc - s->last_dc[component];
|
|
|
|
if (n < 4) {
|
|
|
|
encode_dc(s, val, m->huff_size_dc_luminance, m->huff_code_dc_luminance);
|
|
|
|
huff_size_ac = m->huff_size_ac_luminance;
|
|
|
|
huff_code_ac = m->huff_code_ac_luminance;
|
|
|
|
} else {
|
|
|
|
encode_dc(s, val, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
|
|
|
|
huff_size_ac = m->huff_size_ac_chrominance;
|
|
|
|
huff_code_ac = m->huff_code_ac_chrominance;
|
|
|
|
}
|
|
|
|
s->last_dc[component] = dc;
|
2002-01-26 00:52:59 +00:00
|
|
|
|
|
|
|
/* AC coefs */
|
|
|
|
|
|
|
|
run = 0;
|
2002-02-11 01:24:56 +00:00
|
|
|
last_index = s->block_last_index[n];
|
2002-01-26 00:52:59 +00:00
|
|
|
for(i=1;i<=last_index;i++) {
|
2002-02-11 01:24:56 +00:00
|
|
|
j = zigzag_direct[i];
|
|
|
|
val = block[j];
|
2002-01-26 00:52:59 +00:00
|
|
|
if (val == 0) {
|
|
|
|
run++;
|
|
|
|
} else {
|
|
|
|
while (run >= 16) {
|
2002-02-11 01:24:56 +00:00
|
|
|
jput_bits(&s->pb, huff_size_ac[0xf0], huff_code_ac[0xf0]);
|
2002-01-26 00:52:59 +00:00
|
|
|
run -= 16;
|
|
|
|
}
|
|
|
|
mant = val;
|
|
|
|
if (val < 0) {
|
|
|
|
val = -val;
|
|
|
|
mant--;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* compute the log (XXX: optimize) */
|
|
|
|
nbits = 0;
|
|
|
|
while (val != 0) {
|
|
|
|
val = val >> 1;
|
|
|
|
nbits++;
|
|
|
|
}
|
|
|
|
code = (run << 4) | nbits;
|
|
|
|
|
2002-02-11 01:24:56 +00:00
|
|
|
jput_bits(&s->pb, huff_size_ac[code], huff_code_ac[code]);
|
2002-01-26 00:52:59 +00:00
|
|
|
|
2002-02-11 01:24:56 +00:00
|
|
|
jput_bits(&s->pb, nbits, mant & ((1 << nbits) - 1));
|
2002-01-26 00:52:59 +00:00
|
|
|
run = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* output EOB only if not already 64 values */
|
2002-02-11 01:24:56 +00:00
|
|
|
if (last_index < 63 || run != 0)
|
|
|
|
jput_bits(&s->pb, huff_size_ac[0], huff_code_ac[0]);
|
2002-01-26 00:52:59 +00:00
|
|
|
}
|
|
|
|
|
2002-04-27 18:22:52 +00:00
|
|
|
static inline void clip_coeffs(MpegEncContext *s, DCTELEM *block, int last_index)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
const int maxlevel= s->max_qcoeff;
|
|
|
|
const int minlevel= s->min_qcoeff;
|
|
|
|
|
|
|
|
for(i=0; i<=last_index; i++){
|
|
|
|
const int j = zigzag_direct[i];
|
|
|
|
int level = block[j];
|
|
|
|
|
|
|
|
if (level>maxlevel) level=maxlevel;
|
|
|
|
else if(level<minlevel) level=minlevel;
|
|
|
|
block[j]= level;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-02-11 01:24:56 +00:00
|
|
|
/* End excessive code duplication **************************************/
|
|
|
|
|
|
|
|
/* this function is a reproduction of the one in mjpeg, it includes two
|
|
|
|
* changes, it allows for black&white encoding (it skips the U and V
|
|
|
|
* macroblocks and it outputs the huffman code for 'no change' (dc) and
|
|
|
|
* 'all zero' (ac)) and it takes 4 macroblocks (422) instead of 6 (420) */
|
|
|
|
static void zr_mjpeg_encode_mb(jpeg_enc_t *j) {
|
|
|
|
|
|
|
|
MJpegContext *m = j->s->mjpeg_ctx;
|
|
|
|
|
|
|
|
encode_block(j->s, j->s->block[0], 0);
|
|
|
|
encode_block(j->s, j->s->block[1], 1);
|
|
|
|
if (j->bw) {
|
|
|
|
/* U */
|
|
|
|
jput_bits(&j->s->pb, m->huff_size_dc_chrominance[0],
|
|
|
|
m->huff_code_dc_chrominance[0]);
|
|
|
|
jput_bits(&j->s->pb, m->huff_size_ac_chrominance[0],
|
|
|
|
m->huff_code_ac_chrominance[0]);
|
|
|
|
/* V */
|
|
|
|
jput_bits(&j->s->pb, m->huff_size_dc_chrominance[0],
|
|
|
|
m->huff_code_dc_chrominance[0]);
|
|
|
|
jput_bits(&j->s->pb, m->huff_size_ac_chrominance[0],
|
|
|
|
m->huff_code_ac_chrominance[0]);
|
|
|
|
} else {
|
|
|
|
/* we trick encode_block here so that it uses
|
|
|
|
* chrominance huffman tables instead of luminance ones
|
|
|
|
* (see the effect of second argument of encode_block) */
|
|
|
|
encode_block(j->s, j->s->block[2], 4);
|
|
|
|
encode_block(j->s, j->s->block[3], 5);
|
|
|
|
}
|
2002-01-26 00:52:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* this function can take all kinds of YUV colorspaces
|
2002-02-11 01:24:56 +00:00
|
|
|
* YV12, YVYU, UYVY. The necesary parameters must be set up by the caller
|
2002-01-26 00:52:59 +00:00
|
|
|
* y_ps means "y pixel size", y_rs means "y row size".
|
2002-02-11 01:24:56 +00:00
|
|
|
* For YUYV, for example, is u_buf = y_buf + 1, v_buf = y_buf + 3,
|
|
|
|
* y_ps = 2, u_ps = 4, v_ps = 4, y_rs = u_rs = v_rs.
|
|
|
|
*
|
|
|
|
* The actual buffers must be passed with mjpeg_encode_frame, this is
|
|
|
|
* to make it possible to call encode on the buffer provided by the
|
|
|
|
* codec in draw_frame.
|
2002-01-26 00:52:59 +00:00
|
|
|
*
|
|
|
|
* The data is straightened out at the moment it is put in DCT
|
|
|
|
* blocks, there are therefore no spurious memcopies involved */
|
2002-02-11 01:24:56 +00:00
|
|
|
/* Notice that w must be a multiple of 16 and h must be a multiple of 8 */
|
2002-01-26 00:52:59 +00:00
|
|
|
/* We produce YUV422 jpegs, the colors must be subsampled horizontally,
|
|
|
|
* if the colors are also subsampled vertically, then this function
|
|
|
|
* performs cheap upsampling (better solution will be: a DCT that is
|
|
|
|
* optimized in the case that every two rows are the same) */
|
|
|
|
/* cu = 0 means 'No cheap upsampling'
|
|
|
|
* cu = 1 means 'perform cheap upsampling' */
|
2002-02-11 01:24:56 +00:00
|
|
|
/* The encoder doesn't know anything about interlacing, the halve height
|
|
|
|
* needs to be passed and the double rowstride. Which field gets encoded
|
|
|
|
* is decided by what buffers are passed to mjpeg_encode_frame */
|
|
|
|
jpeg_enc_t *jpeg_enc_init(int w, int h, int y_psize, int y_rsize,
|
|
|
|
int u_psize, int u_rsize, int v_psize, int v_rsize,
|
|
|
|
int cu, int q, int b) {
|
|
|
|
jpeg_enc_t *j;
|
|
|
|
int i = 0;
|
|
|
|
mp_msg(MSGT_VO, MSGL_V, "JPEnc init: %dx%d %d %d %d %d %d %d\n",
|
|
|
|
w, h, y_psize, y_rsize, u_psize,
|
|
|
|
u_rsize, v_psize, v_rsize);
|
|
|
|
|
|
|
|
j = malloc(sizeof(jpeg_enc_t));
|
|
|
|
if (j == NULL) return NULL;
|
|
|
|
|
|
|
|
j->s = malloc(sizeof(MpegEncContext));
|
|
|
|
if (j->s == NULL) {
|
|
|
|
free(j);
|
|
|
|
return NULL;
|
2002-01-26 00:52:59 +00:00
|
|
|
}
|
2002-02-11 01:24:56 +00:00
|
|
|
|
|
|
|
/* info on how to access the pixels */
|
|
|
|
j->y_ps = y_psize;
|
|
|
|
j->u_ps = u_psize;
|
|
|
|
j->v_ps = v_psize;
|
|
|
|
j->y_rs = y_rsize;
|
|
|
|
j->u_rs = u_rsize;
|
|
|
|
j->v_rs = v_rsize;
|
|
|
|
|
|
|
|
j->s->width = w;
|
|
|
|
j->s->height = h;
|
|
|
|
j->s->qscale = q;
|
|
|
|
|
|
|
|
j->s->out_format = FMT_MJPEG;
|
|
|
|
j->s->intra_only = 1;
|
|
|
|
j->s->encoding = 1;
|
|
|
|
j->s->pict_type = I_TYPE;
|
|
|
|
j->s->y_dc_scale = 8;
|
|
|
|
j->s->c_dc_scale = 8;
|
|
|
|
|
|
|
|
j->s->mjpeg_write_tables = 1;
|
|
|
|
j->s->mjpeg_vsample[0] = 1;
|
|
|
|
j->s->mjpeg_vsample[1] = 1;
|
|
|
|
j->s->mjpeg_vsample[2] = 1;
|
|
|
|
j->s->mjpeg_hsample[0] = 2;
|
|
|
|
j->s->mjpeg_hsample[1] = 1;
|
|
|
|
j->s->mjpeg_hsample[2] = 1;
|
|
|
|
|
|
|
|
j->cheap_upsample = cu;
|
|
|
|
j->bw = b;
|
|
|
|
|
|
|
|
/* if libavcodec is used by the decoder then we must not
|
|
|
|
* initialize again, but if it is not initialized then we must
|
2002-03-29 13:46:34 +00:00
|
|
|
* initialize it here. */
|
|
|
|
if (!avcodec_inited) {
|
2002-02-11 01:24:56 +00:00
|
|
|
/* we need to initialize libavcodec */
|
|
|
|
avcodec_init();
|
2002-03-29 13:46:34 +00:00
|
|
|
avcodec_register_all();
|
|
|
|
avcodec_inited=1;
|
2002-02-11 01:24:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mjpeg_init(j->s) < 0) {
|
|
|
|
free(j->s);
|
|
|
|
free(j);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (MPV_common_init(j->s) < 0) {
|
|
|
|
free(j->s);
|
|
|
|
free(j);
|
|
|
|
return NULL;
|
2002-01-26 00:52:59 +00:00
|
|
|
}
|
2002-02-11 01:24:56 +00:00
|
|
|
|
|
|
|
/* correct the value for sc->mb_height */
|
|
|
|
j->s->mb_height = j->s->height/8;
|
|
|
|
j->s->mb_intra = 1;
|
|
|
|
|
2002-07-11 13:05:53 +00:00
|
|
|
j->s->intra_matrix[0] = ff_mpeg1_default_intra_matrix[0];
|
2002-02-11 01:24:56 +00:00
|
|
|
for (i = 1; i < 64; i++)
|
|
|
|
j->s->intra_matrix[i] =
|
2002-07-11 13:05:53 +00:00
|
|
|
(ff_mpeg1_default_intra_matrix[i]*j->s->qscale) >> 3;
|
2002-04-27 18:22:52 +00:00
|
|
|
convert_matrix(j->s->q_intra_matrix, j->s->q_intra_matrix16,
|
|
|
|
j->s->q_intra_matrix16_bias,
|
|
|
|
j->s->intra_matrix, j->s->intra_quant_bias);
|
2002-02-11 01:24:56 +00:00
|
|
|
return j;
|
2002-01-26 00:52:59 +00:00
|
|
|
}
|
|
|
|
|
2002-02-11 01:24:56 +00:00
|
|
|
int jpeg_enc_frame(jpeg_enc_t *j, unsigned char *y_data,
|
|
|
|
unsigned char *u_data, unsigned char *v_data, char *bufr) {
|
2002-04-27 18:22:52 +00:00
|
|
|
int i, k, mb_x, mb_y, overflow;
|
2002-01-26 00:52:59 +00:00
|
|
|
short int *dest;
|
|
|
|
unsigned char *source;
|
|
|
|
/* initialize the buffer */
|
|
|
|
|
2002-02-11 01:24:56 +00:00
|
|
|
init_put_bits(&j->s->pb, bufr, 1024*256, NULL, NULL);
|
2002-01-26 00:52:59 +00:00
|
|
|
|
2002-02-11 01:24:56 +00:00
|
|
|
mjpeg_picture_header(j->s);
|
|
|
|
|
|
|
|
j->s->last_dc[0] = 128;
|
|
|
|
j->s->last_dc[1] = 128;
|
|
|
|
j->s->last_dc[2] = 128;
|
|
|
|
|
|
|
|
for (mb_y = 0; mb_y < j->s->mb_height; mb_y++) {
|
|
|
|
for (mb_x = 0; mb_x < j->s->mb_width; mb_x++) {
|
|
|
|
/* conversion 8 to 16 bit and filling of blocks
|
|
|
|
* must be mmx optimized */
|
2002-01-26 00:52:59 +00:00
|
|
|
/* fill 2 Y macroblocks and one U and one V */
|
2002-02-11 01:24:56 +00:00
|
|
|
source = mb_y * 8 * j->y_rs +
|
|
|
|
16 * j->y_ps * mb_x + y_data;
|
|
|
|
dest = j->s->block[0];
|
2002-01-26 00:52:59 +00:00
|
|
|
for (i = 0; i < 8; i++) {
|
2002-02-11 01:24:56 +00:00
|
|
|
for (k = 0; k < 8; k++) {
|
|
|
|
dest[k] = source[k*j->y_ps];
|
2002-01-26 00:52:59 +00:00
|
|
|
}
|
|
|
|
dest += 8;
|
2002-02-11 01:24:56 +00:00
|
|
|
source += j->y_rs;
|
2002-01-26 00:52:59 +00:00
|
|
|
}
|
2002-02-11 01:24:56 +00:00
|
|
|
source = mb_y * 8 * j->y_rs +
|
|
|
|
(16*mb_x + 8)*j->y_ps + y_data;
|
|
|
|
dest = j->s->block[1];
|
2002-01-26 00:52:59 +00:00
|
|
|
for (i = 0; i < 8; i++) {
|
2002-02-11 01:24:56 +00:00
|
|
|
for (k = 0; k < 8; k++) {
|
|
|
|
dest[k] = source[k*j->y_ps];
|
2002-01-26 00:52:59 +00:00
|
|
|
}
|
|
|
|
dest += 8;
|
2002-02-11 01:24:56 +00:00
|
|
|
source += j->y_rs;
|
2002-01-26 00:52:59 +00:00
|
|
|
}
|
2002-02-11 01:24:56 +00:00
|
|
|
if (!j->bw && j->cheap_upsample) {
|
|
|
|
source = mb_y*4*j->u_rs +
|
|
|
|
8*mb_x*j->u_ps + u_data;
|
|
|
|
dest = j->s->block[2];
|
2002-01-26 00:52:59 +00:00
|
|
|
for (i = 0; i < 4; i++) {
|
2002-02-11 01:24:56 +00:00
|
|
|
for (k = 0; k < 8; k++) {
|
|
|
|
dest[k] = source[k*j->u_ps];
|
|
|
|
dest[k+8] = source[k*j->u_ps];
|
2002-01-26 00:52:59 +00:00
|
|
|
}
|
|
|
|
dest += 16;
|
2002-02-11 01:24:56 +00:00
|
|
|
source += j->u_rs;
|
2002-01-26 00:52:59 +00:00
|
|
|
}
|
2002-02-11 01:24:56 +00:00
|
|
|
source = mb_y*4*j->v_rs +
|
|
|
|
8*mb_x*j->v_ps + v_data;
|
|
|
|
dest = j->s->block[3];
|
2002-01-26 00:52:59 +00:00
|
|
|
for (i = 0; i < 4; i++) {
|
2002-02-11 01:24:56 +00:00
|
|
|
for (k = 0; k < 8; k++) {
|
|
|
|
dest[k] = source[k*j->v_ps];
|
|
|
|
dest[k+8] = source[k*j->v_ps];
|
2002-01-26 00:52:59 +00:00
|
|
|
}
|
|
|
|
dest += 16;
|
2002-02-11 01:24:56 +00:00
|
|
|
source += j->u_rs;
|
2002-01-26 00:52:59 +00:00
|
|
|
}
|
2002-02-11 01:24:56 +00:00
|
|
|
} else if (!j->bw && !j->cheap_upsample) {
|
|
|
|
source = mb_y*8*j->u_rs +
|
|
|
|
8*mb_x*j->u_ps + u_data;
|
|
|
|
dest = j->s->block[2];
|
2002-01-26 00:52:59 +00:00
|
|
|
for (i = 0; i < 8; i++) {
|
2002-02-11 01:24:56 +00:00
|
|
|
for (k = 0; k < 8; k++)
|
|
|
|
dest[k] = source[k*j->u_ps];
|
2002-01-26 00:52:59 +00:00
|
|
|
dest += 8;
|
2002-02-11 01:24:56 +00:00
|
|
|
source += j->u_rs;
|
2002-01-26 00:52:59 +00:00
|
|
|
}
|
2002-02-11 01:24:56 +00:00
|
|
|
source = mb_y*8*j->v_rs +
|
|
|
|
8*mb_x*j->v_ps + v_data;
|
|
|
|
dest = j->s->block[3];
|
2002-01-26 00:52:59 +00:00
|
|
|
for (i = 0; i < 8; i++) {
|
2002-02-11 01:24:56 +00:00
|
|
|
for (k = 0; k < 8; k++)
|
|
|
|
dest[k] = source[k*j->v_ps];
|
2002-01-26 00:52:59 +00:00
|
|
|
dest += 8;
|
2002-02-11 01:24:56 +00:00
|
|
|
source += j->u_rs;
|
2002-01-26 00:52:59 +00:00
|
|
|
}
|
|
|
|
}
|
2002-02-11 01:24:56 +00:00
|
|
|
emms_c(); /* is this really needed? */
|
|
|
|
|
|
|
|
j->s->block_last_index[0] =
|
|
|
|
dct_quantize(j->s, j->s->block[0],
|
2002-04-27 18:22:52 +00:00
|
|
|
0, 8, &overflow);
|
|
|
|
if (overflow) clip_coeffs(j->s, j->s->block[0],
|
|
|
|
j->s->block_last_index[0]);
|
2002-02-11 01:24:56 +00:00
|
|
|
j->s->block_last_index[1] =
|
|
|
|
dct_quantize(j->s, j->s->block[1],
|
2002-04-27 18:22:52 +00:00
|
|
|
1, 8, &overflow);
|
|
|
|
if (overflow) clip_coeffs(j->s, j->s->block[1],
|
|
|
|
j->s->block_last_index[1]);
|
2002-02-11 01:24:56 +00:00
|
|
|
|
|
|
|
if (!j->bw) {
|
|
|
|
j->s->block_last_index[4] =
|
|
|
|
dct_quantize(j->s, j->s->block[2],
|
2002-04-27 18:22:52 +00:00
|
|
|
4, 8, &overflow);
|
|
|
|
if (overflow) clip_coeffs(j->s, j->s->block[2],
|
|
|
|
j->s->block_last_index[2]);
|
2002-02-11 01:24:56 +00:00
|
|
|
j->s->block_last_index[5] =
|
|
|
|
dct_quantize(j->s, j->s->block[3],
|
2002-04-27 18:22:52 +00:00
|
|
|
5, 8, &overflow);
|
|
|
|
if (overflow) clip_coeffs(j->s, j->s->block[3],
|
|
|
|
j->s->block_last_index[3]);
|
2002-01-26 00:52:59 +00:00
|
|
|
}
|
2002-02-11 01:24:56 +00:00
|
|
|
zr_mjpeg_encode_mb(j);
|
2002-01-26 00:52:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
emms_c();
|
2002-02-11 01:24:56 +00:00
|
|
|
mjpeg_picture_trailer(j->s);
|
|
|
|
flush_put_bits(&j->s->pb);
|
|
|
|
|
|
|
|
if (j->s->mjpeg_write_tables == 1)
|
|
|
|
j->s->mjpeg_write_tables = 0;
|
|
|
|
|
2002-02-12 23:30:07 +00:00
|
|
|
return pbBufPtr(&(j->s->pb)) - j->s->pb.buf;
|
2002-02-11 01:24:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void jpeg_enc_uninit(jpeg_enc_t *j) {
|
|
|
|
mjpeg_close(j->s);
|
|
|
|
free(j->s);
|
|
|
|
free(j);
|
2002-01-26 00:52:59 +00:00
|
|
|
}
|
|
|
|
|
2002-02-11 01:24:56 +00:00
|
|
|
#if 0
|
|
|
|
|
|
|
|
#define W 32
|
|
|
|
#define H 32
|
|
|
|
|
|
|
|
int quant_store[MBR+1][MBC+1];
|
|
|
|
unsigned char buf[W*H*3/2];
|
|
|
|
char code[256*1024];
|
|
|
|
|
|
|
|
|
|
|
|
main() {
|
|
|
|
int i, size;
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
memset(buf, 0, W*H);
|
|
|
|
memset(buf+W*H, 255, W*H/4);
|
|
|
|
memset(buf+5*W*H/4, 0, W*H/4);
|
|
|
|
mjpeg_encoder_init(W, H, 1, W, 1, W/2, 1, W/2, 1, 1, 0);
|
|
|
|
|
|
|
|
size = mjpeg_encode_frame(buf, buf+W*H, buf+5*W*H/4, code);
|
|
|
|
fp = fopen("test.jpg", "w");
|
|
|
|
fwrite(code, 1, size, fp);
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
#endif
|