27 #include "libavutil/avassert.h"
28 #include "libavutil/opt.h"
32 #define IOBUF_SIZE 4096
52 int bits_per_pixel,
int pass,
55 int x,
mask, dst_x, j,
b, bpp;
58 static const int masks[] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};
61 switch(bits_per_pixel) {
63 memset(dst, 0, row_size);
65 for(x = 0; x <
width; x++) {
67 if ((mask << j) & 0x80) {
68 b = (src[x >> 3] >> (7 - j)) & 1;
69 dst[dst_x >> 3] |= b << (7 - (dst_x & 7));
75 bpp = bits_per_pixel >> 3;
78 for(x = 0; x <
width; x++) {
80 if ((mask << j) & 0x80) {
93 for(i = 0; i < w; i++) {
94 int a,
b,
c, p, pa, pb, pc;
107 if (pa <= pb && pa <= pc)
122 switch(filter_type) {
124 memcpy(dst, src, size);
128 memcpy(dst, src, bpp);
134 for(i = 0; i < bpp; i++)
135 dst[i] = src[i] - (top[i] >> 1);
137 dst[i] = src[i] - ((src[i-bpp] + top[i]) >> 1);
140 for(i = 0; i < bpp; i++)
141 dst[i] = src[i] - top[i];
156 int cost, bcost = INT_MAX;
157 uint8_t *buf1 = dst, *buf2 = dst + size + 16;
158 for(pred=0; pred<5; pred++) {
162 for(i=0; i<=
size; i++)
163 cost += abs((int8_t)buf1[i]);
183 bytestream_put_be32(f, length);
184 crc =
crc32(0, Z_NULL, 0);
186 crc =
crc32(crc, tagbuf, 4);
189 crc =
crc32(crc, buf, length);
190 memcpy(*f, buf, length);
193 bytestream_put_be32(f, crc);
203 while (s->
zstream.avail_in > 0) {
204 ret = deflate(&s->
zstream, Z_NO_FLUSH);
207 if (s->
zstream.avail_out == 0) {
218 const AVFrame *pict,
int *got_packet)
223 int bits_per_pixel, pass_row_size, enc_row_size;
224 int64_t max_packet_size;
225 int compression_level;
227 uint8_t *crow_base = NULL, *crow_buf, *crow;
228 uint8_t *progressive_buf = NULL;
277 row_size = (avctx->
width * bits_per_pixel + 7) >> 3;
283 Z_DEFAULT_COMPRESSION :
285 ret = deflateInit2(&s->
zstream, compression_level,
286 Z_DEFLATED, 15, 8, Z_DEFAULT_STRATEGY);
290 enc_row_size = deflateBound(&s->
zstream, row_size);
291 max_packet_size = avctx->
height * (int64_t)(enc_row_size +
294 if (max_packet_size > INT_MAX)
306 crow_buf = crow_base + 15;
307 if (is_progressive) {
308 progressive_buf =
av_malloc(row_size + 1);
309 if (!progressive_buf)
312 if (is_progressive) {
324 s->
buf[8] = bit_depth;
325 s->
buf[9] = color_type;
328 s->
buf[12] = is_progressive;
345 int has_alpha,
alpha, i;
350 palette = (uint32_t *)p->
data[1];
352 alpha_ptr = s->
buf + 256 * 3;
354 for(i = 0; i < 256; i++) {
359 *alpha_ptr++ =
alpha;
360 bytestream_put_be24(&ptr, v);
371 if (is_progressive) {
374 for(pass = 0; pass <
NB_PASSES; pass++) {
378 if (pass_row_size > 0) {
380 for(y = 0; y < avctx->
height; y++) {
385 bits_per_pixel, pass,
387 crow =
png_choose_filter(s, crow_buf, progressive_buf, top, pass_row_size, bits_per_pixel>>3);
389 top = progressive_buf;
396 for(y = 0; y < avctx->
height; y++) {
405 ret = deflate(&s->
zstream, Z_FINISH);
406 if (ret == Z_OK || ret == Z_STREAM_END) {
413 if (ret == Z_STREAM_END)
469 s->
dpm = s->
dpi * 10000 / 254;
475 #define OFFSET(x) offsetof(PNGEncContext, x)
476 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
#define PNG_FILTER_VALUE_AVG
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and/or allocate data.
av_cold void ff_dsputil_init(DSPContext *c, AVCodecContext *avctx)
This structure describes decoded (raw) audio or video data.
BYTE int const BYTE int int row_size
static uint8_t * png_choose_filter(PNGEncContext *s, uint8_t *dst, uint8_t *src, uint8_t *top, int size, int bpp)
#define LIBAVUTIL_VERSION_INT
static av_cold int init(AVCodecContext *avctx)
packed RGB 8:8:8, 24bpp, RGBRGB...
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel...
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
#define PNG_COLOR_TYPE_RGB
static const AVClass pngenc_class
#define PNG_COLOR_TYPE_GRAY_ALPHA
#define PNG_COLOR_TYPE_PALETTE
#define PNG_FILTER_VALUE_PAETH
packed RGB 16:16:16, 48bpp, 16R, 16G, 16B, the 2-byte value for each R/G/B component is stored as big...
const char * av_default_item_name(void *ctx)
Return the context name.
#define PNG_COLOR_TYPE_RGB_ALPHA
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
#define CODEC_CAP_INTRA_ONLY
Codec is intra only.
static void sub_png_paeth_prediction(uint8_t *dst, uint8_t *src, uint8_t *top, int w, int bpp)
const char * name
Name of the codec implementation.
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
#define FFSWAP(type, a, b)
const OptionDef options[]
#define PNG_FILTER_VALUE_MIXED
#define FF_COMPRESSION_DEFAULT
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
void(* diff_bytes)(uint8_t *dst, const uint8_t *src1, const uint8_t *src2, int w)
static double alpha(void *priv, double x, double y)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
static void png_get_interlaced_row(uint8_t *dst, int row_size, int bits_per_pixel, int pass, const uint8_t *src, int width)
static const uint16_t mask[17]
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
int dpm
Physical pixel density, in dots per meter, if set.
int ff_png_get_nb_channels(int color_type)
Libavcodec external API header.
AVPixelFormat
Pixel format.
int flags
A combination of AV_PKT_FLAG values.
static unsigned crc32(const uint8_t *data, unsigned size)
uint8_t * bytestream_start
enum AVPictureType pict_type
Picture type of the frame.
#define PNG_FILTER_VALUE_SUB
#define FF_MIN_BUFFER_SIZE
minimum encoding buffer size Used to avoid some checks during header writing.
static void png_write_chunk(uint8_t **f, uint32_t tag, const uint8_t *buf, int length)
#define PNG_COLOR_TYPE_GRAY
int width
picture width / height.
void * av_malloc(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
#define CODEC_FLAG_INTERLACED_DCT
Use interlaced DCT.
int AC3_NAME() encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
static const float pred[4]
#define PNG_FILTER_VALUE_UP
main external API structure.
packed RGBA 16:16:16:16, 64bpp, 16R, 16G, 16B, 16A, the 2-byte value for each R/G/B/A component is st...
const uint8_t ff_png_pass_ymask[NB_PASSES]
void avcodec_get_frame_defaults(AVFrame *frame)
Set the fields of the given AVFrame to default values.
Describe the class of an AVClass context structure.
#define MKTAG(a, b, c, d)
AVFrame * coded_frame
the picture in the bitstream
int dpi
Physical pixel density, in dots per inch, if set.
static av_cold int png_enc_init(AVCodecContext *avctx)
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
common internal api header.
#define CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
#define PNG_FILTER_VALUE_NONE
int prediction_method
prediction method (needed for huffyuv)
void ff_png_zfree(void *opaque, void *ptr)
static void png_filter_row(DSPContext *dsp, uint8_t *dst, int filter_type, uint8_t *src, uint8_t *top, int size, int bpp)
Y , 1bpp, 0 is black, 1 is white, in each byte pixels are ordered from the msb to the lsb...
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
int key_frame
1 -> keyframe, 0-> not
static int png_write_row(PNGEncContext *s, const uint8_t *data, int size)
int ff_png_pass_row_size(int pass, int bits_per_pixel, int width)
#define av_assert0(cond)
assert() equivalent, that is always enabled.
8 bit with PIX_FMT_RGB32 palette
void * ff_png_zalloc(void *opaque, unsigned int items, unsigned int size)
This structure stores compressed data.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.