00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #include "avcodec.h"
00028 #include "dsputil.h"
00029 #include "libavutil/internal.h"
00030
00031 typedef struct VCR1Context {
00032 AVFrame picture;
00033 int delta[16];
00034 int offset[4];
00035 } VCR1Context;
00036
00037 static av_cold int vcr1_common_init(AVCodecContext *avctx)
00038 {
00039 VCR1Context *const a = avctx->priv_data;
00040
00041 avctx->coded_frame = &a->picture;
00042 avcodec_get_frame_defaults(&a->picture);
00043
00044 return 0;
00045 }
00046
00047 static av_cold int vcr1_decode_init(AVCodecContext *avctx)
00048 {
00049 vcr1_common_init(avctx);
00050
00051 avctx->pix_fmt = PIX_FMT_YUV410P;
00052
00053 return 0;
00054 }
00055
00056 static av_cold int vcr1_decode_end(AVCodecContext *avctx)
00057 {
00058 VCR1Context *s = avctx->priv_data;
00059
00060 if (s->picture.data[0])
00061 avctx->release_buffer(avctx, &s->picture);
00062
00063 return 0;
00064 }
00065
00066 static int vcr1_decode_frame(AVCodecContext *avctx, void *data,
00067 int *data_size, AVPacket *avpkt)
00068 {
00069 const uint8_t *buf = avpkt->data;
00070 int buf_size = avpkt->size;
00071 VCR1Context *const a = avctx->priv_data;
00072 AVFrame *picture = data;
00073 AVFrame *const p = &a->picture;
00074 const uint8_t *bytestream = buf;
00075 int i, x, y;
00076
00077 if (p->data[0])
00078 avctx->release_buffer(avctx, p);
00079
00080 if(buf_size < 16 + avctx->height + avctx->width*avctx->height*5/8){
00081 av_log(avctx, AV_LOG_ERROR, "Insufficient input data.\n");
00082 return AVERROR(EINVAL);
00083 }
00084
00085 p->reference = 0;
00086 if (avctx->get_buffer(avctx, p) < 0) {
00087 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00088 return -1;
00089 }
00090 p->pict_type = AV_PICTURE_TYPE_I;
00091 p->key_frame = 1;
00092
00093 for (i = 0; i < 16; i++) {
00094 a->delta[i] = *bytestream++;
00095 bytestream++;
00096 }
00097
00098 for (y = 0; y < avctx->height; y++) {
00099 int offset;
00100 uint8_t *luma = &a->picture.data[0][y * a->picture.linesize[0]];
00101
00102 if ((y & 3) == 0) {
00103 uint8_t *cb = &a->picture.data[1][(y >> 2) * a->picture.linesize[1]];
00104 uint8_t *cr = &a->picture.data[2][(y >> 2) * a->picture.linesize[2]];
00105
00106 for (i = 0; i < 4; i++)
00107 a->offset[i] = *bytestream++;
00108
00109 offset = a->offset[0] - a->delta[bytestream[2] & 0xF];
00110 for (x = 0; x < avctx->width; x += 4) {
00111 luma[0] = offset += a->delta[bytestream[2] & 0xF];
00112 luma[1] = offset += a->delta[bytestream[2] >> 4];
00113 luma[2] = offset += a->delta[bytestream[0] & 0xF];
00114 luma[3] = offset += a->delta[bytestream[0] >> 4];
00115 luma += 4;
00116
00117 *cb++ = bytestream[3];
00118 *cr++ = bytestream[1];
00119
00120 bytestream += 4;
00121 }
00122 } else {
00123 offset = a->offset[y & 3] - a->delta[bytestream[2] & 0xF];
00124
00125 for (x = 0; x < avctx->width; x += 8) {
00126 luma[0] = offset += a->delta[bytestream[2] & 0xF];
00127 luma[1] = offset += a->delta[bytestream[2] >> 4];
00128 luma[2] = offset += a->delta[bytestream[3] & 0xF];
00129 luma[3] = offset += a->delta[bytestream[3] >> 4];
00130 luma[4] = offset += a->delta[bytestream[0] & 0xF];
00131 luma[5] = offset += a->delta[bytestream[0] >> 4];
00132 luma[6] = offset += a->delta[bytestream[1] & 0xF];
00133 luma[7] = offset += a->delta[bytestream[1] >> 4];
00134 luma += 8;
00135 bytestream += 4;
00136 }
00137 }
00138 }
00139
00140 *picture = a->picture;
00141 *data_size = sizeof(AVPicture);
00142
00143 return buf_size;
00144 }
00145
00146 AVCodec ff_vcr1_decoder = {
00147 .name = "vcr1",
00148 .type = AVMEDIA_TYPE_VIDEO,
00149 .id = AV_CODEC_ID_VCR1,
00150 .priv_data_size = sizeof(VCR1Context),
00151 .init = vcr1_decode_init,
00152 .close = vcr1_decode_end,
00153 .decode = vcr1_decode_frame,
00154 .capabilities = CODEC_CAP_DR1,
00155 .long_name = NULL_IF_CONFIG_SMALL("ATI VCR1"),
00156 };
00157
00158
00159 #undef CONFIG_VCR1_ENCODER
00160 #define CONFIG_VCR1_ENCODER 0
00161
00162 #if CONFIG_VCR1_ENCODER
00163
00164 #include "put_bits.h"
00165
00166 static int vcr1_encode_frame(AVCodecContext *avctx, unsigned char *buf,
00167 int buf_size, void *data)
00168 {
00169 VCR1Context *const a = avctx->priv_data;
00170 AVFrame *pict = data;
00171 AVFrame *const p = &a->picture;
00172 int size;
00173
00174 *p = *pict;
00175 p->pict_type = AV_PICTURE_TYPE_I;
00176 p->key_frame = 1;
00177
00178 avpriv_align_put_bits(&a->pb);
00179 flush_put_bits(&a->pb);
00180
00181 size = put_bits_count(&a->pb) / 32;
00182
00183 return size * 4;
00184 }
00185
00186 AVCodec ff_vcr1_encoder = {
00187 .name = "vcr1",
00188 .type = AVMEDIA_TYPE_VIDEO,
00189 .id = AV_CODEC_ID_VCR1,
00190 .priv_data_size = sizeof(VCR1Context),
00191 .init = vcr1_common_init,
00192 .encode = vcr1_encode_frame,
00193 .long_name = NULL_IF_CONFIG_SMALL("ATI VCR1"),
00194 };
00195 #endif