FFmpeg
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 
28 #include <zlib.h>
29 
30 typedef struct PDVContext {
33 } PDVContext;
34 
36 {
37  PDVContext *s = avctx->priv_data;
38 
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 
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 
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 |
136  .init = decode_init,
137  .close = decode_end,
138  .flush = decode_flush,
140 };
FF_CODEC_CAP_INIT_CLEANUP
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: codec_internal.h:42
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
PDVContext::previous_frame
AVFrame * previous_frame
Definition: pdvdec.c:31
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:160
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:374
AVPacket::data
uint8_t * data
Definition: packet.h:524
FFCodec
Definition: codec_internal.h:126
decode_init
static av_cold int decode_init(AVCodecContext *avctx)
Definition: pdvdec.c:35
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:579
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:395
PDVContext::zstream
FFZStream zstream
Definition: pdvdec.c:32
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:130
AVCodecContext::skip_frame
enum AVDiscard skip_frame
Skip decoding for selected frames.
Definition: avcodec.h:1819
inflate
static void inflate(uint8_t *dst, const uint8_t *p1, int width, int threshold, const uint8_t *coordinates[], int coord, int maxc)
Definition: vf_neighbor.c:194
ff_pdv_decoder
const FFCodec ff_pdv_decoder
Definition: pdvdec.c:127
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:148
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
zlib_wrapper.h
av_cold
#define av_cold
Definition: attributes.h:90
AV_FRAME_FLAG_KEY
#define AV_FRAME_FLAG_KEY
A flag to mark frames that are keyframes.
Definition: frame.h:625
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:286
s
#define s(width, name)
Definition: cbs_vp9.c:198
PDVContext
Definition: pdvdec.c:30
AV_GET_BUFFER_FLAG_REF
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
Definition: avcodec.h:425
decode.h
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:271
AVDISCARD_ALL
@ AVDISCARD_ALL
discard all
Definition: defs.h:219
AV_PIX_FMT_MONOBLACK
@ 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
decode_end
static av_cold int decode_end(AVCodecContext *avctx)
Definition: pdvdec.c:48
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:279
AV_CODEC_ID_PDV
@ AV_CODEC_ID_PDV
Definition: codec_id.h:320
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1556
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:525
codec_internal.h
FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM
#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...
Definition: codec_internal.h:54
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:530
AVDISCARD_NONINTRA
@ AVDISCARD_NONINTRA
discard all non intra frames
Definition: defs.h:217
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
av_frame_unref
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:606
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
ff_inflate_end
void ff_inflate_end(FFZStream *zstream)
Wrapper around inflateEnd().
AVCodecContext::height
int height
Definition: avcodec.h:618
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:657
decode_frame
static int decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *avpkt)
Definition: pdvdec.c:58
avcodec.h
ret
ret
Definition: filter_design.txt:187
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
decode_flush
static void decode_flush(AVCodecContext *avctx)
Definition: pdvdec.c:120
av_frame_replace
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:483
AVCodecContext
main external API structure.
Definition: avcodec.h:445
AV_PICTURE_TYPE_P
@ AV_PICTURE_TYPE_P
Predicted.
Definition: avutil.h:280
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
FFZStream
Definition: zlib_wrapper.h:27
AVPacket
This structure stores compressed data.
Definition: packet.h:501
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:472
ff_inflate_init
int ff_inflate_init(FFZStream *zstream, void *logctx)
Wrapper around inflateInit().
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:618
AVFrame::linesize
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:419
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61