]> git.sesse.net Git - ffmpeg/blob - libavcodec/wcmv.c
Merge commit 'f8060865f3e1a16c62e0d337ef0979b6ee4ba457'
[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
60     if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
61         return ret;
62
63     if (s->prev_frame->data[0]) {
64         ret = av_frame_copy(frame, s->prev_frame);
65         if (ret < 0)
66             return ret;
67     } else {
68         ptrdiff_t linesize[4] = { frame->linesize[0], 0, 0, 0 };
69         av_image_fill_black(frame->data, linesize, avctx->pix_fmt, 0,
70                             avctx->width, avctx->height);
71     }
72
73     blocks = bytestream2_get_le16(&gb);
74     if (blocks > 5) {
75         GetByteContext bgb;
76         int x = 0, size;
77
78         if (blocks * 8 >= 0xFFFF)
79             size = bytestream2_get_le24(&gb);
80         else if (blocks * 8 >= 0xFF)
81             size = bytestream2_get_le16(&gb);
82         else
83             size = bytestream2_get_byte(&gb);
84
85         skip = bytestream2_tell(&gb);
86         if (size > avpkt->size - skip)
87             return AVERROR_INVALIDDATA;
88
89         s->zstream.next_in  = avpkt->data + skip;
90         s->zstream.avail_in = size;
91         s->zstream.next_out  = s->block_data;
92         s->zstream.avail_out = sizeof(s->block_data);
93
94         zret = inflate(&s->zstream, Z_FINISH);
95         if (zret != Z_STREAM_END) {
96             av_log(avctx, AV_LOG_ERROR,
97                    "Inflate failed with return code: %d.\n", zret);
98             return AVERROR_INVALIDDATA;
99         }
100
101         ret = inflateReset(&s->zstream);
102         if (ret != Z_OK) {
103             av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", ret);
104             return AVERROR_EXTERNAL;
105         }
106
107         bytestream2_skip(&gb, size);
108         bytestream2_init(&bgb, s->block_data, blocks * 8);
109
110         for (int i = 0; i < blocks; i++) {
111             int w, h;
112
113             bytestream2_skip(&bgb, 4);
114             w = bytestream2_get_le16(&bgb);
115             h = bytestream2_get_le16(&bgb);
116             x += bpp * w * h;
117         }
118
119         if (x >= 0xFFFF)
120             bytestream2_skip(&gb, 3);
121         else if (x >= 0xFF)
122             bytestream2_skip(&gb, 2);
123         else
124             bytestream2_skip(&gb, 1);
125
126         skip = bytestream2_tell(&gb);
127
128         s->zstream.next_in  = avpkt->data + skip;
129         s->zstream.avail_in = avpkt->size - skip;
130
131         bytestream2_init(&gb, s->block_data, blocks * 8);
132     } else if (blocks) {
133         int x = 0;
134
135         bytestream2_seek(&gb, 2, SEEK_SET);
136
137         for (int i = 0; i < blocks; i++) {
138             int w, h;
139
140             bytestream2_skip(&gb, 4);
141             w = bytestream2_get_le16(&gb);
142             h = bytestream2_get_le16(&gb);
143             x += bpp * w * h;
144         }
145
146         if (x >= 0xFFFF)
147             bytestream2_skip(&gb, 3);
148         else if (x >= 0xFF)
149             bytestream2_skip(&gb, 2);
150         else
151             bytestream2_skip(&gb, 1);
152
153         skip = bytestream2_tell(&gb);
154
155         s->zstream.next_in  = avpkt->data + skip;
156         s->zstream.avail_in = avpkt->size - skip;
157
158         bytestream2_seek(&gb, 2, SEEK_SET);
159     }
160
161     for (int block = 0; block < blocks; block++) {
162         int x, y, w, h;
163
164         x = bytestream2_get_le16(&gb);
165         y = bytestream2_get_le16(&gb);
166         w = bytestream2_get_le16(&gb);
167         h = bytestream2_get_le16(&gb);
168
169         if (blocks == 1 && x == 0 && y == 0 && w == avctx->width && h == avctx->height)
170             intra = 1;
171
172         if (x + w > avctx->width || y + h > avctx->height)
173             return AVERROR_INVALIDDATA;
174
175         if (w > avctx->width || h > avctx->height)
176             return AVERROR_INVALIDDATA;
177
178         dst = frame->data[0] + (avctx->height - y - 1) * frame->linesize[0] + x * bpp;
179         for (int i = 0; i < h; i++) {
180             s->zstream.next_out  = dst;
181             s->zstream.avail_out = w * bpp;
182
183             zret = inflate(&s->zstream, Z_SYNC_FLUSH);
184             if (zret != Z_OK && zret != Z_STREAM_END) {
185                 av_log(avctx, AV_LOG_ERROR,
186                        "Inflate failed with return code: %d.\n", zret);
187                 return AVERROR_INVALIDDATA;
188             }
189
190             dst -= frame->linesize[0];
191         }
192     }
193
194     frame->key_frame = intra;
195     frame->pict_type = intra ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
196
197     av_frame_unref(s->prev_frame);
198     if ((ret = av_frame_ref(s->prev_frame, frame)) < 0)
199         return ret;
200
201     *got_frame = 1;
202
203     return avpkt->size;
204 }
205
206 static av_cold int decode_init(AVCodecContext *avctx)
207 {
208     WCMVContext *s = avctx->priv_data;
209     int zret;
210
211     switch (avctx->bits_per_coded_sample) {
212     case 16: avctx->pix_fmt = AV_PIX_FMT_RGB565; break;
213     case 24: avctx->pix_fmt = AV_PIX_FMT_BGR24;  break;
214     case 32: avctx->pix_fmt = AV_PIX_FMT_BGRA;   break;
215     default: av_log(avctx, AV_LOG_ERROR, "Unsupported bits_per_coded_sample: %d\n",
216                     avctx->bits_per_coded_sample);
217              return AVERROR_PATCHWELCOME;
218     }
219
220     s->bpp = avctx->bits_per_coded_sample >> 3;
221
222     s->zstream.zalloc = Z_NULL;
223     s->zstream.zfree = Z_NULL;
224     s->zstream.opaque = Z_NULL;
225     zret = inflateInit(&s->zstream);
226     if (zret != Z_OK) {
227         av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
228         return AVERROR_EXTERNAL;
229     }
230
231     s->prev_frame = av_frame_alloc();
232     if (!s->prev_frame)
233         return AVERROR(ENOMEM);
234
235     return 0;
236 }
237
238 static av_cold int decode_close(AVCodecContext *avctx)
239 {
240     WCMVContext *s = avctx->priv_data;
241
242     av_frame_free(&s->prev_frame);
243     inflateEnd(&s->zstream);
244
245     return 0;
246 }
247
248 AVCodec ff_wcmv_decoder = {
249     .name             = "wcmv",
250     .long_name        = NULL_IF_CONFIG_SMALL("WinCAM Motion Video"),
251     .type             = AVMEDIA_TYPE_VIDEO,
252     .id               = AV_CODEC_ID_WCMV,
253     .priv_data_size   = sizeof(WCMVContext),
254     .init             = decode_init,
255     .close            = decode_close,
256     .decode           = decode_frame,
257     .capabilities     = AV_CODEC_CAP_DR1,
258     .caps_internal    = FF_CODEC_CAP_INIT_THREADSAFE |
259                         FF_CODEC_CAP_INIT_CLEANUP,
260 };