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