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 "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 * 4) {
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  if(avctx->pix_fmt == AV_PIX_FMT_BGR24){
89  buffer[x][1] = ptr[3 * x + 0] - ptr[3 * x + 1] + 0x100;
90  buffer[x][2] = ptr[3 * x + 2] - ptr[3 * x + 1] + 0x100;
91  buffer[x][0] = (ptr[3 * x + 0] + 2 * ptr[3 * x + 1] + ptr[3 * x + 2]) >> 2;
92  }else{
93  buffer[x][1] = ptr[4 * x + 0] - ptr[4 * x + 1] + 0x100;
94  buffer[x][2] = ptr[4 * x + 2] - ptr[4 * x + 1] + 0x100;
95  buffer[x][0] = (ptr[4 * x + 0] + 2 * ptr[4 * x + 1] + ptr[4 * x + 2]) >> 2;
96  }
97 
98  for (i = 0; i < 3; i++) {
99  int pred, diff;
100 
101  PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
102 
103  topleft[i] = top[i];
104  top[i] = buffer[x+1][i];
105 
106  left[i] = buffer[x][i];
107 
108  diff = ((left[i] - pred + 0x100) & 0x1FF) - 0x100;
109 
110  if (i == 0)
112  else
114  }
115  }
116  }
117 
118  return 0;
119 }
120 
122  const AVFrame *frame, int predictor,
123  int mb_x, int mb_y)
124 {
125  int i;
126 
127  if (mb_x == 0 || mb_y == 0) {
128  for (i = 0; i < 3; i++) {
129  uint8_t *ptr;
130  int x, y, h, v, linesize;
131  h = s->hsample[i];
132  v = s->vsample[i];
133  linesize = frame->linesize[i];
134 
135  for (y = 0; y < v; y++) {
136  for (x = 0; x < h; x++) {
137  int pred;
138 
139  ptr = frame->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
140  if (y == 0 && mb_y == 0) {
141  if (x == 0 && mb_x == 0)
142  pred = 128;
143  else
144  pred = ptr[-1];
145  } else {
146  if (x == 0 && mb_x == 0) {
147  pred = ptr[-linesize];
148  } else {
149  PREDICT(pred, ptr[-linesize - 1], ptr[-linesize],
150  ptr[-1], predictor);
151  }
152  }
153 
154  if (i == 0)
155  ff_mjpeg_encode_dc(pb, *ptr - pred, s->huff_size_dc_luminance, s->huff_code_dc_luminance); //FIXME ugly
156  else
158  }
159  }
160  }
161  } else {
162  for (i = 0; i < 3; i++) {
163  uint8_t *ptr;
164  int x, y, h, v, linesize;
165  h = s->hsample[i];
166  v = s->vsample[i];
167  linesize = frame->linesize[i];
168 
169  for (y = 0; y < v; y++) {
170  for (x = 0; x < h; x++) {
171  int pred;
172 
173  ptr = frame->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
174  PREDICT(pred, ptr[-linesize - 1], ptr[-linesize], ptr[-1], predictor);
175 
176  if (i == 0)
177  ff_mjpeg_encode_dc(pb, *ptr - pred, s->huff_size_dc_luminance, s->huff_code_dc_luminance); //FIXME ugly
178  else
180  }
181  }
182  }
183  }
184 }
185 
187  const AVFrame *frame)
188 {
189  const int predictor = avctx->prediction_method + 1;
190  LJpegEncContext *s = avctx->priv_data;
191  const int mb_width = (avctx->width + s->hsample[0] - 1) / s->hsample[0];
192  const int mb_height = (avctx->height + s->vsample[0] - 1) / s->vsample[0];
193  int mb_x, mb_y;
194 
195  for (mb_y = 0; mb_y < mb_height; mb_y++) {
196  if (pb->buf_end - pb->buf - (put_bits_count(pb) >> 3) <
197  mb_width * 4 * 3 * s->hsample[0] * s->vsample[0]) {
198  av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
199  return -1;
200  }
201 
202  for (mb_x = 0; mb_x < mb_width; mb_x++)
203  ljpeg_encode_yuv_mb(s, pb, frame, predictor, mb_x, mb_y);
204  }
205 
206  return 0;
207 }
208 
210  const AVFrame *pict, int *got_packet)
211 {
212  LJpegEncContext *s = avctx->priv_data;
213  PutBitContext pb;
214  const int width = avctx->width;
215  const int height = avctx->height;
216  const int mb_width = (width + s->hsample[0] - 1) / s->hsample[0];
217  const int mb_height = (height + s->vsample[0] - 1) / s->vsample[0];
218  int max_pkt_size = FF_MIN_BUFFER_SIZE;
219  int ret, header_bits;
220 
221  if( avctx->pix_fmt == AV_PIX_FMT_BGR0
222  || avctx->pix_fmt == AV_PIX_FMT_BGRA
223  || avctx->pix_fmt == AV_PIX_FMT_BGR24)
224  max_pkt_size += width * height * 3 * 4;
225  else {
226  max_pkt_size += mb_width * mb_height * 3 * 4
227  * s->hsample[0] * s->vsample[0];
228  }
229 
230  if ((ret = ff_alloc_packet2(avctx, pkt, max_pkt_size)) < 0)
231  return ret;
232 
233  init_put_bits(&pb, pkt->data, pkt->size);
234 
236  s->matrix, s->matrix);
237 
238  header_bits = put_bits_count(&pb);
239 
240  if( avctx->pix_fmt == AV_PIX_FMT_BGR0
241  || avctx->pix_fmt == AV_PIX_FMT_BGRA
242  || avctx->pix_fmt == AV_PIX_FMT_BGR24)
243  ret = ljpeg_encode_bgr(avctx, &pb, pict);
244  else
245  ret = ljpeg_encode_yuv(avctx, &pb, pict);
246  if (ret < 0)
247  return ret;
248 
249  emms_c();
250 
251  ff_mjpeg_escape_FF(&pb, header_bits >> 3);
252  ff_mjpeg_encode_picture_trailer(&pb, header_bits);
253 
254  flush_put_bits(&pb);
255  pkt->size = put_bits_ptr(&pb) - pb.buf;
256  pkt->flags |= AV_PKT_FLAG_KEY;
257  *got_packet = 1;
258 
259  return 0;
260 }
261 
263 {
264  LJpegEncContext *s = avctx->priv_data;
265 
266  av_frame_free(&avctx->coded_frame);
267  av_freep(&s->scratch);
268 
269  return 0;
270 }
271 
273 {
274  LJpegEncContext *s = avctx->priv_data;
275 
276  if ((avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
277  avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
278  avctx->pix_fmt == AV_PIX_FMT_YUV444P) &&
280  av_log(avctx, AV_LOG_ERROR,
281  "Limited range YUV is non-standard, set strict_std_compliance to "
282  "at least unofficial to use it.\n");
283  return AVERROR(EINVAL);
284  }
285 
286  avctx->coded_frame = av_frame_alloc();
287  if (!avctx->coded_frame)
288  return AVERROR(ENOMEM);
289 
291  avctx->coded_frame->key_frame = 1;
292 
293  s->scratch = av_malloc_array(avctx->width + 1, sizeof(*s->scratch));
294 
295  ff_dsputil_init(&s->dsp, avctx);
297 
298  ff_mjpeg_init_hvsample(avctx, s->hsample, s->vsample);
299 
308 
309  return 0;
310 }
311 
313  .name = "ljpeg",
314  .long_name = NULL_IF_CONFIG_SMALL("Lossless JPEG"),
315  .type = AVMEDIA_TYPE_VIDEO,
316  .id = AV_CODEC_ID_LJPEG,
317  .priv_data_size = sizeof(LJpegEncContext),
319  .encode2 = ljpeg_encode_frame,
321  .pix_fmts = (const enum AVPixelFormat[]){
326 };