yading@10
|
1 /*
|
yading@10
|
2 * Quicktime Animation (RLE) Video Encoder
|
yading@10
|
3 * Copyright (C) 2007 Clemens Fruhwirth
|
yading@10
|
4 * Copyright (C) 2007 Alexis Ballier
|
yading@10
|
5 *
|
yading@10
|
6 * This file is based on flashsvenc.c.
|
yading@10
|
7 *
|
yading@10
|
8 * This file is part of FFmpeg.
|
yading@10
|
9 *
|
yading@10
|
10 * FFmpeg is free software; you can redistribute it and/or
|
yading@10
|
11 * modify it under the terms of the GNU Lesser General Public
|
yading@10
|
12 * License as published by the Free Software Foundation; either
|
yading@10
|
13 * version 2.1 of the License, or (at your option) any later version.
|
yading@10
|
14 *
|
yading@10
|
15 * FFmpeg is distributed in the hope that it will be useful,
|
yading@10
|
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
yading@10
|
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
yading@10
|
18 * Lesser General Public License for more details.
|
yading@10
|
19 *
|
yading@10
|
20 * You should have received a copy of the GNU Lesser General Public
|
yading@10
|
21 * License along with FFmpeg; if not, write to the Free Software
|
yading@10
|
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
yading@10
|
23 */
|
yading@10
|
24
|
yading@10
|
25 #include "libavutil/imgutils.h"
|
yading@10
|
26 #include "avcodec.h"
|
yading@10
|
27 #include "bytestream.h"
|
yading@10
|
28 #include "internal.h"
|
yading@10
|
29
|
yading@10
|
30 /** Maximum RLE code for bulk copy */
|
yading@10
|
31 #define MAX_RLE_BULK 127
|
yading@10
|
32 /** Maximum RLE code for repeat */
|
yading@10
|
33 #define MAX_RLE_REPEAT 128
|
yading@10
|
34 /** Maximum RLE code for skip */
|
yading@10
|
35 #define MAX_RLE_SKIP 254
|
yading@10
|
36
|
yading@10
|
37 typedef struct QtrleEncContext {
|
yading@10
|
38 AVCodecContext *avctx;
|
yading@10
|
39 AVFrame frame;
|
yading@10
|
40 int pixel_size;
|
yading@10
|
41 AVPicture previous_frame;
|
yading@10
|
42 unsigned int max_buf_size;
|
yading@10
|
43 int logical_width;
|
yading@10
|
44 /**
|
yading@10
|
45 * This array will contain at ith position the value of the best RLE code
|
yading@10
|
46 * if the line started at pixel i
|
yading@10
|
47 * There can be 3 values :
|
yading@10
|
48 * skip (0) : skip as much as possible pixels because they are equal to the
|
yading@10
|
49 * previous frame ones
|
yading@10
|
50 * repeat (<-1) : repeat that pixel -rle_code times, still as much as
|
yading@10
|
51 * possible
|
yading@10
|
52 * copy (>0) : copy the raw next rle_code pixels */
|
yading@10
|
53 signed char *rlecode_table;
|
yading@10
|
54 /**
|
yading@10
|
55 * This array will contain the length of the best rle encoding of the line
|
yading@10
|
56 * starting at ith pixel */
|
yading@10
|
57 int *length_table;
|
yading@10
|
58 /**
|
yading@10
|
59 * Will contain at ith position the number of consecutive pixels equal to the previous
|
yading@10
|
60 * frame starting from pixel i */
|
yading@10
|
61 uint8_t* skip_table;
|
yading@10
|
62 } QtrleEncContext;
|
yading@10
|
63
|
yading@10
|
64 static av_cold int qtrle_encode_init(AVCodecContext *avctx)
|
yading@10
|
65 {
|
yading@10
|
66 QtrleEncContext *s = avctx->priv_data;
|
yading@10
|
67 int ret;
|
yading@10
|
68
|
yading@10
|
69 if (av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0) {
|
yading@10
|
70 return AVERROR(EINVAL);
|
yading@10
|
71 }
|
yading@10
|
72 s->avctx=avctx;
|
yading@10
|
73 s->logical_width=avctx->width;
|
yading@10
|
74
|
yading@10
|
75 switch (avctx->pix_fmt) {
|
yading@10
|
76 case AV_PIX_FMT_GRAY8:
|
yading@10
|
77 s->logical_width = avctx->width / 4;
|
yading@10
|
78 s->pixel_size = 4;
|
yading@10
|
79 break;
|
yading@10
|
80 case AV_PIX_FMT_RGB555BE:
|
yading@10
|
81 s->pixel_size = 2;
|
yading@10
|
82 break;
|
yading@10
|
83 case AV_PIX_FMT_RGB24:
|
yading@10
|
84 s->pixel_size = 3;
|
yading@10
|
85 break;
|
yading@10
|
86 case AV_PIX_FMT_ARGB:
|
yading@10
|
87 s->pixel_size = 4;
|
yading@10
|
88 break;
|
yading@10
|
89 default:
|
yading@10
|
90 av_log(avctx, AV_LOG_ERROR, "Unsupported colorspace.\n");
|
yading@10
|
91 break;
|
yading@10
|
92 }
|
yading@10
|
93 avctx->bits_per_coded_sample = avctx->pix_fmt == AV_PIX_FMT_GRAY8 ? 40 : s->pixel_size*8;
|
yading@10
|
94
|
yading@10
|
95 s->rlecode_table = av_mallocz(s->logical_width);
|
yading@10
|
96 s->skip_table = av_mallocz(s->logical_width);
|
yading@10
|
97 s->length_table = av_mallocz((s->logical_width + 1)*sizeof(int));
|
yading@10
|
98 if (!s->skip_table || !s->length_table || !s->rlecode_table) {
|
yading@10
|
99 av_log(avctx, AV_LOG_ERROR, "Error allocating memory.\n");
|
yading@10
|
100 return AVERROR(ENOMEM);
|
yading@10
|
101 }
|
yading@10
|
102 if ((ret = avpicture_alloc(&s->previous_frame, avctx->pix_fmt, avctx->width, avctx->height)) < 0) {
|
yading@10
|
103 av_log(avctx, AV_LOG_ERROR, "Error allocating picture\n");
|
yading@10
|
104 return ret;
|
yading@10
|
105 }
|
yading@10
|
106
|
yading@10
|
107 s->max_buf_size = s->logical_width*s->avctx->height*s->pixel_size*2 /* image base material */
|
yading@10
|
108 + 15 /* header + footer */
|
yading@10
|
109 + s->avctx->height*2 /* skip code+rle end */
|
yading@10
|
110 + s->logical_width/MAX_RLE_BULK + 1 /* rle codes */;
|
yading@10
|
111 avctx->coded_frame = &s->frame;
|
yading@10
|
112 return 0;
|
yading@10
|
113 }
|
yading@10
|
114
|
yading@10
|
115 /**
|
yading@10
|
116 * Compute the best RLE sequence for a line
|
yading@10
|
117 */
|
yading@10
|
118 static void qtrle_encode_line(QtrleEncContext *s, const AVFrame *p, int line, uint8_t **buf)
|
yading@10
|
119 {
|
yading@10
|
120 int width=s->logical_width;
|
yading@10
|
121 int i;
|
yading@10
|
122 signed char rlecode;
|
yading@10
|
123
|
yading@10
|
124 /* This will be the number of pixels equal to the preivous frame one's
|
yading@10
|
125 * starting from the ith pixel */
|
yading@10
|
126 unsigned int skipcount;
|
yading@10
|
127 /* This will be the number of consecutive equal pixels in the current
|
yading@10
|
128 * frame, starting from the ith one also */
|
yading@10
|
129 unsigned int av_uninit(repeatcount);
|
yading@10
|
130
|
yading@10
|
131 /* The cost of the three different possibilities */
|
yading@10
|
132 int total_skip_cost;
|
yading@10
|
133 int total_repeat_cost;
|
yading@10
|
134
|
yading@10
|
135 int base_bulk_cost;
|
yading@10
|
136 int lowest_bulk_cost;
|
yading@10
|
137 int lowest_bulk_cost_index;
|
yading@10
|
138 int sec_lowest_bulk_cost;
|
yading@10
|
139 int sec_lowest_bulk_cost_index;
|
yading@10
|
140
|
yading@10
|
141 uint8_t *this_line = p-> data[0] + line*p-> linesize[0] +
|
yading@10
|
142 (width - 1)*s->pixel_size;
|
yading@10
|
143 uint8_t *prev_line = s->previous_frame.data[0] + line*s->previous_frame.linesize[0] +
|
yading@10
|
144 (width - 1)*s->pixel_size;
|
yading@10
|
145
|
yading@10
|
146 s->length_table[width] = 0;
|
yading@10
|
147 skipcount = 0;
|
yading@10
|
148
|
yading@10
|
149 /* Initial values */
|
yading@10
|
150 lowest_bulk_cost = INT_MAX / 2;
|
yading@10
|
151 lowest_bulk_cost_index = width;
|
yading@10
|
152 sec_lowest_bulk_cost = INT_MAX / 2;
|
yading@10
|
153 sec_lowest_bulk_cost_index = width;
|
yading@10
|
154
|
yading@10
|
155 base_bulk_cost = 1 + s->pixel_size;
|
yading@10
|
156
|
yading@10
|
157 for (i = width - 1; i >= 0; i--) {
|
yading@10
|
158
|
yading@10
|
159 int prev_bulk_cost;
|
yading@10
|
160
|
yading@10
|
161 /* If our lowest bulk cost index is too far away, replace it
|
yading@10
|
162 * with the next lowest bulk cost */
|
yading@10
|
163 if (FFMIN(width, i + MAX_RLE_BULK) < lowest_bulk_cost_index) {
|
yading@10
|
164 lowest_bulk_cost = sec_lowest_bulk_cost;
|
yading@10
|
165 lowest_bulk_cost_index = sec_lowest_bulk_cost_index;
|
yading@10
|
166
|
yading@10
|
167 sec_lowest_bulk_cost = INT_MAX / 2;
|
yading@10
|
168 sec_lowest_bulk_cost_index = width;
|
yading@10
|
169 }
|
yading@10
|
170
|
yading@10
|
171 /* Deal with the first pixel's bulk cost */
|
yading@10
|
172 if (!i) {
|
yading@10
|
173 base_bulk_cost++;
|
yading@10
|
174 lowest_bulk_cost++;
|
yading@10
|
175 sec_lowest_bulk_cost++;
|
yading@10
|
176 }
|
yading@10
|
177
|
yading@10
|
178 /* Look at the bulk cost of the previous loop and see if it is
|
yading@10
|
179 * a new lower bulk cost */
|
yading@10
|
180 prev_bulk_cost = s->length_table[i + 1] + base_bulk_cost;
|
yading@10
|
181 if (prev_bulk_cost <= sec_lowest_bulk_cost) {
|
yading@10
|
182 /* If it's lower than the 2nd lowest, then it may be lower
|
yading@10
|
183 * than the lowest */
|
yading@10
|
184 if (prev_bulk_cost <= lowest_bulk_cost) {
|
yading@10
|
185
|
yading@10
|
186 /* If we have found a new lowest bulk cost,
|
yading@10
|
187 * then the 2nd lowest bulk cost is now farther than the
|
yading@10
|
188 * lowest bulk cost, and will never be used */
|
yading@10
|
189 sec_lowest_bulk_cost = INT_MAX / 2;
|
yading@10
|
190
|
yading@10
|
191 lowest_bulk_cost = prev_bulk_cost;
|
yading@10
|
192 lowest_bulk_cost_index = i + 1;
|
yading@10
|
193 } else {
|
yading@10
|
194 /* Then it must be the 2nd lowest bulk cost */
|
yading@10
|
195 sec_lowest_bulk_cost = prev_bulk_cost;
|
yading@10
|
196 sec_lowest_bulk_cost_index = i + 1;
|
yading@10
|
197 }
|
yading@10
|
198 }
|
yading@10
|
199
|
yading@10
|
200 if (!s->frame.key_frame && !memcmp(this_line, prev_line, s->pixel_size))
|
yading@10
|
201 skipcount = FFMIN(skipcount + 1, MAX_RLE_SKIP);
|
yading@10
|
202 else
|
yading@10
|
203 skipcount = 0;
|
yading@10
|
204
|
yading@10
|
205 total_skip_cost = s->length_table[i + skipcount] + 2;
|
yading@10
|
206 s->skip_table[i] = skipcount;
|
yading@10
|
207
|
yading@10
|
208
|
yading@10
|
209 if (i < width - 1 && !memcmp(this_line, this_line + s->pixel_size, s->pixel_size))
|
yading@10
|
210 repeatcount = FFMIN(repeatcount + 1, MAX_RLE_REPEAT);
|
yading@10
|
211 else
|
yading@10
|
212 repeatcount = 1;
|
yading@10
|
213
|
yading@10
|
214 total_repeat_cost = s->length_table[i + repeatcount] + 1 + s->pixel_size;
|
yading@10
|
215
|
yading@10
|
216 /* skip code is free for the first pixel, it costs one byte for repeat and bulk copy
|
yading@10
|
217 * so let's make it aware */
|
yading@10
|
218 if (i == 0) {
|
yading@10
|
219 total_skip_cost--;
|
yading@10
|
220 total_repeat_cost++;
|
yading@10
|
221 }
|
yading@10
|
222
|
yading@10
|
223 if (repeatcount > 1 && (skipcount == 0 || total_repeat_cost < total_skip_cost)) {
|
yading@10
|
224 /* repeat is the best */
|
yading@10
|
225 s->length_table[i] = total_repeat_cost;
|
yading@10
|
226 s->rlecode_table[i] = -repeatcount;
|
yading@10
|
227 }
|
yading@10
|
228 else if (skipcount > 0) {
|
yading@10
|
229 /* skip is the best choice here */
|
yading@10
|
230 s->length_table[i] = total_skip_cost;
|
yading@10
|
231 s->rlecode_table[i] = 0;
|
yading@10
|
232 }
|
yading@10
|
233 else {
|
yading@10
|
234 /* We cannot do neither skip nor repeat
|
yading@10
|
235 * thus we use the best bulk copy */
|
yading@10
|
236
|
yading@10
|
237 s->length_table[i] = lowest_bulk_cost;
|
yading@10
|
238 s->rlecode_table[i] = lowest_bulk_cost_index - i;
|
yading@10
|
239
|
yading@10
|
240 }
|
yading@10
|
241
|
yading@10
|
242 /* These bulk costs increase every iteration */
|
yading@10
|
243 lowest_bulk_cost += s->pixel_size;
|
yading@10
|
244 sec_lowest_bulk_cost += s->pixel_size;
|
yading@10
|
245
|
yading@10
|
246 this_line -= s->pixel_size;
|
yading@10
|
247 prev_line -= s->pixel_size;
|
yading@10
|
248 }
|
yading@10
|
249
|
yading@10
|
250 /* Good ! Now we have the best sequence for this line, let's output it */
|
yading@10
|
251
|
yading@10
|
252 /* We do a special case for the first pixel so that we avoid testing it in
|
yading@10
|
253 * the whole loop */
|
yading@10
|
254
|
yading@10
|
255 i=0;
|
yading@10
|
256 this_line = p-> data[0] + line*p->linesize[0];
|
yading@10
|
257
|
yading@10
|
258 if (s->rlecode_table[0] == 0) {
|
yading@10
|
259 bytestream_put_byte(buf, s->skip_table[0] + 1);
|
yading@10
|
260 i += s->skip_table[0];
|
yading@10
|
261 }
|
yading@10
|
262 else bytestream_put_byte(buf, 1);
|
yading@10
|
263
|
yading@10
|
264
|
yading@10
|
265 while (i < width) {
|
yading@10
|
266 rlecode = s->rlecode_table[i];
|
yading@10
|
267 bytestream_put_byte(buf, rlecode);
|
yading@10
|
268 if (rlecode == 0) {
|
yading@10
|
269 /* Write a skip sequence */
|
yading@10
|
270 bytestream_put_byte(buf, s->skip_table[i] + 1);
|
yading@10
|
271 i += s->skip_table[i];
|
yading@10
|
272 }
|
yading@10
|
273 else if (rlecode > 0) {
|
yading@10
|
274 /* bulk copy */
|
yading@10
|
275 if (s->avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
|
yading@10
|
276 int j;
|
yading@10
|
277 // QT grayscale colorspace has 0=white and 255=black, we will
|
yading@10
|
278 // ignore the palette that is included in the AVFrame because
|
yading@10
|
279 // AV_PIX_FMT_GRAY8 has defined color mapping
|
yading@10
|
280 for (j = 0; j < rlecode*s->pixel_size; ++j)
|
yading@10
|
281 bytestream_put_byte(buf, *(this_line + i*s->pixel_size + j) ^ 0xff);
|
yading@10
|
282 } else {
|
yading@10
|
283 bytestream_put_buffer(buf, this_line + i*s->pixel_size, rlecode*s->pixel_size);
|
yading@10
|
284 }
|
yading@10
|
285 i += rlecode;
|
yading@10
|
286 }
|
yading@10
|
287 else {
|
yading@10
|
288 /* repeat the bits */
|
yading@10
|
289 if (s->avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
|
yading@10
|
290 int j;
|
yading@10
|
291 // QT grayscale colorspace has 0=white and 255=black, ...
|
yading@10
|
292 for (j = 0; j < s->pixel_size; ++j)
|
yading@10
|
293 bytestream_put_byte(buf, *(this_line + i*s->pixel_size + j) ^ 0xff);
|
yading@10
|
294 } else {
|
yading@10
|
295 bytestream_put_buffer(buf, this_line + i*s->pixel_size, s->pixel_size);
|
yading@10
|
296 }
|
yading@10
|
297 i -= rlecode;
|
yading@10
|
298 }
|
yading@10
|
299 }
|
yading@10
|
300 bytestream_put_byte(buf, -1); // end RLE line
|
yading@10
|
301 }
|
yading@10
|
302
|
yading@10
|
303 /** Encode frame including header */
|
yading@10
|
304 static int encode_frame(QtrleEncContext *s, const AVFrame *p, uint8_t *buf)
|
yading@10
|
305 {
|
yading@10
|
306 int i;
|
yading@10
|
307 int start_line = 0;
|
yading@10
|
308 int end_line = s->avctx->height;
|
yading@10
|
309 uint8_t *orig_buf = buf;
|
yading@10
|
310
|
yading@10
|
311 if (!s->frame.key_frame) {
|
yading@10
|
312 unsigned line_size = s->logical_width * s->pixel_size;
|
yading@10
|
313 for (start_line = 0; start_line < s->avctx->height; start_line++)
|
yading@10
|
314 if (memcmp(p->data[0] + start_line*p->linesize[0],
|
yading@10
|
315 s->previous_frame.data[0] + start_line*s->previous_frame.linesize[0],
|
yading@10
|
316 line_size))
|
yading@10
|
317 break;
|
yading@10
|
318
|
yading@10
|
319 for (end_line=s->avctx->height; end_line > start_line; end_line--)
|
yading@10
|
320 if (memcmp(p->data[0] + (end_line - 1)*p->linesize[0],
|
yading@10
|
321 s->previous_frame.data[0] + (end_line - 1)*s->previous_frame.linesize[0],
|
yading@10
|
322 line_size))
|
yading@10
|
323 break;
|
yading@10
|
324 }
|
yading@10
|
325
|
yading@10
|
326 bytestream_put_be32(&buf, 0); // CHUNK SIZE, patched later
|
yading@10
|
327
|
yading@10
|
328 if ((start_line == 0 && end_line == s->avctx->height) || start_line == s->avctx->height)
|
yading@10
|
329 bytestream_put_be16(&buf, 0); // header
|
yading@10
|
330 else {
|
yading@10
|
331 bytestream_put_be16(&buf, 8); // header
|
yading@10
|
332 bytestream_put_be16(&buf, start_line); // starting line
|
yading@10
|
333 bytestream_put_be16(&buf, 0); // unknown
|
yading@10
|
334 bytestream_put_be16(&buf, end_line - start_line); // lines to update
|
yading@10
|
335 bytestream_put_be16(&buf, 0); // unknown
|
yading@10
|
336 }
|
yading@10
|
337 for (i = start_line; i < end_line; i++)
|
yading@10
|
338 qtrle_encode_line(s, p, i, &buf);
|
yading@10
|
339
|
yading@10
|
340 bytestream_put_byte(&buf, 0); // zero skip code = frame finished
|
yading@10
|
341 AV_WB32(orig_buf, buf - orig_buf); // patch the chunk size
|
yading@10
|
342 return buf - orig_buf;
|
yading@10
|
343 }
|
yading@10
|
344
|
yading@10
|
345 static int qtrle_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
|
yading@10
|
346 const AVFrame *pict, int *got_packet)
|
yading@10
|
347 {
|
yading@10
|
348 QtrleEncContext * const s = avctx->priv_data;
|
yading@10
|
349 AVFrame * const p = &s->frame;
|
yading@10
|
350 int ret;
|
yading@10
|
351
|
yading@10
|
352 *p = *pict;
|
yading@10
|
353
|
yading@10
|
354 if ((ret = ff_alloc_packet2(avctx, pkt, s->max_buf_size)) < 0)
|
yading@10
|
355 return ret;
|
yading@10
|
356
|
yading@10
|
357 if (avctx->gop_size == 0 || (s->avctx->frame_number % avctx->gop_size) == 0) {
|
yading@10
|
358 /* I-Frame */
|
yading@10
|
359 p->pict_type = AV_PICTURE_TYPE_I;
|
yading@10
|
360 p->key_frame = 1;
|
yading@10
|
361 } else {
|
yading@10
|
362 /* P-Frame */
|
yading@10
|
363 p->pict_type = AV_PICTURE_TYPE_P;
|
yading@10
|
364 p->key_frame = 0;
|
yading@10
|
365 }
|
yading@10
|
366
|
yading@10
|
367 pkt->size = encode_frame(s, pict, pkt->data);
|
yading@10
|
368
|
yading@10
|
369 /* save the current frame */
|
yading@10
|
370 av_picture_copy(&s->previous_frame, (AVPicture *)p, avctx->pix_fmt, avctx->width, avctx->height);
|
yading@10
|
371
|
yading@10
|
372 if (p->key_frame)
|
yading@10
|
373 pkt->flags |= AV_PKT_FLAG_KEY;
|
yading@10
|
374 *got_packet = 1;
|
yading@10
|
375
|
yading@10
|
376 return 0;
|
yading@10
|
377 }
|
yading@10
|
378
|
yading@10
|
379 static av_cold int qtrle_encode_end(AVCodecContext *avctx)
|
yading@10
|
380 {
|
yading@10
|
381 QtrleEncContext *s = avctx->priv_data;
|
yading@10
|
382
|
yading@10
|
383 avpicture_free(&s->previous_frame);
|
yading@10
|
384 av_free(s->rlecode_table);
|
yading@10
|
385 av_free(s->length_table);
|
yading@10
|
386 av_free(s->skip_table);
|
yading@10
|
387 return 0;
|
yading@10
|
388 }
|
yading@10
|
389
|
yading@10
|
390 AVCodec ff_qtrle_encoder = {
|
yading@10
|
391 .name = "qtrle",
|
yading@10
|
392 .type = AVMEDIA_TYPE_VIDEO,
|
yading@10
|
393 .id = AV_CODEC_ID_QTRLE,
|
yading@10
|
394 .priv_data_size = sizeof(QtrleEncContext),
|
yading@10
|
395 .init = qtrle_encode_init,
|
yading@10
|
396 .encode2 = qtrle_encode_frame,
|
yading@10
|
397 .close = qtrle_encode_end,
|
yading@10
|
398 .pix_fmts = (const enum AVPixelFormat[]){
|
yading@10
|
399 AV_PIX_FMT_RGB24, AV_PIX_FMT_RGB555BE, AV_PIX_FMT_ARGB, AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE
|
yading@10
|
400 },
|
yading@10
|
401 .long_name = NULL_IF_CONFIG_SMALL("QuickTime Animation (RLE) video"),
|
yading@10
|
402 };
|