FFmpeg
 All Data Structures 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 "avcodec.h"
34 #include "dsputil.h"
35 #include "internal.h"
36 #include "mpegvideo.h"
37 #include "mjpeg.h"
38 #include "mjpegenc.h"
39 
40 
42  const AVFrame *pict, int *got_packet)
43 {
44  MpegEncContext * const s = avctx->priv_data;
45  MJpegContext * const m = s->mjpeg_ctx;
46  const int width= s->width;
47  const int height= s->height;
48  AVFrame * const p = &s->current_picture.f;
49  const int predictor= avctx->prediction_method+1;
50  const int mb_width = (width + s->mjpeg_hsample[0] - 1) / s->mjpeg_hsample[0];
51  const int mb_height = (height + s->mjpeg_vsample[0] - 1) / s->mjpeg_vsample[0];
52  int ret, max_pkt_size = FF_MIN_BUFFER_SIZE;
53 
54  if (avctx->pix_fmt == AV_PIX_FMT_BGRA)
55  max_pkt_size += width * height * 3 * 4;
56  else {
57  max_pkt_size += mb_width * mb_height * 3 * 4
58  * s->mjpeg_hsample[0] * s->mjpeg_vsample[0];
59  }
60 
61  if (!s->edge_emu_buffer &&
62  (ret = ff_mpv_frame_size_alloc(s, pict->linesize[0])) < 0) {
63  av_log(avctx, AV_LOG_ERROR, "failed to allocate context scratch buffers.\n");
64  return ret;
65  }
66 
67  if ((ret = ff_alloc_packet2(avctx, pkt, max_pkt_size)) < 0)
68  return ret;
69 
70  init_put_bits(&s->pb, pkt->data, pkt->size);
71 
72  *p = *pict;
74  p->key_frame= 1;
75 
77 
78  s->header_bits= put_bits_count(&s->pb);
79 
80  if(avctx->pix_fmt == AV_PIX_FMT_BGR0
81  || avctx->pix_fmt == AV_PIX_FMT_BGRA
82  || avctx->pix_fmt == AV_PIX_FMT_BGR24){
83  int x, y, i;
84  const int linesize= p->linesize[0];
85  uint16_t (*buffer)[4]= (void *) s->rd_scratchpad;
86  int left[3], top[3], topleft[3];
87 
88  for(i=0; i<3; i++){
89  buffer[0][i]= 1 << (9 - 1);
90  }
91 
92  for(y = 0; y < height; y++) {
93  const int modified_predictor= y ? predictor : 1;
94  uint8_t *ptr = p->data[0] + (linesize * y);
95 
96  if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < width*3*4){
97  av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
98  return -1;
99  }
100 
101  for(i=0; i<3; i++){
102  top[i]= left[i]= topleft[i]= buffer[0][i];
103  }
104  for(x = 0; x < width; x++) {
105  if(avctx->pix_fmt == AV_PIX_FMT_BGR24){
106  buffer[x][1] = ptr[3*x+0] - ptr[3*x+1] + 0x100;
107  buffer[x][2] = ptr[3*x+2] - ptr[3*x+1] + 0x100;
108  buffer[x][0] = (ptr[3*x+0] + 2*ptr[3*x+1] + ptr[3*x+2])>>2;
109  }else{
110  buffer[x][1] = ptr[4*x+0] - ptr[4*x+1] + 0x100;
111  buffer[x][2] = ptr[4*x+2] - ptr[4*x+1] + 0x100;
112  buffer[x][0] = (ptr[4*x+0] + 2*ptr[4*x+1] + ptr[4*x+2])>>2;
113  }
114 
115  for(i=0;i<3;i++) {
116  int pred, diff;
117 
118  PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
119 
120  topleft[i]= top[i];
121  top[i]= buffer[x+1][i];
122 
123  left[i]= buffer[x][i];
124 
125  diff= ((left[i] - pred + 0x100)&0x1FF) - 0x100;
126 
127  if(i==0)
129  else
131  }
132  }
133  }
134  }else{
135  int mb_x, mb_y, i;
136 
137  for(mb_y = 0; mb_y < mb_height; mb_y++) {
138  if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < mb_width * 4 * 3 * s->mjpeg_hsample[0] * s->mjpeg_vsample[0]){
139  av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
140  return -1;
141  }
142  for(mb_x = 0; mb_x < mb_width; mb_x++) {
143  if(mb_x==0 || mb_y==0){
144  for(i=0;i<3;i++) {
145  uint8_t *ptr;
146  int x, y, h, v, linesize;
147  h = s->mjpeg_hsample[i];
148  v = s->mjpeg_vsample[i];
149  linesize= p->linesize[i];
150 
151  for(y=0; y<v; y++){
152  for(x=0; x<h; x++){
153  int pred;
154 
155  ptr = p->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
156  if(y==0 && mb_y==0){
157  if(x==0 && mb_x==0){
158  pred= 128;
159  }else{
160  pred= ptr[-1];
161  }
162  }else{
163  if(x==0 && mb_x==0){
164  pred= ptr[-linesize];
165  }else{
166  PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
167  }
168  }
169 
170  if(i==0)
171  ff_mjpeg_encode_dc(s, *ptr - pred, m->huff_size_dc_luminance, m->huff_code_dc_luminance); //FIXME ugly
172  else
174  }
175  }
176  }
177  }else{
178  for(i=0;i<3;i++) {
179  uint8_t *ptr;
180  int x, y, h, v, linesize;
181  h = s->mjpeg_hsample[i];
182  v = s->mjpeg_vsample[i];
183  linesize= p->linesize[i];
184 
185  for(y=0; y<v; y++){
186  for(x=0; x<h; x++){
187  int pred;
188 
189  ptr = p->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
190  PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
191 
192  if(i==0)
193  ff_mjpeg_encode_dc(s, *ptr - pred, m->huff_size_dc_luminance, m->huff_code_dc_luminance); //FIXME ugly
194  else
196  }
197  }
198  }
199  }
200  }
201  }
202  }
203 
204  emms_c();
205  av_assert0(s->esc_pos == s->header_bits >> 3);
208  s->picture_number++;
209 
210  flush_put_bits(&s->pb);
211  pkt->size = put_bits_ptr(&s->pb) - s->pb.buf;
212  pkt->flags |= AV_PKT_FLAG_KEY;
213  *got_packet = 1;
214 
215  return 0;
216 // return (put_bits_count(&f->pb)+7)/8;
217 }
218 
219 
220 AVCodec ff_ljpeg_encoder = { //FIXME avoid MPV_* lossless JPEG should not need them
221  .name = "ljpeg",
222  .type = AVMEDIA_TYPE_VIDEO,
223  .id = AV_CODEC_ID_LJPEG,
224  .priv_data_size = sizeof(MpegEncContext),
226  .encode2 = encode_picture_lossless,
228  .pix_fmts = (const enum AVPixelFormat[]){
233  .long_name = NULL_IF_CONFIG_SMALL("Lossless JPEG"),
234 };