FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/msvideo1enc.c
Date: 2021-09-24 20:55:06
Exec Total Coverage
Lines: 153 160 95.6%
Branches: 106 114 93.0%

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