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 "jpegtables.h"
37 #include "mjpegenc_common.h"
38 #include "mpegvideo.h"
42 // Don't know, but let's guess 16 bits per code
43 #define MJPEG_HUFFMAN_EST_BITS_PER_CODE 16
45 av_cold int ff_mjpeg_encode_init(MpegEncContext *s)
49 av_assert0(s->slice_context_count == 1);
51 if (s->width > 65500 || s->height > 65500) {
52 av_log(s, AV_LOG_ERROR, "JPEG does not support resolutions above 65500x65500\n");
53 return AVERROR(EINVAL);
56 m = av_malloc(sizeof(MJpegContext));
58 return AVERROR(ENOMEM);
63 // Build default Huffman tables.
64 // These may be overwritten later with more optimal Huffman tables, but
65 // they are needed at least right now for some processes like trellis.
66 ff_mjpeg_build_huffman_codes(m->huff_size_dc_luminance,
67 m->huff_code_dc_luminance,
68 avpriv_mjpeg_bits_dc_luminance,
70 ff_mjpeg_build_huffman_codes(m->huff_size_dc_chrominance,
71 m->huff_code_dc_chrominance,
72 avpriv_mjpeg_bits_dc_chrominance,
74 ff_mjpeg_build_huffman_codes(m->huff_size_ac_luminance,
75 m->huff_code_ac_luminance,
76 avpriv_mjpeg_bits_ac_luminance,
77 avpriv_mjpeg_val_ac_luminance);
78 ff_mjpeg_build_huffman_codes(m->huff_size_ac_chrominance,
79 m->huff_code_ac_chrominance,
80 avpriv_mjpeg_bits_ac_chrominance,
81 avpriv_mjpeg_val_ac_chrominance);
83 ff_init_uni_ac_vlc(m->huff_size_ac_luminance, m->uni_ac_vlc_len);
84 ff_init_uni_ac_vlc(m->huff_size_ac_chrominance, m->uni_chroma_ac_vlc_len);
85 s->intra_ac_vlc_length =
86 s->intra_ac_vlc_last_length = m->uni_ac_vlc_len;
87 s->intra_chroma_ac_vlc_length =
88 s->intra_chroma_ac_vlc_last_length = m->uni_chroma_ac_vlc_len;
90 // Buffers start out empty.
91 m->huff_buffer = NULL;
100 av_cold void ff_mjpeg_encode_close(MpegEncContext *s)
102 av_freep(&s->mjpeg_ctx->huff_buffer);
103 av_freep(&s->mjpeg_ctx);
107 * Encodes and outputs the entire frame in the JPEG format.
109 * @param s The MpegEncContext.
111 void ff_mjpeg_encode_picture_frame(MpegEncContext *s)
113 int i, nbits, code, table_id;
114 MJpegContext *m = s->mjpeg_ctx;
115 uint8_t *huff_size[4] = {m->huff_size_dc_luminance,
116 m->huff_size_dc_chrominance,
117 m->huff_size_ac_luminance,
118 m->huff_size_ac_chrominance};
119 uint16_t *huff_code[4] = {m->huff_code_dc_luminance,
120 m->huff_code_dc_chrominance,
121 m->huff_code_ac_luminance,
122 m->huff_code_ac_chrominance};
123 size_t total_bits = 0;
126 // Estimate the total size first
127 for (i = 0; i < m->huff_ncode; i++) {
128 table_id = m->huff_buffer[i].table_id;
129 code = m->huff_buffer[i].code;
132 total_bits += huff_size[table_id][code] + nbits;
135 bytes_needed = (total_bits + 7) / 8;
136 ff_mpv_reallocate_putbitbuffer(s, bytes_needed, bytes_needed);
138 for (i = 0; i < m->huff_ncode; i++) {
139 table_id = m->huff_buffer[i].table_id;
140 code = m->huff_buffer[i].code;
143 put_bits(&s->pb, huff_size[table_id][code], huff_code[table_id][code]);
145 put_sbits(&s->pb, nbits, m->huff_buffer[i].mant);
153 * Add code and table_id to the JPEG buffer.
155 * @param s The MJpegContext which contains the JPEG buffer.
156 * @param table_id Which Huffman table the code belongs to.
157 * @param code The encoded exponent of the coefficients and the run-bits.
159 static inline void ff_mjpeg_encode_code(MJpegContext *s, uint8_t table_id, int code)
161 MJpegHuffmanCode *c = &s->huff_buffer[s->huff_ncode++];
162 av_assert0(s->huff_ncode < s->huff_capacity);
163 c->table_id = table_id;
168 * Add the coefficient's data to the JPEG buffer.
170 * @param s The MJpegContext which contains the JPEG buffer.
171 * @param table_id Which Huffman table the code belongs to.
172 * @param val The coefficient.
173 * @param run The run-bits.
175 static void ff_mjpeg_encode_coef(MJpegContext *s, uint8_t table_id, int val, int run)
180 av_assert0(run == 0);
181 ff_mjpeg_encode_code(s, table_id, 0);
189 code = (run << 4) | (av_log2_16bit(val) + 1);
191 s->huff_buffer[s->huff_ncode].mant = mant;
192 ff_mjpeg_encode_code(s, table_id, code);
197 * Add the block's data into the JPEG buffer.
199 * @param s The MJpegEncContext that contains the JPEG buffer.
200 * @param block The block.
201 * @param n The block's index or number.
203 static void encode_block(MpegEncContext *s, int16_t *block, int n)
206 int component, dc, last_index, val, run;
207 MJpegContext *m = s->mjpeg_ctx;
209 if (m->error) return;
211 av_assert0(m->huff_capacity >= m->huff_ncode + 64);
214 component = (n <= 3 ? 0 : (n&1) + 1);
215 table_id = (n <= 3 ? 0 : 1);
216 dc = block[0]; /* overflow is impossible */
217 val = dc - s->last_dc[component];
219 ff_mjpeg_encode_coef(m, table_id, val, 0);
221 s->last_dc[component] = dc;
226 last_index = s->block_last_index[n];
229 for(i=1;i<=last_index;i++) {
230 j = s->intra_scantable.permutated[i];
237 ff_mjpeg_encode_code(m, table_id, 0xf0);
240 ff_mjpeg_encode_coef(m, table_id, val, run);
245 /* output EOB only if not already 64 values */
246 if (last_index < 63 || run != 0)
247 ff_mjpeg_encode_code(m, table_id, 0);
250 // Possibly reallocate the huffman code buffer, assuming blocks_per_mb.
251 // Set s->mjpeg_ctx->error on ENOMEM.
252 static void realloc_huffman(MpegEncContext *s, int blocks_per_mb)
254 MJpegContext *m = s->mjpeg_ctx;
255 size_t num_mbs, num_blocks, num_codes;
256 MJpegHuffmanCode *new_buf;
257 if (m->error) return;
258 // Make sure we have enough space to hold this frame.
259 num_mbs = s->mb_width * s->mb_height;
260 num_blocks = num_mbs * blocks_per_mb;
261 av_assert0(m->huff_ncode <=
262 (s->mb_y * s->mb_width + s->mb_x) * blocks_per_mb * 64);
263 num_codes = num_blocks * 64;
265 new_buf = av_fast_realloc(m->huff_buffer, &m->huff_capacity,
266 num_codes * sizeof(MJpegHuffmanCode));
268 m->error = AVERROR(ENOMEM);
270 m->huff_buffer = new_buf;
274 int ff_mjpeg_encode_mb(MpegEncContext *s, int16_t block[12][64])
276 int i, is_chroma_420;
278 // Number of bits used depends on future data.
279 // So, nothing that relies on encoding many times and taking the
280 // one with the fewest bits will work properly here.
281 if (s->i_tex_bits != MJPEG_HUFFMAN_EST_BITS_PER_CODE *
282 s->mjpeg_ctx->huff_ncode) {
283 av_log(s->avctx, AV_LOG_ERROR, "Unsupported encoding method\n");
284 return AVERROR(EINVAL);
287 if (s->chroma_format == CHROMA_444) {
288 realloc_huffman(s, 12);
289 encode_block(s, block[0], 0);
290 encode_block(s, block[2], 2);
291 encode_block(s, block[4], 4);
292 encode_block(s, block[8], 8);
293 encode_block(s, block[5], 5);
294 encode_block(s, block[9], 9);
296 if (16*s->mb_x+8 < s->width) {
297 encode_block(s, block[1], 1);
298 encode_block(s, block[3], 3);
299 encode_block(s, block[6], 6);
300 encode_block(s, block[10], 10);
301 encode_block(s, block[7], 7);
302 encode_block(s, block[11], 11);
305 is_chroma_420 = (s->chroma_format == CHROMA_420);
306 realloc_huffman(s, 5 + (is_chroma_420 ? 1 : 3));
308 encode_block(s, block[i], i);
311 encode_block(s, block[5], 5);
313 encode_block(s, block[6], 6);
314 encode_block(s, block[5], 5);
315 encode_block(s, block[7], 7);
318 if (s->mjpeg_ctx->error)
319 return s->mjpeg_ctx->error;
321 s->i_tex_bits = MJPEG_HUFFMAN_EST_BITS_PER_CODE * s->mjpeg_ctx->huff_ncode;
325 // maximum over s->mjpeg_vsample[i]
327 static int amv_encode_picture(AVCodecContext *avctx, AVPacket *pkt,
328 const AVFrame *pic_arg, int *got_packet)
331 MpegEncContext *s = avctx->priv_data;
334 int chroma_h_shift, chroma_v_shift;
336 av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift, &chroma_v_shift);
339 //CODEC_FLAG_EMU_EDGE have to be cleared
340 if(s->avctx->flags & CODEC_FLAG_EMU_EDGE)
341 return AVERROR(EINVAL);
344 if ((avctx->height & 15) && avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL) {
345 av_log(avctx, AV_LOG_ERROR,
346 "Heights which are not a multiple of 16 might fail with some decoders, "
347 "use vstrict=-1 / -strict -1 to use %d anyway.\n", avctx->height);
348 av_log(avctx, AV_LOG_WARNING, "If you have a device that plays AMV videos, please test if videos "
349 "with such heights work with it and report your findings to ffmpeg-devel@ffmpeg.org\n");
350 return AVERROR_EXPERIMENTAL;
353 pic = av_frame_clone(pic_arg);
355 return AVERROR(ENOMEM);
356 //picture should be flipped upside-down
357 for(i=0; i < 3; i++) {
358 int vsample = i ? 2 >> chroma_v_shift : 2;
359 pic->data[i] += pic->linesize[i] * (vsample * s->height / V_MAX - 1);
360 pic->linesize[i] *= -1;
362 ret = ff_mpv_encode_picture(avctx, pkt, pic, got_packet);
367 #define OFFSET(x) offsetof(MpegEncContext, x)
368 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
369 static const AVOption options[] = {
371 { "pred", "Prediction method", OFFSET(pred), AV_OPT_TYPE_INT, { .i64 = 1 }, 1, 3, VE, "pred" },
372 { "left", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX, VE, "pred" },
373 { "plane", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 2 }, INT_MIN, INT_MAX, VE, "pred" },
374 { "median", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 3 }, INT_MIN, INT_MAX, VE, "pred" },
375 { "huffman", "Huffman table strategy", OFFSET(huffman), AV_OPT_TYPE_INT, { .i64 = HUFFMAN_TABLE_DEFAULT }, 0, NB_HUFFMAN_TABLE_OPTION - 1, VE, "huffman" },
376 { "default", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = HUFFMAN_TABLE_DEFAULT }, INT_MIN, INT_MAX, VE, "huffman" },
377 { "optimal", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = HUFFMAN_TABLE_OPTIMAL }, INT_MIN, INT_MAX, VE, "huffman" },
381 #if CONFIG_MJPEG_ENCODER
383 static const AVClass mjpeg_class = {
384 .class_name = "mjpeg encoder",
385 .item_name = av_default_item_name,
387 .version = LIBAVUTIL_VERSION_INT,
390 AVCodec ff_mjpeg_encoder = {
392 .long_name = NULL_IF_CONFIG_SMALL("MJPEG (Motion JPEG)"),
393 .type = AVMEDIA_TYPE_VIDEO,
394 .id = AV_CODEC_ID_MJPEG,
395 .priv_data_size = sizeof(MpegEncContext),
396 .init = ff_mpv_encode_init,
397 .encode2 = ff_mpv_encode_picture,
398 .close = ff_mpv_encode_end,
399 .capabilities = AV_CODEC_CAP_SLICE_THREADS | AV_CODEC_CAP_FRAME_THREADS | AV_CODEC_CAP_INTRA_ONLY,
400 .pix_fmts = (const enum AVPixelFormat[]){
401 AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_NONE
403 .priv_class = &mjpeg_class,
406 #if CONFIG_AMV_ENCODER
407 static const AVClass amv_class = {
408 .class_name = "amv encoder",
409 .item_name = av_default_item_name,
411 .version = LIBAVUTIL_VERSION_INT,
414 AVCodec ff_amv_encoder = {
416 .long_name = NULL_IF_CONFIG_SMALL("AMV Video"),
417 .type = AVMEDIA_TYPE_VIDEO,
418 .id = AV_CODEC_ID_AMV,
419 .priv_data_size = sizeof(MpegEncContext),
420 .init = ff_mpv_encode_init,
421 .encode2 = amv_encode_picture,
422 .close = ff_mpv_encode_end,
423 .pix_fmts = (const enum AVPixelFormat[]){
424 AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_NONE
426 .priv_class = &amv_class,