2 * WinCAM Motion Video decoder
4 * Copyright (c) 2018 Paul B Mahol
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 #include "libavutil/imgutils.h"
30 #include "bytestream.h"
35 typedef struct WCMVContext {
39 uint8_t block_data[65536*8];
42 static int decode_frame(AVCodecContext *avctx,
43 void *data, int *got_frame,
46 WCMVContext *s = avctx->priv_data;
47 AVFrame *frame = data;
48 int skip, blocks, zret, ret, intra = 0, bpp = s->bpp;
52 ret = inflateReset(&s->zstream);
54 av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", ret);
55 return AVERROR_EXTERNAL;
58 bytestream2_init(&gb, avpkt->data, avpkt->size);
59 blocks = bytestream2_get_le16(&gb);
63 if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
70 if (blocks * 8 >= 0xFFFF)
71 size = bytestream2_get_le24(&gb);
72 else if (blocks * 8 >= 0xFF)
73 size = bytestream2_get_le16(&gb);
75 size = bytestream2_get_byte(&gb);
77 skip = bytestream2_tell(&gb);
78 if (size > avpkt->size - skip)
79 return AVERROR_INVALIDDATA;
81 s->zstream.next_in = avpkt->data + skip;
82 s->zstream.avail_in = size;
83 s->zstream.next_out = s->block_data;
84 s->zstream.avail_out = sizeof(s->block_data);
86 zret = inflate(&s->zstream, Z_FINISH);
87 if (zret != Z_STREAM_END) {
88 av_log(avctx, AV_LOG_ERROR,
89 "Inflate failed with return code: %d.\n", zret);
90 return AVERROR_INVALIDDATA;
93 ret = inflateReset(&s->zstream);
95 av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", ret);
96 return AVERROR_EXTERNAL;
99 bytestream2_skip(&gb, size);
100 bytestream2_init(&bgb, s->block_data, blocks * 8);
102 for (int i = 0; i < blocks; i++) {
105 bytestream2_skip(&bgb, 4);
106 w = bytestream2_get_le16(&bgb);
107 h = bytestream2_get_le16(&bgb);
108 if (x + bpp * (int64_t)w * h > INT_MAX)
109 return AVERROR_INVALIDDATA;
114 bytestream2_skip(&gb, 3);
116 bytestream2_skip(&gb, 2);
118 bytestream2_skip(&gb, 1);
120 skip = bytestream2_tell(&gb);
122 s->zstream.next_in = avpkt->data + skip;
123 s->zstream.avail_in = avpkt->size - skip;
125 bytestream2_init(&gb, s->block_data, blocks * 8);
129 bytestream2_seek(&gb, 2, SEEK_SET);
131 for (int i = 0; i < blocks; i++) {
134 bytestream2_skip(&gb, 4);
135 w = bytestream2_get_le16(&gb);
136 h = bytestream2_get_le16(&gb);
137 if (x + bpp * (int64_t)w * h > INT_MAX)
138 return AVERROR_INVALIDDATA;
143 bytestream2_skip(&gb, 3);
145 bytestream2_skip(&gb, 2);
147 bytestream2_skip(&gb, 1);
149 skip = bytestream2_tell(&gb);
151 s->zstream.next_in = avpkt->data + skip;
152 s->zstream.avail_in = avpkt->size - skip;
154 bytestream2_seek(&gb, 2, SEEK_SET);
157 if (bytestream2_get_bytes_left(&gb) < 8LL * blocks)
158 return AVERROR_INVALIDDATA;
160 if (s->prev_frame->data[0]) {
161 ret = av_frame_copy(frame, s->prev_frame);
165 ptrdiff_t linesize[4] = { frame->linesize[0], 0, 0, 0 };
166 av_image_fill_black(frame->data, linesize, avctx->pix_fmt, 0,
167 avctx->width, avctx->height);
170 for (int block = 0; block < blocks; block++) {
173 x = bytestream2_get_le16(&gb);
174 y = bytestream2_get_le16(&gb);
175 w = bytestream2_get_le16(&gb);
176 h = bytestream2_get_le16(&gb);
178 if (blocks == 1 && x == 0 && y == 0 && w == avctx->width && h == avctx->height)
181 if (x + w > avctx->width || y + h > avctx->height)
182 return AVERROR_INVALIDDATA;
184 if (w > avctx->width || h > avctx->height)
185 return AVERROR_INVALIDDATA;
187 dst = frame->data[0] + (avctx->height - y - 1) * frame->linesize[0] + x * bpp;
188 for (int i = 0; i < h; i++) {
189 s->zstream.next_out = dst;
190 s->zstream.avail_out = w * bpp;
192 zret = inflate(&s->zstream, Z_SYNC_FLUSH);
193 if (zret != Z_OK && zret != Z_STREAM_END) {
194 av_log(avctx, AV_LOG_ERROR,
195 "Inflate failed with return code: %d.\n", zret);
196 return AVERROR_INVALIDDATA;
199 dst -= frame->linesize[0];
203 frame->key_frame = intra;
204 frame->pict_type = intra ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
206 av_frame_unref(s->prev_frame);
207 if ((ret = av_frame_ref(s->prev_frame, frame)) < 0)
215 static av_cold int decode_init(AVCodecContext *avctx)
217 WCMVContext *s = avctx->priv_data;
220 switch (avctx->bits_per_coded_sample) {
221 case 16: avctx->pix_fmt = AV_PIX_FMT_RGB565LE; break;
222 case 24: avctx->pix_fmt = AV_PIX_FMT_BGR24; break;
223 case 32: avctx->pix_fmt = AV_PIX_FMT_BGRA; break;
224 default: av_log(avctx, AV_LOG_ERROR, "Unsupported bits_per_coded_sample: %d\n",
225 avctx->bits_per_coded_sample);
226 return AVERROR_PATCHWELCOME;
229 s->bpp = avctx->bits_per_coded_sample >> 3;
231 s->zstream.zalloc = Z_NULL;
232 s->zstream.zfree = Z_NULL;
233 s->zstream.opaque = Z_NULL;
234 zret = inflateInit(&s->zstream);
236 av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
237 return AVERROR_EXTERNAL;
240 s->prev_frame = av_frame_alloc();
242 return AVERROR(ENOMEM);
247 static av_cold int decode_close(AVCodecContext *avctx)
249 WCMVContext *s = avctx->priv_data;
251 av_frame_free(&s->prev_frame);
252 inflateEnd(&s->zstream);
257 AVCodec ff_wcmv_decoder = {
259 .long_name = NULL_IF_CONFIG_SMALL("WinCAM Motion Video"),
260 .type = AVMEDIA_TYPE_VIDEO,
261 .id = AV_CODEC_ID_WCMV,
262 .priv_data_size = sizeof(WCMVContext),
264 .close = decode_close,
265 .decode = decode_frame,
266 .capabilities = AV_CODEC_CAP_DR1,
267 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
268 FF_CODEC_CAP_INIT_CLEANUP,