2 * Copyright (C) 2005 Rik Snel <rsnel@cube.dyndns.org>, license GPL v2
3 * - based on vd_lavc.c by A'rpi (C) 2002-2003
4 * - parts from ffmpeg Copyright (c) 2000-2003 Fabrice Bellard
6 * This files includes a straightforward (to be) optimized JPEG encoder for
7 * the YUV422 format, based on mjpeg code from ffmpeg.
9 * For an excellent introduction to the JPEG format, see:
10 * http://www.ece.purdue.edu/~bouman/grad-labs/lab8/pdf/lab.pdf
20 #include "img_format.h"
25 #include "libvo/fastmemcpy.h"
28 /* We need this #define because we need ../libavcodec/common.h to #define
29 * be2me_32, otherwise the linker will complain that it doesn't exist */
30 #define HAVE_AV_CONFIG_H
31 #include "libavcodec/avcodec.h"
32 #include "libavcodec/dsputil.h"
33 #include "libavcodec/mpegvideo.h"
40 /* some convenient #define's, is this portable enough? */
41 #define VERBOSE(...) mp_msg(MSGT_DECVIDEO, MSGL_V, "vf_zrmjpeg: " __VA_ARGS__)
42 #define ERROR(...) mp_msg(MSGT_DECVIDEO, MSGL_ERR, "vf_zrmjpeg: " __VA_ARGS__)
43 #define WARNING(...) mp_msg(MSGT_DECVIDEO, MSGL_WARN, \
44 "vf_zrmjpeg: " __VA_ARGS__)
47 extern int avcodec_inited
;
49 /* zrmjpeg_encode_mb needs access to these tables for the black & white
51 typedef struct MJpegContext
{
52 uint8_t huff_size_dc_luminance
[12];
53 uint16_t huff_code_dc_luminance
[12];
54 uint8_t huff_size_dc_chrominance
[12];
55 uint16_t huff_code_dc_chrominance
[12];
57 uint8_t huff_size_ac_luminance
[256];
58 uint16_t huff_code_ac_luminance
[256];
59 uint8_t huff_size_ac_chrominance
[256];
60 uint16_t huff_code_ac_chrominance
[256];
63 /* Begin excessive code duplication ************************************/
64 /* Code coming from mpegvideo.c and mjpeg.c in ../libavcodec ***********/
66 static const unsigned short aanscales
[64] = {
67 /* precomputed values scaled up by 14 bits */
68 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
69 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
70 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
71 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
72 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
73 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
74 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446,
75 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247
78 static void convert_matrix(MpegEncContext
*s
, int (*qmat
)[64],
79 uint16_t (*qmat16
)[2][64], const uint16_t *quant_matrix
,
80 int bias
, int qmin
, int qmax
) {
83 for(qscale
= qmin
; qscale
<= qmax
; qscale
++) {
85 if (s
->dsp
.fdct
== ff_jpeg_fdct_islow
) {
86 for (i
= 0; i
< 64; i
++) {
87 const int j
= s
->dsp
.idct_permutation
[i
];
88 /* 16 <= qscale * quant_matrix[i] <= 7905
89 * 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026
90 * (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i])
91 * >= (1<<36)/249205026
92 * 3444240 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */
93 qmat
[qscale
][i
] = (int)((UINT64_C(1) <<
95 (qscale
*quant_matrix
[j
]));
97 } else if (s
->dsp
.fdct
== fdct_ifast
) {
98 for (i
= 0; i
< 64; i
++) {
99 const int j
= s
->dsp
.idct_permutation
[i
];
100 /* 16 <= qscale * quant_matrix[i] <= 7905
101 * 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026
102 * (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i])
103 * >= (1<<36)/249205026
104 * 3444240 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */
105 qmat
[qscale
][i
] = (int)((UINT64_C(1) <<
106 (QMAT_SHIFT
+ 11))/(aanscales
[i
]
107 *qscale
* quant_matrix
[j
]));
110 for (i
= 0; i
< 64; i
++) {
111 const int j
= s
->dsp
.idct_permutation
[i
];
112 /* We can safely assume that 16 <= quant_matrix[i] <= 255
113 * So 16 <= qscale * quant_matrix[i] <= 7905
114 * so (1<<19) / 16 >= (1<<19) / (qscale * quant_matrix[i]) >= (1<<19) / 7905
115 * so 32768 >= (1<<19) / (qscale * quant_matrix[i]) >= 67 */
116 qmat
[qscale
][i
] = (int)((uint64_t_C(1) <<
117 QMAT_SHIFT_MMX
) / (qscale
119 qmat16
[qscale
][0][i
] = (1 << QMAT_SHIFT_MMX
)
120 /(qscale
* quant_matrix
[j
]);
122 if (qmat16
[qscale
][0][i
] == 0 ||
123 qmat16
[qscale
][0][i
] == 128*256)
124 qmat16
[qscale
][0][i
]=128*256-1;
125 qmat16
[qscale
][1][i
]=ROUNDED_DIV(bias
126 <<(16-QUANT_BIAS_SHIFT
),
127 qmat16
[qscale
][0][i
]);
133 static inline void encode_dc(MpegEncContext
*s
, int val
,
134 uint8_t *huff_size
, uint16_t *huff_code
) {
138 put_bits(&s
->pb
, huff_size
[0], huff_code
[0]);
146 /* compute the log (XXX: optimize) */
153 put_bits(&s
->pb
, huff_size
[nbits
], huff_code
[nbits
]);
154 put_bits(&s
->pb
, nbits
, mant
& ((1 << nbits
) - 1));
158 static void encode_block(MpegEncContext
*s
, DCTELEM
*block
, int n
) {
159 int mant
, nbits
, code
, i
, j
;
160 int component
, dc
, run
, last_index
, val
;
161 MJpegContext
*m
= s
->mjpeg_ctx
;
162 uint8_t *huff_size_ac
;
163 uint16_t *huff_code_ac
;
166 component
= (n
<= 3 ? 0 : n
- 4 + 1);
167 dc
= block
[0]; /* overflow is impossible */
168 val
= dc
- s
->last_dc
[component
];
170 encode_dc(s
, val
, m
->huff_size_dc_luminance
,
171 m
->huff_code_dc_luminance
);
172 huff_size_ac
= m
->huff_size_ac_luminance
;
173 huff_code_ac
= m
->huff_code_ac_luminance
;
175 encode_dc(s
, val
, m
->huff_size_dc_chrominance
,
176 m
->huff_code_dc_chrominance
);
177 huff_size_ac
= m
->huff_size_ac_chrominance
;
178 huff_code_ac
= m
->huff_code_ac_chrominance
;
180 s
->last_dc
[component
] = dc
;
185 last_index
= s
->block_last_index
[n
];
186 for (i
= 1; i
<= last_index
; i
++) {
187 j
= s
->intra_scantable
.permutated
[i
];
192 put_bits(&s
->pb
, huff_size_ac
[0xf0],
202 /* compute the log (XXX: optimize) */
208 code
= (run
<< 4) | nbits
;
210 put_bits(&s
->pb
, huff_size_ac
[code
],
212 put_bits(&s
->pb
, nbits
, mant
& ((1 << nbits
) - 1));
217 /* output EOB only if not already 64 values */
218 if (last_index
< 63 || run
!= 0)
219 put_bits(&s
->pb
, huff_size_ac
[0], huff_code_ac
[0]);
222 static inline void clip_coeffs(MpegEncContext
*s
, DCTELEM
*block
,
225 const int maxlevel
= s
->max_qcoeff
;
226 const int minlevel
= s
->min_qcoeff
;
228 for (i
= 0; i
<= last_index
; i
++) {
229 const int j
= s
->intra_scantable
.permutated
[i
];
230 int level
= block
[j
];
232 if (level
> maxlevel
) level
=maxlevel
;
233 else if(level
< minlevel
) level
=minlevel
;
238 /* End excessive code duplication **************************************/
241 struct MpegEncContext
*s
;
252 /* this function is a reproduction of the one in mjpeg, it includes two
253 * changes, it allows for black&white encoding (it skips the U and V
254 * macroblocks and it outputs the huffman code for 'no change' (dc) and
255 * 'all zero' (ac)) and it takes 4 macroblocks (422) instead of 6 (420) */
256 static void zr_mjpeg_encode_mb(jpeg_enc_t
*j
) {
258 MJpegContext
*m
= j
->s
->mjpeg_ctx
;
260 encode_block(j
->s
, j
->s
->block
[0], 0);
261 encode_block(j
->s
, j
->s
->block
[1], 1);
264 put_bits(&j
->s
->pb
, m
->huff_size_dc_chrominance
[0],
265 m
->huff_code_dc_chrominance
[0]);
266 put_bits(&j
->s
->pb
, m
->huff_size_ac_chrominance
[0],
267 m
->huff_code_ac_chrominance
[0]);
269 put_bits(&j
->s
->pb
, m
->huff_size_dc_chrominance
[0],
270 m
->huff_code_dc_chrominance
[0]);
271 put_bits(&j
->s
->pb
, m
->huff_size_ac_chrominance
[0],
272 m
->huff_code_ac_chrominance
[0]);
274 /* we trick encode_block here so that it uses
275 * chrominance huffman tables instead of luminance ones
276 * (see the effect of second argument of encode_block) */
277 encode_block(j
->s
, j
->s
->block
[2], 4);
278 encode_block(j
->s
, j
->s
->block
[3], 5);
282 /* this function can take all kinds of YUV colorspaces
283 * YV12, YVYU, UYVY. The necesary parameters must be set up by the caller
284 * y_ps means "y pixel size", y_rs means "y row size".
285 * For YUYV, for example, is u_buf = y_buf + 1, v_buf = y_buf + 3,
286 * y_ps = 2, u_ps = 4, v_ps = 4, y_rs = u_rs = v_rs.
288 * The actual buffers must be passed with mjpeg_encode_frame, this is
289 * to make it possible to call encode on the buffer provided by the
290 * codec in draw_frame.
292 * The data is straightened out at the moment it is put in DCT
293 * blocks, there are therefore no spurious memcopies involved */
294 /* Notice that w must be a multiple of 16 and h must be a multiple of 8 */
295 /* We produce YUV422 jpegs, the colors must be subsampled horizontally,
296 * if the colors are also subsampled vertically, then this function
297 * performs cheap upsampling (better solution will be: a DCT that is
298 * optimized in the case that every two rows are the same) */
299 /* cu = 0 means 'No cheap upsampling'
300 * cu = 1 means 'perform cheap upsampling' */
301 /* The encoder doesn't know anything about interlacing, the halve height
302 * needs to be passed and the double rowstride. Which field gets encoded
303 * is decided by what buffers are passed to mjpeg_encode_frame */
304 static jpeg_enc_t
*jpeg_enc_init(int w
, int h
, int y_psize
, int y_rsize
,
305 int u_psize
, int u_rsize
, int v_psize
, int v_rsize
,
306 int cu
, int q
, int b
) {
309 VERBOSE("JPEG encoder init: %dx%d %d %d %d %d %d %d\n",
310 w
, h
, y_psize
, y_rsize
, u_psize
,
311 u_rsize
, v_psize
, v_rsize
);
313 j
= malloc(sizeof(jpeg_enc_t
));
314 if (j
== NULL
) return NULL
;
316 j
->s
= malloc(sizeof(MpegEncContext
));
317 memset(j
->s
,0x00,sizeof(MpegEncContext
));
323 /* info on how to access the pixels */
335 j
->s
->mjpeg_data_only_frames
= 0;
336 j
->s
->out_format
= FMT_MJPEG
;
337 j
->s
->intra_only
= 1;
339 j
->s
->pict_type
= I_TYPE
;
340 j
->s
->y_dc_scale
= 8;
341 j
->s
->c_dc_scale
= 8;
343 j
->s
->mjpeg_write_tables
= 1;
344 j
->s
->mjpeg_vsample
[0] = 1;
345 j
->s
->mjpeg_vsample
[1] = 1;
346 j
->s
->mjpeg_vsample
[2] = 1;
347 j
->s
->mjpeg_hsample
[0] = 2;
348 j
->s
->mjpeg_hsample
[1] = 1;
349 j
->s
->mjpeg_hsample
[2] = 1;
351 j
->cheap_upsample
= cu
;
354 if (mjpeg_init(j
->s
) < 0) {
360 /* alloc bogus avctx to keep MPV_common_init from segfaulting */
361 j
->s
->avctx
= calloc(sizeof(*j
->s
->avctx
), 1);
363 /* make MPV_common_init allocate important buffers, like s->block */
364 j
->s
->avctx
->thread_count
= 1;
366 if (MPV_common_init(j
->s
) < 0) {
372 /* correct the value for sc->mb_height */
373 j
->s
->mb_height
= j
->s
->height
/8;
376 j
->s
->intra_matrix
[0] = ff_mpeg1_default_intra_matrix
[0];
377 for (i
= 1; i
< 64; i
++)
378 j
->s
->intra_matrix
[i
] = clip_uint8(
379 (ff_mpeg1_default_intra_matrix
[i
]*j
->s
->qscale
) >> 3);
380 convert_matrix(j
->s
, j
->s
->q_intra_matrix
, j
->s
->q_intra_matrix16
,
381 j
->s
->intra_matrix
, j
->s
->intra_quant_bias
, 8, 8);
385 static int jpeg_enc_frame(jpeg_enc_t
*j
, unsigned char *y_data
,
386 unsigned char *u_data
, unsigned char *v_data
, char *bufr
) {
387 int i
, k
, mb_x
, mb_y
, overflow
;
389 unsigned char *source
;
390 /* initialize the buffer */
392 init_put_bits(&j
->s
->pb
, bufr
, 1024*256);
394 mjpeg_picture_header(j
->s
);
396 j
->s
->header_bits
= put_bits_count(&j
->s
->pb
);
398 j
->s
->last_dc
[0] = 128;
399 j
->s
->last_dc
[1] = 128;
400 j
->s
->last_dc
[2] = 128;
402 for (mb_y
= 0; mb_y
< j
->s
->mb_height
; mb_y
++) {
403 for (mb_x
= 0; mb_x
< j
->s
->mb_width
; mb_x
++) {
404 /* conversion 8 to 16 bit and filling of blocks
405 * must be mmx optimized */
406 /* fill 2 Y macroblocks and one U and one V */
407 source
= mb_y
* 8 * j
->y_rs
+
408 16 * j
->y_ps
* mb_x
+ y_data
;
409 dest
= j
->s
->block
[0];
410 for (i
= 0; i
< 8; i
++) {
411 for (k
= 0; k
< 8; k
++) {
412 dest
[k
] = source
[k
*j
->y_ps
];
417 source
= mb_y
* 8 * j
->y_rs
+
418 (16*mb_x
+ 8)*j
->y_ps
+ y_data
;
419 dest
= j
->s
->block
[1];
420 for (i
= 0; i
< 8; i
++) {
421 for (k
= 0; k
< 8; k
++) {
422 dest
[k
] = source
[k
*j
->y_ps
];
427 if (!j
->bw
&& j
->cheap_upsample
) {
428 source
= mb_y
*4*j
->u_rs
+
429 8*mb_x
*j
->u_ps
+ u_data
;
430 dest
= j
->s
->block
[2];
431 for (i
= 0; i
< 4; i
++) {
432 for (k
= 0; k
< 8; k
++) {
433 dest
[k
] = source
[k
*j
->u_ps
];
434 dest
[k
+8] = source
[k
*j
->u_ps
];
439 source
= mb_y
*4*j
->v_rs
+
440 8*mb_x
*j
->v_ps
+ v_data
;
441 dest
= j
->s
->block
[3];
442 for (i
= 0; i
< 4; i
++) {
443 for (k
= 0; k
< 8; k
++) {
444 dest
[k
] = source
[k
*j
->v_ps
];
445 dest
[k
+8] = source
[k
*j
->v_ps
];
450 } else if (!j
->bw
&& !j
->cheap_upsample
) {
451 source
= mb_y
*8*j
->u_rs
+
452 8*mb_x
*j
->u_ps
+ u_data
;
453 dest
= j
->s
->block
[2];
454 for (i
= 0; i
< 8; i
++) {
455 for (k
= 0; k
< 8; k
++)
456 dest
[k
] = source
[k
*j
->u_ps
];
460 source
= mb_y
*8*j
->v_rs
+
461 8*mb_x
*j
->v_ps
+ v_data
;
462 dest
= j
->s
->block
[3];
463 for (i
= 0; i
< 8; i
++) {
464 for (k
= 0; k
< 8; k
++)
465 dest
[k
] = source
[k
*j
->v_ps
];
470 emms_c(); /* is this really needed? */
472 j
->s
->block_last_index
[0] =
473 j
->s
->dct_quantize(j
->s
, j
->s
->block
[0],
475 if (overflow
) clip_coeffs(j
->s
, j
->s
->block
[0],
476 j
->s
->block_last_index
[0]);
477 j
->s
->block_last_index
[1] =
478 j
->s
->dct_quantize(j
->s
, j
->s
->block
[1],
480 if (overflow
) clip_coeffs(j
->s
, j
->s
->block
[1],
481 j
->s
->block_last_index
[1]);
484 j
->s
->block_last_index
[4] =
485 j
->s
->dct_quantize(j
->s
, j
->s
->block
[2],
487 if (overflow
) clip_coeffs(j
->s
, j
->s
->block
[2],
488 j
->s
->block_last_index
[2]);
489 j
->s
->block_last_index
[5] =
490 j
->s
->dct_quantize(j
->s
, j
->s
->block
[3],
492 if (overflow
) clip_coeffs(j
->s
, j
->s
->block
[3],
493 j
->s
->block_last_index
[3]);
495 zr_mjpeg_encode_mb(j
);
499 mjpeg_picture_trailer(j
->s
);
500 flush_put_bits(&j
->s
->pb
);
502 if (j
->s
->mjpeg_write_tables
== 1)
503 j
->s
->mjpeg_write_tables
= 0;
505 return pbBufPtr(&(j
->s
->pb
)) - j
->s
->pb
.buf
;
508 static void jpeg_enc_uninit(jpeg_enc_t
*j
) {
516 unsigned char buf
[256*1024];
517 int bw
, fd
, hdec
, vdec
;
526 static int config(struct vf_instance_s
* vf
, int width
, int height
, int d_width
,
527 int d_height
, unsigned int flags
, unsigned int outfmt
){
528 struct vf_priv_s
*priv
= vf
->priv
;
529 float aspect_decision
;
530 int stretchx
, stretchy
, err
= 0, maxstretchx
= 4;
533 VERBOSE("config() called\n");
536 VERBOSE("re-configuring, resetting JPEG encoder\n");
537 jpeg_enc_uninit(priv
->j
);
541 aspect_decision
= ((float)d_width
/(float)d_height
)/
542 ((float)width
/(float)height
);
544 if (aspect_decision
> 1.8 && aspect_decision
< 2.2) {
545 VERBOSE("should correct aspect by stretching x times 2, %d %d\n", 2*width
, priv
->maxwidth
);
546 if (2*width
<= priv
->maxwidth
) {
551 WARNING("unable to correct aspect by stretching, because resulting X will be too large, aspect correction by decimating y not yet implemented\n");
555 /* prestretch movie */
557 /* uncorrecting output for now */
561 /* make the scaling decision
562 * we are capable of stretching the image in the horizontal
563 * direction by factors 1, 2 and 4
564 * we can stretch the image in the vertical direction by a
565 * factor of 1 and 2 AND we must decide about interlacing */
566 if (d_width
> priv
->maxwidth
/2 || height
> priv
->maxheight
/2
567 || maxstretchx
== 1) {
571 if (priv
->vdec
== 2) {
573 } else if (priv
->vdec
== 4) {
577 if (priv
->hdec
> maxstretchx
) {
579 WARNING("horizontal decimation too high, changing to %d (use fd to keep hdec=%d)\n", maxstretchx
, priv
->hdec
);
580 priv
->hdec
= maxstretchx
;
583 stretchx
= priv
->hdec
;
584 } else if (d_width
> priv
->maxwidth
/4 ||
585 height
> priv
->maxheight
/4 ||
590 if (priv
->vdec
== 2) {
592 } else if (priv
->vdec
== 4) {
594 WARNING("vertical decimation too high, changing to 2 (use fd to keep vdec=4)\n");
599 if (priv
->hdec
== 2) {
601 } else if (priv
->hdec
== 4) {
603 WARNING("horizontal decimation too high, changing to 2 (use fd to keep hdec=4)\n");
609 /* output image is maximally stretched */
613 if (priv
->vdec
!= 1 && !priv
->fd
) {
614 WARNING("vertical decimation too high, changing to 1 (use fd to keep vdec=%d)\n", priv
->vdec
);
617 if (priv
->hdec
!= 1 && !priv
->fd
) {
618 WARNING("horizontal decimation too high, changing to 1 (use fd to keep hdec=%d)\n", priv
->hdec
);
623 VERBOSE("generated JPEG's %dx%s%d%s, stretched to %dx%d\n",
624 width
/priv
->hdec
, (priv
->fields
== 2) ? "(" : "",
625 height
/(priv
->vdec
*priv
->fields
),
626 (priv
->fields
== 2) ? "x2)" : "",
627 (width
/priv
->hdec
)*stretchx
,
628 (height
/(priv
->vdec
*priv
->fields
))*
629 stretchy
*priv
->fields
);
632 if ((width
/priv
->hdec
)*stretchx
> priv
->maxwidth
||
633 (height
/(priv
->vdec
*priv
->fields
))*
634 stretchy
*priv
->fields
> priv
->maxheight
) {
635 ERROR("output dimensions too large (%dx%d), max (%dx%d) insert crop to fix\n", (width
/priv
->hdec
)*stretchx
, (height
/(priv
->vdec
*priv
->fields
))*stretchy
*priv
->fields
, priv
->maxwidth
, priv
->maxheight
);
639 if (width
%(16*priv
->hdec
) != 0) {
640 ERROR("width must be a multiple of 16*hdec (%d), use expand\n",
645 if (height
%(8*priv
->fields
*priv
->vdec
) != 0) {
646 ERROR("height must be a multiple of 8*fields*vdec (%d),"
647 " use expand\n", priv
->vdec
*priv
->fields
*8);
653 priv
->y_stride
= width
;
654 priv
->c_stride
= width
/2;
655 priv
->j
= jpeg_enc_init(width
, height
/priv
->fields
, 1,
656 priv
->fields
*priv
->y_stride
, 1,
657 priv
->fields
*priv
->c_stride
, 1,
658 priv
->fields
*priv
->c_stride
, 1,
659 priv
->quality
, priv
->bw
);
661 if (!priv
->j
) return 0;
662 return vf_next_config(vf
, width
, height
, d_width
, d_height
, flags
,
663 (priv
->fields
== 2) ? IMGFMT_ZRMJPEGIT
: IMGFMT_ZRMJPEGNI
);
666 static int put_image(struct vf_instance_s
* vf
, mp_image_t
*mpi
, double pts
){
667 struct vf_priv_s
*priv
= vf
->priv
;
671 for (i
= 0; i
< priv
->fields
; i
++)
672 size
+= jpeg_enc_frame(priv
->j
,
673 mpi
->planes
[0] + i
*priv
->y_stride
,
674 mpi
->planes
[1] + i
*priv
->c_stride
,
675 mpi
->planes
[2] + i
*priv
->c_stride
,
678 dmpi
= vf_get_image(vf
->next
, IMGFMT_ZRMJPEGNI
,
679 MP_IMGTYPE_EXPORT
, 0, mpi
->w
, mpi
->h
);
680 dmpi
->planes
[0] = (uint8_t*)priv
->buf
;
681 dmpi
->planes
[1] = (uint8_t*)size
;
682 return vf_next_put_image(vf
,dmpi
, pts
);
685 static int query_format(struct vf_instance_s
* vf
, unsigned int fmt
){
686 VERBOSE("query_format() called\n");
691 /* strictly speaking the output format of
692 * this filter will be known after config(),
693 * but everything that supports IMGFMT_ZRMJPEGNI
694 * should also support all other IMGFMT_ZRMJPEG* */
695 return vf_next_query_format(vf
, IMGFMT_ZRMJPEGNI
);
701 static void uninit(vf_instance_t
*vf
) {
702 struct vf_priv_s
*priv
= vf
->priv
;
703 VERBOSE("uninit() called\n");
704 if (priv
->j
) jpeg_enc_uninit(priv
->j
);
708 static int open(vf_instance_t
*vf
, char* args
){
709 struct vf_priv_s
*priv
;
710 VERBOSE("open() called: args=\"%s\"\n", args
);
713 vf
->put_image
= put_image
;
714 vf
->query_format
= query_format
;
717 priv
= vf
->priv
= calloc(sizeof(*priv
), 1);
719 ERROR("out of memory error\n");
723 /* maximum displayable size by zoran card, these defaults
724 * are for my own zoran card in PAL mode, these can be changed
725 * by filter options. But... in an ideal world these values would
726 * be queried from the vo device itself... */
727 priv
->maxwidth
= 768;
728 priv
->maxheight
= 576;
734 /* if libavcodec is already initialized, we must not initialize it
735 * again, but if it is not initialized then we mustinitialize it now. */
736 if (!avcodec_inited
) {
737 /* we need to initialize libavcodec */
739 avcodec_register_all();
744 char *arg
, *tmp
, *ptr
, junk
;
747 /* save arguments, to be able to safely modify them */
750 ERROR("out of memory, this is bad\n");
756 while (*tmp
!= ':' && *tmp
) tmp
++;
757 if (*tmp
== ':') *tmp
++ = '\0';
759 VERBOSE("processing filter option \"%s\"\n", ptr
);
760 /* These options deal with the maximum output
761 * resolution of the zoran card. These should
762 * be queried from the vo device, but it is currently
763 * too difficult, so the user should tell the filter */
764 if (!strncmp("maxheight=", ptr
, 10)) {
765 if (sscanf(ptr
+10, "%d%c", &input
, &junk
) != 1)
767 "error parsing parameter to \"maxheight=\", \"%s\", ignoring\n"
770 priv
->maxheight
= input
;
771 VERBOSE("setting maxheight to %d\n",
774 } else if (!strncmp("quality=", ptr
, 8)) {
775 if (sscanf(ptr
+8, "%d%c", &input
, &junk
) != 1)
777 "error parsing parameter to \"quality=\", \"%s\", ignoring\n"
779 else if (input
< 1 || input
> 20)
781 "parameter to \"quality=\" out of range (1..20), %d\n", input
);
783 priv
->quality
= input
;
784 VERBOSE("setting JPEG quality to %d\n",
787 } else if (!strncmp("maxwidth=", ptr
, 9)) {
788 if (sscanf(ptr
+9, "%d%c", &input
, &junk
) != 1)
790 "error parsing parameter to \"maxwidth=\", \"%s\", ignoring\n"
793 priv
->maxwidth
= input
;
794 VERBOSE("setting maxwidth to %d\n",
797 } else if (!strncmp("hdec=", ptr
, 5)) {
798 if (sscanf(ptr
+5, "%d%c", &input
, &junk
) != 1)
800 "error parsing parameter to \"hdec=\", \"%s\", ignoring\n"
802 else if (input
!= 1 && input
!= 2 && input
!= 4)
804 "illegal parameter to \"hdec=\", %d, should be 1, 2 or 4",
809 "setting horizontal decimation to %d\n", priv
->maxwidth
);
811 } else if (!strncmp("vdec=", ptr
, 5)) {
812 if (sscanf(ptr
+5, "%d%c", &input
, &junk
) != 1)
814 "error parsing parameter to \"vdec=\", \"%s\", ignoring\n"
816 else if (input
!= 1 && input
!= 2 && input
!= 4)
818 "illegal parameter to \"vdec=\", %d, should be 1, 2 or 4",
823 "setting vertical decimation to %d\n", priv
->maxwidth
);
825 } else if (!strcasecmp("dc10+-PAL", ptr
) ||
826 !strcasecmp("dc10-PAL", ptr
)) {
827 priv
->maxwidth
= 768;
828 priv
->maxheight
= 576;
829 VERBOSE("setting DC10(+) PAL profile\n");
830 } else if (!strcasecmp("fd", ptr
)) {
832 VERBOSE("forcing decimation\n");
833 } else if (!strcasecmp("nofd", ptr
)) {
835 VERBOSE("decimate only if beautiful\n");
836 } else if (!strcasecmp("bw", ptr
)) {
838 VERBOSE("setting black and white encoding\n");
839 } else if (!strcasecmp("color", ptr
)) {
841 VERBOSE("setting color encoding\n");
842 } else if (!strcasecmp("dc10+-NTSC", ptr
) ||
843 !strcasecmp("dc10-NTSC", ptr
)) {
844 priv
->maxwidth
= 640;
845 priv
->maxheight
= 480;
846 VERBOSE("setting DC10(+) NTSC profile\n");
847 } else if (!strcasecmp("buz-PAL", ptr
) ||
848 !strcasecmp("lml33-PAL", ptr
)) {
849 priv
->maxwidth
= 720;
850 priv
->maxheight
= 576;
851 VERBOSE("setting buz/lml33 PAL profile\n");
852 } else if (!strcasecmp("buz-NTSC", ptr
) ||
853 !strcasecmp("lml33-NTSC", ptr
)) {
854 priv
->maxwidth
= 720;
855 priv
->maxheight
= 480;
856 VERBOSE("setting buz/lml33 NTSC profile\n");
858 WARNING("ignoring unknown filter option "
859 "\"%s\", or missing argument\n",
872 vf_info_t vf_info_zrmjpeg
= {
873 "realtime zoran MJPEG encoding",