| 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/attributes.h" | ||
| 26 | #include "libavutil/common.h" | ||
| 27 | |||
| 28 | typedef struct ZeroCodecContext { | ||
| 29 | AVFrame *previous_frame; | ||
| 30 | FFZStream zstream; | ||
| 31 | } ZeroCodecContext; | ||
| 32 | |||
| 33 | 38 | static int zerocodec_decode_frame(AVCodecContext *avctx, AVFrame *pic, | |
| 34 | int *got_frame, AVPacket *avpkt) | ||
| 35 | { | ||
| 36 | 38 | ZeroCodecContext *zc = avctx->priv_data; | |
| 37 | 38 | AVFrame *prev_pic = zc->previous_frame; | |
| 38 | 38 | z_stream *const zstream = &zc->zstream.zstream; | |
| 39 | 38 | uint8_t *prev = prev_pic->data[0]; | |
| 40 | uint8_t *dst; | ||
| 41 | int i, j, zret, ret; | ||
| 42 | |||
| 43 | 2/2✓ Branch 0 taken 3 times. ✓ Branch 1 taken 35 times. | 38 | if (avpkt->flags & AV_PKT_FLAG_KEY) { | 
| 44 | 3 | pic->flags |= AV_FRAME_FLAG_KEY; | |
| 45 | 3 | pic->pict_type = AV_PICTURE_TYPE_I; | |
| 46 | } else { | ||
| 47 | 1/2✗ Branch 0 not taken. ✓ Branch 1 taken 35 times. | 35 | if (!prev) { | 
| 48 | ✗ | av_log(avctx, AV_LOG_ERROR, "Missing reference frame.\n"); | |
| 49 | ✗ | return AVERROR_INVALIDDATA; | |
| 50 | } | ||
| 51 | |||
| 52 | 35 | prev += (avctx->height - 1) * prev_pic->linesize[0]; | |
| 53 | |||
| 54 | 35 | pic->flags &= ~AV_FRAME_FLAG_KEY; | |
| 55 | 35 | pic->pict_type = AV_PICTURE_TYPE_P; | |
| 56 | } | ||
| 57 | |||
| 58 | 38 | zret = inflateReset(zstream); | |
| 59 | 1/2✗ Branch 0 not taken. ✓ Branch 1 taken 38 times. | 38 | if (zret != Z_OK) { | 
| 60 | ✗ | av_log(avctx, AV_LOG_ERROR, "Could not reset inflate: %d.\n", zret); | |
| 61 | ✗ | return AVERROR_INVALIDDATA; | |
| 62 | } | ||
| 63 | |||
| 64 | 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) | 
| 65 | ✗ | return ret; | |
| 66 | |||
| 67 | 38 | zstream->next_in = avpkt->data; | |
| 68 | 38 | zstream->avail_in = avpkt->size; | |
| 69 | |||
| 70 | 38 | dst = pic->data[0] + (avctx->height - 1) * pic->linesize[0]; | |
| 71 | |||
| 72 | /** | ||
| 73 | * ZeroCodec has very simple interframe compression. If a value | ||
| 74 | * is the same as the previous frame, set it to 0. | ||
| 75 | */ | ||
| 76 | |||
| 77 | 2/2✓ Branch 0 taken 27360 times. ✓ Branch 1 taken 38 times. | 27398 | for (i = 0; i < avctx->height; i++) { | 
| 78 | 27360 | zstream->next_out = dst; | |
| 79 | 27360 | zstream->avail_out = avctx->width << 1; | |
| 80 | |||
| 81 | 27360 | zret = inflate(zstream, Z_SYNC_FLUSH); | |
| 82 | 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) { | 
| 83 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
| 84 | "Inflate failed with return code: %d.\n", zret); | ||
| 85 | ✗ | return AVERROR_INVALIDDATA; | |
| 86 | } | ||
| 87 | |||
| 88 | 2/2✓ Branch 0 taken 25200 times. ✓ Branch 1 taken 2160 times. | 27360 | if (!(avpkt->flags & AV_PKT_FLAG_KEY)) { | 
| 89 | 2/2✓ Branch 0 taken 64512000 times. ✓ Branch 1 taken 25200 times. | 64537200 | for (j = 0; j < avctx->width << 1; j++) | 
| 90 | 64512000 | dst[j] += prev[j] & -!dst[j]; | |
| 91 | 25200 | prev -= prev_pic->linesize[0]; | |
| 92 | } | ||
| 93 | |||
| 94 | 27360 | dst -= pic->linesize[0]; | |
| 95 | } | ||
| 96 | |||
| 97 | 1/2✗ Branch 1 not taken. ✓ Branch 2 taken 38 times. | 38 | if ((ret = av_frame_replace(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 av_cold 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 |