]> git.sesse.net Git - ffmpeg/blob - libavcodec/mjpegenc.c
Merge remote-tracking branch 'cus/stable'
[ffmpeg] / 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 FFmpeg.
12  *
13  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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 //#define DEBUG
34 #include <assert.h>
35
36 #include "avcodec.h"
37 #include "dsputil.h"
38 #include "mpegvideo.h"
39 #include "mjpeg.h"
40 #include "mjpegenc.h"
41
42 /* use two quantizer tables (one for luminance and one for chrominance) */
43 /* not yet working */
44 #undef TWOMATRIXES
45
46
47 av_cold int ff_mjpeg_encode_init(MpegEncContext *s)
48 {
49     MJpegContext *m;
50
51     if (s->width > 65500 || s->height > 65500) {
52         av_log(s, AV_LOG_ERROR, "JPEG does not support resolutions above 65500x65500\n");
53         return -1;
54     }
55
56     m = av_malloc(sizeof(MJpegContext));
57     if (!m)
58         return -1;
59
60     s->min_qcoeff=-1023;
61     s->max_qcoeff= 1023;
62
63     /* build all the huffman tables */
64     ff_mjpeg_build_huffman_codes(m->huff_size_dc_luminance,
65                                  m->huff_code_dc_luminance,
66                                  ff_mjpeg_bits_dc_luminance,
67                                  ff_mjpeg_val_dc);
68     ff_mjpeg_build_huffman_codes(m->huff_size_dc_chrominance,
69                                  m->huff_code_dc_chrominance,
70                                  ff_mjpeg_bits_dc_chrominance,
71                                  ff_mjpeg_val_dc);
72     ff_mjpeg_build_huffman_codes(m->huff_size_ac_luminance,
73                                  m->huff_code_ac_luminance,
74                                  ff_mjpeg_bits_ac_luminance,
75                                  ff_mjpeg_val_ac_luminance);
76     ff_mjpeg_build_huffman_codes(m->huff_size_ac_chrominance,
77                                  m->huff_code_ac_chrominance,
78                                  ff_mjpeg_bits_ac_chrominance,
79                                  ff_mjpeg_val_ac_chrominance);
80
81     s->mjpeg_ctx = m;
82     return 0;
83 }
84
85 void ff_mjpeg_encode_close(MpegEncContext *s)
86 {
87     av_free(s->mjpeg_ctx);
88 }
89
90 /* table_class: 0 = DC coef, 1 = AC coefs */
91 static int put_huffman_table(MpegEncContext *s, int table_class, int table_id,
92                              const uint8_t *bits_table, const uint8_t *value_table)
93 {
94     PutBitContext *p = &s->pb;
95     int n, i;
96
97     put_bits(p, 4, table_class);
98     put_bits(p, 4, table_id);
99
100     n = 0;
101     for(i=1;i<=16;i++) {
102         n += bits_table[i];
103         put_bits(p, 8, bits_table[i]);
104     }
105
106     for(i=0;i<n;i++)
107         put_bits(p, 8, value_table[i]);
108
109     return n + 17;
110 }
111
112 static void jpeg_table_header(MpegEncContext *s)
113 {
114     PutBitContext *p = &s->pb;
115     int i, j, size;
116     uint8_t *ptr;
117
118     /* quant matrixes */
119     put_marker(p, DQT);
120 #ifdef TWOMATRIXES
121     put_bits(p, 16, 2 + 2 * (1 + 64));
122 #else
123     put_bits(p, 16, 2 + 1 * (1 + 64));
124 #endif
125     put_bits(p, 4, 0); /* 8 bit precision */
126     put_bits(p, 4, 0); /* table 0 */
127     for(i=0;i<64;i++) {
128         j = s->intra_scantable.permutated[i];
129         put_bits(p, 8, s->intra_matrix[j]);
130     }
131 #ifdef TWOMATRIXES
132     put_bits(p, 4, 0); /* 8 bit precision */
133     put_bits(p, 4, 1); /* table 1 */
134     for(i=0;i<64;i++) {
135         j = s->intra_scantable.permutated[i];
136         put_bits(p, 8, s->chroma_intra_matrix[j]);
137     }
138 #endif
139
140     /* huffman table */
141     put_marker(p, DHT);
142     flush_put_bits(p);
143     ptr = put_bits_ptr(p);
144     put_bits(p, 16, 0); /* patched later */
145     size = 2;
146     size += put_huffman_table(s, 0, 0, ff_mjpeg_bits_dc_luminance,
147                               ff_mjpeg_val_dc);
148     size += put_huffman_table(s, 0, 1, ff_mjpeg_bits_dc_chrominance,
149                               ff_mjpeg_val_dc);
150
151     size += put_huffman_table(s, 1, 0, ff_mjpeg_bits_ac_luminance,
152                               ff_mjpeg_val_ac_luminance);
153     size += put_huffman_table(s, 1, 1, ff_mjpeg_bits_ac_chrominance,
154                               ff_mjpeg_val_ac_chrominance);
155     AV_WB16(ptr, size);
156 }
157
158 static void jpeg_put_comments(MpegEncContext *s)
159 {
160     PutBitContext *p = &s->pb;
161     int size;
162     uint8_t *ptr;
163
164     if (s->avctx->sample_aspect_ratio.num /* && !lossless */)
165     {
166     /* JFIF header */
167     put_marker(p, APP0);
168     put_bits(p, 16, 16);
169     ff_put_string(p, "JFIF", 1); /* this puts the trailing zero-byte too */
170     put_bits(p, 16, 0x0102); /* v 1.02 */
171     put_bits(p, 8, 0); /* units type: 0 - aspect ratio */
172     put_bits(p, 16, s->avctx->sample_aspect_ratio.num);
173     put_bits(p, 16, s->avctx->sample_aspect_ratio.den);
174     put_bits(p, 8, 0); /* thumbnail width */
175     put_bits(p, 8, 0); /* thumbnail height */
176     }
177
178     /* comment */
179     if(!(s->flags & CODEC_FLAG_BITEXACT)){
180         put_marker(p, COM);
181         flush_put_bits(p);
182         ptr = put_bits_ptr(p);
183         put_bits(p, 16, 0); /* patched later */
184         ff_put_string(p, LIBAVCODEC_IDENT, 1);
185         size = strlen(LIBAVCODEC_IDENT)+3;
186         AV_WB16(ptr, size);
187     }
188
189     if(  s->avctx->pix_fmt == PIX_FMT_YUV420P
190        ||s->avctx->pix_fmt == PIX_FMT_YUV422P
191        ||s->avctx->pix_fmt == PIX_FMT_YUV444P){
192         put_marker(p, COM);
193         flush_put_bits(p);
194         ptr = put_bits_ptr(p);
195         put_bits(p, 16, 0); /* patched later */
196         ff_put_string(p, "CS=ITU601", 1);
197         size = strlen("CS=ITU601")+3;
198         AV_WB16(ptr, size);
199     }
200 }
201
202 void ff_mjpeg_encode_picture_header(MpegEncContext *s)
203 {
204     const int lossless= s->avctx->codec_id != CODEC_ID_MJPEG;
205
206     put_marker(&s->pb, SOI);
207
208     // hack for AMV mjpeg format
209     if(s->avctx->codec_id == CODEC_ID_AMV) return;
210
211     jpeg_put_comments(s);
212
213     jpeg_table_header(s);
214
215     switch(s->avctx->codec_id){
216     case CODEC_ID_MJPEG:  put_marker(&s->pb, SOF0 ); break;
217     case CODEC_ID_LJPEG:  put_marker(&s->pb, SOF3 ); break;
218     default: assert(0);
219     }
220
221     put_bits(&s->pb, 16, 17);
222     if(lossless && (s->avctx->pix_fmt == PIX_FMT_BGR0
223                     || s->avctx->pix_fmt == PIX_FMT_BGRA
224                     || s->avctx->pix_fmt == PIX_FMT_BGR24))
225         put_bits(&s->pb, 8, 9); /* 9 bits/component RCT */
226     else
227         put_bits(&s->pb, 8, 8); /* 8 bits/component */
228     put_bits(&s->pb, 16, s->height);
229     put_bits(&s->pb, 16, s->width);
230     put_bits(&s->pb, 8, 3); /* 3 components */
231
232     /* Y component */
233     put_bits(&s->pb, 8, 1); /* component number */
234     put_bits(&s->pb, 4, s->mjpeg_hsample[0]); /* H factor */
235     put_bits(&s->pb, 4, s->mjpeg_vsample[0]); /* V factor */
236     put_bits(&s->pb, 8, 0); /* select matrix */
237
238     /* Cb component */
239     put_bits(&s->pb, 8, 2); /* component number */
240     put_bits(&s->pb, 4, s->mjpeg_hsample[1]); /* H factor */
241     put_bits(&s->pb, 4, s->mjpeg_vsample[1]); /* V factor */
242 #ifdef TWOMATRIXES
243     put_bits(&s->pb, 8, lossless ? 0 : 1); /* select matrix */
244 #else
245     put_bits(&s->pb, 8, 0); /* select matrix */
246 #endif
247
248     /* Cr component */
249     put_bits(&s->pb, 8, 3); /* component number */
250     put_bits(&s->pb, 4, s->mjpeg_hsample[2]); /* H factor */
251     put_bits(&s->pb, 4, s->mjpeg_vsample[2]); /* V factor */
252 #ifdef TWOMATRIXES
253     put_bits(&s->pb, 8, lossless ? 0 : 1); /* select matrix */
254 #else
255     put_bits(&s->pb, 8, 0); /* select matrix */
256 #endif
257
258     /* scan header */
259     put_marker(&s->pb, SOS);
260     put_bits(&s->pb, 16, 12); /* length */
261     put_bits(&s->pb, 8, 3); /* 3 components */
262
263     /* Y component */
264     put_bits(&s->pb, 8, 1); /* index */
265     put_bits(&s->pb, 4, 0); /* DC huffman table index */
266     put_bits(&s->pb, 4, 0); /* AC huffman table index */
267
268     /* Cb component */
269     put_bits(&s->pb, 8, 2); /* index */
270     put_bits(&s->pb, 4, 1); /* DC huffman table index */
271     put_bits(&s->pb, 4, lossless ? 0 : 1); /* AC huffman table index */
272
273     /* Cr component */
274     put_bits(&s->pb, 8, 3); /* index */
275     put_bits(&s->pb, 4, 1); /* DC huffman table index */
276     put_bits(&s->pb, 4, lossless ? 0 : 1); /* AC huffman table index */
277
278     put_bits(&s->pb, 8, lossless ? s->avctx->prediction_method+1 : 0); /* Ss (not used) */
279
280     switch(s->avctx->codec_id){
281     case CODEC_ID_MJPEG:  put_bits(&s->pb, 8, 63); break; /* Se (not used) */
282     case CODEC_ID_LJPEG:  put_bits(&s->pb, 8,  0); break; /* not used */
283     default: assert(0);
284     }
285
286     put_bits(&s->pb, 8, 0); /* Ah/Al (not used) */
287 }
288
289 static void escape_FF(MpegEncContext *s, int start)
290 {
291     int size= put_bits_count(&s->pb) - start*8;
292     int i, ff_count;
293     uint8_t *buf= s->pb.buf + start;
294     int align= (-(size_t)(buf))&3;
295
296     assert((size&7) == 0);
297     size >>= 3;
298
299     ff_count=0;
300     for(i=0; i<size && i<align; i++){
301         if(buf[i]==0xFF) ff_count++;
302     }
303     for(; i<size-15; i+=16){
304         int acc, v;
305
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;
314
315         acc>>=4;
316         acc+= (acc>>16);
317         acc+= (acc>>8);
318         ff_count+= acc&0xFF;
319     }
320     for(; i<size; i++){
321         if(buf[i]==0xFF) ff_count++;
322     }
323
324     if(ff_count==0) return;
325
326     flush_put_bits(&s->pb);
327     skip_put_bytes(&s->pb, ff_count);
328
329     for(i=size-1; ff_count; i--){
330         int v= buf[i];
331
332         if(v==0xFF){
333 //printf("%d %d\n", i, ff_count);
334             buf[i+ff_count]= 0;
335             ff_count--;
336         }
337
338         buf[i+ff_count]= v;
339     }
340 }
341
342 void ff_mjpeg_encode_stuffing(PutBitContext * pbc)
343 {
344     int length;
345     length= (-put_bits_count(pbc))&7;
346     if(length) put_bits(pbc, length, (1<<length)-1);
347 }
348
349 void ff_mjpeg_encode_picture_trailer(MpegEncContext *s)
350 {
351     ff_mjpeg_encode_stuffing(&s->pb);
352     flush_put_bits(&s->pb);
353
354     assert((s->header_bits&7)==0);
355
356     escape_FF(s, s->header_bits>>3);
357
358     put_marker(&s->pb, EOI);
359 }
360
361 void ff_mjpeg_encode_dc(MpegEncContext *s, int val,
362                         uint8_t *huff_size, uint16_t *huff_code)
363 {
364     int mant, nbits;
365
366     if (val == 0) {
367         put_bits(&s->pb, huff_size[0], huff_code[0]);
368     } else {
369         mant = val;
370         if (val < 0) {
371             val = -val;
372             mant--;
373         }
374
375         nbits= av_log2_16bit(val) + 1;
376
377         put_bits(&s->pb, huff_size[nbits], huff_code[nbits]);
378
379         put_sbits(&s->pb, nbits, mant);
380     }
381 }
382
383 static void encode_block(MpegEncContext *s, DCTELEM *block, int n)
384 {
385     int mant, nbits, code, i, j;
386     int component, dc, run, last_index, val;
387     MJpegContext *m = s->mjpeg_ctx;
388     uint8_t *huff_size_ac;
389     uint16_t *huff_code_ac;
390
391     /* DC coef */
392     component = (n <= 3 ? 0 : (n&1) + 1);
393     dc = block[0]; /* overflow is impossible */
394     val = dc - s->last_dc[component];
395     if (n < 4) {
396         ff_mjpeg_encode_dc(s, val, m->huff_size_dc_luminance, m->huff_code_dc_luminance);
397         huff_size_ac = m->huff_size_ac_luminance;
398         huff_code_ac = m->huff_code_ac_luminance;
399     } else {
400         ff_mjpeg_encode_dc(s, val, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
401         huff_size_ac = m->huff_size_ac_chrominance;
402         huff_code_ac = m->huff_code_ac_chrominance;
403     }
404     s->last_dc[component] = dc;
405
406     /* AC coefs */
407
408     run = 0;
409     last_index = s->block_last_index[n];
410     for(i=1;i<=last_index;i++) {
411         j = s->intra_scantable.permutated[i];
412         val = block[j];
413         if (val == 0) {
414             run++;
415         } else {
416             while (run >= 16) {
417                 put_bits(&s->pb, huff_size_ac[0xf0], huff_code_ac[0xf0]);
418                 run -= 16;
419             }
420             mant = val;
421             if (val < 0) {
422                 val = -val;
423                 mant--;
424             }
425
426             nbits= av_log2(val) + 1;
427             code = (run << 4) | nbits;
428
429             put_bits(&s->pb, huff_size_ac[code], huff_code_ac[code]);
430
431             put_sbits(&s->pb, nbits, mant);
432             run = 0;
433         }
434     }
435
436     /* output EOB only if not already 64 values */
437     if (last_index < 63 || run != 0)
438         put_bits(&s->pb, huff_size_ac[0], huff_code_ac[0]);
439 }
440
441 void ff_mjpeg_encode_mb(MpegEncContext *s, DCTELEM block[6][64])
442 {
443     int i;
444     for(i=0;i<5;i++) {
445         encode_block(s, block[i], i);
446     }
447     if (s->chroma_format == CHROMA_420) {
448         encode_block(s, block[5], 5);
449     } else {
450         encode_block(s, block[6], 6);
451         encode_block(s, block[5], 5);
452         encode_block(s, block[7], 7);
453     }
454
455     s->i_tex_bits += get_bits_diff(s);
456 }
457
458 // maximum over s->mjpeg_vsample[i]
459 #define V_MAX 2
460 static int amv_encode_picture(AVCodecContext *avctx, AVPacket *pkt,
461                               const AVFrame *pic_arg, int *got_packet)
462
463 {
464     MpegEncContext *s = avctx->priv_data;
465     AVFrame pic = *pic_arg;
466     int i;
467
468     //CODEC_FLAG_EMU_EDGE have to be cleared
469     if(s->avctx->flags & CODEC_FLAG_EMU_EDGE)
470         return -1;
471
472     //picture should be flipped upside-down
473     for(i=0; i < 3; i++) {
474         pic.data[i] += (pic.linesize[i] * (s->mjpeg_vsample[i] * (8 * s->mb_height -((s->height/V_MAX)&7)) - 1 ));
475         pic.linesize[i] *= -1;
476     }
477     return ff_MPV_encode_picture(avctx, pkt, &pic, got_packet);
478 }
479
480 AVCodec ff_mjpeg_encoder = {
481     .name           = "mjpeg",
482     .type           = AVMEDIA_TYPE_VIDEO,
483     .id             = CODEC_ID_MJPEG,
484     .priv_data_size = sizeof(MpegEncContext),
485     .init           = ff_MPV_encode_init,
486     .encode2        = ff_MPV_encode_picture,
487     .close          = ff_MPV_encode_end,
488     .pix_fmts       = (const enum PixelFormat[]){
489         PIX_FMT_YUVJ420P, PIX_FMT_YUVJ422P, PIX_FMT_NONE
490     },
491     .long_name      = NULL_IF_CONFIG_SMALL("MJPEG (Motion JPEG)"),
492 };
493
494 AVCodec ff_amv_encoder = {
495     .name           = "amv",
496     .type           = AVMEDIA_TYPE_VIDEO,
497     .id             = CODEC_ID_AMV,
498     .priv_data_size = sizeof(MpegEncContext),
499     .init           = ff_MPV_encode_init,
500     .encode2        = amv_encode_picture,
501     .close          = ff_MPV_encode_end,
502     .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUVJ420P, PIX_FMT_YUVJ422P, PIX_FMT_NONE},
503     .long_name      = NULL_IF_CONFIG_SMALL("AMV Video"),
504 };