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