]> git.sesse.net Git - ffmpeg/blob - libavcodec/cscd.c
Merge commit '7ebfb466aec2c4628fcd42a72b29034efcaba4bc'
[ffmpeg] / libavcodec / cscd.c
1 /*
2  * CamStudio decoder
3  * Copyright (c) 2006 Reimar Doeffinger
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 #include <stdio.h>
22 #include <stdlib.h>
23
24 #include "avcodec.h"
25 #include "internal.h"
26 #include "libavutil/common.h"
27
28 #if CONFIG_ZLIB
29 #include <zlib.h>
30 #endif
31 #include "libavutil/lzo.h"
32
33 typedef struct {
34     AVFrame pic;
35     int linelen, height, bpp;
36     unsigned int decomp_size;
37     unsigned char* decomp_buf;
38 } CamStudioContext;
39
40 static void copy_frame_default(AVFrame *f, const uint8_t *src,
41                                int linelen, int height) {
42     int i, src_stride = FFALIGN(linelen, 4);
43     uint8_t *dst = f->data[0];
44     dst += (height - 1) * f->linesize[0];
45     for (i = height; i; i--) {
46         memcpy(dst, src, linelen);
47         src += src_stride;
48         dst -= f->linesize[0];
49     }
50 }
51
52 static void add_frame_default(AVFrame *f, const uint8_t *src,
53                               int linelen, int height) {
54     int i, j, src_stride = FFALIGN(linelen, 4);
55     uint8_t *dst = f->data[0];
56     dst += (height - 1) * f->linesize[0];
57     for (i = height; i; i--) {
58         for (j = linelen; j; j--)
59             *dst++ += *src++;
60         src += src_stride - linelen;
61         dst -= f->linesize[0] + linelen;
62     }
63 }
64
65 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
66                         AVPacket *avpkt) {
67     const uint8_t *buf = avpkt->data;
68     int buf_size = avpkt->size;
69     CamStudioContext *c = avctx->priv_data;
70     AVFrame *picture = data;
71     int ret;
72
73     if (buf_size < 2) {
74         av_log(avctx, AV_LOG_ERROR, "coded frame too small\n");
75         return AVERROR_INVALIDDATA;
76     }
77
78     c->pic.reference = 3;
79     c->pic.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_READABLE |
80                           FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
81     if ((ret = avctx->reget_buffer(avctx, &c->pic)) < 0) {
82         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
83         return ret;
84     }
85
86     // decompress data
87     switch ((buf[0] >> 1) & 7) {
88         case 0: { // lzo compression
89             int outlen = c->decomp_size, inlen = buf_size - 2;
90             if (av_lzo1x_decode(c->decomp_buf, &outlen, &buf[2], &inlen))
91                 av_log(avctx, AV_LOG_ERROR, "error during lzo decompression\n");
92             break;
93         }
94         case 1: { // zlib compression
95 #if CONFIG_ZLIB
96             unsigned long dlen = c->decomp_size;
97             if (uncompress(c->decomp_buf, &dlen, &buf[2], buf_size - 2) != Z_OK)
98                 av_log(avctx, AV_LOG_ERROR, "error during zlib decompression\n");
99             break;
100 #else
101             av_log(avctx, AV_LOG_ERROR, "compiled without zlib support\n");
102             return AVERROR(ENOSYS);
103 #endif
104         }
105         default:
106             av_log(avctx, AV_LOG_ERROR, "unknown compression\n");
107             return AVERROR_INVALIDDATA;
108     }
109
110     // flip upside down, add difference frame
111     if (buf[0] & 1) { // keyframe
112         c->pic.pict_type = AV_PICTURE_TYPE_I;
113         c->pic.key_frame = 1;
114               copy_frame_default(&c->pic, c->decomp_buf,
115                                  c->linelen, c->height);
116     } else {
117         c->pic.pict_type = AV_PICTURE_TYPE_P;
118         c->pic.key_frame = 0;
119               add_frame_default(&c->pic, c->decomp_buf,
120                                 c->linelen, c->height);
121     }
122
123     *picture = c->pic;
124     *got_frame = 1;
125     return buf_size;
126 }
127
128 static av_cold int decode_init(AVCodecContext *avctx) {
129     CamStudioContext *c = avctx->priv_data;
130     int stride;
131     switch (avctx->bits_per_coded_sample) {
132         case 16: avctx->pix_fmt = AV_PIX_FMT_RGB555LE; break;
133         case 24: avctx->pix_fmt = AV_PIX_FMT_BGR24; break;
134         case 32: avctx->pix_fmt = AV_PIX_FMT_BGRA; break;
135         default:
136             av_log(avctx, AV_LOG_ERROR,
137                    "CamStudio codec error: invalid depth %i bpp\n",
138                    avctx->bits_per_coded_sample);
139             return AVERROR_INVALIDDATA;
140     }
141     c->bpp = avctx->bits_per_coded_sample;
142     avcodec_get_frame_defaults(&c->pic);
143     c->pic.data[0] = NULL;
144     c->linelen = avctx->width * avctx->bits_per_coded_sample / 8;
145     c->height = avctx->height;
146     stride = FFALIGN(c->linelen, 4);
147     c->decomp_size = c->height * stride;
148     c->decomp_buf = av_malloc(c->decomp_size + AV_LZO_OUTPUT_PADDING);
149     if (!c->decomp_buf) {
150         av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
151         return AVERROR(ENOMEM);
152     }
153     return 0;
154 }
155
156 static av_cold int decode_end(AVCodecContext *avctx) {
157     CamStudioContext *c = avctx->priv_data;
158     av_freep(&c->decomp_buf);
159     if (c->pic.data[0])
160         avctx->release_buffer(avctx, &c->pic);
161     return 0;
162 }
163
164 AVCodec ff_cscd_decoder = {
165     .name           = "camstudio",
166     .type           = AVMEDIA_TYPE_VIDEO,
167     .id             = AV_CODEC_ID_CSCD,
168     .priv_data_size = sizeof(CamStudioContext),
169     .init           = decode_init,
170     .close          = decode_end,
171     .decode         = decode_frame,
172     .capabilities   = CODEC_CAP_DR1,
173     .long_name      = NULL_IF_CONFIG_SMALL("CamStudio"),
174 };