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