]> git.sesse.net Git - ffmpeg/blob - libavcodec/hq_hqa.c
Merge commit 'eaa2d123f0a643664721593d248ece6bcd85f1e6'
[ffmpeg] / libavcodec / hq_hqa.c
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) << 6;
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) << 6;
75     }
76
77     for (;;) {
78         val  = get_vlc2(gb, c->hq_ac_vlc.table, 9, 2);
79         pos += ff_hq_ac_skips[val];
80         if (pos >= 64)
81             break;
82         block[ff_zigzag_direct[pos]] = (ff_hq_ac_syms[val] * q[pos]) >> 12;
83         pos++;
84     }
85
86     return 0;
87 }
88
89 static int hq_decode_mb(HQContext *c, AVFrame *pic,
90                         GetBitContext *gb, int x, int y)
91 {
92     int qgroup, flag;
93     int i, ret;
94
95     qgroup = get_bits(gb, 4);
96     flag = get_bits1(gb);
97
98     for (i = 0; i < 8; i++) {
99         ret = hq_decode_block(c, gb, c->block[i], qgroup, i >= 4, 0);
100         if (ret < 0)
101             return ret;
102     }
103
104     put_blocks(c, pic, 0, x,      y, flag, c->block[0], c->block[2]);
105     put_blocks(c, pic, 0, x + 8,  y, flag, c->block[1], c->block[3]);
106     put_blocks(c, pic, 2, x >> 1, y, flag, c->block[4], c->block[5]);
107     put_blocks(c, pic, 1, x >> 1, y, flag, c->block[6], c->block[7]);
108
109     return 0;
110 }
111
112 static int hq_decode_frame(HQContext *ctx, AVFrame *pic,
113                            int prof_num, size_t data_size)
114 {
115     const HQProfile *profile;
116     GetBitContext gb;
117     const uint8_t *perm, *src = ctx->gbc.buffer;
118     uint32_t slice_off[21];
119     int slice, start_off, next_off, i, ret;
120
121     if (prof_num >= NUM_HQ_PROFILES) {
122         profile = &ff_hq_profile[0];
123         avpriv_request_sample(ctx->avctx, "HQ Profile %d", prof_num);
124     } else {
125         profile = &ff_hq_profile[prof_num];
126         av_log(ctx->avctx, AV_LOG_VERBOSE, "HQ Profile %d\n", prof_num);
127     }
128
129     ctx->avctx->coded_width         = FFALIGN(profile->width,  16);
130     ctx->avctx->coded_height        = FFALIGN(profile->height, 16);
131     ctx->avctx->width               = profile->width;
132     ctx->avctx->height              = profile->height;
133     ctx->avctx->bits_per_raw_sample = 8;
134     ctx->avctx->pix_fmt             = AV_PIX_FMT_YUV422P;
135
136     ret = ff_get_buffer(ctx->avctx, pic, 0);
137     if (ret < 0) {
138         av_log(ctx->avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
139         return ret;
140     }
141
142     /* Offsets are stored from CUV position, so adjust them accordingly. */
143     for (i = 0; i < profile->num_slices + 1; i++)
144         slice_off[i] = bytestream2_get_be24(&ctx->gbc) - 4;
145
146     next_off = 0;
147     for (slice = 0; slice < profile->num_slices; slice++) {
148         start_off = next_off;
149         next_off  = profile->tab_h * (slice + 1) / profile->num_slices;
150         perm = profile->perm_tab + start_off * profile->tab_w * 2;
151
152         if (slice_off[slice] < (profile->num_slices + 1) * 3 ||
153             slice_off[slice] >= slice_off[slice + 1] ||
154             slice_off[slice + 1] > data_size) {
155             av_log(ctx->avctx, AV_LOG_ERROR,
156                    "Invalid slice size %zu.\n", data_size);
157             break;
158         }
159         init_get_bits(&gb, src + slice_off[slice],
160                       (slice_off[slice + 1] - slice_off[slice]) * 8);
161
162         for (i = 0; i < (next_off - start_off) * profile->tab_w; i++) {
163             ret = hq_decode_mb(ctx, pic, &gb, perm[0] * 16, perm[1] * 16);
164             if (ret < 0) {
165                 av_log(ctx->avctx, AV_LOG_ERROR,
166                        "Error decoding macroblock %d at slice %d.\n", i, slice);
167                 return ret;
168             }
169             perm += 2;
170         }
171     }
172
173     return 0;
174 }
175
176 static int hqa_decode_mb(HQContext *c, AVFrame *pic, int qgroup,
177                          GetBitContext *gb, int x, int y)
178 {
179     int flag = 0;
180     int i, ret, cbp;
181
182     cbp = get_vlc2(gb, c->hqa_cbp_vlc.table, 5, 1);
183
184     for (i = 0; i < 12; i++)
185         memset(c->block[i], 0, sizeof(*c->block));
186     for (i = 0; i < 12; i++)
187         c->block[i][0] = -128 * (1 << 6);
188
189     if (cbp) {
190         flag = get_bits1(gb);
191
192         cbp |= cbp << 4;
193         if (cbp & 0x3)
194             cbp |= 0x500;
195         if (cbp & 0xC)
196             cbp |= 0xA00;
197         for (i = 0; i < 12; i++) {
198             if (!(cbp & (1 << i)))
199                 continue;
200             ret = hq_decode_block(c, gb, c->block[i], qgroup, i >= 8, 1);
201             if (ret < 0)
202                 return ret;
203         }
204     }
205
206     put_blocks(c, pic, 3, x,      y, flag, c->block[ 0], c->block[ 2]);
207     put_blocks(c, pic, 3, x + 8,  y, flag, c->block[ 1], c->block[ 3]);
208     put_blocks(c, pic, 0, x,      y, flag, c->block[ 4], c->block[ 6]);
209     put_blocks(c, pic, 0, x + 8,  y, flag, c->block[ 5], c->block[ 7]);
210     put_blocks(c, pic, 2, x >> 1, y, flag, c->block[ 8], c->block[ 9]);
211     put_blocks(c, pic, 1, x >> 1, y, flag, c->block[10], c->block[11]);
212
213     return 0;
214 }
215
216 static int hqa_decode_slice(HQContext *ctx, AVFrame *pic, GetBitContext *gb,
217                             int quant, int slice_no, int w, int h)
218 {
219     int i, j, off;
220     int ret;
221
222     for (i = 0; i < h; i += 16) {
223         off = (slice_no * 16 + i * 3) & 0x70;
224         for (j = off; j < w; j += 128) {
225             ret = hqa_decode_mb(ctx, pic, quant, gb, j, i);
226             if (ret < 0) {
227                 av_log(ctx->avctx, AV_LOG_ERROR,
228                        "Error decoding macroblock at %dx%d.\n", i, j);
229                 return ret;
230             }
231         }
232     }
233
234     return 0;
235 }
236
237 static int hqa_decode_frame(HQContext *ctx, AVFrame *pic, size_t data_size)
238 {
239     GetBitContext gb;
240     const int num_slices = 8;
241     uint32_t slice_off[9];
242     int i, slice, ret;
243     int width, height, quant;
244     const uint8_t *src = ctx->gbc.buffer;
245
246     width  = bytestream2_get_be16(&ctx->gbc);
247     height = bytestream2_get_be16(&ctx->gbc);
248
249     ctx->avctx->coded_width         = FFALIGN(width,  16);
250     ctx->avctx->coded_height        = FFALIGN(height, 16);
251     ctx->avctx->width               = width;
252     ctx->avctx->height              = height;
253     ctx->avctx->bits_per_raw_sample = 8;
254     ctx->avctx->pix_fmt             = AV_PIX_FMT_YUVA422P;
255
256     av_log(ctx->avctx, AV_LOG_VERBOSE, "HQA Profile\n");
257
258     quant = bytestream2_get_byte(&ctx->gbc);
259     bytestream2_skip(&ctx->gbc, 3);
260     if (quant >= NUM_HQ_QUANTS) {
261         av_log(ctx->avctx, AV_LOG_ERROR,
262                "Invalid quantization matrix %d.\n", quant);
263         return AVERROR_INVALIDDATA;
264     }
265
266     ret = ff_get_buffer(ctx->avctx, pic, 0);
267     if (ret < 0) {
268         av_log(ctx->avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
269         return ret;
270     }
271
272     /* Offsets are stored from HQA1 position, so adjust them accordingly. */
273     for (i = 0; i < num_slices + 1; i++)
274         slice_off[i] = bytestream2_get_be32(&ctx->gbc) - 4;
275
276     for (slice = 0; slice < num_slices; slice++) {
277         if (slice_off[slice] < (num_slices + 1) * 3 ||
278             slice_off[slice] >= slice_off[slice + 1] ||
279             slice_off[slice + 1] > data_size) {
280             av_log(ctx->avctx, AV_LOG_ERROR,
281                    "Invalid slice size %zu.\n", data_size);
282             break;
283         }
284         init_get_bits(&gb, src + slice_off[slice],
285                       (slice_off[slice + 1] - slice_off[slice]) * 8);
286
287         ret = hqa_decode_slice(ctx, pic, &gb, quant, slice, width, height);
288         if (ret < 0)
289             return ret;
290     }
291
292     return 0;
293 }
294
295 static int hq_hqa_decode_frame(AVCodecContext *avctx, void *data,
296                                int *got_frame, AVPacket *avpkt)
297 {
298     HQContext *ctx = avctx->priv_data;
299     AVFrame *pic = data;
300     uint32_t info_tag;
301     unsigned int data_size;
302     int tag, ret;
303
304     bytestream2_init(&ctx->gbc, avpkt->data, avpkt->size);
305     if (bytestream2_get_bytes_left(&ctx->gbc) < 4 + 4) {
306         av_log(avctx, AV_LOG_ERROR, "Frame is too small (%d).\n", avpkt->size);
307         return AVERROR_INVALIDDATA;
308     }
309
310     info_tag = bytestream2_get_le32(&ctx->gbc);
311     if (info_tag == MKTAG('I', 'N', 'F', 'O')) {
312         int info_size = bytestream2_get_le32(&ctx->gbc);
313         if (bytestream2_get_bytes_left(&ctx->gbc) < info_size) {
314             av_log(avctx, AV_LOG_ERROR, "Invalid INFO size (%d).\n", info_size);
315             return AVERROR_INVALIDDATA;
316         }
317         ff_canopus_parse_info_tag(avctx, ctx->gbc.buffer, info_size);
318
319         bytestream2_skip(&ctx->gbc, info_size);
320     }
321
322     data_size = bytestream2_get_bytes_left(&ctx->gbc);
323     if (data_size < 4) {
324         av_log(avctx, AV_LOG_ERROR, "Frame is too small (%d).\n", data_size);
325         return AVERROR_INVALIDDATA;
326     }
327
328     /* HQ defines dimensions and number of slices, and thus slice traversal
329      * order. HQA has no size constraint and a fixed number of slices, so it
330      * needs a separate scheme for it. */
331     tag = bytestream2_get_le32(&ctx->gbc);
332     if ((tag & 0x00FFFFFF) == (MKTAG('U', 'V', 'C', ' ') & 0x00FFFFFF)) {
333         ret = hq_decode_frame(ctx, pic, tag >> 24, data_size);
334     } else if (tag == MKTAG('H', 'Q', 'A', '1')) {
335         ret = hqa_decode_frame(ctx, pic, data_size);
336     } else {
337         av_log(avctx, AV_LOG_ERROR, "Not a HQ/HQA frame.\n");
338         return AVERROR_INVALIDDATA;
339     }
340     if (ret < 0) {
341         av_log(avctx, AV_LOG_ERROR, "Error decoding frame.\n");
342         return ret;
343     }
344
345     pic->key_frame = 1;
346     pic->pict_type = AV_PICTURE_TYPE_I;
347
348     *got_frame = 1;
349
350     return avpkt->size;
351 }
352
353 static av_cold int hq_hqa_decode_init(AVCodecContext *avctx)
354 {
355     HQContext *ctx = avctx->priv_data;
356     ctx->avctx = avctx;
357
358     ff_hqdsp_init(&ctx->hqhqadsp);
359
360     return ff_hq_init_vlcs(ctx);
361 }
362
363 static av_cold int hq_hqa_decode_close(AVCodecContext *avctx)
364 {
365     HQContext *ctx = avctx->priv_data;
366
367     ff_free_vlc(&ctx->hq_ac_vlc);
368     ff_free_vlc(&ctx->hqa_cbp_vlc);
369
370     return 0;
371 }
372
373 AVCodec ff_hq_hqa_decoder = {
374     .name           = "hq_hqa",
375     .long_name      = NULL_IF_CONFIG_SMALL("Canopus HQ/HQA"),
376     .type           = AVMEDIA_TYPE_VIDEO,
377     .id             = AV_CODEC_ID_HQ_HQA,
378     .priv_data_size = sizeof(HQContext),
379     .init           = hq_hqa_decode_init,
380     .decode         = hq_hqa_decode_frame,
381     .close          = hq_hqa_decode_close,
382     .capabilities   = CODEC_CAP_DR1,
383     .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE |
384                       FF_CODEC_CAP_INIT_CLEANUP,
385 };