Avoid passing test invalid arguments when string is empty.
[mplayer/glamo.git] / libmpcodecs / vf_zrmjpeg.c
blobc2eae4049dffe972332e051b9fb4dd8a8faf4661
1 /**
2 * \file vf_zrmjpeg.c
4 * \brief Does mjpeg encoding as required by the zrmjpeg filter as well
5 * as by the zr video driver.
6 */
7 /*
8 * Copyright (C) 2005 Rik Snel <rsnel@cube.dyndns.org>, license GPL v2
9 * - based on vd_lavc.c by A'rpi (C) 2002-2003
10 * - parts from ffmpeg Copyright (c) 2000-2003 Fabrice Bellard
12 * This files includes a straightforward (to be) optimized JPEG encoder for
13 * the YUV422 format, based on mjpeg code from ffmpeg.
15 * For an excellent introduction to the JPEG format, see:
16 * http://www.ece.purdue.edu/~bouman/grad-labs/lab8/pdf/lab.pdf
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <inttypes.h>
23 #include "config.h"
24 #include "mp_msg.h"
26 #include "img_format.h"
27 #include "mp_image.h"
28 #include "vf.h"
30 #ifdef USE_FASTMEMCPY
31 #include "libvo/fastmemcpy.h"
32 #endif
34 /* We need this #define because we need ../libavcodec/common.h to #define
35 * be2me_32, otherwise the linker will complain that it doesn't exist */
36 #define HAVE_AV_CONFIG_H
37 #include "libavcodec/avcodec.h"
38 #include "libavcodec/dsputil.h"
39 #include "libavcodec/mpegvideo.h"
40 //#include "jpeg_enc.h" /* this file is not present yet */
42 #undef malloc
43 #undef free
44 #undef realloc
46 extern int avcodec_inited;
48 /* some convenient #define's, is this portable enough? */
49 /// Printout with vf_zrmjpeg: prefix at VERBOSE level
50 #define VERBOSE(...) mp_msg(MSGT_DECVIDEO, MSGL_V, "vf_zrmjpeg: " __VA_ARGS__)
51 /// Printout with vf_zrmjpeg: prefix at ERROR level
52 #define ERROR(...) mp_msg(MSGT_DECVIDEO, MSGL_ERR, "vf_zrmjpeg: " __VA_ARGS__)
53 /// Printout with vf_zrmjpeg: prefix at WARNING level
54 #define WARNING(...) mp_msg(MSGT_DECVIDEO, MSGL_WARN, \
55 "vf_zrmjpeg: " __VA_ARGS__)
57 // "local" flag in vd_ffmpeg.c. If not set, avcodec_init() et. al. need to be called
58 // set when init is done, so that initialization is not done twice.
59 extern int avcodec_inited;
61 /// structure copied from mjpeg.c
62 /* zrmjpeg_encode_mb needs access to these tables for the black & white
63 * option */
64 typedef struct MJpegContext {
65 uint8_t huff_size_dc_luminance[12];
66 uint16_t huff_code_dc_luminance[12];
67 uint8_t huff_size_dc_chrominance[12];
68 uint16_t huff_code_dc_chrominance[12];
70 uint8_t huff_size_ac_luminance[256];
71 uint16_t huff_code_ac_luminance[256];
72 uint8_t huff_size_ac_chrominance[256];
73 uint16_t huff_code_ac_chrominance[256];
74 } MJpegContext;
76 /// The get_pixels() routine to use. The real routine comes from dsputil
77 static void (*get_pixels)(DCTELEM *restrict block, const uint8_t *pixels, int line_size);
79 /* Begin excessive code duplication ************************************/
80 /* Code coming from mpegvideo.c and mjpeg.c in ../libavcodec ***********/
82 /// copy of the table in mpegvideo.c
83 static const unsigned short aanscales[64] = {
84 /**< precomputed values scaled up by 14 bits */
85 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
86 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
87 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
88 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
89 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
90 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
91 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446,
92 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247
95 /// Precompute DCT quantizing matrix
96 /**
97 * This routine will precompute the combined DCT matrix with qscale
98 * and DCT renorm needed by the MPEG encoder here. It is basically the
99 * same as the routine with the same name in mpegvideo.c, except for
100 * some coefficient changes. The matrix will be computed in two variations,
101 * depending on the DCT version used. The second used by the MMX version of DCT.
103 * \param s MpegEncContext pointer
104 * \param qmat[OUT] pointer to where the matrix is stored
105 * \param qmat16[OUT] pointer to where matrix for MMX is stored.
106 * This matrix is not permutated
107 * and second 64 entries are bias
108 * \param quant_matrix[IN] the quantizion matrix to use
109 * \param bias bias for the quantizer
110 * \param qmin minimum qscale value to set up for
111 * \param qmax maximum qscale value to set up for
113 * Only rows between qmin and qmax will be populated in the matrix.
114 * In this MJPEG encoder, only the value 8 for qscale is used.
116 static void convert_matrix(MpegEncContext *s, int (*qmat)[64],
117 uint16_t (*qmat16)[2][64], const uint16_t *quant_matrix,
118 int bias, int qmin, int qmax) {
119 int qscale;
121 for(qscale = qmin; qscale <= qmax; qscale++) {
122 int i;
123 if (s->dsp.fdct == ff_jpeg_fdct_islow) {
124 for (i = 0; i < 64; i++) {
125 const int j = s->dsp.idct_permutation[i];
126 /* 16 <= qscale * quant_matrix[i] <= 7905
127 * 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026
128 * (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i])
129 * >= (1<<36)/249205026
130 * 3444240 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */
131 qmat[qscale][i] = (int)((UINT64_C(1) <<
132 (QMAT_SHIFT-3))/
133 (qscale*quant_matrix[j]));
135 } else if (s->dsp.fdct == fdct_ifast) {
136 for (i = 0; i < 64; i++) {
137 const int j = s->dsp.idct_permutation[i];
138 /* 16 <= qscale * quant_matrix[i] <= 7905
139 * 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026
140 * (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i])
141 * >= (1<<36)/249205026
142 * 3444240 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */
143 qmat[qscale][i] = (int)((UINT64_C(1) <<
144 (QMAT_SHIFT + 11))/(aanscales[i]
145 *qscale * quant_matrix[j]));
147 } else {
148 for (i = 0; i < 64; i++) {
149 const int j = s->dsp.idct_permutation[i];
150 /* We can safely assume that 16 <= quant_matrix[i] <= 255
151 * So 16 <= qscale * quant_matrix[i] <= 7905
152 * so (1<<19) / 16 >= (1<<19) / (qscale * quant_matrix[i]) >= (1<<19) / 7905
153 * so 32768 >= (1<<19) / (qscale * quant_matrix[i]) >= 67 */
154 qmat[qscale][i] = (int)((UINT64_C(1) <<
155 QMAT_SHIFT_MMX) / (qscale
156 *quant_matrix[j]));
157 qmat16[qscale][0][i] = (1 << QMAT_SHIFT_MMX)
158 /(qscale * quant_matrix[j]);
160 if (qmat16[qscale][0][i] == 0 ||
161 qmat16[qscale][0][i] == 128*256)
162 qmat16[qscale][0][i]=128*256-1;
163 qmat16[qscale][1][i]=ROUNDED_DIV(bias
164 <<(16-QUANT_BIAS_SHIFT),
165 qmat16[qscale][0][i]);
171 /// Emit the DC value into a MJPEG code sream
173 * This routine is only intended to be used from encode_block
175 * \param s pointer to MpegEncContext structure
176 * \param val the DC value to emit
177 * \param huff_size pointer to huffman code size array
178 * \param huff_code pointer to the code array corresponding to \a huff_size
180 * This routine is a clone of mjpeg_encode_dc
182 static inline void encode_dc(MpegEncContext *s, int val,
183 uint8_t *huff_size, uint16_t *huff_code) {
184 int mant, nbits;
186 if (val == 0) {
187 put_bits(&s->pb, huff_size[0], huff_code[0]);
188 } else {
189 mant = val;
190 if (val < 0) {
191 val = -val;
192 mant--;
194 nbits= av_log2_16bit(val) + 1;
195 put_bits(&s->pb, huff_size[nbits], huff_code[nbits]);
196 put_bits(&s->pb, nbits, mant & ((1 << nbits) - 1));
200 /// Huffman encode and emit one DCT block into the MJPEG code stream
202 * \param s pointer to MpegEncContext structure
203 * \param block pointer to the DCT block to emit
204 * \param n
206 * This routine is a duplicate of encode_block in mjpeg.c
208 static void encode_block(MpegEncContext *s, DCTELEM *block, int n) {
209 int mant, nbits, code, i, j;
210 int component, dc, run, last_index, val;
211 MJpegContext *m = s->mjpeg_ctx;
212 uint8_t *huff_size_ac;
213 uint16_t *huff_code_ac;
215 /* DC coef */
216 component = (n <= 3 ? 0 : n - 4 + 1);
217 dc = block[0]; /* overflow is impossible */
218 val = dc - s->last_dc[component];
219 if (n < 4) {
220 encode_dc(s, val, m->huff_size_dc_luminance,
221 m->huff_code_dc_luminance);
222 huff_size_ac = m->huff_size_ac_luminance;
223 huff_code_ac = m->huff_code_ac_luminance;
224 } else {
225 encode_dc(s, val, m->huff_size_dc_chrominance,
226 m->huff_code_dc_chrominance);
227 huff_size_ac = m->huff_size_ac_chrominance;
228 huff_code_ac = m->huff_code_ac_chrominance;
230 s->last_dc[component] = dc;
232 /* AC coefs */
234 run = 0;
235 last_index = s->block_last_index[n];
236 for (i = 1; i <= last_index; i++) {
237 j = s->intra_scantable.permutated[i];
238 val = block[j];
239 if (val == 0) run++;
240 else {
241 while (run >= 16) {
242 put_bits(&s->pb, huff_size_ac[0xf0],
243 huff_code_ac[0xf0]);
244 run -= 16;
246 mant = val;
247 if (val < 0) {
248 val = -val;
249 mant--;
252 nbits= av_log2_16bit(val) + 1;
253 code = (run << 4) | nbits;
255 put_bits(&s->pb, huff_size_ac[code],
256 huff_code_ac[code]);
257 put_bits(&s->pb, nbits, mant & ((1 << nbits) - 1));
258 run = 0;
262 /* output EOB only if not already 64 values */
263 if (last_index < 63 || run != 0)
264 put_bits(&s->pb, huff_size_ac[0], huff_code_ac[0]);
267 /// clip overflowing DCT coefficients
269 * If the computed DCT coefficients in a block overflow, this routine
270 * will go through them and clip them to be in the valid range.
272 * \param s pointer to MpegEncContext
273 * \param block pointer to DCT block to process
274 * \param last_index index of the last non-zero coefficient in block
276 * The max and min level, which are clipped to, are stored in
277 * s->min_qcoeff and s->max_qcoeff respectively.
279 static inline void clip_coeffs(MpegEncContext *s, DCTELEM *block,
280 int last_index) {
281 int i;
282 const int maxlevel= s->max_qcoeff;
283 const int minlevel= s->min_qcoeff;
285 for (i = 0; i <= last_index; i++) {
286 const int j = s->intra_scantable.permutated[i];
287 int level = block[j];
289 if (level > maxlevel) level=maxlevel;
290 else if(level < minlevel) level=minlevel;
291 block[j]= level;
295 /* End excessive code duplication **************************************/
297 typedef struct {
298 struct MpegEncContext *s;
299 int cheap_upsample;
300 int bw;
301 int y_rs;
302 int u_rs;
303 int v_rs;
304 } jpeg_enc_t;
306 // Huffman encode and emit one MCU of MJPEG code
308 * \param j pointer to jpeg_enc_t structure
310 * This function huffman encodes one MCU, and emits the
311 * resulting bitstream into the MJPEG code that is currently worked on.
313 * this function is a reproduction of the one in mjpeg, it includes two
314 * changes, it allows for black&white encoding (it skips the U and V
315 * macroblocks and it outputs the huffman code for 'no change' (dc) and
316 * 'all zero' (ac)) and it takes 4 macroblocks (422) instead of 6 (420)
318 static av_always_inline void zr_mjpeg_encode_mb(jpeg_enc_t *j) {
320 MJpegContext *m = j->s->mjpeg_ctx;
322 encode_block(j->s, j->s->block[0], 0);
323 encode_block(j->s, j->s->block[1], 1);
324 if (j->bw) {
325 /* U */
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]);
330 /* V */
331 put_bits(&j->s->pb, m->huff_size_dc_chrominance[0],
332 m->huff_code_dc_chrominance[0]);
333 put_bits(&j->s->pb, m->huff_size_ac_chrominance[0],
334 m->huff_code_ac_chrominance[0]);
335 } else {
336 /* we trick encode_block here so that it uses
337 * chrominance huffman tables instead of luminance ones
338 * (see the effect of second argument of encode_block) */
339 encode_block(j->s, j->s->block[2], 4);
340 encode_block(j->s, j->s->block[3], 5);
344 /// Fill one DCT MCU from planar storage
346 * This routine will convert one MCU from YUYV planar storage into 4
347 * DCT macro blocks, converting from 8-bit format in the planar
348 * storage to 16-bit format used in the DCT.
350 * \param j pointer to jpeg_enc structure, and also storage for DCT macro blocks
351 * \param x pixel x-coordinate for the first pixel
352 * \param y pixel y-coordinate for the first pixel
353 * \param y_data pointer to the Y plane
354 * \param u_data pointer to the U plane
355 * \param v_data pointer to the V plane
357 static av_always_inline void fill_block(jpeg_enc_t *j, int x, int y,
358 unsigned char *y_data, unsigned char *u_data,
359 unsigned char *v_data)
361 int i, k;
362 short int *dest;
363 unsigned char *source;
365 // The first Y, Y0
366 get_pixels(j->s->block[0], y*8*j->y_rs + 16*x + y_data, j->y_rs);
367 // The second Y, Y1
368 get_pixels(j->s->block[1], y*8*j->y_rs + 16*x + 8 + y_data, j->y_rs);
370 if (!j->bw && j->cheap_upsample) {
371 source = y * 4 * j->u_rs + 8*x + u_data;
372 dest = j->s->block[2];
373 for (i = 0; i < 4; i++) {
374 for (k = 0; k < 8; k++) {
375 dest[k] = source[k]; // First row
376 dest[k+8] = source[k]; // Duplicate to next row
379 dest += 16;
380 source += j->u_rs;
382 source = y * 4 * j->v_rs + 8*x + v_data;
383 dest = j->s->block[3];
384 for (i = 0; i < 4; i++) {
385 for (k = 0; k < 8; k++) {
386 dest[k] = source[k];
387 dest[k+8] = source[k];
389 dest += 16;
390 source += j->u_rs;
392 } else if (!j->bw && !j->cheap_upsample) {
393 // U
394 get_pixels(j->s->block[2], y*8*j->u_rs + 8*x + u_data, j->u_rs);
395 // V
396 get_pixels(j->s->block[3], y*8*j->v_rs + 8*x + v_data, j->v_rs);
401 * \brief initialize mjpeg encoder
403 * This routine is to set up the parameters and initialize the mjpeg encoder.
404 * It does all the initializations needed of lower level routines.
405 * The formats accepted by this encoder is YUV422P and YUV420
407 * \param w width in pixels of the image to encode, must be a multiple of 16
408 * \param h height in pixels of the image to encode, must be a multiple of 8
409 * \param y_rsize size of each plane row Y component
410 * \param y_rsize size of each plane row U component
411 * \param v_rsize size of each plane row V component
412 * \param cu "cheap upsample". Set to 0 for YUV422 format, 1 for YUV420 format
413 * when set to 1, the encoder will assume that there is only half th
414 * number of rows of chroma information, and every chroma row is
415 * duplicated.
416 * \param q quality parameter for the mjpeg encode. Between 1 and 20 where 1
417 * is best quality and 20 is the worst quality.
418 * \param b monochrome flag. When set to 1, the mjpeg output is monochrome.
419 * In that case, the colour information is omitted, and actually the
420 * colour planes are not touched.
422 * \returns an appropriately set up jpeg_enc_t structure
424 * The actual plane buffer addreses are passed by jpeg_enc_frame().
426 * The encoder doesn't know anything about interlacing, the halve height
427 * needs to be passed and the double rowstride. Which field gets encoded
428 * is decided by what buffers are passed to mjpeg_encode_frame()
430 static jpeg_enc_t *jpeg_enc_init(int w, int h, int y_rsize,
431 int u_rsize, int v_rsize,
432 int cu, int q, int b) {
433 jpeg_enc_t *j;
434 int i = 0;
435 VERBOSE("JPEG encoder init: %dx%d %d %d %d cu=%d q=%d bw=%d\n",
436 w, h, y_rsize, u_rsize, v_rsize, cu, q, b);
438 j = av_mallocz(sizeof(jpeg_enc_t));
439 if (j == NULL) return NULL;
441 j->s = av_mallocz(sizeof(MpegEncContext));
442 if (j->s == NULL) {
443 av_free(j);
444 return NULL;
447 /* info on how to access the pixels */
448 j->y_rs = y_rsize;
449 j->u_rs = u_rsize;
450 j->v_rs = v_rsize;
452 j->s->width = w; // image width and height
453 j->s->height = h;
454 j->s->qscale = q; // Encoding quality
456 j->s->mjpeg_data_only_frames = 0;
457 j->s->out_format = FMT_MJPEG;
458 j->s->intra_only = 1; // Generate only intra pictures for jpeg
459 j->s->encoding = 1; // Set mode to encode
460 j->s->pict_type = I_TYPE;
461 j->s->y_dc_scale = 8;
462 j->s->c_dc_scale = 8;
465 * This sets up the MCU (Minimal Code Unit) number
466 * of appearances of the various component
467 * for the SOF0 table in the generated MJPEG.
468 * The values are not used for anything else.
469 * The current setup is simply YUV422, with two horizontal Y components
470 * for every UV component.
472 j->s->mjpeg_write_tables = 1; // setup to write tables
473 j->s->mjpeg_vsample[0] = 1; // 1 appearance of Y vertically
474 j->s->mjpeg_vsample[1] = 1; // 1 appearance of U vertically
475 j->s->mjpeg_vsample[2] = 1; // 1 appearance of V vertically
476 j->s->mjpeg_hsample[0] = 2; // 2 appearances of Y horizontally
477 j->s->mjpeg_hsample[1] = 1; // 1 appearance of U horizontally
478 j->s->mjpeg_hsample[2] = 1; // 1 appearance of V horizontally
480 j->cheap_upsample = cu;
481 j->bw = b;
483 // Is this needed?
484 /* if libavcodec is used by the decoder then we must not
485 * initialize again, but if it is not initialized then we must
486 * initialize it here. */
487 if (!avcodec_inited) {
488 avcodec_init();
489 avcodec_register_all();
490 avcodec_inited=1;
493 // Build mjpeg huffman code tables, setting up j->s->mjpeg_ctx
494 if (mjpeg_init(j->s) < 0) {
495 av_free(j->s);
496 av_free(j);
497 return NULL;
500 /* alloc bogus avctx to keep MPV_common_init from segfaulting */
501 j->s->avctx = avcodec_alloc_context();
502 if (j->s->avctx == NULL) {
503 av_free(j->s);
504 av_free(j);
505 return NULL;
508 // Set some a minimum amount of default values that are needed
509 // Indicates that we should generated normal MJPEG
510 j->s->avctx->codec_id = CODEC_ID_MJPEG;
511 // Which DCT method to use. AUTO will select the fastest one
512 j->s->avctx->dct_algo = FF_DCT_AUTO;
513 j->s->intra_quant_bias= 1<<(QUANT_BIAS_SHIFT-1); //(a + x/2)/x
515 j->s->avctx->thread_count = 1;
517 /* make MPV_common_init allocate important buffers, like s->block
518 * Also initializes dsputil */
519 if (MPV_common_init(j->s) < 0) {
520 av_free(j->s);
521 av_free(j);
522 return NULL;
525 /* correct the value for sc->mb_height. MPV_common_init put other
526 * values there */
527 j->s->mb_height = j->s->height/8;
528 j->s->mb_intra = 1;
530 // Init q matrix
531 j->s->intra_matrix[0] = ff_mpeg1_default_intra_matrix[0];
532 for (i = 1; i < 64; i++)
533 j->s->intra_matrix[i] = av_clip_uint8(
534 (ff_mpeg1_default_intra_matrix[i]*j->s->qscale) >> 3);
536 // precompute matrix
537 convert_matrix(j->s, j->s->q_intra_matrix, j->s->q_intra_matrix16,
538 j->s->intra_matrix, j->s->intra_quant_bias, 8, 8);
540 /* Pick up the selection of the optimal get_pixels() routine
541 * to use, which was done in MPV_common_init() */
542 get_pixels = j->s->dsp.get_pixels;
544 return j;
548 * \brief mjpeg encode an image
550 * This routine will take a 3-plane YUV422 image and encoded it with MJPEG
551 * base line format, as suitable as input for the Zoran hardare MJPEG chips.
553 * It requires that the \a j parameter points the structure set up by the
554 * jpeg_enc_init() routine.
556 * \param j pointer to jpeg_enc_t structure as created by jpeg_enc_init()
557 * \param y_data pointer to Y component plane, packed one byte/pixel
558 * \param u_data pointer to U component plane, packed one byte per every
559 * other pixel
560 * \param v_data pointer to V component plane, packed one byte per every
561 * other pixel
562 * \param bufr pointer to the buffer where the mjpeg encoded code is stored
564 * \returns the number of bytes stored into \a bufr
566 * If \a j->s->mjpeg_write_tables is set, it will also emit the mjpeg tables,
567 * otherwise it will just emit the data. The \a j->s->mjpeg_write_tables
568 * variable will be reset to 0 by the routine.
570 static int jpeg_enc_frame(jpeg_enc_t *j, uint8_t *y_data,
571 uint8_t *u_data, uint8_t *v_data, uint8_t *bufr) {
572 int mb_x, mb_y, overflow;
573 /* initialize the buffer */
575 init_put_bits(&j->s->pb, bufr, 1024*256);
577 // Emit the mjpeg header blocks
578 mjpeg_picture_header(j->s);
580 j->s->header_bits = put_bits_count(&j->s->pb);
582 j->s->last_dc[0] = 128;
583 j->s->last_dc[1] = 128;
584 j->s->last_dc[2] = 128;
586 for (mb_y = 0; mb_y < j->s->mb_height; mb_y++) {
587 for (mb_x = 0; mb_x < j->s->mb_width; mb_x++) {
589 * Fill one DCT block (8x8 pixels) from
590 * 2 Y macroblocks and one U and one V
592 fill_block(j, mb_x, mb_y, y_data, u_data, v_data);
593 emms_c(); /* is this really needed? */
595 j->s->block_last_index[0] =
596 j->s->dct_quantize(j->s, j->s->block[0],
597 0, 8, &overflow);
598 if (overflow) clip_coeffs(j->s, j->s->block[0],
599 j->s->block_last_index[0]);
600 j->s->block_last_index[1] =
601 j->s->dct_quantize(j->s, j->s->block[1],
602 1, 8, &overflow);
603 if (overflow) clip_coeffs(j->s, j->s->block[1],
604 j->s->block_last_index[1]);
606 if (!j->bw) {
607 j->s->block_last_index[4] =
608 j->s->dct_quantize(j->s, j->s->block[2],
609 4, 8, &overflow);
610 if (overflow) clip_coeffs(j->s, j->s->block[2],
611 j->s->block_last_index[2]);
612 j->s->block_last_index[5] =
613 j->s->dct_quantize(j->s, j->s->block[3],
614 5, 8, &overflow);
615 if (overflow) clip_coeffs(j->s, j->s->block[3],
616 j->s->block_last_index[3]);
618 zr_mjpeg_encode_mb(j);
621 emms_c();
622 mjpeg_picture_trailer(j->s);
623 flush_put_bits(&j->s->pb);
625 if (j->s->mjpeg_write_tables == 1)
626 j->s->mjpeg_write_tables = 0;
628 return pbBufPtr(&(j->s->pb)) - j->s->pb.buf;
631 /// the real uninit routine
633 * This is the real routine that does the uninit of the ZRMJPEG filter
635 * \param j pointer to jpeg_enc structure
637 static void jpeg_enc_uninit(jpeg_enc_t *j) {
638 mjpeg_close(j->s);
639 av_free(j->s);
640 av_free(j);
643 /// Private structure for ZRMJPEG filter
644 struct vf_priv_s {
645 jpeg_enc_t *j;
646 unsigned char buf[256*1024];
647 int bw, fd, hdec, vdec;
648 int fields;
649 int y_stride;
650 int c_stride;
651 int quality;
652 int maxwidth;
653 int maxheight;
656 /// vf CONFIGURE entry point for the ZRMJPEG filter
658 * \param vf video filter instance pointer
659 * \param width image source width in pixels
660 * \param height image source height in pixels
661 * \param d_width width of requested window, just a hint
662 * \param d_height height of requested window, just a hint
663 * \param flags vf filter flags
664 * \param outfmt
666 * \returns returns 0 on error
668 * This routine will make the necessary hardware-related decisions for
669 * the ZRMJPEG filter, do the initialization of the MJPEG encoder, and
670 * then select one of the ZRJMJPEGIT or ZRMJPEGNI filters and then
671 * arrange to dispatch to the config() entry pointer for the one
672 * selected.
674 static int config(struct vf_instance_s* vf, int width, int height, int d_width,
675 int d_height, unsigned int flags, unsigned int outfmt){
676 struct vf_priv_s *priv = vf->priv;
677 float aspect_decision;
678 int stretchx, stretchy, err = 0, maxstretchx = 4;
679 priv->fields = 1;
681 VERBOSE("config() called\n");
683 if (priv->j) {
684 VERBOSE("re-configuring, resetting JPEG encoder\n");
685 jpeg_enc_uninit(priv->j);
686 priv->j = NULL;
689 aspect_decision = ((float)d_width/(float)d_height)/
690 ((float)width/(float)height);
692 if (aspect_decision > 1.8 && aspect_decision < 2.2) {
693 VERBOSE("should correct aspect by stretching x times 2, %d %d\n", 2*width, priv->maxwidth);
694 if (2*width <= priv->maxwidth) {
695 d_width = 2*width;
696 d_height = height;
697 maxstretchx = 2;
698 } else {
699 WARNING("unable to correct aspect by stretching, because resulting X will be too large, aspect correction by decimating y not yet implemented\n");
700 d_width = width;
701 d_height = height;
703 /* prestretch movie */
704 } else {
705 /* uncorrecting output for now */
706 d_width = width;
707 d_height = height;
709 /* make the scaling decision
710 * we are capable of stretching the image in the horizontal
711 * direction by factors 1, 2 and 4
712 * we can stretch the image in the vertical direction by a
713 * factor of 1 and 2 AND we must decide about interlacing */
714 if (d_width > priv->maxwidth/2 || height > priv->maxheight/2
715 || maxstretchx == 1) {
716 stretchx = 1;
717 stretchy = 1;
718 priv->fields = 2;
719 if (priv->vdec == 2) {
720 priv->fields = 1;
721 } else if (priv->vdec == 4) {
722 priv->fields = 1;
723 stretchy = 2;
725 if (priv->hdec > maxstretchx) {
726 if (priv->fd) {
727 WARNING("horizontal decimation too high, "
728 "changing to %d (use fd to keep"
729 " hdec=%d)\n",
730 maxstretchx, priv->hdec);
731 priv->hdec = maxstretchx;
734 stretchx = priv->hdec;
735 } else if (d_width > priv->maxwidth/4 ||
736 height > priv->maxheight/4 ||
737 maxstretchx == 2) {
738 stretchx = 2;
739 stretchy = 1;
740 priv->fields = 1;
741 if (priv->vdec == 2) {
742 stretchy = 2;
743 } else if (priv->vdec == 4) {
744 if (!priv->fd) {
745 WARNING("vertical decimation too high, "
746 "changing to 2 (use fd to keep "
747 "vdec=4)\n");
748 priv->vdec = 2;
750 stretchy = 2;
752 if (priv->hdec == 2) {
753 stretchx = 4;
754 } else if (priv->hdec == 4) {
755 if (priv->fd) {
756 WARNING("horizontal decimation too high, "
757 "changing to 2 (use fd to keep "
758 "hdec=4)\n");
759 priv->hdec = 2;
761 stretchx = 4;
763 } else {
764 /* output image is maximally stretched */
765 stretchx = 4;
766 stretchy = 2;
767 priv->fields = 1;
768 if (priv->vdec != 1 && !priv->fd) {
769 WARNING("vertical decimation too high, changing to 1 "
770 "(use fd to keep vdec=%d)\n",
771 priv->vdec);
772 priv->vdec = 1;
774 if (priv->hdec != 1 && !priv->fd) {
775 WARNING("horizontal decimation too high, changing to 1 (use fd to keep hdec=%d)\n", priv->hdec);
776 priv->hdec = 1;
780 VERBOSE("generated JPEG's %dx%s%d%s, stretched to %dx%d\n",
781 width/priv->hdec, (priv->fields == 2) ? "(" : "",
782 height/(priv->vdec*priv->fields),
783 (priv->fields == 2) ? "x2)" : "",
784 (width/priv->hdec)*stretchx,
785 (height/(priv->vdec*priv->fields))*
786 stretchy*priv->fields);
789 if ((width/priv->hdec)*stretchx > priv->maxwidth ||
790 (height/(priv->vdec*priv->fields))*
791 stretchy*priv->fields > priv->maxheight) {
792 ERROR("output dimensions too large (%dx%d), max (%dx%d) "
793 "insert crop to fix\n",
794 (width/priv->hdec)*stretchx,
795 (height/(priv->vdec*priv->fields))*
796 stretchy*priv->fields,
797 priv->maxwidth, priv->maxheight);
798 err = 1;
801 if (width%(16*priv->hdec) != 0) {
802 ERROR("width must be a multiple of 16*hdec (%d), use expand\n",
803 priv->hdec*16);
804 err = 1;
807 if (height%(8*priv->fields*priv->vdec) != 0) {
808 ERROR("height must be a multiple of 8*fields*vdec (%d),"
809 " use expand\n", priv->vdec*priv->fields*8);
810 err = 1;
813 if (err) return 0;
815 priv->y_stride = width;
816 priv->c_stride = width/2;
817 priv->j = jpeg_enc_init(width, height/priv->fields,
818 priv->fields*priv->y_stride,
819 priv->fields*priv->c_stride,
820 priv->fields*priv->c_stride,
821 1, priv->quality, priv->bw);
823 if (!priv->j) return 0;
824 return vf_next_config(vf, width, height, d_width, d_height, flags,
825 (priv->fields == 2) ? IMGFMT_ZRMJPEGIT : IMGFMT_ZRMJPEGNI);
828 /// put_image entrypoint for the ZRMJPEG vf filter
829 /***
830 * \param vf pointer to vf_instance
831 * \param mpi pointer to mp_image_t structure
832 * \param pts
834 static int put_image(struct vf_instance_s* vf, mp_image_t *mpi, double pts){
835 struct vf_priv_s *priv = vf->priv;
836 int size = 0;
837 int i;
838 mp_image_t* dmpi;
839 for (i = 0; i < priv->fields; i++)
840 size += jpeg_enc_frame(priv->j,
841 mpi->planes[0] + i*priv->y_stride,
842 mpi->planes[1] + i*priv->c_stride,
843 mpi->planes[2] + i*priv->c_stride,
844 priv->buf + size);
846 dmpi = vf_get_image(vf->next, IMGFMT_ZRMJPEGNI,
847 MP_IMGTYPE_EXPORT, 0, mpi->w, mpi->h);
848 dmpi->planes[0] = (uint8_t*)priv->buf;
849 dmpi->planes[1] = (uint8_t*)size;
850 return vf_next_put_image(vf,dmpi, pts);
853 /// query_format entrypoint for the ZRMJPEG vf filter
854 /***
855 * \param vf pointer to vf_instance
856 * \param fmt image format to query for
858 * \returns 0 if image format in fmt is not supported
860 * Given the image format specified by \a fmt, this routine is called
861 * to ask if the format is supported or not.
863 static int query_format(struct vf_instance_s* vf, unsigned int fmt){
864 VERBOSE("query_format() called\n");
866 switch (fmt) {
867 case IMGFMT_YV12:
868 case IMGFMT_YUY2:
869 /* strictly speaking the output format of
870 * this filter will be known after config(),
871 * but everything that supports IMGFMT_ZRMJPEGNI
872 * should also support all other IMGFMT_ZRMJPEG* */
873 return vf_next_query_format(vf, IMGFMT_ZRMJPEGNI);
876 return 0;
879 /// vf UNINIT entry point for the ZRMJPEG filter
881 * \param vf pointer to the vf instance structure
883 static void uninit(vf_instance_t *vf) {
884 struct vf_priv_s *priv = vf->priv;
885 VERBOSE("uninit() called\n");
886 if (priv->j) jpeg_enc_uninit(priv->j);
887 free(priv);
890 /// vf OPEN entry point for the ZRMJPEG filter
892 * \param vf pointer to the vf instance structure
893 * \param args the argument list string for the -vf zrmjpeg command
895 * \returns 0 for error, 1 for success
897 * This routine will do some basic initialization of local structures etc.,
898 * and then parse the command line arguments specific for the ZRMJPEG filter.
900 static int open(vf_instance_t *vf, char* args){
901 struct vf_priv_s *priv;
902 VERBOSE("open() called: args=\"%s\"\n", args);
904 vf->config = config;
905 vf->put_image = put_image;
906 vf->query_format = query_format;
907 vf->uninit = uninit;
909 priv = vf->priv = calloc(sizeof(*priv), 1);
910 if (!vf->priv) {
911 ERROR("out of memory error\n");
912 return 0;
915 /* maximum displayable size by zoran card, these defaults
916 * are for my own zoran card in PAL mode, these can be changed
917 * by filter options. But... in an ideal world these values would
918 * be queried from the vo device itself... */
919 priv->maxwidth = 768;
920 priv->maxheight = 576;
922 priv->quality = 2;
923 priv->hdec = 1;
924 priv->vdec = 1;
926 /* if libavcodec is already initialized, we must not initialize it
927 * again, but if it is not initialized then we mustinitialize it now. */
928 if (!avcodec_inited) {
929 /* we need to initialize libavcodec */
930 avcodec_init();
931 avcodec_register_all();
932 avcodec_inited=1;
935 if (args) {
936 char *arg, *tmp, *ptr, junk;
937 int last = 0, input;
939 /* save arguments, to be able to safely modify them */
940 arg = strdup(args);
941 if (!arg) {
942 ERROR("out of memory, this is bad\n");
943 return 0;
946 tmp = ptr = arg;
947 do {
948 while (*tmp != ':' && *tmp) tmp++;
949 if (*tmp == ':') *tmp++ = '\0';
950 else last = 1;
951 VERBOSE("processing filter option \"%s\"\n", ptr);
952 /* These options deal with the maximum output
953 * resolution of the zoran card. These should
954 * be queried from the vo device, but it is currently
955 * too difficult, so the user should tell the filter */
956 if (!strncmp("maxheight=", ptr, 10)) {
957 if (sscanf(ptr+10, "%d%c", &input, &junk) != 1)
958 ERROR(
959 "error parsing parameter to \"maxheight=\", \"%s\", ignoring\n"
960 , ptr + 10);
961 else {
962 priv->maxheight = input;
963 VERBOSE("setting maxheight to %d\n",
964 priv->maxheight);
966 } else if (!strncmp("quality=", ptr, 8)) {
967 if (sscanf(ptr+8, "%d%c", &input, &junk) != 1)
968 ERROR(
969 "error parsing parameter to \"quality=\", \"%s\", ignoring\n"
970 , ptr + 8);
971 else if (input < 1 || input > 20)
972 ERROR(
973 "parameter to \"quality=\" out of range (1..20), %d\n", input);
974 else {
975 priv->quality = input;
976 VERBOSE("setting JPEG quality to %d\n",
977 priv->quality);
979 } else if (!strncmp("maxwidth=", ptr, 9)) {
980 if (sscanf(ptr+9, "%d%c", &input, &junk) != 1)
981 ERROR(
982 "error parsing parameter to \"maxwidth=\", \"%s\", ignoring\n"
983 , ptr + 9);
984 else {
985 priv->maxwidth = input;
986 VERBOSE("setting maxwidth to %d\n",
987 priv->maxwidth);
989 } else if (!strncmp("hdec=", ptr, 5)) {
990 if (sscanf(ptr+5, "%d%c", &input, &junk) != 1)
991 ERROR(
992 "error parsing parameter to \"hdec=\", \"%s\", ignoring\n"
993 , ptr + 9);
994 else if (input != 1 && input != 2 && input != 4)
995 ERROR(
996 "illegal parameter to \"hdec=\", %d, should be 1, 2 or 4",
997 input);
998 else {
999 priv->hdec = input;
1000 VERBOSE(
1001 "setting horizontal decimation to %d\n", priv->maxwidth);
1003 } else if (!strncmp("vdec=", ptr, 5)) {
1004 if (sscanf(ptr+5, "%d%c", &input, &junk) != 1)
1005 ERROR(
1006 "error parsing parameter to \"vdec=\", \"%s\", ignoring\n"
1007 , ptr + 9);
1008 else if (input != 1 && input != 2 && input != 4)
1009 ERROR(
1010 "illegal parameter to \"vdec=\", %d, should be 1, 2 or 4",
1011 input);
1012 else {
1013 priv->vdec = input;
1014 VERBOSE(
1015 "setting vertical decimation to %d\n", priv->maxwidth);
1017 } else if (!strcasecmp("dc10+-PAL", ptr) ||
1018 !strcasecmp("dc10-PAL", ptr)) {
1019 priv->maxwidth = 768;
1020 priv->maxheight = 576;
1021 VERBOSE("setting DC10(+) PAL profile\n");
1022 } else if (!strcasecmp("fd", ptr)) {
1023 priv->fd = 1;
1024 VERBOSE("forcing decimation\n");
1025 } else if (!strcasecmp("nofd", ptr)) {
1026 priv->fd = 0;
1027 VERBOSE("decimate only if beautiful\n");
1028 } else if (!strcasecmp("bw", ptr)) {
1029 priv->bw = 1;
1030 VERBOSE("setting black and white encoding\n");
1031 } else if (!strcasecmp("color", ptr)) {
1032 priv->bw = 0;
1033 VERBOSE("setting color encoding\n");
1034 } else if (!strcasecmp("dc10+-NTSC", ptr) ||
1035 !strcasecmp("dc10-NTSC", ptr)) {
1036 priv->maxwidth = 640;
1037 priv->maxheight = 480;
1038 VERBOSE("setting DC10(+) NTSC profile\n");
1039 } else if (!strcasecmp("buz-PAL", ptr) ||
1040 !strcasecmp("lml33-PAL", ptr)) {
1041 priv->maxwidth = 720;
1042 priv->maxheight = 576;
1043 VERBOSE("setting buz/lml33 PAL profile\n");
1044 } else if (!strcasecmp("buz-NTSC", ptr) ||
1045 !strcasecmp("lml33-NTSC", ptr)) {
1046 priv->maxwidth = 720;
1047 priv->maxheight = 480;
1048 VERBOSE("setting buz/lml33 NTSC profile\n");
1049 } else {
1050 WARNING("ignoring unknown filter option "
1051 "\"%s\", or missing argument\n",
1052 ptr);
1054 ptr = tmp;
1055 } while (!last);
1057 free(arg);
1061 return 1;
1064 vf_info_t vf_info_zrmjpeg = {
1065 "realtime zoran MJPEG encoding",
1066 "zrmjpeg",
1067 "Rik Snel",
1069 open,
1070 NULL