]> git.sesse.net Git - ffmpeg/blob - libavcodec/mjpegenc_common.c
Merge commit '391ecc961ced2bde7aecb3053ac35191f838fae8'
[ffmpeg] / libavcodec / mjpegenc_common.c
1 /*
2  * lossless JPEG shared bits
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 #include <stdint.h>
22 #include <string.h>
23
24 #include "libavutil/common.h"
25 #include "libavutil/pixdesc.h"
26 #include "libavutil/pixfmt.h"
27
28 #include "avcodec.h"
29 #include "idctdsp.h"
30 #include "put_bits.h"
31 #include "mjpegenc_common.h"
32 #include "mjpeg.h"
33
34 /* table_class: 0 = DC coef, 1 = AC coefs */
35 static int put_huffman_table(PutBitContext *p, int table_class, int table_id,
36                              const uint8_t *bits_table, const uint8_t *value_table)
37 {
38     int n, i;
39
40     put_bits(p, 4, table_class);
41     put_bits(p, 4, table_id);
42
43     n = 0;
44     for(i=1;i<=16;i++) {
45         n += bits_table[i];
46         put_bits(p, 8, bits_table[i]);
47     }
48
49     for(i=0;i<n;i++)
50         put_bits(p, 8, value_table[i]);
51
52     return n + 17;
53 }
54
55 static void jpeg_table_header(AVCodecContext *avctx, PutBitContext *p,
56                               ScanTable *intra_scantable,
57                               uint16_t luma_intra_matrix[64],
58                               uint16_t chroma_intra_matrix[64],
59                               int hsample[3])
60 {
61     int i, j, size;
62     uint8_t *ptr;
63
64     if (avctx->codec_id != AV_CODEC_ID_LJPEG) {
65         int matrix_count = 1 + !!memcmp(luma_intra_matrix,
66                                         chroma_intra_matrix,
67                                         sizeof(luma_intra_matrix[0]) * 64);
68     /* quant matrixes */
69     put_marker(p, DQT);
70     put_bits(p, 16, 2 + matrix_count * (1 + 64));
71     put_bits(p, 4, 0); /* 8 bit precision */
72     put_bits(p, 4, 0); /* table 0 */
73     for(i=0;i<64;i++) {
74         j = intra_scantable->permutated[i];
75         put_bits(p, 8, luma_intra_matrix[j]);
76     }
77
78         if (matrix_count > 1) {
79             put_bits(p, 4, 0); /* 8 bit precision */
80             put_bits(p, 4, 1); /* table 1 */
81             for(i=0;i<64;i++) {
82                 j = intra_scantable->permutated[i];
83                 put_bits(p, 8, chroma_intra_matrix[j]);
84             }
85         }
86     }
87
88     if(avctx->active_thread_type & FF_THREAD_SLICE){
89         put_marker(p, DRI);
90         put_bits(p, 16, 4);
91         put_bits(p, 16, (avctx->width-1)/(8*hsample[0]) + 1);
92     }
93
94     /* huffman table */
95     put_marker(p, DHT);
96     flush_put_bits(p);
97     ptr = put_bits_ptr(p);
98     put_bits(p, 16, 0); /* patched later */
99     size = 2;
100     size += put_huffman_table(p, 0, 0, avpriv_mjpeg_bits_dc_luminance,
101                               avpriv_mjpeg_val_dc);
102     size += put_huffman_table(p, 0, 1, avpriv_mjpeg_bits_dc_chrominance,
103                               avpriv_mjpeg_val_dc);
104
105     size += put_huffman_table(p, 1, 0, avpriv_mjpeg_bits_ac_luminance,
106                               avpriv_mjpeg_val_ac_luminance);
107     size += put_huffman_table(p, 1, 1, avpriv_mjpeg_bits_ac_chrominance,
108                               avpriv_mjpeg_val_ac_chrominance);
109     AV_WB16(ptr, size);
110 }
111
112 static void jpeg_put_comments(AVCodecContext *avctx, PutBitContext *p)
113 {
114     int size;
115     uint8_t *ptr;
116
117     if (avctx->sample_aspect_ratio.num > 0 && avctx->sample_aspect_ratio.den > 0) {
118         /* JFIF header */
119         put_marker(p, APP0);
120         put_bits(p, 16, 16);
121         avpriv_put_string(p, "JFIF", 1); /* this puts the trailing zero-byte too */
122         put_bits(p, 16, 0x0102);         /* v 1.02 */
123         put_bits(p,  8, 0);              /* units type: 0 - aspect ratio */
124         put_bits(p, 16, avctx->sample_aspect_ratio.num);
125         put_bits(p, 16, avctx->sample_aspect_ratio.den);
126         put_bits(p, 8, 0); /* thumbnail width */
127         put_bits(p, 8, 0); /* thumbnail height */
128     }
129
130     /* comment */
131     if (!(avctx->flags & CODEC_FLAG_BITEXACT)) {
132         put_marker(p, COM);
133         flush_put_bits(p);
134         ptr = put_bits_ptr(p);
135         put_bits(p, 16, 0); /* patched later */
136         avpriv_put_string(p, LIBAVCODEC_IDENT, 1);
137         size = strlen(LIBAVCODEC_IDENT)+3;
138         AV_WB16(ptr, size);
139     }
140
141     if (avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
142         avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
143         avctx->pix_fmt == AV_PIX_FMT_YUV444P) {
144         put_marker(p, COM);
145         flush_put_bits(p);
146         ptr = put_bits_ptr(p);
147         put_bits(p, 16, 0); /* patched later */
148         avpriv_put_string(p, "CS=ITU601", 1);
149         size = strlen("CS=ITU601")+3;
150         AV_WB16(ptr, size);
151     }
152 }
153
154 void ff_mjpeg_init_hvsample(AVCodecContext *avctx, int hsample[3], int vsample[3])
155 {
156     int chroma_h_shift, chroma_v_shift;
157
158     av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift,
159                                      &chroma_v_shift);
160     if (avctx->codec->id == AV_CODEC_ID_LJPEG &&
161         (   avctx->pix_fmt == AV_PIX_FMT_BGR0
162          || avctx->pix_fmt == AV_PIX_FMT_BGRA
163          || avctx->pix_fmt == AV_PIX_FMT_BGR24)) {
164         vsample[0] = hsample[0] =
165         vsample[1] = hsample[1] =
166         vsample[2] = hsample[2] = 1;
167     } else if (avctx->pix_fmt == AV_PIX_FMT_YUV444P || avctx->pix_fmt == AV_PIX_FMT_YUVJ444P) {
168         vsample[0] = vsample[1] = vsample[2] = 2;
169         hsample[0] = hsample[1] = hsample[2] = 1;
170     } else {
171         vsample[0] = 2;
172         vsample[1] = 2 >> chroma_v_shift;
173         vsample[2] = 2 >> chroma_v_shift;
174         hsample[0] = 2;
175         hsample[1] = 2 >> chroma_h_shift;
176         hsample[2] = 2 >> chroma_h_shift;
177     }
178 }
179
180 void ff_mjpeg_encode_picture_header(AVCodecContext *avctx, PutBitContext *pb,
181                                     ScanTable *intra_scantable,
182                                     uint16_t luma_intra_matrix[64],
183                                     uint16_t chroma_intra_matrix[64])
184 {
185     const int lossless = avctx->codec_id != AV_CODEC_ID_MJPEG && avctx->codec_id != AV_CODEC_ID_AMV;
186     int hsample[3], vsample[3];
187     int i;
188     int chroma_matrix = !!memcmp(luma_intra_matrix,
189                                  chroma_intra_matrix,
190                                  sizeof(luma_intra_matrix[0])*64);
191
192     ff_mjpeg_init_hvsample(avctx, hsample, vsample);
193
194     put_marker(pb, SOI);
195
196     // hack for AMV mjpeg format
197     if(avctx->codec_id == AV_CODEC_ID_AMV) goto end;
198
199     jpeg_put_comments(avctx, pb);
200
201     jpeg_table_header(avctx, pb, intra_scantable, luma_intra_matrix, chroma_intra_matrix, hsample);
202
203     switch (avctx->codec_id) {
204     case AV_CODEC_ID_MJPEG:  put_marker(pb, SOF0 ); break;
205     case AV_CODEC_ID_LJPEG:  put_marker(pb, SOF3 ); break;
206     default: av_assert0(0);
207     }
208
209     put_bits(pb, 16, 17);
210     if (lossless && (  avctx->pix_fmt == AV_PIX_FMT_BGR0
211                     || avctx->pix_fmt == AV_PIX_FMT_BGRA
212                     || avctx->pix_fmt == AV_PIX_FMT_BGR24))
213         put_bits(pb, 8, 9); /* 9 bits/component RCT */
214     else
215         put_bits(pb, 8, 8); /* 8 bits/component */
216     put_bits(pb, 16, avctx->height);
217     put_bits(pb, 16, avctx->width);
218     put_bits(pb, 8, 3); /* 3 components */
219
220     /* Y component */
221     put_bits(pb, 8, 1); /* component number */
222     put_bits(pb, 4, hsample[0]); /* H factor */
223     put_bits(pb, 4, vsample[0]); /* V factor */
224     put_bits(pb, 8, 0); /* select matrix */
225
226     /* Cb component */
227     put_bits(pb, 8, 2); /* component number */
228     put_bits(pb, 4, hsample[1]); /* H factor */
229     put_bits(pb, 4, vsample[1]); /* V factor */
230     put_bits(pb, 8, lossless ? 0 : chroma_matrix); /* select matrix */
231
232     /* Cr component */
233     put_bits(pb, 8, 3); /* component number */
234     put_bits(pb, 4, hsample[2]); /* H factor */
235     put_bits(pb, 4, vsample[2]); /* V factor */
236     put_bits(pb, 8, lossless ? 0 : chroma_matrix); /* select matrix */
237
238     /* scan header */
239     put_marker(pb, SOS);
240     put_bits(pb, 16, 12); /* length */
241     put_bits(pb, 8, 3); /* 3 components */
242
243     /* Y component */
244     put_bits(pb, 8, 1); /* index */
245     put_bits(pb, 4, 0); /* DC huffman table index */
246     put_bits(pb, 4, 0); /* AC huffman table index */
247
248     /* Cb component */
249     put_bits(pb, 8, 2); /* index */
250     put_bits(pb, 4, 1); /* DC huffman table index */
251     put_bits(pb, 4, lossless ? 0 : 1); /* AC huffman table index */
252
253     /* Cr component */
254     put_bits(pb, 8, 3); /* index */
255     put_bits(pb, 4, 1); /* DC huffman table index */
256     put_bits(pb, 4, lossless ? 0 : 1); /* AC huffman table index */
257
258     put_bits(pb, 8, lossless ? avctx->prediction_method + 1 : 0); /* Ss (not used) */
259
260     switch (avctx->codec_id) {
261     case AV_CODEC_ID_MJPEG:  put_bits(pb, 8, 63); break; /* Se (not used) */
262     case AV_CODEC_ID_LJPEG:  put_bits(pb, 8,  0); break; /* not used */
263     default: av_assert0(0);
264     }
265
266     put_bits(pb, 8, 0); /* Ah/Al (not used) */
267
268 end:
269     if (!lossless) {
270         MpegEncContext *s = avctx->priv_data;
271         av_assert0(avctx->codec->priv_data_size == sizeof(MpegEncContext));
272
273         s->esc_pos = put_bits_count(pb) >> 3;
274         for(i=1; i<s->slice_context_count; i++)
275             s->thread_context[i]->esc_pos = 0;
276     }
277 }
278
279 void ff_mjpeg_escape_FF(PutBitContext *pb, int start)
280 {
281     int size;
282     int i, ff_count;
283     uint8_t *buf = pb->buf + start;
284     int align= (-(size_t)(buf))&3;
285     int pad = (-put_bits_count(pb))&7;
286
287     if (pad)
288         put_bits(pb, pad, (1<<pad)-1);
289
290     flush_put_bits(pb);
291     size = put_bits_count(pb) - start * 8;
292
293     av_assert1((size&7) == 0);
294     size >>= 3;
295
296     ff_count=0;
297     for(i=0; i<size && i<align; i++){
298         if(buf[i]==0xFF) ff_count++;
299     }
300     for(; i<size-15; i+=16){
301         int acc, v;
302
303         v= *(uint32_t*)(&buf[i]);
304         acc= (((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
305         v= *(uint32_t*)(&buf[i+4]);
306         acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
307         v= *(uint32_t*)(&buf[i+8]);
308         acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
309         v= *(uint32_t*)(&buf[i+12]);
310         acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
311
312         acc>>=4;
313         acc+= (acc>>16);
314         acc+= (acc>>8);
315         ff_count+= acc&0xFF;
316     }
317     for(; i<size; i++){
318         if(buf[i]==0xFF) ff_count++;
319     }
320
321     if(ff_count==0) return;
322
323     flush_put_bits(pb);
324     skip_put_bytes(pb, ff_count);
325
326     for(i=size-1; ff_count; i--){
327         int v= buf[i];
328
329         if(v==0xFF){
330             buf[i+ff_count]= 0;
331             ff_count--;
332         }
333
334         buf[i+ff_count]= v;
335     }
336 }
337
338 void ff_mjpeg_encode_stuffing(MpegEncContext *s)
339 {
340     int i;
341     PutBitContext *pbc = &s->pb;
342     int mb_y = s->mb_y - !s->mb_x;
343
344     ff_mjpeg_escape_FF(pbc, s->esc_pos);
345
346     if((s->avctx->active_thread_type & FF_THREAD_SLICE) && mb_y < s->mb_height)
347         put_marker(pbc, RST0 + (mb_y&7));
348     s->esc_pos = put_bits_count(pbc) >> 3;
349
350     for(i=0; i<3; i++)
351         s->last_dc[i] = 128 << s->intra_dc_precision;
352 }
353
354 void ff_mjpeg_encode_picture_trailer(PutBitContext *pb, int header_bits)
355 {
356     av_assert1((header_bits & 7) == 0);
357
358     put_marker(pb, EOI);
359 }
360
361 void ff_mjpeg_encode_dc(PutBitContext *pb, int val,
362                         uint8_t *huff_size, uint16_t *huff_code)
363 {
364     int mant, nbits;
365
366     if (val == 0) {
367         put_bits(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(pb, huff_size[nbits], huff_code[nbits]);
378
379         put_sbits(pb, nbits, mant);
380     }
381 }