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 "avcodec.h"
00026 #include "bytestream.h"
00027
00029 #define MAX_RLE_BULK 127
00030
00031 #define MAX_RLE_REPEAT 128
00032
00033 #define MAX_RLE_SKIP 254
00034
00035 typedef struct QtrleEncContext {
00036 AVCodecContext *avctx;
00037 AVFrame frame;
00038 int pixel_size;
00039 AVPicture previous_frame;
00040 unsigned int max_buf_size;
00050 signed char *rlecode_table;
00054 int *length_table;
00058 uint8_t* skip_table;
00059 } QtrleEncContext;
00060
00061 static av_cold int qtrle_encode_init(AVCodecContext *avctx)
00062 {
00063 QtrleEncContext *s = avctx->priv_data;
00064
00065 if (avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 0) {
00066 return -1;
00067 }
00068 s->avctx=avctx;
00069
00070 switch (avctx->pix_fmt) {
00071
00072
00073
00074 case PIX_FMT_RGB24:
00075 s->pixel_size = 3;
00076 break;
00077 default:
00078 av_log(avctx, AV_LOG_ERROR, "Unsupported colorspace.\n");
00079 break;
00080 }
00081 avctx->bits_per_coded_sample = s->pixel_size*8;
00082
00083 s->rlecode_table = av_mallocz(s->avctx->width);
00084 s->skip_table = av_mallocz(s->avctx->width);
00085 s->length_table = av_mallocz((s->avctx->width + 1)*sizeof(int));
00086 if (!s->skip_table || !s->length_table || !s->rlecode_table) {
00087 av_log(avctx, AV_LOG_ERROR, "Error allocating memory.\n");
00088 return -1;
00089 }
00090 if (avpicture_alloc(&s->previous_frame, avctx->pix_fmt, avctx->width, avctx->height) < 0) {
00091 av_log(avctx, AV_LOG_ERROR, "Error allocating picture\n");
00092 return -1;
00093 }
00094
00095 s->max_buf_size = s->avctx->width*s->avctx->height*s->pixel_size
00096 + 15
00097 + s->avctx->height*2
00098 + s->avctx->width/MAX_RLE_BULK + 1 ;
00099 avctx->coded_frame = &s->frame;
00100 return 0;
00101 }
00102
00106 static void qtrle_encode_line(QtrleEncContext *s, AVFrame *p, int line, uint8_t **buf)
00107 {
00108 int width=s->avctx->width;
00109 int i;
00110 signed char rlecode;
00111
00112
00113 unsigned int bulkcount;
00114
00115
00116 unsigned int skipcount;
00117
00118
00119 unsigned int av_uninit(repeatcount);
00120
00121
00122 int total_bulk_cost;
00123 int total_skip_cost;
00124 int total_repeat_cost;
00125
00126 int temp_cost;
00127 int j;
00128
00129 uint8_t *this_line = p-> data[0] + line*p->linesize[0] + (width - 1)*s->pixel_size;
00130 uint8_t *prev_line = s->previous_frame.data[0] + line*p->linesize[0] + (width - 1)*s->pixel_size;
00131
00132 s->length_table[width] = 0;
00133 skipcount = 0;
00134
00135 for (i = width - 1; i >= 0; i--) {
00136
00137 if (!s->frame.key_frame && !memcmp(this_line, prev_line, s->pixel_size))
00138 skipcount = FFMIN(skipcount + 1, MAX_RLE_SKIP);
00139 else
00140 skipcount = 0;
00141
00142 total_skip_cost = s->length_table[i + skipcount] + 2;
00143 s->skip_table[i] = skipcount;
00144
00145
00146 if (i < width - 1 && !memcmp(this_line, this_line + s->pixel_size, s->pixel_size))
00147 repeatcount = FFMIN(repeatcount + 1, MAX_RLE_REPEAT);
00148 else
00149 repeatcount = 1;
00150
00151 total_repeat_cost = s->length_table[i + repeatcount] + 1 + s->pixel_size;
00152
00153
00154
00155 if (i == 0) {
00156 total_skip_cost--;
00157 total_repeat_cost++;
00158 }
00159
00160 if (repeatcount > 1 && (skipcount == 0 || total_repeat_cost < total_skip_cost)) {
00161
00162 s->length_table[i] = total_repeat_cost;
00163 s->rlecode_table[i] = -repeatcount;
00164 }
00165 else if (skipcount > 0) {
00166
00167 s->length_table[i] = total_skip_cost;
00168 s->rlecode_table[i] = 0;
00169 }
00170 else {
00171
00172
00173
00174 int limit = FFMIN(width - i, MAX_RLE_BULK);
00175
00176 temp_cost = 1 + s->pixel_size + !i;
00177 total_bulk_cost = INT_MAX;
00178
00179 for (j = 1; j <= limit; j++) {
00180 if (s->length_table[i + j] + temp_cost < total_bulk_cost) {
00181
00182 total_bulk_cost = s->length_table[i + j] + temp_cost;
00183 bulkcount = j;
00184 }
00185 temp_cost += s->pixel_size;
00186 }
00187
00188 s->length_table[i] = total_bulk_cost;
00189 s->rlecode_table[i] = bulkcount;
00190 }
00191
00192 this_line -= s->pixel_size;
00193 prev_line -= s->pixel_size;
00194 }
00195
00196
00197
00198
00199
00200
00201 i=0;
00202 this_line = p-> data[0] + line*p->linesize[0];
00203 prev_line = s->previous_frame.data[0] + line*p->linesize[0];
00204
00205 if (s->rlecode_table[0] == 0) {
00206 bytestream_put_byte(buf, s->skip_table[0] + 1);
00207 i += s->skip_table[0];
00208 }
00209 else bytestream_put_byte(buf, 1);
00210
00211
00212 while (i < width) {
00213 rlecode = s->rlecode_table[i];
00214 bytestream_put_byte(buf, rlecode);
00215 if (rlecode == 0) {
00216
00217 bytestream_put_byte(buf, s->skip_table[i] + 1);
00218 i += s->skip_table[i];
00219 }
00220 else if (rlecode > 0) {
00221
00222 bytestream_put_buffer(buf, this_line + i*s->pixel_size, rlecode*s->pixel_size);
00223 i += rlecode;
00224 }
00225 else {
00226
00227 bytestream_put_buffer(buf, this_line + i*s->pixel_size, s->pixel_size);
00228 i -= rlecode;
00229 }
00230 }
00231 bytestream_put_byte(buf, -1);
00232 }
00233
00235 static int encode_frame(QtrleEncContext *s, AVFrame *p, uint8_t *buf)
00236 {
00237 int i;
00238 int start_line = 0;
00239 int end_line = s->avctx->height;
00240 uint8_t *orig_buf = buf;
00241
00242 if (!s->frame.key_frame) {
00243 for (start_line = 0; start_line < s->avctx->height; start_line++)
00244 if (memcmp(p->data[0] + start_line*p->linesize[0],
00245 s->previous_frame.data[0] + start_line*p->linesize[0],
00246 p->linesize[0]))
00247 break;
00248
00249 for (end_line=s->avctx->height; end_line > start_line; end_line--)
00250 if (memcmp(p->data[0] + (end_line - 1)*p->linesize[0],
00251 s->previous_frame.data[0] + (end_line - 1)*p->linesize[0],
00252 p->linesize[0]))
00253 break;
00254 }
00255
00256 bytestream_put_be32(&buf, 0);
00257
00258 if ((start_line == 0 && end_line == s->avctx->height) || start_line == s->avctx->height)
00259 bytestream_put_be16(&buf, 0);
00260 else {
00261 bytestream_put_be16(&buf, 8);
00262 bytestream_put_be16(&buf, start_line);
00263 bytestream_put_be16(&buf, 0);
00264 bytestream_put_be16(&buf, end_line - start_line);
00265 bytestream_put_be16(&buf, 0);
00266 }
00267 for (i = start_line; i < end_line; i++)
00268 qtrle_encode_line(s, p, i, &buf);
00269
00270 bytestream_put_byte(&buf, 0);
00271 AV_WB32(orig_buf, buf - orig_buf);
00272 return buf - orig_buf;
00273 }
00274
00275 static int qtrle_encode_frame(AVCodecContext *avctx, uint8_t *buf, int buf_size, void *data)
00276 {
00277 QtrleEncContext * const s = avctx->priv_data;
00278 AVFrame *pict = data;
00279 AVFrame * const p = &s->frame;
00280 int chunksize;
00281
00282 *p = *pict;
00283
00284 if (buf_size < s->max_buf_size) {
00285
00286 av_log(avctx, AV_LOG_ERROR, "buf_size %d < %d\n", buf_size, s->max_buf_size);
00287 return -1;
00288 }
00289
00290 if (avctx->gop_size == 0 || (s->avctx->frame_number % avctx->gop_size) == 0) {
00291
00292 p->pict_type = FF_I_TYPE;
00293 p->key_frame = 1;
00294 } else {
00295
00296 p->pict_type = FF_P_TYPE;
00297 p->key_frame = 0;
00298 }
00299
00300 chunksize = encode_frame(s, pict, buf);
00301
00302
00303 av_picture_copy(&s->previous_frame, (AVPicture *)p, avctx->pix_fmt, avctx->width, avctx->height);
00304 return chunksize;
00305 }
00306
00307 static av_cold int qtrle_encode_end(AVCodecContext *avctx)
00308 {
00309 QtrleEncContext *s = avctx->priv_data;
00310
00311 avpicture_free(&s->previous_frame);
00312 av_free(s->rlecode_table);
00313 av_free(s->length_table);
00314 av_free(s->skip_table);
00315 return 0;
00316 }
00317
00318 AVCodec qtrle_encoder = {
00319 "qtrle",
00320 CODEC_TYPE_VIDEO,
00321 CODEC_ID_QTRLE,
00322 sizeof(QtrleEncContext),
00323 qtrle_encode_init,
00324 qtrle_encode_frame,
00325 qtrle_encode_end,
00326 .pix_fmts = (enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_NONE},
00327 .long_name = NULL_IF_CONFIG_SMALL("QuickTime Animation (RLE) video"),
00328 };