Libav 0.7.1
|
00001 /* 00002 * AVS video decoder. 00003 * Copyright (c) 2006 Aurelien Jacobs <aurel@gnuage.org> 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 00022 #include "avcodec.h" 00023 #include "get_bits.h" 00024 00025 00026 typedef struct { 00027 AVFrame picture; 00028 } AvsContext; 00029 00030 typedef enum { 00031 AVS_VIDEO = 0x01, 00032 AVS_AUDIO = 0x02, 00033 AVS_PALETTE = 0x03, 00034 AVS_GAME_DATA = 0x04, 00035 } AvsBlockType; 00036 00037 typedef enum { 00038 AVS_I_FRAME = 0x00, 00039 AVS_P_FRAME_3X3 = 0x01, 00040 AVS_P_FRAME_2X2 = 0x02, 00041 AVS_P_FRAME_2X3 = 0x03, 00042 } AvsVideoSubType; 00043 00044 00045 static int 00046 avs_decode_frame(AVCodecContext * avctx, 00047 void *data, int *data_size, AVPacket *avpkt) 00048 { 00049 const uint8_t *buf = avpkt->data; 00050 const uint8_t *buf_end = avpkt->data + avpkt->size; 00051 int buf_size = avpkt->size; 00052 AvsContext *const avs = avctx->priv_data; 00053 AVFrame *picture = data; 00054 AVFrame *const p = (AVFrame *) & avs->picture; 00055 const uint8_t *table, *vect; 00056 uint8_t *out; 00057 int i, j, x, y, stride, vect_w = 3, vect_h = 3; 00058 AvsVideoSubType sub_type; 00059 AvsBlockType type; 00060 GetBitContext change_map; 00061 00062 if (avctx->reget_buffer(avctx, p)) { 00063 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n"); 00064 return -1; 00065 } 00066 p->reference = 1; 00067 p->pict_type = AV_PICTURE_TYPE_P; 00068 p->key_frame = 0; 00069 00070 out = avs->picture.data[0]; 00071 stride = avs->picture.linesize[0]; 00072 00073 if (buf_end - buf < 4) 00074 return AVERROR_INVALIDDATA; 00075 sub_type = buf[0]; 00076 type = buf[1]; 00077 buf += 4; 00078 00079 if (type == AVS_PALETTE) { 00080 int first, last; 00081 uint32_t *pal = (uint32_t *) avs->picture.data[1]; 00082 00083 first = AV_RL16(buf); 00084 last = first + AV_RL16(buf + 2); 00085 if (first >= 256 || last > 256 || buf_end - buf < 4 + 4 + 3 * (last - first)) 00086 return AVERROR_INVALIDDATA; 00087 buf += 4; 00088 for (i=first; i<last; i++, buf+=3) 00089 pal[i] = (buf[0] << 18) | (buf[1] << 10) | (buf[2] << 2); 00090 00091 sub_type = buf[0]; 00092 type = buf[1]; 00093 buf += 4; 00094 } 00095 00096 if (type != AVS_VIDEO) 00097 return -1; 00098 00099 switch (sub_type) { 00100 case AVS_I_FRAME: 00101 p->pict_type = AV_PICTURE_TYPE_I; 00102 p->key_frame = 1; 00103 case AVS_P_FRAME_3X3: 00104 vect_w = 3; 00105 vect_h = 3; 00106 break; 00107 00108 case AVS_P_FRAME_2X2: 00109 vect_w = 2; 00110 vect_h = 2; 00111 break; 00112 00113 case AVS_P_FRAME_2X3: 00114 vect_w = 2; 00115 vect_h = 3; 00116 break; 00117 00118 default: 00119 return -1; 00120 } 00121 00122 if (buf_end - buf < 256 * vect_w * vect_h) 00123 return AVERROR_INVALIDDATA; 00124 table = buf + (256 * vect_w * vect_h); 00125 if (sub_type != AVS_I_FRAME) { 00126 int map_size = ((318 / vect_w + 7) / 8) * (198 / vect_h); 00127 if (buf_end - table < map_size) 00128 return AVERROR_INVALIDDATA; 00129 init_get_bits(&change_map, table, map_size * 8); 00130 table += map_size; 00131 } 00132 00133 for (y=0; y<198; y+=vect_h) { 00134 for (x=0; x<318; x+=vect_w) { 00135 if (sub_type == AVS_I_FRAME || get_bits1(&change_map)) { 00136 if (buf_end - table < 1) 00137 return AVERROR_INVALIDDATA; 00138 vect = &buf[*table++ * (vect_w * vect_h)]; 00139 for (j=0; j<vect_w; j++) { 00140 out[(y + 0) * stride + x + j] = vect[(0 * vect_w) + j]; 00141 out[(y + 1) * stride + x + j] = vect[(1 * vect_w) + j]; 00142 if (vect_h == 3) 00143 out[(y + 2) * stride + x + j] = 00144 vect[(2 * vect_w) + j]; 00145 } 00146 } 00147 } 00148 if (sub_type != AVS_I_FRAME) 00149 align_get_bits(&change_map); 00150 } 00151 00152 *picture = *(AVFrame *) & avs->picture; 00153 *data_size = sizeof(AVPicture); 00154 00155 return buf_size; 00156 } 00157 00158 static av_cold int avs_decode_init(AVCodecContext * avctx) 00159 { 00160 avctx->pix_fmt = PIX_FMT_PAL8; 00161 avcodec_set_dimensions(avctx, 318, 198); 00162 return 0; 00163 } 00164 00165 AVCodec ff_avs_decoder = { 00166 "avs", 00167 AVMEDIA_TYPE_VIDEO, 00168 CODEC_ID_AVS, 00169 sizeof(AvsContext), 00170 avs_decode_init, 00171 NULL, 00172 NULL, 00173 avs_decode_frame, 00174 CODEC_CAP_DR1, 00175 .long_name = NULL_IF_CONFIG_SMALL("AVS (Audio Video Standard) video"), 00176 };