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

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 FFmpeg.
00006  *
00007  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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 #include <unistd.h>
00041 
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         switch (opcode & 0xe0) {
00130 
00131         /* Skip blocks */
00132         case 0x80:
00133             while (n_blocks--) {
00134               ADVANCE_BLOCK();
00135             }
00136             break;
00137 
00138         /* Fill blocks with one color */
00139         case 0xa0:
00140             colorA = AV_RB16 (&s->buf[stream_ptr]);
00141             stream_ptr += 2;
00142             while (n_blocks--) {
00143                 block_ptr = row_ptr + pixel_ptr;
00144                 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
00145                     for (pixel_x = 0; pixel_x < 4; pixel_x++){
00146                         pixels[block_ptr] = colorA;
00147                         block_ptr++;
00148                     }
00149                     block_ptr += row_inc;
00150                 }
00151                 ADVANCE_BLOCK();
00152             }
00153             break;
00154 
00155         /* Fill blocks with 4 colors */
00156         case 0xc0:
00157             colorA = AV_RB16 (&s->buf[stream_ptr]);
00158             stream_ptr += 2;
00159         case 0x20:
00160             colorB = AV_RB16 (&s->buf[stream_ptr]);
00161             stream_ptr += 2;
00162 
00163             /* sort out the colors */
00164             color4[0] = colorB;
00165             color4[1] = 0;
00166             color4[2] = 0;
00167             color4[3] = colorA;
00168 
00169             /* red components */
00170             ta = (colorA >> 10) & 0x1F;
00171             tb = (colorB >> 10) & 0x1F;
00172             color4[1] |= ((11 * ta + 21 * tb) >> 5) << 10;
00173             color4[2] |= ((21 * ta + 11 * tb) >> 5) << 10;
00174 
00175             /* green components */
00176             ta = (colorA >> 5) & 0x1F;
00177             tb = (colorB >> 5) & 0x1F;
00178             color4[1] |= ((11 * ta + 21 * tb) >> 5) << 5;
00179             color4[2] |= ((21 * ta + 11 * tb) >> 5) << 5;
00180 
00181             /* blue components */
00182             ta = colorA & 0x1F;
00183             tb = colorB & 0x1F;
00184             color4[1] |= ((11 * ta + 21 * tb) >> 5);
00185             color4[2] |= ((21 * ta + 11 * tb) >> 5);
00186 
00187             while (n_blocks--) {
00188                 block_ptr = row_ptr + pixel_ptr;
00189                 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
00190                     index = s->buf[stream_ptr++];
00191                     for (pixel_x = 0; pixel_x < 4; pixel_x++){
00192                         idx = (index >> (2 * (3 - pixel_x))) & 0x03;
00193                         pixels[block_ptr] = color4[idx];
00194                         block_ptr++;
00195                     }
00196                     block_ptr += row_inc;
00197                 }
00198                 ADVANCE_BLOCK();
00199             }
00200             break;
00201 
00202         /* Fill block with 16 colors */
00203         case 0x00:
00204             block_ptr = row_ptr + pixel_ptr;
00205             for (pixel_y = 0; pixel_y < 4; pixel_y++) {
00206                 for (pixel_x = 0; pixel_x < 4; pixel_x++){
00207                     /* We already have color of upper left pixel */
00208                     if ((pixel_y != 0) || (pixel_x !=0)) {
00209                         colorA = AV_RB16 (&s->buf[stream_ptr]);
00210                         stream_ptr += 2;
00211                     }
00212                     pixels[block_ptr] = colorA;
00213                     block_ptr++;
00214                 }
00215                 block_ptr += row_inc;
00216             }
00217             ADVANCE_BLOCK();
00218             break;
00219 
00220         /* Unknown opcode */
00221         default:
00222             av_log(s->avctx, AV_LOG_ERROR, "Unknown opcode %d in rpza chunk."
00223                  " Skip remaining %d bytes of chunk data.\n", opcode,
00224                  chunk_size - stream_ptr);
00225             return;
00226         } /* Opcode switch */
00227     }
00228 }
00229 
00230 static av_cold int rpza_decode_init(AVCodecContext *avctx)
00231 {
00232     RpzaContext *s = avctx->priv_data;
00233 
00234     s->avctx = avctx;
00235     avctx->pix_fmt = PIX_FMT_RGB555;
00236 
00237     s->frame.data[0] = NULL;
00238 
00239     return 0;
00240 }
00241 
00242 static int rpza_decode_frame(AVCodecContext *avctx,
00243                              void *data, int *data_size,
00244                              const uint8_t *buf, int buf_size)
00245 {
00246     RpzaContext *s = avctx->priv_data;
00247 
00248     s->buf = buf;
00249     s->size = buf_size;
00250 
00251     s->frame.reference = 1;
00252     s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
00253     if (avctx->reget_buffer(avctx, &s->frame)) {
00254         av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
00255         return -1;
00256     }
00257 
00258     rpza_decode_stream(s);
00259 
00260     *data_size = sizeof(AVFrame);
00261     *(AVFrame*)data = s->frame;
00262 
00263     /* always report that the buffer was completely consumed */
00264     return buf_size;
00265 }
00266 
00267 static av_cold int rpza_decode_end(AVCodecContext *avctx)
00268 {
00269     RpzaContext *s = avctx->priv_data;
00270 
00271     if (s->frame.data[0])
00272         avctx->release_buffer(avctx, &s->frame);
00273 
00274     return 0;
00275 }
00276 
00277 AVCodec rpza_decoder = {
00278     "rpza",
00279     CODEC_TYPE_VIDEO,
00280     CODEC_ID_RPZA,
00281     sizeof(RpzaContext),
00282     rpza_decode_init,
00283     NULL,
00284     rpza_decode_end,
00285     rpza_decode_frame,
00286     CODEC_CAP_DR1,
00287     .long_name = NULL_IF_CONFIG_SMALL("QuickTime video (RPZA)"),
00288 };

Generated on Tue Nov 4 2014 12:59:22 for ffmpeg by  doxygen 1.7.1