FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/flashsvenc.c
Date: 2024-04-19 17:50:32
Exec Total Coverage
Lines: 84 94 89.4%
Functions: 5 5 100.0%
Branches: 29 40 72.5%

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 <stdint.h>
48 #include <zlib.h>
49
50 #include "libavutil/buffer.h"
51
52 #include "avcodec.h"
53 #include "codec_internal.h"
54 #include "encode.h"
55 #include "put_bits.h"
56 #include "bytestream.h"
57
58 /* These values are hardcoded for now. */
59 #define BLOCK_WIDTH (4 * 16U)
60 #define BLOCK_HEIGHT (4 * 16U)
61
62 typedef struct FlashSVContext {
63 AVCodecContext *avctx;
64 const uint8_t *previous_frame;
65 AVBufferRef *prev_frame_buf;
66 int image_width, image_height;
67 unsigned packet_size;
68 int64_t last_key_frame;
69 uint8_t tmpblock[3 * 256 * 256];
70 } FlashSVContext;
71
72 4550 static int copy_region_enc(const uint8_t *sptr, uint8_t *dptr, int dx, int dy,
73 int h, int w, int stride, const uint8_t *pfptr)
74 {
75 int i, j;
76 4550 int diff = 0;
77
78
2/2
✓ Branch 0 taken 260900 times.
✓ Branch 1 taken 4550 times.
265450 for (i = dx + h; i > dx; i--) {
79 260900 const uint8_t *nsptr = sptr + i * stride + dy * 3;
80 260900 const uint8_t *npfptr = pfptr + i * stride + dy * 3;
81
2/2
✓ Branch 0 taken 45792600 times.
✓ Branch 1 taken 260900 times.
46053500 for (j = 0; j < w * 3; j++) {
82 45792600 diff |= npfptr[j] ^ nsptr[j];
83 45792600 dptr[j] = nsptr[j];
84 }
85 260900 dptr += w * 3;
86 }
87
2/2
✓ Branch 0 taken 4459 times.
✓ Branch 1 taken 91 times.
4550 if (diff)
88 4459 return 1;
89 91 return 0;
90 }
91
92 4 static av_cold int flashsv_encode_end(AVCodecContext *avctx)
93 {
94 4 FlashSVContext *s = avctx->priv_data;
95
96 4 av_buffer_unref(&s->prev_frame_buf);
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 int h_blocks, v_blocks, nb_blocks;
105
106 4 s->avctx = avctx;
107
108
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) {
109 av_log(avctx, AV_LOG_ERROR,
110 "Input dimensions too large, input must be max 4095x4095 !\n");
111 return AVERROR_INVALIDDATA;
112 }
113
114 4 s->last_key_frame = 0;
115
116 4 s->image_width = avctx->width;
117 4 s->image_height = avctx->height;
118
119 4 h_blocks = (s->image_width + BLOCK_WIDTH - 1) / BLOCK_WIDTH;
120 4 v_blocks = (s->image_height + BLOCK_WIDTH - 1) / BLOCK_WIDTH;
121 4 nb_blocks = h_blocks * v_blocks;
122 4 s->packet_size = 4 + nb_blocks * (2 + 3 * BLOCK_WIDTH * BLOCK_HEIGHT);
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 const 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
2/2
✓ Branch 0 taken 800 times.
✓ Branch 1 taken 200 times.
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
2/2
✓ Branch 0 taken 600 times.
✓ Branch 1 taken 200 times.
800 int cur_blk_height = (j < v_blocks) ? block_height : v_part;
157
158 /* loop over all block rows */
159
2/2
✓ Branch 0 taken 4550 times.
✓ Branch 1 taken 800 times.
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
2/2
✓ Branch 0 taken 3750 times.
✓ Branch 1 taken 800 times.
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 4550 p->linesize[0], previous_frame);
171
172
3/4
✓ Branch 0 taken 91 times.
✓ Branch 1 taken 4459 times.
✓ Branch 2 taken 91 times.
✗ Branch 3 not taken.
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
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4550 times.
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
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 200 times.
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 uint8_t *prev_frame = s->previous_frame;
206 int res;
207 200 int I_frame = 0;
208 200 int opt_w = 4, opt_h = 4;
209
210 /* First frame needs to be a keyframe */
211
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 196 times.
200 if (!s->previous_frame) {
212 4 prev_frame = pict->data[0];
213 4 I_frame = 1;
214 }
215
216 /* Check the placement of keyframes */
217
1/2
✓ Branch 0 taken 200 times.
✗ Branch 1 not taken.
200 if (avctx->gop_size > 0 &&
218
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 200 times.
200 avctx->frame_num >= s->last_key_frame + avctx->gop_size) {
219 I_frame = 1;
220 }
221
222 200 res = ff_alloc_packet(avctx, pkt, s->packet_size);
223
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 200 times.
200 if (res < 0)
224 return res;
225
226 200 pkt->size = encode_bitstream(s, pict, pkt->data, pkt->size,
227 opt_w * 16, opt_h * 16,
228 prev_frame, &I_frame);
229
230 //mark the frame type so the muxer can mux it correctly
231
1/2
✓ Branch 0 taken 200 times.
✗ Branch 1 not taken.
200 if (I_frame) {
232 200 s->last_key_frame = avctx->frame_num;
233 ff_dlog(avctx, "Inserting keyframe at frame %"PRId64"\n", avctx->frame_num);
234 }
235
236
1/2
✓ Branch 0 taken 200 times.
✗ Branch 1 not taken.
200 if (I_frame)
237 200 pkt->flags |= AV_PKT_FLAG_KEY;
238 200 *got_packet = 1;
239
240 //save the current frame
241 200 res = av_buffer_replace(&s->prev_frame_buf, pict->buf[0]);
242
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 200 times.
200 if (res < 0)
243 return res;
244 200 s->previous_frame = pict->data[0];
245
246 200 return 0;
247 }
248
249 const FFCodec ff_flashsv_encoder = {
250 .p.name = "flashsv",
251 CODEC_LONG_NAME("Flash Screen Video"),
252 .p.type = AVMEDIA_TYPE_VIDEO,
253 .p.id = AV_CODEC_ID_FLASHSV,
254 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
255 .priv_data_size = sizeof(FlashSVContext),
256 .init = flashsv_encode_init,
257 FF_CODEC_ENCODE_CB(flashsv_encode_frame),
258 .close = flashsv_encode_end,
259 .p.pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_BGR24, AV_PIX_FMT_NONE },
260 };
261