]> git.sesse.net Git - ffmpeg/blob - libavcodec/wcmv.c
Revert "avcodec/wcmv: Avoid copying frames if they are unchanged"
[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_reget_buffer(avctx, s->prev_frame, 0)) < 0)
61         return ret;
62
63     blocks = bytestream2_get_le16(&gb);
64     if (blocks > 5) {
65         GetByteContext bgb;
66         int x = 0, size;
67
68         if (blocks * 8 >= 0xFFFF)
69             size = bytestream2_get_le24(&gb);
70         else if (blocks * 8 >= 0xFF)
71             size = bytestream2_get_le16(&gb);
72         else
73             size = bytestream2_get_byte(&gb);
74
75         skip = bytestream2_tell(&gb);
76         if (size > avpkt->size - skip)
77             return AVERROR_INVALIDDATA;
78
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);
83
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;
89         }
90
91         ret = inflateReset(&s->zstream);
92         if (ret != Z_OK) {
93             av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", ret);
94             return AVERROR_EXTERNAL;
95         }
96
97         bytestream2_skip(&gb, size);
98         bytestream2_init(&bgb, s->block_data, blocks * 8);
99
100         for (int i = 0; i < blocks; i++) {
101             int w, h;
102
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;
108             x += bpp * w * h;
109         }
110
111         if (x >= 0xFFFF)
112             bytestream2_skip(&gb, 3);
113         else if (x >= 0xFF)
114             bytestream2_skip(&gb, 2);
115         else
116             bytestream2_skip(&gb, 1);
117
118         skip = bytestream2_tell(&gb);
119
120         s->zstream.next_in  = avpkt->data + skip;
121         s->zstream.avail_in = avpkt->size - skip;
122
123         bytestream2_init(&gb, s->block_data, blocks * 8);
124     } else if (blocks) {
125         int x = 0;
126
127         bytestream2_seek(&gb, 2, SEEK_SET);
128
129         for (int i = 0; i < blocks; i++) {
130             int w, h;
131
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;
137             x += bpp * w * h;
138         }
139
140         if (x >= 0xFFFF)
141             bytestream2_skip(&gb, 3);
142         else if (x >= 0xFF)
143             bytestream2_skip(&gb, 2);
144         else
145             bytestream2_skip(&gb, 1);
146
147         skip = bytestream2_tell(&gb);
148
149         s->zstream.next_in  = avpkt->data + skip;
150         s->zstream.avail_in = avpkt->size - skip;
151
152         bytestream2_seek(&gb, 2, SEEK_SET);
153     }
154
155     if (bytestream2_get_bytes_left(&gb) < 8LL * blocks)
156         return AVERROR_INVALIDDATA;
157
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);
162     }
163
164     for (int block = 0; block < blocks; block++) {
165         int x, y, w, h;
166
167         x = bytestream2_get_le16(&gb);
168         y = bytestream2_get_le16(&gb);
169         w = bytestream2_get_le16(&gb);
170         h = bytestream2_get_le16(&gb);
171
172         if (blocks == 1 && x == 0 && y == 0 && w == avctx->width && h == avctx->height)
173             intra = 1;
174
175         if (x + w > avctx->width || y + h > avctx->height)
176             return AVERROR_INVALIDDATA;
177
178         if (w > avctx->width || h > avctx->height)
179             return AVERROR_INVALIDDATA;
180
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;
185
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;
191             }
192
193             dst -= s->prev_frame->linesize[0];
194         }
195     }
196
197     s->prev_frame->key_frame = intra;
198     s->prev_frame->pict_type = intra ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
199
200     if ((ret = av_frame_ref(frame, s->prev_frame)) < 0)
201         return ret;
202
203     *got_frame = 1;
204
205     return avpkt->size;
206 }
207
208 static av_cold int decode_init(AVCodecContext *avctx)
209 {
210     WCMVContext *s = avctx->priv_data;
211     int zret;
212
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;
220     }
221
222     s->bpp = avctx->bits_per_coded_sample >> 3;
223
224     s->zstream.zalloc = Z_NULL;
225     s->zstream.zfree = Z_NULL;
226     s->zstream.opaque = Z_NULL;
227     zret = inflateInit(&s->zstream);
228     if (zret != Z_OK) {
229         av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
230         return AVERROR_EXTERNAL;
231     }
232
233     s->prev_frame = av_frame_alloc();
234     if (!s->prev_frame)
235         return AVERROR(ENOMEM);
236
237     return 0;
238 }
239
240 static av_cold int decode_close(AVCodecContext *avctx)
241 {
242     WCMVContext *s = avctx->priv_data;
243
244     av_frame_free(&s->prev_frame);
245     inflateEnd(&s->zstream);
246
247     return 0;
248 }
249
250 AVCodec ff_wcmv_decoder = {
251     .name             = "wcmv",
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),
256     .init             = decode_init,
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,
262 };