FFmpeg  2.6.9
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
mjpegenc_common.c
Go to the documentation of this file.
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/common.h"
27 #include "libavutil/pixdesc.h"
28 #include "libavutil/pixfmt.h"
29 
30 #include "avcodec.h"
31 #include "idctdsp.h"
32 #include "put_bits.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 
58  ScanTable *intra_scantable,
59  uint16_t luma_intra_matrix[64],
60  uint16_t chroma_intra_matrix[64],
61  int hsample[3])
62 {
63  int i, j, size;
64  uint8_t *ptr;
65 
66  if (avctx->codec_id != AV_CODEC_ID_LJPEG) {
67  int matrix_count = 1 + !!memcmp(luma_intra_matrix,
68  chroma_intra_matrix,
69  sizeof(luma_intra_matrix[0]) * 64);
70  /* quant matrixes */
71  put_marker(p, DQT);
72  put_bits(p, 16, 2 + matrix_count * (1 + 64));
73  put_bits(p, 4, 0); /* 8 bit precision */
74  put_bits(p, 4, 0); /* table 0 */
75  for(i=0;i<64;i++) {
76  j = intra_scantable->permutated[i];
77  put_bits(p, 8, luma_intra_matrix[j]);
78  }
79 
80  if (matrix_count > 1) {
81  put_bits(p, 4, 0); /* 8 bit precision */
82  put_bits(p, 4, 1); /* table 1 */
83  for(i=0;i<64;i++) {
84  j = intra_scantable->permutated[i];
85  put_bits(p, 8, chroma_intra_matrix[j]);
86  }
87  }
88  }
89 
91  put_marker(p, DRI);
92  put_bits(p, 16, 4);
93  put_bits(p, 16, (avctx->width-1)/(8*hsample[0]) + 1);
94  }
95 
96  /* huffman table */
97  put_marker(p, DHT);
98  flush_put_bits(p);
99  ptr = put_bits_ptr(p);
100  put_bits(p, 16, 0); /* patched later */
101  size = 2;
106 
111  AV_WB16(ptr, size);
112 }
113 
115 {
116  int size;
117  uint8_t *ptr;
118 
119  if (avctx->sample_aspect_ratio.num > 0 && avctx->sample_aspect_ratio.den > 0) {
120  AVRational sar = avctx->sample_aspect_ratio;
121 
122  if (sar.num > 65535 || sar.den > 65535) {
123  if (!av_reduce(&sar.num, &sar.den, avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den, 65535))
124  av_log(avctx, AV_LOG_WARNING,
125  "Cannot store exact aspect ratio %d:%d\n",
126  avctx->sample_aspect_ratio.num,
127  avctx->sample_aspect_ratio.den);
128  }
129 
130  /* JFIF header */
131  put_marker(p, APP0);
132  put_bits(p, 16, 16);
133  avpriv_put_string(p, "JFIF", 1); /* this puts the trailing zero-byte too */
134  put_bits(p, 16, 0x0102); /* v 1.02 */
135  put_bits(p, 8, 0); /* units type: 0 - aspect ratio */
136  put_bits(p, 16, sar.num);
137  put_bits(p, 16, sar.den);
138  put_bits(p, 8, 0); /* thumbnail width */
139  put_bits(p, 8, 0); /* thumbnail height */
140  }
141 
142  /* comment */
143  if (!(avctx->flags & CODEC_FLAG_BITEXACT)) {
144  put_marker(p, COM);
145  flush_put_bits(p);
146  ptr = put_bits_ptr(p);
147  put_bits(p, 16, 0); /* patched later */
149  size = strlen(LIBAVCODEC_IDENT)+3;
150  AV_WB16(ptr, size);
151  }
152 
153  if (avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
154  avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
155  avctx->pix_fmt == AV_PIX_FMT_YUV444P) {
156  put_marker(p, COM);
157  flush_put_bits(p);
158  ptr = put_bits_ptr(p);
159  put_bits(p, 16, 0); /* patched later */
160  avpriv_put_string(p, "CS=ITU601", 1);
161  size = strlen("CS=ITU601")+3;
162  AV_WB16(ptr, size);
163  }
164 }
165 
166 void ff_mjpeg_init_hvsample(AVCodecContext *avctx, int hsample[3], int vsample[3])
167 {
168  int chroma_h_shift, chroma_v_shift;
169 
170  av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift,
171  &chroma_v_shift);
172  if (avctx->codec->id == AV_CODEC_ID_LJPEG &&
173  ( avctx->pix_fmt == AV_PIX_FMT_BGR0
174  || avctx->pix_fmt == AV_PIX_FMT_BGRA
175  || avctx->pix_fmt == AV_PIX_FMT_BGR24)) {
176  vsample[0] = hsample[0] =
177  vsample[1] = hsample[1] =
178  vsample[2] = hsample[2] = 1;
179  } else if (avctx->pix_fmt == AV_PIX_FMT_YUV444P || avctx->pix_fmt == AV_PIX_FMT_YUVJ444P) {
180  vsample[0] = vsample[1] = vsample[2] = 2;
181  hsample[0] = hsample[1] = hsample[2] = 1;
182  } else {
183  vsample[0] = 2;
184  vsample[1] = 2 >> chroma_v_shift;
185  vsample[2] = 2 >> chroma_v_shift;
186  hsample[0] = 2;
187  hsample[1] = 2 >> chroma_h_shift;
188  hsample[2] = 2 >> chroma_h_shift;
189  }
190 }
191 
193  ScanTable *intra_scantable,
194  uint16_t luma_intra_matrix[64],
195  uint16_t chroma_intra_matrix[64])
196 {
197  const int lossless = avctx->codec_id != AV_CODEC_ID_MJPEG && avctx->codec_id != AV_CODEC_ID_AMV;
198  int hsample[3], vsample[3];
199  int i;
200  int chroma_matrix = !!memcmp(luma_intra_matrix,
201  chroma_intra_matrix,
202  sizeof(luma_intra_matrix[0])*64);
203 
204  ff_mjpeg_init_hvsample(avctx, hsample, vsample);
205 
206  put_marker(pb, SOI);
207 
208  // hack for AMV mjpeg format
209  if(avctx->codec_id == AV_CODEC_ID_AMV) goto end;
210 
211  jpeg_put_comments(avctx, pb);
212 
213  jpeg_table_header(avctx, pb, intra_scantable, luma_intra_matrix, chroma_intra_matrix, hsample);
214 
215  switch (avctx->codec_id) {
216  case AV_CODEC_ID_MJPEG: put_marker(pb, SOF0 ); break;
217  case AV_CODEC_ID_LJPEG: put_marker(pb, SOF3 ); break;
218  default: av_assert0(0);
219  }
220 
221  put_bits(pb, 16, 17);
222  if (lossless && ( avctx->pix_fmt == AV_PIX_FMT_BGR0
223  || avctx->pix_fmt == AV_PIX_FMT_BGRA
224  || avctx->pix_fmt == AV_PIX_FMT_BGR24))
225  put_bits(pb, 8, 9); /* 9 bits/component RCT */
226  else
227  put_bits(pb, 8, 8); /* 8 bits/component */
228  put_bits(pb, 16, avctx->height);
229  put_bits(pb, 16, avctx->width);
230  put_bits(pb, 8, 3); /* 3 components */
231 
232  /* Y component */
233  put_bits(pb, 8, 1); /* component number */
234  put_bits(pb, 4, hsample[0]); /* H factor */
235  put_bits(pb, 4, vsample[0]); /* V factor */
236  put_bits(pb, 8, 0); /* select matrix */
237 
238  /* Cb component */
239  put_bits(pb, 8, 2); /* component number */
240  put_bits(pb, 4, hsample[1]); /* H factor */
241  put_bits(pb, 4, vsample[1]); /* V factor */
242  put_bits(pb, 8, lossless ? 0 : chroma_matrix); /* select matrix */
243 
244  /* Cr component */
245  put_bits(pb, 8, 3); /* component number */
246  put_bits(pb, 4, hsample[2]); /* H factor */
247  put_bits(pb, 4, vsample[2]); /* V factor */
248  put_bits(pb, 8, lossless ? 0 : chroma_matrix); /* select matrix */
249 
250  /* scan header */
251  put_marker(pb, SOS);
252  put_bits(pb, 16, 12); /* length */
253  put_bits(pb, 8, 3); /* 3 components */
254 
255  /* Y component */
256  put_bits(pb, 8, 1); /* index */
257  put_bits(pb, 4, 0); /* DC huffman table index */
258  put_bits(pb, 4, 0); /* AC huffman table index */
259 
260  /* Cb component */
261  put_bits(pb, 8, 2); /* index */
262  put_bits(pb, 4, 1); /* DC huffman table index */
263  put_bits(pb, 4, lossless ? 0 : 1); /* AC huffman table index */
264 
265  /* Cr component */
266  put_bits(pb, 8, 3); /* index */
267  put_bits(pb, 4, 1); /* DC huffman table index */
268  put_bits(pb, 4, lossless ? 0 : 1); /* AC huffman table index */
269 
270  put_bits(pb, 8, lossless ? avctx->prediction_method + 1 : 0); /* Ss (not used) */
271 
272  switch (avctx->codec_id) {
273  case AV_CODEC_ID_MJPEG: put_bits(pb, 8, 63); break; /* Se (not used) */
274  case AV_CODEC_ID_LJPEG: put_bits(pb, 8, 0); break; /* not used */
275  default: av_assert0(0);
276  }
277 
278  put_bits(pb, 8, 0); /* Ah/Al (not used) */
279 
280 end:
281  if (!lossless) {
282  MpegEncContext *s = avctx->priv_data;
283  av_assert0(avctx->codec->priv_data_size == sizeof(MpegEncContext));
284 
285  s->esc_pos = put_bits_count(pb) >> 3;
286  for(i=1; i<s->slice_context_count; i++)
287  s->thread_context[i]->esc_pos = 0;
288  }
289 }
290 
292 {
293  int size;
294  int i, ff_count;
295  uint8_t *buf = pb->buf + start;
296  int align= (-(size_t)(buf))&3;
297  int pad = (-put_bits_count(pb))&7;
298 
299  if (pad)
300  put_bits(pb, pad, (1<<pad)-1);
301 
302  flush_put_bits(pb);
303  size = put_bits_count(pb) - start * 8;
304 
305  av_assert1((size&7) == 0);
306  size >>= 3;
307 
308  ff_count=0;
309  for(i=0; i<size && i<align; i++){
310  if(buf[i]==0xFF) ff_count++;
311  }
312  for(; i<size-15; i+=16){
313  int acc, v;
314 
315  v= *(uint32_t*)(&buf[i]);
316  acc= (((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
317  v= *(uint32_t*)(&buf[i+4]);
318  acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
319  v= *(uint32_t*)(&buf[i+8]);
320  acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
321  v= *(uint32_t*)(&buf[i+12]);
322  acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
323 
324  acc>>=4;
325  acc+= (acc>>16);
326  acc+= (acc>>8);
327  ff_count+= acc&0xFF;
328  }
329  for(; i<size; i++){
330  if(buf[i]==0xFF) ff_count++;
331  }
332 
333  if(ff_count==0) return;
334 
335  flush_put_bits(pb);
336  skip_put_bytes(pb, ff_count);
337 
338  for(i=size-1; ff_count; i--){
339  int v= buf[i];
340 
341  if(v==0xFF){
342  buf[i+ff_count]= 0;
343  ff_count--;
344  }
345 
346  buf[i+ff_count]= v;
347  }
348 }
349 
351 {
352  int i;
353  PutBitContext *pbc = &s->pb;
354  int mb_y = s->mb_y - !s->mb_x;
355 
356  int ret = ff_mpv_reallocate_putbitbuffer(s, put_bits_count(&s->pb) / 8 + 100,
357  put_bits_count(&s->pb) / 4 + 1000);
358  if (ret < 0) {
359  av_log(s->avctx, AV_LOG_ERROR, "Buffer reallocation failed\n");
360  goto fail;
361  }
362 
363  ff_mjpeg_escape_FF(pbc, s->esc_pos);
364 
365  if((s->avctx->active_thread_type & FF_THREAD_SLICE) && mb_y < s->mb_height)
366  put_marker(pbc, RST0 + (mb_y&7));
367  s->esc_pos = put_bits_count(pbc) >> 3;
368 fail:
369 
370  for(i=0; i<3; i++)
371  s->last_dc[i] = 128 << s->intra_dc_precision;
372 
373  return ret;
374 }
375 
377 {
378  av_assert1((header_bits & 7) == 0);
379 
380  put_marker(pb, EOI);
381 }
382 
384  uint8_t *huff_size, uint16_t *huff_code)
385 {
386  int mant, nbits;
387 
388  if (val == 0) {
389  put_bits(pb, huff_size[0], huff_code[0]);
390  } else {
391  mant = val;
392  if (val < 0) {
393  val = -val;
394  mant--;
395  }
396 
397  nbits= av_log2_16bit(val) + 1;
398 
399  put_bits(pb, huff_size[nbits], huff_code[nbits]);
400 
401  put_sbits(pb, nbits, mant);
402  }
403 }
Definition: mjpeg.h:116
const struct AVCodec * codec
Definition: avcodec.h:1248
const char const char void * val
Definition: avisynth_c.h:672
float v
const char * s
Definition: avisynth_c.h:669
static void put_sbits(PutBitContext *pb, int n, int32_t value)
Definition: put_bits.h:200
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:73
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:198
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:181
const uint8_t avpriv_mjpeg_bits_ac_luminance[17]
Definition: mjpeg.c:73
int acc
Definition: yuv2rgb.c:532
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:1621
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1442
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
void ff_mjpeg_encode_picture_header(AVCodecContext *avctx, PutBitContext *pb, ScanTable *intra_scantable, uint16_t luma_intra_matrix[64], uint16_t chroma_intra_matrix[64])
MJPEG encoder and decoder.
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
uint8_t
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:67
int intra_dc_precision
Definition: mpegvideo.h:592
#define CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
Definition: avcodec.h:757
ptrdiff_t size
Definition: opengl_enc.c:101
Definition: mjpeg.h:78
Definition: mjpeg.h:84
#define AV_WB16(p, v)
Definition: intreadwrite.h:405
#define av_log(a,...)
enum AVCodecID id
Definition: avcodec.h:3187
int slice_context_count
number of used thread_contexts
Definition: mpegvideo.h:291
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:175
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:227
int last_dc[3]
last DC values for MPEG1
Definition: mpegvideo.h:320
Definition: mjpeg.h:44
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:2057
int active_thread_type
Which multithreading methods are in use by the codec.
Definition: avcodec.h:2770
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:102
Definition: mjpeg.h:61
int flags
CODEC_FLAG_*.
Definition: avcodec.h:1333
uint8_t * buf
Definition: put_bits.h:38
void ff_mjpeg_encode_picture_trailer(PutBitContext *pb, int header_bits)
Libavcodec external API header.
static int put_bits_count(PutBitContext *s)
Definition: put_bits.h:85
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:72
int ff_mpv_reallocate_putbitbuffer(MpegEncContext *s, size_t threshold, size_t size_increase)
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition: rational.c:35
static void skip_put_bytes(PutBitContext *s, int n)
Skip the given number of bytes.
Definition: put_bits.h:236
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:53
Definition: mjpeg.h:77
ret
Definition: avfilter.c:974
int width
picture width / height.
Definition: avcodec.h:1412
int priv_data_size
Definition: avcodec.h:3211
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
void ff_mjpeg_escape_FF(PutBitContext *pb, int start)
int n
Definition: avisynth_c.h:589
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:71
Definition: mjpeg.h:47
Definition: mjpeg.h:76
const AVS_VideoInfo int align
Definition: avisynth_c.h:696
struct MpegEncContext * thread_context[MAX_THREADS]
Definition: mpegvideo.h:290
void ff_mjpeg_encode_dc(PutBitContext *pb, int val, uint8_t *huff_size, uint16_t *huff_code)
#define FF_THREAD_SLICE
Decode more than one part of a single frame at once.
Definition: avcodec.h:2763
enum AVCodecID codec_id
Definition: avcodec.h:1256
main external API structure.
Definition: avcodec.h:1239
void * buf
Definition: avisynth_c.h:595
static void jpeg_table_header(AVCodecContext *avctx, PutBitContext *p, ScanTable *intra_scantable, uint16_t luma_intra_matrix[64], uint16_t chroma_intra_matrix[64], int hsample[3])
void ff_mjpeg_init_hvsample(AVCodecContext *avctx, int hsample[3], int vsample[3])
rational number numerator/denominator
Definition: rational.h:43
packed BGR 8:8:8, 32bpp, BGRXBGRX... X=unused/undefined
Definition: pixfmt.h:265
int ff_mjpeg_encode_stuffing(MpegEncContext *s)
MpegEncContext.
Definition: mpegvideo.h:213
const uint8_t avpriv_mjpeg_val_ac_luminance[]
Definition: mjpeg.c:75
struct AVCodecContext * avctx
Definition: mpegvideo.h:230
PutBitContext pb
bit output
Definition: mpegvideo.h:286
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:68
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:101
common internal and external API header
Definition: mjpeg.h:66
int prediction_method
prediction method (needed for huffyuv)
Definition: avcodec.h:1602
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV444P and setting color_...
Definition: pixfmt.h:82
int den
denominator
Definition: rational.h:45
void * priv_data
Definition: avcodec.h:1281
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
Definition: mjpeg.h:80
#define av_log2_16bit
Definition: intmath.h:106
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:52
void INT64 start
Definition: avisynth_c.h:595
static void jpeg_put_comments(AVCodecContext *avctx, PutBitContext *p)
bitstream writer API