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 "get_bits.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 if(buf_size<=0)
00127 return -1;
00128
00129 init_get_bits(&rc.gb, buf, buf_size*8);
00130 rc.save = 0;
00131 rc.run = 0;
00132 rc.run2 = 0;
00133 rc.lossy = l->lossy;
00134
00135 rc.sum = 8;
00136 rc.count = 1;
00137
00138
00139 val = loco_get_rice(&rc);
00140 data[0] = 128 + val;
00141
00142 for (i = 1; i < width; i++) {
00143 val = loco_get_rice(&rc);
00144 data[i * step] = data[i * step - step] + val;
00145 }
00146 data += stride;
00147 for (j = 1; j < height; j++) {
00148
00149 val = loco_get_rice(&rc);
00150 data[0] = data[-stride] + val;
00151
00152 for (i = 1; i < width; i++) {
00153 val = loco_get_rice(&rc);
00154 data[i * step] = loco_predict(&data[i * step], stride, step) + val;
00155 }
00156 data += stride;
00157 }
00158
00159 return (get_bits_count(&rc.gb) + 7) >> 3;
00160 }
00161
00162 static int decode_frame(AVCodecContext *avctx,
00163 void *data, int *data_size,
00164 AVPacket *avpkt)
00165 {
00166 const uint8_t *buf = avpkt->data;
00167 int buf_size = avpkt->size;
00168 LOCOContext * const l = avctx->priv_data;
00169 AVFrame * const p = &l->pic;
00170 int decoded;
00171
00172 if(p->data[0])
00173 avctx->release_buffer(avctx, p);
00174
00175 p->reference = 0;
00176 if(avctx->get_buffer(avctx, p) < 0){
00177 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00178 return -1;
00179 }
00180 p->key_frame = 1;
00181
00182 switch(l->mode) {
00183 case LOCO_CYUY2: case LOCO_YUY2: case LOCO_UYVY:
00184 decoded = loco_decode_plane(l, p->data[0], avctx->width, avctx->height,
00185 p->linesize[0], buf, buf_size, 1);
00186 buf += decoded; buf_size -= decoded;
00187 decoded = loco_decode_plane(l, p->data[1], avctx->width / 2, avctx->height,
00188 p->linesize[1], buf, buf_size, 1);
00189 buf += decoded; buf_size -= decoded;
00190 decoded = loco_decode_plane(l, p->data[2], avctx->width / 2, avctx->height,
00191 p->linesize[2], buf, buf_size, 1);
00192 break;
00193 case LOCO_CYV12: case LOCO_YV12:
00194 decoded = loco_decode_plane(l, p->data[0], avctx->width, avctx->height,
00195 p->linesize[0], buf, buf_size, 1);
00196 buf += decoded; buf_size -= decoded;
00197 decoded = loco_decode_plane(l, p->data[2], avctx->width / 2, avctx->height / 2,
00198 p->linesize[2], buf, buf_size, 1);
00199 buf += decoded; buf_size -= decoded;
00200 decoded = loco_decode_plane(l, p->data[1], avctx->width / 2, avctx->height / 2,
00201 p->linesize[1], buf, buf_size, 1);
00202 break;
00203 case LOCO_CRGB: case LOCO_RGB:
00204 decoded = loco_decode_plane(l, p->data[0] + p->linesize[0]*(avctx->height-1), avctx->width, avctx->height,
00205 -p->linesize[0], buf, buf_size, 3);
00206 buf += decoded; buf_size -= decoded;
00207 decoded = loco_decode_plane(l, p->data[0] + p->linesize[0]*(avctx->height-1) + 1, avctx->width, avctx->height,
00208 -p->linesize[0], buf, buf_size, 3);
00209 buf += decoded; buf_size -= decoded;
00210 decoded = loco_decode_plane(l, p->data[0] + p->linesize[0]*(avctx->height-1) + 2, avctx->width, avctx->height,
00211 -p->linesize[0], buf, buf_size, 3);
00212 break;
00213 case LOCO_CRGBA: case LOCO_RGBA:
00214 decoded = loco_decode_plane(l, p->data[0], 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] + 1, avctx->width, avctx->height,
00218 p->linesize[0], buf, buf_size, 4);
00219 buf += decoded; buf_size -= decoded;
00220 decoded = loco_decode_plane(l, p->data[0] + 2, avctx->width, avctx->height,
00221 p->linesize[0], buf, buf_size, 4);
00222 buf += decoded; buf_size -= decoded;
00223 decoded = loco_decode_plane(l, p->data[0] + 3, avctx->width, avctx->height,
00224 p->linesize[0], buf, buf_size, 4);
00225 break;
00226 }
00227
00228 *data_size = sizeof(AVFrame);
00229 *(AVFrame*)data = l->pic;
00230
00231 return buf_size < 0 ? -1 : buf_size;
00232 }
00233
00234 static av_cold int decode_init(AVCodecContext *avctx){
00235 LOCOContext * const l = avctx->priv_data;
00236 int version;
00237
00238 l->avctx = avctx;
00239 if (avctx->extradata_size < 12) {
00240 av_log(avctx, AV_LOG_ERROR, "Extradata size must be >= 12 instead of %i\n",
00241 avctx->extradata_size);
00242 return -1;
00243 }
00244 version = AV_RL32(avctx->extradata);
00245 switch(version) {
00246 case 1:
00247 l->lossy = 0;
00248 break;
00249 case 2:
00250 l->lossy = AV_RL32(avctx->extradata + 8);
00251 break;
00252 default:
00253 l->lossy = AV_RL32(avctx->extradata + 8);
00254 av_log_ask_for_sample(avctx, "This is LOCO codec version %i.\n", version);
00255 }
00256
00257 l->mode = AV_RL32(avctx->extradata + 4);
00258 switch(l->mode) {
00259 case LOCO_CYUY2: case LOCO_YUY2: case LOCO_UYVY:
00260 avctx->pix_fmt = PIX_FMT_YUV422P;
00261 break;
00262 case LOCO_CRGB: case LOCO_RGB:
00263 avctx->pix_fmt = PIX_FMT_BGR24;
00264 break;
00265 case LOCO_CYV12: case LOCO_YV12:
00266 avctx->pix_fmt = PIX_FMT_YUV420P;
00267 break;
00268 case LOCO_CRGBA: case LOCO_RGBA:
00269 avctx->pix_fmt = PIX_FMT_RGB32;
00270 break;
00271 default:
00272 av_log(avctx, AV_LOG_INFO, "Unknown colorspace, index = %i\n", l->mode);
00273 return -1;
00274 }
00275 if(avctx->debug & FF_DEBUG_PICT_INFO)
00276 av_log(avctx, AV_LOG_INFO, "lossy:%i, version:%i, mode: %i\n", l->lossy, version, l->mode);
00277
00278 avcodec_get_frame_defaults(&l->pic);
00279
00280 return 0;
00281 }
00282
00283 static av_cold int decode_end(AVCodecContext *avctx){
00284 LOCOContext * const l = avctx->priv_data;
00285 AVFrame *pic = &l->pic;
00286
00287 if (pic->data[0])
00288 avctx->release_buffer(avctx, pic);
00289
00290 return 0;
00291 }
00292
00293 AVCodec ff_loco_decoder = {
00294 .name = "loco",
00295 .type = AVMEDIA_TYPE_VIDEO,
00296 .id = CODEC_ID_LOCO,
00297 .priv_data_size = sizeof(LOCOContext),
00298 .init = decode_init,
00299 .close = decode_end,
00300 .decode = decode_frame,
00301 .capabilities = CODEC_CAP_DR1,
00302 .long_name = NULL_IF_CONFIG_SMALL("LOCO"),
00303 };