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"
38 #include "libavcodec/mjpegenc.h"
42 extern int avcodec_inited
;
45 /* Begin excessive code duplication ************************************/
46 /* Code coming from mpegvideo.c and mjpeg.c in ../libavcodec ***********/
48 static const unsigned short aanscales
[64] = {
49 /* precomputed values scaled up by 14 bits */
50 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
51 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
52 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
53 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
54 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
55 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
56 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446,
57 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247
60 static void convert_matrix(MpegEncContext
*s
, int (*qmat
)[64],
61 uint16_t (*qmat16
)[2][64], const uint16_t *quant_matrix
,
62 int bias
, int qmin
, int qmax
)
66 for(qscale
=qmin
; qscale
<=qmax
; qscale
++){
68 if (s
->dsp
.fdct
== ff_jpeg_fdct_islow
) {
69 for (i
= 0; i
< 64; i
++) {
70 const int j
= s
->dsp
.idct_permutation
[i
];
71 /* 16 <= qscale * quant_matrix[i] <= 7905
72 * 19952 <= aanscales[i] * \
73 * qscale * quant_matrix[i] <= 205026
74 * (1<<36)/19952 >= (1<<36)/(aanscales[i] * \
75 * qscale * quant_matrix[i]) >= (1<<36)/249205025
76 * 3444240 >= (1<<36)/(aanscales[i] *
77 * qscale * quant_matrix[i]) >= 275 */
78 qmat
[qscale
][i
] = (int)((UINT64_C(1) << (QMAT_SHIFT
-3))/
79 (qscale
* quant_matrix
[j
]));
81 } else if (s
->dsp
.fdct
== fdct_ifast
) {
83 const int j
= s
->dsp
.idct_permutation
[i
];
84 /* 16 <= qscale * quant_matrix[i] <= 7905 */
85 /* 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026 */
86 /* (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */
87 /* 3444240 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */
89 qmat
[qscale
][i
] = (int)((UINT64_C(1) << (QMAT_SHIFT
+ 11)) /
90 (aanscales
[i
] * qscale
* quant_matrix
[j
]));
94 const int j
= s
->dsp
.idct_permutation
[i
];
95 /* We can safely suppose that 16 <= quant_matrix[i] <= 255
96 So 16 <= qscale * quant_matrix[i] <= 7905
97 so (1<<19) / 16 >= (1<<19) / (qscale * quant_matrix[i]) >= (1<<19) / 7905
98 so 32768 >= (1<<19) / (qscale * quant_matrix[i]) >= 67
100 qmat
[qscale
][i
] = (int)((UINT64_C(1) << QMAT_SHIFT_MMX
) / (qscale
* quant_matrix
[j
]));
101 qmat16
[qscale
][0][i
] = (1 << QMAT_SHIFT_MMX
) / (qscale
* quant_matrix
[j
]);
103 if(qmat16
[qscale
][0][i
]==0 || qmat16
[qscale
][0][i
]==128*256) qmat16
[qscale
][0][i
]=128*256-1;
104 qmat16
[qscale
][1][i
]= ROUNDED_DIV(bias
<<(16-QUANT_BIAS_SHIFT
), qmat16
[qscale
][0][i
]);
110 static inline void encode_dc(MpegEncContext
*s
, int val
,
111 uint8_t *huff_size
, uint16_t *huff_code
)
116 put_bits(&s
->pb
, huff_size
[0], huff_code
[0]);
124 /* compute the log (XXX: optimize) */
131 put_bits(&s
->pb
, huff_size
[nbits
], huff_code
[nbits
]);
133 put_bits(&s
->pb
, nbits
, mant
& ((1 << nbits
) - 1));
137 static void encode_block(MpegEncContext
*s
, DCTELEM
*block
, int n
)
139 int mant
, nbits
, code
, i
, j
;
140 int component
, dc
, run
, last_index
, val
;
141 MJpegContext
*m
= s
->mjpeg_ctx
;
142 uint8_t *huff_size_ac
;
143 uint16_t *huff_code_ac
;
146 component
= (n
<= 3 ? 0 : n
- 4 + 1);
147 dc
= block
[0]; /* overflow is impossible */
148 val
= dc
- s
->last_dc
[component
];
150 encode_dc(s
, val
, m
->huff_size_dc_luminance
, m
->huff_code_dc_luminance
);
151 huff_size_ac
= m
->huff_size_ac_luminance
;
152 huff_code_ac
= m
->huff_code_ac_luminance
;
154 encode_dc(s
, val
, m
->huff_size_dc_chrominance
, m
->huff_code_dc_chrominance
);
155 huff_size_ac
= m
->huff_size_ac_chrominance
;
156 huff_code_ac
= m
->huff_code_ac_chrominance
;
158 s
->last_dc
[component
] = dc
;
163 last_index
= s
->block_last_index
[n
];
164 for(i
=1;i
<=last_index
;i
++) {
165 j
= s
->intra_scantable
.permutated
[i
];
171 put_bits(&s
->pb
, huff_size_ac
[0xf0], huff_code_ac
[0xf0]);
180 /* compute the log (XXX: optimize) */
186 code
= (run
<< 4) | nbits
;
188 put_bits(&s
->pb
, huff_size_ac
[code
], huff_code_ac
[code
]);
190 put_bits(&s
->pb
, nbits
, mant
& ((1 << nbits
) - 1));
195 /* output EOB only if not already 64 values */
196 if (last_index
< 63 || run
!= 0)
197 put_bits(&s
->pb
, huff_size_ac
[0], huff_code_ac
[0]);
200 static inline void clip_coeffs(MpegEncContext
*s
, DCTELEM
*block
, int last_index
)
203 const int maxlevel
= s
->max_qcoeff
;
204 const int minlevel
= s
->min_qcoeff
;
206 for(i
=0; i
<=last_index
; i
++){
207 const int j
= s
->intra_scantable
.permutated
[i
];
208 int level
= block
[j
];
210 if (level
>maxlevel
) level
=maxlevel
;
211 else if(level
<minlevel
) level
=minlevel
;
216 /* End excessive code duplication **************************************/
218 /* this function is a reproduction of the one in mjpeg, it includes two
219 * changes, it allows for black&white encoding (it skips the U and V
220 * macroblocks and it outputs the huffman code for 'no change' (dc) and
221 * 'all zero' (ac)) and it takes 4 macroblocks (422) instead of 6 (420) */
222 static void zr_mjpeg_encode_mb(jpeg_enc_t
*j
) {
224 MJpegContext
*m
= j
->s
->mjpeg_ctx
;
226 encode_block(j
->s
, j
->s
->block
[0], 0);
227 encode_block(j
->s
, j
->s
->block
[1], 1);
230 put_bits(&j
->s
->pb
, m
->huff_size_dc_chrominance
[0],
231 m
->huff_code_dc_chrominance
[0]);
232 put_bits(&j
->s
->pb
, m
->huff_size_ac_chrominance
[0],
233 m
->huff_code_ac_chrominance
[0]);
235 put_bits(&j
->s
->pb
, m
->huff_size_dc_chrominance
[0],
236 m
->huff_code_dc_chrominance
[0]);
237 put_bits(&j
->s
->pb
, m
->huff_size_ac_chrominance
[0],
238 m
->huff_code_ac_chrominance
[0]);
240 /* we trick encode_block here so that it uses
241 * chrominance huffman tables instead of luminance ones
242 * (see the effect of second argument of encode_block) */
243 encode_block(j
->s
, j
->s
->block
[2], 4);
244 encode_block(j
->s
, j
->s
->block
[3], 5);
248 /* this function can take all kinds of YUV colorspaces
249 * YV12, YVYU, UYVY. The necesary parameters must be set up by the caller
250 * y_ps means "y pixel size", y_rs means "y row size".
251 * For YUYV, for example, is u_buf = y_buf + 1, v_buf = y_buf + 3,
252 * y_ps = 2, u_ps = 4, v_ps = 4, y_rs = u_rs = v_rs.
254 * The actual buffers must be passed with mjpeg_encode_frame, this is
255 * to make it possible to call encode on the buffer provided by the
256 * codec in draw_frame.
258 * The data is straightened out at the moment it is put in DCT
259 * blocks, there are therefore no spurious memcopies involved */
260 /* Notice that w must be a multiple of 16 and h must be a multiple of 8 */
261 /* We produce YUV422 jpegs, the colors must be subsampled horizontally,
262 * if the colors are also subsampled vertically, then this function
263 * performs cheap upsampling (better solution will be: a DCT that is
264 * optimized in the case that every two rows are the same) */
265 /* cu = 0 means 'No cheap upsampling'
266 * cu = 1 means 'perform cheap upsampling' */
267 /* The encoder doesn't know anything about interlacing, the halve height
268 * needs to be passed and the double rowstride. Which field gets encoded
269 * is decided by what buffers are passed to mjpeg_encode_frame */
270 jpeg_enc_t
*jpeg_enc_init(int w
, int h
, int y_psize
, int y_rsize
,
271 int u_psize
, int u_rsize
, int v_psize
, int v_rsize
,
272 int cu
, int q
, int b
) {
275 mp_msg(MSGT_VO
, MSGL_V
, "JPEnc init: %dx%d %d %d %d %d %d %d\n",
276 w
, h
, y_psize
, y_rsize
, u_psize
,
277 u_rsize
, v_psize
, v_rsize
);
279 j
= av_malloc(sizeof(jpeg_enc_t
));
280 if (j
== NULL
) return NULL
;
282 j
->s
= av_malloc(sizeof(MpegEncContext
));
283 memset(j
->s
,0x00,sizeof(MpegEncContext
));
289 /* info on how to access the pixels */
301 j
->s
->out_format
= FMT_MJPEG
;
302 j
->s
->intra_only
= 1;
304 j
->s
->pict_type
= I_TYPE
;
305 j
->s
->y_dc_scale
= 8;
306 j
->s
->c_dc_scale
= 8;
308 //FIXME j->s->mjpeg_write_tables = 1;
309 j
->s
->mjpeg_vsample
[0] = 1;
310 j
->s
->mjpeg_vsample
[1] = 1;
311 j
->s
->mjpeg_vsample
[2] = 1;
312 j
->s
->mjpeg_hsample
[0] = 2;
313 j
->s
->mjpeg_hsample
[1] = 1;
314 j
->s
->mjpeg_hsample
[2] = 1;
316 j
->cheap_upsample
= cu
;
319 /* if libavcodec is used by the decoder then we must not
320 * initialize again, but if it is not initialized then we must
321 * initialize it here. */
322 if (!avcodec_inited
) {
323 /* we need to initialize libavcodec */
325 avcodec_register_all();
329 if (ff_mjpeg_encode_init(j
->s
) < 0) {
335 /* alloc bogus avctx to keep MPV_common_init from segfaulting */
336 j
->s
->avctx
= calloc(sizeof(*j
->s
->avctx
), 1);
337 /* Set up to encode mjpeg */
338 j
->s
->avctx
->codec_id
= CODEC_ID_MJPEG
;
340 /* make MPV_common_init allocate important buffers, like s->block */
341 j
->s
->avctx
->thread_count
= 1;
343 if (MPV_common_init(j
->s
) < 0) {
349 /* correct the value for sc->mb_height */
350 j
->s
->mb_height
= j
->s
->height
/8;
353 j
->s
->intra_matrix
[0] = ff_mpeg1_default_intra_matrix
[0];
354 for (i
= 1; i
< 64; i
++)
355 j
->s
->intra_matrix
[i
] = av_clip_uint8(
356 (ff_mpeg1_default_intra_matrix
[i
]*j
->s
->qscale
) >> 3);
357 convert_matrix(j
->s
, j
->s
->q_intra_matrix
, j
->s
->q_intra_matrix16
,
358 j
->s
->intra_matrix
, j
->s
->intra_quant_bias
, 8, 8);
362 int jpeg_enc_frame(jpeg_enc_t
*j
, unsigned char *y_data
,
363 unsigned char *u_data
, unsigned char *v_data
, char *bufr
) {
364 int i
, k
, mb_x
, mb_y
, overflow
;
366 unsigned char *source
;
367 /* initialize the buffer */
369 init_put_bits(&j
->s
->pb
, bufr
, 1024*256);
371 ff_mjpeg_encode_picture_header(j
->s
);
373 j
->s
->header_bits
= put_bits_count(&j
->s
->pb
);
375 j
->s
->last_dc
[0] = 128;
376 j
->s
->last_dc
[1] = 128;
377 j
->s
->last_dc
[2] = 128;
379 for (mb_y
= 0; mb_y
< j
->s
->mb_height
; mb_y
++) {
380 for (mb_x
= 0; mb_x
< j
->s
->mb_width
; mb_x
++) {
381 /* conversion 8 to 16 bit and filling of blocks
382 * must be mmx optimized */
383 /* fill 2 Y macroblocks and one U and one V */
384 source
= mb_y
* 8 * j
->y_rs
+
385 16 * j
->y_ps
* mb_x
+ y_data
;
386 dest
= j
->s
->block
[0];
387 for (i
= 0; i
< 8; i
++) {
388 for (k
= 0; k
< 8; k
++) {
389 dest
[k
] = source
[k
*j
->y_ps
];
394 source
= mb_y
* 8 * j
->y_rs
+
395 (16*mb_x
+ 8)*j
->y_ps
+ y_data
;
396 dest
= j
->s
->block
[1];
397 for (i
= 0; i
< 8; i
++) {
398 for (k
= 0; k
< 8; k
++) {
399 dest
[k
] = source
[k
*j
->y_ps
];
404 if (!j
->bw
&& j
->cheap_upsample
) {
405 source
= mb_y
*4*j
->u_rs
+
406 8*mb_x
*j
->u_ps
+ u_data
;
407 dest
= j
->s
->block
[2];
408 for (i
= 0; i
< 4; i
++) {
409 for (k
= 0; k
< 8; k
++) {
410 dest
[k
] = source
[k
*j
->u_ps
];
411 dest
[k
+8] = source
[k
*j
->u_ps
];
416 source
= mb_y
*4*j
->v_rs
+
417 8*mb_x
*j
->v_ps
+ v_data
;
418 dest
= j
->s
->block
[3];
419 for (i
= 0; i
< 4; i
++) {
420 for (k
= 0; k
< 8; k
++) {
421 dest
[k
] = source
[k
*j
->v_ps
];
422 dest
[k
+8] = source
[k
*j
->v_ps
];
427 } else if (!j
->bw
&& !j
->cheap_upsample
) {
428 source
= mb_y
*8*j
->u_rs
+
429 8*mb_x
*j
->u_ps
+ u_data
;
430 dest
= j
->s
->block
[2];
431 for (i
= 0; i
< 8; i
++) {
432 for (k
= 0; k
< 8; k
++)
433 dest
[k
] = source
[k
*j
->u_ps
];
437 source
= mb_y
*8*j
->v_rs
+
438 8*mb_x
*j
->v_ps
+ v_data
;
439 dest
= j
->s
->block
[3];
440 for (i
= 0; i
< 8; i
++) {
441 for (k
= 0; k
< 8; k
++)
442 dest
[k
] = source
[k
*j
->v_ps
];
447 emms_c(); /* is this really needed? */
449 j
->s
->block_last_index
[0] =
450 j
->s
->dct_quantize(j
->s
, j
->s
->block
[0],
452 if (overflow
) clip_coeffs(j
->s
, j
->s
->block
[0],
453 j
->s
->block_last_index
[0]);
454 j
->s
->block_last_index
[1] =
455 j
->s
->dct_quantize(j
->s
, j
->s
->block
[1],
457 if (overflow
) clip_coeffs(j
->s
, j
->s
->block
[1],
458 j
->s
->block_last_index
[1]);
461 j
->s
->block_last_index
[4] =
462 j
->s
->dct_quantize(j
->s
, j
->s
->block
[2],
464 if (overflow
) clip_coeffs(j
->s
, j
->s
->block
[2],
465 j
->s
->block_last_index
[2]);
466 j
->s
->block_last_index
[5] =
467 j
->s
->dct_quantize(j
->s
, j
->s
->block
[3],
469 if (overflow
) clip_coeffs(j
->s
, j
->s
->block
[3],
470 j
->s
->block_last_index
[3]);
472 zr_mjpeg_encode_mb(j
);
476 ff_mjpeg_encode_picture_trailer(j
->s
);
477 flush_put_bits(&j
->s
->pb
);
480 //if (j->s->mjpeg_write_tables == 1)
481 // j->s->mjpeg_write_tables = 0;
483 return pbBufPtr(&(j
->s
->pb
)) - j
->s
->pb
.buf
;
486 void jpeg_enc_uninit(jpeg_enc_t
*j
) {
487 ff_mjpeg_encode_close(j
->s
);
497 int quant_store
[MBR
+1][MBC
+1];
498 unsigned char buf
[W
*H
*3/2];
507 memset(buf
+W
*H
, 255, W
*H
/4);
508 memset(buf
+5*W
*H
/4, 0, W
*H
/4);
509 mjpeg_encoder_init(W
, H
, 1, W
, 1, W
/2, 1, W
/2, 1, 1, 0);
511 size
= mjpeg_encode_frame(buf
, buf
+W
*H
, buf
+5*W
*H
/4, code
);
512 fp
= fopen("test.jpg", "w");
513 fwrite(code
, 1, size
, fp
);