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);
60 if ((ret = ff_reget_buffer(avctx, s->prev_frame, 0)) < 0)
63 blocks = bytestream2_get_le16(&gb);
68 if (blocks * 8 >= 0xFFFF)
69 size = bytestream2_get_le24(&gb);
70 else if (blocks * 8 >= 0xFF)
71 size = bytestream2_get_le16(&gb);
73 size = bytestream2_get_byte(&gb);
75 skip = bytestream2_tell(&gb);
76 if (size > avpkt->size - skip)
77 return AVERROR_INVALIDDATA;
79 s->zstream.next_in = avpkt->data + skip;
80 s->zstream.avail_in = size;
81 s->zstream.next_out = s->block_data;
82 s->zstream.avail_out = sizeof(s->block_data);
84 zret = inflate(&s->zstream, Z_FINISH);
85 if (zret != Z_STREAM_END) {
86 av_log(avctx, AV_LOG_ERROR,
87 "Inflate failed with return code: %d.\n", zret);
88 return AVERROR_INVALIDDATA;
91 ret = inflateReset(&s->zstream);
93 av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", ret);
94 return AVERROR_EXTERNAL;
97 bytestream2_skip(&gb, size);
98 bytestream2_init(&bgb, s->block_data, blocks * 8);
100 for (int i = 0; i < blocks; i++) {
103 bytestream2_skip(&bgb, 4);
104 w = bytestream2_get_le16(&bgb);
105 h = bytestream2_get_le16(&bgb);
106 if (x + bpp * (int64_t)w * h > INT_MAX)
107 return AVERROR_INVALIDDATA;
112 bytestream2_skip(&gb, 3);
114 bytestream2_skip(&gb, 2);
116 bytestream2_skip(&gb, 1);
118 skip = bytestream2_tell(&gb);
120 s->zstream.next_in = avpkt->data + skip;
121 s->zstream.avail_in = avpkt->size - skip;
123 bytestream2_init(&gb, s->block_data, blocks * 8);
127 bytestream2_seek(&gb, 2, SEEK_SET);
129 for (int i = 0; i < blocks; i++) {
132 bytestream2_skip(&gb, 4);
133 w = bytestream2_get_le16(&gb);
134 h = bytestream2_get_le16(&gb);
135 if (x + bpp * (int64_t)w * h > INT_MAX)
136 return AVERROR_INVALIDDATA;
141 bytestream2_skip(&gb, 3);
143 bytestream2_skip(&gb, 2);
145 bytestream2_skip(&gb, 1);
147 skip = bytestream2_tell(&gb);
149 s->zstream.next_in = avpkt->data + skip;
150 s->zstream.avail_in = avpkt->size - skip;
152 bytestream2_seek(&gb, 2, SEEK_SET);
155 if (bytestream2_get_bytes_left(&gb) < 8LL * blocks)
156 return AVERROR_INVALIDDATA;
158 if (!avctx->frame_number) {
159 ptrdiff_t linesize[4] = { s->prev_frame->linesize[0], 0, 0, 0 };
160 av_image_fill_black(s->prev_frame->data, linesize, avctx->pix_fmt, 0,
161 avctx->width, avctx->height);
164 for (int block = 0; block < blocks; block++) {
167 x = bytestream2_get_le16(&gb);
168 y = bytestream2_get_le16(&gb);
169 w = bytestream2_get_le16(&gb);
170 h = bytestream2_get_le16(&gb);
172 if (blocks == 1 && x == 0 && y == 0 && w == avctx->width && h == avctx->height)
175 if (x + w > avctx->width || y + h > avctx->height)
176 return AVERROR_INVALIDDATA;
178 if (w > avctx->width || h > avctx->height)
179 return AVERROR_INVALIDDATA;
181 dst = s->prev_frame->data[0] + (avctx->height - y - 1) * s->prev_frame->linesize[0] + x * bpp;
182 for (int i = 0; i < h; i++) {
183 s->zstream.next_out = dst;
184 s->zstream.avail_out = w * bpp;
186 zret = inflate(&s->zstream, Z_SYNC_FLUSH);
187 if (zret != Z_OK && zret != Z_STREAM_END) {
188 av_log(avctx, AV_LOG_ERROR,
189 "Inflate failed with return code: %d.\n", zret);
190 return AVERROR_INVALIDDATA;
193 dst -= s->prev_frame->linesize[0];
197 s->prev_frame->key_frame = intra;
198 s->prev_frame->pict_type = intra ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
200 if ((ret = av_frame_ref(frame, s->prev_frame)) < 0)
208 static av_cold int decode_init(AVCodecContext *avctx)
210 WCMVContext *s = avctx->priv_data;
213 switch (avctx->bits_per_coded_sample) {
214 case 16: avctx->pix_fmt = AV_PIX_FMT_RGB565LE; break;
215 case 24: avctx->pix_fmt = AV_PIX_FMT_BGR24; break;
216 case 32: avctx->pix_fmt = AV_PIX_FMT_BGRA; break;
217 default: av_log(avctx, AV_LOG_ERROR, "Unsupported bits_per_coded_sample: %d\n",
218 avctx->bits_per_coded_sample);
219 return AVERROR_PATCHWELCOME;
222 s->bpp = avctx->bits_per_coded_sample >> 3;
224 s->zstream.zalloc = Z_NULL;
225 s->zstream.zfree = Z_NULL;
226 s->zstream.opaque = Z_NULL;
227 zret = inflateInit(&s->zstream);
229 av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
230 return AVERROR_EXTERNAL;
233 s->prev_frame = av_frame_alloc();
235 return AVERROR(ENOMEM);
240 static av_cold int decode_close(AVCodecContext *avctx)
242 WCMVContext *s = avctx->priv_data;
244 av_frame_free(&s->prev_frame);
245 inflateEnd(&s->zstream);
250 AVCodec ff_wcmv_decoder = {
252 .long_name = NULL_IF_CONFIG_SMALL("WinCAM Motion Video"),
253 .type = AVMEDIA_TYPE_VIDEO,
254 .id = AV_CODEC_ID_WCMV,
255 .priv_data_size = sizeof(WCMVContext),
257 .close = decode_close,
258 .decode = decode_frame,
259 .capabilities = AV_CODEC_CAP_DR1,
260 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
261 FF_CODEC_CAP_INIT_CLEANUP,