• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavcodec/qtrleenc.c

Go to the documentation of this file.
00001 /*
00002  * Quicktime Animation (RLE) Video Encoder
00003  * Copyright (C) 2007 Clemens Fruhwirth
00004  * Copyright (C) 2007 Alexis Ballier
00005  *
00006  * This file is based on flashsvenc.c.
00007  *
00008  * This file is part of FFmpeg.
00009  *
00010  * FFmpeg is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or (at your option) any later version.
00014  *
00015  * FFmpeg is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with FFmpeg; if not, write to the Free Software
00022  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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     case PIX_FMT_RGB555BE:
00072         s->pixel_size = 2;
00073         break;
00074     case PIX_FMT_RGB24:
00075         s->pixel_size = 3;
00076         break;
00077     case PIX_FMT_ARGB:
00078         s->pixel_size = 4;
00079         break;
00080     default:
00081         av_log(avctx, AV_LOG_ERROR, "Unsupported colorspace.\n");
00082         break;
00083     }
00084     avctx->bits_per_coded_sample = s->pixel_size*8;
00085 
00086     s->rlecode_table = av_mallocz(s->avctx->width);
00087     s->skip_table    = av_mallocz(s->avctx->width);
00088     s->length_table  = av_mallocz((s->avctx->width + 1)*sizeof(int));
00089     if (!s->skip_table || !s->length_table || !s->rlecode_table) {
00090         av_log(avctx, AV_LOG_ERROR, "Error allocating memory.\n");
00091         return -1;
00092     }
00093     if (avpicture_alloc(&s->previous_frame, avctx->pix_fmt, avctx->width, avctx->height) < 0) {
00094         av_log(avctx, AV_LOG_ERROR, "Error allocating picture\n");
00095         return -1;
00096     }
00097 
00098     s->max_buf_size = s->avctx->width*s->avctx->height*s->pixel_size /* image base material */
00099                       + 15                                           /* header + footer */
00100                       + s->avctx->height*2                           /* skip code+rle end */
00101                       + s->avctx->width/MAX_RLE_BULK + 1             /* rle codes */;
00102     avctx->coded_frame = &s->frame;
00103     return 0;
00104 }
00105 
00109 static void qtrle_encode_line(QtrleEncContext *s, AVFrame *p, int line, uint8_t **buf)
00110 {
00111     int width=s->avctx->width;
00112     int i;
00113     signed char rlecode;
00114 
00115     /* We will use it to compute the best bulk copy sequence */
00116     unsigned int bulkcount;
00117     /* This will be the number of pixels equal to the preivous frame one's
00118      * starting from the ith pixel */
00119     unsigned int skipcount;
00120     /* This will be the number of consecutive equal pixels in the current
00121      * frame, starting from the ith one also */
00122     unsigned int av_uninit(repeatcount);
00123 
00124     /* The cost of the three different possibilities */
00125     int total_bulk_cost;
00126     int total_skip_cost;
00127     int total_repeat_cost;
00128 
00129     int temp_cost;
00130     int j;
00131 
00132     uint8_t *this_line = p->               data[0] + line*p->               linesize[0] +
00133         (width - 1)*s->pixel_size;
00134     uint8_t *prev_line = s->previous_frame.data[0] + line*s->previous_frame.linesize[0] +
00135         (width - 1)*s->pixel_size;
00136 
00137     s->length_table[width] = 0;
00138     skipcount = 0;
00139 
00140     for (i = width - 1; i >= 0; i--) {
00141 
00142         if (!s->frame.key_frame && !memcmp(this_line, prev_line, s->pixel_size))
00143             skipcount = FFMIN(skipcount + 1, MAX_RLE_SKIP);
00144         else
00145             skipcount = 0;
00146 
00147         total_skip_cost  = s->length_table[i + skipcount] + 2;
00148         s->skip_table[i] = skipcount;
00149 
00150 
00151         if (i < width - 1 && !memcmp(this_line, this_line + s->pixel_size, s->pixel_size))
00152             repeatcount = FFMIN(repeatcount + 1, MAX_RLE_REPEAT);
00153         else
00154             repeatcount = 1;
00155 
00156         total_repeat_cost = s->length_table[i + repeatcount] + 1 + s->pixel_size;
00157 
00158         /* skip code is free for the first pixel, it costs one byte for repeat and bulk copy
00159          * so let's make it aware */
00160         if (i == 0) {
00161             total_skip_cost--;
00162             total_repeat_cost++;
00163         }
00164 
00165         if (repeatcount > 1 && (skipcount == 0 || total_repeat_cost < total_skip_cost)) {
00166             /* repeat is the best */
00167             s->length_table[i]  = total_repeat_cost;
00168             s->rlecode_table[i] = -repeatcount;
00169         }
00170         else if (skipcount > 0) {
00171             /* skip is the best choice here */
00172             s->length_table[i]  = total_skip_cost;
00173             s->rlecode_table[i] = 0;
00174         }
00175         else {
00176             /* We cannot do neither skip nor repeat
00177              * thus we search for the best bulk copy to do */
00178 
00179             int limit = FFMIN(width - i, MAX_RLE_BULK);
00180 
00181             temp_cost = 1 + s->pixel_size + !i;
00182             total_bulk_cost = INT_MAX;
00183 
00184             for (j = 1; j <= limit; j++) {
00185                 if (s->length_table[i + j] + temp_cost < total_bulk_cost) {
00186                     /* We have found a better bulk copy ... */
00187                     total_bulk_cost = s->length_table[i + j] + temp_cost;
00188                     bulkcount = j;
00189                 }
00190                 temp_cost += s->pixel_size;
00191             }
00192 
00193             s->length_table[i]  = total_bulk_cost;
00194             s->rlecode_table[i] = bulkcount;
00195         }
00196 
00197         this_line -= s->pixel_size;
00198         prev_line -= s->pixel_size;
00199     }
00200 
00201     /* Good ! Now we have the best sequence for this line, let's ouput it */
00202 
00203     /* We do a special case for the first pixel so that we avoid testing it in
00204      * the whole loop */
00205 
00206     i=0;
00207     this_line = p->               data[0] + line*p->linesize[0];
00208 
00209     if (s->rlecode_table[0] == 0) {
00210         bytestream_put_byte(buf, s->skip_table[0] + 1);
00211         i += s->skip_table[0];
00212     }
00213     else bytestream_put_byte(buf, 1);
00214 
00215 
00216     while (i < width) {
00217         rlecode = s->rlecode_table[i];
00218         bytestream_put_byte(buf, rlecode);
00219         if (rlecode == 0) {
00220             /* Write a skip sequence */
00221             bytestream_put_byte(buf, s->skip_table[i] + 1);
00222             i += s->skip_table[i];
00223         }
00224         else if (rlecode > 0) {
00225             /* bulk copy */
00226             bytestream_put_buffer(buf, this_line + i*s->pixel_size, rlecode*s->pixel_size);
00227             i += rlecode;
00228         }
00229         else {
00230             /* repeat the bits */
00231             bytestream_put_buffer(buf, this_line + i*s->pixel_size, s->pixel_size);
00232             i -= rlecode;
00233         }
00234     }
00235     bytestream_put_byte(buf, -1); // end RLE line
00236 }
00237 
00239 static int encode_frame(QtrleEncContext *s, AVFrame *p, uint8_t *buf)
00240 {
00241     int i;
00242     int start_line = 0;
00243     int end_line = s->avctx->height;
00244     uint8_t *orig_buf = buf;
00245 
00246     if (!s->frame.key_frame) {
00247         unsigned line_size = s->avctx->width * s->pixel_size;
00248         for (start_line = 0; start_line < s->avctx->height; start_line++)
00249             if (memcmp(p->data[0] + start_line*p->linesize[0],
00250                        s->previous_frame.data[0] + start_line*s->previous_frame.linesize[0],
00251                        line_size))
00252                 break;
00253 
00254         for (end_line=s->avctx->height; end_line > start_line; end_line--)
00255             if (memcmp(p->data[0] + (end_line - 1)*p->linesize[0],
00256                        s->previous_frame.data[0] + (end_line - 1)*s->previous_frame.linesize[0],
00257                        line_size))
00258                 break;
00259     }
00260 
00261     bytestream_put_be32(&buf, 0);                         // CHUNK SIZE, patched later
00262 
00263     if ((start_line == 0 && end_line == s->avctx->height) || start_line == s->avctx->height)
00264         bytestream_put_be16(&buf, 0);                     // header
00265     else {
00266         bytestream_put_be16(&buf, 8);                     // header
00267         bytestream_put_be16(&buf, start_line);            // starting line
00268         bytestream_put_be16(&buf, 0);                     // unknown
00269         bytestream_put_be16(&buf, end_line - start_line); // lines to update
00270         bytestream_put_be16(&buf, 0);                     // unknown
00271     }
00272     for (i = start_line; i < end_line; i++)
00273         qtrle_encode_line(s, p, i, &buf);
00274 
00275     bytestream_put_byte(&buf, 0);                         // zero skip code = frame finished
00276     AV_WB32(orig_buf, buf - orig_buf);                    // patch the chunk size
00277     return buf - orig_buf;
00278 }
00279 
00280 static int qtrle_encode_frame(AVCodecContext *avctx, uint8_t *buf, int buf_size, void *data)
00281 {
00282     QtrleEncContext * const s = avctx->priv_data;
00283     AVFrame *pict = data;
00284     AVFrame * const p = &s->frame;
00285     int chunksize;
00286 
00287     *p = *pict;
00288 
00289     if (buf_size < s->max_buf_size) {
00290         /* Upper bound check for compressed data */
00291         av_log(avctx, AV_LOG_ERROR, "buf_size %d <  %d\n", buf_size, s->max_buf_size);
00292         return -1;
00293     }
00294 
00295     if (avctx->gop_size == 0 || (s->avctx->frame_number % avctx->gop_size) == 0) {
00296         /* I-Frame */
00297         p->pict_type = FF_I_TYPE;
00298         p->key_frame = 1;
00299     } else {
00300         /* P-Frame */
00301         p->pict_type = FF_P_TYPE;
00302         p->key_frame = 0;
00303     }
00304 
00305     chunksize = encode_frame(s, pict, buf);
00306 
00307     /* save the current frame */
00308     av_picture_copy(&s->previous_frame, (AVPicture *)p, avctx->pix_fmt, avctx->width, avctx->height);
00309     return chunksize;
00310 }
00311 
00312 static av_cold int qtrle_encode_end(AVCodecContext *avctx)
00313 {
00314     QtrleEncContext *s = avctx->priv_data;
00315 
00316     avpicture_free(&s->previous_frame);
00317     av_free(s->rlecode_table);
00318     av_free(s->length_table);
00319     av_free(s->skip_table);
00320     return 0;
00321 }
00322 
00323 AVCodec qtrle_encoder = {
00324     "qtrle",
00325     AVMEDIA_TYPE_VIDEO,
00326     CODEC_ID_QTRLE,
00327     sizeof(QtrleEncContext),
00328     qtrle_encode_init,
00329     qtrle_encode_frame,
00330     qtrle_encode_end,
00331     .pix_fmts = (const enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGB555BE, PIX_FMT_ARGB, PIX_FMT_NONE},
00332     .long_name = NULL_IF_CONFIG_SMALL("QuickTime Animation (RLE) video"),
00333 };

Generated on Fri Sep 16 2011 17:17:42 for FFmpeg by  doxygen 1.7.1