FFmpeg
Loading...
Searching...
No Matches
zerocodec.c
Go to the documentation of this file.
1/*
2 * ZeroCodec Decoder
3 *
4 * Copyright (c) 2012, Derek Buitenhuis
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19#include <zlib.h>
20
21#include "avcodec.h"
22#include "codec_internal.h"
23#include "decode.h"
24#include "zlib_wrapper.h"
26#include "libavutil/common.h"
27
32
34 int *got_frame, AVPacket *avpkt)
35{
36 ZeroCodecContext *zc = avctx->priv_data;
37 AVFrame *prev_pic = zc->previous_frame;
38 z_stream *const zstream = &zc->zstream.zstream;
39 uint8_t *prev = prev_pic->data[0];
40 uint8_t *dst;
41 int i, j, zret, ret;
42
43 if (avpkt->flags & AV_PKT_FLAG_KEY) {
46 } else {
47 if (!prev) {
48 av_log(avctx, AV_LOG_ERROR, "Missing reference frame.\n");
50 }
51
52 prev += (avctx->height - 1) * prev_pic->linesize[0];
53
56 }
57
58 zret = inflateReset(zstream);
59 if (zret != Z_OK) {
60 av_log(avctx, AV_LOG_ERROR, "Could not reset inflate: %d.\n", zret);
62 }
63
64 if ((ret = ff_get_buffer(avctx, pic, AV_GET_BUFFER_FLAG_REF)) < 0)
65 return ret;
66
67 zstream->next_in = avpkt->data;
68 zstream->avail_in = avpkt->size;
69
70 dst = pic->data[0] + (avctx->height - 1) * pic->linesize[0];
71
72 /**
73 * ZeroCodec has very simple interframe compression. If a value
74 * is the same as the previous frame, set it to 0.
75 */
76
77 for (i = 0; i < avctx->height; i++) {
78 zstream->next_out = dst;
79 zstream->avail_out = avctx->width << 1;
80
81 zret = inflate(zstream, Z_SYNC_FLUSH);
82 if (zret != Z_OK && zret != Z_STREAM_END) {
83 av_log(avctx, AV_LOG_ERROR,
84 "Inflate failed with return code: %d.\n", zret);
86 }
87
88 if (!(avpkt->flags & AV_PKT_FLAG_KEY)) {
89 for (j = 0; j < avctx->width << 1; j++)
90 dst[j] += prev[j] & -!dst[j];
91 prev -= prev_pic->linesize[0];
92 }
93
94 dst -= pic->linesize[0];
95 }
96
97 if ((ret = av_frame_replace(zc->previous_frame, pic)) < 0)
98 return ret;
99
100 *got_frame = 1;
101
102 return avpkt->size;
103}
104
106{
107 ZeroCodecContext *zc = avctx->priv_data;
108
110
112
113 return 0;
114}
115
117{
118 ZeroCodecContext *zc = avctx->priv_data;
119
121 avctx->bits_per_raw_sample = 8;
122
124 if (!zc->previous_frame)
125 return AVERROR(ENOMEM);
126
127 return ff_inflate_init(&zc->zstream, avctx);
128}
129
131{
132 ZeroCodecContext *zc = avctx->priv_data;
133
135}
136
138 .p.type = AVMEDIA_TYPE_VIDEO,
139 .p.name = "zerocodec",
140 CODEC_LONG_NAME("ZeroCodec Lossless Video"),
141 .p.id = AV_CODEC_ID_ZEROCODEC,
142 .priv_data_size = sizeof(ZeroCodecContext),
145 .flush = zerocodec_decode_flush,
146 .close = zerocodec_decode_close,
147 .p.capabilities = AV_CODEC_CAP_DR1,
148 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
149};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
const FFCodec ff_zerocodec_decoder
Definition zerocodec.c:137
Libavcodec external API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#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...
common internal and external API header
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition decode.c:1777
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
#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_ZEROCODEC
Definition codec_id.h:210
#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
Macro definitions for various function/variable attributes.
#define av_cold
Definition attributes.h:117
@ AV_PIX_FMT_UYVY422
packed YUV 4:2:2, 16bpp, Cb Y0 Cr Y1
Definition pixfmt.h:88
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
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition avcodec.h:1571
void * priv_data
Definition avcodec.h:470
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 flags
Frame flags, a combination of AV_FRAME_FLAGS.
Definition frame.h:716
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
enum AVPictureType pict_type
Picture type of the frame.
Definition frame.h:564
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
z_stream zstream
FFZStream zstream
Definition zerocodec.c:30
AVFrame * previous_frame
Definition zerocodec.c:29
#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)
static av_cold int zerocodec_decode_close(AVCodecContext *avctx)
Definition zerocodec.c:105
static av_cold void zerocodec_decode_flush(AVCodecContext *avctx)
Definition zerocodec.c:130
static int zerocodec_decode_frame(AVCodecContext *avctx, AVFrame *pic, int *got_frame, AVPacket *avpkt)
Definition zerocodec.c:33
static av_cold int zerocodec_decode_init(AVCodecContext *avctx)
Definition zerocodec.c:116
void ff_inflate_end(FFZStream *zstream)
Wrapper around inflateEnd().
int ff_inflate_init(FFZStream *zstream, void *logctx)
Wrapper around inflateInit().