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
36
44
50
53
58
62
65 {
69 const int linesize = frame->
linesize[0];
71 int left[4], top[4], topleft[4];
72 int x, y, i;
73
74 #if FF_API_PRIVATE_OPT
79 #endif
80
81 for (i = 0; i < 4; i++)
82 buffer[0][i] = 1 << (9 - 1);
83
84 for (y = 0; y <
height; y++) {
85 const int modified_predictor = y ? s->
pred : 1;
87
90 return -1;
91 }
92
93 for (i = 0; i < 4; i++)
94 top[i]= left[i]= topleft[i]=
buffer[0][i];
95
96 for (x = 0; x <
width; x++) {
98 buffer[x][1] = ptr[3 * x + 0] - ptr[3 * x + 1] + 0x100;
99 buffer[x][2] = ptr[3 * x + 2] - ptr[3 * x + 1] + 0x100;
100 buffer[x][0] = (ptr[3 * x + 0] + 2 * ptr[3 * x + 1] + ptr[3 * x + 2]) >> 2;
101 }else{
102 buffer[x][1] = ptr[4 * x + 0] - ptr[4 * x + 1] + 0x100;
103 buffer[x][2] = ptr[4 * x + 2] - ptr[4 * x + 1] + 0x100;
104 buffer[x][0] = (ptr[4 * x + 0] + 2 * ptr[4 * x + 1] + ptr[4 * x + 2]) >> 2;
106 buffer[x][3] = ptr[4 * x + 3];
107 }
108
111
112 PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
113
114 topleft[i] = top[i];
116
118
119 diff = ((left[i] - pred + 0x100) & 0x1FF) - 0x100;
120
121 if (i == 0 || i == 3)
123 else
125 }
126 }
127 }
128
129 return 0;
130 }
131
134 int mb_x, int mb_y)
135 {
136 int i;
137
138 if (mb_x == 0 || mb_y == 0) {
139 for (i = 0; i < 3; i++) {
141 int x, y,
h, v, linesize;
145
146 for (y = 0; y < v; y++) {
147 for (x = 0; x <
h; x++) {
149
150 ptr = frame->
data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x);
//FIXME optimize this crap
151 if (y == 0 && mb_y == 0) {
152 if (x == 0 && mb_x == 0)
153 pred = 128;
154 else
155 pred = ptr[-1];
156 } else {
157 if (x == 0 && mb_x == 0) {
158 pred = ptr[-linesize];
159 } else {
160 PREDICT(pred, ptr[-linesize - 1], ptr[-linesize],
161 ptr[-1], predictor);
162 }
163 }
164
165 if (i == 0)
167 else
169 }
170 }
171 }
172 } else {
173 for (i = 0; i < 3; i++) {
175 int x, y,
h, v, linesize;
179
180 for (y = 0; y < v; y++) {
181 for (x = 0; x <
h; x++) {
183
184 ptr = frame->
data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x);
//FIXME optimize this crap
185 PREDICT(pred, ptr[-linesize - 1], ptr[-linesize], ptr[-1], predictor);
186
187 if (i == 0)
189 else
191 }
192 }
193 }
194 }
195 }
196
199 {
203 int mb_x, mb_y;
204
205 #if FF_API_PRIVATE_OPT
210 #endif
211
212 for (mb_y = 0; mb_y < mb_height; mb_y++) {
216 return -1;
217 }
218
219 for (mb_x = 0; mb_x < mb_width; mb_x++)
221 }
222
223 return 0;
224 }
225
227 const AVFrame *pict,
int *got_packet)
228 {
234 const int mb_height = (height + s->
vsample[0] - 1) / s->
vsample[0];
236 int ret, header_bits;
237
240 max_pkt_size += width * height * 3 * 4;
242 max_pkt_size += width * height * 4 * 4;
243 else {
244 max_pkt_size += mb_width * mb_height * 3 * 4
246 }
247
249 return ret;
250
252
255
257
262 else
264 if (ret < 0)
265 return ret;
266
267 emms_c();
268
271
275 *got_packet = 1;
276
277 return 0;
278 }
279
281 {
283
285
286 return 0;
287 }
288
290 {
292
299 "Limited range YUV is non-standard, set strict_std_compliance to "
300 "at least unofficial to use it.\n");
302 }
303
304 #if FF_API_CODED_FRAME
309 #endif
310
314
318
320
329
330 return 0;
334 }
335
336 #define OFFSET(x) offsetof(LJpegEncContext, x)
337 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
343
345 };
346
352 };
353
360 .priv_class = &ljpeg_class,
370 };
This structure describes decoded (raw) audio or video data.
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
#define LIBAVUTIL_VERSION_INT
memory handling functions
static av_cold int init(AVCodecContext *avctx)
enum AVColorRange color_range
MPEG vs JPEG YUV range.
void ff_mjpeg_init_hvsample(AVCodecContext *avctx, int hsample[4], int vsample[4])
static const AVClass ljpeg_class
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
MJPEG encoder and decoder.
#define AV_CODEC_CAP_INTRA_ONLY
Codec is intra only.
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
uint8_t huff_size_dc_chrominance[12]
void ff_mjpeg_build_huffman_codes(uint8_t *huff_size, uint16_t *huff_code, const uint8_t *bits_table, const uint8_t *val_table)
const uint8_t avpriv_mjpeg_bits_dc_luminance[17]
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV422P and setting col...
#define AV_INPUT_BUFFER_MIN_SIZE
minimum encoding buffer size Used to avoid some checks during header writing.
#define FF_COMPLIANCE_UNOFFICIAL
Allow unofficial extensions.
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
#define PREDICT(ret, topleft, top, left, predictor)
static void predictor(uint8_t *src, int size)
int width
width and height of the video frame
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
static uint8_t * put_bits_ptr(PutBitContext *s)
Return the pointer to the byte where the bitstream writer will put the next bit.
const uint8_t avpriv_mjpeg_bits_dc_chrominance[17]
#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...
const char * name
Name of the codec implementation.
void ff_mjpeg_encode_picture_trailer(PutBitContext *pb, int header_bits)
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
int flags
A combination of AV_PKT_FLAG values.
reference-counted frame API
static int put_bits_count(PutBitContext *s)
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
static int ljpeg_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pict, int *got_packet)
enum AVPictureType pict_type
Picture type of the frame.
static av_cold int ljpeg_encode_init(AVCodecContext *avctx)
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
int width
picture width / height.
static av_cold int ljpeg_encode_close(AVCodecContext *avctx)
uint16_t huff_code_dc_chrominance[12]
void ff_mjpeg_escape_FF(PutBitContext *pb, int start)
uint8_t idct_permutation[64]
IDCT input permutation.
packed RGB 8:8:8, 24bpp, BGRBGR...
static const float pred[4]
void ff_mjpeg_encode_dc(PutBitContext *pb, int val, uint8_t *huff_size, uint16_t *huff_code)
Libavcodec external API header.
attribute_deprecated int prediction_method
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
main external API structure.
const uint8_t avpriv_mjpeg_val_dc[12]
BYTE int const BYTE int int int height
Describe the class of an AVClass context structure.
const uint8_t ff_zigzag_direct[64]
packed BGR 8:8:8, 32bpp, BGRXBGRX... X=unused/undefined
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
void ff_mjpeg_encode_picture_header(AVCodecContext *avctx, PutBitContext *pb, ScanTable *intra_scantable, int pred, uint16_t luma_intra_matrix[64], uint16_t chroma_intra_matrix[64])
static enum AVPixelFormat pix_fmts[]
static int ljpeg_encode_yuv(AVCodecContext *avctx, PutBitContext *pb, const AVFrame *frame)
static int ljpeg_encode_bgr(AVCodecContext *avctx, PutBitContext *pb, const AVFrame *frame)
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
the normal 219*2^(n-8) "MPEG" YUV ranges
uint16_t huff_code_dc_luminance[12]
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
#define FF_DISABLE_DEPRECATION_WARNINGS
common internal api header.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
uint8_t huff_size_dc_luminance[12]
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
attribute_deprecated AVFrame * coded_frame
the picture in the bitstream
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
static av_always_inline int diff(const uint32_t a, const uint32_t b)
av_cold void ff_init_scantable(uint8_t *permutation, ScanTable *st, const uint8_t *src_scantable)
static void ljpeg_encode_yuv_mb(LJpegEncContext *s, PutBitContext *pb, const AVFrame *frame, int predictor, int mb_x, int mb_y)
av_cold void ff_idctdsp_init(IDCTDSPContext *c, AVCodecContext *avctx)
#define FF_ENABLE_DEPRECATION_WARNINGS
int key_frame
1 -> keyframe, 0-> not
#define av_malloc_array(a, b)
AVPixelFormat
Pixel format.
This structure stores compressed data.
int strict_std_compliance
strictly follow the standard (MPEG4, ...).
static const AVOption options[]