Libav
mjpegenc_common.c
Go to the documentation of this file.
1 /*
2  * lossless JPEG shared bits
3  *
4  * This file is part of Libav.
5  *
6  * Libav 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  * Libav 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 Libav; 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(PutBitContext *p, ScanTable *intra_scantable,
56  uint16_t intra_matrix[64])
57 {
58  int i, j, size;
59  uint8_t *ptr;
60 
61  /* quant matrixes */
62  put_marker(p, DQT);
63  put_bits(p, 16, 2 + 1 * (1 + 64));
64  put_bits(p, 4, 0); /* 8 bit precision */
65  put_bits(p, 4, 0); /* table 0 */
66  for(i=0;i<64;i++) {
67  j = intra_scantable->permutated[i];
68  put_bits(p, 8, intra_matrix[j]);
69  }
70 
71  /* huffman table */
72  put_marker(p, DHT);
73  flush_put_bits(p);
74  ptr = put_bits_ptr(p);
75  put_bits(p, 16, 0); /* patched later */
76  size = 2;
81 
86  AV_WB16(ptr, size);
87 }
88 
90 {
91  int size;
92  uint8_t *ptr;
93 
94  if (avctx->sample_aspect_ratio.num > 0 && avctx->sample_aspect_ratio.den > 0) {
95  /* JFIF header */
96  put_marker(p, APP0);
97  put_bits(p, 16, 16);
98  avpriv_put_string(p, "JFIF", 1); /* this puts the trailing zero-byte too */
99  put_bits(p, 16, 0x0201); /* v 1.02 */
100  put_bits(p, 8, 0); /* units type: 0 - aspect ratio */
101  put_bits(p, 16, avctx->sample_aspect_ratio.num);
102  put_bits(p, 16, avctx->sample_aspect_ratio.den);
103  put_bits(p, 8, 0); /* thumbnail width */
104  put_bits(p, 8, 0); /* thumbnail height */
105  }
106 
107  /* comment */
108  if (!(avctx->flags & CODEC_FLAG_BITEXACT)) {
109  put_marker(p, COM);
110  flush_put_bits(p);
111  ptr = put_bits_ptr(p);
112  put_bits(p, 16, 0); /* patched later */
114  size = strlen(LIBAVCODEC_IDENT)+3;
115  AV_WB16(ptr, size);
116  }
117 
118  if (avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
119  avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
120  avctx->pix_fmt == AV_PIX_FMT_YUV444P) {
121  put_marker(p, COM);
122  flush_put_bits(p);
123  ptr = put_bits_ptr(p);
124  put_bits(p, 16, 0); /* patched later */
125  avpriv_put_string(p, "CS=ITU601", 1);
126  size = strlen("CS=ITU601")+3;
127  AV_WB16(ptr, size);
128  }
129 }
130 
132  ScanTable *intra_scantable,
133  uint16_t intra_matrix[64])
134 {
135  int chroma_h_shift, chroma_v_shift;
136  const int lossless = avctx->codec_id != AV_CODEC_ID_MJPEG;
137  int hsample[3], vsample[3];
138 
139  av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift,
140  &chroma_v_shift);
141 
142  if (avctx->codec->id == AV_CODEC_ID_LJPEG &&
143  avctx->pix_fmt == AV_PIX_FMT_BGR24) {
144  vsample[0] = hsample[0] =
145  vsample[1] = hsample[1] =
146  vsample[2] = hsample[2] = 1;
147  } else {
148  vsample[0] = 2;
149  vsample[1] = 2 >> chroma_v_shift;
150  vsample[2] = 2 >> chroma_v_shift;
151  hsample[0] = 2;
152  hsample[1] = 2 >> chroma_h_shift;
153  hsample[2] = 2 >> chroma_h_shift;
154  }
155 
156  put_marker(pb, SOI);
157 
158  jpeg_put_comments(avctx, pb);
159 
160  jpeg_table_header(pb, intra_scantable, intra_matrix);
161 
162  switch (avctx->codec_id) {
163  case AV_CODEC_ID_MJPEG: put_marker(pb, SOF0 ); break;
164  case AV_CODEC_ID_LJPEG: put_marker(pb, SOF3 ); break;
165  default: assert(0);
166  }
167 
168  put_bits(pb, 16, 17);
169  if (lossless && avctx->pix_fmt == AV_PIX_FMT_BGR24)
170  put_bits(pb, 8, 9); /* 9 bits/component RCT */
171  else
172  put_bits(pb, 8, 8); /* 8 bits/component */
173  put_bits(pb, 16, avctx->height);
174  put_bits(pb, 16, avctx->width);
175  put_bits(pb, 8, 3); /* 3 components */
176 
177  /* Y component */
178  put_bits(pb, 8, 1); /* component number */
179  put_bits(pb, 4, hsample[0]); /* H factor */
180  put_bits(pb, 4, vsample[0]); /* V factor */
181  put_bits(pb, 8, 0); /* select matrix */
182 
183  /* Cb component */
184  put_bits(pb, 8, 2); /* component number */
185  put_bits(pb, 4, hsample[1]); /* H factor */
186  put_bits(pb, 4, vsample[1]); /* V factor */
187  put_bits(pb, 8, 0); /* select matrix */
188 
189  /* Cr component */
190  put_bits(pb, 8, 3); /* component number */
191  put_bits(pb, 4, hsample[2]); /* H factor */
192  put_bits(pb, 4, vsample[2]); /* V factor */
193  put_bits(pb, 8, 0); /* select matrix */
194 
195  /* scan header */
196  put_marker(pb, SOS);
197  put_bits(pb, 16, 12); /* length */
198  put_bits(pb, 8, 3); /* 3 components */
199 
200  /* Y component */
201  put_bits(pb, 8, 1); /* index */
202  put_bits(pb, 4, 0); /* DC huffman table index */
203  put_bits(pb, 4, 0); /* AC huffman table index */
204 
205  /* Cb component */
206  put_bits(pb, 8, 2); /* index */
207  put_bits(pb, 4, 1); /* DC huffman table index */
208  put_bits(pb, 4, lossless ? 0 : 1); /* AC huffman table index */
209 
210  /* Cr component */
211  put_bits(pb, 8, 3); /* index */
212  put_bits(pb, 4, 1); /* DC huffman table index */
213  put_bits(pb, 4, lossless ? 0 : 1); /* AC huffman table index */
214 
215  put_bits(pb, 8, lossless ? avctx->prediction_method + 1 : 0); /* Ss (not used) */
216 
217  switch (avctx->codec_id) {
218  case AV_CODEC_ID_MJPEG: put_bits(pb, 8, 63); break; /* Se (not used) */
219  case AV_CODEC_ID_LJPEG: put_bits(pb, 8, 0); break; /* not used */
220  default: assert(0);
221  }
222 
223  put_bits(pb, 8, 0); /* Ah/Al (not used) */
224 }
225 
226 static void escape_FF(PutBitContext *pb, int start)
227 {
228  int size = put_bits_count(pb) - start * 8;
229  int i, ff_count;
230  uint8_t *buf = pb->buf + start;
231  int align= (-(size_t)(buf))&3;
232 
233  assert((size&7) == 0);
234  size >>= 3;
235 
236  ff_count=0;
237  for(i=0; i<size && i<align; i++){
238  if(buf[i]==0xFF) ff_count++;
239  }
240  for(; i<size-15; i+=16){
241  int acc, v;
242 
243  v= *(uint32_t*)(&buf[i]);
244  acc= (((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
245  v= *(uint32_t*)(&buf[i+4]);
246  acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
247  v= *(uint32_t*)(&buf[i+8]);
248  acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
249  v= *(uint32_t*)(&buf[i+12]);
250  acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
251 
252  acc>>=4;
253  acc+= (acc>>16);
254  acc+= (acc>>8);
255  ff_count+= acc&0xFF;
256  }
257  for(; i<size; i++){
258  if(buf[i]==0xFF) ff_count++;
259  }
260 
261  if(ff_count==0) return;
262 
263  flush_put_bits(pb);
264  skip_put_bytes(pb, ff_count);
265 
266  for(i=size-1; ff_count; i--){
267  int v= buf[i];
268 
269  if(v==0xFF){
270  buf[i+ff_count]= 0;
271  ff_count--;
272  }
273 
274  buf[i+ff_count]= v;
275  }
276 }
277 
279 {
280  int length;
281  length= (-put_bits_count(pbc))&7;
282  if(length) put_bits(pbc, length, (1<<length)-1);
283 }
284 
286 {
288  flush_put_bits(pb);
289 
290  assert((header_bits & 7) == 0);
291 
292  escape_FF(pb, header_bits >> 3);
293 
294  put_marker(pb, EOI);
295 }
296 
298  uint8_t *huff_size, uint16_t *huff_code)
299 {
300  int mant, nbits;
301 
302  if (val == 0) {
303  put_bits(pb, huff_size[0], huff_code[0]);
304  } else {
305  mant = val;
306  if (val < 0) {
307  val = -val;
308  mant--;
309  }
310 
311  nbits= av_log2_16bit(val) + 1;
312 
313  put_bits(pb, huff_size[nbits], huff_code[nbits]);
314 
315  put_sbits(pb, nbits, mant);
316  }
317 }
Definition: mjpeg.h:116
const struct AVCodec * codec
Definition: avcodec.h:1059
int size
static void put_sbits(PutBitContext *pb, int n, int32_t value)
Definition: put_bits.h:172
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:70
const uint8_t avpriv_mjpeg_bits_ac_luminance[17]
Definition: mjpeg.c:73
int acc
Definition: yuv2rgb.c:471
Scantable.
Definition: idctdsp.h:29
int num
numerator
Definition: rational.h:44
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel...
Definition: avcodec.h:1429
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1254
Definition: mjpeg.h:75
const uint8_t avpriv_mjpeg_bits_dc_chrominance[17]
Definition: mjpeg.c:70
const uint8_t avpriv_mjpeg_bits_ac_chrominance[17]
Definition: mjpeg.c:99
uint8_t permutated[64]
Definition: idctdsp.h:31
MJPEG encoder and decoder.
uint8_t
#define CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
Definition: avcodec.h:658
Definition: mjpeg.h:78
Definition: mjpeg.h:84
enum AVCodecID id
Definition: avcodec.h:2810
static uint8_t * put_bits_ptr(PutBitContext *s)
Return the pointer to the byte where the bitstream writer will put the next bit.
Definition: put_bits.h:199
static void jpeg_table_header(PutBitContext *p, ScanTable *intra_scantable, uint16_t intra_matrix[64])
Definition: mjpeg.h:44
Definition: mjpeg.h:61
void ff_mjpeg_encode_picture_header(AVCodecContext *avctx, PutBitContext *pb, ScanTable *intra_scantable, uint16_t intra_matrix[64])
int flags
CODEC_FLAG_*.
Definition: avcodec.h:1144
uint8_t * buf
Definition: put_bits.h:38
void ff_mjpeg_encode_picture_trailer(PutBitContext *pb, int header_bits)
static void put_bits(PutBitContext *s, int n, unsigned int value)
Write up to 31 bits into a bitstream.
Definition: put_bits.h:134
static int put_bits_count(PutBitContext *s)
Definition: put_bits.h:67
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:69
static void skip_put_bytes(PutBitContext *s, int n)
Skip the given number of bytes.
Definition: put_bits.h:208
Definition: mjpeg.h:77
int width
picture width / height.
Definition: avcodec.h:1224
const uint8_t avpriv_mjpeg_bits_dc_luminance[17]
Definition: mjpeg.c:65
const uint8_t avpriv_mjpeg_val_dc[12]
Definition: mjpeg.c:67
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:68
Definition: mjpeg.h:47
Definition: mjpeg.h:76
void ff_mjpeg_encode_dc(PutBitContext *pb, int val, uint8_t *huff_size, uint16_t *huff_code)
Libavcodec external API header.
enum AVCodecID codec_id
Definition: avcodec.h:1067
static void escape_FF(PutBitContext *pb, int start)
main external API structure.
Definition: avcodec.h:1050
#define AV_WB16(p, d)
Definition: intreadwrite.h:213
const uint8_t avpriv_mjpeg_val_ac_luminance[]
Definition: mjpeg.c:75
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:65
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:83
common internal and external API header
int prediction_method
prediction method (needed for huffyuv)
Definition: avcodec.h:1410
FF_ENABLE_DEPRECATION_WARNINGS int av_pix_fmt_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift)
Utility function to access log2_chroma_w log2_chroma_h from the pixel format AVPixFmtDescriptor.
Definition: pixdesc.c:1625
int den
denominator
Definition: rational.h:45
static void put_marker(PutBitContext *p, int code)
Definition: mjpeg.h:123
const uint8_t avpriv_mjpeg_val_ac_chrominance[]
Definition: mjpeg.c:102
pixel format definitions
#define av_log2_16bit
Definition: intmath.h:86
static int put_huffman_table(PutBitContext *p, int table_class, int table_id, const uint8_t *bits_table, const uint8_t *value_table)
#define LIBAVCODEC_IDENT
Definition: version.h:43
void avpriv_put_string(PutBitContext *pb, const char *string, int terminate_string)
Put the string string in the bitstream.
Definition: bitstream.c:50
static void jpeg_put_comments(AVCodecContext *avctx, PutBitContext *p)
void ff_mjpeg_encode_stuffing(PutBitContext *pbc)
bitstream writer API