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 <zlib.h> |
29 |
|
|
|
30 |
|
|
typedef struct PDVContext { |
31 |
|
|
AVFrame *previous_frame; |
32 |
|
|
FFZStream zstream; |
33 |
|
|
} PDVContext; |
34 |
|
|
|
35 |
|
✗ |
static av_cold int decode_init(AVCodecContext *avctx) |
36 |
|
|
{ |
37 |
|
✗ |
PDVContext *s = avctx->priv_data; |
38 |
|
|
|
39 |
|
✗ |
avctx->pix_fmt = AV_PIX_FMT_MONOBLACK; |
40 |
|
|
|
41 |
|
✗ |
s->previous_frame = av_frame_alloc(); |
42 |
|
✗ |
if (!s->previous_frame) |
43 |
|
✗ |
return AVERROR(ENOMEM); |
44 |
|
|
|
45 |
|
✗ |
return ff_inflate_init(&s->zstream, avctx); |
46 |
|
|
} |
47 |
|
|
|
48 |
|
✗ |
static av_cold int decode_end(AVCodecContext *avctx) |
49 |
|
|
{ |
50 |
|
✗ |
PDVContext *s = avctx->priv_data; |
51 |
|
|
|
52 |
|
✗ |
av_frame_free(&s->previous_frame); |
53 |
|
✗ |
ff_inflate_end(&s->zstream); |
54 |
|
|
|
55 |
|
✗ |
return 0; |
56 |
|
|
} |
57 |
|
|
|
58 |
|
✗ |
static int decode_frame(AVCodecContext *avctx, AVFrame *frame, |
59 |
|
|
int *got_frame, AVPacket *avpkt) |
60 |
|
|
{ |
61 |
|
✗ |
PDVContext *s = avctx->priv_data; |
62 |
|
✗ |
AVFrame *prev_frame = s->previous_frame; |
63 |
|
✗ |
z_stream *const zstream = &s->zstream.zstream; |
64 |
|
✗ |
uint8_t *dst, *prev = prev_frame->data[0]; |
65 |
|
|
int ret, zret; |
66 |
|
|
|
67 |
|
✗ |
if (avctx->skip_frame >= AVDISCARD_ALL || |
68 |
|
✗ |
(avctx->skip_frame >= AVDISCARD_NONINTRA && |
69 |
|
✗ |
!(avpkt->flags & AV_PKT_FLAG_KEY))) |
70 |
|
✗ |
return avpkt->size; |
71 |
|
|
|
72 |
|
✗ |
zret = inflateReset(zstream); |
73 |
|
✗ |
if (zret != Z_OK) { |
74 |
|
✗ |
av_log(avctx, AV_LOG_ERROR, "Could not reset inflate: %d.\n", zret); |
75 |
|
✗ |
return AVERROR_INVALIDDATA; |
76 |
|
|
} |
77 |
|
|
|
78 |
|
✗ |
if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0) |
79 |
|
✗ |
return ret; |
80 |
|
|
|
81 |
|
✗ |
zstream->next_in = avpkt->data; |
82 |
|
✗ |
zstream->avail_in = avpkt->size; |
83 |
|
|
|
84 |
|
✗ |
dst = frame->data[0]; |
85 |
|
✗ |
for (int i = 0; i < avctx->height; i++) { |
86 |
|
✗ |
zstream->next_out = dst; |
87 |
|
✗ |
zstream->avail_out = (avctx->width + 7) >> 3; |
88 |
|
|
|
89 |
|
✗ |
zret = inflate(zstream, Z_SYNC_FLUSH); |
90 |
|
✗ |
if (zret != Z_OK && zret != Z_STREAM_END) { |
91 |
|
✗ |
av_log(avctx, AV_LOG_ERROR, |
92 |
|
|
"Inflate failed with return code: %d.\n", zret); |
93 |
|
✗ |
return AVERROR_INVALIDDATA; |
94 |
|
|
} |
95 |
|
|
|
96 |
|
✗ |
if (prev && !(avpkt->flags & AV_PKT_FLAG_KEY)) { |
97 |
|
✗ |
for (int j = 0; j < (avctx->width + 7) >> 3; j++) |
98 |
|
✗ |
dst[j] ^= prev[j]; |
99 |
|
✗ |
prev += prev_frame->linesize[0]; |
100 |
|
|
} |
101 |
|
|
|
102 |
|
✗ |
dst += frame->linesize[0]; |
103 |
|
|
} |
104 |
|
|
|
105 |
|
✗ |
if ((ret = av_frame_replace(s->previous_frame, frame)) < 0) |
106 |
|
✗ |
return ret; |
107 |
|
|
|
108 |
|
✗ |
if (avpkt->flags & AV_PKT_FLAG_KEY) { |
109 |
|
✗ |
frame->flags |= AV_FRAME_FLAG_KEY; |
110 |
|
✗ |
frame->pict_type = AV_PICTURE_TYPE_I; |
111 |
|
|
} else { |
112 |
|
✗ |
frame->pict_type = AV_PICTURE_TYPE_P; |
113 |
|
|
} |
114 |
|
|
|
115 |
|
✗ |
*got_frame = 1; |
116 |
|
|
|
117 |
|
✗ |
return avpkt->size; |
118 |
|
|
} |
119 |
|
|
|
120 |
|
✗ |
static void decode_flush(AVCodecContext *avctx) |
121 |
|
|
{ |
122 |
|
✗ |
PDVContext *s = avctx->priv_data; |
123 |
|
|
|
124 |
|
✗ |
av_frame_unref(s->previous_frame); |
125 |
|
✗ |
} |
126 |
|
|
|
127 |
|
|
const FFCodec ff_pdv_decoder = { |
128 |
|
|
.p.name = "pdv", |
129 |
|
|
CODEC_LONG_NAME("PDV (PlayDate Video)"), |
130 |
|
|
.priv_data_size = sizeof(PDVContext), |
131 |
|
|
.p.type = AVMEDIA_TYPE_VIDEO, |
132 |
|
|
.p.id = AV_CODEC_ID_PDV, |
133 |
|
|
.p.capabilities = AV_CODEC_CAP_DR1, |
134 |
|
|
.caps_internal = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM | |
135 |
|
|
FF_CODEC_CAP_INIT_CLEANUP, |
136 |
|
|
.init = decode_init, |
137 |
|
|
.close = decode_end, |
138 |
|
|
.flush = decode_flush, |
139 |
|
|
FF_CODEC_DECODE_CB(decode_frame), |
140 |
|
|
}; |
141 |
|
|
|