/** * \brief initialize mjpeg encoder * * This routine is to set up the parameters and initialize the mjpeg encoder. * It does all the initializations needed of lower level routines. * The formats accepted by this encoder is YUV422P and YUV420 * * \param w width in pixels of the image to encode, must be a multiple of 16 * \param h height in pixels of the image to encode, must be a multiple of 8 * \param y_rsize size of each plane row Y component * \param y_rsize size of each plane row U component * \param v_rsize size of each plane row V component * \param cu "cheap upsample". Set to 0 for YUV422 format, 1 for YUV420 format * when set to 1, the encoder will assume that there is only half th * number of rows of chroma information, and every chroma row is * duplicated. * \param q quality parameter for the mjpeg encode. Between 1 and 20 where 1 * is best quality and 20 is the worst quality. * \param b monochrome flag. When set to 1, the mjpeg output is monochrome. * In that case, the colour information is omitted, and actually the * colour planes are not touched. * * \returns an appropriately set up jpeg_enc_t structure * * The actual plane buffer addreses are passed by jpeg_enc_frame(). * * The encoder doesn't know anything about interlacing, the halve height * needs to be passed and the double rowstride. Which field gets encoded * is decided by what buffers are passed to mjpeg_encode_frame() */ static jpeg_enc_t *jpeg_enc_init(int w, int h, int y_rsize, int u_rsize, int v_rsize, int cu, int q, int b) { jpeg_enc_t *j; int i = 0; VERBOSE("JPEG encoder init: %dx%d %d %d %d cu=%d q=%d bw=%d\n", w, h, y_rsize, u_rsize, v_rsize, cu, q, b); j = av_mallocz(sizeof(jpeg_enc_t)); if (j == NULL) return NULL; j->s = av_mallocz(sizeof(MpegEncContext)); if (j->s == NULL) { av_free(j); return NULL; } /* info on how to access the pixels */ j->y_rs = y_rsize; j->u_rs = u_rsize; j->v_rs = v_rsize; j->s->width = w; // image width and height j->s->height = h; j->s->qscale = q; // Encoding quality j->s->out_format = FMT_MJPEG; j->s->intra_only = 1; // Generate only intra pictures for jpeg j->s->encoding = 1; // Set mode to encode j->s->pict_type = AV_PICTURE_TYPE_I; j->s->y_dc_scale = 8; j->s->c_dc_scale = 8; /* * This sets up the MCU (Minimal Code Unit) number * of appearances of the various component * for the SOF0 table in the generated MJPEG. * The values are not used for anything else. * The current setup is simply YUV422, with two horizontal Y components * for every UV component. */ //FIXME j->s->mjpeg_write_tables = 1; // setup to write tables j->s->mjpeg_vsample[0] = 1; // 1 appearance of Y vertically j->s->mjpeg_vsample[1] = 1; // 1 appearance of U vertically j->s->mjpeg_vsample[2] = 1; // 1 appearance of V vertically j->s->mjpeg_hsample[0] = 2; // 2 appearances of Y horizontally j->s->mjpeg_hsample[1] = 1; // 1 appearance of U horizontally j->s->mjpeg_hsample[2] = 1; // 1 appearance of V horizontally j->cheap_upsample = cu; j->bw = b; init_avcodec(); // Build mjpeg huffman code tables, setting up j->s->mjpeg_ctx if (ff_mjpeg_encode_init(j->s) < 0) { av_free(j->s); av_free(j); return NULL; } /* alloc bogus avctx to keep MPV_common_init from segfaulting */ j->s->avctx = avcodec_alloc_context(); if (j->s->avctx == NULL) { av_free(j->s); av_free(j); return NULL; } // Set some a minimum amount of default values that are needed // Indicates that we should generated normal MJPEG j->s->avctx->codec_id = AV_CODEC_ID_MJPEG; // Which DCT method to use. AUTO will select the fastest one j->s->avctx->dct_algo = FF_DCT_AUTO; j->s->intra_quant_bias= 1<<(QUANT_BIAS_SHIFT-1); //(a + x/2)/x // indicate we 'decode' to jpeg 4:2:2 j->s->avctx->pix_fmt = PIX_FMT_YUVJ422P; j->s->avctx->thread_count = 1; /* make MPV_common_init allocate important buffers, like s->block * Also initializes dsputil */ if (ff_MPV_common_init(j->s) < 0) { av_free(j->s); av_free(j); return NULL; } /* correct the value for sc->mb_height. MPV_common_init put other * values there */ j->s->mb_height = j->s->height/8; j->s->mb_intra = 1; // Init q matrix j->s->intra_matrix[0] = ff_mpeg1_default_intra_matrix[0]; for (i = 1; i < 64; i++) j->s->intra_matrix[i] = av_clip_uint8( (ff_mpeg1_default_intra_matrix[i]*j->s->qscale) >> 3); // precompute matrix convert_matrix(j->s, j->s->q_intra_matrix, j->s->q_intra_matrix16, j->s->intra_matrix, j->s->intra_quant_bias, 8, 8); /* Pick up the selection of the optimal get_pixels() routine * to use, which was done in MPV_common_init() */ get_pixels = j->s->dsp.get_pixels; return j; }
/* The encoder doesn't know anything about interlacing, the halve height * needs to be passed and the double rowstride. Which field gets encoded * is decided by what buffers are passed to mjpeg_encode_frame */ jpeg_enc_t *jpeg_enc_init(int w, int h, int y_psize, int y_rsize, int u_psize, int u_rsize, int v_psize, int v_rsize, int cu, int q, int b) { jpeg_enc_t *j; int i = 0; mp_msg(MSGT_VO, MSGL_V, "JPEnc init: %dx%d %d %d %d %d %d %d\n", w, h, y_psize, y_rsize, u_psize, u_rsize, v_psize, v_rsize); j = av_malloc(sizeof(jpeg_enc_t)); if (j == NULL) return NULL; j->s = av_malloc(sizeof(MpegEncContext)); memset(j->s,0x00,sizeof(MpegEncContext)); if (j->s == NULL) { av_free(j); return NULL; } /* info on how to access the pixels */ j->y_ps = y_psize; j->u_ps = u_psize; j->v_ps = v_psize; j->y_rs = y_rsize; j->u_rs = u_rsize; j->v_rs = v_rsize; j->s->width = w; j->s->height = h; j->s->qscale = q; j->s->out_format = FMT_MJPEG; j->s->intra_only = 1; j->s->encoding = 1; j->s->pict_type = I_TYPE; j->s->y_dc_scale = 8; j->s->c_dc_scale = 8; //FIXME j->s->mjpeg_write_tables = 1; j->s->mjpeg_vsample[0] = 1; j->s->mjpeg_vsample[1] = 1; j->s->mjpeg_vsample[2] = 1; j->s->mjpeg_hsample[0] = 2; j->s->mjpeg_hsample[1] = 1; j->s->mjpeg_hsample[2] = 1; j->cheap_upsample = cu; j->bw = b; /* if libavcodec is used by the decoder then we must not * initialize again, but if it is not initialized then we must * initialize it here. */ if (!avcodec_inited) { /* we need to initialize libavcodec */ avcodec_init(); avcodec_register_all(); avcodec_inited=1; } if (ff_mjpeg_encode_init(j->s) < 0) { av_free(j->s); av_free(j); return NULL; } /* alloc bogus avctx to keep MPV_common_init from segfaulting */ j->s->avctx = calloc(sizeof(*j->s->avctx), 1); /* Set up to encode mjpeg */ j->s->avctx->codec_id = CODEC_ID_MJPEG; /* make MPV_common_init allocate important buffers, like s->block */ j->s->avctx->thread_count = 1; if (MPV_common_init(j->s) < 0) { av_free(j->s); av_free(j); return NULL; } /* correct the value for sc->mb_height */ j->s->mb_height = j->s->height/8; j->s->mb_intra = 1; j->s->intra_matrix[0] = ff_mpeg1_default_intra_matrix[0]; for (i = 1; i < 64; i++) j->s->intra_matrix[i] = av_clip_uint8( (ff_mpeg1_default_intra_matrix[i]*j->s->qscale) >> 3); convert_matrix(j->s, j->s->q_intra_matrix, j->s->q_intra_matrix16, j->s->intra_matrix, j->s->intra_quant_bias, 8, 8); return j; }