]> git.sesse.net Git - ffmpeg/blob - libavcodec/mjpegenc_common.c
avformat/avio: Add Metacube support
[ffmpeg] / libavcodec / mjpegenc_common.c
1 /*
2  * lossless JPEG shared bits
3  * Copyright (c) 2000, 2001 Fabrice Bellard
4  * Copyright (c) 2003 Alex Beregszaszi
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 #include <stdint.h>
24 #include <string.h>
25
26 #include "libavutil/pixdesc.h"
27 #include "libavutil/pixfmt.h"
28
29 #include "avcodec.h"
30 #include "idctdsp.h"
31 #include "jpegtables.h"
32 #include "put_bits.h"
33 #include "mjpegenc.h"
34 #include "mjpegenc_common.h"
35 #include "mjpeg.h"
36
37 /* table_class: 0 = DC coef, 1 = AC coefs */
38 static int put_huffman_table(PutBitContext *p, int table_class, int table_id,
39                              const uint8_t *bits_table, const uint8_t *value_table)
40 {
41     int n, i;
42
43     put_bits(p, 4, table_class);
44     put_bits(p, 4, table_id);
45
46     n = 0;
47     for(i=1;i<=16;i++) {
48         n += bits_table[i];
49         put_bits(p, 8, bits_table[i]);
50     }
51
52     for(i=0;i<n;i++)
53         put_bits(p, 8, value_table[i]);
54
55     return n + 17;
56 }
57
58 static void jpeg_table_header(AVCodecContext *avctx, PutBitContext *p,
59                               ScanTable *intra_scantable,
60                               uint16_t luma_intra_matrix[64],
61                               uint16_t chroma_intra_matrix[64],
62                               int hsample[3])
63 {
64     int i, j, size;
65     uint8_t *ptr;
66     MpegEncContext *s = NULL;
67
68     /* Since avctx->priv_data will point to LJpegEncContext in this case */
69     if (avctx->codec_id != AV_CODEC_ID_LJPEG)
70         s = avctx->priv_data;
71
72     if (avctx->codec_id != AV_CODEC_ID_LJPEG) {
73         int matrix_count = 1 + !!memcmp(luma_intra_matrix,
74                                         chroma_intra_matrix,
75                                         sizeof(luma_intra_matrix[0]) * 64);
76     if (s && s->force_duplicated_matrix)
77         matrix_count = 2;
78     /* quant matrixes */
79     put_marker(p, DQT);
80     put_bits(p, 16, 2 + matrix_count * (1 + 64));
81     put_bits(p, 4, 0); /* 8 bit precision */
82     put_bits(p, 4, 0); /* table 0 */
83     for(i=0;i<64;i++) {
84         j = intra_scantable->permutated[i];
85         put_bits(p, 8, luma_intra_matrix[j]);
86     }
87
88         if (matrix_count > 1) {
89             put_bits(p, 4, 0); /* 8 bit precision */
90             put_bits(p, 4, 1); /* table 1 */
91             for(i=0;i<64;i++) {
92                 j = intra_scantable->permutated[i];
93                 put_bits(p, 8, chroma_intra_matrix[j]);
94             }
95         }
96     }
97
98     if(avctx->active_thread_type & FF_THREAD_SLICE){
99         put_marker(p, DRI);
100         put_bits(p, 16, 4);
101         put_bits(p, 16, (avctx->width-1)/(8*hsample[0]) + 1);
102     }
103
104     /* huffman table */
105     put_marker(p, DHT);
106     flush_put_bits(p);
107     ptr = put_bits_ptr(p);
108     put_bits(p, 16, 0); /* patched later */
109     size = 2;
110
111     // Only MJPEG can have a variable Huffman variable. All other
112     // formats use the default Huffman table.
113     if (s && s->huffman == HUFFMAN_TABLE_OPTIMAL) {
114         size += put_huffman_table(p, 0, 0, s->mjpeg_ctx->bits_dc_luminance,
115                                   s->mjpeg_ctx->val_dc_luminance);
116         size += put_huffman_table(p, 0, 1, s->mjpeg_ctx->bits_dc_chrominance,
117                                   s->mjpeg_ctx->val_dc_chrominance);
118
119         size += put_huffman_table(p, 1, 0, s->mjpeg_ctx->bits_ac_luminance,
120                                   s->mjpeg_ctx->val_ac_luminance);
121         size += put_huffman_table(p, 1, 1, s->mjpeg_ctx->bits_ac_chrominance,
122                                   s->mjpeg_ctx->val_ac_chrominance);
123     } else {
124         size += put_huffman_table(p, 0, 0, avpriv_mjpeg_bits_dc_luminance,
125                                   avpriv_mjpeg_val_dc);
126         size += put_huffman_table(p, 0, 1, avpriv_mjpeg_bits_dc_chrominance,
127                                   avpriv_mjpeg_val_dc);
128
129         size += put_huffman_table(p, 1, 0, avpriv_mjpeg_bits_ac_luminance,
130                                   avpriv_mjpeg_val_ac_luminance);
131         size += put_huffman_table(p, 1, 1, avpriv_mjpeg_bits_ac_chrominance,
132                                   avpriv_mjpeg_val_ac_chrominance);
133     }
134     AV_WB16(ptr, size);
135 }
136
137 static void jpeg_put_comments(AVCodecContext *avctx, PutBitContext *p)
138 {
139     int size;
140     uint8_t *ptr;
141
142     if (avctx->sample_aspect_ratio.num > 0 && avctx->sample_aspect_ratio.den > 0) {
143         AVRational sar = avctx->sample_aspect_ratio;
144
145         if (sar.num > 65535 || sar.den > 65535) {
146             if (!av_reduce(&sar.num, &sar.den, avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den, 65535))
147                 av_log(avctx, AV_LOG_WARNING,
148                     "Cannot store exact aspect ratio %d:%d\n",
149                     avctx->sample_aspect_ratio.num,
150                     avctx->sample_aspect_ratio.den);
151         }
152
153         /* JFIF header */
154         put_marker(p, APP0);
155         put_bits(p, 16, 16);
156         ff_put_string(p, "JFIF", 1); /* this puts the trailing zero-byte too */
157         /* The most significant byte is used for major revisions, the least
158          * significant byte for minor revisions. Version 1.02 is the current
159          * released revision. */
160         put_bits(p, 16, 0x0102);
161         put_bits(p,  8, 0);              /* units type: 0 - aspect ratio */
162         put_bits(p, 16, sar.num);
163         put_bits(p, 16, sar.den);
164         put_bits(p, 8, 0); /* thumbnail width */
165         put_bits(p, 8, 0); /* thumbnail height */
166     }
167
168     /* comment */
169     if (!(avctx->flags & AV_CODEC_FLAG_BITEXACT)) {
170         put_marker(p, COM);
171         flush_put_bits(p);
172         ptr = put_bits_ptr(p);
173         put_bits(p, 16, 0); /* patched later */
174         ff_put_string(p, LIBAVCODEC_IDENT, 1);
175         size = strlen(LIBAVCODEC_IDENT)+3;
176         AV_WB16(ptr, size);
177     }
178
179     if (((avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
180           avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
181           avctx->pix_fmt == AV_PIX_FMT_YUV444P) && avctx->color_range != AVCOL_RANGE_JPEG)
182         || avctx->color_range == AVCOL_RANGE_MPEG) {
183         put_marker(p, COM);
184         flush_put_bits(p);
185         ptr = put_bits_ptr(p);
186         put_bits(p, 16, 0); /* patched later */
187         ff_put_string(p, "CS=ITU601", 1);
188         size = strlen("CS=ITU601")+3;
189         AV_WB16(ptr, size);
190     }
191 }
192
193 void ff_mjpeg_init_hvsample(AVCodecContext *avctx, int hsample[4], int vsample[4])
194 {
195     int chroma_h_shift, chroma_v_shift;
196
197     av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift,
198                                      &chroma_v_shift);
199     if (avctx->codec->id == AV_CODEC_ID_LJPEG &&
200         (   avctx->pix_fmt == AV_PIX_FMT_BGR0
201          || avctx->pix_fmt == AV_PIX_FMT_BGRA
202          || avctx->pix_fmt == AV_PIX_FMT_BGR24)) {
203         vsample[0] = hsample[0] =
204         vsample[1] = hsample[1] =
205         vsample[2] = hsample[2] =
206         vsample[3] = hsample[3] = 1;
207     } else if (avctx->pix_fmt == AV_PIX_FMT_YUV444P || avctx->pix_fmt == AV_PIX_FMT_YUVJ444P) {
208         vsample[0] = vsample[1] = vsample[2] = 2;
209         hsample[0] = hsample[1] = hsample[2] = 1;
210     } else {
211         vsample[0] = 2;
212         vsample[1] = 2 >> chroma_v_shift;
213         vsample[2] = 2 >> chroma_v_shift;
214         hsample[0] = 2;
215         hsample[1] = 2 >> chroma_h_shift;
216         hsample[2] = 2 >> chroma_h_shift;
217     }
218 }
219
220 void ff_mjpeg_encode_picture_header(AVCodecContext *avctx, PutBitContext *pb,
221                                     ScanTable *intra_scantable, int pred,
222                                     uint16_t luma_intra_matrix[64],
223                                     uint16_t chroma_intra_matrix[64])
224 {
225     const int lossless = avctx->codec_id != AV_CODEC_ID_MJPEG && avctx->codec_id != AV_CODEC_ID_AMV;
226     int hsample[4], vsample[4];
227     int i;
228     int components = 3 + (avctx->pix_fmt == AV_PIX_FMT_BGRA);
229     int chroma_matrix = !!memcmp(luma_intra_matrix,
230                                  chroma_intra_matrix,
231                                  sizeof(luma_intra_matrix[0])*64);
232
233     ff_mjpeg_init_hvsample(avctx, hsample, vsample);
234
235     put_marker(pb, SOI);
236
237     // hack for AMV mjpeg format
238     if(avctx->codec_id == AV_CODEC_ID_AMV) goto end;
239
240     jpeg_put_comments(avctx, pb);
241
242     jpeg_table_header(avctx, pb, intra_scantable, luma_intra_matrix, chroma_intra_matrix, hsample);
243
244     switch (avctx->codec_id) {
245     case AV_CODEC_ID_MJPEG:  put_marker(pb, SOF0 ); break;
246     case AV_CODEC_ID_LJPEG:  put_marker(pb, SOF3 ); break;
247     default: av_assert0(0);
248     }
249
250     put_bits(pb, 16, 17);
251     if (lossless && (  avctx->pix_fmt == AV_PIX_FMT_BGR0
252                     || avctx->pix_fmt == AV_PIX_FMT_BGRA
253                     || avctx->pix_fmt == AV_PIX_FMT_BGR24))
254         put_bits(pb, 8, 9); /* 9 bits/component RCT */
255     else
256         put_bits(pb, 8, 8); /* 8 bits/component */
257     put_bits(pb, 16, avctx->height);
258     put_bits(pb, 16, avctx->width);
259     put_bits(pb, 8, components); /* 3 or 4 components */
260
261     /* Y component */
262     put_bits(pb, 8, 1); /* component number */
263     put_bits(pb, 4, hsample[0]); /* H factor */
264     put_bits(pb, 4, vsample[0]); /* V factor */
265     put_bits(pb, 8, 0); /* select matrix */
266
267     /* Cb component */
268     put_bits(pb, 8, 2); /* component number */
269     put_bits(pb, 4, hsample[1]); /* H factor */
270     put_bits(pb, 4, vsample[1]); /* V factor */
271     put_bits(pb, 8, lossless ? 0 : chroma_matrix); /* select matrix */
272
273     /* Cr component */
274     put_bits(pb, 8, 3); /* component number */
275     put_bits(pb, 4, hsample[2]); /* H factor */
276     put_bits(pb, 4, vsample[2]); /* V factor */
277     put_bits(pb, 8, lossless ? 0 : chroma_matrix); /* select matrix */
278
279     if (components == 4) {
280         put_bits(pb, 8, 4); /* component number */
281         put_bits(pb, 4, hsample[3]); /* H factor */
282         put_bits(pb, 4, vsample[3]); /* V factor */
283         put_bits(pb, 8, 0); /* select matrix */
284     }
285
286     /* scan header */
287     put_marker(pb, SOS);
288     put_bits(pb, 16, 6 + 2*components); /* length */
289     put_bits(pb, 8, components); /* 3 components */
290
291     /* Y component */
292     put_bits(pb, 8, 1); /* index */
293     put_bits(pb, 4, 0); /* DC huffman table index */
294     put_bits(pb, 4, 0); /* AC huffman table index */
295
296     /* Cb component */
297     put_bits(pb, 8, 2); /* index */
298     put_bits(pb, 4, 1); /* DC huffman table index */
299     put_bits(pb, 4, lossless ? 0 : 1); /* AC huffman table index */
300
301     /* Cr component */
302     put_bits(pb, 8, 3); /* index */
303     put_bits(pb, 4, 1); /* DC huffman table index */
304     put_bits(pb, 4, lossless ? 0 : 1); /* AC huffman table index */
305
306     if (components == 4) {
307         /* Alpha component */
308         put_bits(pb, 8, 4); /* index */
309         put_bits(pb, 4, 0); /* DC huffman table index */
310         put_bits(pb, 4, 0); /* AC huffman table index */
311     }
312
313     put_bits(pb, 8, lossless ? pred : 0); /* Ss (not used) */
314
315     switch (avctx->codec_id) {
316     case AV_CODEC_ID_MJPEG:  put_bits(pb, 8, 63); break; /* Se (not used) */
317     case AV_CODEC_ID_LJPEG:  put_bits(pb, 8,  0); break; /* not used */
318     default: av_assert0(0);
319     }
320
321     put_bits(pb, 8, 0); /* Ah/Al (not used) */
322
323 end:
324     if (!lossless) {
325         MpegEncContext *s = avctx->priv_data;
326         av_assert0(avctx->codec->priv_data_size == sizeof(MpegEncContext));
327
328         s->esc_pos = put_bytes_count(pb, 0);
329         for(i=1; i<s->slice_context_count; i++)
330             s->thread_context[i]->esc_pos = 0;
331     }
332 }
333
334 void ff_mjpeg_escape_FF(PutBitContext *pb, int start)
335 {
336     int size;
337     int i, ff_count;
338     uint8_t *buf = pb->buf + start;
339     int align= (-(size_t)(buf))&3;
340     int pad = (-put_bits_count(pb))&7;
341
342     if (pad)
343         put_bits(pb, pad, (1<<pad)-1);
344
345     flush_put_bits(pb);
346     size = put_bytes_output(pb) - start;
347
348     ff_count=0;
349     for(i=0; i<size && i<align; i++){
350         if(buf[i]==0xFF) ff_count++;
351     }
352     for(; i<size-15; i+=16){
353         int acc, v;
354
355         v= *(uint32_t*)(&buf[i]);
356         acc= (((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
357         v= *(uint32_t*)(&buf[i+4]);
358         acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
359         v= *(uint32_t*)(&buf[i+8]);
360         acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
361         v= *(uint32_t*)(&buf[i+12]);
362         acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
363
364         acc>>=4;
365         acc+= (acc>>16);
366         acc+= (acc>>8);
367         ff_count+= acc&0xFF;
368     }
369     for(; i<size; i++){
370         if(buf[i]==0xFF) ff_count++;
371     }
372
373     if(ff_count==0) return;
374
375     flush_put_bits(pb);
376     skip_put_bytes(pb, ff_count);
377
378     for(i=size-1; ff_count; i--){
379         int v= buf[i];
380
381         if(v==0xFF){
382             buf[i+ff_count]= 0;
383             ff_count--;
384         }
385
386         buf[i+ff_count]= v;
387     }
388 }
389
390 /* isn't this function nicer than the one in the libjpeg ? */
391 void ff_mjpeg_build_huffman_codes(uint8_t *huff_size, uint16_t *huff_code,
392                                   const uint8_t *bits_table,
393                                   const uint8_t *val_table)
394 {
395     int k, code;
396
397     k = 0;
398     code = 0;
399     for (int i = 1; i <= 16; i++) {
400         int nb = bits_table[i];
401         for (int j = 0; j < nb; j++) {
402             int sym = val_table[k++];
403             huff_size[sym] = i;
404             huff_code[sym] = code;
405             code++;
406         }
407         code <<= 1;
408     }
409 }
410
411 void ff_mjpeg_encode_picture_trailer(PutBitContext *pb, int header_bits)
412 {
413     av_assert1((header_bits & 7) == 0);
414
415     put_marker(pb, EOI);
416 }
417
418 void ff_mjpeg_encode_dc(PutBitContext *pb, int val,
419                         uint8_t *huff_size, uint16_t *huff_code)
420 {
421     int mant, nbits;
422
423     if (val == 0) {
424         put_bits(pb, huff_size[0], huff_code[0]);
425     } else {
426         mant = val;
427         if (val < 0) {
428             val = -val;
429             mant--;
430         }
431
432         nbits= av_log2_16bit(val) + 1;
433
434         put_bits(pb, huff_size[nbits], huff_code[nbits]);
435
436         put_sbits(pb, nbits, mant);
437     }
438 }
439
440 int ff_mjpeg_encode_check_pix_fmt(AVCodecContext *avctx)
441 {
442     if (avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL &&
443         avctx->color_range != AVCOL_RANGE_JPEG &&
444         (avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
445          avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
446          avctx->pix_fmt == AV_PIX_FMT_YUV444P ||
447          avctx->color_range == AVCOL_RANGE_MPEG)) {
448         av_log(avctx, AV_LOG_ERROR,
449                "Non full-range YUV is non-standard, set strict_std_compliance "
450                "to at most unofficial to use it.\n");
451         return AVERROR(EINVAL);
452     }
453     return 0;
454 }