Libav 0.7.1
|
00001 /* 00002 * ATI VCR1 codec 00003 * Copyright (c) 2003 Michael Niedermayer 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 00027 #include "avcodec.h" 00028 #include "dsputil.h" 00029 00030 //#undef NDEBUG 00031 //#include <assert.h> 00032 00033 /* Disable the encoder. */ 00034 #undef CONFIG_VCR1_ENCODER 00035 #define CONFIG_VCR1_ENCODER 0 00036 00037 typedef struct VCR1Context{ 00038 AVCodecContext *avctx; 00039 AVFrame picture; 00040 int delta[16]; 00041 int offset[4]; 00042 } VCR1Context; 00043 00044 static int decode_frame(AVCodecContext *avctx, 00045 void *data, int *data_size, 00046 AVPacket *avpkt) 00047 { 00048 const uint8_t *buf = avpkt->data; 00049 int buf_size = avpkt->size; 00050 VCR1Context * const a = avctx->priv_data; 00051 AVFrame *picture = data; 00052 AVFrame * const p= (AVFrame*)&a->picture; 00053 const uint8_t *bytestream= buf; 00054 int i, x, y; 00055 00056 if(p->data[0]) 00057 avctx->release_buffer(avctx, p); 00058 00059 p->reference= 0; 00060 if(avctx->get_buffer(avctx, p) < 0){ 00061 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); 00062 return -1; 00063 } 00064 p->pict_type= AV_PICTURE_TYPE_I; 00065 p->key_frame= 1; 00066 00067 for(i=0; i<16; i++){ 00068 a->delta[i]= *(bytestream++); 00069 bytestream++; 00070 } 00071 00072 for(y=0; y<avctx->height; y++){ 00073 int offset; 00074 uint8_t *luma= &a->picture.data[0][ y*a->picture.linesize[0] ]; 00075 00076 if((y&3) == 0){ 00077 uint8_t *cb= &a->picture.data[1][ (y>>2)*a->picture.linesize[1] ]; 00078 uint8_t *cr= &a->picture.data[2][ (y>>2)*a->picture.linesize[2] ]; 00079 00080 for(i=0; i<4; i++) 00081 a->offset[i]= *(bytestream++); 00082 00083 offset= a->offset[0] - a->delta[ bytestream[2]&0xF ]; 00084 for(x=0; x<avctx->width; x+=4){ 00085 luma[0]=( offset += a->delta[ bytestream[2]&0xF ]); 00086 luma[1]=( offset += a->delta[ bytestream[2]>>4 ]); 00087 luma[2]=( offset += a->delta[ bytestream[0]&0xF ]); 00088 luma[3]=( offset += a->delta[ bytestream[0]>>4 ]); 00089 luma += 4; 00090 00091 *(cb++) = bytestream[3]; 00092 *(cr++) = bytestream[1]; 00093 00094 bytestream+= 4; 00095 } 00096 }else{ 00097 offset= a->offset[y&3] - a->delta[ bytestream[2]&0xF ]; 00098 00099 for(x=0; x<avctx->width; x+=8){ 00100 luma[0]=( offset += a->delta[ bytestream[2]&0xF ]); 00101 luma[1]=( offset += a->delta[ bytestream[2]>>4 ]); 00102 luma[2]=( offset += a->delta[ bytestream[3]&0xF ]); 00103 luma[3]=( offset += a->delta[ bytestream[3]>>4 ]); 00104 luma[4]=( offset += a->delta[ bytestream[0]&0xF ]); 00105 luma[5]=( offset += a->delta[ bytestream[0]>>4 ]); 00106 luma[6]=( offset += a->delta[ bytestream[1]&0xF ]); 00107 luma[7]=( offset += a->delta[ bytestream[1]>>4 ]); 00108 luma += 8; 00109 bytestream+= 4; 00110 } 00111 } 00112 } 00113 00114 *picture= *(AVFrame*)&a->picture; 00115 *data_size = sizeof(AVPicture); 00116 00117 emms_c(); 00118 00119 return buf_size; 00120 } 00121 00122 #if CONFIG_VCR1_ENCODER 00123 static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){ 00124 VCR1Context * const a = avctx->priv_data; 00125 AVFrame *pict = data; 00126 AVFrame * const p= (AVFrame*)&a->picture; 00127 int size; 00128 00129 *p = *pict; 00130 p->pict_type= AV_PICTURE_TYPE_I; 00131 p->key_frame= 1; 00132 00133 emms_c(); 00134 00135 align_put_bits(&a->pb); 00136 while(get_bit_count(&a->pb)&31) 00137 put_bits(&a->pb, 8, 0); 00138 00139 size= get_bit_count(&a->pb)/32; 00140 00141 return size*4; 00142 } 00143 #endif 00144 00145 static av_cold void common_init(AVCodecContext *avctx){ 00146 VCR1Context * const a = avctx->priv_data; 00147 00148 avctx->coded_frame= (AVFrame*)&a->picture; 00149 a->avctx= avctx; 00150 } 00151 00152 static av_cold int decode_init(AVCodecContext *avctx){ 00153 00154 common_init(avctx); 00155 00156 avctx->pix_fmt= PIX_FMT_YUV410P; 00157 00158 return 0; 00159 } 00160 00161 static av_cold int decode_end(AVCodecContext *avctx){ 00162 VCR1Context *s = avctx->priv_data; 00163 00164 if (s->picture.data[0]) 00165 avctx->release_buffer(avctx, &s->picture); 00166 00167 return 0; 00168 } 00169 00170 #if CONFIG_VCR1_ENCODER 00171 static av_cold int encode_init(AVCodecContext *avctx){ 00172 00173 common_init(avctx); 00174 00175 return 0; 00176 } 00177 #endif 00178 00179 AVCodec ff_vcr1_decoder = { 00180 "vcr1", 00181 AVMEDIA_TYPE_VIDEO, 00182 CODEC_ID_VCR1, 00183 sizeof(VCR1Context), 00184 decode_init, 00185 NULL, 00186 decode_end, 00187 decode_frame, 00188 CODEC_CAP_DR1, 00189 .long_name = NULL_IF_CONFIG_SMALL("ATI VCR1"), 00190 }; 00191 00192 #if CONFIG_VCR1_ENCODER 00193 AVCodec ff_vcr1_encoder = { 00194 "vcr1", 00195 AVMEDIA_TYPE_VIDEO, 00196 CODEC_ID_VCR1, 00197 sizeof(VCR1Context), 00198 encode_init, 00199 encode_frame, 00200 //encode_end, 00201 .long_name = NULL_IF_CONFIG_SMALL("ATI VCR1"), 00202 }; 00203 #endif