]> git.sesse.net Git - ffmpeg/blob - libavcodec/cscd.c
Merge remote-tracking branch 'qatar/master'
[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 "libavutil/common.h"
26
27 #if CONFIG_ZLIB
28 #include <zlib.h>
29 #endif
30 #include "libavutil/lzo.h"
31
32 typedef struct {
33     AVFrame pic;
34     int linelen, height, bpp;
35     unsigned int decomp_size;
36     unsigned char* decomp_buf;
37 } CamStudioContext;
38
39 static void copy_frame_default(AVFrame *f, const uint8_t *src,
40                                int linelen, int height) {
41     int i, src_stride = FFALIGN(linelen, 4);
42     uint8_t *dst = f->data[0];
43     dst += (height - 1) * f->linesize[0];
44     for (i = height; i; i--) {
45         memcpy(dst, src, linelen);
46         src += src_stride;
47         dst -= f->linesize[0];
48     }
49 }
50
51 static void add_frame_default(AVFrame *f, const uint8_t *src,
52                               int linelen, int height) {
53     int i, j, src_stride = FFALIGN(linelen, 4);
54     uint8_t *dst = f->data[0];
55     dst += (height - 1) * f->linesize[0];
56     for (i = height; i; i--) {
57         for (j = linelen; j; j--)
58             *dst++ += *src++;
59         src += src_stride - linelen;
60         dst -= f->linesize[0] + linelen;
61     }
62 }
63
64 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
65                         AVPacket *avpkt) {
66     const uint8_t *buf = avpkt->data;
67     int buf_size = avpkt->size;
68     CamStudioContext *c = avctx->priv_data;
69     AVFrame *picture = data;
70
71     if (buf_size < 2) {
72         av_log(avctx, AV_LOG_ERROR, "coded frame too small\n");
73         return -1;
74     }
75
76     c->pic.reference = 3;
77     c->pic.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_READABLE |
78                           FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
79     if (avctx->reget_buffer(avctx, &c->pic) < 0) {
80         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
81         return -1;
82     }
83
84     // decompress data
85     switch ((buf[0] >> 1) & 7) {
86         case 0: { // lzo compression
87             int outlen = c->decomp_size, inlen = buf_size - 2;
88             if (av_lzo1x_decode(c->decomp_buf, &outlen, &buf[2], &inlen))
89                 av_log(avctx, AV_LOG_ERROR, "error during lzo decompression\n");
90             break;
91         }
92         case 1: { // zlib compression
93 #if CONFIG_ZLIB
94             unsigned long dlen = c->decomp_size;
95             if (uncompress(c->decomp_buf, &dlen, &buf[2], buf_size - 2) != Z_OK)
96                 av_log(avctx, AV_LOG_ERROR, "error during zlib decompression\n");
97             break;
98 #else
99             av_log(avctx, AV_LOG_ERROR, "compiled without zlib support\n");
100             return -1;
101 #endif
102         }
103         default:
104             av_log(avctx, AV_LOG_ERROR, "unknown compression\n");
105             return -1;
106     }
107
108     // flip upside down, add difference frame
109     if (buf[0] & 1) { // keyframe
110         c->pic.pict_type = AV_PICTURE_TYPE_I;
111         c->pic.key_frame = 1;
112               copy_frame_default(&c->pic, c->decomp_buf,
113                                  c->linelen, c->height);
114     } else {
115         c->pic.pict_type = AV_PICTURE_TYPE_P;
116         c->pic.key_frame = 0;
117               add_frame_default(&c->pic, c->decomp_buf,
118                                 c->linelen, c->height);
119     }
120
121     *picture = c->pic;
122     *data_size = sizeof(AVFrame);
123     return buf_size;
124 }
125
126 static av_cold int decode_init(AVCodecContext *avctx) {
127     CamStudioContext *c = avctx->priv_data;
128     int stride;
129     switch (avctx->bits_per_coded_sample) {
130         case 16: avctx->pix_fmt = AV_PIX_FMT_RGB555LE; break;
131         case 24: avctx->pix_fmt = AV_PIX_FMT_BGR24; break;
132         case 32: avctx->pix_fmt = AV_PIX_FMT_BGRA; break;
133         default:
134             av_log(avctx, AV_LOG_ERROR,
135                    "CamStudio codec error: invalid depth %i bpp\n",
136                    avctx->bits_per_coded_sample);
137             return AVERROR_INVALIDDATA;
138     }
139     c->bpp = avctx->bits_per_coded_sample;
140     avcodec_get_frame_defaults(&c->pic);
141     c->pic.data[0] = NULL;
142     c->linelen = avctx->width * avctx->bits_per_coded_sample / 8;
143     c->height = avctx->height;
144     stride = FFALIGN(c->linelen, 4);
145     c->decomp_size = c->height * stride;
146     c->decomp_buf = av_malloc(c->decomp_size + AV_LZO_OUTPUT_PADDING);
147     if (!c->decomp_buf) {
148         av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
149         return AVERROR(ENOMEM);
150     }
151     return 0;
152 }
153
154 static av_cold int decode_end(AVCodecContext *avctx) {
155     CamStudioContext *c = avctx->priv_data;
156     av_freep(&c->decomp_buf);
157     if (c->pic.data[0])
158         avctx->release_buffer(avctx, &c->pic);
159     return 0;
160 }
161
162 AVCodec ff_cscd_decoder = {
163     .name           = "camstudio",
164     .type           = AVMEDIA_TYPE_VIDEO,
165     .id             = AV_CODEC_ID_CSCD,
166     .priv_data_size = sizeof(CamStudioContext),
167     .init           = decode_init,
168     .close          = decode_end,
169     .decode         = decode_frame,
170     .capabilities   = CODEC_CAP_DR1,
171     .long_name      = NULL_IF_CONFIG_SMALL("CamStudio"),
172 };