FFmpeg  4.3.8
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 FFmpeg.
6  *
7  * FFmpeg 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  * FFmpeg 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 FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * @file
24  * LOCO codec.
25  */
26 
27 #include "avcodec.h"
28 #include "get_bits.h"
29 #include "golomb.h"
30 #include "internal.h"
31 #include "mathops.h"
32 
33 enum LOCO_MODE {
34  LOCO_UNKN = 0,
35  LOCO_CYUY2 = -1,
36  LOCO_CRGB = -2,
37  LOCO_CRGBA = -3,
38  LOCO_CYV12 = -4,
39  LOCO_YUY2 = 1,
40  LOCO_UYVY = 2,
41  LOCO_RGB = 3,
42  LOCO_RGBA = 4,
43  LOCO_YV12 = 5,
44 };
45 
46 typedef struct LOCOContext {
48  int lossy;
50 } LOCOContext;
51 
52 typedef struct RICEContext {
54  int save, run, run2; /* internal rice decoder state */
55  int sum, count; /* sum and count for getting rice parameter */
56  int lossy;
57 } RICEContext;
58 
60 {
61  int cnt = 0;
62  int val = r->count;
63 
64  while (r->sum > val && cnt < 9) {
65  val <<= 1;
66  cnt++;
67  }
68 
69  return cnt;
70 }
71 
72 static inline void loco_update_rice_param(RICEContext *r, int val)
73 {
74  r->sum += val;
75  r->count++;
76 
77  if (r->count == 16) {
78  r->sum >>= 1;
79  r->count >>= 1;
80  }
81 }
82 
83 static inline int loco_get_rice(RICEContext *r)
84 {
85  unsigned v;
86  if (r->run > 0) { /* we have zero run */
87  r->run--;
89  return 0;
90  }
91  if (get_bits_left(&r->gb) < 1)
92  return INT_MIN;
93  v = get_ur_golomb_jpegls(&r->gb, loco_get_rice_param(r), INT_MAX, 0);
94  if (v == -1)
95  return INT_MIN;
96  loco_update_rice_param(r, (v + 1) >> 1);
97  if (!v) {
98  if (r->save >= 0) {
99  int run = get_ur_golomb_jpegls(&r->gb, 2, INT_MAX, 0);
100  if (run == -1)
101  return INT_MIN;
102  r->run = run;
103  if (r->run > 1)
104  r->save += r->run + 1;
105  else
106  r->save -= 3;
107  } else
108  r->run2++;
109  } else {
110  v = ((v >> 1) + r->lossy) ^ -(v & 1);
111  if (r->run2 > 0) {
112  if (r->run2 > 2)
113  r->save += r->run2;
114  else
115  r->save -= 3;
116  r->run2 = 0;
117  }
118  }
119 
120  return v;
121 }
122 
123 /* LOCO main predictor - LOCO-I/JPEG-LS predictor */
124 static inline int loco_predict(uint8_t* data, int stride)
125 {
126  int a, b, c;
127 
128  a = data[-stride];
129  b = data[-1];
130  c = data[-stride - 1];
131 
132  return mid_pred(a, a + b - c, b);
133 }
134 
136  int stride, const uint8_t *buf, int buf_size)
137 {
138  RICEContext rc;
139  unsigned val;
140  int ret;
141  int i, j;
142 
143  if(buf_size<=0)
144  return -1;
145 
146  if ((ret = init_get_bits8(&rc.gb, buf, buf_size)) < 0)
147  return ret;
148 
149  rc.save = 0;
150  rc.run = 0;
151  rc.run2 = 0;
152  rc.lossy = l->lossy;
153 
154  rc.sum = 8;
155  rc.count = 1;
156 
157  /* restore top left pixel */
158  val = loco_get_rice(&rc);
159  if (val == INT_MIN)
160  return AVERROR_INVALIDDATA;
161  data[0] = 128 + val;
162  /* restore top line */
163  for (i = 1; i < width; i++) {
164  val = loco_get_rice(&rc);
165  if (val == INT_MIN)
166  return AVERROR_INVALIDDATA;
167  data[i] = data[i - 1] + val;
168  }
169  data += stride;
170  for (j = 1; j < height; j++) {
171  /* restore left column */
172  val = loco_get_rice(&rc);
173  if (val == INT_MIN)
174  return AVERROR_INVALIDDATA;
175  data[0] = data[-stride] + val;
176  /* restore all other pixels */
177  for (i = 1; i < width; i++) {
178  val = loco_get_rice(&rc);
179  if (val == INT_MIN)
180  return -1;
181  data[i] = loco_predict(&data[i], stride) + val;
182  }
183  data += stride;
184  }
185 
186  return (get_bits_count(&rc.gb) + 7) >> 3;
187 }
188 
189 static void rotate_faulty_loco(uint8_t *data, int width, int height, int stride)
190 {
191  int y;
192 
193  for (y=1; y<height; y++) {
194  if (width>=y) {
195  memmove(data + y*stride,
196  data + y*(stride + 1),
197  (width-y));
198  if (y+1 < height)
199  memmove(data + y*stride + (width-y),
200  data + (y+1)*stride, y);
201  }
202  }
203 }
204 
206  void *data, int *got_frame,
207  AVPacket *avpkt)
208 {
209  LOCOContext * const l = avctx->priv_data;
210  const uint8_t *buf = avpkt->data;
211  int buf_size = avpkt->size;
212  AVFrame * const p = data;
213  int decoded, ret;
214 
215  if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
216  return ret;
217  p->key_frame = 1;
218 
219 #define ADVANCE_BY_DECODED do { \
220  if (decoded < 0 || decoded >= buf_size) goto buf_too_small; \
221  buf += decoded; buf_size -= decoded; \
222 } while(0)
223  switch(l->mode) {
224  case LOCO_CYUY2: case LOCO_YUY2: case LOCO_UYVY:
225  decoded = loco_decode_plane(l, p->data[0], avctx->width, avctx->height,
226  p->linesize[0], buf, buf_size);
228  decoded = loco_decode_plane(l, p->data[1], avctx->width / 2, avctx->height,
229  p->linesize[1], buf, buf_size);
231  decoded = loco_decode_plane(l, p->data[2], avctx->width / 2, avctx->height,
232  p->linesize[2], buf, buf_size);
233  break;
234  case LOCO_CYV12: case LOCO_YV12:
235  decoded = loco_decode_plane(l, p->data[0], avctx->width, avctx->height,
236  p->linesize[0], buf, buf_size);
238  decoded = loco_decode_plane(l, p->data[2], avctx->width / 2, avctx->height / 2,
239  p->linesize[2], buf, buf_size);
241  decoded = loco_decode_plane(l, p->data[1], avctx->width / 2, avctx->height / 2,
242  p->linesize[1], buf, buf_size);
243  break;
244  case LOCO_CRGB: case LOCO_RGB:
245  decoded = loco_decode_plane(l, p->data[1] + p->linesize[1]*(avctx->height-1), avctx->width, avctx->height,
246  -p->linesize[1], buf, buf_size);
248  decoded = loco_decode_plane(l, p->data[0] + p->linesize[0]*(avctx->height-1), avctx->width, avctx->height,
249  -p->linesize[0], buf, buf_size);
251  decoded = loco_decode_plane(l, p->data[2] + p->linesize[2]*(avctx->height-1), avctx->width, avctx->height,
252  -p->linesize[2], buf, buf_size);
253  if (avctx->width & 1) {
254  rotate_faulty_loco(p->data[0] + p->linesize[0]*(avctx->height-1), avctx->width, avctx->height, -p->linesize[0]);
255  rotate_faulty_loco(p->data[1] + p->linesize[1]*(avctx->height-1), avctx->width, avctx->height, -p->linesize[1]);
256  rotate_faulty_loco(p->data[2] + p->linesize[2]*(avctx->height-1), avctx->width, avctx->height, -p->linesize[2]);
257  }
258  break;
259  case LOCO_CRGBA:
260  case LOCO_RGBA:
261  decoded = loco_decode_plane(l, p->data[1] + p->linesize[1]*(avctx->height-1), avctx->width, avctx->height,
262  -p->linesize[1], buf, buf_size);
264  decoded = loco_decode_plane(l, p->data[0] + p->linesize[0]*(avctx->height-1), avctx->width, avctx->height,
265  -p->linesize[0], buf, buf_size);
267  decoded = loco_decode_plane(l, p->data[2] + p->linesize[2]*(avctx->height-1), avctx->width, avctx->height,
268  -p->linesize[2], buf, buf_size);
270  decoded = loco_decode_plane(l, p->data[3] + p->linesize[3]*(avctx->height-1), avctx->width, avctx->height,
271  -p->linesize[3], buf, buf_size);
272  break;
273  default:
274  av_assert0(0);
275  }
276 
277  if (decoded < 0 || decoded > buf_size)
278  goto buf_too_small;
279  buf_size -= decoded;
280 
281  *got_frame = 1;
282 
283  return avpkt->size - buf_size;
284 buf_too_small:
285  av_log(avctx, AV_LOG_ERROR, "Input data too small.\n");
286  return AVERROR(EINVAL);
287 }
288 
290 {
291  LOCOContext * const l = avctx->priv_data;
292  int version;
293 
294  l->avctx = avctx;
295  if (avctx->extradata_size < 12) {
296  av_log(avctx, AV_LOG_ERROR, "Extradata size must be >= 12 instead of %i\n",
297  avctx->extradata_size);
298  return AVERROR_INVALIDDATA;
299  }
300  version = AV_RL32(avctx->extradata);
301  switch (version) {
302  case 1:
303  l->lossy = 0;
304  break;
305  case 2:
306  l->lossy = AV_RL32(avctx->extradata + 8);
307  break;
308  default:
309  l->lossy = AV_RL32(avctx->extradata + 8);
310  avpriv_request_sample(avctx, "LOCO codec version %i", version);
311  }
312 
313  if (l->lossy > 65536U) {
314  av_log(avctx, AV_LOG_ERROR, "lossy %i is too large\n", l->lossy);
315  return AVERROR_INVALIDDATA;
316  }
317 
318  l->mode = AV_RL32(avctx->extradata + 4);
319  switch (l->mode) {
320  case LOCO_CYUY2:
321  case LOCO_YUY2:
322  case LOCO_UYVY:
323  avctx->pix_fmt = AV_PIX_FMT_YUV422P;
324  break;
325  case LOCO_CRGB:
326  case LOCO_RGB:
327  avctx->pix_fmt = AV_PIX_FMT_GBRP;
328  break;
329  case LOCO_CYV12:
330  case LOCO_YV12:
331  avctx->pix_fmt = AV_PIX_FMT_YUV420P;
332  break;
333  case LOCO_CRGBA:
334  case LOCO_RGBA:
335  avctx->pix_fmt = AV_PIX_FMT_GBRAP;
336  break;
337  default:
338  av_log(avctx, AV_LOG_INFO, "Unknown colorspace, index = %i\n", l->mode);
339  return AVERROR_INVALIDDATA;
340  }
341  if (avctx->debug & FF_DEBUG_PICT_INFO)
342  av_log(avctx, AV_LOG_INFO, "lossy:%i, version:%i, mode: %i\n", l->lossy, version, l->mode);
343 
344  return 0;
345 }
346 
348  .name = "loco",
349  .long_name = NULL_IF_CONFIG_SMALL("LOCO"),
350  .type = AVMEDIA_TYPE_VIDEO,
351  .id = AV_CODEC_ID_LOCO,
352  .priv_data_size = sizeof(LOCOContext),
353  .init = decode_init,
354  .decode = decode_frame,
355  .capabilities = AV_CODEC_CAP_DR1,
356 };
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
version
Definition: libkvazaar.c:292
This structure describes decoded (raw) audio or video data.
Definition: frame.h:300
Definition: loco.c:41
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
#define avpriv_request_sample(...)
planar GBR 4:4:4 24bpp
Definition: pixfmt.h:168
int size
Definition: packet.h:356
const char * b
Definition: vf_curves.c:116
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:736
static int get_ur_golomb_jpegls(GetBitContext *gb, int k, int limit, int esc_len)
read unsigned golomb rice code (jpegls).
Definition: golomb.h:431
uint8_t run
Definition: svq3.c:209
int stride
Definition: mace.c:144
AVCodec.
Definition: codec.h:190
Definition: loco.c:43
int run2
Definition: loco.c:54
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:71
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
static void rotate_faulty_loco(uint8_t *data, int width, int height, int stride)
Definition: loco.c:189
static int loco_get_rice_param(RICEContext *r)
Definition: loco.c:59
uint8_t
#define av_cold
Definition: attributes.h:88
static int loco_decode_plane(LOCOContext *l, uint8_t *data, int width, int height, int stride, const uint8_t *buf, int buf_size)
Definition: loco.c:135
#define FF_DEBUG_PICT_INFO
Definition: avcodec.h:1612
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:627
const char data[16]
Definition: mxf.c:91
#define height
uint8_t * data
Definition: packet.h:355
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:219
bitstream reader API header.
int run
Definition: loco.c:54
#define av_log(a,...)
#define U(x)
Definition: vp56_arith.h:37
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:849
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:188
const char * r
Definition: vf_curves.c:114
int count
Definition: loco.c:55
const char * name
Name of the codec implementation.
Definition: codec.h:197
static int loco_get_rice(RICEContext *r)
Definition: loco.c:83
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:70
int save
Definition: loco.c:54
int lossy
Definition: loco.c:48
static void loco_update_rice_param(RICEContext *r, int val)
Definition: loco.c:72
static av_cold int decode_init(AVCodecContext *avctx)
Definition: loco.c:289
#define width
int width
picture width / height.
Definition: avcodec.h:699
LOCO_MODE
Definition: loco.c:33
#define AV_RL32
Definition: intreadwrite.h:146
GetBitContext gb
Definition: loco.c:53
AVCodecContext * avctx
Definition: loco.c:47
int lossy
Definition: loco.c:56
Definition: loco.c:36
int sum
Definition: loco.c:55
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
Libavcodec external API header.
Definition: loco.c:40
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:331
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:677
int debug
debug
Definition: avcodec.h:1611
main external API structure.
Definition: avcodec.h:526
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1854
int extradata_size
Definition: avcodec.h:628
Definition: loco.c:42
#define mid_pred
Definition: mathops.h:97
#define ADVANCE_BY_DECODED
enum LOCO_MODE mode
Definition: loco.c:49
AVCodec ff_loco_decoder
Definition: loco.c:347
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:314
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
common internal api header.
planar GBRA 4:4:4:4 32bpp
Definition: pixfmt.h:215
static double c[64]
static int loco_predict(uint8_t *data, int stride)
Definition: loco.c:124
void * priv_data
Definition: avcodec.h:553
Definition: loco.c:39
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:378
Definition: loco.c:34
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: loco.c:205
exp golomb vlc stuff
static double val(void *priv, double ch)
Definition: aeval.c:76
This structure stores compressed data.
Definition: packet.h:332
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:50