Mercurial > libavcodec.hg
annotate flashsvenc.c @ 12183:4fccdee316ea libavcodec
Add ff_draw_pc_font()
author | pross |
---|---|
date | Sun, 18 Jul 2010 07:44:38 +0000 |
parents | 7dd2a45249a9 |
children |
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 /** | |
11644
7dd2a45249a9
Remove explicit filename from Doxygen @file commands.
diego
parents:
11560
diff
changeset
|
26 * @file |
4374 | 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 "avcodec.h" | |
9411
4cb7c65fc775
Split bitstream.h, put the bitstream writer stuff in the new file
stefano
parents:
8718
diff
changeset
|
62 #include "put_bits.h" |
4374 | 63 #include "bytestream.h" |
64 | |
65 | |
66 typedef struct FlashSVContext { | |
67 AVCodecContext *avctx; | |
68 uint8_t *previous_frame; | |
69 AVFrame frame; | |
70 int image_width, image_height; | |
71 int block_width, block_height; | |
72 uint8_t* tmpblock; | |
73 uint8_t* encbuffer; | |
74 int block_size; | |
75 z_stream zstream; | |
4651
ff9749708137
Respect the gop size (-g) for marking I frames. Use -g 0 gives the old behaviour.
banan
parents:
4644
diff
changeset
|
76 int last_key_frame; |
4374 | 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 | |
6517
48759bfbd073
Apply 'cold' attribute to init/uninit functions in libavcodec
zuxy
parents:
5017
diff
changeset
|
100 static av_cold int flashsv_encode_init(AVCodecContext *avctx) |
4374 | 101 { |
4827 | 102 FlashSVContext *s = avctx->priv_data; |
4374 | 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 // Needed if zlib unused or init aborted before deflateInit | |
112 memset(&(s->zstream), 0, sizeof(z_stream)); | |
113 | |
4651
ff9749708137
Respect the gop size (-g) for marking I frames. Use -g 0 gives the old behaviour.
banan
parents:
4644
diff
changeset
|
114 s->last_key_frame=0; |
ff9749708137
Respect the gop size (-g) for marking I frames. Use -g 0 gives the old behaviour.
banan
parents:
4644
diff
changeset
|
115 |
4374 | 116 s->image_width = avctx->width; |
117 s->image_height = avctx->height; | |
118 | |
119 s->tmpblock = av_mallocz(3*256*256); | |
120 s->encbuffer = av_mallocz(s->image_width*s->image_height*3); | |
121 | |
122 if (!s->tmpblock || !s->encbuffer) { | |
123 av_log(avctx, AV_LOG_ERROR, "Memory allocation failed.\n"); | |
124 return -1; | |
125 } | |
126 | |
127 return 0; | |
128 } | |
129 | |
130 | |
131 static int encode_bitstream(FlashSVContext *s, AVFrame *p, uint8_t *buf, int buf_size, | |
132 int block_width, int block_height, uint8_t *previous_frame, int* I_frame) { | |
133 | |
134 PutBitContext pb; | |
135 int h_blocks, v_blocks, h_part, v_part, i, j; | |
136 int buf_pos, res; | |
137 int pred_blocks = 0; | |
138 | |
139 init_put_bits(&pb, buf, buf_size*8); | |
140 | |
141 put_bits(&pb, 4, (block_width/16)-1); | |
142 put_bits(&pb, 12, s->image_width); | |
143 put_bits(&pb, 4, (block_height/16)-1); | |
144 put_bits(&pb, 12, s->image_height); | |
145 flush_put_bits(&pb); | |
146 buf_pos=4; | |
147 | |
148 h_blocks = s->image_width / block_width; | |
149 h_part = s->image_width % block_width; | |
150 v_blocks = s->image_height / block_height; | |
151 v_part = s->image_height % block_height; | |
152 | |
153 /* loop over all block columns */ | |
154 for (j = 0; j < v_blocks + (v_part?1:0); j++) | |
155 { | |
156 | |
157 int hp = j*block_height; // horiz position in frame | |
158 int hs = (j<v_blocks)?block_height:v_part; // size of block | |
159 | |
160 /* loop over all block rows */ | |
161 for (i = 0; i < h_blocks + (h_part?1:0); i++) | |
162 { | |
163 int wp = i*block_width; // vert position in frame | |
164 int ws = (i<h_blocks)?block_width:h_part; // size of block | |
165 int ret=Z_OK; | |
166 uint8_t *ptr; | |
167 | |
168 ptr = buf+buf_pos; | |
169 | |
170 //copy the block to the temp buffer before compression (if it differs from the previous frame's block) | |
171 res = copy_region_enc(p->data[0], s->tmpblock, s->image_height-(hp+hs+1), wp, hs, ws, p->linesize[0], previous_frame); | |
172 | |
173 if (res || *I_frame) { | |
174 unsigned long zsize; | |
175 zsize = 3*block_width*block_height; | |
176 ret = compress2(ptr+2, &zsize, s->tmpblock, 3*ws*hs, 9); | |
177 | |
178 | |
179 //ret = deflateReset(&(s->zstream)); | |
180 if (ret != Z_OK) | |
181 av_log(s->avctx, AV_LOG_ERROR, "error while compressing block %dx%d\n", i, j); | |
182 | |
183 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
|
184 buf_pos += zsize+2; |
4374 | 185 //av_log(avctx, AV_LOG_ERROR, "buf_pos = %d\n", buf_pos); |
186 } else { | |
187 pred_blocks++; | |
188 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
|
189 buf_pos += 2; |
4374 | 190 } |
191 } | |
192 } | |
193 | |
194 if (pred_blocks) | |
195 *I_frame = 0; | |
196 else | |
197 *I_frame = 1; | |
198 | |
199 return buf_pos; | |
200 } | |
201 | |
202 | |
203 static int flashsv_encode_frame(AVCodecContext *avctx, uint8_t *buf, int buf_size, void *data) | |
204 { | |
4827 | 205 FlashSVContext * const s = avctx->priv_data; |
4374 | 206 AVFrame *pict = data; |
207 AVFrame * const p = &s->frame; | |
4992
52d8e61c0280
Added support for instances where linesize[0] is negative.
banan
parents:
4962
diff
changeset
|
208 uint8_t *pfptr; |
4374 | 209 int res; |
210 int I_frame = 0; | |
211 int opt_w, opt_h; | |
212 | |
213 *p = *pict; | |
214 | |
4651
ff9749708137
Respect the gop size (-g) for marking I frames. Use -g 0 gives the old behaviour.
banan
parents:
4644
diff
changeset
|
215 /* First frame needs to be a keyframe */ |
4644 | 216 if (avctx->frame_number == 0) { |
5017 | 217 s->previous_frame = av_mallocz(FFABS(p->linesize[0])*s->image_height); |
4374 | 218 if (!s->previous_frame) { |
219 av_log(avctx, AV_LOG_ERROR, "Memory allocation failed.\n"); | |
220 return -1; | |
221 } | |
222 I_frame = 1; | |
223 } | |
224 | |
4992
52d8e61c0280
Added support for instances where linesize[0] is negative.
banan
parents:
4962
diff
changeset
|
225 if (p->linesize[0] < 0) |
52d8e61c0280
Added support for instances where linesize[0] is negative.
banan
parents:
4962
diff
changeset
|
226 pfptr = s->previous_frame - ((s->image_height-1) * p->linesize[0]); |
52d8e61c0280
Added support for instances where linesize[0] is negative.
banan
parents:
4962
diff
changeset
|
227 else |
52d8e61c0280
Added support for instances where linesize[0] is negative.
banan
parents:
4962
diff
changeset
|
228 pfptr = s->previous_frame; |
52d8e61c0280
Added support for instances where linesize[0] is negative.
banan
parents:
4962
diff
changeset
|
229 |
4651
ff9749708137
Respect the gop size (-g) for marking I frames. Use -g 0 gives the old behaviour.
banan
parents:
4644
diff
changeset
|
230 /* Check the placement of keyframes */ |
ff9749708137
Respect the gop size (-g) for marking I frames. Use -g 0 gives the old behaviour.
banan
parents:
4644
diff
changeset
|
231 if (avctx->gop_size > 0) { |
ff9749708137
Respect the gop size (-g) for marking I frames. Use -g 0 gives the old behaviour.
banan
parents:
4644
diff
changeset
|
232 if (avctx->frame_number >= s->last_key_frame + avctx->gop_size) { |
ff9749708137
Respect the gop size (-g) for marking I frames. Use -g 0 gives the old behaviour.
banan
parents:
4644
diff
changeset
|
233 I_frame = 1; |
ff9749708137
Respect the gop size (-g) for marking I frames. Use -g 0 gives the old behaviour.
banan
parents:
4644
diff
changeset
|
234 } |
ff9749708137
Respect the gop size (-g) for marking I frames. Use -g 0 gives the old behaviour.
banan
parents:
4644
diff
changeset
|
235 } |
ff9749708137
Respect the gop size (-g) for marking I frames. Use -g 0 gives the old behaviour.
banan
parents:
4644
diff
changeset
|
236 |
5015 | 237 opt_w=4; |
238 opt_h=4; | |
4374 | 239 |
240 if (buf_size < s->image_width*s->image_height*3) { | |
241 //Conservative upper bound check for compressed data | |
242 av_log(avctx, AV_LOG_ERROR, "buf_size %d < %d\n", buf_size, s->image_width*s->image_height*3); | |
243 return -1; | |
244 } | |
245 | |
4992
52d8e61c0280
Added support for instances where linesize[0] is negative.
banan
parents:
4962
diff
changeset
|
246 res = encode_bitstream(s, p, buf, buf_size, opt_w*16, opt_h*16, pfptr, &I_frame); |
5016 | 247 |
4374 | 248 //save the current frame |
4992
52d8e61c0280
Added support for instances where linesize[0] is negative.
banan
parents:
4962
diff
changeset
|
249 if(p->linesize[0] > 0) |
52d8e61c0280
Added support for instances where linesize[0] is negative.
banan
parents:
4962
diff
changeset
|
250 memcpy(s->previous_frame, p->data[0], s->image_height*p->linesize[0]); |
52d8e61c0280
Added support for instances where linesize[0] is negative.
banan
parents:
4962
diff
changeset
|
251 else |
5017 | 252 memcpy(s->previous_frame, p->data[0] + p->linesize[0] * (s->image_height-1), s->image_height*FFABS(p->linesize[0])); |
4374 | 253 |
254 //mark the frame type so the muxer can mux it correctly | |
255 if (I_frame) { | |
256 p->pict_type = FF_I_TYPE; | |
257 p->key_frame = 1; | |
4651
ff9749708137
Respect the gop size (-g) for marking I frames. Use -g 0 gives the old behaviour.
banan
parents:
4644
diff
changeset
|
258 s->last_key_frame = avctx->frame_number; |
ff9749708137
Respect the gop size (-g) for marking I frames. Use -g 0 gives the old behaviour.
banan
parents:
4644
diff
changeset
|
259 av_log(avctx, AV_LOG_DEBUG, "Inserting key frame at frame %d\n",avctx->frame_number); |
4374 | 260 } else { |
261 p->pict_type = FF_P_TYPE; | |
262 p->key_frame = 0; | |
263 } | |
264 | |
265 avctx->coded_frame = p; | |
266 | |
267 return res; | |
268 } | |
269 | |
6517
48759bfbd073
Apply 'cold' attribute to init/uninit functions in libavcodec
zuxy
parents:
5017
diff
changeset
|
270 static av_cold int flashsv_encode_end(AVCodecContext *avctx) |
4374 | 271 { |
4827 | 272 FlashSVContext *s = avctx->priv_data; |
4374 | 273 |
274 deflateEnd(&(s->zstream)); | |
275 | |
276 av_free(s->encbuffer); | |
277 av_free(s->previous_frame); | |
278 av_free(s->tmpblock); | |
279 | |
280 return 0; | |
281 } | |
282 | |
283 AVCodec flashsv_encoder = { | |
284 "flashsv", | |
11560
8a4984c5cacc
Define AVMediaType enum, and use it instead of enum CodecType, which
stefano
parents:
10397
diff
changeset
|
285 AVMEDIA_TYPE_VIDEO, |
4374 | 286 CODEC_ID_FLASHSV, |
287 sizeof(FlashSVContext), | |
288 flashsv_encode_init, | |
289 flashsv_encode_frame, | |
290 flashsv_encode_end, | |
10146
38cfe222e1a4
Mark all pix_fmts and supported_framerates compound literals as const.
reimar
parents:
9411
diff
changeset
|
291 .pix_fmts = (const enum PixelFormat[]){PIX_FMT_BGR24, PIX_FMT_NONE}, |
7040
e943e1409077
Make AVCodec long_names definition conditional depending on CONFIG_SMALL.
stefano
parents:
6788
diff
changeset
|
292 .long_name = NULL_IF_CONFIG_SMALL("Flash Screen Video"), |
4374 | 293 }; |
294 |