1 /* Straightforward (to be) optimized JPEG encoder for the YUV422 format
2 * based on mjpeg code from ffmpeg.
4 * Copyright (c) 2002, Rik Snel
5 * Parts from ffmpeg Copyright (c) 2000-2002 Fabrice Bellard
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 * For an excellent introduction to the JPEG format, see:
22 * http://www.ece.purdue.edu/~bouman/grad-labs/lab8/pdf/lab.pdf
27 #include <sys/types.h>
32 /* We need this #define because we need ../libavcodec/common.h to #define
33 * be2me_32, otherwise the linker will complain that it doesn't exist */
34 #define HAVE_AV_CONFIG_H
35 #include "libavcodec/avcodec.h"
36 #include "libavcodec/dsputil.h"
37 #include "libavcodec/mpegvideo.h"
41 extern int avcodec_inited
;
43 /* zr_mjpeg_encode_mb needs access to these tables for the black & white
45 typedef struct MJpegContext
{
46 uint8_t huff_size_dc_luminance
[12];
47 uint16_t huff_code_dc_luminance
[12];
48 uint8_t huff_size_dc_chrominance
[12];
49 uint16_t huff_code_dc_chrominance
[12];
51 uint8_t huff_size_ac_luminance
[256];
52 uint16_t huff_code_ac_luminance
[256];
53 uint8_t huff_size_ac_chrominance
[256];
54 uint16_t huff_code_ac_chrominance
[256];
58 /* Begin excessive code duplication ************************************/
59 /* Code coming from mpegvideo.c and mjpeg.c in ../libavcodec ***********/
61 static const unsigned short aanscales
[64] = {
62 /* precomputed values scaled up by 14 bits */
63 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
64 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
65 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
66 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
67 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
68 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
69 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446,
70 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247
73 static void convert_matrix(MpegEncContext
*s
, int (*qmat
)[64],
74 uint16_t (*qmat16
)[2][64], const uint16_t *quant_matrix
,
75 int bias
, int qmin
, int qmax
)
79 for(qscale
=qmin
; qscale
<=qmax
; qscale
++){
81 if (s
->dsp
.fdct
== ff_jpeg_fdct_islow
) {
82 for (i
= 0; i
< 64; i
++) {
83 const int j
= s
->dsp
.idct_permutation
[i
];
84 /* 16 <= qscale * quant_matrix[i] <= 7905
85 * 19952 <= aanscales[i] * \
86 * qscale * quant_matrix[i] <= 205026
87 * (1<<36)/19952 >= (1<<36)/(aanscales[i] * \
88 * qscale * quant_matrix[i]) >= (1<<36)/249205025
89 * 3444240 >= (1<<36)/(aanscales[i] *
90 * qscale * quant_matrix[i]) >= 275 */
91 qmat
[qscale
][i
] = (int)((UINT64_C(1) << (QMAT_SHIFT
-3))/
92 (qscale
* quant_matrix
[j
]));
94 } else if (s
->dsp
.fdct
== fdct_ifast
) {
96 const int j
= s
->dsp
.idct_permutation
[i
];
97 /* 16 <= qscale * quant_matrix[i] <= 7905 */
98 /* 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026 */
99 /* (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */
100 /* 3444240 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */
102 qmat
[qscale
][i
] = (int)((UINT64_C(1) << (QMAT_SHIFT
+ 11)) /
103 (aanscales
[i
] * qscale
* quant_matrix
[j
]));
107 const int j
= s
->dsp
.idct_permutation
[i
];
108 /* We can safely suppose that 16 <= quant_matrix[i] <= 255
109 So 16 <= qscale * quant_matrix[i] <= 7905
110 so (1<<19) / 16 >= (1<<19) / (qscale * quant_matrix[i]) >= (1<<19) / 7905
111 so 32768 >= (1<<19) / (qscale * quant_matrix[i]) >= 67
113 qmat
[qscale
][i
] = (int)((UINT64_C(1) << QMAT_SHIFT_MMX
) / (qscale
* quant_matrix
[j
]));
114 qmat16
[qscale
][0][i
] = (1 << QMAT_SHIFT_MMX
) / (qscale
* quant_matrix
[j
]);
116 if(qmat16
[qscale
][0][i
]==0 || qmat16
[qscale
][0][i
]==128*256) qmat16
[qscale
][0][i
]=128*256-1;
117 qmat16
[qscale
][1][i
]= ROUNDED_DIV(bias
<<(16-QUANT_BIAS_SHIFT
), qmat16
[qscale
][0][i
]);
123 static inline void encode_dc(MpegEncContext
*s
, int val
,
124 uint8_t *huff_size
, uint16_t *huff_code
)
129 put_bits(&s
->pb
, huff_size
[0], huff_code
[0]);
137 /* compute the log (XXX: optimize) */
144 put_bits(&s
->pb
, huff_size
[nbits
], huff_code
[nbits
]);
146 put_bits(&s
->pb
, nbits
, mant
& ((1 << nbits
) - 1));
150 static void encode_block(MpegEncContext
*s
, DCTELEM
*block
, int n
)
152 int mant
, nbits
, code
, i
, j
;
153 int component
, dc
, run
, last_index
, val
;
154 MJpegContext
*m
= s
->mjpeg_ctx
;
155 uint8_t *huff_size_ac
;
156 uint16_t *huff_code_ac
;
159 component
= (n
<= 3 ? 0 : n
- 4 + 1);
160 dc
= block
[0]; /* overflow is impossible */
161 val
= dc
- s
->last_dc
[component
];
163 encode_dc(s
, val
, m
->huff_size_dc_luminance
, m
->huff_code_dc_luminance
);
164 huff_size_ac
= m
->huff_size_ac_luminance
;
165 huff_code_ac
= m
->huff_code_ac_luminance
;
167 encode_dc(s
, val
, m
->huff_size_dc_chrominance
, m
->huff_code_dc_chrominance
);
168 huff_size_ac
= m
->huff_size_ac_chrominance
;
169 huff_code_ac
= m
->huff_code_ac_chrominance
;
171 s
->last_dc
[component
] = dc
;
176 last_index
= s
->block_last_index
[n
];
177 for(i
=1;i
<=last_index
;i
++) {
178 j
= s
->intra_scantable
.permutated
[i
];
184 put_bits(&s
->pb
, huff_size_ac
[0xf0], huff_code_ac
[0xf0]);
193 /* compute the log (XXX: optimize) */
199 code
= (run
<< 4) | nbits
;
201 put_bits(&s
->pb
, huff_size_ac
[code
], huff_code_ac
[code
]);
203 put_bits(&s
->pb
, nbits
, mant
& ((1 << nbits
) - 1));
208 /* output EOB only if not already 64 values */
209 if (last_index
< 63 || run
!= 0)
210 put_bits(&s
->pb
, huff_size_ac
[0], huff_code_ac
[0]);
213 static inline void clip_coeffs(MpegEncContext
*s
, DCTELEM
*block
, int last_index
)
216 const int maxlevel
= s
->max_qcoeff
;
217 const int minlevel
= s
->min_qcoeff
;
219 for(i
=0; i
<=last_index
; i
++){
220 const int j
= s
->intra_scantable
.permutated
[i
];
221 int level
= block
[j
];
223 if (level
>maxlevel
) level
=maxlevel
;
224 else if(level
<minlevel
) level
=minlevel
;
229 /* End excessive code duplication **************************************/
231 /* this function is a reproduction of the one in mjpeg, it includes two
232 * changes, it allows for black&white encoding (it skips the U and V
233 * macroblocks and it outputs the huffman code for 'no change' (dc) and
234 * 'all zero' (ac)) and it takes 4 macroblocks (422) instead of 6 (420) */
235 static void zr_mjpeg_encode_mb(jpeg_enc_t
*j
) {
237 MJpegContext
*m
= j
->s
->mjpeg_ctx
;
239 encode_block(j
->s
, j
->s
->block
[0], 0);
240 encode_block(j
->s
, j
->s
->block
[1], 1);
243 put_bits(&j
->s
->pb
, m
->huff_size_dc_chrominance
[0],
244 m
->huff_code_dc_chrominance
[0]);
245 put_bits(&j
->s
->pb
, m
->huff_size_ac_chrominance
[0],
246 m
->huff_code_ac_chrominance
[0]);
248 put_bits(&j
->s
->pb
, m
->huff_size_dc_chrominance
[0],
249 m
->huff_code_dc_chrominance
[0]);
250 put_bits(&j
->s
->pb
, m
->huff_size_ac_chrominance
[0],
251 m
->huff_code_ac_chrominance
[0]);
253 /* we trick encode_block here so that it uses
254 * chrominance huffman tables instead of luminance ones
255 * (see the effect of second argument of encode_block) */
256 encode_block(j
->s
, j
->s
->block
[2], 4);
257 encode_block(j
->s
, j
->s
->block
[3], 5);
261 /* this function can take all kinds of YUV colorspaces
262 * YV12, YVYU, UYVY. The necesary parameters must be set up by the caller
263 * y_ps means "y pixel size", y_rs means "y row size".
264 * For YUYV, for example, is u_buf = y_buf + 1, v_buf = y_buf + 3,
265 * y_ps = 2, u_ps = 4, v_ps = 4, y_rs = u_rs = v_rs.
267 * The actual buffers must be passed with mjpeg_encode_frame, this is
268 * to make it possible to call encode on the buffer provided by the
269 * codec in draw_frame.
271 * The data is straightened out at the moment it is put in DCT
272 * blocks, there are therefore no spurious memcopies involved */
273 /* Notice that w must be a multiple of 16 and h must be a multiple of 8 */
274 /* We produce YUV422 jpegs, the colors must be subsampled horizontally,
275 * if the colors are also subsampled vertically, then this function
276 * performs cheap upsampling (better solution will be: a DCT that is
277 * optimized in the case that every two rows are the same) */
278 /* cu = 0 means 'No cheap upsampling'
279 * cu = 1 means 'perform cheap upsampling' */
280 /* The encoder doesn't know anything about interlacing, the halve height
281 * needs to be passed and the double rowstride. Which field gets encoded
282 * is decided by what buffers are passed to mjpeg_encode_frame */
283 jpeg_enc_t
*jpeg_enc_init(int w
, int h
, int y_psize
, int y_rsize
,
284 int u_psize
, int u_rsize
, int v_psize
, int v_rsize
,
285 int cu
, int q
, int b
) {
288 mp_msg(MSGT_VO
, MSGL_V
, "JPEnc init: %dx%d %d %d %d %d %d %d\n",
289 w
, h
, y_psize
, y_rsize
, u_psize
,
290 u_rsize
, v_psize
, v_rsize
);
292 j
= av_malloc(sizeof(jpeg_enc_t
));
293 if (j
== NULL
) return NULL
;
295 j
->s
= av_malloc(sizeof(MpegEncContext
));
296 memset(j
->s
,0x00,sizeof(MpegEncContext
));
302 /* info on how to access the pixels */
314 j
->s
->out_format
= FMT_MJPEG
;
315 j
->s
->intra_only
= 1;
317 j
->s
->pict_type
= I_TYPE
;
318 j
->s
->y_dc_scale
= 8;
319 j
->s
->c_dc_scale
= 8;
321 //FIXME j->s->mjpeg_write_tables = 1;
322 j
->s
->mjpeg_vsample
[0] = 1;
323 j
->s
->mjpeg_vsample
[1] = 1;
324 j
->s
->mjpeg_vsample
[2] = 1;
325 j
->s
->mjpeg_hsample
[0] = 2;
326 j
->s
->mjpeg_hsample
[1] = 1;
327 j
->s
->mjpeg_hsample
[2] = 1;
329 j
->cheap_upsample
= cu
;
332 /* if libavcodec is used by the decoder then we must not
333 * initialize again, but if it is not initialized then we must
334 * initialize it here. */
335 if (!avcodec_inited
) {
336 /* we need to initialize libavcodec */
338 avcodec_register_all();
342 if (ff_mjpeg_encode_init(j
->s
) < 0) {
348 /* alloc bogus avctx to keep MPV_common_init from segfaulting */
349 j
->s
->avctx
= calloc(sizeof(*j
->s
->avctx
), 1);
350 /* Set up to encode mjpeg */
351 j
->s
->avctx
->codec_id
= CODEC_ID_MJPEG
;
353 /* make MPV_common_init allocate important buffers, like s->block */
354 j
->s
->avctx
->thread_count
= 1;
356 if (MPV_common_init(j
->s
) < 0) {
362 /* correct the value for sc->mb_height */
363 j
->s
->mb_height
= j
->s
->height
/8;
366 j
->s
->intra_matrix
[0] = ff_mpeg1_default_intra_matrix
[0];
367 for (i
= 1; i
< 64; i
++)
368 j
->s
->intra_matrix
[i
] = av_clip_uint8(
369 (ff_mpeg1_default_intra_matrix
[i
]*j
->s
->qscale
) >> 3);
370 convert_matrix(j
->s
, j
->s
->q_intra_matrix
, j
->s
->q_intra_matrix16
,
371 j
->s
->intra_matrix
, j
->s
->intra_quant_bias
, 8, 8);
375 int jpeg_enc_frame(jpeg_enc_t
*j
, unsigned char *y_data
,
376 unsigned char *u_data
, unsigned char *v_data
, char *bufr
) {
377 int i
, k
, mb_x
, mb_y
, overflow
;
379 unsigned char *source
;
380 /* initialize the buffer */
382 init_put_bits(&j
->s
->pb
, bufr
, 1024*256);
384 ff_mjpeg_encode_picture_header(j
->s
);
386 j
->s
->header_bits
= put_bits_count(&j
->s
->pb
);
388 j
->s
->last_dc
[0] = 128;
389 j
->s
->last_dc
[1] = 128;
390 j
->s
->last_dc
[2] = 128;
392 for (mb_y
= 0; mb_y
< j
->s
->mb_height
; mb_y
++) {
393 for (mb_x
= 0; mb_x
< j
->s
->mb_width
; mb_x
++) {
394 /* conversion 8 to 16 bit and filling of blocks
395 * must be mmx optimized */
396 /* fill 2 Y macroblocks and one U and one V */
397 source
= mb_y
* 8 * j
->y_rs
+
398 16 * j
->y_ps
* mb_x
+ y_data
;
399 dest
= j
->s
->block
[0];
400 for (i
= 0; i
< 8; i
++) {
401 for (k
= 0; k
< 8; k
++) {
402 dest
[k
] = source
[k
*j
->y_ps
];
407 source
= mb_y
* 8 * j
->y_rs
+
408 (16*mb_x
+ 8)*j
->y_ps
+ y_data
;
409 dest
= j
->s
->block
[1];
410 for (i
= 0; i
< 8; i
++) {
411 for (k
= 0; k
< 8; k
++) {
412 dest
[k
] = source
[k
*j
->y_ps
];
417 if (!j
->bw
&& j
->cheap_upsample
) {
418 source
= mb_y
*4*j
->u_rs
+
419 8*mb_x
*j
->u_ps
+ u_data
;
420 dest
= j
->s
->block
[2];
421 for (i
= 0; i
< 4; i
++) {
422 for (k
= 0; k
< 8; k
++) {
423 dest
[k
] = source
[k
*j
->u_ps
];
424 dest
[k
+8] = source
[k
*j
->u_ps
];
429 source
= mb_y
*4*j
->v_rs
+
430 8*mb_x
*j
->v_ps
+ v_data
;
431 dest
= j
->s
->block
[3];
432 for (i
= 0; i
< 4; i
++) {
433 for (k
= 0; k
< 8; k
++) {
434 dest
[k
] = source
[k
*j
->v_ps
];
435 dest
[k
+8] = source
[k
*j
->v_ps
];
440 } else if (!j
->bw
&& !j
->cheap_upsample
) {
441 source
= mb_y
*8*j
->u_rs
+
442 8*mb_x
*j
->u_ps
+ u_data
;
443 dest
= j
->s
->block
[2];
444 for (i
= 0; i
< 8; i
++) {
445 for (k
= 0; k
< 8; k
++)
446 dest
[k
] = source
[k
*j
->u_ps
];
450 source
= mb_y
*8*j
->v_rs
+
451 8*mb_x
*j
->v_ps
+ v_data
;
452 dest
= j
->s
->block
[3];
453 for (i
= 0; i
< 8; i
++) {
454 for (k
= 0; k
< 8; k
++)
455 dest
[k
] = source
[k
*j
->v_ps
];
460 emms_c(); /* is this really needed? */
462 j
->s
->block_last_index
[0] =
463 j
->s
->dct_quantize(j
->s
, j
->s
->block
[0],
465 if (overflow
) clip_coeffs(j
->s
, j
->s
->block
[0],
466 j
->s
->block_last_index
[0]);
467 j
->s
->block_last_index
[1] =
468 j
->s
->dct_quantize(j
->s
, j
->s
->block
[1],
470 if (overflow
) clip_coeffs(j
->s
, j
->s
->block
[1],
471 j
->s
->block_last_index
[1]);
474 j
->s
->block_last_index
[4] =
475 j
->s
->dct_quantize(j
->s
, j
->s
->block
[2],
477 if (overflow
) clip_coeffs(j
->s
, j
->s
->block
[2],
478 j
->s
->block_last_index
[2]);
479 j
->s
->block_last_index
[5] =
480 j
->s
->dct_quantize(j
->s
, j
->s
->block
[3],
482 if (overflow
) clip_coeffs(j
->s
, j
->s
->block
[3],
483 j
->s
->block_last_index
[3]);
485 zr_mjpeg_encode_mb(j
);
489 ff_mjpeg_encode_picture_trailer(j
->s
);
490 flush_put_bits(&j
->s
->pb
);
493 //if (j->s->mjpeg_write_tables == 1)
494 // j->s->mjpeg_write_tables = 0;
496 return pbBufPtr(&(j
->s
->pb
)) - j
->s
->pb
.buf
;
499 void jpeg_enc_uninit(jpeg_enc_t
*j
) {
500 ff_mjpeg_encode_close(j
->s
);
510 int quant_store
[MBR
+1][MBC
+1];
511 unsigned char buf
[W
*H
*3/2];
520 memset(buf
+W
*H
, 255, W
*H
/4);
521 memset(buf
+5*W
*H
/4, 0, W
*H
/4);
522 mjpeg_encoder_init(W
, H
, 1, W
, 1, W
/2, 1, W
/2, 1, 1, 0);
524 size
= mjpeg_encode_frame(buf
, buf
+W
*H
, buf
+5*W
*H
/4, code
);
525 fp
= fopen("test.jpg", "w");
526 fwrite(code
, 1, size
, fp
);