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 av_cold int ff_mjpeg_encode_init(MpegEncContext *s)
45 if (s->width > 65500 || s->height > 65500) {
46 av_log(s, AV_LOG_ERROR, "JPEG does not support resolutions above 65500x65500\n");
47 return AVERROR(EINVAL);
50 m = av_malloc(sizeof(MJpegContext));
52 return AVERROR(ENOMEM);
57 /* build all the huffman tables */
58 ff_mjpeg_build_huffman_codes(m->huff_size_dc_luminance,
59 m->huff_code_dc_luminance,
60 avpriv_mjpeg_bits_dc_luminance,
62 ff_mjpeg_build_huffman_codes(m->huff_size_dc_chrominance,
63 m->huff_code_dc_chrominance,
64 avpriv_mjpeg_bits_dc_chrominance,
66 ff_mjpeg_build_huffman_codes(m->huff_size_ac_luminance,
67 m->huff_code_ac_luminance,
68 avpriv_mjpeg_bits_ac_luminance,
69 avpriv_mjpeg_val_ac_luminance);
70 ff_mjpeg_build_huffman_codes(m->huff_size_ac_chrominance,
71 m->huff_code_ac_chrominance,
72 avpriv_mjpeg_bits_ac_chrominance,
73 avpriv_mjpeg_val_ac_chrominance);
79 void ff_mjpeg_encode_close(MpegEncContext *s)
81 av_free(s->mjpeg_ctx);
84 static void encode_block(MpegEncContext *s, int16_t *block, int n)
86 int mant, nbits, code, i, j;
87 int component, dc, run, last_index, val;
88 MJpegContext *m = s->mjpeg_ctx;
89 uint8_t *huff_size_ac;
90 uint16_t *huff_code_ac;
93 component = (n <= 3 ? 0 : (n&1) + 1);
94 dc = block[0]; /* overflow is impossible */
95 val = dc - s->last_dc[component];
97 ff_mjpeg_encode_dc(&s->pb, val, m->huff_size_dc_luminance, m->huff_code_dc_luminance);
98 huff_size_ac = m->huff_size_ac_luminance;
99 huff_code_ac = m->huff_code_ac_luminance;
101 ff_mjpeg_encode_dc(&s->pb, val, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
102 huff_size_ac = m->huff_size_ac_chrominance;
103 huff_code_ac = m->huff_code_ac_chrominance;
105 s->last_dc[component] = dc;
110 last_index = s->block_last_index[n];
111 for(i=1;i<=last_index;i++) {
112 j = s->intra_scantable.permutated[i];
118 put_bits(&s->pb, huff_size_ac[0xf0], huff_code_ac[0xf0]);
127 nbits= av_log2_16bit(val) + 1;
128 code = (run << 4) | nbits;
130 put_bits(&s->pb, huff_size_ac[code], huff_code_ac[code]);
132 put_sbits(&s->pb, nbits, mant);
137 /* output EOB only if not already 64 values */
138 if (last_index < 63 || run != 0)
139 put_bits(&s->pb, huff_size_ac[0], huff_code_ac[0]);
142 void ff_mjpeg_encode_mb(MpegEncContext *s, int16_t block[12][64])
145 if (s->chroma_format == CHROMA_444) {
146 encode_block(s, block[0], 0);
147 encode_block(s, block[2], 2);
148 encode_block(s, block[4], 4);
149 encode_block(s, block[8], 8);
150 encode_block(s, block[5], 5);
151 encode_block(s, block[9], 9);
153 if (16*s->mb_x+8 < s->width) {
154 encode_block(s, block[1], 1);
155 encode_block(s, block[3], 3);
156 encode_block(s, block[6], 6);
157 encode_block(s, block[10], 10);
158 encode_block(s, block[7], 7);
159 encode_block(s, block[11], 11);
163 encode_block(s, block[i], i);
165 if (s->chroma_format == CHROMA_420) {
166 encode_block(s, block[5], 5);
168 encode_block(s, block[6], 6);
169 encode_block(s, block[5], 5);
170 encode_block(s, block[7], 7);
174 s->i_tex_bits += get_bits_diff(s);
177 // maximum over s->mjpeg_vsample[i]
179 static int amv_encode_picture(AVCodecContext *avctx, AVPacket *pkt,
180 const AVFrame *pic_arg, int *got_packet)
183 MpegEncContext *s = avctx->priv_data;
186 int chroma_h_shift, chroma_v_shift;
188 av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift, &chroma_v_shift);
190 //CODEC_FLAG_EMU_EDGE have to be cleared
191 if(s->avctx->flags & CODEC_FLAG_EMU_EDGE)
192 return AVERROR(EINVAL);
194 if ((avctx->height & 15) && avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL) {
195 av_log(avctx, AV_LOG_ERROR,
196 "Heights which are not a multiple of 16 might fail with some decoders, "
197 "use vstrict=-1 / -strict -1 to use %d anyway.\n", avctx->height);
198 av_log(avctx, AV_LOG_WARNING, "If you have a device that plays AMV videos, please test if videos "
199 "with such heights work with it and report your findings to ffmpeg-devel@ffmpeg.org\n");
200 return AVERROR_EXPERIMENTAL;
203 pic = av_frame_clone(pic_arg);
205 return AVERROR(ENOMEM);
206 //picture should be flipped upside-down
207 for(i=0; i < 3; i++) {
208 int vsample = i ? 2 >> chroma_v_shift : 2;
209 pic->data[i] += pic->linesize[i] * (vsample * s->height / V_MAX - 1);
210 pic->linesize[i] *= -1;
212 ret = ff_MPV_encode_picture(avctx, pkt, pic, got_packet);
217 #if CONFIG_MJPEG_ENCODER
218 AVCodec ff_mjpeg_encoder = {
220 .long_name = NULL_IF_CONFIG_SMALL("MJPEG (Motion JPEG)"),
221 .type = AVMEDIA_TYPE_VIDEO,
222 .id = AV_CODEC_ID_MJPEG,
223 .priv_data_size = sizeof(MpegEncContext),
224 .init = ff_MPV_encode_init,
225 .encode2 = ff_MPV_encode_picture,
226 .close = ff_MPV_encode_end,
227 .capabilities = CODEC_CAP_SLICE_THREADS | CODEC_CAP_FRAME_THREADS | CODEC_CAP_INTRA_ONLY,
228 .pix_fmts = (const enum AVPixelFormat[]){
229 AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_NONE
233 #if CONFIG_AMV_ENCODER
234 AVCodec ff_amv_encoder = {
236 .long_name = NULL_IF_CONFIG_SMALL("AMV Video"),
237 .type = AVMEDIA_TYPE_VIDEO,
238 .id = AV_CODEC_ID_AMV,
239 .priv_data_size = sizeof(MpegEncContext),
240 .init = ff_MPV_encode_init,
241 .encode2 = amv_encode_picture,
242 .close = ff_MPV_encode_end,
243 .pix_fmts = (const enum AVPixelFormat[]){
244 AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_NONE