5020
|
1 /*
|
|
2 * MJPEG encoder
|
|
3 * Copyright (c) 2000, 2001 Fabrice Bellard.
|
|
4 * Copyright (c) 2003 Alex Beregszaszi
|
|
5 * Copyright (c) 2003-2004 Michael Niedermayer
|
|
6 *
|
|
7 * This file is part of FFmpeg.
|
|
8 *
|
|
9 * FFmpeg is free software; you can redistribute it and/or
|
|
10 * modify it under the terms of the GNU Lesser General Public
|
|
11 * License as published by the Free Software Foundation; either
|
|
12 * version 2.1 of the License, or (at your option) any later version.
|
|
13 *
|
|
14 * FFmpeg is distributed in the hope that it will be useful,
|
|
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
17 * Lesser General Public License for more details.
|
|
18 *
|
|
19 * You should have received a copy of the GNU Lesser General Public
|
|
20 * License along with FFmpeg; if not, write to the Free Software
|
|
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
22 *
|
|
23 * Support for external huffman table, various fixes (AVID workaround),
|
|
24 * aspecting, new decode_frame mechanism and apple mjpeg-b support
|
|
25 * by Alex Beregszaszi
|
|
26 */
|
|
27
|
|
28 /**
|
|
29 * @file mjpegenc.c
|
|
30 * MJPEG encoder.
|
|
31 */
|
|
32
|
|
33 //#define DEBUG
|
|
34 #include <assert.h>
|
|
35
|
|
36 #include "avcodec.h"
|
|
37 #include "dsputil.h"
|
|
38 #include "mpegvideo.h"
|
|
39 #include "mjpeg.h"
|
5028
|
40 #include "mjpegenc.h"
|
5020
|
41
|
|
42 /* use two quantizer tables (one for luminance and one for chrominance) */
|
|
43 /* not yet working */
|
|
44 #undef TWOMATRIXES
|
|
45
|
|
46
|
|
47 int mjpeg_init(MpegEncContext *s)
|
|
48 {
|
|
49 MJpegContext *m;
|
|
50
|
|
51 m = av_malloc(sizeof(MJpegContext));
|
|
52 if (!m)
|
|
53 return -1;
|
|
54
|
|
55 s->min_qcoeff=-1023;
|
|
56 s->max_qcoeff= 1023;
|
|
57
|
|
58 /* build all the huffman tables */
|
5021
|
59 ff_mjpeg_build_huffman_codes(m->huff_size_dc_luminance,
|
|
60 m->huff_code_dc_luminance,
|
|
61 ff_mjpeg_bits_dc_luminance,
|
|
62 ff_mjpeg_val_dc_luminance);
|
|
63 ff_mjpeg_build_huffman_codes(m->huff_size_dc_chrominance,
|
|
64 m->huff_code_dc_chrominance,
|
|
65 ff_mjpeg_bits_dc_chrominance,
|
|
66 ff_mjpeg_val_dc_chrominance);
|
|
67 ff_mjpeg_build_huffman_codes(m->huff_size_ac_luminance,
|
|
68 m->huff_code_ac_luminance,
|
|
69 ff_mjpeg_bits_ac_luminance,
|
|
70 ff_mjpeg_val_ac_luminance);
|
|
71 ff_mjpeg_build_huffman_codes(m->huff_size_ac_chrominance,
|
|
72 m->huff_code_ac_chrominance,
|
|
73 ff_mjpeg_bits_ac_chrominance,
|
|
74 ff_mjpeg_val_ac_chrominance);
|
5020
|
75
|
|
76 s->mjpeg_ctx = m;
|
|
77 return 0;
|
|
78 }
|
|
79
|
|
80 void mjpeg_close(MpegEncContext *s)
|
|
81 {
|
|
82 av_free(s->mjpeg_ctx);
|
|
83 }
|
|
84
|
|
85 /* table_class: 0 = DC coef, 1 = AC coefs */
|
|
86 static int put_huffman_table(MpegEncContext *s, int table_class, int table_id,
|
|
87 const uint8_t *bits_table, const uint8_t *value_table)
|
|
88 {
|
|
89 PutBitContext *p = &s->pb;
|
|
90 int n, i;
|
|
91
|
|
92 put_bits(p, 4, table_class);
|
|
93 put_bits(p, 4, table_id);
|
|
94
|
|
95 n = 0;
|
|
96 for(i=1;i<=16;i++) {
|
|
97 n += bits_table[i];
|
|
98 put_bits(p, 8, bits_table[i]);
|
|
99 }
|
|
100
|
|
101 for(i=0;i<n;i++)
|
|
102 put_bits(p, 8, value_table[i]);
|
|
103
|
|
104 return n + 17;
|
|
105 }
|
|
106
|
|
107 static void jpeg_table_header(MpegEncContext *s)
|
|
108 {
|
|
109 PutBitContext *p = &s->pb;
|
|
110 int i, j, size;
|
|
111 uint8_t *ptr;
|
|
112
|
|
113 /* quant matrixes */
|
|
114 put_marker(p, DQT);
|
|
115 #ifdef TWOMATRIXES
|
|
116 put_bits(p, 16, 2 + 2 * (1 + 64));
|
|
117 #else
|
|
118 put_bits(p, 16, 2 + 1 * (1 + 64));
|
|
119 #endif
|
|
120 put_bits(p, 4, 0); /* 8 bit precision */
|
|
121 put_bits(p, 4, 0); /* table 0 */
|
|
122 for(i=0;i<64;i++) {
|
|
123 j = s->intra_scantable.permutated[i];
|
|
124 put_bits(p, 8, s->intra_matrix[j]);
|
|
125 }
|
|
126 #ifdef TWOMATRIXES
|
|
127 put_bits(p, 4, 0); /* 8 bit precision */
|
|
128 put_bits(p, 4, 1); /* table 1 */
|
|
129 for(i=0;i<64;i++) {
|
|
130 j = s->intra_scantable.permutated[i];
|
|
131 put_bits(p, 8, s->chroma_intra_matrix[j]);
|
|
132 }
|
|
133 #endif
|
|
134
|
|
135 /* huffman table */
|
|
136 put_marker(p, DHT);
|
|
137 flush_put_bits(p);
|
|
138 ptr = pbBufPtr(p);
|
|
139 put_bits(p, 16, 0); /* patched later */
|
|
140 size = 2;
|
5021
|
141 size += put_huffman_table(s, 0, 0, ff_mjpeg_bits_dc_luminance,
|
|
142 ff_mjpeg_val_dc_luminance);
|
|
143 size += put_huffman_table(s, 0, 1, ff_mjpeg_bits_dc_chrominance,
|
|
144 ff_mjpeg_val_dc_chrominance);
|
5020
|
145
|
5021
|
146 size += put_huffman_table(s, 1, 0, ff_mjpeg_bits_ac_luminance,
|
|
147 ff_mjpeg_val_ac_luminance);
|
|
148 size += put_huffman_table(s, 1, 1, ff_mjpeg_bits_ac_chrominance,
|
|
149 ff_mjpeg_val_ac_chrominance);
|
5020
|
150 ptr[0] = size >> 8;
|
|
151 ptr[1] = size;
|
|
152 }
|
|
153
|
|
154 static void jpeg_put_comments(MpegEncContext *s)
|
|
155 {
|
|
156 PutBitContext *p = &s->pb;
|
|
157 int size;
|
|
158 uint8_t *ptr;
|
|
159
|
|
160 if (s->aspect_ratio_info /* && !lossless */)
|
|
161 {
|
|
162 /* JFIF header */
|
|
163 put_marker(p, APP0);
|
|
164 put_bits(p, 16, 16);
|
|
165 ff_put_string(p, "JFIF", 1); /* this puts the trailing zero-byte too */
|
|
166 put_bits(p, 16, 0x0201); /* v 1.02 */
|
|
167 put_bits(p, 8, 0); /* units type: 0 - aspect ratio */
|
|
168 put_bits(p, 16, s->avctx->sample_aspect_ratio.num);
|
|
169 put_bits(p, 16, s->avctx->sample_aspect_ratio.den);
|
|
170 put_bits(p, 8, 0); /* thumbnail width */
|
|
171 put_bits(p, 8, 0); /* thumbnail height */
|
|
172 }
|
|
173
|
|
174 /* comment */
|
|
175 if(!(s->flags & CODEC_FLAG_BITEXACT)){
|
|
176 put_marker(p, COM);
|
|
177 flush_put_bits(p);
|
|
178 ptr = pbBufPtr(p);
|
|
179 put_bits(p, 16, 0); /* patched later */
|
|
180 ff_put_string(p, LIBAVCODEC_IDENT, 1);
|
|
181 size = strlen(LIBAVCODEC_IDENT)+3;
|
|
182 ptr[0] = size >> 8;
|
|
183 ptr[1] = size;
|
|
184 }
|
|
185
|
|
186 if( s->avctx->pix_fmt == PIX_FMT_YUV420P
|
|
187 ||s->avctx->pix_fmt == PIX_FMT_YUV422P
|
|
188 ||s->avctx->pix_fmt == PIX_FMT_YUV444P){
|
|
189 put_marker(p, COM);
|
|
190 flush_put_bits(p);
|
|
191 ptr = pbBufPtr(p);
|
|
192 put_bits(p, 16, 0); /* patched later */
|
|
193 ff_put_string(p, "CS=ITU601", 1);
|
|
194 size = strlen("CS=ITU601")+3;
|
|
195 ptr[0] = size >> 8;
|
|
196 ptr[1] = size;
|
|
197 }
|
|
198 }
|
|
199
|
|
200 void mjpeg_picture_header(MpegEncContext *s)
|
|
201 {
|
|
202 const int lossless= s->avctx->codec_id != CODEC_ID_MJPEG;
|
|
203
|
|
204 put_marker(&s->pb, SOI);
|
|
205
|
|
206 jpeg_put_comments(s);
|
|
207
|
|
208 jpeg_table_header(s);
|
|
209
|
|
210 switch(s->avctx->codec_id){
|
|
211 case CODEC_ID_MJPEG: put_marker(&s->pb, SOF0 ); break;
|
|
212 case CODEC_ID_LJPEG: put_marker(&s->pb, SOF3 ); break;
|
|
213 default: assert(0);
|
|
214 }
|
|
215
|
|
216 put_bits(&s->pb, 16, 17);
|
|
217 if(lossless && s->avctx->pix_fmt == PIX_FMT_RGB32)
|
|
218 put_bits(&s->pb, 8, 9); /* 9 bits/component RCT */
|
|
219 else
|
|
220 put_bits(&s->pb, 8, 8); /* 8 bits/component */
|
|
221 put_bits(&s->pb, 16, s->height);
|
|
222 put_bits(&s->pb, 16, s->width);
|
|
223 put_bits(&s->pb, 8, 3); /* 3 components */
|
|
224
|
|
225 /* Y component */
|
|
226 put_bits(&s->pb, 8, 1); /* component number */
|
|
227 put_bits(&s->pb, 4, s->mjpeg_hsample[0]); /* H factor */
|
|
228 put_bits(&s->pb, 4, s->mjpeg_vsample[0]); /* V factor */
|
|
229 put_bits(&s->pb, 8, 0); /* select matrix */
|
|
230
|
|
231 /* Cb component */
|
|
232 put_bits(&s->pb, 8, 2); /* component number */
|
|
233 put_bits(&s->pb, 4, s->mjpeg_hsample[1]); /* H factor */
|
|
234 put_bits(&s->pb, 4, s->mjpeg_vsample[1]); /* V factor */
|
|
235 #ifdef TWOMATRIXES
|
|
236 put_bits(&s->pb, 8, lossless ? 0 : 1); /* select matrix */
|
|
237 #else
|
|
238 put_bits(&s->pb, 8, 0); /* select matrix */
|
|
239 #endif
|
|
240
|
|
241 /* Cr component */
|
|
242 put_bits(&s->pb, 8, 3); /* component number */
|
|
243 put_bits(&s->pb, 4, s->mjpeg_hsample[2]); /* H factor */
|
|
244 put_bits(&s->pb, 4, s->mjpeg_vsample[2]); /* V factor */
|
|
245 #ifdef TWOMATRIXES
|
|
246 put_bits(&s->pb, 8, lossless ? 0 : 1); /* select matrix */
|
|
247 #else
|
|
248 put_bits(&s->pb, 8, 0); /* select matrix */
|
|
249 #endif
|
|
250
|
|
251 /* scan header */
|
|
252 put_marker(&s->pb, SOS);
|
|
253 put_bits(&s->pb, 16, 12); /* length */
|
|
254 put_bits(&s->pb, 8, 3); /* 3 components */
|
|
255
|
|
256 /* Y component */
|
|
257 put_bits(&s->pb, 8, 1); /* index */
|
|
258 put_bits(&s->pb, 4, 0); /* DC huffman table index */
|
|
259 put_bits(&s->pb, 4, 0); /* AC huffman table index */
|
|
260
|
|
261 /* Cb component */
|
|
262 put_bits(&s->pb, 8, 2); /* index */
|
|
263 put_bits(&s->pb, 4, 1); /* DC huffman table index */
|
|
264 put_bits(&s->pb, 4, lossless ? 0 : 1); /* AC huffman table index */
|
|
265
|
|
266 /* Cr component */
|
|
267 put_bits(&s->pb, 8, 3); /* index */
|
|
268 put_bits(&s->pb, 4, 1); /* DC huffman table index */
|
|
269 put_bits(&s->pb, 4, lossless ? 0 : 1); /* AC huffman table index */
|
|
270
|
|
271 put_bits(&s->pb, 8, lossless ? s->avctx->prediction_method+1 : 0); /* Ss (not used) */
|
|
272
|
|
273 switch(s->avctx->codec_id){
|
|
274 case CODEC_ID_MJPEG: put_bits(&s->pb, 8, 63); break; /* Se (not used) */
|
|
275 case CODEC_ID_LJPEG: put_bits(&s->pb, 8, 0); break; /* not used */
|
|
276 default: assert(0);
|
|
277 }
|
|
278
|
|
279 put_bits(&s->pb, 8, 0); /* Ah/Al (not used) */
|
|
280 }
|
|
281
|
|
282 static void escape_FF(MpegEncContext *s, int start)
|
|
283 {
|
|
284 int size= put_bits_count(&s->pb) - start*8;
|
|
285 int i, ff_count;
|
|
286 uint8_t *buf= s->pb.buf + start;
|
|
287 int align= (-(size_t)(buf))&3;
|
|
288
|
|
289 assert((size&7) == 0);
|
|
290 size >>= 3;
|
|
291
|
|
292 ff_count=0;
|
|
293 for(i=0; i<size && i<align; i++){
|
|
294 if(buf[i]==0xFF) ff_count++;
|
|
295 }
|
|
296 for(; i<size-15; i+=16){
|
|
297 int acc, v;
|
|
298
|
|
299 v= *(uint32_t*)(&buf[i]);
|
|
300 acc= (((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
|
|
301 v= *(uint32_t*)(&buf[i+4]);
|
|
302 acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
|
|
303 v= *(uint32_t*)(&buf[i+8]);
|
|
304 acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
|
|
305 v= *(uint32_t*)(&buf[i+12]);
|
|
306 acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
|
|
307
|
|
308 acc>>=4;
|
|
309 acc+= (acc>>16);
|
|
310 acc+= (acc>>8);
|
|
311 ff_count+= acc&0xFF;
|
|
312 }
|
|
313 for(; i<size; i++){
|
|
314 if(buf[i]==0xFF) ff_count++;
|
|
315 }
|
|
316
|
|
317 if(ff_count==0) return;
|
|
318
|
|
319 /* skip put bits */
|
|
320 for(i=0; i<ff_count-3; i+=4)
|
|
321 put_bits(&s->pb, 32, 0);
|
|
322 put_bits(&s->pb, (ff_count-i)*8, 0);
|
|
323 flush_put_bits(&s->pb);
|
|
324
|
|
325 for(i=size-1; ff_count; i--){
|
|
326 int v= buf[i];
|
|
327
|
|
328 if(v==0xFF){
|
|
329 //printf("%d %d\n", i, ff_count);
|
|
330 buf[i+ff_count]= 0;
|
|
331 ff_count--;
|
|
332 }
|
|
333
|
|
334 buf[i+ff_count]= v;
|
|
335 }
|
|
336 }
|
|
337
|
|
338 void ff_mjpeg_stuffing(PutBitContext * pbc)
|
|
339 {
|
|
340 int length;
|
|
341 length= (-put_bits_count(pbc))&7;
|
|
342 if(length) put_bits(pbc, length, (1<<length)-1);
|
|
343 }
|
|
344
|
|
345 void mjpeg_picture_trailer(MpegEncContext *s)
|
|
346 {
|
|
347 ff_mjpeg_stuffing(&s->pb);
|
|
348 flush_put_bits(&s->pb);
|
|
349
|
|
350 assert((s->header_bits&7)==0);
|
|
351
|
|
352 escape_FF(s, s->header_bits>>3);
|
|
353
|
|
354 put_marker(&s->pb, EOI);
|
|
355 }
|
|
356
|
5028
|
357 void mjpeg_encode_dc(MpegEncContext *s, int val,
|
5020
|
358 uint8_t *huff_size, uint16_t *huff_code)
|
|
359 {
|
|
360 int mant, nbits;
|
|
361
|
|
362 if (val == 0) {
|
|
363 put_bits(&s->pb, huff_size[0], huff_code[0]);
|
|
364 } else {
|
|
365 mant = val;
|
|
366 if (val < 0) {
|
|
367 val = -val;
|
|
368 mant--;
|
|
369 }
|
|
370
|
|
371 nbits= av_log2_16bit(val) + 1;
|
|
372
|
|
373 put_bits(&s->pb, huff_size[nbits], huff_code[nbits]);
|
|
374
|
|
375 put_bits(&s->pb, nbits, mant & ((1 << nbits) - 1));
|
|
376 }
|
|
377 }
|
|
378
|
|
379 static void encode_block(MpegEncContext *s, DCTELEM *block, int n)
|
|
380 {
|
|
381 int mant, nbits, code, i, j;
|
|
382 int component, dc, run, last_index, val;
|
|
383 MJpegContext *m = s->mjpeg_ctx;
|
|
384 uint8_t *huff_size_ac;
|
|
385 uint16_t *huff_code_ac;
|
|
386
|
|
387 /* DC coef */
|
|
388 component = (n <= 3 ? 0 : (n&1) + 1);
|
|
389 dc = block[0]; /* overflow is impossible */
|
|
390 val = dc - s->last_dc[component];
|
|
391 if (n < 4) {
|
|
392 mjpeg_encode_dc(s, val, m->huff_size_dc_luminance, m->huff_code_dc_luminance);
|
|
393 huff_size_ac = m->huff_size_ac_luminance;
|
|
394 huff_code_ac = m->huff_code_ac_luminance;
|
|
395 } else {
|
|
396 mjpeg_encode_dc(s, val, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
|
|
397 huff_size_ac = m->huff_size_ac_chrominance;
|
|
398 huff_code_ac = m->huff_code_ac_chrominance;
|
|
399 }
|
|
400 s->last_dc[component] = dc;
|
|
401
|
|
402 /* AC coefs */
|
|
403
|
|
404 run = 0;
|
|
405 last_index = s->block_last_index[n];
|
|
406 for(i=1;i<=last_index;i++) {
|
|
407 j = s->intra_scantable.permutated[i];
|
|
408 val = block[j];
|
|
409 if (val == 0) {
|
|
410 run++;
|
|
411 } else {
|
|
412 while (run >= 16) {
|
|
413 put_bits(&s->pb, huff_size_ac[0xf0], huff_code_ac[0xf0]);
|
|
414 run -= 16;
|
|
415 }
|
|
416 mant = val;
|
|
417 if (val < 0) {
|
|
418 val = -val;
|
|
419 mant--;
|
|
420 }
|
|
421
|
|
422 nbits= av_log2(val) + 1;
|
|
423 code = (run << 4) | nbits;
|
|
424
|
|
425 put_bits(&s->pb, huff_size_ac[code], huff_code_ac[code]);
|
|
426
|
|
427 put_bits(&s->pb, nbits, mant & ((1 << nbits) - 1));
|
|
428 run = 0;
|
|
429 }
|
|
430 }
|
|
431
|
|
432 /* output EOB only if not already 64 values */
|
|
433 if (last_index < 63 || run != 0)
|
|
434 put_bits(&s->pb, huff_size_ac[0], huff_code_ac[0]);
|
|
435 }
|
|
436
|
|
437 void mjpeg_encode_mb(MpegEncContext *s,
|
|
438 DCTELEM block[6][64])
|
|
439 {
|
|
440 int i;
|
|
441 for(i=0;i<5;i++) {
|
|
442 encode_block(s, block[i], i);
|
|
443 }
|
|
444 if (s->chroma_format == CHROMA_420) {
|
|
445 encode_block(s, block[5], 5);
|
|
446 } else {
|
|
447 encode_block(s, block[6], 6);
|
|
448 encode_block(s, block[5], 5);
|
|
449 encode_block(s, block[7], 7);
|
|
450 }
|
|
451 }
|