• 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_RGB555:
00072         s->pixel_size = 2;
00073         break;*/
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 /* image base material */
00096                       + 15                                           /* header + footer */
00097                       + s->avctx->height*2                           /* skip code+rle end */
00098                       + s->avctx->width/MAX_RLE_BULK + 1             /* rle codes */;
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     /* We will use it to compute the best bulk copy sequence */
00113     unsigned int bulkcount;
00114     /* This will be the number of pixels equal to the preivous frame one's
00115      * starting from the ith pixel */
00116     unsigned int skipcount;
00117     /* This will be the number of consecutive equal pixels in the current
00118      * frame, starting from the ith one also */
00119     unsigned int av_uninit(repeatcount);
00120 
00121     /* The cost of the three different possibilities */
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] +
00130         (width - 1)*s->pixel_size;
00131     uint8_t *prev_line = s->previous_frame.data[0] + line*s->previous_frame.linesize[0] +
00132         (width - 1)*s->pixel_size;
00133 
00134     s->length_table[width] = 0;
00135     skipcount = 0;
00136 
00137     for (i = width - 1; i >= 0; i--) {
00138 
00139         if (!s->frame.key_frame && !memcmp(this_line, prev_line, s->pixel_size))
00140             skipcount = FFMIN(skipcount + 1, MAX_RLE_SKIP);
00141         else
00142             skipcount = 0;
00143 
00144         total_skip_cost  = s->length_table[i + skipcount] + 2;
00145         s->skip_table[i] = skipcount;
00146 
00147 
00148         if (i < width - 1 && !memcmp(this_line, this_line + s->pixel_size, s->pixel_size))
00149             repeatcount = FFMIN(repeatcount + 1, MAX_RLE_REPEAT);
00150         else
00151             repeatcount = 1;
00152 
00153         total_repeat_cost = s->length_table[i + repeatcount] + 1 + s->pixel_size;
00154 
00155         /* skip code is free for the first pixel, it costs one byte for repeat and bulk copy
00156          * so let's make it aware */
00157         if (i == 0) {
00158             total_skip_cost--;
00159             total_repeat_cost++;
00160         }
00161 
00162         if (repeatcount > 1 && (skipcount == 0 || total_repeat_cost < total_skip_cost)) {
00163             /* repeat is the best */
00164             s->length_table[i]  = total_repeat_cost;
00165             s->rlecode_table[i] = -repeatcount;
00166         }
00167         else if (skipcount > 0) {
00168             /* skip is the best choice here */
00169             s->length_table[i]  = total_skip_cost;
00170             s->rlecode_table[i] = 0;
00171         }
00172         else {
00173             /* We cannot do neither skip nor repeat
00174              * thus we search for the best bulk copy to do */
00175 
00176             int limit = FFMIN(width - i, MAX_RLE_BULK);
00177 
00178             temp_cost = 1 + s->pixel_size + !i;
00179             total_bulk_cost = INT_MAX;
00180 
00181             for (j = 1; j <= limit; j++) {
00182                 if (s->length_table[i + j] + temp_cost < total_bulk_cost) {
00183                     /* We have found a better bulk copy ... */
00184                     total_bulk_cost = s->length_table[i + j] + temp_cost;
00185                     bulkcount = j;
00186                 }
00187                 temp_cost += s->pixel_size;
00188             }
00189 
00190             s->length_table[i]  = total_bulk_cost;
00191             s->rlecode_table[i] = bulkcount;
00192         }
00193 
00194         this_line -= s->pixel_size;
00195         prev_line -= s->pixel_size;
00196     }
00197 
00198     /* Good ! Now we have the best sequence for this line, let's ouput it */
00199 
00200     /* We do a special case for the first pixel so that we avoid testing it in
00201      * the whole loop */
00202 
00203     i=0;
00204     this_line = p->               data[0] + line*p->linesize[0];
00205     prev_line = s->previous_frame.data[0] + line*p->linesize[0];
00206 
00207     if (s->rlecode_table[0] == 0) {
00208         bytestream_put_byte(buf, s->skip_table[0] + 1);
00209         i += s->skip_table[0];
00210     }
00211     else bytestream_put_byte(buf, 1);
00212 
00213 
00214     while (i < width) {
00215         rlecode = s->rlecode_table[i];
00216         bytestream_put_byte(buf, rlecode);
00217         if (rlecode == 0) {
00218             /* Write a skip sequence */
00219             bytestream_put_byte(buf, s->skip_table[i] + 1);
00220             i += s->skip_table[i];
00221         }
00222         else if (rlecode > 0) {
00223             /* bulk copy */
00224             bytestream_put_buffer(buf, this_line + i*s->pixel_size, rlecode*s->pixel_size);
00225             i += rlecode;
00226         }
00227         else {
00228             /* repeat the bits */
00229             bytestream_put_buffer(buf, this_line + i*s->pixel_size, s->pixel_size);
00230             i -= rlecode;
00231         }
00232     }
00233     bytestream_put_byte(buf, -1); // end RLE line
00234 }
00235 
00237 static int encode_frame(QtrleEncContext *s, AVFrame *p, uint8_t *buf)
00238 {
00239     int i;
00240     int start_line = 0;
00241     int end_line = s->avctx->height;
00242     uint8_t *orig_buf = buf;
00243 
00244     if (!s->frame.key_frame) {
00245         unsigned line_size = s->avctx->width * s->pixel_size;
00246         for (start_line = 0; start_line < s->avctx->height; start_line++)
00247             if (memcmp(p->data[0] + start_line*p->linesize[0],
00248                        s->previous_frame.data[0] + start_line*s->previous_frame.linesize[0],
00249                        line_size))
00250                 break;
00251 
00252         for (end_line=s->avctx->height; end_line > start_line; end_line--)
00253             if (memcmp(p->data[0] + (end_line - 1)*p->linesize[0],
00254                        s->previous_frame.data[0] + (end_line - 1)*s->previous_frame.linesize[0],
00255                        line_size))
00256                 break;
00257     }
00258 
00259     bytestream_put_be32(&buf, 0);                         // CHUNK SIZE, patched later
00260 
00261     if ((start_line == 0 && end_line == s->avctx->height) || start_line == s->avctx->height)
00262         bytestream_put_be16(&buf, 0);                     // header
00263     else {
00264         bytestream_put_be16(&buf, 8);                     // header
00265         bytestream_put_be16(&buf, start_line);            // starting line
00266         bytestream_put_be16(&buf, 0);                     // unknown
00267         bytestream_put_be16(&buf, end_line - start_line); // lines to update
00268         bytestream_put_be16(&buf, 0);                     // unknown
00269     }
00270     for (i = start_line; i < end_line; i++)
00271         qtrle_encode_line(s, p, i, &buf);
00272 
00273     bytestream_put_byte(&buf, 0);                         // zero skip code = frame finished
00274     AV_WB32(orig_buf, buf - orig_buf);                    // patch the chunk size
00275     return buf - orig_buf;
00276 }
00277 
00278 static int qtrle_encode_frame(AVCodecContext *avctx, uint8_t *buf, int buf_size, void *data)
00279 {
00280     QtrleEncContext * const s = avctx->priv_data;
00281     AVFrame *pict = data;
00282     AVFrame * const p = &s->frame;
00283     int chunksize;
00284 
00285     *p = *pict;
00286 
00287     if (buf_size < s->max_buf_size) {
00288         /* Upper bound check for compressed data */
00289         av_log(avctx, AV_LOG_ERROR, "buf_size %d <  %d\n", buf_size, s->max_buf_size);
00290         return -1;
00291     }
00292 
00293     if (avctx->gop_size == 0 || (s->avctx->frame_number % avctx->gop_size) == 0) {
00294         /* I-Frame */
00295         p->pict_type = FF_I_TYPE;
00296         p->key_frame = 1;
00297     } else {
00298         /* P-Frame */
00299         p->pict_type = FF_P_TYPE;
00300         p->key_frame = 0;
00301     }
00302 
00303     chunksize = encode_frame(s, pict, buf);
00304 
00305     /* save the current frame */
00306     av_picture_copy(&s->previous_frame, (AVPicture *)p, avctx->pix_fmt, avctx->width, avctx->height);
00307     return chunksize;
00308 }
00309 
00310 static av_cold int qtrle_encode_end(AVCodecContext *avctx)
00311 {
00312     QtrleEncContext *s = avctx->priv_data;
00313 
00314     avpicture_free(&s->previous_frame);
00315     av_free(s->rlecode_table);
00316     av_free(s->length_table);
00317     av_free(s->skip_table);
00318     return 0;
00319 }
00320 
00321 AVCodec qtrle_encoder = {
00322     "qtrle",
00323     CODEC_TYPE_VIDEO,
00324     CODEC_ID_QTRLE,
00325     sizeof(QtrleEncContext),
00326     qtrle_encode_init,
00327     qtrle_encode_frame,
00328     qtrle_encode_end,
00329     .pix_fmts = (enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_NONE},
00330     .long_name = NULL_IF_CONFIG_SMALL("QuickTime Animation (RLE) video"),
00331 };

Generated on Sat Feb 16 2013 09:23:13 for ffmpeg by  doxygen 1.7.1