00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00029 #include "avcodec.h"
00030 #include "bytestream.h"
00031 #include "libavutil/imgutils.h"
00032
00033 typedef struct PCXContext {
00034 AVFrame picture;
00035 } PCXContext;
00036
00037 static const uint32_t monoblack_pal[16] = { 0x000000, 0xFFFFFF };
00038
00039 static av_cold int pcx_encode_init(AVCodecContext *avctx)
00040 {
00041 PCXContext *s = avctx->priv_data;
00042
00043 avcodec_get_frame_defaults(&s->picture);
00044 avctx->coded_frame = &s->picture;
00045
00046 return 0;
00047 }
00048
00059 static int pcx_rle_encode( uint8_t *dst, int dst_size,
00060 const uint8_t *src, int src_plane_size, int nplanes)
00061 {
00062 int p;
00063 const uint8_t *dst_start = dst;
00064
00065
00066 if (dst_size < 2LL * src_plane_size * nplanes || src_plane_size <= 0)
00067 return -1;
00068
00069 for (p = 0; p < nplanes; p++) {
00070 int count = 1;
00071 const uint8_t *src_plane = src + p;
00072 const uint8_t *src_plane_end = src_plane + src_plane_size * nplanes;
00073 uint8_t prev = *src_plane;
00074 src_plane += nplanes;
00075
00076 for (; ; src_plane += nplanes) {
00077 if (src_plane < src_plane_end && *src_plane == prev && count < 0x3F) {
00078
00079 ++count;
00080 } else {
00081
00082 if (count != 1 || prev >= 0xC0)
00083 *dst++ = 0xC0 | count;
00084 *dst++ = prev;
00085
00086 if (src_plane == src_plane_end)
00087 break;
00088
00089
00090 count = 1;
00091 prev = *src_plane;
00092 }
00093 }
00094 }
00095
00096 return dst - dst_start;
00097 }
00098
00099 static int pcx_encode_frame(AVCodecContext *avctx,
00100 unsigned char *buf, int buf_size, void *data)
00101 {
00102 PCXContext *s = avctx->priv_data;
00103 AVFrame *const pict = &s->picture;
00104 const uint8_t *buf_start = buf;
00105 const uint8_t *buf_end = buf + buf_size;
00106
00107 int bpp, nplanes, i, y, line_bytes, written;
00108 const uint32_t *pal = NULL;
00109 uint32_t palette256[256];
00110 const uint8_t *src;
00111
00112 *pict = *(AVFrame *)data;
00113 pict->pict_type = AV_PICTURE_TYPE_I;
00114 pict->key_frame = 1;
00115
00116 if (avctx->width > 65535 || avctx->height > 65535) {
00117 av_log(avctx, AV_LOG_ERROR, "image dimensions do not fit in 16 bits\n");
00118 return -1;
00119 }
00120
00121 switch (avctx->pix_fmt) {
00122 case PIX_FMT_RGB24:
00123 bpp = 8;
00124 nplanes = 3;
00125 break;
00126 case PIX_FMT_RGB8:
00127 case PIX_FMT_BGR8:
00128 case PIX_FMT_RGB4_BYTE:
00129 case PIX_FMT_BGR4_BYTE:
00130 case PIX_FMT_GRAY8:
00131 bpp = 8;
00132 nplanes = 1;
00133 ff_set_systematic_pal2(palette256, avctx->pix_fmt);
00134 pal = palette256;
00135 break;
00136 case PIX_FMT_PAL8:
00137 bpp = 8;
00138 nplanes = 1;
00139 pal = (uint32_t *)pict->data[1];
00140 break;
00141 case PIX_FMT_MONOBLACK:
00142 bpp = 1;
00143 nplanes = 1;
00144 pal = monoblack_pal;
00145 break;
00146 default:
00147 av_log(avctx, AV_LOG_ERROR, "unsupported pixfmt\n");
00148 return -1;
00149 }
00150
00151 line_bytes = (avctx->width * bpp + 7) >> 3;
00152 line_bytes = (line_bytes + 1) & ~1;
00153
00154 bytestream_put_byte(&buf, 10);
00155 bytestream_put_byte(&buf, 5);
00156 bytestream_put_byte(&buf, 1);
00157 bytestream_put_byte(&buf, bpp);
00158 bytestream_put_le16(&buf, 0);
00159 bytestream_put_le16(&buf, 0);
00160 bytestream_put_le16(&buf, avctx->width - 1);
00161 bytestream_put_le16(&buf, avctx->height - 1);
00162 bytestream_put_le16(&buf, 0);
00163 bytestream_put_le16(&buf, 0);
00164 for (i = 0; i < 16; i++)
00165 bytestream_put_be24(&buf, pal ? pal[i] : 0);
00166 bytestream_put_byte(&buf, 0);
00167 bytestream_put_byte(&buf, nplanes);
00168 bytestream_put_le16(&buf, line_bytes);
00169
00170 while (buf - buf_start < 128)
00171 *buf++= 0;
00172
00173 src = pict->data[0];
00174
00175 for (y = 0; y < avctx->height; y++) {
00176 if ((written = pcx_rle_encode(buf, buf_end - buf,
00177 src, line_bytes, nplanes)) < 0) {
00178 av_log(avctx, AV_LOG_ERROR, "buffer too small\n");
00179 return -1;
00180 }
00181 buf += written;
00182 src += pict->linesize[0];
00183 }
00184
00185 if (nplanes == 1 && bpp == 8) {
00186 if (buf_end - buf < 257) {
00187 av_log(avctx, AV_LOG_ERROR, "buffer too small\n");
00188 return -1;
00189 }
00190 bytestream_put_byte(&buf, 12);
00191 for (i = 0; i < 256; i++) {
00192 bytestream_put_be24(&buf, pal[i]);
00193 }
00194 }
00195
00196 return buf - buf_start;
00197 }
00198
00199 AVCodec ff_pcx_encoder = {
00200 .name = "pcx",
00201 .type = AVMEDIA_TYPE_VIDEO,
00202 .id = CODEC_ID_PCX,
00203 .priv_data_size = sizeof(PCXContext),
00204 .init = pcx_encode_init,
00205 .encode = pcx_encode_frame,
00206 .pix_fmts = (const enum PixelFormat[]){
00207 PIX_FMT_RGB24,
00208 PIX_FMT_RGB8, PIX_FMT_BGR8, PIX_FMT_RGB4_BYTE, PIX_FMT_BGR4_BYTE, PIX_FMT_GRAY8, PIX_FMT_PAL8,
00209 PIX_FMT_MONOBLACK,
00210 PIX_FMT_NONE},
00211 .long_name = NULL_IF_CONFIG_SMALL("PC Paintbrush PCX image"),
00212 };