FFmpeg  4.4.9
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 "internal.h"
54 #include "put_bits.h"
55 #include "bytestream.h"
56 
57 /* These values are hardcoded for now. */
58 #define BLOCK_WIDTH (4 * 16U)
59 #define BLOCK_HEIGHT (4 * 16U)
60 
61 typedef struct FlashSVContext {
67  unsigned packet_size;
70  int block_size;
72  uint8_t tmpblock[3 * 256 * 256];
74 
75 static int copy_region_enc(uint8_t *sptr, uint8_t *dptr, int dx, int dy,
76  int h, int w, int stride, const uint8_t *pfptr,
77  int pstride)
78 {
79  int i, j;
80  uint8_t *nsptr;
81  const uint8_t *npfptr;
82  int diff = 0;
83 
84  for (i = dx + h; i > dx; i--) {
85  nsptr = sptr + i * stride + dy * 3;
86  npfptr = pfptr + i * pstride + dy * 3;
87  for (j = 0; j < w * 3; j++) {
88  diff |= npfptr[j] ^ nsptr[j];
89  dptr[j] = nsptr[j];
90  }
91  dptr += w * 3;
92  }
93  if (diff)
94  return 1;
95  return 0;
96 }
97 
99 {
100  FlashSVContext *s = avctx->priv_data;
101 
102  av_freep(&s->encbuffer);
103  av_buffer_unref(&s->prev_frame_buf);
104 
105  return 0;
106 }
107 
109 {
110  FlashSVContext *s = avctx->priv_data;
111  int h_blocks, v_blocks, nb_blocks;
112 
113  s->avctx = avctx;
114 
115  if (avctx->width > 4095 || avctx->height > 4095) {
116  av_log(avctx, AV_LOG_ERROR,
117  "Input dimensions too large, input must be max 4095x4095 !\n");
118  return AVERROR_INVALIDDATA;
119  }
120 
121  s->last_key_frame = 0;
122 
123  s->image_width = avctx->width;
124  s->image_height = avctx->height;
125 
126  h_blocks = (s->image_width + BLOCK_WIDTH - 1) / BLOCK_WIDTH;
127  v_blocks = (s->image_height + BLOCK_WIDTH - 1) / BLOCK_WIDTH;
128  nb_blocks = h_blocks * v_blocks;
129  s->packet_size = 4 + nb_blocks * (2 + 3 * BLOCK_WIDTH * BLOCK_HEIGHT);
130 
131  s->encbuffer = av_mallocz(s->image_width * s->image_height * 3);
132 
133  if (!s->encbuffer) {
134  av_log(avctx, AV_LOG_ERROR, "Memory allocation failed.\n");
135  return AVERROR(ENOMEM);
136  }
137 
138  return 0;
139 }
140 
141 
142 static int encode_bitstream(FlashSVContext *s, const AVFrame *p, uint8_t *buf,
143  int buf_size, int block_width, int block_height,
144  const uint8_t *previous_frame,
145  int previous_linesize, int *I_frame)
146 {
147 
148  PutBitContext pb;
149  int h_blocks, v_blocks, h_part, v_part, i, j;
150  int buf_pos, res;
151  int pred_blocks = 0;
152 
153  init_put_bits(&pb, buf, buf_size);
154 
155  put_bits(&pb, 4, block_width / 16 - 1);
156  put_bits(&pb, 12, s->image_width);
157  put_bits(&pb, 4, block_height / 16 - 1);
158  put_bits(&pb, 12, s->image_height);
159  flush_put_bits(&pb);
160  buf_pos = 4;
161 
162  h_blocks = s->image_width / block_width;
163  h_part = s->image_width % block_width;
164  v_blocks = s->image_height / block_height;
165  v_part = s->image_height % block_height;
166 
167  /* loop over all block columns */
168  for (j = 0; j < v_blocks + (v_part ? 1 : 0); j++) {
169 
170  int y_pos = j * block_height; // vertical position in frame
171  int cur_blk_height = (j < v_blocks) ? block_height : v_part;
172 
173  /* loop over all block rows */
174  for (i = 0; i < h_blocks + (h_part ? 1 : 0); i++) {
175  int x_pos = i * block_width; // horizontal position in frame
176  int cur_blk_width = (i < h_blocks) ? block_width : h_part;
177  int ret = Z_OK;
178  uint8_t *ptr = buf + buf_pos;
179 
180  /* copy the block to the temp buffer before compression
181  * (if it differs from the previous frame's block) */
182  res = copy_region_enc(p->data[0], s->tmpblock,
183  s->image_height - (y_pos + cur_blk_height + 1),
184  x_pos, cur_blk_height, cur_blk_width,
185  p->linesize[0], previous_frame,
186  previous_linesize);
187 
188  if (res || *I_frame) {
189  unsigned long zsize = 3 * block_width * block_height;
190  ret = compress2(ptr + 2, &zsize, s->tmpblock,
191  3 * cur_blk_width * cur_blk_height, 9);
192 
193  if (ret != Z_OK)
194  av_log(s->avctx, AV_LOG_ERROR,
195  "error while compressing block %dx%d\n", i, j);
196 
197  bytestream_put_be16(&ptr, zsize);
198  buf_pos += zsize + 2;
199  ff_dlog(s->avctx, "buf_pos = %d\n", buf_pos);
200  } else {
201  pred_blocks++;
202  bytestream_put_be16(&ptr, 0);
203  buf_pos += 2;
204  }
205  }
206  }
207 
208  if (pred_blocks)
209  *I_frame = 0;
210  else
211  *I_frame = 1;
212 
213  return buf_pos;
214 }
215 
216 
218  const AVFrame *pict, int *got_packet)
219 {
220  FlashSVContext * const s = avctx->priv_data;
221  const AVFrame * const p = pict;
222  const uint8_t *prev_frame = s->previous_frame;
223  int prev_linesize = s->previous_linesize;
224  int res;
225  int I_frame = 0;
226  int opt_w = 4, opt_h = 4;
227 
228  /* First frame needs to be a keyframe */
229  if (!s->previous_frame) {
230  prev_frame = pict->data[0];
231  prev_linesize = pict->linesize[0];
232  I_frame = 1;
233  }
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  res = ff_alloc_packet2(avctx, pkt, s->packet_size, 0);
242  if (res < 0)
243  return res;
244 
245  pkt->size = encode_bitstream(s, p, pkt->data, pkt->size, opt_w * 16, opt_h * 16,
246  prev_frame, prev_linesize, &I_frame);
247 
248  //mark the frame type so the muxer can mux it correctly
249  if (I_frame) {
250 #if FF_API_CODED_FRAME
253  avctx->coded_frame->key_frame = 1;
255 #endif
256  s->last_key_frame = avctx->frame_number;
257  ff_dlog(avctx, "Inserting keyframe at frame %d\n", avctx->frame_number);
258  } else {
259 #if FF_API_CODED_FRAME
262  avctx->coded_frame->key_frame = 0;
264 #endif
265  }
266 
267  if (I_frame)
269  *got_packet = 1;
270 
271  //save the current frame
272  res = av_buffer_replace(&s->prev_frame_buf, pict->buf[0]);
273  if (res < 0)
274  return res;
275  s->previous_frame = pict->data[0];
276  s->previous_linesize = pict->linesize[0];
277 
278  return 0;
279 }
280 
282  .name = "flashsv",
283  .long_name = NULL_IF_CONFIG_SMALL("Flash Screen Video"),
284  .type = AVMEDIA_TYPE_VIDEO,
285  .id = AV_CODEC_ID_FLASHSV,
286  .priv_data_size = sizeof(FlashSVContext),
288  .encode2 = flashsv_encode_frame,
289  .close = flashsv_encode_end,
291 };
#define av_cold
Definition: attributes.h:88
uint8_t
Libavcodec external API header.
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:31
refcounted data buffer API
#define s(width, name)
Definition: cbs_vp9.c:257
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:34
static av_cold int flashsv_encode_end(AVCodecContext *avctx)
Definition: flashsvenc.c:98
static av_cold int flashsv_encode_init(AVCodecContext *avctx)
Definition: flashsvenc.c:108
AVCodec ff_flashsv_encoder
Definition: flashsvenc.c:281
#define BLOCK_HEIGHT
Definition: flashsvenc.c:59
static int copy_region_enc(uint8_t *sptr, uint8_t *dptr, int dx, int dy, int h, int w, int stride, const uint8_t *pfptr, int pstride)
Definition: flashsvenc.c:75
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 previous_linesize, int *I_frame)
Definition: flashsvenc.c:142
#define BLOCK_WIDTH
Definition: flashsvenc.c:58
static int flashsv_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pict, int *got_packet)
Definition: flashsvenc.c:217
@ AV_CODEC_ID_FLASHSV
Definition: codec_id.h:135
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:410
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:125
int av_buffer_replace(AVBufferRef **pdst, AVBufferRef *src)
Ensure dst refers to the same data as src.
Definition: buffer.c:219
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
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
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:274
@ AV_PICTURE_TYPE_P
Predicted.
Definition: avutil.h:275
int i
Definition: input.c:407
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:218
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:83
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:84
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:309
uint8_t w
Definition: llviddspenc.c:39
int stride
Definition: mace.c:144
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
@ AV_PIX_FMT_BGR24
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:69
bitstream writer API
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:57
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:110
A reference to a data buffer.
Definition: buffer.h:84
main external API structure.
Definition: avcodec.h:536
int width
picture width / height.
Definition: avcodec.h:709
attribute_deprecated AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:1768
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
Definition: avcodec.h:731
int frame_number
Frame counter, set by libavcodec.
Definition: avcodec.h:1227
void * priv_data
Definition: avcodec.h:563
AVCodec.
Definition: codec.h:197
const char * name
Name of the codec implementation.
Definition: codec.h:204
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:332
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:396
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
Definition: frame.h:509
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:349
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:401
This structure stores compressed data.
Definition: packet.h:346
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:375
int size
Definition: packet.h:370
uint8_t * data
Definition: packet.h:369
const uint8_t * previous_frame
Definition: flashsvenc.c:63
uint8_t * encbuffer
Definition: flashsvenc.c:69
int last_key_frame
Definition: flashsvenc.c:71
int block_height
Definition: flashsv.c:55
uint8_t * tmpblock
Definition: flashsv.c:56
int previous_linesize
Definition: flashsvenc.c:64
int image_height
Definition: flashsv.c:54
int block_width
Definition: flashsv.c:55
int image_width
Definition: flashsv.c:54
unsigned packet_size
Definition: flashsvenc.c:67
AVCodecContext * avctx
Definition: flashsv.c:52
int block_size
Definition: flashsv.c:57
AVBufferRef * prev_frame_buf
Definition: flashsvenc.c:65
#define ff_dlog(a,...)
#define av_freep(p)
#define av_log(a,...)
AVPacket * pkt
Definition: movenc.c:59
static av_always_inline int diff(const uint32_t a, const uint32_t b)