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 "internal.h"
35 #include "mpegvideo.h"
36 #include "mjpeg.h"
37 #include "mjpegenc.h"
38 
39 
41  const AVFrame *pict, int *got_packet)
42 {
43  MpegEncContext * const s = avctx->priv_data;
44  MJpegContext * const m = s->mjpeg_ctx;
45  const int width= s->width;
46  const int height= s->height;
47  AVFrame * const p = &s->current_picture.f;
48  const int predictor= avctx->prediction_method+1;
49  const int mb_width = (width + s->mjpeg_hsample[0] - 1) / s->mjpeg_hsample[0];
50  const int mb_height = (height + s->mjpeg_vsample[0] - 1) / s->mjpeg_vsample[0];
51  int ret, max_pkt_size = FF_MIN_BUFFER_SIZE;
52 
53  if (avctx->pix_fmt == AV_PIX_FMT_BGRA)
54  max_pkt_size += width * height * 3 * 4;
55  else {
56  max_pkt_size += mb_width * mb_height * 3 * 4
57  * s->mjpeg_hsample[0] * s->mjpeg_vsample[0];
58  }
59 
60  if (!s->edge_emu_buffer &&
61  (ret = ff_mpv_frame_size_alloc(s, pict->linesize[0])) < 0) {
62  av_log(avctx, AV_LOG_ERROR, "failed to allocate context scratch buffers.\n");
63  return ret;
64  }
65 
66  if ((ret = ff_alloc_packet2(avctx, pkt, max_pkt_size)) < 0)
67  return ret;
68 
69  init_put_bits(&s->pb, pkt->data, pkt->size);
70 
71  av_frame_unref(p);
72  ret = av_frame_ref(p, pict);
73  if (ret < 0)
74  return ret;
76  p->key_frame= 1;
77 
79 
80  s->header_bits= put_bits_count(&s->pb);
81 
82  if(avctx->pix_fmt == AV_PIX_FMT_BGR0
83  || avctx->pix_fmt == AV_PIX_FMT_BGRA
84  || avctx->pix_fmt == AV_PIX_FMT_BGR24){
85  int x, y, i;
86  const int linesize= p->linesize[0];
87  uint16_t (*buffer)[4]= (void *) s->rd_scratchpad;
88  int left[3], top[3], topleft[3];
89 
90  for(i=0; i<3; i++){
91  buffer[0][i]= 1 << (9 - 1);
92  }
93 
94  for(y = 0; y < height; y++) {
95  const int modified_predictor= y ? predictor : 1;
96  uint8_t *ptr = p->data[0] + (linesize * y);
97 
98  if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < width*3*4){
99  av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
100  return -1;
101  }
102 
103  for(i=0; i<3; i++){
104  top[i]= left[i]= topleft[i]= buffer[0][i];
105  }
106  for(x = 0; x < width; x++) {
107  if(avctx->pix_fmt == AV_PIX_FMT_BGR24){
108  buffer[x][1] = ptr[3*x+0] - ptr[3*x+1] + 0x100;
109  buffer[x][2] = ptr[3*x+2] - ptr[3*x+1] + 0x100;
110  buffer[x][0] = (ptr[3*x+0] + 2*ptr[3*x+1] + ptr[3*x+2])>>2;
111  }else{
112  buffer[x][1] = ptr[4*x+0] - ptr[4*x+1] + 0x100;
113  buffer[x][2] = ptr[4*x+2] - ptr[4*x+1] + 0x100;
114  buffer[x][0] = (ptr[4*x+0] + 2*ptr[4*x+1] + ptr[4*x+2])>>2;
115  }
116 
117  for(i=0;i<3;i++) {
118  int pred, diff;
119 
120  PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
121 
122  topleft[i]= top[i];
123  top[i]= buffer[x+1][i];
124 
125  left[i]= buffer[x][i];
126 
127  diff= ((left[i] - pred + 0x100)&0x1FF) - 0x100;
128 
129  if(i==0)
131  else
133  }
134  }
135  }
136  }else{
137  int mb_x, mb_y, i;
138 
139  for(mb_y = 0; mb_y < mb_height; mb_y++) {
140  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]){
141  av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
142  return -1;
143  }
144  for(mb_x = 0; mb_x < mb_width; mb_x++) {
145  if(mb_x==0 || mb_y==0){
146  for(i=0;i<3;i++) {
147  uint8_t *ptr;
148  int x, y, h, v, linesize;
149  h = s->mjpeg_hsample[i];
150  v = s->mjpeg_vsample[i];
151  linesize= p->linesize[i];
152 
153  for(y=0; y<v; y++){
154  for(x=0; x<h; x++){
155  int pred;
156 
157  ptr = p->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
158  if(y==0 && mb_y==0){
159  if(x==0 && mb_x==0){
160  pred= 128;
161  }else{
162  pred= ptr[-1];
163  }
164  }else{
165  if(x==0 && mb_x==0){
166  pred= ptr[-linesize];
167  }else{
168  PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
169  }
170  }
171 
172  if(i==0)
173  ff_mjpeg_encode_dc(s, *ptr - pred, m->huff_size_dc_luminance, m->huff_code_dc_luminance); //FIXME ugly
174  else
176  }
177  }
178  }
179  }else{
180  for(i=0;i<3;i++) {
181  uint8_t *ptr;
182  int x, y, h, v, linesize;
183  h = s->mjpeg_hsample[i];
184  v = s->mjpeg_vsample[i];
185  linesize= p->linesize[i];
186 
187  for(y=0; y<v; y++){
188  for(x=0; x<h; x++){
189  int pred;
190 
191  ptr = p->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
192  PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
193 
194  if(i==0)
195  ff_mjpeg_encode_dc(s, *ptr - pred, m->huff_size_dc_luminance, m->huff_code_dc_luminance); //FIXME ugly
196  else
198  }
199  }
200  }
201  }
202  }
203  }
204  }
205 
206  emms_c();
207  av_assert0(s->esc_pos == s->header_bits >> 3);
210  s->picture_number++;
211 
212  flush_put_bits(&s->pb);
213  pkt->size = put_bits_ptr(&s->pb) - s->pb.buf;
214  pkt->flags |= AV_PKT_FLAG_KEY;
215  *got_packet = 1;
216 
217  return 0;
218 // return (put_bits_count(&f->pb)+7)/8;
219 }
220 
221 
222 AVCodec ff_ljpeg_encoder = { //FIXME avoid MPV_* lossless JPEG should not need them
223  .name = "ljpeg",
224  .type = AVMEDIA_TYPE_VIDEO,
225  .id = AV_CODEC_ID_LJPEG,
226  .priv_data_size = sizeof(MpegEncContext),
228  .encode2 = encode_picture_lossless,
230  .pix_fmts = (const enum AVPixelFormat[]){
235  .long_name = NULL_IF_CONFIG_SMALL("Lossless JPEG"),
236 };
AVCodec ff_ljpeg_encoder
Definition: ljpegenc.c:222
float v
int picture_number
Definition: mpegvideo.h:275
const char * s
Definition: avisynth_c.h:668
This structure describes decoded (raw) audio or video data.
Definition: frame.h:76
struct MpegEncContext MpegEncContext
MpegEncContext.
uint8_t * rd_scratchpad
scratchpad for rate distortion mb decision
Definition: mpegvideo.h:365
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:73
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
MJPEG encoder.
static int encode_picture_lossless(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pict, int *got_packet)
Definition: ljpegenc.c:40
int ff_MPV_encode_end(AVCodecContext *avctx)
int mjpeg_hsample[3]
horizontal sampling factors, default = {2, 1, 1}
Definition: mpegvideo.h:627
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
mpegvideo header.
MJPEG encoder and decoder.
uint16_t huff_code_dc_chrominance[12]
Definition: mjpegenc.h:44
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
uint8_t
window constants for m
static AVPacket pkt
Definition: demuxing.c:56
#define emms_c()
Picture current_picture
copy of the current picture structure.
Definition: mpegvideo.h:343
uint8_t * data
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV422P and setting color_...
Definition: pixfmt.h:81
uint8_t huff_size_dc_chrominance[12]
Definition: mjpegenc.h:43
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
int ff_MPV_encode_init(AVCodecContext *avctx)
#define PREDICT(ret, topleft, top, left, predictor)
Definition: mjpeg.h:128
static void predictor(uint8_t *src, int size)
Definition: exr.c:188
Discrete Time axis x
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:199
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:99
uint8_t * edge_emu_buffer
temporary buffer for if MVs point to out-of-frame data
Definition: mpegvideo.h:364
uint8_t * buf
Definition: put_bits.h:44
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
const char * name
Name of the codec implementation.
external API header
int flags
A combination of AV_PKT_FLAG values.
static int put_bits_count(PutBitContext *s)
Definition: put_bits.h:73
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:72
void ff_mjpeg_encode_picture_header(MpegEncContext *s)
Definition: mjpegenc.c:204
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:144
#define FF_MIN_BUFFER_SIZE
minimum encoding buffer size Used to avoid some checks during header writing.
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV420P and setting color_...
Definition: pixfmt.h:80
ret
Definition: avfilter.c:821
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:71
int ff_mpv_frame_size_alloc(MpegEncContext *s, int linesize)
Definition: mpegvideo.c:204
#define diff(a, as, b, bs)
Definition: vf_phase.c:80
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int size)
Check AVPacket size and/or allocate data.
static const float pred[4]
Definition: siprdata.h:259
struct MJpegContext * mjpeg_ctx
Definition: mpegvideo.h:625
int mjpeg_vsample[3]
vertical sampling factors, default = {2, 1, 1}
Definition: mpegvideo.h:626
static int width
Definition: tests/utils.c:158
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:101
main external API structure.
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:375
int height
picture size. must be a multiple of 16
Definition: mpegvideo.h:245
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
void ff_mjpeg_encode_picture_trailer(MpegEncContext *s)
Definition: mjpegenc.c:368
uint8_t * buf_end
Definition: put_bits.h:44
BYTE int const BYTE int int int height
Definition: avisynth_c.h:713
synthesis window for stochastic i
packed BGR 8:8:8, 32bpp, BGR0BGR0...
Definition: pixfmt.h:217
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:330
int av_frame_ref(AVFrame *dst, AVFrame *src)
Setup a new reference to the data described by an given frame.
Definition: frame.c:228
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:87
MpegEncContext.
Definition: mpegvideo.h:241
struct AVCodecContext * avctx
Definition: mpegvideo.h:243
uint16_t huff_code_dc_luminance[12]
Definition: mjpegenc.h:42
PutBitContext pb
bit output
Definition: mpegvideo.h:314
uint8_t huff_size_dc_luminance[12]
Definition: mjpegenc.h:41
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:68
common internal api header.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:81
void ff_mjpeg_encode_stuffing(MpegEncContext *s)
Definition: mjpegenc.c:349
int prediction_method
prediction method (needed for huffyuv)
the buffer and buffer reference mechanism is intended to as much as expensive copies of that data while still allowing the filters to produce correct results The data is stored in buffers represented by AVFilterBuffer structures They must not be accessed but through references stored in AVFilterBufferRef structures Several references can point to the same buffer
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV444P and setting color_...
Definition: pixfmt.h:82
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:54
function y
Definition: D.m:1
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:139
void ff_mjpeg_encode_dc(MpegEncContext *s, int val, uint8_t *huff_size, uint16_t *huff_code)
Definition: mjpegenc.c:377
struct AVFrame f
Definition: mpegvideo.h:98
AVPixelFormat
Pixel format.
Definition: pixfmt.h:66
This structure stores compressed data.