GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/flashsvenc.c Lines: 84 104 80.8 %
Date: 2020-09-25 23:16:12 Branches: 29 46 63.0 %

Line Branch Exec Source
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 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
/* 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 "internal.h"
53
#include "put_bits.h"
54
#include "bytestream.h"
55
56
57
typedef struct FlashSVContext {
58
    AVCodecContext *avctx;
59
    uint8_t        *previous_frame;
60
    int             image_width, image_height;
61
    int             block_width, block_height;
62
    uint8_t        *encbuffer;
63
    int             block_size;
64
    int             last_key_frame;
65
    uint8_t         tmpblock[3 * 256 * 256];
66
} FlashSVContext;
67
68
4550
static int copy_region_enc(uint8_t *sptr, uint8_t *dptr, int dx, int dy,
69
                           int h, int w, int stride, uint8_t *pfptr)
70
{
71
    int i, j;
72
    uint8_t *nsptr;
73
    uint8_t *npfptr;
74
4550
    int diff = 0;
75
76
265450
    for (i = dx + h; i > dx; i--) {
77
260900
        nsptr  = sptr  + i * stride + dy * 3;
78
260900
        npfptr = pfptr + i * stride + dy * 3;
79
46053500
        for (j = 0; j < w * 3; j++) {
80
45792600
            diff    |= npfptr[j] ^ nsptr[j];
81
45792600
            dptr[j]  = nsptr[j];
82
        }
83
260900
        dptr += w * 3;
84
    }
85
4550
    if (diff)
86
4550
        return 1;
87
    return 0;
88
}
89
90
4
static av_cold int flashsv_encode_end(AVCodecContext *avctx)
91
{
92
4
    FlashSVContext *s = avctx->priv_data;
93
94
4
    av_freep(&s->encbuffer);
95
4
    av_freep(&s->previous_frame);
96
97
4
    return 0;
98
}
99
100
4
static av_cold int flashsv_encode_init(AVCodecContext *avctx)
101
{
102
4
    FlashSVContext *s = avctx->priv_data;
103
104
4
    s->avctx = avctx;
105
106

4
    if (avctx->width > 4095 || avctx->height > 4095) {
107
        av_log(avctx, AV_LOG_ERROR,
108
               "Input dimensions too large, input must be max 4095x4095 !\n");
109
        return AVERROR_INVALIDDATA;
110
    }
111
112
4
    s->last_key_frame = 0;
113
114
4
    s->image_width  = avctx->width;
115
4
    s->image_height = avctx->height;
116
117
4
    s->encbuffer = av_mallocz(s->image_width * s->image_height * 3);
118
119
4
    if (!s->encbuffer) {
120
        av_log(avctx, AV_LOG_ERROR, "Memory allocation failed.\n");
121
        return AVERROR(ENOMEM);
122
    }
123
124
4
    return 0;
125
}
126
127
128
200
static int encode_bitstream(FlashSVContext *s, const AVFrame *p, uint8_t *buf,
129
                            int buf_size, int block_width, int block_height,
130
                            uint8_t *previous_frame, int *I_frame)
131
{
132
133
    PutBitContext pb;
134
    int h_blocks, v_blocks, h_part, v_part, i, j;
135
    int buf_pos, res;
136
200
    int pred_blocks = 0;
137
138
200
    init_put_bits(&pb, buf, buf_size);
139
140
200
    put_bits(&pb,  4, block_width / 16 - 1);
141
200
    put_bits(&pb, 12, s->image_width);
142
200
    put_bits(&pb,  4, block_height / 16 - 1);
143
200
    put_bits(&pb, 12, s->image_height);
144
200
    flush_put_bits(&pb);
145
200
    buf_pos = 4;
146
147
200
    h_blocks = s->image_width  / block_width;
148
200
    h_part   = s->image_width  % block_width;
149
200
    v_blocks = s->image_height / block_height;
150
200
    v_part   = s->image_height % block_height;
151
152
    /* loop over all block columns */
153
1000
    for (j = 0; j < v_blocks + (v_part ? 1 : 0); j++) {
154
155
800
        int y_pos = j * block_height; // vertical position in frame
156
800
        int cur_blk_height = (j < v_blocks) ? block_height : v_part;
157
158
        /* loop over all block rows */
159
5350
        for (i = 0; i < h_blocks + (h_part ? 1 : 0); i++) {
160
4550
            int x_pos = i * block_width; // horizontal position in frame
161
4550
            int cur_blk_width = (i < h_blocks) ? block_width : h_part;
162
4550
            int ret = Z_OK;
163
4550
            uint8_t *ptr = buf + buf_pos;
164
165
            /* copy the block to the temp buffer before compression
166
             * (if it differs from the previous frame's block) */
167
4550
            res = copy_region_enc(p->data[0], s->tmpblock,
168
4550
                                  s->image_height - (y_pos + cur_blk_height + 1),
169
                                  x_pos, cur_blk_height, cur_blk_width,
170
                                  p->linesize[0], previous_frame);
171
172

4550
            if (res || *I_frame) {
173
4550
                unsigned long zsize = 3 * block_width * block_height;
174
4550
                ret = compress2(ptr + 2, &zsize, s->tmpblock,
175
4550
                                3 * cur_blk_width * cur_blk_height, 9);
176
177
4550
                if (ret != Z_OK)
178
                    av_log(s->avctx, AV_LOG_ERROR,
179
                           "error while compressing block %dx%d\n", i, j);
180
181
4550
                bytestream_put_be16(&ptr, zsize);
182
4550
                buf_pos += zsize + 2;
183
                ff_dlog(s->avctx, "buf_pos = %d\n", buf_pos);
184
            } else {
185
                pred_blocks++;
186
                bytestream_put_be16(&ptr, 0);
187
                buf_pos += 2;
188
            }
189
        }
190
    }
