FFmpeg
Loading...
Searching...
No Matches
pdvdec.c
Go to the documentation of this file.
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
29
30#include <zlib.h>
31
36
38{
39 PDVContext *s = avctx->priv_data;
40
42
43 s->previous_frame = av_frame_alloc();
44 if (!s->previous_frame)
45 return AVERROR(ENOMEM);
46
47 return ff_inflate_init(&s->zstream, avctx);
48}
49
51{
52 PDVContext *s = avctx->priv_data;
53
54 av_frame_free(&s->previous_frame);
55 ff_inflate_end(&s->zstream);
56
57 return 0;
58}
59
61 int *got_frame, AVPacket *avpkt)
62{
63 PDVContext *s = avctx->priv_data;
64 AVFrame *prev_frame = s->previous_frame;
65 z_stream *const zstream = &s->zstream.zstream;
66 uint8_t *dst, *prev = prev_frame->data[0];
67 int ret, zret;
68
69 if (avctx->skip_frame >= AVDISCARD_ALL ||
70 (avctx->skip_frame >= AVDISCARD_NONINTRA &&
71 !(avpkt->flags & AV_PKT_FLAG_KEY)))
72 return avpkt->size;
73
74 zret = inflateReset(zstream);
75 if (zret != Z_OK) {
76 av_log(avctx, AV_LOG_ERROR, "Could not reset inflate: %d.\n", zret);
78 }
79
80 if (avpkt->size * 1032LL < ((avctx->width + 7) >> 3) * avctx->height) //Asymptotic max compression of deflate
82
83 if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
84 return ret;
85
86 zstream->next_in = avpkt->data;
87 zstream->avail_in = avpkt->size;
88
89 dst = frame->data[0];
90 for (int i = 0; i < avctx->height; i++) {
91 zstream->next_out = dst;
92 zstream->avail_out = (avctx->width + 7) >> 3;
93
94 zret = inflate(zstream, Z_SYNC_FLUSH);
95 if (zret != Z_OK && zret != Z_STREAM_END) {
96 av_log(avctx, AV_LOG_ERROR,
97 "Inflate failed with return code: %d.\n", zret);
99 }
100
101 if (prev && !(avpkt->flags & AV_PKT_FLAG_KEY)) {
102 for (int j = 0; j < (avctx->width + 7) >> 3; j++)
103 dst[j] ^= prev[j];
104 prev += prev_frame->linesize[0];
105 }
106
107 dst += frame->linesize[0];
108 }
109
110 if ((ret = av_frame_replace(s->previous_frame, frame)) < 0)
111 return ret;
112
113 if (avpkt->flags & AV_PKT_FLAG_KEY) {
114 frame->flags |= AV_FRAME_FLAG_KEY;
115 frame->pict_type = AV_PICTURE_TYPE_I;
116 } else {
117 frame->pict_type = AV_PICTURE_TYPE_P;
118 }
119
120 *got_frame = 1;
121
122 return avpkt->size;
123}
124
126{
127 PDVContext *s = avctx->priv_data;
128
129 av_frame_unref(s->previous_frame);
130}
131
133 .p.name = "pdv",
134 CODEC_LONG_NAME("PDV (PlayDate Video)"),
135 .priv_data_size = sizeof(PDVContext),
136 .p.type = AVMEDIA_TYPE_VIDEO,
137 .p.id = AV_CODEC_ID_PDV,
138 .p.capabilities = AV_CODEC_CAP_DR1,
139 .caps_internal = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM |
141 .init = decode_init,
142 .close = decode_end,
143 .flush = decode_flush,
145};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
static av_cold void decode_flush(AVCodecContext *avctx)
Definition agm.c:1253
const FFCodec ff_pdv_decoder
Definition pdvdec.c:132
Libavcodec external API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM
The decoder extracts and fills its parameters even if the frame is skipped due to the skip_frame sett...
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition decode.c:1777
static AVFrame * frame
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
Definition avcodec.h:415
@ AV_CODEC_ID_PDV
Definition codec_id.h:315
@ AVDISCARD_ALL
discard all
Definition defs.h:232
@ AVDISCARD_NONINTRA
discard all non intra frames
Definition defs.h:230
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition packet.h:650
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_FRAME_FLAG_KEY
A flag to mark frames that are keyframes.
Definition frame.h:687
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition frame.c:496
int av_frame_replace(AVFrame *dst, const AVFrame *src)
Ensure the destination frame refers to the same data described by the source frame,...
Definition frame.c:376
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition frame.c:52
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
@ AV_PICTURE_TYPE_I
Intra.
Definition avutil.h:278
@ AV_PICTURE_TYPE_P
Predicted.
Definition avutil.h:279
static av_cold int decode_init(AVCodecContext *avctx)
Definition 4xm.c:998
static av_cold int decode_end(AVCodecContext *avctx)
Definition 4xm.c:980
static int decode_frame(AVCodecContext *avctx, AVFrame *picture, int *got_frame, AVPacket *avpkt)
Definition 4xm.c:837
static av_cold void decode_flush(AVCodecContext *avctx)
Definition pdvdec.c:125
static int decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *avpkt)
Definition pdvdec.c:60
static av_cold int decode_init(AVCodecContext *avctx)
Definition pdvdec.c:37
static av_cold int decode_end(AVCodecContext *avctx)
Definition pdvdec.c:50
Macro definitions for various function/variable attributes.
#define av_cold
Definition attributes.h:117
@ AV_PIX_FMT_MONOBLACK
Y , 1bpp, 0 is black, 1 is white, in each byte pixels are ordered from the msb to the lsb.
Definition pixfmt.h:83
main external API structure.
Definition avcodec.h:443
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition avcodec.h:643
int width
picture width / height.
Definition avcodec.h:604
void * priv_data
Definition avcodec.h:470
enum AVDiscard skip_frame
Skip decoding for selected frames.
Definition avcodec.h:1667
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition frame.h:493
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition frame.h:517
This structure stores compressed data.
Definition packet.h:580
int flags
A combination of AV_PKT_FLAG values.
Definition packet.h:609
int size
Definition packet.h:604
uint8_t * data
Definition packet.h:603
AVFrame * previous_frame
Definition pdvdec.c:33
FFZStream zstream
Definition pdvdec.c:34
#define av_log(a,...)
static void inflate(uint8_t *dst, const uint8_t *p1, int width, int threshold, const uint8_t *coordinates[], int coord, int maxc)
void ff_inflate_end(FFZStream *zstream)
Wrapper around inflateEnd().
int ff_inflate_init(FFZStream *zstream, void *logctx)
Wrapper around inflateInit().