2 * lossless JPEG shared bits
3 * Copyright (c) 2000, 2001 Fabrice Bellard
4 * Copyright (c) 2003 Alex Beregszaszi
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "libavutil/pixdesc.h"
27 #include "libavutil/pixfmt.h"
31 #include "jpegtables.h"
34 #include "mjpegenc_common.h"
37 /* table_class: 0 = DC coef, 1 = AC coefs */
38 static int put_huffman_table(PutBitContext *p, int table_class, int table_id,
39 const uint8_t *bits_table, const uint8_t *value_table)
43 put_bits(p, 4, table_class);
44 put_bits(p, 4, table_id);
49 put_bits(p, 8, bits_table[i]);
53 put_bits(p, 8, value_table[i]);
58 static void jpeg_table_header(AVCodecContext *avctx, PutBitContext *p,
59 ScanTable *intra_scantable,
60 uint16_t luma_intra_matrix[64],
61 uint16_t chroma_intra_matrix[64],
66 MpegEncContext *s = NULL;
68 /* Since avctx->priv_data will point to LJpegEncContext in this case */
69 if (avctx->codec_id != AV_CODEC_ID_LJPEG)
72 if (avctx->codec_id != AV_CODEC_ID_LJPEG) {
73 int matrix_count = 1 + !!memcmp(luma_intra_matrix,
75 sizeof(luma_intra_matrix[0]) * 64);
76 if (s && s->force_duplicated_matrix)
80 put_bits(p, 16, 2 + matrix_count * (1 + 64));
81 put_bits(p, 4, 0); /* 8 bit precision */
82 put_bits(p, 4, 0); /* table 0 */
84 j = intra_scantable->permutated[i];
85 put_bits(p, 8, luma_intra_matrix[j]);
88 if (matrix_count > 1) {
89 put_bits(p, 4, 0); /* 8 bit precision */
90 put_bits(p, 4, 1); /* table 1 */
92 j = intra_scantable->permutated[i];
93 put_bits(p, 8, chroma_intra_matrix[j]);
98 if(avctx->active_thread_type & FF_THREAD_SLICE){
101 put_bits(p, 16, (avctx->width-1)/(8*hsample[0]) + 1);
107 ptr = put_bits_ptr(p);
108 put_bits(p, 16, 0); /* patched later */
111 // Only MJPEG can have a variable Huffman variable. All other
112 // formats use the default Huffman table.
113 if (s && s->huffman == HUFFMAN_TABLE_OPTIMAL) {
114 size += put_huffman_table(p, 0, 0, s->mjpeg_ctx->bits_dc_luminance,
115 s->mjpeg_ctx->val_dc_luminance);
116 size += put_huffman_table(p, 0, 1, s->mjpeg_ctx->bits_dc_chrominance,
117 s->mjpeg_ctx->val_dc_chrominance);
119 size += put_huffman_table(p, 1, 0, s->mjpeg_ctx->bits_ac_luminance,
120 s->mjpeg_ctx->val_ac_luminance);
121 size += put_huffman_table(p, 1, 1, s->mjpeg_ctx->bits_ac_chrominance,
122 s->mjpeg_ctx->val_ac_chrominance);
124 size += put_huffman_table(p, 0, 0, avpriv_mjpeg_bits_dc_luminance,
125 avpriv_mjpeg_val_dc);
126 size += put_huffman_table(p, 0, 1, avpriv_mjpeg_bits_dc_chrominance,
127 avpriv_mjpeg_val_dc);
129 size += put_huffman_table(p, 1, 0, avpriv_mjpeg_bits_ac_luminance,
130 avpriv_mjpeg_val_ac_luminance);
131 size += put_huffman_table(p, 1, 1, avpriv_mjpeg_bits_ac_chrominance,
132 avpriv_mjpeg_val_ac_chrominance);
137 static void jpeg_put_comments(AVCodecContext *avctx, PutBitContext *p)
142 if (avctx->sample_aspect_ratio.num > 0 && avctx->sample_aspect_ratio.den > 0) {
143 AVRational sar = avctx->sample_aspect_ratio;
145 if (sar.num > 65535 || sar.den > 65535) {
146 if (!av_reduce(&sar.num, &sar.den, avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den, 65535))
147 av_log(avctx, AV_LOG_WARNING,
148 "Cannot store exact aspect ratio %d:%d\n",
149 avctx->sample_aspect_ratio.num,
150 avctx->sample_aspect_ratio.den);
156 ff_put_string(p, "JFIF", 1); /* this puts the trailing zero-byte too */
157 /* The most significant byte is used for major revisions, the least
158 * significant byte for minor revisions. Version 1.02 is the current
159 * released revision. */
160 put_bits(p, 16, 0x0102);
161 put_bits(p, 8, 0); /* units type: 0 - aspect ratio */
162 put_bits(p, 16, sar.num);
163 put_bits(p, 16, sar.den);
164 put_bits(p, 8, 0); /* thumbnail width */
165 put_bits(p, 8, 0); /* thumbnail height */
169 if (!(avctx->flags & AV_CODEC_FLAG_BITEXACT)) {
172 ptr = put_bits_ptr(p);
173 put_bits(p, 16, 0); /* patched later */
174 ff_put_string(p, LIBAVCODEC_IDENT, 1);
175 size = strlen(LIBAVCODEC_IDENT)+3;
179 if (((avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
180 avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
181 avctx->pix_fmt == AV_PIX_FMT_YUV444P) && avctx->color_range != AVCOL_RANGE_JPEG)
182 || avctx->color_range == AVCOL_RANGE_MPEG) {
185 ptr = put_bits_ptr(p);
186 put_bits(p, 16, 0); /* patched later */
187 ff_put_string(p, "CS=ITU601", 1);
188 size = strlen("CS=ITU601")+3;
193 void ff_mjpeg_init_hvsample(AVCodecContext *avctx, int hsample[4], int vsample[4])
195 int chroma_h_shift, chroma_v_shift;
197 av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift,
199 if (avctx->codec->id == AV_CODEC_ID_LJPEG &&
200 ( avctx->pix_fmt == AV_PIX_FMT_BGR0
201 || avctx->pix_fmt == AV_PIX_FMT_BGRA
202 || avctx->pix_fmt == AV_PIX_FMT_BGR24)) {
203 vsample[0] = hsample[0] =
204 vsample[1] = hsample[1] =
205 vsample[2] = hsample[2] =
206 vsample[3] = hsample[3] = 1;
207 } else if (avctx->pix_fmt == AV_PIX_FMT_YUV444P || avctx->pix_fmt == AV_PIX_FMT_YUVJ444P) {
208 vsample[0] = vsample[1] = vsample[2] = 2;
209 hsample[0] = hsample[1] = hsample[2] = 1;
212 vsample[1] = 2 >> chroma_v_shift;
213 vsample[2] = 2 >> chroma_v_shift;
215 hsample[1] = 2 >> chroma_h_shift;
216 hsample[2] = 2 >> chroma_h_shift;
220 void ff_mjpeg_encode_picture_header(AVCodecContext *avctx, PutBitContext *pb,
221 ScanTable *intra_scantable, int pred,
222 uint16_t luma_intra_matrix[64],
223 uint16_t chroma_intra_matrix[64])
225 const int lossless = avctx->codec_id != AV_CODEC_ID_MJPEG && avctx->codec_id != AV_CODEC_ID_AMV;
226 int hsample[4], vsample[4];
228 int components = 3 + (avctx->pix_fmt == AV_PIX_FMT_BGRA);
229 int chroma_matrix = !!memcmp(luma_intra_matrix,
231 sizeof(luma_intra_matrix[0])*64);
233 ff_mjpeg_init_hvsample(avctx, hsample, vsample);
237 // hack for AMV mjpeg format
238 if(avctx->codec_id == AV_CODEC_ID_AMV) goto end;
240 jpeg_put_comments(avctx, pb);
242 jpeg_table_header(avctx, pb, intra_scantable, luma_intra_matrix, chroma_intra_matrix, hsample);
244 switch (avctx->codec_id) {
245 case AV_CODEC_ID_MJPEG: put_marker(pb, SOF0 ); break;
246 case AV_CODEC_ID_LJPEG: put_marker(pb, SOF3 ); break;
247 default: av_assert0(0);
250 put_bits(pb, 16, 17);
251 if (lossless && ( avctx->pix_fmt == AV_PIX_FMT_BGR0
252 || avctx->pix_fmt == AV_PIX_FMT_BGRA
253 || avctx->pix_fmt == AV_PIX_FMT_BGR24))
254 put_bits(pb, 8, 9); /* 9 bits/component RCT */
256 put_bits(pb, 8, 8); /* 8 bits/component */
257 put_bits(pb, 16, avctx->height);
258 put_bits(pb, 16, avctx->width);
259 put_bits(pb, 8, components); /* 3 or 4 components */
262 put_bits(pb, 8, 1); /* component number */
263 put_bits(pb, 4, hsample[0]); /* H factor */
264 put_bits(pb, 4, vsample[0]); /* V factor */
265 put_bits(pb, 8, 0); /* select matrix */
268 put_bits(pb, 8, 2); /* component number */
269 put_bits(pb, 4, hsample[1]); /* H factor */
270 put_bits(pb, 4, vsample[1]); /* V factor */
271 put_bits(pb, 8, lossless ? 0 : chroma_matrix); /* select matrix */
274 put_bits(pb, 8, 3); /* component number */
275 put_bits(pb, 4, hsample[2]); /* H factor */
276 put_bits(pb, 4, vsample[2]); /* V factor */
277 put_bits(pb, 8, lossless ? 0 : chroma_matrix); /* select matrix */
279 if (components == 4) {
280 put_bits(pb, 8, 4); /* component number */
281 put_bits(pb, 4, hsample[3]); /* H factor */
282 put_bits(pb, 4, vsample[3]); /* V factor */
283 put_bits(pb, 8, 0); /* select matrix */
288 put_bits(pb, 16, 6 + 2*components); /* length */
289 put_bits(pb, 8, components); /* 3 components */
292 put_bits(pb, 8, 1); /* index */
293 put_bits(pb, 4, 0); /* DC huffman table index */
294 put_bits(pb, 4, 0); /* AC huffman table index */
297 put_bits(pb, 8, 2); /* index */
298 put_bits(pb, 4, 1); /* DC huffman table index */
299 put_bits(pb, 4, lossless ? 0 : 1); /* AC huffman table index */
302 put_bits(pb, 8, 3); /* index */
303 put_bits(pb, 4, 1); /* DC huffman table index */
304 put_bits(pb, 4, lossless ? 0 : 1); /* AC huffman table index */
306 if (components == 4) {
307 /* Alpha component */
308 put_bits(pb, 8, 4); /* index */
309 put_bits(pb, 4, 0); /* DC huffman table index */
310 put_bits(pb, 4, 0); /* AC huffman table index */
313 put_bits(pb, 8, lossless ? pred : 0); /* Ss (not used) */
315 switch (avctx->codec_id) {
316 case AV_CODEC_ID_MJPEG: put_bits(pb, 8, 63); break; /* Se (not used) */
317 case AV_CODEC_ID_LJPEG: put_bits(pb, 8, 0); break; /* not used */
318 default: av_assert0(0);
321 put_bits(pb, 8, 0); /* Ah/Al (not used) */
325 MpegEncContext *s = avctx->priv_data;
326 av_assert0(avctx->codec->priv_data_size == sizeof(MpegEncContext));
328 s->esc_pos = put_bits_count(pb) >> 3;
329 for(i=1; i<s->slice_context_count; i++)
330 s->thread_context[i]->esc_pos = 0;
334 void ff_mjpeg_escape_FF(PutBitContext *pb, int start)
338 uint8_t *buf = pb->buf + start;
339 int align= (-(size_t)(buf))&3;
340 int pad = (-put_bits_count(pb))&7;
343 put_bits(pb, pad, (1<<pad)-1);
346 size = put_bits_count(pb) - start * 8;
348 av_assert1((size&7) == 0);
352 for(i=0; i<size && i<align; i++){
353 if(buf[i]==0xFF) ff_count++;
355 for(; i<size-15; i+=16){
358 v= *(uint32_t*)(&buf[i]);
359 acc= (((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
360 v= *(uint32_t*)(&buf[i+4]);
361 acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
362 v= *(uint32_t*)(&buf[i+8]);
363 acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
364 v= *(uint32_t*)(&buf[i+12]);
365 acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
373 if(buf[i]==0xFF) ff_count++;
376 if(ff_count==0) return;
379 skip_put_bytes(pb, ff_count);
381 for(i=size-1; ff_count; i--){
393 /* isn't this function nicer than the one in the libjpeg ? */
394 void ff_mjpeg_build_huffman_codes(uint8_t *huff_size, uint16_t *huff_code,
395 const uint8_t *bits_table,
396 const uint8_t *val_table)
402 for (int i = 1; i <= 16; i++) {
403 int nb = bits_table[i];
404 for (int j = 0; j < nb; j++) {
405 int sym = val_table[k++];
407 huff_code[sym] = code;
414 void ff_mjpeg_encode_picture_trailer(PutBitContext *pb, int header_bits)
416 av_assert1((header_bits & 7) == 0);
421 void ff_mjpeg_encode_dc(PutBitContext *pb, int val,
422 uint8_t *huff_size, uint16_t *huff_code)
427 put_bits(pb, huff_size[0], huff_code[0]);
435 nbits= av_log2_16bit(val) + 1;
437 put_bits(pb, huff_size[nbits], huff_code[nbits]);
439 put_sbits(pb, nbits, mant);