FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mqcenc.c
Date: 2022-07-05 19:52:29
Exec Total Coverage
Lines: 75 75 100.0%
Branches: 23 24 95.8%

Line Branch Exec Source
1 /*
2 * MQ-coder encoder
3 * Copyright (c) 2007 Kamil Nowosad
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 * MQ-coder encoder
24 * @file
25 * @author Kamil Nowosad
26 */
27
28 #include <string.h>
29
30 #include "libavutil/avassert.h"
31 #include "mqc.h"
32
33 72808564 static void byteout(MqcState *mqc)
34 {
35 72808564 retry:
36
2/2
✓ Branch 0 taken 427677 times.
✓ Branch 1 taken 72380887 times.
72808564 if (*mqc->bp == 0xff){
37 427677 mqc->bp++;
38 427677 *mqc->bp = mqc->c >> 20;
39 427677 mqc->c &= 0xfffff;
40 427677 mqc->ct = 7;
41
2/2
✓ Branch 0 taken 2121958 times.
✓ Branch 1 taken 70258929 times.
72380887 } else if ((mqc->c & 0x8000000)){
42 2121958 (*mqc->bp)++;
43 2121958 mqc->c &= 0x7ffffff;
44 2121958 goto retry;
45 } else{
46 70258929 mqc->bp++;
47 70258929 *mqc->bp = mqc->c >> 19;
48 70258929 mqc->c &= 0x7ffff;
49 70258929 mqc->ct = 8;
50 }
51 70686606 }
52
53 442748377 static void renorme(MqcState *mqc)
54 {
55 do{
56 442748377 mqc->a += mqc->a;
57 442748377 mqc->c += mqc->c;
58
2/2
✓ Branch 0 taken 54983056 times.
✓ Branch 1 taken 387765321 times.
442748377 if (!--mqc->ct)
59 54983056 byteout(mqc);
60
2/2
✓ Branch 0 taken 111052716 times.
✓ Branch 1 taken 331695661 times.
442748377 } while (!(mqc->a & 0x8000));
61 331695661 }
62
63 7851775 static void setbits(MqcState *mqc)
64 {
65 7851775 int tmp = mqc->c + mqc->a;
66 7851775 mqc->c |= 0xffff;
67
2/2
✓ Branch 0 taken 2181134 times.
✓ Branch 1 taken 5670641 times.
7851775 if (mqc->c >= tmp)
68 2181134 mqc->c -= 0x8000;
69 7851775 }
70
71 413400 void ff_mqc_initenc(MqcState *mqc, uint8_t *bp)
72 {
73 413400 ff_mqc_init_contexts(mqc);
74 413400 mqc->a = 0x8000;
75 413400 mqc->c = 0;
76 413400 mqc->bp = bp-1;
77 413400 mqc->bpstart = bp;
78
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 413400 times.
413400 mqc->ct = 12 + (*mqc->bp == 0xff);
79 413400 }
80
81 538704935 void ff_mqc_encode(MqcState *mqc, uint8_t *cxstate, int d)
82 {
83 int qe;
84
85 538704935 qe = ff_mqc_qe[*cxstate];
86 538704935 mqc->a -= qe;
87
2/2
✓ Branch 0 taken 369558361 times.
✓ Branch 1 taken 169146574 times.
538704935 if ((*cxstate & 1) == d){
88
2/2
✓ Branch 0 taken 162549087 times.
✓ Branch 1 taken 207009274 times.
369558361 if (!(mqc->a & 0x8000)){
89
2/2
✓ Branch 0 taken 39555258 times.
✓ Branch 1 taken 122993829 times.
162549087 if (mqc->a < qe)
90 39555258 mqc->a = qe;
91 else
92 122993829 mqc->c += qe;
93 162549087 *cxstate = ff_mqc_nmps[*cxstate];
94 162549087 renorme(mqc);
95 } else
96 207009274 mqc->c += qe;
97 } else{
98
2/2
✓ Branch 0 taken 34367689 times.
✓ Branch 1 taken 134778885 times.
169146574 if (mqc->a < qe)
99 34367689 mqc->c += qe;
100 else
101 134778885 mqc->a = qe;
102 169146574 *cxstate = ff_mqc_nlps[*cxstate];
103 169146574 renorme(mqc);
104 }
105 538704935 }
106
107 7851775 static int mqc_flush(MqcState *mqc)
108 {
109 7851775 setbits(mqc);
110 7851775 mqc->c = mqc->c << mqc->ct;
111 7851775 byteout(mqc);
112 7851775 mqc->c = mqc->c << mqc->ct;
113 7851775 byteout(mqc);
114
2/2
✓ Branch 0 taken 1873969 times.
✓ Branch 1 taken 5977806 times.
7851775 if (*mqc->bp != 0xff)
115 1873969 mqc->bp++;
116 7851775 return mqc->bp - mqc->bpstart;
117 }
118
119 7851775 int ff_mqc_flush_to(MqcState *mqc, uint8_t *dst, int *dst_len)
120 {
121 7851775 MqcState mqc2 = *mqc;
122 7851775 mqc2.bpstart=
123 7851775 mqc2.bp = dst;
124 7851775 *mqc2.bp = *mqc->bp;
125 7851775 mqc_flush(&mqc2);
126 7851775 *dst_len = mqc2.bp - dst;
127
2/2
✓ Branch 0 taken 79777 times.
✓ Branch 1 taken 7771998 times.
7851775 if (mqc->bp < mqc->bpstart) {
128 av_assert1(mqc->bpstart - mqc->bp == 1);
129 av_assert1(*dst_len > 0);
130 av_assert1(mqc->bp[0] == 0 && dst[0] == 0);
131 79777 (*dst_len) --;
132 79777 memmove(dst, dst+1, *dst_len);
133 79777 return mqc->bp - mqc->bpstart + 1 + *dst_len;
134 }
135 7771998 return mqc->bp - mqc->bpstart + *dst_len;
136 }
137