loco.c
Go to the documentation of this file.
1 /*
2  * LOCO codec
3  * Copyright (c) 2005 Konstantin Shishkov
4  *
5  * This file is part of Libav.
6  *
7  * Libav is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * Libav is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
27 #include "avcodec.h"
28 #include "get_bits.h"
29 #include "golomb.h"
30 #include "internal.h"
31 #include "mathops.h"
32 
35 
36 typedef struct LOCOContext{
39  int lossy;
40  int mode;
41 } LOCOContext;
42 
43 typedef struct RICEContext{
45  int save, run, run2; /* internal rice decoder state */
46  int sum, count; /* sum and count for getting rice parameter */
47  int lossy;
49 
51 {
52  int cnt = 0;
53  int val = r->count;
54 
55  while(r->sum > val && cnt < 9) {
56  val <<= 1;
57  cnt++;
58  }
59 
60  return cnt;
61 }
62 
63 static inline void loco_update_rice_param(RICEContext *r, int val)
64 {
65  r->sum += val;
66  r->count++;
67 
68  if(r->count == 16) {
69  r->sum >>= 1;
70  r->count >>= 1;
71  }
72 }
73 
74 static inline int loco_get_rice(RICEContext *r)
75 {
76  int v;
77  if (r->run > 0) { /* we have zero run */
78  r->run--;
80  return 0;
81  }
82  v = get_ur_golomb_jpegls(&r->gb, loco_get_rice_param(r), INT_MAX, 0);
83  loco_update_rice_param(r, (v+1)>>1);
84  if (!v) {
85  if (r->save >= 0) {
86  r->run = get_ur_golomb_jpegls(&r->gb, 2, INT_MAX, 0);
87  if(r->run > 1)
88  r->save += r->run + 1;
89  else
90  r->save -= 3;
91  }
92  else
93  r->run2++;
94  } else {
95  v = ((v>>1) + r->lossy) ^ -(v&1);
96  if (r->run2 > 0) {
97  if (r->run2 > 2)
98  r->save += r->run2;
99  else
100  r->save -= 3;
101  r->run2 = 0;
102  }
103  }
104 
105  return v;
106 }
107 
108 /* LOCO main predictor - LOCO-I/JPEG-LS predictor */
109 static inline int loco_predict(uint8_t* data, int stride, int step)
110 {
111  int a, b, c;
112 
113  a = data[-stride];
114  b = data[-step];
115  c = data[-stride - step];
116 
117  return mid_pred(a, a + b - c, b);
118 }
119 
121  int stride, const uint8_t *buf, int buf_size, int step)
122 {
123  RICEContext rc;
124  int val;
125  int i, j;
126 
127  init_get_bits(&rc.gb, buf, buf_size*8);
128  rc.save = 0;
129  rc.run = 0;
130  rc.run2 = 0;
131  rc.lossy = l->lossy;
132 
133  rc.sum = 8;
134  rc.count = 1;
135 
136  /* restore top left pixel */
137  val = loco_get_rice(&rc);
138  data[0] = 128 + val;
139  /* restore top line */
140  for (i = 1; i < width; i++) {
141  val = loco_get_rice(&rc);
142  data[i * step] = data[i * step - step] + val;
143  }
144  data += stride;
145  for (j = 1; j < height; j++) {
146  /* restore left column */
147  val = loco_get_rice(&rc);
148  data[0] = data[-stride] + val;
149  /* restore all other pixels */
150  for (i = 1; i < width; i++) {
151  val = loco_get_rice(&rc);
152  data[i * step] = loco_predict(&data[i * step], stride, step) + val;
153  }
154  data += stride;
155  }
156 
157  return (get_bits_count(&rc.gb) + 7) >> 3;
158 }
159 
160 static int decode_frame(AVCodecContext *avctx,
161  void *data, int *got_frame,
162  AVPacket *avpkt)
163 {
164  const uint8_t *buf = avpkt->data;
165  int buf_size = avpkt->size;
166  LOCOContext * const l = avctx->priv_data;
167  AVFrame * const p = &l->pic;
168  int decoded;
169 
170  if(p->data[0])
171  avctx->release_buffer(avctx, p);
172 
173  p->reference = 0;
174  if(ff_get_buffer(avctx, p) < 0){
175  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
176  return -1;
177  }
178  p->key_frame = 1;
179 
180  switch(l->mode) {
181  case LOCO_CYUY2: case LOCO_YUY2: case LOCO_UYVY:
182  decoded = loco_decode_plane(l, p->data[0], avctx->width, avctx->height,
183  p->linesize[0], buf, buf_size, 1);
184  if (decoded >= buf_size)
185  goto buf_too_small;
186  buf += decoded; buf_size -= decoded;
187 
188  decoded = loco_decode_plane(l, p->data[1], avctx->width / 2, avctx->height,
189  p->linesize[1], buf, buf_size, 1);
190  if (decoded >= buf_size)
191  goto buf_too_small;
192  buf += decoded; buf_size -= decoded;
193 
194  decoded = loco_decode_plane(l, p->data[2], avctx->width / 2, avctx->height,
195  p->linesize[2], buf, buf_size, 1);
196  break;
197  case LOCO_CYV12: case LOCO_YV12:
198  decoded = loco_decode_plane(l, p->data[0], avctx->width, avctx->height,
199  p->linesize[0], buf, buf_size, 1);
200  if (decoded >= buf_size)
201  goto buf_too_small;
202  buf += decoded; buf_size -= decoded;
203 
204  decoded = loco_decode_plane(l, p->data[2], avctx->width / 2, avctx->height / 2,
205  p->linesize[2], buf, buf_size, 1);
206  if (decoded >= buf_size)
207  goto buf_too_small;
208  buf += decoded; buf_size -= decoded;
209 
210  decoded = loco_decode_plane(l, p->data[1], avctx->width / 2, avctx->height / 2,
211  p->linesize[1], buf, buf_size, 1);
212  break;
213  case LOCO_CRGB: case LOCO_RGB:
214  decoded = loco_decode_plane(l, p->data[0] + p->linesize[0]*(avctx->height-1), avctx->width, avctx->height,
215  -p->linesize[0], buf, buf_size, 3);
216  if (decoded >= buf_size)
217  goto buf_too_small;
218  buf += decoded; buf_size -= decoded;
219 
220  decoded = loco_decode_plane(l, p->data[0] + p->linesize[0]*(avctx->height-1) + 1, avctx->width, avctx->height,
221  -p->linesize[0], buf, buf_size, 3);
222  if (decoded >= buf_size)
223  goto buf_too_small;
224  buf += decoded; buf_size -= decoded;
225 
226  decoded = loco_decode_plane(l, p->data[0] + p->linesize[0]*(avctx->height-1) + 2, avctx->width, avctx->height,
227  -p->linesize[0], buf, buf_size, 3);
228  break;
229  case LOCO_RGBA:
230  decoded = loco_decode_plane(l, p->data[0], avctx->width, avctx->height,
231  p->linesize[0], buf, buf_size, 4);
232  if (decoded >= buf_size)
233  goto buf_too_small;
234  buf += decoded; buf_size -= decoded;
235 
236  decoded = loco_decode_plane(l, p->data[0] + 1, avctx->width, avctx->height,
237  p->linesize[0], buf, buf_size, 4);
238  if (decoded >= buf_size)
239  goto buf_too_small;
240  buf += decoded; buf_size -= decoded;
241 
242  decoded = loco_decode_plane(l, p->data[0] + 2, avctx->width, avctx->height,
243  p->linesize[0], buf, buf_size, 4);
244  if (decoded >= buf_size)
245  goto buf_too_small;
246  buf += decoded; buf_size -= decoded;
247 
248  decoded = loco_decode_plane(l, p->data[0] + 3, avctx->width, avctx->height,
249  p->linesize[0], buf, buf_size, 4);
250  break;
251  }
252 
253  *got_frame = 1;
254  *(AVFrame*)data = l->pic;
255 
256  return buf_size;
257 buf_too_small:
258  av_log(avctx, AV_LOG_ERROR, "Input data too small.\n");
259  return AVERROR(EINVAL);
260 }
261 
262 static av_cold int decode_init(AVCodecContext *avctx){
263  LOCOContext * const l = avctx->priv_data;
264  int version;
265 
266  l->avctx = avctx;
267  if (avctx->extradata_size < 12) {
268  av_log(avctx, AV_LOG_ERROR, "Extradata size must be >= 12 instead of %i\n",
269  avctx->extradata_size);
270  return -1;
271  }
272  version = AV_RL32(avctx->extradata);
273  switch(version) {
274  case 1:
275  l->lossy = 0;
276  break;
277  case 2:
278  l->lossy = AV_RL32(avctx->extradata + 8);
279  break;
280  default:
281  l->lossy = AV_RL32(avctx->extradata + 8);
282  av_log_ask_for_sample(avctx, "This is LOCO codec version %i.\n", version);
283  }
284 
285  l->mode = AV_RL32(avctx->extradata + 4);
286  switch(l->mode) {
287  case LOCO_CYUY2: case LOCO_YUY2: case LOCO_UYVY:
288  avctx->pix_fmt = AV_PIX_FMT_YUV422P;
289  break;
290  case LOCO_CRGB: case LOCO_RGB:
291  avctx->pix_fmt = AV_PIX_FMT_BGR24;
292  break;
293  case LOCO_CYV12: case LOCO_YV12:
294  avctx->pix_fmt = AV_PIX_FMT_YUV420P;
295  break;
296  case LOCO_CRGBA: case LOCO_RGBA:
297  avctx->pix_fmt = AV_PIX_FMT_RGB32;
298  break;
299  default:
300  av_log(avctx, AV_LOG_INFO, "Unknown colorspace, index = %i\n", l->mode);
301  return -1;
302  }
303  if(avctx->debug & FF_DEBUG_PICT_INFO)
304  av_log(avctx, AV_LOG_INFO, "lossy:%i, version:%i, mode: %i\n", l->lossy, version, l->mode);
305 
306  return 0;
307 }
308 
309 static av_cold int decode_end(AVCodecContext *avctx){
310  LOCOContext * const l = avctx->priv_data;
311  AVFrame *pic = &l->pic;
312 
313  if (pic->data[0])
314  avctx->release_buffer(avctx, pic);
315 
316  return 0;
317 }
318 
320  .name = "loco",
321  .type = AVMEDIA_TYPE_VIDEO,
322  .id = AV_CODEC_ID_LOCO,
323  .priv_data_size = sizeof(LOCOContext),
324  .init = decode_init,
325  .close = decode_end,
326  .decode = decode_frame,
327  .capabilities = CODEC_CAP_DR1,
328  .long_name = NULL_IF_CONFIG_SMALL("LOCO"),
329 };
This structure describes decoded (raw) audio or video data.
Definition: avcodec.h:989
Definition: loco.c:34
void(* release_buffer)(struct AVCodecContext *c, AVFrame *pic)
Called to release buffers which were allocated with get_buffer.
Definition: avcodec.h:2259
static av_cold int decode_end(AVCodecContext *avctx)
Definition: loco.c:309
static int loco_predict(uint8_t *data, int stride, int step)
Definition: loco.c:109
int size
Definition: avcodec.h:916
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1533
static int get_ur_golomb_jpegls(GetBitContext *gb, int k, int limit, int esc_len)
read unsigned golomb rice code (jpegls).
Definition: golomb.h:286
int stride
Definition: mace.c:144
AVCodec.
Definition: avcodec.h:2960
Definition: loco.c:34
int run2
Definition: loco.c:45
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:228
static int loco_get_rice_param(RICEContext *r)
Definition: loco.c:50
uint8_t
#define b
Definition: input.c:52
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1454
const char data[16]
Definition: mxf.c:66
uint8_t * data
Definition: avcodec.h:915
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:192
bitstream reader API header.
int run
Definition: loco.c:45
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:335
#define r
Definition: input.c:51
void av_log_ask_for_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message asking for a sample.
int reference
is this picture used as reference The values for this are the same as the MpegEncContext.picture_structure variable, that is 1->top field, 2->bottom field, 3->frame/both fields.
Definition: avcodec.h:1132
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:88
AVFrame pic
Definition: loco.c:38
int count
Definition: loco.c:46
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
const char * name
Name of the codec implementation.
Definition: avcodec.h:2967
static int loco_get_rice(RICEContext *r)
Definition: loco.c:74
struct LOCOContext LOCOContext
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:69
int save
Definition: loco.c:45
int lossy
Definition: loco.c:39
struct RICEContext RICEContext
static void loco_update_rice_param(RICEContext *r, int val)
Definition: loco.c:63
static av_cold int decode_init(AVCodecContext *avctx)
Definition: loco.c:262
int width
picture width / height.
Definition: avcodec.h:1508
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame)
Get a buffer for a frame.
Definition: utils.c:464
LOCO_MODE
Definition: loco.c:33
#define AV_RL32
Definition: intreadwrite.h:146
GetBitContext gb
Definition: loco.c:44
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:68
static int loco_decode_plane(LOCOContext *l, uint8_t *data, int width, int height, int stride, const uint8_t *buf, int buf_size, int step)
Definition: loco.c:120
AVCodecContext * avctx
Definition: loco.c:37
int lossy
Definition: loco.c:47
Definition: loco.c:33
int sum
Definition: loco.c:46
static int width
Definition: utils.c:156
external API header
version
Definition: ffv1enc.c:1069
Definition: loco.c:34
int linesize[AV_NUM_DATA_POINTERS]
Size, in bytes, of the data for each picture/channel plane.
Definition: avcodec.h:1008
int debug
debug
Definition: avcodec.h:2568
main external API structure.
Definition: avcodec.h:1339
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:326
int extradata_size
Definition: avcodec.h:1455
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:372
Definition: loco.c:34
#define mid_pred
Definition: mathops.h:94
static int step
Definition: avplay.c:252
AVCodec ff_loco_decoder
Definition: loco.c:319
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: avcodec.h:997
int height
Definition: gxfenc.c:72
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:65
common internal api header.
void * priv_data
Definition: avcodec.h:1382
Definition: loco.c:34
int key_frame
1 -> keyframe, 0-> not
Definition: avcodec.h:1058
Definition: loco.c:33
int mode
Definition: loco.c:40
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: loco.c:160
exp golomb vlc stuff
This structure stores compressed data.
Definition: avcodec.h:898