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  /* The most significant byte is used for major revisions, the least
100  * significant byte for minor revisions. Version 1.02 is the current
101  * released revision. */
102  put_bits(p, 16, 0x0102);
103  put_bits(p, 8, 0); /* units type: 0 - aspect ratio */
104  put_bits(p, 16, avctx->sample_aspect_ratio.num);
105  put_bits(p, 16, avctx->sample_aspect_ratio.den);
106  put_bits(p, 8, 0); /* thumbnail width */
107  put_bits(p, 8, 0); /* thumbnail height */
108  }
109 
110  /* comment */
111  if (!(avctx->flags & CODEC_FLAG_BITEXACT)) {
112  put_marker(p, COM);
113  flush_put_bits(p);
114  ptr = put_bits_ptr(p);
115  put_bits(p, 16, 0); /* patched later */
117  size = strlen(LIBAVCODEC_IDENT)+3;
118  AV_WB16(ptr, size);
119  }
120 
121  if (avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
122  avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
123  avctx->pix_fmt == AV_PIX_FMT_YUV444P) {
124  put_marker(p, COM);
125  flush_put_bits(p);
126  ptr = put_bits_ptr(p);
127  put_bits(p, 16, 0); /* patched later */
128  avpriv_put_string(p, "CS=ITU601", 1);
129  size = strlen("CS=ITU601")+3;
130  AV_WB16(ptr, size);
131  }
132 }
133 
135  ScanTable *intra_scantable,
136  uint16_t intra_matrix[64])
137 {
138  int chroma_h_shift, chroma_v_shift;
139  const int lossless = avctx->codec_id != AV_CODEC_ID_MJPEG;
140  int hsample[3], vsample[3];
141 
142  av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift,
143  &chroma_v_shift);
144 
145  if (avctx->codec->id == AV_CODEC_ID_LJPEG &&
146  avctx->pix_fmt == AV_PIX_FMT_BGR24) {
147  vsample[0] = hsample[0] =
148  vsample[1] = hsample[1] =
149  vsample[2] = hsample[2] = 1;
150  } else {
151  vsample[0] = 2;
152  vsample[1] = 2 >> chroma_v_shift;
153  vsample[2] = 2 >> chroma_v_shift;
154  hsample[0] = 2;
155  hsample[1] = 2 >> chroma_h_shift;
156  hsample[2] = 2 >> chroma_h_shift;
157  }
158 
159  put_marker(pb, SOI);
160 
161  jpeg_put_comments(avctx, pb);
162 
163  jpeg_table_header(pb, intra_scantable, intra_matrix);
164 
165  switch (avctx->codec_id) {
166  case AV_CODEC_ID_MJPEG: put_marker(pb, SOF0 ); break;
167  case AV_CODEC_ID_LJPEG: put_marker(pb, SOF3 ); break;
168  default: assert(0);
169  }
170 
171  put_bits(pb, 16, 17);
172  if (lossless && avctx->pix_fmt == AV_PIX_FMT_BGR24)
173  put_bits(pb, 8, 9); /* 9 bits/component RCT */
174  else
175  put_bits(pb, 8, 8); /* 8 bits/component */
176  put_bits(pb, 16, avctx->height);
177  put_bits(pb, 16, avctx->width);
178  put_bits(pb, 8, 3); /* 3 components */
179 
180  /* Y component */
181  put_bits(pb, 8, 1); /* component number */
182  put_bits(pb, 4, hsample[0]); /* H factor */
183  put_bits(pb, 4, vsample[0]); /* V factor */
184  put_bits(pb, 8, 0); /* select matrix */
185 
186  /* Cb component */
187  put_bits(pb, 8, 2); /* component number */
188  put_bits(pb, 4, hsample[1]); /* H factor */
189  put_bits(pb, 4, vsample[1]); /* V factor */
190  put_bits(pb, 8, 0); /* select matrix */
191 
192  /* Cr component */
193  put_bits(pb, 8, 3); /* component number */
194  put_bits(pb, 4, hsample[2]); /* H factor */
195  put_bits(pb, 4, vsample[2]); /* V factor */
196  put_bits(pb, 8, 0); /* select matrix */
197 
198  /* scan header */
199  put_marker(pb, SOS);
200  put_bits(pb, 16, 12); /* length */
201  put_bits(pb, 8, 3); /* 3 components */
202 
203  /* Y component */
204  put_bits(pb, 8, 1); /* index */
205  put_bits(pb, 4, 0); /* DC huffman table index */
206  put_bits(pb, 4, 0); /* AC huffman table index */
207 
208  /* Cb component */
209  put_bits(pb, 8, 2); /* index */
210  put_bits(pb, 4, 1); /* DC huffman table index */
211  put_bits(pb, 4, lossless ? 0 : 1); /* AC huffman table index */
212 
213  /* Cr component */
214  put_bits(pb, 8, 3); /* index */
215  put_bits(pb, 4, 1); /* DC huffman table index */
216  put_bits(pb, 4, lossless ? 0 : 1); /* AC huffman table index */
217 
218  put_bits(pb, 8, lossless ? avctx->prediction_method + 1 : 0); /* Ss (not used) */
219 
220  switch (avctx->codec_id) {
221  case AV_CODEC_ID_MJPEG: put_bits(pb, 8, 63); break; /* Se (not used) */
222  case AV_CODEC_ID_LJPEG: put_bits(pb, 8, 0); break; /* not used */
223  default: assert(0);
224  }
225 
226  put_bits(pb, 8, 0); /* Ah/Al (not used) */
227 }
228 
229 static void escape_FF(PutBitContext *pb, int start)
230 {
231  int size = put_bits_count(pb) - start * 8;
232  int i, ff_count;
233  uint8_t *buf = pb->buf + start;
234  int align= (-(size_t)(buf))&3;
235 
236  assert((size&7) == 0);
237  size >>= 3;
238 
239  ff_count=0;
240  for(i=0; i<size && i<align; i++){
241  if(buf[i]==0xFF) ff_count++;
242  }
243  for(; i<size-15; i+=16){
244  int acc, v;
245 
246  v= *(uint32_t*)(&buf[i]);
247  acc= (((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
248  v= *(uint32_t*)(&buf[i+4]);
249  acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
250  v= *(uint32_t*)(&buf[i+8]);
251  acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
252  v= *(uint32_t*)(&buf[i+12]);
253  acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
254 
255  acc>>=4;
256  acc+= (acc>>16);
257  acc+= (acc>>8);
258  ff_count+= acc&0xFF;
259  }
260  for(; i<size; i++){
261  if(buf[i]==0xFF) ff_count++;
262  }
263 
264  if(ff_count==0) return;
265 
266  flush_put_bits(pb);
267  skip_put_bytes(pb, ff_count);
268 
269  for(i=size-1; ff_count; i--){
270  int v= buf[i];
271 
272  if(v==0xFF){
273  buf[i+ff_count]= 0;
274  ff_count--;
275  }
276 
277  buf[i+ff_count]= v;
278  }
279 }
280 
282 {
283  int length;
284  length= (-put_bits_count(pbc))&7;
285  if(length) put_bits(pbc, length, (1<<length)-1);
286 }
287 
289 {
291  flush_put_bits(pb);
292 
293  assert((header_bits & 7) == 0);
294 
295  escape_FF(pb, header_bits >> 3);
296 
297  put_marker(pb, EOI);
298 }
299 
301  uint8_t *huff_size, uint16_t *huff_code)
302 {
303  int mant, nbits;
304 
305  if (val == 0) {
306  put_bits(pb, huff_size[0], huff_code[0]);
307  } else {
308  mant = val;
309  if (val < 0) {
310  val = -val;
311  mant--;
312  }
313 
314  nbits= av_log2_16bit(val) + 1;
315 
316  put_bits(pb, huff_size[nbits], huff_code[nbits]);
317 
318  put_sbits(pb, nbits, mant);
319  }
320 }
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:1445
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1270
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:2826
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:1229
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:1426
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