vo_gl: Some small whitespace and {} cosmetics
[mplayer/glamo.git] / libmpcodecs / vf_zrmjpeg.c
blob0b87d01ce894461ed1e471fd8874f27afb14c2d9
1 /*
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.
29 /**
30 * \file vf_zrmjpeg.c
32 * \brief Does mjpeg encoding as required by the zrmjpeg filter as well
33 * as by the zr video driver.
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <inttypes.h>
41 #include "config.h"
42 #include "mp_msg.h"
44 #include "img_format.h"
45 #include "mp_image.h"
46 #include "vf.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"
54 #undef malloc
55 #undef free
57 /* some convenient #define's, is this portable enough? */
58 /// Printout with vf_zrmjpeg: prefix at VERBOSE level
59 #define VERBOSE(...) mp_msg(MSGT_DECVIDEO, MSGL_V, "vf_zrmjpeg: " __VA_ARGS__)
60 /// Printout with vf_zrmjpeg: prefix at ERROR level
61 #define ERROR(...) mp_msg(MSGT_DECVIDEO, MSGL_ERR, "vf_zrmjpeg: " __VA_ARGS__)
62 /// Printout with vf_zrmjpeg: prefix at WARNING level
63 #define WARNING(...) mp_msg(MSGT_DECVIDEO, MSGL_WARN, \
64 "vf_zrmjpeg: " __VA_ARGS__)
66 // "local" flag in vd_ffmpeg.c. If not set, avcodec_init() et. al. need to be called
67 // set when init is done, so that initialization is not done twice.
68 extern int avcodec_initialized;
70 /// The get_pixels() routine to use. The real routine comes from dsputil
71 static void (*get_pixels)(DCTELEM *restrict block, const uint8_t *pixels, int line_size);
73 /* Begin excessive code duplication ************************************/
74 /* Code coming from mpegvideo.c and mjpeg.c in ../libavcodec ***********/
76 /// copy of the table in mpegvideo.c
77 static const unsigned short aanscales[64] = {
78 /**< precomputed values scaled up by 14 bits */
79 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
80 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
81 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
82 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
83 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
84 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
85 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446,
86 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247
89 /// Precompute DCT quantizing matrix
90 /**
91 * This routine will precompute the combined DCT matrix with qscale
92 * and DCT renorm needed by the MPEG encoder here. It is basically the
93 * same as the routine with the same name in mpegvideo.c, except for
94 * some coefficient changes. The matrix will be computed in two variations,
95 * depending on the DCT version used. The second used by the MMX version of DCT.
97 * \param s MpegEncContext pointer
98 * \param qmat[OUT] pointer to where the matrix is stored
99 * \param qmat16[OUT] pointer to where matrix for MMX is stored.
100 * This matrix is not permutated
101 * and second 64 entries are bias
102 * \param quant_matrix[IN] the quantizion matrix to use
103 * \param bias bias for the quantizer
104 * \param qmin minimum qscale value to set up for
105 * \param qmax maximum qscale value to set up for
107 * Only rows between qmin and qmax will be populated in the matrix.
108 * In this MJPEG encoder, only the value 8 for qscale is used.
110 static void convert_matrix(MpegEncContext *s, int (*qmat)[64],
111 uint16_t (*qmat16)[2][64], const uint16_t *quant_matrix,
112 int bias, int qmin, int qmax) {
113 int qscale;
115 for(qscale = qmin; qscale <= qmax; qscale++) {
116 int i;
117 if (s->dsp.fdct == ff_jpeg_fdct_islow) {
118 for (i = 0; i < 64; i++) {
119 const int j = s->dsp.idct_permutation[i];
120 /* 16 <= qscale * quant_matrix[i] <= 7905
121 * 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026
122 * (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i])
123 * >= (1<<36)/249205026
124 * 3444240 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */
125 qmat[qscale][i] = (int)((UINT64_C(1) <<
126 (QMAT_SHIFT-3))/
127 (qscale*quant_matrix[j]));
129 } else if (s->dsp.fdct == fdct_ifast) {
130 for (i = 0; i < 64; i++) {
131 const int j = s->dsp.idct_permutation[i];
132 /* 16 <= qscale * quant_matrix[i] <= 7905
133 * 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026
134 * (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i])
135 * >= (1<<36)/249205026
136 * 3444240 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */
137 qmat[qscale][i] = (int)((UINT64_C(1) <<
138 (QMAT_SHIFT + 11))/(aanscales[i]
139 *qscale * quant_matrix[j]));
141 } else {
142 for (i = 0; i < 64; i++) {
143 const int j = s->dsp.idct_permutation[i];
144 /* We can safely assume that 16 <= quant_matrix[i] <= 255
145 * So 16 <= qscale * quant_matrix[i] <= 7905
146 * so (1<<19) / 16 >= (1<<19) / (qscale * quant_matrix[i]) >= (1<<19) / 7905
147 * so 32768 >= (1<<19) / (qscale * quant_matrix[i]) >= 67 */
148 qmat[qscale][i] = (int)((UINT64_C(1) <<
149 QMAT_SHIFT_MMX) / (qscale
150 *quant_matrix[j]));
151 qmat16[qscale][0][i] = (1 << QMAT_SHIFT_MMX)
152 /(qscale * quant_matrix[j]);
154 if (qmat16[qscale][0][i] == 0 ||
155 qmat16[qscale][0][i] == 128*256)
156 qmat16[qscale][0][i]=128*256-1;
157 qmat16[qscale][1][i]=ROUNDED_DIV(bias
158 <<(16-QUANT_BIAS_SHIFT),
159 qmat16[qscale][0][i]);
165 /// Emit the DC value into a MJPEG code sream
167 * This routine is only intended to be used from encode_block
169 * \param s pointer to MpegEncContext structure
170 * \param val the DC value to emit
171 * \param huff_size pointer to huffman code size array
172 * \param huff_code pointer to the code array corresponding to \a huff_size
174 * This routine is a clone of mjpeg_encode_dc
176 static inline void encode_dc(MpegEncContext *s, int val,
177 uint8_t *huff_size, uint16_t *huff_code) {
178 int mant, nbits;
180 if (val == 0) {
181 put_bits(&s->pb, huff_size[0], huff_code[0]);
182 } else {
183 mant = val;
184 if (val < 0) {
185 val = -val;
186 mant--;
188 nbits= av_log2_16bit(val) + 1;
189 put_bits(&s->pb, huff_size[nbits], huff_code[nbits]);
190 put_bits(&s->pb, nbits, mant & ((1 << nbits) - 1));
194 /// Huffman encode and emit one DCT block into the MJPEG code stream
196 * \param s pointer to MpegEncContext structure
197 * \param block pointer to the DCT block to emit
198 * \param n
200 * This routine is a duplicate of encode_block in mjpeg.c
202 static void encode_block(MpegEncContext *s, DCTELEM *block, int n) {
203 int mant, nbits, code, i, j;
204 int component, dc, run, last_index, val;
205 MJpegContext *m = s->mjpeg_ctx;
206 uint8_t *huff_size_ac;
207 uint16_t *huff_code_ac;
209 /* DC coef */
210 component = (n <= 3 ? 0 : n - 4 + 1);
211 dc = block[0]; /* overflow is impossible */
212 val = dc - s->last_dc[component];
213 if (n < 4) {
214 encode_dc(s, val, m->huff_size_dc_luminance,
215 m->huff_code_dc_luminance);
216 huff_size_ac = m->huff_size_ac_luminance;
217 huff_code_ac = m->huff_code_ac_luminance;
218 } else {
219 encode_dc(s, val, m->huff_size_dc_chrominance,
220 m->huff_code_dc_chrominance);
221 huff_size_ac = m->huff_size_ac_chrominance;
222 huff_code_ac = m->huff_code_ac_chrominance;
224 s->last_dc[component] = dc;
226 /* AC coefs */
228 run = 0;
229 last_index = s->block_last_index[n];
230 for (i = 1; i <= last_index; i++) {
231 j = s->intra_scantable.permutated[i];
232 val = block[j];
233 if (val == 0) run++;
234 else {
235 while (run >= 16) {
236 put_bits(&s->pb, huff_size_ac[0xf0],
237 huff_code_ac[0xf0]);
238 run -= 16;
240 mant = val;
241 if (val < 0) {
242 val = -val;
243 mant--;
246 nbits= av_log2_16bit(val) + 1;
247 code = (run << 4) | nbits;
249 put_bits(&s->pb, huff_size_ac[code],
250 huff_code_ac[code]);
251 put_bits(&s->pb, nbits, mant & ((1 << nbits) - 1));
252 run = 0;
256 /* output EOB only if not already 64 values */
257 if (last_index < 63 || run != 0)
258 put_bits(&s->pb, huff_size_ac[0], huff_code_ac[0]);
261 /// clip overflowing DCT coefficients
263 * If the computed DCT coefficients in a block overflow, this routine
264 * will go through them and clip them to be in the valid range.
266 * \param s pointer to MpegEncContext
267 * \param block pointer to DCT block to process
268 * \param last_index index of the last non-zero coefficient in block
270 * The max and min level, which are clipped to, are stored in
271 * s->min_qcoeff and s->max_qcoeff respectively.
273 static inline void clip_coeffs(MpegEncContext *s, DCTELEM *block,
274 int last_index) {
275 int i;
276 const int maxlevel= s->max_qcoeff;
277 const int minlevel= s->min_qcoeff;
279 for (i = 0; i <= last_index; i++) {
280 const int j = s->intra_scantable.permutated[i];
281 int level = block[j];
283 if (level > maxlevel) level=maxlevel;
284 else if(level < minlevel) level=minlevel;
285 block[j]= level;
289 /* End excessive code duplication **************************************/
291 typedef struct {
292 struct MpegEncContext *s;
293 int cheap_upsample;
294 int bw;
295 int y_rs;
296 int u_rs;
297 int v_rs;
298 } jpeg_enc_t;
300 // Huffman encode and emit one MCU of MJPEG code
302 * \param j pointer to jpeg_enc_t structure
304 * This function huffman encodes one MCU, and emits the
305 * resulting bitstream into the MJPEG code that is currently worked on.
307 * this function is a reproduction of the one in mjpeg, it includes two
308 * changes, it allows for black&white encoding (it skips the U and V
309 * macroblocks and it outputs the huffman code for 'no change' (dc) and
310 * 'all zero' (ac)) and it takes 4 macroblocks (422) instead of 6 (420)
312 static av_always_inline void zr_mjpeg_encode_mb(jpeg_enc_t *j) {
314 MJpegContext *m = j->s->mjpeg_ctx;
316 encode_block(j->s, j->s->block[0], 0);
317 encode_block(j->s, j->s->block[1], 1);
318 if (j->bw) {
319 /* U */
320 put_bits(&j->s->pb, m->huff_size_dc_chrominance[0],
321 m->huff_code_dc_chrominance[0]);
322 put_bits(&j->s->pb, m->huff_size_ac_chrominance[0],
323 m->huff_code_ac_chrominance[0]);
324 /* V */
325 put_bits(&j->s->pb, m->huff_size_dc_chrominance[0],
326 m->huff_code_dc_chrominance[0]);
327 put_bits(&j->s->pb, m->huff_size_ac_chrominance[0],
328 m->huff_code_ac_chrominance[0]);
329 } else {
330 /* we trick encode_block here so that it uses
331 * chrominance huffman tables instead of luminance ones
332 * (see the effect of second argument of encode_block) */
333 encode_block(j->s, j->s->block[2], 4);
334 encode_block(j->s, j->s->block[3], 5);
338 /// Fill one DCT MCU from planar storage
340 * This routine will convert one MCU from YUYV planar storage into 4
341 * DCT macro blocks, converting from 8-bit format in the planar
342 * storage to 16-bit format used in the DCT.
344 * \param j pointer to jpeg_enc structure, and also storage for DCT macro blocks
345 * \param x pixel x-coordinate for the first pixel
346 * \param y pixel y-coordinate for the first pixel
347 * \param y_data pointer to the Y plane
348 * \param u_data pointer to the U plane
349 * \param v_data pointer to the V plane
351 static av_always_inline void fill_block(jpeg_enc_t *j, int x, int y,
352 unsigned char *y_data, unsigned char *u_data,
353 unsigned char *v_data)
355 int i, k;
356 short int *dest;
357 unsigned char *source;
359 // The first Y, Y0
360 get_pixels(j->s->block[0], y*8*j->y_rs + 16*x + y_data, j->y_rs);
361 // The second Y, Y1
362 get_pixels(j->s->block[1], y*8*j->y_rs + 16*x + 8 + y_data, j->y_rs);
364 if (!j->bw && j->cheap_upsample) {
365 source = y * 4 * j->u_rs + 8*x + u_data;
366 dest = j->s->block[2];
367 for (i = 0; i < 4; i++) {
368 for (k = 0; k < 8; k++) {
369 dest[k] = source[k]; // First row
370 dest[k+8] = source[k]; // Duplicate to next row
373 dest += 16;
374 source += j->u_rs;
376 source = y * 4 * j->v_rs + 8*x + v_data;
377 dest = j->s->block[3];
378 for (i = 0; i < 4; i++) {
379 for (k = 0; k < 8; k++) {
380 dest[k] = source[k];
381 dest[k+8] = source[k];
383 dest += 16;
384 source += j->u_rs;
386 } else if (!j->bw && !j->cheap_upsample) {
387 // U
388 get_pixels(j->s->block[2], y*8*j->u_rs + 8*x + u_data, j->u_rs);
389 // V
390 get_pixels(j->s->block[3], y*8*j->v_rs + 8*x + v_data, j->v_rs);
395 * \brief initialize mjpeg encoder
397 * This routine is to set up the parameters and initialize the mjpeg encoder.
398 * It does all the initializations needed of lower level routines.
399 * The formats accepted by this encoder is YUV422P and YUV420
401 * \param w width in pixels of the image to encode, must be a multiple of 16
402 * \param h height in pixels of the image to encode, must be a multiple of 8
403 * \param y_rsize size of each plane row Y component
404 * \param y_rsize size of each plane row U component
405 * \param v_rsize size of each plane row V component
406 * \param cu "cheap upsample". Set to 0 for YUV422 format, 1 for YUV420 format
407 * when set to 1, the encoder will assume that there is only half th
408 * number of rows of chroma information, and every chroma row is
409 * duplicated.
410 * \param q quality parameter for the mjpeg encode. Between 1 and 20 where 1
411 * is best quality and 20 is the worst quality.
412 * \param b monochrome flag. When set to 1, the mjpeg output is monochrome.
413 * In that case, the colour information is omitted, and actually the
414 * colour planes are not touched.
416 * \returns an appropriately set up jpeg_enc_t structure
418 * The actual plane buffer addreses are passed by jpeg_enc_frame().
420 * The encoder doesn't know anything about interlacing, the halve height
421 * needs to be passed and the double rowstride. Which field gets encoded
422 * is decided by what buffers are passed to mjpeg_encode_frame()
424 static jpeg_enc_t *jpeg_enc_init(int w, int h, int y_rsize,
425 int u_rsize, int v_rsize,
426 int cu, int q, int b) {
427 jpeg_enc_t *j;
428 int i = 0;
429 VERBOSE("JPEG encoder init: %dx%d %d %d %d cu=%d q=%d bw=%d\n",
430 w, h, y_rsize, u_rsize, v_rsize, cu, q, b);
432 j = av_mallocz(sizeof(jpeg_enc_t));
433 if (j == NULL) return NULL;
435 j->s = av_mallocz(sizeof(MpegEncContext));
436 if (j->s == NULL) {
437 av_free(j);
438 return NULL;
441 /* info on how to access the pixels */
442 j->y_rs = y_rsize;
443 j->u_rs = u_rsize;
444 j->v_rs = v_rsize;
446 j->s->width = w; // image width and height
447 j->s->height = h;
448 j->s->qscale = q; // Encoding quality
450 j->s->out_format = FMT_MJPEG;
451 j->s->intra_only = 1; // Generate only intra pictures for jpeg
452 j->s->encoding = 1; // Set mode to encode
453 j->s->pict_type = FF_I_TYPE;
454 j->s->y_dc_scale = 8;
455 j->s->c_dc_scale = 8;
458 * This sets up the MCU (Minimal Code Unit) number
459 * of appearances of the various component
460 * for the SOF0 table in the generated MJPEG.
461 * The values are not used for anything else.
462 * The current setup is simply YUV422, with two horizontal Y components
463 * for every UV component.
465 //FIXME j->s->mjpeg_write_tables = 1; // setup to write tables
466 j->s->mjpeg_vsample[0] = 1; // 1 appearance of Y vertically
467 j->s->mjpeg_vsample[1] = 1; // 1 appearance of U vertically
468 j->s->mjpeg_vsample[2] = 1; // 1 appearance of V vertically
469 j->s->mjpeg_hsample[0] = 2; // 2 appearances of Y horizontally
470 j->s->mjpeg_hsample[1] = 1; // 1 appearance of U horizontally
471 j->s->mjpeg_hsample[2] = 1; // 1 appearance of V horizontally
473 j->cheap_upsample = cu;
474 j->bw = b;
476 // Is this needed?
477 /* if libavcodec is used by the decoder then we must not
478 * initialize again, but if it is not initialized then we must
479 * initialize it here. */
480 if (!avcodec_initialized) {
481 avcodec_init();
482 avcodec_register_all();
483 avcodec_initialized=1;
486 // Build mjpeg huffman code tables, setting up j->s->mjpeg_ctx
487 if (ff_mjpeg_encode_init(j->s) < 0) {
488 av_free(j->s);
489 av_free(j);
490 return NULL;
493 /* alloc bogus avctx to keep MPV_common_init from segfaulting */
494 j->s->avctx = avcodec_alloc_context();
495 if (j->s->avctx == NULL) {
496 av_free(j->s);
497 av_free(j);
498 return NULL;
501 // Set some a minimum amount of default values that are needed
502 // Indicates that we should generated normal MJPEG
503 j->s->avctx->codec_id = CODEC_ID_MJPEG;
504 // Which DCT method to use. AUTO will select the fastest one
505 j->s->avctx->dct_algo = FF_DCT_AUTO;
506 j->s->intra_quant_bias= 1<<(QUANT_BIAS_SHIFT-1); //(a + x/2)/x
507 // indicate we 'decode' to jpeg 4:2:2
508 j->s->avctx->pix_fmt = PIX_FMT_YUVJ422P;
510 j->s->avctx->thread_count = 1;
512 /* make MPV_common_init allocate important buffers, like s->block
513 * Also initializes dsputil */
514 if (MPV_common_init(j->s) < 0) {
515 av_free(j->s);
516 av_free(j);
517 return NULL;
520 /* correct the value for sc->mb_height. MPV_common_init put other
521 * values there */
522 j->s->mb_height = j->s->height/8;
523 j->s->mb_intra = 1;
525 // Init q matrix
526 j->s->intra_matrix[0] = ff_mpeg1_default_intra_matrix[0];
527 for (i = 1; i < 64; i++)
528 j->s->intra_matrix[i] = av_clip_uint8(
529 (ff_mpeg1_default_intra_matrix[i]*j->s->qscale) >> 3);
531 // precompute matrix
532 convert_matrix(j->s, j->s->q_intra_matrix, j->s->q_intra_matrix16,
533 j->s->intra_matrix, j->s->intra_quant_bias, 8, 8);
535 /* Pick up the selection of the optimal get_pixels() routine
536 * to use, which was done in MPV_common_init() */
537 get_pixels = j->s->dsp.get_pixels;
539 return j;
543 * \brief mjpeg encode an image
545 * This routine will take a 3-plane YUV422 image and encoded it with MJPEG
546 * base line format, as suitable as input for the Zoran hardare MJPEG chips.
548 * It requires that the \a j parameter points the structure set up by the
549 * jpeg_enc_init() routine.
551 * \param j pointer to jpeg_enc_t structure as created by jpeg_enc_init()
552 * \param y_data pointer to Y component plane, packed one byte/pixel
553 * \param u_data pointer to U component plane, packed one byte per every
554 * other pixel
555 * \param v_data pointer to V component plane, packed one byte per every
556 * other pixel
557 * \param bufr pointer to the buffer where the mjpeg encoded code is stored
559 * \returns the number of bytes stored into \a bufr
561 * If \a j->s->mjpeg_write_tables is set, it will also emit the mjpeg tables,
562 * otherwise it will just emit the data. The \a j->s->mjpeg_write_tables
563 * variable will be reset to 0 by the routine.
565 static int jpeg_enc_frame(jpeg_enc_t *j, uint8_t *y_data,
566 uint8_t *u_data, uint8_t *v_data, uint8_t *bufr) {
567 int mb_x, mb_y, overflow;
568 /* initialize the buffer */
570 init_put_bits(&j->s->pb, bufr, 1024*256);
572 // Emit the mjpeg header blocks
573 ff_mjpeg_encode_picture_header(j->s);
575 j->s->header_bits = put_bits_count(&j->s->pb);
577 j->s->last_dc[0] = 128;
578 j->s->last_dc[1] = 128;
579 j->s->last_dc[2] = 128;
581 for (mb_y = 0; mb_y < j->s->mb_height; mb_y++) {
582 for (mb_x = 0; mb_x < j->s->mb_width; mb_x++) {
584 * Fill one DCT block (8x8 pixels) from
585 * 2 Y macroblocks and one U and one V
587 fill_block(j, mb_x, mb_y, y_data, u_data, v_data);
588 emms_c(); /* is this really needed? */
590 j->s->block_last_index[0] =
591 j->s->dct_quantize(j->s, j->s->block[0],
592 0, 8, &overflow);
593 if (overflow) clip_coeffs(j->s, j->s->block[0],
594 j->s->block_last_index[0]);
595 j->s->block_last_index[1] =
596 j->s->dct_quantize(j->s, j->s->block[1],
597 1, 8, &overflow);
598 if (overflow) clip_coeffs(j->s, j->s->block[1],
599 j->s->block_last_index[1]);
601 if (!j->bw) {
602 j->s->block_last_index[4] =
603 j->s->dct_quantize(j->s, j->s->block[2],
604 4, 8, &overflow);
605 if (overflow) clip_coeffs(j->s, j->s->block[2],
606 j->s->block_last_index[2]);
607 j->s->block_last_index[5] =
608 j->s->dct_quantize(j->s, j->s->block[3],
609 5, 8, &overflow);
610 if (overflow) clip_coeffs(j->s, j->s->block[3],
611 j->s->block_last_index[3]);
613 zr_mjpeg_encode_mb(j);
616 emms_c();
617 ff_mjpeg_encode_picture_trailer(j->s);
618 flush_put_bits(&j->s->pb);
620 //FIXME
621 //if (j->s->mjpeg_write_tables == 1)
622 // j->s->mjpeg_write_tables = 0;
624 return put_bits_ptr(&(j->s->pb)) - j->s->pb.buf;
627 /// the real uninit routine
629 * This is the real routine that does the uninit of the ZRMJPEG filter
631 * \param j pointer to jpeg_enc structure
633 static void jpeg_enc_uninit(jpeg_enc_t *j) {
634 ff_mjpeg_encode_close(j->s);
635 av_free(j->s);
636 av_free(j);
639 /// Private structure for ZRMJPEG filter
640 struct vf_priv_s {
641 jpeg_enc_t *j;
642 unsigned char buf[256*1024];
643 int bw, fd, hdec, vdec;
644 int fields;
645 int y_stride;
646 int c_stride;
647 int quality;
648 int maxwidth;
649 int maxheight;
652 /// vf CONFIGURE entry point for the ZRMJPEG filter
654 * \param vf video filter instance pointer
655 * \param width image source width in pixels
656 * \param height image source height in pixels
657 * \param d_width width of requested window, just a hint
658 * \param d_height height of requested window, just a hint
659 * \param flags vf filter flags
660 * \param outfmt
662 * \returns returns 0 on error
664 * This routine will make the necessary hardware-related decisions for
665 * the ZRMJPEG filter, do the initialization of the MJPEG encoder, and
666 * then select one of the ZRJMJPEGIT or ZRMJPEGNI filters and then
667 * arrange to dispatch to the config() entry pointer for the one
668 * selected.
670 static int config(struct vf_instance *vf, int width, int height, int d_width,
671 int d_height, unsigned int flags, unsigned int outfmt){
672 struct vf_priv_s *priv = vf->priv;
673 float aspect_decision;
674 int stretchx, stretchy, err = 0, maxstretchx = 4;
675 priv->fields = 1;
677 VERBOSE("config() called\n");
679 if (priv->j) {
680 VERBOSE("re-configuring, resetting JPEG encoder\n");
681 jpeg_enc_uninit(priv->j);
682 priv->j = NULL;
685 aspect_decision = ((float)d_width/(float)d_height)/
686 ((float)width/(float)height);
688 if (aspect_decision > 1.8 && aspect_decision < 2.2) {
689 VERBOSE("should correct aspect by stretching x times 2, %d %d\n", 2*width, priv->maxwidth);
690 if (2*width <= priv->maxwidth) {
691 d_width = 2*width;
692 d_height = height;
693 maxstretchx = 2;
694 } else {
695 WARNING("unable to correct aspect by stretching, because resulting X will be too large, aspect correction by decimating y not yet implemented\n");
696 d_width = width;
697 d_height = height;
699 /* prestretch movie */
700 } else {
701 /* uncorrecting output for now */
702 d_width = width;
703 d_height = height;
705 /* make the scaling decision
706 * we are capable of stretching the image in the horizontal
707 * direction by factors 1, 2 and 4
708 * we can stretch the image in the vertical direction by a
709 * factor of 1 and 2 AND we must decide about interlacing */
710 if (d_width > priv->maxwidth/2 || height > priv->maxheight/2
711 || maxstretchx == 1) {
712 stretchx = 1;
713 stretchy = 1;
714 priv->fields = 2;
715 if (priv->vdec == 2) {
716 priv->fields = 1;
717 } else if (priv->vdec == 4) {
718 priv->fields = 1;
719 stretchy = 2;
721 if (priv->hdec > maxstretchx) {
722 if (priv->fd) {
723 WARNING("horizontal decimation too high, "
724 "changing to %d (use fd to keep"
725 " hdec=%d)\n",
726 maxstretchx, priv->hdec);
727 priv->hdec = maxstretchx;
730 stretchx = priv->hdec;
731 } else if (d_width > priv->maxwidth/4 ||
732 height > priv->maxheight/4 ||
733 maxstretchx == 2) {
734 stretchx = 2;
735 stretchy = 1;
736 priv->fields = 1;
737 if (priv->vdec == 2) {
738 stretchy = 2;
739 } else if (priv->vdec == 4) {
740 if (!priv->fd) {
741 WARNING("vertical decimation too high, "
742 "changing to 2 (use fd to keep "
743 "vdec=4)\n");
744 priv->vdec = 2;
746 stretchy = 2;
748 if (priv->hdec == 2) {
749 stretchx = 4;
750 } else if (priv->hdec == 4) {
751 if (priv->fd) {
752 WARNING("horizontal decimation too high, "
753 "changing to 2 (use fd to keep "
754 "hdec=4)\n");
755 priv->hdec = 2;
757 stretchx = 4;
759 } else {
760 /* output image is maximally stretched */
761 stretchx = 4;
762 stretchy = 2;
763 priv->fields = 1;
764 if (priv->vdec != 1 && !priv->fd) {
765 WARNING("vertical decimation too high, changing to 1 "
766 "(use fd to keep vdec=%d)\n",
767 priv->vdec);
768 priv->vdec = 1;
770 if (priv->hdec != 1 && !priv->fd) {
771 WARNING("horizontal decimation too high, changing to 1 (use fd to keep hdec=%d)\n", priv->hdec);
772 priv->hdec = 1;
776 VERBOSE("generated JPEG's %dx%s%d%s, stretched to %dx%d\n",
777 width/priv->hdec, (priv->fields == 2) ? "(" : "",
778 height/(priv->vdec*priv->fields),
779 (priv->fields == 2) ? "x2)" : "",
780 (width/priv->hdec)*stretchx,
781 (height/(priv->vdec*priv->fields))*
782 stretchy*priv->fields);
785 if ((width/priv->hdec)*stretchx > priv->maxwidth ||
786 (height/(priv->vdec*priv->fields))*
787 stretchy*priv->fields > priv->maxheight) {
788 ERROR("output dimensions too large (%dx%d), max (%dx%d) "
789 "insert crop to fix\n",
790 (width/priv->hdec)*stretchx,
791 (height/(priv->vdec*priv->fields))*
792 stretchy*priv->fields,
793 priv->maxwidth, priv->maxheight);
794 err = 1;
797 if (width%(16*priv->hdec) != 0) {
798 ERROR("width must be a multiple of 16*hdec (%d), use expand\n",
799 priv->hdec*16);
800 err = 1;
803 if (height%(8*priv->fields*priv->vdec) != 0) {
804 ERROR("height must be a multiple of 8*fields*vdec (%d),"
805 " use expand\n", priv->vdec*priv->fields*8);
806 err = 1;
809 if (err) return 0;
811 priv->y_stride = width;
812 priv->c_stride = width/2;
813 priv->j = jpeg_enc_init(width, height/priv->fields,
814 priv->fields*priv->y_stride,
815 priv->fields*priv->c_stride,
816 priv->fields*priv->c_stride,
817 1, priv->quality, priv->bw);
819 if (!priv->j) return 0;
820 return vf_next_config(vf, width, height, d_width, d_height, flags,
821 (priv->fields == 2) ? IMGFMT_ZRMJPEGIT : IMGFMT_ZRMJPEGNI);
824 /// put_image entrypoint for the ZRMJPEG vf filter
825 /***
826 * \param vf pointer to vf_instance
827 * \param mpi pointer to mp_image_t structure
828 * \param pts
830 static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts){
831 struct vf_priv_s *priv = vf->priv;
832 int size = 0;
833 int i;
834 mp_image_t* dmpi;
835 for (i = 0; i < priv->fields; i++)
836 size += jpeg_enc_frame(priv->j,
837 mpi->planes[0] + i*priv->y_stride,
838 mpi->planes[1] + i*priv->c_stride,
839 mpi->planes[2] + i*priv->c_stride,
840 priv->buf + size);
842 dmpi = vf_get_image(vf->next, IMGFMT_ZRMJPEGNI,
843 MP_IMGTYPE_EXPORT, 0, mpi->w, mpi->h);
844 dmpi->planes[0] = (uint8_t*)priv->buf;
845 dmpi->planes[1] = (uint8_t*)size;
846 return vf_next_put_image(vf,dmpi, pts);
849 /// query_format entrypoint for the ZRMJPEG vf filter
850 /***
851 * \param vf pointer to vf_instance
852 * \param fmt image format to query for
854 * \returns 0 if image format in fmt is not supported
856 * Given the image format specified by \a fmt, this routine is called
857 * to ask if the format is supported or not.
859 static int query_format(struct vf_instance *vf, unsigned int fmt){
860 VERBOSE("query_format() called\n");
862 switch (fmt) {
863 case IMGFMT_YV12:
864 case IMGFMT_YUY2:
865 /* strictly speaking the output format of
866 * this filter will be known after config(),
867 * but everything that supports IMGFMT_ZRMJPEGNI
868 * should also support all other IMGFMT_ZRMJPEG* */
869 return vf_next_query_format(vf, IMGFMT_ZRMJPEGNI);
872 return 0;
875 /// vf UNINIT entry point for the ZRMJPEG filter
877 * \param vf pointer to the vf instance structure
879 static void uninit(vf_instance_t *vf) {
880 struct vf_priv_s *priv = vf->priv;
881 VERBOSE("uninit() called\n");
882 if (priv->j) jpeg_enc_uninit(priv->j);
883 free(priv);
886 /// vf OPEN entry point for the ZRMJPEG filter
888 * \param vf pointer to the vf instance structure
889 * \param args the argument list string for the -vf zrmjpeg command
891 * \returns 0 for error, 1 for success
893 * This routine will do some basic initialization of local structures etc.,
894 * and then parse the command line arguments specific for the ZRMJPEG filter.
896 static int vf_open(vf_instance_t *vf, char *args){
897 struct vf_priv_s *priv;
898 VERBOSE("vf_open() called: args=\"%s\"\n", args);
900 vf->config = config;
901 vf->put_image = put_image;
902 vf->query_format = query_format;
903 vf->uninit = uninit;
905 priv = vf->priv = calloc(sizeof(*priv), 1);
906 if (!vf->priv) {
907 ERROR("out of memory error\n");
908 return 0;
911 /* maximum displayable size by zoran card, these defaults
912 * are for my own zoran card in PAL mode, these can be changed
913 * by filter options. But... in an ideal world these values would
914 * be queried from the vo device itself... */
915 priv->maxwidth = 768;
916 priv->maxheight = 576;
918 priv->quality = 2;
919 priv->hdec = 1;
920 priv->vdec = 1;
922 /* if libavcodec is already initialized, we must not initialize it
923 * again, but if it is not initialized then we mustinitialize it now. */
924 if (!avcodec_initialized) {
925 /* we need to initialize libavcodec */
926 avcodec_init();
927 avcodec_register_all();
928 avcodec_initialized=1;
931 if (args) {
932 char *arg, *tmp, *ptr, junk;
933 int last = 0, input;
935 /* save arguments, to be able to safely modify them */
936 arg = strdup(args);
937 if (!arg) {
938 ERROR("out of memory, this is bad\n");
939 return 0;
942 tmp = ptr = arg;
943 do {
944 while (*tmp != ':' && *tmp) tmp++;
945 if (*tmp == ':') *tmp++ = '\0';
946 else last = 1;
947 VERBOSE("processing filter option \"%s\"\n", ptr);
948 /* These options deal with the maximum output
949 * resolution of the zoran card. These should
950 * be queried from the vo device, but it is currently
951 * too difficult, so the user should tell the filter */
952 if (!strncmp("maxheight=", ptr, 10)) {
953 if (sscanf(ptr+10, "%d%c", &input, &junk) != 1)
954 ERROR(
955 "error parsing parameter to \"maxheight=\", \"%s\", ignoring\n"
956 , ptr + 10);
957 else {
958 priv->maxheight = input;
959 VERBOSE("setting maxheight to %d\n",
960 priv->maxheight);
962 } else if (!strncmp("quality=", ptr, 8)) {
963 if (sscanf(ptr+8, "%d%c", &input, &junk) != 1)
964 ERROR(
965 "error parsing parameter to \"quality=\", \"%s\", ignoring\n"
966 , ptr + 8);
967 else if (input < 1 || input > 20)
968 ERROR(
969 "parameter to \"quality=\" out of range (1..20), %d\n", input);
970 else {
971 priv->quality = input;
972 VERBOSE("setting JPEG quality to %d\n",
973 priv->quality);
975 } else if (!strncmp("maxwidth=", ptr, 9)) {
976 if (sscanf(ptr+9, "%d%c", &input, &junk) != 1)
977 ERROR(
978 "error parsing parameter to \"maxwidth=\", \"%s\", ignoring\n"
979 , ptr + 9);
980 else {
981 priv->maxwidth = input;
982 VERBOSE("setting maxwidth to %d\n",
983 priv->maxwidth);
985 } else if (!strncmp("hdec=", ptr, 5)) {
986 if (sscanf(ptr+5, "%d%c", &input, &junk) != 1)
987 ERROR(
988 "error parsing parameter to \"hdec=\", \"%s\", ignoring\n"
989 , ptr + 9);
990 else if (input != 1 && input != 2 && input != 4)
991 ERROR(
992 "illegal parameter to \"hdec=\", %d, should be 1, 2 or 4",
993 input);
994 else {
995 priv->hdec = input;
996 VERBOSE(
997 "setting horizontal decimation to %d\n", priv->maxwidth);
999 } else if (!strncmp("vdec=", ptr, 5)) {
1000 if (sscanf(ptr+5, "%d%c", &input, &junk) != 1)
1001 ERROR(
1002 "error parsing parameter to \"vdec=\", \"%s\", ignoring\n"
1003 , ptr + 9);
1004 else if (input != 1 && input != 2 && input != 4)
1005 ERROR(
1006 "illegal parameter to \"vdec=\", %d, should be 1, 2 or 4",
1007 input);
1008 else {
1009 priv->vdec = input;
1010 VERBOSE(
1011 "setting vertical decimation to %d\n", priv->maxwidth);
1013 } else if (!strcasecmp("dc10+-PAL", ptr) ||
1014 !strcasecmp("dc10-PAL", ptr)) {
1015 priv->maxwidth = 768;
1016 priv->maxheight = 576;
1017 VERBOSE("setting DC10(+) PAL profile\n");
1018 } else if (!strcasecmp("fd", ptr)) {
1019 priv->fd = 1;
1020 VERBOSE("forcing decimation\n");
1021 } else if (!strcasecmp("nofd", ptr)) {
1022 priv->fd = 0;
1023 VERBOSE("decimate only if beautiful\n");
1024 } else if (!strcasecmp("bw", ptr)) {
1025 priv->bw = 1;
1026 VERBOSE("setting black and white encoding\n");
1027 } else if (!strcasecmp("color", ptr)) {
1028 priv->bw = 0;
1029 VERBOSE("setting color encoding\n");
1030 } else if (!strcasecmp("dc10+-NTSC", ptr) ||
1031 !strcasecmp("dc10-NTSC", ptr)) {
1032 priv->maxwidth = 640;
1033 priv->maxheight = 480;
1034 VERBOSE("setting DC10(+) NTSC profile\n");
1035 } else if (!strcasecmp("buz-PAL", ptr) ||
1036 !strcasecmp("lml33-PAL", ptr)) {
1037 priv->maxwidth = 720;
1038 priv->maxheight = 576;
1039 VERBOSE("setting buz/lml33 PAL profile\n");
1040 } else if (!strcasecmp("buz-NTSC", ptr) ||
1041 !strcasecmp("lml33-NTSC", ptr)) {
1042 priv->maxwidth = 720;
1043 priv->maxheight = 480;
1044 VERBOSE("setting buz/lml33 NTSC profile\n");
1045 } else {
1046 WARNING("ignoring unknown filter option "
1047 "\"%s\", or missing argument\n",
1048 ptr);
1050 ptr = tmp;
1051 } while (!last);
1053 free(arg);
1057 return 1;
1060 const vf_info_t vf_info_zrmjpeg = {
1061 "realtime zoran MJPEG encoding",
1062 "zrmjpeg",
1063 "Rik Snel",
1065 vf_open,
1066 NULL