mjpegenc: remove commented out never-to-be-finished WIP cruft
[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 av_cold int ff_mjpeg_encode_init(MpegEncContext *s)
41 {
42 MJpegContext *m;
43
44 m = av_malloc(sizeof(MJpegContext));
45 if (!m)
46 return -1;
47
48 s->min_qcoeff=-1023;
49 s->max_qcoeff= 1023;
50
51 /* build all the huffman tables */
52 ff_mjpeg_build_huffman_codes(m->huff_size_dc_luminance,
53 m->huff_code_dc_luminance,
54 avpriv_mjpeg_bits_dc_luminance,
55 avpriv_mjpeg_val_dc);
56 ff_mjpeg_build_huffman_codes(m->huff_size_dc_chrominance,
57 m->huff_code_dc_chrominance,
58 avpriv_mjpeg_bits_dc_chrominance,
59 avpriv_mjpeg_val_dc);
60 ff_mjpeg_build_huffman_codes(m->huff_size_ac_luminance,
61 m->huff_code_ac_luminance,
62 avpriv_mjpeg_bits_ac_luminance,
63 avpriv_mjpeg_val_ac_luminance);
64 ff_mjpeg_build_huffman_codes(m->huff_size_ac_chrominance,
65 m->huff_code_ac_chrominance,
66 avpriv_mjpeg_bits_ac_chrominance,
67 avpriv_mjpeg_val_ac_chrominance);
68
69 s->mjpeg_ctx = m;
70 return 0;
71 }
72
73 void ff_mjpeg_encode_close(MpegEncContext *s)
74 {
75 av_free(s->mjpeg_ctx);
76 }
77
78 /* table_class: 0 = DC coef, 1 = AC coefs */
79 static int put_huffman_table(PutBitContext *p, int table_class, int table_id,
80 const uint8_t *bits_table, const uint8_t *value_table)
81 {
82 int n, i;
83
84 put_bits(p, 4, table_class);
85 put_bits(p, 4, table_id);
86
87 n = 0;
88 for(i=1;i<=16;i++) {
89 n += bits_table[i];
90 put_bits(p, 8, bits_table[i]);
91 }
92
93 for(i=0;i<n;i++)
94 put_bits(p, 8, value_table[i]);
95
96 return n + 17;
97 }
98
99 static void jpeg_table_header(MpegEncContext *s)
100 {
101 PutBitContext *p = &s->pb;
102 int i, j, size;
103 uint8_t *ptr;
104
105 /* quant matrixes */
106 put_marker(p, DQT);
107 put_bits(p, 16, 2 + 1 * (1 + 64));
108 put_bits(p, 4, 0); /* 8 bit precision */
109 put_bits(p, 4, 0); /* table 0 */
110 for(i=0;i<64;i++) {
111 j = s->intra_scantable.permutated[i];
112 put_bits(p, 8, s->intra_matrix[j]);
113 }
114
115 /* huffman table */
116 put_marker(p, DHT);
117 flush_put_bits(p);
118 ptr = put_bits_ptr(p);
119 put_bits(p, 16, 0); /* patched later */
120 size = 2;
121 size += put_huffman_table(p, 0, 0, avpriv_mjpeg_bits_dc_luminance,
122 avpriv_mjpeg_val_dc);
123 size += put_huffman_table(p, 0, 1, avpriv_mjpeg_bits_dc_chrominance,
124 avpriv_mjpeg_val_dc);
125
126 size += put_huffman_table(p, 1, 0, avpriv_mjpeg_bits_ac_luminance,
127 avpriv_mjpeg_val_ac_luminance);
128 size += put_huffman_table(p, 1, 1, avpriv_mjpeg_bits_ac_chrominance,
129 avpriv_mjpeg_val_ac_chrominance);
130 AV_WB16(ptr, size);
131 }
132
133 static void jpeg_put_comments(AVCodecContext *avctx, PutBitContext *p)
134 {
135 int size;
136 uint8_t *ptr;
137
138 if (avctx->sample_aspect_ratio.num > 0 && avctx->sample_aspect_ratio.den > 0) {
139 /* JFIF header */
140 put_marker(p, APP0);
141 put_bits(p, 16, 16);
142 avpriv_put_string(p, "JFIF", 1); /* this puts the trailing zero-byte too */
143 put_bits(p, 16, 0x0201); /* v 1.02 */
144 put_bits(p, 8, 0); /* units type: 0 - aspect ratio */
145 put_bits(p, 16, avctx->sample_aspect_ratio.num);
146 put_bits(p, 16, avctx->sample_aspect_ratio.den);
147 put_bits(p, 8, 0); /* thumbnail width */
148 put_bits(p, 8, 0); /* thumbnail height */
149 }
150
151 /* comment */
152 if (!(avctx->flags & CODEC_FLAG_BITEXACT)) {
153 put_marker(p, COM);
154 flush_put_bits(p);
155 ptr = put_bits_ptr(p);
156 put_bits(p, 16, 0); /* patched later */
157 avpriv_put_string(p, LIBAVCODEC_IDENT, 1);
158 size = strlen(LIBAVCODEC_IDENT)+3;
159 AV_WB16(ptr, size);
160 }
161
162 if (avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
163 avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
164 avctx->pix_fmt == AV_PIX_FMT_YUV444P) {
165 put_marker(p, COM);
166 flush_put_bits(p);
167 ptr = put_bits_ptr(p);
168 put_bits(p, 16, 0); /* patched later */
169 avpriv_put_string(p, "CS=ITU601", 1);
170 size = strlen("CS=ITU601")+3;
171 AV_WB16(ptr, size);
172 }
173 }
174
175 void ff_mjpeg_encode_picture_header(MpegEncContext *s)
176 {
177 const int lossless= s->avctx->codec_id != AV_CODEC_ID_MJPEG;
178
179 put_marker(&s->pb, SOI);
180
181 jpeg_put_comments(s->avctx, &s->pb);
182
183 jpeg_table_header(s);
184
185 switch(s->avctx->codec_id){
186 case AV_CODEC_ID_MJPEG: put_marker(&s->pb, SOF0 ); break;
187 case AV_CODEC_ID_LJPEG: put_marker(&s->pb, SOF3 ); break;
188 default: assert(0);
189 }
190
191 put_bits(&s->pb, 16, 17);
192 if(lossless && s->avctx->pix_fmt == AV_PIX_FMT_BGRA)
193 put_bits(&s->pb, 8, 9); /* 9 bits/component RCT */
194 else
195 put_bits(&s->pb, 8, 8); /* 8 bits/component */
196 put_bits(&s->pb, 16, s->height);
197 put_bits(&s->pb, 16, s->width);
198 put_bits(&s->pb, 8, 3); /* 3 components */
199
200 /* Y component */
201 put_bits(&s->pb, 8, 1); /* component number */
202 put_bits(&s->pb, 4, s->mjpeg_hsample[0]); /* H factor */
203 put_bits(&s->pb, 4, s->mjpeg_vsample[0]); /* V factor */
204 put_bits(&s->pb, 8, 0); /* select matrix */
205
206 /* Cb component */
207 put_bits(&s->pb, 8, 2); /* component number */
208 put_bits(&s->pb, 4, s->mjpeg_hsample[1]); /* H factor */
209 put_bits(&s->pb, 4, s->mjpeg_vsample[1]); /* V factor */
210 put_bits(&s->pb, 8, 0); /* select matrix */
211
212 /* Cr component */
213 put_bits(&s->pb, 8, 3); /* component number */
214 put_bits(&s->pb, 4, s->mjpeg_hsample[2]); /* H factor */
215 put_bits(&s->pb, 4, s->mjpeg_vsample[2]); /* V factor */
216 put_bits(&s->pb, 8, 0); /* select matrix */
217
218 /* scan header */
219 put_marker(&s->pb, SOS);
220 put_bits(&s->pb, 16, 12); /* length */
221 put_bits(&s->pb, 8, 3); /* 3 components */
222
223 /* Y component */
224 put_bits(&s->pb, 8, 1); /* index */
225 put_bits(&s->pb, 4, 0); /* DC huffman table index */
226 put_bits(&s->pb, 4, 0); /* AC huffman table index */
227
228 /* Cb component */
229 put_bits(&s->pb, 8, 2); /* index */
230 put_bits(&s->pb, 4, 1); /* DC huffman table index */
231 put_bits(&s->pb, 4, lossless ? 0 : 1); /* AC huffman table index */
232
233 /* Cr component */
234 put_bits(&s->pb, 8, 3); /* index */
235 put_bits(&s->pb, 4, 1); /* DC huffman table index */
236 put_bits(&s->pb, 4, lossless ? 0 : 1); /* AC huffman table index */
237
238 put_bits(&s->pb, 8, lossless ? s->avctx->prediction_method+1 : 0); /* Ss (not used) */
239
240 switch(s->avctx->codec_id){
241 case AV_CODEC_ID_MJPEG: put_bits(&s->pb, 8, 63); break; /* Se (not used) */
242 case AV_CODEC_ID_LJPEG: put_bits(&s->pb, 8, 0); break; /* not used */
243 default: assert(0);
244 }
245
246 put_bits(&s->pb, 8, 0); /* Ah/Al (not used) */
247 }
248
249 static void escape_FF(MpegEncContext *s, int start)
250 {
251 int size= put_bits_count(&s->pb) - start*8;
252 int i, ff_count;
253 uint8_t *buf= s->pb.buf + start;
254 int align= (-(size_t)(buf))&3;
255
256 assert((size&7) == 0);
257 size >>= 3;
258
259 ff_count=0;
260 for(i=0; i<size && i<align; i++){
261 if(buf[i]==0xFF) ff_count++;
262 }
263 for(; i<size-15; i+=16){
264 int acc, v;
265
266 v= *(uint32_t*)(&buf[i]);
267 acc= (((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
268 v= *(uint32_t*)(&buf[i+4]);
269 acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
270 v= *(uint32_t*)(&buf[i+8]);
271 acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
272 v= *(uint32_t*)(&buf[i+12]);
273 acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
274
275 acc>>=4;
276 acc+= (acc>>16);
277 acc+= (acc>>8);
278 ff_count+= acc&0xFF;
279 }
280 for(; i<size; i++){
281 if(buf[i]==0xFF) ff_count++;
282 }
283
284 if(ff_count==0) return;
285
286 flush_put_bits(&s->pb);
287 skip_put_bytes(&s->pb, ff_count);
288
289 for(i=size-1; ff_count; i--){
290 int v= buf[i];
291
292 if(v==0xFF){
293 buf[i+ff_count]= 0;
294 ff_count--;
295 }
296
297 buf[i+ff_count]= v;
298 }
299 }
300
301 void ff_mjpeg_encode_stuffing(PutBitContext * pbc)
302 {
303 int length;
304 length= (-put_bits_count(pbc))&7;
305 if(length) put_bits(pbc, length, (1<<length)-1);
306 }
307
308 void ff_mjpeg_encode_picture_trailer(MpegEncContext *s)
309 {
310 ff_mjpeg_encode_stuffing(&s->pb);
311 flush_put_bits(&s->pb);
312
313 assert((s->header_bits&7)==0);
314
315 escape_FF(s, s->header_bits>>3);
316
317 put_marker(&s->pb, EOI);
318 }
319
320 void ff_mjpeg_encode_dc(MpegEncContext *s, int val,
321 uint8_t *huff_size, uint16_t *huff_code)
322 {
323 int mant, nbits;
324
325 if (val == 0) {
326 put_bits(&s->pb, huff_size[0], huff_code[0]);
327 } else {
328 mant = val;
329 if (val < 0) {
330 val = -val;
331 mant--;
332 }
333
334 nbits= av_log2_16bit(val) + 1;
335
336 put_bits(&s->pb, huff_size[nbits], huff_code[nbits]);
337
338 put_sbits(&s->pb, nbits, mant);
339 }
340 }
341
342 static void encode_block(MpegEncContext *s, int16_t *block, int n)
343 {
344 int mant, nbits, code, i, j;
345 int component, dc, run, last_index, val;
346 MJpegContext *m = s->mjpeg_ctx;
347 uint8_t *huff_size_ac;
348 uint16_t *huff_code_ac;
349
350 /* DC coef */
351 component = (n <= 3 ? 0 : (n&1) + 1);
352 dc = block[0]; /* overflow is impossible */
353 val = dc - s->last_dc[component];
354 if (n < 4) {
355 ff_mjpeg_encode_dc(s, val, m->huff_size_dc_luminance, m->huff_code_dc_luminance);
356 huff_size_ac = m->huff_size_ac_luminance;
357 huff_code_ac = m->huff_code_ac_luminance;
358 } else {
359 ff_mjpeg_encode_dc(s, val, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
360 huff_size_ac = m->huff_size_ac_chrominance;
361 huff_code_ac = m->huff_code_ac_chrominance;
362 }
363 s->last_dc[component] = dc;
364
365 /* AC coefs */
366
367 run = 0;
368 last_index = s->block_last_index[n];
369 for(i=1;i<=last_index;i++) {
370 j = s->intra_scantable.permutated[i];
371 val = block[j];
372 if (val == 0) {
373 run++;
374 } else {
375 while (run >= 16) {
376 put_bits(&s->pb, huff_size_ac[0xf0], huff_code_ac[0xf0]);
377 run -= 16;
378 }
379 mant = val;
380 if (val < 0) {
381 val = -val;
382 mant--;
383 }
384
385 nbits= av_log2(val) + 1;
386 code = (run << 4) | nbits;
387
388 put_bits(&s->pb, huff_size_ac[code], huff_code_ac[code]);
389
390 put_sbits(&s->pb, nbits, mant);
391 run = 0;
392 }
393 }
394
395 /* output EOB only if not already 64 values */
396 if (last_index < 63 || run != 0)
397 put_bits(&s->pb, huff_size_ac[0], huff_code_ac[0]);
398 }
399
400 void ff_mjpeg_encode_mb(MpegEncContext *s, int16_t block[6][64])
401 {
402 int i;
403 for(i=0;i<5;i++) {
404 encode_block(s, block[i], i);
405 }
406 if (s->chroma_format == CHROMA_420) {
407 encode_block(s, block[5], 5);
408 } else {
409 encode_block(s, block[6], 6);
410 encode_block(s, block[5], 5);
411 encode_block(s, block[7], 7);
412 }
413
414 s->i_tex_bits += get_bits_diff(s);
415 }
416
417 AVCodec ff_mjpeg_encoder = {
418 .name = "mjpeg",
419 .long_name = NULL_IF_CONFIG_SMALL("MJPEG (Motion JPEG)"),
420 .type = AVMEDIA_TYPE_VIDEO,
421 .id = AV_CODEC_ID_MJPEG,
422 .priv_data_size = sizeof(MpegEncContext),
423 .init = ff_MPV_encode_init,
424 .encode2 = ff_MPV_encode_picture,
425 .close = ff_MPV_encode_end,
426 .pix_fmts = (const enum AVPixelFormat[]){
427 AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_NONE
428 },
429 };