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 "jpegtables.h"
31 #include "put_bits.h"
32 #include "mjpegenc.h"
33 #include "mjpegenc_common.h"
34 #include "mjpeg.h"
35 
36 /* table_class: 0 = DC coef, 1 = AC coefs */
37 static int put_huffman_table(PutBitContext *p, int table_class, int table_id,
38  const uint8_t *bits_table, const uint8_t *value_table)
39 {
40  int n, i;
41 
42  put_bits(p, 4, table_class);
43  put_bits(p, 4, table_id);
44 
45  n = 0;
46  for(i=1;i<=16;i++) {
47  n += bits_table[i];
48  put_bits(p, 8, bits_table[i]);
49  }
50 
51  for(i=0;i<n;i++)
52  put_bits(p, 8, value_table[i]);
53 
54  return n + 17;
55 }
56 
57 static void jpeg_table_header(PutBitContext *p, ScanTable *intra_scantable,
58  uint16_t intra_matrix[64])
59 {
60  int i, j, size;
61  uint8_t *ptr;
62 
63  /* quant matrixes */
64  put_marker(p, DQT);
65  put_bits(p, 16, 2 + 1 * (1 + 64));
66  put_bits(p, 4, 0); /* 8 bit precision */
67  put_bits(p, 4, 0); /* table 0 */
68  for(i=0;i<64;i++) {
69  j = intra_scantable->permutated[i];
70  put_bits(p, 8, intra_matrix[j]);
71  }
72 
73  /* huffman table */
74  put_marker(p, DHT);
75  flush_put_bits(p);
76  ptr = put_bits_ptr(p);
77  put_bits(p, 16, 0); /* patched later */
78  size = 2;
83 
88  AV_WB16(ptr, size);
89 }
90 
92 {
93  int size;
94  uint8_t *ptr;
95 
96  if (avctx->sample_aspect_ratio.num > 0 && avctx->sample_aspect_ratio.den > 0) {
97  /* JFIF header */
98  put_marker(p, APP0);
99  put_bits(p, 16, 16);
100  avpriv_put_string(p, "JFIF", 1); /* this puts the trailing zero-byte too */
101  /* The most significant byte is used for major revisions, the least
102  * significant byte for minor revisions. Version 1.02 is the current
103  * released revision. */
104  put_bits(p, 16, 0x0102);
105  put_bits(p, 8, 0); /* units type: 0 - aspect ratio */
106  put_bits(p, 16, avctx->sample_aspect_ratio.num);
107  put_bits(p, 16, avctx->sample_aspect_ratio.den);
108  put_bits(p, 8, 0); /* thumbnail width */
109  put_bits(p, 8, 0); /* thumbnail height */
110  }
111 
112  /* comment */
113  if (!(avctx->flags & AV_CODEC_FLAG_BITEXACT)) {
114  put_marker(p, COM);
115  flush_put_bits(p);
116  ptr = put_bits_ptr(p);
117  put_bits(p, 16, 0); /* patched later */
119  size = strlen(LIBAVCODEC_IDENT)+3;
120  AV_WB16(ptr, size);
121  }
122 
123  if (avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
124  avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
125  avctx->pix_fmt == AV_PIX_FMT_YUV444P) {
126  put_marker(p, COM);
127  flush_put_bits(p);
128  ptr = put_bits_ptr(p);
129  put_bits(p, 16, 0); /* patched later */
130  avpriv_put_string(p, "CS=ITU601", 1);
131  size = strlen("CS=ITU601")+3;
132  AV_WB16(ptr, size);
133  }
134 }
135 
137  ScanTable *intra_scantable, int pred,
138  uint16_t intra_matrix[64])
139 {
140  int chroma_h_shift, chroma_v_shift;
141  const int lossless = avctx->codec_id != AV_CODEC_ID_MJPEG;
142  int hsample[3], vsample[3];
143 
144  av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift,
145  &chroma_v_shift);
146 
147  if (avctx->codec->id == AV_CODEC_ID_LJPEG &&
148  avctx->pix_fmt == AV_PIX_FMT_BGR24) {
149  vsample[0] = hsample[0] =
150  vsample[1] = hsample[1] =
151  vsample[2] = hsample[2] = 1;
152  } else {
153  vsample[0] = 2;
154  vsample[1] = 2 >> chroma_v_shift;
155  vsample[2] = 2 >> chroma_v_shift;
156  hsample[0] = 2;
157  hsample[1] = 2 >> chroma_h_shift;
158  hsample[2] = 2 >> chroma_h_shift;
159  }
160 
161  put_marker(pb, SOI);
162 
163  jpeg_put_comments(avctx, pb);
164 
165  jpeg_table_header(pb, intra_scantable, intra_matrix);
166 
167  switch (avctx->codec_id) {
168  case AV_CODEC_ID_MJPEG: put_marker(pb, SOF0 ); break;
169  case AV_CODEC_ID_LJPEG: put_marker(pb, SOF3 ); break;
170  default: assert(0);
171  }
172 
173  put_bits(pb, 16, 17);
174  if (lossless && avctx->pix_fmt == AV_PIX_FMT_BGR24)
175  put_bits(pb, 8, 9); /* 9 bits/component RCT */
176  else
177  put_bits(pb, 8, 8); /* 8 bits/component */
178  put_bits(pb, 16, avctx->height);
179  put_bits(pb, 16, avctx->width);
180  put_bits(pb, 8, 3); /* 3 components */
181 
182  /* Y component */
183  put_bits(pb, 8, 1); /* component number */
184  put_bits(pb, 4, hsample[0]); /* H factor */
185  put_bits(pb, 4, vsample[0]); /* V factor */
186  put_bits(pb, 8, 0); /* select matrix */
187 
188  /* Cb component */
189  put_bits(pb, 8, 2); /* component number */
190  put_bits(pb, 4, hsample[1]); /* H factor */
191  put_bits(pb, 4, vsample[1]); /* V factor */
192  put_bits(pb, 8, 0); /* select matrix */
193 
194  /* Cr component */
195  put_bits(pb, 8, 3); /* component number */
196  put_bits(pb, 4, hsample[2]); /* H factor */
197  put_bits(pb, 4, vsample[2]); /* V factor */
198  put_bits(pb, 8, 0); /* select matrix */
199 
200  /* scan header */
201  put_marker(pb, SOS);
202  put_bits(pb, 16, 12); /* length */
203  put_bits(pb, 8, 3); /* 3 components */
204 
205  /* Y component */
206  put_bits(pb, 8, 1); /* index */
207  put_bits(pb, 4, 0); /* DC huffman table index */
208  put_bits(pb, 4, 0); /* AC huffman table index */
209 
210  /* Cb component */
211  put_bits(pb, 8, 2); /* 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  /* Cr component */
216  put_bits(pb, 8, 3); /* index */
217  put_bits(pb, 4, 1); /* DC huffman table index */
218  put_bits(pb, 4, lossless ? 0 : 1); /* AC huffman table index */
219 
220  put_bits(pb, 8, lossless ? pred : 0); /* Ss (not used) */
221 
222  switch (avctx->codec_id) {
223  case AV_CODEC_ID_MJPEG: put_bits(pb, 8, 63); break; /* Se (not used) */
224  case AV_CODEC_ID_LJPEG: put_bits(pb, 8, 0); break; /* not used */
225  default: assert(0);
226  }
227 
228  put_bits(pb, 8, 0); /* Ah/Al (not used) */
229 }
230 
231 static void escape_FF(PutBitContext *pb, int start)
232 {
233  int size = put_bits_count(pb) - start * 8;
234  int i, ff_count;
235  uint8_t *buf = pb->buf + start;
236  int align= (-(size_t)(buf))&3;
237 
238  assert((size&7) == 0);
239  size >>= 3;
240 
241  ff_count=0;
242  for(i=0; i<size && i<align; i++){
243  if(buf[i]==0xFF) ff_count++;
244  }
245  for(; i<size-15; i+=16){
246  int acc, v;
247 
248  v= *(uint32_t*)(&buf[i]);
249  acc= (((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
250  v= *(uint32_t*)(&buf[i+4]);
251  acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
252  v= *(uint32_t*)(&buf[i+8]);
253  acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
254  v= *(uint32_t*)(&buf[i+12]);
255  acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
256 
257  acc>>=4;
258  acc+= (acc>>16);
259  acc+= (acc>>8);
260  ff_count+= acc&0xFF;
261  }
262  for(; i<size; i++){
263  if(buf[i]==0xFF) ff_count++;
264  }
265 
266  if(ff_count==0) return;
267 
268  flush_put_bits(pb);
269  skip_put_bytes(pb, ff_count);
270 
271  for(i=size-1; ff_count; i--){
272  int v= buf[i];
273 
274  if(v==0xFF){
275  buf[i+ff_count]= 0;
276  ff_count--;
277  }
278 
279  buf[i+ff_count]= v;
280  }
281 }
282 
284 {
285  int length;
286  length= (-put_bits_count(pbc))&7;
287  if(length) put_bits(pbc, length, (1<<length)-1);
288 }
289 
291 {
293  flush_put_bits(pb);
294 
295  assert((header_bits & 7) == 0);
296 
297  escape_FF(pb, header_bits >> 3);
298 
299  put_marker(pb, EOI);
300 }
301 
303  uint8_t *huff_size, uint16_t *huff_code)
304 {
305  int mant, nbits;
306 
307  if (val == 0) {
308  put_bits(pb, huff_size[0], huff_code[0]);
309  } else {
310  mant = val;
311  if (val < 0) {
312  val = -val;
313  mant--;
314  }
315 
316  nbits= av_log2_16bit(val) + 1;
317 
318  put_bits(pb, huff_size[nbits], huff_code[nbits]);
319 
320  put_sbits(pb, nbits, mant);
321  }
322 }
const struct AVCodec * codec
Definition: avcodec.h:1418
int size
Definition: mjpeg.h:71
Definition: mjpeg.h:111
Definition: mjpeg.h:73
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:64
Definition: mjpeg.h:42
int acc
Definition: yuv2rgb.c:482
MJPEG encoder.
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:1804
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1621
void ff_mjpeg_encode_picture_header(AVCodecContext *avctx, PutBitContext *pb, ScanTable *intra_scantable, int pred, uint16_t intra_matrix[64])
uint8_t permutated[64]
Definition: idctdsp.h:31
MJPEG encoder and decoder.
Definition: mjpeg.h:72
uint8_t
const uint8_t avpriv_mjpeg_bits_dc_luminance[17]
Definition: jpegtables.c:65
enum AVCodecID id
Definition: avcodec.h:3134
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])
const uint8_t avpriv_mjpeg_bits_dc_chrominance[17]
Definition: jpegtables.c:70
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:1793
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:1503
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
Definition: mjpeg.h:39
Definition: mjpeg.h:70
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:63
Definition: mjpeg.h:79
static void put_marker(PutBitContext *p, enum JpegMarker code)
Definition: mjpegenc.h:54
Definition: mjpeg.h:56
static void skip_put_bytes(PutBitContext *s, int n)
Skip the given number of bytes.
Definition: put_bits.h:208
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
Definition: avcodec.h:788
int width
picture width / height.
Definition: avcodec.h:1580
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:62
static const float pred[4]
Definition: siprdata.h:259
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:1426
static void escape_FF(PutBitContext *pb, int start)
main external API structure.
Definition: avcodec.h:1409
const uint8_t avpriv_mjpeg_val_dc[12]
Definition: jpegtables.c:67
const uint8_t avpriv_mjpeg_bits_ac_chrominance[17]
Definition: jpegtables.c:99
const uint8_t avpriv_mjpeg_val_ac_chrominance[]
Definition: jpegtables.c:102
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:59
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 den
denominator
Definition: rational.h:45
const uint8_t avpriv_mjpeg_bits_ac_luminance[17]
Definition: jpegtables.c:73
pixel format definitions
#define av_log2_16bit
Definition: intmath.h:86
const uint8_t avpriv_mjpeg_val_ac_luminance[]
Definition: jpegtables.c:75
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:42
void avpriv_put_string(PutBitContext *pb, const char *string, int terminate_string)
Put the string string in the bitstream.
Definition: bitstream.c:51
static void jpeg_put_comments(AVCodecContext *avctx, PutBitContext *p)
#define AV_WB16(p, val)
Definition: intreadwrite.h:218
void ff_mjpeg_encode_stuffing(PutBitContext *pbc)
bitstream writer API