| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * PDV video format | ||
| 3 | * | ||
| 4 | * Copyright (c) 2023 Paul B Mahol | ||
| 5 | * | ||
| 6 | * This file is part of FFmpeg. | ||
| 7 | * | ||
| 8 | * FFmpeg is free software; you can redistribute it and/or | ||
| 9 | * modify it under the terms of the GNU Lesser General Public | ||
| 10 | * License as published by the Free Software Foundation; either | ||
| 11 | * version 2.1 of the License, or (at your option) any later version. | ||
| 12 | * | ||
| 13 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 16 | * Lesser General Public License for more details. | ||
| 17 | * | ||
| 18 | * You should have received a copy of the GNU Lesser General Public | ||
| 19 | * License along with FFmpeg; if not, write to the Free Software | ||
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 21 | */ | ||
| 22 | |||
| 23 | #include "avcodec.h" | ||
| 24 | #include "codec_internal.h" | ||
| 25 | #include "decode.h" | ||
| 26 | #include "zlib_wrapper.h" | ||
| 27 | |||
| 28 | #include "libavutil/attributes.h" | ||
| 29 | |||
| 30 | #include <zlib.h> | ||
| 31 | |||
| 32 | typedef struct PDVContext { | ||
| 33 | AVFrame *previous_frame; | ||
| 34 | FFZStream zstream; | ||
| 35 | } PDVContext; | ||
| 36 | |||
| 37 | 7 | static av_cold int decode_init(AVCodecContext *avctx) | |
| 38 | { | ||
| 39 | 7 | PDVContext *s = avctx->priv_data; | |
| 40 | |||
| 41 | 7 | avctx->pix_fmt = AV_PIX_FMT_MONOBLACK; | |
| 42 | |||
| 43 | 7 | s->previous_frame = av_frame_alloc(); | |
| 44 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
|
7 | if (!s->previous_frame) |
| 45 | ✗ | return AVERROR(ENOMEM); | |
| 46 | |||
| 47 | 7 | return ff_inflate_init(&s->zstream, avctx); | |
| 48 | } | ||
| 49 | |||
| 50 | 7 | static av_cold int decode_end(AVCodecContext *avctx) | |
| 51 | { | ||
| 52 | 7 | PDVContext *s = avctx->priv_data; | |
| 53 | |||
| 54 | 7 | av_frame_free(&s->previous_frame); | |
| 55 | 7 | ff_inflate_end(&s->zstream); | |
| 56 | |||
| 57 | 7 | return 0; | |
| 58 | } | ||
| 59 | |||
| 60 | 10 | static int decode_frame(AVCodecContext *avctx, AVFrame *frame, | |
| 61 | int *got_frame, AVPacket *avpkt) | ||
| 62 | { | ||
| 63 | 10 | PDVContext *s = avctx->priv_data; | |
| 64 | 10 | AVFrame *prev_frame = s->previous_frame; | |
| 65 | 10 | z_stream *const zstream = &s->zstream.zstream; | |
| 66 | 10 | uint8_t *dst, *prev = prev_frame->data[0]; | |
| 67 | int ret, zret; | ||
| 68 | |||
| 69 |
1/2✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
|
10 | if (avctx->skip_frame >= AVDISCARD_ALL || |
| 70 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 6 times.
|
10 | (avctx->skip_frame >= AVDISCARD_NONINTRA && |
| 71 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
|
4 | !(avpkt->flags & AV_PKT_FLAG_KEY))) |
| 72 | 2 | return avpkt->size; | |
| 73 | |||
| 74 | 8 | zret = inflateReset(zstream); | |
| 75 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
|
8 | if (zret != Z_OK) { |
| 76 | ✗ | av_log(avctx, AV_LOG_ERROR, "Could not reset inflate: %d.\n", zret); | |
| 77 | ✗ | return AVERROR_INVALIDDATA; | |
| 78 | } | ||
| 79 | |||
| 80 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
|
8 | if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0) |
| 81 | ✗ | return ret; | |
| 82 | |||
| 83 | 8 | zstream->next_in = avpkt->data; | |
| 84 | 8 | zstream->avail_in = avpkt->size; | |
| 85 | |||
| 86 | 8 | dst = frame->data[0]; | |
| 87 |
2/2✓ Branch 0 taken 2304 times.
✓ Branch 1 taken 8 times.
|
2312 | for (int i = 0; i < avctx->height; i++) { |
| 88 | 2304 | zstream->next_out = dst; | |
| 89 | 2304 | zstream->avail_out = (avctx->width + 7) >> 3; | |
| 90 | |||
| 91 | 2304 | zret = inflate(zstream, Z_SYNC_FLUSH); | |
| 92 |
3/4✓ Branch 0 taken 8 times.
✓ Branch 1 taken 2296 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
|
2304 | if (zret != Z_OK && zret != Z_STREAM_END) { |
| 93 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
| 94 | "Inflate failed with return code: %d.\n", zret); | ||
| 95 | ✗ | return AVERROR_INVALIDDATA; | |
| 96 | } | ||
| 97 | |||
| 98 |
4/4✓ Branch 0 taken 1440 times.
✓ Branch 1 taken 864 times.
✓ Branch 2 taken 576 times.
✓ Branch 3 taken 864 times.
|
2304 | if (prev && !(avpkt->flags & AV_PKT_FLAG_KEY)) { |
| 99 |
2/2✓ Branch 0 taken 25344 times.
✓ Branch 1 taken 576 times.
|
25920 | for (int j = 0; j < (avctx->width + 7) >> 3; j++) |
| 100 | 25344 | dst[j] ^= prev[j]; | |
| 101 | 576 | prev += prev_frame->linesize[0]; | |
| 102 | } | ||
| 103 | |||
| 104 | 2304 | dst += frame->linesize[0]; | |
| 105 | } | ||
| 106 | |||
| 107 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
|
8 | if ((ret = av_frame_replace(s->previous_frame, frame)) < 0) |
| 108 | ✗ | return ret; | |
| 109 | |||
| 110 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2 times.
|
8 | if (avpkt->flags & AV_PKT_FLAG_KEY) { |
| 111 | 6 | frame->flags |= AV_FRAME_FLAG_KEY; | |
| 112 | 6 | frame->pict_type = AV_PICTURE_TYPE_I; | |
| 113 | } else { | ||
| 114 | 2 | frame->pict_type = AV_PICTURE_TYPE_P; | |
| 115 | } | ||
| 116 | |||
| 117 | 8 | *got_frame = 1; | |
| 118 | |||
| 119 | 8 | return avpkt->size; | |
| 120 | } | ||
| 121 | |||
| 122 | ✗ | static av_cold void decode_flush(AVCodecContext *avctx) | |
| 123 | { | ||
| 124 | ✗ | PDVContext *s = avctx->priv_data; | |
| 125 | |||
| 126 | ✗ | av_frame_unref(s->previous_frame); | |
| 127 | ✗ | } | |
| 128 | |||
| 129 | const FFCodec ff_pdv_decoder = { | ||
| 130 | .p.name = "pdv", | ||
| 131 | CODEC_LONG_NAME("PDV (PlayDate Video)"), | ||
| 132 | .priv_data_size = sizeof(PDVContext), | ||
| 133 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
| 134 | .p.id = AV_CODEC_ID_PDV, | ||
| 135 | .p.capabilities = AV_CODEC_CAP_DR1, | ||
| 136 | .caps_internal = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM | | ||
| 137 | FF_CODEC_CAP_INIT_CLEANUP, | ||
| 138 | .init = decode_init, | ||
| 139 | .close = decode_end, | ||
| 140 | .flush = decode_flush, | ||
| 141 | FF_CODEC_DECODE_CB(decode_frame), | ||
| 142 | }; | ||
| 143 |