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/common.h"
27 #include "libavutil/pixdesc.h"
28 #include "libavutil/pixfmt.h"
32 #include "jpegtables.h"
35 #include "mjpegenc_common.h"
38 /* table_class: 0 = DC coef, 1 = AC coefs */
39 static int put_huffman_table(PutBitContext *p, int table_class, int table_id,
40 const uint8_t *bits_table, const uint8_t *value_table)
44 put_bits(p, 4, table_class);
45 put_bits(p, 4, table_id);
50 put_bits(p, 8, bits_table[i]);
54 put_bits(p, 8, value_table[i]);
59 static void jpeg_table_header(AVCodecContext *avctx, PutBitContext *p,
60 ScanTable *intra_scantable,
61 uint16_t luma_intra_matrix[64],
62 uint16_t chroma_intra_matrix[64],
68 if (avctx->codec_id != AV_CODEC_ID_LJPEG) {
69 int matrix_count = 1 + !!memcmp(luma_intra_matrix,
71 sizeof(luma_intra_matrix[0]) * 64);
74 put_bits(p, 16, 2 + matrix_count * (1 + 64));
75 put_bits(p, 4, 0); /* 8 bit precision */
76 put_bits(p, 4, 0); /* table 0 */
78 j = intra_scantable->permutated[i];
79 put_bits(p, 8, luma_intra_matrix[j]);
82 if (matrix_count > 1) {
83 put_bits(p, 4, 0); /* 8 bit precision */
84 put_bits(p, 4, 1); /* table 1 */
86 j = intra_scantable->permutated[i];
87 put_bits(p, 8, chroma_intra_matrix[j]);
92 if(avctx->active_thread_type & FF_THREAD_SLICE){
95 put_bits(p, 16, (avctx->width-1)/(8*hsample[0]) + 1);
101 ptr = put_bits_ptr(p);
102 put_bits(p, 16, 0); /* patched later */
104 size += put_huffman_table(p, 0, 0, avpriv_mjpeg_bits_dc_luminance,
105 avpriv_mjpeg_val_dc);
106 size += put_huffman_table(p, 0, 1, avpriv_mjpeg_bits_dc_chrominance,
107 avpriv_mjpeg_val_dc);
109 size += put_huffman_table(p, 1, 0, avpriv_mjpeg_bits_ac_luminance,
110 avpriv_mjpeg_val_ac_luminance);
111 size += put_huffman_table(p, 1, 1, avpriv_mjpeg_bits_ac_chrominance,
112 avpriv_mjpeg_val_ac_chrominance);
116 static void jpeg_put_comments(AVCodecContext *avctx, PutBitContext *p)
121 if (avctx->sample_aspect_ratio.num > 0 && avctx->sample_aspect_ratio.den > 0) {
125 avpriv_put_string(p, "JFIF", 1); /* this puts the trailing zero-byte too */
126 /* The most significant byte is used for major revisions, the least
127 * significant byte for minor revisions. Version 1.02 is the current
128 * released revision. */
129 put_bits(p, 16, 0x0102);
130 put_bits(p, 8, 0); /* units type: 0 - aspect ratio */
131 put_bits(p, 16, avctx->sample_aspect_ratio.num);
132 put_bits(p, 16, avctx->sample_aspect_ratio.den);
133 put_bits(p, 8, 0); /* thumbnail width */
134 put_bits(p, 8, 0); /* thumbnail height */
138 if (!(avctx->flags & AV_CODEC_FLAG_BITEXACT)) {
141 ptr = put_bits_ptr(p);
142 put_bits(p, 16, 0); /* patched later */
143 avpriv_put_string(p, LIBAVCODEC_IDENT, 1);
144 size = strlen(LIBAVCODEC_IDENT)+3;
148 if (((avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
149 avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
150 avctx->pix_fmt == AV_PIX_FMT_YUV444P) && avctx->color_range != AVCOL_RANGE_JPEG)
151 || avctx->color_range == AVCOL_RANGE_MPEG) {
154 ptr = put_bits_ptr(p);
155 put_bits(p, 16, 0); /* patched later */
156 avpriv_put_string(p, "CS=ITU601", 1);
157 size = strlen("CS=ITU601")+3;
162 void ff_mjpeg_init_hvsample(AVCodecContext *avctx, int hsample[4], int vsample[4])
164 int chroma_h_shift, chroma_v_shift;
166 av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift,
168 if (avctx->codec->id == AV_CODEC_ID_LJPEG &&
169 ( avctx->pix_fmt == AV_PIX_FMT_BGR0
170 || avctx->pix_fmt == AV_PIX_FMT_BGRA
171 || avctx->pix_fmt == AV_PIX_FMT_BGR24)) {
172 vsample[0] = hsample[0] =
173 vsample[1] = hsample[1] =
174 vsample[2] = hsample[2] =
175 vsample[3] = hsample[3] = 1;
176 } else if (avctx->pix_fmt == AV_PIX_FMT_YUV444P || avctx->pix_fmt == AV_PIX_FMT_YUVJ444P) {
177 vsample[0] = vsample[1] = vsample[2] = 2;
178 hsample[0] = hsample[1] = hsample[2] = 1;
181 vsample[1] = 2 >> chroma_v_shift;
182 vsample[2] = 2 >> chroma_v_shift;
184 hsample[1] = 2 >> chroma_h_shift;
185 hsample[2] = 2 >> chroma_h_shift;
189 void ff_mjpeg_encode_picture_header(AVCodecContext *avctx, PutBitContext *pb,
190 ScanTable *intra_scantable,
191 uint16_t luma_intra_matrix[64],
192 uint16_t chroma_intra_matrix[64])
194 const int lossless = avctx->codec_id != AV_CODEC_ID_MJPEG && avctx->codec_id != AV_CODEC_ID_AMV;
195 int hsample[4], vsample[4];
197 int components = 3 + (avctx->pix_fmt == AV_PIX_FMT_BGRA);
198 int chroma_matrix = !!memcmp(luma_intra_matrix,
200 sizeof(luma_intra_matrix[0])*64);
202 ff_mjpeg_init_hvsample(avctx, hsample, vsample);
206 // hack for AMV mjpeg format
207 if(avctx->codec_id == AV_CODEC_ID_AMV) goto end;
209 jpeg_put_comments(avctx, pb);
211 jpeg_table_header(avctx, pb, intra_scantable, luma_intra_matrix, chroma_intra_matrix, hsample);
213 switch (avctx->codec_id) {
214 case AV_CODEC_ID_MJPEG: put_marker(pb, SOF0 ); break;
215 case AV_CODEC_ID_LJPEG: put_marker(pb, SOF3 ); break;
216 default: av_assert0(0);
219 put_bits(pb, 16, 17);
220 if (lossless && ( avctx->pix_fmt == AV_PIX_FMT_BGR0
221 || avctx->pix_fmt == AV_PIX_FMT_BGRA
222 || avctx->pix_fmt == AV_PIX_FMT_BGR24))
223 put_bits(pb, 8, 9); /* 9 bits/component RCT */
225 put_bits(pb, 8, 8); /* 8 bits/component */
226 put_bits(pb, 16, avctx->height);
227 put_bits(pb, 16, avctx->width);
228 put_bits(pb, 8, components); /* 3 or 4 components */
231 put_bits(pb, 8, 1); /* component number */
232 put_bits(pb, 4, hsample[0]); /* H factor */
233 put_bits(pb, 4, vsample[0]); /* V factor */
234 put_bits(pb, 8, 0); /* select matrix */
237 put_bits(pb, 8, 2); /* component number */
238 put_bits(pb, 4, hsample[1]); /* H factor */
239 put_bits(pb, 4, vsample[1]); /* V factor */
240 put_bits(pb, 8, lossless ? 0 : chroma_matrix); /* select matrix */
243 put_bits(pb, 8, 3); /* component number */
244 put_bits(pb, 4, hsample[2]); /* H factor */
245 put_bits(pb, 4, vsample[2]); /* V factor */
246 put_bits(pb, 8, lossless ? 0 : chroma_matrix); /* select matrix */
248 if (components == 4) {
249 put_bits(pb, 8, 4); /* component number */
250 put_bits(pb, 4, hsample[3]); /* H factor */
251 put_bits(pb, 4, vsample[3]); /* V factor */
252 put_bits(pb, 8, 0); /* select matrix */
257 put_bits(pb, 16, 6 + 2*components); /* length */
258 put_bits(pb, 8, components); /* 3 components */
261 put_bits(pb, 8, 1); /* index */
262 put_bits(pb, 4, 0); /* DC huffman table index */
263 put_bits(pb, 4, 0); /* AC huffman table index */
266 put_bits(pb, 8, 2); /* index */
267 put_bits(pb, 4, 1); /* DC huffman table index */
268 put_bits(pb, 4, lossless ? 0 : 1); /* AC huffman table index */
271 put_bits(pb, 8, 3); /* index */
272 put_bits(pb, 4, 1); /* DC huffman table index */
273 put_bits(pb, 4, lossless ? 0 : 1); /* AC huffman table index */
275 if (components == 4) {
276 /* Alpha component */
277 put_bits(pb, 8, 4); /* index */
278 put_bits(pb, 4, 0); /* DC huffman table index */
279 put_bits(pb, 4, 0); /* AC huffman table index */
282 put_bits(pb, 8, lossless ? avctx->prediction_method + 1 : 0); /* Ss (not used) */
284 switch (avctx->codec_id) {
285 case AV_CODEC_ID_MJPEG: put_bits(pb, 8, 63); break; /* Se (not used) */
286 case AV_CODEC_ID_LJPEG: put_bits(pb, 8, 0); break; /* not used */
287 default: av_assert0(0);
290 put_bits(pb, 8, 0); /* Ah/Al (not used) */
294 MpegEncContext *s = avctx->priv_data;
295 av_assert0(avctx->codec->priv_data_size == sizeof(MpegEncContext));
297 s->esc_pos = put_bits_count(pb) >> 3;
298 for(i=1; i<s->slice_context_count; i++)
299 s->thread_context[i]->esc_pos = 0;
303 void ff_mjpeg_escape_FF(PutBitContext *pb, int start)
307 uint8_t *buf = pb->buf + start;
308 int align= (-(size_t)(buf))&3;
309 int pad = (-put_bits_count(pb))&7;
312 put_bits(pb, pad, (1<<pad)-1);
315 size = put_bits_count(pb) - start * 8;
317 av_assert1((size&7) == 0);
321 for(i=0; i<size && i<align; i++){
322 if(buf[i]==0xFF) ff_count++;
324 for(; i<size-15; i+=16){
327 v= *(uint32_t*)(&buf[i]);
328 acc= (((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
329 v= *(uint32_t*)(&buf[i+4]);
330 acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
331 v= *(uint32_t*)(&buf[i+8]);
332 acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
333 v= *(uint32_t*)(&buf[i+12]);
334 acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
342 if(buf[i]==0xFF) ff_count++;
345 if(ff_count==0) return;
348 skip_put_bytes(pb, ff_count);
350 for(i=size-1; ff_count; i--){
362 int ff_mjpeg_encode_stuffing(MpegEncContext *s)
365 PutBitContext *pbc = &s->pb;
366 int mb_y = s->mb_y - !s->mb_x;
368 int ret = ff_mpv_reallocate_putbitbuffer(s, put_bits_count(&s->pb) / 8 + 100,
369 put_bits_count(&s->pb) / 4 + 1000);
371 av_log(s->avctx, AV_LOG_ERROR, "Buffer reallocation failed\n");
375 ff_mjpeg_escape_FF(pbc, s->esc_pos);
377 if((s->avctx->active_thread_type & FF_THREAD_SLICE) && mb_y < s->mb_height)
378 put_marker(pbc, RST0 + (mb_y&7));
379 s->esc_pos = put_bits_count(pbc) >> 3;
383 s->last_dc[i] = 128 << s->intra_dc_precision;
388 void ff_mjpeg_encode_picture_trailer(PutBitContext *pb, int header_bits)
390 av_assert1((header_bits & 7) == 0);
395 void ff_mjpeg_encode_dc(PutBitContext *pb, int val,
396 uint8_t *huff_size, uint16_t *huff_code)
401 put_bits(pb, huff_size[0], huff_code[0]);
409 nbits= av_log2_16bit(val) + 1;
411 put_bits(pb, huff_size[nbits], huff_code[nbits]);
413 put_sbits(pb, nbits, mant);