annotate flashsvenc.c @ 4535:e3b224087a85 libavcodec

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