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 "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  if ((ret = ff_alloc_packet(pkt, max_pkt_size)) < 0) {
61  av_log(avctx, AV_LOG_ERROR, "Error getting output packet of size %d.\n", max_pkt_size);
62  return ret;
63  }
64 
65  init_put_bits(&s->pb, pkt->data, pkt->size);
66 
67  *p = *pict;
69  p->key_frame= 1;
70 
72 
73  s->header_bits= put_bits_count(&s->pb);
74 
75  if(avctx->pix_fmt == AV_PIX_FMT_BGRA){
76  int x, y, i;
77  const int linesize= p->linesize[0];
78  uint16_t (*buffer)[4]= (void *) s->rd_scratchpad;
79  int left[3], top[3], topleft[3];
80 
81  for(i=0; i<3; i++){
82  buffer[0][i]= 1 << (9 - 1);
83  }
84 
85  for(y = 0; y < height; y++) {
86  const int modified_predictor= y ? predictor : 1;
87  uint8_t *ptr = p->data[0] + (linesize * y);
88 
89  if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < width*3*4){
90  av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
91  return -1;
92  }
93 
94  for(i=0; i<3; i++){
95  top[i]= left[i]= topleft[i]= buffer[0][i];
96  }
97  for(x = 0; x < width; x++) {
98  buffer[x][1] = ptr[4*x+0] - ptr[4*x+1] + 0x100;
99  buffer[x][2] = ptr[4*x+2] - ptr[4*x+1] + 0x100;
100  buffer[x][0] = (ptr[4*x+0] + 2*ptr[4*x+1] + ptr[4*x+2])>>2;
101 
102  for(i=0;i<3;i++) {
103  int pred, diff;
104 
105  PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
106 
107  topleft[i]= top[i];
108  top[i]= buffer[x+1][i];
109 
110  left[i]= buffer[x][i];
111 
112  diff= ((left[i] - pred + 0x100)&0x1FF) - 0x100;
113 
114  if(i==0)
116  else
118  }
119  }
120  }
121  }else{
122  int mb_x, mb_y, i;
123 
124  for(mb_y = 0; mb_y < mb_height; mb_y++) {
125  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]){
126  av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
127  return -1;
128  }
129  for(mb_x = 0; mb_x < mb_width; mb_x++) {
130  if(mb_x==0 || mb_y==0){
131  for(i=0;i<3;i++) {
132  uint8_t *ptr;
133  int x, y, h, v, linesize;
134  h = s->mjpeg_hsample[i];
135  v = s->mjpeg_vsample[i];
136  linesize= p->linesize[i];
137 
138  for(y=0; y<v; y++){
139  for(x=0; x<h; x++){
140  int pred;
141 
142  ptr = p->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
143  if(y==0 && mb_y==0){
144  if(x==0 && mb_x==0){
145  pred= 128;
146  }else{
147  pred= ptr[-1];
148  }
149  }else{
150  if(x==0 && mb_x==0){
151  pred= ptr[-linesize];
152  }else{
153  PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
154  }
155  }
156 
157  if(i==0)
158  ff_mjpeg_encode_dc(s, *ptr - pred, m->huff_size_dc_luminance, m->huff_code_dc_luminance); //FIXME ugly
159  else
161  }
162  }
163  }
164  }else{
165  for(i=0;i<3;i++) {
166  uint8_t *ptr;
167  int x, y, h, v, linesize;
168  h = s->mjpeg_hsample[i];
169  v = s->mjpeg_vsample[i];
170  linesize= p->linesize[i];
171 
172  for(y=0; y<v; y++){
173  for(x=0; x<h; x++){
174  int pred;
175 
176  ptr = p->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
177  PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
178 
179  if(i==0)
180  ff_mjpeg_encode_dc(s, *ptr - pred, m->huff_size_dc_luminance, m->huff_code_dc_luminance); //FIXME ugly
181  else
183  }
184  }
185  }
186  }
187  }
188  }
189  }
190 
191  emms_c();
192 
194  s->picture_number++;
195 
196  flush_put_bits(&s->pb);
197  pkt->size = put_bits_ptr(&s->pb) - s->pb.buf;
198  pkt->flags |= AV_PKT_FLAG_KEY;
199  *got_packet = 1;
200 
201  return 0;
202 // return (put_bits_count(&f->pb)+7)/8;
203 }
204 
205 
206 AVCodec ff_ljpeg_encoder = { //FIXME avoid MPV_* lossless JPEG should not need them
207  .name = "ljpeg",
208  .type = AVMEDIA_TYPE_VIDEO,
209  .id = AV_CODEC_ID_LJPEG,
210  .priv_data_size = sizeof(MpegEncContext),
212  .encode2 = encode_picture_lossless,
214  .long_name = NULL_IF_CONFIG_SMALL("Lossless JPEG"),
215 };
AVCodec ff_ljpeg_encoder
Definition: ljpegenc.c:206
int picture_number
Definition: mpegvideo.h:245
This structure describes decoded (raw) audio or video data.
Definition: avcodec.h:989
struct MpegEncContext MpegEncContext
MpegEncContext.
uint8_t * rd_scratchpad
scratchpad for rate distortion mb decision
Definition: mpegvideo.h:338
MJPEG encoder.
static int encode_picture_lossless(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pict, int *got_packet)
Definition: ljpegenc.c:41
int ff_MPV_encode_end(AVCodecContext *avctx)
int mjpeg_hsample[3]
horizontal sampling factors, default = {2, 1, 1}
Definition: mpegvideo.h:605
int size
Definition: avcodec.h:916
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1533
mpegvideo header.
AVCodec.
Definition: avcodec.h:2960
MJPEG encoder and decoder.
uint16_t huff_code_dc_chrominance[12]
Definition: mjpegenc.h:43
uint8_t
#define emms_c()
Definition: internal.h:145
Picture current_picture
copy of the current picture structure.
Definition: mpegvideo.h:313
uint8_t * data
Definition: avcodec.h:915
uint8_t huff_size_dc_chrominance[12]
Definition: mjpegenc.h:42
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:335
int ff_MPV_encode_init(AVCodecContext *avctx)
#define PREDICT(ret, topleft, top, left, predictor)
Definition: mjpeg.h:128
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:201
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:88
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:96
uint8_t * buf
Definition: put_bits.h:42
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
const char * name
Name of the codec implementation.
Definition: avcodec.h:2967
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:921
static int put_bits_count(PutBitContext *s)
Definition: put_bits.h:70
void ff_mjpeg_encode_picture_header(MpegEncContext *s)
Definition: mjpegenc.c:197
enum AVPictureType pict_type
Picture type of the frame, see ?_TYPE below.
Definition: avcodec.h:1065
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:880
static const float pred[4]
Definition: siprdata.h:259
struct MJpegContext * mjpeg_ctx
Definition: mpegvideo.h:603
int mjpeg_vsample[3]
vertical sampling factors, default = {2, 1, 1}
Definition: mpegvideo.h:604
static int width
Definition: utils.c:156
external API header
int linesize[AV_NUM_DATA_POINTERS]
Size, in bytes, of the data for each picture/channel plane.
Definition: avcodec.h:1008
main external API structure.
Definition: avcodec.h:1339
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:326
int height
picture size. must be a multiple of 16
Definition: mpegvideo.h:215
void ff_mjpeg_encode_picture_trailer(MpegEncContext *s)
Definition: mjpegenc.c:338
uint8_t * buf_end
Definition: put_bits.h:42
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: avcodec.h:997
int height
Definition: gxfenc.c:72
MpegEncContext.
Definition: mpegvideo.h:211
struct AVCodecContext * avctx
Definition: mpegvideo.h:213
uint16_t huff_code_dc_luminance[12]
Definition: mjpegenc.h:41
PutBitContext pb
bit output
Definition: mpegvideo.h:284
uint8_t huff_size_dc_luminance[12]
Definition: mjpegenc.h:40
common internal api header.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:86
int prediction_method
prediction method (needed for huffyuv)
Definition: avcodec.h:1705
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:52
DSP utils.
void * priv_data
Definition: avcodec.h:1382
int key_frame
1 -> keyframe, 0-> not
Definition: avcodec.h:1058
void ff_mjpeg_encode_dc(MpegEncContext *s, int val, uint8_t *huff_size, uint16_t *huff_code)
Definition: mjpegenc.c:350
struct AVFrame f
Definition: mpegvideo.h:95
This structure stores compressed data.
Definition: avcodec.h:898