FFmpeg
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 <stdint.h>
48 #include <zlib.h>
49 
50 #include "libavutil/buffer.h"
51 
52 #include "avcodec.h"
53 #include "codec_internal.h"
54 #include "encode.h"
55 #include "put_bits.h"
56 #include "bytestream.h"
57 
58 /* These values are hardcoded for now. */
59 #define BLOCK_WIDTH (4 * 16U)
60 #define BLOCK_HEIGHT (4 * 16U)
61 
62 typedef struct FlashSVContext {
64  const uint8_t *previous_frame;
67  unsigned packet_size;
69  uint8_t tmpblock[3 * 256 * 256];
72 
73 static int copy_region_enc(const uint8_t *sptr, uint8_t *dptr, int dx, int dy,
74  int h, int w, int stride, const uint8_t *pfptr)
75 {
76  int i, j;
77  int diff = 0;
78 
79  for (i = dx + h; i > dx; i--) {
80  const uint8_t *nsptr = sptr + i * stride + dy * 3;
81  const uint8_t *npfptr = pfptr + i * stride + dy * 3;
82  for (j = 0; j < w * 3; j++) {
83  diff |= npfptr[j] ^ nsptr[j];
84  dptr[j] = nsptr[j];
85  }
86  dptr += w * 3;
87  }
88  if (diff)
89  return 1;
90  return 0;
91 }
92 
94 {
95  FlashSVContext *s = avctx->priv_data;
96 
97  av_buffer_unref(&s->prev_frame_buf);
98 
99  return 0;
100 }
101 
103 {
104  FlashSVContext *s = avctx->priv_data;
105  int h_blocks, v_blocks, nb_blocks;
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  s->last_key_frame = 0;
116 
117  s->image_width = avctx->width;
118  s->image_height = avctx->height;
119 
120  h_blocks = (s->image_width + BLOCK_WIDTH - 1) / BLOCK_WIDTH;
121  v_blocks = (s->image_height + BLOCK_WIDTH - 1) / BLOCK_WIDTH;
122  nb_blocks = h_blocks * v_blocks;
123  s->packet_size = 4 + nb_blocks * (2 + 3 * BLOCK_WIDTH * BLOCK_HEIGHT);
124 
125  s->compression_level = avctx->compression_level == FF_COMPRESSION_DEFAULT
126  ? Z_DEFAULT_COMPRESSION
127  : av_clip(avctx->compression_level, 0, 9);
128 
129  return 0;
130 }
131 
132 
133 static int encode_bitstream(FlashSVContext *s, const AVFrame *p, uint8_t *buf,
134  int buf_size, int block_width, int block_height,
135  const uint8_t *previous_frame, int *I_frame)
136 {
137 
138  PutBitContext pb;
139  int h_blocks, v_blocks, h_part, v_part, i, j;
140  int buf_pos, res;
141  int pred_blocks = 0;
142 
143  init_put_bits(&pb, buf, buf_size);
144 
145  put_bits(&pb, 4, block_width / 16 - 1);
146  put_bits(&pb, 12, s->image_width);
147  put_bits(&pb, 4, block_height / 16 - 1);
148  put_bits(&pb, 12, s->image_height);
149  flush_put_bits(&pb);
150  buf_pos = 4;
151 
152  h_blocks = s->image_width / block_width;
153  h_part = s->image_width % block_width;
154  v_blocks = s->image_height / block_height;
155  v_part = s->image_height % block_height;
156 
157  /* loop over all block columns */
158  for (j = 0; j < v_blocks + (v_part ? 1 : 0); j++) {
159 
160  int y_pos = j * block_height; // vertical position in frame
161  int cur_blk_height = (j < v_blocks) ? block_height : v_part;
162 
163  /* loop over all block rows */
164  for (i = 0; i < h_blocks + (h_part ? 1 : 0); i++) {
165  int x_pos = i * block_width; // horizontal position in frame
166  int cur_blk_width = (i < h_blocks) ? block_width : h_part;
167  int ret = Z_OK;
168  uint8_t *ptr = buf + buf_pos;
169 
170  /* copy the block to the temp buffer before compression
171  * (if it differs from the previous frame's block) */
172  res = copy_region_enc(p->data[0], s->tmpblock,
173  s->image_height - (y_pos + cur_blk_height + 1),
174  x_pos, cur_blk_height, cur_blk_width,
175  p->linesize[0], previous_frame);
176 
177  if (res || *I_frame) {
178  unsigned long zsize = 3 * block_width * block_height + 12;
179  ret = compress2(ptr + 2, &zsize, s->tmpblock,
180  3 * cur_blk_width * cur_blk_height,
181  s->compression_level);
182 
183  if (ret != Z_OK)
184  av_log(s->avctx, AV_LOG_ERROR,
185  "error while compressing block %dx%d\n", i, j);
186 
187  bytestream_put_be16(&ptr, zsize);
188  buf_pos += zsize + 2;
189  ff_dlog(s->avctx, "buf_pos = %d\n", buf_pos);
190  } else {
191  pred_blocks++;
192  bytestream_put_be16(&ptr, 0);
193  buf_pos += 2;
194  }
195  }
196  }
197 
198  if (pred_blocks)
199  *I_frame = 0;
200  else
201  *I_frame = 1;
202 
203  return buf_pos;
204 }
205 
206 
208  const AVFrame *pict, int *got_packet)
209 {
210  FlashSVContext * const s = avctx->priv_data;
211  const uint8_t *prev_frame = s->previous_frame;
212  int res;
213  int I_frame = 0;
214  int opt_w = 4, opt_h = 4;
215 
216  /* First frame needs to be a keyframe */
217  if (!s->previous_frame) {
218  prev_frame = pict->data[0];
219  I_frame = 1;
220  }
221 
222  /* Check the placement of keyframes */
223  if (avctx->gop_size > 0 &&
224  avctx->frame_num >= s->last_key_frame + avctx->gop_size) {
225  I_frame = 1;
226  }
227 
228  res = ff_alloc_packet(avctx, pkt, s->packet_size);
229  if (res < 0)
230  return res;
231 
232  pkt->size = encode_bitstream(s, pict, pkt->data, pkt->size,
233  opt_w * 16, opt_h * 16,
234  prev_frame, &I_frame);
235 
236  //mark the frame type so the muxer can mux it correctly
237  if (I_frame) {
238  s->last_key_frame = avctx->frame_num;
239  ff_dlog(avctx, "Inserting keyframe at frame %"PRId64"\n", avctx->frame_num);
240  }
241 
242  if (I_frame)
244  *got_packet = 1;
245 
246  //save the current frame
247  res = av_buffer_replace(&s->prev_frame_buf, pict->buf[0]);
248  if (res < 0)
249  return res;
250  s->previous_frame = pict->data[0];
251 
252  return 0;
253 }
254 
256  .p.name = "flashsv",
257  CODEC_LONG_NAME("Flash Screen Video"),
258  .p.type = AVMEDIA_TYPE_VIDEO,
259  .p.id = AV_CODEC_ID_FLASHSV,
261  .priv_data_size = sizeof(FlashSVContext),
264  .close = flashsv_encode_end,
265  .p.pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_BGR24, AV_PIX_FMT_NONE },
266 };
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:71
FlashSVContext::previous_frame
const uint8_t * previous_frame
Definition: flashsvenc.c:64
av_clip
#define av_clip
Definition: common.h:100
FlashSVContext::image_width
int image_width
Definition: flashsv.c:58
ff_flashsv_encoder
const FFCodec ff_flashsv_encoder
Definition: flashsvenc.c:255
FlashSVContext::packet_size
unsigned packet_size
Definition: flashsvenc.c:67
int64_t
long long int64_t
Definition: coverity.c:34
init_put_bits
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:62
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:389
put_bits
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:223
w
uint8_t w
Definition: llviddspenc.c:38
AVPacket::data
uint8_t * data
Definition: packet.h:539
encode.h
FlashSVContext::compression_level
int compression_level
Definition: flashsvenc.c:70
copy_region_enc
static int copy_region_enc(const uint8_t *sptr, uint8_t *dptr, int dx, int dy, int h, int w, int stride, const uint8_t *pfptr)
Definition: flashsvenc.c:73
FFCodec
Definition: codec_internal.h:127
AV_PIX_FMT_BGR24
@ AV_PIX_FMT_BGR24
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:76
FF_COMPRESSION_DEFAULT
#define FF_COMPRESSION_DEFAULT
Definition: avcodec.h:1256
AVFrame::buf
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
Definition: frame.h:602
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:594
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:410
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
FF_CODEC_ENCODE_CB
#define FF_CODEC_ENCODE_CB(func)
Definition: codec_internal.h:320
pkt
AVPacket * pkt
Definition: movenc.c:60
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
av_cold
#define av_cold
Definition: attributes.h:90
FlashSVContext::tmpblock
uint8_t * tmpblock
Definition: flashsv.c:60
s
#define s(width, name)
Definition: cbs_vp9.c:198
flashsv_encode_frame
static int flashsv_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pict, int *got_packet)
Definition: flashsvenc.c:207
BLOCK_HEIGHT
#define BLOCK_HEIGHT
Definition: flashsvenc.c:60
AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
#define AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
This encoder can reorder user opaque values from input AVFrames and return them with corresponding ou...
Definition: codec.h:159
PutBitContext
Definition: put_bits.h:50
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:296
av_buffer_unref
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it.
Definition: buffer.c:139
FlashSVContext
Definition: flashsv.c:55
AV_CODEC_ID_FLASHSV
@ AV_CODEC_ID_FLASHSV
Definition: codec_id.h:138
ff_dlog
#define ff_dlog(a,...)
Definition: tableprint_vlc.h:28
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:368
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:540
AVCodecContext::gop_size
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
Definition: avcodec.h:1037
codec_internal.h
flashsv_encode_init
static av_cold int flashsv_encode_init(AVCodecContext *avctx)
Definition: flashsvenc.c:102
buffer.h
diff
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)
Definition: vf_paletteuse.c:166
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:545
FlashSVContext::last_key_frame
int64_t last_key_frame
Definition: flashsvenc.c:68
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
BLOCK_WIDTH
#define BLOCK_WIDTH
Definition: flashsvenc.c:59
FlashSVContext::image_height
int image_height
Definition: flashsv.c:58
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
av_buffer_replace
int av_buffer_replace(AVBufferRef **pdst, const AVBufferRef *src)
Ensure dst refers to the same data as src.
Definition: buffer.c:233
AVCodecContext::height
int height
Definition: avcodec.h:624
avcodec.h
encode_bitstream
static int encode_bitstream(FlashSVContext *s, const AVFrame *p, uint8_t *buf, int buf_size, int block_width, int block_height, const uint8_t *previous_frame, int *I_frame)
Definition: flashsvenc.c:133
stride
#define stride
Definition: h264pred_template.c:537
flashsv_encode_end
static av_cold int flashsv_encode_end(AVCodecContext *avctx)
Definition: flashsvenc.c:93
AVCodecContext::frame_num
int64_t frame_num
Frame counter, set by libavcodec.
Definition: avcodec.h:2041
ret
ret
Definition: filter_design.txt:187
AVCodecContext
main external API structure.
Definition: avcodec.h:451
FlashSVContext::prev_frame_buf
AVBufferRef * prev_frame_buf
Definition: flashsvenc.c:65
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:72
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AVBufferRef
A reference to a data buffer.
Definition: buffer.h:82
flush_put_bits
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:143
AVPacket
This structure stores compressed data.
Definition: packet.h:516
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:478
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:624
bytestream.h
AVFrame::linesize
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:434
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
h
h
Definition: vp9dsp_template.c:2070
FlashSVContext::avctx
AVCodecContext * avctx
Definition: flashsv.c:56
put_bits.h
ff_alloc_packet
int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and allocate data.
Definition: encode.c:62
AVCodecContext::compression_level
int compression_level
Definition: avcodec.h:1255