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 #define ADVANCE_BY_DECODED do { \
00183 if (decoded < 0) goto stop; \
00184 buf += decoded; buf_size -= decoded; \
00185 } while(0)
00186 switch(l->mode) {
00187 case LOCO_CYUY2: case LOCO_YUY2: case LOCO_UYVY:
00188 decoded = loco_decode_plane(l, p->data[0], avctx->width, avctx->height,
00189 p->linesize[0], buf, buf_size, 1);
00190 ADVANCE_BY_DECODED;
00191 decoded = loco_decode_plane(l, p->data[1], avctx->width / 2, avctx->height,
00192 p->linesize[1], buf, buf_size, 1);
00193 ADVANCE_BY_DECODED;
00194 decoded = loco_decode_plane(l, p->data[2], avctx->width / 2, avctx->height,
00195 p->linesize[2], buf, buf_size, 1);
00196 break;
00197 case LOCO_CYV12: case LOCO_YV12:
00198 decoded = loco_decode_plane(l, p->data[0], avctx->width, avctx->height,
00199 p->linesize[0], buf, buf_size, 1);
00200 ADVANCE_BY_DECODED;
00201 decoded = loco_decode_plane(l, p->data[2], avctx->width / 2, avctx->height / 2,
00202 p->linesize[2], buf, buf_size, 1);
00203 ADVANCE_BY_DECODED;
00204 decoded = loco_decode_plane(l, p->data[1], avctx->width / 2, avctx->height / 2,
00205 p->linesize[1], buf, buf_size, 1);
00206 break;
00207 case LOCO_CRGB: case LOCO_RGB:
00208 decoded = loco_decode_plane(l, p->data[0] + p->linesize[0]*(avctx->height-1), avctx->width, avctx->height,
00209 -p->linesize[0], buf, buf_size, 3);
00210 ADVANCE_BY_DECODED;
00211 decoded = loco_decode_plane(l, p->data[0] + p->linesize[0]*(avctx->height-1) + 1, avctx->width, avctx->height,
00212 -p->linesize[0], buf, buf_size, 3);
00213 ADVANCE_BY_DECODED;
00214 decoded = loco_decode_plane(l, p->data[0] + p->linesize[0]*(avctx->height-1) + 2, avctx->width, avctx->height,
00215 -p->linesize[0], buf, buf_size, 3);
00216 break;
00217 case LOCO_CRGBA: case LOCO_RGBA:
00218 decoded = loco_decode_plane(l, p->data[0], avctx->width, avctx->height,
00219 p->linesize[0], buf, buf_size, 4);
00220 ADVANCE_BY_DECODED;
00221 decoded = loco_decode_plane(l, p->data[0] + 1, avctx->width, avctx->height,
00222 p->linesize[0], buf, buf_size, 4);
00223 ADVANCE_BY_DECODED;
00224 decoded = loco_decode_plane(l, p->data[0] + 2, avctx->width, avctx->height,
00225 p->linesize[0], buf, buf_size, 4);
00226 ADVANCE_BY_DECODED;
00227 decoded = loco_decode_plane(l, p->data[0] + 3, avctx->width, avctx->height,
00228 p->linesize[0], buf, buf_size, 4);
00229 break;
00230 }
00231 stop:
00232
00233 *data_size = sizeof(AVFrame);
00234 *(AVFrame*)data = l->pic;
00235
00236 return buf_size < 0 ? -1 : avpkt->size - buf_size;
00237 }
00238
00239 static av_cold int decode_init(AVCodecContext *avctx){
00240 LOCOContext * const l = avctx->priv_data;
00241 int version;
00242
00243 l->avctx = avctx;
00244 if (avctx->extradata_size < 12) {
00245 av_log(avctx, AV_LOG_ERROR, "Extradata size must be >= 12 instead of %i\n",
00246 avctx->extradata_size);
00247 return -1;
00248 }
00249 version = AV_RL32(avctx->extradata);
00250 switch(version) {
00251 case 1:
00252 l->lossy = 0;
00253 break;
00254 case 2:
00255 l->lossy = AV_RL32(avctx->extradata + 8);
00256 break;
00257 default:
00258 l->lossy = AV_RL32(avctx->extradata + 8);
00259 av_log_ask_for_sample(avctx, "This is LOCO codec version %i.\n", version);
00260 }
00261
00262 l->mode = AV_RL32(avctx->extradata + 4);
00263 switch(l->mode) {
00264 case LOCO_CYUY2: case LOCO_YUY2: case LOCO_UYVY:
00265 avctx->pix_fmt = PIX_FMT_YUV422P;
00266 break;
00267 case LOCO_CRGB: case LOCO_RGB:
00268 avctx->pix_fmt = PIX_FMT_BGR24;
00269 break;
00270 case LOCO_CYV12: case LOCO_YV12:
00271 avctx->pix_fmt = PIX_FMT_YUV420P;
00272 break;
00273 case LOCO_CRGBA: case LOCO_RGBA:
00274 avctx->pix_fmt = PIX_FMT_RGB32;
00275 break;
00276 default:
00277 av_log(avctx, AV_LOG_INFO, "Unknown colorspace, index = %i\n", l->mode);
00278 return -1;
00279 }
00280 if(avctx->debug & FF_DEBUG_PICT_INFO)
00281 av_log(avctx, AV_LOG_INFO, "lossy:%i, version:%i, mode: %i\n", l->lossy, version, l->mode);
00282
00283 avcodec_get_frame_defaults(&l->pic);
00284
00285 return 0;
00286 }
00287
00288 static av_cold int decode_end(AVCodecContext *avctx){
00289 LOCOContext * const l = avctx->priv_data;
00290 AVFrame *pic = &l->pic;
00291
00292 if (pic->data[0])
00293 avctx->release_buffer(avctx, pic);
00294
00295 return 0;
00296 }
00297
00298 AVCodec ff_loco_decoder = {
00299 .name = "loco",
00300 .type = AVMEDIA_TYPE_VIDEO,
00301 .id = AV_CODEC_ID_LOCO,
00302 .priv_data_size = sizeof(LOCOContext),
00303 .init = decode_init,
00304 .close = decode_end,
00305 .decode = decode_frame,
00306 .capabilities = CODEC_CAP_DR1,
00307 .long_name = NULL_IF_CONFIG_SMALL("LOCO"),
00308 };