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 "mpegvideo.h"
40 av_cold int ff_mjpeg_encode_init(MpegEncContext *s)
44 if (s->width > 65500 || s->height > 65500) {
45 av_log(s, AV_LOG_ERROR, "JPEG does not support resolutions above 65500x65500\n");
46 return AVERROR(EINVAL);
49 m = av_malloc(sizeof(MJpegContext));
51 return AVERROR(ENOMEM);
56 /* build all the huffman tables */
57 ff_mjpeg_build_huffman_codes(m->huff_size_dc_luminance,
58 m->huff_code_dc_luminance,
59 avpriv_mjpeg_bits_dc_luminance,
61 ff_mjpeg_build_huffman_codes(m->huff_size_dc_chrominance,
62 m->huff_code_dc_chrominance,
63 avpriv_mjpeg_bits_dc_chrominance,
65 ff_mjpeg_build_huffman_codes(m->huff_size_ac_luminance,
66 m->huff_code_ac_luminance,
67 avpriv_mjpeg_bits_ac_luminance,
68 avpriv_mjpeg_val_ac_luminance);
69 ff_mjpeg_build_huffman_codes(m->huff_size_ac_chrominance,
70 m->huff_code_ac_chrominance,
71 avpriv_mjpeg_bits_ac_chrominance,
72 avpriv_mjpeg_val_ac_chrominance);
78 void ff_mjpeg_encode_close(MpegEncContext *s)
80 av_free(s->mjpeg_ctx);
83 /* table_class: 0 = DC coef, 1 = AC coefs */
84 static int put_huffman_table(PutBitContext *p, int table_class, int table_id,
85 const uint8_t *bits_table, const uint8_t *value_table)
89 put_bits(p, 4, table_class);
90 put_bits(p, 4, table_id);
95 put_bits(p, 8, bits_table[i]);
99 put_bits(p, 8, value_table[i]);
104 static void jpeg_table_header(AVCodecContext *avctx, PutBitContext *p,
105 ScanTable *intra_scantable,
106 uint16_t luma_intra_matrix[64],
107 uint16_t chroma_intra_matrix[64],
113 if (avctx->codec_id != AV_CODEC_ID_LJPEG) {
114 int matrix_count = 1 + !!memcmp(luma_intra_matrix,
116 sizeof(luma_intra_matrix[0]) * 64);
119 put_bits(p, 16, 2 + matrix_count * (1 + 64));
120 put_bits(p, 4, 0); /* 8 bit precision */
121 put_bits(p, 4, 0); /* table 0 */
123 j = intra_scantable->permutated[i];
124 put_bits(p, 8, luma_intra_matrix[j]);
127 if (matrix_count > 1) {
128 put_bits(p, 4, 0); /* 8 bit precision */
129 put_bits(p, 4, 1); /* table 1 */
131 j = intra_scantable->permutated[i];
132 put_bits(p, 8, chroma_intra_matrix[j]);
137 if(avctx->active_thread_type & FF_THREAD_SLICE){
140 put_bits(p, 16, (avctx->width-1)/(8*hsample[0]) + 1);
146 ptr = put_bits_ptr(p);
147 put_bits(p, 16, 0); /* patched later */
149 size += put_huffman_table(p, 0, 0, avpriv_mjpeg_bits_dc_luminance,
150 avpriv_mjpeg_val_dc);
151 size += put_huffman_table(p, 0, 1, avpriv_mjpeg_bits_dc_chrominance,
152 avpriv_mjpeg_val_dc);
154 size += put_huffman_table(p, 1, 0, avpriv_mjpeg_bits_ac_luminance,
155 avpriv_mjpeg_val_ac_luminance);
156 size += put_huffman_table(p, 1, 1, avpriv_mjpeg_bits_ac_chrominance,
157 avpriv_mjpeg_val_ac_chrominance);
161 static void jpeg_put_comments(AVCodecContext *avctx, PutBitContext *p)
166 if (avctx->sample_aspect_ratio.num > 0 && avctx->sample_aspect_ratio.den > 0) {
170 avpriv_put_string(p, "JFIF", 1); /* this puts the trailing zero-byte too */
171 put_bits(p, 16, 0x0102); /* v 1.02 */
172 put_bits(p, 8, 0); /* units type: 0 - aspect ratio */
173 put_bits(p, 16, avctx->sample_aspect_ratio.num);
174 put_bits(p, 16, avctx->sample_aspect_ratio.den);
175 put_bits(p, 8, 0); /* thumbnail width */
176 put_bits(p, 8, 0); /* thumbnail height */
180 if (!(avctx->flags & CODEC_FLAG_BITEXACT)) {
183 ptr = put_bits_ptr(p);
184 put_bits(p, 16, 0); /* patched later */
185 avpriv_put_string(p, LIBAVCODEC_IDENT, 1);
186 size = strlen(LIBAVCODEC_IDENT)+3;
190 if (avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
191 avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
192 avctx->pix_fmt == AV_PIX_FMT_YUV444P) {
195 ptr = put_bits_ptr(p);
196 put_bits(p, 16, 0); /* patched later */
197 avpriv_put_string(p, "CS=ITU601", 1);
198 size = strlen("CS=ITU601")+3;
203 void ff_mjpeg_init_hvsample(AVCodecContext *avctx, int hsample[3], int vsample[3])
205 int chroma_h_shift, chroma_v_shift;
207 av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift,
209 if (avctx->codec->id == AV_CODEC_ID_LJPEG &&
210 ( avctx->pix_fmt == AV_PIX_FMT_BGR0
211 || avctx->pix_fmt == AV_PIX_FMT_BGRA
212 || avctx->pix_fmt == AV_PIX_FMT_BGR24)) {
213 vsample[0] = hsample[0] =
214 vsample[1] = hsample[1] =
215 vsample[2] = hsample[2] = 1;
216 } else if (avctx->pix_fmt == AV_PIX_FMT_YUV444P || avctx->pix_fmt == AV_PIX_FMT_YUVJ444P) {
217 vsample[0] = vsample[1] = vsample[2] = 2;
218 hsample[0] = hsample[1] = hsample[2] = 1;
221 vsample[1] = 2 >> chroma_v_shift;
222 vsample[2] = 2 >> chroma_v_shift;
224 hsample[1] = 2 >> chroma_h_shift;
225 hsample[2] = 2 >> chroma_h_shift;
229 void ff_mjpeg_encode_picture_header(AVCodecContext *avctx, PutBitContext *pb,
230 ScanTable *intra_scantable,
231 uint16_t luma_intra_matrix[64],
232 uint16_t chroma_intra_matrix[64])
234 const int lossless = avctx->codec_id != AV_CODEC_ID_MJPEG && avctx->codec_id != AV_CODEC_ID_AMV;
235 int hsample[3], vsample[3];
237 int chroma_matrix = !!memcmp(luma_intra_matrix,
239 sizeof(luma_intra_matrix[0])*64);
241 ff_mjpeg_init_hvsample(avctx, hsample, vsample);
245 // hack for AMV mjpeg format
246 if(avctx->codec_id == AV_CODEC_ID_AMV) goto end;
248 jpeg_put_comments(avctx, pb);
250 jpeg_table_header(avctx, pb, intra_scantable, luma_intra_matrix, chroma_intra_matrix, hsample);
252 switch (avctx->codec_id) {
253 case AV_CODEC_ID_MJPEG: put_marker(pb, SOF0 ); break;
254 case AV_CODEC_ID_LJPEG: put_marker(pb, SOF3 ); break;
255 default: av_assert0(0);
258 put_bits(pb, 16, 17);
259 if (lossless && ( avctx->pix_fmt == AV_PIX_FMT_BGR0
260 || avctx->pix_fmt == AV_PIX_FMT_BGRA
261 || avctx->pix_fmt == AV_PIX_FMT_BGR24))
262 put_bits(pb, 8, 9); /* 9 bits/component RCT */
264 put_bits(pb, 8, 8); /* 8 bits/component */
265 put_bits(pb, 16, avctx->height);
266 put_bits(pb, 16, avctx->width);
267 put_bits(pb, 8, 3); /* 3 components */
270 put_bits(pb, 8, 1); /* component number */
271 put_bits(pb, 4, hsample[0]); /* H factor */
272 put_bits(pb, 4, vsample[0]); /* V factor */
273 put_bits(pb, 8, 0); /* select matrix */
276 put_bits(pb, 8, 2); /* component number */
277 put_bits(pb, 4, hsample[1]); /* H factor */
278 put_bits(pb, 4, vsample[1]); /* V factor */
279 put_bits(pb, 8, lossless ? 0 : chroma_matrix); /* select matrix */
282 put_bits(pb, 8, 3); /* component number */
283 put_bits(pb, 4, hsample[2]); /* H factor */
284 put_bits(pb, 4, vsample[2]); /* V factor */
285 put_bits(pb, 8, lossless ? 0 : chroma_matrix); /* select matrix */
289 put_bits(pb, 16, 12); /* length */
290 put_bits(pb, 8, 3); /* 3 components */
293 put_bits(pb, 8, 1); /* index */
294 put_bits(pb, 4, 0); /* DC huffman table index */
295 put_bits(pb, 4, 0); /* AC huffman table index */
298 put_bits(pb, 8, 2); /* index */
299 put_bits(pb, 4, 1); /* DC huffman table index */
300 put_bits(pb, 4, lossless ? 0 : 1); /* AC huffman table index */
303 put_bits(pb, 8, 3); /* index */
304 put_bits(pb, 4, 1); /* DC huffman table index */
305 put_bits(pb, 4, lossless ? 0 : 1); /* AC huffman table index */
307 put_bits(pb, 8, lossless ? avctx->prediction_method + 1 : 0); /* Ss (not used) */
309 switch (avctx->codec_id) {
310 case AV_CODEC_ID_MJPEG: put_bits(pb, 8, 63); break; /* Se (not used) */
311 case AV_CODEC_ID_LJPEG: put_bits(pb, 8, 0); break; /* not used */
312 default: av_assert0(0);
315 put_bits(pb, 8, 0); /* Ah/Al (not used) */
319 MpegEncContext *s = avctx->priv_data;
320 av_assert0(avctx->codec->priv_data_size == sizeof(MpegEncContext));
322 s->esc_pos = put_bits_count(pb) >> 3;
323 for(i=1; i<s->slice_context_count; i++)
324 s->thread_context[i]->esc_pos = 0;
328 void ff_mjpeg_escape_FF(PutBitContext *pb, int start)
332 uint8_t *buf = pb->buf + start;
333 int align= (-(size_t)(buf))&3;
334 int pad = (-put_bits_count(pb))&7;
337 put_bits(pb, pad, (1<<pad)-1);
340 size = put_bits_count(pb) - start * 8;
342 av_assert1((size&7) == 0);
346 for(i=0; i<size && i<align; i++){
347 if(buf[i]==0xFF) ff_count++;
349 for(; i<size-15; i+=16){
352 v= *(uint32_t*)(&buf[i]);
353 acc= (((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
354 v= *(uint32_t*)(&buf[i+4]);
355 acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
356 v= *(uint32_t*)(&buf[i+8]);
357 acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
358 v= *(uint32_t*)(&buf[i+12]);
359 acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
367 if(buf[i]==0xFF) ff_count++;
370 if(ff_count==0) return;
373 skip_put_bytes(pb, ff_count);
375 for(i=size-1; ff_count; i--){
387 void ff_mjpeg_encode_stuffing(MpegEncContext *s)
390 PutBitContext *pbc = &s->pb;
391 int mb_y = s->mb_y - !s->mb_x;
393 ff_mjpeg_escape_FF(pbc, s->esc_pos);
395 if((s->avctx->active_thread_type & FF_THREAD_SLICE) && mb_y < s->mb_height)
396 put_marker(pbc, RST0 + (mb_y&7));
397 s->esc_pos = put_bits_count(pbc) >> 3;
400 s->last_dc[i] = 128 << s->intra_dc_precision;
403 void ff_mjpeg_encode_picture_trailer(PutBitContext *pb, int header_bits)
405 av_assert1((header_bits & 7) == 0);
410 void ff_mjpeg_encode_dc(PutBitContext *pb, int val,
411 uint8_t *huff_size, uint16_t *huff_code)
416 put_bits(pb, huff_size[0], huff_code[0]);
424 nbits= av_log2_16bit(val) + 1;
426 put_bits(pb, huff_size[nbits], huff_code[nbits]);
428 put_sbits(pb, nbits, mant);
432 static void encode_block(MpegEncContext *s, int16_t *block, int n)
434 int mant, nbits, code, i, j;
435 int component, dc, run, last_index, val;
436 MJpegContext *m = s->mjpeg_ctx;
437 uint8_t *huff_size_ac;
438 uint16_t *huff_code_ac;
441 component = (n <= 3 ? 0 : (n&1) + 1);
442 dc = block[0]; /* overflow is impossible */
443 val = dc - s->last_dc[component];
445 ff_mjpeg_encode_dc(&s->pb, val, m->huff_size_dc_luminance, m->huff_code_dc_luminance);
446 huff_size_ac = m->huff_size_ac_luminance;
447 huff_code_ac = m->huff_code_ac_luminance;
449 ff_mjpeg_encode_dc(&s->pb, val, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
450 huff_size_ac = m->huff_size_ac_chrominance;
451 huff_code_ac = m->huff_code_ac_chrominance;
453 s->last_dc[component] = dc;
458 last_index = s->block_last_index[n];
459 for(i=1;i<=last_index;i++) {
460 j = s->intra_scantable.permutated[i];
466 put_bits(&s->pb, huff_size_ac[0xf0], huff_code_ac[0xf0]);
475 nbits= av_log2_16bit(val) + 1;
476 code = (run << 4) | nbits;
478 put_bits(&s->pb, huff_size_ac[code], huff_code_ac[code]);
480 put_sbits(&s->pb, nbits, mant);
485 /* output EOB only if not already 64 values */
486 if (last_index < 63 || run != 0)
487 put_bits(&s->pb, huff_size_ac[0], huff_code_ac[0]);
490 void ff_mjpeg_encode_mb(MpegEncContext *s, int16_t block[12][64])
493 if (s->chroma_format == CHROMA_444) {
494 encode_block(s, block[0], 0);
495 encode_block(s, block[2], 2);
496 encode_block(s, block[4], 4);
497 encode_block(s, block[8], 8);
498 encode_block(s, block[5], 5);
499 encode_block(s, block[9], 9);
501 if (16*s->mb_x+8 < s->width) {
502 encode_block(s, block[1], 1);
503 encode_block(s, block[3], 3);
504 encode_block(s, block[6], 6);
505 encode_block(s, block[10], 10);
506 encode_block(s, block[7], 7);
507 encode_block(s, block[11], 11);
511 encode_block(s, block[i], i);
513 if (s->chroma_format == CHROMA_420) {
514 encode_block(s, block[5], 5);
516 encode_block(s, block[6], 6);
517 encode_block(s, block[5], 5);
518 encode_block(s, block[7], 7);
522 s->i_tex_bits += get_bits_diff(s);
525 // maximum over s->mjpeg_vsample[i]
527 static int amv_encode_picture(AVCodecContext *avctx, AVPacket *pkt,
528 const AVFrame *pic_arg, int *got_packet)
531 MpegEncContext *s = avctx->priv_data;
534 int chroma_h_shift, chroma_v_shift;
536 av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift, &chroma_v_shift);
538 //CODEC_FLAG_EMU_EDGE have to be cleared
539 if(s->avctx->flags & CODEC_FLAG_EMU_EDGE)
540 return AVERROR(EINVAL);
542 if ((avctx->height & 15) && avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL) {
543 av_log(avctx, AV_LOG_ERROR,
544 "Heights which are not a multiple of 16 might fail with some decoders, "
545 "use vstrict=-1 / -strict -1 to use %d anyway.\n", avctx->height);
546 av_log(avctx, AV_LOG_WARNING, "If you have a device that plays AMV videos, please test if videos "
547 "with such heights work with it and report your findings to ffmpeg-devel@ffmpeg.org\n");
548 return AVERROR_EXPERIMENTAL;
551 pic = av_frame_clone(pic_arg);
553 return AVERROR(ENOMEM);
554 //picture should be flipped upside-down
555 for(i=0; i < 3; i++) {
556 int vsample = i ? 2 >> chroma_v_shift : 2;
557 pic->data[i] += pic->linesize[i] * (vsample * s->height / V_MAX - 1);
558 pic->linesize[i] *= -1;
560 ret = ff_MPV_encode_picture(avctx, pkt, pic, got_packet);
565 #if CONFIG_MJPEG_ENCODER
566 AVCodec ff_mjpeg_encoder = {
568 .long_name = NULL_IF_CONFIG_SMALL("MJPEG (Motion JPEG)"),
569 .type = AVMEDIA_TYPE_VIDEO,
570 .id = AV_CODEC_ID_MJPEG,
571 .priv_data_size = sizeof(MpegEncContext),
572 .init = ff_MPV_encode_init,
573 .encode2 = ff_MPV_encode_picture,
574 .close = ff_MPV_encode_end,
575 .capabilities = CODEC_CAP_SLICE_THREADS | CODEC_CAP_FRAME_THREADS | CODEC_CAP_INTRA_ONLY,
576 .pix_fmts = (const enum AVPixelFormat[]){
577 AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_NONE
581 #if CONFIG_AMV_ENCODER
582 AVCodec ff_amv_encoder = {
584 .long_name = NULL_IF_CONFIG_SMALL("AMV Video"),
585 .type = AVMEDIA_TYPE_VIDEO,
586 .id = AV_CODEC_ID_AMV,
587 .priv_data_size = sizeof(MpegEncContext),
588 .init = ff_MPV_encode_init,
589 .encode2 = amv_encode_picture,
590 .close = ff_MPV_encode_end,
591 .pix_fmts = (const enum AVPixelFormat[]){
592 AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_NONE