FFmpeg  4.4.5
ljpegenc.c
Go to the documentation of this file.
1 /*
2  * lossless JPEG encoder
3  * Copyright (c) 2000, 2001 Fabrice Bellard
4  * Copyright (c) 2003 Alex Beregszaszi
5  * Copyright (c) 2003-2004 Michael Niedermayer
6  *
7  * Support for external huffman table, various fixes (AVID workaround),
8  * aspecting, new decode_frame mechanism and apple mjpeg-b support
9  * by Alex Beregszaszi
10  *
11  * This file is part of FFmpeg.
12  *
13  * FFmpeg is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU Lesser General Public
15  * License as published by the Free Software Foundation; either
16  * version 2.1 of the License, or (at your option) any later version.
17  *
18  * FFmpeg is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21  * Lesser General Public License for more details.
22  *
23  * You should have received a copy of the GNU Lesser General Public
24  * License along with FFmpeg; if not, write to the Free Software
25  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26  */
27 
28 /**
29  * @file
30  * lossless JPEG encoder.
31  */
32 
33 #include "libavutil/frame.h"
34 #include "libavutil/mem.h"
35 #include "libavutil/opt.h"
36 #include "libavutil/pixdesc.h"
37 
38 #include "avcodec.h"
39 #include "idctdsp.h"
40 #include "internal.h"
41 #include "jpegtables.h"
42 #include "mathops.h"
43 #include "mjpegenc_common.h"
44 #include "mjpeg.h"
45 
46 typedef struct LJpegEncContext {
47  AVClass *class;
50  uint16_t matrix[64];
51 
52  int vsample[4];
53  int hsample[4];
54 
55  uint16_t huff_code_dc_luminance[12];
59 
60  uint16_t (*scratch)[4];
61  int pred;
63 
65  const AVFrame *frame)
66 {
67  LJpegEncContext *s = avctx->priv_data;
68  const int width = frame->width;
69  const int height = frame->height;
70  const int linesize = frame->linesize[0];
71  uint16_t (*buffer)[4] = s->scratch;
72  int left[4], top[4], topleft[4];
73  int x, y, i;
74 
75 #if FF_API_PRIVATE_OPT
77  if (avctx->prediction_method)
78  s->pred = avctx->prediction_method + 1;
80 #endif
81 
82  for (i = 0; i < 4; i++)
83  buffer[0][i] = 1 << (9 - 1);
84 
85  for (y = 0; y < height; y++) {
86  const int modified_predictor = y ? s->pred : 1;
87  uint8_t *ptr = frame->data[0] + (linesize * y);
88 
89  if (pb->buf_end - pb->buf - (put_bits_count(pb) >> 3) < width * 4 * 4) {
90  av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
91  return -1;
92  }
93 
94  for (i = 0; i < 4; i++)
95  top[i]= left[i]= topleft[i]= buffer[0][i];
96 
97  for (x = 0; x < width; x++) {
98  if(avctx->pix_fmt == AV_PIX_FMT_BGR24){
99  buffer[x][1] = ptr[3 * x + 0] - ptr[3 * x + 1] + 0x100;
100  buffer[x][2] = ptr[3 * x + 2] - ptr[3 * x + 1] + 0x100;
101  buffer[x][0] = (ptr[3 * x + 0] + 2 * ptr[3 * x + 1] + ptr[3 * x + 2]) >> 2;
102  }else{
103  buffer[x][1] = ptr[4 * x + 0] - ptr[4 * x + 1] + 0x100;
104  buffer[x][2] = ptr[4 * x + 2] - ptr[4 * x + 1] + 0x100;
105  buffer[x][0] = (ptr[4 * x + 0] + 2 * ptr[4 * x + 1] + ptr[4 * x + 2]) >> 2;
106  if (avctx->pix_fmt == AV_PIX_FMT_BGRA)
107  buffer[x][3] = ptr[4 * x + 3];
108  }
109 
110  for (i = 0; i < 3 + (avctx->pix_fmt == AV_PIX_FMT_BGRA); i++) {
111  int pred, diff;
112 
113  PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
114 
115  topleft[i] = top[i];
116  top[i] = buffer[x+1][i];
117 
118  left[i] = buffer[x][i];
119 
120  diff = ((left[i] - pred + 0x100) & 0x1FF) - 0x100;
121 
122  if (i == 0 || i == 3)
123  ff_mjpeg_encode_dc(pb, diff, s->huff_size_dc_luminance, s->huff_code_dc_luminance); //FIXME ugly
124  else
125  ff_mjpeg_encode_dc(pb, diff, s->huff_size_dc_chrominance, s->huff_code_dc_chrominance);
126  }
127  }
128  }
129 
130  return 0;
131 }
132 
134  const AVFrame *frame, int predictor,
135  int mb_x, int mb_y)
136 {
137  int i;
138 
139  if (mb_x == 0 || mb_y == 0) {
140  for (i = 0; i < 3; i++) {
141  uint8_t *ptr;
142  int x, y, h, v, linesize;
143  h = s->hsample[i];
144  v = s->vsample[i];
145  linesize = frame->linesize[i];
146 
147  for (y = 0; y < v; y++) {
148  for (x = 0; x < h; x++) {
149  int pred;
150 
151  ptr = frame->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
152  if (y == 0 && mb_y == 0) {
153  if (x == 0 && mb_x == 0)
154  pred = 128;
155  else
156  pred = ptr[-1];
157  } else {
158  if (x == 0 && mb_x == 0) {
159  pred = ptr[-linesize];
160  } else {
161  PREDICT(pred, ptr[-linesize - 1], ptr[-linesize],
162  ptr[-1], predictor);
163  }
164  }
165 
166  if (i == 0)
167  ff_mjpeg_encode_dc(pb, *ptr - pred, s->huff_size_dc_luminance, s->huff_code_dc_luminance); //FIXME ugly
168  else
169  ff_mjpeg_encode_dc(pb, *ptr - pred, s->huff_size_dc_chrominance, s->huff_code_dc_chrominance);
170  }
171  }
172  }
173  } else {
174  for (i = 0; i < 3; i++) {
175  uint8_t *ptr;
176  int x, y, h, v, linesize;
177  h = s->hsample[i];
178  v = s->vsample[i];
179  linesize = frame->linesize[i];
180 
181  for (y = 0; y < v; y++) {
182  for (x = 0; x < h; x++) {
183  int pred;
184 
185  ptr = frame->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
186  PREDICT(pred, ptr[-linesize - 1], ptr[-linesize], ptr[-1], predictor);
187 
188  if (i == 0)
189  ff_mjpeg_encode_dc(pb, *ptr - pred, s->huff_size_dc_luminance, s->huff_code_dc_luminance); //FIXME ugly
190  else
191  ff_mjpeg_encode_dc(pb, *ptr - pred, s->huff_size_dc_chrominance, s->huff_code_dc_chrominance);
192  }
193  }
194  }
195  }
196 }
197 
199  const AVFrame *frame)
200 {
201  LJpegEncContext *s = avctx->priv_data;
202  const int mb_width = (avctx->width + s->hsample[0] - 1) / s->hsample[0];
203  const int mb_height = (avctx->height + s->vsample[0] - 1) / s->vsample[0];
204  int mb_x, mb_y;
205 
206 #if FF_API_PRIVATE_OPT
208  if (avctx->prediction_method)
209  s->pred = avctx->prediction_method + 1;
211 #endif
212 
213  for (mb_y = 0; mb_y < mb_height; mb_y++) {
214  if (pb->buf_end - pb->buf - (put_bits_count(pb) >> 3) <
215  mb_width * 4 * 3 * s->hsample[0] * s->vsample[0]) {
216  av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
217  return -1;
218  }
219 
220  for (mb_x = 0; mb_x < mb_width; mb_x++)
221  ljpeg_encode_yuv_mb(s, pb, frame, s->pred, mb_x, mb_y);
222  }
223 
224  return 0;
225 }
226 
228  const AVFrame *pict, int *got_packet)
229 {
230  LJpegEncContext *s = avctx->priv_data;
231  PutBitContext pb;
232  const int width = avctx->width;
233  const int height = avctx->height;
234  const int mb_width = (width + s->hsample[0] - 1) / s->hsample[0];
235  const int mb_height = (height + s->vsample[0] - 1) / s->vsample[0];
236  int max_pkt_size = AV_INPUT_BUFFER_MIN_SIZE;
237  int ret, header_bits;
238 
239  if( avctx->pix_fmt == AV_PIX_FMT_BGR0
240  || avctx->pix_fmt == AV_PIX_FMT_BGR24)
241  max_pkt_size += width * height * 3 * 4;
242  else if(avctx->pix_fmt == AV_PIX_FMT_BGRA)
243  max_pkt_size += width * height * 4 * 4;
244  else {
245  max_pkt_size += mb_width * mb_height * 3 * 4
246  * s->hsample[0] * s->vsample[0];
247  }
248 
249  if ((ret = ff_alloc_packet2(avctx, pkt, max_pkt_size, 0)) < 0)
250  return ret;
251 
252  init_put_bits(&pb, pkt->data, pkt->size);
253 
254  ff_mjpeg_encode_picture_header(avctx, &pb, &s->scantable,
255  s->pred, s->matrix, s->matrix);
256 
257  header_bits = put_bits_count(&pb);
258 
259  if( avctx->pix_fmt == AV_PIX_FMT_BGR0
260  || avctx->pix_fmt == AV_PIX_FMT_BGRA
261  || avctx->pix_fmt == AV_PIX_FMT_BGR24)
262  ret = ljpeg_encode_bgr(avctx, &pb, pict);
263  else
264  ret = ljpeg_encode_yuv(avctx, &pb, pict);
265  if (ret < 0)
266  return ret;
267 
268  emms_c();
269 
270  ff_mjpeg_escape_FF(&pb, header_bits >> 3);
271  ff_mjpeg_encode_picture_trailer(&pb, header_bits);
272 
273  flush_put_bits(&pb);
274  pkt->size = put_bits_ptr(&pb) - pb.buf;
276  *got_packet = 1;
277 
278  return 0;
279 }
280 
282 {
283  LJpegEncContext *s = avctx->priv_data;
284 
285  av_freep(&s->scratch);
286 
287  return 0;
288 }
289 
291 {
292  LJpegEncContext *s = avctx->priv_data;
293 
294  if ((avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
295  avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
296  avctx->pix_fmt == AV_PIX_FMT_YUV444P ||
297  avctx->color_range == AVCOL_RANGE_MPEG) &&
299  av_log(avctx, AV_LOG_ERROR,
300  "Limited range YUV is non-standard, set strict_std_compliance to "
301  "at least unofficial to use it.\n");
302  return AVERROR(EINVAL);
303  }
304 
305 #if FF_API_CODED_FRAME
308  avctx->coded_frame->key_frame = 1;
310 #endif
311 
312  s->scratch = av_malloc_array(avctx->width + 1, sizeof(*s->scratch));
313  if (!s->scratch)
314  return AVERROR(ENOMEM);
315 
316  ff_idctdsp_init(&s->idsp, avctx);
317  ff_init_scantable(s->idsp.idct_permutation, &s->scantable,
319 
320  ff_mjpeg_init_hvsample(avctx, s->hsample, s->vsample);
321 
322  ff_mjpeg_build_huffman_codes(s->huff_size_dc_luminance,
323  s->huff_code_dc_luminance,
326  ff_mjpeg_build_huffman_codes(s->huff_size_dc_chrominance,
327  s->huff_code_dc_chrominance,
330 
331  return 0;
332 }
333 
334 #define OFFSET(x) offsetof(LJpegEncContext, x)
335 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
336 static const AVOption options[] = {
337 { "pred", "Prediction method", OFFSET(pred), AV_OPT_TYPE_INT, { .i64 = 1 }, 1, 3, VE, "pred" },
338  { "left", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX, VE, "pred" },
339  { "plane", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 2 }, INT_MIN, INT_MAX, VE, "pred" },
340  { "median", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 3 }, INT_MIN, INT_MAX, VE, "pred" },
341 
342  { NULL},
343 };
344 
345 static const AVClass ljpeg_class = {
346  .class_name = "ljpeg",
347  .item_name = av_default_item_name,
348  .option = options,
349  .version = LIBAVUTIL_VERSION_INT,
350 };
351 
353  .name = "ljpeg",
354  .long_name = NULL_IF_CONFIG_SMALL("Lossless JPEG"),
355  .type = AVMEDIA_TYPE_VIDEO,
356  .id = AV_CODEC_ID_LJPEG,
357  .priv_data_size = sizeof(LJpegEncContext),
358  .priv_class = &ljpeg_class,
360  .encode2 = ljpeg_encode_frame,
361  .close = ljpeg_encode_close,
362  .capabilities = AV_CODEC_CAP_FRAME_THREADS,
363  .pix_fmts = (const enum AVPixelFormat[]){
368 };
#define av_cold
Definition: attributes.h:88
uint8_t
Libavcodec external API header.
#define FF_COMPLIANCE_UNOFFICIAL
Allow unofficial extensions.
Definition: avcodec.h:1605
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:31
#define s(width, name)
Definition: cbs_vp9.c:257
#define NULL
Definition: coverity.c:32
static AVFrame * frame
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
Definition: encode.c:33
static void predictor(uint8_t *src, ptrdiff_t size)
Definition: exrenc.c:163
reference-counted frame API
@ AV_OPT_TYPE_CONST
Definition: opt.h:234
@ AV_OPT_TYPE_INT
Definition: opt.h:225
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: codec.h:108
@ AV_CODEC_ID_LJPEG
Definition: codec_id.h:58
#define AV_INPUT_BUFFER_MIN_SIZE
minimum encoding buffer size Used to avoid some checks during header writing.
Definition: avcodec.h:222
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:410
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:274
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
av_cold void ff_idctdsp_init(IDCTDSPContext *c, AVCodecContext *avctx)
Definition: idctdsp.c:238
av_cold void ff_init_scantable(uint8_t *permutation, ScanTable *st, const uint8_t *src_scantable)
Definition: idctdsp.c:29
int i
Definition: input.c:407
const uint8_t avpriv_mjpeg_val_dc[12]
Definition: jpegtables.c:67
const uint8_t avpriv_mjpeg_bits_dc_luminance[17]
Definition: jpegtables.c:65
const uint8_t avpriv_mjpeg_bits_dc_chrominance[17]
Definition: jpegtables.c:70
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:83
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:84
#define emms_c()
Definition: internal.h:54
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:309
AVCodec ff_ljpeg_encoder
Definition: ljpegenc.c:352
static int ljpeg_encode_bgr(AVCodecContext *avctx, PutBitContext *pb, const AVFrame *frame)
Definition: ljpegenc.c:64
static av_cold int ljpeg_encode_close(AVCodecContext *avctx)
Definition: ljpegenc.c:281
static const AVOption options[]
Definition: ljpegenc.c:336
#define VE
Definition: ljpegenc.c:335
static av_cold int ljpeg_encode_init(AVCodecContext *avctx)
Definition: ljpegenc.c:290
static void ljpeg_encode_yuv_mb(LJpegEncContext *s, PutBitContext *pb, const AVFrame *frame, int predictor, int mb_x, int mb_y)
Definition: ljpegenc.c:133
static int ljpeg_encode_yuv(AVCodecContext *avctx, PutBitContext *pb, const AVFrame *frame)
Definition: ljpegenc.c:198
static int ljpeg_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pict, int *got_packet)
Definition: ljpegenc.c:227
static const AVClass ljpeg_class
Definition: ljpegenc.c:345
#define OFFSET(x)
Definition: ljpegenc.c:334
const uint8_t ff_zigzag_direct[64]
Definition: mathtables.c:98
Memory handling functions.
MJPEG encoder and decoder.
#define PREDICT(ret, topleft, top, left, predictor)
Definition: mjpeg.h:118
void ff_mjpeg_init_hvsample(AVCodecContext *avctx, int hsample[4], int vsample[4])
void ff_mjpeg_encode_picture_header(AVCodecContext *avctx, PutBitContext *pb, ScanTable *intra_scantable, int pred, uint16_t luma_intra_matrix[64], uint16_t chroma_intra_matrix[64])
void ff_mjpeg_escape_FF(PutBitContext *pb, int start)
void ff_mjpeg_build_huffman_codes(uint8_t *huff_size, uint16_t *huff_code, const uint8_t *bits_table, const uint8_t *val_table)
void ff_mjpeg_encode_dc(PutBitContext *pb, int val, uint8_t *huff_size, uint16_t *huff_code)
void ff_mjpeg_encode_picture_trailer(PutBitContext *pb, int header_bits)
AVOptions.
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition: pixfmt.h:569
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
@ AV_PIX_FMT_BGR0
packed BGR 8:8:8, 32bpp, BGRXBGRX... X=unused/undefined
Definition: pixfmt.h:240
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:70
@ AV_PIX_FMT_BGRA
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:95
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:71
@ AV_PIX_FMT_YUVJ422P
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV422P and setting col...
Definition: pixfmt.h:79
@ AV_PIX_FMT_BGR24
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:69
@ AV_PIX_FMT_YUVJ444P
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
Definition: pixfmt.h:80
@ AV_PIX_FMT_YUVJ420P
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
Definition: pixfmt.h:78
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:57
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:342
static int put_bits_count(PutBitContext *s)
Definition: put_bits.h:76
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:110
static char buffer[20]
Definition: seek.c:32
static const float pred[4]
Definition: siprdata.h:259
Describe the class of an AVClass context structure.
Definition: log.h:67
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
main external API structure.
Definition: avcodec.h:536
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:746
int width
picture width / height.
Definition: avcodec.h:709
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:1171
int strict_std_compliance
strictly follow the standard (MPEG-4, ...).
Definition: avcodec.h:1601
attribute_deprecated AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:1764
attribute_deprecated int prediction_method
Definition: avcodec.h:895
void * priv_data
Definition: avcodec.h:563
AVCodec.
Definition: codec.h:197
const char * name
Name of the codec implementation.
Definition: codec.h:204
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:332
int width
Definition: frame.h:376
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:396
int height
Definition: frame.h:376
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:349
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:401
AVOption.
Definition: opt.h:248
This structure stores compressed data.
Definition: packet.h:346
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:375
int size
Definition: packet.h:370
uint8_t * data
Definition: packet.h:369
ScanTable scantable
Definition: ljpegenc.c:49
uint16_t huff_code_dc_chrominance[12]
Definition: ljpegenc.c:56
IDCTDSPContext idsp
Definition: ljpegenc.c:48
int hsample[4]
Definition: ljpegenc.c:53
uint8_t huff_size_dc_luminance[12]
Definition: ljpegenc.c:57
uint16_t(* scratch)[4]
Definition: ljpegenc.c:60
int vsample[4]
Definition: ljpegenc.c:52
uint8_t huff_size_dc_chrominance[12]
Definition: ljpegenc.c:58
uint16_t matrix[64]
Definition: ljpegenc.c:50
uint16_t huff_code_dc_luminance[12]
Definition: ljpegenc.c:55
uint8_t * buf
Definition: put_bits.h:47
uint8_t * buf_end
Definition: put_bits.h:47
Scantable.
Definition: idctdsp.h:31
#define av_malloc_array(a, b)
#define av_freep(p)
#define av_log(a,...)
AVPacket * pkt
Definition: movenc.c:59
#define height
#define width
static av_always_inline int diff(const uint32_t a, const uint32_t b)