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 };