]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/pcxenc.c
Hap decoder and encoder
[ffmpeg] / libavcodec / pcxenc.c
index c8259cd8b24e3e7c6c6199f9f8291066857e3ba4..8553fe166e74524c152ce294117d2c066f66d8f9 100644 (file)
 #include "bytestream.h"
 #include "internal.h"
 
-typedef struct PCXContext {
-    AVFrame picture;
-} PCXContext;
-
 static const uint32_t monoblack_pal[16] = { 0x000000, 0xFFFFFF };
 
 static av_cold int pcx_encode_init(AVCodecContext *avctx)
 {
-    PCXContext *s = avctx->priv_data;
+    avctx->coded_frame = av_frame_alloc();
+    if (!avctx->coded_frame)
+        return AVERROR(ENOMEM);
+
+    avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
+    avctx->coded_frame->key_frame = 1;
 
-    avcodec_get_frame_defaults(&s->picture);
-    avctx->coded_frame = &s->picture;
+    return 0;
+}
 
+static av_cold int pcx_encode_close(AVCodecContext *avctx)
+{
+    av_frame_free(&avctx->coded_frame);
     return 0;
 }
 
@@ -64,7 +68,7 @@ static int pcx_rle_encode(      uint8_t *dst, int dst_size,
 
     // check worst-case upper bound on dst_size
     if (dst_size < 2LL * src_plane_size * nplanes || src_plane_size <= 0)
-        return -1;
+        return AVERROR(EINVAL);
 
     for (p = 0; p < nplanes; p++) {
         int count = 1;
@@ -99,8 +103,6 @@ static int pcx_rle_encode(      uint8_t *dst, int dst_size,
 static int pcx_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
                             const AVFrame *frame, int *got_packet)
 {
-    PCXContext *s = avctx->priv_data;
-    AVFrame *const pict = &s->picture;
     const uint8_t *buf_end;
     uint8_t *buf;
 
@@ -108,38 +110,34 @@ static int pcx_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
     const uint32_t *pal = NULL;
     const uint8_t *src;
 
-    *pict = *frame;
-    pict->pict_type = AV_PICTURE_TYPE_I;
-    pict->key_frame = 1;
-
     if (avctx->width > 65535 || avctx->height > 65535) {
         av_log(avctx, AV_LOG_ERROR, "image dimensions do not fit in 16 bits\n");
-        return -1;
+        return AVERROR(EINVAL);
     }
 
     switch (avctx->pix_fmt) {
-    case PIX_FMT_RGB24:
+    case AV_PIX_FMT_RGB24:
         bpp = 8;
         nplanes = 3;
         break;
-    case PIX_FMT_RGB8:
-    case PIX_FMT_BGR8:
-    case PIX_FMT_RGB4_BYTE:
-    case PIX_FMT_BGR4_BYTE:
-    case PIX_FMT_GRAY8:
-    case PIX_FMT_PAL8:
+    case AV_PIX_FMT_RGB8:
+    case AV_PIX_FMT_BGR8:
+    case AV_PIX_FMT_RGB4_BYTE:
+    case AV_PIX_FMT_BGR4_BYTE:
+    case AV_PIX_FMT_GRAY8:
+    case AV_PIX_FMT_PAL8:
         bpp = 8;
         nplanes = 1;
-        pal = (uint32_t *)pict->data[1];
+        pal = (uint32_t *)frame->data[1];
         break;
-    case PIX_FMT_MONOBLACK:
+    case AV_PIX_FMT_MONOBLACK:
         bpp = 1;
         nplanes = 1;
         pal = monoblack_pal;
         break;
     default:
         av_log(avctx, AV_LOG_ERROR, "unsupported pixfmt\n");
-        return -1;
+        return AVERROR(EINVAL);
     }
 
     line_bytes = (avctx->width * bpp + 7) >> 3;
@@ -172,22 +170,22 @@ static int pcx_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
     while (buf - pkt->data < 128)
         *buf++= 0;
 
-    src = pict->data[0];
+    src = frame->data[0];
 
     for (y = 0; y < avctx->height; y++) {
         if ((written = pcx_rle_encode(buf, buf_end - buf,
                                       src, line_bytes, nplanes)) < 0) {
             av_log(avctx, AV_LOG_ERROR, "buffer too small\n");
-            return -1;
+            return AVERROR_BUG;
         }
         buf += written;
-        src += pict->linesize[0];
+        src += frame->linesize[0];
     }
 
     if (nplanes == 1 && bpp == 8) {
         if (buf_end - buf < 257) {
             av_log(avctx, AV_LOG_ERROR, "buffer too small\n");
-            return -1;
+            return AVERROR_BUG;
         }
         bytestream_put_byte(&buf, 12);
         for (i = 0; i < 256; i++) {
@@ -204,15 +202,17 @@ static int pcx_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
 
 AVCodec ff_pcx_encoder = {
     .name           = "pcx",
+    .long_name      = NULL_IF_CONFIG_SMALL("PC Paintbrush PCX image"),
     .type           = AVMEDIA_TYPE_VIDEO,
-    .id             = CODEC_ID_PCX,
-    .priv_data_size = sizeof(PCXContext),
+    .id             = AV_CODEC_ID_PCX,
     .init           = pcx_encode_init,
+    .close          = pcx_encode_close,
     .encode2        = pcx_encode_frame,
-    .pix_fmts = (const enum PixelFormat[]){
-        PIX_FMT_RGB24,
-        PIX_FMT_RGB8, PIX_FMT_BGR8, PIX_FMT_RGB4_BYTE, PIX_FMT_BGR4_BYTE, PIX_FMT_GRAY8, PIX_FMT_PAL8,
-        PIX_FMT_MONOBLACK,
-        PIX_FMT_NONE},
-    .long_name = NULL_IF_CONFIG_SMALL("PC Paintbrush PCX image"),
+    .pix_fmts       = (const enum AVPixelFormat[]){
+        AV_PIX_FMT_RGB24,
+        AV_PIX_FMT_RGB8, AV_PIX_FMT_BGR8, AV_PIX_FMT_RGB4_BYTE, AV_PIX_FMT_BGR4_BYTE,
+        AV_PIX_FMT_GRAY8, AV_PIX_FMT_PAL8,
+        AV_PIX_FMT_MONOBLACK,
+        AV_PIX_FMT_NONE
+    },
 };