3 * Copyright (c) 2000, 2001 Fabrice Bellard
4 * Copyright (c) 2003 Alex Beregszaszi
5 * Copyright (c) 2003-2004 Michael Niedermayer
7 * Support for external huffman table, various fixes (AVID workaround),
8 * aspecting, new decode_frame mechanism and apple mjpeg-b support
11 * This file is part of FFmpeg.
13 * FFmpeg is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation; either
16 * version 2.1 of the License, or (at your option) any later version.
18 * FFmpeg is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with FFmpeg; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
33 #include "libavutil/pixdesc.h"
36 #include "mjpegenc_common.h"
37 #include "mpegvideo.h"
41 static uint8_t uni_ac_vlc_len[64 * 64 * 2];
42 static uint8_t uni_chroma_ac_vlc_len[64 * 64 * 2];
44 static av_cold void init_uni_ac_vlc(const uint8_t huff_size_ac[256], uint8_t *uni_ac_vlc_len)
48 for (i = 0; i < 128; i++) {
53 for (run = 0; run < 64; run++) {
55 int alevel = FFABS(level);
57 len = (run >> 4) * huff_size_ac[0xf0];
59 nbits= av_log2_16bit(alevel) + 1;
60 code = ((15&run) << 4) | nbits;
62 len += huff_size_ac[code] + nbits;
64 uni_ac_vlc_len[UNI_AC_ENC_INDEX(run, i)] = len;
65 // We ignore EOB as its just a constant which does not change generally
70 av_cold int ff_mjpeg_encode_init(MpegEncContext *s)
74 if (s->width > 65500 || s->height > 65500) {
75 av_log(s, AV_LOG_ERROR, "JPEG does not support resolutions above 65500x65500\n");
76 return AVERROR(EINVAL);
79 m = av_malloc(sizeof(MJpegContext));
81 return AVERROR(ENOMEM);
86 /* build all the huffman tables */
87 ff_mjpeg_build_huffman_codes(m->huff_size_dc_luminance,
88 m->huff_code_dc_luminance,
89 avpriv_mjpeg_bits_dc_luminance,
91 ff_mjpeg_build_huffman_codes(m->huff_size_dc_chrominance,
92 m->huff_code_dc_chrominance,
93 avpriv_mjpeg_bits_dc_chrominance,
95 ff_mjpeg_build_huffman_codes(m->huff_size_ac_luminance,
96 m->huff_code_ac_luminance,
97 avpriv_mjpeg_bits_ac_luminance,
98 avpriv_mjpeg_val_ac_luminance);
99 ff_mjpeg_build_huffman_codes(m->huff_size_ac_chrominance,
100 m->huff_code_ac_chrominance,
101 avpriv_mjpeg_bits_ac_chrominance,
102 avpriv_mjpeg_val_ac_chrominance);
104 init_uni_ac_vlc(m->huff_size_ac_luminance, uni_ac_vlc_len);
105 init_uni_ac_vlc(m->huff_size_ac_chrominance, uni_chroma_ac_vlc_len);
106 s->intra_ac_vlc_length =
107 s->intra_ac_vlc_last_length = uni_ac_vlc_len;
108 s->intra_chroma_ac_vlc_length =
109 s->intra_chroma_ac_vlc_last_length = uni_chroma_ac_vlc_len;
115 av_cold void ff_mjpeg_encode_close(MpegEncContext *s)
117 av_freep(&s->mjpeg_ctx);
120 static void encode_block(MpegEncContext *s, int16_t *block, int n)
122 int mant, nbits, code, i, j;
123 int component, dc, run, last_index, val;
124 MJpegContext *m = s->mjpeg_ctx;
125 uint8_t *huff_size_ac;
126 uint16_t *huff_code_ac;
129 component = (n <= 3 ? 0 : (n&1) + 1);
130 dc = block[0]; /* overflow is impossible */
131 val = dc - s->last_dc[component];
133 ff_mjpeg_encode_dc(&s->pb, val, m->huff_size_dc_luminance, m->huff_code_dc_luminance);
134 huff_size_ac = m->huff_size_ac_luminance;
135 huff_code_ac = m->huff_code_ac_luminance;
137 ff_mjpeg_encode_dc(&s->pb, val, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
138 huff_size_ac = m->huff_size_ac_chrominance;
139 huff_code_ac = m->huff_code_ac_chrominance;
141 s->last_dc[component] = dc;
146 last_index = s->block_last_index[n];
147 for(i=1;i<=last_index;i++) {
148 j = s->intra_scantable.permutated[i];
154 put_bits(&s->pb, huff_size_ac[0xf0], huff_code_ac[0xf0]);
163 nbits= av_log2_16bit(val) + 1;
164 code = (run << 4) | nbits;
166 put_bits(&s->pb, huff_size_ac[code], huff_code_ac[code]);
168 put_sbits(&s->pb, nbits, mant);
173 /* output EOB only if not already 64 values */
174 if (last_index < 63 || run != 0)
175 put_bits(&s->pb, huff_size_ac[0], huff_code_ac[0]);
178 void ff_mjpeg_encode_mb(MpegEncContext *s, int16_t block[12][64])
181 if (s->chroma_format == CHROMA_444) {
182 encode_block(s, block[0], 0);
183 encode_block(s, block[2], 2);
184 encode_block(s, block[4], 4);
185 encode_block(s, block[8], 8);
186 encode_block(s, block[5], 5);
187 encode_block(s, block[9], 9);
189 if (16*s->mb_x+8 < s->width) {
190 encode_block(s, block[1], 1);
191 encode_block(s, block[3], 3);
192 encode_block(s, block[6], 6);
193 encode_block(s, block[10], 10);
194 encode_block(s, block[7], 7);
195 encode_block(s, block[11], 11);
199 encode_block(s, block[i], i);
201 if (s->chroma_format == CHROMA_420) {
202 encode_block(s, block[5], 5);
204 encode_block(s, block[6], 6);
205 encode_block(s, block[5], 5);
206 encode_block(s, block[7], 7);
210 s->i_tex_bits += get_bits_diff(s);
213 // maximum over s->mjpeg_vsample[i]
215 static int amv_encode_picture(AVCodecContext *avctx, AVPacket *pkt,
216 const AVFrame *pic_arg, int *got_packet)
219 MpegEncContext *s = avctx->priv_data;
222 int chroma_h_shift, chroma_v_shift;
224 av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift, &chroma_v_shift);
226 //CODEC_FLAG_EMU_EDGE have to be cleared
227 if(s->avctx->flags & CODEC_FLAG_EMU_EDGE)
228 return AVERROR(EINVAL);
230 if ((avctx->height & 15) && avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL) {
231 av_log(avctx, AV_LOG_ERROR,
232 "Heights which are not a multiple of 16 might fail with some decoders, "
233 "use vstrict=-1 / -strict -1 to use %d anyway.\n", avctx->height);
234 av_log(avctx, AV_LOG_WARNING, "If you have a device that plays AMV videos, please test if videos "
235 "with such heights work with it and report your findings to ffmpeg-devel@ffmpeg.org\n");
236 return AVERROR_EXPERIMENTAL;
239 pic = av_frame_clone(pic_arg);
241 return AVERROR(ENOMEM);
242 //picture should be flipped upside-down
243 for(i=0; i < 3; i++) {
244 int vsample = i ? 2 >> chroma_v_shift : 2;
245 pic->data[i] += pic->linesize[i] * (vsample * s->height / V_MAX - 1);
246 pic->linesize[i] *= -1;
248 ret = ff_mpv_encode_picture(avctx, pkt, pic, got_packet);
253 #if CONFIG_MJPEG_ENCODER
254 FF_MPV_GENERIC_CLASS(mjpeg)
256 AVCodec ff_mjpeg_encoder = {
258 .long_name = NULL_IF_CONFIG_SMALL("MJPEG (Motion JPEG)"),
259 .type = AVMEDIA_TYPE_VIDEO,
260 .id = AV_CODEC_ID_MJPEG,
261 .priv_data_size = sizeof(MpegEncContext),
262 .init = ff_mpv_encode_init,
263 .encode2 = ff_mpv_encode_picture,
264 .close = ff_mpv_encode_end,
265 .capabilities = CODEC_CAP_SLICE_THREADS | CODEC_CAP_FRAME_THREADS | CODEC_CAP_INTRA_ONLY,
266 .pix_fmts = (const enum AVPixelFormat[]){
267 AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_NONE
269 .priv_class = &mjpeg_class,
272 #if CONFIG_AMV_ENCODER
273 FF_MPV_GENERIC_CLASS(amv)
275 AVCodec ff_amv_encoder = {
277 .long_name = NULL_IF_CONFIG_SMALL("AMV Video"),
278 .type = AVMEDIA_TYPE_VIDEO,
279 .id = AV_CODEC_ID_AMV,
280 .priv_data_size = sizeof(MpegEncContext),
281 .init = ff_mpv_encode_init,
282 .encode2 = amv_encode_picture,
283 .close = ff_mpv_encode_end,
284 .pix_fmts = (const enum AVPixelFormat[]){
285 AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_NONE
287 .priv_class = &amv_class,