FFmpeg  2.8.17
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
hq_hqa.c
Go to the documentation of this file.
1 /*
2  * Canopus HQ/HQA decoder
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg 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  * FFmpeg 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 FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include <stdint.h>
22 
23 #include "libavutil/attributes.h"
24 #include "libavutil/intreadwrite.h"
25 
26 #include "avcodec.h"
27 #include "canopus.h"
28 #include "internal.h"
29 
30 #include "hq_hqa.h"
31 #include "hq_hqadsp.h"
32 
33 /* HQ/HQA slices are a set of macroblocks belonging to a frame, and
34  * they usually form a pseudorandom pattern (probably because it is
35  * nicer to display on partial decode).
36  *
37  * For HQA it just happens that each slice is on every 8th macroblock,
38  * but they can be on any frame width like
39  * X.......X.
40  * ......X...
41  * ....X.....
42  * ..X.......
43  * etc.
44  *
45  * The original decoder has special handling for edge macroblocks,
46  * while lavc simply aligns coded_width and coded_height.
47  */
48 
49 static inline void put_blocks(HQContext *c, AVFrame *pic,
50  int plane, int x, int y, int ilace,
51  int16_t *block0, int16_t *block1)
52 {
53  uint8_t *p = pic->data[plane] + x;
54 
55  c->hqhqadsp.idct_put(p + y * pic->linesize[plane],
56  pic->linesize[plane] << ilace, block0);
57  c->hqhqadsp.idct_put(p + (y + (ilace ? 1 : 8)) * pic->linesize[plane],
58  pic->linesize[plane] << ilace, block1);
59 }
60 
61 static int hq_decode_block(HQContext *c, GetBitContext *gb, int16_t block[64],
62  int qsel, int is_chroma, int is_hqa)
63 {
64  const int32_t *q;
65  int val, pos = 1;
66 
67  memset(block, 0, 64 * sizeof(*block));
68 
69  if (!is_hqa) {
70  block[0] = get_sbits(gb, 9) * 64;
71  q = ff_hq_quants[qsel][is_chroma][get_bits(gb, 2)];
72  } else {
73  q = ff_hq_quants[qsel][is_chroma][get_bits(gb, 2)];
74  block[0] = get_sbits(gb, 9) * 64;
75  }
76 
77  for (;;) {
78  val = get_vlc2(gb, c->hq_ac_vlc.table, 9, 2);
79  if (val < 0)
80  return AVERROR_INVALIDDATA;
81 
82  pos += ff_hq_ac_skips[val];
83  if (pos >= 64)
84  break;
85  block[ff_zigzag_direct[pos]] = (int)(ff_hq_ac_syms[val] * (unsigned)q[pos]) >> 12;
86  pos++;
87  }
88 
89  return 0;
90 }
91 
92 static int hq_decode_mb(HQContext *c, AVFrame *pic,
93  GetBitContext *gb, int x, int y)
94 {
95  int qgroup, flag;
96  int i, ret;
97 
98  qgroup = get_bits(gb, 4);
99  flag = get_bits1(gb);
100 
101  for (i = 0; i < 8; i++) {
102  ret = hq_decode_block(c, gb, c->block[i], qgroup, i >= 4, 0);
103  if (ret < 0)
104  return ret;
105  }
106 
107  put_blocks(c, pic, 0, x, y, flag, c->block[0], c->block[2]);
108  put_blocks(c, pic, 0, x + 8, y, flag, c->block[1], c->block[3]);
109  put_blocks(c, pic, 2, x >> 1, y, flag, c->block[4], c->block[5]);
110  put_blocks(c, pic, 1, x >> 1, y, flag, c->block[6], c->block[7]);
111 
112  return 0;
113 }
114 
115 static int hq_decode_frame(HQContext *ctx, AVFrame *pic,
116  int prof_num, size_t data_size)
117 {
118  const HQProfile *profile;
119  GetBitContext gb;
120  const uint8_t *perm, *src = ctx->gbc.buffer;
121  uint32_t slice_off[21];
122  int slice, start_off, next_off, i, ret;
123 
124  if ((unsigned)prof_num >= NUM_HQ_PROFILES) {
125  profile = &ff_hq_profile[0];
126  avpriv_request_sample(ctx->avctx, "HQ Profile %d", prof_num);
127  } else {
128  profile = &ff_hq_profile[prof_num];
129  av_log(ctx->avctx, AV_LOG_VERBOSE, "HQ Profile %d\n", prof_num);
130  }
131 
132  ctx->avctx->coded_width = FFALIGN(profile->width, 16);
133  ctx->avctx->coded_height = FFALIGN(profile->height, 16);
134  ctx->avctx->width = profile->width;
135  ctx->avctx->height = profile->height;
136  ctx->avctx->bits_per_raw_sample = 8;
138 
139  ret = ff_get_buffer(ctx->avctx, pic, 0);
140  if (ret < 0)
141  return ret;
142 
143  /* Offsets are stored from CUV position, so adjust them accordingly. */
144  for (i = 0; i < profile->num_slices + 1; i++)
145  slice_off[i] = bytestream2_get_be24(&ctx->gbc) - 4;
146 
147  next_off = 0;
148  for (slice = 0; slice < profile->num_slices; slice++) {
149  start_off = next_off;
150  next_off = profile->tab_h * (slice + 1) / profile->num_slices;
151  perm = profile->perm_tab + start_off * profile->tab_w * 2;
152 
153  if (slice_off[slice] < (profile->num_slices + 1) * 3 ||
154  slice_off[slice] >= slice_off[slice + 1] ||
155  slice_off[slice + 1] > data_size) {
156  av_log(ctx->avctx, AV_LOG_ERROR,
157  "Invalid slice size %zu.\n", data_size);
158  break;
159  }
160  init_get_bits(&gb, src + slice_off[slice],
161  (slice_off[slice + 1] - slice_off[slice]) * 8);
162 
163  for (i = 0; i < (next_off - start_off) * profile->tab_w; i++) {
164  ret = hq_decode_mb(ctx, pic, &gb, perm[0] * 16, perm[1] * 16);
165  if (ret < 0) {
166  av_log(ctx->avctx, AV_LOG_ERROR,
167  "Error decoding macroblock %d at slice %d.\n", i, slice);
168  return ret;
169  }
170  perm += 2;
171  }
172  }
173 
174  return 0;
175 }
176 
177 static int hqa_decode_mb(HQContext *c, AVFrame *pic, int qgroup,
178  GetBitContext *gb, int x, int y)
179 {
180  int flag = 0;
181  int i, ret, cbp;
182 
183  if (get_bits_left(gb) < 1)
184  return AVERROR_INVALIDDATA;
185 
186  cbp = get_vlc2(gb, c->hqa_cbp_vlc.table, 5, 1);
187 
188  for (i = 0; i < 12; i++)
189  memset(c->block[i], 0, sizeof(*c->block));
190  for (i = 0; i < 12; i++)
191  c->block[i][0] = -128 * (1 << 6);
192 
193  if (cbp) {
194  flag = get_bits1(gb);
195 
196  cbp |= cbp << 4;
197  if (cbp & 0x3)
198  cbp |= 0x500;
199  if (cbp & 0xC)
200  cbp |= 0xA00;
201  for (i = 0; i < 12; i++) {
202  if (!(cbp & (1 << i)))
203  continue;
204  ret = hq_decode_block(c, gb, c->block[i], qgroup, i >= 8, 1);
205  if (ret < 0)
206  return ret;
207  }
208  }
209 
210  put_blocks(c, pic, 3, x, y, flag, c->block[ 0], c->block[ 2]);
211  put_blocks(c, pic, 3, x + 8, y, flag, c->block[ 1], c->block[ 3]);
212  put_blocks(c, pic, 0, x, y, flag, c->block[ 4], c->block[ 6]);
213  put_blocks(c, pic, 0, x + 8, y, flag, c->block[ 5], c->block[ 7]);
214  put_blocks(c, pic, 2, x >> 1, y, flag, c->block[ 8], c->block[ 9]);
215  put_blocks(c, pic, 1, x >> 1, y, flag, c->block[10], c->block[11]);
216 
217  return 0;
218 }
219 
220 static int hqa_decode_slice(HQContext *ctx, AVFrame *pic, GetBitContext *gb,
221  int quant, int slice_no, int w, int h)
222 {
223  int i, j, off;
224  int ret;
225 
226  for (i = 0; i < h; i += 16) {
227  off = (slice_no * 16 + i * 3) & 0x70;
228  for (j = off; j < w; j += 128) {
229  ret = hqa_decode_mb(ctx, pic, quant, gb, j, i);
230  if (ret < 0) {
231  av_log(ctx->avctx, AV_LOG_ERROR,
232  "Error decoding macroblock at %dx%d.\n", i, j);
233  return ret;
234  }
235  }
236  }
237 
238  return 0;
239 }
240 
241 static int hqa_decode_frame(HQContext *ctx, AVFrame *pic, size_t data_size)
242 {
243  GetBitContext gb;
244  const int num_slices = 8;
245  uint32_t slice_off[9];
246  int i, slice, ret;
247  int width, height, quant;
248  const uint8_t *src = ctx->gbc.buffer;
249 
250  if (bytestream2_get_bytes_left(&ctx->gbc) < 8 + 4*(num_slices + 1))
251  return AVERROR_INVALIDDATA;
252 
253  width = bytestream2_get_be16(&ctx->gbc);
254  height = bytestream2_get_be16(&ctx->gbc);
255 
256  ret = ff_set_dimensions(ctx->avctx, width, height);
257  if (ret < 0)
258  return ret;
259 
260  ctx->avctx->coded_width = FFALIGN(width, 16);
261  ctx->avctx->coded_height = FFALIGN(height, 16);
262  ctx->avctx->bits_per_raw_sample = 8;
264 
265  av_log(ctx->avctx, AV_LOG_VERBOSE, "HQA Profile\n");
266 
267  quant = bytestream2_get_byte(&ctx->gbc);
268  bytestream2_skip(&ctx->gbc, 3);
269  if (quant >= NUM_HQ_QUANTS) {
270  av_log(ctx->avctx, AV_LOG_ERROR,
271  "Invalid quantization matrix %d.\n", quant);
272  return AVERROR_INVALIDDATA;
273  }
274 
275  ret = ff_get_buffer(ctx->avctx, pic, 0);
276  if (ret < 0)
277  return ret;
278 
279  /* Offsets are stored from HQA1 position, so adjust them accordingly. */
280  for (i = 0; i < num_slices + 1; i++)
281  slice_off[i] = bytestream2_get_be32(&ctx->gbc) - 4;
282 
283  for (slice = 0; slice < num_slices; slice++) {
284  if (slice_off[slice] < (num_slices + 1) * 3 ||
285  slice_off[slice] >= slice_off[slice + 1] ||
286  slice_off[slice + 1] > data_size) {
287  av_log(ctx->avctx, AV_LOG_ERROR,
288  "Invalid slice size %zu.\n", data_size);
289  break;
290  }
291  init_get_bits(&gb, src + slice_off[slice],
292  (slice_off[slice + 1] - slice_off[slice]) * 8);
293 
294  ret = hqa_decode_slice(ctx, pic, &gb, quant, slice, width, height);
295  if (ret < 0)
296  return ret;
297  }
298 
299  return 0;
300 }
301 
302 static int hq_hqa_decode_frame(AVCodecContext *avctx, void *data,
303  int *got_frame, AVPacket *avpkt)
304 {
305  HQContext *ctx = avctx->priv_data;
306  AVFrame *pic = data;
307  uint32_t info_tag;
308  unsigned int data_size;
309  int ret;
310  unsigned tag;
311 
312  bytestream2_init(&ctx->gbc, avpkt->data, avpkt->size);
313  if (bytestream2_get_bytes_left(&ctx->gbc) < 4 + 4) {
314  av_log(avctx, AV_LOG_ERROR, "Frame is too small (%d).\n", avpkt->size);
315  return AVERROR_INVALIDDATA;
316  }
317 
318  info_tag = bytestream2_peek_le32(&ctx->gbc);
319  if (info_tag == MKTAG('I', 'N', 'F', 'O')) {
320  int info_size;
321  bytestream2_skip(&ctx->gbc, 4);
322  info_size = bytestream2_get_le32(&ctx->gbc);
323  if (info_size < 0 || bytestream2_get_bytes_left(&ctx->gbc) < info_size) {
324  av_log(avctx, AV_LOG_ERROR, "Invalid INFO size (%d).\n", info_size);
325  return AVERROR_INVALIDDATA;
326  }
327  ff_canopus_parse_info_tag(avctx, ctx->gbc.buffer, info_size);
328 
329  bytestream2_skip(&ctx->gbc, info_size);
330  }
331 
332  data_size = bytestream2_get_bytes_left(&ctx->gbc);
333  if (data_size < 4) {
334  av_log(avctx, AV_LOG_ERROR, "Frame is too small (%d).\n", data_size);
335  return AVERROR_INVALIDDATA;
336  }
337 
338  /* HQ defines dimensions and number of slices, and thus slice traversal
339  * order. HQA has no size constraint and a fixed number of slices, so it
340  * needs a separate scheme for it. */
341  tag = bytestream2_get_le32(&ctx->gbc);
342  if ((tag & 0x00FFFFFF) == (MKTAG('U', 'V', 'C', ' ') & 0x00FFFFFF)) {
343  ret = hq_decode_frame(ctx, pic, tag >> 24, data_size);
344  } else if (tag == MKTAG('H', 'Q', 'A', '1')) {
345  ret = hqa_decode_frame(ctx, pic, data_size);
346  } else {
347  av_log(avctx, AV_LOG_ERROR, "Not a HQ/HQA frame.\n");
348  return AVERROR_INVALIDDATA;
349  }
350  if (ret < 0) {
351  av_log(avctx, AV_LOG_ERROR, "Error decoding frame.\n");
352  return ret;
353  }
354 
355  pic->key_frame = 1;
357 
358  *got_frame = 1;
359 
360  return avpkt->size;
361 }
362 
364 {
365  HQContext *ctx = avctx->priv_data;
366  ctx->avctx = avctx;
367 
368  ff_hqdsp_init(&ctx->hqhqadsp);
369 
370  return ff_hq_init_vlcs(ctx);
371 }
372 
374 {
375  HQContext *ctx = avctx->priv_data;
376 
377  ff_free_vlc(&ctx->hq_ac_vlc);
378  ff_free_vlc(&ctx->hqa_cbp_vlc);
379 
380  return 0;
381 }
382 
384  .name = "hq_hqa",
385  .long_name = NULL_IF_CONFIG_SMALL("Canopus HQ/HQA"),
386  .type = AVMEDIA_TYPE_VIDEO,
387  .id = AV_CODEC_ID_HQ_HQA,
388  .priv_data_size = sizeof(HQContext),
391  .close = hq_hqa_decode_close,
392  .capabilities = AV_CODEC_CAP_DR1,
393  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
395 };
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: internal.h:48
int plane
Definition: avisynth_c.h:291
const char const char void * val
Definition: avisynth_c.h:634
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
This structure describes decoded (raw) audio or video data.
Definition: frame.h:171
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
int width
Definition: hq_hqa.h:47
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:1706
static int hq_decode_frame(HQContext *ctx, AVFrame *pic, int prof_num, size_t data_size)
Definition: hq_hqa.c:115
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:261
int16_t block[12][64]
Definition: hq_hqa.h:42
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Check that the provided frame dimensions are valid and set them on the codec context.
Definition: utils.c:216
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
#define avpriv_request_sample(...)
int ff_canopus_parse_info_tag(AVCodecContext *avctx, const uint8_t *src, size_t size)
Definition: canopus.c:30
int size
Definition: avcodec.h:1434
void(* idct_put)(uint8_t *dst, int stride, int16_t *block)
Definition: hq_hqadsp.h:33
static int hq_hqa_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: hq_hqa.c:302
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1732
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:133
static av_cold int hq_hqa_decode_init(AVCodecContext *avctx)
Definition: hq_hqa.c:363
static int hq_decode_mb(HQContext *c, AVFrame *pic, GetBitContext *gb, int x, int y)
Definition: hq_hqa.c:92
const int32_t *const ff_hq_quants[16][2][4]
Definition: hq_hqadata.c:1126
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:3013
AVCodec.
Definition: avcodec.h:3482
AVCodec ff_hq_hqa_decoder
Definition: hq_hqa.c:383
static int get_sbits(GetBitContext *s, int n)
Definition: get_bits.h:246
const HQProfile ff_hq_profile[NUM_HQ_PROFILES]
Definition: hq_hqadata.c:8343
const uint8_t * perm_tab
Definition: hq_hqa.h:46
Macro definitions for various function/variable attributes.
#define FFALIGN(x, a)
Definition: common.h:97
VLC hq_ac_vlc
Definition: hq_hqa.h:40
HQ/HQA variant of AAN IDCT It differs from the standard AAN IDCT in precision and in the second stage...
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: internal.h:40
uint8_t
#define av_cold
Definition: attributes.h:74
uint8_t * data
Definition: avcodec.h:1433
const uint8_t * buffer
Definition: bytestream.h:34
uint32_t tag
Definition: movenc.c:1339
static int hqa_decode_slice(HQContext *ctx, AVFrame *pic, GetBitContext *gb, int quant, int slice_no, int w, int h)
Definition: hq_hqa.c:220
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
#define av_log(a,...)
int flag
Definition: checkasm.c:76
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:594
planar YUV 4:2:2 24bpp, (1 Cr & Cb sample per 2x1 Y & A samples)
Definition: pixfmt.h:285
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
int profile
Definition: mxfenc.c:1820
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:164
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:178
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:154
const char * name
Name of the codec implementation.
Definition: avcodec.h:3489
int num_slices
Definition: hq_hqa.h:48
int tab_h
Definition: hq_hqa.h:49
Libavcodec external API header.
VLC hqa_cbp_vlc
Definition: hq_hqa.h:41
int ff_hq_init_vlcs(HQContext *c)
Definition: hq_hqadata.c:8368
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:71
static int hqa_decode_mb(HQContext *c, AVFrame *pic, int qgroup, GetBitContext *gb, int x, int y)
Definition: hq_hqa.c:177
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:242
float y
int width
picture width / height.
Definition: avcodec.h:1691
static int hqa_decode_frame(HQContext *ctx, AVFrame *pic, size_t data_size)
Definition: hq_hqa.c:241
int32_t
#define NUM_HQ_PROFILES
Definition: hq_hqa.h:32
perm
Definition: f_perms.c:74
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:561
GetByteContext gbc
Definition: hq_hqa.h:38
AVS_Value src
Definition: avisynth_c.h:482
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:199
main external API structure.
Definition: avcodec.h:1512
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:1048
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:305
BYTE int const BYTE int int int height
Definition: avisynth_c.h:676
const int16_t ff_hq_ac_syms[NUM_HQ_AC_ENTRIES]
Definition: hq_hqadata.c:1342
int coded_height
Definition: avcodec.h:1706
int tab_w
Definition: hq_hqa.h:49
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:415
const uint8_t ff_zigzag_direct[64]
Definition: mathtables.c:98
static void put_blocks(HQContext *c, AVFrame *pic, int plane, int x, int y, int ilace, int16_t *block0, int16_t *block1)
Definition: hq_hqa.c:49
const uint8_t * quant
static int16_t block1[64]
Definition: dct-test.c:111
#define NUM_HQ_QUANTS
Definition: hq_hqa.h:33
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:182
static int decode(AVCodecContext *avctx, void *data, int *got_sub, AVPacket *avpkt)
Definition: ccaption_dec.c:521
common internal api header.
if(ret< 0)
Definition: vf_mcdeint.c:280
static double c[64]
static int hq_decode_block(HQContext *c, GetBitContext *gb, int16_t block[64], int qsel, int is_chroma, int is_hqa)
Definition: hq_hqa.c:61
void * priv_data
Definition: avcodec.h:1554
static av_cold int hq_hqa_decode_close(AVCodecContext *avctx)
Definition: hq_hqa.c:373
AVCodecContext * avctx
Definition: hq_hqa.h:36
HQDSPContext hqhqadsp
Definition: hq_hqa.h:37
VLC_TYPE(* table)[2]
code, bits
Definition: get_bits.h:66
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:237
av_cold void ff_hqdsp_init(HQDSPContext *c)
Definition: hq_hqadsp.c:127
int height
Definition: hq_hqa.h:47
#define MKTAG(a, b, c, d)
Definition: common.h:341
This structure stores compressed data.
Definition: avcodec.h:1410
void ff_free_vlc(VLC *vlc)
Definition: bitstream.c:364
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:857
const uint8_t ff_hq_ac_skips[NUM_HQ_AC_ENTRIES]
Definition: hq_hqadata.c:1292
static int width
static int16_t block[64]
Definition: dct-test.c:110