FFmpeg
screenpresso.c
Go to the documentation of this file.
1 /*
2  * Screenpresso decoder
3  * Copyright (C) 2015 Vittorio Giovara <vittorio.giovara@gmail.com>
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * @file
24  * Screenpresso decoder
25  *
26  * Fourcc: SPV1
27  *
28  * Screenpresso simply horizontally flips and then deflates frames,
29  * alternating full pictures and deltas. Deltas are related to the currently
30  * rebuilt frame (not the reference), and since there is no coordinate system
31  * they contain exactly as many pixel as the keyframe.
32  *
33  * Supports: BGR0, BGR24, RGB555
34  */
35 
36 #include <stdint.h>
37 #include <string.h>
38 #include <zlib.h>
39 
40 #include "libavutil/imgutils.h"
41 #include "libavutil/internal.h"
42 #include "libavutil/mem.h"
43 
44 #include "avcodec.h"
45 #include "codec_internal.h"
46 #include "internal.h"
47 
48 typedef struct ScreenpressoContext {
50 
51  /* zlib interaction */
52  uint8_t *inflated_buf;
53  uLongf inflated_size;
55 
57 {
59 
60  av_frame_free(&ctx->current);
61  av_freep(&ctx->inflated_buf);
62 
63  return 0;
64 }
65 
67 {
69 
70  /* These needs to be set to estimate uncompressed buffer */
71  int ret = av_image_check_size(avctx->width, avctx->height, 0, avctx);
72  if (ret < 0) {
73  av_log(avctx, AV_LOG_ERROR, "Invalid image size %dx%d.\n",
74  avctx->width, avctx->height);
75  return ret;
76  }
77 
78  /* Allocate current frame */
79  ctx->current = av_frame_alloc();
80  if (!ctx->current)
81  return AVERROR(ENOMEM);
82 
83  /* Allocate maximum size possible, a full RGBA frame */
84  ctx->inflated_size = avctx->width * avctx->height * 4;
85  ctx->inflated_buf = av_malloc(ctx->inflated_size);
86  if (!ctx->inflated_buf)
87  return AVERROR(ENOMEM);
88 
89  return 0;
90 }
91 
92 static void sum_delta_flipped(uint8_t *dst, int dst_linesize,
93  const uint8_t *src, int src_linesize,
94  int bytewidth, int height)
95 {
96  int i;
97  for (; height > 0; height--) {
98  const uint8_t *src1 = &src[(height - 1) * src_linesize];
99  for (i = 0; i < bytewidth; i++)
100  dst[i] += src1[i];
101  dst += dst_linesize;
102  }
103 }
104 
106  int *got_frame, AVPacket *avpkt)
107 {
109  uLongf length = ctx->inflated_size;
110  int keyframe, component_size, src_linesize;
111  int ret;
112 
113  /* Size check */
114  if (avpkt->size < 3) {
115  av_log(avctx, AV_LOG_ERROR, "Packet too small (%d)\n", avpkt->size);
116  return AVERROR_INVALIDDATA;
117  }
118 
119  /* Compression level (4 bits) and keyframe information (1 bit) */
120  av_log(avctx, AV_LOG_DEBUG, "Compression level %d\n", avpkt->data[0] >> 4);
121  keyframe = avpkt->data[0] & 1;
122 
123  /* Pixel size */
124  component_size = ((avpkt->data[1] >> 2) & 0x03) + 1;
125  switch (component_size) {
126  case 2:
127  avctx->pix_fmt = AV_PIX_FMT_RGB555LE;
128  break;
129  case 3:
130  avctx->pix_fmt = AV_PIX_FMT_BGR24;
131  break;
132  case 4:
133  avctx->pix_fmt = AV_PIX_FMT_BGR0;
134  break;
135  default:
136  av_log(avctx, AV_LOG_ERROR, "Invalid bits per pixel value (%d)\n",
137  component_size);
138  return AVERROR_INVALIDDATA;
139  }
140 
141  /* Inflate the frame after the 2 byte header */
142  ret = uncompress(ctx->inflated_buf, &length,
143  avpkt->data + 2, avpkt->size - 2);
144  if (ret) {
145  av_log(avctx, AV_LOG_ERROR, "Deflate error %d.\n", ret);
146  return AVERROR_UNKNOWN;
147  }
148 
149  ret = ff_reget_buffer(avctx, ctx->current, 0);
150  if (ret < 0)
151  return ret;
152 
153  /* Codec has aligned strides */
154  src_linesize = FFALIGN(avctx->width * component_size, 4);
155 
156  /* When a keyframe is found, copy it (flipped) */
157  if (keyframe)
158  av_image_copy_plane(ctx->current->data[0] +
159  ctx->current->linesize[0] * (avctx->height - 1),
160  -1 * ctx->current->linesize[0],
161  ctx->inflated_buf, src_linesize,
162  avctx->width * component_size, avctx->height);
163  /* Otherwise sum the delta on top of the current frame */
164  else
165  sum_delta_flipped(ctx->current->data[0], ctx->current->linesize[0],
166  ctx->inflated_buf, src_linesize,
167  avctx->width * component_size, avctx->height);
168 
169  /* Frame is ready to be output */
170  ret = av_frame_ref(frame, ctx->current);
171  if (ret < 0)
172  return ret;
173 
174  /* Usual properties */
175  if (keyframe) {
176  frame->pict_type = AV_PICTURE_TYPE_I;
177  frame->key_frame = 1;
178  } else {
179  frame->pict_type = AV_PICTURE_TYPE_P;
180  }
181  *got_frame = 1;
182 
183  return avpkt->size;
184 }
185 
187  .p.name = "screenpresso",
188  .p.long_name = NULL_IF_CONFIG_SMALL("Screenpresso"),
189  .p.type = AVMEDIA_TYPE_VIDEO,
190  .p.id = AV_CODEC_ID_SCREENPRESSO,
191  .init = screenpresso_init,
193  .close = screenpresso_close,
194  .priv_data_size = sizeof(ScreenpressoContext),
195  .p.capabilities = AV_CODEC_CAP_DR1,
196  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
198 };
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:39
ScreenpressoContext::inflated_size
uLongf inflated_size
Definition: screenpresso.c:53
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
src1
const pixel * src1
Definition: h264pred_template.c:421
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:111
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:325
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:374
FFCodec
Definition: codec_internal.h:112
AV_PIX_FMT_BGR24
@ AV_PIX_FMT_BGR24
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:69
AV_CODEC_ID_SCREENPRESSO
@ AV_CODEC_ID_SCREENPRESSO
Definition: codec_id.h:242
AVERROR_UNKNOWN
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:73
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:30
av_image_copy_plane
void av_image_copy_plane(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize, int bytewidth, int height)
Copy image plane from src to dst.
Definition: imgutils.c:374
ScreenpressoContext
Definition: screenpresso.c:48
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:116
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:99
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:254
ScreenpressoContext::inflated_buf
uint8_t * inflated_buf
Definition: screenpresso.c:52
ff_screenpresso_decoder
const FFCodec ff_screenpresso_decoder
Definition: screenpresso.c:186
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:201
ctx
AVFormatContext * ctx
Definition: movenc.c:48
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:274
AV_PIX_FMT_BGR0
@ AV_PIX_FMT_BGR0
packed BGR 8:8:8, 32bpp, BGRXBGRX... X=unused/undefined
Definition: pixfmt.h:230
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:375
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
av_frame_ref
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
Definition: frame.c:343
codec_internal.h
height
#define height
AV_PIX_FMT_RGB555LE
@ AV_PIX_FMT_RGB555LE
packed RGB 5:5:5, 16bpp, (msb)1X 5R 5G 5B(lsb), little-endian, X=unused/undefined
Definition: pixfmt.h:108
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
internal.h
sum_delta_flipped
static void sum_delta_flipped(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize, int bytewidth, int height)
Definition: screenpresso.c:92
FF_CODEC_CAP_INIT_THREADSAFE
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: codec_internal.h:31
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:203
AVCodecContext::height
int height
Definition: avcodec.h:562
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:599
avcodec.h
screenpresso_decode_frame
static int screenpresso_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *avpkt)
Definition: screenpresso.c:105
ff_reget_buffer
int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Identical in function to ff_get_buffer(), except it reuses the existing buffer if available.
Definition: decode.c:1521
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
AVCodecContext
main external API structure.
Definition: avcodec.h:389
screenpresso_init
static av_cold int screenpresso_init(AVCodecContext *avctx)
Definition: screenpresso.c:66
AV_PICTURE_TYPE_P
@ AV_PICTURE_TYPE_P
Predicted.
Definition: avutil.h:275
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
mem.h
FFALIGN
#define FFALIGN(x, a)
Definition: macros.h:78
AVPacket
This structure stores compressed data.
Definition: packet.h:351
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:416
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
ScreenpressoContext::current
AVFrame * current
Definition: screenpresso.c:49
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:562
imgutils.h
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
screenpresso_close
static av_cold int screenpresso_close(AVCodecContext *avctx)
Definition: screenpresso.c:56
av_image_check_size
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
Definition: imgutils.c:318