/src/ffmpeg/libavcodec/pdvenc.c
Line | Count | Source |
1 | | /* |
2 | | * PDV video encoder |
3 | | * |
4 | | * Copyright (c) 2026 Priyanshu Thapliyal <priyanshuthapliyal2005@gmail.com> |
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 "libavutil/common.h" |
24 | | #include "libavutil/imgutils.h" |
25 | | #include "libavutil/mem.h" |
26 | | #include "avcodec.h" |
27 | | #include "codec_internal.h" |
28 | | #include "encode.h" |
29 | | #include "zlib_wrapper.h" |
30 | | |
31 | | #include <limits.h> |
32 | | #include <zlib.h> |
33 | | |
34 | | typedef struct PDVEncContext { |
35 | | FFZStream zstream; |
36 | | uint8_t *previous_frame; |
37 | | uint8_t *work_frame; |
38 | | int row_size; |
39 | | int frame_size; |
40 | | int frame_number; |
41 | | int last_keyframe; |
42 | | } PDVEncContext; |
43 | | |
44 | | static av_cold int encode_init(AVCodecContext *avctx) |
45 | 1.20k | { |
46 | 1.20k | PDVEncContext *s = avctx->priv_data; |
47 | 1.20k | int ret; |
48 | | |
49 | 1.20k | ret = av_image_check_size(avctx->width, avctx->height, 0, avctx); |
50 | 1.20k | if (ret < 0) |
51 | 0 | return ret; |
52 | | |
53 | 1.20k | s->row_size = (avctx->width + 7) >> 3; |
54 | | |
55 | 1.20k | s->frame_size = s->row_size * avctx->height; |
56 | | |
57 | 1.20k | s->previous_frame = av_malloc(s->frame_size); |
58 | 1.20k | s->work_frame = av_malloc(s->frame_size); |
59 | 1.20k | if (!s->previous_frame || !s->work_frame) |
60 | 0 | return AVERROR(ENOMEM); |
61 | | |
62 | 1.20k | avctx->bits_per_coded_sample = 1; |
63 | | |
64 | 1.20k | return ff_deflate_init(&s->zstream, |
65 | 1.20k | avctx->compression_level == FF_COMPRESSION_DEFAULT ? |
66 | 1.20k | Z_DEFAULT_COMPRESSION : |
67 | 1.20k | av_clip(avctx->compression_level, 0, 9), |
68 | 1.20k | avctx); |
69 | 1.20k | } |
70 | | |
71 | | static av_cold int encode_end(AVCodecContext *avctx) |
72 | 1.20k | { |
73 | 1.20k | PDVEncContext *s = avctx->priv_data; |
74 | | |
75 | 1.20k | av_freep(&s->previous_frame); |
76 | 1.20k | av_freep(&s->work_frame); |
77 | 1.20k | ff_deflate_end(&s->zstream); |
78 | | |
79 | 1.20k | return 0; |
80 | 1.20k | } |
81 | | |
82 | | static int encode_frame(AVCodecContext *avctx, AVPacket *pkt, |
83 | | const AVFrame *frame, int *got_packet) |
84 | 17.9k | { |
85 | 17.9k | PDVEncContext *s = avctx->priv_data; |
86 | 17.9k | z_stream *const zstream = &s->zstream.zstream; |
87 | 17.9k | uint8_t *prev = s->previous_frame; |
88 | 17.9k | uint8_t *curr = s->work_frame; |
89 | 17.9k | uint8_t *payload; |
90 | 17.9k | const int keyframe = s->frame_number == 0 || avctx->gop_size <= 1 || |
91 | 12.9k | s->frame_number - s->last_keyframe >= avctx->gop_size; |
92 | 17.9k | int ret; |
93 | 17.9k | int zret; |
94 | | |
95 | 17.9k | if (s->frame_number == INT_MAX) { |
96 | 0 | av_log(avctx, AV_LOG_ERROR, "Frame counter reached INT_MAX.\n"); |
97 | 0 | return AVERROR(EINVAL); |
98 | 0 | } |
99 | | |
100 | 17.9k | { |
101 | 17.9k | const uint8_t *src = frame->data[0]; |
102 | 17.9k | const ptrdiff_t src_linesize = frame->linesize[0]; |
103 | | |
104 | 6.80M | for (int y = 0; y < avctx->height; y++) { |
105 | 6.78M | memcpy(curr + y * s->row_size, src, s->row_size); |
106 | 6.78M | src += src_linesize; |
107 | 6.78M | } |
108 | 17.9k | } |
109 | | |
110 | 17.9k | ret = ff_get_encode_buffer(avctx, pkt, deflateBound(zstream, s->frame_size), 0); |
111 | 17.9k | if (ret < 0) |
112 | 0 | return ret; |
113 | | |
114 | 17.9k | if (keyframe) { |
115 | 5.01k | payload = curr; |
116 | 12.9k | } else { |
117 | 16.2M | for (int i = 0; i < s->frame_size; i++) |
118 | 16.2M | prev[i] ^= curr[i]; |
119 | 12.9k | payload = prev; |
120 | 12.9k | } |
121 | | |
122 | 17.9k | zret = deflateReset(zstream); |
123 | 17.9k | if (zret != Z_OK) { |
124 | 0 | av_log(avctx, AV_LOG_ERROR, "Could not reset deflate: %d.\n", zret); |
125 | 0 | return AVERROR_EXTERNAL; |
126 | 0 | } |
127 | | |
128 | 17.9k | zstream->next_in = payload; |
129 | 17.9k | zstream->avail_in = s->frame_size; |
130 | 17.9k | zstream->next_out = pkt->data; |
131 | 17.9k | zstream->avail_out = pkt->size; |
132 | | |
133 | 17.9k | zret = deflate(zstream, Z_FINISH); |
134 | 17.9k | if (zret != Z_STREAM_END) { |
135 | 0 | av_log(avctx, AV_LOG_ERROR, "Deflate failed with return code: %d.\n", zret); |
136 | 0 | return AVERROR_EXTERNAL; |
137 | 0 | } |
138 | | |
139 | 17.9k | pkt->size = zstream->total_out; |
140 | 17.9k | if (keyframe) { |
141 | 5.01k | pkt->flags |= AV_PKT_FLAG_KEY; |
142 | 5.01k | s->last_keyframe = s->frame_number; |
143 | 5.01k | } |
144 | | |
145 | 17.9k | FFSWAP(uint8_t*, s->previous_frame, s->work_frame); |
146 | 17.9k | s->frame_number++; |
147 | 17.9k | *got_packet = 1; |
148 | | |
149 | 17.9k | return 0; |
150 | 17.9k | } |
151 | | |
152 | | const FFCodec ff_pdv_encoder = { |
153 | | .p.name = "pdv", |
154 | | CODEC_LONG_NAME("PDV (PlayDate Video)"), |
155 | | .p.type = AVMEDIA_TYPE_VIDEO, |
156 | | .p.id = AV_CODEC_ID_PDV, |
157 | | .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE | |
158 | | AV_CODEC_CAP_EXPERIMENTAL, |
159 | | .priv_data_size = sizeof(PDVEncContext), |
160 | | .init = encode_init, |
161 | | FF_CODEC_ENCODE_CB(encode_frame), |
162 | | .close = encode_end, |
163 | | CODEC_PIXFMTS(AV_PIX_FMT_MONOBLACK), |
164 | | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, |
165 | | }; |