libavcodec/mjpegenc.c
4922a5b0
 /*
  * MJPEG encoder
406792e7
  * Copyright (c) 2000, 2001 Fabrice Bellard
4922a5b0
  * Copyright (c) 2003 Alex Beregszaszi
  * Copyright (c) 2003-2004 Michael Niedermayer
  *
7b94177e
  * Support for external huffman table, various fixes (AVID workaround),
  * aspecting, new decode_frame mechanism and apple mjpeg-b support
  *                                  by Alex Beregszaszi
  *
4922a5b0
  * This file is part of FFmpeg.
  *
  * 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.
  *
  * 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 FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 /**
ba87f080
  * @file
4922a5b0
  * MJPEG encoder.
  */
 
058d5f2f
 #include "libavutil/pixdesc.h"
 
4922a5b0
 #include "avcodec.h"
4978850c
 #include "jpegtables.h"
adcb8392
 #include "mjpegenc_common.h"
4922a5b0
 #include "mpegvideo.h"
 #include "mjpeg.h"
d9c9259f
 #include "mjpegenc.h"
4922a5b0
 
7cac568b
 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
         }
     }
 }
 
98a6fff9
 av_cold int ff_mjpeg_encode_init(MpegEncContext *s)
4922a5b0
 {
     MJpegContext *m;
 
d2581dcc
     if (s->width > 65500 || s->height > 65500) {
         av_log(s, AV_LOG_ERROR, "JPEG does not support resolutions above 65500x65500\n");
2ebaadf3
         return AVERROR(EINVAL);
d2581dcc
     }
 
4922a5b0
     m = av_malloc(sizeof(MJpegContext));
     if (!m)
2ebaadf3
         return AVERROR(ENOMEM);
4922a5b0
 
     s->min_qcoeff=-1023;
     s->max_qcoeff= 1023;
 
     /* build all the huffman tables */
944721c1
     ff_mjpeg_build_huffman_codes(m->huff_size_dc_luminance,
                                  m->huff_code_dc_luminance,
a0845bae
                                  avpriv_mjpeg_bits_dc_luminance,
                                  avpriv_mjpeg_val_dc);
944721c1
     ff_mjpeg_build_huffman_codes(m->huff_size_dc_chrominance,
                                  m->huff_code_dc_chrominance,
a0845bae
                                  avpriv_mjpeg_bits_dc_chrominance,
                                  avpriv_mjpeg_val_dc);
944721c1
     ff_mjpeg_build_huffman_codes(m->huff_size_ac_luminance,
                                  m->huff_code_ac_luminance,
a0845bae
                                  avpriv_mjpeg_bits_ac_luminance,
                                  avpriv_mjpeg_val_ac_luminance);
944721c1
     ff_mjpeg_build_huffman_codes(m->huff_size_ac_chrominance,
                                  m->huff_code_ac_chrominance,
a0845bae
                                  avpriv_mjpeg_bits_ac_chrominance,
                                  avpriv_mjpeg_val_ac_chrominance);
4922a5b0
 
7cac568b
     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;
 
4922a5b0
     s->mjpeg_ctx = m;
     return 0;
 }
 
f8b743b9
 av_cold void ff_mjpeg_encode_close(MpegEncContext *s)
4922a5b0
 {
0c6c5d7b
     av_freep(&s->mjpeg_ctx);
4922a5b0
 }
 
88bd7fdc
 static void encode_block(MpegEncContext *s, int16_t *block, int n)
4922a5b0
 {
     int mant, nbits, code, i, j;
     int component, dc, run, last_index, val;
     MJpegContext *m = s->mjpeg_ctx;
     uint8_t *huff_size_ac;
     uint16_t *huff_code_ac;
 
     /* DC coef */
     component = (n <= 3 ? 0 : (n&1) + 1);
     dc = block[0]; /* overflow is impossible */
     val = dc - s->last_dc[component];
     if (n < 4) {
86eb2eaa
         ff_mjpeg_encode_dc(&s->pb, val, m->huff_size_dc_luminance, m->huff_code_dc_luminance);
4922a5b0
         huff_size_ac = m->huff_size_ac_luminance;
         huff_code_ac = m->huff_code_ac_luminance;
     } else {
86eb2eaa
         ff_mjpeg_encode_dc(&s->pb, val, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
4922a5b0
         huff_size_ac = m->huff_size_ac_chrominance;
         huff_code_ac = m->huff_code_ac_chrominance;
     }
     s->last_dc[component] = dc;
 
     /* AC coefs */
 
     run = 0;
     last_index = s->block_last_index[n];
     for(i=1;i<=last_index;i++) {
         j = s->intra_scantable.permutated[i];
         val = block[j];
         if (val == 0) {
             run++;
         } else {
             while (run >= 16) {
                 put_bits(&s->pb, huff_size_ac[0xf0], huff_code_ac[0xf0]);
                 run -= 16;
             }
             mant = val;
             if (val < 0) {
                 val = -val;
                 mant--;
             }
 
6005ab3e
             nbits= av_log2_16bit(val) + 1;
4922a5b0
             code = (run << 4) | nbits;
 
             put_bits(&s->pb, huff_size_ac[code], huff_code_ac[code]);
 
6647ab80
             put_sbits(&s->pb, nbits, mant);
4922a5b0
             run = 0;
         }
     }
 
     /* output EOB only if not already 64 values */
     if (last_index < 63 || run != 0)
         put_bits(&s->pb, huff_size_ac[0], huff_code_ac[0]);
 }
 
256f5306
 void ff_mjpeg_encode_mb(MpegEncContext *s, int16_t block[12][64])
4922a5b0
 {
     int i;
633ae5a2
     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);
9f02d4ed
         encode_block(s, block[5], 5);
633ae5a2
         encode_block(s, block[9], 9);
 
28b85b1f
         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);
         }
