FFmpeg  4.3.7
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 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 /* Encoding development sponsored by http://fh-campuswien.ac.at */
24 
25 /**
26  * @file
27  * Flash Screen Video encoder
28  * @author Alex Beregszaszi
29  * @author Benjamin Larsson
30  *
31  * A description of the bitstream format for Flash Screen Video version 1/2
32  * is part of the SWF File Format Specification (version 10), which can be
33  * downloaded from http://www.adobe.com/devnet/swf.html.
34  */
35 
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 {
63  int block_size;
64  z_stream zstream;
66  uint8_t tmpblock[3 * 256 * 256];
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_freep(&s->encbuffer);
99 
100  return 0;
101 }
102 
104 {
105  FlashSVContext *s = avctx->priv_data;
106 
107  s->avctx = avctx;
108 
109  if (avctx->width > 4095 || avctx->height > 4095) {
110  av_log(avctx, AV_LOG_ERROR,
111  "Input dimensions too large, input must be max 4095x4095 !\n");
112  return AVERROR_INVALIDDATA;
113  }
114 
115  // Needed if zlib unused or init aborted before deflateInit
116  memset(&s->zstream, 0, sizeof(z_stream));
117 
118  s->last_key_frame = 0;
119 
120  s->image_width = avctx->width;
121  s->image_height = avctx->height;
122 
123  s->encbuffer = av_mallocz(s->image_width * s->image_height * 3);
124 
125  if (!s->encbuffer) {
126  av_log(avctx, AV_LOG_ERROR, "Memory allocation failed.\n");
127  return AVERROR(ENOMEM);
128  }
129 
130  return 0;
131 }
132 
133 
134 static int encode_bitstream(FlashSVContext *s, const AVFrame *p, uint8_t *buf,
135  int buf_size, int block_width, int block_height,
136  uint8_t *previous_frame, int *I_frame)
137 {
138 
139  PutBitContext pb;
140  int h_blocks, v_blocks, h_part, v_part, i, j;
141  int buf_pos, res;
142  int pred_blocks = 0;
143 
144  init_put_bits(&pb, buf, buf_size);
145 
146  put_bits(&pb, 4, block_width / 16 - 1);
147  put_bits(&pb, 12, s->image_width);
148  put_bits(&pb, 4, block_height / 16 - 1);
149  put_bits(&pb, 12, s->image_height);
150  flush_put_bits(&pb);
151  buf_pos = 4;
152 
153  h_blocks = s->image_width / block_width;
154  h_part = s->image_width % block_width;
155  v_blocks = s->image_height / block_height;
156  v_part = s->image_height % block_height;
157 
158  /* loop over all block columns */
159  for (j = 0; j < v_blocks + (v_part ? 1 : 0); j++) {
160 
161  int y_pos = j * block_height; // vertical position in frame
162  int cur_blk_height = (j < v_blocks) ? block_height : v_part;
163 
164  /* loop over all block rows */
165  for (i = 0; i < h_blocks + (h_part ? 1 : 0); i++) {
166  int x_pos = i * block_width; // horizontal position in frame
167  int cur_blk_width = (i < h_blocks) ? block_width : h_part;
168  int ret = Z_OK;
169  uint8_t *ptr = buf + buf_pos;
170 
171  /* copy the block to the temp buffer before compression
172  * (if it differs from the previous frame's block) */
173  res = copy_region_enc(p->data[0], s->tmpblock,
174  s->image_height - (y_pos + cur_blk_height + 1),
175  x_pos, cur_blk_height, cur_blk_width,
176  p->linesize[0], previous_frame);
177 
178  if (res || *I_frame) {
179  unsigned long zsize = 3 * block_width * block_height;
180  ret = compress2(ptr + 2, &zsize, s->tmpblock,
181  3 * cur_blk_width * cur_blk_height, 9);
182 
183  //ret = deflateReset(&s->zstream);
184  if (ret != Z_OK)
186  "error while compressing block %dx%d\n", i, j);
187 
188  bytestream_put_be16(&ptr, zsize);
189  buf_pos += zsize + 2;
190  ff_dlog(s->avctx, "buf_pos = %d\n", buf_pos);
191  } else {
192  pred_blocks++;
193  bytestream_put_be16(&ptr, 0);
194  buf_pos += 2;
195  }
196  }
197  }
198 
199  if (pred_blocks)
200  *I_frame = 0;
201  else
202  *I_frame = 1;
203 
204  return buf_pos;
205 }
206 
207 
209  const AVFrame *pict, int *got_packet)
210 {
211  FlashSVContext * const s = avctx->priv_data;
212  const AVFrame * const p = pict;
213  uint8_t *pfptr;
214  int res;
215  int I_frame = 0;
216  int opt_w = 4, opt_h = 4;
217 
218  /* First frame needs to be a keyframe */
219  if (avctx->frame_number == 0) {
221  if (!s->previous_frame) {
222  av_log(avctx, AV_LOG_ERROR, "Memory allocation failed.\n");
223  return AVERROR(ENOMEM);
224  }
225  I_frame = 1;
226  }
227 
228  if (p->linesize[0] < 0)
229  pfptr = s->previous_frame - (s->image_height - 1) * p->linesize[0];
230  else
231  pfptr = s->previous_frame;
232 
233  /* Check the placement of keyframes */
234  if (avctx->gop_size > 0 &&
235  avctx->frame_number >= s->last_key_frame + avctx->gop_size) {
236  I_frame = 1;
237  }
238 
239  if ((res = ff_alloc_packet2(avctx, pkt, s->image_width * s->image_height * 3, 0)) < 0)
240  return res;
241 
242  pkt->size = encode_bitstream(s, p, pkt->data, pkt->size, opt_w * 16, opt_h * 16,
243  pfptr, &I_frame);
244 
245  //save the current frame
246  if (p->linesize[0] > 0)
247  memcpy(s->previous_frame, p->data[0], s->image_height * p->linesize[0]);
248  else
249  memcpy(s->previous_frame,
250  p->data[0] + p->linesize[0] * (s->image_height - 1),
251  s->image_height * FFABS(p->linesize[0]));
252 
253  //mark the frame type so the muxer can mux it correctly
254  if (I_frame) {
255 #if FF_API_CODED_FRAME
258  avctx->coded_frame->key_frame = 1;
260 #endif
261  s->last_key_frame = avctx->frame_number;
262  ff_dlog(avctx, "Inserting keyframe at frame %d\n", avctx->frame_number);
263  } else {
264 #if FF_API_CODED_FRAME
267  avctx->coded_frame->key_frame = 0;
269 #endif
270  }
271 
272  if (I_frame)
273  pkt->flags |= AV_PKT_FLAG_KEY;
274  *got_packet = 1;
275 
276  return 0;
277 }
278 
280  .name = "flashsv",
281  .long_name = NULL_IF_CONFIG_SMALL("Flash Screen Video"),
282  .type = AVMEDIA_TYPE_VIDEO,
283  .id = AV_CODEC_ID_FLASHSV,
284  .priv_data_size = sizeof(FlashSVContext),
286  .encode2 = flashsv_encode_frame,
287  .close = flashsv_encode_end,
289 };
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:134
#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:300
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:208
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
int size
Definition: packet.h:356
static AVPacket pkt
int stride
Definition: mace.c:144
AVCodec.
Definition: codec.h:190
uint8_t * encbuffer
Definition: flashsvenc.c:62
AVCodecContext * avctx
Definition: flashsv.c:52
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
Definition: encode.c:32
uint8_t
#define av_cold
Definition: attributes.h:88
uint8_t * data
Definition: packet.h:355
#define ff_dlog(a,...)
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_log(a,...)
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:388
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
static int flashsv_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pict, int *got_packet)
Definition: flashsvenc.c:208
int block_width
Definition: flashsv.c:55
#define AVERROR(e)
Definition: error.h:43
uint8_t * tmpblock
Definition: flashsv.c:56
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:188
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:237
const char * name
Name of the codec implementation.
Definition: codec.h:197
AVCodec ff_flashsv_encoder
Definition: flashsvenc.c:279
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:361
int last_key_frame
Definition: flashsvenc.c:65
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:383
static av_cold int flashsv_encode_end(AVCodecContext *avctx)
Definition: flashsvenc.c:91
int width
picture width / height.
Definition: avcodec.h:699
uint8_t w
Definition: llviddspenc.c:38
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
#define s(width, name)
Definition: cbs_vp9.c:257
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:69
int block_size
Definition: flashsv.c:57
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:331
main external API structure.
Definition: avcodec.h:526
z_stream zstream
Definition: flashsv.c:58
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:275
uint8_t * previous_frame
Definition: flashsvenc.c:59
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:314
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
Definition: avcodec.h:721
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:84
common internal api header.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:101
if(ret< 0)
Definition: vf_mcdeint.c:279
attribute_deprecated AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:1776
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:48
void * priv_data
Definition: avcodec.h:553
static av_cold int flashsv_encode_init(AVCodecContext *avctx)
Definition: flashsvenc.c:103
static av_always_inline int diff(const uint32_t a, const uint32_t b)
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:85
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:378
int frame_number
Frame counter, set by libavcodec.
Definition: avcodec.h:1217
#define av_freep(p)
int block_height
Definition: flashsv.c:55
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
This structure stores compressed data.
Definition: packet.h:332
Predicted.
Definition: avutil.h:275
int image_height
Definition: flashsv.c:54
bitstream writer API