FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/flashsvenc.c
Date: 2021-09-23 20:34:37
Exec Total Coverage
Lines: 83 101 82.2%
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 "encode.h"
53 #include "internal.h"
54 #include "put_bits.h"
55 #include "bytestream.h"
56
57
58 typedef struct FlashSVContext {
59 AVCodecContext *avctx;
60 uint8_t *previous_frame;
61 int image_width, image_height;
62 int block_width, block_height;
63 uint8_t *encbuffer;
64 int block_size;
65 int last_key_frame;
66 uint8_t tmpblock[3 * 256 * 256];
67 } FlashSVContext;
68
69 4550 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)
71 {
72 int i, j;
73 uint8_t *nsptr;
74 uint8_t *npfptr;
75 4550 int diff = 0;
76
77
2/2
✓ Branch 0 taken 260900 times.
✓ Branch 1 taken 4550 times.
265450 for (i = dx + h; i > dx; i--) {
78 260900 nsptr = sptr + i * stride + dy * 3;
79 260900 npfptr = pfptr + i * stride + dy * 3;
80
2/2
✓ Branch 0 taken 45792600 times.
✓ Branch 1 taken 260900 times.
46053500 for (j = 0; j < w * 3; j++) {
81 45792600 diff |= npfptr[j] ^ nsptr[j];
82 45792600 dptr[j] = nsptr[j];
83 }
84 260900 dptr += w * 3;
85 }
86
1/2
✓ Branch 0 taken 4550 times.
✗ Branch 1 not taken.
4550 if (diff)
87 4550 return 1;
88 return 0;
89 }
90
91 4 static av_cold int flashsv_encode_end(AVCodecContext *avctx)
92 {
93 4 FlashSVContext *s = avctx->priv_data;
94
95 4 av_freep(&s->encbuffer);
96 4 av_freep(&s->previous_frame);
97
98 4 return 0;
99 }
100
101 4 static av_cold int flashsv_encode_init(AVCodecContext *avctx)
102 {
103 4 FlashSVContext *s = avctx->priv_data;
104
105 4 s->avctx = avctx;
106
107
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
4 if (avctx->width > 4095 || avctx->height > 4095) {
108 av_log(avctx, AV_LOG_ERROR,
109 "Input dimensions too large, input must be max 4095x4095 !\n");
110 return AVERROR_INVALIDDATA;
111 }
112
113 4 s->last_key_frame = 0;
114
115 4 s->image_width = avctx->width;
116 4 s->image_height = avctx->height;
117
118 4 s->encbuffer = av_mallocz(s->image_width * s->image_height * 3);
119
120
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!s->encbuffer) {
121 av_log(avctx, AV_LOG_ERROR, "Memory allocation failed.\n");
122 return AVERROR(ENOMEM);
123 }
124
125 4 return 0;
126 }
127
128
129 200 static int encode_bitstream(FlashSVContext *s, const AVFrame *p, uint8_t *buf,
130 int buf_size, int block_width, int block_height,
131 uint8_t *previous_frame, int *I_frame)
132 {
133
134 PutBitContext pb;
135 int h_blocks, v_blocks, h_part, v_part, i, j;
136 int buf_pos, res;
137 200 int pred_blocks = 0;
138
139 200 init_put_bits(&pb, buf, buf_size);
140
141 200 put_bits(&pb, 4, block_width / 16 - 1);
142 200 put_bits(&pb, 12, s->image_width);
143 200 put_bits(&pb, 4, block_height / 16 - 1);
144 200 put_bits(&pb, 12, s->image_height);
145 200 flush_put_bits(&pb);
146 200 buf_pos = 4;
147
148 200 h_blocks = s->image_width / block_width;
149 200 h_part = s->image_width % block_width;
150 200 v_blocks = s->image_height / block_height;
151 200 v_part = s->image_height % block_height;
152
153 /* loop over all block columns */
154
2/2
✓ Branch 0 taken 800 times.
✓ Branch 1 taken 200 times.
1000 for (j = 0; j < v_blocks + (v_part ? 1 : 0); j++) {
155
156 800 int y_pos = j * block_height; // vertical position in frame
157
2/2
✓ Branch 0 taken 600 times.
✓ Branch 1 taken 200 times.
800 int cur_blk_height = (j < v_blocks) ? block_height : v_part;
158
159 /* loop over all block rows */
160
2/2
✓ Branch 0 taken 4550 times.
✓ Branch 1 taken 800 times.
5350 for (i = 0; i < h_blocks + (h_part ? 1 : 0); i++) {
161 4550 int x_pos = i * block_width; // horizontal position in frame
162
2/2
✓ Branch 0 taken 3750 times.
✓ Branch 1 taken 800 times.
4550 int cur_blk_width = (i < h_blocks) ? block_width : h_part;
163 4550 int ret = Z_OK;
164 4550 uint8_t *ptr = buf + buf_pos;
165
166 /* copy the block to the temp buffer before compression
167 * (if it differs from the previous frame's block) */
168 4550 res = copy_region_enc(p->data[0], s->tmpblock,
169 4550 s->image_height - (y_pos + cur_blk_height + 1),
170 x_pos, cur_blk_height, cur_blk_width,
171 4550 p->linesize[0], previous_frame);
172
173
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 4550 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
4550 if (res || *I_frame) {
174 4550 unsigned long zsize = 3 * block_width * block_height;
175 4550 ret = compress2(ptr + 2, &zsize, s->tmpblock,
176 4550 3 * cur_blk_width * cur_blk_height, 9);
177
178
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4550 times.
4550 if (ret != Z_OK)
179 av_log(s->avctx, AV_LOG_ERROR,
180 "error while compressing block %dx%d\n", i, j);
181
182 4550 bytestream_put_be16(&ptr, zsize);
183 4550 buf_pos += zsize + 2;
184 ff_dlog(s->avctx, "buf_pos = %d\n", buf_pos);
185 } else {
186 pred_blocks++;
187 bytestream_put_be16(&ptr, 0);
188 buf_pos += 2;
189 }
190 }
191 }
192
193
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 200 times.
200 if (pred_blocks)
194 *I_frame = 0;
195 else
196 200 *I_frame = 1;
197
198 200 return buf_pos;
199 }
200
201
202 200 static int flashsv_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
203 const AVFrame *pict, int *got_packet)
204 {
205 200 FlashSVContext * const s = avctx->priv_data;
206 200 const AVFrame * const p = pict;
207 uint8_t *pfptr;
208 int res;
209 200 int I_frame = 0;
210 200 int opt_w = 4, opt_h = 4;
211
212 /* First frame needs to be a keyframe */
213
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 196 times.
200 if (avctx->frame_number == 0) {
214 4 s->previous_frame = av_mallocz(FFABS(p->linesize[0]) * s->image_height);
215
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!s->previous_frame) {
216 av_log(avctx, AV_LOG_ERROR, "Memory allocation failed.\n");
217 return AVERROR(ENOMEM);
218 }
219 4 I_frame = 1;
220 }
221
222
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 200 times.
200 if (p->linesize[0] < 0)
223 pfptr = s->previous_frame - (s->image_height - 1) * p->linesize[0];
224 else
225 200 pfptr = s->previous_frame;
226
227 /* Check the placement of keyframes */
228
1/2
✓ Branch 0 taken 200 times.
✗ Branch 1 not taken.
200 if (avctx->gop_size > 0 &&
229
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 200 times.
200 avctx->frame_number >= s->last_key_frame + avctx->gop_size) {
230 I_frame = 1;
231 }
232
233
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 200 times.
200 if ((res = ff_alloc_packet(avctx, pkt, s->image_width * s->image_height * 3)) < 0)
234 return res;
235
236 200 pkt->size = encode_bitstream(s, p, pkt->data, pkt->size, opt_w * 16, opt_h * 16,
237 pfptr, &I_frame);
238
239 //save the current frame
240
1/2
✓ Branch 0 taken 200 times.
✗ Branch 1 not taken.
200 if (p->linesize[0] > 0)
241 200 memcpy(s->previous_frame, p->data[0], s->image_height * p->linesize[0]);
242 else
243 memcpy(s->previous_frame,
244 p->data[0] + p->linesize[0] * (s->image_height - 1),
245 s->image_height * FFABS(p->linesize[0]));
246
247 //mark the frame type so the muxer can mux it correctly
248
1/2
✓ Branch 0 taken 200 times.
✗ Branch 1 not taken.
200 if (I_frame) {
249 200 s->last_key_frame = avctx->frame_number;
250 ff_dlog(avctx, "Inserting keyframe at frame %d\n", avctx->frame_number);
251 }
252
253
1/2
✓ Branch 0 taken 200 times.
✗ Branch 1 not taken.
200 if (I_frame)
254 200 pkt->flags |= AV_PKT_FLAG_KEY;
255 200 *got_packet = 1;
256
257 200 return 0;
258 }
259
260 const AVCodec ff_flashsv_encoder = {
261 .name = "flashsv",
262 .long_name = NULL_IF_CONFIG_SMALL("Flash Screen Video"),
263 .type = AVMEDIA_TYPE_VIDEO,
264 .id = AV_CODEC_ID_FLASHSV,
265 .priv_data_size = sizeof(FlashSVContext),
266 .init = flashsv_encode_init,
267 .encode2 = flashsv_encode_frame,
268 .close = flashsv_encode_end,
269 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_BGR24, AV_PIX_FMT_NONE },
270 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
271 };
272