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 "bitstream.h"
00029 #include "golomb.h"
00030
00031 enum LOCO_MODE {LOCO_UNKN=0, LOCO_CYUY2=-1, LOCO_CRGB=-2, LOCO_CRGBA=-3, LOCO_CYV12=-4,
00032 LOCO_YUY2=1, LOCO_UYVY=2, LOCO_RGB=3, LOCO_RGBA=4, LOCO_YV12=5};
00033
00034 typedef struct LOCOContext{
00035 AVCodecContext *avctx;
00036 AVFrame pic;
00037 int lossy;
00038 int mode;
00039 } LOCOContext;
00040
00041 typedef struct RICEContext{
00042 GetBitContext gb;
00043 int save, run, run2;
00044 int sum, count;
00045 int lossy;
00046 }RICEContext;
00047
00048 static int loco_get_rice_param(RICEContext *r)
00049 {
00050 int cnt = 0;
00051 int val = r->count;
00052
00053 while(r->sum > val && cnt < 9) {
00054 val <<= 1;
00055 cnt++;
00056 }
00057
00058 return cnt;
00059 }
00060
00061 static inline void loco_update_rice_param(RICEContext *r, int val)
00062 {
00063 r->sum += val;
00064 r->count++;
00065
00066 if(r->count == 16) {
00067 r->sum >>= 1;
00068 r->count >>= 1;
00069 }
00070 }
00071
00072 static inline int loco_get_rice(RICEContext *r)
00073 {
00074 int v;
00075 if (r->run > 0) {
00076 r->run--;
00077 loco_update_rice_param(r, 0);
00078 return 0;
00079 }
00080 v = get_ur_golomb_jpegls(&r->gb, loco_get_rice_param(r), INT_MAX, 0);
00081 loco_update_rice_param(r, (v+1)>>1);
00082 if (!v) {
00083 if (r->save >= 0) {
00084 r->run = get_ur_golomb_jpegls(&r->gb, 2, INT_MAX, 0);
00085 if(r->run > 1)
00086 r->save += r->run + 1;
00087 else
00088 r->save -= 3;
00089 }
00090 else
00091 r->run2++;
00092 } else {
00093 v = ((v>>1) + r->lossy) ^ -(v&1);
00094 if (r->run2 > 0) {
00095 if (r->run2 > 2)
00096 r->save += r->run2;
00097 else
00098 r->save -= 3;
00099 r->run2 = 0;
00100 }
00101 }
00102
00103 return v;
00104 }
00105
00106
00107 static inline int loco_predict(uint8_t* data, int stride, int step)
00108 {
00109 int a, b, c;
00110
00111 a = data[-stride];
00112 b = data[-step];
00113 c = data[-stride - step];
00114
00115 return mid_pred(a, a + b - c, b);
00116 }
00117
00118 static int loco_decode_plane(LOCOContext *l, uint8_t *data, int width, int height,
00119 int stride, const uint8_t *buf, int buf_size, int step)
00120 {
00121 RICEContext rc;
00122 int val;
00123 int i, j;
00124
00125 init_get_bits(&rc.gb, buf, buf_size*8);
00126 rc.save = 0;
00127 rc.run = 0;
00128 rc.run2 = 0;
00129 rc.lossy = l->lossy;
00130
00131 rc.sum = 8;
00132 rc.count = 1;
00133
00134
00135 val = loco_get_rice(&rc);
00136 data[0] = 128 + val;
00137
00138 for (i = 1; i < width; i++) {
00139 val = loco_get_rice(&rc);
00140 data[i * step] = data[i * step - step] + val;
00141 }
00142 data += stride;
00143 for (j = 1; j < height; j++) {
00144
00145 val = loco_get_rice(&rc);
00146 data[0] = data[-stride] + val;
00147
00148 for (i = 1; i < width; i++) {
00149 val = loco_get_rice(&rc);
00150 data[i * step] = loco_predict(&data[i * step], stride, step) + val;
00151 }
00152 data += stride;
00153 }
00154
00155 return ((get_bits_count(&rc.gb) + 7) >> 3);
00156 }
00157
00158 static int decode_frame(AVCodecContext *avctx,
00159 void *data, int *data_size,
00160 const uint8_t *buf, int buf_size)
00161 {
00162 LOCOContext * const l = avctx->priv_data;
00163 AVFrame * const p= (AVFrame*)&l->pic;
00164 int decoded;
00165
00166 if(p->data[0])
00167 avctx->release_buffer(avctx, p);
00168
00169 p->reference = 0;
00170 if(avctx->get_buffer(avctx, p) < 0){
00171 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00172 return -1;
00173 }
00174 p->key_frame = 1;
00175
00176 switch(l->mode) {
00177 case LOCO_CYUY2: case LOCO_YUY2: case LOCO_UYVY:
00178 decoded = loco_decode_plane(l, p->data[0], avctx->width, avctx->height,
00179 p->linesize[0], buf, buf_size, 1);
00180 buf += decoded; buf_size -= decoded;
00181 decoded = loco_decode_plane(l, p->data[1], avctx->width / 2, avctx->height,
00182 p->linesize[1], buf, buf_size, 1);
00183 buf += decoded; buf_size -= decoded;
00184 decoded = loco_decode_plane(l, p->data[2], avctx->width / 2, avctx->height,
00185 p->linesize[2], buf, buf_size, 1);
00186 break;
00187 case LOCO_CYV12: case LOCO_YV12:
00188 decoded = loco_decode_plane(l, p->data[0], avctx->width, avctx->height,
00189 p->linesize[0], buf, buf_size, 1);
00190 buf += decoded; buf_size -= decoded;
00191 decoded = loco_decode_plane(l, p->data[2], avctx->width / 2, avctx->height / 2,
00192 p->linesize[2], buf, buf_size, 1);
00193 buf += decoded; buf_size -= decoded;
00194 decoded = loco_decode_plane(l, p->data[1], avctx->width / 2, avctx->height / 2,
00195 p->linesize[1], buf, buf_size, 1);
00196 break;
00197 case LOCO_CRGB: case LOCO_RGB:
00198 decoded = loco_decode_plane(l, p->data[0] + p->linesize[0]*(avctx->height-1), avctx->width, avctx->height,
00199 -p->linesize[0], buf, buf_size, 3);
00200 buf += decoded; buf_size -= decoded;
00201 decoded = loco_decode_plane(l, p->data[0] + p->linesize[0]*(avctx->height-1) + 1, avctx->width, avctx->height,
00202 -p->linesize[0], buf, buf_size, 3);
00203 buf += decoded; buf_size -= decoded;
00204 decoded = loco_decode_plane(l, p->data[0] + p->linesize[0]*(avctx->height-1) + 2, avctx->width, avctx->height,
00205 -p->linesize[0], buf, buf_size, 3);
00206 break;
00207 case LOCO_RGBA:
00208 decoded = loco_decode_plane(l, p->data[0], avctx->width, avctx->height,
00209 p->linesize[0], buf, buf_size, 4);
00210 buf += decoded; buf_size -= decoded;
00211 decoded = loco_decode_plane(l, p->data[0] + 1, avctx->width, avctx->height,
00212 p->linesize[0], buf, buf_size, 4);
00213 buf += decoded; buf_size -= decoded;
00214 decoded = loco_decode_plane(l, p->data[0] + 2, avctx->width, avctx->height,
00215 p->linesize[0], buf, buf_size, 4);
00216 buf += decoded; buf_size -= decoded;
00217 decoded = loco_decode_plane(l, p->data[0] + 3, avctx->width, avctx->height,
00218 p->linesize[0], buf, buf_size, 4);
00219 break;
00220 }
00221
00222 *data_size = sizeof(AVFrame);
00223 *(AVFrame*)data = l->pic;
00224
00225 return buf_size;
00226 }
00227
00228 static int decode_init(AVCodecContext *avctx){
00229 LOCOContext * const l = avctx->priv_data;
00230 int version;
00231
00232 l->avctx = avctx;
00233 if (avctx->extradata_size < 12) {
00234 av_log(avctx, AV_LOG_ERROR, "Extradata size must be >= 12 instead of %i\n",
00235 avctx->extradata_size);
00236 return -1;
00237 }
00238 version = AV_RL32(avctx->extradata);
00239 switch(version) {
00240 case 1:
00241 l->lossy = 0;
00242 break;
00243 case 2:
00244 l->lossy = AV_RL32(avctx->extradata + 8);
00245 break;
00246 default:
00247 l->lossy = AV_RL32(avctx->extradata + 8);
00248 av_log(avctx, AV_LOG_INFO, "This is LOCO codec version %i, please upload file for study\n", version);
00249 }
00250
00251 l->mode = AV_RL32(avctx->extradata + 4);
00252 switch(l->mode) {
00253 case LOCO_CYUY2: case LOCO_YUY2: case LOCO_UYVY:
00254 avctx->pix_fmt = PIX_FMT_YUV422P;
00255 break;
00256 case LOCO_CRGB: case LOCO_RGB:
00257 avctx->pix_fmt = PIX_FMT_BGR24;
00258 break;
00259 case LOCO_CYV12: case LOCO_YV12:
00260 avctx->pix_fmt = PIX_FMT_YUV420P;
00261 break;
00262 case LOCO_CRGBA: case LOCO_RGBA:
00263 avctx->pix_fmt = PIX_FMT_RGB32;
00264 break;
00265 default:
00266 av_log(avctx, AV_LOG_INFO, "Unknown colorspace, index = %i\n", l->mode);
00267 return -1;
00268 }
00269 if(avctx->debug & FF_DEBUG_PICT_INFO)
00270 av_log(avctx, AV_LOG_INFO, "lossy:%i, version:%i, mode: %i\n", l->lossy, version, l->mode);
00271
00272 return 0;
00273 }
00274
00275 AVCodec loco_decoder = {
00276 "loco",
00277 CODEC_TYPE_VIDEO,
00278 CODEC_ID_LOCO,
00279 sizeof(LOCOContext),
00280 decode_init,
00281 NULL,
00282 NULL,
00283 decode_frame,
00284 CODEC_CAP_DR1,
00285 };