2 * lossless JPEG shared bits
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "libavutil/common.h"
25 #include "libavutil/pixdesc.h"
26 #include "libavutil/pixfmt.h"
31 #include "mjpegenc_common.h"
34 /* table_class: 0 = DC coef, 1 = AC coefs */
35 static int put_huffman_table(PutBitContext *p, int table_class, int table_id,
36 const uint8_t *bits_table, const uint8_t *value_table)
40 put_bits(p, 4, table_class);
41 put_bits(p, 4, table_id);
46 put_bits(p, 8, bits_table[i]);
50 put_bits(p, 8, value_table[i]);
55 static void jpeg_table_header(AVCodecContext *avctx, PutBitContext *p,
56 ScanTable *intra_scantable,
57 uint16_t luma_intra_matrix[64],
58 uint16_t chroma_intra_matrix[64],
64 if (avctx->codec_id != AV_CODEC_ID_LJPEG) {
65 int matrix_count = 1 + !!memcmp(luma_intra_matrix,
67 sizeof(luma_intra_matrix[0]) * 64);
70 put_bits(p, 16, 2 + matrix_count * (1 + 64));
71 put_bits(p, 4, 0); /* 8 bit precision */
72 put_bits(p, 4, 0); /* table 0 */
74 j = intra_scantable->permutated[i];
75 put_bits(p, 8, luma_intra_matrix[j]);
78 if (matrix_count > 1) {
79 put_bits(p, 4, 0); /* 8 bit precision */
80 put_bits(p, 4, 1); /* table 1 */
82 j = intra_scantable->permutated[i];
83 put_bits(p, 8, chroma_intra_matrix[j]);
88 if(avctx->active_thread_type & FF_THREAD_SLICE){
91 put_bits(p, 16, (avctx->width-1)/(8*hsample[0]) + 1);
97 ptr = put_bits_ptr(p);
98 put_bits(p, 16, 0); /* patched later */
100 size += put_huffman_table(p, 0, 0, avpriv_mjpeg_bits_dc_luminance,
101 avpriv_mjpeg_val_dc);
102 size += put_huffman_table(p, 0, 1, avpriv_mjpeg_bits_dc_chrominance,
103 avpriv_mjpeg_val_dc);
105 size += put_huffman_table(p, 1, 0, avpriv_mjpeg_bits_ac_luminance,
106 avpriv_mjpeg_val_ac_luminance);
107 size += put_huffman_table(p, 1, 1, avpriv_mjpeg_bits_ac_chrominance,
108 avpriv_mjpeg_val_ac_chrominance);
112 static void jpeg_put_comments(AVCodecContext *avctx, PutBitContext *p)
117 if (avctx->sample_aspect_ratio.num > 0 && avctx->sample_aspect_ratio.den > 0) {
121 avpriv_put_string(p, "JFIF", 1); /* this puts the trailing zero-byte too */
122 put_bits(p, 16, 0x0102); /* v 1.02 */
123 put_bits(p, 8, 0); /* units type: 0 - aspect ratio */
124 put_bits(p, 16, avctx->sample_aspect_ratio.num);
125 put_bits(p, 16, avctx->sample_aspect_ratio.den);
126 put_bits(p, 8, 0); /* thumbnail width */
127 put_bits(p, 8, 0); /* thumbnail height */
131 if (!(avctx->flags & CODEC_FLAG_BITEXACT)) {
134 ptr = put_bits_ptr(p);
135 put_bits(p, 16, 0); /* patched later */
136 avpriv_put_string(p, LIBAVCODEC_IDENT, 1);
137 size = strlen(LIBAVCODEC_IDENT)+3;
141 if (avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
142 avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
143 avctx->pix_fmt == AV_PIX_FMT_YUV444P) {
146 ptr = put_bits_ptr(p);
147 put_bits(p, 16, 0); /* patched later */
148 avpriv_put_string(p, "CS=ITU601", 1);
149 size = strlen("CS=ITU601")+3;
154 void ff_mjpeg_init_hvsample(AVCodecContext *avctx, int hsample[3], int vsample[3])
156 int chroma_h_shift, chroma_v_shift;
158 av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift,
160 if (avctx->codec->id == AV_CODEC_ID_LJPEG &&
161 ( avctx->pix_fmt == AV_PIX_FMT_BGR0
162 || avctx->pix_fmt == AV_PIX_FMT_BGRA
163 || avctx->pix_fmt == AV_PIX_FMT_BGR24)) {
164 vsample[0] = hsample[0] =
165 vsample[1] = hsample[1] =
166 vsample[2] = hsample[2] = 1;
167 } else if (avctx->pix_fmt == AV_PIX_FMT_YUV444P || avctx->pix_fmt == AV_PIX_FMT_YUVJ444P) {
168 vsample[0] = vsample[1] = vsample[2] = 2;
169 hsample[0] = hsample[1] = hsample[2] = 1;
172 vsample[1] = 2 >> chroma_v_shift;
173 vsample[2] = 2 >> chroma_v_shift;
175 hsample[1] = 2 >> chroma_h_shift;
176 hsample[2] = 2 >> chroma_h_shift;
180 void ff_mjpeg_encode_picture_header(AVCodecContext *avctx, PutBitContext *pb,
181 ScanTable *intra_scantable,
182 uint16_t luma_intra_matrix[64],
183 uint16_t chroma_intra_matrix[64])
185 const int lossless = avctx->codec_id != AV_CODEC_ID_MJPEG && avctx->codec_id != AV_CODEC_ID_AMV;
186 int hsample[3], vsample[3];
188 int chroma_matrix = !!memcmp(luma_intra_matrix,
190 sizeof(luma_intra_matrix[0])*64);
192 ff_mjpeg_init_hvsample(avctx, hsample, vsample);
196 // hack for AMV mjpeg format
197 if(avctx->codec_id == AV_CODEC_ID_AMV) goto end;
199 jpeg_put_comments(avctx, pb);
201 jpeg_table_header(avctx, pb, intra_scantable, luma_intra_matrix, chroma_intra_matrix, hsample);
203 switch (avctx->codec_id) {
204 case AV_CODEC_ID_MJPEG: put_marker(pb, SOF0 ); break;
205 case AV_CODEC_ID_LJPEG: put_marker(pb, SOF3 ); break;
206 default: av_assert0(0);
209 put_bits(pb, 16, 17);
210 if (lossless && ( avctx->pix_fmt == AV_PIX_FMT_BGR0
211 || avctx->pix_fmt == AV_PIX_FMT_BGRA
212 || avctx->pix_fmt == AV_PIX_FMT_BGR24))
213 put_bits(pb, 8, 9); /* 9 bits/component RCT */
215 put_bits(pb, 8, 8); /* 8 bits/component */
216 put_bits(pb, 16, avctx->height);
217 put_bits(pb, 16, avctx->width);
218 put_bits(pb, 8, 3); /* 3 components */
221 put_bits(pb, 8, 1); /* component number */
222 put_bits(pb, 4, hsample[0]); /* H factor */
223 put_bits(pb, 4, vsample[0]); /* V factor */
224 put_bits(pb, 8, 0); /* select matrix */
227 put_bits(pb, 8, 2); /* component number */
228 put_bits(pb, 4, hsample[1]); /* H factor */
229 put_bits(pb, 4, vsample[1]); /* V factor */
230 put_bits(pb, 8, lossless ? 0 : chroma_matrix); /* select matrix */
233 put_bits(pb, 8, 3); /* component number */
234 put_bits(pb, 4, hsample[2]); /* H factor */
235 put_bits(pb, 4, vsample[2]); /* V factor */
236 put_bits(pb, 8, lossless ? 0 : chroma_matrix); /* select matrix */
240 put_bits(pb, 16, 12); /* length */
241 put_bits(pb, 8, 3); /* 3 components */
244 put_bits(pb, 8, 1); /* index */
245 put_bits(pb, 4, 0); /* DC huffman table index */
246 put_bits(pb, 4, 0); /* AC huffman table index */
249 put_bits(pb, 8, 2); /* index */
250 put_bits(pb, 4, 1); /* DC huffman table index */
251 put_bits(pb, 4, lossless ? 0 : 1); /* AC huffman table index */
254 put_bits(pb, 8, 3); /* index */
255 put_bits(pb, 4, 1); /* DC huffman table index */
256 put_bits(pb, 4, lossless ? 0 : 1); /* AC huffman table index */
258 put_bits(pb, 8, lossless ? avctx->prediction_method + 1 : 0); /* Ss (not used) */
260 switch (avctx->codec_id) {
261 case AV_CODEC_ID_MJPEG: put_bits(pb, 8, 63); break; /* Se (not used) */
262 case AV_CODEC_ID_LJPEG: put_bits(pb, 8, 0); break; /* not used */
263 default: av_assert0(0);
266 put_bits(pb, 8, 0); /* Ah/Al (not used) */
270 MpegEncContext *s = avctx->priv_data;
271 av_assert0(avctx->codec->priv_data_size == sizeof(MpegEncContext));
273 s->esc_pos = put_bits_count(pb) >> 3;
274 for(i=1; i<s->slice_context_count; i++)
275 s->thread_context[i]->esc_pos = 0;
279 void ff_mjpeg_escape_FF(PutBitContext *pb, int start)
283 uint8_t *buf = pb->buf + start;
284 int align= (-(size_t)(buf))&3;
285 int pad = (-put_bits_count(pb))&7;
288 put_bits(pb, pad, (1<<pad)-1);
291 size = put_bits_count(pb) - start * 8;
293 av_assert1((size&7) == 0);
297 for(i=0; i<size && i<align; i++){
298 if(buf[i]==0xFF) ff_count++;
300 for(; i<size-15; i+=16){
303 v= *(uint32_t*)(&buf[i]);
304 acc= (((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
305 v= *(uint32_t*)(&buf[i+4]);
306 acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
307 v= *(uint32_t*)(&buf[i+8]);
308 acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
309 v= *(uint32_t*)(&buf[i+12]);
310 acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
318 if(buf[i]==0xFF) ff_count++;
321 if(ff_count==0) return;
324 skip_put_bytes(pb, ff_count);
326 for(i=size-1; ff_count; i--){
338 void ff_mjpeg_encode_stuffing(MpegEncContext *s)
341 PutBitContext *pbc = &s->pb;
342 int mb_y = s->mb_y - !s->mb_x;
344 ff_mjpeg_escape_FF(pbc, s->esc_pos);
346 if((s->avctx->active_thread_type & FF_THREAD_SLICE) && mb_y < s->mb_height)
347 put_marker(pbc, RST0 + (mb_y&7));
348 s->esc_pos = put_bits_count(pbc) >> 3;
351 s->last_dc[i] = 128 << s->intra_dc_precision;
354 void ff_mjpeg_encode_picture_trailer(PutBitContext *pb, int header_bits)
356 av_assert1((header_bits & 7) == 0);
361 void ff_mjpeg_encode_dc(PutBitContext *pb, int val,
362 uint8_t *huff_size, uint16_t *huff_code)
367 put_bits(pb, huff_size[0], huff_code[0]);
375 nbits= av_log2_16bit(val) + 1;
377 put_bits(pb, huff_size[nbits], huff_code[nbits]);
379 put_sbits(pb, nbits, mant);