191
192
200
    if (pred_blocks)
193
        *I_frame = 0;
194
    else
195
200
        *I_frame = 1;
196
197
200
    return buf_pos;
198
}
199
200
201
200
static int flashsv_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
202
                                const AVFrame *pict, int *got_packet)
203
{
204
200
    FlashSVContext * const s = avctx->priv_data;
205
200
    const AVFrame * const p = pict;
206
    uint8_t *pfptr;
207
    int res;
208
200
    int I_frame = 0;
209
200
    int opt_w = 4, opt_h = 4;
210
211
    /* First frame needs to be a keyframe */
212
200
    if (avctx->frame_number == 0) {
213
4
        s->previous_frame = av_mallocz(FFABS(p->linesize[0]) * s->image_height);
214
4
        if (!s->previous_frame) {
215
            av_log(avctx, AV_LOG_ERROR, "Memory allocation failed.\n");
216
            return AVERROR(ENOMEM);
217
        }
218
4
        I_frame = 1;
219
    }
220
221
200
    if (p->linesize[0] < 0)
222
        pfptr = s->previous_frame - (s->image_height - 1) * p->linesize[0];
223
    else
224
200
        pfptr = s->previous_frame;
225
226
    /* Check the placement of keyframes */
227
200
    if (avctx->gop_size > 0 &&
228
200
        avctx->frame_number >= s->last_key_frame + avctx->gop_size) {
229
        I_frame = 1;
230
    }
231
232
200
    if ((res = ff_alloc_packet2(avctx, pkt, s->image_width * s->image_height * 3, 0)) < 0)
233
        return res;
234
235
200
    pkt->size = encode_bitstream(s, p, pkt->data, pkt->size, opt_w * 16, opt_h * 16,
236
                                 pfptr, &I_frame);
237
238
    //save the current frame
239
200
    if (p->linesize[0] > 0)
240
200
        memcpy(s->previous_frame, p->data[0], s->image_height * p->linesize[0]);
241
    else
242
        memcpy(s->previous_frame,
243
               p->data[0] + p->linesize[0] * (s->image_height - 1),
244
               s->image_height * FFABS(p->linesize[0]));
245
246
    //mark the frame type so the muxer can mux it correctly
247
200
    if (I_frame) {
248
#if FF_API_CODED_FRAME
249
FF_DISABLE_DEPRECATION_WARNINGS
250
200
        avctx->coded_frame->pict_type      = AV_PICTURE_TYPE_I;
251
200
        avctx->coded_frame->key_frame      = 1;
252
FF_ENABLE_DEPRECATION_WARNINGS
253
#endif
254
200
        s->last_key_frame = avctx->frame_number;
255
        ff_dlog(avctx, "Inserting keyframe at frame %d\n", avctx->frame_number);
256
    } else {
257
#if FF_API_CODED_FRAME
258
FF_DISABLE_DEPRECATION_WARNINGS
259
        avctx->coded_frame->pict_type = AV_PICTURE_TYPE_P;
260
        avctx->coded_frame->key_frame = 0;
261
FF_ENABLE_DEPRECATION_WARNINGS
262
#endif
263
    }
264
265
200
    if (I_frame)
266
200
        pkt->flags |= AV_PKT_FLAG_KEY;
267
200
    *got_packet = 1;
268
269
200
    return 0;
270
}
271
272
AVCodec ff_flashsv_encoder = {
273
    .name           = "flashsv",
274
    .long_name      = NULL_IF_CONFIG_SMALL("Flash Screen Video"),
275
    .type           = AVMEDIA_TYPE_VIDEO,
276
    .id             = AV_CODEC_ID_FLASHSV,
277
    .priv_data_size = sizeof(FlashSVContext),
278
    .init           = flashsv_encode_init,
279
    .encode2        = flashsv_encode_frame,
280
    .close          = flashsv_encode_end,
281
    .pix_fmts       = (const enum AVPixelFormat[]){ AV_PIX_FMT_BGR24, AV_PIX_FMT_NONE },
282
};