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