FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/truemotion2rt.c
Date: 2021-09-16 08:47:15
Exec Total Coverage
Lines: 94 106 88.7%
Branches: 62 72 86.1%

Line Branch Exec Source
1 /*
2 * Duck TrueMotion 2.0 Real Time decoder
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include "libavutil/imgutils.h"
26 #include "libavutil/internal.h"
27 #include "libavutil/intreadwrite.h"
28
29 #define BITSTREAM_READER_LE
30 #include "avcodec.h"
31 #include "get_bits.h"
32 #include "internal.h"
33
34 typedef struct TrueMotion2RTContext {
35 GetBitContext gb;
36 int delta_size;
37 int hscale;
38 } TrueMotion2RTContext;
39
40 static const int16_t delta_tab2[] = {
41 5, -7, 36, -36,
42 };
43
44 static const int16_t delta_tab3[] = {
45 2, -3, 8, -8, 18, -18, 36, -36,
46 };
47
48 static const int16_t delta_tab4[] = {
49 1, -1, 2, -3, 8, -8, 18, -18, 36, -36, 54, -54, 96, -96, 144, -144,
50 };
51
52 static const int16_t *const delta_tabs[] = {
53 delta_tab2, delta_tab3, delta_tab4,
54 };
55
56 /* Returns the number of bytes consumed from the bytestream, or
57 * AVERROR_INVALIDDATA if there was an error while decoding the header. */
58 3 static int truemotion2rt_decode_header(AVCodecContext *avctx, const AVPacket *avpkt)
59 {
60 3 TrueMotion2RTContext *s = avctx->priv_data;
61 int header_size;
62 3 uint8_t header_buffer[128] = { 0 }; /* logical maximum header size */
63 3 const uint8_t *buf = avpkt->data;
64 3 int size = avpkt->size;
65 int width, height;
66 int ret, i;
67
68
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (size < 1) {
69 av_log(avctx, AV_LOG_ERROR, "input packet too small (%d)\n", size);
70 return AVERROR_INVALIDDATA;
71 }
72
73 3 header_size = ((buf[0] >> 5) | (buf[0] << 3)) & 0x7f;
74
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (header_size < 10) {
75 av_log(avctx, AV_LOG_ERROR, "invalid header size (%d)\n", header_size);
76 return AVERROR_INVALIDDATA;
77 }
78
79
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (header_size + 1 > size) {
80 av_log(avctx, AV_LOG_ERROR, "input packet too small (%d)\n", size);
81 return AVERROR_INVALIDDATA;
82 }
83
84 /* unscramble the header bytes with a XOR operation */
85
2/2
✓ Branch 0 taken 57 times.
✓ Branch 1 taken 3 times.
60 for (i = 1; i < header_size; i++)
86 57 header_buffer[i - 1] = buf[i] ^ buf[i + 1];
87
88 3 s->delta_size = header_buffer[1];
89
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 s->hscale = 1 + !!header_buffer[3];
90
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
3 if (s->delta_size < 2 || s->delta_size > 4)
91 return AVERROR_INVALIDDATA;
92
93 3 height = AV_RL16(header_buffer + 5);
94 3 width = AV_RL16(header_buffer + 7);
95
96 3 ret = ff_set_dimensions(avctx, width, height);
97
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0)
98 return ret;
99
100 3 av_log(avctx, AV_LOG_DEBUG, "Header size: %d\n", header_size);
101 3 return header_size;
102 }
103
104 3 static int truemotion2rt_decode_frame(AVCodecContext *avctx, void *data,
105 int *got_frame, AVPacket *avpkt)
106 {
107 3 TrueMotion2RTContext *s = avctx->priv_data;
108 3 AVFrame * const p = data;
109 3 GetBitContext *gb = &s->gb;
110 uint8_t *dst;
111 int x, y, delta_mode;
112 int ret;
113
114 3 ret = truemotion2rt_decode_header(avctx, avpkt);
115
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0)
116 return ret;
117
118
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if ((avctx->width + s->hscale - 1)/ s->hscale * avctx->height * s->delta_size > avpkt->size * 8LL * 4)
119 return AVERROR_INVALIDDATA;
120
121 3 ret = init_get_bits8(gb, avpkt->data + ret, avpkt->size - ret);
122
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0)
123 return ret;
124
125 3 ret = ff_get_buffer(avctx, p, 0);
126
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0)
127 return ret;
128
129 3 skip_bits(gb, 32);
130 3 delta_mode = s->delta_size - 2;
131 3 dst = p->data[0];
132
2/2
✓ Branch 0 taken 660 times.
✓ Branch 1 taken 3 times.
663 for (y = 0; y < avctx->height; y++) {
133 660 int diff = 0;
134
2/2
✓ Branch 0 taken 92768 times.
✓ Branch 1 taken 660 times.
93428 for (x = 0; x < avctx->width; x += s->hscale) {
135 92768 diff += delta_tabs[delta_mode][get_bits(gb, s->delta_size)];
136
2/2
✓ Branch 0 taken 92352 times.
✓ Branch 1 taken 416 times.
92768 dst[x] = av_clip_uint8((y ? dst[x - p->linesize[0]] : 0) + diff);
137 }
138 660 dst += p->linesize[0];
139 }
140
141
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 if (s->hscale > 1) {
142 2 dst = p->data[0];
143
2/2
✓ Branch 0 taken 428 times.
✓ Branch 1 taken 2 times.
430 for (y = 0; y < avctx->height; y++) {
144
2/2
✓ Branch 0 taken 44512 times.
✓ Branch 1 taken 428 times.
44940 for (x = 1; x < avctx->width; x += s->hscale)
145 44512 dst[x] = dst[x - 1];
146 428 dst += p->linesize[0];
147 }
148 }
149
150 3 dst = p->data[0];
151
2/2
✓ Branch 0 taken 660 times.
✓ Branch 1 taken 3 times.
663 for (y = 0; y < avctx->height; y++) {
152
2/2
✓ Branch 0 taken 137280 times.
✓ Branch 1 taken 660 times.
137940 for (x = 0; x < avctx->width; x++)
153 137280 dst[x] = av_clip_uint8(dst[x] + (dst[x] - 128) / 3);
154 660 dst += p->linesize[0];
155 }
156
157 3 dst = p->data[1];
158
2/2
✓ Branch 0 taken 165 times.
✓ Branch 1 taken 3 times.
168 for (y = 0; y < avctx->height >> 2; y++) {
159 165 int diff = 0;
160
2/2
✓ Branch 0 taken 5798 times.
✓ Branch 1 taken 165 times.
5963 for (x = 0; x < avctx->width >> 2; x += s->hscale) {
161 5798 diff += delta_tabs[delta_mode][get_bits(gb, s->delta_size)];
162
2/2
✓ Branch 0 taken 5694 times.
✓ Branch 1 taken 104 times.
5798 dst[x] = av_clip_uint8((y ? dst[x - p->linesize[1]] : 128) + diff);
163 }
164 165 dst += p->linesize[1];
165 }
166
167
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 if (s->hscale > 1) {
168 2 dst = p->data[1];
169
2/2
✓ Branch 0 taken 107 times.
✓ Branch 1 taken 2 times.
109 for (y = 0; y < avctx->height >> 2; y++) {
170
2/2
✓ Branch 0 taken 2782 times.
✓ Branch 1 taken 107 times.
2889 for (x = 1; x < avctx->width >> 2; x += s->hscale)
171 2782 dst[x] = dst[x - 1];
172 107 dst += p->linesize[1];
173 }
174 }
175
176 3 dst = p->data[1];
177
2/2
✓ Branch 0 taken 165 times.
✓ Branch 1 taken 3 times.
168 for (y = 0; y < avctx->height >> 2; y++) {
178
2/2
✓ Branch 0 taken 8580 times.
✓ Branch 1 taken 165 times.
8745 for (x = 0; x < avctx->width >> 2; x++)
179 8580 dst[x] += (dst[x] - 128) / 8;
180 165 dst += p->linesize[1];
181 }
182
183 3 dst = p->data[2];
184
2/2
✓ Branch 0 taken 165 times.
✓ Branch 1 taken 3 times.
168 for (y = 0; y < avctx->height >> 2; y++) {
185 165 int diff = 0;
186
2/2
✓ Branch 0 taken 5798 times.
✓ Branch 1 taken 165 times.
5963 for (x = 0; x < avctx->width >> 2; x += s->hscale) {
187 5798 diff += delta_tabs[delta_mode][get_bits(gb, s->delta_size)];
188
2/2
✓ Branch 0 taken 5694 times.
✓ Branch 1 taken 104 times.
5798 dst[x] = av_clip_uint8((y ? dst[x - p->linesize[2]] : 128) + diff);
189 }
190 165 dst += p->linesize[2];
191 }
192
193
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 if (s->hscale > 1) {
194 2 dst = p->data[2];
195
2/2
✓ Branch 0 taken 107 times.
✓ Branch 1 taken 2 times.
109 for (y = 0; y < avctx->height >> 2; y++) {
196
2/2
✓ Branch 0 taken 2782 times.
✓ Branch 1 taken 107 times.
2889 for (x = 1; x < avctx->width >> 2; x += s->hscale)
197 2782 dst[x] = dst[x - 1];
198 107 dst += p->linesize[2];
199 }
200 }
201
202 3 dst = p->data[2];
203
2/2
✓ Branch 0 taken 165 times.
✓ Branch 1 taken 3 times.
168 for (y = 0; y < avctx->height >> 2; y++) {
204
2/2
✓ Branch 0 taken 8580 times.
✓ Branch 1 taken 165 times.
8745 for (x = 0; x < avctx->width >> 2; x++)
205 8580 dst[x] += (dst[x] - 128) / 8;
206 165 dst += p->linesize[2];
207 }
208
209 3 p->pict_type = AV_PICTURE_TYPE_I;
210 3 p->key_frame = 1;
211 3 *got_frame = 1;
212
213 3 return avpkt->size;
214 }
215
216 6 static av_cold int truemotion2rt_decode_init(AVCodecContext *avctx)
217 {
218 6 avctx->pix_fmt = AV_PIX_FMT_YUV410P;
219 6 return 0;
220 }
221
222 const AVCodec ff_truemotion2rt_decoder = {
223 .name = "truemotion2rt",
224 .long_name = NULL_IF_CONFIG_SMALL("Duck TrueMotion 2.0 Real Time"),
225 .type = AVMEDIA_TYPE_VIDEO,
226 .id = AV_CODEC_ID_TRUEMOTION2RT,
227 .priv_data_size = sizeof(TrueMotion2RTContext),
228 .init = truemotion2rt_decode_init,
229 .decode = truemotion2rt_decode_frame,
230 .capabilities = AV_CODEC_CAP_DR1,
231 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
232 };
233