00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "libavutil/imgutils.h"
00026 #include "avcodec.h"
00027 #include "bytestream.h"
00028 #include "internal.h"
00029
00031 #define MAX_RLE_BULK 127
00032
00033 #define MAX_RLE_REPEAT 128
00034
00035 #define MAX_RLE_SKIP 254
00036
00037 typedef struct QtrleEncContext {
00038 AVCodecContext *avctx;
00039 AVFrame frame;
00040 int pixel_size;
00041 AVPicture previous_frame;
00042 unsigned int max_buf_size;
00043 int logical_width;
00053 signed char *rlecode_table;
00057 int *length_table;
00061 uint8_t* skip_table;
00062 } QtrleEncContext;
00063
00064 static av_cold int qtrle_encode_init(AVCodecContext *avctx)
00065 {
00066 QtrleEncContext *s = avctx->priv_data;
00067
00068 if (av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0) {
00069 return -1;
00070 }
00071 s->avctx=avctx;
00072 s->logical_width=avctx->width;
00073
00074 switch (avctx->pix_fmt) {
00075 case PIX_FMT_GRAY8:
00076 s->logical_width = avctx->width / 4;
00077 s->pixel_size = 4;
00078 break;
00079 case PIX_FMT_RGB555BE:
00080 s->pixel_size = 2;
00081 break;
00082 case PIX_FMT_RGB24:
00083 s->pixel_size = 3;
00084 break;
00085 case PIX_FMT_ARGB:
00086 s->pixel_size = 4;
00087 break;
00088 default:
00089 av_log(avctx, AV_LOG_ERROR, "Unsupported colorspace.\n");
00090 break;
00091 }
00092 avctx->bits_per_coded_sample = avctx->pix_fmt == PIX_FMT_GRAY8 ? 40 : s->pixel_size*8;
00093
00094 s->rlecode_table = av_mallocz(s->logical_width);
00095 s->skip_table = av_mallocz(s->logical_width);
00096 s->length_table = av_mallocz((s->logical_width + 1)*sizeof(int));
00097 if (!s->skip_table || !s->length_table || !s->rlecode_table) {
00098 av_log(avctx, AV_LOG_ERROR, "Error allocating memory.\n");
00099 return -1;
00100 }
00101 if (avpicture_alloc(&s->previous_frame, avctx->pix_fmt, avctx->width, avctx->height) < 0) {
00102 av_log(avctx, AV_LOG_ERROR, "Error allocating picture\n");
00103 return -1;
00104 }
00105
00106 s->max_buf_size = s->logical_width*s->avctx->height*s->pixel_size*2
00107 + 15
00108 + s->avctx->height*2
00109 + s->logical_width/MAX_RLE_BULK + 1 ;
00110 avctx->coded_frame = &s->frame;
00111 return 0;
00112 }
00113
00117 static void qtrle_encode_line(QtrleEncContext *s, const AVFrame *p, int line, uint8_t **buf)
00118 {
00119 int width=s->logical_width;
00120 int i;
00121 signed char rlecode;
00122
00123
00124 unsigned int av_uninit(bulkcount);
00125
00126
00127 unsigned int skipcount;
00128
00129
00130 unsigned int av_uninit(repeatcount);
00131
00132
00133 int total_bulk_cost;
00134 int total_skip_cost;
00135 int total_repeat_cost;
00136
00137 int temp_cost;
00138 int j;
00139
00140 uint8_t *this_line = p-> data[0] + line*p-> linesize[0] +
00141 (width - 1)*s->pixel_size;
00142 uint8_t *prev_line = s->previous_frame.data[0] + line*s->previous_frame.linesize[0] +
00143 (width - 1)*s->pixel_size;
00144
00145 s->length_table[width] = 0;
00146 skipcount = 0;
00147
00148 for (i = width - 1; i >= 0; i--) {
00149
00150 if (!s->frame.key_frame && !memcmp(this_line, prev_line, s->pixel_size))
00151 skipcount = FFMIN(skipcount + 1, MAX_RLE_SKIP);
00152 else
00153 skipcount = 0;
00154
00155 total_skip_cost = s->length_table[i + skipcount] + 2;
00156 s->skip_table[i] = skipcount;
00157
00158
00159 if (i < width - 1 && !memcmp(this_line, this_line + s->pixel_size, s->pixel_size))
00160 repeatcount = FFMIN(repeatcount + 1, MAX_RLE_REPEAT);
00161 else
00162 repeatcount = 1;
00163
00164 total_repeat_cost = s->length_table[i + repeatcount] + 1 + s->pixel_size;
00165
00166
00167
00168 if (i == 0) {
00169 total_skip_cost--;
00170 total_repeat_cost++;
00171 }
00172
00173 if (repeatcount > 1 && (skipcount == 0 || total_repeat_cost < total_skip_cost)) {
00174
00175 s->length_table[i] = total_repeat_cost;
00176 s->rlecode_table[i] = -repeatcount;
00177 }
00178 else if (skipcount > 0) {
00179
00180 s->length_table[i] = total_skip_cost;
00181 s->rlecode_table[i] = 0;
00182 }
00183 else {
00184
00185
00186
00187 int limit = FFMIN(width - i, MAX_RLE_BULK);
00188
00189 temp_cost = 1 + s->pixel_size + !i;
00190 total_bulk_cost = INT_MAX;
00191
00192 for (j = 1; j <= limit; j++) {
00193 if (s->length_table[i + j] + temp_cost < total_bulk_cost) {
00194
00195 total_bulk_cost = s->length_table[i + j] + temp_cost;
00196 bulkcount = j;
00197 }
00198 temp_cost += s->pixel_size;
00199 }
00200
00201 s->length_table[i] = total_bulk_cost;
00202 s->rlecode_table[i] = bulkcount;
00203 }
00204
00205 this_line -= s->pixel_size;
00206 prev_line -= s->pixel_size;
00207 }
00208
00209
00210
00211
00212
00213
00214 i=0;
00215 this_line = p-> data[0] + line*p->linesize[0];
00216
00217 if (s->rlecode_table[0] == 0) {
00218 bytestream_put_byte(buf, s->skip_table[0] + 1);
00219 i += s->skip_table[0];
00220 }
00221 else bytestream_put_byte(buf, 1);
00222
00223
00224 while (i < width) {
00225 rlecode = s->rlecode_table[i];
00226 bytestream_put_byte(buf, rlecode);
00227 if (rlecode == 0) {
00228
00229 bytestream_put_byte(buf, s->skip_table[i] + 1);
00230 i += s->skip_table[i];
00231 }
00232 else if (rlecode > 0) {
00233
00234 if (s->avctx->pix_fmt == PIX_FMT_GRAY8) {
00235 int j;
00236
00237
00238
00239 for (j = 0; j < rlecode*s->pixel_size; ++j)
00240 bytestream_put_byte(buf, *(this_line + i*s->pixel_size + j) ^ 0xff);
00241 } else {
00242 bytestream_put_buffer(buf, this_line + i*s->pixel_size, rlecode*s->pixel_size);
00243 }
00244 i += rlecode;
00245 }
00246 else {
00247
00248 if (s->avctx->pix_fmt == PIX_FMT_GRAY8) {
00249 int j;
00250
00251 for (j = 0; j < s->pixel_size; ++j)
00252 bytestream_put_byte(buf, *(this_line + i*s->pixel_size + j) ^ 0xff);
00253 } else {
00254 bytestream_put_buffer(buf, this_line + i*s->pixel_size, s->pixel_size);
00255 }
00256 i -= rlecode;
00257 }
00258 }
00259 bytestream_put_byte(buf, -1);
00260 }
00261
00263 static int encode_frame(QtrleEncContext *s, const AVFrame *p, uint8_t *buf)
00264 {
00265 int i;
00266 int start_line = 0;
00267 int end_line = s->avctx->height;
00268 uint8_t *orig_buf = buf;
00269
00270 if (!s->frame.key_frame) {
00271 unsigned line_size = s->logical_width * s->pixel_size;
00272 for (start_line = 0; start_line < s->avctx->height; start_line++)
00273 if (memcmp(p->data[0] + start_line*p->linesize[0],
00274 s->previous_frame.data[0] + start_line*s->previous_frame.linesize[0],
00275 line_size))
00276 break;
00277
00278 for (end_line=s->avctx->height; end_line > start_line; end_line--)
00279 if (memcmp(p->data[0] + (end_line - 1)*p->linesize[0],
00280 s->previous_frame.data[0] + (end_line - 1)*s->previous_frame.linesize[0],
00281 line_size))
00282 break;
00283 }
00284
00285 bytestream_put_be32(&buf, 0);
00286
00287 if ((start_line == 0 && end_line == s->avctx->height) || start_line == s->avctx->height)
00288 bytestream_put_be16(&buf, 0);
00289 else {
00290 bytestream_put_be16(&buf, 8);
00291 bytestream_put_be16(&buf, start_line);
00292 bytestream_put_be16(&buf, 0);
00293 bytestream_put_be16(&buf, end_line - start_line);
00294 bytestream_put_be16(&buf, 0);
00295 }
00296 for (i = start_line; i < end_line; i++)
00297 qtrle_encode_line(s, p, i, &buf);
00298
00299 bytestream_put_byte(&buf, 0);
00300 AV_WB32(orig_buf, buf - orig_buf);
00301 return buf - orig_buf;
00302 }
00303
00304 static int qtrle_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
00305 const AVFrame *pict, int *got_packet)
00306 {
00307 QtrleEncContext * const s = avctx->priv_data;
00308 AVFrame * const p = &s->frame;
00309 int ret;
00310
00311 *p = *pict;
00312
00313 if ((ret = ff_alloc_packet2(avctx, pkt, s->max_buf_size)) < 0)
00314 return ret;
00315
00316 if (avctx->gop_size == 0 || (s->avctx->frame_number % avctx->gop_size) == 0) {
00317
00318 p->pict_type = AV_PICTURE_TYPE_I;
00319 p->key_frame = 1;
00320 } else {
00321
00322 p->pict_type = AV_PICTURE_TYPE_P;
00323 p->key_frame = 0;
00324 }
00325
00326 pkt->size = encode_frame(s, pict, pkt->data);
00327
00328
00329 av_picture_copy(&s->previous_frame, (AVPicture *)p, avctx->pix_fmt, avctx->width, avctx->height);
00330
00331 if (p->key_frame)
00332 pkt->flags |= AV_PKT_FLAG_KEY;
00333 *got_packet = 1;
00334
00335 return 0;
00336 }
00337
00338 static av_cold int qtrle_encode_end(AVCodecContext *avctx)
00339 {
00340 QtrleEncContext *s = avctx->priv_data;
00341
00342 avpicture_free(&s->previous_frame);
00343 av_free(s->rlecode_table);
00344 av_free(s->length_table);
00345 av_free(s->skip_table);
00346 return 0;
00347 }
00348
00349 AVCodec ff_qtrle_encoder = {
00350 .name = "qtrle",
00351 .type = AVMEDIA_TYPE_VIDEO,
00352 .id = CODEC_ID_QTRLE,
00353 .priv_data_size = sizeof(QtrleEncContext),
00354 .init = qtrle_encode_init,
00355 .encode2 = qtrle_encode_frame,
00356 .close = qtrle_encode_end,
00357 .pix_fmts = (const enum PixelFormat[]){
00358 PIX_FMT_RGB24, PIX_FMT_RGB555BE, PIX_FMT_ARGB, PIX_FMT_GRAY8, PIX_FMT_NONE
00359 },
00360 .long_name = NULL_IF_CONFIG_SMALL("QuickTime Animation (RLE) video"),
00361 };