FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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: BGR24
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 "internal.h"
46 
47 typedef struct ScreenpressoContext {
49 
50  /* zlib interation */
52  uLongf inflated_size;
54 
56 {
58 
59  av_frame_free(&ctx->current);
60  av_freep(&ctx->inflated_buf);
61 
62  return 0;
63 }
64 
66 {
68 
69  /* These needs to be set to estimate uncompressed buffer */
70  int ret = av_image_check_size(avctx->width, avctx->height, 0, avctx);
71  if (ret < 0) {
72  av_log(avctx, AV_LOG_ERROR, "Invalid image size %dx%d.\n",
73  avctx->width, avctx->height);
74  return ret;
75  }
76 
77  /* Allocate current frame */
78  ctx->current = av_frame_alloc();
79  if (!ctx->current)
80  return AVERROR(ENOMEM);
81 
82  avctx->pix_fmt = AV_PIX_FMT_BGR24;
83 
84  /* Allocate maximum size possible, a full frame */
85  ctx->inflated_size = avctx->width * avctx->height * 3;
87  if (!ctx->inflated_buf)
88  return AVERROR(ENOMEM);
89 
90  return 0;
91 }
92 
93 static void sum_delta_flipped(uint8_t *dst, int dst_linesize,
94  const uint8_t *src, int src_linesize,
95  int bytewidth, int height)
96 {
97  int i;
98  for (; height > 0; height--) {
99  for (i = 0; i < bytewidth; i++)
100  dst[i] += src[(height - 1) * src_linesize + i];
101  dst += dst_linesize;
102  }
103 }
104 
106  int *got_frame, AVPacket *avpkt)
107 {
109  AVFrame *frame = data;
110  uLongf length = ctx->inflated_size;
111  int keyframe;
112  int ret;
113 
114  /* Size check */
115  if (avpkt->size < 3) {
116  av_log(avctx, AV_LOG_ERROR, "Packet too small (%d)\n", avpkt->size);
117  return AVERROR_INVALIDDATA;
118  }
119 
120  /* Basic sanity check, but not really harmful */
121  if ((avpkt->data[0] != 0x73 && avpkt->data[0] != 0x72) ||
122  avpkt->data[1] != 8) { // bpp probably
123  av_log(avctx, AV_LOG_WARNING, "Unknown header 0x%02X%02X\n",
124  avpkt->data[0], avpkt->data[1]);
125  }
126  keyframe = (avpkt->data[0] == 0x73);
127 
128  /* Inflate the frame after the 2 byte header */
129  ret = uncompress(ctx->inflated_buf, &length,
130  avpkt->data + 2, avpkt->size - 2);
131  if (ret) {
132  av_log(avctx, AV_LOG_ERROR, "Deflate error %d.\n", ret);
133  return AVERROR_UNKNOWN;
134  }
135 
136  ret = ff_reget_buffer(avctx, ctx->current);
137  if (ret < 0)
138  return ret;
139 
140  /* When a keyframe is found, copy it (flipped) */
141  if (keyframe)
142  av_image_copy_plane(ctx->current->data[0] +
143  ctx->current->linesize[0] * (avctx->height - 1),
144  -1 * ctx->current->linesize[0],
145  ctx->inflated_buf, avctx->width * 3,
146  avctx->width * 3, avctx->height);
147  /* Otherwise sum the delta on top of the current frame */
148  else
149  sum_delta_flipped(ctx->current->data[0], ctx->current->linesize[0],
150  ctx->inflated_buf, avctx->width * 3,
151  avctx->width * 3, avctx->height);
152 
153  /* Frame is ready to be output */
154  ret = av_frame_ref(frame, ctx->current);
155  if (ret < 0)
156  return ret;
157 
158  /* Usual properties */
159  if (keyframe) {
160  frame->pict_type = AV_PICTURE_TYPE_I;
161  frame->key_frame = 1;
162  } else {
163  frame->pict_type = AV_PICTURE_TYPE_P;
164  }
165  *got_frame = 1;
166 
167  return 0;
168 }
169 
171  .name = "screenpresso",
172  .long_name = NULL_IF_CONFIG_SMALL("Screenpresso"),
173  .type = AVMEDIA_TYPE_VIDEO,
175  .init = screenpresso_init,
176  .decode = screenpresso_decode_frame,
177  .close = screenpresso_close,
178  .priv_data_size = sizeof(ScreenpressoContext),
179  .capabilities = AV_CODEC_CAP_DR1,
180  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
182 };
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: internal.h:48
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
This structure describes decoded (raw) audio or video data.
Definition: frame.h:181
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
AVFormatContext * ctx
Definition: movenc-test.c:48
misc image utilities
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
memory handling functions
int size
Definition: avcodec.h:1468
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1752
AVCodec ff_screenpresso_decoder
Definition: screenpresso.c:170
AVCodec.
Definition: avcodec.h:3392
#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: internal.h:40
uint8_t
#define av_cold
Definition: attributes.h:82
#define av_malloc(s)
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:141
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:375
static AVFrame * frame
static av_cold int screenpresso_close(AVCodecContext *avctx)
Definition: screenpresso.c:55
uint8_t * data
Definition: avcodec.h:1467
#define av_log(a,...)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:154
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
GLsizei GLsizei * length
Definition: opengl_enc.c:115
const char * name
Name of the codec implementation.
Definition: avcodec.h:3399
common internal API header
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:251
int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
Identical in function to av_frame_make_writable(), except it uses ff_get_buffer() to allocate the buf...
Definition: utils.c:943
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:252
uint8_t * inflated_buf
Definition: screenpresso.c:51
int width
picture width / height.
Definition: avcodec.h:1711
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:66
#define src
Definition: vp9dsp.c:530
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:209
main external API structure.
Definition: avcodec.h:1532
BYTE int const BYTE int int int height
Definition: avisynth_c.h:676
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:192
common internal api header.
static av_cold int screenpresso_init(AVCodecContext *avctx)
Definition: screenpresso.c:65
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:71
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:93
void * priv_data
Definition: avcodec.h:1574
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:247
#define av_freep(p)
static int screenpresso_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: screenpresso.c:105
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:287
This structure stores compressed data.
Definition: avcodec.h:1444
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:856
Predicted.
Definition: avutil.h:267