libavcodec/rpza.c
Go to the documentation of this file.
00001 /*
00002  * Quicktime Video (RPZA) Video Decoder
00003  * Copyright (C) 2003 the ffmpeg project
00004  *
00005  * This file is part of Libav.
00006  *
00007  * Libav is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * Libav is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with Libav; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00037 #include <stdio.h>
00038 #include <stdlib.h>
00039 #include <string.h>
00040 
00041 #include "libavutil/common.h"
00042 #include "libavutil/intreadwrite.h"
00043 #include "avcodec.h"
00044 
00045 typedef struct RpzaContext {
00046 
00047     AVCodecContext *avctx;
00048     AVFrame frame;
00049 
00050     const unsigned char *buf;
00051     int size;
00052 
00053 } RpzaContext;
00054 
00055 #define ADVANCE_BLOCK() \
00056 { \
00057     pixel_ptr += 4; \
00058     if (pixel_ptr >= width) \
00059     { \
00060         pixel_ptr = 0; \
00061         row_ptr += stride * 4; \
00062     } \
00063     total_blocks--; \
00064     if (total_blocks < 0) \
00065     { \
00066         av_log(s->avctx, AV_LOG_ERROR, "warning: block counter just went negative (this should not happen)\n"); \
00067         return; \
00068     } \
00069 }
00070 
00071 static void rpza_decode_stream(RpzaContext *s)
00072 {
00073     int width = s->avctx->width;
00074     int stride = s->frame.linesize[0] / 2;
00075     int row_inc = stride - 4;
00076     int stream_ptr = 0;
00077     int chunk_size;
00078     unsigned char opcode;
00079     int n_blocks;
00080     unsigned short colorA = 0, colorB;
00081     unsigned short color4[4];
00082     unsigned char index, idx;
00083     unsigned short ta, tb;
00084     unsigned short *pixels = (unsigned short *)s->frame.data[0];
00085 
00086     int row_ptr = 0;
00087     int pixel_ptr = 0;
00088     int block_ptr;
00089     int pixel_x, pixel_y;
00090     int total_blocks;
00091 
00092     /* First byte is always 0xe1. Warn if it's different */
00093     if (s->buf[stream_ptr] != 0xe1)
00094         av_log(s->avctx, AV_LOG_ERROR, "First chunk byte is 0x%02x instead of 0xe1\n",
00095             s->buf[stream_ptr]);
00096 
00097     /* Get chunk size, ingnoring first byte */
00098     chunk_size = AV_RB32(&s->buf[stream_ptr]) & 0x00FFFFFF;
00099     stream_ptr += 4;
00100 
00101     /* If length mismatch use size from MOV file and try to decode anyway */
00102     if (chunk_size != s->size)
00103         av_log(s->avctx, AV_LOG_ERROR, "MOV chunk size != encoded chunk size; using MOV chunk size\n");
00104 
00105     chunk_size = s->size;
00106 
00107     /* Number of 4x4 blocks in frame. */
00108     total_blocks = ((s->avctx->width + 3) / 4) * ((s->avctx->height + 3) / 4);
00109 
00110     /* Process chunk data */
00111     while (stream_ptr < chunk_size) {
00112         opcode = s->buf[stream_ptr++]; /* Get opcode */
00113 
00114         n_blocks = (opcode & 0x1f) + 1; /* Extract block counter from opcode */
00115 
00116         /* If opcode MSbit is 0, we need more data to decide what to do */
00117         if ((opcode & 0x80) == 0) {
00118             colorA = (opcode << 8) | (s->buf[stream_ptr++]);
00119             opcode = 0;
00120             if ((s->buf[stream_ptr] & 0x80) != 0) {
00121                 /* Must behave as opcode 110xxxxx, using colorA computed
00122                  * above. Use fake opcode 0x20 to enter switch block at
00123                  * the right place */
00124                 opcode = 0x20;
00125                 n_blocks = 1;
00126             }
00127         }
00128 
00129         n_blocks = FFMIN(n_blocks, total_blocks);
00130 
00131         switch (opcode & 0xe0) {
00132 
00133         /* Skip blocks */
00134         case 0x80:
00135             while (n_blocks--) {
00136               ADVANCE_BLOCK();
00137             }
00138             break;
00139 
00140         /* Fill blocks with one color */
00141         case 0xa0:
00142             colorA = AV_RB16 (&s->buf[stream_ptr]);
00143             stream_ptr += 2;
00144             while (n_blocks--) {
00145                 block_ptr = row_ptr + pixel_ptr;
00146                 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
00147                     for (pixel_x = 0; pixel_x < 4; pixel_x++){
00148                         pixels[block_ptr] = colorA;
00149                         block_ptr++;
00150                     }
00151                     block_ptr += row_inc;
00152                 }
00153                 ADVANCE_BLOCK();
00154             }
00155             break;
00156 
00157         /* Fill blocks with 4 colors */
00158         case 0xc0:
00159             colorA = AV_RB16 (&s->buf[stream_ptr]);
00160             stream_ptr += 2;
00161         case 0x20:
00162             colorB = AV_RB16 (&s->buf[stream_ptr]);
00163             stream_ptr += 2;
00164 
00165             /* sort out the colors */
00166             color4[0] = colorB;
00167             color4[1] = 0;
00168             color4[2] = 0;
00169             color4[3] = colorA;
00170 
00171             /* red components */
00172             ta = (colorA >> 10) & 0x1F;
00173             tb = (colorB >> 10) & 0x1F;
00174             color4[1] |= ((11 * ta + 21 * tb) >> 5) << 10;
00175             color4[2] |= ((21 * ta + 11 * tb) >> 5) << 10;
00176 
00177             /* green components */
00178             ta = (colorA >> 5) & 0x1F;
00179             tb = (colorB >> 5) & 0x1F;
00180             color4[1] |= ((11 * ta + 21 * tb) >> 5) << 5;
00181             color4[2] |= ((21 * ta + 11 * tb) >> 5) << 5;
00182 
00183             /* blue components */
00184             ta = colorA & 0x1F;
00185             tb = colorB & 0x1F;
00186             color4[1] |= ((11 * ta + 21 * tb) >> 5);
00187             color4[2] |= ((21 * ta + 11 * tb) >> 5);
00188 
00189             if (s->size - stream_ptr < n_blocks * 4)
00190                 return;
00191             while (n_blocks--) {
00192                 block_ptr = row_ptr + pixel_ptr;
00193                 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
00194                     index = s->buf[stream_ptr++];
00195                     for (pixel_x = 0; pixel_x < 4; pixel_x++){
00196                         idx = (index >> (2 * (3 - pixel_x))) & 0x03;
00197                         pixels[block_ptr] = color4[idx];
00198                         block_ptr++;
00199                     }
00200                     block_ptr += row_inc;
00201                 }
00202                 ADVANCE_BLOCK();
00203             }
00204             break;
00205 
00206         /* Fill block with 16 colors */
00207         case 0x00:
00208             if (s->size - stream_ptr < 30)
00209                 return;
00210             block_ptr = row_ptr + pixel_ptr;
00211             for (pixel_y = 0; pixel_y < 4; pixel_y++) {
00212                 for (pixel_x = 0; pixel_x < 4; pixel_x++){
00213                     /* We already have color of upper left pixel */
00214                     if ((pixel_y != 0) || (pixel_x !=0)) {
00215                         colorA = AV_RB16 (&s->buf[stream_ptr]);
00216                         stream_ptr += 2;
00217                     }
00218                     pixels[block_ptr] = colorA;
00219                     block_ptr++;
00220                 }
00221                 block_ptr += row_inc;
00222             }
00223             ADVANCE_BLOCK();
00224             break;
00225 
00226         /* Unknown opcode */
00227         default:
00228             av_log(s->avctx, AV_LOG_ERROR, "Unknown opcode %d in rpza chunk."
00229                  " Skip remaining %d bytes of chunk data.\n", opcode,
00230                  chunk_size - stream_ptr);
00231             return;
00232         } /* Opcode switch */
00233     }
00234 }
00235 
00236 static av_cold int rpza_decode_init(AVCodecContext *avctx)
00237 {
00238     RpzaContext *s = avctx->priv_data;
00239 
00240     s->avctx = avctx;
00241     avctx->pix_fmt = PIX_FMT_RGB555;
00242 
00243     s->frame.data[0] = NULL;
00244 
00245     return 0;
00246 }
00247 
00248 static int rpza_decode_frame(AVCodecContext *avctx,
00249                              void *data, int *data_size,
00250                              AVPacket *avpkt)
00251 {
00252     const uint8_t *buf = avpkt->data;
00253     int buf_size = avpkt->size;
00254     RpzaContext *s = avctx->priv_data;
00255 
00256     s->buf = buf;
00257     s->size = buf_size;
00258 
00259     s->frame.reference = 1;
00260     s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
00261     if (avctx->reget_buffer(avctx, &s->frame)) {
00262         av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
00263         return -1;
00264     }
00265 
00266     rpza_decode_stream(s);
00267 
00268     *data_size = sizeof(AVFrame);
00269     *(AVFrame*)data = s->frame;
00270 
00271     /* always report that the buffer was completely consumed */
00272     return buf_size;
00273 }
00274 
00275 static av_cold int rpza_decode_end(AVCodecContext *avctx)
00276 {
00277     RpzaContext *s = avctx->priv_data;
00278 
00279     if (s->frame.data[0])
00280         avctx->release_buffer(avctx, &s->frame);
00281 
00282     return 0;
00283 }
00284 
00285 AVCodec ff_rpza_decoder = {
00286     .name           = "rpza",
00287     .type           = AVMEDIA_TYPE_VIDEO,
00288     .id             = CODEC_ID_RPZA,
00289     .priv_data_size = sizeof(RpzaContext),
00290     .init           = rpza_decode_init,
00291     .close          = rpza_decode_end,
00292     .decode         = rpza_decode_frame,
00293     .capabilities   = CODEC_CAP_DR1,
00294     .long_name = NULL_IF_CONFIG_SMALL("QuickTime video (RPZA)"),
00295 };