FFmpeg
Loading...
Searching...
No Matches
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
25#include "libavutil/thread.h"
26
27#include "avcodec.h"
28#include "bytestream.h"
29#include "canopus.h"
30#include "codec_internal.h"
31#include "decode.h"
32#include "get_bits.h"
33#include "hq_common.h"
34#include "hq_hqadata.h"
35#include "hq_hqadsp.h"
36#include "vlc.h"
37
38/* HQ/HQA slices are a set of macroblocks belonging to a frame, and
39 * they usually form a pseudorandom pattern (probably because it is
40 * nicer to display on partial decode).
41 *
42 * For HQA it just happens that each slice is on every 8th macroblock,
43 * but they can be on any frame width like
44 * X.......X.
45 * ......X...
46 * ....X.....
47 * ..X.......
48 * etc.
49 *
50 * The original decoder has special handling for edge macroblocks,
51 * while lavc simply aligns coded_width and coded_height.
52 */
53
60
61static const int32_t *hq_quants[NUM_HQ_QUANTS][2][4];
62
64
65static inline void put_blocks(HQContext *c, AVFrame *pic,
66 int plane, int x, int y, int ilace,
67 int16_t *block0, int16_t *block1)
68{
69 uint8_t *p = pic->data[plane] + x;
70
71 c->hqhqadsp.idct_put(p + y * pic->linesize[plane],
72 pic->linesize[plane] << ilace, block0);
73 c->hqhqadsp.idct_put(p + (y + (ilace ? 1 : 8)) * pic->linesize[plane],
74 pic->linesize[plane] << ilace, block1);
75}
76
77static int hq_decode_block(HQContext *c, GetBitContext *gb, int16_t block[64],
78 int qsel, int is_chroma, int is_hqa)
79{
80 const int32_t *q;
81
82 if (!is_hqa) {
83 block[0] = get_sbits(gb, 9) * 64;
84 q = hq_quants[qsel][is_chroma][get_bits(gb, 2)];
85 } else {
86 q = hq_quants[qsel][is_chroma][get_bits(gb, 2)];
87 block[0] = get_sbits(gb, 9) * 64;
88 }
89
90 OPEN_READER(re, gb);
91 for (int pos = 0;;) {
92 int level, run;
93 UPDATE_CACHE(re, gb);
94 GET_RL_VLC(level, run, re, gb, hq_ac_rvlc, 9, 2, 0);
95 if (run == HQ_AC_INVALID_RUN) {
96 CLOSE_READER(re, gb);
98 }
99
100 pos += run;
101 if (pos >= 64)
102 break;
103 block[ff_zigzag_direct[pos]] = (int)(level * (unsigned)q[pos]) >> 12;
104 }
105 CLOSE_READER(re, gb);
106
107 return 0;
108}
109
110static int hq_decode_mb(HQContext *c, AVFrame *pic,
111 GetBitContext *gb, int x, int y)
112{
113 int qgroup, flag;
114 int i, ret;
115
116 memset(c->block, 0, 8 * sizeof(c->block[0]));
117
118 qgroup = get_bits(gb, 4);
119 flag = get_bits1(gb);
120
121 for (i = 0; i < 8; i++) {
122 ret = hq_decode_block(c, gb, c->block[i], qgroup, i >= 4, 0);
123 if (ret < 0)
124 return ret;
125 }
126
127 put_blocks(c, pic, 0, x, y, flag, c->block[0], c->block[2]);
128 put_blocks(c, pic, 0, x + 8, y, flag, c->block[1], c->block[3]);
129 put_blocks(c, pic, 2, x >> 1, y, flag, c->block[4], c->block[5]);
130 put_blocks(c, pic, 1, x >> 1, y, flag, c->block[6], c->block[7]);
131
132 return 0;
133}
134
136 int prof_num, size_t data_size)
137{
138 const HQProfile *profile;
139 GetBitContext gb;
140 const uint8_t *src = gbc->buffer;
141 uint32_t slice_off[21];
142 int slice, start_off, next_off, i, ret;
143
144 if ((unsigned)prof_num >= NUM_HQ_PROFILES) {
145 profile = &hq_profile[0];
146 avpriv_request_sample(ctx->avctx, "HQ Profile %d", prof_num);
147 } else {
148 profile = &hq_profile[prof_num];
149 av_log(ctx->avctx, AV_LOG_VERBOSE, "HQ Profile %d\n", prof_num);
150 }
151
152 if (bytestream2_get_bytes_left(gbc) < 3 * (profile->num_slices + 1))
153 return AVERROR_INVALIDDATA;
154
155 ctx->avctx->coded_width = FFALIGN(profile->width, 16);
156 ctx->avctx->coded_height = FFALIGN(profile->height, 16);
157 ctx->avctx->width = profile->width;
158 ctx->avctx->height = profile->height;
159 ctx->avctx->bits_per_raw_sample = 8;
160 ctx->avctx->pix_fmt = AV_PIX_FMT_YUV422P;
161
162 ret = ff_get_buffer(ctx->avctx, pic, 0);
163 if (ret < 0)
164 return ret;
165
166 /* Offsets are stored from CUV position, so adjust them accordingly. */
167 for (i = 0; i < profile->num_slices + 1; i++)
168 slice_off[i] = bytestream2_get_be24u(gbc) - 4;
169
170 next_off = 0;
171 const uint8_t *perm = hq_perms + profile->tab_offset;
172 for (slice = 0; slice < profile->num_slices; slice++) {
173 start_off = next_off;
174 next_off = profile->tab_h * (slice + 1) / profile->num_slices;
175
176 if (slice_off[slice] < (profile->num_slices + 1) * 3 ||
177 slice_off[slice] >= slice_off[slice + 1] ||
178 slice_off[slice + 1] > data_size) {
179 av_log(ctx->avctx, AV_LOG_ERROR,
180 "Invalid slice size %zu.\n", data_size);
181 break;
182 }
183 init_get_bits(&gb, src + slice_off[slice],
184 (slice_off[slice + 1] - slice_off[slice]) * 8);
185
186 for (i = 0; i < (next_off - start_off) * profile->tab_w; i++) {
187 ret = hq_decode_mb(ctx, pic, &gb, perm[0] * 16, perm[1] * 16);
188 if (ret < 0) {
189 av_log(ctx->avctx, AV_LOG_ERROR,
190 "Error decoding macroblock %d at slice %d.\n", i, slice);
191 return ret;
192 }
193 perm += 2;
194 }
195 }
196
197 return 0;
198}
199
200static int hqa_decode_mb(HQContext *c, AVFrame *pic, int qgroup,
201 GetBitContext *gb, int x, int y)
202{
203 int flag = 0;
204 int i, ret;
205
206 if (get_bits_left(gb) < 1)
207 return AVERROR_INVALIDDATA;
208
209 memset(c->block, 0, 12 * sizeof(c->block[0]));
210 for (i = 0; i < 12; i++)
211 c->block[i][0] = -128 * (1 << 6);
212
213 int cbp = get_vlc2(gb, ff_hq_cbp_vlc, HQ_CBP_VLC_BITS, 1);
214 if (cbp) {
215 flag = get_bits1(gb);
216
217 if (cbp & 0x3)
218 cbp |= 0x500;
219 if (cbp & 0xC)
220 cbp |= 0xA00;
221 for (i = 0; i < 12; i++) {
222 if (!(cbp & (1 << i)))
223 continue;
224 ret = hq_decode_block(c, gb, c->block[i], qgroup, i >= 8, 1);
225 if (ret < 0)
226 return ret;
227 }
228 }
229
230 put_blocks(c, pic, 3, x, y, flag, c->block[ 0], c->block[ 2]);
231 put_blocks(c, pic, 3, x + 8, y, flag, c->block[ 1], c->block[ 3]);
232 put_blocks(c, pic, 0, x, y, flag, c->block[ 4], c->block[ 6]);
233 put_blocks(c, pic, 0, x + 8, y, flag, c->block[ 5], c->block[ 7]);
234 put_blocks(c, pic, 2, x >> 1, y, flag, c->block[ 8], c->block[ 9]);
235 put_blocks(c, pic, 1, x >> 1, y, flag, c->block[10], c->block[11]);
236
237 return 0;
238}
239
241 int quant, int slice_no, int w, int h)
242{
243 int i, j, off;
244 int ret;
245
246 for (i = 0; i < h; i += 16) {
247 off = (slice_no * 16 + i * 3) & 0x70;
248 for (j = off; j < w; j += 128) {
249 ret = hqa_decode_mb(ctx, pic, quant, gb, j, i);
250 if (ret < 0) {
251 av_log(ctx->avctx, AV_LOG_ERROR,
252 "Error decoding macroblock at %dx%d.\n", i, j);
253 return ret;
254 }
255 }
256 }
257
258 return 0;
259}
260
261static int hqa_decode_frame(HQContext *ctx, AVFrame *pic, GetByteContext *gbc, size_t data_size)
262{
263 GetBitContext gb;
264 const int num_slices = 8;
265 uint32_t slice_off[9];
266 int i, slice, ret;
267 const uint8_t *src = gbc->buffer;
268
269 if (bytestream2_get_bytes_left(gbc) < 8 + 4*(num_slices + 1))
270 return AVERROR_INVALIDDATA;
271
272 int width = bytestream2_get_be16u(gbc);
273 int height = bytestream2_get_be16u(gbc);
274
275 ret = ff_set_dimensions(ctx->avctx, width, height);
276 if (ret < 0)
277 return ret;
278
279 ctx->avctx->coded_width = FFALIGN(width, 16);
280 ctx->avctx->coded_height = FFALIGN(height, 16);
281 ctx->avctx->bits_per_raw_sample = 8;
282 ctx->avctx->pix_fmt = AV_PIX_FMT_YUVA422P;
283
284 av_log(ctx->avctx, AV_LOG_VERBOSE, "HQA Profile\n");
285
286 int quant = bytestream2_get_byteu(gbc);
287 bytestream2_skipu(gbc, 3);
288 if (quant >= NUM_HQ_QUANTS) {
289 av_log(ctx->avctx, AV_LOG_ERROR,
290 "Invalid quantization matrix %d.\n", quant);
291 return AVERROR_INVALIDDATA;
292 }
293
294 ret = ff_get_buffer(ctx->avctx, pic, 0);
295 if (ret < 0)
296 return ret;
297
298 /* Offsets are stored from HQA1 position, so adjust them accordingly. */
299 for (i = 0; i < num_slices + 1; i++)
300 slice_off[i] = bytestream2_get_be32u(gbc) - 4;
301
302 for (slice = 0; slice < num_slices; slice++) {
303 if (slice_off[slice] < (num_slices + 1) * 3 ||
304 slice_off[slice] >= slice_off[slice + 1] ||
305 slice_off[slice + 1] > data_size) {
306 av_log(ctx->avctx, AV_LOG_ERROR,
307 "Invalid slice size %zu.\n", data_size);
308 break;
309 }
310 init_get_bits(&gb, src + slice_off[slice],
311 (slice_off[slice + 1] - slice_off[slice]) * 8);
312
313 ret = hqa_decode_slice(ctx, pic, &gb, quant, slice, width, height);
314 if (ret < 0)
315 return ret;
316 }
317
318 return 0;
319}
320
322 int *got_frame, AVPacket *avpkt)
323{
324 HQContext *ctx = avctx->priv_data;
325 GetByteContext gbc0, *const gbc = &gbc0;
326 unsigned int data_size;
327 int ret;
328
329 if (avpkt->size < 4 + 4) {
330 av_log(avctx, AV_LOG_ERROR, "Frame is too small (%d).\n", avpkt->size);
331 return AVERROR_INVALIDDATA;
332 }
333 bytestream2_init(gbc, avpkt->data, avpkt->size);
334
335 uint32_t info_tag = bytestream2_peek_le32u(gbc);
336 if (info_tag == MKTAG('I', 'N', 'F', 'O')) {
337 bytestream2_skipu(gbc, 4);
338 int info_size = bytestream2_get_le32u(gbc);
339 if (info_size < 0 || bytestream2_get_bytes_left(gbc) < info_size) {
340 av_log(avctx, AV_LOG_ERROR, "Invalid INFO size (%d).\n", info_size);
341 return AVERROR_INVALIDDATA;
342 }
343 ff_canopus_parse_info_tag(avctx, gbc->buffer, info_size);
344
345 bytestream2_skipu(gbc, info_size);
346 }
347
348 data_size = bytestream2_get_bytes_left(gbc);
349 if (data_size < 4) {
350 av_log(avctx, AV_LOG_ERROR, "Frame is too small (%d).\n", data_size);
351 return AVERROR_INVALIDDATA;
352 }
353
354 /* HQ defines dimensions and number of slices, and thus slice traversal
355 * order. HQA has no size constraint and a fixed number of slices, so it
356 * needs a separate scheme for it. */
357 unsigned tag = bytestream2_get_le32u(gbc);
358 if ((tag & 0x00FFFFFF) == (MKTAG('U', 'V', 'C', ' ') & 0x00FFFFFF)) {
359 ret = hq_decode_frame(ctx, pic, gbc, tag >> 24, data_size);
360 } else if (tag == MKTAG('H', 'Q', 'A', '1')) {
361 ret = hqa_decode_frame(ctx, pic, gbc, data_size);
362 } else {
363 av_log(avctx, AV_LOG_ERROR, "Not a HQ/HQA frame.\n");
364 return AVERROR_INVALIDDATA;
365 }
366 if (ret < 0) {
367 av_log(avctx, AV_LOG_ERROR, "Error decoding frame.\n");
368 return ret;
369 }
370
371 *got_frame = 1;
372
373 return avpkt->size;
374}
375
376static av_cold void hq_init_static(void)
377{
379 hq_ac_lens, 1, hq_ac_sym, 2, 2, 0, 0);
380
381 for (size_t i = 0; i < FF_ARRAY_ELEMS(hq_ac_rvlc); ++i) {
382 int len = hq_ac_rvlc[i].len;
383 int sym = (int16_t)hq_ac_rvlc[i].sym, level = sym;
384
385 // The invalid code has been remapped to HQ_AC_INVALID_RUN,
386 // so the VLC is complete.
387 av_assert1(len != 0);
388
389 if (len > 0) {
390 level = sym >> 7;
391 hq_ac_rvlc[i].run = sym & 0x7F;
392 }
393 hq_ac_rvlc[i].len8 = len;
394 hq_ac_rvlc[i].level = level;
395 }
396
397 for (size_t i = 0; i < FF_ARRAY_ELEMS(hq_quants); ++i) {
398 for (size_t j = 0; j < FF_ARRAY_ELEMS(hq_quants[0]); ++j) {
399 for (size_t k = 0; k < FF_ARRAY_ELEMS(hq_quants[0][0]); ++k)
400 hq_quants[i][j][k] = qmats[hq_quant_map[i][j][k]];
401 }
402 }
403}
404
406{
407 static AVOnce init_static_once = AV_ONCE_INIT;
408 HQContext *ctx = avctx->priv_data;
409 ctx->avctx = avctx;
410
411 ff_hqdsp_init(&ctx->hqhqadsp);
412
413 ff_thread_once(&init_static_once, hq_init_static);
414
415 return 0;
416}
417
419 .p.name = "hq_hqa",
420 CODEC_LONG_NAME("Canopus HQ/HQA"),
421 .p.type = AVMEDIA_TYPE_VIDEO,
422 .p.id = AV_CODEC_ID_HQ_HQA,
423 .priv_data_size = sizeof(HQContext),
426 .p.capabilities = AV_CODEC_CAP_DR1,
427};
const FFCodec ff_hq_hqa_decoder
Definition hq_hqa.c:418
int32_t
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition avassert.h:58
Libavcodec external API header.
static av_always_inline void bytestream2_skipu(GetByteContext *g, unsigned int size)
Definition bytestream.h:174
static av_always_inline int bytestream2_get_bytes_left(const GetByteContext *g)
Definition bytestream.h:158
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition bytestream.h:137
int ff_canopus_parse_info_tag(AVCodecContext *avctx, const uint8_t *src, size_t size)
Definition canopus.c:30
#define flag(name)
Definition cbs_h264.c:60
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
static int16_t block[64]
Definition dct.c:125
static int16_t block1[64]
Definition dct.c:126
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition decode.c:1777
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Definition utils.c:91
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
perm
Definition f_perms.c:75
bitstream reader API header.
static av_always_inline int get_vlc2(GetBitContext *s, const VLCElem *table, int bits, int max_depth)
Parse a vlc code.
Definition get_bits.h:645
static int get_sbits(GetBitContext *s, int n)
Definition get_bits.h:322
#define CLOSE_READER(name, gb)
Definition get_bits.h:189
static int get_bits_left(GetBitContext *gb)
Definition get_bits.h:688
static unsigned int get_bits1(GetBitContext *s)
Definition get_bits.h:391
#define OPEN_READER(name, gb)
Definition get_bits.h:182
#define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update)
Definition get_bits.h:602
#define UPDATE_CACHE(name, gb)
Definition get_bits.h:213
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition get_bits.h:337
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition get_bits.h:517
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
@ AV_CODEC_ID_HQ_HQA
Definition codec_id.h:237
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AV_LOG_VERBOSE
Detailed information.
Definition log.h:226
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
const VLCElem ff_hq_cbp_vlc[1<< HQ_CBP_VLC_BITS]
Definition hq_common.c:27
#define HQ_CBP_VLC_BITS
Definition hq_common.h:25
static int hq_decode_mb(HQContext *c, AVFrame *pic, GetBitContext *gb, int x, int y)
Definition hq_hqa.c:110
static const int32_t * hq_quants[NUM_HQ_QUANTS][2][4]
Definition hq_hqa.c:61
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:77
static av_cold void hq_init_static(void)
Definition hq_hqa.c:376
static int hqa_decode_frame(HQContext *ctx, AVFrame *pic, GetByteContext *gbc, size_t data_size)
Definition hq_hqa.c:261
static int hq_decode_frame(HQContext *ctx, AVFrame *pic, GetByteContext *gbc, int prof_num, size_t data_size)
Definition hq_hqa.c:135
static RL_VLC_ELEM hq_ac_rvlc[1184]
Definition hq_hqa.c:63
static int hq_hqa_decode_frame(AVCodecContext *avctx, AVFrame *pic, int *got_frame, AVPacket *avpkt)
Definition hq_hqa.c:321
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:65
static int hqa_decode_mb(HQContext *c, AVFrame *pic, int qgroup, GetBitContext *gb, int x, int y)
Definition hq_hqa.c:200
static int hqa_decode_slice(HQContext *ctx, AVFrame *pic, GetBitContext *gb, int quant, int slice_no, int w, int h)
Definition hq_hqa.c:240
static av_cold int hq_hqa_decode_init(AVCodecContext *avctx)
Definition hq_hqa.c:405
static const uint8_t hq_quant_map[NUM_HQ_QUANTS][2][4]
static const uint8_t hq_ac_lens[NUM_HQ_AC_ENTRIES]
#define NUM_HQ_AC_ENTRIES
Definition hq_hqadata.h:27
#define HQ_AC_INVALID_RUN
static const HQProfile hq_profile[NUM_HQ_PROFILES]
static const int32_t qmats[NUM_QMATS][MAT_SIZE]
Definition hq_hqadata.h:115
#define NUM_HQ_QUANTS
Definition hq_hqadata.h:29
#define NUM_HQ_PROFILES
Definition hq_hqadata.h:28
static const uint8_t hq_perms[]
static const int16_t hq_ac_sym[NUM_HQ_AC_ENTRIES]
av_cold void ff_hqdsp_init(HQDSPContext *c)
Definition hq_hqadsp.c:127
HQ/HQA variant of AAN IDCT It differs from the standard AAN IDCT in precision and in the second stage...
Macro definitions for various function/variable attributes.
#define av_cold
Definition attributes.h:117
#define AVOnce
Definition thread.h:202
static int ff_thread_once(char *control, void(*routine)(void))
Definition thread.h:205
#define AV_ONCE_INIT
Definition thread.h:203
uint8_t w
Definition llvidencdsp.c:39
#define MKTAG(a, b, c, d)
Definition macros.h:55
#define FFALIGN(x, a)
Definition macros.h:78
const uint8_t ff_zigzag_direct[64]
Definition mathtables.c:137
#define DECLARE_ALIGNED(n, t, v)
Declare a variable that is aligned in memory.
uint32_t tag
Definition movenc.c:2073
int profile
Definition mxfenc.c:2299
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition pixfmt.h:77
@ AV_PIX_FMT_YUVA422P
planar YUV 4:2:2 24bpp, (1 Cr & Cb sample per 2x1 Y & A samples)
Definition pixfmt.h:173
#define FF_ARRAY_ELEMS(a)
unsigned int pos
Definition spdifenc.c:431
main external API structure.
Definition avcodec.h:443
void * priv_data
Definition avcodec.h:470
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition frame.h:493
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition frame.h:517
This structure stores compressed data.
Definition packet.h:580
int size
Definition packet.h:604
uint8_t * data
Definition packet.h:603
const uint8_t * buffer
Definition bytestream.h:34
AVCodecContext * avctx
Definition hq_hqa.c:55
HQDSPContext hqhqadsp
Definition hq_hqa.c:56
int16_t block[12][64]
Definition hq_hqa.c:58
uint8_t run
Definition svq3.c:207
uint8_t level
Definition svq3.c:208
#define avpriv_request_sample(...)
#define av_log(a,...)
#define src
Definition vp8dsp.c:248
static AVFormatContext * ctx
Definition movenc.c:49
#define height
Definition dsp.h:89
#define width
Definition dsp.h:89
#define VLC_INIT_STATIC_TABLE_FROM_LENGTHS(vlc_table, nb_bits, nb_codes, lens, lens_wrap, syms, syms_wrap, syms_size, offset, flags)
Definition vlc.h:288
VLCElem RL_VLC_ELEM
Definition vlc.h:48
static const uint8_t quant[64]
Definition vmixdec.c:71
int len
static double c[64]