annotate flashsvenc.c @ 10159:6110cd97dc8e libavcodec

Use reget_buffer instead of get_buffer. Fixes issue1367. Patch by jarkko.palviainen.sesca.com.
author banan
date Thu, 10 Sep 2009 19:01:53 +0000
parents 38cfe222e1a4
children d7ed9dcc78e3
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 /**
8718
e9d9d946f213 Use full internal pathname in doxygen @file directives.
diego
parents: 7040
diff changeset
26 * @file libavcodec/flashsvenc.c
4374
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 "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
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
63 #include "bytestream.h"
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
64
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
65
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
66 typedef struct FlashSVContext {
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
67 AVCodecContext *avctx;
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
68 uint8_t *previous_frame;
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
69 AVFrame frame;
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
70 int image_width, image_height;
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
71 int block_width, block_height;
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
72 uint8_t* tmpblock;
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
73 uint8_t* encbuffer;
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
74 int block_size;
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
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
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
77 } FlashSVContext;
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
78
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
79 static int copy_region_enc(uint8_t *sptr, uint8_t *dptr,
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
80 int dx, int dy, int h, int w, int stride, uint8_t *pfptr) {
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
81 int i,j;
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
82 uint8_t *nsptr;
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
83 uint8_t *npfptr;
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
84 int diff = 0;
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
85
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
86 for (i = dx+h; i > dx; i--) {
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
87 nsptr = sptr+(i*stride)+dy*3;
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
88 npfptr = pfptr+(i*stride)+dy*3;
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
89 for (j=0 ; j<w*3 ; j++) {
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
90 diff |=npfptr[j]^nsptr[j];
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
91 dptr[j] = nsptr[j];
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
92 }
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
93 dptr += w*3;
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
94 }
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
95 if (diff)
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
96 return 1;
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
97 return 0;
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
98 }
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
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
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
101 {
4827
b3ee9a1526b0 Get rid of unnecessary pointer casts.
diego
parents: 4651
diff changeset
102 FlashSVContext *s = avctx->priv_data;
4374
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
103
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
104 s->avctx = avctx;
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
105
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
106 if ((avctx->width > 4095) || (avctx->height > 4095)) {
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
107 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
108 return -1;
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
109 }
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
110
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
111 if (avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 0) {
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
112 return -1;
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
113 }
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
114
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
115 // Needed if zlib unused or init aborted before deflateInit
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
116 memset(&(s->zstream), 0, sizeof(z_stream));
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
117
4651
ff9749708137 Respect the gop size (-g) for marking I frames. Use -g 0 gives the old behaviour.
banan
parents: 4644
diff changeset
118 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
119
4374
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
120 s->image_width = avctx->width;
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
121 s->image_height = avctx->height;
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
122
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
123 s->tmpblock = av_mallocz(3*256*256);
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
124 s->encbuffer = av_mallocz(s->image_width*s->image_height*3);
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
125
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
126 if (!s->tmpblock || !s->encbuffer) {
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
127 av_log(avctx, AV_LOG_ERROR, "Memory allocation failed.\n");
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
128 return -1;
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 return 0;
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
132 }
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
133
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
134
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
135 static int encode_bitstream(FlashSVContext *s, AVFrame *p, uint8_t *buf, int buf_size,
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
136 int block_width, int block_height, uint8_t *previous_frame, int* I_frame) {
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
137
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
138 PutBitContext pb;
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
139 int h_blocks, v_blocks, h_part, v_part, i, j;
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
140 int buf_pos, res;
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
141 int pred_blocks = 0;
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
142
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
143 init_put_bits(&pb, buf, buf_size*8);
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
144
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
145 put_bits(&pb, 4, (block_width/16)-1);
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
146 put_bits(&pb, 12, s->image_width);
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
147 put_bits(&pb, 4, (block_height/16)-1);
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
148 put_bits(&pb, 12, s->image_height);
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
149 flush_put_bits(&pb);
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
150 buf_pos=4;
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
151
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
152 h_blocks = s->image_width / block_width;
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
153 h_part = s->image_width % block_width;
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
154 v_blocks = s->image_height / block_height;
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
155 v_part = s->image_height % block_height;
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
156
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
157 /* loop over all block columns */
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
158 for (j = 0; j < v_blocks + (v_part?1:0); j++)
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
159 {
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
160
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
161 int hp = j*block_height; // horiz position in frame
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
162 int hs = (j<v_blocks)?block_height:v_part; // size of block
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
163
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
164 /* loop over all block rows */
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
165 for (i = 0; i < h_blocks + (h_part?1:0); i++)
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
166 {
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
167 int wp = i*block_width; // vert position in frame
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
168 int ws = (i<h_blocks)?block_width:h_part; // size of block
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
169 int ret=Z_OK;
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
170 uint8_t *ptr;
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
171
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
172 ptr = buf+buf_pos;
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
173
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
174 //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
175 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
176
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
177 if (res || *I_frame) {
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
178 unsigned long zsize;
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
179 zsize = 3*block_width*block_height;
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
180 ret = compress2(ptr+2, &zsize, s->tmpblock, 3*ws*hs, 9);
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
181
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
182
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
183 //ret = deflateReset(&(s->zstream));
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
184 if (ret != Z_OK)
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
185 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
186
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
187 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
188 buf_pos += zsize+2;
4374
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
189 //av_log(avctx, AV_LOG_ERROR, "buf_pos = %d\n", buf_pos);
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
190 } else {
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
191 pred_blocks++;
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
192 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
193 buf_pos += 2;
4374
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
194 }
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
195 }
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
196 }
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
197
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
198 if (pred_blocks)
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
199 *I_frame = 0;
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
200 else
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
201 *I_frame = 1;
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
202
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
203 return buf_pos;
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
204 }
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
205
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
206
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
207 static int flashsv_encode_frame(AVCodecContext *avctx, uint8_t *buf, int buf_size, void *data)
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
208 {
4827
b3ee9a1526b0 Get rid of unnecessary pointer casts.
diego
parents: 4651
diff changeset
209 FlashSVContext * const s = avctx->priv_data;
4374
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
210 AVFrame *pict = data;
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
211 AVFrame * const p = &s->frame;
4992
52d8e61c0280 Added support for instances where linesize[0] is negative.
banan
parents: 4962
diff changeset
212 uint8_t *pfptr;
4374
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
213 int res;
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
214 int I_frame = 0;
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
215 int opt_w, opt_h;
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
216
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
217 *p = *pict;
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
218
4651
ff9749708137 Respect the gop size (-g) for marking I frames. Use -g 0 gives the old behaviour.
banan
parents: 4644
diff changeset
219 /* First frame needs to be a keyframe */
4644
414d484f6483 Simplify, use avctx->frame_number.
banan
parents: 4406
diff changeset
220 if (avctx->frame_number == 0) {
5017
1eebbc25db1f Use FFABS instead of abs.
banan
parents: 5016
diff changeset
221 s->previous_frame = av_mallocz(FFABS(p->linesize[0])*s->image_height);
4374
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
222 if (!s->previous_frame) {
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
223 av_log(avctx, AV_LOG_ERROR, "Memory allocation failed.\n");
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
224 return -1;
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
225 }
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
226 I_frame = 1;
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
227 }
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
228
4992
52d8e61c0280 Added support for instances where linesize[0] is negative.
banan
parents: 4962
diff changeset
229 if (p->linesize[0] < 0)
52d8e61c0280 Added support for instances where linesize[0] is negative.
banan
parents: 4962
diff changeset
230 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
231 else
52d8e61c0280 Added support for instances where linesize[0] is negative.
banan
parents: 4962
diff changeset
232 pfptr = s->previous_frame;
52d8e61c0280 Added support for instances where linesize[0] is negative.
banan
parents: 4962
diff changeset
233
4651
ff9749708137 Respect the gop size (-g) for marking I frames. Use -g 0 gives the old behaviour.
banan
parents: 4644
diff changeset
234 /* 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
235 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
236 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
237 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
238 }
ff9749708137 Respect the gop size (-g) for marking I frames. Use -g 0 gives the old behaviour.
banan
parents: 4644
diff changeset
239 }
ff9749708137 Respect the gop size (-g) for marking I frames. Use -g 0 gives the old behaviour.
banan
parents: 4644
diff changeset
240
5015
c9b17d87df0a Change default 1 pass blocksize to 64x64.
banan
parents: 4992
diff changeset
241 opt_w=4;
c9b17d87df0a Change default 1 pass blocksize to 64x64.
banan
parents: 4992
diff changeset
242 opt_h=4;
4374
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
243
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
244 if (buf_size < s->image_width*s->image_height*3) {
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
245 //Conservative upper bound check for compressed data
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
246 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
247 return -1;
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
248 }
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
249
4992
52d8e61c0280 Added support for instances where linesize[0] is negative.
banan
parents: 4962
diff changeset
250 res = encode_bitstream(s, p, buf, buf_size, opt_w*16, opt_h*16, pfptr, &I_frame);
5016
3674582fb4e7 Remove lots of unused commented code. (Cleanup)
banan
parents: 5015
diff changeset
251
4374
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
252 //save the current frame
4992
52d8e61c0280 Added support for instances where linesize[0] is negative.
banan
parents: 4962
diff changeset
253 if(p->linesize[0] > 0)
52d8e61c0280 Added support for instances where linesize[0] is negative.
banan
parents: 4962
diff changeset
254 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
255 else
5017
1eebbc25db1f Use FFABS instead of abs.
banan
parents: 5016
diff changeset
256 memcpy(s->previous_frame, p->data[0] + p->linesize[0] * (s->image_height-1), s->image_height*FFABS(p->linesize[0]));
4374
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
257
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
258 //mark the frame type so the muxer can mux it correctly
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
259 if (I_frame) {
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
260 p->pict_type = FF_I_TYPE;
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
261 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
262 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
263 av_log(avctx, AV_LOG_DEBUG, "Inserting key frame at frame %d\n",avctx->frame_number);
4374
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
264 } else {
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
265 p->pict_type = FF_P_TYPE;
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
266 p->key_frame = 0;
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
267 }
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
268
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
269 avctx->coded_frame = p;
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
270
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
271 return res;
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
272 }
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
273
6517
48759bfbd073 Apply 'cold' attribute to init/uninit functions in libavcodec
zuxy
parents: 5017
diff changeset
274 static av_cold int flashsv_encode_end(AVCodecContext *avctx)
4374
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
275 {
4827
b3ee9a1526b0 Get rid of unnecessary pointer casts.
diego
parents: 4651
diff changeset
276 FlashSVContext *s = avctx->priv_data;
4374
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
277
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
278 deflateEnd(&(s->zstream));
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
279
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
280 av_free(s->encbuffer);
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
281 av_free(s->previous_frame);
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
282 av_free(s->tmpblock);
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
283
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
284 return 0;
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
285 }
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
286
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
287 AVCodec flashsv_encoder = {
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
288 "flashsv",
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
289 CODEC_TYPE_VIDEO,
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
290 CODEC_ID_FLASHSV,
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
291 sizeof(FlashSVContext),
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
292 flashsv_encode_init,
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
293 flashsv_encode_frame,
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
294 flashsv_encode_end,
10146
38cfe222e1a4 Mark all pix_fmts and supported_framerates compound literals as const.
reimar
parents: 9411
diff changeset
295 .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
296 .long_name = NULL_IF_CONFIG_SMALL("Flash Screen Video"),
4374
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
297 };
65bb4ce33467 Flash screen video encoder.
banan
parents:
diff changeset
298