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
508 // indicate we 'decode' to jpeg 4:2:2
509 j
->s
->avctx
->pix_fmt
= PIX_FMT_YUVJ422P
;
511 j
->s
->avctx
->thread_count
= 1;
513 /* make MPV_common_init allocate important buffers, like s->block
514 * Also initializes dsputil */
515 if (MPV_common_init(j
->s
) < 0) {
521 /* correct the value for sc->mb_height. MPV_common_init put other
523 j
->s
->mb_height
= j
->s
->height
/8;
527 j
->s
->intra_matrix
[0] = ff_mpeg1_default_intra_matrix
[0];
528 for (i
= 1; i
< 64; i
++)
529 j
->s
->intra_matrix
[i
] = av_clip_uint8(
530 (ff_mpeg1_default_intra_matrix
[i
]*j
->s
->qscale
) >> 3);
533 convert_matrix(j
->s
, j
->s
->q_intra_matrix
, j
->s
->q_intra_matrix16
,
534 j
->s
->intra_matrix
, j
->s
->intra_quant_bias
, 8, 8);
536 /* Pick up the selection of the optimal get_pixels() routine
537 * to use, which was done in MPV_common_init() */
538 get_pixels
= j
->s
->dsp
.get_pixels
;
544 * \brief mjpeg encode an image
546 * This routine will take a 3-plane YUV422 image and encoded it with MJPEG
547 * base line format, as suitable as input for the Zoran hardare MJPEG chips.
549 * It requires that the \a j parameter points the structure set up by the
550 * jpeg_enc_init() routine.
552 * \param j pointer to jpeg_enc_t structure as created by jpeg_enc_init()
553 * \param y_data pointer to Y component plane, packed one byte/pixel
554 * \param u_data pointer to U component plane, packed one byte per every
556 * \param v_data pointer to V component plane, packed one byte per every
558 * \param bufr pointer to the buffer where the mjpeg encoded code is stored
560 * \returns the number of bytes stored into \a bufr
562 * If \a j->s->mjpeg_write_tables is set, it will also emit the mjpeg tables,
563 * otherwise it will just emit the data. The \a j->s->mjpeg_write_tables
564 * variable will be reset to 0 by the routine.
566 static int jpeg_enc_frame(jpeg_enc_t
*j
, uint8_t *y_data
,
567 uint8_t *u_data
, uint8_t *v_data
, uint8_t *bufr
) {
568 int mb_x
, mb_y
, overflow
;
569 /* initialize the buffer */
571 init_put_bits(&j
->s
->pb
, bufr
, 1024*256);
573 // Emit the mjpeg header blocks
574 ff_mjpeg_encode_picture_header(j
->s
);
576 j
->s
->header_bits
= put_bits_count(&j
->s
->pb
);
578 j
->s
->last_dc
[0] = 128;
579 j
->s
->last_dc
[1] = 128;
580 j
->s
->last_dc
[2] = 128;
582 for (mb_y
= 0; mb_y
< j
->s
->mb_height
; mb_y
++) {
583 for (mb_x
= 0; mb_x
< j
->s
->mb_width
; mb_x
++) {
585 * Fill one DCT block (8x8 pixels) from
586 * 2 Y macroblocks and one U and one V
588 fill_block(j
, mb_x
, mb_y
, y_data
, u_data
, v_data
);
589 emms_c(); /* is this really needed? */
591 j
->s
->block_last_index
[0] =
592 j
->s
->dct_quantize(j
->s
, j
->s
->block
[0],
594 if (overflow
) clip_coeffs(j
->s
, j
->s
->block
[0],
595 j
->s
->block_last_index
[0]);
596 j
->s
->block_last_index
[1] =
597 j
->s
->dct_quantize(j
->s
, j
->s
->block
[1],
599 if (overflow
) clip_coeffs(j
->s
, j
->s
->block
[1],
600 j
->s
->block_last_index
[1]);
603 j
->s
->block_last_index
[4] =
604 j
->s
->dct_quantize(j
->s
, j
->s
->block
[2],
606 if (overflow
) clip_coeffs(j
->s
, j
->s
->block
[2],
607 j
->s
->block_last_index
[2]);
608 j
->s
->block_last_index
[5] =
609 j
->s
->dct_quantize(j
->s
, j
->s
->block
[3],
611 if (overflow
) clip_coeffs(j
->s
, j
->s
->block
[3],
612 j
->s
->block_last_index
[3]);
614 zr_mjpeg_encode_mb(j
);
618 ff_mjpeg_encode_picture_trailer(j
->s
);
619 flush_put_bits(&j
->s
->pb
);
622 //if (j->s->mjpeg_write_tables == 1)
623 // j->s->mjpeg_write_tables = 0;
625 return put_bits_ptr(&(j
->s
->pb
)) - j
->s
->pb
.buf
;
628 /// the real uninit routine
630 * This is the real routine that does the uninit of the ZRMJPEG filter
632 * \param j pointer to jpeg_enc structure
634 static void jpeg_enc_uninit(jpeg_enc_t
*j
) {
635 ff_mjpeg_encode_close(j
->s
);
640 /// Private structure for ZRMJPEG filter
643 unsigned char buf
[256*1024];
644 int bw
, fd
, hdec
, vdec
;
653 /// vf CONFIGURE entry point for the ZRMJPEG filter
655 * \param vf video filter instance pointer
656 * \param width image source width in pixels
657 * \param height image source height in pixels
658 * \param d_width width of requested window, just a hint
659 * \param d_height height of requested window, just a hint
660 * \param flags vf filter flags
663 * \returns returns 0 on error
665 * This routine will make the necessary hardware-related decisions for
666 * the ZRMJPEG filter, do the initialization of the MJPEG encoder, and
667 * then select one of the ZRJMJPEGIT or ZRMJPEGNI filters and then
668 * arrange to dispatch to the config() entry pointer for the one
671 static int config(struct vf_instance_s
* vf
, int width
, int height
, int d_width
,
672 int d_height
, unsigned int flags
, unsigned int outfmt
){
673 struct vf_priv_s
*priv
= vf
->priv
;
674 float aspect_decision
;
675 int stretchx
, stretchy
, err
= 0, maxstretchx
= 4;
678 VERBOSE("config() called\n");
681 VERBOSE("re-configuring, resetting JPEG encoder\n");
682 jpeg_enc_uninit(priv
->j
);
686 aspect_decision
= ((float)d_width
/(float)d_height
)/
687 ((float)width
/(float)height
);
689 if (aspect_decision
> 1.8 && aspect_decision
< 2.2) {
690 VERBOSE("should correct aspect by stretching x times 2, %d %d\n", 2*width
, priv
->maxwidth
);
691 if (2*width
<= priv
->maxwidth
) {
696 WARNING("unable to correct aspect by stretching, because resulting X will be too large, aspect correction by decimating y not yet implemented\n");
700 /* prestretch movie */
702 /* uncorrecting output for now */
706 /* make the scaling decision
707 * we are capable of stretching the image in the horizontal
708 * direction by factors 1, 2 and 4
709 * we can stretch the image in the vertical direction by a
710 * factor of 1 and 2 AND we must decide about interlacing */
711 if (d_width
> priv
->maxwidth
/2 || height
> priv
->maxheight
/2
712 || maxstretchx
== 1) {
716 if (priv
->vdec
== 2) {
718 } else if (priv
->vdec
== 4) {
722 if (priv
->hdec
> maxstretchx
) {
724 WARNING("horizontal decimation too high, "
725 "changing to %d (use fd to keep"
727 maxstretchx
, priv
->hdec
);
728 priv
->hdec
= maxstretchx
;
731 stretchx
= priv
->hdec
;
732 } else if (d_width
> priv
->maxwidth
/4 ||
733 height
> priv
->maxheight
/4 ||
738 if (priv
->vdec
== 2) {
740 } else if (priv
->vdec
== 4) {
742 WARNING("vertical decimation too high, "
743 "changing to 2 (use fd to keep "
749 if (priv
->hdec
== 2) {
751 } else if (priv
->hdec
== 4) {
753 WARNING("horizontal decimation too high, "
754 "changing to 2 (use fd to keep "
761 /* output image is maximally stretched */
765 if (priv
->vdec
!= 1 && !priv
->fd
) {
766 WARNING("vertical decimation too high, changing to 1 "
767 "(use fd to keep vdec=%d)\n",
771 if (priv
->hdec
!= 1 && !priv
->fd
) {
772 WARNING("horizontal decimation too high, changing to 1 (use fd to keep hdec=%d)\n", priv
->hdec
);
777 VERBOSE("generated JPEG's %dx%s%d%s, stretched to %dx%d\n",
778 width
/priv
->hdec
, (priv
->fields
== 2) ? "(" : "",
779 height
/(priv
->vdec
*priv
->fields
),
780 (priv
->fields
== 2) ? "x2)" : "",
781 (width
/priv
->hdec
)*stretchx
,
782 (height
/(priv
->vdec
*priv
->fields
))*
783 stretchy
*priv
->fields
);
786 if ((width
/priv
->hdec
)*stretchx
> priv
->maxwidth
||
787 (height
/(priv
->vdec
*priv
->fields
))*
788 stretchy
*priv
->fields
> priv
->maxheight
) {
789 ERROR("output dimensions too large (%dx%d), max (%dx%d) "
790 "insert crop to fix\n",
791 (width
/priv
->hdec
)*stretchx
,
792 (height
/(priv
->vdec
*priv
->fields
))*
793 stretchy
*priv
->fields
,
794 priv
->maxwidth
, priv
->maxheight
);
798 if (width
%(16*priv
->hdec
) != 0) {
799 ERROR("width must be a multiple of 16*hdec (%d), use expand\n",
804 if (height
%(8*priv
->fields
*priv
->vdec
) != 0) {
805 ERROR("height must be a multiple of 8*fields*vdec (%d),"
806 " use expand\n", priv
->vdec
*priv
->fields
*8);
812 priv
->y_stride
= width
;
813 priv
->c_stride
= width
/2;
814 priv
->j
= jpeg_enc_init(width
, height
/priv
->fields
,
815 priv
->fields
*priv
->y_stride
,
816 priv
->fields
*priv
->c_stride
,
817 priv
->fields
*priv
->c_stride
,
818 1, priv
->quality
, priv
->bw
);
820 if (!priv
->j
) return 0;
821 return vf_next_config(vf
, width
, height
, d_width
, d_height
, flags
,
822 (priv
->fields
== 2) ? IMGFMT_ZRMJPEGIT
: IMGFMT_ZRMJPEGNI
);
825 /// put_image entrypoint for the ZRMJPEG vf filter
827 * \param vf pointer to vf_instance
828 * \param mpi pointer to mp_image_t structure
831 static int put_image(struct vf_instance_s
* vf
, mp_image_t
*mpi
, double pts
){
832 struct vf_priv_s
*priv
= vf
->priv
;
836 for (i
= 0; i
< priv
->fields
; i
++)
837 size
+= jpeg_enc_frame(priv
->j
,
838 mpi
->planes
[0] + i
*priv
->y_stride
,
839 mpi
->planes
[1] + i
*priv
->c_stride
,
840 mpi
->planes
[2] + i
*priv
->c_stride
,
843 dmpi
= vf_get_image(vf
->next
, IMGFMT_ZRMJPEGNI
,
844 MP_IMGTYPE_EXPORT
, 0, mpi
->w
, mpi
->h
);
845 dmpi
->planes
[0] = (uint8_t*)priv
->buf
;
846 dmpi
->planes
[1] = (uint8_t*)size
;
847 return vf_next_put_image(vf
,dmpi
, pts
);
850 /// query_format entrypoint for the ZRMJPEG vf filter
852 * \param vf pointer to vf_instance
853 * \param fmt image format to query for
855 * \returns 0 if image format in fmt is not supported
857 * Given the image format specified by \a fmt, this routine is called
858 * to ask if the format is supported or not.
860 static int query_format(struct vf_instance_s
* vf
, unsigned int fmt
){
861 VERBOSE("query_format() called\n");
866 /* strictly speaking the output format of
867 * this filter will be known after config(),
868 * but everything that supports IMGFMT_ZRMJPEGNI
869 * should also support all other IMGFMT_ZRMJPEG* */
870 return vf_next_query_format(vf
, IMGFMT_ZRMJPEGNI
);
876 /// vf UNINIT entry point for the ZRMJPEG filter
878 * \param vf pointer to the vf instance structure
880 static void uninit(vf_instance_t
*vf
) {
881 struct vf_priv_s
*priv
= vf
->priv
;
882 VERBOSE("uninit() called\n");
883 if (priv
->j
) jpeg_enc_uninit(priv
->j
);
887 /// vf OPEN entry point for the ZRMJPEG filter
889 * \param vf pointer to the vf instance structure
890 * \param args the argument list string for the -vf zrmjpeg command
892 * \returns 0 for error, 1 for success
894 * This routine will do some basic initialization of local structures etc.,
895 * and then parse the command line arguments specific for the ZRMJPEG filter.
897 static int open(vf_instance_t
*vf
, char* args
){
898 struct vf_priv_s
*priv
;
899 VERBOSE("open() called: args=\"%s\"\n", args
);
902 vf
->put_image
= put_image
;
903 vf
->query_format
= query_format
;
906 priv
= vf
->priv
= calloc(sizeof(*priv
), 1);
908 ERROR("out of memory error\n");
912 /* maximum displayable size by zoran card, these defaults
913 * are for my own zoran card in PAL mode, these can be changed
914 * by filter options. But... in an ideal world these values would
915 * be queried from the vo device itself... */
916 priv
->maxwidth
= 768;
917 priv
->maxheight
= 576;
923 /* if libavcodec is already initialized, we must not initialize it
924 * again, but if it is not initialized then we mustinitialize it now. */
925 if (!avcodec_initialized
) {
926 /* we need to initialize libavcodec */
928 avcodec_register_all();
929 avcodec_initialized
=1;
933 char *arg
, *tmp
, *ptr
, junk
;
936 /* save arguments, to be able to safely modify them */
939 ERROR("out of memory, this is bad\n");
945 while (*tmp
!= ':' && *tmp
) tmp
++;
946 if (*tmp
== ':') *tmp
++ = '\0';
948 VERBOSE("processing filter option \"%s\"\n", ptr
);
949 /* These options deal with the maximum output
950 * resolution of the zoran card. These should
951 * be queried from the vo device, but it is currently
952 * too difficult, so the user should tell the filter */
953 if (!strncmp("maxheight=", ptr
, 10)) {
954 if (sscanf(ptr
+10, "%d%c", &input
, &junk
) != 1)
956 "error parsing parameter to \"maxheight=\", \"%s\", ignoring\n"
959 priv
->maxheight
= input
;
960 VERBOSE("setting maxheight to %d\n",
963 } else if (!strncmp("quality=", ptr
, 8)) {
964 if (sscanf(ptr
+8, "%d%c", &input
, &junk
) != 1)
966 "error parsing parameter to \"quality=\", \"%s\", ignoring\n"
968 else if (input
< 1 || input
> 20)
970 "parameter to \"quality=\" out of range (1..20), %d\n", input
);
972 priv
->quality
= input
;
973 VERBOSE("setting JPEG quality to %d\n",
976 } else if (!strncmp("maxwidth=", ptr
, 9)) {
977 if (sscanf(ptr
+9, "%d%c", &input
, &junk
) != 1)
979 "error parsing parameter to \"maxwidth=\", \"%s\", ignoring\n"
982 priv
->maxwidth
= input
;
983 VERBOSE("setting maxwidth to %d\n",
986 } else if (!strncmp("hdec=", ptr
, 5)) {
987 if (sscanf(ptr
+5, "%d%c", &input
, &junk
) != 1)
989 "error parsing parameter to \"hdec=\", \"%s\", ignoring\n"
991 else if (input
!= 1 && input
!= 2 && input
!= 4)
993 "illegal parameter to \"hdec=\", %d, should be 1, 2 or 4",
998 "setting horizontal decimation to %d\n", priv
->maxwidth
);
1000 } else if (!strncmp("vdec=", ptr
, 5)) {
1001 if (sscanf(ptr
+5, "%d%c", &input
, &junk
) != 1)
1003 "error parsing parameter to \"vdec=\", \"%s\", ignoring\n"
1005 else if (input
!= 1 && input
!= 2 && input
!= 4)
1007 "illegal parameter to \"vdec=\", %d, should be 1, 2 or 4",
1012 "setting vertical decimation to %d\n", priv
->maxwidth
);
1014 } else if (!strcasecmp("dc10+-PAL", ptr
) ||
1015 !strcasecmp("dc10-PAL", ptr
)) {
1016 priv
->maxwidth
= 768;
1017 priv
->maxheight
= 576;
1018 VERBOSE("setting DC10(+) PAL profile\n");
1019 } else if (!strcasecmp("fd", ptr
)) {
1021 VERBOSE("forcing decimation\n");
1022 } else if (!strcasecmp("nofd", ptr
)) {
1024 VERBOSE("decimate only if beautiful\n");
1025 } else if (!strcasecmp("bw", ptr
)) {
1027 VERBOSE("setting black and white encoding\n");
1028 } else if (!strcasecmp("color", ptr
)) {
1030 VERBOSE("setting color encoding\n");
1031 } else if (!strcasecmp("dc10+-NTSC", ptr
) ||
1032 !strcasecmp("dc10-NTSC", ptr
)) {
1033 priv
->maxwidth
= 640;
1034 priv
->maxheight
= 480;
1035 VERBOSE("setting DC10(+) NTSC profile\n");
1036 } else if (!strcasecmp("buz-PAL", ptr
) ||
1037 !strcasecmp("lml33-PAL", ptr
)) {
1038 priv
->maxwidth
= 720;
1039 priv
->maxheight
= 576;
1040 VERBOSE("setting buz/lml33 PAL profile\n");
1041 } else if (!strcasecmp("buz-NTSC", ptr
) ||
1042 !strcasecmp("lml33-NTSC", ptr
)) {
1043 priv
->maxwidth
= 720;
1044 priv
->maxheight
= 480;
1045 VERBOSE("setting buz/lml33 NTSC profile\n");
1047 WARNING("ignoring unknown filter option "
1048 "\"%s\", or missing argument\n",
1061 const vf_info_t vf_info_zrmjpeg
= {
1062 "realtime zoran MJPEG encoding",