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->flags |= AV_FRAME_FLAG_KEY; | |
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->flags &= ~AV_FRAME_FLAG_KEY; | |
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 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 38 times.
|
38 | if ((ret = av_frame_replace(zc->previous_frame, pic)) < 0) |
97 | ✗ | return ret; | |
98 | |||
99 | 38 | *got_frame = 1; | |
100 | |||
101 | 38 | return avpkt->size; | |
102 | } | ||
103 | |||
104 | 2 | static av_cold int zerocodec_decode_close(AVCodecContext *avctx) | |
105 | { | ||
106 | 2 | ZeroCodecContext *zc = avctx->priv_data; | |
107 | |||
108 | 2 | av_frame_free(&zc->previous_frame); | |
109 | |||
110 | 2 | ff_inflate_end(&zc->zstream); | |
111 | |||
112 | 2 | return 0; | |
113 | } | ||
114 | |||
115 | 2 | static av_cold int zerocodec_decode_init(AVCodecContext *avctx) | |
116 | { | ||
117 | 2 | ZeroCodecContext *zc = avctx->priv_data; | |
118 | |||
119 | 2 | avctx->pix_fmt = AV_PIX_FMT_UYVY422; | |
120 | 2 | avctx->bits_per_raw_sample = 8; | |
121 | |||
122 | 2 | zc->previous_frame = av_frame_alloc(); | |
123 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (!zc->previous_frame) |
124 | ✗ | return AVERROR(ENOMEM); | |
125 | |||
126 | 2 | return ff_inflate_init(&zc->zstream, avctx); | |
127 | } | ||
128 | |||
129 | ✗ | static void zerocodec_decode_flush(AVCodecContext *avctx) | |
130 | { | ||
131 | ✗ | ZeroCodecContext *zc = avctx->priv_data; | |
132 | |||
133 | ✗ | av_frame_unref(zc->previous_frame); | |
134 | ✗ | } | |
135 | |||
136 | const FFCodec ff_zerocodec_decoder = { | ||
137 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
138 | .p.name = "zerocodec", | ||
139 | CODEC_LONG_NAME("ZeroCodec Lossless Video"), | ||
140 | .p.id = AV_CODEC_ID_ZEROCODEC, | ||
141 | .priv_data_size = sizeof(ZeroCodecContext), | ||
142 | .init = zerocodec_decode_init, | ||
143 | FF_CODEC_DECODE_CB(zerocodec_decode_frame), | ||
144 | .flush = zerocodec_decode_flush, | ||
145 | .close = zerocodec_decode_close, | ||
146 | .p.capabilities = AV_CODEC_CAP_DR1, | ||
147 | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, | ||
148 | }; | ||
149 |