FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/msvideo1enc.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 154 162 95.1%
Functions: 3 3 100.0%
Branches: 107 116 92.2%

Line Branch Exec Source
1 /*
2 * Microsoft Video-1 Encoder
3 * Copyright (c) 2009 Konstantin Shishkov
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * @file
24 * Microsoft Video-1 encoder
25 */
26
27 #include "avcodec.h"
28 #include "codec_internal.h"
29 #include "encode.h"
30 #include "bytestream.h"
31 #include "libavutil/lfg.h"
32 #include "libavutil/mem.h"
33 #include "elbg.h"
34 #include "libavutil/imgutils.h"
35 /**
36 * Encoder context
37 */
38 typedef struct Msvideo1EncContext {
39 AVCodecContext *avctx;
40 struct ELBGContext *elbg;
41 AVLFG rnd;
42 uint8_t *prev;
43
44 int block[16*3];
45 int block2[16*3];
46 int codebook[8*3];
47 int codebook2[8*3];
48 int output[16*3];
49 int output2[16*3];
50 int avg[3];
51 int bestpos;
52 int keyint;
53 } Msvideo1EncContext;
54
55 enum MSV1Mode{
56 MODE_SKIP = 0,
57 MODE_FILL,
58 MODE_2COL,
59 MODE_8COL,
60 };
61
62 #define SKIP_PREFIX 0x8400
63 #define SKIPS_MAX 0x03FF
64 #define MKRGB555(in, off) (((in)[off] << 10) | ((in)[(off) + 1] << 5) | ((in)[(off) + 2]))
65
66 static const int remap[16] = { 0, 1, 4, 5, 2, 3, 6, 7, 8, 9, 12, 13, 10, 11, 14, 15 };
67
68 150 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
69 const AVFrame *pict, int *got_packet)
70 {
71 150 Msvideo1EncContext * const c = avctx->priv_data;
72 150 const AVFrame *p = pict;
73 const uint16_t *src;
74 uint8_t *prevptr;
75 uint8_t *dst, *buf;
76 150 int keyframe = 0;
77 150 int no_skips = 1;
78 int i, j, k, x, y, ret;
79 150 int skips = 0;
80 150 int quality = 24;
81
82
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 150 times.
150 if ((ret = ff_alloc_packet(avctx, pkt, avctx->width*avctx->height*9 + FF_INPUT_BUFFER_MIN_SIZE)) < 0)
83 return ret;
84 150 dst= buf= pkt->data;
85
86
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 147 times.
150 if(!c->prev)
87 3 c->prev = av_malloc(avctx->width * 3 * (avctx->height + 3));
88
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 150 times.
150 if (!c->prev)
89 return AVERROR(ENOMEM);
90 150 prevptr = c->prev + avctx->width * 3 * (FFALIGN(avctx->height, 4) - 1);
91 150 src = (const uint16_t*)(p->data[0] + p->linesize[0]*(FFALIGN(avctx->height, 4) - 1));
92
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 144 times.
150 if(c->keyint >= avctx->keyint_min)
93 6 keyframe = 1;
94
95
96
2/2
✓ Branch 0 taken 10800 times.
✓ Branch 1 taken 150 times.
10950 for(y = 0; y < avctx->height; y += 4){
97
2/2
✓ Branch 0 taken 950400 times.
✓ Branch 1 taken 10800 times.
961200 for(x = 0; x < avctx->width; x += 4){
98 950400 int bestmode = MODE_SKIP;
99 950400 int bestscore = INT_MAX;
100 950400 int flags = 0;
101 int score;
102
103
2/2
✓ Branch 0 taken 3801600 times.
✓ Branch 1 taken 950400 times.
4752000 for(j = 0; j < 4; j++){
104
2/2
✓ Branch 0 taken 15206400 times.
✓ Branch 1 taken 3801600 times.
19008000 for(i = 0; i < 4; i++){
105 15206400 uint16_t val = src[x + i - j*p->linesize[0]/2];
106
2/2
✓ Branch 0 taken 45619200 times.
✓ Branch 1 taken 15206400 times.
60825600 for(k = 0; k < 3; k++){
107 45619200 c->block[(i + j*4)*3 + k] =
108 45619200 c->block2[remap[i + j*4]*3 + k] = (val >> (10-k*5)) & 0x1F;
109 }
110 }
111 }
112
2/2
✓ Branch 0 taken 912384 times.
✓ Branch 1 taken 38016 times.
950400 if(!keyframe){
113 912384 bestscore = 0;
114
2/2
✓ Branch 0 taken 3649536 times.
✓ Branch 1 taken 912384 times.
4561920 for(j = 0; j < 4; j++){
115
2/2
✓ Branch 0 taken 43794432 times.
✓ Branch 1 taken 3649536 times.
47443968 for(i = 0; i < 4*3; i++){
116 43794432 int t = prevptr[x*3 + i - j*3*avctx->width] - c->block[i + j*4*3];
117 43794432 bestscore += t*t;
118 }
119 }
120 912384 bestscore /= quality;
121 }
122 // try to find optimal value to fill whole 4x4 block
123 950400 score = 0;
124 950400 ret = avpriv_elbg_do(&c->elbg, c->block, 3, 16, c->avg,
125 950400 1, 1, c->output, &c->rnd, 0);
126
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 950400 times.
950400 if (ret < 0)
127 return ret;
128
2/2
✓ Branch 0 taken 12469 times.
✓ Branch 1 taken 937931 times.
950400 if(c->avg[0] == 1) // red component = 1 will be written as skip code
129 12469 c->avg[0] = 0;
130
2/2
✓ Branch 0 taken 3801600 times.
✓ Branch 1 taken 950400 times.
4752000 for(j = 0; j < 4; j++){
131
2/2
✓ Branch 0 taken 15206400 times.
✓ Branch 1 taken 3801600 times.
19008000 for(i = 0; i < 4; i++){
132
2/2
✓ Branch 0 taken 45619200 times.
✓ Branch 1 taken 15206400 times.
60825600 for(k = 0; k < 3; k++){
133 45619200 int t = c->avg[k] - c->block[(i+j*4)*3+k];
134 45619200 score += t*t;
135 }
136 }
137 }
138 950400 score /= quality;
139 950400 score += 2;
140
2/2
✓ Branch 0 taken 555268 times.
✓ Branch 1 taken 395132 times.
950400 if(score < bestscore){
141 555268 bestscore = score;
142 555268 bestmode = MODE_FILL;
143 }
144 // search for optimal filling of 2-color block
145 950400 score = 0;
146 950400 ret = avpriv_elbg_do(&c->elbg, c->block, 3, 16, c->codebook,
147 950400 2, 1, c->output, &c->rnd, 0);
148
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 950400 times.
950400 if (ret < 0)
149 return ret;
150 // last output value should be always 1, swap codebooks if needed
151
2/2
✓ Branch 0 taken 371444 times.
✓ Branch 1 taken 578956 times.
950400 if(!c->output[15]){
152
2/2
✓ Branch 0 taken 1114332 times.
✓ Branch 1 taken 371444 times.
1485776 for(i = 0; i < 3; i++)
153 1114332 FFSWAP(uint8_t, c->codebook[i], c->codebook[i+3]);
154
2/2
✓ Branch 0 taken 5943104 times.
✓ Branch 1 taken 371444 times.
6314548 for(i = 0; i < 16; i++)
155 5943104 c->output[i] ^= 1;
156 }
157
2/2
✓ Branch 0 taken 3801600 times.
✓ Branch 1 taken 950400 times.
4752000 for(j = 0; j < 4; j++){
158
2/2
✓ Branch 0 taken 15206400 times.
✓ Branch 1 taken 3801600 times.
19008000 for(i = 0; i < 4; i++){
159
2/2
✓ Branch 0 taken 45619200 times.
✓ Branch 1 taken 15206400 times.
60825600 for(k = 0; k < 3; k++){
160 45619200 int t = c->codebook[c->output[i+j*4]*3 + k] - c->block[i*3+k+j*4*3];
161 45619200 score += t*t;
162 }
163 }
164 }
165 950400 score /= quality;
166 950400 score += 6;
167
2/2
✓ Branch 0 taken 247307 times.
✓ Branch 1 taken 703093 times.
950400 if(score < bestscore){
168 247307 bestscore = score;
169 247307 bestmode = MODE_2COL;
170 }
171 // search for optimal filling of 2-color 2x2 subblocks
172 950400 score = 0;
173
2/2
✓ Branch 0 taken 3801600 times.
✓ Branch 1 taken 950400 times.
4752000 for(i = 0; i < 4; i++){
174 3801600 ret = avpriv_elbg_do(&c->elbg, c->block2 + i * 4 * 3, 3, 4,
175 3801600 c->codebook2 + i * 2 * 3, 2, 1,
176 3801600 c->output2 + i * 4, &c->rnd, 0);
177
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3801600 times.
3801600 if (ret < 0)
178 return ret;
179 }
180 // last value should be always 1, swap codebooks if needed
181
2/2
✓ Branch 0 taken 567641 times.
✓ Branch 1 taken 382759 times.
950400 if(!c->output2[15]){
182
2/2
✓ Branch 0 taken 1702923 times.
✓ Branch 1 taken 567641 times.
2270564 for(i = 0; i < 3; i++)
183 1702923 FFSWAP(uint8_t, c->codebook2[i+18], c->codebook2[i+21]);
184
2/2
✓ Branch 0 taken 2270564 times.
✓ Branch 1 taken 567641 times.
2838205 for(i = 12; i < 16; i++)
185 2270564 c->output2[i] ^= 1;
186 }
187
2/2
✓ Branch 0 taken 3801600 times.
✓ Branch 1 taken 950400 times.
4752000 for(j = 0; j < 4; j++){
188
2/2
✓ Branch 0 taken 15206400 times.
✓ Branch 1 taken 3801600 times.
19008000 for(i = 0; i < 4; i++){
189
2/2
✓ Branch 0 taken 45619200 times.
✓ Branch 1 taken 15206400 times.
60825600 for(k = 0; k < 3; k++){
190 45619200 int t = c->codebook2[(c->output2[remap[i+j*4]] + (i&2) + (j&2)*2)*3+k] - c->block[i*3+k + j*4*3];
191 45619200 score += t*t;
192 }
193 }
194 }
195 950400 score /= quality;
196 950400 score += 18;
197
2/2
✓ Branch 0 taken 143607 times.
✓ Branch 1 taken 806793 times.
950400 if(score < bestscore){
198 143607 bestscore = score;
199 143607 bestmode = MODE_8COL;
200 }
201
202
2/2
✓ Branch 0 taken 351933 times.
✓ Branch 1 taken 598467 times.
950400 if(bestmode == MODE_SKIP){
203 351933 skips++;
204 351933 no_skips = 0;
205 }
206
5/6
✓ Branch 0 taken 598467 times.
✓ Branch 1 taken 351933 times.
✓ Branch 2 taken 490987 times.
✓ Branch 3 taken 107480 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 842920 times.
950400 if((bestmode != MODE_SKIP && skips) || skips == SKIPS_MAX){
207 107480 bytestream_put_le16(&dst, skips | SKIP_PREFIX);
208 107480 skips = 0;
209 }
210
211
4/4
✓ Branch 0 taken 320440 times.
✓ Branch 1 taken 134420 times.
✓ Branch 2 taken 143607 times.
✓ Branch 3 taken 351933 times.
950400 switch(bestmode){
212 320440 case MODE_FILL:
213 320440 bytestream_put_le16(&dst, MKRGB555(c->avg,0) | 0x8000);
214
2/2
✓ Branch 0 taken 1281760 times.
✓ Branch 1 taken 320440 times.
1602200 for(j = 0; j < 4; j++)
215
2/2
✓ Branch 0 taken 5127040 times.
✓ Branch 1 taken 1281760 times.
6408800 for(i = 0; i < 4; i++)
216
2/2
✓ Branch 0 taken 15381120 times.
✓ Branch 1 taken 5127040 times.
20508160 for(k = 0; k < 3; k++)
217 15381120 prevptr[x*3 + i*3 + k - j*3*avctx->width] = c->avg[k];
218 320440 break;
219 134420 case MODE_2COL:
220
2/2
✓ Branch 0 taken 537680 times.
✓ Branch 1 taken 134420 times.
672100 for(j = 0; j < 4; j++){
221
2/2
✓ Branch 0 taken 2150720 times.
✓ Branch 1 taken 537680 times.
2688400 for(i = 0; i < 4; i++){
222 2150720 flags |= (c->output[i + j*4]^1) << (i + j*4);
223
2/2
✓ Branch 0 taken 6452160 times.
✓ Branch 1 taken 2150720 times.
8602880 for(k = 0; k < 3; k++)
224 6452160 prevptr[x*3 + i*3 + k - j*3*avctx->width] = c->codebook[c->output[i + j*4]*3 + k];
225 }
226 }
227 134420 bytestream_put_le16(&dst, flags);
228 134420 bytestream_put_le16(&dst, MKRGB555(c->codebook, 0));
229 134420 bytestream_put_le16(&dst, MKRGB555(c->codebook, 3));
230 134420 break;
231 143607 case MODE_8COL:
232
2/2
✓ Branch 0 taken 574428 times.
✓ Branch 1 taken 143607 times.
718035 for(j = 0; j < 4; j++){
233
2/2
✓ Branch 0 taken 2297712 times.
✓ Branch 1 taken 574428 times.
2872140 for(i = 0; i < 4; i++){
234 2297712 flags |= (c->output2[remap[i + j*4]]^1) << (i + j*4);
235
2/2
✓ Branch 0 taken 6893136 times.
✓ Branch 1 taken 2297712 times.
9190848 for(k = 0; k < 3; k++)
236 6893136 prevptr[x*3 + i*3 + k - j*3*avctx->width] = c->codebook2[(c->output2[remap[i+j*4]] + (i&2) + (j&2)*2)*3 + k];
237 }
238 }
239 143607 bytestream_put_le16(&dst, flags);
240 143607 bytestream_put_le16(&dst, MKRGB555(c->codebook2, 0) | 0x8000);
241
2/2
✓ Branch 0 taken 1005249 times.
✓ Branch 1 taken 143607 times.
1148856 for(i = 3; i < 24; i += 3)
242 1005249 bytestream_put_le16(&dst, MKRGB555(c->codebook2, i));
243 143607 break;
244 }
245 }
246 10800 src -= p->linesize[0] << 1;
247 10800 prevptr -= avctx->width * 3 * 4;
248 }
249
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 121 times.
150 if(skips)
250 29 bytestream_put_le16(&dst, skips | SKIP_PREFIX);
251 //EOF
252 150 bytestream_put_byte(&dst, 0);
253 150 bytestream_put_byte(&dst, 0);
254
255
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 144 times.
150 if(no_skips)
256 6 keyframe = 1;
257
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 144 times.
150 if(keyframe)
258 6 c->keyint = 0;
259 else
260 144 c->keyint++;
261
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 144 times.
150 if (keyframe) pkt->flags |= AV_PKT_FLAG_KEY;
262 150 pkt->size = dst - buf;
263 150 *got_packet = 1;
264
265 150 return 0;
266 }
267
268
269 /**
270 * init encoder
271 */
272 3 static av_cold int encode_init(AVCodecContext *avctx)
273 {
274 3 Msvideo1EncContext * const c = avctx->priv_data;
275
276 3 c->avctx = avctx;
277
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0) {
278 return -1;
279 }
280
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
3 if((avctx->width&3) || (avctx->height&3)){
281 av_log(avctx, AV_LOG_ERROR, "width and height must be multiples of 4\n");
282 return -1;
283 }
284
285 3 avctx->bits_per_coded_sample = 16;
286
287 3 c->keyint = avctx->keyint_min;
288 3 av_lfg_init(&c->rnd, 1);
289
290 3 return 0;
291 }
292
293
294
295 /**
296 * Uninit encoder
297 */
298 3 static av_cold int encode_end(AVCodecContext *avctx)
299 {
300 3 Msvideo1EncContext * const c = avctx->priv_data;
301
302 3 av_freep(&c->prev);
303 3 avpriv_elbg_free(&c->elbg);
304
305 3 return 0;
306 }
307
308 const FFCodec ff_msvideo1_encoder = {
309 .p.name = "msvideo1",
310 CODEC_LONG_NAME("Microsoft Video-1"),
311 .p.type = AVMEDIA_TYPE_VIDEO,
312 .p.id = AV_CODEC_ID_MSVIDEO1,
313 .p.capabilities = AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
314 .priv_data_size = sizeof(Msvideo1EncContext),
315 .init = encode_init,
316 FF_CODEC_ENCODE_CB(encode_frame),
317 .close = encode_end,
318 .p.pix_fmts = (const enum AVPixelFormat[]){AV_PIX_FMT_RGB555, AV_PIX_FMT_NONE},
319 };
320