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