Mercurial > libavcodec.hg
annotate flashsvenc.c @ 4644:414d484f6483 libavcodec
Simplify, use avctx->frame_number.
author | banan |
---|---|
date | Sat, 10 Mar 2007 09:20:07 +0000 |
parents | 548b2c0a2a51 |
children | ff9749708137 |
rev | line source |
---|---|
4374 | 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 flashsvenc.c | |
27 * Flash Screen Video encoder | |
28 * @author Alex Beregszaszi | |
29 * @author Benjamin Larsson | |
30 */ | |
31 | |
32 /* Bitstream description | |
33 * The picture is divided into blocks that are zlib-compressed. | |
34 * | |
35 * The decoder is fed complete frames, the frameheader contains: | |
36 * 4bits of block width | |
37 * 12bits of frame width | |
38 * 4bits of block height | |
39 * 12bits of frame height | |
40 * | |
41 * Directly after the header are the compressed blocks. The blocks | |
42 * have their compressed size represented with 16bits in the beginig. | |
43 * If the size = 0 then the block is unchanged from the previous frame. | |
44 * All blocks are decompressed until the buffer is consumed. | |
45 * | |
46 * Encoding ideas, a basic encoder would just use a fixed block size. | |
47 * Block sizes can be multipels of 16, from 16 to 256. The blocks don't | |
48 * have to be quadratic. A brute force search with a set of different | |
49 * block sizes should give a better result than to just use a fixed size. | |
50 */ | |
51 | |
52 /* TODO: | |
53 * Don't reencode the frame in brute force mode if the frame is a dupe. Speed up. | |
54 * Make the difference check faster. | |
55 */ | |
56 | |
57 #include <stdio.h> | |
58 #include <stdlib.h> | |
59 #include <zlib.h> | |
60 | |
61 #include "common.h" | |
62 #include "avcodec.h" | |
63 #include "bitstream.h" | |
64 #include "bytestream.h" | |
65 | |
66 | |
67 typedef struct FlashSVContext { | |
68 AVCodecContext *avctx; | |
69 uint8_t *previous_frame; | |
70 AVFrame frame; | |
71 int image_width, image_height; | |
72 int block_width, block_height; | |
73 uint8_t* tmpblock; | |
74 uint8_t* encbuffer; | |
75 int block_size; | |
76 z_stream zstream; | |
77 } FlashSVContext; | |
78 | |
79 static int copy_region_enc(uint8_t *sptr, uint8_t *dptr, | |
80 int dx, int dy, int h, int w, int stride, uint8_t *pfptr) { | |
81 int i,j; | |
82 uint8_t *nsptr; | |
83 uint8_t *npfptr; | |
84 int diff = 0; | |
85 | |
86 for (i = dx+h; i > dx; i--) { | |
87 nsptr = sptr+(i*stride)+dy*3; | |
88 npfptr = pfptr+(i*stride)+dy*3; | |
89 for (j=0 ; j<w*3 ; j++) { | |
90 diff |=npfptr[j]^nsptr[j]; | |
91 dptr[j] = nsptr[j]; | |
92 } | |
93 dptr += w*3; | |
94 } | |
95 if (diff) | |
96 return 1; | |
97 return 0; | |
98 } | |
99 | |
100 static int flashsv_encode_init(AVCodecContext *avctx) | |
101 { | |
102 FlashSVContext *s = (FlashSVContext *)avctx->priv_data; | |
103 | |
104 s->avctx = avctx; | |
105 | |
106 if ((avctx->width > 4095) || (avctx->height > 4095)) { | |
107 av_log(avctx, AV_LOG_ERROR, "Input dimensions too large, input must be max 4096x4096 !\n"); | |
108 return -1; | |
109 } | |
110 | |
111 if (avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 0) { | |
112 return -1; | |
113 } | |
114 | |
115 // Needed if zlib unused or init aborted before deflateInit | |
116 memset(&(s->zstream), 0, sizeof(z_stream)); | |
117 /* | |
118 s->zstream.zalloc = NULL; //av_malloc; | |
119 s->zstream.zfree = NULL; //av_free; | |
120 s->zstream.opaque = NULL; | |
121 zret = deflateInit(&(s->zstream), 9); | |
122 if (zret != Z_OK) { | |
123 av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret); | |
124 return -1; | |
125 } | |
126 */ | |
127 | |
128 s->image_width = avctx->width; | |
129 s->image_height = avctx->height; | |
130 | |
131 s->tmpblock = av_mallocz(3*256*256); | |
132 s->encbuffer = av_mallocz(s->image_width*s->image_height*3); | |
133 | |
134 if (!s->tmpblock || !s->encbuffer) { | |
135 av_log(avctx, AV_LOG_ERROR, "Memory allocation failed.\n"); | |
136 return -1; | |
137 } | |
138 | |
139 return 0; | |
140 } | |
141 | |
142 | |
143 static int encode_bitstream(FlashSVContext *s, AVFrame *p, uint8_t *buf, int buf_size, | |
144 int block_width, int block_height, uint8_t *previous_frame, int* I_frame) { | |
145 | |
146 PutBitContext pb; | |
147 int h_blocks, v_blocks, h_part, v_part, i, j; | |
148 int buf_pos, res; | |
149 int pred_blocks = 0; | |
150 | |
151 init_put_bits(&pb, buf, buf_size*8); | |
152 | |
153 put_bits(&pb, 4, (block_width/16)-1); | |
154 put_bits(&pb, 12, s->image_width); | |
155 put_bits(&pb, 4, (block_height/16)-1); | |
156 put_bits(&pb, 12, s->image_height); | |
157 flush_put_bits(&pb); | |
158 buf_pos=4; | |
159 | |
160 h_blocks = s->image_width / block_width; | |
161 h_part = s->image_width % block_width; | |
162 v_blocks = s->image_height / block_height; | |
163 v_part = s->image_height % block_height; | |
164 | |
165 /* loop over all block columns */ | |
166 for (j = 0; j < v_blocks + (v_part?1:0); j++) | |
167 { | |
168 | |
169 int hp = j*block_height; // horiz position in frame | |
170 int hs = (j<v_blocks)?block_height:v_part; // size of block | |
171 | |
172 /* loop over all block rows */ | |
173 for (i = 0; i < h_blocks + (h_part?1:0); i++) | |
174 { | |
175 int wp = i*block_width; // vert position in frame | |
176 int ws = (i<h_blocks)?block_width:h_part; // size of block | |
177 int ret=Z_OK; | |
178 uint8_t *ptr; | |
179 | |
180 ptr = buf+buf_pos; | |
181 | |
182 //copy the block to the temp buffer before compression (if it differs from the previous frame's block) | |
183 res = copy_region_enc(p->data[0], s->tmpblock, s->image_height-(hp+hs+1), wp, hs, ws, p->linesize[0], previous_frame); | |
184 | |
185 if (res || *I_frame) { | |
186 unsigned long zsize; | |
187 zsize = 3*block_width*block_height; | |
188 ret = compress2(ptr+2, &zsize, s->tmpblock, 3*ws*hs, 9); | |
189 | |
190 | |
191 //ret = deflateReset(&(s->zstream)); | |
192 if (ret != Z_OK) | |
193 av_log(s->avctx, AV_LOG_ERROR, "error while compressing block %dx%d\n", i, j); | |
194 /* | |
195 s->zstream.next_in = s->tmpblock; | |
196 s->zstream.avail_in = 3*ws*hs; | |
197 s->zstream.total_in = 0; | |
198 | |
199 s->zstream.next_out = ptr+2; | |
200 s->zstream.avail_out = buf_size-buf_pos-2; | |
201 s->zstream.total_out = 0; | |
202 | |
203 ret = deflate(&(s->zstream), Z_FINISH); | |
204 if ((ret != Z_OK) && (ret != Z_STREAM_END)) | |
205 av_log(s->avctx, AV_LOG_ERROR, "error while compressing block %dx%d\n", i, j); | |
206 | |
207 size = s->zstream.total_out; | |
208 //av_log(avctx, AV_LOG_INFO, "compressed blocks: %d\n", size); | |
209 */ | |
210 bytestream_put_be16(&ptr,(unsigned int)zsize); | |
4377
75942fdfd1a1
Add flashsv encoder to changelog and fix the encoder so it actually works.
banan
parents:
4374
diff
changeset
|
211 buf_pos += zsize+2; |
4374 | 212 //av_log(avctx, AV_LOG_ERROR, "buf_pos = %d\n", buf_pos); |
213 } else { | |
214 pred_blocks++; | |
215 bytestream_put_be16(&ptr,0); | |
4377
75942fdfd1a1
Add flashsv encoder to changelog and fix the encoder so it actually works.
banan
parents:
4374
diff
changeset
|
216 buf_pos += 2; |
4374 | 217 } |
218 } | |
219 } | |
220 | |
221 if (pred_blocks) | |
222 *I_frame = 0; | |
223 else | |
224 *I_frame = 1; | |
225 | |
226 return buf_pos; | |
227 } | |
228 | |
229 | |
230 static int flashsv_encode_frame(AVCodecContext *avctx, uint8_t *buf, int buf_size, void *data) | |
231 { | |
232 FlashSVContext * const s = (FlashSVContext *)avctx->priv_data; | |
233 AVFrame *pict = data; | |
234 AVFrame * const p = &s->frame; | |
235 int res; | |
236 int I_frame = 0; | |
237 int opt_w, opt_h; | |
238 | |
239 *p = *pict; | |
240 | |
4644 | 241 if (avctx->frame_number == 0) { |
4406
548b2c0a2a51
Don't copy more then needed, bugfix by Gus Scheidt ellocogato at gmail dot com and Michel Bardiaux mbardiaux at mediaxim dot be.
banan
parents:
4378
diff
changeset
|
242 s->previous_frame = av_mallocz(p->linesize[0]*s->image_height); |
4374 | 243 if (!s->previous_frame) { |
244 av_log(avctx, AV_LOG_ERROR, "Memory allocation failed.\n"); | |
245 return -1; | |
246 } | |
247 I_frame = 1; | |
248 } | |
249 | |
250 #if 0 | |
251 int w, h; | |
252 int optim_sizes[16][16]; | |
253 int smallest_size; | |
254 //Try all possible combinations and store the encoded frame sizes | |
255 for (w=1 ; w<17 ; w++) { | |
256 for (h=1 ; h<17 ; h++) { | |
257 optim_sizes[w-1][h-1] = encode_bitstream(s, p, s->encbuffer, s->image_width*s->image_height*4, w*16, h*16, s->previous_frame); | |
258 //av_log(avctx, AV_LOG_ERROR, "[%d][%d]size = %d\n",w,h,optim_sizes[w-1][h-1]); | |
259 } | |
260 } | |
261 | |
262 //Search for the smallest framesize and encode the frame with those parameters | |
263 smallest_size=optim_sizes[0][0]; | |
264 opt_w = 0; | |
265 opt_h = 0; | |
266 for (w=0 ; w<16 ; w++) { | |
267 for (h=0 ; h<16 ; h++) { | |
268 if (optim_sizes[w][h] < smallest_size) { | |
269 smallest_size = optim_sizes[w][h]; | |
270 opt_w = w; | |
271 opt_h = h; | |
272 } | |
273 } | |
274 } | |
275 res = encode_bitstream(s, p, buf, buf_size, (opt_w+1)*16, (opt_h+1)*16, s->previous_frame); | |
276 av_log(avctx, AV_LOG_ERROR, "[%d][%d]optimal size = %d, res = %d|\n", opt_w, opt_h, smallest_size, res); | |
277 | |
278 if (buf_size < res) | |
279 av_log(avctx, AV_LOG_ERROR, "buf_size %d < res %d\n", buf_size, res); | |
280 | |
281 #else | |
282 opt_w=1; | |
283 opt_h=1; | |
284 | |
285 if (buf_size < s->image_width*s->image_height*3) { | |
286 //Conservative upper bound check for compressed data | |
287 av_log(avctx, AV_LOG_ERROR, "buf_size %d < %d\n", buf_size, s->image_width*s->image_height*3); | |
288 return -1; | |
289 } | |
290 | |
291 res = encode_bitstream(s, p, buf, buf_size, opt_w*16, opt_h*16, s->previous_frame, &I_frame); | |
292 #endif | |
293 //save the current frame | |
4406
548b2c0a2a51
Don't copy more then needed, bugfix by Gus Scheidt ellocogato at gmail dot com and Michel Bardiaux mbardiaux at mediaxim dot be.
banan
parents:
4378
diff
changeset
|
294 memcpy(s->previous_frame, p->data[0], s->image_height*p->linesize[0]); |
4374 | 295 |
296 //mark the frame type so the muxer can mux it correctly | |
297 if (I_frame) { | |
298 p->pict_type = FF_I_TYPE; | |
299 p->key_frame = 1; | |
300 } else { | |
301 p->pict_type = FF_P_TYPE; | |
302 p->key_frame = 0; | |
303 } | |
304 | |
305 avctx->coded_frame = p; | |
306 | |
307 return res; | |
308 } | |
309 | |
310 static int flashsv_encode_end(AVCodecContext *avctx) | |
311 { | |
312 FlashSVContext *s = (FlashSVContext *)avctx->priv_data; | |
313 | |
314 deflateEnd(&(s->zstream)); | |
315 | |
316 av_free(s->encbuffer); | |
317 av_free(s->previous_frame); | |
318 av_free(s->tmpblock); | |
319 | |
320 return 0; | |
321 } | |
322 | |
323 AVCodec flashsv_encoder = { | |
324 "flashsv", | |
325 CODEC_TYPE_VIDEO, | |
326 CODEC_ID_FLASHSV, | |
327 sizeof(FlashSVContext), | |
328 flashsv_encode_init, | |
329 flashsv_encode_frame, | |
330 flashsv_encode_end, | |
331 .pix_fmts = (enum PixelFormat[]){PIX_FMT_BGR24, -1}, | |
332 }; | |
333 |