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