]> git.sesse.net Git - ffmpeg/blob - libavcodec/wcmv.c
Merge commit 'f1011ea28a4048ddec97794ca3e9901474fe055f'
[ffmpeg] / libavcodec / wcmv.c
1 /*
2  * WinCAM Motion Video decoder
3  *
4  * Copyright (c) 2018 Paul B Mahol
5  *
6  * This file is part of FFmpeg.
7  *
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.
12  *
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.
17  *
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
21  */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include "libavutil/imgutils.h"
28
29 #include "avcodec.h"
30 #include "bytestream.h"
31 #include "internal.h"
32
33 #include <zlib.h>
34
35 typedef struct WCMVContext {
36     int         bpp;
37     z_stream    zstream;
38     AVFrame    *prev_frame;
39     uint8_t     block_data[65536*8];
40 } WCMVContext;
41
42 static int decode_frame(AVCodecContext *avctx,
43                         void *data, int *got_frame,
44                         AVPacket *avpkt)
45 {
46     WCMVContext *s = avctx->priv_data;
47     AVFrame *frame = data;
48     int skip, blocks, zret, ret, intra = 0, bpp = s->bpp;
49     GetByteContext gb;
50     uint8_t *dst;
51
52     ret = inflateReset(&s->zstream);
53     if (ret != Z_OK) {
54         av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", ret);
55         return AVERROR_EXTERNAL;
56     }
57
58     bytestream2_init(&gb, avpkt->data, avpkt->size);
59     blocks = bytestream2_get_le16(&gb);
60     if (!blocks)
61         return avpkt->size;
62
63     if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
64         return ret;
65
66     if (blocks > 5) {
67         GetByteContext bgb;
68         int x = 0, size;
69
70         if (blocks * 8 >= 0xFFFF)
71             size = bytestream2_get_le24(&gb);
72         else if (blocks * 8 >= 0xFF)
73             size = bytestream2_get_le16(&gb);
74         else
75             size = bytestream2_get_byte(&gb);
76
77         skip = bytestream2_tell(&gb);
78         if (size > avpkt->size - skip)
79             return AVERROR_INVALIDDATA;
80
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);
85
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;
91         }
92
93         ret = inflateReset(&s->zstream);
94         if (ret != Z_OK) {
95             av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", ret);
96             return AVERROR_EXTERNAL;
97         }
98
99         bytestream2_skip(&gb, size);
100         bytestream2_init(&bgb, s->block_data, blocks * 8);
101
102         for (int i = 0; i < blocks; i++) {
103             int w, h;
104
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;
110             x += bpp * w * h;
111         }
112
113         if (x >= 0xFFFF)
114             bytestream2_skip(&gb, 3);
115         else if (x >= 0xFF)
116             bytestream2_skip(&gb, 2);
117         else
118             bytestream2_skip(&gb, 1);
119
120         skip = bytestream2_tell(&gb);
121
122         s->zstream.next_in  = avpkt->data + skip;
123         s->zstream.avail_in = avpkt->size - skip;
124
125         bytestream2_init(&gb, s->block_data, blocks * 8);
126     } else if (blocks) {
127         int x = 0;
128
129         bytestream2_seek(&gb, 2, SEEK_SET);
130
131         for (int i = 0; i < blocks; i++) {
132             int w, h;
133
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;
139             x += bpp * w * h;
140         }
141
142         if (x >= 0xFFFF)
143             bytestream2_skip(&gb, 3);
144         else if (x >= 0xFF)
145             bytestream2_skip(&gb, 2);
146         else
147             bytestream2_skip(&gb, 1);
148
149         skip = bytestream2_tell(&gb);
150
151         s->zstream.next_in  = avpkt->data + skip;
152         s->zstream.avail_in = avpkt->size - skip;
153
154         bytestream2_seek(&gb, 2, SEEK_SET);
155     }
156
157     if (s->prev_frame->data[0]) {
158         ret = av_frame_copy(frame, s->prev_frame);
159         if (ret < 0)
160             return ret;
161     } else {
162         ptrdiff_t linesize[4] = { frame->linesize[0], 0, 0, 0 };
163         av_image_fill_black(frame->data, linesize, avctx->pix_fmt, 0,
164                             avctx->width, avctx->height);
165     }
166
167     for (int block = 0; block < blocks; block++) {
168         int x, y, w, h;
169
170         x = bytestream2_get_le16(&gb);
171         y = bytestream2_get_le16(&gb);
172         w = bytestream2_get_le16(&gb);
173         h = bytestream2_get_le16(&gb);
174
175         if (blocks == 1 && x == 0 && y == 0 && w == avctx->width && h == avctx->height)
176             intra = 1;
177
178         if (x + w > avctx->width || y + h > avctx->height)
179             return AVERROR_INVALIDDATA;
180
181         if (w > avctx->width || h > avctx->height)
182             return AVERROR_INVALIDDATA;
183
184         dst = frame->data[0] + (avctx->height - y - 1) * frame->linesize[0] + x * bpp;
185         for (int i = 0; i < h; i++) {
186             s->zstream.next_out  = dst;
187             s->zstream.avail_out = w * bpp;
188
189             zret = inflate(&s->zstream, Z_SYNC_FLUSH);
190             if (zret != Z_OK && zret != Z_STREAM_END) {
191                 av_log(avctx, AV_LOG_ERROR,
192                        "Inflate failed with return code: %d.\n", zret);
193                 return AVERROR_INVALIDDATA;
194             }
195
196             dst -= frame->linesize[0];
197         }
198     }
199
200     frame->key_frame = intra;
201     frame->pict_type = intra ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
202
203     av_frame_unref(s->prev_frame);
204     if ((ret = av_frame_ref(s->prev_frame, frame)) < 0)
205         return ret;
206
207     *got_frame = 1;
208
209     return avpkt->size;
210 }
211
212 static av_cold int decode_init(AVCodecContext *avctx)
213 {
214     WCMVContext *s = avctx->priv_data;
215     int zret;
216
217     switch (avctx->bits_per_coded_sample) {
218     case 16: avctx->pix_fmt = AV_PIX_FMT_RGB565LE; break;
219     case 24: avctx->pix_fmt = AV_PIX_FMT_BGR24;  break;
220     case 32: avctx->pix_fmt = AV_PIX_FMT_BGRA;   break;
221     default: av_log(avctx, AV_LOG_ERROR, "Unsupported bits_per_coded_sample: %d\n",
222                     avctx->bits_per_coded_sample);
223              return AVERROR_PATCHWELCOME;
224     }
225
226     s->bpp = avctx->bits_per_coded_sample >> 3;
227
228     s->zstream.zalloc = Z_NULL;
229     s->zstream.zfree = Z_NULL;
230     s->zstream.opaque = Z_NULL;
231     zret = inflateInit(&s->zstream);
232     if (zret != Z_OK) {
233         av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
234         return AVERROR_EXTERNAL;
235     }
236
237     s->prev_frame = av_frame_alloc();
238     if (!s->prev_frame)
239         return AVERROR(ENOMEM);
240
241     return 0;
242 }
243
244 static av_cold int decode_close(AVCodecContext *avctx)
245 {
246     WCMVContext *s = avctx->priv_data;
247
248     av_frame_free(&s->prev_frame);
249     inflateEnd(&s->zstream);
250
251     return 0;
252 }
253
254 AVCodec ff_wcmv_decoder = {
255     .name             = "wcmv",
256     .long_name        = NULL_IF_CONFIG_SMALL("WinCAM Motion Video"),
257     .type             = AVMEDIA_TYPE_VIDEO,
258     .id               = AV_CODEC_ID_WCMV,
259     .priv_data_size   = sizeof(WCMVContext),
260     .init             = decode_init,
261     .close            = decode_close,
262     .decode           = decode_frame,
263     .capabilities     = AV_CODEC_CAP_DR1,
264     .caps_internal    = FF_CODEC_CAP_INIT_THREADSAFE |
265                         FF_CODEC_CAP_INIT_CLEANUP,
266 };