FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/zerocodec.c
Date: 2022-12-05 20:26:17
Exec Total Coverage
Lines: 44 56 78.6%
Functions: 3 4 75.0%
Branches: 16 22 72.7%

Line Branch Exec Source
1 /*
2 * ZeroCodec Decoder
3 *
4 * Copyright (c) 2012, Derek Buitenhuis
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <zlib.h>
20
21 #include "avcodec.h"
22 #include "codec_internal.h"
23 #include "decode.h"
24 #include "zlib_wrapper.h"
25 #include "libavutil/common.h"
26
27 typedef struct ZeroCodecContext {
28 AVFrame *previous_frame;
29 FFZStream zstream;
30 } ZeroCodecContext;
31
32 38 static int zerocodec_decode_frame(AVCodecContext *avctx, AVFrame *pic,
33 int *got_frame, AVPacket *avpkt)
34 {
35 38 ZeroCodecContext *zc = avctx->priv_data;
36 38 AVFrame *prev_pic = zc->previous_frame;
37 38 z_stream *const zstream = &zc->zstream.zstream;
38 38 uint8_t *prev = prev_pic->data[0];
39 uint8_t *dst;
40 int i, j, zret, ret;
41
42
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 35 times.
38 if (avpkt->flags & AV_PKT_FLAG_KEY) {
43 3 pic->key_frame = 1;
44 3 pic->pict_type = AV_PICTURE_TYPE_I;
45 } else {
46
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 35 times.
35 if (!prev) {
47 av_log(avctx, AV_LOG_ERROR, "Missing reference frame.\n");
48 return AVERROR_INVALIDDATA;
49 }
50
51 35 prev += (avctx->height - 1) * prev_pic->linesize[0];
52
53 35 pic->key_frame = 0;
54 35 pic->pict_type = AV_PICTURE_TYPE_P;
55 }
56
57 38 zret = inflateReset(zstream);
58
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38 times.
38 if (zret != Z_OK) {
59 av_log(avctx, AV_LOG_ERROR, "Could not reset inflate: %d.\n", zret);
60 return AVERROR_INVALIDDATA;
61 }
62
63
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 38 times.
38 if ((ret = ff_get_buffer(avctx, pic, AV_GET_BUFFER_FLAG_REF)) < 0)
64 return ret;
65
66 38 zstream->next_in = avpkt->data;
67 38 zstream->avail_in = avpkt->size;
68
69 38 dst = pic->data[0] + (avctx->height - 1) * pic->linesize[0];
70
71 /**
72 * ZeroCodec has very simple interframe compression. If a value
73 * is the same as the previous frame, set it to 0.
74 */
75
76
2/2
✓ Branch 0 taken 27360 times.
✓ Branch 1 taken 38 times.
27398 for (i = 0; i < avctx->height; i++) {
77 27360 zstream->next_out = dst;
78 27360 zstream->avail_out = avctx->width << 1;
79
80 27360 zret = inflate(zstream, Z_SYNC_FLUSH);
81
3/4
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 27322 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 38 times.
27360 if (zret != Z_OK && zret != Z_STREAM_END) {
82 av_log(avctx, AV_LOG_ERROR,
83 "Inflate failed with return code: %d.\n", zret);
84 return AVERROR_INVALIDDATA;
85 }
86
87
2/2
✓ Branch 0 taken 25200 times.
✓ Branch 1 taken 2160 times.
27360 if (!(avpkt->flags & AV_PKT_FLAG_KEY)) {
88
2/2
✓ Branch 0 taken 64512000 times.
✓ Branch 1 taken 25200 times.
64537200 for (j = 0; j < avctx->width << 1; j++)
89 64512000 dst[j] += prev[j] & -!dst[j];
90 25200 prev -= prev_pic->linesize[0];
91 }
92
93 27360 dst -= pic->linesize[0];
94 }
95
96 38 av_frame_unref(zc->previous_frame);
97
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 38 times.
38 if ((ret = av_frame_ref(zc->previous_frame, pic)) < 0)
98 return ret;
99
100 38 *got_frame = 1;
101
102 38 return avpkt->size;
103 }
104
105 2 static av_cold int zerocodec_decode_close(AVCodecContext *avctx)
106 {
107 2 ZeroCodecContext *zc = avctx->priv_data;
108
109 2 av_frame_free(&zc->previous_frame);
110
111 2 ff_inflate_end(&zc->zstream);
112
113 2 return 0;
114 }
115
116 2 static av_cold int zerocodec_decode_init(AVCodecContext *avctx)
117 {
118 2 ZeroCodecContext *zc = avctx->priv_data;
119
120 2 avctx->pix_fmt = AV_PIX_FMT_UYVY422;
121 2 avctx->bits_per_raw_sample = 8;
122
123 2 zc->previous_frame = av_frame_alloc();
124
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!zc->previous_frame)
125 return AVERROR(ENOMEM);
126
127 2 return ff_inflate_init(&zc->zstream, avctx);
128 }
129
130 static void zerocodec_decode_flush(AVCodecContext *avctx)
131 {
132 ZeroCodecContext *zc = avctx->priv_data;
133
134 av_frame_unref(zc->previous_frame);
135 }
136
137 const FFCodec ff_zerocodec_decoder = {
138 .p.type = AVMEDIA_TYPE_VIDEO,
139 .p.name = "zerocodec",
140 CODEC_LONG_NAME("ZeroCodec Lossless Video"),
141 .p.id = AV_CODEC_ID_ZEROCODEC,
142 .priv_data_size = sizeof(ZeroCodecContext),
143 .init = zerocodec_decode_init,
144 FF_CODEC_DECODE_CB(zerocodec_decode_frame),
145 .flush = zerocodec_decode_flush,
146 .close = zerocodec_decode_close,
147 .p.capabilities = AV_CODEC_CAP_DR1,
148 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
149 };
150