sync with en/mplayer.1 rev. 30611
[mplayer/glamo.git] / libmpcodecs / vf_zrmjpeg.c
blob6b4c9a7c64e90320388402e8c8747561df6e7157
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"
53 //#include "jpeg_enc.h" /* this file is not present yet */
55 #undef malloc
56 #undef free
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
91 /**
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) {
114 int qscale;
116 for(qscale = qmin; qscale <= qmax; qscale++) {
117 int i;
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) <<
127 (QMAT_SHIFT-3))/
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]));
142 } else {
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
151 *quant_matrix[j]));
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) {
179 int mant, nbits;
181 if (val == 0) {
182 put_bits(&s->pb, huff_size[0], huff_code[0]);
183 } else {
184 mant = val;
185 if (val < 0) {
186 val = -val;
187 mant--;
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
199 * \param n
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;
210 /* DC coef */
211 component = (n <= 3 ? 0 : n - 4 + 1);
212 dc = block[0]; /* overflow is impossible */
213 val = dc - s->last_dc[component];
214 if (n < 4) {
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;
219 } else {
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;
227 /* AC coefs */
229 run = 0;
230 last_index = s->block_last_index[n];
231 for (i = 1; i <= last_index; i++) {
232 j = s->intra_scantable.permutated[i];
233 val = block[j];
234 if (val == 0) run++;
235 else {
236 while (run >= 16) {
237 put_bits(&s->pb, huff_size_ac[0xf0],
238 huff_code_ac[0xf0]);
239 run -= 16;
241 mant = val;
242 if (val < 0) {
243 val = -val;
244 mant--;
247 nbits= av_log2_16bit(val) + 1;
248 code = (run << 4) | nbits;
250 put_bits(&s->pb, huff_size_ac[code],
251 huff_code_ac[code]);
252 put_bits(&s->pb, nbits, mant & ((1 << nbits) - 1));
253 run = 0;
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,
275 int last_index) {
276 int i;
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;
286 block[j]= level;
290 /* End excessive code duplication **************************************/
292 typedef struct {
293 struct MpegEncContext *s;
294 int cheap_upsample;
295 int bw;
296 int y_rs;
297 int u_rs;
298 int v_rs;
299 } jpeg_enc_t;
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);
319 if (j->bw) {
320 /* U */
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]);
325 /* V */
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 } else {
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)
356 int i, k;
357 short int *dest;
358 unsigned char *source;
360 // The first Y, Y0
361 get_pixels(j->s->block[0], y*8*j->y_rs + 16*x + y_data, j->y_rs);
362 // The second Y, Y1
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
374 dest += 16;
375 source += j->u_rs;
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++) {
381 dest[k] = source[k];
382 dest[k+8] = source[k];
384 dest += 16;
385 source += j->u_rs;
387 } else if (!j->bw && !j->cheap_upsample) {
388 // U
389 get_pixels(j->s->block[2], y*8*j->u_rs + 8*x + u_data, j->u_rs);
390 // V
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
410 * duplicated.
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) {
428 jpeg_enc_t *j;
429 int i = 0;
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));
437 if (j->s == NULL) {
438 av_free(j);
439 return NULL;
442 /* info on how to access the pixels */
443 j->y_rs = y_rsize;
444 j->u_rs = u_rsize;
445 j->v_rs = v_rsize;
447 j->s->width = w; // image width and height
448 j->s->height = h;
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;
475 j->bw = b;
477 // Is this needed?
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) {
482 avcodec_init();
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) {
489 av_free(j->s);
490 av_free(j);
491 return NULL;
494 /* alloc bogus avctx to keep MPV_common_init from segfaulting */
495 j->s->avctx = avcodec_alloc_context();
496 if (j->s->avctx == NULL) {
497 av_free(j->s);
498 av_free(j);
499 return 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) {
516 av_free(j->s);
517 av_free(j);
518 return NULL;
521 /* correct the value for sc->mb_height. MPV_common_init put other
522 * values there */
523 j->s->mb_height = j->s->height/8;
524 j->s->mb_intra = 1;
526 // Init q matrix
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);
532 // precompute matrix
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;
540 return j;
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
555 * other pixel
556 * \param v_data pointer to V component plane, packed one byte per every
557 * other pixel
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],
593 0, 8, &overflow);
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],
598 1, 8, &overflow);
599 if (overflow) clip_coeffs(j->s, j->s->block[1],
600 j->s->block_last_index[1]);
602 if (!j->bw) {
603 j->s->block_last_index[4] =
604 j->s->dct_quantize(j->s, j->s->block[2],
605 4, 8, &overflow);
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],
610 5, 8, &overflow);
611 if (overflow) clip_coeffs(j->s, j->s->block[3],
612 j->s->block_last_index[3]);
614 zr_mjpeg_encode_mb(j);
617 emms_c();
618 ff_mjpeg_encode_picture_trailer(j->s);
619 flush_put_bits(&j->s->pb);
621 //FIXME
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);
636 av_free(j->s);
637 av_free(j);
640 /// Private structure for ZRMJPEG filter
641 struct vf_priv_s {
642 jpeg_enc_t *j;
643 unsigned char buf[256*1024];
644 int bw, fd, hdec, vdec;
645 int fields;
646 int y_stride;
647 int c_stride;
648 int quality;
649 int maxwidth;
650 int maxheight;
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
661 * \param outfmt
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
669 * selected.
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;
676 priv->fields = 1;
678 VERBOSE("config() called\n");
680 if (priv->j) {
681 VERBOSE("re-configuring, resetting JPEG encoder\n");
682 jpeg_enc_uninit(priv->j);
683 priv->j = NULL;
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) {
692 d_width = 2*width;
693 d_height = height;
694 maxstretchx = 2;
695 } else {
696 WARNING("unable to correct aspect by stretching, because resulting X will be too large, aspect correction by decimating y not yet implemented\n");
697 d_width = width;
698 d_height = height;
700 /* prestretch movie */
701 } else {
702 /* uncorrecting output for now */
703 d_width = width;
704 d_height = height;
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) {
713 stretchx = 1;
714 stretchy = 1;
715 priv->fields = 2;
716 if (priv->vdec == 2) {
717 priv->fields = 1;
718 } else if (priv->vdec == 4) {
719 priv->fields = 1;
720 stretchy = 2;
722 if (priv->hdec > maxstretchx) {
723 if (priv->fd) {
724 WARNING("horizontal decimation too high, "
725 "changing to %d (use fd to keep"
726 " hdec=%d)\n",
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 ||
734 maxstretchx == 2) {
735 stretchx = 2;
736 stretchy = 1;
737 priv->fields = 1;
738 if (priv->vdec == 2) {
739 stretchy = 2;
740 } else if (priv->vdec == 4) {
741 if (!priv->fd) {
742 WARNING("vertical decimation too high, "
743 "changing to 2 (use fd to keep "
744 "vdec=4)\n");
745 priv->vdec = 2;
747 stretchy = 2;
749 if (priv->hdec == 2) {
750 stretchx = 4;
751 } else if (priv->hdec == 4) {
752 if (priv->fd) {
753 WARNING("horizontal decimation too high, "
754 "changing to 2 (use fd to keep "
755 "hdec=4)\n");
756 priv->hdec = 2;
758 stretchx = 4;
760 } else {
761 /* output image is maximally stretched */
762 stretchx = 4;
763 stretchy = 2;
764 priv->fields = 1;
765 if (priv->vdec != 1 && !priv->fd) {
766 WARNING("vertical decimation too high, changing to 1 "
767 "(use fd to keep vdec=%d)\n",
768 priv->vdec);
769 priv->vdec = 1;
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);
773 priv->hdec = 1;
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);
795 err = 1;
798 if (width%(16*priv->hdec) != 0) {
799 ERROR("width must be a multiple of 16*hdec (%d), use expand\n",
800 priv->hdec*16);
801 err = 1;
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);
807 err = 1;
810 if (err) return 0;
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
826 /***
827 * \param vf pointer to vf_instance
828 * \param mpi pointer to mp_image_t structure
829 * \param pts
831 static int put_image(struct vf_instance_s* vf, mp_image_t *mpi, double pts){
832 struct vf_priv_s *priv = vf->priv;
833 int size = 0;
834 int i;
835 mp_image_t* dmpi;
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,
841 priv->buf + size);
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
851 /***
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");
863 switch (fmt) {
864 case IMGFMT_YV12:
865 case IMGFMT_YUY2:
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);
873 return 0;
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);
884 free(priv);
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);
901 vf->config = config;
902 vf->put_image = put_image;
903 vf->query_format = query_format;
904 vf->uninit = uninit;
906 priv = vf->priv = calloc(sizeof(*priv), 1);
907 if (!vf->priv) {
908 ERROR("out of memory error\n");
909 return 0;
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;
919 priv->quality = 2;
920 priv->hdec = 1;
921 priv->vdec = 1;
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 */
927 avcodec_init();
928 avcodec_register_all();
929 avcodec_initialized=1;
932 if (args) {
933 char *arg, *tmp, *ptr, junk;
934 int last = 0, input;
936 /* save arguments, to be able to safely modify them */
937 arg = strdup(args);
938 if (!arg) {
939 ERROR("out of memory, this is bad\n");
940 return 0;
943 tmp = ptr = arg;
944 do {
945 while (*tmp != ':' && *tmp) tmp++;
946 if (*tmp == ':') *tmp++ = '\0';
947 else last = 1;
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)
955 ERROR(
956 "error parsing parameter to \"maxheight=\", \"%s\", ignoring\n"
957 , ptr + 10);
958 else {
959 priv->maxheight = input;
960 VERBOSE("setting maxheight to %d\n",
961 priv->maxheight);
963 } else if (!strncmp("quality=", ptr, 8)) {
964 if (sscanf(ptr+8, "%d%c", &input, &junk) != 1)
965 ERROR(
966 "error parsing parameter to \"quality=\", \"%s\", ignoring\n"
967 , ptr + 8);
968 else if (input < 1 || input > 20)
969 ERROR(
970 "parameter to \"quality=\" out of range (1..20), %d\n", input);
971 else {
972 priv->quality = input;
973 VERBOSE("setting JPEG quality to %d\n",
974 priv->quality);
976 } else if (!strncmp("maxwidth=", ptr, 9)) {
977 if (sscanf(ptr+9, "%d%c", &input, &junk) != 1)
978 ERROR(
979 "error parsing parameter to \"maxwidth=\", \"%s\", ignoring\n"
980 , ptr + 9);
981 else {
982 priv->maxwidth = input;
983 VERBOSE("setting maxwidth to %d\n",
984 priv->maxwidth);
986 } else if (!strncmp("hdec=", ptr, 5)) {
987 if (sscanf(ptr+5, "%d%c", &input, &junk) != 1)
988 ERROR(
989 "error parsing parameter to \"hdec=\", \"%s\", ignoring\n"
990 , ptr + 9);
991 else if (input != 1 && input != 2 && input != 4)
992 ERROR(
993 "illegal parameter to \"hdec=\", %d, should be 1, 2 or 4",
994 input);
995 else {
996 priv->hdec = input;
997 VERBOSE(
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)
1002 ERROR(
1003 "error parsing parameter to \"vdec=\", \"%s\", ignoring\n"
1004 , ptr + 9);
1005 else if (input != 1 && input != 2 && input != 4)
1006 ERROR(
1007 "illegal parameter to \"vdec=\", %d, should be 1, 2 or 4",
1008 input);
1009 else {
1010 priv->vdec = input;
1011 VERBOSE(
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)) {
1020 priv->fd = 1;
1021 VERBOSE("forcing decimation\n");
1022 } else if (!strcasecmp("nofd", ptr)) {
1023 priv->fd = 0;
1024 VERBOSE("decimate only if beautiful\n");
1025 } else if (!strcasecmp("bw", ptr)) {
1026 priv->bw = 1;
1027 VERBOSE("setting black and white encoding\n");
1028 } else if (!strcasecmp("color", ptr)) {
1029 priv->bw = 0;
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");
1046 } else {
1047 WARNING("ignoring unknown filter option "
1048 "\"%s\", or missing argument\n",
1049 ptr);
1051 ptr = tmp;
1052 } while (!last);
1054 free(arg);
1058 return 1;
1061 const vf_info_t vf_info_zrmjpeg = {
1062 "realtime zoran MJPEG encoding",
1063 "zrmjpeg",
1064 "Rik Snel",
1066 open,
1067 NULL