Libav
fic.c
Go to the documentation of this file.
1 /*
2  * Mirillis FIC decoder
3  *
4  * Copyright (c) 2014 Konstantin Shishkov
5  * Copyright (c) 2014 Derek Buitenhuis
6  *
7  * This file is part of Libav.
8  *
9  * Libav is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * Libav is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with Libav; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23 
24 #include "libavutil/common.h"
25 #include "avcodec.h"
26 #include "internal.h"
27 #include "dsputil.h"
28 #include "get_bits.h"
29 #include "golomb.h"
30 
31 typedef struct FICThreadContext {
32  DECLARE_ALIGNED(16, int16_t, block)[64];
34  int slice_h;
35  int src_size;
36  int y_off;
38 
39 typedef struct FICContext {
42 
45 
48 
49  const uint8_t *qmat;
50 
52 
55 } FICContext;
56 
57 static const uint8_t fic_qmat_hq[64] = {
58  1, 2, 2, 2, 3, 3, 3, 4,
59  2, 2, 2, 3, 3, 3, 4, 4,
60  2, 2, 3, 3, 3, 4, 4, 4,
61  2, 2, 3, 3, 3, 4, 4, 5,
62  2, 3, 3, 3, 4, 4, 5, 6,
63  3, 3, 3, 4, 4, 5, 6, 7,
64  3, 3, 3, 4, 4, 5, 7, 7,
65  3, 3, 4, 4, 5, 7, 7, 7,
66 };
67 
68 static const uint8_t fic_qmat_lq[64] = {
69  1, 5, 6, 7, 8, 9, 9, 11,
70  5, 5, 7, 8, 9, 9, 11, 12,
71  6, 7, 8, 9, 9, 11, 11, 12,
72  7, 7, 8, 9, 9, 11, 12, 13,
73  7, 8, 9, 9, 10, 11, 13, 16,
74  8, 9, 9, 10, 11, 13, 16, 19,
75  8, 9, 9, 11, 12, 15, 18, 23,
76  9, 9, 11, 12, 15, 18, 23, 27
77 };
78 
79 static const uint8_t fic_header[7] = { 0, 0, 1, 'F', 'I', 'C', 'V' };
80 
81 #define FIC_HEADER_SIZE 27
82 
84  uint8_t *dst, int stride, int16_t *block)
85 {
86  int i, num_coeff;
87 
88  /* Is it a skip block? */
89  if (get_bits1(gb)) {
90  /* This is a P-frame. */
91  ctx->frame->key_frame = 0;
93 
94  return 0;
95  }
96 
97  ctx->dsp.clear_block(block);
98 
99  num_coeff = get_bits(gb, 7);
100  if (num_coeff > 64)
101  return AVERROR_INVALIDDATA;
102 
103  for (i = 0; i < num_coeff; i++)
104  block[ctx->scantable.permutated[i]] = get_se_golomb(gb) * ctx->qmat[i];
105 
106  ctx->dsp.idct_put(dst, stride, block);
107 
108  return 0;
109 }
110 
111 static int fic_decode_slice(AVCodecContext *avctx, void *tdata)
112 {
113  FICContext *ctx = avctx->priv_data;
114  FICThreadContext *tctx = tdata;
115  GetBitContext gb;
116  uint8_t *src = tctx->src;
117  int slice_h = tctx->slice_h;
118  int src_size = tctx->src_size;
119  int y_off = tctx->y_off;
120  int x, y, p;
121 
122  init_get_bits(&gb, src, src_size * 8);
123 
124  for (p = 0; p < 3; p++) {
125  int stride = ctx->frame->linesize[p];
126  uint8_t* dst = ctx->frame->data[p] + (y_off >> !!p) * stride;
127 
128  for (y = 0; y < (slice_h >> !!p); y += 8) {
129  for (x = 0; x < (ctx->aligned_width >> !!p); x += 8) {
130  int ret;
131 
132  if ((ret = fic_decode_block(ctx, &gb, dst + x, stride, tctx->block)) != 0)
133  return ret;
134  }
135 
136  dst += 8 * stride;
137  }
138  }
139 
140  return 0;
141 }
142 
143 static int fic_decode_frame(AVCodecContext *avctx, void *data,
144  int *got_frame, AVPacket *avpkt)
145 {
146  FICContext *ctx = avctx->priv_data;
147  uint8_t *src = avpkt->data;
148  int ret;
149  int slice, nslices;
150  int msize;
151  int tsize;
152  uint8_t *sdata;
153 
154  if ((ret = ff_reget_buffer(avctx, ctx->frame)) < 0) {
155  av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
156  return ret;
157  }
158 
159  /* Header + at least one slice (4) */
160  if (avpkt->size < FIC_HEADER_SIZE + 4) {
161  av_log(avctx, AV_LOG_ERROR, "Frame data is too small.\n");
162  return AVERROR_INVALIDDATA;
163  }
164 
165  /* Check for header. */
166  if (memcmp(src, fic_header, 7))
167  av_log(avctx, AV_LOG_WARNING, "Invalid FIC Header.\n");
168 
169  /* Is it a skip frame? */
170  if (src[17])
171  goto skip;
172 
173  nslices = src[13];
174  if (!nslices) {
175  av_log(avctx, AV_LOG_ERROR, "Zero slices found.\n");
176  return AVERROR_INVALIDDATA;
177  }
178 
179  /* High or Low Quality Matrix? */
180  ctx->qmat = src[23] ? fic_qmat_hq : fic_qmat_lq;
181 
182  /* Skip cursor data. */
183  tsize = AV_RB24(src + 24);
184  if (tsize > avpkt->size - FIC_HEADER_SIZE) {
185  av_log(avctx, AV_LOG_ERROR, "Invalid cursor data size.\n");
186  return AVERROR_INVALIDDATA;
187  }
188 
189  /* Slice height for all but the last slice. */
190  ctx->slice_h = 16 * (ctx->aligned_height >> 4) / nslices;
191  if (ctx->slice_h % 16)
192  ctx->slice_h = FFALIGN(ctx->slice_h - 16, 16);
193 
194  /* First slice offset and remaining data. */
195  sdata = src + tsize + FIC_HEADER_SIZE + 4 * nslices;
196  msize = avpkt->size - nslices * 4 - tsize - FIC_HEADER_SIZE;
197 
198  if (msize <= 0) {
199  av_log(avctx, AV_LOG_ERROR, "Not enough frame data to decode.\n");
200  return AVERROR_INVALIDDATA;
201  }
202 
203  /*
204  * Set the frametype to I initially. It will be set to P if the frame
205  * has any dependencies (skip blocks). There will be a race condition
206  * inside the slice decode function to set these, but we do not care.
207  * since they will only ever be set to 0/P.
208  */
209  ctx->frame->key_frame = 1;
211 
212  /* Allocate slice data. */
214  nslices * sizeof(ctx->slice_data[0]));
215  if (!ctx->slice_data_size) {
216  av_log(avctx, AV_LOG_ERROR, "Could not allocate slice data.\n");
217  return AVERROR(ENOMEM);
218  }
219 
220  for (slice = 0; slice < nslices; slice++) {
221  int slice_off = AV_RB32(src + tsize + FIC_HEADER_SIZE + slice * 4);
222  int slice_size;
223  int y_off = ctx->slice_h * slice;
224  int slice_h = ctx->slice_h;
225 
226  /*
227  * Either read the slice size, or consume all data left.
228  * Also, special case the last slight height.
229  */
230  if (slice == nslices - 1) {
231  slice_size = msize;
232  slice_h = FFALIGN(avctx->height - ctx->slice_h * (nslices - 1), 16);
233  } else {
234  slice_size = AV_RB32(src + tsize + FIC_HEADER_SIZE + slice * 4 + 4);
235  }
236 
237  slice_size -= slice_off;
238 
239  if (slice_off > msize || slice_off + slice_size > msize)
240  continue;
241 
242  ctx->slice_data[slice].src = sdata + slice_off;
243  ctx->slice_data[slice].src_size = slice_size;
244  ctx->slice_data[slice].slice_h = slice_h;
245  ctx->slice_data[slice].y_off = y_off;
246  }
247 
248  if (ret = avctx->execute(avctx, fic_decode_slice, ctx->slice_data,
249  NULL, nslices, sizeof(ctx->slice_data[0])) < 0)
250  return ret;
251 
252 skip:
253  *got_frame = 1;
254  if ((ret = av_frame_ref(data, ctx->frame)) < 0)
255  return ret;
256 
257  return avpkt->size;
258 }
259 
261 {
262  FICContext *ctx = avctx->priv_data;
263 
264  av_freep(&ctx->slice_data);
265  av_frame_free(&ctx->frame);
266 
267  return 0;
268 }
269 
271 {
272  FICContext *ctx = avctx->priv_data;
273 
274  /* Initialize various context values */
275  ctx->avctx = avctx;
276  ctx->aligned_width = FFALIGN(avctx->width, 16);
277  ctx->aligned_height = FFALIGN(avctx->height, 16);
278 
279  avctx->pix_fmt = AV_PIX_FMT_YUV420P;
280  avctx->bits_per_raw_sample = 8;
281 
282  ctx->frame = av_frame_alloc();
283  if (!ctx->frame)
284  return AVERROR(ENOMEM);
285 
286  ff_dsputil_init(&ctx->dsp, avctx);
287 
289 
290  return 0;
291 }
292 
294  .name = "fic",
295  .long_name = NULL_IF_CONFIG_SMALL("Mirillis FIC"),
296  .type = AVMEDIA_TYPE_VIDEO,
297  .id = AV_CODEC_ID_FIC,
298  .priv_data_size = sizeof(FICContext),
302  .capabilities = CODEC_CAP_DR1 | CODEC_CAP_SLICE_THREADS,
303 };
av_cold void ff_dsputil_init(DSPContext *c, AVCodecContext *avctx)
Definition: dsputil.c:2440
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
This structure describes decoded (raw) audio or video data.
Definition: frame.h:107
static int get_se_golomb(GetBitContext *gb)
read signed exp golomb code.
Definition: golomb.h:179
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:240
int slice_h
Definition: fic.c:54
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:129
Scantable.
Definition: dsputil.h:111
int size
Definition: avcodec.h:974
#define AV_RB24
Definition: intreadwrite.h:64
#define DECLARE_ALIGNED(n, t, v)
Definition: mem.h:58
const uint8_t * qmat
Definition: fic.c:49
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1247
int aligned_height
Definition: fic.c:53
uint8_t permutated[64]
Definition: dsputil.h:113
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:2488
int num_slices
Definition: fic.c:54
int aligned_width
Definition: fic.c:53
int stride
Definition: mace.c:144
AVCodec.
Definition: avcodec.h:2755
#define FFALIGN(x, a)
Definition: common.h:62
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
static av_cold int fic_decode_close(AVCodecContext *avctx)
Definition: fic.c:260
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:269
void(* idct_put)(uint8_t *dest, int line_size, int16_t *block)
block -> idct -> clip to unsigned 8 bit -> dest.
Definition: dsputil.h:220
uint8_t
#define av_cold
Definition: attributes.h:66
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:43
#define AV_RB32
Definition: intreadwrite.h:130
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
Definition: frame.c:174
int src_size
Definition: fic.c:35
static const uint8_t fic_header[7]
Definition: fic.c:79
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:711
const char data[16]
Definition: mxf.c:66
uint8_t * data
Definition: avcodec.h:973
bitstream reader API header.
uint8_t idct_permutation[64]
idct input permutation.
Definition: dsputil.h:240
Definition: fic.c:39
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:55
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:142
static const uint8_t fic_qmat_lq[64]
Definition: fic.c:68
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:148
const char * name
Name of the codec implementation.
Definition: avcodec.h:2762
static int fic_decode_block(FICContext *ctx, GetBitContext *gb, uint8_t *dst, int stride, int16_t *block)
Definition: fic.c:83
AVCodec ff_fic_decoder
Definition: fic.c:293
enum AVPictureType cur_frame_type
Definition: fic.c:51
int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
Identical in function to av_frame_make_writable(), except it uses ff_get_buffer() to allocate the buf...
Definition: utils.c:755
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:168
int width
picture width / height.
Definition: avcodec.h:1217
static av_cold int fic_decode_init(AVCodecContext *avctx)
Definition: fic.c:270
NULL
Definition: eval.c:55
uint8_t * src
Definition: fic.c:33
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:125
main external API structure.
Definition: avcodec.h:1054
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:489
av_cold void ff_init_scantable(uint8_t *permutation, ScanTable *st, const uint8_t *src_scantable)
Definition: dsputil.c:107
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:271
int slice_data_size
Definition: fic.c:47
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:375
int y_off
Definition: fic.c:36
AVPictureType
Definition: avutil.h:252
ScanTable scantable
Definition: fic.c:44
FICThreadContext * slice_data
Definition: fic.c:46
static const uint8_t fic_qmat_hq[64]
Definition: fic.c:57
static int fic_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: fic.c:143
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:113
AVCodecContext * avctx
Definition: fic.c:40
int16_t block[64]
Definition: fic.c:32
void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer, reusing the given one if large enough.
Definition: mem.c:368
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:65
#define CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
Definition: avcodec.h:786
common internal api header.
#define FIC_HEADER_SIZE
Definition: fic.c:81
const uint8_t ff_zigzag_direct[64]
Definition: mathtables.c:113
common internal and external API header
void(* clear_block)(int16_t *block)
Definition: dsputil.h:142
static av_cold int init(AVCodecParserContext *s)
Definition: h264_parser.c:498
int slice_h
Definition: fic.c:34
DSP utils.
void * priv_data
Definition: avcodec.h:1090
int(* execute)(struct AVCodecContext *c, int(*func)(struct AVCodecContext *c2, void *arg), void *arg2, int *ret, int count, int size)
The codec may call this to execute several independent things.
Definition: avcodec.h:2554
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:163
AVFrame * frame
Definition: fic.c:41
DSPContext dsp
Definition: fic.c:43
static int fic_decode_slice(AVCodecContext *avctx, void *tdata)
Definition: fic.c:111
exp golomb vlc stuff
This structure stores compressed data.
Definition: avcodec.h:950
Predicted.
Definition: avutil.h:254
DSPContext.
Definition: dsputil.h:124
static int16_t block[64]
Definition: dct-test.c:170