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"
48 /* We need this #define because we need ../libavcodec/common.h to #define
49 * be2me_32, otherwise the linker will complain that it doesn't exist */
50 #define HAVE_AV_CONFIG_H
51 #include "libavcodec/avcodec.h"
52 #include "libavcodec/mjpegenc.h"
53 //#include "jpeg_enc.h" /* this file is not present yet */
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 // "local" flag in vd_ffmpeg.c. If not set, avcodec_init() et. al. need to be called
68 // set when init is done, so that initialization is not done twice.
69 extern int avcodec_initialized
;
71 /// The get_pixels() routine to use. The real routine comes from dsputil
72 static void (*get_pixels
)(DCTELEM
*restrict block
, const uint8_t *pixels
, int line_size
);
74 /* Begin excessive code duplication ************************************/
75 /* Code coming from mpegvideo.c and mjpeg.c in ../libavcodec ***********/
77 /// copy of the table in mpegvideo.c
78 static const unsigned short aanscales
[64] = {
79 /**< precomputed values scaled up by 14 bits */
80 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
81 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
82 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
83 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
84 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
85 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
86 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446,
87 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247
90 /// Precompute DCT quantizing matrix
92 * This routine will precompute the combined DCT matrix with qscale
93 * and DCT renorm needed by the MPEG encoder here. It is basically the
94 * same as the routine with the same name in mpegvideo.c, except for
95 * some coefficient changes. The matrix will be computed in two variations,
96 * depending on the DCT version used. The second used by the MMX version of DCT.
98 * \param s MpegEncContext pointer
99 * \param qmat[OUT] pointer to where the matrix is stored
100 * \param qmat16[OUT] pointer to where matrix for MMX is stored.
101 * This matrix is not permutated
102 * and second 64 entries are bias
103 * \param quant_matrix[IN] the quantizion matrix to use
104 * \param bias bias for the quantizer
105 * \param qmin minimum qscale value to set up for
106 * \param qmax maximum qscale value to set up for
108 * Only rows between qmin and qmax will be populated in the matrix.
109 * In this MJPEG encoder, only the value 8 for qscale is used.
111 static void convert_matrix(MpegEncContext
*s
, int (*qmat
)[64],
112 uint16_t (*qmat16
)[2][64], const uint16_t *quant_matrix
,
113 int bias
, int qmin
, int qmax
) {
116 for(qscale
= qmin
; qscale
<= qmax
; qscale
++) {
118 if (s
->dsp
.fdct
== ff_jpeg_fdct_islow
) {
119 for (i
= 0; i
< 64; i
++) {
120 const int j
= s
->dsp
.idct_permutation
[i
];
121 /* 16 <= qscale * quant_matrix[i] <= 7905
122 * 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026
123 * (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i])
124 * >= (1<<36)/249205026
125 * 3444240 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */
126 qmat
[qscale
][i
] = (int)((UINT64_C(1) <<
128 (qscale
*quant_matrix
[j
]));
130 } else if (s
->dsp
.fdct
== fdct_ifast
) {
131 for (i
= 0; i
< 64; i
++) {
132 const int j
= s
->dsp
.idct_permutation
[i
];
133 /* 16 <= qscale * quant_matrix[i] <= 7905
134 * 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026
135 * (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i])
136 * >= (1<<36)/249205026
137 * 3444240 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */
138 qmat
[qscale
][i
] = (int)((UINT64_C(1) <<
139 (QMAT_SHIFT
+ 11))/(aanscales
[i
]
140 *qscale
* quant_matrix
[j
]));
143 for (i
= 0; i
< 64; i
++) {
144 const int j
= s
->dsp
.idct_permutation
[i
];
145 /* We can safely assume that 16 <= quant_matrix[i] <= 255
146 * So 16 <= qscale * quant_matrix[i] <= 7905
147 * so (1<<19) / 16 >= (1<<19) / (qscale * quant_matrix[i]) >= (1<<19) / 7905
148 * so 32768 >= (1<<19) / (qscale * quant_matrix[i]) >= 67 */
149 qmat
[qscale
][i
] = (int)((UINT64_C(1) <<
150 QMAT_SHIFT_MMX
) / (qscale
152 qmat16
[qscale
][0][i
] = (1 << QMAT_SHIFT_MMX
)
153 /(qscale
* quant_matrix
[j
]);
155 if (qmat16
[qscale
][0][i
] == 0 ||
156 qmat16
[qscale
][0][i
] == 128*256)
157 qmat16
[qscale
][0][i
]=128*256-1;
158 qmat16
[qscale
][1][i
]=ROUNDED_DIV(bias
159 <<(16-QUANT_BIAS_SHIFT
),
160 qmat16
[qscale
][0][i
]);
166 /// Emit the DC value into a MJPEG code sream
168 * This routine is only intended to be used from encode_block
170 * \param s pointer to MpegEncContext structure
171 * \param val the DC value to emit
172 * \param huff_size pointer to huffman code size array
173 * \param huff_code pointer to the code array corresponding to \a huff_size
175 * This routine is a clone of mjpeg_encode_dc
177 static inline void encode_dc(MpegEncContext
*s
, int val
,
178 uint8_t *huff_size
, uint16_t *huff_code
) {
182 put_bits(&s
->pb
, huff_size
[0], huff_code
[0]);
189 nbits
= av_log2_16bit(val
) + 1;
190 put_bits(&s
->pb
, huff_size
[nbits
], huff_code
[nbits
]);
191 put_bits(&s
->pb
, nbits
, mant
& ((1 << nbits
) - 1));
195 /// Huffman encode and emit one DCT block into the MJPEG code stream
197 * \param s pointer to MpegEncContext structure
198 * \param block pointer to the DCT block to emit
201 * This routine is a duplicate of encode_block in mjpeg.c
203 static void encode_block(MpegEncContext
*s
, DCTELEM
*block
, int n
) {
204 int mant
, nbits
, code
, i
, j
;
205 int component
, dc
, run
, last_index
, val
;
206 MJpegContext
*m
= s
->mjpeg_ctx
;
207 uint8_t *huff_size_ac
;
208 uint16_t *huff_code_ac
;
211 component
= (n
<= 3 ? 0 : n
- 4 + 1);
212 dc
= block
[0]; /* overflow is impossible */
213 val
= dc
- s
->last_dc
[component
];
215 encode_dc(s
, val
, m
->huff_size_dc_luminance
,
216 m
->huff_code_dc_luminance
);
217 huff_size_ac
= m
->huff_size_ac_luminance
;
218 huff_code_ac
= m
->huff_code_ac_luminance
;
220 encode_dc(s
, val
, m
->huff_size_dc_chrominance
,
221 m
->huff_code_dc_chrominance
);
222 huff_size_ac
= m
->huff_size_ac_chrominance
;
223 huff_code_ac
= m
->huff_code_ac_chrominance
;
225 s
->last_dc
[component
] = dc
;
230 last_index
= s
->block_last_index
[n
];
231 for (i
= 1; i
<= last_index
; i
++) {
232 j
= s
->intra_scantable
.permutated
[i
];
237 put_bits(&s
->pb
, huff_size_ac
[0xf0],
247 nbits
= av_log2_16bit(val
) + 1;
248 code
= (run
<< 4) | nbits
;
250 put_bits(&s
->pb
, huff_size_ac
[code
],
252 put_bits(&s
->pb
, nbits
, mant
& ((1 << nbits
) - 1));
257 /* output EOB only if not already 64 values */
258 if (last_index
< 63 || run
!= 0)
259 put_bits(&s
->pb
, huff_size_ac
[0], huff_code_ac
[0]);
262 /// clip overflowing DCT coefficients
264 * If the computed DCT coefficients in a block overflow, this routine
265 * will go through them and clip them to be in the valid range.
267 * \param s pointer to MpegEncContext
268 * \param block pointer to DCT block to process
269 * \param last_index index of the last non-zero coefficient in block
271 * The max and min level, which are clipped to, are stored in
272 * s->min_qcoeff and s->max_qcoeff respectively.
274 static inline void clip_coeffs(MpegEncContext
*s
, DCTELEM
*block
,
277 const int maxlevel
= s
->max_qcoeff
;
278 const int minlevel
= s
->min_qcoeff
;
280 for (i
= 0; i
<= last_index
; i
++) {
281 const int j
= s
->intra_scantable
.permutated
[i
];
282 int level
= block
[j
];
284 if (level
> maxlevel
) level
=maxlevel
;
285 else if(level
< minlevel
) level
=minlevel
;
290 /* End excessive code duplication **************************************/
293 struct MpegEncContext
*s
;
301 // Huffman encode and emit one MCU of MJPEG code
303 * \param j pointer to jpeg_enc_t structure
305 * This function huffman encodes one MCU, and emits the
306 * resulting bitstream into the MJPEG code that is currently worked on.
308 * this function is a reproduction of the one in mjpeg, it includes two
309 * changes, it allows for black&white encoding (it skips the U and V
310 * macroblocks and it outputs the huffman code for 'no change' (dc) and
311 * 'all zero' (ac)) and it takes 4 macroblocks (422) instead of 6 (420)
313 static av_always_inline
void zr_mjpeg_encode_mb(jpeg_enc_t
*j
) {
315 MJpegContext
*m
= j
->s
->mjpeg_ctx
;
317 encode_block(j
->s
, j
->s
->block
[0], 0);
318 encode_block(j
->s
, j
->s
->block
[1], 1);
321 put_bits(&j
->s
->pb
, m
->huff_size_dc_chrominance
[0],
322 m
->huff_code_dc_chrominance
[0]);
323 put_bits(&j
->s
->pb
, m
->huff_size_ac_chrominance
[0],
324 m
->huff_code_ac_chrominance
[0]);
326 put_bits(&j
->s
->pb
, m
->huff_size_dc_chrominance
[0],
327 m
->huff_code_dc_chrominance
[0]);
328 put_bits(&j
->s
->pb
, m
->huff_size_ac_chrominance
[0],
329 m
->huff_code_ac_chrominance
[0]);
331 /* we trick encode_block here so that it uses
332 * chrominance huffman tables instead of luminance ones
333 * (see the effect of second argument of encode_block) */
334 encode_block(j
->s
, j
->s
->block
[2], 4);
335 encode_block(j
->s
, j
->s
->block
[3], 5);
339 /// Fill one DCT MCU from planar storage
341 * This routine will convert one MCU from YUYV planar storage into 4
342 * DCT macro blocks, converting from 8-bit format in the planar
343 * storage to 16-bit format used in the DCT.
345 * \param j pointer to jpeg_enc structure, and also storage for DCT macro blocks
346 * \param x pixel x-coordinate for the first pixel
347 * \param y pixel y-coordinate for the first pixel
348 * \param y_data pointer to the Y plane
349 * \param u_data pointer to the U plane
350 * \param v_data pointer to the V plane
352 static av_always_inline
void fill_block(jpeg_enc_t
*j
, int x
, int y
,
353 unsigned char *y_data
, unsigned char *u_data
,
354 unsigned char *v_data
)
358 unsigned char *source
;
361 get_pixels(j
->s
->block
[0], y
*8*j
->y_rs
+ 16*x
+ y_data
, j
->y_rs
);
363 get_pixels(j
->s
->block
[1], y
*8*j
->y_rs
+ 16*x
+ 8 + y_data
, j
->y_rs
);
365 if (!j
->bw
&& j
->cheap_upsample
) {
366 source
= y
* 4 * j
->u_rs
+ 8*x
+ u_data
;
367 dest
= j
->s
->block
[2];
368 for (i
= 0; i
< 4; i
++) {
369 for (k
= 0; k
< 8; k
++) {
370 dest
[k
] = source
[k
]; // First row
371 dest
[k
+8] = source
[k
]; // Duplicate to next row
377 source
= y
* 4 * j
->v_rs
+ 8*x
+ v_data
;
378 dest
= j
->s
->block
[3];
379 for (i
= 0; i
< 4; i
++) {
380 for (k
= 0; k
< 8; k
++) {
382 dest
[k
+8] = source
[k
];
387 } else if (!j
->bw
&& !j
->cheap_upsample
) {
389 get_pixels(j
->s
->block
[2], y
*8*j
->u_rs
+ 8*x
+ u_data
, j
->u_rs
);
391 get_pixels(j
->s
->block
[3], y
*8*j
->v_rs
+ 8*x
+ v_data
, j
->v_rs
);
396 * \brief initialize mjpeg encoder
398 * This routine is to set up the parameters and initialize the mjpeg encoder.
399 * It does all the initializations needed of lower level routines.
400 * The formats accepted by this encoder is YUV422P and YUV420
402 * \param w width in pixels of the image to encode, must be a multiple of 16
403 * \param h height in pixels of the image to encode, must be a multiple of 8
404 * \param y_rsize size of each plane row Y component
405 * \param y_rsize size of each plane row U component
406 * \param v_rsize size of each plane row V component
407 * \param cu "cheap upsample". Set to 0 for YUV422 format, 1 for YUV420 format
408 * when set to 1, the encoder will assume that there is only half th
409 * number of rows of chroma information, and every chroma row is
411 * \param q quality parameter for the mjpeg encode. Between 1 and 20 where 1
412 * is best quality and 20 is the worst quality.
413 * \param b monochrome flag. When set to 1, the mjpeg output is monochrome.
414 * In that case, the colour information is omitted, and actually the
415 * colour planes are not touched.
417 * \returns an appropriately set up jpeg_enc_t structure
419 * The actual plane buffer addreses are passed by jpeg_enc_frame().
421 * The encoder doesn't know anything about interlacing, the halve height
422 * needs to be passed and the double rowstride. Which field gets encoded
423 * is decided by what buffers are passed to mjpeg_encode_frame()
425 static jpeg_enc_t
*jpeg_enc_init(int w
, int h
, int y_rsize
,
426 int u_rsize
, int v_rsize
,
427 int cu
, int q
, int b
) {
430 VERBOSE("JPEG encoder init: %dx%d %d %d %d cu=%d q=%d bw=%d\n",
431 w
, h
, y_rsize
, u_rsize
, v_rsize
, cu
, q
, b
);
433 j
= av_mallocz(sizeof(jpeg_enc_t
));
434 if (j
== NULL
) return NULL
;
436 j
->s
= av_mallocz(sizeof(MpegEncContext
));
442 /* info on how to access the pixels */
447 j
->s
->width
= w
; // image width and height
449 j
->s
->qscale
= q
; // Encoding quality
451 j
->s
->out_format
= FMT_MJPEG
;
452 j
->s
->intra_only
= 1; // Generate only intra pictures for jpeg
453 j
->s
->encoding
= 1; // Set mode to encode
454 j
->s
->pict_type
= FF_I_TYPE
;
455 j
->s
->y_dc_scale
= 8;
456 j
->s
->c_dc_scale
= 8;
459 * This sets up the MCU (Minimal Code Unit) number
460 * of appearances of the various component
461 * for the SOF0 table in the generated MJPEG.
462 * The values are not used for anything else.
463 * The current setup is simply YUV422, with two horizontal Y components
464 * for every UV component.
466 //FIXME j->s->mjpeg_write_tables = 1; // setup to write tables
467 j
->s
->mjpeg_vsample
[0] = 1; // 1 appearance of Y vertically
468 j
->s
->mjpeg_vsample
[1] = 1; // 1 appearance of U vertically
469 j
->s
->mjpeg_vsample
[2] = 1; // 1 appearance of V vertically
470 j
->s
->mjpeg_hsample
[0] = 2; // 2 appearances of Y horizontally
471 j
->s
->mjpeg_hsample
[1] = 1; // 1 appearance of U horizontally
472 j
->s
->mjpeg_hsample
[2] = 1; // 1 appearance of V horizontally
474 j
->cheap_upsample
= cu
;
478 /* if libavcodec is used by the decoder then we must not
479 * initialize again, but if it is not initialized then we must
480 * initialize it here. */
481 if (!avcodec_initialized
) {
483 avcodec_register_all();
484 avcodec_initialized
=1;
487 // Build mjpeg huffman code tables, setting up j->s->mjpeg_ctx
488 if (ff_mjpeg_encode_init(j
->s
) < 0) {
494 /* alloc bogus avctx to keep MPV_common_init from segfaulting */
495 j
->s
->avctx
= avcodec_alloc_context();
496 if (j
->s
->avctx
== NULL
) {
502 // Set some a minimum amount of default values that are needed
503 // Indicates that we should generated normal MJPEG
504 j
->s
->avctx
->codec_id
= CODEC_ID_MJPEG
;
505 // Which DCT method to use. AUTO will select the fastest one
506 j
->s
->avctx
->dct_algo
= FF_DCT_AUTO
;
507 j
->s
->intra_quant_bias
= 1<<(QUANT_BIAS_SHIFT
-1); //(a + x/2)/x
509 j
->s
->avctx
->thread_count
= 1;
511 /* make MPV_common_init allocate important buffers, like s->block
512 * Also initializes dsputil */
513 if (MPV_common_init(j
->s
) < 0) {
519 /* correct the value for sc->mb_height. MPV_common_init put other
521 j
->s
->mb_height
= j
->s
->height
/8;
525 j
->s
->intra_matrix
[0] = ff_mpeg1_default_intra_matrix
[0];
526 for (i
= 1; i
< 64; i
++)
527 j
->s
->intra_matrix
[i
] = av_clip_uint8(
528 (ff_mpeg1_default_intra_matrix
[i
]*j
->s
->qscale
) >> 3);
531 convert_matrix(j
->s
, j
->s
->q_intra_matrix
, j
->s
->q_intra_matrix16
,
532 j
->s
->intra_matrix
, j
->s
->intra_quant_bias
, 8, 8);
534 /* Pick up the selection of the optimal get_pixels() routine
535 * to use, which was done in MPV_common_init() */
536 get_pixels
= j
->s
->dsp
.get_pixels
;
542 * \brief mjpeg encode an image
544 * This routine will take a 3-plane YUV422 image and encoded it with MJPEG
545 * base line format, as suitable as input for the Zoran hardare MJPEG chips.
547 * It requires that the \a j parameter points the structure set up by the
548 * jpeg_enc_init() routine.
550 * \param j pointer to jpeg_enc_t structure as created by jpeg_enc_init()
551 * \param y_data pointer to Y component plane, packed one byte/pixel
552 * \param u_data pointer to U component plane, packed one byte per every
554 * \param v_data pointer to V component plane, packed one byte per every
556 * \param bufr pointer to the buffer where the mjpeg encoded code is stored
558 * \returns the number of bytes stored into \a bufr
560 * If \a j->s->mjpeg_write_tables is set, it will also emit the mjpeg tables,
561 * otherwise it will just emit the data. The \a j->s->mjpeg_write_tables
562 * variable will be reset to 0 by the routine.
564 static int jpeg_enc_frame(jpeg_enc_t
*j
, uint8_t *y_data
,
565 uint8_t *u_data
, uint8_t *v_data
, uint8_t *bufr
) {
566 int mb_x
, mb_y
, overflow
;
567 /* initialize the buffer */
569 init_put_bits(&j
->s
->pb
, bufr
, 1024*256);
571 // Emit the mjpeg header blocks
572 ff_mjpeg_encode_picture_header(j
->s
);
574 j
->s
->header_bits
= put_bits_count(&j
->s
->pb
);
576 j
->s
->last_dc
[0] = 128;
577 j
->s
->last_dc
[1] = 128;
578 j
->s
->last_dc
[2] = 128;
580 for (mb_y
= 0; mb_y
< j
->s
->mb_height
; mb_y
++) {
581 for (mb_x
= 0; mb_x
< j
->s
->mb_width
; mb_x
++) {
583 * Fill one DCT block (8x8 pixels) from
584 * 2 Y macroblocks and one U and one V
586 fill_block(j
, mb_x
, mb_y
, y_data
, u_data
, v_data
);
587 emms_c(); /* is this really needed? */
589 j
->s
->block_last_index
[0] =
590 j
->s
->dct_quantize(j
->s
, j
->s
->block
[0],
592 if (overflow
) clip_coeffs(j
->s
, j
->s
->block
[0],
593 j
->s
->block_last_index
[0]);
594 j
->s
->block_last_index
[1] =
595 j
->s
->dct_quantize(j
->s
, j
->s
->block
[1],
597 if (overflow
) clip_coeffs(j
->s
, j
->s
->block
[1],
598 j
->s
->block_last_index
[1]);
601 j
->s
->block_last_index
[4] =
602 j
->s
->dct_quantize(j
->s
, j
->s
->block
[2],
604 if (overflow
) clip_coeffs(j
->s
, j
->s
->block
[2],
605 j
->s
->block_last_index
[2]);
606 j
->s
->block_last_index
[5] =
607 j
->s
->dct_quantize(j
->s
, j
->s
->block
[3],
609 if (overflow
) clip_coeffs(j
->s
, j
->s
->block
[3],
610 j
->s
->block_last_index
[3]);
612 zr_mjpeg_encode_mb(j
);
616 ff_mjpeg_encode_picture_trailer(j
->s
);
617 flush_put_bits(&j
->s
->pb
);
620 //if (j->s->mjpeg_write_tables == 1)
621 // j->s->mjpeg_write_tables = 0;
623 return pbBufPtr(&(j
->s
->pb
)) - j
->s
->pb
.buf
;
626 /// the real uninit routine
628 * This is the real routine that does the uninit of the ZRMJPEG filter
630 * \param j pointer to jpeg_enc structure
632 static void jpeg_enc_uninit(jpeg_enc_t
*j
) {
633 ff_mjpeg_encode_close(j
->s
);
638 /// Private structure for ZRMJPEG filter
641 unsigned char buf
[256*1024];
642 int bw
, fd
, hdec
, vdec
;
651 /// vf CONFIGURE entry point for the ZRMJPEG filter
653 * \param vf video filter instance pointer
654 * \param width image source width in pixels
655 * \param height image source height in pixels
656 * \param d_width width of requested window, just a hint
657 * \param d_height height of requested window, just a hint
658 * \param flags vf filter flags
661 * \returns returns 0 on error
663 * This routine will make the necessary hardware-related decisions for
664 * the ZRMJPEG filter, do the initialization of the MJPEG encoder, and
665 * then select one of the ZRJMJPEGIT or ZRMJPEGNI filters and then
666 * arrange to dispatch to the config() entry pointer for the one
669 static int config(struct vf_instance_s
* vf
, int width
, int height
, int d_width
,
670 int d_height
, unsigned int flags
, unsigned int outfmt
){
671 struct vf_priv_s
*priv
= vf
->priv
;
672 float aspect_decision
;
673 int stretchx
, stretchy
, err
= 0, maxstretchx
= 4;
676 VERBOSE("config() called\n");
679 VERBOSE("re-configuring, resetting JPEG encoder\n");
680 jpeg_enc_uninit(priv
->j
);
684 aspect_decision
= ((float)d_width
/(float)d_height
)/
685 ((float)width
/(float)height
);
687 if (aspect_decision
> 1.8 && aspect_decision
< 2.2) {
688 VERBOSE("should correct aspect by stretching x times 2, %d %d\n", 2*width
, priv
->maxwidth
);
689 if (2*width
<= priv
->maxwidth
) {
694 WARNING("unable to correct aspect by stretching, because resulting X will be too large, aspect correction by decimating y not yet implemented\n");
698 /* prestretch movie */
700 /* uncorrecting output for now */
704 /* make the scaling decision
705 * we are capable of stretching the image in the horizontal
706 * direction by factors 1, 2 and 4
707 * we can stretch the image in the vertical direction by a
708 * factor of 1 and 2 AND we must decide about interlacing */
709 if (d_width
> priv
->maxwidth
/2 || height
> priv
->maxheight
/2
710 || maxstretchx
== 1) {
714 if (priv
->vdec
== 2) {
716 } else if (priv
->vdec
== 4) {
720 if (priv
->hdec
> maxstretchx
) {
722 WARNING("horizontal decimation too high, "
723 "changing to %d (use fd to keep"
725 maxstretchx
, priv
->hdec
);
726 priv
->hdec
= maxstretchx
;
729 stretchx
= priv
->hdec
;
730 } else if (d_width
> priv
->maxwidth
/4 ||
731 height
> priv
->maxheight
/4 ||
736 if (priv
->vdec
== 2) {
738 } else if (priv
->vdec
== 4) {
740 WARNING("vertical decimation too high, "
741 "changing to 2 (use fd to keep "
747 if (priv
->hdec
== 2) {
749 } else if (priv
->hdec
== 4) {
751 WARNING("horizontal decimation too high, "
752 "changing to 2 (use fd to keep "
759 /* output image is maximally stretched */
763 if (priv
->vdec
!= 1 && !priv
->fd
) {
764 WARNING("vertical decimation too high, changing to 1 "
765 "(use fd to keep vdec=%d)\n",
769 if (priv
->hdec
!= 1 && !priv
->fd
) {
770 WARNING("horizontal decimation too high, changing to 1 (use fd to keep hdec=%d)\n", priv
->hdec
);
775 VERBOSE("generated JPEG's %dx%s%d%s, stretched to %dx%d\n",
776 width
/priv
->hdec
, (priv
->fields
== 2) ? "(" : "",
777 height
/(priv
->vdec
*priv
->fields
),
778 (priv
->fields
== 2) ? "x2)" : "",
779 (width
/priv
->hdec
)*stretchx
,
780 (height
/(priv
->vdec
*priv
->fields
))*
781 stretchy
*priv
->fields
);
784 if ((width
/priv
->hdec
)*stretchx
> priv
->maxwidth
||
785 (height
/(priv
->vdec
*priv
->fields
))*
786 stretchy
*priv
->fields
> priv
->maxheight
) {
787 ERROR("output dimensions too large (%dx%d), max (%dx%d) "
788 "insert crop to fix\n",
789 (width
/priv
->hdec
)*stretchx
,
790 (height
/(priv
->vdec
*priv
->fields
))*
791 stretchy
*priv
->fields
,
792 priv
->maxwidth
, priv
->maxheight
);
796 if (width
%(16*priv
->hdec
) != 0) {
797 ERROR("width must be a multiple of 16*hdec (%d), use expand\n",
802 if (height
%(8*priv
->fields
*priv
->vdec
) != 0) {
803 ERROR("height must be a multiple of 8*fields*vdec (%d),"
804 " use expand\n", priv
->vdec
*priv
->fields
*8);
810 priv
->y_stride
= width
;
811 priv
->c_stride
= width
/2;
812 priv
->j
= jpeg_enc_init(width
, height
/priv
->fields
,
813 priv
->fields
*priv
->y_stride
,
814 priv
->fields
*priv
->c_stride
,
815 priv
->fields
*priv
->c_stride
,
816 1, priv
->quality
, priv
->bw
);
818 if (!priv
->j
) return 0;
819 return vf_next_config(vf
, width
, height
, d_width
, d_height
, flags
,
820 (priv
->fields
== 2) ? IMGFMT_ZRMJPEGIT
: IMGFMT_ZRMJPEGNI
);
823 /// put_image entrypoint for the ZRMJPEG vf filter
825 * \param vf pointer to vf_instance
826 * \param mpi pointer to mp_image_t structure
829 static int put_image(struct vf_instance_s
* vf
, mp_image_t
*mpi
, double pts
){
830 struct vf_priv_s
*priv
= vf
->priv
;
834 for (i
= 0; i
< priv
->fields
; i
++)
835 size
+= jpeg_enc_frame(priv
->j
,
836 mpi
->planes
[0] + i
*priv
->y_stride
,
837 mpi
->planes
[1] + i
*priv
->c_stride
,
838 mpi
->planes
[2] + i
*priv
->c_stride
,
841 dmpi
= vf_get_image(vf
->next
, IMGFMT_ZRMJPEGNI
,
842 MP_IMGTYPE_EXPORT
, 0, mpi
->w
, mpi
->h
);
843 dmpi
->planes
[0] = (uint8_t*)priv
->buf
;
844 dmpi
->planes
[1] = (uint8_t*)size
;
845 return vf_next_put_image(vf
,dmpi
, pts
);
848 /// query_format entrypoint for the ZRMJPEG vf filter
850 * \param vf pointer to vf_instance
851 * \param fmt image format to query for
853 * \returns 0 if image format in fmt is not supported
855 * Given the image format specified by \a fmt, this routine is called
856 * to ask if the format is supported or not.
858 static int query_format(struct vf_instance_s
* vf
, unsigned int fmt
){
859 VERBOSE("query_format() called\n");
864 /* strictly speaking the output format of
865 * this filter will be known after config(),
866 * but everything that supports IMGFMT_ZRMJPEGNI
867 * should also support all other IMGFMT_ZRMJPEG* */
868 return vf_next_query_format(vf
, IMGFMT_ZRMJPEGNI
);
874 /// vf UNINIT entry point for the ZRMJPEG filter
876 * \param vf pointer to the vf instance structure
878 static void uninit(vf_instance_t
*vf
) {
879 struct vf_priv_s
*priv
= vf
->priv
;
880 VERBOSE("uninit() called\n");
881 if (priv
->j
) jpeg_enc_uninit(priv
->j
);
885 /// vf OPEN entry point for the ZRMJPEG filter
887 * \param vf pointer to the vf instance structure
888 * \param args the argument list string for the -vf zrmjpeg command
890 * \returns 0 for error, 1 for success
892 * This routine will do some basic initialization of local structures etc.,
893 * and then parse the command line arguments specific for the ZRMJPEG filter.
895 static int open(vf_instance_t
*vf
, char* args
){
896 struct vf_priv_s
*priv
;
897 VERBOSE("open() called: args=\"%s\"\n", args
);
900 vf
->put_image
= put_image
;
901 vf
->query_format
= query_format
;
904 priv
= vf
->priv
= calloc(sizeof(*priv
), 1);
906 ERROR("out of memory error\n");
910 /* maximum displayable size by zoran card, these defaults
911 * are for my own zoran card in PAL mode, these can be changed
912 * by filter options. But... in an ideal world these values would
913 * be queried from the vo device itself... */
914 priv
->maxwidth
= 768;
915 priv
->maxheight
= 576;
921 /* if libavcodec is already initialized, we must not initialize it
922 * again, but if it is not initialized then we mustinitialize it now. */
923 if (!avcodec_initialized
) {
924 /* we need to initialize libavcodec */
926 avcodec_register_all();
927 avcodec_initialized
=1;
931 char *arg
, *tmp
, *ptr
, junk
;
934 /* save arguments, to be able to safely modify them */
937 ERROR("out of memory, this is bad\n");
943 while (*tmp
!= ':' && *tmp
) tmp
++;
944 if (*tmp
== ':') *tmp
++ = '\0';
946 VERBOSE("processing filter option \"%s\"\n", ptr
);
947 /* These options deal with the maximum output
948 * resolution of the zoran card. These should
949 * be queried from the vo device, but it is currently
950 * too difficult, so the user should tell the filter */
951 if (!strncmp("maxheight=", ptr
, 10)) {
952 if (sscanf(ptr
+10, "%d%c", &input
, &junk
) != 1)
954 "error parsing parameter to \"maxheight=\", \"%s\", ignoring\n"
957 priv
->maxheight
= input
;
958 VERBOSE("setting maxheight to %d\n",
961 } else if (!strncmp("quality=", ptr
, 8)) {
962 if (sscanf(ptr
+8, "%d%c", &input
, &junk
) != 1)
964 "error parsing parameter to \"quality=\", \"%s\", ignoring\n"
966 else if (input
< 1 || input
> 20)
968 "parameter to \"quality=\" out of range (1..20), %d\n", input
);
970 priv
->quality
= input
;
971 VERBOSE("setting JPEG quality to %d\n",
974 } else if (!strncmp("maxwidth=", ptr
, 9)) {
975 if (sscanf(ptr
+9, "%d%c", &input
, &junk
) != 1)
977 "error parsing parameter to \"maxwidth=\", \"%s\", ignoring\n"
980 priv
->maxwidth
= input
;
981 VERBOSE("setting maxwidth to %d\n",
984 } else if (!strncmp("hdec=", ptr
, 5)) {
985 if (sscanf(ptr
+5, "%d%c", &input
, &junk
) != 1)
987 "error parsing parameter to \"hdec=\", \"%s\", ignoring\n"
989 else if (input
!= 1 && input
!= 2 && input
!= 4)
991 "illegal parameter to \"hdec=\", %d, should be 1, 2 or 4",
996 "setting horizontal decimation to %d\n", priv
->maxwidth
);
998 } else if (!strncmp("vdec=", ptr
, 5)) {
999 if (sscanf(ptr
+5, "%d%c", &input
, &junk
) != 1)
1001 "error parsing parameter to \"vdec=\", \"%s\", ignoring\n"
1003 else if (input
!= 1 && input
!= 2 && input
!= 4)
1005 "illegal parameter to \"vdec=\", %d, should be 1, 2 or 4",
1010 "setting vertical decimation to %d\n", priv
->maxwidth
);
1012 } else if (!strcasecmp("dc10+-PAL", ptr
) ||
1013 !strcasecmp("dc10-PAL", ptr
)) {
1014 priv
->maxwidth
= 768;
1015 priv
->maxheight
= 576;
1016 VERBOSE("setting DC10(+) PAL profile\n");
1017 } else if (!strcasecmp("fd", ptr
)) {
1019 VERBOSE("forcing decimation\n");
1020 } else if (!strcasecmp("nofd", ptr
)) {
1022 VERBOSE("decimate only if beautiful\n");
1023 } else if (!strcasecmp("bw", ptr
)) {
1025 VERBOSE("setting black and white encoding\n");
1026 } else if (!strcasecmp("color", ptr
)) {
1028 VERBOSE("setting color encoding\n");
1029 } else if (!strcasecmp("dc10+-NTSC", ptr
) ||
1030 !strcasecmp("dc10-NTSC", ptr
)) {
1031 priv
->maxwidth
= 640;
1032 priv
->maxheight
= 480;
1033 VERBOSE("setting DC10(+) NTSC profile\n");
1034 } else if (!strcasecmp("buz-PAL", ptr
) ||
1035 !strcasecmp("lml33-PAL", ptr
)) {
1036 priv
->maxwidth
= 720;
1037 priv
->maxheight
= 576;
1038 VERBOSE("setting buz/lml33 PAL profile\n");
1039 } else if (!strcasecmp("buz-NTSC", ptr
) ||
1040 !strcasecmp("lml33-NTSC", ptr
)) {
1041 priv
->maxwidth
= 720;
1042 priv
->maxheight
= 480;
1043 VERBOSE("setting buz/lml33 NTSC profile\n");
1045 WARNING("ignoring unknown filter option "
1046 "\"%s\", or missing argument\n",
1059 const vf_info_t vf_info_zrmjpeg
= {
1060 "realtime zoran MJPEG encoding",