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
55
56
68
71 {
72 int i, j;
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 {
94
96
100
101 return 0;
102 }
103
105 {
107
109
112 "Input dimensions too large, input must be max 4095x4095 !\n");
114 }
115
116 // Needed if zlib unused or init aborted before deflateInit
117 memset(&s->
zstream, 0,
sizeof(z_stream));
118
120
123
126
130 }
131
132 return 0;
133 }
134
135
137 int buf_size, int block_width, int block_height,
138 uint8_t *previous_frame,
int *I_frame)
139 {
140
142 int h_blocks, v_blocks, h_part, v_part, i, j;
143 int buf_pos, res;
144 int pred_blocks = 0;
145
147
148 put_bits(&pb, 4, block_width / 16 - 1);
150 put_bits(&pb, 4, block_height / 16 - 1);
153 buf_pos = 4;
154
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;
172
173 /* copy the block to the temp buffer before compression
174 * (if it differs from the previous frame's block) */
177 x_pos, cur_blk_height, cur_blk_width,
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;
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 {
214 const AVFrame *
const p = pict;
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 */
226 }
227 I_frame = 1;
228 }
229
232 else
234
235 /* Check the placement of keyframes */
238 I_frame = 1;
239 }
240
242 return res;
243
245 pfptr, &I_frame);
246
247 //save the current frame
250 else
254
255 //mark the frame type so the muxer can mux it correctly
256 if (I_frame) {
257 #if FF_API_CODED_FRAME
262 #endif
265 } else {
266 #if FF_API_CODED_FRAME
271 #endif
272 }
273
274 if (I_frame)
276 *got_packet = 1;
277
278 return 0;
279 }
280
291 };
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)
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
This structure describes decoded (raw) audio or video data.
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
static av_cold int init(AVCodecContext *avctx)
static int copy_region_enc(uint8_t *sptr, uint8_t *dptr, int dx, int dy, int h, int w, int stride, uint8_t *pfptr)
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
static int flashsv_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pict, int *got_packet)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
const char * name
Name of the codec implementation.
AVCodec ff_flashsv_encoder
int flags
A combination of AV_PKT_FLAG values.
enum AVPictureType pict_type
Picture type of the frame.
static av_cold int flashsv_encode_end(AVCodecContext *avctx)
int width
picture width / height.
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
packed RGB 8:8:8, 24bpp, BGRBGR...
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
main external API structure.
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
static enum AVPixelFormat pix_fmts[]
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
GLint GLenum GLboolean GLsizei stride
#define FF_DISABLE_DEPRECATION_WARNINGS
common internal api header.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
attribute_deprecated AVFrame * coded_frame
the picture in the bitstream
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
static av_cold int flashsv_encode_init(AVCodecContext *avctx)
static av_always_inline int diff(const uint32_t a, const uint32_t b)
#define FF_ENABLE_DEPRECATION_WARNINGS
int key_frame
1 -> keyframe, 0-> not
int frame_number
Frame counter, set by libavcodec.
AVPixelFormat
Pixel format.
This structure stores compressed data.
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...