FFmpeg
Loading...
Searching...
No Matches
wcmv.c
Go to the documentation of this file.
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
25#include "libavutil/imgutils.h"
26
27#include "avcodec.h"
28#include "bytestream.h"
29#include "codec_internal.h"
30#include "decode.h"
31#include "zlib_wrapper.h"
32
33#include <zlib.h>
34
35typedef struct WCMVContext {
36 int bpp;
39 uint8_t block_data[65536*8];
41
43 int *got_frame, AVPacket *avpkt)
44{
45 WCMVContext *s = avctx->priv_data;
46 z_stream *const zstream = &s->zstream.zstream;
47 int skip, blocks, zret, ret, intra = 0, flags = 0, bpp = s->bpp;
49 uint8_t *dst;
50
51 ret = inflateReset(zstream);
52 if (ret != Z_OK) {
53 av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", ret);
54 return AVERROR_EXTERNAL;
55 }
56
57 bytestream2_init(&gb, avpkt->data, avpkt->size);
58 blocks = bytestream2_get_le16(&gb);
59 if (!blocks)
61
62 if ((ret = ff_reget_buffer(avctx, s->prev_frame, flags)) < 0)
63 return ret;
64
65 if (blocks > 5) {
67 int x = 0, size;
68
69 if (blocks * 8 >= 0xFFFF)
70 size = bytestream2_get_le24(&gb);
71 else if (blocks * 8 >= 0xFF)
72 size = bytestream2_get_le16(&gb);
73 else
74 size = bytestream2_get_byte(&gb);
75
77 if (size > avpkt->size - skip)
79
80 zstream->next_in = avpkt->data + skip;
81 zstream->avail_in = size;
82 zstream->next_out = s->block_data;
83 zstream->avail_out = sizeof(s->block_data);
84
85 zret = inflate(zstream, Z_FINISH);
86 if (zret != Z_STREAM_END) {
87 av_log(avctx, AV_LOG_ERROR,
88 "Inflate failed with return code: %d.\n", zret);
90 }
91
92 ret = inflateReset(zstream);
93 if (ret != Z_OK) {
94 av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", ret);
95 return AVERROR_EXTERNAL;
96 }
97
99 bytestream2_init(&bgb, s->block_data, blocks * 8);
100
101 for (int i = 0; i < blocks; i++) {
102 int w, h;
103
104 bytestream2_skip(&bgb, 4);
105 w = bytestream2_get_le16(&bgb);
106 h = bytestream2_get_le16(&bgb);
107 if (x + bpp * (int64_t)w * h > INT_MAX)
108 return AVERROR_INVALIDDATA;
109 x += bpp * w * h;
110 }
111
112 if (x >= 0xFFFF)
113 bytestream2_skip(&gb, 3);
114 else if (x >= 0xFF)
115 bytestream2_skip(&gb, 2);
116 else
117 bytestream2_skip(&gb, 1);
118
119 skip = bytestream2_tell(&gb);
120
121 zstream->next_in = avpkt->data + skip;
122 zstream->avail_in = avpkt->size - skip;
123
124 bytestream2_init(&gb, s->block_data, blocks * 8);
125 } else if (blocks) {
126 int x = 0;
127
128 bytestream2_seek(&gb, 2, SEEK_SET);
129
130 for (int i = 0; i < blocks; i++) {
131 int w, h;
132
133 bytestream2_skip(&gb, 4);
134 w = bytestream2_get_le16(&gb);
135 h = bytestream2_get_le16(&gb);
136 if (x + bpp * (int64_t)w * h > INT_MAX)
137 return AVERROR_INVALIDDATA;
138 x += bpp * w * h;
139 }
140
141 if (x >= 0xFFFF)
142 bytestream2_skip(&gb, 3);
143 else if (x >= 0xFF)
144 bytestream2_skip(&gb, 2);
145 else
146 bytestream2_skip(&gb, 1);
147
148 skip = bytestream2_tell(&gb);
149
150 zstream->next_in = avpkt->data + skip;
151 zstream->avail_in = avpkt->size - skip;
152
153 bytestream2_seek(&gb, 2, SEEK_SET);
154 }
155
156 if (bytestream2_get_bytes_left(&gb) < 8LL * blocks)
157 return AVERROR_INVALIDDATA;
158
159 if (!avctx->frame_num) {
160 ptrdiff_t linesize[4] = { s->prev_frame->linesize[0], 0, 0, 0 };
161 av_image_fill_black(s->prev_frame->data, linesize, avctx->pix_fmt, 0,
162 avctx->width, avctx->height);
163 }
164
165 for (int block = 0; block < blocks; block++) {
166 int x, y, w, h;
167
168 x = bytestream2_get_le16(&gb);
169 y = bytestream2_get_le16(&gb);
170 w = bytestream2_get_le16(&gb);
171 h = bytestream2_get_le16(&gb);
172
173 if (blocks == 1 && x == 0 && y == 0 && w == avctx->width && h == avctx->height)
174 intra = 1;
175
176 if (x + w > avctx->width || y + h > avctx->height)
177 return AVERROR_INVALIDDATA;
178
179 if (w > avctx->width || h > avctx->height)
180 return AVERROR_INVALIDDATA;
181
182 dst = s->prev_frame->data[0] + (avctx->height - y - 1) * s->prev_frame->linesize[0] + x * bpp;
183 for (int i = 0; i < h; i++) {
184 zstream->next_out = dst;
185 zstream->avail_out = w * bpp;
186
187 zret = inflate(zstream, Z_SYNC_FLUSH);
188 if (zret != Z_OK && zret != Z_STREAM_END) {
189 av_log(avctx, AV_LOG_ERROR,
190 "Inflate failed with return code: %d.\n", zret);
191 return AVERROR_INVALIDDATA;
192 }
193
194 dst -= s->prev_frame->linesize[0];
195 }
196 }
197
198 if (intra)
199 s->prev_frame->flags |= AV_FRAME_FLAG_KEY;
200 else
201 s->prev_frame->flags &= ~AV_FRAME_FLAG_KEY;
202 s->prev_frame->pict_type = intra ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
203
204 if ((ret = av_frame_ref(frame, s->prev_frame)) < 0)
205 return ret;
206
207 *got_frame = 1;
208
209 return avpkt->size;
210}
211
213{
214 WCMVContext *s = avctx->priv_data;
215
216 switch (avctx->bits_per_coded_sample) {
217 case 16: avctx->pix_fmt = AV_PIX_FMT_RGB565LE; break;
218 case 24: avctx->pix_fmt = AV_PIX_FMT_BGR24; break;
219 case 32: avctx->pix_fmt = AV_PIX_FMT_BGRA; break;
220 default: av_log(avctx, AV_LOG_ERROR, "Unsupported bits_per_coded_sample: %d\n",
221 avctx->bits_per_coded_sample);
223 }
224
225 s->bpp = avctx->bits_per_coded_sample >> 3;
226
227 s->prev_frame = av_frame_alloc();
228 if (!s->prev_frame)
229 return AVERROR(ENOMEM);
230
231 return ff_inflate_init(&s->zstream, avctx);
232}
233
235{
236 WCMVContext *s = avctx->priv_data;
237
238 av_frame_free(&s->prev_frame);
239 ff_inflate_end(&s->zstream);
240
241 return 0;
242}
243
245 .p.name = "wcmv",
246 CODEC_LONG_NAME("WinCAM Motion Video"),
247 .p.type = AVMEDIA_TYPE_VIDEO,
248 .p.id = AV_CODEC_ID_WCMV,
249 .priv_data_size = sizeof(WCMVContext),
250 .init = decode_init,
253 .p.capabilities = AV_CODEC_CAP_DR1,
254 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
255};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
const FFCodec ff_wcmv_decoder
Definition wcmv.c:244
static av_cold void close(AVCodecParserContext *s)
Definition apv_parser.c:197
Libavcodec external API header.
static void BS_FUNC skip(BSCTX *bc, unsigned int n)
Skip n bits in the buffer.
static av_always_inline int bytestream2_get_bytes_left(const GetByteContext *g)
Definition bytestream.h:158
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition bytestream.h:137
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition bytestream.h:168
static av_always_inline int bytestream2_seek(GetByteContext *g, int offset, int whence)
Definition bytestream.h:212
static av_always_inline int bytestream2_tell(const GetByteContext *g)
Definition bytestream.h:192
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
long long int64_t
Definition coverity.c:34
static int16_t block[64]
Definition dct.c:125
int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Identical in function to ff_get_buffer(), except it reuses the existing buffer if available.
Definition decode.c:1906
#define FF_REGET_BUFFER_FLAG_READONLY
the returned buffer does not need to be writable
Definition decode.h:137
static AVFrame * frame
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
@ AV_CODEC_ID_WCMV
Definition codec_id.h:285
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition error.h:64
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition error.h:59
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_FRAME_FLAG_KEY
A flag to mark frames that are keyframes.
Definition frame.h:687
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
Definition frame.c:278
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition frame.c:52
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
int av_image_fill_black(uint8_t *const dst_data[4], const ptrdiff_t dst_linesize[4], enum AVPixelFormat pix_fmt, enum AVColorRange range, int width, int height)
Overwrite the image data with black.
Definition imgutils.c:660
@ AV_PICTURE_TYPE_I
Intra.
Definition avutil.h:278
@ AV_PICTURE_TYPE_P
Predicted.
Definition avutil.h:279
misc image utilities
static av_cold int decode_init(AVCodecContext *avctx)
Definition 4xm.c:998
static int decode_frame(AVCodecContext *avctx, AVFrame *picture, int *got_frame, AVPacket *avpkt)
Definition 4xm.c:837
static av_cold int decode_close(AVCodecContext *avctx)
Definition aacdec.c:1220
#define av_cold
Definition attributes.h:117
uint8_t w
Definition llvidencdsp.c:39
@ AV_PIX_FMT_BGRA
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition pixfmt.h:102
@ AV_PIX_FMT_RGB565LE
packed RGB 5:6:5, 16bpp, (msb) 5R 6G 5B(lsb), little-endian
Definition pixfmt.h:113
@ AV_PIX_FMT_BGR24
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition pixfmt.h:76
main external API structure.
Definition avcodec.h:443
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition avcodec.h:643
int width
picture width / height.
Definition avcodec.h:604
int64_t frame_num
Frame counter, set by libavcodec.
Definition avcodec.h:1883
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition avcodec.h:1564
void * priv_data
Definition avcodec.h:470
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
This structure stores compressed data.
Definition packet.h:580
int size
Definition packet.h:604
uint8_t * data
Definition packet.h:603
uint8_t block_data[65536 *8]
Definition wcmv.c:39
AVFrame * prev_frame
Definition wcmv.c:38
FFZStream zstream
Definition wcmv.c:37
int bpp
Definition wcmv.c:36
#define av_log(a,...)
int size
static void inflate(uint8_t *dst, const uint8_t *p1, int width, int threshold, const uint8_t *coordinates[], int coord, int maxc)
static int decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *avpkt)
Definition wcmv.c:42
static av_cold int decode_close(AVCodecContext *avctx)
Definition wcmv.c:234
static av_cold int decode_init(AVCodecContext *avctx)
Definition wcmv.c:212
void ff_inflate_end(FFZStream *zstream)
Wrapper around inflateEnd().
int ff_inflate_init(FFZStream *zstream, void *logctx)
Wrapper around inflateInit().