flashsv: Employ explicit AVCodec struct initializers.
[libav.git] / libavcodec / flashsvenc.c
CommitLineData
053185c2
BL
1/*
2 * Flash Screen Video encoder
3 * Copyright (C) 2004 Alex Beregszaszi
4 * Copyright (C) 2006 Benjamin Larsson
5 *
2912e87a 6 * This file is part of Libav.
053185c2 7 *
2912e87a 8 * Libav is free software; you can redistribute it and/or
053185c2
BL
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 *
2912e87a 13 * Libav is distributed in the hope that it will be useful,
053185c2
BL
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
2912e87a 19 * License along with Libav; if not, write to the Free Software
053185c2
BL
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23/* Encoding development sponsored by http://fh-campuswien.ac.at */
24
25/**
ba87f080 26 * @file
053185c2
BL
27 * Flash Screen Video encoder
28 * @author Alex Beregszaszi
29 * @author Benjamin Larsson
30 */
31
32/* Bitstream description
33 * The picture is divided into blocks that are zlib-compressed.
34 *
35 * The decoder is fed complete frames, the frameheader contains:
36 * 4bits of block width
37 * 12bits of frame width
38 * 4bits of block height
39 * 12bits of frame height
40 *
41 * Directly after the header are the compressed blocks. The blocks
42 * have their compressed size represented with 16bits in the beginig.
43 * If the size = 0 then the block is unchanged from the previous frame.
44 * All blocks are decompressed until the buffer is consumed.
45 *
46 * Encoding ideas, a basic encoder would just use a fixed block size.
47 * Block sizes can be multipels of 16, from 16 to 256. The blocks don't
48 * have to be quadratic. A brute force search with a set of different
49 * block sizes should give a better result than to just use a fixed size.
50 */
51
52/* TODO:
53 * Don't reencode the frame in brute force mode if the frame is a dupe. Speed up.
54 * Make the difference check faster.
55 */
56
57#include <stdio.h>
58#include <stdlib.h>
59#include <zlib.h>
60
053185c2 61#include "avcodec.h"
b2755007 62#include "put_bits.h"
053185c2
BL
63#include "bytestream.h"
64
65
66typedef struct FlashSVContext {
67 AVCodecContext *avctx;
293fe6da
DB
68 uint8_t *previous_frame;
69 AVFrame frame;
70 int image_width, image_height;
71 int block_width, block_height;
72 uint8_t *tmpblock;
73 uint8_t *encbuffer;
74 int block_size;
75 z_stream zstream;
76 int last_key_frame;
053185c2
BL
77} FlashSVContext;
78
293fe6da
DB
79static int copy_region_enc(uint8_t *sptr, uint8_t *dptr, int dx, int dy,
80 int h, int w, int stride, uint8_t *pfptr)
81{
82 int i, j;
053185c2
BL
83 uint8_t *nsptr;
84 uint8_t *npfptr;
85 int diff = 0;
86
293fe6da
DB
87 for (i = dx + h; i > dx; i--) {
88 nsptr = sptr + (i * stride) + dy * 3;
89 npfptr = pfptr + (i * stride) + dy * 3;
90 for (j = 0; j < w * 3; j++) {
91 diff |= npfptr[j] ^ nsptr[j];
92 dptr[j] = nsptr[j];
053185c2 93 }
293fe6da 94 dptr += w * 3;
053185c2
BL
95 }
96 if (diff)
97 return 1;
98 return 0;
99}
100
98a6fff9 101static av_cold int flashsv_encode_init(AVCodecContext *avctx)
053185c2 102{
e4141433 103 FlashSVContext *s = avctx->priv_data;
053185c2
BL
104
105 s->avctx = avctx;
106
107 if ((avctx->width > 4095) || (avctx->height > 4095)) {
108 av_log(avctx, AV_LOG_ERROR, "Input dimensions too large, input must be max 4096x4096 !\n");
109 return -1;
110 }
111
053185c2
BL
112 // Needed if zlib unused or init aborted before deflateInit
113 memset(&(s->zstream), 0, sizeof(z_stream));
053185c2 114
293fe6da 115 s->last_key_frame = 0;
8736d68a 116
293fe6da 117 s->image_width = avctx->width;
053185c2
BL
118 s->image_height = avctx->height;
119
293fe6da
DB
120 s->tmpblock = av_mallocz(3 * 256 * 256);
121 s->encbuffer = av_mallocz(s->image_width * s->image_height * 3);
053185c2
BL
122
123 if (!s->tmpblock || !s->encbuffer) {
124 av_log(avctx, AV_LOG_ERROR, "Memory allocation failed.\n");
125 return -1;
126 }
127
128 return 0;
129}
130
131
293fe6da
DB
132static int encode_bitstream(FlashSVContext *s, AVFrame *p, uint8_t *buf,
133 int buf_size, int block_width, int block_height,
134 uint8_t *previous_frame, int *I_frame)
135{
053185c2
BL
136
137 PutBitContext pb;
138 int h_blocks, v_blocks, h_part, v_part, i, j;
139 int buf_pos, res;
140 int pred_blocks = 0;
141
293fe6da 142 init_put_bits(&pb, buf, buf_size * 8);
053185c2 143
293fe6da 144 put_bits(&pb, 4, (block_width / 16) - 1);
053185c2 145 put_bits(&pb, 12, s->image_width);
293fe6da 146 put_bits(&pb, 4, (block_height / 16) - 1);
053185c2
BL
147 put_bits(&pb, 12, s->image_height);
148 flush_put_bits(&pb);
293fe6da 149 buf_pos = 4;
053185c2 150
293fe6da
DB
151 h_blocks = s->image_width / block_width;
152 h_part = s->image_width % block_width;
053185c2 153 v_blocks = s->image_height / block_height;
293fe6da 154 v_part = s->image_height % block_height;
053185c2
BL
155
156 /* loop over all block columns */
293fe6da 157 for (j = 0; j < v_blocks + (v_part ? 1 : 0); j++) {
053185c2 158
293fe6da
DB
159 int hp = j * block_height; // horiz position in frame
160 int hs = (j < v_blocks) ? block_height : v_part; // size of block
053185c2
BL
161
162 /* loop over all block rows */
293fe6da
DB
163 for (i = 0; i < h_blocks + (h_part ? 1 : 0); i++) {
164 int wp = i * block_width; // vert position in frame
165 int ws = (i < h_blocks) ? block_width : h_part; // size of block
166 int ret = Z_OK;
053185c2
BL
167 uint8_t *ptr;
168
293fe6da 169 ptr = buf + buf_pos;
053185c2 170
293fe6da
DB
171 /* copy the block to the temp buffer before compression
172 * (if it differs from the previous frame's block) */
173 res = copy_region_enc(p->data[0], s->tmpblock,
174 s->image_height - (hp + hs + 1),
175 wp, hs, ws, p->linesize[0], previous_frame);
053185c2
BL
176
177 if (res || *I_frame) {
178 unsigned long zsize;
293fe6da
DB
179 zsize = 3 * block_width * block_height;
180 ret = compress2(ptr + 2, &zsize, s->tmpblock, 3 * ws * hs, 9);
053185c2
BL
181
182
183 //ret = deflateReset(&(s->zstream));
184 if (ret != Z_OK)
185 av_log(s->avctx, AV_LOG_ERROR, "error while compressing block %dx%d\n", i, j);
053185c2 186
293fe6da
DB
187 bytestream_put_be16(&ptr, (unsigned int) zsize);
188 buf_pos += zsize + 2;
053185c2
BL
189 //av_log(avctx, AV_LOG_ERROR, "buf_pos = %d\n", buf_pos);
190 } else {
191 pred_blocks++;
293fe6da 192 bytestream_put_be16(&ptr, 0);
8adc51f2 193 buf_pos += 2;
053185c2
BL
194 }
195 }
196 }
197
198 if (pred_blocks)
199 *I_frame = 0;
200 else
201 *I_frame = 1;
202
203 return buf_pos;
204}
205
206
293fe6da
DB
207static int flashsv_encode_frame(AVCodecContext *avctx, uint8_t *buf,
208 int buf_size, void *data)
053185c2 209{
e4141433 210 FlashSVContext * const s = avctx->priv_data;
053185c2
BL
211 AVFrame *pict = data;
212 AVFrame * const p = &s->frame;
d2bc0473 213 uint8_t *pfptr;
053185c2
BL
214 int res;
215 int I_frame = 0;
216 int opt_w, opt_h;
217
218 *p = *pict;
219
8736d68a 220 /* First frame needs to be a keyframe */
64472fcf 221 if (avctx->frame_number == 0) {
293fe6da 222 s->previous_frame = av_mallocz(FFABS(p->linesize[0]) * s->image_height);
053185c2
BL
223 if (!s->previous_frame) {
224 av_log(avctx, AV_LOG_ERROR, "Memory allocation failed.\n");
225 return -1;
226 }
227 I_frame = 1;
053185c2
BL
228 }
229
d2bc0473 230 if (p->linesize[0] < 0)
293fe6da 231 pfptr = s->previous_frame - ((s->image_height - 1) * p->linesize[0]);
d2bc0473
BL
232 else
233 pfptr = s->previous_frame;
234
8736d68a
BL
235 /* Check the placement of keyframes */
236 if (avctx->gop_size > 0) {
237 if (avctx->frame_number >= s->last_key_frame + avctx->gop_size) {
238 I_frame = 1;
239 }
240 }
241
293fe6da
DB
242 opt_w = 4;
243 opt_h = 4;
053185c2
BL
244
245 if (buf_size < s->image_width*s->image_height*3) {
246 //Conservative upper bound check for compressed data
293fe6da
DB
247 av_log(avctx, AV_LOG_ERROR, "buf_size %d < %d\n",
248 buf_size, s->image_width * s->image_height * 3);
053185c2
BL
249 return -1;
250 }
251
293fe6da 252 res = encode_bitstream(s, p, buf, buf_size, opt_w * 16, opt_h * 16, pfptr, &I_frame);
54353abe 253
053185c2 254 //save the current frame
293fe6da
DB
255 if (p->linesize[0] > 0)
256 memcpy(s->previous_frame, p->data[0], s->image_height * p->linesize[0]);
d2bc0473 257 else
293fe6da
DB
258 memcpy(s->previous_frame, p->data[0] + p->linesize[0] * (s->image_height - 1),
259 s->image_height * FFABS(p->linesize[0]));
053185c2
BL
260
261 //mark the frame type so the muxer can mux it correctly
262 if (I_frame) {
263 p->pict_type = FF_I_TYPE;
264 p->key_frame = 1;
8736d68a 265 s->last_key_frame = avctx->frame_number;
293fe6da 266 av_log(avctx, AV_LOG_DEBUG, "Inserting key frame at frame %d\n", avctx->frame_number);
053185c2
BL
267 } else {
268 p->pict_type = FF_P_TYPE;
269 p->key_frame = 0;
270 }
271
272 avctx->coded_frame = p;
273
274 return res;
275}
276
98a6fff9 277static av_cold int flashsv_encode_end(AVCodecContext *avctx)
053185c2 278{
e4141433 279 FlashSVContext *s = avctx->priv_data;
053185c2
BL
280
281 deflateEnd(&(s->zstream));
282
283 av_free(s->encbuffer);
284 av_free(s->previous_frame);
285 av_free(s->tmpblock);
286
287 return 0;
288}
289
d36beb3f 290AVCodec ff_flashsv_encoder = {
46cb2da1
DB
291 .name = "flashsv",
292 .type = AVMEDIA_TYPE_VIDEO,
293 .id = CODEC_ID_FLASHSV,
294 .priv_data_size = sizeof(FlashSVContext),
295 .init = flashsv_encode_init,
296 .encode = flashsv_encode_frame,
297 .close = flashsv_encode_end,
298 .pix_fmts = (const enum PixelFormat[]){PIX_FMT_BGR24, PIX_FMT_NONE},
299 .long_name = NULL_IF_CONFIG_SMALL("Flash Screen Video"),
053185c2
BL
300};
301