2 * This files includes a straightforward (to be) optimized JPEG encoder for
3 * the YUV422 format, based on mjpeg code from ffmpeg.
5 * For an excellent introduction to the JPEG format, see:
6 * http://www.ece.purdue.edu/~bouman/grad-labs/lab8/pdf/lab.pdf
8 * Copyright (C) 2005 Rik Snel <rsnel@cube.dyndns.org>
9 * - based on vd_lavc.c by A'rpi (C) 2002-2003
10 * - parts from ffmpeg Copyright (c) 2000-2003 Fabrice Bellard
12 * This file is part of MPlayer.
14 * MPlayer is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * MPlayer is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License along
25 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
26 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
32 * \brief Does mjpeg encoding as required by the zrmjpeg filter as well
33 * as by the zr video driver.
44 #include "img_format.h"
46 #include "vd_ffmpeg.h"
49 /* We need this #define because we need ../libavcodec/common.h to #define
50 * be2me_32, otherwise the linker will complain that it doesn't exist */
51 #define HAVE_AV_CONFIG_H
52 #include "libavcodec/avcodec.h"
53 #include "libavcodec/mjpegenc.h"
58 /* some convenient #define's, is this portable enough? */
59 /// Printout with vf_zrmjpeg: prefix at VERBOSE level
60 #define VERBOSE(...) mp_msg(MSGT_DECVIDEO, MSGL_V, "vf_zrmjpeg: " __VA_ARGS__)
61 /// Printout with vf_zrmjpeg: prefix at ERROR level
62 #define ERROR(...) mp_msg(MSGT_DECVIDEO, MSGL_ERR, "vf_zrmjpeg: " __VA_ARGS__)
63 /// Printout with vf_zrmjpeg: prefix at WARNING level
64 #define WARNING(...) mp_msg(MSGT_DECVIDEO, MSGL_WARN, \
65 "vf_zrmjpeg: " __VA_ARGS__)
67 /// The get_pixels() routine to use. The real routine comes from dsputil
68 static void (*get_pixels
)(DCTELEM
*restrict block
, const uint8_t *pixels
, int line_size
);
70 /* Begin excessive code duplication ************************************/
71 /* Code coming from mpegvideo.c and mjpeg.c in ../libavcodec ***********/
73 /// copy of the table in mpegvideo.c
74 static const unsigned short aanscales
[64] = {
75 /**< precomputed values scaled up by 14 bits */
76 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
77 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
78 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
79 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
80 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
81 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
82 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446,
83 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247
86 /// Precompute DCT quantizing matrix
88 * This routine will precompute the combined DCT matrix with qscale
89 * and DCT renorm needed by the MPEG encoder here. It is basically the
90 * same as the routine with the same name in mpegvideo.c, except for
91 * some coefficient changes. The matrix will be computed in two variations,
92 * depending on the DCT version used. The second used by the MMX version of DCT.
94 * \param s MpegEncContext pointer
95 * \param qmat[OUT] pointer to where the matrix is stored
96 * \param qmat16[OUT] pointer to where matrix for MMX is stored.
97 * This matrix is not permutated
98 * and second 64 entries are bias
99 * \param quant_matrix[IN] the quantizion matrix to use
100 * \param bias bias for the quantizer
101 * \param qmin minimum qscale value to set up for
102 * \param qmax maximum qscale value to set up for
104 * Only rows between qmin and qmax will be populated in the matrix.
105 * In this MJPEG encoder, only the value 8 for qscale is used.
107 static void convert_matrix(MpegEncContext
*s
, int (*qmat
)[64],
108 uint16_t (*qmat16
)[2][64], const uint16_t *quant_matrix
,
109 int bias
, int qmin
, int qmax
) {
112 for(qscale
= qmin
; qscale
<= qmax
; qscale
++) {
114 if (s
->dsp
.fdct
== ff_jpeg_fdct_islow
) {
115 for (i
= 0; i
< 64; i
++) {
116 const int j
= s
->dsp
.idct_permutation
[i
];
117 /* 16 <= qscale * quant_matrix[i] <= 7905
118 * 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026
119 * (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i])
120 * >= (1<<36)/249205026
121 * 3444240 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */
122 qmat
[qscale
][i
] = (int)((UINT64_C(1) <<
124 (qscale
*quant_matrix
[j
]));
126 } else if (s
->dsp
.fdct
== fdct_ifast
) {
127 for (i
= 0; i
< 64; i
++) {
128 const int j
= s
->dsp
.idct_permutation
[i
];
129 /* 16 <= qscale * quant_matrix[i] <= 7905
130 * 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026
131 * (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i])
132 * >= (1<<36)/249205026
133 * 3444240 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */
134 qmat
[qscale
][i
] = (int)((UINT64_C(1) <<
135 (QMAT_SHIFT
+ 11))/(aanscales
[i
]
136 *qscale
* quant_matrix
[j
]));
139 for (i
= 0; i
< 64; i
++) {
140 const int j
= s
->dsp
.idct_permutation
[i
];
141 /* We can safely assume that 16 <= quant_matrix[i] <= 255
142 * So 16 <= qscale * quant_matrix[i] <= 7905
143 * so (1<<19) / 16 >= (1<<19) / (qscale * quant_matrix[i]) >= (1<<19) / 7905
144 * so 32768 >= (1<<19) / (qscale * quant_matrix[i]) >= 67 */
145 qmat
[qscale
][i
] = (int)((UINT64_C(1) <<
146 QMAT_SHIFT_MMX
) / (qscale
148 qmat16
[qscale
][0][i
] = (1 << QMAT_SHIFT_MMX
)
149 /(qscale
* quant_matrix
[j
]);
151 if (qmat16
[qscale
][0][i
] == 0 ||
152 qmat16
[qscale
][0][i
] == 128*256)
153 qmat16
[qscale
][0][i
]=128*256-1;
154 qmat16
[qscale
][1][i
]=ROUNDED_DIV(bias
155 <<(16-QUANT_BIAS_SHIFT
),
156 qmat16
[qscale
][0][i
]);
162 /// Emit the DC value into a MJPEG code sream
164 * This routine is only intended to be used from encode_block
166 * \param s pointer to MpegEncContext structure
167 * \param val the DC value to emit
168 * \param huff_size pointer to huffman code size array
169 * \param huff_code pointer to the code array corresponding to \a huff_size
171 * This routine is a clone of mjpeg_encode_dc
173 static inline void encode_dc(MpegEncContext
*s
, int val
,
174 uint8_t *huff_size
, uint16_t *huff_code
) {
178 put_bits(&s
->pb
, huff_size
[0], huff_code
[0]);
185 nbits
= av_log2_16bit(val
) + 1;
186 put_bits(&s
->pb
, huff_size
[nbits
], huff_code
[nbits
]);
187 put_bits(&s
->pb
, nbits
, mant
& ((1 << nbits
) - 1));
191 /// Huffman encode and emit one DCT block into the MJPEG code stream
193 * \param s pointer to MpegEncContext structure
194 * \param block pointer to the DCT block to emit
197 * This routine is a duplicate of encode_block in mjpeg.c
199 static void encode_block(MpegEncContext
*s
, DCTELEM
*block
, int n
) {
200 int mant
, nbits
, code
, i
, j
;
201 int component
, dc
, run
, last_index
, val
;
202 MJpegContext
*m
= s
->mjpeg_ctx
;
203 uint8_t *huff_size_ac
;
204 uint16_t *huff_code_ac
;
207 component
= (n
<= 3 ? 0 : n
- 4 + 1);
208 dc
= block
[0]; /* overflow is impossible */
209 val
= dc
- s
->last_dc
[component
];
211 encode_dc(s
, val
, m
->huff_size_dc_luminance
,
212 m
->huff_code_dc_luminance
);
213 huff_size_ac
= m
->huff_size_ac_luminance
;
214 huff_code_ac
= m
->huff_code_ac_luminance
;
216 encode_dc(s
, val
, m
->huff_size_dc_chrominance
,
217 m
->huff_code_dc_chrominance
);
218 huff_size_ac
= m
->huff_size_ac_chrominance
;
219 huff_code_ac
= m
->huff_code_ac_chrominance
;
221 s
->last_dc
[component
] = dc
;
226 last_index
= s
->block_last_index
[n
];
227 for (i
= 1; i
<= last_index
; i
++) {
228 j
= s
->intra_scantable
.permutated
[i
];
233 put_bits(&s
->pb
, huff_size_ac
[0xf0],
243 nbits
= av_log2_16bit(val
) + 1;
244 code
= (run
<< 4) | nbits
;
246 put_bits(&s
->pb
, huff_size_ac
[code
],
248 put_bits(&s
->pb
, nbits
, mant
& ((1 << nbits
) - 1));
253 /* output EOB only if not already 64 values */
254 if (last_index
< 63 || run
!= 0)
255 put_bits(&s
->pb
, huff_size_ac
[0], huff_code_ac
[0]);
258 /// clip overflowing DCT coefficients
260 * If the computed DCT coefficients in a block overflow, this routine
261 * will go through them and clip them to be in the valid range.
263 * \param s pointer to MpegEncContext
264 * \param block pointer to DCT block to process
265 * \param last_index index of the last non-zero coefficient in block
267 * The max and min level, which are clipped to, are stored in
268 * s->min_qcoeff and s->max_qcoeff respectively.
270 static inline void clip_coeffs(MpegEncContext
*s
, DCTELEM
*block
,
273 const int maxlevel
= s
->max_qcoeff
;
274 const int minlevel
= s
->min_qcoeff
;
276 for (i
= 0; i
<= last_index
; i
++) {
277 const int j
= s
->intra_scantable
.permutated
[i
];
278 int level
= block
[j
];
280 if (level
> maxlevel
) level
=maxlevel
;
281 else if(level
< minlevel
) level
=minlevel
;
286 /* End excessive code duplication **************************************/
289 struct MpegEncContext
*s
;
297 // Huffman encode and emit one MCU of MJPEG code
299 * \param j pointer to jpeg_enc_t structure
301 * This function huffman encodes one MCU, and emits the
302 * resulting bitstream into the MJPEG code that is currently worked on.
304 * this function is a reproduction of the one in mjpeg, it includes two
305 * changes, it allows for black&white encoding (it skips the U and V
306 * macroblocks and it outputs the huffman code for 'no change' (dc) and
307 * 'all zero' (ac)) and it takes 4 macroblocks (422) instead of 6 (420)
309 static av_always_inline
void zr_mjpeg_encode_mb(jpeg_enc_t
*j
) {
311 MJpegContext
*m
= j
->s
->mjpeg_ctx
;
313 encode_block(j
->s
, j
->s
->block
[0], 0);
314 encode_block(j
->s
, j
->s
->block
[1], 1);
317 put_bits(&j
->s
->pb
, m
->huff_size_dc_chrominance
[0],
318 m
->huff_code_dc_chrominance
[0]);
319 put_bits(&j
->s
->pb
, m
->huff_size_ac_chrominance
[0],
320 m
->huff_code_ac_chrominance
[0]);
322 put_bits(&j
->s
->pb
, m
->huff_size_dc_chrominance
[0],
323 m
->huff_code_dc_chrominance
[0]);
324 put_bits(&j
->s
->pb
, m
->huff_size_ac_chrominance
[0],
325 m
->huff_code_ac_chrominance
[0]);
327 /* we trick encode_block here so that it uses
328 * chrominance huffman tables instead of luminance ones
329 * (see the effect of second argument of encode_block) */
330 encode_block(j
->s
, j
->s
->block
[2], 4);
331 encode_block(j
->s
, j
->s
->block
[3], 5);
335 /// Fill one DCT MCU from planar storage
337 * This routine will convert one MCU from YUYV planar storage into 4
338 * DCT macro blocks, converting from 8-bit format in the planar
339 * storage to 16-bit format used in the DCT.
341 * \param j pointer to jpeg_enc structure, and also storage for DCT macro blocks
342 * \param x pixel x-coordinate for the first pixel
343 * \param y pixel y-coordinate for the first pixel
344 * \param y_data pointer to the Y plane
345 * \param u_data pointer to the U plane
346 * \param v_data pointer to the V plane
348 static av_always_inline
void fill_block(jpeg_enc_t
*j
, int x
, int y
,
349 unsigned char *y_data
, unsigned char *u_data
,
350 unsigned char *v_data
)
354 unsigned char *source
;
357 get_pixels(j
->s
->block
[0], y
*8*j
->y_rs
+ 16*x
+ y_data
, j
->y_rs
);
359 get_pixels(j
->s
->block
[1], y
*8*j
->y_rs
+ 16*x
+ 8 + y_data
, j
->y_rs
);
361 if (!j
->bw
&& j
->cheap_upsample
) {
362 source
= y
* 4 * j
->u_rs
+ 8*x
+ u_data
;
363 dest
= j
->s
->block
[2];
364 for (i
= 0; i
< 4; i
++) {
365 for (k
= 0; k
< 8; k
++) {
366 dest
[k
] = source
[k
]; // First row
367 dest
[k
+8] = source
[k
]; // Duplicate to next row
373 source
= y
* 4 * j
->v_rs
+ 8*x
+ v_data
;
374 dest
= j
->s
->block
[3];
375 for (i
= 0; i
< 4; i
++) {
376 for (k
= 0; k
< 8; k
++) {
378 dest
[k
+8] = source
[k
];
383 } else if (!j
->bw
&& !j
->cheap_upsample
) {
385 get_pixels(j
->s
->block
[2], y
*8*j
->u_rs
+ 8*x
+ u_data
, j
->u_rs
);
387 get_pixels(j
->s
->block
[3], y
*8*j
->v_rs
+ 8*x
+ v_data
, j
->v_rs
);
392 * \brief initialize mjpeg encoder
394 * This routine is to set up the parameters and initialize the mjpeg encoder.
395 * It does all the initializations needed of lower level routines.
396 * The formats accepted by this encoder is YUV422P and YUV420
398 * \param w width in pixels of the image to encode, must be a multiple of 16
399 * \param h height in pixels of the image to encode, must be a multiple of 8
400 * \param y_rsize size of each plane row Y component
401 * \param y_rsize size of each plane row U component
402 * \param v_rsize size of each plane row V component
403 * \param cu "cheap upsample". Set to 0 for YUV422 format, 1 for YUV420 format
404 * when set to 1, the encoder will assume that there is only half th
405 * number of rows of chroma information, and every chroma row is
407 * \param q quality parameter for the mjpeg encode. Between 1 and 20 where 1
408 * is best quality and 20 is the worst quality.
409 * \param b monochrome flag. When set to 1, the mjpeg output is monochrome.
410 * In that case, the colour information is omitted, and actually the
411 * colour planes are not touched.
413 * \returns an appropriately set up jpeg_enc_t structure
415 * The actual plane buffer addreses are passed by jpeg_enc_frame().
417 * The encoder doesn't know anything about interlacing, the halve height
418 * needs to be passed and the double rowstride. Which field gets encoded
419 * is decided by what buffers are passed to mjpeg_encode_frame()
421 static jpeg_enc_t
*jpeg_enc_init(int w
, int h
, int y_rsize
,
422 int u_rsize
, int v_rsize
,
423 int cu
, int q
, int b
) {
426 VERBOSE("JPEG encoder init: %dx%d %d %d %d cu=%d q=%d bw=%d\n",
427 w
, h
, y_rsize
, u_rsize
, v_rsize
, cu
, q
, b
);
429 j
= av_mallocz(sizeof(jpeg_enc_t
));
430 if (j
== NULL
) return NULL
;
432 j
->s
= av_mallocz(sizeof(MpegEncContext
));
438 /* info on how to access the pixels */
443 j
->s
->width
= w
; // image width and height
445 j
->s
->qscale
= q
; // Encoding quality
447 j
->s
->out_format
= FMT_MJPEG
;
448 j
->s
->intra_only
= 1; // Generate only intra pictures for jpeg
449 j
->s
->encoding
= 1; // Set mode to encode
450 j
->s
->pict_type
= FF_I_TYPE
;
451 j
->s
->y_dc_scale
= 8;
452 j
->s
->c_dc_scale
= 8;
455 * This sets up the MCU (Minimal Code Unit) number
456 * of appearances of the various component
457 * for the SOF0 table in the generated MJPEG.
458 * The values are not used for anything else.
459 * The current setup is simply YUV422, with two horizontal Y components
460 * for every UV component.
462 //FIXME j->s->mjpeg_write_tables = 1; // setup to write tables
463 j
->s
->mjpeg_vsample
[0] = 1; // 1 appearance of Y vertically
464 j
->s
->mjpeg_vsample
[1] = 1; // 1 appearance of U vertically
465 j
->s
->mjpeg_vsample
[2] = 1; // 1 appearance of V vertically
466 j
->s
->mjpeg_hsample
[0] = 2; // 2 appearances of Y horizontally
467 j
->s
->mjpeg_hsample
[1] = 1; // 1 appearance of U horizontally
468 j
->s
->mjpeg_hsample
[2] = 1; // 1 appearance of V horizontally
470 j
->cheap_upsample
= cu
;
475 // Build mjpeg huffman code tables, setting up j->s->mjpeg_ctx
476 if (ff_mjpeg_encode_init(j
->s
) < 0) {
482 /* alloc bogus avctx to keep MPV_common_init from segfaulting */
483 j
->s
->avctx
= avcodec_alloc_context();
484 if (j
->s
->avctx
== NULL
) {
490 // Set some a minimum amount of default values that are needed
491 // Indicates that we should generated normal MJPEG
492 j
->s
->avctx
->codec_id
= CODEC_ID_MJPEG
;
493 // Which DCT method to use. AUTO will select the fastest one
494 j
->s
->avctx
->dct_algo
= FF_DCT_AUTO
;
495 j
->s
->intra_quant_bias
= 1<<(QUANT_BIAS_SHIFT
-1); //(a + x/2)/x
496 // indicate we 'decode' to jpeg 4:2:2
497 j
->s
->avctx
->pix_fmt
= PIX_FMT_YUVJ422P
;
499 j
->s
->avctx
->thread_count
= 1;
501 /* make MPV_common_init allocate important buffers, like s->block
502 * Also initializes dsputil */
503 if (MPV_common_init(j
->s
) < 0) {
509 /* correct the value for sc->mb_height. MPV_common_init put other
511 j
->s
->mb_height
= j
->s
->height
/8;
515 j
->s
->intra_matrix
[0] = ff_mpeg1_default_intra_matrix
[0];
516 for (i
= 1; i
< 64; i
++)
517 j
->s
->intra_matrix
[i
] = av_clip_uint8(
518 (ff_mpeg1_default_intra_matrix
[i
]*j
->s
->qscale
) >> 3);
521 convert_matrix(j
->s
, j
->s
->q_intra_matrix
, j
->s
->q_intra_matrix16
,
522 j
->s
->intra_matrix
, j
->s
->intra_quant_bias
, 8, 8);
524 /* Pick up the selection of the optimal get_pixels() routine
525 * to use, which was done in MPV_common_init() */
526 get_pixels
= j
->s
->dsp
.get_pixels
;
532 * \brief mjpeg encode an image
534 * This routine will take a 3-plane YUV422 image and encoded it with MJPEG
535 * base line format, as suitable as input for the Zoran hardare MJPEG chips.
537 * It requires that the \a j parameter points the structure set up by the
538 * jpeg_enc_init() routine.
540 * \param j pointer to jpeg_enc_t structure as created by jpeg_enc_init()
541 * \param y_data pointer to Y component plane, packed one byte/pixel
542 * \param u_data pointer to U component plane, packed one byte per every
544 * \param v_data pointer to V component plane, packed one byte per every
546 * \param bufr pointer to the buffer where the mjpeg encoded code is stored
548 * \returns the number of bytes stored into \a bufr
550 * If \a j->s->mjpeg_write_tables is set, it will also emit the mjpeg tables,
551 * otherwise it will just emit the data. The \a j->s->mjpeg_write_tables
552 * variable will be reset to 0 by the routine.
554 static int jpeg_enc_frame(jpeg_enc_t
*j
, uint8_t *y_data
,
555 uint8_t *u_data
, uint8_t *v_data
, uint8_t *bufr
) {
556 int mb_x
, mb_y
, overflow
;
557 /* initialize the buffer */
559 init_put_bits(&j
->s
->pb
, bufr
, 1024*256);
561 // Emit the mjpeg header blocks
562 ff_mjpeg_encode_picture_header(j
->s
);
564 j
->s
->header_bits
= put_bits_count(&j
->s
->pb
);
566 j
->s
->last_dc
[0] = 128;
567 j
->s
->last_dc
[1] = 128;
568 j
->s
->last_dc
[2] = 128;
570 for (mb_y
= 0; mb_y
< j
->s
->mb_height
; mb_y
++) {
571 for (mb_x
= 0; mb_x
< j
->s
->mb_width
; mb_x
++) {
573 * Fill one DCT block (8x8 pixels) from
574 * 2 Y macroblocks and one U and one V
576 fill_block(j
, mb_x
, mb_y
, y_data
, u_data
, v_data
);
577 emms_c(); /* is this really needed? */
579 j
->s
->block_last_index
[0] =
580 j
->s
->dct_quantize(j
->s
, j
->s
->block
[0],
582 if (overflow
) clip_coeffs(j
->s
, j
->s
->block
[0],
583 j
->s
->block_last_index
[0]);
584 j
->s
->block_last_index
[1] =
585 j
->s
->dct_quantize(j
->s
, j
->s
->block
[1],
587 if (overflow
) clip_coeffs(j
->s
, j
->s
->block
[1],
588 j
->s
->block_last_index
[1]);
591 j
->s
->block_last_index
[4] =
592 j
->s
->dct_quantize(j
->s
, j
->s
->block
[2],
594 if (overflow
) clip_coeffs(j
->s
, j
->s
->block
[2],
595 j
->s
->block_last_index
[2]);
596 j
->s
->block_last_index
[5] =
597 j
->s
->dct_quantize(j
->s
, j
->s
->block
[3],
599 if (overflow
) clip_coeffs(j
->s
, j
->s
->block
[3],
600 j
->s
->block_last_index
[3]);
602 zr_mjpeg_encode_mb(j
);
606 ff_mjpeg_encode_picture_trailer(j
->s
);
607 flush_put_bits(&j
->s
->pb
);
610 //if (j->s->mjpeg_write_tables == 1)
611 // j->s->mjpeg_write_tables = 0;
613 return put_bits_ptr(&(j
->s
->pb
)) - j
->s
->pb
.buf
;
616 /// the real uninit routine
618 * This is the real routine that does the uninit of the ZRMJPEG filter
620 * \param j pointer to jpeg_enc structure
622 static void jpeg_enc_uninit(jpeg_enc_t
*j
) {
623 ff_mjpeg_encode_close(j
->s
);
628 /// Private structure for ZRMJPEG filter
631 unsigned char buf
[256*1024];
632 int bw
, fd
, hdec
, vdec
;
641 /// vf CONFIGURE entry point for the ZRMJPEG filter
643 * \param vf video filter instance pointer
644 * \param width image source width in pixels
645 * \param height image source height in pixels
646 * \param d_width width of requested window, just a hint
647 * \param d_height height of requested window, just a hint
648 * \param flags vf filter flags
651 * \returns returns 0 on error
653 * This routine will make the necessary hardware-related decisions for
654 * the ZRMJPEG filter, do the initialization of the MJPEG encoder, and
655 * then select one of the ZRJMJPEGIT or ZRMJPEGNI filters and then
656 * arrange to dispatch to the config() entry pointer for the one
659 static int config(struct vf_instance
*vf
, int width
, int height
, int d_width
,
660 int d_height
, unsigned int flags
, unsigned int outfmt
){
661 struct vf_priv_s
*priv
= vf
->priv
;
662 float aspect_decision
;
663 int stretchx
, stretchy
, err
= 0, maxstretchx
= 4;
666 VERBOSE("config() called\n");
669 VERBOSE("re-configuring, resetting JPEG encoder\n");
670 jpeg_enc_uninit(priv
->j
);
674 aspect_decision
= ((float)d_width
/(float)d_height
)/
675 ((float)width
/(float)height
);
677 if (aspect_decision
> 1.8 && aspect_decision
< 2.2) {
678 VERBOSE("should correct aspect by stretching x times 2, %d %d\n", 2*width
, priv
->maxwidth
);
679 if (2*width
<= priv
->maxwidth
) {
684 WARNING("unable to correct aspect by stretching, because resulting X will be too large, aspect correction by decimating y not yet implemented\n");
688 /* prestretch movie */
690 /* uncorrecting output for now */
694 /* make the scaling decision
695 * we are capable of stretching the image in the horizontal
696 * direction by factors 1, 2 and 4
697 * we can stretch the image in the vertical direction by a
698 * factor of 1 and 2 AND we must decide about interlacing */
699 if (d_width
> priv
->maxwidth
/2 || height
> priv
->maxheight
/2
700 || maxstretchx
== 1) {
704 if (priv
->vdec
== 2) {
706 } else if (priv
->vdec
== 4) {
710 if (priv
->hdec
> maxstretchx
) {
712 WARNING("horizontal decimation too high, "
713 "changing to %d (use fd to keep"
715 maxstretchx
, priv
->hdec
);
716 priv
->hdec
= maxstretchx
;
719 stretchx
= priv
->hdec
;
720 } else if (d_width
> priv
->maxwidth
/4 ||
721 height
> priv
->maxheight
/4 ||
726 if (priv
->vdec
== 2) {
728 } else if (priv
->vdec
== 4) {
730 WARNING("vertical decimation too high, "
731 "changing to 2 (use fd to keep "
737 if (priv
->hdec
== 2) {
739 } else if (priv
->hdec
== 4) {
741 WARNING("horizontal decimation too high, "
742 "changing to 2 (use fd to keep "
749 /* output image is maximally stretched */
753 if (priv
->vdec
!= 1 && !priv
->fd
) {
754 WARNING("vertical decimation too high, changing to 1 "
755 "(use fd to keep vdec=%d)\n",
759 if (priv
->hdec
!= 1 && !priv
->fd
) {
760 WARNING("horizontal decimation too high, changing to 1 (use fd to keep hdec=%d)\n", priv
->hdec
);
765 VERBOSE("generated JPEG's %dx%s%d%s, stretched to %dx%d\n",
766 width
/priv
->hdec
, (priv
->fields
== 2) ? "(" : "",
767 height
/(priv
->vdec
*priv
->fields
),
768 (priv
->fields
== 2) ? "x2)" : "",
769 (width
/priv
->hdec
)*stretchx
,
770 (height
/(priv
->vdec
*priv
->fields
))*
771 stretchy
*priv
->fields
);
774 if ((width
/priv
->hdec
)*stretchx
> priv
->maxwidth
||
775 (height
/(priv
->vdec
*priv
->fields
))*
776 stretchy
*priv
->fields
> priv
->maxheight
) {
777 ERROR("output dimensions too large (%dx%d), max (%dx%d) "
778 "insert crop to fix\n",
779 (width
/priv
->hdec
)*stretchx
,
780 (height
/(priv
->vdec
*priv
->fields
))*
781 stretchy
*priv
->fields
,
782 priv
->maxwidth
, priv
->maxheight
);
786 if (width
%(16*priv
->hdec
) != 0) {
787 ERROR("width must be a multiple of 16*hdec (%d), use expand\n",
792 if (height
%(8*priv
->fields
*priv
->vdec
) != 0) {
793 ERROR("height must be a multiple of 8*fields*vdec (%d),"
794 " use expand\n", priv
->vdec
*priv
->fields
*8);
800 priv
->y_stride
= width
;
801 priv
->c_stride
= width
/2;
802 priv
->j
= jpeg_enc_init(width
, height
/priv
->fields
,
803 priv
->fields
*priv
->y_stride
,
804 priv
->fields
*priv
->c_stride
,
805 priv
->fields
*priv
->c_stride
,
806 1, priv
->quality
, priv
->bw
);
808 if (!priv
->j
) return 0;
809 return vf_next_config(vf
, width
, height
, d_width
, d_height
, flags
,
810 (priv
->fields
== 2) ? IMGFMT_ZRMJPEGIT
: IMGFMT_ZRMJPEGNI
);
813 /// put_image entrypoint for the ZRMJPEG vf filter
815 * \param vf pointer to vf_instance
816 * \param mpi pointer to mp_image_t structure
819 static int put_image(struct vf_instance
*vf
, mp_image_t
*mpi
, double pts
){
820 struct vf_priv_s
*priv
= vf
->priv
;
824 for (i
= 0; i
< priv
->fields
; i
++)
825 size
+= jpeg_enc_frame(priv
->j
,
826 mpi
->planes
[0] + i
*priv
->y_stride
,
827 mpi
->planes
[1] + i
*priv
->c_stride
,
828 mpi
->planes
[2] + i
*priv
->c_stride
,
831 dmpi
= vf_get_image(vf
->next
, IMGFMT_ZRMJPEGNI
,
832 MP_IMGTYPE_EXPORT
, 0, mpi
->w
, mpi
->h
);
833 dmpi
->planes
[0] = (uint8_t*)priv
->buf
;
834 dmpi
->planes
[1] = (uint8_t*)size
;
835 return vf_next_put_image(vf
,dmpi
, pts
);
838 /// query_format entrypoint for the ZRMJPEG vf filter
840 * \param vf pointer to vf_instance
841 * \param fmt image format to query for
843 * \returns 0 if image format in fmt is not supported
845 * Given the image format specified by \a fmt, this routine is called
846 * to ask if the format is supported or not.
848 static int query_format(struct vf_instance
*vf
, unsigned int fmt
){
849 VERBOSE("query_format() called\n");
854 /* strictly speaking the output format of
855 * this filter will be known after config(),
856 * but everything that supports IMGFMT_ZRMJPEGNI
857 * should also support all other IMGFMT_ZRMJPEG* */
858 return vf_next_query_format(vf
, IMGFMT_ZRMJPEGNI
);
864 /// vf UNINIT entry point for the ZRMJPEG filter
866 * \param vf pointer to the vf instance structure
868 static void uninit(vf_instance_t
*vf
) {
869 struct vf_priv_s
*priv
= vf
->priv
;
870 VERBOSE("uninit() called\n");
871 if (priv
->j
) jpeg_enc_uninit(priv
->j
);
875 /// vf OPEN entry point for the ZRMJPEG filter
877 * \param vf pointer to the vf instance structure
878 * \param args the argument list string for the -vf zrmjpeg command
880 * \returns 0 for error, 1 for success
882 * This routine will do some basic initialization of local structures etc.,
883 * and then parse the command line arguments specific for the ZRMJPEG filter.
885 static int vf_open(vf_instance_t
*vf
, char *args
){
886 struct vf_priv_s
*priv
;
887 VERBOSE("vf_open() called: args=\"%s\"\n", args
);
890 vf
->put_image
= put_image
;
891 vf
->query_format
= query_format
;
894 priv
= vf
->priv
= calloc(sizeof(*priv
), 1);
896 ERROR("out of memory error\n");
900 /* maximum displayable size by zoran card, these defaults
901 * are for my own zoran card in PAL mode, these can be changed
902 * by filter options. But... in an ideal world these values would
903 * be queried from the vo device itself... */
904 priv
->maxwidth
= 768;
905 priv
->maxheight
= 576;
914 char *arg
, *tmp
, *ptr
, junk
;
917 /* save arguments, to be able to safely modify them */
920 ERROR("out of memory, this is bad\n");
926 while (*tmp
!= ':' && *tmp
) tmp
++;
927 if (*tmp
== ':') *tmp
++ = '\0';
929 VERBOSE("processing filter option \"%s\"\n", ptr
);
930 /* These options deal with the maximum output
931 * resolution of the zoran card. These should
932 * be queried from the vo device, but it is currently
933 * too difficult, so the user should tell the filter */
934 if (!strncmp("maxheight=", ptr
, 10)) {
935 if (sscanf(ptr
+10, "%d%c", &input
, &junk
) != 1)
937 "error parsing parameter to \"maxheight=\", \"%s\", ignoring\n"
940 priv
->maxheight
= input
;
941 VERBOSE("setting maxheight to %d\n",
944 } else if (!strncmp("quality=", ptr
, 8)) {
945 if (sscanf(ptr
+8, "%d%c", &input
, &junk
) != 1)
947 "error parsing parameter to \"quality=\", \"%s\", ignoring\n"
949 else if (input
< 1 || input
> 20)
951 "parameter to \"quality=\" out of range (1..20), %d\n", input
);
953 priv
->quality
= input
;
954 VERBOSE("setting JPEG quality to %d\n",
957 } else if (!strncmp("maxwidth=", ptr
, 9)) {
958 if (sscanf(ptr
+9, "%d%c", &input
, &junk
) != 1)
960 "error parsing parameter to \"maxwidth=\", \"%s\", ignoring\n"
963 priv
->maxwidth
= input
;
964 VERBOSE("setting maxwidth to %d\n",
967 } else if (!strncmp("hdec=", ptr
, 5)) {
968 if (sscanf(ptr
+5, "%d%c", &input
, &junk
) != 1)
970 "error parsing parameter to \"hdec=\", \"%s\", ignoring\n"
972 else if (input
!= 1 && input
!= 2 && input
!= 4)
974 "illegal parameter to \"hdec=\", %d, should be 1, 2 or 4",
979 "setting horizontal decimation to %d\n", priv
->maxwidth
);
981 } else if (!strncmp("vdec=", ptr
, 5)) {
982 if (sscanf(ptr
+5, "%d%c", &input
, &junk
) != 1)
984 "error parsing parameter to \"vdec=\", \"%s\", ignoring\n"
986 else if (input
!= 1 && input
!= 2 && input
!= 4)
988 "illegal parameter to \"vdec=\", %d, should be 1, 2 or 4",
993 "setting vertical decimation to %d\n", priv
->maxwidth
);
995 } else if (!strcasecmp("dc10+-PAL", ptr
) ||
996 !strcasecmp("dc10-PAL", ptr
)) {
997 priv
->maxwidth
= 768;
998 priv
->maxheight
= 576;
999 VERBOSE("setting DC10(+) PAL profile\n");
1000 } else if (!strcasecmp("fd", ptr
)) {
1002 VERBOSE("forcing decimation\n");
1003 } else if (!strcasecmp("nofd", ptr
)) {
1005 VERBOSE("decimate only if beautiful\n");
1006 } else if (!strcasecmp("bw", ptr
)) {
1008 VERBOSE("setting black and white encoding\n");
1009 } else if (!strcasecmp("color", ptr
)) {
1011 VERBOSE("setting color encoding\n");
1012 } else if (!strcasecmp("dc10+-NTSC", ptr
) ||
1013 !strcasecmp("dc10-NTSC", ptr
)) {
1014 priv
->maxwidth
= 640;
1015 priv
->maxheight
= 480;
1016 VERBOSE("setting DC10(+) NTSC profile\n");
1017 } else if (!strcasecmp("buz-PAL", ptr
) ||
1018 !strcasecmp("lml33-PAL", ptr
)) {
1019 priv
->maxwidth
= 720;
1020 priv
->maxheight
= 576;
1021 VERBOSE("setting buz/lml33 PAL profile\n");
1022 } else if (!strcasecmp("buz-NTSC", ptr
) ||
1023 !strcasecmp("lml33-NTSC", ptr
)) {
1024 priv
->maxwidth
= 720;
1025 priv
->maxheight
= 480;
1026 VERBOSE("setting buz/lml33 NTSC profile\n");
1028 WARNING("ignoring unknown filter option "
1029 "\"%s\", or missing argument\n",
1042 const vf_info_t vf_info_zrmjpeg
= {
1043 "realtime zoran MJPEG encoding",