FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups 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  /* JFIF header */
121  put_marker(p, APP0);
122  put_bits(p, 16, 16);
123  avpriv_put_string(p, "JFIF", 1); /* this puts the trailing zero-byte too */
124  put_bits(p, 16, 0x0102); /* v 1.02 */
125  put_bits(p, 8, 0); /* units type: 0 - aspect ratio */
126  put_bits(p, 16, avctx->sample_aspect_ratio.num);
127  put_bits(p, 16, avctx->sample_aspect_ratio.den);
128  put_bits(p, 8, 0); /* thumbnail width */
129  put_bits(p, 8, 0); /* thumbnail height */
130  }
131 
132  /* comment */
133  if (!(avctx->flags & CODEC_FLAG_BITEXACT)) {
134  put_marker(p, COM);
135  flush_put_bits(p);
136  ptr = put_bits_ptr(p);
137  put_bits(p, 16, 0); /* patched later */
139  size = strlen(LIBAVCODEC_IDENT)+3;
140  AV_WB16(ptr, size);
141  }
142 
143  if (avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
144  avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
145  avctx->pix_fmt == AV_PIX_FMT_YUV444P) {
146  put_marker(p, COM);
147  flush_put_bits(p);
148  ptr = put_bits_ptr(p);
149  put_bits(p, 16, 0); /* patched later */
150  avpriv_put_string(p, "CS=ITU601", 1);
151  size = strlen("CS=ITU601")+3;
152  AV_WB16(ptr, size);
153  }
154 }
155 
156 void ff_mjpeg_init_hvsample(AVCodecContext *avctx, int hsample[3], int vsample[3])
157 {
158  int chroma_h_shift, chroma_v_shift;
159 
160  av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift,
161  &chroma_v_shift);
162  if (avctx->codec->id == AV_CODEC_ID_LJPEG &&
163  ( avctx->pix_fmt == AV_PIX_FMT_BGR0
164  || avctx->pix_fmt == AV_PIX_FMT_BGRA
165  || avctx->pix_fmt == AV_PIX_FMT_BGR24)) {
166  vsample[0] = hsample[0] =
167  vsample[1] = hsample[1] =
168  vsample[2] = hsample[2] = 1;
169  } else if (avctx->pix_fmt == AV_PIX_FMT_YUV444P || avctx->pix_fmt == AV_PIX_FMT_YUVJ444P) {
170  vsample[0] = vsample[1] = vsample[2] = 2;
171  hsample[0] = hsample[1] = hsample[2] = 1;
172  } else {
173  vsample[0] = 2;
174  vsample[1] = 2 >> chroma_v_shift;
175  vsample[2] = 2 >> chroma_v_shift;
176  hsample[0] = 2;
177  hsample[1] = 2 >> chroma_h_shift;
178  hsample[2] = 2 >> chroma_h_shift;
179  }
180 }
181 
183  ScanTable *intra_scantable,
184  uint16_t luma_intra_matrix[64],
185  uint16_t chroma_intra_matrix[64])
186 {
187  const int lossless = avctx->codec_id != AV_CODEC_ID_MJPEG && avctx->codec_id != AV_CODEC_ID_AMV;
188  int hsample[3], vsample[3];
189  int i;
190  int chroma_matrix = !!memcmp(luma_intra_matrix,
191  chroma_intra_matrix,
192  sizeof(luma_intra_matrix[0])*64);
193 
194  ff_mjpeg_init_hvsample(avctx, hsample, vsample);
195 
196  put_marker(pb, SOI);
197 
198  // hack for AMV mjpeg format
199  if(avctx->codec_id == AV_CODEC_ID_AMV) goto end;
200 
201  jpeg_put_comments(avctx, pb);
202 
203  jpeg_table_header(avctx, pb, intra_scantable, luma_intra_matrix, chroma_intra_matrix, hsample);
204 
205  switch (avctx->codec_id) {
206  case AV_CODEC_ID_MJPEG: put_marker(pb, SOF0 ); break;
207  case AV_CODEC_ID_LJPEG: put_marker(pb, SOF3 ); break;
208  default: av_assert0(0);
209  }
210 
211  put_bits(pb, 16, 17);
212  if (lossless && ( avctx->pix_fmt == AV_PIX_FMT_BGR0
213  || avctx->pix_fmt == AV_PIX_FMT_BGRA
214  || avctx->pix_fmt == AV_PIX_FMT_BGR24))
215  put_bits(pb, 8, 9); /* 9 bits/component RCT */
216  else
217  put_bits(pb, 8, 8); /* 8 bits/component */
218  put_bits(pb, 16, avctx->height);
219  put_bits(pb, 16, avctx->width);
220  put_bits(pb, 8, 3); /* 3 components */
221 
222  /* Y component */
223  put_bits(pb, 8, 1); /* component number */
224  put_bits(pb, 4, hsample[0]); /* H factor */
225  put_bits(pb, 4, vsample[0]); /* V factor */
226  put_bits(pb, 8, 0); /* select matrix */
227 
228  /* Cb component */
229  put_bits(pb, 8, 2); /* component number */
230  put_bits(pb, 4, hsample[1]); /* H factor */
231  put_bits(pb, 4, vsample[1]); /* V factor */
232  put_bits(pb, 8, lossless ? 0 : chroma_matrix); /* select matrix */
233 
234  /* Cr component */
235  put_bits(pb, 8, 3); /* component number */
236  put_bits(pb, 4, hsample[2]); /* H factor */
237  put_bits(pb, 4, vsample[2]); /* V factor */
238  put_bits(pb, 8, lossless ? 0 : chroma_matrix); /* select matrix */
239 
240  /* scan header */
241  put_marker(pb, SOS);
242  put_bits(pb, 16, 12); /* length */
243  put_bits(pb, 8, 3); /* 3 components */
244 
245  /* Y component */
246  put_bits(pb, 8, 1); /* index */
247  put_bits(pb, 4, 0); /* DC huffman table index */
248  put_bits(pb, 4, 0); /* AC huffman table index */
249 
250  /* Cb component */
251  put_bits(pb, 8, 2); /* index */
252  put_bits(pb, 4, 1); /* DC huffman table index */
253  put_bits(pb, 4, lossless ? 0 : 1); /* AC huffman table index */
254 
255  /* Cr component */
256  put_bits(pb, 8, 3); /* index */
257  put_bits(pb, 4, 1); /* DC huffman table index */
258  put_bits(pb, 4, lossless ? 0 : 1); /* AC huffman table index */
259 
260  put_bits(pb, 8, lossless ? avctx->prediction_method + 1 : 0); /* Ss (not used) */
261 
262  switch (avctx->codec_id) {
263  case AV_CODEC_ID_MJPEG: put_bits(pb, 8, 63); break; /* Se (not used) */
264  case AV_CODEC_ID_LJPEG: put_bits(pb, 8, 0); break; /* not used */
265  default: av_assert0(0);
266  }
267 
268  put_bits(pb, 8, 0); /* Ah/Al (not used) */
269 
270 end:
271  if (!lossless) {
272  MpegEncContext *s = avctx->priv_data;
273  av_assert0(avctx->codec->priv_data_size == sizeof(MpegEncContext));
274 
275  s->esc_pos = put_bits_count(pb) >> 3;
276  for(i=1; i<s->slice_context_count; i++)
277  s->thread_context[i]->esc_pos = 0;
278  }
279 }
280 
282 {
283  int size;
284  int i, ff_count;
285  uint8_t *buf = pb->buf + start;
286  int align= (-(size_t)(buf))&3;
287  int pad = (-put_bits_count(pb))&7;
288 
289  if (pad)
290  put_bits(pb, pad, (1<<pad)-1);
291 
292  flush_put_bits(pb);
293  size = put_bits_count(pb) - start * 8;
294 
295  av_assert1((size&7) == 0);
296  size >>= 3;
297 
298  ff_count=0;
299  for(i=0; i<size && i<align; i++){
300  if(buf[i]==0xFF) ff_count++;
301  }
302  for(; i<size-15; i+=16){
303  int acc, v;
304 
305  v= *(uint32_t*)(&buf[i]);
306  acc= (((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
307  v= *(uint32_t*)(&buf[i+4]);
308  acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
309  v= *(uint32_t*)(&buf[i+8]);
310  acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
311  v= *(uint32_t*)(&buf[i+12]);
312  acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
313 
314  acc>>=4;
315  acc+= (acc>>16);
316  acc+= (acc>>8);
317  ff_count+= acc&0xFF;
318  }
319  for(; i<size; i++){
320  if(buf[i]==0xFF) ff_count++;
321  }
322 
323  if(ff_count==0) return;
324 
325  flush_put_bits(pb);
326  skip_put_bytes(pb, ff_count);
327 
328  for(i=size-1; ff_count; i--){
329  int v= buf[i];
330 
331  if(v==0xFF){
332  buf[i+ff_count]= 0;
333  ff_count--;
334  }
335 
336  buf[i+ff_count]= v;
337  }
338 }
339 
341 {
342  int i;
343  PutBitContext *pbc = &s->pb;
344  int mb_y = s->mb_y - !s->mb_x;
345 
346  ff_mjpeg_escape_FF(pbc, s->esc_pos);
347 
348  if((s->avctx->active_thread_type & FF_THREAD_SLICE) && mb_y < s->mb_height)
349  put_marker(pbc, RST0 + (mb_y&7));
350  s->esc_pos = put_bits_count(pbc) >> 3;
351 
352  for(i=0; i<3; i++)
353  s->last_dc[i] = 128 << s->intra_dc_precision;
354 }
355 
357 {
358  av_assert1((header_bits & 7) == 0);
359 
360  put_marker(pb, EOI);
361 }
362 
364  uint8_t *huff_size, uint16_t *huff_code)
365 {
366  int mant, nbits;
367 
368  if (val == 0) {
369  put_bits(pb, huff_size[0], huff_code[0]);
370  } else {
371  mant = val;
372  if (val < 0) {
373  val = -val;
374  mant--;
375  }
376 
377  nbits= av_log2_16bit(val) + 1;
378 
379  put_bits(pb, huff_size[nbits], huff_code[nbits]);
380 
381  put_sbits(pb, nbits, mant);
382  }
383 }