MAX_PCI_DEVICES 64 is not enough on my system (even though lspci only shows 25 device...
[mplayer/greg.git] / libmpcodecs / vf_zrmjpeg.c
blob0d45666aa42df79ebab7230b0aa011488b2181cf
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
509 j->s->avctx->thread_count = 1;
511 /* make MPV_common_init allocate important buffers, like s->block
512 * Also initializes dsputil */
513 if (MPV_common_init(j->s) < 0) {
514 av_free(j->s);
515 av_free(j);
516 return NULL;
519 /* correct the value for sc->mb_height. MPV_common_init put other
520 * values there */
521 j->s->mb_height = j->s->height/8;
522 j->s->mb_intra = 1;
524 // Init q matrix
525 j->s->intra_matrix[0] = ff_mpeg1_default_intra_matrix[0];
526 for (i = 1; i < 64; i++)
527 j->s->intra_matrix[i] = av_clip_uint8(
528 (ff_mpeg1_default_intra_matrix[i]*j->s->qscale) >> 3);
530 // precompute matrix
531 convert_matrix(j->s, j->s->q_intra_matrix, j->s->q_intra_matrix16,
532 j->s->intra_matrix, j->s->intra_quant_bias, 8, 8);
534 /* Pick up the selection of the optimal get_pixels() routine
535 * to use, which was done in MPV_common_init() */
536 get_pixels = j->s->dsp.get_pixels;
538 return j;
542 * \brief mjpeg encode an image
544 * This routine will take a 3-plane YUV422 image and encoded it with MJPEG
545 * base line format, as suitable as input for the Zoran hardare MJPEG chips.
547 * It requires that the \a j parameter points the structure set up by the
548 * jpeg_enc_init() routine.
550 * \param j pointer to jpeg_enc_t structure as created by jpeg_enc_init()
551 * \param y_data pointer to Y component plane, packed one byte/pixel
552 * \param u_data pointer to U component plane, packed one byte per every
553 * other pixel
554 * \param v_data pointer to V component plane, packed one byte per every
555 * other pixel
556 * \param bufr pointer to the buffer where the mjpeg encoded code is stored
558 * \returns the number of bytes stored into \a bufr
560 * If \a j->s->mjpeg_write_tables is set, it will also emit the mjpeg tables,
561 * otherwise it will just emit the data. The \a j->s->mjpeg_write_tables
562 * variable will be reset to 0 by the routine.
564 static int jpeg_enc_frame(jpeg_enc_t *j, uint8_t *y_data,
565 uint8_t *u_data, uint8_t *v_data, uint8_t *bufr) {
566 int mb_x, mb_y, overflow;
567 /* initialize the buffer */
569 init_put_bits(&j->s->pb, bufr, 1024*256);
571 // Emit the mjpeg header blocks
572 ff_mjpeg_encode_picture_header(j->s);
574 j->s->header_bits = put_bits_count(&j->s->pb);
576 j->s->last_dc[0] = 128;
577 j->s->last_dc[1] = 128;
578 j->s->last_dc[2] = 128;
580 for (mb_y = 0; mb_y < j->s->mb_height; mb_y++) {
581 for (mb_x = 0; mb_x < j->s->mb_width; mb_x++) {
583 * Fill one DCT block (8x8 pixels) from
584 * 2 Y macroblocks and one U and one V
586 fill_block(j, mb_x, mb_y, y_data, u_data, v_data);
587 emms_c(); /* is this really needed? */
589 j->s->block_last_index[0] =
590 j->s->dct_quantize(j->s, j->s->block[0],
591 0, 8, &overflow);
592 if (overflow) clip_coeffs(j->s, j->s->block[0],
593 j->s->block_last_index[0]);
594 j->s->block_last_index[1] =
595 j->s->dct_quantize(j->s, j->s->block[1],
596 1, 8, &overflow);
597 if (overflow) clip_coeffs(j->s, j->s->block[1],
598 j->s->block_last_index[1]);
600 if (!j->bw) {
601 j->s->block_last_index[4] =
602 j->s->dct_quantize(j->s, j->s->block[2],
603 4, 8, &overflow);
604 if (overflow) clip_coeffs(j->s, j->s->block[2],
605 j->s->block_last_index[2]);
606 j->s->block_last_index[5] =
607 j->s->dct_quantize(j->s, j->s->block[3],
608 5, 8, &overflow);
609 if (overflow) clip_coeffs(j->s, j->s->block[3],
610 j->s->block_last_index[3]);
612 zr_mjpeg_encode_mb(j);
615 emms_c();
616 ff_mjpeg_encode_picture_trailer(j->s);
617 flush_put_bits(&j->s->pb);
619 //FIXME
620 //if (j->s->mjpeg_write_tables == 1)
621 // j->s->mjpeg_write_tables = 0;
623 return pbBufPtr(&(j->s->pb)) - j->s->pb.buf;
626 /// the real uninit routine
628 * This is the real routine that does the uninit of the ZRMJPEG filter
630 * \param j pointer to jpeg_enc structure
632 static void jpeg_enc_uninit(jpeg_enc_t *j) {
633 ff_mjpeg_encode_close(j->s);
634 av_free(j->s);
635 av_free(j);
638 /// Private structure for ZRMJPEG filter
639 struct vf_priv_s {
640 jpeg_enc_t *j;
641 unsigned char buf[256*1024];
642 int bw, fd, hdec, vdec;
643 int fields;
644 int y_stride;
645 int c_stride;
646 int quality;
647 int maxwidth;
648 int maxheight;
651 /// vf CONFIGURE entry point for the ZRMJPEG filter
653 * \param vf video filter instance pointer
654 * \param width image source width in pixels
655 * \param height image source height in pixels
656 * \param d_width width of requested window, just a hint
657 * \param d_height height of requested window, just a hint
658 * \param flags vf filter flags
659 * \param outfmt
661 * \returns returns 0 on error
663 * This routine will make the necessary hardware-related decisions for
664 * the ZRMJPEG filter, do the initialization of the MJPEG encoder, and
665 * then select one of the ZRJMJPEGIT or ZRMJPEGNI filters and then
666 * arrange to dispatch to the config() entry pointer for the one
667 * selected.
669 static int config(struct vf_instance_s* vf, int width, int height, int d_width,
670 int d_height, unsigned int flags, unsigned int outfmt){
671 struct vf_priv_s *priv = vf->priv;
672 float aspect_decision;
673 int stretchx, stretchy, err = 0, maxstretchx = 4;
674 priv->fields = 1;
676 VERBOSE("config() called\n");
678 if (priv->j) {
679 VERBOSE("re-configuring, resetting JPEG encoder\n");
680 jpeg_enc_uninit(priv->j);
681 priv->j = NULL;
684 aspect_decision = ((float)d_width/(float)d_height)/
685 ((float)width/(float)height);
687 if (aspect_decision > 1.8 && aspect_decision < 2.2) {
688 VERBOSE("should correct aspect by stretching x times 2, %d %d\n", 2*width, priv->maxwidth);
689 if (2*width <= priv->maxwidth) {
690 d_width = 2*width;
691 d_height = height;
692 maxstretchx = 2;
693 } else {
694 WARNING("unable to correct aspect by stretching, because resulting X will be too large, aspect correction by decimating y not yet implemented\n");
695 d_width = width;
696 d_height = height;
698 /* prestretch movie */
699 } else {
700 /* uncorrecting output for now */
701 d_width = width;
702 d_height = height;
704 /* make the scaling decision
705 * we are capable of stretching the image in the horizontal
706 * direction by factors 1, 2 and 4
707 * we can stretch the image in the vertical direction by a
708 * factor of 1 and 2 AND we must decide about interlacing */
709 if (d_width > priv->maxwidth/2 || height > priv->maxheight/2
710 || maxstretchx == 1) {
711 stretchx = 1;
712 stretchy = 1;
713 priv->fields = 2;
714 if (priv->vdec == 2) {
715 priv->fields = 1;
716 } else if (priv->vdec == 4) {
717 priv->fields = 1;
718 stretchy = 2;
720 if (priv->hdec > maxstretchx) {
721 if (priv->fd) {
722 WARNING("horizontal decimation too high, "
723 "changing to %d (use fd to keep"
724 " hdec=%d)\n",
725 maxstretchx, priv->hdec);
726 priv->hdec = maxstretchx;
729 stretchx = priv->hdec;
730 } else if (d_width > priv->maxwidth/4 ||
731 height > priv->maxheight/4 ||
732 maxstretchx == 2) {
733 stretchx = 2;
734 stretchy = 1;
735 priv->fields = 1;
736 if (priv->vdec == 2) {
737 stretchy = 2;
738 } else if (priv->vdec == 4) {
739 if (!priv->fd) {
740 WARNING("vertical decimation too high, "
741 "changing to 2 (use fd to keep "
742 "vdec=4)\n");
743 priv->vdec = 2;
745 stretchy = 2;
747 if (priv->hdec == 2) {
748 stretchx = 4;
749 } else if (priv->hdec == 4) {
750 if (priv->fd) {
751 WARNING("horizontal decimation too high, "
752 "changing to 2 (use fd to keep "
753 "hdec=4)\n");
754 priv->hdec = 2;
756 stretchx = 4;
758 } else {
759 /* output image is maximally stretched */
760 stretchx = 4;
761 stretchy = 2;
762 priv->fields = 1;
763 if (priv->vdec != 1 && !priv->fd) {
764 WARNING("vertical decimation too high, changing to 1 "
765 "(use fd to keep vdec=%d)\n",
766 priv->vdec);
767 priv->vdec = 1;
769 if (priv->hdec != 1 && !priv->fd) {
770 WARNING("horizontal decimation too high, changing to 1 (use fd to keep hdec=%d)\n", priv->hdec);
771 priv->hdec = 1;
775 VERBOSE("generated JPEG's %dx%s%d%s, stretched to %dx%d\n",
776 width/priv->hdec, (priv->fields == 2) ? "(" : "",
777 height/(priv->vdec*priv->fields),
778 (priv->fields == 2) ? "x2)" : "",
779 (width/priv->hdec)*stretchx,
780 (height/(priv->vdec*priv->fields))*
781 stretchy*priv->fields);
784 if ((width/priv->hdec)*stretchx > priv->maxwidth ||
785 (height/(priv->vdec*priv->fields))*
786 stretchy*priv->fields > priv->maxheight) {
787 ERROR("output dimensions too large (%dx%d), max (%dx%d) "
788 "insert crop to fix\n",
789 (width/priv->hdec)*stretchx,
790 (height/(priv->vdec*priv->fields))*
791 stretchy*priv->fields,
792 priv->maxwidth, priv->maxheight);
793 err = 1;
796 if (width%(16*priv->hdec) != 0) {
797 ERROR("width must be a multiple of 16*hdec (%d), use expand\n",
798 priv->hdec*16);
799 err = 1;
802 if (height%(8*priv->fields*priv->vdec) != 0) {
803 ERROR("height must be a multiple of 8*fields*vdec (%d),"
804 " use expand\n", priv->vdec*priv->fields*8);
805 err = 1;
808 if (err) return 0;
810 priv->y_stride = width;
811 priv->c_stride = width/2;
812 priv->j = jpeg_enc_init(width, height/priv->fields,
813 priv->fields*priv->y_stride,
814 priv->fields*priv->c_stride,
815 priv->fields*priv->c_stride,
816 1, priv->quality, priv->bw);
818 if (!priv->j) return 0;
819 return vf_next_config(vf, width, height, d_width, d_height, flags,
820 (priv->fields == 2) ? IMGFMT_ZRMJPEGIT : IMGFMT_ZRMJPEGNI);
823 /// put_image entrypoint for the ZRMJPEG vf filter
824 /***
825 * \param vf pointer to vf_instance
826 * \param mpi pointer to mp_image_t structure
827 * \param pts
829 static int put_image(struct vf_instance_s* vf, mp_image_t *mpi, double pts){
830 struct vf_priv_s *priv = vf->priv;
831 int size = 0;
832 int i;
833 mp_image_t* dmpi;
834 for (i = 0; i < priv->fields; i++)
835 size += jpeg_enc_frame(priv->j,
836 mpi->planes[0] + i*priv->y_stride,
837 mpi->planes[1] + i*priv->c_stride,
838 mpi->planes[2] + i*priv->c_stride,
839 priv->buf + size);
841 dmpi = vf_get_image(vf->next, IMGFMT_ZRMJPEGNI,
842 MP_IMGTYPE_EXPORT, 0, mpi->w, mpi->h);
843 dmpi->planes[0] = (uint8_t*)priv->buf;
844 dmpi->planes[1] = (uint8_t*)size;
845 return vf_next_put_image(vf,dmpi, pts);
848 /// query_format entrypoint for the ZRMJPEG vf filter
849 /***
850 * \param vf pointer to vf_instance
851 * \param fmt image format to query for
853 * \returns 0 if image format in fmt is not supported
855 * Given the image format specified by \a fmt, this routine is called
856 * to ask if the format is supported or not.
858 static int query_format(struct vf_instance_s* vf, unsigned int fmt){
859 VERBOSE("query_format() called\n");
861 switch (fmt) {
862 case IMGFMT_YV12:
863 case IMGFMT_YUY2:
864 /* strictly speaking the output format of
865 * this filter will be known after config(),
866 * but everything that supports IMGFMT_ZRMJPEGNI
867 * should also support all other IMGFMT_ZRMJPEG* */
868 return vf_next_query_format(vf, IMGFMT_ZRMJPEGNI);
871 return 0;
874 /// vf UNINIT entry point for the ZRMJPEG filter
876 * \param vf pointer to the vf instance structure
878 static void uninit(vf_instance_t *vf) {
879 struct vf_priv_s *priv = vf->priv;
880 VERBOSE("uninit() called\n");
881 if (priv->j) jpeg_enc_uninit(priv->j);
882 free(priv);
885 /// vf OPEN entry point for the ZRMJPEG filter
887 * \param vf pointer to the vf instance structure
888 * \param args the argument list string for the -vf zrmjpeg command
890 * \returns 0 for error, 1 for success
892 * This routine will do some basic initialization of local structures etc.,
893 * and then parse the command line arguments specific for the ZRMJPEG filter.
895 static int open(vf_instance_t *vf, char* args){
896 struct vf_priv_s *priv;
897 VERBOSE("open() called: args=\"%s\"\n", args);
899 vf->config = config;
900 vf->put_image = put_image;
901 vf->query_format = query_format;
902 vf->uninit = uninit;
904 priv = vf->priv = calloc(sizeof(*priv), 1);
905 if (!vf->priv) {
906 ERROR("out of memory error\n");
907 return 0;
910 /* maximum displayable size by zoran card, these defaults
911 * are for my own zoran card in PAL mode, these can be changed
912 * by filter options. But... in an ideal world these values would
913 * be queried from the vo device itself... */
914 priv->maxwidth = 768;
915 priv->maxheight = 576;
917 priv->quality = 2;
918 priv->hdec = 1;
919 priv->vdec = 1;
921 /* if libavcodec is already initialized, we must not initialize it
922 * again, but if it is not initialized then we mustinitialize it now. */
923 if (!avcodec_initialized) {
924 /* we need to initialize libavcodec */
925 avcodec_init();
926 avcodec_register_all();
927 avcodec_initialized=1;
930 if (args) {
931 char *arg, *tmp, *ptr, junk;
932 int last = 0, input;
934 /* save arguments, to be able to safely modify them */
935 arg = strdup(args);
936 if (!arg) {
937 ERROR("out of memory, this is bad\n");
938 return 0;
941 tmp = ptr = arg;
942 do {
943 while (*tmp != ':' && *tmp) tmp++;
944 if (*tmp == ':') *tmp++ = '\0';
945 else last = 1;
946 VERBOSE("processing filter option \"%s\"\n", ptr);
947 /* These options deal with the maximum output
948 * resolution of the zoran card. These should
949 * be queried from the vo device, but it is currently
950 * too difficult, so the user should tell the filter */
951 if (!strncmp("maxheight=", ptr, 10)) {
952 if (sscanf(ptr+10, "%d%c", &input, &junk) != 1)
953 ERROR(
954 "error parsing parameter to \"maxheight=\", \"%s\", ignoring\n"
955 , ptr + 10);
956 else {
957 priv->maxheight = input;
958 VERBOSE("setting maxheight to %d\n",
959 priv->maxheight);
961 } else if (!strncmp("quality=", ptr, 8)) {
962 if (sscanf(ptr+8, "%d%c", &input, &junk) != 1)
963 ERROR(
964 "error parsing parameter to \"quality=\", \"%s\", ignoring\n"
965 , ptr + 8);
966 else if (input < 1 || input > 20)
967 ERROR(
968 "parameter to \"quality=\" out of range (1..20), %d\n", input);
969 else {
970 priv->quality = input;
971 VERBOSE("setting JPEG quality to %d\n",
972 priv->quality);
974 } else if (!strncmp("maxwidth=", ptr, 9)) {
975 if (sscanf(ptr+9, "%d%c", &input, &junk) != 1)
976 ERROR(
977 "error parsing parameter to \"maxwidth=\", \"%s\", ignoring\n"
978 , ptr + 9);
979 else {
980 priv->maxwidth = input;
981 VERBOSE("setting maxwidth to %d\n",
982 priv->maxwidth);
984 } else if (!strncmp("hdec=", ptr, 5)) {
985 if (sscanf(ptr+5, "%d%c", &input, &junk) != 1)
986 ERROR(
987 "error parsing parameter to \"hdec=\", \"%s\", ignoring\n"
988 , ptr + 9);
989 else if (input != 1 && input != 2 && input != 4)
990 ERROR(
991 "illegal parameter to \"hdec=\", %d, should be 1, 2 or 4",
992 input);
993 else {
994 priv->hdec = input;
995 VERBOSE(
996 "setting horizontal decimation to %d\n", priv->maxwidth);
998 } else if (!strncmp("vdec=", ptr, 5)) {
999 if (sscanf(ptr+5, "%d%c", &input, &junk) != 1)
1000 ERROR(
1001 "error parsing parameter to \"vdec=\", \"%s\", ignoring\n"
1002 , ptr + 9);
1003 else if (input != 1 && input != 2 && input != 4)
1004 ERROR(
1005 "illegal parameter to \"vdec=\", %d, should be 1, 2 or 4",
1006 input);
1007 else {
1008 priv->vdec = input;
1009 VERBOSE(
1010 "setting vertical decimation to %d\n", priv->maxwidth);
1012 } else if (!strcasecmp("dc10+-PAL", ptr) ||
1013 !strcasecmp("dc10-PAL", ptr)) {
1014 priv->maxwidth = 768;
1015 priv->maxheight = 576;
1016 VERBOSE("setting DC10(+) PAL profile\n");
1017 } else if (!strcasecmp("fd", ptr)) {
1018 priv->fd = 1;
1019 VERBOSE("forcing decimation\n");
1020 } else if (!strcasecmp("nofd", ptr)) {
1021 priv->fd = 0;
1022 VERBOSE("decimate only if beautiful\n");
1023 } else if (!strcasecmp("bw", ptr)) {
1024 priv->bw = 1;
1025 VERBOSE("setting black and white encoding\n");
1026 } else if (!strcasecmp("color", ptr)) {
1027 priv->bw = 0;
1028 VERBOSE("setting color encoding\n");
1029 } else if (!strcasecmp("dc10+-NTSC", ptr) ||
1030 !strcasecmp("dc10-NTSC", ptr)) {
1031 priv->maxwidth = 640;
1032 priv->maxheight = 480;
1033 VERBOSE("setting DC10(+) NTSC profile\n");
1034 } else if (!strcasecmp("buz-PAL", ptr) ||
1035 !strcasecmp("lml33-PAL", ptr)) {
1036 priv->maxwidth = 720;
1037 priv->maxheight = 576;
1038 VERBOSE("setting buz/lml33 PAL profile\n");
1039 } else if (!strcasecmp("buz-NTSC", ptr) ||
1040 !strcasecmp("lml33-NTSC", ptr)) {
1041 priv->maxwidth = 720;
1042 priv->maxheight = 480;
1043 VERBOSE("setting buz/lml33 NTSC profile\n");
1044 } else {
1045 WARNING("ignoring unknown filter option "
1046 "\"%s\", or missing argument\n",
1047 ptr);
1049 ptr = tmp;
1050 } while (!last);
1052 free(arg);
1056 return 1;
1059 const vf_info_t vf_info_zrmjpeg = {
1060 "realtime zoran MJPEG encoding",
1061 "zrmjpeg",
1062 "Rik Snel",
1064 open,
1065 NULL