2 * Flash Screen Video encoder
3 * Copyright (C) 2004 Alex Beregszaszi
4 * Copyright (C) 2006 Benjamin Larsson
6 * This file is part of FFmpeg.
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.
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.
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
23 /* Encoding development sponsored by http://fh-campuswien.ac.at */
27 * Flash Screen Video encoder
28 * @author Alex Beregszaszi
29 * @author Benjamin Larsson
31 * A description of the bitstream format for Flash Screen Video version 1/2
32 * is part of the SWF File Format Specification (version 10), which can be
33 * downloaded from http://www.adobe.com/devnet/swf.html.
37 * Encoding ideas: A basic encoder would just use a fixed block size.
38 * Block sizes can be multiples of 16, from 16 to 256. The blocks don't
39 * have to be quadratic. A brute force search with a set of different
40 * block sizes should give a better result than to just use a fixed size.
43 * Don't reencode the frame in brute force mode if the frame is a dupe.
44 * Speed up. Make the difference check faster.
54 #include "bytestream.h"
57 typedef struct FlashSVContext {
58 AVCodecContext *avctx;
59 uint8_t *previous_frame;
60 int image_width, image_height;
61 int block_width, block_height;
66 uint8_t tmpblock[3 * 256 * 256];
69 static int copy_region_enc(uint8_t *sptr, uint8_t *dptr, int dx, int dy,
70 int h, int w, int stride, uint8_t *pfptr)
77 for (i = dx + h; i > dx; i--) {
78 nsptr = sptr + i * stride + dy * 3;
79 npfptr = pfptr + i * stride + dy * 3;
80 for (j = 0; j < w * 3; j++) {
81 diff |= npfptr[j] ^ nsptr[j];
91 static av_cold int flashsv_encode_end(AVCodecContext *avctx)
93 FlashSVContext *s = avctx->priv_data;
95 deflateEnd(&s->zstream);
97 av_freep(&s->encbuffer);
98 av_freep(&s->previous_frame);
103 static av_cold int flashsv_encode_init(AVCodecContext *avctx)
105 FlashSVContext *s = avctx->priv_data;
109 if (avctx->width > 4095 || avctx->height > 4095) {
110 av_log(avctx, AV_LOG_ERROR,
111 "Input dimensions too large, input must be max 4095x4095 !\n");
112 return AVERROR_INVALIDDATA;
115 // Needed if zlib unused or init aborted before deflateInit
116 memset(&s->zstream, 0, sizeof(z_stream));
118 s->last_key_frame = 0;
120 s->image_width = avctx->width;
121 s->image_height = avctx->height;
123 s->encbuffer = av_mallocz(s->image_width * s->image_height * 3);
126 av_log(avctx, AV_LOG_ERROR, "Memory allocation failed.\n");
127 return AVERROR(ENOMEM);
134 static int encode_bitstream(FlashSVContext *s, const AVFrame *p, uint8_t *buf,
135 int buf_size, int block_width, int block_height,
136 uint8_t *previous_frame, int *I_frame)
140 int h_blocks, v_blocks, h_part, v_part, i, j;
144 init_put_bits(&pb, buf, buf_size);
146 put_bits(&pb, 4, block_width / 16 - 1);
147 put_bits(&pb, 12, s->image_width);
148 put_bits(&pb, 4, block_height / 16 - 1);
149 put_bits(&pb, 12, s->image_height);
153 h_blocks = s->image_width / block_width;
154 h_part = s->image_width % block_width;
155 v_blocks = s->image_height / block_height;
156 v_part = s->image_height % block_height;
158 /* loop over all block columns */
159 for (j = 0; j < v_blocks + (v_part ? 1 : 0); j++) {
161 int y_pos = j * block_height; // vertical position in frame
162 int cur_blk_height = (j < v_blocks) ? block_height : v_part;
164 /* loop over all block rows */
165 for (i = 0; i < h_blocks + (h_part ? 1 : 0); i++) {
166 int x_pos = i * block_width; // horizontal position in frame
167 int cur_blk_width = (i < h_blocks) ? block_width : h_part;
169 uint8_t *ptr = buf + buf_pos;
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 - (y_pos + cur_blk_height + 1),
175 x_pos, cur_blk_height, cur_blk_width,
176 p->linesize[0], previous_frame);
178 if (res || *I_frame) {
179 unsigned long zsize = 3 * block_width * block_height;
180 ret = compress2(ptr + 2, &zsize, s->tmpblock,
181 3 * cur_blk_width * cur_blk_height, 9);
183 //ret = deflateReset(&s->zstream);
185 av_log(s->avctx, AV_LOG_ERROR,
186 "error while compressing block %dx%d\n", i, j);
188 bytestream_put_be16(&ptr, zsize);
189 buf_pos += zsize + 2;
190 ff_dlog(s->avctx, "buf_pos = %d\n", buf_pos);
193 bytestream_put_be16(&ptr, 0);
208 static int flashsv_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
209 const AVFrame *pict, int *got_packet)
211 FlashSVContext * const s = avctx->priv_data;
212 const AVFrame * const p = pict;
216 int opt_w = 4, opt_h = 4;
218 /* First frame needs to be a keyframe */
219 if (avctx->frame_number == 0) {
220 s->previous_frame = av_mallocz(FFABS(p->linesize[0]) * s->image_height);
221 if (!s->previous_frame) {
222 av_log(avctx, AV_LOG_ERROR, "Memory allocation failed.\n");
223 return AVERROR(ENOMEM);
228 if (p->linesize[0] < 0)
229 pfptr = s->previous_frame - (s->image_height - 1) * p->linesize[0];
231 pfptr = s->previous_frame;
233 /* Check the placement of keyframes */
234 if (avctx->gop_size > 0 &&
235 avctx->frame_number >= s->last_key_frame + avctx->gop_size) {
239 if ((res = ff_alloc_packet2(avctx, pkt, s->image_width * s->image_height * 3, 0)) < 0)
242 pkt->size = encode_bitstream(s, p, pkt->data, pkt->size, opt_w * 16, opt_h * 16,
245 //save the current frame
246 if (p->linesize[0] > 0)
247 memcpy(s->previous_frame, p->data[0], s->image_height * p->linesize[0]);
249 memcpy(s->previous_frame,
250 p->data[0] + p->linesize[0] * (s->image_height - 1),
251 s->image_height * FFABS(p->linesize[0]));
253 //mark the frame type so the muxer can mux it correctly
255 #if FF_API_CODED_FRAME
256 FF_DISABLE_DEPRECATION_WARNINGS
257 avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
258 avctx->coded_frame->key_frame = 1;
259 FF_ENABLE_DEPRECATION_WARNINGS
261 s->last_key_frame = avctx->frame_number;
262 ff_dlog(avctx, "Inserting keyframe at frame %d\n", avctx->frame_number);
264 #if FF_API_CODED_FRAME
265 FF_DISABLE_DEPRECATION_WARNINGS
266 avctx->coded_frame->pict_type = AV_PICTURE_TYPE_P;
267 avctx->coded_frame->key_frame = 0;
268 FF_ENABLE_DEPRECATION_WARNINGS
273 pkt->flags |= AV_PKT_FLAG_KEY;
279 AVCodec ff_flashsv_encoder = {
281 .long_name = NULL_IF_CONFIG_SMALL("Flash Screen Video"),
282 .type = AVMEDIA_TYPE_VIDEO,
283 .id = AV_CODEC_ID_FLASHSV,
284 .priv_data_size = sizeof(FlashSVContext),
285 .init = flashsv_encode_init,
286 .encode2 = flashsv_encode_frame,
287 .close = flashsv_encode_end,
288 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_BGR24, AV_PIX_FMT_NONE },