Libav
flashsvenc.c
Go to the documentation of this file.
1 /*
2  * Flash Screen Video encoder
3  * Copyright (C) 2004 Alex Beregszaszi
4  * Copyright (C) 2006 Benjamin Larsson
5  *
6  * This file is part of Libav.
7  *
8  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 /* Encoding development sponsored by http://fh-campuswien.ac.at */
24 
36 /*
37  * Encoding ideas: A basic encoder would just use a fixed block size.
38  * Block sizes can be multiples of 16, from 16 to 256. The blocks don't
39  * have to be quadratic. A brute force search with a set of different
40  * block sizes should give a better result than to just use a fixed size.
41  *
42  * TODO:
43  * Don't reencode the frame in brute force mode if the frame is a dupe.
44  * Speed up. Make the difference check faster.
45  */
46 
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <zlib.h>
50 
51 #include "avcodec.h"
52 #include "internal.h"
53 #include "put_bits.h"
54 #include "bytestream.h"
55 
56 
57 typedef struct FlashSVContext {
64  int block_size;
65  z_stream zstream;
68 
69 static int copy_region_enc(uint8_t *sptr, uint8_t *dptr, int dx, int dy,
70  int h, int w, int stride, uint8_t *pfptr)
71 {
72  int i, j;
73  uint8_t *nsptr;
74  uint8_t *npfptr;
75  int diff = 0;
76 
77  for (i = dx + h; i > dx; i--) {
78  nsptr = sptr + i * stride + dy * 3;
79  npfptr = pfptr + i * stride + dy * 3;
80  for (j = 0; j < w * 3; j++) {
81  diff |= npfptr[j] ^ nsptr[j];
82  dptr[j] = nsptr[j];
83  }
84  dptr += w * 3;
85  }
86  if (diff)
87  return 1;
88  return 0;
89 }
90 
92 {
93  FlashSVContext *s = avctx->priv_data;
94 
95  deflateEnd(&s->zstream);
96 
97  av_free(s->encbuffer);
99  av_free(s->tmpblock);
100 
101  return 0;
102 }
103 
105 {
106  FlashSVContext *s = avctx->priv_data;
107 
108  s->avctx = avctx;
109 
110  if (avctx->width > 4095 || avctx->height > 4095) {
111  av_log(avctx, AV_LOG_ERROR,
112  "Input dimensions too large, input must be max 4096x4096 !\n");
113  return AVERROR_INVALIDDATA;
114  }
115 
116  // Needed if zlib unused or init aborted before deflateInit
117  memset(&s->zstream, 0, sizeof(z_stream));
118 
119  s->last_key_frame = 0;
120 
121  s->image_width = avctx->width;
122  s->image_height = avctx->height;
123 
124  s->tmpblock = av_mallocz(3 * 256 * 256);
125  s->encbuffer = av_mallocz(s->image_width * s->image_height * 3);
126 
127  if (!s->tmpblock || !s->encbuffer) {
128  av_log(avctx, AV_LOG_ERROR, "Memory allocation failed.\n");
129  return AVERROR(ENOMEM);
130  }
131 
132  return 0;
133 }
134 
135 
136 static int encode_bitstream(FlashSVContext *s, const AVFrame *p, uint8_t *buf,
137  int buf_size, int block_width, int block_height,
138  uint8_t *previous_frame, int *I_frame)
139 {
140 
141  PutBitContext pb;
142  int h_blocks, v_blocks, h_part, v_part, i, j;
143  int buf_pos, res;
144  int pred_blocks = 0;
145 
146  init_put_bits(&pb, buf, buf_size * 8);
147 
148  put_bits(&pb, 4, block_width / 16 - 1);
149  put_bits(&pb, 12, s->image_width);
150  put_bits(&pb, 4, block_height / 16 - 1);
151  put_bits(&pb, 12, s->image_height);
152  flush_put_bits(&pb);
153  buf_pos = 4;
154 
155  h_blocks = s->image_width / block_width;
156  h_part = s->image_width % block_width;
157  v_blocks = s->image_height / block_height;
158  v_part = s->image_height % block_height;
159 
160  /* loop over all block columns */
161  for (j = 0; j < v_blocks + (v_part ? 1 : 0); j++) {
162 
163  int y_pos = j * block_height; // vertical position in frame
164  int cur_blk_height = (j < v_blocks) ? block_height : v_part;
165 
166  /* loop over all block rows */
167  for (i = 0; i < h_blocks + (h_part ? 1 : 0); i++) {
168  int x_pos = i * block_width; // horizontal position in frame
169  int cur_blk_width = (i < h_blocks) ? block_width : h_part;
170  int ret = Z_OK;
171  uint8_t *ptr = buf + buf_pos;
172 
173  /* copy the block to the temp buffer before compression
174  * (if it differs from the previous frame's block) */
175  res = copy_region_enc(p->data[0], s->tmpblock,
176  s->image_height - (y_pos + cur_blk_height + 1),
177  x_pos, cur_blk_height, cur_blk_width,
178  p->linesize[0], previous_frame);
179 
180  if (res || *I_frame) {
181  unsigned long zsize = 3 * block_width * block_height;
182  ret = compress2(ptr + 2, &zsize, s->tmpblock,
183  3 * cur_blk_width * cur_blk_height, 9);
184 
185  //ret = deflateReset(&s->zstream);
186  if (ret != Z_OK)
188  "error while compressing block %dx%d\n", i, j);
189 
190  bytestream_put_be16(&ptr, zsize);
191  buf_pos += zsize + 2;
192  ff_dlog(s->avctx, "buf_pos = %d\n", buf_pos);
193  } else {
194  pred_blocks++;
195  bytestream_put_be16(&ptr, 0);
196  buf_pos += 2;
197  }
198  }
199  }
200 
201  if (pred_blocks)
202  *I_frame = 0;
203  else
204  *I_frame = 1;
205 
206  return buf_pos;
207 }
208 
209 
211  const AVFrame *pict, int *got_packet)
212 {
213  FlashSVContext * const s = avctx->priv_data;
214  const AVFrame * const p = pict;
215  uint8_t *pfptr;
216  int res;
217  int I_frame = 0;
218  int opt_w = 4, opt_h = 4;
219 
220  /* First frame needs to be a keyframe */
221  if (avctx->frame_number == 0) {
223  if (!s->previous_frame) {
224  av_log(avctx, AV_LOG_ERROR, "Memory allocation failed.\n");
225  return AVERROR(ENOMEM);
226  }
227  I_frame = 1;
228  }
229 
230  if (p->linesize[0] < 0)
231  pfptr = s->previous_frame - (s->image_height - 1) * p->linesize[0];
232  else
233  pfptr = s->previous_frame;
234 
235  /* Check the placement of keyframes */
236  if (avctx->gop_size > 0 &&
237  avctx->frame_number >= s->last_key_frame + avctx->gop_size) {
238  I_frame = 1;
239  }
240 
241  if ((res = ff_alloc_packet(pkt, s->image_width * s->image_height * 3)) < 0) {
242  //Conservative upper bound check for compressed data
243  av_log(avctx, AV_LOG_ERROR, "Error getting output packet of size %d.\n",
244  s->image_width * s->image_height * 3);
245  return res;
246  }
247 
248  pkt->size = encode_bitstream(s, p, pkt->data, pkt->size, opt_w * 16, opt_h * 16,
249  pfptr, &I_frame);
250 
251  //save the current frame
252  if (p->linesize[0] > 0)
253  memcpy(s->previous_frame, p->data[0], s->image_height * p->linesize[0]);
254  else
255  memcpy(s->previous_frame,
256  p->data[0] + p->linesize[0] * (s->image_height - 1),
257  s->image_height * FFABS(p->linesize[0]));
258 
259  //mark the frame type so the muxer can mux it correctly
260  if (I_frame) {
261 #if FF_API_CODED_FRAME
264  avctx->coded_frame->key_frame = 1;
266 #endif
267  s->last_key_frame = avctx->frame_number;
268  ff_dlog(avctx, "Inserting keyframe at frame %d\n", avctx->frame_number);
269  } else {
270 #if FF_API_CODED_FRAME
273  avctx->coded_frame->key_frame = 0;
275 #endif
276  }
277 
278  if (I_frame)
279  pkt->flags |= AV_PKT_FLAG_KEY;
280  *got_packet = 1;
281 
282  return 0;
283 }
284 
286  .name = "flashsv",
287  .long_name = NULL_IF_CONFIG_SMALL("Flash Screen Video"),
288  .type = AVMEDIA_TYPE_VIDEO,
289  .id = AV_CODEC_ID_FLASHSV,
290  .priv_data_size = sizeof(FlashSVContext),
292  .encode2 = flashsv_encode_frame,
293  .close = flashsv_encode_end,
295 };
static int encode_bitstream(FlashSVContext *s, const AVFrame *p, uint8_t *buf, int buf_size, int block_width, int block_height, uint8_t *previous_frame, int *I_frame)
Definition: flashsvenc.c:136
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
This structure describes decoded (raw) audio or video data.
Definition: frame.h:140
int size
Definition: avcodec.h:1347
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
int stride
Definition: mace.c:144
AVCodec.
Definition: avcodec.h:3120
uint8_t * encbuffer
Definition: flashsvenc.c:63
AVCodecContext * avctx
Definition: flashsv.c:52
uint8_t
#define av_cold
Definition: attributes.h:66
uint8_t * data
Definition: avcodec.h:1346
int image_width
Definition: flashsv.c:54
static int copy_region_enc(uint8_t *sptr, uint8_t *dptr, int dx, int dy, int h, int w, int stride, uint8_t *pfptr)
Definition: flashsvenc.c:69
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1378
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:190
static int flashsv_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pict, int *got_packet)
Definition: flashsvenc.c:210
int block_width
Definition: flashsv.c:55
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:148
const char * name
Name of the codec implementation.
Definition: avcodec.h:3127
static void put_bits(PutBitContext *s, int n, unsigned int value)
Write up to 31 bits into a bitstream.
Definition: put_bits.h:134
AVCodec ff_flashsv_encoder
Definition: flashsvenc.c:285
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1352
int last_key_frame
Definition: flashsvenc.c:66
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:201
static av_cold int flashsv_encode_end(AVCodecContext *avctx)
Definition: flashsvenc.c:91
int width
picture width / height.
Definition: avcodec.h:1580
#define FFABS(a)
Definition: common.h:61
int ff_alloc_packet(AVPacket *avpkt, int size)
Check AVPacket size and/or allocate data.
Definition: utils.c:1211
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:62
if(ac->has_optimized_func)
int block_size
Definition: flashsv.c:57
Libavcodec external API header.
#define ff_dlog(ctx,...)
Definition: internal.h:60
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:158
main external API structure.
Definition: avcodec.h:1409
z_stream zstream
Definition: flashsv.c:58
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:257
uint8_t * previous_frame
Definition: flashsvenc.c:59
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:146
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
Definition: avcodec.h:1606
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:77
common internal api header.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:83
attribute_deprecated AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2797
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:48
static av_cold int init(AVCodecParserContext *s)
Definition: h264_parser.c:582
void * priv_data
Definition: avcodec.h:1451
uint8_t * tmpblock
Definition: flashsv.c:56
static av_cold int flashsv_encode_init(AVCodecContext *avctx)
Definition: flashsvenc.c:104
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:78
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:196
int frame_number
Frame counter, set by libavcodec.
Definition: avcodec.h:2183
int block_height
Definition: flashsv.c:55
AVPixelFormat
Pixel format.
Definition: pixfmt.h:57
This structure stores compressed data.
Definition: avcodec.h:1323
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:211
Predicted.
Definition: avutil.h:261
int image_height
Definition: flashsv.c:54
bitstream writer API