FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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/pixdesc.h"
36 
37 #include "avcodec.h"
38 #include "idctdsp.h"
39 #include "internal.h"
40 #include "jpegtables.h"
41 #include "mjpegenc_common.h"
42 #include "mpegvideo.h"
43 #include "mjpeg.h"
44 #include "mjpegenc.h"
45 
46 typedef struct LJpegEncContext {
49  uint16_t matrix[64];
50 
51  int vsample[3];
52  int hsample[3];
53 
54  uint16_t huff_code_dc_luminance[12];
58 
59  uint16_t (*scratch)[4];
61 
63  const AVFrame *frame)
64 {
65  LJpegEncContext *s = avctx->priv_data;
66  const int width = frame->width;
67  const int height = frame->height;
68  const int linesize = frame->linesize[0];
69  uint16_t (*buffer)[4] = s->scratch;
70  const int predictor = avctx->prediction_method+1;
71  int left[3], top[3], topleft[3];
72  int x, y, i;
73 
74  for (i = 0; i < 3; i++)
75  buffer[0][i] = 1 << (9 - 1);
76 
77  for (y = 0; y < height; y++) {
78  const int modified_predictor = y ? predictor : 1;
79  uint8_t *ptr = frame->data[0] + (linesize * y);
80 
81  if (pb->buf_end - pb->buf - (put_bits_count(pb) >> 3) < width * 3 * 4) {
82  av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
83  return -1;
84  }
85 
86  for (i = 0; i < 3; i++)
87  top[i]= left[i]= topleft[i]= buffer[0][i];
88 
89  for (x = 0; x < width; x++) {
90  if(avctx->pix_fmt == AV_PIX_FMT_BGR24){
91  buffer[x][1] = ptr[3 * x + 0] - ptr[3 * x + 1] + 0x100;
92  buffer[x][2] = ptr[3 * x + 2] - ptr[3 * x + 1] + 0x100;
93  buffer[x][0] = (ptr[3 * x + 0] + 2 * ptr[3 * x + 1] + ptr[3 * x + 2]) >> 2;
94  }else{
95  buffer[x][1] = ptr[4 * x + 0] - ptr[4 * x + 1] + 0x100;
96  buffer[x][2] = ptr[4 * x + 2] - ptr[4 * x + 1] + 0x100;
97  buffer[x][0] = (ptr[4 * x + 0] + 2 * ptr[4 * x + 1] + ptr[4 * x + 2]) >> 2;
98  }
99 
100  for (i = 0; i < 3; i++) {
101  int pred, diff;
102 
103  PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
104 
105  topleft[i] = top[i];
106  top[i] = buffer[x+1][i];
107 
108  left[i] = buffer[x][i];
109 
110  diff = ((left[i] - pred + 0x100) & 0x1FF) - 0x100;
111 
112  if (i == 0)
114  else
116  }
117  }
118  }
119 
120  return 0;
121 }
122 
124  const AVFrame *frame, int predictor,
125  int mb_x, int mb_y)
126 {
127  int i;
128 
129  if (mb_x == 0 || mb_y == 0) {
130  for (i = 0; i < 3; i++) {
131  uint8_t *ptr;
132  int x, y, h, v, linesize;
133  h = s->hsample[i];
134  v = s->vsample[i];
135  linesize = frame->linesize[i];
136 
137  for (y = 0; y < v; y++) {
138  for (x = 0; x < h; x++) {
139  int pred;
140 
141  ptr = frame->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
142  if (y == 0 && mb_y == 0) {
143  if (x == 0 && mb_x == 0)
144  pred = 128;
145  else
146  pred = ptr[-1];
147  } else {
148  if (x == 0 && mb_x == 0) {
149  pred = ptr[-linesize];
150  } else {
151  PREDICT(pred, ptr[-linesize - 1], ptr[-linesize],
152  ptr[-1], predictor);
153  }
154  }
155 
156  if (i == 0)
157  ff_mjpeg_encode_dc(pb, *ptr - pred, s->huff_size_dc_luminance, s->huff_code_dc_luminance); //FIXME ugly
158  else
160  }
161  }
162  }
163  } else {
164  for (i = 0; i < 3; i++) {
165  uint8_t *ptr;
166  int x, y, h, v, linesize;
167  h = s->hsample[i];
168  v = s->vsample[i];
169  linesize = frame->linesize[i];
170 
171  for (y = 0; y < v; y++) {
172  for (x = 0; x < h; x++) {
173  int pred;
174 
175  ptr = frame->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
176  PREDICT(pred, ptr[-linesize - 1], ptr[-linesize], ptr[-1], predictor);
177 
178  if (i == 0)
179  ff_mjpeg_encode_dc(pb, *ptr - pred, s->huff_size_dc_luminance, s->huff_code_dc_luminance); //FIXME ugly
180  else
182  }
183  }
184  }
185  }
186 }
187 
189  const AVFrame *frame)
190 {
191  const int predictor = avctx->prediction_method + 1;
192  LJpegEncContext *s = avctx->priv_data;
193  const int mb_width = (avctx->width + s->hsample[0] - 1) / s->hsample[0];
194  const int mb_height = (avctx->height + s->vsample[0] - 1) / s->vsample[0];
195  int mb_x, mb_y;
196 
197  for (mb_y = 0; mb_y < mb_height; mb_y++) {
198  if (pb->buf_end - pb->buf - (put_bits_count(pb) >> 3) <
199  mb_width * 4 * 3 * s->hsample[0] * s->vsample[0]) {
200  av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
201  return -1;
202  }
203 
204  for (mb_x = 0; mb_x < mb_width; mb_x++)
205  ljpeg_encode_yuv_mb(s, pb, frame, predictor, mb_x, mb_y);
206  }
207 
208  return 0;
209 }
210 
212  const AVFrame *pict, int *got_packet)
213 {
214  LJpegEncContext *s = avctx->priv_data;
215  PutBitContext pb;
216  const int width = avctx->width;
217  const int height = avctx->height;
218  const int mb_width = (width + s->hsample[0] - 1) / s->hsample[0];
219  const int mb_height = (height + s->vsample[0] - 1) / s->vsample[0];
220  int max_pkt_size = FF_MIN_BUFFER_SIZE;
221  int ret, header_bits;
222 
223  if( avctx->pix_fmt == AV_PIX_FMT_BGR0
224  || avctx->pix_fmt == AV_PIX_FMT_BGRA
225  || avctx->pix_fmt == AV_PIX_FMT_BGR24)
226  max_pkt_size += width * height * 3 * 4;
227  else {
228  max_pkt_size += mb_width * mb_height * 3 * 4
229  * s->hsample[0] * s->vsample[0];
230  }
231 
232  if ((ret = ff_alloc_packet2(avctx, pkt, max_pkt_size)) < 0)
233  return ret;
234 
235  init_put_bits(&pb, pkt->data, pkt->size);
236 
238  s->matrix, s->matrix);
239 
240  header_bits = put_bits_count(&pb);
241 
242  if( avctx->pix_fmt == AV_PIX_FMT_BGR0
243  || avctx->pix_fmt == AV_PIX_FMT_BGRA
244  || avctx->pix_fmt == AV_PIX_FMT_BGR24)
245  ret = ljpeg_encode_bgr(avctx, &pb, pict);
246  else
247  ret = ljpeg_encode_yuv(avctx, &pb, pict);
248  if (ret < 0)
249  return ret;
250 
251  emms_c();
252 
253  ff_mjpeg_escape_FF(&pb, header_bits >> 3);
254  ff_mjpeg_encode_picture_trailer(&pb, header_bits);
255 
256  flush_put_bits(&pb);
257  pkt->size = put_bits_ptr(&pb) - pb.buf;
258  pkt->flags |= AV_PKT_FLAG_KEY;
259  *got_packet = 1;
260 
261  return 0;
262 }
263 
265 {
266  LJpegEncContext *s = avctx->priv_data;
267 
268  av_frame_free(&avctx->coded_frame);
269  av_freep(&s->scratch);
270 
271  return 0;
272 }
273 
275 {
276  LJpegEncContext *s = avctx->priv_data;
277 
278  if ((avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
279  avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
280  avctx->pix_fmt == AV_PIX_FMT_YUV444P ||
281  avctx->color_range == AVCOL_RANGE_MPEG) &&
283  av_log(avctx, AV_LOG_ERROR,
284  "Limited range YUV is non-standard, set strict_std_compliance to "
285  "at least unofficial to use it.\n");
286  return AVERROR(EINVAL);
287  }
288 
289  avctx->coded_frame = av_frame_alloc();
290  if (!avctx->coded_frame)
291  return AVERROR(ENOMEM);
292 
294  avctx->coded_frame->key_frame = 1;
295 
296  s->scratch = av_malloc_array(avctx->width + 1, sizeof(*s->scratch));
297  if (!s->scratch)
298  goto fail;
299 
300  ff_idctdsp_init(&s->idsp, avctx);
303 
304  ff_mjpeg_init_hvsample(avctx, s->hsample, s->vsample);
305 
314 
315  return 0;
316 fail:
317  ljpeg_encode_close(avctx);
318  return AVERROR(ENOMEM);
319 }
320 
322  .name = "ljpeg",
323  .long_name = NULL_IF_CONFIG_SMALL("Lossless JPEG"),
324  .type = AVMEDIA_TYPE_VIDEO,
325  .id = AV_CODEC_ID_LJPEG,
326  .priv_data_size = sizeof(LJpegEncContext),
328  .encode2 = ljpeg_encode_frame,
329  .close = ljpeg_encode_close,
331  .pix_fmts = (const enum AVPixelFormat[]){
336 };
AVCodec ff_ljpeg_encoder
Definition: ljpegenc.c:321
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and/or allocate data.
Definition: utils.c:1736
IDCTDSPContext idsp
Definition: ljpegenc.c:47
float v
const char * s
Definition: avisynth_c.h:631
This structure describes decoded (raw) audio or video data.
Definition: frame.h:171
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:68
memory handling functions
AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2745
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
MJPEG encoder.
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:1960
Scantable.
Definition: idctdsp.h:29
int size
Definition: avcodec.h:1163
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1444
mpegvideo header.
static AVPacket pkt
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])
AVCodec.
Definition: avcodec.h:3181
MJPEG encoder and decoder.
uint16_t(* scratch)[4]
Definition: ljpegenc.c:59
uint8_t huff_size_dc_chrominance[12]
Definition: ljpegenc.c:57
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: jpegtables.c:127
uint8_t
#define av_cold
Definition: attributes.h:74
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:135
#define CODEC_CAP_INTRA_ONLY
Codec is intra only.
Definition: avcodec.h:886
const uint8_t avpriv_mjpeg_bits_dc_luminance[17]
Definition: jpegtables.c:65
static AVFrame * frame
uint8_t * data
Definition: avcodec.h:1162
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV422P and setting col...
Definition: pixfmt.h:76
#define av_log(a,...)
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1208
#define PREDICT(ret, topleft, top, left, predictor)
Definition: mjpeg.h:118
static void predictor(uint8_t *src, int size)
Definition: exr.c:220
int width
width and height of the video frame
Definition: frame.h:220
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
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:219
#define AVERROR(e)
Definition: error.h:43
const uint8_t avpriv_mjpeg_bits_dc_chrominance[17]
Definition: jpegtables.c:70
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:148
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:175
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:97
uint8_t * buf
Definition: put_bits.h:38
const char * name
Name of the codec implementation.
Definition: avcodec.h:3188
void ff_mjpeg_encode_picture_trailer(PutBitContext *pb, int header_bits)
uint16_t matrix[64]
Definition: ljpegenc.c:49
ScanTable scantable
Definition: ljpegenc.c:48
Libavcodec external API header.
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1168
reference-counted frame API
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:67
static int ljpeg_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pict, int *got_packet)
Definition: ljpegenc.c:211
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:242
static av_cold int ljpeg_encode_init(AVCodecContext *avctx)
Definition: ljpegenc.c:274
float y
#define FF_MIN_BUFFER_SIZE
minimum encoding buffer size Used to avoid some checks during header writing.
Definition: avcodec.h:637
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
Definition: pixfmt.h:75
ret
Definition: avfilter.c:974
int width
picture width / height.
Definition: avcodec.h:1414
static av_cold int ljpeg_encode_close(AVCodecContext *avctx)
Definition: ljpegenc.c:264
uint16_t huff_code_dc_chrominance[12]
Definition: ljpegenc.c:55
void ff_mjpeg_escape_FF(PutBitContext *pb, int start)
uint8_t idct_permutation[64]
IDCT input permutation.
Definition: idctdsp.h:94
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:66
static const float pred[4]
Definition: siprdata.h:259
#define FF_COMPLIANCE_UNOFFICIAL
Allow unofficial extensions.
Definition: avcodec.h:2547
void ff_mjpeg_encode_dc(PutBitContext *pb, int val, uint8_t *huff_size, uint16_t *huff_code)
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:199
main external API structure.
Definition: avcodec.h:1241
uint8_t * buf_end
Definition: put_bits.h:38
const uint8_t avpriv_mjpeg_val_dc[12]
Definition: jpegtables.c:67
void ff_mjpeg_init_hvsample(AVCodecContext *avctx, int hsample[3], int vsample[3])
BYTE int const BYTE int int int height
Definition: avisynth_c.h:676
const uint8_t ff_zigzag_direct[64]
Definition: mathtables.c:117
packed BGR 8:8:8, 32bpp, BGRXBGRX... X=unused/undefined
Definition: pixfmt.h:267
static int ljpeg_encode_yuv(AVCodecContext *avctx, PutBitContext *pb, const AVFrame *frame)
Definition: ljpegenc.c:188
static int ljpeg_encode_bgr(AVCodecContext *avctx, PutBitContext *pb, const AVFrame *frame)
Definition: ljpegenc.c:62
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:182
the normal 219*2^(n-8) "MPEG" YUV ranges
Definition: pixfmt.h:522
uint16_t huff_code_dc_luminance[12]
Definition: ljpegenc.c:54
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:63
common internal api header.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:101
#define CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: avcodec.h:866
int vsample[3]
Definition: ljpegenc.c:51
int prediction_method
prediction method (needed for huffyuv)
Definition: avcodec.h:1604
uint8_t huff_size_dc_luminance[12]
Definition: ljpegenc.c:56
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
Definition: pixfmt.h:77
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:48
void * priv_data
Definition: avcodec.h:1283
static av_always_inline int diff(const uint32_t a, const uint32_t b)
av_cold void ff_init_scantable(uint8_t *permutation, ScanTable *st, const uint8_t *src_scantable)
Definition: idctdsp.c:29
static void ljpeg_encode_yuv_mb(LJpegEncContext *s, PutBitContext *pb, const AVFrame *frame, int predictor, int mb_x, int mb_y)
Definition: ljpegenc.c:123
av_cold void ff_idctdsp_init(IDCTDSPContext *c, AVCodecContext *avctx)
Definition: idctdsp.c:241
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:237
int hsample[3]
Definition: ljpegenc.c:52
int height
Definition: frame.h:220
#define av_freep(p)
#define av_malloc_array(a, b)
AVPixelFormat
Pixel format.
Definition: pixfmt.h:61
This structure stores compressed data.
Definition: avcodec.h:1139
int strict_std_compliance
strictly follow the standard (MPEG4, ...).
Definition: avcodec.h:2543
GLuint buffer
Definition: opengl_enc.c:102
static int width