Revision ce10e858

View differences:

libavcodec/qtrleenc.c
39 39
    int pixel_size;
40 40
    AVPicture previous_frame;
41 41
    unsigned int max_buf_size;
42
    int logical_width;
42 43
    /**
43 44
     * This array will contain at ith position the value of the best RLE code
44 45
     * if the line started at pixel i
......
67 68
        return -1;
68 69
    }
69 70
    s->avctx=avctx;
71
    s->logical_width=avctx->width;
70 72

  
71 73
    switch (avctx->pix_fmt) {
74
    case PIX_FMT_GRAY8:
75
        s->logical_width = avctx->width / 4;
76
        s->pixel_size = 4;
77
        break;
72 78
    case PIX_FMT_RGB555BE:
73 79
        s->pixel_size = 2;
74 80
        break;
......
82 88
        av_log(avctx, AV_LOG_ERROR, "Unsupported colorspace.\n");
83 89
        break;
84 90
    }
85
    avctx->bits_per_coded_sample = s->pixel_size*8;
91
    avctx->bits_per_coded_sample = avctx->pix_fmt == PIX_FMT_GRAY8 ? 40 : s->pixel_size*8;
86 92

  
87
    s->rlecode_table = av_mallocz(s->avctx->width);
88
    s->skip_table    = av_mallocz(s->avctx->width);
89
    s->length_table  = av_mallocz((s->avctx->width + 1)*sizeof(int));
93
    s->rlecode_table = av_mallocz(s->logical_width);
94
    s->skip_table    = av_mallocz(s->logical_width);
95
    s->length_table  = av_mallocz((s->logical_width + 1)*sizeof(int));
90 96
    if (!s->skip_table || !s->length_table || !s->rlecode_table) {
91 97
        av_log(avctx, AV_LOG_ERROR, "Error allocating memory.\n");
92 98
        return -1;
......
96 102
        return -1;
97 103
    }
98 104

  
99
    s->max_buf_size = s->avctx->width*s->avctx->height*s->pixel_size /* image base material */
100
                      + 15                                           /* header + footer */
101
                      + s->avctx->height*2                           /* skip code+rle end */
102
                      + s->avctx->width/MAX_RLE_BULK + 1             /* rle codes */;
105
    s->max_buf_size = s->logical_width*s->avctx->height*s->pixel_size /* image base material */
106
                      + 15                                            /* header + footer */
107
                      + s->avctx->height*2                            /* skip code+rle end */
108
                      + s->logical_width/MAX_RLE_BULK + 1             /* rle codes */;
103 109
    avctx->coded_frame = &s->frame;
104 110
    return 0;
105 111
}
......
109 115
 */
110 116
static void qtrle_encode_line(QtrleEncContext *s, AVFrame *p, int line, uint8_t **buf)
111 117
{
112
    int width=s->avctx->width;
118
    int width=s->logical_width;
113 119
    int i;
114 120
    signed char rlecode;
115 121

  
......
224 230
        }
225 231
        else if (rlecode > 0) {
226 232
            /* bulk copy */
227
            bytestream_put_buffer(buf, this_line + i*s->pixel_size, rlecode*s->pixel_size);
233
            if (s->avctx->pix_fmt == PIX_FMT_GRAY8) {
234
                // QT grayscale colorspace has 0=white and 255=black, we will
235
                // ignore the palette that is included in the AVFrame because
236
                // PIX_FMT_GRAY8 has defined color mapping
237
                for (int j = 0; j < rlecode*s->pixel_size; ++j)
238
                    bytestream_put_byte(buf, *(this_line + i*s->pixel_size + j) ^ 0xff);
239
            } else {
240
                bytestream_put_buffer(buf, this_line + i*s->pixel_size, rlecode*s->pixel_size);
241
            }
228 242
            i += rlecode;
229 243
        }
230 244
        else {
231 245
            /* repeat the bits */
232
            bytestream_put_buffer(buf, this_line + i*s->pixel_size, s->pixel_size);
246
            if (s->avctx->pix_fmt == PIX_FMT_GRAY8) {
247
                // QT grayscale colorspace has 0=white and 255=black, ...
248
                for (int j = 0; j < s->pixel_size; ++j)
249
                    bytestream_put_byte(buf, *(this_line + i*s->pixel_size + j) ^ 0xff);
250
            } else {
251
                bytestream_put_buffer(buf, this_line + i*s->pixel_size, s->pixel_size);
252
            }
233 253
            i -= rlecode;
234 254
        }
235 255
    }
......
245 265
    uint8_t *orig_buf = buf;
246 266

  
247 267
    if (!s->frame.key_frame) {
248
        unsigned line_size = s->avctx->width * s->pixel_size;
268
        unsigned line_size = s->logical_width * s->pixel_size;
249 269
        for (start_line = 0; start_line < s->avctx->height; start_line++)
250 270
            if (memcmp(p->data[0] + start_line*p->linesize[0],
251 271
                       s->previous_frame.data[0] + start_line*s->previous_frame.linesize[0],
......
329 349
    qtrle_encode_init,
330 350
    qtrle_encode_frame,
331 351
    qtrle_encode_end,
332
    .pix_fmts = (const enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGB555BE, PIX_FMT_ARGB, PIX_FMT_NONE},
352
    .pix_fmts = (const enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGB555BE, PIX_FMT_ARGB, PIX_FMT_GRAY8, PIX_FMT_NONE},
333 353
    .long_name = NULL_IF_CONFIG_SMALL("QuickTime Animation (RLE) video"),
334 354
};

Also available in: Unified diff