asvdec.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2003 Michael Niedermayer
3  *
4  * This file is part of Libav.
5  *
6  * Libav is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * Libav is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with Libav; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
26 #include "libavutil/attributes.h"
27 #include "libavutil/mem.h"
28 
29 #include "asv.h"
30 #include "avcodec.h"
31 #include "put_bits.h"
32 #include "dsputil.h"
33 #include "internal.h"
34 #include "mathops.h"
35 #include "mpeg12data.h"
36 
37 //#undef NDEBUG
38 //#include <assert.h>
39 
40 #define VLC_BITS 6
41 #define ASV2_LEVEL_VLC_BITS 10
42 
43 static VLC ccp_vlc;
44 static VLC level_vlc;
45 static VLC dc_ccp_vlc;
46 static VLC ac_ccp_vlc;
48 
50 {
51  static int done = 0;
52 
53  if (!done) {
54  done = 1;
55 
56  INIT_VLC_STATIC(&ccp_vlc, VLC_BITS, 17,
57  &ff_asv_ccp_tab[0][1], 2, 1,
58  &ff_asv_ccp_tab[0][0], 2, 1, 64);
59  INIT_VLC_STATIC(&dc_ccp_vlc, VLC_BITS, 8,
60  &ff_asv_dc_ccp_tab[0][1], 2, 1,
61  &ff_asv_dc_ccp_tab[0][0], 2, 1, 64);
62  INIT_VLC_STATIC(&ac_ccp_vlc, VLC_BITS, 16,
63  &ff_asv_ac_ccp_tab[0][1], 2, 1,
64  &ff_asv_ac_ccp_tab[0][0], 2, 1, 64);
65  INIT_VLC_STATIC(&level_vlc, VLC_BITS, 7,
66  &ff_asv_level_tab[0][1], 2, 1,
67  &ff_asv_level_tab[0][0], 2, 1, 64);
68  INIT_VLC_STATIC(&asv2_level_vlc, ASV2_LEVEL_VLC_BITS, 63,
69  &ff_asv2_level_tab[0][1], 2, 1,
70  &ff_asv2_level_tab[0][0], 2, 1, 1024);
71  }
72 }
73 
74 //FIXME write a reversed bitstream reader to avoid the double reverse
75 static inline int asv2_get_bits(GetBitContext *gb, int n)
76 {
77  return ff_reverse[get_bits(gb, n) << (8-n)];
78 }
79 
80 static inline int asv1_get_level(GetBitContext *gb)
81 {
82  int code = get_vlc2(gb, level_vlc.table, VLC_BITS, 1);
83 
84  if (code == 3)
85  return get_sbits(gb, 8);
86  else
87  return code - 3;
88 }
89 
90 static inline int asv2_get_level(GetBitContext *gb)
91 {
92  int code = get_vlc2(gb, asv2_level_vlc.table, ASV2_LEVEL_VLC_BITS, 1);
93 
94  if (code == 31)
95  return (int8_t)asv2_get_bits(gb, 8);
96  else
97  return code - 31;
98 }
99 
100 static inline int asv1_decode_block(ASV1Context *a, DCTELEM block[64])
101 {
102  int i;
103 
104  block[0] = 8 * get_bits(&a->gb, 8);
105 
106  for (i = 0; i < 11; i++) {
107  const int ccp = get_vlc2(&a->gb, ccp_vlc.table, VLC_BITS, 1);
108 
109  if (ccp) {
110  if (ccp == 16)
111  break;
112  if (ccp < 0 || i >= 10) {
113  av_log(a->avctx, AV_LOG_ERROR, "coded coeff pattern damaged\n");
114  return -1;
115  }
116 
117  if (ccp & 8)
118  block[a->scantable.permutated[4 * i + 0]] = (asv1_get_level(&a->gb) * a->intra_matrix[4 * i + 0]) >> 4;
119  if (ccp & 4)
120  block[a->scantable.permutated[4 * i + 1]] = (asv1_get_level(&a->gb) * a->intra_matrix[4 * i + 1]) >> 4;
121  if (ccp & 2)
122  block[a->scantable.permutated[4 * i + 2]] = (asv1_get_level(&a->gb) * a->intra_matrix[4 * i + 2]) >> 4;
123  if (ccp & 1)
124  block[a->scantable.permutated[4 * i + 3]] = (asv1_get_level(&a->gb) * a->intra_matrix[4 * i + 3]) >> 4;
125  }
126  }
127 
128  return 0;
129 }
130 
131 static inline int asv2_decode_block(ASV1Context *a, DCTELEM block[64])
132 {
133  int i, count, ccp;
134 
135  count = asv2_get_bits(&a->gb, 4);
136 
137  block[0] = 8 * asv2_get_bits(&a->gb, 8);
138 
139  ccp = get_vlc2(&a->gb, dc_ccp_vlc.table, VLC_BITS, 1);
140  if (ccp) {
141  if (ccp & 4)
142  block[a->scantable.permutated[1]] = (asv2_get_level(&a->gb) * a->intra_matrix[1]) >> 4;
143  if (ccp & 2)
144  block[a->scantable.permutated[2]] = (asv2_get_level(&a->gb) * a->intra_matrix[2]) >> 4;
145  if (ccp & 1)
146  block[a->scantable.permutated[3]] = (asv2_get_level(&a->gb) * a->intra_matrix[3]) >> 4;
147  }
148 
149  for (i = 1; i < count + 1; i++) {
150  const int ccp = get_vlc2(&a->gb, ac_ccp_vlc.table, VLC_BITS, 1);
151 
152  if (ccp) {
153  if (ccp & 8)
154  block[a->scantable.permutated[4*i + 0]] = (asv2_get_level(&a->gb) * a->intra_matrix[4*i + 0]) >> 4;
155  if (ccp & 4)
156  block[a->scantable.permutated[4*i + 1]] = (asv2_get_level(&a->gb) * a->intra_matrix[4*i + 1]) >> 4;
157  if (ccp & 2)
158  block[a->scantable.permutated[4*i + 2]] = (asv2_get_level(&a->gb) * a->intra_matrix[4*i + 2]) >> 4;
159  if (ccp & 1)
160  block[a->scantable.permutated[4*i + 3]] = (asv2_get_level(&a->gb) * a->intra_matrix[4*i + 3]) >> 4;
161  }
162  }
163 
164  return 0;
165 }
166 
167 static inline int decode_mb(ASV1Context *a, DCTELEM block[6][64])
168 {
169  int i;
170 
171  a->dsp.clear_blocks(block[0]);
172 
173  if (a->avctx->codec_id == AV_CODEC_ID_ASV1) {
174  for (i = 0; i < 6; i++) {
175  if (asv1_decode_block(a, block[i]) < 0)
176  return -1;
177  }
178  } else {
179  for (i = 0; i < 6; i++) {
180  if (asv2_decode_block(a, block[i]) < 0)
181  return -1;
182  }
183  }
184  return 0;
185 }
186 
187 static inline void idct_put(ASV1Context *a, int mb_x, int mb_y)
188 {
189  DCTELEM (*block)[64] = a->block;
190  int linesize = a->picture.linesize[0];
191 
192  uint8_t *dest_y = a->picture.data[0] + (mb_y * 16* linesize ) + mb_x * 16;
193  uint8_t *dest_cb = a->picture.data[1] + (mb_y * 8 * a->picture.linesize[1]) + mb_x * 8;
194  uint8_t *dest_cr = a->picture.data[2] + (mb_y * 8 * a->picture.linesize[2]) + mb_x * 8;
195 
196  a->dsp.idct_put(dest_y , linesize, block[0]);
197  a->dsp.idct_put(dest_y + 8, linesize, block[1]);
198  a->dsp.idct_put(dest_y + 8*linesize , linesize, block[2]);
199  a->dsp.idct_put(dest_y + 8*linesize + 8, linesize, block[3]);
200 
201  if (!(a->avctx->flags&CODEC_FLAG_GRAY)) {
202  a->dsp.idct_put(dest_cb, a->picture.linesize[1], block[4]);
203  a->dsp.idct_put(dest_cr, a->picture.linesize[2], block[5]);
204  }
205 }
206 
207 static int decode_frame(AVCodecContext *avctx,
208  void *data, int *got_frame,
209  AVPacket *avpkt)
210 {
211  ASV1Context * const a = avctx->priv_data;
212  const uint8_t *buf = avpkt->data;
213  int buf_size = avpkt->size;
214  AVFrame *picture = data;
215  AVFrame * const p = &a->picture;
216  int mb_x, mb_y;
217 
218  if (p->data[0])
219  avctx->release_buffer(avctx, p);
220 
221  p->reference = 0;
222  if (ff_get_buffer(avctx, p) < 0) {
223  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
224  return -1;
225  }
227  p->key_frame = 1;
228 
230  buf_size);
231  if (!a->bitstream_buffer)
232  return AVERROR(ENOMEM);
233 
234  if (avctx->codec_id == AV_CODEC_ID_ASV1)
235  a->dsp.bswap_buf((uint32_t*)a->bitstream_buffer, (const uint32_t*)buf, buf_size/4);
236  else {
237  int i;
238  for (i = 0; i < buf_size; i++)
239  a->bitstream_buffer[i] = ff_reverse[buf[i]];
240  }
241 
242  init_get_bits(&a->gb, a->bitstream_buffer, buf_size*8);
243 
244  for (mb_y = 0; mb_y < a->mb_height2; mb_y++) {
245  for (mb_x = 0; mb_x < a->mb_width2; mb_x++) {
246  if (decode_mb(a, a->block) < 0)
247  return -1;
248 
249  idct_put(a, mb_x, mb_y);
250  }
251  }
252 
253  if (a->mb_width2 != a->mb_width) {
254  mb_x = a->mb_width2;
255  for (mb_y = 0; mb_y < a->mb_height2; mb_y++) {
256  if (decode_mb(a, a->block) < 0)
257  return -1;
258 
259  idct_put(a, mb_x, mb_y);
260  }
261  }
262 
263  if (a->mb_height2 != a->mb_height) {
264  mb_y = a->mb_height2;
265  for (mb_x = 0; mb_x < a->mb_width; mb_x++) {
266  if (decode_mb(a, a->block) < 0)
267  return -1;
268 
269  idct_put(a, mb_x, mb_y);
270  }
271  }
272 
273  *picture = a->picture;
274  *got_frame = 1;
275 
276  emms_c();
277 
278  return (get_bits_count(&a->gb) + 31) / 32 * 4;
279 }
280 
282 {
283  ASV1Context * const a = avctx->priv_data;
284  AVFrame *p = &a->picture;
285  const int scale = avctx->codec_id == AV_CODEC_ID_ASV1 ? 1 : 2;
286  int i;
287 
288  if (avctx->extradata_size < 1) {
289  av_log(avctx, AV_LOG_ERROR, "No extradata provided\n");
290  return AVERROR_INVALIDDATA;
291  }
292 
293  ff_asv_common_init(avctx);
294  init_vlcs(a);
296  avctx->pix_fmt = AV_PIX_FMT_YUV420P;
297 
298  a->inv_qscale = avctx->extradata[0];
299  if (a->inv_qscale == 0) {
300  av_log(avctx, AV_LOG_ERROR, "illegal qscale 0\n");
301  if (avctx->codec_id == AV_CODEC_ID_ASV1)
302  a->inv_qscale = 6;
303  else
304  a->inv_qscale = 10;
305  }
306 
307  for (i = 0; i < 64; i++) {
308  int index = ff_asv_scantab[i];
309 
310  a->intra_matrix[i] = 64 * scale * ff_mpeg1_default_intra_matrix[index] / a->inv_qscale;
311  }
312 
313  p->qstride = a->mb_width;
314  p->qscale_table = av_malloc(p->qstride * a->mb_height);
315  p->quality = (32 * scale + a->inv_qscale / 2) / a->inv_qscale;
316  memset(p->qscale_table, p->quality, p->qstride * a->mb_height);
317 
318  return 0;
319 }
320 
322 {
323  ASV1Context * const a = avctx->priv_data;
324 
327  a->bitstream_buffer_size = 0;
328 
329  if (a->picture.data[0])
330  avctx->release_buffer(avctx, &a->picture);
331 
332  return 0;
333 }
334 
336  .name = "asv1",
337  .type = AVMEDIA_TYPE_VIDEO,
338  .id = AV_CODEC_ID_ASV1,
339  .priv_data_size = sizeof(ASV1Context),
340  .init = decode_init,
341  .close = decode_end,
342  .decode = decode_frame,
343  .capabilities = CODEC_CAP_DR1,
344  .long_name = NULL_IF_CONFIG_SMALL("ASUS V1"),
345 };
346 
348  .name = "asv2",
349  .type = AVMEDIA_TYPE_VIDEO,
350  .id = AV_CODEC_ID_ASV2,
351  .priv_data_size = sizeof(ASV1Context),
352  .init = decode_init,
353  .close = decode_end,
354  .decode = decode_frame,
355  .capabilities = CODEC_CAP_DR1,
356  .long_name = NULL_IF_CONFIG_SMALL("ASUS V2"),
357 };
358 
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:61
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: asvdec.c:207
This structure describes decoded (raw) audio or video data.
Definition: avcodec.h:989
int qstride
QP store stride.
Definition: avcodec.h:1145
const uint8_t ff_asv_ac_ccp_tab[16][2]
Definition: asv.c:60
void(* release_buffer)(struct AVCodecContext *c, AVFrame *pic)
Called to release buffers which were allocated with get_buffer.
Definition: avcodec.h:2259
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:237
memory handling functions
int size
Definition: avcodec.h:916
AVCodecContext * avctx
Definition: asv.h:40
#define VLC_BITS
Definition: asvdec.c:40
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1533
void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer with padding, reusing the given one if large enough.
Definition: utils.c:85
uint8_t permutated[64]
Definition: dsputil.h:183
unsigned int bitstream_buffer_size
Definition: asv.h:55
AVCodec.
Definition: avcodec.h:2960
static int get_sbits(GetBitContext *s, int n)
Definition: get_bits.h:223
Macro definitions for various function/variable attributes.
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:151
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:228
const uint8_t ff_asv2_level_tab[63][2]
Definition: asv.c:67
static av_cold int decode_init(AVCodecContext *avctx)
Definition: asvdec.c:281
uint8_t
#define INIT_VLC_STATIC(vlc, bits, a, b, c, d, e, f, g, static_size)
Definition: get_bits.h:436
uint8_t * bitstream_buffer
Definition: asv.h:54
static VLC asv2_level_vlc
Definition: asvdec.c:47
#define emms_c()
Definition: internal.h:145
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1454
static int asv2_decode_block(ASV1Context *a, DCTELEM block[64])
Definition: asvdec.c:131
const char data[16]
Definition: mxf.c:66
const uint8_t ff_asv_ccp_tab[17][2]
Definition: asv.c:43
static int asv2_get_level(GetBitContext *gb)
Definition: asvdec.c:90
uint8_t * data
Definition: avcodec.h:915
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:192
uint8_t idct_permutation[64]
idct input permutation.
Definition: dsputil.h:425
const uint8_t ff_asv_dc_ccp_tab[8][2]
Definition: asv.c:55
int mb_height2
Definition: asv.h:50
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:335
const uint16_t ff_mpeg1_default_intra_matrix[64]
Definition: mpeg12data.c:30
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
void(* bswap_buf)(uint32_t *dst, const uint32_t *src, int w)
Definition: dsputil.h:343
void(* idct_put)(uint8_t *dest, int line_size, DCTELEM *block)
block -> idct -> clip to unsigned 8 bit -> dest.
Definition: dsputil.h:405
av_cold void ff_asv_common_init(AVCodecContext *avctx)
Definition: asv.c:83
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:88
int flags
CODEC_FLAG_*.
Definition: avcodec.h:1434
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
#define ASV2_LEVEL_VLC_BITS
Definition: asvdec.c:41
uint16_t intra_matrix[64]
Definition: asv.h:52
AVCodec ff_asv2_decoder
Definition: asvdec.c:347
ASUS V1/V2 encoder/decoder common data.
struct ASV1Context ASV1Context
Definition: get_bits.h:63
ScanTable scantable
Definition: asv.h:45
static int asv2_get_bits(GetBitContext *gb, int n)
Definition: asvdec.c:75
GetBitContext gb
Definition: asv.h:44
static AVFrame * picture
static DCTELEM block[64]
Definition: dct-test.c:169
enum AVPictureType pict_type
Picture type of the frame, see ?_TYPE below.
Definition: avcodec.h:1065
static VLC level_vlc
Definition: asvdec.c:44
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame)
Get a buffer for a frame.
Definition: utils.c:464
static VLC ccp_vlc
Definition: asvdec.c:43
int quality
quality (between 1 (good) and FF_LAMBDA_MAX (bad))
Definition: avcodec.h:1122
static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE(*table)[2], int bits, int max_depth)
Parse a vlc code.
Definition: get_bits.h:515
static void idct_put(ASV1Context *a, int mb_x, int mb_y)
Definition: asvdec.c:187
int mb_width
Definition: asv.h:47
external API header
static VLC ac_ccp_vlc
Definition: asvdec.c:46
enum AVCodecID codec_id
Definition: avcodec.h:1350
DSPContext dsp
Definition: asv.h:41
int linesize[AV_NUM_DATA_POINTERS]
Size, in bytes, of the data for each picture/channel plane.
Definition: avcodec.h:1008
AVCodec ff_asv1_decoder
Definition: asvdec.c:335
main external API structure.
Definition: avcodec.h:1339
static VLC dc_ccp_vlc
Definition: asvdec.c:45
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:326
MPEG1/2 tables.
int extradata_size
Definition: avcodec.h:1455
static int asv1_decode_block(ASV1Context *a, DCTELEM block[64])
Definition: asvdec.c:100
int index
Definition: gxfenc.c:72
DCTELEM block[6][64]
Definition: asv.h:51
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:372
int mb_width2
Definition: asv.h:49
short DCTELEM
Definition: dsputil.h:39
static const uint16_t scale[4]
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: avcodec.h:997
int8_t * qscale_table
QP table.
Definition: avcodec.h:1139
void(* clear_blocks)(DCTELEM *blocks)
Definition: dsputil.h:219
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:65
int inv_qscale
Definition: asv.h:46
common internal api header.
AVFrame picture
Definition: asv.h:42
DSP utils.
void * priv_data
Definition: avcodec.h:1382
void ff_init_scantable(uint8_t *permutation, ScanTable *st, const uint8_t *src_scantable)
Definition: dsputil.c:122
VLC_TYPE(* table)[2]
code, bits
Definition: get_bits.h:65
static av_cold void init_vlcs(ASV1Context *a)
Definition: asvdec.c:49
int key_frame
1 -> keyframe, 0-> not
Definition: avcodec.h:1058
static int asv1_get_level(GetBitContext *gb)
Definition: asvdec.c:80
static int decode_mb(ASV1Context *a, DCTELEM block[6][64])
Definition: asvdec.c:167
const uint8_t ff_reverse[256]
Definition: mathtables.c:70
const uint8_t ff_asv_scantab[64]
Definition: asv.c:32
static av_cold int decode_end(AVCodecContext *avctx)
Definition: asvdec.c:321
const uint8_t ff_asv_level_tab[7][2]
Definition: asv.c:51
This structure stores compressed data.
Definition: avcodec.h:898
int mb_height
Definition: asv.h:48
bitstream writer API