Libav
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 Libav.
12  *
13  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
25  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26  */
27 
33 #include "libavutil/frame.h"
34 #include "libavutil/mem.h"
35 #include "libavutil/pixdesc.h"
36 
37 #include "avcodec.h"
38 #include "dsputil.h"
39 #include "internal.h"
40 #include "mpegvideo.h"
41 #include "mjpeg.h"
42 #include "mjpegenc.h"
43 
44 typedef struct LJpegEncContext {
47  uint16_t matrix[64];
48 
49  int vsample[3];
50  int hsample[3];
51 
52  uint16_t huff_code_dc_luminance[12];
56 
57  uint16_t (*scratch)[4];
59 
61  const AVFrame *frame)
62 {
63  LJpegEncContext *s = avctx->priv_data;
64  const int width = frame->width;
65  const int height = frame->height;
66  const int linesize = frame->linesize[0];
67  uint16_t (*buffer)[4] = s->scratch;
68  const int predictor = avctx->prediction_method+1;
69  int left[3], top[3], topleft[3];
70  int x, y, i;
71 
72  for (i = 0; i < 3; i++)
73  buffer[0][i] = 1 << (9 - 1);
74 
75  for (y = 0; y < height; y++) {
76  const int modified_predictor = y ? predictor : 1;
77  uint8_t *ptr = frame->data[0] + (linesize * y);
78 
79  if (pb->buf_end - pb->buf - (put_bits_count(pb) >> 3) < width * 3 * 3) {
80  av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
81  return -1;
82  }
83 
84  for (i = 0; i < 3; i++)
85  top[i]= left[i]= topleft[i]= buffer[0][i];
86 
87  for (x = 0; x < width; x++) {
88  buffer[x][1] = ptr[3 * x + 0] - ptr[3 * x + 1] + 0x100;
89  buffer[x][2] = ptr[3 * x + 2] - ptr[3 * x + 1] + 0x100;
90  buffer[x][0] = (ptr[3 * x + 0] + 2 * ptr[3 * x + 1] + ptr[3 * x + 2]) >> 2;
91 
92  for (i = 0; i < 3; i++) {
93  int pred, diff;
94 
95  PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
96 
97  topleft[i] = top[i];
98  top[i] = buffer[x+1][i];
99 
100  left[i] = buffer[x][i];
101 
102  diff = ((left[i] - pred + 0x100) & 0x1FF) - 0x100;
103 
104  if (i == 0)
106  else
108  }
109  }
110  }
111 
112  return 0;
113 }
114 
116  const AVFrame *frame, int predictor,
117  int mb_x, int mb_y)
118 {
119  int i;
120 
121  if (mb_x == 0 || mb_y == 0) {
122  for (i = 0; i < 3; i++) {
123  uint8_t *ptr;
124  int x, y, h, v, linesize;
125  h = s->hsample[i];
126  v = s->vsample[i];
127  linesize = frame->linesize[i];
128 
129  for (y = 0; y < v; y++) {
130  for (x = 0; x < h; x++) {
131  int pred;
132 
133  ptr = frame->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
134  if (y == 0 && mb_y == 0) {
135  if (x == 0 && mb_x == 0)
136  pred = 128;
137  else
138  pred = ptr[-1];
139  } else {
140  if (x == 0 && mb_x == 0) {
141  pred = ptr[-linesize];
142  } else {
143  PREDICT(pred, ptr[-linesize - 1], ptr[-linesize],
144  ptr[-1], predictor);
145  }
146  }
147 
148  if (i == 0)
149  ff_mjpeg_encode_dc(pb, *ptr - pred, s->huff_size_dc_luminance, s->huff_code_dc_luminance); //FIXME ugly
150  else
152  }
153  }
154  }
155  } else {
156  for (i = 0; i < 3; i++) {
157  uint8_t *ptr;
158  int x, y, h, v, linesize;
159  h = s->hsample[i];
160  v = s->vsample[i];
161  linesize = frame->linesize[i];
162 
163  for (y = 0; y < v; y++) {
164  for (x = 0; x < h; x++) {
165  int pred;
166 
167  ptr = frame->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
168  PREDICT(pred, ptr[-linesize - 1], ptr[-linesize], ptr[-1], predictor);
169 
170  if (i == 0)
171  ff_mjpeg_encode_dc(pb, *ptr - pred, s->huff_size_dc_luminance, s->huff_code_dc_luminance); //FIXME ugly
172  else
174  }
175  }
176  }
177  }
178 }
179 
181  const AVFrame *frame)
182 {
183  const int predictor = avctx->prediction_method + 1;
184  LJpegEncContext *s = avctx->priv_data;
185  const int mb_width = (avctx->width + s->hsample[0] - 1) / s->hsample[0];
186  const int mb_height = (avctx->height + s->vsample[0] - 1) / s->vsample[0];
187  int mb_x, mb_y;
188 
189  for (mb_y = 0; mb_y < mb_height; mb_y++) {
190  if (pb->buf_end - pb->buf - (put_bits_count(pb) >> 3) <
191  mb_width * 4 * 3 * s->hsample[0] * s->vsample[0]) {
192  av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
193  return -1;
194  }
195 
196  for (mb_x = 0; mb_x < mb_width; mb_x++)
197  ljpeg_encode_yuv_mb(s, pb, frame, predictor, mb_x, mb_y);
198  }
199 
200  return 0;
201 }
202 
204  const AVFrame *pict, int *got_packet)
205 {
206  LJpegEncContext *s = avctx->priv_data;
207  PutBitContext pb;
208  const int width = avctx->width;
209  const int height = avctx->height;
210  const int mb_width = (width + s->hsample[0] - 1) / s->hsample[0];
211  const int mb_height = (height + s->vsample[0] - 1) / s->vsample[0];
212  int max_pkt_size = FF_MIN_BUFFER_SIZE;
213  int ret, header_bits;
214 
215  if (avctx->pix_fmt == AV_PIX_FMT_BGR24)
216  max_pkt_size += width * height * 3 * 3;
217  else {
218  max_pkt_size += mb_width * mb_height * 3 * 4
219  * s->hsample[0] * s->vsample[0];
220  }
221  if ((ret = ff_alloc_packet(pkt, max_pkt_size)) < 0) {
222  av_log(avctx, AV_LOG_ERROR, "Error getting output packet of size %d.\n", max_pkt_size);
223  return ret;
224  }
225 
226  init_put_bits(&pb, pkt->data, pkt->size);
227 
229  s->matrix);
230 
231  header_bits = put_bits_count(&pb);
232 
233  if (avctx->pix_fmt == AV_PIX_FMT_BGR24)
234  ret = ljpeg_encode_bgr(avctx, &pb, pict);
235  else
236  ret = ljpeg_encode_yuv(avctx, &pb, pict);
237  if (ret < 0)
238  return ret;
239 
240  emms_c();
241 
242  ff_mjpeg_encode_picture_trailer(&pb, header_bits);
243 
244  flush_put_bits(&pb);
245  pkt->size = put_bits_ptr(&pb) - pb.buf;
246  pkt->flags |= AV_PKT_FLAG_KEY;
247  *got_packet = 1;
248 
249  return 0;
250 }
251 
253 {
254  LJpegEncContext *s = avctx->priv_data;
255 
256  av_frame_free(&avctx->coded_frame);
257  av_freep(&s->scratch);
258 
259  return 0;
260 }
261 
263 {
264  LJpegEncContext *s = avctx->priv_data;
265  int chroma_v_shift, chroma_h_shift;
266 
267  if ((avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
268  avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
269  avctx->pix_fmt == AV_PIX_FMT_YUV444P) &&
271  av_log(avctx, AV_LOG_ERROR,
272  "Limited range YUV is non-standard, set strict_std_compliance to "
273  "at least unofficial to use it.\n");
274  return AVERROR(EINVAL);
275  }
276 
277  avctx->coded_frame = av_frame_alloc();
278  if (!avctx->coded_frame)
279  return AVERROR(ENOMEM);
280 
282  avctx->coded_frame->key_frame = 1;
283 
284  s->scratch = av_malloc_array(avctx->width + 1, sizeof(*s->scratch));
285 
286  ff_dsputil_init(&s->dsp, avctx);
288 
289  av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift,
290  &chroma_v_shift);
291 
292  if (avctx->pix_fmt == AV_PIX_FMT_BGR24) {
293  s->vsample[0] = s->hsample[0] =
294  s->vsample[1] = s->hsample[1] =
295  s->vsample[2] = s->hsample[2] = 1;
296  } else {
297  s->vsample[0] = 2;
298  s->vsample[1] = 2 >> chroma_v_shift;
299  s->vsample[2] = 2 >> chroma_v_shift;
300  s->hsample[0] = 2;
301  s->hsample[1] = 2 >> chroma_h_shift;
302  s->hsample[2] = 2 >> chroma_h_shift;
303  }
304 
313 
314  return 0;
315 }
316 
318  .name = "ljpeg",
319  .long_name = NULL_IF_CONFIG_SMALL("Lossless JPEG"),
320  .type = AVMEDIA_TYPE_VIDEO,
321  .id = AV_CODEC_ID_LJPEG,
322  .priv_data_size = sizeof(LJpegEncContext),
324  .encode2 = ljpeg_encode_frame,
326  .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUVJ420P,
333  AV_PIX_FMT_NONE },
334 };
AVCodec ff_ljpeg_encoder
Definition: ljpegenc.c:317
av_cold void ff_dsputil_init(DSPContext *c, AVCodecContext *avctx)
Definition: dsputil.c:2440
This structure describes decoded (raw) audio or video data.
Definition: frame.h:107
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:70
memory handling functions
AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2506
MJPEG encoder.
Scantable.
Definition: dsputil.h:111
int size
Definition: avcodec.h:974
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1247
const uint8_t avpriv_mjpeg_bits_dc_chrominance[17]
Definition: mjpeg.c:70
mpegvideo header.
AVCodec.
Definition: avcodec.h:2755
MJPEG encoder and decoder.
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
uint16_t(* scratch)[4]
Definition: ljpegenc.c:57
DSPContext dsp
Definition: ljpegenc.c:45
uint8_t huff_size_dc_chrominance[12]
Definition: ljpegenc.c:55
uint8_t
#define av_cold
Definition: attributes.h:66
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:43
#define emms_c()
Definition: internal.h:46
uint8_t * data
Definition: avcodec.h:973
void ff_mjpeg_build_huffman_codes(uint8_t *huff_size, uint16_t *huff_code, const uint8_t *bits_table, const uint8_t *val_table)
Definition: mjpeg.c:127
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV422P and setting color_...
Definition: pixfmt.h:78
uint8_t idct_permutation[64]
idct input permutation.
Definition: dsputil.h:240
#define FF_COMPLIANCE_UNOFFICIAL
Allow unofficial extensions.
Definition: avcodec.h:2331
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1023
#define PREDICT(ret, topleft, top, left, predictor)
Definition: mjpeg.h:128
int width
width and height of the video frame
Definition: frame.h:146
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
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:204
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:55
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:142
void ff_mjpeg_encode_picture_trailer(PutBitContext *pb, int header_bits)
Definition: mjpegenc.c:334
uint8_t * buf
Definition: put_bits.h:43
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:148
const char * name
Name of the codec implementation.
Definition: avcodec.h:2762
uint16_t matrix[64]
Definition: ljpegenc.c:47
ScanTable scantable
Definition: ljpegenc.c:46
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:979
reference-counted frame API
static int put_bits_count(PutBitContext *s)
Definition: put_bits.h:72
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:69
void ff_mjpeg_encode_picture_header(AVCodecContext *avctx, PutBitContext *pb, ScanTable *intra_scantable, uint16_t intra_matrix[64])
Definition: mjpegenc.c:180
static int ljpeg_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pict, int *got_packet)
Definition: ljpegenc.c:203
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:168
static av_cold int ljpeg_encode_init(AVCodecContext *avctx)
Definition: ljpegenc.c:262
#define FF_MIN_BUFFER_SIZE
minimum encoding buffer size Used to avoid some checks during header writing.
Definition: avcodec.h:516
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV420P and setting color_...
Definition: pixfmt.h:77
int width
picture width / height.
Definition: avcodec.h:1217
static av_cold int ljpeg_encode_close(AVCodecContext *avctx)
Definition: ljpegenc.c:252
void ff_mjpeg_encode_dc(PutBitContext *pb, int val, uint8_t *huff_size, uint16_t *huff_code)
Definition: mjpegenc.c:346
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
uint16_t huff_code_dc_chrominance[12]
Definition: ljpegenc.c:53
static char buffer[20]
Definition: seek-test.c:31
int ff_alloc_packet(AVPacket *avpkt, int size)
Check AVPacket size and/or allocate data.
Definition: utils.c:1125
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:68
static const float pred[4]
Definition: siprdata.h:259
static int width
Definition: utils.c:156
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:125
main external API structure.
Definition: avcodec.h:1054
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:489
av_cold void ff_init_scantable(uint8_t *permutation, ScanTable *st, const uint8_t *src_scantable)
Definition: dsputil.c:107
uint8_t * buf_end
Definition: put_bits.h:43
static int ljpeg_encode_yuv(AVCodecContext *avctx, PutBitContext *pb, const AVFrame *frame)
Definition: ljpegenc.c:180
static int ljpeg_encode_bgr(AVCodecContext *avctx, PutBitContext *pb, const AVFrame *frame)
Definition: ljpegenc.c:60
static void * av_malloc_array(size_t nmemb, size_t size)
Definition: mem.h:92
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:113
int height
Definition: gxfenc.c:72
uint16_t huff_code_dc_luminance[12]
Definition: ljpegenc.c:52
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:65
common internal api header.
const uint8_t ff_zigzag_direct[64]
Definition: mathtables.c:113
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:88
int vsample[3]
Definition: ljpegenc.c:49
int prediction_method
prediction method (needed for huffyuv)
Definition: avcodec.h:1403
uint8_t huff_size_dc_luminance[12]
Definition: ljpegenc.c:54
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:1533
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV444P and setting color_...
Definition: pixfmt.h:79
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:53
static av_cold int init(AVCodecParserContext *s)
Definition: h264_parser.c:498
DSP utils.
void * priv_data
Definition: avcodec.h:1090
static void ljpeg_encode_yuv_mb(LJpegEncContext *s, PutBitContext *pb, const AVFrame *frame, int predictor, int mb_x, int mb_y)
Definition: ljpegenc.c:115
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:163
int hsample[3]
Definition: ljpegenc.c:50
int height
Definition: frame.h:146
AVPixelFormat
Pixel format.
Definition: pixfmt.h:63
This structure stores compressed data.
Definition: avcodec.h:950
int strict_std_compliance
strictly follow the standard (MPEG4, ...).
Definition: avcodec.h:2327
DSPContext.
Definition: dsputil.h:124