diff options
Diffstat (limited to 'libavcodec/mjpegenc.c')
-rw-r--r-- | libavcodec/mjpegenc.c | 157 |
1 files changed, 139 insertions, 18 deletions
diff --git a/libavcodec/mjpegenc.c b/libavcodec/mjpegenc.c index a460886..24f8d1d 100644 --- a/libavcodec/mjpegenc.c +++ b/libavcodec/mjpegenc.c @@ -8,20 +8,20 @@ * aspecting, new decode_frame mechanism and apple mjpeg-b support * by Alex Beregszaszi * - * This file is part of Libav. + * This file is part of FFmpeg. * - * Libav is free software; you can redistribute it and/or + * FFmpeg is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * - * Libav is distributed in the hope that it will be useful, + * FFmpeg is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with Libav; if not, write to the Free Software + * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ @@ -30,8 +30,6 @@ * MJPEG encoder. */ -#include <assert.h> - #include "libavutil/pixdesc.h" #include "avcodec.h" @@ -40,10 +38,44 @@ #include "mjpeg.h" #include "mjpegenc.h" +static uint8_t uni_ac_vlc_len[64 * 64 * 2]; +static uint8_t uni_chroma_ac_vlc_len[64 * 64 * 2]; + +static av_cold void init_uni_ac_vlc(const uint8_t huff_size_ac[256], uint8_t *uni_ac_vlc_len) +{ + int i; + + for (i = 0; i < 128; i++) { + int level = i - 64; + int run; + if (!level) + continue; + for (run = 0; run < 64; run++) { + int len, code, nbits; + int alevel = FFABS(level); + + len = (run >> 4) * huff_size_ac[0xf0]; + + nbits= av_log2_16bit(alevel) + 1; + code = ((15&run) << 4) | nbits; + + len += huff_size_ac[code] + nbits; + + uni_ac_vlc_len[UNI_AC_ENC_INDEX(run, i)] = len; + // We ignore EOB as its just a constant which does not change generally + } + } +} + av_cold int ff_mjpeg_encode_init(MpegEncContext *s) { MJpegContext *m; + if (s->width > 65500 || s->height > 65500) { + av_log(s, AV_LOG_ERROR, "JPEG does not support resolutions above 65500x65500\n"); + return AVERROR(EINVAL); + } + m = av_malloc(sizeof(MJpegContext)); if (!m) return AVERROR(ENOMEM); @@ -69,13 +101,20 @@ av_cold int ff_mjpeg_encode_init(MpegEncContext *s) avpriv_mjpeg_bits_ac_chrominance, avpriv_mjpeg_val_ac_chrominance); + init_uni_ac_vlc(m->huff_size_ac_luminance, uni_ac_vlc_len); + init_uni_ac_vlc(m->huff_size_ac_chrominance, uni_chroma_ac_vlc_len); + s->intra_ac_vlc_length = + s->intra_ac_vlc_last_length = uni_ac_vlc_len; + s->intra_chroma_ac_vlc_length = + s->intra_chroma_ac_vlc_last_length = uni_chroma_ac_vlc_len; + s->mjpeg_ctx = m; return 0; } -void ff_mjpeg_encode_close(MpegEncContext *s) +av_cold void ff_mjpeg_encode_close(MpegEncContext *s) { - av_free(s->mjpeg_ctx); + av_freep(&s->mjpeg_ctx); } static void encode_block(MpegEncContext *s, int16_t *block, int n) @@ -121,7 +160,7 @@ static void encode_block(MpegEncContext *s, int16_t *block, int n) mant--; } - nbits= av_log2(val) + 1; + nbits= av_log2_16bit(val) + 1; code = (run << 4) | nbits; put_bits(&s->pb, huff_size_ac[code], huff_code_ac[code]); @@ -136,23 +175,84 @@ static void encode_block(MpegEncContext *s, int16_t *block, int n) put_bits(&s->pb, huff_size_ac[0], huff_code_ac[0]); } -void ff_mjpeg_encode_mb(MpegEncContext *s, int16_t block[8][64]) +void ff_mjpeg_encode_mb(MpegEncContext *s, int16_t block[12][64]) { int i; - for(i=0;i<5;i++) { - encode_block(s, block[i], i); - } - if (s->chroma_format == CHROMA_420) { + if (s->chroma_format == CHROMA_444) { + encode_block(s, block[0], 0); + encode_block(s, block[2], 2); + encode_block(s, block[4], 4); + encode_block(s, block[8], 8); encode_block(s, block[5], 5); + encode_block(s, block[9], 9); + + if (16*s->mb_x+8 < s->width) { + encode_block(s, block[1], 1); + encode_block(s, block[3], 3); + encode_block(s, block[6], 6); + encode_block(s, block[10], 10); + encode_block(s, block[7], 7); + encode_block(s, block[11], 11); + } } else { - encode_block(s, block[6], 6); - encode_block(s, block[5], 5); - encode_block(s, block[7], 7); + for(i=0;i<5;i++) { + encode_block(s, block[i], i); + } + if (s->chroma_format == CHROMA_420) { + encode_block(s, block[5], 5); + } else { + encode_block(s, block[6], 6); + encode_block(s, block[5], 5); + encode_block(s, block[7], 7); + } } s->i_tex_bits += get_bits_diff(s); } +// maximum over s->mjpeg_vsample[i] +#define V_MAX 2 +static int amv_encode_picture(AVCodecContext *avctx, AVPacket *pkt, + const AVFrame *pic_arg, int *got_packet) + +{ + MpegEncContext *s = avctx->priv_data; + AVFrame *pic; + int i, ret; + int chroma_h_shift, chroma_v_shift; + + av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift, &chroma_v_shift); + + //CODEC_FLAG_EMU_EDGE have to be cleared + if(s->avctx->flags & CODEC_FLAG_EMU_EDGE) + return AVERROR(EINVAL); + + if ((avctx->height & 15) && avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL) { + av_log(avctx, AV_LOG_ERROR, + "Heights which are not a multiple of 16 might fail with some decoders, " + "use vstrict=-1 / -strict -1 to use %d anyway.\n", avctx->height); + av_log(avctx, AV_LOG_WARNING, "If you have a device that plays AMV videos, please test if videos " + "with such heights work with it and report your findings to ffmpeg-devel@ffmpeg.org\n"); + return AVERROR_EXPERIMENTAL; + } + + pic = av_frame_clone(pic_arg); + if (!pic) + return AVERROR(ENOMEM); + //picture should be flipped upside-down + for(i=0; i < 3; i++) { + int vsample = i ? 2 >> chroma_v_shift : 2; + pic->data[i] += pic->linesize[i] * (vsample * s->height / V_MAX - 1); + pic->linesize[i] *= -1; + } + ret = ff_mpv_encode_picture(avctx, pkt, pic, got_packet); + av_frame_free(&pic); + return ret; +} + +#if CONFIG_MJPEG_ENCODER +FF_MPV_GENERIC_CLASS(mjpeg) + AVCodec ff_mjpeg_encoder = { .name = "mjpeg", .long_name = NULL_IF_CONFIG_SMALL("MJPEG (Motion JPEG)"), @@ -162,7 +262,28 @@ AVCodec ff_mjpeg_encoder = { .init = ff_mpv_encode_init, .encode2 = ff_mpv_encode_picture, .close = ff_mpv_encode_end, + .capabilities = CODEC_CAP_SLICE_THREADS | CODEC_CAP_FRAME_THREADS | CODEC_CAP_INTRA_ONLY, + .pix_fmts = (const enum AVPixelFormat[]){ + AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_NONE + }, + .priv_class = &mjpeg_class, +}; +#endif +#if CONFIG_AMV_ENCODER +FF_MPV_GENERIC_CLASS(amv) + +AVCodec ff_amv_encoder = { + .name = "amv", + .long_name = NULL_IF_CONFIG_SMALL("AMV Video"), + .type = AVMEDIA_TYPE_VIDEO, + .id = AV_CODEC_ID_AMV, + .priv_data_size = sizeof(MpegEncContext), + .init = ff_mpv_encode_init, + .encode2 = amv_encode_picture, + .close = ff_mpv_encode_end, .pix_fmts = (const enum AVPixelFormat[]){ - AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_NONE + AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_NONE }, + .priv_class = &amv_class, }; +#endif |