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"
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],
67 if (avctx->codec_id != AV_CODEC_ID_LJPEG) {
68 int matrix_count = 1 + !!memcmp(luma_intra_matrix,
70 sizeof(luma_intra_matrix[0]) * 64);
73 put_bits(p, 16, 2 + matrix_count * (1 + 64));
74 put_bits(p, 4, 0); /* 8 bit precision */
75 put_bits(p, 4, 0); /* table 0 */
77 j = intra_scantable->permutated[i];
78 put_bits(p, 8, luma_intra_matrix[j]);
81 if (matrix_count > 1) {
82 put_bits(p, 4, 0); /* 8 bit precision */
83 put_bits(p, 4, 1); /* table 1 */
85 j = intra_scantable->permutated[i];
86 put_bits(p, 8, chroma_intra_matrix[j]);
91 if(avctx->active_thread_type & FF_THREAD_SLICE){
94 put_bits(p, 16, (avctx->width-1)/(8*hsample[0]) + 1);
100 ptr = put_bits_ptr(p);
101 put_bits(p, 16, 0); /* patched later */
103 size += put_huffman_table(p, 0, 0, avpriv_mjpeg_bits_dc_luminance,
104 avpriv_mjpeg_val_dc);
105 size += put_huffman_table(p, 0, 1, avpriv_mjpeg_bits_dc_chrominance,
106 avpriv_mjpeg_val_dc);
108 size += put_huffman_table(p, 1, 0, avpriv_mjpeg_bits_ac_luminance,
109 avpriv_mjpeg_val_ac_luminance);
110 size += put_huffman_table(p, 1, 1, avpriv_mjpeg_bits_ac_chrominance,
111 avpriv_mjpeg_val_ac_chrominance);
115 static void jpeg_put_comments(AVCodecContext *avctx, PutBitContext *p)
120 if (avctx->sample_aspect_ratio.num > 0 && avctx->sample_aspect_ratio.den > 0) {
124 avpriv_put_string(p, "JFIF", 1); /* this puts the trailing zero-byte too */
125 put_bits(p, 16, 0x0102); /* v 1.02 */
126 put_bits(p, 8, 0); /* units type: 0 - aspect ratio */
127 put_bits(p, 16, avctx->sample_aspect_ratio.num);
128 put_bits(p, 16, avctx->sample_aspect_ratio.den);
129 put_bits(p, 8, 0); /* thumbnail width */
130 put_bits(p, 8, 0); /* thumbnail height */
134 if (!(avctx->flags & CODEC_FLAG_BITEXACT)) {
137 ptr = put_bits_ptr(p);
138 put_bits(p, 16, 0); /* patched later */
139 avpriv_put_string(p, LIBAVCODEC_IDENT, 1);
140 size = strlen(LIBAVCODEC_IDENT)+3;
144 if (avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
145 avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
146 avctx->pix_fmt == AV_PIX_FMT_YUV444P) {
149 ptr = put_bits_ptr(p);
150 put_bits(p, 16, 0); /* patched later */
151 avpriv_put_string(p, "CS=ITU601", 1);
152 size = strlen("CS=ITU601")+3;
157 void ff_mjpeg_init_hvsample(AVCodecContext *avctx, int hsample[3], int vsample[3])
159 int chroma_h_shift, chroma_v_shift;
161 av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift,
163 if (avctx->codec->id == AV_CODEC_ID_LJPEG &&
164 ( avctx->pix_fmt == AV_PIX_FMT_BGR0
165 || avctx->pix_fmt == AV_PIX_FMT_BGRA
166 || avctx->pix_fmt == AV_PIX_FMT_BGR24)) {
167 vsample[0] = hsample[0] =
168 vsample[1] = hsample[1] =
169 vsample[2] = hsample[2] = 1;
170 } else if (avctx->pix_fmt == AV_PIX_FMT_YUV444P || avctx->pix_fmt == AV_PIX_FMT_YUVJ444P) {
171 vsample[0] = vsample[1] = vsample[2] = 2;
172 hsample[0] = hsample[1] = hsample[2] = 1;
175 vsample[1] = 2 >> chroma_v_shift;
176 vsample[2] = 2 >> chroma_v_shift;
178 hsample[1] = 2 >> chroma_h_shift;
179 hsample[2] = 2 >> chroma_h_shift;
183 void ff_mjpeg_encode_picture_header(AVCodecContext *avctx, PutBitContext *pb,
184 ScanTable *intra_scantable,
185 uint16_t luma_intra_matrix[64],
186 uint16_t chroma_intra_matrix[64])
188 const int lossless = avctx->codec_id != AV_CODEC_ID_MJPEG && avctx->codec_id != AV_CODEC_ID_AMV;
189 int hsample[3], vsample[3];
191 int chroma_matrix = !!memcmp(luma_intra_matrix,
193 sizeof(luma_intra_matrix[0])*64);
195 ff_mjpeg_init_hvsample(avctx, hsample, vsample);
199 // hack for AMV mjpeg format
200 if(avctx->codec_id == AV_CODEC_ID_AMV) goto end;
202 jpeg_put_comments(avctx, pb);
204 jpeg_table_header(avctx, pb, intra_scantable, luma_intra_matrix, chroma_intra_matrix, hsample);
206 switch (avctx->codec_id) {
207 case AV_CODEC_ID_MJPEG: put_marker(pb, SOF0 ); break;
208 case AV_CODEC_ID_LJPEG: put_marker(pb, SOF3 ); break;
209 default: av_assert0(0);
212 put_bits(pb, 16, 17);
213 if (lossless && ( avctx->pix_fmt == AV_PIX_FMT_BGR0
214 || avctx->pix_fmt == AV_PIX_FMT_BGRA
215 || avctx->pix_fmt == AV_PIX_FMT_BGR24))
216 put_bits(pb, 8, 9); /* 9 bits/component RCT */
218 put_bits(pb, 8, 8); /* 8 bits/component */
219 put_bits(pb, 16, avctx->height);
220 put_bits(pb, 16, avctx->width);
221 put_bits(pb, 8, 3); /* 3 components */
224 put_bits(pb, 8, 1); /* component number */
225 put_bits(pb, 4, hsample[0]); /* H factor */
226 put_bits(pb, 4, vsample[0]); /* V factor */
227 put_bits(pb, 8, 0); /* select matrix */
230 put_bits(pb, 8, 2); /* component number */
231 put_bits(pb, 4, hsample[1]); /* H factor */
232 put_bits(pb, 4, vsample[1]); /* V factor */
233 put_bits(pb, 8, lossless ? 0 : chroma_matrix); /* select matrix */
236 put_bits(pb, 8, 3); /* component number */
237 put_bits(pb, 4, hsample[2]); /* H factor */
238 put_bits(pb, 4, vsample[2]); /* V factor */
239 put_bits(pb, 8, lossless ? 0 : chroma_matrix); /* select matrix */
243 put_bits(pb, 16, 12); /* length */
244 put_bits(pb, 8, 3); /* 3 components */
247 put_bits(pb, 8, 1); /* index */
248 put_bits(pb, 4, 0); /* DC huffman table index */
249 put_bits(pb, 4, 0); /* AC huffman table index */
252 put_bits(pb, 8, 2); /* index */
253 put_bits(pb, 4, 1); /* DC huffman table index */
254 put_bits(pb, 4, lossless ? 0 : 1); /* AC huffman table index */
257 put_bits(pb, 8, 3); /* index */
258 put_bits(pb, 4, 1); /* DC huffman table index */
259 put_bits(pb, 4, lossless ? 0 : 1); /* AC huffman table index */
261 put_bits(pb, 8, lossless ? avctx->prediction_method + 1 : 0); /* Ss (not used) */
263 switch (avctx->codec_id) {
264 case AV_CODEC_ID_MJPEG: put_bits(pb, 8, 63); break; /* Se (not used) */
265 case AV_CODEC_ID_LJPEG: put_bits(pb, 8, 0); break; /* not used */
266 default: av_assert0(0);
269 put_bits(pb, 8, 0); /* Ah/Al (not used) */
273 MpegEncContext *s = avctx->priv_data;
274 av_assert0(avctx->codec->priv_data_size == sizeof(MpegEncContext));
276 s->esc_pos = put_bits_count(pb) >> 3;
277 for(i=1; i<s->slice_context_count; i++)
278 s->thread_context[i]->esc_pos = 0;
282 void ff_mjpeg_escape_FF(PutBitContext *pb, int start)
286 uint8_t *buf = pb->buf + start;
287 int align= (-(size_t)(buf))&3;
288 int pad = (-put_bits_count(pb))&7;
291 put_bits(pb, pad, (1<<pad)-1);
294 size = put_bits_count(pb) - start * 8;
296 av_assert1((size&7) == 0);
300 for(i=0; i<size && i<align; i++){
301 if(buf[i]==0xFF) ff_count++;
303 for(; i<size-15; i+=16){
306 v= *(uint32_t*)(&buf[i]);
307 acc= (((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
308 v= *(uint32_t*)(&buf[i+4]);
309 acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
310 v= *(uint32_t*)(&buf[i+8]);
311 acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
312 v= *(uint32_t*)(&buf[i+12]);
313 acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
321 if(buf[i]==0xFF) ff_count++;
324 if(ff_count==0) return;
327 skip_put_bytes(pb, ff_count);
329 for(i=size-1; ff_count; i--){
341 void ff_mjpeg_encode_stuffing(MpegEncContext *s)
344 PutBitContext *pbc = &s->pb;
345 int mb_y = s->mb_y - !s->mb_x;
347 ff_mjpeg_escape_FF(pbc, s->esc_pos);
349 if((s->avctx->active_thread_type & FF_THREAD_SLICE) && mb_y < s->mb_height)
350 put_marker(pbc, RST0 + (mb_y&7));
351 s->esc_pos = put_bits_count(pbc) >> 3;
354 s->last_dc[i] = 128 << s->intra_dc_precision;
357 void ff_mjpeg_encode_picture_trailer(PutBitContext *pb, int header_bits)
359 av_assert1((header_bits & 7) == 0);
364 void ff_mjpeg_encode_dc(PutBitContext *pb, int val,
365 uint8_t *huff_size, uint16_t *huff_code)
370 put_bits(pb, huff_size[0], huff_code[0]);
378 nbits= av_log2_16bit(val) + 1;
380 put_bits(pb, huff_size[nbits], huff_code[nbits]);
382 put_sbits(pb, nbits, mant);