633ae5a2
     } else {
         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);
         }
4922a5b0
     }
23b8342a
 
     s->i_tex_bits += get_bits_diff(s);
4922a5b0
 }
63ab52b7
 
f2f28861
 // maximum over s->mjpeg_vsample[i]
 #define V_MAX 2
be2e211d
 static int amv_encode_picture(AVCodecContext *avctx, AVPacket *pkt,
7d75febe
                               const AVFrame *pic_arg, int *got_packet)
f2f28861
 
be2e211d
 {
f2f28861
     MpegEncContext *s = avctx->priv_data;
5b3f4b3e
     AVFrame *pic;
     int i, ret;
93947d88
     int chroma_h_shift, chroma_v_shift;
 
     av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift, &chroma_v_shift);
f2f28861
 
2fb593dc
 #if FF_API_EMU_EDGE
f2f28861
     //CODEC_FLAG_EMU_EDGE have to be cleared
     if(s->avctx->flags & CODEC_FLAG_EMU_EDGE)
2ebaadf3
         return AVERROR(EINVAL);
2fb593dc
 #endif
f2f28861
 
d41a4a76
     if ((avctx->height & 15) && avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL) {
181b2c37
         av_log(avctx, AV_LOG_ERROR,
d41a4a76
                "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;
181b2c37
     }
 
a26e9c10
     pic = av_frame_clone(pic_arg);
19a2d101
     if (!pic)
         return AVERROR(ENOMEM);
f2f28861
     //picture should be flipped upside-down
     for(i=0; i < 3; i++) {
93947d88
         int vsample = i ? 2 >> chroma_v_shift : 2;
6d695d7a
         pic->data[i] += pic->linesize[i] * (vsample * s->height / V_MAX - 1);
5b3f4b3e
         pic->linesize[i] *= -1;
f2f28861
     }
c1df467d
     ret = ff_mpv_encode_picture(avctx, pkt, pic, got_packet);
5b3f4b3e
     av_frame_free(&pic);
     return ret;
f2f28861
 }
 
2862b637
 #define OFFSET(x) offsetof(MpegEncContext, x)
 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
 static const AVOption options[] = {
f3af379b
 FF_MPV_COMMON_OPTS
2862b637
 { "pred", "Prediction method", OFFSET(pred), AV_OPT_TYPE_INT, { .i64 = 1 }, 1, 3, VE, "pred" },
     { "left",   NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX, VE, "pred" },
     { "plane",  NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 2 }, INT_MIN, INT_MAX, VE, "pred" },
     { "median", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 3 }, INT_MIN, INT_MAX, VE, "pred" },
 
f3af379b
 { NULL},
2862b637
 };
 
137e8081
 #if CONFIG_MJPEG_ENCODER
dbf172e6
 
 static const AVClass mjpeg_class = {
     .class_name = "mjpeg encoder",
     .item_name  = av_default_item_name,
2862b637
     .option     = options,
dbf172e6
     .version    = LIBAVUTIL_VERSION_INT,
 };
ae6ba2db
 
e7e2df27
 AVCodec ff_mjpeg_encoder = {
ec6402b7
     .name           = "mjpeg",
b2bed932
     .long_name      = NULL_IF_CONFIG_SMALL("MJPEG (Motion JPEG)"),
ec6402b7
     .type           = AVMEDIA_TYPE_VIDEO,
36ef5369
     .id             = AV_CODEC_ID_MJPEG,
ec6402b7
     .priv_data_size = sizeof(MpegEncContext),
835f798c
     .init           = ff_mpv_encode_init,
     .encode2        = ff_mpv_encode_picture,
     .close          = ff_mpv_encode_end,
444e9874
     .capabilities   = AV_CODEC_CAP_SLICE_THREADS | AV_CODEC_CAP_FRAME_THREADS | AV_CODEC_CAP_INTRA_ONLY,
716d413c
     .pix_fmts       = (const enum AVPixelFormat[]){
9f02d4ed
         AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_NONE
00c3b67b
     },
ae6ba2db
     .priv_class     = &mjpeg_class,
63ab52b7
 };
137e8081
 #endif
 #if CONFIG_AMV_ENCODER
dbf172e6
 static const AVClass amv_class = {
     .class_name = "amv encoder",
     .item_name  = av_default_item_name,
f3af379b
     .option     = options,
dbf172e6
     .version    = LIBAVUTIL_VERSION_INT,
 };
ae6ba2db
 
f2f28861
 AVCodec ff_amv_encoder = {
023e9960
     .name           = "amv",
b46f1910
     .long_name      = NULL_IF_CONFIG_SMALL("AMV Video"),
023e9960
     .type           = AVMEDIA_TYPE_VIDEO,
7a72695c
     .id             = AV_CODEC_ID_AMV,
023e9960
     .priv_data_size = sizeof(MpegEncContext),
c1df467d
     .init           = ff_mpv_encode_init,
be2e211d
     .encode2        = amv_encode_picture,
c1df467d
     .close          = ff_mpv_encode_end,
ac627b3d
     .pix_fmts       = (const enum AVPixelFormat[]){
b227be34
         AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_NONE
f06269dd
     },
ae6ba2db
     .priv_class     = &amv_class,
f2f28861
 };
137e8081
 #endif